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