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