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