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