syscall_emul.cc (6703:2707e7b63f53) syscall_emul.cc (6712:b95abe00dd9d)
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
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>
35#include <cstdio>
36#include <iostream>
36#include <iostream>
37#include <string>
37
38#include "sim/syscall_emul.hh"
39#include "base/chunk_generator.hh"
40#include "base/trace.hh"
41#include "config/the_isa.hh"
42#include "cpu/thread_context.hh"
43#include "cpu/base.hh"
44#include "mem/page_table.hh"
45#include "sim/process.hh"
46#include "sim/system.hh"
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#if TRACING_ON
56 int index = 0;
57#endif
58 DPRINTFR(SyscallVerbose,
59 "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
60 curTick, tc->getCpuPtr()->name(), name,
61 process->getSyscallArg(tc, index),
62 process->getSyscallArg(tc, index),
63 process->getSyscallArg(tc, index),
64 process->getSyscallArg(tc, index));
65
66 SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
67
68 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
69 curTick,tc->getCpuPtr()->name(), name, retval.value());
70
71 if (!(flags & SyscallDesc::SuppressReturnValue))
72 process->setSyscallReturn(tc, retval);
73}
74
75
76SyscallReturn
77unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
78 ThreadContext *tc)
79{
80 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
81
82 return 1;
83}
84
85
86SyscallReturn
87ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
88 ThreadContext *tc)
89{
90 int index = 0;
91 warn("ignoring syscall %s(%d, %d, ...)", desc->name,
92 process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
93
94 return 0;
95}
96
97
98SyscallReturn
99exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
100 ThreadContext *tc)
101{
102 if (process->system->numRunningContexts() == 1) {
103 // Last running context... exit simulator
104 int index = 0;
105 exitSimLoop("target called exit()",
106 process->getSyscallArg(tc, index) & 0xff);
107 } else {
108 // other running threads... just halt this one
109 tc->halt();
110 }
111
112 return 1;
113}
114
115
116SyscallReturn
117exitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
118 ThreadContext *tc)
119{
120 // really should just halt all thread contexts belonging to this
121 // process in case there's another process running...
122 int index = 0;
123 exitSimLoop("target called exit()",
124 process->getSyscallArg(tc, index) & 0xff);
125
126 return 1;
127}
128
129
130SyscallReturn
131getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
132{
133 return (int)VMPageSize;
134}
135
136
137SyscallReturn
138brkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
139{
140 // change brk addr to first arg
141 int index = 0;
142 Addr new_brk = p->getSyscallArg(tc, index);
143
144 // in Linux at least, brk(0) returns the current break value
145 // (note that the syscall and the glibc function have different behavior)
146 if (new_brk == 0)
147 return p->brk_point;
148
149 if (new_brk > p->brk_point) {
150 // might need to allocate some new pages
151 for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
152 VMPageSize); !gen.done(); gen.next()) {
153 if (!p->pTable->translate(gen.addr()))
154 p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
155 VMPageSize);
156
157 // if the address is already there, zero it out
158 else {
159 uint8_t zero = 0;
160 TranslatingPort *tp = tc->getMemPort();
161
162 // split non-page aligned accesses
163 Addr next_page = roundUp(gen.addr(), VMPageSize);
164 uint32_t size_needed = next_page - gen.addr();
165 tp->memsetBlob(gen.addr(), zero, size_needed);
166 if (gen.addr() + VMPageSize > next_page &&
167 next_page < new_brk &&
168 p->pTable->translate(next_page))
169 {
170 size_needed = VMPageSize - size_needed;
171 tp->memsetBlob(next_page, zero, size_needed);
172 }
173 }
174 }
175 }
176
177 p->brk_point = new_brk;
178 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
179 return p->brk_point;
180}
181
182
183SyscallReturn
184closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
185{
186 int index = 0;
187 int target_fd = p->getSyscallArg(tc, index);
188 int status = close(p->sim_fd(target_fd));
189 if (status >= 0)
190 p->free_fd(target_fd);
191 return status;
192}
193
194
195SyscallReturn
196readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
197{
198 int index = 0;
199 int fd = p->sim_fd(p->getSyscallArg(tc, index));
200 Addr bufPtr = p->getSyscallArg(tc, index);
201 int nbytes = p->getSyscallArg(tc, index);
202 BufferArg bufArg(bufPtr, nbytes);
203
204 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
205
206 if (bytes_read != -1)
207 bufArg.copyOut(tc->getMemPort());
208
209 return bytes_read;
210}
211
212SyscallReturn
213writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
214{
215 int index = 0;
216 int fd = p->sim_fd(p->getSyscallArg(tc, index));
217 Addr bufPtr = p->getSyscallArg(tc, index);
218 int nbytes = p->getSyscallArg(tc, index);
219 BufferArg bufArg(bufPtr, nbytes);
220
221 bufArg.copyIn(tc->getMemPort());
222
223 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
224
225 fsync(fd);
226
227 return bytes_written;
228}
229
230
231SyscallReturn
232lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
233{
234 int index = 0;
235 int fd = p->sim_fd(p->getSyscallArg(tc, index));
236 uint64_t offs = p->getSyscallArg(tc, index);
237 int whence = p->getSyscallArg(tc, index);
238
239 off_t result = lseek(fd, offs, whence);
240
241 return (result == (off_t)-1) ? -errno : result;
242}
243
244
245SyscallReturn
246_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
247{
248 int index = 0;
249 int fd = p->sim_fd(p->getSyscallArg(tc, index));
250 uint64_t offset_high = p->getSyscallArg(tc, index);
251 uint32_t offset_low = p->getSyscallArg(tc, index);
252 Addr result_ptr = p->getSyscallArg(tc, index);
253 int whence = p->getSyscallArg(tc, index);
254
255 uint64_t offset = (offset_high << 32) | offset_low;
256
257 uint64_t result = lseek(fd, offset, whence);
258 result = TheISA::htog(result);
259
260 if (result == (off_t)-1) {
261 //The seek failed.
262 return -errno;
263 } else {
264 // The seek succeeded.
265 // Copy "result" to "result_ptr"
266 // XXX We'll assume that the size of loff_t is 64 bits on the
267 // target platform
268 BufferArg result_buf(result_ptr, sizeof(result));
269 memcpy(result_buf.bufferPtr(), &result, sizeof(result));
270 result_buf.copyOut(tc->getMemPort());
271 return 0;
272 }
273
274
275 return (result == (off_t)-1) ? -errno : result;
276}
277
278
279SyscallReturn
280munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
281{
282 // given that we don't really implement mmap, munmap is really easy
283 return 0;
284}
285
286
287const char *hostname = "m5.eecs.umich.edu";
288
289SyscallReturn
290gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
291{
292 int index = 0;
293 Addr bufPtr = p->getSyscallArg(tc, index);
294 int name_len = p->getSyscallArg(tc, index);
295 BufferArg name(bufPtr, name_len);
296
297 strncpy((char *)name.bufferPtr(), hostname, name_len);
298
299 name.copyOut(tc->getMemPort());
300
301 return 0;
302}
303
304SyscallReturn
305getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
306{
307 int result = 0;
308 int index;
309 Addr bufPtr = p->getSyscallArg(tc, index);
310 unsigned long size = p->getSyscallArg(tc, index);
311 BufferArg buf(bufPtr, size);
312
313 // Is current working directory defined?
314 string cwd = p->getcwd();
315 if (!cwd.empty()) {
316 if (cwd.length() >= size) {
317 // Buffer too small
318 return -ERANGE;
319 }
320 strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
321 result = cwd.length();
322 }
323 else {
324 if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
325 result = strlen((char *)buf.bufferPtr());
326 }
327 else {
328 result = -1;
329 }
330 }
331
332 buf.copyOut(tc->getMemPort());
333
334 return (result == -1) ? -errno : result;
335}
336
337
338SyscallReturn
339readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
340{
341 string path;
342
343 int index = 0;
344 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
345 return (TheISA::IntReg)-EFAULT;
346
347 // Adjust path for current working directory
348 path = p->fullPath(path);
349
350 Addr bufPtr = p->getSyscallArg(tc, index);
351 size_t bufsiz = p->getSyscallArg(tc, index);
352
353 BufferArg buf(bufPtr, bufsiz);
354
355 int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
356
357 buf.copyOut(tc->getMemPort());
358
359 return (result == -1) ? -errno : result;
360}
361
362SyscallReturn
363unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
364{
365 string path;
366
367 int index = 0;
368 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
369 return (TheISA::IntReg)-EFAULT;
370
371 // Adjust path for current working directory
372 path = p->fullPath(path);
373
374 int result = unlink(path.c_str());
375 return (result == -1) ? -errno : result;
376}
377
378
379SyscallReturn
380mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
381{
382 string path;
383
384 int index = 0;
385 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
386 return (TheISA::IntReg)-EFAULT;
387
388 // Adjust path for current working directory
389 path = p->fullPath(path);
390
391 mode_t mode = p->getSyscallArg(tc, index);
392
393 int result = mkdir(path.c_str(), mode);
394 return (result == -1) ? -errno : result;
395}
396
397SyscallReturn
398renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
399{
400 string old_name;
401
402 int index = 0;
403 if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index)))
404 return -EFAULT;
405
406 string new_name;
407
408 if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index)))
409 return -EFAULT;
410
411 // Adjust path for current working directory
412 old_name = p->fullPath(old_name);
413 new_name = p->fullPath(new_name);
414
415 int64_t result = rename(old_name.c_str(), new_name.c_str());
416 return (result == -1) ? -errno : result;
417}
418
419SyscallReturn
420truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
421{
422 string path;
423
424 int index = 0;
425 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
426 return -EFAULT;
427
428 off_t length = p->getSyscallArg(tc, index);
429
430 // Adjust path for current working directory
431 path = p->fullPath(path);
432
433 int result = truncate(path.c_str(), length);
434 return (result == -1) ? -errno : result;
435}
436
437SyscallReturn
438ftruncateFunc(SyscallDesc *desc, int num,
439 LiveProcess *process, ThreadContext *tc)
440{
441 int index = 0;
442 int fd = process->sim_fd(process->getSyscallArg(tc, index));
443
444 if (fd < 0)
445 return -EBADF;
446
447 off_t length = process->getSyscallArg(tc, index);
448
449 int result = ftruncate(fd, length);
450 return (result == -1) ? -errno : result;
451}
452
453SyscallReturn
454truncate64Func(SyscallDesc *desc, int num,
455 LiveProcess *process, ThreadContext *tc)
456{
457 int index = 0;
458 string path;
459
460 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index)))
461 return -EFAULT;
462
463 loff_t length = process->getSyscallArg(tc, index, 64);
464
465 // Adjust path for current working directory
466 path = process->fullPath(path);
467
468 int result = truncate64(path.c_str(), length);
469 return (result == -1) ? -errno : result;
470}
471
472SyscallReturn
473ftruncate64Func(SyscallDesc *desc, int num,
474 LiveProcess *process, ThreadContext *tc)
475{
476 int index = 0;
477 int fd = process->sim_fd(process->getSyscallArg(tc, index));
478
479 if (fd < 0)
480 return -EBADF;
481
482 loff_t length = process->getSyscallArg(tc, index, 64);
483
484 int result = ftruncate64(fd, length);
485 return (result == -1) ? -errno : result;
486}
487
488SyscallReturn
489umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
490{
491 // Letting the simulated program change the simulator's umask seems like
492 // a bad idea. Compromise by just returning the current umask but not
493 // changing anything.
494 mode_t oldMask = umask(0);
495 umask(oldMask);
496 return (int)oldMask;
497}
498
499SyscallReturn
500chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
501{
502 string path;
503
504 int index = 0;
505 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
506 return -EFAULT;
507
508 /* XXX endianess */
509 uint32_t owner = p->getSyscallArg(tc, index);
510 uid_t hostOwner = owner;
511 uint32_t group = p->getSyscallArg(tc, index);
512 gid_t hostGroup = group;
513
514 // Adjust path for current working directory
515 path = p->fullPath(path);
516
517 int result = chown(path.c_str(), hostOwner, hostGroup);
518 return (result == -1) ? -errno : result;
519}
520
521SyscallReturn
522fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
523{
524 int index = 0;
525 int fd = process->sim_fd(process->getSyscallArg(tc, index));
526
527 if (fd < 0)
528 return -EBADF;
529
530 /* XXX endianess */
531 uint32_t owner = process->getSyscallArg(tc, index);
532 uid_t hostOwner = owner;
533 uint32_t group = process->getSyscallArg(tc, index);
534 gid_t hostGroup = group;
535
536 int result = fchown(fd, hostOwner, hostGroup);
537 return (result == -1) ? -errno : result;
538}
539
540
541SyscallReturn
542dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
543{
544 int index = 0;
545 int fd = process->sim_fd(process->getSyscallArg(tc, index));
546 if (fd < 0)
547 return -EBADF;
548
549 Process::FdMap *fdo = process->sim_fd_obj(fd);
550
551 int result = dup(fd);
552 return (result == -1) ? -errno :
553 process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
554}
555
556
557SyscallReturn
558fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
559 ThreadContext *tc)
560{
561 int index = 0;
562 int fd = process->getSyscallArg(tc, index);
563
564 if (fd < 0 || process->sim_fd(fd) < 0)
565 return -EBADF;
566
567 int cmd = process->getSyscallArg(tc, index);
568 switch (cmd) {
569 case 0: // F_DUPFD
570 // if we really wanted to support this, we'd need to do it
571 // in the target fd space.
572 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
573 return -EMFILE;
574
575 case 1: // F_GETFD (get close-on-exec flag)
576 case 2: // F_SETFD (set close-on-exec flag)
577 return 0;
578
579 case 3: // F_GETFL (get file flags)
580 case 4: // F_SETFL (set file flags)
581 // not sure if this is totally valid, but we'll pass it through
582 // to the underlying OS
583 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
584 return fcntl(process->sim_fd(fd), cmd);
585 // return 0;
586
587 case 7: // F_GETLK (get lock)
588 case 8: // F_SETLK (set lock)
589 case 9: // F_SETLKW (set lock and wait)
590 // don't mess with file locking... just act like it's OK
591 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
592 return 0;
593
594 default:
595 warn("Unknown fcntl command %d\n", cmd);
596 return 0;
597 }
598}
599
600SyscallReturn
601fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
602 ThreadContext *tc)
603{
604 int index = 0;
605 int fd = process->getSyscallArg(tc, index);
606
607 if (fd < 0 || process->sim_fd(fd) < 0)
608 return -EBADF;
609
610 int cmd = process->getSyscallArg(tc, index);
611 switch (cmd) {
612 case 33: //F_GETLK64
613 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
614 return -EMFILE;
615
616 case 34: // F_SETLK64
617 case 35: // F_SETLKW64
618 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
619 return -EMFILE;
620
621 default:
622 // not sure if this is totally valid, but we'll pass it through
623 // to the underlying OS
624 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
625 return fcntl(process->sim_fd(fd), cmd);
626 // return 0;
627 }
628}
629
630SyscallReturn
631pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
632 ThreadContext *tc)
633{
634 int fds[2], sim_fds[2];
635 int pipe_retval = pipe(fds);
636
637 if (pipe_retval < 0) {
638 // error
639 return pipe_retval;
640 }
641
642 sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
643 sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
644
645 process->setReadPipeSource(sim_fds[0], sim_fds[1]);
646 // Alpha Linux convention for pipe() is that fd[0] is returned as
647 // the return value of the function, and fd[1] is returned in r20.
648 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
649 return sim_fds[0];
650}
651
652
653SyscallReturn
654getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
655 ThreadContext *tc)
656{
657 // Make up a PID. There's no interprocess communication in
658 // fake_syscall mode, so there's no way for a process to know it's
659 // not getting a unique value.
660
661 tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
662 return process->pid();
663}
664
665
666SyscallReturn
667getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
668 ThreadContext *tc)
669{
670 // Make up a UID and EUID... it shouldn't matter, and we want the
671 // simulation to be deterministic.
672
673 // EUID goes in r20.
674 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
675 return process->uid(); // UID
676}
677
678
679SyscallReturn
680getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
681 ThreadContext *tc)
682{
683 // Get current group ID. EGID goes in r20.
684 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
685 return process->gid();
686}
687
688
689SyscallReturn
690setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
691 ThreadContext *tc)
692{
693 // can't fathom why a benchmark would call this.
694 int index = 0;
695 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
696 return 0;
697}
698
699SyscallReturn
700getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
701 ThreadContext *tc)
702{
703 // Make up a PID. There's no interprocess communication in
704 // fake_syscall mode, so there's no way for a process to know it's
705 // not getting a unique value.
706
707 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
708 return process->pid();
709}
710
711SyscallReturn
712getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
713 ThreadContext *tc)
714{
715 return process->ppid();
716}
717
718SyscallReturn
719getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
720 ThreadContext *tc)
721{
722 return process->uid(); // UID
723}
724
725SyscallReturn
726geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
727 ThreadContext *tc)
728{
729 return process->euid(); // UID
730}
731
732SyscallReturn
733getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
734 ThreadContext *tc)
735{
736 return process->gid();
737}
738
739SyscallReturn
740getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
741 ThreadContext *tc)
742{
743 return process->egid();
744}
745
746
747SyscallReturn
748cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
749 ThreadContext *tc)
750{
751 int index = 0;
752 IntReg flags = process->getSyscallArg(tc, index);
753 IntReg newStack = process->getSyscallArg(tc, index);
754
755 DPRINTF(SyscallVerbose, "In sys_clone:\n");
756 DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
757 DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
758
759
760 if (flags != 0x10f00) {
761 warn("This sys_clone implementation assumes flags "
762 "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
763 "(0x10f00), and may not work correctly with given flags "
764 "0x%llx\n", flags);
765 }
766
767 ThreadContext* ctc; // child thread context
768 if ( ( ctc = process->findFreeContext() ) != NULL ) {
769 DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
770
771 ctc->clearArchRegs();
772
773 // Arch-specific cloning code
774 #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
775 // Cloning the misc. regs for these archs is enough
776 TheISA::copyMiscRegs(tc, ctc);
777 #elif THE_ISA == SPARC_ISA
778 TheISA::copyRegs(tc, ctc);
779
780 // TODO: Explain what this code actually does :-)
781 ctc->setIntReg(NumIntArchRegs + 6, 0);
782 ctc->setIntReg(NumIntArchRegs + 4, 0);
783 ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
784 ctc->setIntReg(NumIntArchRegs + 5, NWindows);
785 ctc->setMiscReg(MISCREG_CWP, 0);
786 ctc->setIntReg(NumIntArchRegs + 7, 0);
787 ctc->setMiscRegNoEffect(MISCREG_TL, 0);
788 ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
789
790 for (int y = 8; y < 32; y++)
791 ctc->setIntReg(y, tc->readIntReg(y));
792 #else
793 fatal("sys_clone is not implemented for this ISA\n");
794 #endif
795
796 // Set up stack register
797 ctc->setIntReg(TheISA::StackPointerReg, newStack);
798
799 // Set up syscall return values in parent and child
800 ctc->setIntReg(ReturnValueReg, 0); // return value, child
801
802 // Alpha needs SyscallSuccessReg=0 in child
803 #if THE_ISA == ALPHA_ISA
804 ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
805 #endif
806
807 // In SPARC/Linux, clone returns 0 on pseudo-return register if
808 // parent, non-zero if child
809 #if THE_ISA == SPARC_ISA
810 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
811 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
812 #endif
813
814 ctc->setPC(tc->readNextPC());
815 ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
816 ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst));
817
818 ctc->activate();
819
820 // Should return nonzero child TID in parent's syscall return register,
821 // but for our pthread library any non-zero value will work
822 return 1;
823 } else {
824 fatal("Called sys_clone, but no unallocated thread contexts found!\n");
825 return 0;
826 }
827}
828
38
39#include "sim/syscall_emul.hh"
40#include "base/chunk_generator.hh"
41#include "base/trace.hh"
42#include "config/the_isa.hh"
43#include "cpu/thread_context.hh"
44#include "cpu/base.hh"
45#include "mem/page_table.hh"
46#include "sim/process.hh"
47#include "sim/system.hh"
48#include "sim/sim_exit.hh"
49
50using namespace std;
51using namespace TheISA;
52
53void
54SyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
55{
56#if TRACING_ON
57 int index = 0;
58#endif
59 DPRINTFR(SyscallVerbose,
60 "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
61 curTick, tc->getCpuPtr()->name(), name,
62 process->getSyscallArg(tc, index),
63 process->getSyscallArg(tc, index),
64 process->getSyscallArg(tc, index),
65 process->getSyscallArg(tc, index));
66
67 SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
68
69 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
70 curTick,tc->getCpuPtr()->name(), name, retval.value());
71
72 if (!(flags & SyscallDesc::SuppressReturnValue))
73 process->setSyscallReturn(tc, retval);
74}
75
76
77SyscallReturn
78unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
79 ThreadContext *tc)
80{
81 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
82
83 return 1;
84}
85
86
87SyscallReturn
88ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
89 ThreadContext *tc)
90{
91 int index = 0;
92 warn("ignoring syscall %s(%d, %d, ...)", desc->name,
93 process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
94
95 return 0;
96}
97
98
99SyscallReturn
100exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
101 ThreadContext *tc)
102{
103 if (process->system->numRunningContexts() == 1) {
104 // Last running context... exit simulator
105 int index = 0;
106 exitSimLoop("target called exit()",
107 process->getSyscallArg(tc, index) & 0xff);
108 } else {
109 // other running threads... just halt this one
110 tc->halt();
111 }
112
113 return 1;
114}
115
116
117SyscallReturn
118exitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
119 ThreadContext *tc)
120{
121 // really should just halt all thread contexts belonging to this
122 // process in case there's another process running...
123 int index = 0;
124 exitSimLoop("target called exit()",
125 process->getSyscallArg(tc, index) & 0xff);
126
127 return 1;
128}
129
130
131SyscallReturn
132getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
133{
134 return (int)VMPageSize;
135}
136
137
138SyscallReturn
139brkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
140{
141 // change brk addr to first arg
142 int index = 0;
143 Addr new_brk = p->getSyscallArg(tc, index);
144
145 // in Linux at least, brk(0) returns the current break value
146 // (note that the syscall and the glibc function have different behavior)
147 if (new_brk == 0)
148 return p->brk_point;
149
150 if (new_brk > p->brk_point) {
151 // might need to allocate some new pages
152 for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
153 VMPageSize); !gen.done(); gen.next()) {
154 if (!p->pTable->translate(gen.addr()))
155 p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
156 VMPageSize);
157
158 // if the address is already there, zero it out
159 else {
160 uint8_t zero = 0;
161 TranslatingPort *tp = tc->getMemPort();
162
163 // split non-page aligned accesses
164 Addr next_page = roundUp(gen.addr(), VMPageSize);
165 uint32_t size_needed = next_page - gen.addr();
166 tp->memsetBlob(gen.addr(), zero, size_needed);
167 if (gen.addr() + VMPageSize > next_page &&
168 next_page < new_brk &&
169 p->pTable->translate(next_page))
170 {
171 size_needed = VMPageSize - size_needed;
172 tp->memsetBlob(next_page, zero, size_needed);
173 }
174 }
175 }
176 }
177
178 p->brk_point = new_brk;
179 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
180 return p->brk_point;
181}
182
183
184SyscallReturn
185closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
186{
187 int index = 0;
188 int target_fd = p->getSyscallArg(tc, index);
189 int status = close(p->sim_fd(target_fd));
190 if (status >= 0)
191 p->free_fd(target_fd);
192 return status;
193}
194
195
196SyscallReturn
197readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
198{
199 int index = 0;
200 int fd = p->sim_fd(p->getSyscallArg(tc, index));
201 Addr bufPtr = p->getSyscallArg(tc, index);
202 int nbytes = p->getSyscallArg(tc, index);
203 BufferArg bufArg(bufPtr, nbytes);
204
205 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
206
207 if (bytes_read != -1)
208 bufArg.copyOut(tc->getMemPort());
209
210 return bytes_read;
211}
212
213SyscallReturn
214writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
215{
216 int index = 0;
217 int fd = p->sim_fd(p->getSyscallArg(tc, index));
218 Addr bufPtr = p->getSyscallArg(tc, index);
219 int nbytes = p->getSyscallArg(tc, index);
220 BufferArg bufArg(bufPtr, nbytes);
221
222 bufArg.copyIn(tc->getMemPort());
223
224 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
225
226 fsync(fd);
227
228 return bytes_written;
229}
230
231
232SyscallReturn
233lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
234{
235 int index = 0;
236 int fd = p->sim_fd(p->getSyscallArg(tc, index));
237 uint64_t offs = p->getSyscallArg(tc, index);
238 int whence = p->getSyscallArg(tc, index);
239
240 off_t result = lseek(fd, offs, whence);
241
242 return (result == (off_t)-1) ? -errno : result;
243}
244
245
246SyscallReturn
247_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
248{
249 int index = 0;
250 int fd = p->sim_fd(p->getSyscallArg(tc, index));
251 uint64_t offset_high = p->getSyscallArg(tc, index);
252 uint32_t offset_low = p->getSyscallArg(tc, index);
253 Addr result_ptr = p->getSyscallArg(tc, index);
254 int whence = p->getSyscallArg(tc, index);
255
256 uint64_t offset = (offset_high << 32) | offset_low;
257
258 uint64_t result = lseek(fd, offset, whence);
259 result = TheISA::htog(result);
260
261 if (result == (off_t)-1) {
262 //The seek failed.
263 return -errno;
264 } else {
265 // The seek succeeded.
266 // Copy "result" to "result_ptr"
267 // XXX We'll assume that the size of loff_t is 64 bits on the
268 // target platform
269 BufferArg result_buf(result_ptr, sizeof(result));
270 memcpy(result_buf.bufferPtr(), &result, sizeof(result));
271 result_buf.copyOut(tc->getMemPort());
272 return 0;
273 }
274
275
276 return (result == (off_t)-1) ? -errno : result;
277}
278
279
280SyscallReturn
281munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
282{
283 // given that we don't really implement mmap, munmap is really easy
284 return 0;
285}
286
287
288const char *hostname = "m5.eecs.umich.edu";
289
290SyscallReturn
291gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
292{
293 int index = 0;
294 Addr bufPtr = p->getSyscallArg(tc, index);
295 int name_len = p->getSyscallArg(tc, index);
296 BufferArg name(bufPtr, name_len);
297
298 strncpy((char *)name.bufferPtr(), hostname, name_len);
299
300 name.copyOut(tc->getMemPort());
301
302 return 0;
303}
304
305SyscallReturn
306getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
307{
308 int result = 0;
309 int index;
310 Addr bufPtr = p->getSyscallArg(tc, index);
311 unsigned long size = p->getSyscallArg(tc, index);
312 BufferArg buf(bufPtr, size);
313
314 // Is current working directory defined?
315 string cwd = p->getcwd();
316 if (!cwd.empty()) {
317 if (cwd.length() >= size) {
318 // Buffer too small
319 return -ERANGE;
320 }
321 strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
322 result = cwd.length();
323 }
324 else {
325 if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
326 result = strlen((char *)buf.bufferPtr());
327 }
328 else {
329 result = -1;
330 }
331 }
332
333 buf.copyOut(tc->getMemPort());
334
335 return (result == -1) ? -errno : result;
336}
337
338
339SyscallReturn
340readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
341{
342 string path;
343
344 int index = 0;
345 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
346 return (TheISA::IntReg)-EFAULT;
347
348 // Adjust path for current working directory
349 path = p->fullPath(path);
350
351 Addr bufPtr = p->getSyscallArg(tc, index);
352 size_t bufsiz = p->getSyscallArg(tc, index);
353
354 BufferArg buf(bufPtr, bufsiz);
355
356 int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
357
358 buf.copyOut(tc->getMemPort());
359
360 return (result == -1) ? -errno : result;
361}
362
363SyscallReturn
364unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
365{
366 string path;
367
368 int index = 0;
369 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
370 return (TheISA::IntReg)-EFAULT;
371
372 // Adjust path for current working directory
373 path = p->fullPath(path);
374
375 int result = unlink(path.c_str());
376 return (result == -1) ? -errno : result;
377}
378
379
380SyscallReturn
381mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
382{
383 string path;
384
385 int index = 0;
386 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
387 return (TheISA::IntReg)-EFAULT;
388
389 // Adjust path for current working directory
390 path = p->fullPath(path);
391
392 mode_t mode = p->getSyscallArg(tc, index);
393
394 int result = mkdir(path.c_str(), mode);
395 return (result == -1) ? -errno : result;
396}
397
398SyscallReturn
399renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
400{
401 string old_name;
402
403 int index = 0;
404 if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index)))
405 return -EFAULT;
406
407 string new_name;
408
409 if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index)))
410 return -EFAULT;
411
412 // Adjust path for current working directory
413 old_name = p->fullPath(old_name);
414 new_name = p->fullPath(new_name);
415
416 int64_t result = rename(old_name.c_str(), new_name.c_str());
417 return (result == -1) ? -errno : result;
418}
419
420SyscallReturn
421truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
422{
423 string path;
424
425 int index = 0;
426 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
427 return -EFAULT;
428
429 off_t length = p->getSyscallArg(tc, index);
430
431 // Adjust path for current working directory
432 path = p->fullPath(path);
433
434 int result = truncate(path.c_str(), length);
435 return (result == -1) ? -errno : result;
436}
437
438SyscallReturn
439ftruncateFunc(SyscallDesc *desc, int num,
440 LiveProcess *process, ThreadContext *tc)
441{
442 int index = 0;
443 int fd = process->sim_fd(process->getSyscallArg(tc, index));
444
445 if (fd < 0)
446 return -EBADF;
447
448 off_t length = process->getSyscallArg(tc, index);
449
450 int result = ftruncate(fd, length);
451 return (result == -1) ? -errno : result;
452}
453
454SyscallReturn
455truncate64Func(SyscallDesc *desc, int num,
456 LiveProcess *process, ThreadContext *tc)
457{
458 int index = 0;
459 string path;
460
461 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index)))
462 return -EFAULT;
463
464 loff_t length = process->getSyscallArg(tc, index, 64);
465
466 // Adjust path for current working directory
467 path = process->fullPath(path);
468
469 int result = truncate64(path.c_str(), length);
470 return (result == -1) ? -errno : result;
471}
472
473SyscallReturn
474ftruncate64Func(SyscallDesc *desc, int num,
475 LiveProcess *process, ThreadContext *tc)
476{
477 int index = 0;
478 int fd = process->sim_fd(process->getSyscallArg(tc, index));
479
480 if (fd < 0)
481 return -EBADF;
482
483 loff_t length = process->getSyscallArg(tc, index, 64);
484
485 int result = ftruncate64(fd, length);
486 return (result == -1) ? -errno : result;
487}
488
489SyscallReturn
490umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
491{
492 // Letting the simulated program change the simulator's umask seems like
493 // a bad idea. Compromise by just returning the current umask but not
494 // changing anything.
495 mode_t oldMask = umask(0);
496 umask(oldMask);
497 return (int)oldMask;
498}
499
500SyscallReturn
501chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
502{
503 string path;
504
505 int index = 0;
506 if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
507 return -EFAULT;
508
509 /* XXX endianess */
510 uint32_t owner = p->getSyscallArg(tc, index);
511 uid_t hostOwner = owner;
512 uint32_t group = p->getSyscallArg(tc, index);
513 gid_t hostGroup = group;
514
515 // Adjust path for current working directory
516 path = p->fullPath(path);
517
518 int result = chown(path.c_str(), hostOwner, hostGroup);
519 return (result == -1) ? -errno : result;
520}
521
522SyscallReturn
523fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
524{
525 int index = 0;
526 int fd = process->sim_fd(process->getSyscallArg(tc, index));
527
528 if (fd < 0)
529 return -EBADF;
530
531 /* XXX endianess */
532 uint32_t owner = process->getSyscallArg(tc, index);
533 uid_t hostOwner = owner;
534 uint32_t group = process->getSyscallArg(tc, index);
535 gid_t hostGroup = group;
536
537 int result = fchown(fd, hostOwner, hostGroup);
538 return (result == -1) ? -errno : result;
539}
540
541
542SyscallReturn
543dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
544{
545 int index = 0;
546 int fd = process->sim_fd(process->getSyscallArg(tc, index));
547 if (fd < 0)
548 return -EBADF;
549
550 Process::FdMap *fdo = process->sim_fd_obj(fd);
551
552 int result = dup(fd);
553 return (result == -1) ? -errno :
554 process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
555}
556
557
558SyscallReturn
559fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
560 ThreadContext *tc)
561{
562 int index = 0;
563 int fd = process->getSyscallArg(tc, index);
564
565 if (fd < 0 || process->sim_fd(fd) < 0)
566 return -EBADF;
567
568 int cmd = process->getSyscallArg(tc, index);
569 switch (cmd) {
570 case 0: // F_DUPFD
571 // if we really wanted to support this, we'd need to do it
572 // in the target fd space.
573 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
574 return -EMFILE;
575
576 case 1: // F_GETFD (get close-on-exec flag)
577 case 2: // F_SETFD (set close-on-exec flag)
578 return 0;
579
580 case 3: // F_GETFL (get file flags)
581 case 4: // F_SETFL (set file flags)
582 // not sure if this is totally valid, but we'll pass it through
583 // to the underlying OS
584 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
585 return fcntl(process->sim_fd(fd), cmd);
586 // return 0;
587
588 case 7: // F_GETLK (get lock)
589 case 8: // F_SETLK (set lock)
590 case 9: // F_SETLKW (set lock and wait)
591 // don't mess with file locking... just act like it's OK
592 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
593 return 0;
594
595 default:
596 warn("Unknown fcntl command %d\n", cmd);
597 return 0;
598 }
599}
600
601SyscallReturn
602fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
603 ThreadContext *tc)
604{
605 int index = 0;
606 int fd = process->getSyscallArg(tc, index);
607
608 if (fd < 0 || process->sim_fd(fd) < 0)
609 return -EBADF;
610
611 int cmd = process->getSyscallArg(tc, index);
612 switch (cmd) {
613 case 33: //F_GETLK64
614 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
615 return -EMFILE;
616
617 case 34: // F_SETLK64
618 case 35: // F_SETLKW64
619 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
620 return -EMFILE;
621
622 default:
623 // not sure if this is totally valid, but we'll pass it through
624 // to the underlying OS
625 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
626 return fcntl(process->sim_fd(fd), cmd);
627 // return 0;
628 }
629}
630
631SyscallReturn
632pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
633 ThreadContext *tc)
634{
635 int fds[2], sim_fds[2];
636 int pipe_retval = pipe(fds);
637
638 if (pipe_retval < 0) {
639 // error
640 return pipe_retval;
641 }
642
643 sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
644 sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
645
646 process->setReadPipeSource(sim_fds[0], sim_fds[1]);
647 // Alpha Linux convention for pipe() is that fd[0] is returned as
648 // the return value of the function, and fd[1] is returned in r20.
649 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
650 return sim_fds[0];
651}
652
653
654SyscallReturn
655getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
656 ThreadContext *tc)
657{
658 // Make up a PID. There's no interprocess communication in
659 // fake_syscall mode, so there's no way for a process to know it's
660 // not getting a unique value.
661
662 tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
663 return process->pid();
664}
665
666
667SyscallReturn
668getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
669 ThreadContext *tc)
670{
671 // Make up a UID and EUID... it shouldn't matter, and we want the
672 // simulation to be deterministic.
673
674 // EUID goes in r20.
675 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
676 return process->uid(); // UID
677}
678
679
680SyscallReturn
681getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
682 ThreadContext *tc)
683{
684 // Get current group ID. EGID goes in r20.
685 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
686 return process->gid();
687}
688
689
690SyscallReturn
691setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
692 ThreadContext *tc)
693{
694 // can't fathom why a benchmark would call this.
695 int index = 0;
696 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
697 return 0;
698}
699
700SyscallReturn
701getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
702 ThreadContext *tc)
703{
704 // Make up a PID. There's no interprocess communication in
705 // fake_syscall mode, so there's no way for a process to know it's
706 // not getting a unique value.
707
708 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
709 return process->pid();
710}
711
712SyscallReturn
713getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
714 ThreadContext *tc)
715{
716 return process->ppid();
717}
718
719SyscallReturn
720getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
721 ThreadContext *tc)
722{
723 return process->uid(); // UID
724}
725
726SyscallReturn
727geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
728 ThreadContext *tc)
729{
730 return process->euid(); // UID
731}
732
733SyscallReturn
734getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
735 ThreadContext *tc)
736{
737 return process->gid();
738}
739
740SyscallReturn
741getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
742 ThreadContext *tc)
743{
744 return process->egid();
745}
746
747
748SyscallReturn
749cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
750 ThreadContext *tc)
751{
752 int index = 0;
753 IntReg flags = process->getSyscallArg(tc, index);
754 IntReg newStack = process->getSyscallArg(tc, index);
755
756 DPRINTF(SyscallVerbose, "In sys_clone:\n");
757 DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
758 DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
759
760
761 if (flags != 0x10f00) {
762 warn("This sys_clone implementation assumes flags "
763 "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
764 "(0x10f00), and may not work correctly with given flags "
765 "0x%llx\n", flags);
766 }
767
768 ThreadContext* ctc; // child thread context
769 if ( ( ctc = process->findFreeContext() ) != NULL ) {
770 DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
771
772 ctc->clearArchRegs();
773
774 // Arch-specific cloning code
775 #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
776 // Cloning the misc. regs for these archs is enough
777 TheISA::copyMiscRegs(tc, ctc);
778 #elif THE_ISA == SPARC_ISA
779 TheISA::copyRegs(tc, ctc);
780
781 // TODO: Explain what this code actually does :-)
782 ctc->setIntReg(NumIntArchRegs + 6, 0);
783 ctc->setIntReg(NumIntArchRegs + 4, 0);
784 ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
785 ctc->setIntReg(NumIntArchRegs + 5, NWindows);
786 ctc->setMiscReg(MISCREG_CWP, 0);
787 ctc->setIntReg(NumIntArchRegs + 7, 0);
788 ctc->setMiscRegNoEffect(MISCREG_TL, 0);
789 ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
790
791 for (int y = 8; y < 32; y++)
792 ctc->setIntReg(y, tc->readIntReg(y));
793 #else
794 fatal("sys_clone is not implemented for this ISA\n");
795 #endif
796
797 // Set up stack register
798 ctc->setIntReg(TheISA::StackPointerReg, newStack);
799
800 // Set up syscall return values in parent and child
801 ctc->setIntReg(ReturnValueReg, 0); // return value, child
802
803 // Alpha needs SyscallSuccessReg=0 in child
804 #if THE_ISA == ALPHA_ISA
805 ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
806 #endif
807
808 // In SPARC/Linux, clone returns 0 on pseudo-return register if
809 // parent, non-zero if child
810 #if THE_ISA == SPARC_ISA
811 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
812 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
813 #endif
814
815 ctc->setPC(tc->readNextPC());
816 ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
817 ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst));
818
819 ctc->activate();
820
821 // Should return nonzero child TID in parent's syscall return register,
822 // but for our pthread library any non-zero value will work
823 return 1;
824 } else {
825 fatal("Called sys_clone, but no unallocated thread contexts found!\n");
826 return 0;
827 }
828}
829