syscall_emul.cc revision 10931:42d846318962
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->sim_fd(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->reset_fd_entry(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->sim_fd(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->sim_fd(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->sim_fd(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->sim_fd(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    }
369    else {
370        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
371            result = strlen((char *)buf.bufferPtr());
372        }
373        else {
374            result = -1;
375        }
376    }
377
378    buf.copyOut(tc->getMemProxy());
379
380    return (result == -1) ? -errno : result;
381}
382
383/// Target open() handler.
384SyscallReturn
385readlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
386         ThreadContext *tc)
387{
388    return readlinkFunc(desc, callnum, process, tc, 0);
389}
390
391SyscallReturn
392readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
393        int index)
394{
395    string path;
396
397    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
398        return -EFAULT;
399
400    // Adjust path for current working directory
401    path = p->fullPath(path);
402
403    Addr bufPtr = p->getSyscallArg(tc, index);
404    size_t bufsiz = p->getSyscallArg(tc, index);
405
406    BufferArg buf(bufPtr, bufsiz);
407
408    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
409
410    buf.copyOut(tc->getMemProxy());
411
412    return (result == -1) ? -errno : result;
413}
414
415SyscallReturn
416unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
417{
418    return unlinkHelper(desc, num, p, tc, 0);
419}
420
421SyscallReturn
422unlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
423           int index)
424{
425    string path;
426
427    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
428        return -EFAULT;
429
430    // Adjust path for current working directory
431    path = p->fullPath(path);
432
433    int result = unlink(path.c_str());
434    return (result == -1) ? -errno : result;
435}
436
437
438SyscallReturn
439mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
440{
441    string path;
442
443    int index = 0;
444    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
445        return -EFAULT;
446
447    // Adjust path for current working directory
448    path = p->fullPath(path);
449
450    mode_t mode = p->getSyscallArg(tc, index);
451
452    int result = mkdir(path.c_str(), mode);
453    return (result == -1) ? -errno : result;
454}
455
456SyscallReturn
457renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
458{
459    string old_name;
460
461    int index = 0;
462    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
463        return -EFAULT;
464
465    string new_name;
466
467    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
468        return -EFAULT;
469
470    // Adjust path for current working directory
471    old_name = p->fullPath(old_name);
472    new_name = p->fullPath(new_name);
473
474    int64_t result = rename(old_name.c_str(), new_name.c_str());
475    return (result == -1) ? -errno : result;
476}
477
478SyscallReturn
479truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
480{
481    string path;
482
483    int index = 0;
484    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
485        return -EFAULT;
486
487    off_t length = p->getSyscallArg(tc, index);
488
489    // Adjust path for current working directory
490    path = p->fullPath(path);
491
492    int result = truncate(path.c_str(), length);
493    return (result == -1) ? -errno : result;
494}
495
496SyscallReturn
497ftruncateFunc(SyscallDesc *desc, int num,
498              LiveProcess *process, ThreadContext *tc)
499{
500    int index = 0;
501    int tgt_fd = process->getSyscallArg(tc, index);
502    off_t length = process->getSyscallArg(tc, index);
503
504    int sim_fd = process->sim_fd(tgt_fd);
505    if (sim_fd < 0)
506        return -EBADF;
507
508    int result = ftruncate(sim_fd, length);
509    return (result == -1) ? -errno : result;
510}
511
512SyscallReturn
513truncate64Func(SyscallDesc *desc, int num,
514                LiveProcess *process, ThreadContext *tc)
515{
516    int index = 0;
517    string path;
518
519    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
520       return -EFAULT;
521
522    int64_t length = process->getSyscallArg(tc, index, 64);
523
524    // Adjust path for current working directory
525    path = process->fullPath(path);
526
527#if NO_STAT64
528    int result = truncate(path.c_str(), length);
529#else
530    int result = truncate64(path.c_str(), length);
531#endif
532    return (result == -1) ? -errno : result;
533}
534
535SyscallReturn
536ftruncate64Func(SyscallDesc *desc, int num,
537                LiveProcess *process, ThreadContext *tc)
538{
539    int index = 0;
540    int tgt_fd = process->getSyscallArg(tc, index);
541    int64_t length = process->getSyscallArg(tc, index, 64);
542
543    int sim_fd = process->sim_fd(tgt_fd);
544    if (sim_fd < 0)
545        return -EBADF;
546
547#if NO_STAT64
548    int result = ftruncate(sim_fd, length);
549#else
550    int result = ftruncate64(sim_fd, length);
551#endif
552    return (result == -1) ? -errno : result;
553}
554
555SyscallReturn
556umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
557{
558    // Letting the simulated program change the simulator's umask seems like
559    // a bad idea.  Compromise by just returning the current umask but not
560    // changing anything.
561    mode_t oldMask = umask(0);
562    umask(oldMask);
563    return (int)oldMask;
564}
565
566SyscallReturn
567chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
568{
569    string path;
570
571    int index = 0;
572    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
573        return -EFAULT;
574
575    /* XXX endianess */
576    uint32_t owner = p->getSyscallArg(tc, index);
577    uid_t hostOwner = owner;
578    uint32_t group = p->getSyscallArg(tc, index);
579    gid_t hostGroup = group;
580
581    // Adjust path for current working directory
582    path = p->fullPath(path);
583
584    int result = chown(path.c_str(), hostOwner, hostGroup);
585    return (result == -1) ? -errno : result;
586}
587
588SyscallReturn
589fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
590{
591    int index = 0;
592    int tgt_fd = process->getSyscallArg(tc, index);
593
594    int sim_fd = process->sim_fd(tgt_fd);
595    if (sim_fd < 0)
596        return -EBADF;
597
598    /* XXX endianess */
599    uint32_t owner = process->getSyscallArg(tc, index);
600    uid_t hostOwner = owner;
601    uint32_t group = process->getSyscallArg(tc, index);
602    gid_t hostGroup = group;
603
604    int result = fchown(sim_fd, hostOwner, hostGroup);
605    return (result == -1) ? -errno : result;
606}
607
608
609SyscallReturn
610dupFunc(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->sim_fd(tgt_fd);
616    if (sim_fd < 0)
617        return -EBADF;
618
619    FDEntry *fde = process->get_fd_entry(tgt_fd);
620
621    int result = dup(sim_fd);
622    return (result == -1) ? -errno :
623        process->alloc_fd(result, fde->filename, fde->flags, fde->mode, false);
624}
625
626
627SyscallReturn
628fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
629          ThreadContext *tc)
630{
631    int index = 0;
632    int tgt_fd = process->getSyscallArg(tc, index);
633
634    int sim_fd = process->sim_fd(tgt_fd);
635    if (sim_fd < 0)
636        return -EBADF;
637
638    int cmd = process->getSyscallArg(tc, index);
639    switch (cmd) {
640      case 0: // F_DUPFD
641        // if we really wanted to support this, we'd need to do it
642        // in the target fd space.
643        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
644        return -EMFILE;
645
646      case 1: // F_GETFD (get close-on-exec flag)
647      case 2: // F_SETFD (set close-on-exec flag)
648        return 0;
649
650      case 3: // F_GETFL (get file flags)
651      case 4: // F_SETFL (set file flags)
652        // not sure if this is totally valid, but we'll pass it through
653        // to the underlying OS
654        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
655        return fcntl(sim_fd, cmd);
656        // return 0;
657
658      case 7: // F_GETLK  (get lock)
659      case 8: // F_SETLK  (set lock)
660      case 9: // F_SETLKW (set lock and wait)
661        // don't mess with file locking... just act like it's OK
662        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
663        return 0;
664
665      default:
666        warn("Unknown fcntl command %d\n", cmd);
667        return 0;
668    }
669}
670
671SyscallReturn
672fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
673            ThreadContext *tc)
674{
675    int index = 0;
676    int tgt_fd = process->getSyscallArg(tc, index);
677
678    int sim_fd = process->sim_fd(tgt_fd);
679    if (sim_fd < 0)
680        return -EBADF;
681
682    int cmd = process->getSyscallArg(tc, index);
683    switch (cmd) {
684      case 33: //F_GETLK64
685        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
686        return -EMFILE;
687
688      case 34: // F_SETLK64
689      case 35: // F_SETLKW64
690        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
691             tgt_fd);
692        return -EMFILE;
693
694      default:
695        // not sure if this is totally valid, but we'll pass it through
696        // to the underlying OS
697        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
698        return fcntl(sim_fd, cmd);
699        // return 0;
700    }
701}
702
703SyscallReturn
704pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
705         ThreadContext *tc)
706{
707    int fds[2], sim_fds[2];
708    int pipe_retval = pipe(fds);
709
710    if (pipe_retval < 0) {
711        // error
712        return pipe_retval;
713    }
714
715    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
716    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
717
718    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
719    // Alpha Linux convention for pipe() is that fd[0] is returned as
720    // the return value of the function, and fd[1] is returned in r20.
721    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
722    return sim_fds[0];
723}
724
725
726SyscallReturn
727getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
728           ThreadContext *tc)
729{
730    // Make up a PID.  There's no interprocess communication in
731    // fake_syscall mode, so there's no way for a process to know it's
732    // not getting a unique value.
733
734    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
735    return process->pid();
736}
737
738
739SyscallReturn
740getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
741           ThreadContext *tc)
742{
743    // Make up a UID and EUID... it shouldn't matter, and we want the
744    // simulation to be deterministic.
745
746    // EUID goes in r20.
747    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
748    return process->uid();              // UID
749}
750
751
752SyscallReturn
753getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
754           ThreadContext *tc)
755{
756    // Get current group ID.  EGID goes in r20.
757    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
758    return process->gid();
759}
760
761
762SyscallReturn
763setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
764           ThreadContext *tc)
765{
766    // can't fathom why a benchmark would call this.
767    int index = 0;
768    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
769    return 0;
770}
771
772SyscallReturn
773getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
774           ThreadContext *tc)
775{
776    // Make up a PID.  There's no interprocess communication in
777    // fake_syscall mode, so there's no way for a process to know it's
778    // not getting a unique value.
779
780    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
781    return process->pid();
782}
783
784SyscallReturn
785getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
786           ThreadContext *tc)
787{
788    return process->ppid();
789}
790
791SyscallReturn
792getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
793           ThreadContext *tc)
794{
795    return process->uid();              // UID
796}
797
798SyscallReturn
799geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
800           ThreadContext *tc)
801{
802    return process->euid();             // UID
803}
804
805SyscallReturn
806getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
807           ThreadContext *tc)
808{
809    return process->gid();
810}
811
812SyscallReturn
813getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
814           ThreadContext *tc)
815{
816    return process->egid();
817}
818
819
820SyscallReturn
821cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
822           ThreadContext *tc)
823{
824    int index = 0;
825    IntReg flags = process->getSyscallArg(tc, index);
826    IntReg newStack = process->getSyscallArg(tc, index);
827
828    DPRINTF(SyscallVerbose, "In sys_clone:\n");
829    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
830    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
831
832
833    if (flags != 0x10f00) {
834        warn("This sys_clone implementation assumes flags "
835             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
836             "(0x10f00), and may not work correctly with given flags "
837             "0x%llx\n", flags);
838    }
839
840    ThreadContext* ctc; // child thread context
841    if ( ( ctc = process->findFreeContext() ) != NULL ) {
842        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
843
844        ctc->clearArchRegs();
845
846        // Arch-specific cloning code
847        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
848            // Cloning the misc. regs for these archs is enough
849            TheISA::copyMiscRegs(tc, ctc);
850        #elif THE_ISA == SPARC_ISA
851            TheISA::copyRegs(tc, ctc);
852
853            // TODO: Explain what this code actually does :-)
854            ctc->setIntReg(NumIntArchRegs + 6, 0);
855            ctc->setIntReg(NumIntArchRegs + 4, 0);
856            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
857            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
858            ctc->setMiscReg(MISCREG_CWP, 0);
859            ctc->setIntReg(NumIntArchRegs + 7, 0);
860            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
861            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
862
863            for (int y = 8; y < 32; y++)
864                ctc->setIntReg(y, tc->readIntReg(y));
865        #elif THE_ISA == ARM_ISA
866            TheISA::copyRegs(tc, ctc);
867        #else
868            fatal("sys_clone is not implemented for this ISA\n");
869        #endif
870
871        // Set up stack register
872        ctc->setIntReg(TheISA::StackPointerReg, newStack);
873
874        // Set up syscall return values in parent and child
875        ctc->setIntReg(ReturnValueReg, 0); // return value, child
876
877        // Alpha needs SyscallSuccessReg=0 in child
878        #if THE_ISA == ALPHA_ISA
879            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
880        #endif
881
882        // In SPARC/Linux, clone returns 0 on pseudo-return register if
883        // parent, non-zero if child
884        #if THE_ISA == SPARC_ISA
885            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
886            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
887        #endif
888
889        ctc->pcState(tc->nextInstAddr());
890
891        ctc->activate();
892
893        // Should return nonzero child TID in parent's syscall return register,
894        // but for our pthread library any non-zero value will work
895        return 1;
896    } else {
897        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
898        return 0;
899    }
900}
901
902SyscallReturn
903accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
904        int index)
905{
906    string path;
907    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
908        return -EFAULT;
909
910    // Adjust path for current working directory
911    path = p->fullPath(path);
912
913    mode_t mode = p->getSyscallArg(tc, index);
914
915    int result = access(path.c_str(), mode);
916    return (result == -1) ? -errno : result;
917}
918
919SyscallReturn
920accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
921{
922    return accessFunc(desc, callnum, p, tc, 0);
923}
924
925