Merge zizzer:/bk/newmem
[gem5.git] / src / sim / syscall_emul.cc
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 * Authors: Steve Reinhardt
29 * Ali Saidi
30 */
31
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <string>
36 #include <iostream>
37
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"
45
46 #include "sim/sim_events.hh"
47
48 using namespace std;
49 using namespace TheISA;
50
51 void
52 SyscallDesc::doSyscall(int callnum, Process *process, ThreadContext *tc)
53 {
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));
58
59 SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
60
61 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
62 curTick,tc->getCpuPtr()->name(), name, retval.value());
63
64 if (!(flags & SyscallDesc::SuppressReturnValue))
65 tc->setSyscallReturn(retval);
66 }
67
68
69 SyscallReturn
70 unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
71 ThreadContext *tc)
72 {
73 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
74
75 return 1;
76 }
77
78
79 SyscallReturn
80 ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
81 ThreadContext *tc)
82 {
83 warn("ignoring syscall %s(%d, %d, ...)", desc->name,
84 tc->getSyscallArg(0), tc->getSyscallArg(1));
85
86 return 0;
87 }
88
89
90 SyscallReturn
91 exitFunc(SyscallDesc *desc, int callnum, Process *process,
92 ThreadContext *tc)
93 {
94 new SimExitEvent("target called exit()", tc->getSyscallArg(0) & 0xff);
95
96 return 1;
97 }
98
99
100 SyscallReturn
101 getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
102 {
103 return (int)VMPageSize;
104 }
105
106
107 SyscallReturn
108 obreakFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
109 {
110 Addr junk;
111
112 // change brk addr to first arg
113 Addr new_brk = tc->getSyscallArg(0);
114 if (new_brk != 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),
119 VMPageSize);
120 }
121 p->brk_point = new_brk;
122 }
123 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
124 return p->brk_point;
125 }
126
127
128 SyscallReturn
129 closeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
130 {
131 int target_fd = tc->getSyscallArg(0);
132 int status = close(p->sim_fd(target_fd));
133 if (status >= 0)
134 p->free_fd(target_fd);
135 return status;
136 }
137
138
139 SyscallReturn
140 readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
141 {
142 int fd = p->sim_fd(tc->getSyscallArg(0));
143 int nbytes = tc->getSyscallArg(2);
144 BufferArg bufArg(tc->getSyscallArg(1), nbytes);
145
146 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
147
148 if (bytes_read != -1)
149 bufArg.copyOut(tc->getMemPort());
150
151 return bytes_read;
152 }
153
154 SyscallReturn
155 writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
156 {
157 int fd = p->sim_fd(tc->getSyscallArg(0));
158 int nbytes = tc->getSyscallArg(2);
159 BufferArg bufArg(tc->getSyscallArg(1), nbytes);
160
161 bufArg.copyIn(tc->getMemPort());
162
163 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
164
165 fsync(fd);
166
167 return bytes_written;
168 }
169
170
171 SyscallReturn
172 lseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
173 {
174 int fd = p->sim_fd(tc->getSyscallArg(0));
175 uint64_t offs = tc->getSyscallArg(1);
176 int whence = tc->getSyscallArg(2);
177
178 off_t result = lseek(fd, offs, whence);
179
180 return (result == (off_t)-1) ? -errno : result;
181 }
182
183
184 SyscallReturn
185 munmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
186 {
187 // given that we don't really implement mmap, munmap is really easy
188 return 0;
189 }
190
191
192 const char *hostname = "m5.eecs.umich.edu";
193
194 SyscallReturn
195 gethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
196 {
197 int name_len = tc->getSyscallArg(1);
198 BufferArg name(tc->getSyscallArg(0), name_len);
199
200 strncpy((char *)name.bufferPtr(), hostname, name_len);
201
202 name.copyOut(tc->getMemPort());
203
204 return 0;
205 }
206
207 SyscallReturn
208 unlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
209 {
210 string path;
211
212 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
213 return (TheISA::IntReg)-EFAULT;
214
215 int result = unlink(path.c_str());
216 return (result == -1) ? -errno : result;
217 }
218
219 SyscallReturn
220 renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
221 {
222 string old_name;
223
224 if (!tc->getMemPort()->tryReadString(old_name, tc->getSyscallArg(0)))
225 return -EFAULT;
226
227 string new_name;
228
229 if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1)))
230 return -EFAULT;
231
232 int64_t result = rename(old_name.c_str(), new_name.c_str());
233 return (result == -1) ? -errno : result;
234 }
235
236 SyscallReturn
237 truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
238 {
239 string path;
240
241 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
242 return -EFAULT;
243
244 off_t length = tc->getSyscallArg(1);
245
246 int result = truncate(path.c_str(), length);
247 return (result == -1) ? -errno : result;
248 }
249
250 SyscallReturn
251 ftruncateFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
252 {
253 int fd = process->sim_fd(tc->getSyscallArg(0));
254
255 if (fd < 0)
256 return -EBADF;
257
258 off_t length = tc->getSyscallArg(1);
259
260 int result = ftruncate(fd, length);
261 return (result == -1) ? -errno : result;
262 }
263
264 SyscallReturn
265 chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
266 {
267 string path;
268
269 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
270 return -EFAULT;
271
272 /* XXX endianess */
273 uint32_t owner = tc->getSyscallArg(1);
274 uid_t hostOwner = owner;
275 uint32_t group = tc->getSyscallArg(2);
276 gid_t hostGroup = group;
277
278 int result = chown(path.c_str(), hostOwner, hostGroup);
279 return (result == -1) ? -errno : result;
280 }
281
282 SyscallReturn
283 fchownFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
284 {
285 int fd = process->sim_fd(tc->getSyscallArg(0));
286
287 if (fd < 0)
288 return -EBADF;
289
290 /* XXX endianess */
291 uint32_t owner = tc->getSyscallArg(1);
292 uid_t hostOwner = owner;
293 uint32_t group = tc->getSyscallArg(2);
294 gid_t hostGroup = group;
295
296 int result = fchown(fd, hostOwner, hostGroup);
297 return (result == -1) ? -errno : result;
298 }
299
300
301 SyscallReturn
302 fcntlFunc(SyscallDesc *desc, int num, Process *process,
303 ThreadContext *tc)
304 {
305 int fd = tc->getSyscallArg(0);
306
307 if (fd < 0 || process->sim_fd(fd) < 0)
308 return -EBADF;
309
310 int cmd = tc->getSyscallArg(1);
311 switch (cmd) {
312 case 0: // F_DUPFD
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);
316 return -EMFILE;
317
318 case 1: // F_GETFD (get close-on-exec flag)
319 case 2: // F_SETFD (set close-on-exec flag)
320 return 0;
321
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);
328 // return 0;
329
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);
335 return 0;
336
337 default:
338 warn("Unknown fcntl command %d\n", cmd);
339 return 0;
340 }
341 }
342
343 SyscallReturn
344 fcntl64Func(SyscallDesc *desc, int num, Process *process,
345 ThreadContext *tc)
346 {
347 int fd = tc->getSyscallArg(0);
348
349 if (fd < 0 || process->sim_fd(fd) < 0)
350 return -EBADF;
351
352 int cmd = tc->getSyscallArg(1);
353 switch (cmd) {
354 case 33: //F_GETLK64
355 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
356 return -EMFILE;
357
358 case 34: // F_SETLK64
359 case 35: // F_SETLKW64
360 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
361 return -EMFILE;
362
363 default:
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);
368 // return 0;
369 }
370 }
371
372 SyscallReturn
373 pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
374 ThreadContext *tc)
375 {
376 int fds[2], sim_fds[2];
377 int pipe_retval = pipe(fds);
378
379 if (pipe_retval < 0) {
380 // error
381 return pipe_retval;
382 }
383
384 sim_fds[0] = process->alloc_fd(fds[0]);
385 sim_fds[1] = process->alloc_fd(fds[1]);
386
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]);
390 return sim_fds[0];
391 }
392
393
394 SyscallReturn
395 getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
396 ThreadContext *tc)
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 tc->setIntReg(SyscallPseudoReturnReg, 99);
403 return 100;
404 }
405
406
407 SyscallReturn
408 getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
409 ThreadContext *tc)
410 {
411 // Make up a UID and EUID... it shouldn't matter, and we want the
412 // simulation to be deterministic.
413
414 // EUID goes in r20.
415 tc->setIntReg(SyscallPseudoReturnReg, 100); //EUID
416 return 100; // UID
417 }
418
419
420 SyscallReturn
421 getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
422 ThreadContext *tc)
423 {
424 // Get current group ID. EGID goes in r20.
425 tc->setIntReg(SyscallPseudoReturnReg, 100); //EGID
426 return 100;
427 }
428
429
430 SyscallReturn
431 setuidFunc(SyscallDesc *desc, int callnum, Process *process,
432 ThreadContext *tc)
433 {
434 // can't fathom why a benchmark would call this.
435 warn("Ignoring call to setuid(%d)\n", tc->getSyscallArg(0));
436 return 0;
437 }
438
439 SyscallReturn
440 getpidFunc(SyscallDesc *desc, int callnum, Process *process,
441 ThreadContext *tc)
442 {
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.
446
447 tc->setIntReg(SyscallPseudoReturnReg, 99); //PID
448 return 100;
449 }
450
451 SyscallReturn
452 getppidFunc(SyscallDesc *desc, int callnum, Process *process,
453 ThreadContext *tc)
454 {
455 return 99;
456 }
457
458 SyscallReturn
459 getuidFunc(SyscallDesc *desc, int callnum, Process *process,
460 ThreadContext *tc)
461 {
462 return 100; // UID
463 }
464
465 SyscallReturn
466 geteuidFunc(SyscallDesc *desc, int callnum, Process *process,
467 ThreadContext *tc)
468 {
469 return 100; // UID
470 }
471
472 SyscallReturn
473 getgidFunc(SyscallDesc *desc, int callnum, Process *process,
474 ThreadContext *tc)
475 {
476 return 100;
477 }
478
479 SyscallReturn
480 getegidFunc(SyscallDesc *desc, int callnum, Process *process,
481 ThreadContext *tc)
482 {
483 return 100;
484 }
485
486