2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Reinhardt
38 #include "sim/syscall_emul.hh"
39 #include "base/chunk_generator.hh"
40 #include "base/trace.hh"
41 #include "cpu/thread_context.hh"
42 #include "cpu/base.hh"
43 #include "mem/page_table.hh"
44 #include "sim/process.hh"
46 #include "sim/sim_exit.hh"
49 using namespace TheISA
;
52 SyscallDesc::doSyscall(int callnum
, LiveProcess
*process
, ThreadContext
*tc
)
54 DPRINTFR(SyscallVerbose
, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
55 curTick
,tc
->getCpuPtr()->name(), name
,
56 tc
->getSyscallArg(0),tc
->getSyscallArg(1),
57 tc
->getSyscallArg(2),tc
->getSyscallArg(3));
59 SyscallReturn retval
= (*funcPtr
)(this, callnum
, process
, tc
);
61 DPRINTFR(SyscallVerbose
, "%d: %s: syscall %s returns %d\n",
62 curTick
,tc
->getCpuPtr()->name(), name
, retval
.value());
64 if (!(flags
& SyscallDesc::SuppressReturnValue
))
65 tc
->setSyscallReturn(retval
);
70 unimplementedFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
73 fatal("syscall %s (#%d) unimplemented.", desc
->name
, callnum
);
80 ignoreFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
83 warn("ignoring syscall %s(%d, %d, ...)", desc
->name
,
84 tc
->getSyscallArg(0), tc
->getSyscallArg(1));
91 exitFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
95 exitSimLoop("target called exit()", tc
->getSyscallArg(0) & 0xff);
103 getpagesizeFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
105 return (int)VMPageSize
;
110 obreakFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
114 // change brk addr to first arg
115 Addr new_brk
= tc
->getSyscallArg(0);
117 for (ChunkGenerator
gen(p
->brk_point
, new_brk
- p
->brk_point
,
118 VMPageSize
); !gen
.done(); gen
.next()) {
119 if (!p
->pTable
->translate(gen
.addr(), junk
))
120 p
->pTable
->allocate(roundDown(gen
.addr(), VMPageSize
),
123 p
->brk_point
= new_brk
;
125 DPRINTF(SyscallVerbose
, "Break Point changed to: %#X\n", p
->brk_point
);
131 closeFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
133 int target_fd
= tc
->getSyscallArg(0);
134 int status
= close(p
->sim_fd(target_fd
));
136 p
->free_fd(target_fd
);
142 readFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
144 int fd
= p
->sim_fd(tc
->getSyscallArg(0));
145 int nbytes
= tc
->getSyscallArg(2);
146 BufferArg
bufArg(tc
->getSyscallArg(1), nbytes
);
148 int bytes_read
= read(fd
, bufArg
.bufferPtr(), nbytes
);
150 if (bytes_read
!= -1)
151 bufArg
.copyOut(tc
->getMemPort());
157 writeFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
159 int fd
= p
->sim_fd(tc
->getSyscallArg(0));
160 int nbytes
= tc
->getSyscallArg(2);
161 BufferArg
bufArg(tc
->getSyscallArg(1), nbytes
);
163 bufArg
.copyIn(tc
->getMemPort());
165 int bytes_written
= write(fd
, bufArg
.bufferPtr(), nbytes
);
169 return bytes_written
;
174 lseekFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
176 int fd
= p
->sim_fd(tc
->getSyscallArg(0));
177 uint64_t offs
= tc
->getSyscallArg(1);
178 int whence
= tc
->getSyscallArg(2);
180 off_t result
= lseek(fd
, offs
, whence
);
182 return (result
== (off_t
)-1) ? -errno
: result
;
187 munmapFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
189 // given that we don't really implement mmap, munmap is really easy
194 const char *hostname
= "m5.eecs.umich.edu";
197 gethostnameFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
199 int name_len
= tc
->getSyscallArg(1);
200 BufferArg
name(tc
->getSyscallArg(0), name_len
);
202 strncpy((char *)name
.bufferPtr(), hostname
, name_len
);
204 name
.copyOut(tc
->getMemPort());
210 unlinkFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
214 if (!tc
->getMemPort()->tryReadString(path
, tc
->getSyscallArg(0)))
215 return (TheISA::IntReg
)-EFAULT
;
217 int result
= unlink(path
.c_str());
218 return (result
== -1) ? -errno
: result
;
222 renameFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
226 if (!tc
->getMemPort()->tryReadString(old_name
, tc
->getSyscallArg(0)))
231 if (!tc
->getMemPort()->tryReadString(new_name
, tc
->getSyscallArg(1)))
234 int64_t result
= rename(old_name
.c_str(), new_name
.c_str());
235 return (result
== -1) ? -errno
: result
;
239 truncateFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
243 if (!tc
->getMemPort()->tryReadString(path
, tc
->getSyscallArg(0)))
246 off_t length
= tc
->getSyscallArg(1);
248 int result
= truncate(path
.c_str(), length
);
249 return (result
== -1) ? -errno
: result
;
253 ftruncateFunc(SyscallDesc
*desc
, int num
, LiveProcess
*process
, ThreadContext
*tc
)
255 int fd
= process
->sim_fd(tc
->getSyscallArg(0));
260 off_t length
= tc
->getSyscallArg(1);
262 int result
= ftruncate(fd
, length
);
263 return (result
== -1) ? -errno
: result
;
267 chownFunc(SyscallDesc
*desc
, int num
, LiveProcess
*p
, ThreadContext
*tc
)
271 if (!tc
->getMemPort()->tryReadString(path
, tc
->getSyscallArg(0)))
275 uint32_t owner
= tc
->getSyscallArg(1);
276 uid_t hostOwner
= owner
;
277 uint32_t group
= tc
->getSyscallArg(2);
278 gid_t hostGroup
= group
;
280 int result
= chown(path
.c_str(), hostOwner
, hostGroup
);
281 return (result
== -1) ? -errno
: result
;
285 fchownFunc(SyscallDesc
*desc
, int num
, LiveProcess
*process
, ThreadContext
*tc
)
287 int fd
= process
->sim_fd(tc
->getSyscallArg(0));
293 uint32_t owner
= tc
->getSyscallArg(1);
294 uid_t hostOwner
= owner
;
295 uint32_t group
= tc
->getSyscallArg(2);
296 gid_t hostGroup
= group
;
298 int result
= fchown(fd
, hostOwner
, hostGroup
);
299 return (result
== -1) ? -errno
: result
;
304 dupFunc(SyscallDesc
*desc
, int num
, LiveProcess
*process
, ThreadContext
*tc
)
306 int fd
= process
->sim_fd(tc
->getSyscallArg(0));
311 int result
= dup(fd
);
312 return (result
== -1) ? -errno
: process
->alloc_fd(result
);
317 fcntlFunc(SyscallDesc
*desc
, int num
, LiveProcess
*process
,
320 int fd
= tc
->getSyscallArg(0);
322 if (fd
< 0 || process
->sim_fd(fd
) < 0)
325 int cmd
= tc
->getSyscallArg(1);
328 // if we really wanted to support this, we'd need to do it
329 // in the target fd space.
330 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd
);
333 case 1: // F_GETFD (get close-on-exec flag)
334 case 2: // F_SETFD (set close-on-exec flag)
337 case 3: // F_GETFL (get file flags)
338 case 4: // F_SETFL (set file flags)
339 // not sure if this is totally valid, but we'll pass it through
340 // to the underlying OS
341 warn("fcntl(%d, %d) passed through to host\n", fd
, cmd
);
342 return fcntl(process
->sim_fd(fd
), cmd
);
345 case 7: // F_GETLK (get lock)
346 case 8: // F_SETLK (set lock)
347 case 9: // F_SETLKW (set lock and wait)
348 // don't mess with file locking... just act like it's OK
349 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd
, cmd
);
353 warn("Unknown fcntl command %d\n", cmd
);
359 fcntl64Func(SyscallDesc
*desc
, int num
, LiveProcess
*process
,
362 int fd
= tc
->getSyscallArg(0);
364 if (fd
< 0 || process
->sim_fd(fd
) < 0)
367 int cmd
= tc
->getSyscallArg(1);
370 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd
);
373 case 34: // F_SETLK64
374 case 35: // F_SETLKW64
375 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd
);
379 // not sure if this is totally valid, but we'll pass it through
380 // to the underlying OS
381 warn("fcntl64(%d, %d) passed through to host\n", fd
, cmd
);
382 return fcntl(process
->sim_fd(fd
), cmd
);
388 pipePseudoFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
391 int fds
[2], sim_fds
[2];
392 int pipe_retval
= pipe(fds
);
394 if (pipe_retval
< 0) {
399 sim_fds
[0] = process
->alloc_fd(fds
[0]);
400 sim_fds
[1] = process
->alloc_fd(fds
[1]);
402 // Alpha Linux convention for pipe() is that fd[0] is returned as
403 // the return value of the function, and fd[1] is returned in r20.
404 tc
->setIntReg(SyscallPseudoReturnReg
, sim_fds
[1]);
410 getpidPseudoFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
413 // Make up a PID. There's no interprocess communication in
414 // fake_syscall mode, so there's no way for a process to know it's
415 // not getting a unique value.
417 tc
->setIntReg(SyscallPseudoReturnReg
, process
->ppid());
418 return process
->pid();
423 getuidPseudoFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
426 // Make up a UID and EUID... it shouldn't matter, and we want the
427 // simulation to be deterministic.
430 tc
->setIntReg(SyscallPseudoReturnReg
, process
->euid()); //EUID
431 return process
->uid(); // UID
436 getgidPseudoFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
439 // Get current group ID. EGID goes in r20.
440 tc
->setIntReg(SyscallPseudoReturnReg
, process
->egid()); //EGID
441 return process
->gid();
446 setuidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
449 // can't fathom why a benchmark would call this.
450 warn("Ignoring call to setuid(%d)\n", tc
->getSyscallArg(0));
455 getpidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
458 // Make up a PID. There's no interprocess communication in
459 // fake_syscall mode, so there's no way for a process to know it's
460 // not getting a unique value.
462 tc
->setIntReg(SyscallPseudoReturnReg
, process
->ppid()); //PID
463 return process
->pid();
467 getppidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
470 return process
->ppid();
474 getuidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
477 return process
->uid(); // UID
481 geteuidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
484 return process
->euid(); // UID
488 getgidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
491 return process
->gid();
495 getegidFunc(SyscallDesc
*desc
, int callnum
, LiveProcess
*process
,
498 return process
->egid();