blob: 682d11267aec20565898325717d5a3b03ac4cb1f [file] [log] [blame]
Steve Reinhardt79767942003-12-01 19:34:38 -08001/*
Ali Saidiad9b28f2005-03-10 14:20:12 -05002 * Copyright (c) 2003-2005 The Regents of The University of Michigan
Steve Reinhardt79767942003-12-01 19:34:38 -08003 * 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
Ali Saidi53d2c932006-02-18 23:44:22 -050029#include <fcntl.h>
Steve Reinhardt79767942003-12-01 19:34:38 -080030#include <unistd.h>
31
32#include <string>
33#include <iostream>
34
35#include "sim/syscall_emul.hh"
36#include "base/trace.hh"
37#include "cpu/exec_context.hh"
Nathan Binkert13c005a2005-06-04 20:50:10 -040038#include "cpu/base.hh"
Steve Reinhardt79767942003-12-01 19:34:38 -080039#include "sim/process.hh"
40
41#include "sim/sim_events.hh"
42
43using namespace std;
Gabe Black463aa6d2006-02-19 02:34:37 -050044using namespace TheISA;
Steve Reinhardt79767942003-12-01 19:34:38 -080045
46void
47SyscallDesc::doSyscall(int callnum, Process *process, ExecContext *xc)
48{
49 DPRINTFR(SyscallVerbose, "%s: syscall %s called\n",
50 xc->cpu->name(), name);
51
Ali Saidi232134a2005-03-09 15:52:10 -050052 SyscallReturn retval = (*funcPtr)(this, callnum, process, xc);
Steve Reinhardt79767942003-12-01 19:34:38 -080053
54 DPRINTFR(SyscallVerbose, "%s: syscall %s returns %d\n",
Ali Saidi232134a2005-03-09 15:52:10 -050055 xc->cpu->name(), name, retval.value());
Steve Reinhardt79767942003-12-01 19:34:38 -080056
Ali Saidi232134a2005-03-09 15:52:10 -050057 if (!(flags & SyscallDesc::SuppressReturnValue))
Steve Reinhardt79767942003-12-01 19:34:38 -080058 xc->setSyscallReturn(retval);
59}
60
61
Ali Saidi232134a2005-03-09 15:52:10 -050062SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -080063unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
64 ExecContext *xc)
65{
Steve Reinhardt99bf6ed2005-11-10 21:05:31 -050066 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
Steve Reinhardt79767942003-12-01 19:34:38 -080067}
68
69
Ali Saidi232134a2005-03-09 15:52:10 -050070SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -080071ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
72 ExecContext *xc)
73{
Steve Reinhardt99bf6ed2005-11-10 21:05:31 -050074 warn("ignoring syscall %s(%d, %d, ...)", desc->name,
75 xc->getSyscallArg(0), xc->getSyscallArg(1));
Steve Reinhardt79767942003-12-01 19:34:38 -080076
Ali Saidiad9b28f2005-03-10 14:20:12 -050077 return 0;
Steve Reinhardt79767942003-12-01 19:34:38 -080078}
79
80
Ali Saidi232134a2005-03-09 15:52:10 -050081SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -080082exitFunc(SyscallDesc *desc, int callnum, Process *process,
83 ExecContext *xc)
84{
85 new SimExitEvent("syscall caused exit", xc->getSyscallArg(0) & 0xff);
86
Ali Saidiad9b28f2005-03-10 14:20:12 -050087 return 1;
Steve Reinhardt79767942003-12-01 19:34:38 -080088}
89
90
Ali Saidi232134a2005-03-09 15:52:10 -050091SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -080092getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
93{
Gabe Black463aa6d2006-02-19 02:34:37 -050094 return (int)VMPageSize;
Steve Reinhardt79767942003-12-01 19:34:38 -080095}
96
97
Ali Saidi232134a2005-03-09 15:52:10 -050098SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -080099obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
100{
101 // change brk addr to first arg
Steve Reinhardtd38f9952004-02-10 21:43:57 -0800102 Addr new_brk = xc->getSyscallArg(0);
103 if (new_brk != 0)
Ali Saidi232134a2005-03-09 15:52:10 -0500104 {
Steve Reinhardtd38f9952004-02-10 21:43:57 -0800105 p->brk_point = xc->getSyscallArg(0);
Ali Saidi232134a2005-03-09 15:52:10 -0500106 }
Ali Saidiad9b28f2005-03-10 14:20:12 -0500107 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
108 return p->brk_point;
Steve Reinhardt79767942003-12-01 19:34:38 -0800109}
110
111
Ali Saidi232134a2005-03-09 15:52:10 -0500112SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -0800113closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
114{
Steve Reinhardt44108762005-11-10 21:08:33 -0500115 int target_fd = xc->getSyscallArg(0);
116 int status = close(p->sim_fd(target_fd));
117 if (status >= 0)
118 p->free_fd(target_fd);
119 return status;
Steve Reinhardt79767942003-12-01 19:34:38 -0800120}
121
122
Ali Saidi232134a2005-03-09 15:52:10 -0500123SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -0800124readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
125{
126 int fd = p->sim_fd(xc->getSyscallArg(0));
127 int nbytes = xc->getSyscallArg(2);
128 BufferArg bufArg(xc->getSyscallArg(1), nbytes);
129
130 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
131
132 if (bytes_read != -1)
133 bufArg.copyOut(xc->mem);
134
Ali Saidiad9b28f2005-03-10 14:20:12 -0500135 return bytes_read;
Steve Reinhardt79767942003-12-01 19:34:38 -0800136}
137
Ali Saidi232134a2005-03-09 15:52:10 -0500138SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -0800139writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
140{
141 int fd = p->sim_fd(xc->getSyscallArg(0));
142 int nbytes = xc->getSyscallArg(2);
143 BufferArg bufArg(xc->getSyscallArg(1), nbytes);
144
145 bufArg.copyIn(xc->mem);
146
147 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
148
149 fsync(fd);
150
Ali Saidiad9b28f2005-03-10 14:20:12 -0500151 return bytes_written;
Steve Reinhardt79767942003-12-01 19:34:38 -0800152}
153
154
Ali Saidi232134a2005-03-09 15:52:10 -0500155SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -0800156lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
157{
158 int fd = p->sim_fd(xc->getSyscallArg(0));
159 uint64_t offs = xc->getSyscallArg(1);
160 int whence = xc->getSyscallArg(2);
161
162 off_t result = lseek(fd, offs, whence);
163
Ali Saidiad9b28f2005-03-10 14:20:12 -0500164 return (result == (off_t)-1) ? -errno : result;
Steve Reinhardt79767942003-12-01 19:34:38 -0800165}
166
167
Ali Saidi232134a2005-03-09 15:52:10 -0500168SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -0800169munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
170{
171 // given that we don't really implement mmap, munmap is really easy
Ali Saidiad9b28f2005-03-10 14:20:12 -0500172 return 0;
Steve Reinhardt79767942003-12-01 19:34:38 -0800173}
174
175
176const char *hostname = "m5.eecs.umich.edu";
177
Ali Saidi232134a2005-03-09 15:52:10 -0500178SyscallReturn
Steve Reinhardt79767942003-12-01 19:34:38 -0800179gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
180{
181 int name_len = xc->getSyscallArg(1);
182 BufferArg name(xc->getSyscallArg(0), name_len);
183
184 strncpy((char *)name.bufferPtr(), hostname, name_len);
185
186 name.copyOut(xc->mem);
187
Ali Saidiad9b28f2005-03-10 14:20:12 -0500188 return 0;
Steve Reinhardt79767942003-12-01 19:34:38 -0800189}
190
Ali Saidi232134a2005-03-09 15:52:10 -0500191SyscallReturn
David Oehmke95c248c2004-02-05 12:16:17 -0500192unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
193{
Steve Reinhardt22eccce2005-06-03 16:19:34 -0400194 string path;
David Oehmke95c248c2004-02-05 12:16:17 -0500195
Gabe Black10c79ef2006-02-16 01:22:51 -0500196 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
Ali Saidi232134a2005-03-09 15:52:10 -0500197 return (TheISA::IntReg)-EFAULT;
David Oehmke95c248c2004-02-05 12:16:17 -0500198
199 int result = unlink(path.c_str());
Ali Saidiad9b28f2005-03-10 14:20:12 -0500200 return (result == -1) ? -errno : result;
David Oehmke95c248c2004-02-05 12:16:17 -0500201}
202
Ali Saidi232134a2005-03-09 15:52:10 -0500203SyscallReturn
David Oehmke95c248c2004-02-05 12:16:17 -0500204renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
205{
Steve Reinhardt22eccce2005-06-03 16:19:34 -0400206 string old_name;
David Oehmke95c248c2004-02-05 12:16:17 -0500207
Gabe Black10c79ef2006-02-16 01:22:51 -0500208 if (xc->mem->readString(old_name, xc->getSyscallArg(0)) != NoFault)
Ali Saidiad9b28f2005-03-10 14:20:12 -0500209 return -EFAULT;
David Oehmke95c248c2004-02-05 12:16:17 -0500210
Steve Reinhardt22eccce2005-06-03 16:19:34 -0400211 string new_name;
David Oehmke95c248c2004-02-05 12:16:17 -0500212
Gabe Black10c79ef2006-02-16 01:22:51 -0500213 if (xc->mem->readString(new_name, xc->getSyscallArg(1)) != NoFault)
Ali Saidiad9b28f2005-03-10 14:20:12 -0500214 return -EFAULT;
David Oehmke95c248c2004-02-05 12:16:17 -0500215
Steve Reinhardt22eccce2005-06-03 16:19:34 -0400216 int64_t result = rename(old_name.c_str(), new_name.c_str());
Ali Saidiad9b28f2005-03-10 14:20:12 -0500217 return (result == -1) ? -errno : result;
David Oehmke95c248c2004-02-05 12:16:17 -0500218}
Steve Reinhardt79767942003-12-01 19:34:38 -0800219
Steve Reinhardt22eccce2005-06-03 16:19:34 -0400220SyscallReturn
221truncateFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
222{
223 string path;
224
Gabe Black10c79ef2006-02-16 01:22:51 -0500225 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
Steve Reinhardt22eccce2005-06-03 16:19:34 -0400226 return -EFAULT;
227
228 off_t length = xc->getSyscallArg(1);
229
230 int result = truncate(path.c_str(), length);
231 return (result == -1) ? -errno : result;
232}
233
234SyscallReturn
235ftruncateFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
236{
237 int fd = process->sim_fd(xc->getSyscallArg(0));
238
239 if (fd < 0)
240 return -EBADF;
241
242 off_t length = xc->getSyscallArg(1);
243
244 int result = ftruncate(fd, length);
245 return (result == -1) ? -errno : result;
246}
Kevin Lim6da93ea2005-11-22 12:08:08 -0500247
248SyscallReturn
249chownFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
250{
251 string path;
252
Gabe Black10c79ef2006-02-16 01:22:51 -0500253 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
Kevin Lim6da93ea2005-11-22 12:08:08 -0500254 return -EFAULT;
255
256 /* XXX endianess */
257 uint32_t owner = xc->getSyscallArg(1);
258 uid_t hostOwner = owner;
259 uint32_t group = xc->getSyscallArg(2);
260 gid_t hostGroup = group;
261
262 int result = chown(path.c_str(), hostOwner, hostGroup);
263 return (result == -1) ? -errno : result;
264}
265
266SyscallReturn
267fchownFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
268{
269 int fd = process->sim_fd(xc->getSyscallArg(0));
270
271 if (fd < 0)
272 return -EBADF;
273
274 /* XXX endianess */
275 uint32_t owner = xc->getSyscallArg(1);
276 uid_t hostOwner = owner;
277 uint32_t group = xc->getSyscallArg(2);
278 gid_t hostGroup = group;
279
280 int result = fchown(fd, hostOwner, hostGroup);
281 return (result == -1) ? -errno : result;
282}
Ali Saidi53d2c932006-02-18 23:44:22 -0500283
284
285SyscallReturn
286fcntlFunc(SyscallDesc *desc, int num, Process *process,
287 ExecContext *xc)
288{
289 int fd = xc->getSyscallArg(0);
290
291 if (fd < 0 || process->sim_fd(fd) < 0)
292 return -EBADF;
293
294 int cmd = xc->getSyscallArg(1);
295 switch (cmd) {
296 case 0: // F_DUPFD
297 // if we really wanted to support this, we'd need to do it
298 // in the target fd space.
299 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
300 return -EMFILE;
301
302 case 1: // F_GETFD (get close-on-exec flag)
303 case 2: // F_SETFD (set close-on-exec flag)
304 return 0;
305
306 case 3: // F_GETFL (get file flags)
307 case 4: // F_SETFL (set file flags)
308 // not sure if this is totally valid, but we'll pass it through
309 // to the underlying OS
310 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
311 return fcntl(process->sim_fd(fd), cmd);
312 // return 0;
313
314 case 7: // F_GETLK (get lock)
315 case 8: // F_SETLK (set lock)
316 case 9: // F_SETLKW (set lock and wait)
317 // don't mess with file locking... just act like it's OK
318 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
319 return 0;
320
321 default:
322 warn("Unknown fcntl command %d\n", cmd);
323 return 0;
324 }
325}
326
Ali Saidice3a6342006-03-09 15:42:09 -0500327SyscallReturn
328pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
329 ExecContext *xc)
330{
331 int fds[2], sim_fds[2];
332 int pipe_retval = pipe(fds);
333
334 if (pipe_retval < 0) {
335 // error
336 return pipe_retval;
337 }
338
339 sim_fds[0] = process->alloc_fd(fds[0]);
340 sim_fds[1] = process->alloc_fd(fds[1]);
341
342 // Alpha Linux convention for pipe() is that fd[0] is returned as
343 // the return value of the function, and fd[1] is returned in r20.
344 xc->regs.intRegFile[SyscallPseudoReturnReg] = sim_fds[1];
345 return sim_fds[0];
346}
347
348
349SyscallReturn
350getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
351 ExecContext *xc)
352{
353 // Make up a PID. There's no interprocess communication in
354 // fake_syscall mode, so there's no way for a process to know it's
355 // not getting a unique value.
356
357 xc->regs.intRegFile[SyscallPseudoReturnReg] = 99;
358 return 100;
359}
360
361
362SyscallReturn
363getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
364 ExecContext *xc)
365{
366 // Make up a UID and EUID... it shouldn't matter, and we want the
367 // simulation to be deterministic.
368
369 // EUID goes in r20.
370 xc->regs.intRegFile[SyscallPseudoReturnReg] = 100; // EUID
371 return 100; // UID
372}
373
374
375SyscallReturn
376getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
377 ExecContext *xc)
378{
379 // Get current group ID. EGID goes in r20.
380 xc->regs.intRegFile[SyscallPseudoReturnReg] = 100;
381 return 100;
382}
383
384
385SyscallReturn
386setuidFunc(SyscallDesc *desc, int callnum, Process *process,
387 ExecContext *xc)
388{
389 // can't fathom why a benchmark would call this.
390 warn("Ignoring call to setuid(%d)\n", xc->getSyscallArg(0));
391 return 0;
392}
393
394SyscallReturn
395getpidFunc(SyscallDesc *desc, int callnum, Process *process,
396 ExecContext *xc)
397{
398 // Make up a PID. There's no interprocess communication in
399 // fake_syscall mode, so there's no way for a process to know it's
400 // not getting a unique value.
401
402 xc->regs.intRegFile[SyscallPseudoReturnReg] = 99;
403 return 100;
404}
405
406SyscallReturn
407getppidFunc(SyscallDesc *desc, int callnum, Process *process,
408 ExecContext *xc)
409{
410 return 99;
411}
412
413SyscallReturn
414getuidFunc(SyscallDesc *desc, int callnum, Process *process,
415 ExecContext *xc)
416{
417 return 100; // UID
418}
419
420SyscallReturn
421geteuidFunc(SyscallDesc *desc, int callnum, Process *process,
422 ExecContext *xc)
423{
424 return 100; // UID
425}
426
427SyscallReturn
428getgidFunc(SyscallDesc *desc, int callnum, Process *process,
429 ExecContext *xc)
430{
431 return 100;
432}
433
434SyscallReturn
435getegidFunc(SyscallDesc *desc, int callnum, Process *process,
436 ExecContext *xc)
437{
438 return 100;
439}
440
Ali Saidi53d2c932006-02-18 23:44:22 -0500441