syscall_emul.cc revision 5543
1360SN/A/*
21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
3360SN/A * All rights reserved.
4360SN/A *
5360SN/A * Redistribution and use in source and binary forms, with or without
6360SN/A * modification, are permitted provided that the following conditions are
7360SN/A * met: redistributions of source code must retain the above copyright
8360SN/A * notice, this list of conditions and the following disclaimer;
9360SN/A * redistributions in binary form must reproduce the above copyright
10360SN/A * notice, this list of conditions and the following disclaimer in the
11360SN/A * documentation and/or other materials provided with the distribution;
12360SN/A * neither the name of the copyright holders nor the names of its
13360SN/A * contributors may be used to endorse or promote products derived from
14360SN/A * this software without specific prior written permission.
15360SN/A *
16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
30360SN/A */
31360SN/A
322093SN/A#include <fcntl.h>
33360SN/A#include <unistd.h>
34360SN/A
35360SN/A#include <string>
36360SN/A#include <iostream>
37360SN/A
38360SN/A#include "sim/syscall_emul.hh"
392474SN/A#include "base/chunk_generator.hh"
40360SN/A#include "base/trace.hh"
412680Sktlim@umich.edu#include "cpu/thread_context.hh"
421717SN/A#include "cpu/base.hh"
432474SN/A#include "mem/page_table.hh"
44360SN/A#include "sim/process.hh"
45360SN/A
462667Sstever@eecs.umich.edu#include "sim/sim_exit.hh"
47360SN/A
48360SN/Ausing namespace std;
492107SN/Ausing namespace TheISA;
50360SN/A
51360SN/Avoid
523114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
53360SN/A{
542495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
552680Sktlim@umich.edu             curTick,tc->getCpuPtr()->name(), name,
562680Sktlim@umich.edu             tc->getSyscallArg(0),tc->getSyscallArg(1),
572680Sktlim@umich.edu             tc->getSyscallArg(2),tc->getSyscallArg(3));
58360SN/A
592680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
60360SN/A
612495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
622680Sktlim@umich.edu             curTick,tc->getCpuPtr()->name(), name, retval.value());
63360SN/A
641450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
652680Sktlim@umich.edu        tc->setSyscallReturn(retval);
66360SN/A}
67360SN/A
68360SN/A
691450SN/ASyscallReturn
703114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
712680Sktlim@umich.edu                  ThreadContext *tc)
72360SN/A{
731969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
742484SN/A
752484SN/A    return 1;
76360SN/A}
77360SN/A
78360SN/A
791450SN/ASyscallReturn
803114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
812680Sktlim@umich.edu           ThreadContext *tc)
82360SN/A{
831969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
842680Sktlim@umich.edu         tc->getSyscallArg(0), tc->getSyscallArg(1));
85360SN/A
861458SN/A    return 0;
87360SN/A}
88360SN/A
89360SN/A
901450SN/ASyscallReturn
913114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
922680Sktlim@umich.edu         ThreadContext *tc)
93360SN/A{
942834Sksewell@umich.edu    if (tc->exit()) {
952834Sksewell@umich.edu        exitSimLoop("target called exit()", tc->getSyscallArg(0) & 0xff);
962834Sksewell@umich.edu    }
97360SN/A
981458SN/A    return 1;
99360SN/A}
100360SN/A
101360SN/A
1021450SN/ASyscallReturn
1033114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
104360SN/A{
1052107SN/A    return (int)VMPageSize;
106360SN/A}
107360SN/A
108360SN/A
1091450SN/ASyscallReturn
1103114Sgblack@eecs.umich.eduobreakFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
111360SN/A{
1122474SN/A    Addr junk;
1132474SN/A
114360SN/A    // change brk addr to first arg
1152680Sktlim@umich.edu    Addr new_brk = tc->getSyscallArg(0);
1162474SN/A    if (new_brk != 0) {
1172474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1182474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1192474SN/A            if (!p->pTable->translate(gen.addr(), junk))
1202474SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1212474SN/A                                    VMPageSize);
1222474SN/A        }
1232474SN/A        p->brk_point = new_brk;
1241450SN/A    }
1251458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1261458SN/A    return p->brk_point;
127360SN/A}
128360SN/A
129360SN/A
1301450SN/ASyscallReturn
1313114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
132360SN/A{
1332680Sktlim@umich.edu    int target_fd = tc->getSyscallArg(0);
1341970SN/A    int status = close(p->sim_fd(target_fd));
1351970SN/A    if (status >= 0)
1361970SN/A        p->free_fd(target_fd);
1371970SN/A    return status;
138360SN/A}
139360SN/A
140360SN/A
1411450SN/ASyscallReturn
1423114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
143360SN/A{
1442680Sktlim@umich.edu    int fd = p->sim_fd(tc->getSyscallArg(0));
1452680Sktlim@umich.edu    int nbytes = tc->getSyscallArg(2);
1462680Sktlim@umich.edu    BufferArg bufArg(tc->getSyscallArg(1), nbytes);
147360SN/A
148360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
149360SN/A
150360SN/A    if (bytes_read != -1)
1512680Sktlim@umich.edu        bufArg.copyOut(tc->getMemPort());
152360SN/A
1531458SN/A    return bytes_read;
154360SN/A}
155360SN/A
1561450SN/ASyscallReturn
1573114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
158360SN/A{
1592680Sktlim@umich.edu    int fd = p->sim_fd(tc->getSyscallArg(0));
1602680Sktlim@umich.edu    int nbytes = tc->getSyscallArg(2);
1612680Sktlim@umich.edu    BufferArg bufArg(tc->getSyscallArg(1), nbytes);
162360SN/A
1632680Sktlim@umich.edu    bufArg.copyIn(tc->getMemPort());
164360SN/A
165360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
166360SN/A
167360SN/A    fsync(fd);
168360SN/A
1691458SN/A    return bytes_written;
170360SN/A}
171360SN/A
172360SN/A
1731450SN/ASyscallReturn
1743114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
175360SN/A{
1762680Sktlim@umich.edu    int fd = p->sim_fd(tc->getSyscallArg(0));
1772680Sktlim@umich.edu    uint64_t offs = tc->getSyscallArg(1);
1782680Sktlim@umich.edu    int whence = tc->getSyscallArg(2);
179360SN/A
180360SN/A    off_t result = lseek(fd, offs, whence);
181360SN/A
1821458SN/A    return (result == (off_t)-1) ? -errno : result;
183360SN/A}
184360SN/A
185360SN/A
1861450SN/ASyscallReturn
1874118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1884118Sgblack@eecs.umich.edu{
1894118Sgblack@eecs.umich.edu    int fd = p->sim_fd(tc->getSyscallArg(0));
1904118Sgblack@eecs.umich.edu    uint64_t offset_high = tc->getSyscallArg(1);
1914118Sgblack@eecs.umich.edu    uint32_t offset_low = tc->getSyscallArg(2);
1924118Sgblack@eecs.umich.edu    Addr result_ptr = tc->getSyscallArg(3);
1934118Sgblack@eecs.umich.edu    int whence = tc->getSyscallArg(4);
1944118Sgblack@eecs.umich.edu
1954118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
1964118Sgblack@eecs.umich.edu
1974118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
1984118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
1994118Sgblack@eecs.umich.edu
2004118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2014118Sgblack@eecs.umich.edu        //The seek failed.
2024118Sgblack@eecs.umich.edu        return -errno;
2034118Sgblack@eecs.umich.edu    } else {
2044118Sgblack@eecs.umich.edu        //The seek succeeded.
2054118Sgblack@eecs.umich.edu        //Copy "result" to "result_ptr"
2064118Sgblack@eecs.umich.edu        //XXX We'll assume that the size of loff_t is 64 bits on the
2074118Sgblack@eecs.umich.edu        //target platform
2084118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2094118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2104118Sgblack@eecs.umich.edu        result_buf.copyOut(tc->getMemPort());
2114118Sgblack@eecs.umich.edu        return 0;
2124118Sgblack@eecs.umich.edu    }
2134118Sgblack@eecs.umich.edu
2144118Sgblack@eecs.umich.edu
2154118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2164118Sgblack@eecs.umich.edu}
2174118Sgblack@eecs.umich.edu
2184118Sgblack@eecs.umich.edu
2194118Sgblack@eecs.umich.eduSyscallReturn
2203114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
221360SN/A{
222360SN/A    // given that we don't really implement mmap, munmap is really easy
2231458SN/A    return 0;
224360SN/A}
225360SN/A
226360SN/A
227360SN/Aconst char *hostname = "m5.eecs.umich.edu";
228360SN/A
2291450SN/ASyscallReturn
2303114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
231360SN/A{
2322680Sktlim@umich.edu    int name_len = tc->getSyscallArg(1);
2332680Sktlim@umich.edu    BufferArg name(tc->getSyscallArg(0), name_len);
234360SN/A
235360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
236360SN/A
2372680Sktlim@umich.edu    name.copyOut(tc->getMemPort());
238360SN/A
2391458SN/A    return 0;
240360SN/A}
241360SN/A
2421450SN/ASyscallReturn
2435513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2445513SMichael.Adler@intel.com{
2455513SMichael.Adler@intel.com    int result = 0;
2465513SMichael.Adler@intel.com    unsigned long size = tc->getSyscallArg(1);
2475513SMichael.Adler@intel.com    BufferArg buf(tc->getSyscallArg(0), size);
2485513SMichael.Adler@intel.com
2495513SMichael.Adler@intel.com    // Is current working directory defined?
2505513SMichael.Adler@intel.com    string cwd = p->getcwd();
2515513SMichael.Adler@intel.com    if (!cwd.empty()) {
2525513SMichael.Adler@intel.com        if (cwd.length() >= size) {
2535513SMichael.Adler@intel.com            // Buffer too small
2545513SMichael.Adler@intel.com            return -ERANGE;
2555513SMichael.Adler@intel.com        }
2565513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
2575513SMichael.Adler@intel.com        result = cwd.length();
2585513SMichael.Adler@intel.com    }
2595513SMichael.Adler@intel.com    else {
2605513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
2615513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
2625513SMichael.Adler@intel.com        }
2635513SMichael.Adler@intel.com        else {
2645513SMichael.Adler@intel.com            result = -1;
2655513SMichael.Adler@intel.com        }
2665513SMichael.Adler@intel.com    }
2675513SMichael.Adler@intel.com
2685513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
2695513SMichael.Adler@intel.com
2705513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
2715513SMichael.Adler@intel.com}
2725513SMichael.Adler@intel.com
2735513SMichael.Adler@intel.com
2745513SMichael.Adler@intel.comSyscallReturn
2755513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2765513SMichael.Adler@intel.com{
2775513SMichael.Adler@intel.com    string path;
2785513SMichael.Adler@intel.com
2795513SMichael.Adler@intel.com    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
2805513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
2815513SMichael.Adler@intel.com
2825513SMichael.Adler@intel.com    // Adjust path for current working directory
2835513SMichael.Adler@intel.com    path = p->fullPath(path);
2845513SMichael.Adler@intel.com
2855513SMichael.Adler@intel.com    size_t bufsiz = tc->getSyscallArg(2);
2865513SMichael.Adler@intel.com    BufferArg buf(tc->getSyscallArg(1), bufsiz);
2875513SMichael.Adler@intel.com
2885513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
2895513SMichael.Adler@intel.com
2905513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
2915513SMichael.Adler@intel.com
2925513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
2935513SMichael.Adler@intel.com}
2945513SMichael.Adler@intel.com
2955513SMichael.Adler@intel.comSyscallReturn
2963114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
297511SN/A{
2981706SN/A    string path;
299360SN/A
3002680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
3011450SN/A        return (TheISA::IntReg)-EFAULT;
302511SN/A
3033669Sbinkertn@umich.edu    // Adjust path for current working directory
3043669Sbinkertn@umich.edu    path = p->fullPath(path);
3053669Sbinkertn@umich.edu
306511SN/A    int result = unlink(path.c_str());
3071458SN/A    return (result == -1) ? -errno : result;
308511SN/A}
309511SN/A
3105513SMichael.Adler@intel.com
3115513SMichael.Adler@intel.comSyscallReturn
3125513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3135513SMichael.Adler@intel.com{
3145513SMichael.Adler@intel.com    string path;
3155513SMichael.Adler@intel.com
3165513SMichael.Adler@intel.com    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
3175513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3185513SMichael.Adler@intel.com
3195513SMichael.Adler@intel.com    // Adjust path for current working directory
3205513SMichael.Adler@intel.com    path = p->fullPath(path);
3215513SMichael.Adler@intel.com
3225513SMichael.Adler@intel.com    mode_t mode = tc->getSyscallArg(1);
3235513SMichael.Adler@intel.com
3245513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
3255513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3265513SMichael.Adler@intel.com}
3275513SMichael.Adler@intel.com
3281450SN/ASyscallReturn
3293114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
330511SN/A{
3311706SN/A    string old_name;
332511SN/A
3332680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(old_name, tc->getSyscallArg(0)))
3341458SN/A        return -EFAULT;
335511SN/A
3361706SN/A    string new_name;
337511SN/A
3382680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1)))
3391458SN/A        return -EFAULT;
340511SN/A
3413669Sbinkertn@umich.edu    // Adjust path for current working directory
3423669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
3433669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
3443669Sbinkertn@umich.edu
3451706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
3461458SN/A    return (result == -1) ? -errno : result;
347511SN/A}
348511SN/A
3491706SN/ASyscallReturn
3503114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3511706SN/A{
3521706SN/A    string path;
3531706SN/A
3542680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
3551706SN/A        return -EFAULT;
3561706SN/A
3572680Sktlim@umich.edu    off_t length = tc->getSyscallArg(1);
3581706SN/A
3593669Sbinkertn@umich.edu    // Adjust path for current working directory
3603669Sbinkertn@umich.edu    path = p->fullPath(path);
3613669Sbinkertn@umich.edu
3621706SN/A    int result = truncate(path.c_str(), length);
3631706SN/A    return (result == -1) ? -errno : result;
3641706SN/A}
3651706SN/A
3661706SN/ASyscallReturn
3673114Sgblack@eecs.umich.eduftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
3681706SN/A{
3692680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
3701706SN/A
3711706SN/A    if (fd < 0)
3721706SN/A        return -EBADF;
3731706SN/A
3742680Sktlim@umich.edu    off_t length = tc->getSyscallArg(1);
3751706SN/A
3761706SN/A    int result = ftruncate(fd, length);
3771706SN/A    return (result == -1) ? -errno : result;
3781706SN/A}
3791999SN/A
3801999SN/ASyscallReturn
3815513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
3825513SMichael.Adler@intel.com{
3835513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
3845513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
3855513SMichael.Adler@intel.com    // changing anything.
3865513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
3875513SMichael.Adler@intel.com    umask(oldMask);
3885521Snate@binkert.org    return (int)oldMask;
3895513SMichael.Adler@intel.com}
3905513SMichael.Adler@intel.com
3915513SMichael.Adler@intel.comSyscallReturn
3923114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3931999SN/A{
3941999SN/A    string path;
3951999SN/A
3962680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
3971999SN/A        return -EFAULT;
3981999SN/A
3991999SN/A    /* XXX endianess */
4002680Sktlim@umich.edu    uint32_t owner = tc->getSyscallArg(1);
4011999SN/A    uid_t hostOwner = owner;
4022680Sktlim@umich.edu    uint32_t group = tc->getSyscallArg(2);
4031999SN/A    gid_t hostGroup = group;
4041999SN/A
4053669Sbinkertn@umich.edu    // Adjust path for current working directory
4063669Sbinkertn@umich.edu    path = p->fullPath(path);
4073669Sbinkertn@umich.edu
4081999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
4091999SN/A    return (result == -1) ? -errno : result;
4101999SN/A}
4111999SN/A
4121999SN/ASyscallReturn
4133114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
4141999SN/A{
4152680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
4161999SN/A
4171999SN/A    if (fd < 0)
4181999SN/A        return -EBADF;
4191999SN/A
4201999SN/A    /* XXX endianess */
4212680Sktlim@umich.edu    uint32_t owner = tc->getSyscallArg(1);
4221999SN/A    uid_t hostOwner = owner;
4232680Sktlim@umich.edu    uint32_t group = tc->getSyscallArg(2);
4241999SN/A    gid_t hostGroup = group;
4251999SN/A
4261999SN/A    int result = fchown(fd, hostOwner, hostGroup);
4271999SN/A    return (result == -1) ? -errno : result;
4281999SN/A}
4292093SN/A
4302093SN/A
4312093SN/ASyscallReturn
4323114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
4333079Sstever@eecs.umich.edu{
4343079Sstever@eecs.umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
4353079Sstever@eecs.umich.edu    if (fd < 0)
4363079Sstever@eecs.umich.edu        return -EBADF;
4373079Sstever@eecs.umich.edu
4385282Srstrong@cs.ucsd.edu    Process::FdMap *fdo = process->sim_fd_obj(tc->getSyscallArg(0));
4395282Srstrong@cs.ucsd.edu
4403079Sstever@eecs.umich.edu    int result = dup(fd);
4415282Srstrong@cs.ucsd.edu    return (result == -1) ? -errno : process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
4423079Sstever@eecs.umich.edu}
4433079Sstever@eecs.umich.edu
4443079Sstever@eecs.umich.edu
4453079Sstever@eecs.umich.eduSyscallReturn
4463114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
4472680Sktlim@umich.edu          ThreadContext *tc)
4482093SN/A{
4492680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4502093SN/A
4512093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
4522093SN/A        return -EBADF;
4532093SN/A
4542680Sktlim@umich.edu    int cmd = tc->getSyscallArg(1);
4552093SN/A    switch (cmd) {
4562093SN/A      case 0: // F_DUPFD
4572093SN/A        // if we really wanted to support this, we'd need to do it
4582093SN/A        // in the target fd space.
4592093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
4602093SN/A        return -EMFILE;
4612093SN/A
4622093SN/A      case 1: // F_GETFD (get close-on-exec flag)
4632093SN/A      case 2: // F_SETFD (set close-on-exec flag)
4642093SN/A        return 0;
4652093SN/A
4662093SN/A      case 3: // F_GETFL (get file flags)
4672093SN/A      case 4: // F_SETFL (set file flags)
4682093SN/A        // not sure if this is totally valid, but we'll pass it through
4692093SN/A        // to the underlying OS
4702093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
4712093SN/A        return fcntl(process->sim_fd(fd), cmd);
4722093SN/A        // return 0;
4732093SN/A
4742093SN/A      case 7: // F_GETLK  (get lock)
4752093SN/A      case 8: // F_SETLK  (set lock)
4762093SN/A      case 9: // F_SETLKW (set lock and wait)
4772093SN/A        // don't mess with file locking... just act like it's OK
4782093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
4792093SN/A        return 0;
4802093SN/A
4812093SN/A      default:
4822093SN/A        warn("Unknown fcntl command %d\n", cmd);
4832093SN/A        return 0;
4842093SN/A    }
4852093SN/A}
4862093SN/A
4872238SN/ASyscallReturn
4883114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
4892687Sksewell@umich.edu            ThreadContext *tc)
4902687Sksewell@umich.edu{
4912687Sksewell@umich.edu    int fd = tc->getSyscallArg(0);
4922687Sksewell@umich.edu
4932687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
4942687Sksewell@umich.edu        return -EBADF;
4952687Sksewell@umich.edu
4962687Sksewell@umich.edu    int cmd = tc->getSyscallArg(1);
4972687Sksewell@umich.edu    switch (cmd) {
4982687Sksewell@umich.edu      case 33: //F_GETLK64
4992687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
5002687Sksewell@umich.edu        return -EMFILE;
5012687Sksewell@umich.edu
5022687Sksewell@umich.edu      case 34: // F_SETLK64
5032687Sksewell@umich.edu      case 35: // F_SETLKW64
5042687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
5052687Sksewell@umich.edu        return -EMFILE;
5062687Sksewell@umich.edu
5072687Sksewell@umich.edu      default:
5082687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
5092687Sksewell@umich.edu        // to the underlying OS
5102687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
5112687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
5122687Sksewell@umich.edu        // return 0;
5132687Sksewell@umich.edu    }
5142687Sksewell@umich.edu}
5152687Sksewell@umich.edu
5162687Sksewell@umich.eduSyscallReturn
5173114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5182680Sktlim@umich.edu         ThreadContext *tc)
5192238SN/A{
5202238SN/A    int fds[2], sim_fds[2];
5212238SN/A    int pipe_retval = pipe(fds);
5222093SN/A
5232238SN/A    if (pipe_retval < 0) {
5242238SN/A        // error
5252238SN/A        return pipe_retval;
5262238SN/A    }
5272238SN/A
5285282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
5295282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
5302238SN/A
5315282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
5322238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
5332238SN/A    // the return value of the function, and fd[1] is returned in r20.
5342680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
5352238SN/A    return sim_fds[0];
5362238SN/A}
5372238SN/A
5382238SN/A
5392238SN/ASyscallReturn
5403114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5412680Sktlim@umich.edu           ThreadContext *tc)
5422238SN/A{
5432238SN/A    // Make up a PID.  There's no interprocess communication in
5442238SN/A    // fake_syscall mode, so there's no way for a process to know it's
5452238SN/A    // not getting a unique value.
5462238SN/A
5473114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
5483114Sgblack@eecs.umich.edu    return process->pid();
5492238SN/A}
5502238SN/A
5512238SN/A
5522238SN/ASyscallReturn
5533114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5542680Sktlim@umich.edu           ThreadContext *tc)
5552238SN/A{
5562238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
5572238SN/A    // simulation to be deterministic.
5582238SN/A
5592238SN/A    // EUID goes in r20.
5603114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
5615543Ssaidi@eecs.umich.edu    return process->uid();              // UID
5622238SN/A}
5632238SN/A
5642238SN/A
5652238SN/ASyscallReturn
5663114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5672680Sktlim@umich.edu           ThreadContext *tc)
5682238SN/A{
5692238SN/A    // Get current group ID.  EGID goes in r20.
5703114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
5713114Sgblack@eecs.umich.edu    return process->gid();
5722238SN/A}
5732238SN/A
5742238SN/A
5752238SN/ASyscallReturn
5763114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5772680Sktlim@umich.edu           ThreadContext *tc)
5782238SN/A{
5792238SN/A    // can't fathom why a benchmark would call this.
5802680Sktlim@umich.edu    warn("Ignoring call to setuid(%d)\n", tc->getSyscallArg(0));
5812238SN/A    return 0;
5822238SN/A}
5832238SN/A
5842238SN/ASyscallReturn
5853114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5862680Sktlim@umich.edu           ThreadContext *tc)
5872238SN/A{
5882238SN/A    // Make up a PID.  There's no interprocess communication in
5892238SN/A    // fake_syscall mode, so there's no way for a process to know it's
5902238SN/A    // not getting a unique value.
5912238SN/A
5923114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
5933114Sgblack@eecs.umich.edu    return process->pid();
5942238SN/A}
5952238SN/A
5962238SN/ASyscallReturn
5973114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5982680Sktlim@umich.edu           ThreadContext *tc)
5992238SN/A{
6003114Sgblack@eecs.umich.edu    return process->ppid();
6012238SN/A}
6022238SN/A
6032238SN/ASyscallReturn
6043114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6052680Sktlim@umich.edu           ThreadContext *tc)
6062238SN/A{
6075543Ssaidi@eecs.umich.edu    return process->uid();              // UID
6082238SN/A}
6092238SN/A
6102238SN/ASyscallReturn
6113114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6122680Sktlim@umich.edu           ThreadContext *tc)
6132238SN/A{
6145543Ssaidi@eecs.umich.edu    return process->euid();             // UID
6152238SN/A}
6162238SN/A
6172238SN/ASyscallReturn
6183114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6192680Sktlim@umich.edu           ThreadContext *tc)
6202238SN/A{
6213114Sgblack@eecs.umich.edu    return process->gid();
6222238SN/A}
6232238SN/A
6242238SN/ASyscallReturn
6253114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6262680Sktlim@umich.edu           ThreadContext *tc)
6272238SN/A{
6283114Sgblack@eecs.umich.edu    return process->egid();
6292238SN/A}
6302238SN/A
6312238SN/A
632