syscall_emul.cc revision 3669:3607aaed36b6
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_exit.hh"
47
48using namespace std;
49using namespace TheISA;
50
51void
52SyscallDesc::doSyscall(int callnum, LiveProcess *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
69SyscallReturn
70unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71                  ThreadContext *tc)
72{
73    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
74
75    return 1;
76}
77
78
79SyscallReturn
80ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *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
90SyscallReturn
91exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
92         ThreadContext *tc)
93{
94    if (tc->exit()) {
95        exitSimLoop("target called exit()", tc->getSyscallArg(0) & 0xff);
96    }
97
98    return 1;
99}
100
101
102SyscallReturn
103getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
104{
105    return (int)VMPageSize;
106}
107
108
109SyscallReturn
110obreakFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
111{
112    Addr junk;
113
114    // change brk addr to first arg
115    Addr new_brk = tc->getSyscallArg(0);
116    if (new_brk != 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),
121                                    VMPageSize);
122        }
123        p->brk_point = new_brk;
124    }
125    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
126    return p->brk_point;
127}
128
129
130SyscallReturn
131closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
132{
133    int target_fd = tc->getSyscallArg(0);
134    int status = close(p->sim_fd(target_fd));
135    if (status >= 0)
136        p->free_fd(target_fd);
137    return status;
138}
139
140
141SyscallReturn
142readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
143{
144    int fd = p->sim_fd(tc->getSyscallArg(0));
145    int nbytes = tc->getSyscallArg(2);
146    BufferArg bufArg(tc->getSyscallArg(1), nbytes);
147
148    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
149
150    if (bytes_read != -1)
151        bufArg.copyOut(tc->getMemPort());
152
153    return bytes_read;
154}
155
156SyscallReturn
157writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
158{
159    int fd = p->sim_fd(tc->getSyscallArg(0));
160    int nbytes = tc->getSyscallArg(2);
161    BufferArg bufArg(tc->getSyscallArg(1), nbytes);
162
163    bufArg.copyIn(tc->getMemPort());
164
165    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
166
167    fsync(fd);
168
169    return bytes_written;
170}
171
172
173SyscallReturn
174lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
175{
176    int fd = p->sim_fd(tc->getSyscallArg(0));
177    uint64_t offs = tc->getSyscallArg(1);
178    int whence = tc->getSyscallArg(2);
179
180    off_t result = lseek(fd, offs, whence);
181
182    return (result == (off_t)-1) ? -errno : result;
183}
184
185
186SyscallReturn
187munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
188{
189    // given that we don't really implement mmap, munmap is really easy
190    return 0;
191}
192
193
194const char *hostname = "m5.eecs.umich.edu";
195
196SyscallReturn
197gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
198{
199    int name_len = tc->getSyscallArg(1);
200    BufferArg name(tc->getSyscallArg(0), name_len);
201
202    strncpy((char *)name.bufferPtr(), hostname, name_len);
203
204    name.copyOut(tc->getMemPort());
205
206    return 0;
207}
208
209SyscallReturn
210unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
211{
212    string path;
213
214    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
215        return (TheISA::IntReg)-EFAULT;
216
217    // Adjust path for current working directory
218    path = p->fullPath(path);
219
220    int result = unlink(path.c_str());
221    return (result == -1) ? -errno : result;
222}
223
224SyscallReturn
225renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
226{
227    string old_name;
228
229    if (!tc->getMemPort()->tryReadString(old_name, tc->getSyscallArg(0)))
230        return -EFAULT;
231
232    string new_name;
233
234    if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1)))
235        return -EFAULT;
236
237    // Adjust path for current working directory
238    old_name = p->fullPath(old_name);
239    new_name = p->fullPath(new_name);
240
241    int64_t result = rename(old_name.c_str(), new_name.c_str());
242    return (result == -1) ? -errno : result;
243}
244
245SyscallReturn
246truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
247{
248    string path;
249
250    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
251        return -EFAULT;
252
253    off_t length = tc->getSyscallArg(1);
254
255    // Adjust path for current working directory
256    path = p->fullPath(path);
257
258    int result = truncate(path.c_str(), length);
259    return (result == -1) ? -errno : result;
260}
261
262SyscallReturn
263ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
264{
265    int fd = process->sim_fd(tc->getSyscallArg(0));
266
267    if (fd < 0)
268        return -EBADF;
269
270    off_t length = tc->getSyscallArg(1);
271
272    int result = ftruncate(fd, length);
273    return (result == -1) ? -errno : result;
274}
275
276SyscallReturn
277chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
278{
279    string path;
280
281    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
282        return -EFAULT;
283
284    /* XXX endianess */
285    uint32_t owner = tc->getSyscallArg(1);
286    uid_t hostOwner = owner;
287    uint32_t group = tc->getSyscallArg(2);
288    gid_t hostGroup = group;
289
290    // Adjust path for current working directory
291    path = p->fullPath(path);
292
293    int result = chown(path.c_str(), hostOwner, hostGroup);
294    return (result == -1) ? -errno : result;
295}
296
297SyscallReturn
298fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
299{
300    int fd = process->sim_fd(tc->getSyscallArg(0));
301
302    if (fd < 0)
303        return -EBADF;
304
305    /* XXX endianess */
306    uint32_t owner = tc->getSyscallArg(1);
307    uid_t hostOwner = owner;
308    uint32_t group = tc->getSyscallArg(2);
309    gid_t hostGroup = group;
310
311    int result = fchown(fd, hostOwner, hostGroup);
312    return (result == -1) ? -errno : result;
313}
314
315
316SyscallReturn
317dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
318{
319    int fd = process->sim_fd(tc->getSyscallArg(0));
320
321    if (fd < 0)
322        return -EBADF;
323
324    int result = dup(fd);
325    return (result == -1) ? -errno : process->alloc_fd(result);
326}
327
328
329SyscallReturn
330fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
331          ThreadContext *tc)
332{
333    int fd = tc->getSyscallArg(0);
334
335    if (fd < 0 || process->sim_fd(fd) < 0)
336        return -EBADF;
337
338    int cmd = tc->getSyscallArg(1);
339    switch (cmd) {
340      case 0: // F_DUPFD
341        // if we really wanted to support this, we'd need to do it
342        // in the target fd space.
343        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
344        return -EMFILE;
345
346      case 1: // F_GETFD (get close-on-exec flag)
347      case 2: // F_SETFD (set close-on-exec flag)
348        return 0;
349
350      case 3: // F_GETFL (get file flags)
351      case 4: // F_SETFL (set file flags)
352        // not sure if this is totally valid, but we'll pass it through
353        // to the underlying OS
354        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
355        return fcntl(process->sim_fd(fd), cmd);
356        // return 0;
357
358      case 7: // F_GETLK  (get lock)
359      case 8: // F_SETLK  (set lock)
360      case 9: // F_SETLKW (set lock and wait)
361        // don't mess with file locking... just act like it's OK
362        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
363        return 0;
364
365      default:
366        warn("Unknown fcntl command %d\n", cmd);
367        return 0;
368    }
369}
370
371SyscallReturn
372fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
373            ThreadContext *tc)
374{
375    int fd = tc->getSyscallArg(0);
376
377    if (fd < 0 || process->sim_fd(fd) < 0)
378        return -EBADF;
379
380    int cmd = tc->getSyscallArg(1);
381    switch (cmd) {
382      case 33: //F_GETLK64
383        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
384        return -EMFILE;
385
386      case 34: // F_SETLK64
387      case 35: // F_SETLKW64
388        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
389        return -EMFILE;
390
391      default:
392        // not sure if this is totally valid, but we'll pass it through
393        // to the underlying OS
394        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
395        return fcntl(process->sim_fd(fd), cmd);
396        // return 0;
397    }
398}
399
400SyscallReturn
401pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
402         ThreadContext *tc)
403{
404    int fds[2], sim_fds[2];
405    int pipe_retval = pipe(fds);
406
407    if (pipe_retval < 0) {
408        // error
409        return pipe_retval;
410    }
411
412    sim_fds[0] = process->alloc_fd(fds[0]);
413    sim_fds[1] = process->alloc_fd(fds[1]);
414
415    // Alpha Linux convention for pipe() is that fd[0] is returned as
416    // the return value of the function, and fd[1] is returned in r20.
417    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
418    return sim_fds[0];
419}
420
421
422SyscallReturn
423getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
424           ThreadContext *tc)
425{
426    // Make up a PID.  There's no interprocess communication in
427    // fake_syscall mode, so there's no way for a process to know it's
428    // not getting a unique value.
429
430    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
431    return process->pid();
432}
433
434
435SyscallReturn
436getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
437           ThreadContext *tc)
438{
439    // Make up a UID and EUID... it shouldn't matter, and we want the
440    // simulation to be deterministic.
441
442    // EUID goes in r20.
443    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
444    return process->uid();		// UID
445}
446
447
448SyscallReturn
449getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
450           ThreadContext *tc)
451{
452    // Get current group ID.  EGID goes in r20.
453    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
454    return process->gid();
455}
456
457
458SyscallReturn
459setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
460           ThreadContext *tc)
461{
462    // can't fathom why a benchmark would call this.
463    warn("Ignoring call to setuid(%d)\n", tc->getSyscallArg(0));
464    return 0;
465}
466
467SyscallReturn
468getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
469           ThreadContext *tc)
470{
471    // Make up a PID.  There's no interprocess communication in
472    // fake_syscall mode, so there's no way for a process to know it's
473    // not getting a unique value.
474
475    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
476    return process->pid();
477}
478
479SyscallReturn
480getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
481           ThreadContext *tc)
482{
483    return process->ppid();
484}
485
486SyscallReturn
487getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
488           ThreadContext *tc)
489{
490    return process->uid();		// UID
491}
492
493SyscallReturn
494geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
495           ThreadContext *tc)
496{
497    return process->euid();		// UID
498}
499
500SyscallReturn
501getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
502           ThreadContext *tc)
503{
504    return process->gid();
505}
506
507SyscallReturn
508getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
509           ThreadContext *tc)
510{
511    return process->egid();
512}
513
514
515