syscall_emul.cc revision 2665
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"
41360SN/A#include "cpu/exec_context.hh"
421717SN/A#include "cpu/base.hh"
432474SN/A#include "mem/page_table.hh"
44360SN/A#include "sim/process.hh"
45360SN/A
46360SN/A#include "sim/sim_events.hh"
47360SN/A
48360SN/Ausing namespace std;
492107SN/Ausing namespace TheISA;
50360SN/A
51360SN/Avoid
52360SN/ASyscallDesc::doSyscall(int callnum, Process *process, ExecContext *xc)
53360SN/A{
542495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
552495SN/A             curTick,xc->getCpuPtr()->name(), name,
562495SN/A             xc->getSyscallArg(0),xc->getSyscallArg(1),
572495SN/A             xc->getSyscallArg(2),xc->getSyscallArg(3));
58360SN/A
591450SN/A    SyscallReturn retval = (*funcPtr)(this, callnum, process, xc);
60360SN/A
612495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
622495SN/A             curTick,xc->getCpuPtr()->name(), name, retval.value());
63360SN/A
641450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
65360SN/A        xc->setSyscallReturn(retval);
66360SN/A}
67360SN/A
68360SN/A
691450SN/ASyscallReturn
70360SN/AunimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
71360SN/A                  ExecContext *xc)
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
80360SN/AignoreFunc(SyscallDesc *desc, int callnum, Process *process,
81360SN/A           ExecContext *xc)
82360SN/A{
831969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
841969SN/A         xc->getSyscallArg(0), xc->getSyscallArg(1));
85360SN/A
861458SN/A    return 0;
87360SN/A}
88360SN/A
89360SN/A
901450SN/ASyscallReturn
91360SN/AexitFunc(SyscallDesc *desc, int callnum, Process *process,
92360SN/A         ExecContext *xc)
93360SN/A{
942442SN/A    new SimExitEvent("target called exit()", xc->getSyscallArg(0) & 0xff);
95360SN/A
961458SN/A    return 1;
97360SN/A}
98360SN/A
99360SN/A
1001450SN/ASyscallReturn
101360SN/AgetpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
102360SN/A{
1032107SN/A    return (int)VMPageSize;
104360SN/A}
105360SN/A
106360SN/A
1071450SN/ASyscallReturn
108360SN/AobreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
109360SN/A{
1102474SN/A    Addr junk;
1112474SN/A
112360SN/A    // change brk addr to first arg
113543SN/A    Addr new_brk = xc->getSyscallArg(0);
1142474SN/A    if (new_brk != 0) {
1152474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1162474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1172474SN/A            if (!p->pTable->translate(gen.addr(), junk))
1182474SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1192474SN/A                                    VMPageSize);
1202474SN/A        }
1212474SN/A        p->brk_point = new_brk;
1221450SN/A    }
1231458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1241458SN/A    return p->brk_point;
125360SN/A}
126360SN/A
127360SN/A
1281450SN/ASyscallReturn
129360SN/AcloseFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
130360SN/A{
1311970SN/A    int target_fd = xc->getSyscallArg(0);
1321970SN/A    int status = close(p->sim_fd(target_fd));
1331970SN/A    if (status >= 0)
1341970SN/A        p->free_fd(target_fd);
1351970SN/A    return status;
136360SN/A}
137360SN/A
138360SN/A
1391450SN/ASyscallReturn
140360SN/AreadFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
141360SN/A{
142360SN/A    int fd = p->sim_fd(xc->getSyscallArg(0));
143360SN/A    int nbytes = xc->getSyscallArg(2);
144360SN/A    BufferArg bufArg(xc->getSyscallArg(1), nbytes);
145360SN/A
146360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
147360SN/A
148360SN/A    if (bytes_read != -1)
1492426SN/A        bufArg.copyOut(xc->getMemPort());
150360SN/A
1511458SN/A    return bytes_read;
152360SN/A}
153360SN/A
1541450SN/ASyscallReturn
155360SN/AwriteFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
156360SN/A{
157360SN/A    int fd = p->sim_fd(xc->getSyscallArg(0));
158360SN/A    int nbytes = xc->getSyscallArg(2);
159360SN/A    BufferArg bufArg(xc->getSyscallArg(1), nbytes);
160360SN/A
1612426SN/A    bufArg.copyIn(xc->getMemPort());
162360SN/A
163360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
164360SN/A
165360SN/A    fsync(fd);
166360SN/A
1671458SN/A    return bytes_written;
168360SN/A}
169360SN/A
170360SN/A
1711450SN/ASyscallReturn
172360SN/AlseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
173360SN/A{
174360SN/A    int fd = p->sim_fd(xc->getSyscallArg(0));
175360SN/A    uint64_t offs = xc->getSyscallArg(1);
176360SN/A    int whence = xc->getSyscallArg(2);
177360SN/A
178360SN/A    off_t result = lseek(fd, offs, whence);
179360SN/A
1801458SN/A    return (result == (off_t)-1) ? -errno : result;
181360SN/A}
182360SN/A
183360SN/A
1841450SN/ASyscallReturn
185360SN/AmunmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
186360SN/A{
187360SN/A    // given that we don't really implement mmap, munmap is really easy
1881458SN/A    return 0;
189360SN/A}
190360SN/A
191360SN/A
192360SN/Aconst char *hostname = "m5.eecs.umich.edu";
193360SN/A
1941450SN/ASyscallReturn
195360SN/AgethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
196360SN/A{
197360SN/A    int name_len = xc->getSyscallArg(1);
198360SN/A    BufferArg name(xc->getSyscallArg(0), name_len);
199360SN/A
200360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
201360SN/A
2022426SN/A    name.copyOut(xc->getMemPort());
203360SN/A
2041458SN/A    return 0;
205360SN/A}
206360SN/A
2071450SN/ASyscallReturn
208511SN/AunlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
209511SN/A{
2101706SN/A    string path;
211360SN/A
2122461SN/A    if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
2131450SN/A        return (TheISA::IntReg)-EFAULT;
214511SN/A
215511SN/A    int result = unlink(path.c_str());
2161458SN/A    return (result == -1) ? -errno : result;
217511SN/A}
218511SN/A
2191450SN/ASyscallReturn
220511SN/ArenameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
221511SN/A{
2221706SN/A    string old_name;
223511SN/A
2242461SN/A    if (!xc->getMemPort()->tryReadString(old_name, xc->getSyscallArg(0)))
2251458SN/A        return -EFAULT;
226511SN/A
2271706SN/A    string new_name;
228511SN/A
2292461SN/A    if (!xc->getMemPort()->tryReadString(new_name, xc->getSyscallArg(1)))
2301458SN/A        return -EFAULT;
231511SN/A
2321706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
2331458SN/A    return (result == -1) ? -errno : result;
234511SN/A}
235511SN/A
2361706SN/ASyscallReturn
2371706SN/AtruncateFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
2381706SN/A{
2391706SN/A    string path;
2401706SN/A
2412461SN/A    if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
2421706SN/A        return -EFAULT;
2431706SN/A
2441706SN/A    off_t length = xc->getSyscallArg(1);
2451706SN/A
2461706SN/A    int result = truncate(path.c_str(), length);
2471706SN/A    return (result == -1) ? -errno : result;
2481706SN/A}
2491706SN/A
2501706SN/ASyscallReturn
2511706SN/AftruncateFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
2521706SN/A{
2531706SN/A    int fd = process->sim_fd(xc->getSyscallArg(0));
2541706SN/A
2551706SN/A    if (fd < 0)
2561706SN/A        return -EBADF;
2571706SN/A
2581706SN/A    off_t length = xc->getSyscallArg(1);
2591706SN/A
2601706SN/A    int result = ftruncate(fd, length);
2611706SN/A    return (result == -1) ? -errno : result;
2621706SN/A}
2631999SN/A
2641999SN/ASyscallReturn
2651999SN/AchownFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
2661999SN/A{
2671999SN/A    string path;
2681999SN/A
2692461SN/A    if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
2701999SN/A        return -EFAULT;
2711999SN/A
2721999SN/A    /* XXX endianess */
2731999SN/A    uint32_t owner = xc->getSyscallArg(1);
2741999SN/A    uid_t hostOwner = owner;
2751999SN/A    uint32_t group = xc->getSyscallArg(2);
2761999SN/A    gid_t hostGroup = group;
2771999SN/A
2781999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
2791999SN/A    return (result == -1) ? -errno : result;
2801999SN/A}
2811999SN/A
2821999SN/ASyscallReturn
2831999SN/AfchownFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
2841999SN/A{
2851999SN/A    int fd = process->sim_fd(xc->getSyscallArg(0));
2861999SN/A
2871999SN/A    if (fd < 0)
2881999SN/A        return -EBADF;
2891999SN/A
2901999SN/A    /* XXX endianess */
2911999SN/A    uint32_t owner = xc->getSyscallArg(1);
2921999SN/A    uid_t hostOwner = owner;
2931999SN/A    uint32_t group = xc->getSyscallArg(2);
2941999SN/A    gid_t hostGroup = group;
2951999SN/A
2961999SN/A    int result = fchown(fd, hostOwner, hostGroup);
2971999SN/A    return (result == -1) ? -errno : result;
2981999SN/A}
2992093SN/A
3002093SN/A
3012093SN/ASyscallReturn
3022093SN/AfcntlFunc(SyscallDesc *desc, int num, Process *process,
3032093SN/A          ExecContext *xc)
3042093SN/A{
3052093SN/A    int fd = xc->getSyscallArg(0);
3062093SN/A
3072093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
3082093SN/A        return -EBADF;
3092093SN/A
3102093SN/A    int cmd = xc->getSyscallArg(1);
3112093SN/A    switch (cmd) {
3122093SN/A      case 0: // F_DUPFD
3132093SN/A        // if we really wanted to support this, we'd need to do it
3142093SN/A        // in the target fd space.
3152093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
3162093SN/A        return -EMFILE;
3172093SN/A
3182093SN/A      case 1: // F_GETFD (get close-on-exec flag)
3192093SN/A      case 2: // F_SETFD (set close-on-exec flag)
3202093SN/A        return 0;
3212093SN/A
3222093SN/A      case 3: // F_GETFL (get file flags)
3232093SN/A      case 4: // F_SETFL (set file flags)
3242093SN/A        // not sure if this is totally valid, but we'll pass it through
3252093SN/A        // to the underlying OS
3262093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
3272093SN/A        return fcntl(process->sim_fd(fd), cmd);
3282093SN/A        // return 0;
3292093SN/A
3302093SN/A      case 7: // F_GETLK  (get lock)
3312093SN/A      case 8: // F_SETLK  (set lock)
3322093SN/A      case 9: // F_SETLKW (set lock and wait)
3332093SN/A        // don't mess with file locking... just act like it's OK
3342093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
3352093SN/A        return 0;
3362093SN/A
3372093SN/A      default:
3382093SN/A        warn("Unknown fcntl command %d\n", cmd);
3392093SN/A        return 0;
3402093SN/A    }
3412093SN/A}
3422093SN/A
3432238SN/ASyscallReturn
3442238SN/ApipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
3452238SN/A         ExecContext *xc)
3462238SN/A{
3472238SN/A    int fds[2], sim_fds[2];
3482238SN/A    int pipe_retval = pipe(fds);
3492093SN/A
3502238SN/A    if (pipe_retval < 0) {
3512238SN/A        // error
3522238SN/A        return pipe_retval;
3532238SN/A    }
3542238SN/A
3552238SN/A    sim_fds[0] = process->alloc_fd(fds[0]);
3562238SN/A    sim_fds[1] = process->alloc_fd(fds[1]);
3572238SN/A
3582238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
3592238SN/A    // the return value of the function, and fd[1] is returned in r20.
3602264SN/A    xc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
3612238SN/A    return sim_fds[0];
3622238SN/A}
3632238SN/A
3642238SN/A
3652238SN/ASyscallReturn
3662238SN/AgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
3672238SN/A           ExecContext *xc)
3682238SN/A{
3692238SN/A    // Make up a PID.  There's no interprocess communication in
3702238SN/A    // fake_syscall mode, so there's no way for a process to know it's
3712238SN/A    // not getting a unique value.
3722238SN/A
3732264SN/A    xc->setIntReg(SyscallPseudoReturnReg, 99);
3742238SN/A    return 100;
3752238SN/A}
3762238SN/A
3772238SN/A
3782238SN/ASyscallReturn
3792238SN/AgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
3802238SN/A           ExecContext *xc)
3812238SN/A{
3822238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
3832238SN/A    // simulation to be deterministic.
3842238SN/A
3852238SN/A    // EUID goes in r20.
3862264SN/A    xc->setIntReg(SyscallPseudoReturnReg, 100); //EUID
3872238SN/A    return 100;		// UID
3882238SN/A}
3892238SN/A
3902238SN/A
3912238SN/ASyscallReturn
3922238SN/AgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
3932238SN/A           ExecContext *xc)
3942238SN/A{
3952238SN/A    // Get current group ID.  EGID goes in r20.
3962264SN/A    xc->setIntReg(SyscallPseudoReturnReg, 100); //EGID
3972238SN/A    return 100;
3982238SN/A}
3992238SN/A
4002238SN/A
4012238SN/ASyscallReturn
4022238SN/AsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
4032238SN/A           ExecContext *xc)
4042238SN/A{
4052238SN/A    // can't fathom why a benchmark would call this.
4062238SN/A    warn("Ignoring call to setuid(%d)\n", xc->getSyscallArg(0));
4072238SN/A    return 0;
4082238SN/A}
4092238SN/A
4102238SN/ASyscallReturn
4112238SN/AgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
4122238SN/A           ExecContext *xc)
4132238SN/A{
4142238SN/A    // Make up a PID.  There's no interprocess communication in
4152238SN/A    // fake_syscall mode, so there's no way for a process to know it's
4162238SN/A    // not getting a unique value.
4172238SN/A
4182264SN/A    xc->setIntReg(SyscallPseudoReturnReg, 99); //PID
4192238SN/A    return 100;
4202238SN/A}
4212238SN/A
4222238SN/ASyscallReturn
4232238SN/AgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
4242238SN/A           ExecContext *xc)
4252238SN/A{
4262238SN/A    return 99;
4272238SN/A}
4282238SN/A
4292238SN/ASyscallReturn
4302238SN/AgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
4312238SN/A           ExecContext *xc)
4322238SN/A{
4332238SN/A    return 100;		// UID
4342238SN/A}
4352238SN/A
4362238SN/ASyscallReturn
4372238SN/AgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
4382238SN/A           ExecContext *xc)
4392238SN/A{
4402238SN/A    return 100;		// UID
4412238SN/A}
4422238SN/A
4432238SN/ASyscallReturn
4442238SN/AgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
4452238SN/A           ExecContext *xc)
4462238SN/A{
4472238SN/A    return 100;
4482238SN/A}
4492238SN/A
4502238SN/ASyscallReturn
4512238SN/AgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
4522238SN/A           ExecContext *xc)
4532238SN/A{
4542238SN/A    return 100;
4552238SN/A}
4562238SN/A
4572238SN/A
458