Merge zizzer.eecs.umich.edu:/bk/newmem
[gem5.git] / src / sim / syscall_emul.cc
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Steve Reinhardt
29 * Ali Saidi
30 */
31
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <string>
36 #include <iostream>
37
38 #include "sim/syscall_emul.hh"
39 #include "base/chunk_generator.hh"
40 #include "base/trace.hh"
41 #include "cpu/thread_context.hh"
42 #include "cpu/base.hh"
43 #include "mem/page_table.hh"
44 #include "sim/process.hh"
45
46 #include "sim/sim_exit.hh"
47
48 using namespace std;
49 using namespace TheISA;
50
51 void
52 SyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
53 {
54 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
55 curTick,tc->getCpuPtr()->name(), name,
56 tc->getSyscallArg(0),tc->getSyscallArg(1),
57 tc->getSyscallArg(2),tc->getSyscallArg(3));
58
59 SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
60
61 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
62 curTick,tc->getCpuPtr()->name(), name, retval.value());
63
64 if (!(flags & SyscallDesc::SuppressReturnValue))
65 tc->setSyscallReturn(retval);
66 }
67
68
69 SyscallReturn
70 unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71 ThreadContext *tc)
72 {
73 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
74
75 return 1;
76 }
77
78
79 SyscallReturn
80 ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
81 ThreadContext *tc)
82 {
83 warn("ignoring syscall %s(%d, %d, ...)", desc->name,
84 tc->getSyscallArg(0), tc->getSyscallArg(1));
85
86 return 0;
87 }
88
89
90 SyscallReturn
91 exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
92 ThreadContext *tc)
93 {
94 if (tc->exit()) {
95 exitSimLoop("target called exit()", tc->getSyscallArg(0) & 0xff);
96 }
97
98 return 1;
99 }
100
101
102 SyscallReturn
103 getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
104 {
105 return (int)VMPageSize;
106 }
107
108
109 SyscallReturn
110 obreakFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
111 {
112 Addr junk;
113
114 // change brk addr to first arg
115 Addr new_brk = tc->getSyscallArg(0);
116 if (new_brk != 0) {
117 for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
118 VMPageSize); !gen.done(); gen.next()) {
119 if (!p->pTable->translate(gen.addr(), junk))
120 p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
121 VMPageSize);
122 }
123 p->brk_point = new_brk;
124 }
125 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
126 return p->brk_point;
127 }
128
129
130 SyscallReturn
131 closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
132 {
133 int target_fd = tc->getSyscallArg(0);
134 int status = close(p->sim_fd(target_fd));
135 if (status >= 0)
136 p->free_fd(target_fd);
137 return status;
138 }
139
140
141 SyscallReturn
142 readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
143 {
144 int fd = p->sim_fd(tc->getSyscallArg(0));
145 int nbytes = tc->getSyscallArg(2);
146 BufferArg bufArg(tc->getSyscallArg(1), nbytes);
147
148 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
149
150 if (bytes_read != -1)
151 bufArg.copyOut(tc->getMemPort());
152
153 return bytes_read;
154 }
155
156 SyscallReturn
157 writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
158 {
159 int fd = p->sim_fd(tc->getSyscallArg(0));
160 int nbytes = tc->getSyscallArg(2);
161 BufferArg bufArg(tc->getSyscallArg(1), nbytes);
162
163 bufArg.copyIn(tc->getMemPort());
164
165 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
166
167 fsync(fd);
168
169 return bytes_written;
170 }
171
172
173 SyscallReturn
174 lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
175 {
176 int fd = p->sim_fd(tc->getSyscallArg(0));
177 uint64_t offs = tc->getSyscallArg(1);
178 int whence = tc->getSyscallArg(2);
179
180 off_t result = lseek(fd, offs, whence);
181
182 return (result == (off_t)-1) ? -errno : result;
183 }
184
185
186 SyscallReturn
187 _llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
188 {
189 int fd = p->sim_fd(tc->getSyscallArg(0));
190 uint64_t offset_high = tc->getSyscallArg(1);
191 uint32_t offset_low = tc->getSyscallArg(2);
192 Addr result_ptr = tc->getSyscallArg(3);
193 int whence = tc->getSyscallArg(4);
194
195 uint64_t offset = (offset_high << 32) | offset_low;
196
197 uint64_t result = lseek(fd, offset, whence);
198 result = TheISA::htog(result);
199
200 if (result == (off_t)-1) {
201 //The seek failed.
202 return -errno;
203 } else {
204 //The seek succeeded.
205 //Copy "result" to "result_ptr"
206 //XXX We'll assume that the size of loff_t is 64 bits on the
207 //target platform
208 BufferArg result_buf(result_ptr, sizeof(result));
209 memcpy(result_buf.bufferPtr(), &result, sizeof(result));
210 result_buf.copyOut(tc->getMemPort());
211 return 0;
212 }
213
214
215 return (result == (off_t)-1) ? -errno : result;
216 }
217
218
219 SyscallReturn
220 munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
221 {
222 // given that we don't really implement mmap, munmap is really easy
223 return 0;
224 }
225
226
227 const char *hostname = "m5.eecs.umich.edu";
228
229 SyscallReturn
230 gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
231 {
232 int name_len = tc->getSyscallArg(1);
233 BufferArg name(tc->getSyscallArg(0), name_len);
234
235 strncpy((char *)name.bufferPtr(), hostname, name_len);
236
237 name.copyOut(tc->getMemPort());
238
239 return 0;
240 }
241
242 SyscallReturn
243 unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
244 {
245 string path;
246
247 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
248 return (TheISA::IntReg)-EFAULT;
249
250 // Adjust path for current working directory
251 path = p->fullPath(path);
252
253 int result = unlink(path.c_str());
254 return (result == -1) ? -errno : result;
255 }
256
257 SyscallReturn
258 renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
259 {
260 string old_name;
261
262 if (!tc->getMemPort()->tryReadString(old_name, tc->getSyscallArg(0)))
263 return -EFAULT;
264
265 string new_name;
266
267 if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1)))
268 return -EFAULT;
269
270 // Adjust path for current working directory
271 old_name = p->fullPath(old_name);
272 new_name = p->fullPath(new_name);
273
274 int64_t result = rename(old_name.c_str(), new_name.c_str());
275 return (result == -1) ? -errno : result;
276 }
277
278 SyscallReturn
279 truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
280 {
281 string path;
282
283 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
284 return -EFAULT;
285
286 off_t length = tc->getSyscallArg(1);
287
288 // Adjust path for current working directory
289 path = p->fullPath(path);
290
291 int result = truncate(path.c_str(), length);
292 return (result == -1) ? -errno : result;
293 }
294
295 SyscallReturn
296 ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
297 {
298 int fd = process->sim_fd(tc->getSyscallArg(0));
299
300 if (fd < 0)
301 return -EBADF;
302
303 off_t length = tc->getSyscallArg(1);
304
305 int result = ftruncate(fd, length);
306 return (result == -1) ? -errno : result;
307 }
308
309 SyscallReturn
310 chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
311 {
312 string path;
313
314 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
315 return -EFAULT;
316
317 /* XXX endianess */
318 uint32_t owner = tc->getSyscallArg(1);
319 uid_t hostOwner = owner;
320 uint32_t group = tc->getSyscallArg(2);
321 gid_t hostGroup = group;
322
323 // Adjust path for current working directory
324 path = p->fullPath(path);
325
326 int result = chown(path.c_str(), hostOwner, hostGroup);
327 return (result == -1) ? -errno : result;
328 }
329
330 SyscallReturn
331 fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
332 {
333 int fd = process->sim_fd(tc->getSyscallArg(0));
334
335 if (fd < 0)
336 return -EBADF;
337
338 /* XXX endianess */
339 uint32_t owner = tc->getSyscallArg(1);
340 uid_t hostOwner = owner;
341 uint32_t group = tc->getSyscallArg(2);
342 gid_t hostGroup = group;
343
344 int result = fchown(fd, hostOwner, hostGroup);
345 return (result == -1) ? -errno : result;
346 }
347
348
349 SyscallReturn
350 dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
351 {
352 int fd = process->sim_fd(tc->getSyscallArg(0));
353
354 if (fd < 0)
355 return -EBADF;
356
357 int result = dup(fd);
358 return (result == -1) ? -errno : process->alloc_fd(result);
359 }
360
361
362 SyscallReturn
363 fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
364 ThreadContext *tc)
365 {
366 int fd = tc->getSyscallArg(0);
367
368 if (fd < 0 || process->sim_fd(fd) < 0)
369 return -EBADF;
370
371 int cmd = tc->getSyscallArg(1);
372 switch (cmd) {
373 case 0: // F_DUPFD
374 // if we really wanted to support this, we'd need to do it
375 // in the target fd space.
376 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
377 return -EMFILE;
378
379 case 1: // F_GETFD (get close-on-exec flag)
380 case 2: // F_SETFD (set close-on-exec flag)
381 return 0;
382
383 case 3: // F_GETFL (get file flags)
384 case 4: // F_SETFL (set file flags)
385 // not sure if this is totally valid, but we'll pass it through
386 // to the underlying OS
387 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
388 return fcntl(process->sim_fd(fd), cmd);
389 // return 0;
390
391 case 7: // F_GETLK (get lock)
392 case 8: // F_SETLK (set lock)
393 case 9: // F_SETLKW (set lock and wait)
394 // don't mess with file locking... just act like it's OK
395 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
396 return 0;
397
398 default:
399 warn("Unknown fcntl command %d\n", cmd);
400 return 0;
401 }
402 }
403
404 SyscallReturn
405 fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
406 ThreadContext *tc)
407 {
408 int fd = tc->getSyscallArg(0);
409
410 if (fd < 0 || process->sim_fd(fd) < 0)
411 return -EBADF;
412
413 int cmd = tc->getSyscallArg(1);
414 switch (cmd) {
415 case 33: //F_GETLK64
416 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
417 return -EMFILE;
418
419 case 34: // F_SETLK64
420 case 35: // F_SETLKW64
421 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
422 return -EMFILE;
423
424 default:
425 // not sure if this is totally valid, but we'll pass it through
426 // to the underlying OS
427 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
428 return fcntl(process->sim_fd(fd), cmd);
429 // return 0;
430 }
431 }
432
433 SyscallReturn
434 pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
435 ThreadContext *tc)
436 {
437 int fds[2], sim_fds[2];
438 int pipe_retval = pipe(fds);
439
440 if (pipe_retval < 0) {
441 // error
442 return pipe_retval;
443 }
444
445 sim_fds[0] = process->alloc_fd(fds[0]);
446 sim_fds[1] = process->alloc_fd(fds[1]);
447
448 // Alpha Linux convention for pipe() is that fd[0] is returned as
449 // the return value of the function, and fd[1] is returned in r20.
450 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
451 return sim_fds[0];
452 }
453
454
455 SyscallReturn
456 getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
457 ThreadContext *tc)
458 {
459 // Make up a PID. There's no interprocess communication in
460 // fake_syscall mode, so there's no way for a process to know it's
461 // not getting a unique value.
462
463 tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
464 return process->pid();
465 }
466
467
468 SyscallReturn
469 getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
470 ThreadContext *tc)
471 {
472 // Make up a UID and EUID... it shouldn't matter, and we want the
473 // simulation to be deterministic.
474
475 // EUID goes in r20.
476 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
477 return process->uid(); // UID
478 }
479
480
481 SyscallReturn
482 getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
483 ThreadContext *tc)
484 {
485 // Get current group ID. EGID goes in r20.
486 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
487 return process->gid();
488 }
489
490
491 SyscallReturn
492 setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
493 ThreadContext *tc)
494 {
495 // can't fathom why a benchmark would call this.
496 warn("Ignoring call to setuid(%d)\n", tc->getSyscallArg(0));
497 return 0;
498 }
499
500 SyscallReturn
501 getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
502 ThreadContext *tc)
503 {
504 // Make up a PID. There's no interprocess communication in
505 // fake_syscall mode, so there's no way for a process to know it's
506 // not getting a unique value.
507
508 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
509 return process->pid();
510 }
511
512 SyscallReturn
513 getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
514 ThreadContext *tc)
515 {
516 return process->ppid();
517 }
518
519 SyscallReturn
520 getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
521 ThreadContext *tc)
522 {
523 return process->uid(); // UID
524 }
525
526 SyscallReturn
527 geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
528 ThreadContext *tc)
529 {
530 return process->euid(); // UID
531 }
532
533 SyscallReturn
534 getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
535 ThreadContext *tc)
536 {
537 return process->gid();
538 }
539
540 SyscallReturn
541 getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
542 ThreadContext *tc)
543 {
544 return process->egid();
545 }
546
547