syscall_emul.cc revision 1969
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
29#include <unistd.h>
30
31#include <string>
32#include <iostream>
33
34#include "sim/syscall_emul.hh"
35#include "base/trace.hh"
36#include "cpu/exec_context.hh"
37#include "cpu/base.hh"
38#include "sim/process.hh"
39
40#include "sim/sim_events.hh"
41
42using namespace std;
43
44void
45SyscallDesc::doSyscall(int callnum, Process *process, ExecContext *xc)
46{
47    DPRINTFR(SyscallVerbose, "%s: syscall %s called\n",
48             xc->cpu->name(), name);
49
50    SyscallReturn retval = (*funcPtr)(this, callnum, process, xc);
51
52    DPRINTFR(SyscallVerbose, "%s: syscall %s returns %d\n",
53             xc->cpu->name(), name, retval.value());
54
55    if (!(flags & SyscallDesc::SuppressReturnValue))
56        xc->setSyscallReturn(retval);
57}
58
59
60SyscallReturn
61unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
62                  ExecContext *xc)
63{
64    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
65}
66
67
68SyscallReturn
69ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
70           ExecContext *xc)
71{
72    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
73         xc->getSyscallArg(0), xc->getSyscallArg(1));
74
75    return 0;
76}
77
78
79SyscallReturn
80exitFunc(SyscallDesc *desc, int callnum, Process *process,
81         ExecContext *xc)
82{
83    new SimExitEvent("syscall caused exit", xc->getSyscallArg(0) & 0xff);
84
85    return 1;
86}
87
88
89SyscallReturn
90getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
91{
92    return VMPageSize;
93}
94
95
96SyscallReturn
97obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
98{
99    // change brk addr to first arg
100    Addr new_brk = xc->getSyscallArg(0);
101    if (new_brk != 0)
102    {
103        p->brk_point = xc->getSyscallArg(0);
104    }
105    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
106    return p->brk_point;
107}
108
109
110SyscallReturn
111closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
112{
113    int fd = p->sim_fd(xc->getSyscallArg(0));
114    return close(fd);
115}
116
117
118SyscallReturn
119readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
120{
121    int fd = p->sim_fd(xc->getSyscallArg(0));
122    int nbytes = xc->getSyscallArg(2);
123    BufferArg bufArg(xc->getSyscallArg(1), nbytes);
124
125    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
126
127    if (bytes_read != -1)
128        bufArg.copyOut(xc->mem);
129
130    return bytes_read;
131}
132
133SyscallReturn
134writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
135{
136    int fd = p->sim_fd(xc->getSyscallArg(0));
137    int nbytes = xc->getSyscallArg(2);
138    BufferArg bufArg(xc->getSyscallArg(1), nbytes);
139
140    bufArg.copyIn(xc->mem);
141
142    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
143
144    fsync(fd);
145
146    return bytes_written;
147}
148
149
150SyscallReturn
151lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
152{
153    int fd = p->sim_fd(xc->getSyscallArg(0));
154    uint64_t offs = xc->getSyscallArg(1);
155    int whence = xc->getSyscallArg(2);
156
157    off_t result = lseek(fd, offs, whence);
158
159    return (result == (off_t)-1) ? -errno : result;
160}
161
162
163SyscallReturn
164munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
165{
166    // given that we don't really implement mmap, munmap is really easy
167    return 0;
168}
169
170
171const char *hostname = "m5.eecs.umich.edu";
172
173SyscallReturn
174gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
175{
176    int name_len = xc->getSyscallArg(1);
177    BufferArg name(xc->getSyscallArg(0), name_len);
178
179    strncpy((char *)name.bufferPtr(), hostname, name_len);
180
181    name.copyOut(xc->mem);
182
183    return 0;
184}
185
186SyscallReturn
187unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
188{
189    string path;
190
191    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
192        return (TheISA::IntReg)-EFAULT;
193
194    int result = unlink(path.c_str());
195    return (result == -1) ? -errno : result;
196}
197
198SyscallReturn
199renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
200{
201    string old_name;
202
203    if (xc->mem->readString(old_name, xc->getSyscallArg(0)) != No_Fault)
204        return -EFAULT;
205
206    string new_name;
207
208    if (xc->mem->readString(new_name, xc->getSyscallArg(1)) != No_Fault)
209        return -EFAULT;
210
211    int64_t result = rename(old_name.c_str(), new_name.c_str());
212    return (result == -1) ? -errno : result;
213}
214
215SyscallReturn
216truncateFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
217{
218    string path;
219
220    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
221        return -EFAULT;
222
223    off_t length = xc->getSyscallArg(1);
224
225    int result = truncate(path.c_str(), length);
226    return (result == -1) ? -errno : result;
227}
228
229SyscallReturn
230ftruncateFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
231{
232    int fd = process->sim_fd(xc->getSyscallArg(0));
233
234    if (fd < 0)
235        return -EBADF;
236
237    off_t length = xc->getSyscallArg(1);
238
239    int result = ftruncate(fd, length);
240    return (result == -1) ? -errno : result;
241}
242