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