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