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#include "sim/system.hh"
46
47#include "sim/sim_exit.hh"
48
49using namespace std;
50using namespace TheISA;
51
52void
53SyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
54{
55 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
56 curTick,tc->getCpuPtr()->name(), name,
57 process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1),
58 process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 3));
59
60 SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
61
62 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
63 curTick,tc->getCpuPtr()->name(), name, retval.value());
64
65 if (!(flags & SyscallDesc::SuppressReturnValue))
66 process->setSyscallReturn(tc, retval);
67}
68
69
70SyscallReturn
71unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
72 ThreadContext *tc)
73{
74 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
75
76 return 1;
77}
78
79
80SyscallReturn
81ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
82 ThreadContext *tc)
83{
84 warn("ignoring syscall %s(%d, %d, ...)", desc->name,
85 process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1));
86
87 return 0;
88}
89
90
91SyscallReturn
92exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
93 ThreadContext *tc)
94{
94 if (tc->exit()) {
95 if (process->system->numRunningContexts() == 1) {
96 // Last running context... exit simulator
97 exitSimLoop("target called exit()",
96 process->getSyscallArg(tc, 0) & 0xff);
98 process->getSyscallArg(tc, 0) & 0xff);
99 } else {
100 // other running threads... just halt this one
101 tc->halt();
102 }
103
104 return 1;
105}
106
107
108SyscallReturn
109getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
110{
111 return (int)VMPageSize;
112}
113
114
115SyscallReturn
116brkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
117{
118 // change brk addr to first arg
119 Addr new_brk = p->getSyscallArg(tc, 0);
120
121 // in Linux at least, brk(0) returns the current break value
122 // (note that the syscall and the glibc function have different behavior)
123 if (new_brk == 0)
124 return p->brk_point;
125
126 if (new_brk > p->brk_point) {
127 // might need to allocate some new pages
128 for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
129 VMPageSize); !gen.done(); gen.next()) {
130 if (!p->pTable->translate(gen.addr()))
131 p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
132 VMPageSize);
133 }
134 }
135
136 p->brk_point = new_brk;
137 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
138 return p->brk_point;
139}
140
141
142SyscallReturn
143closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
144{
145 int target_fd = p->getSyscallArg(tc, 0);
146 int status = close(p->sim_fd(target_fd));
147 if (status >= 0)
148 p->free_fd(target_fd);
149 return status;
150}
151
152
153SyscallReturn
154readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
155{
156 int fd = p->sim_fd(p->getSyscallArg(tc, 0));
157 int nbytes = p->getSyscallArg(tc, 2);
158 BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
159
160 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
161
162 if (bytes_read != -1)
163 bufArg.copyOut(tc->getMemPort());
164
165 return bytes_read;
166}
167
168SyscallReturn
169writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
170{
171 int fd = p->sim_fd(p->getSyscallArg(tc, 0));
172 int nbytes = p->getSyscallArg(tc, 2);
173 BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
174
175 bufArg.copyIn(tc->getMemPort());
176
177 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
178
179 fsync(fd);
180
181 return bytes_written;
182}
183
184
185SyscallReturn
186lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
187{
188 int fd = p->sim_fd(p->getSyscallArg(tc, 0));
189 uint64_t offs = p->getSyscallArg(tc, 1);
190 int whence = p->getSyscallArg(tc, 2);
191
192 off_t result = lseek(fd, offs, whence);
193
194 return (result == (off_t)-1) ? -errno : result;
195}
196
197
198SyscallReturn
199_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
200{
201 int fd = p->sim_fd(p->getSyscallArg(tc, 0));
202 uint64_t offset_high = p->getSyscallArg(tc, 1);
203 uint32_t offset_low = p->getSyscallArg(tc, 2);
204 Addr result_ptr = p->getSyscallArg(tc, 3);
205 int whence = p->getSyscallArg(tc, 4);
206
207 uint64_t offset = (offset_high << 32) | offset_low;
208
209 uint64_t result = lseek(fd, offset, whence);
210 result = TheISA::htog(result);
211
212 if (result == (off_t)-1) {
213 //The seek failed.
214 return -errno;
215 } else {
216 //The seek succeeded.
217 //Copy "result" to "result_ptr"
218 //XXX We'll assume that the size of loff_t is 64 bits on the
219 //target platform
220 BufferArg result_buf(result_ptr, sizeof(result));
221 memcpy(result_buf.bufferPtr(), &result, sizeof(result));
222 result_buf.copyOut(tc->getMemPort());
223 return 0;
224 }
225
226
227 return (result == (off_t)-1) ? -errno : result;
228}
229
230
231SyscallReturn
232munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
233{
234 // given that we don't really implement mmap, munmap is really easy
235 return 0;
236}
237
238
239const char *hostname = "m5.eecs.umich.edu";
240
241SyscallReturn
242gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
243{
244 int name_len = p->getSyscallArg(tc, 1);
245 BufferArg name(p->getSyscallArg(tc, 0), name_len);
246
247 strncpy((char *)name.bufferPtr(), hostname, name_len);
248
249 name.copyOut(tc->getMemPort());
250
251 return 0;
252}
253
254SyscallReturn
255getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
256{
257 int result = 0;
258 unsigned long size = p->getSyscallArg(tc, 1);
259 BufferArg buf(p->getSyscallArg(tc, 0), size);
260
261 // Is current working directory defined?
262 string cwd = p->getcwd();
263 if (!cwd.empty()) {
264 if (cwd.length() >= size) {
265 // Buffer too small
266 return -ERANGE;
267 }
268 strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
269 result = cwd.length();
270 }
271 else {
272 if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
273 result = strlen((char *)buf.bufferPtr());
274 }
275 else {
276 result = -1;
277 }
278 }
279
280 buf.copyOut(tc->getMemPort());
281
282 return (result == -1) ? -errno : result;
283}
284
285
286SyscallReturn
287readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
288{
289 string path;
290
291 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
292 return (TheISA::IntReg)-EFAULT;
293
294 // Adjust path for current working directory
295 path = p->fullPath(path);
296
297 size_t bufsiz = p->getSyscallArg(tc, 2);
298 BufferArg buf(p->getSyscallArg(tc, 1), bufsiz);
299
300 int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
301
302 buf.copyOut(tc->getMemPort());
303
304 return (result == -1) ? -errno : result;
305}
306
307SyscallReturn
308unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
309{
310 string path;
311
312 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
313 return (TheISA::IntReg)-EFAULT;
314
315 // Adjust path for current working directory
316 path = p->fullPath(path);
317
318 int result = unlink(path.c_str());
319 return (result == -1) ? -errno : result;
320}
321
322
323SyscallReturn
324mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
325{
326 string path;
327
328 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
329 return (TheISA::IntReg)-EFAULT;
330
331 // Adjust path for current working directory
332 path = p->fullPath(path);
333
334 mode_t mode = p->getSyscallArg(tc, 1);
335
336 int result = mkdir(path.c_str(), mode);
337 return (result == -1) ? -errno : result;
338}
339
340SyscallReturn
341renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
342{
343 string old_name;
344
345 if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0)))
346 return -EFAULT;
347
348 string new_name;
349
350 if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1)))
351 return -EFAULT;
352
353 // Adjust path for current working directory
354 old_name = p->fullPath(old_name);
355 new_name = p->fullPath(new_name);
356
357 int64_t result = rename(old_name.c_str(), new_name.c_str());
358 return (result == -1) ? -errno : result;
359}
360
361SyscallReturn
362truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
363{
364 string path;
365
366 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
367 return -EFAULT;
368
369 off_t length = p->getSyscallArg(tc, 1);
370
371 // Adjust path for current working directory
372 path = p->fullPath(path);
373
374 int result = truncate(path.c_str(), length);
375 return (result == -1) ? -errno : result;
376}
377
378SyscallReturn
379ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
380{
381 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
382
383 if (fd < 0)
384 return -EBADF;
385
386 off_t length = process->getSyscallArg(tc, 1);
387
388 int result = ftruncate(fd, length);
389 return (result == -1) ? -errno : result;
390}
391
392SyscallReturn
393umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
394{
395 // Letting the simulated program change the simulator's umask seems like
396 // a bad idea. Compromise by just returning the current umask but not
397 // changing anything.
398 mode_t oldMask = umask(0);
399 umask(oldMask);
400 return (int)oldMask;
401}
402
403SyscallReturn
404chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
405{
406 string path;
407
408 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
409 return -EFAULT;
410
411 /* XXX endianess */
412 uint32_t owner = p->getSyscallArg(tc, 1);
413 uid_t hostOwner = owner;
414 uint32_t group = p->getSyscallArg(tc, 2);
415 gid_t hostGroup = group;
416
417 // Adjust path for current working directory
418 path = p->fullPath(path);
419
420 int result = chown(path.c_str(), hostOwner, hostGroup);
421 return (result == -1) ? -errno : result;
422}
423
424SyscallReturn
425fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
426{
427 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
428
429 if (fd < 0)
430 return -EBADF;
431
432 /* XXX endianess */
433 uint32_t owner = process->getSyscallArg(tc, 1);
434 uid_t hostOwner = owner;
435 uint32_t group = process->getSyscallArg(tc, 2);
436 gid_t hostGroup = group;
437
438 int result = fchown(fd, hostOwner, hostGroup);
439 return (result == -1) ? -errno : result;
440}
441
442
443SyscallReturn
444dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
445{
446 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
447 if (fd < 0)
448 return -EBADF;
449
450 Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0));
451
452 int result = dup(fd);
453 return (result == -1) ? -errno : process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
454}
455
456
457SyscallReturn
458fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
459 ThreadContext *tc)
460{
461 int fd = process->getSyscallArg(tc, 0);
462
463 if (fd < 0 || process->sim_fd(fd) < 0)
464 return -EBADF;
465
466 int cmd = process->getSyscallArg(tc, 1);
467 switch (cmd) {
468 case 0: // F_DUPFD
469 // if we really wanted to support this, we'd need to do it
470 // in the target fd space.
471 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
472 return -EMFILE;
473
474 case 1: // F_GETFD (get close-on-exec flag)
475 case 2: // F_SETFD (set close-on-exec flag)
476 return 0;
477
478 case 3: // F_GETFL (get file flags)
479 case 4: // F_SETFL (set file flags)
480 // not sure if this is totally valid, but we'll pass it through
481 // to the underlying OS
482 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
483 return fcntl(process->sim_fd(fd), cmd);
484 // return 0;
485
486 case 7: // F_GETLK (get lock)
487 case 8: // F_SETLK (set lock)
488 case 9: // F_SETLKW (set lock and wait)
489 // don't mess with file locking... just act like it's OK
490 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
491 return 0;
492
493 default:
494 warn("Unknown fcntl command %d\n", cmd);
495 return 0;
496 }
497}
498
499SyscallReturn
500fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
501 ThreadContext *tc)
502{
503 int fd = process->getSyscallArg(tc, 0);
504
505 if (fd < 0 || process->sim_fd(fd) < 0)
506 return -EBADF;
507
508 int cmd = process->getSyscallArg(tc, 1);
509 switch (cmd) {
510 case 33: //F_GETLK64
511 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
512 return -EMFILE;
513
514 case 34: // F_SETLK64
515 case 35: // F_SETLKW64
516 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
517 return -EMFILE;
518
519 default:
520 // not sure if this is totally valid, but we'll pass it through
521 // to the underlying OS
522 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
523 return fcntl(process->sim_fd(fd), cmd);
524 // return 0;
525 }
526}
527
528SyscallReturn
529pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
530 ThreadContext *tc)
531{
532 int fds[2], sim_fds[2];
533 int pipe_retval = pipe(fds);
534
535 if (pipe_retval < 0) {
536 // error
537 return pipe_retval;
538 }
539
540 sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
541 sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
542
543 process->setReadPipeSource(sim_fds[0], sim_fds[1]);
544 // Alpha Linux convention for pipe() is that fd[0] is returned as
545 // the return value of the function, and fd[1] is returned in r20.
546 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
547 return sim_fds[0];
548}
549
550
551SyscallReturn
552getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
553 ThreadContext *tc)
554{
555 // Make up a PID. There's no interprocess communication in
556 // fake_syscall mode, so there's no way for a process to know it's
557 // not getting a unique value.
558
559 tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
560 return process->pid();
561}
562
563
564SyscallReturn
565getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
566 ThreadContext *tc)
567{
568 // Make up a UID and EUID... it shouldn't matter, and we want the
569 // simulation to be deterministic.
570
571 // EUID goes in r20.
572 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
573 return process->uid(); // UID
574}
575
576
577SyscallReturn
578getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
579 ThreadContext *tc)
580{
581 // Get current group ID. EGID goes in r20.
582 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
583 return process->gid();
584}
585
586
587SyscallReturn
588setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
589 ThreadContext *tc)
590{
591 // can't fathom why a benchmark would call this.
592 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0));
593 return 0;
594}
595
596SyscallReturn
597getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
598 ThreadContext *tc)
599{
600 // Make up a PID. There's no interprocess communication in
601 // fake_syscall mode, so there's no way for a process to know it's
602 // not getting a unique value.
603
604 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
605 return process->pid();
606}
607
608SyscallReturn
609getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
610 ThreadContext *tc)
611{
612 return process->ppid();
613}
614
615SyscallReturn
616getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
617 ThreadContext *tc)
618{
619 return process->uid(); // UID
620}
621
622SyscallReturn
623geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
624 ThreadContext *tc)
625{
626 return process->euid(); // UID
627}
628
629SyscallReturn
630getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
631 ThreadContext *tc)
632{
633 return process->gid();
634}
635
636SyscallReturn
637getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
638 ThreadContext *tc)
639{
640 return process->egid();
641}
642
643