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