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_events.hh"
49 using namespace TheISA
;
52 SyscallDesc::doSyscall(int callnum
, Process
*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
, Process
*process
,
73 fatal("syscall %s (#%d) unimplemented.", desc
->name
, callnum
);
80 ignoreFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
83 warn("ignoring syscall %s(%d, %d, ...)", desc
->name
,
84 tc
->getSyscallArg(0), tc
->getSyscallArg(1));
91 exitFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
94 new SimExitEvent("target called exit()", tc
->getSyscallArg(0) & 0xff);
101 getpagesizeFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
103 return (int)VMPageSize
;
108 obreakFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
112 // change brk addr to first arg
113 Addr new_brk
= tc
->getSyscallArg(0);
115 for (ChunkGenerator
gen(p
->brk_point
, new_brk
- p
->brk_point
,
116 VMPageSize
); !gen
.done(); gen
.next()) {
117 if (!p
->pTable
->translate(gen
.addr(), junk
))
118 p
->pTable
->allocate(roundDown(gen
.addr(), VMPageSize
),
121 p
->brk_point
= new_brk
;
123 DPRINTF(SyscallVerbose
, "Break Point changed to: %#X\n", p
->brk_point
);
129 closeFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
131 int target_fd
= tc
->getSyscallArg(0);
132 int status
= close(p
->sim_fd(target_fd
));
134 p
->free_fd(target_fd
);
140 readFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
142 int fd
= p
->sim_fd(tc
->getSyscallArg(0));
143 int nbytes
= tc
->getSyscallArg(2);
144 BufferArg
bufArg(tc
->getSyscallArg(1), nbytes
);
146 int bytes_read
= read(fd
, bufArg
.bufferPtr(), nbytes
);
148 if (bytes_read
!= -1)
149 bufArg
.copyOut(tc
->getMemPort());
155 writeFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
157 int fd
= p
->sim_fd(tc
->getSyscallArg(0));
158 int nbytes
= tc
->getSyscallArg(2);
159 BufferArg
bufArg(tc
->getSyscallArg(1), nbytes
);
161 bufArg
.copyIn(tc
->getMemPort());
163 int bytes_written
= write(fd
, bufArg
.bufferPtr(), nbytes
);
167 return bytes_written
;
172 lseekFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
174 int fd
= p
->sim_fd(tc
->getSyscallArg(0));
175 uint64_t offs
= tc
->getSyscallArg(1);
176 int whence
= tc
->getSyscallArg(2);
178 off_t result
= lseek(fd
, offs
, whence
);
180 return (result
== (off_t
)-1) ? -errno
: result
;
185 munmapFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
187 // given that we don't really implement mmap, munmap is really easy
192 const char *hostname
= "m5.eecs.umich.edu";
195 gethostnameFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
197 int name_len
= tc
->getSyscallArg(1);
198 BufferArg
name(tc
->getSyscallArg(0), name_len
);
200 strncpy((char *)name
.bufferPtr(), hostname
, name_len
);
202 name
.copyOut(tc
->getMemPort());
208 unlinkFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
212 if (!tc
->getMemPort()->tryReadString(path
, tc
->getSyscallArg(0)))
213 return (TheISA::IntReg
)-EFAULT
;
215 int result
= unlink(path
.c_str());
216 return (result
== -1) ? -errno
: result
;
220 renameFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
224 if (!tc
->getMemPort()->tryReadString(old_name
, tc
->getSyscallArg(0)))
229 if (!tc
->getMemPort()->tryReadString(new_name
, tc
->getSyscallArg(1)))
232 int64_t result
= rename(old_name
.c_str(), new_name
.c_str());
233 return (result
== -1) ? -errno
: result
;
237 truncateFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
241 if (!tc
->getMemPort()->tryReadString(path
, tc
->getSyscallArg(0)))
244 off_t length
= tc
->getSyscallArg(1);
246 int result
= truncate(path
.c_str(), length
);
247 return (result
== -1) ? -errno
: result
;
251 ftruncateFunc(SyscallDesc
*desc
, int num
, Process
*process
, ThreadContext
*tc
)
253 int fd
= process
->sim_fd(tc
->getSyscallArg(0));
258 off_t length
= tc
->getSyscallArg(1);
260 int result
= ftruncate(fd
, length
);
261 return (result
== -1) ? -errno
: result
;
265 chownFunc(SyscallDesc
*desc
, int num
, Process
*p
, ThreadContext
*tc
)
269 if (!tc
->getMemPort()->tryReadString(path
, tc
->getSyscallArg(0)))
273 uint32_t owner
= tc
->getSyscallArg(1);
274 uid_t hostOwner
= owner
;
275 uint32_t group
= tc
->getSyscallArg(2);
276 gid_t hostGroup
= group
;
278 int result
= chown(path
.c_str(), hostOwner
, hostGroup
);
279 return (result
== -1) ? -errno
: result
;
283 fchownFunc(SyscallDesc
*desc
, int num
, Process
*process
, ThreadContext
*tc
)
285 int fd
= process
->sim_fd(tc
->getSyscallArg(0));
291 uint32_t owner
= tc
->getSyscallArg(1);
292 uid_t hostOwner
= owner
;
293 uint32_t group
= tc
->getSyscallArg(2);
294 gid_t hostGroup
= group
;
296 int result
= fchown(fd
, hostOwner
, hostGroup
);
297 return (result
== -1) ? -errno
: result
;
302 fcntlFunc(SyscallDesc
*desc
, int num
, Process
*process
,
305 int fd
= tc
->getSyscallArg(0);
307 if (fd
< 0 || process
->sim_fd(fd
) < 0)
310 int cmd
= tc
->getSyscallArg(1);
313 // if we really wanted to support this, we'd need to do it
314 // in the target fd space.
315 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd
);
318 case 1: // F_GETFD (get close-on-exec flag)
319 case 2: // F_SETFD (set close-on-exec flag)
322 case 3: // F_GETFL (get file flags)
323 case 4: // F_SETFL (set file flags)
324 // not sure if this is totally valid, but we'll pass it through
325 // to the underlying OS
326 warn("fcntl(%d, %d) passed through to host\n", fd
, cmd
);
327 return fcntl(process
->sim_fd(fd
), cmd
);
330 case 7: // F_GETLK (get lock)
331 case 8: // F_SETLK (set lock)
332 case 9: // F_SETLKW (set lock and wait)
333 // don't mess with file locking... just act like it's OK
334 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd
, cmd
);
338 warn("Unknown fcntl command %d\n", cmd
);
344 fcntl64Func(SyscallDesc
*desc
, int num
, Process
*process
,
347 int fd
= tc
->getSyscallArg(0);
349 if (fd
< 0 || process
->sim_fd(fd
) < 0)
352 int cmd
= tc
->getSyscallArg(1);
355 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd
);
358 case 34: // F_SETLK64
359 case 35: // F_SETLKW64
360 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd
);
364 // not sure if this is totally valid, but we'll pass it through
365 // to the underlying OS
366 warn("fcntl64(%d, %d) passed through to host\n", fd
, cmd
);
367 return fcntl(process
->sim_fd(fd
), cmd
);
373 pipePseudoFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
376 int fds
[2], sim_fds
[2];
377 int pipe_retval
= pipe(fds
);
379 if (pipe_retval
< 0) {
384 sim_fds
[0] = process
->alloc_fd(fds
[0]);
385 sim_fds
[1] = process
->alloc_fd(fds
[1]);
387 // Alpha Linux convention for pipe() is that fd[0] is returned as
388 // the return value of the function, and fd[1] is returned in r20.
389 tc
->setIntReg(SyscallPseudoReturnReg
, sim_fds
[1]);
395 getpidPseudoFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
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.
402 tc
->setIntReg(SyscallPseudoReturnReg
, 99);
408 getuidPseudoFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
411 // Make up a UID and EUID... it shouldn't matter, and we want the
412 // simulation to be deterministic.
415 tc
->setIntReg(SyscallPseudoReturnReg
, 100); //EUID
421 getgidPseudoFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
424 // Get current group ID. EGID goes in r20.
425 tc
->setIntReg(SyscallPseudoReturnReg
, 100); //EGID
431 setuidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
434 // can't fathom why a benchmark would call this.
435 warn("Ignoring call to setuid(%d)\n", tc
->getSyscallArg(0));
440 getpidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
443 // Make up a PID. There's no interprocess communication in
444 // fake_syscall mode, so there's no way for a process to know it's
445 // not getting a unique value.
447 tc
->setIntReg(SyscallPseudoReturnReg
, 99); //PID
452 getppidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
459 getuidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
466 geteuidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
473 getgidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,
480 getegidFunc(SyscallDesc
*desc
, int callnum
, Process
*process
,