syscall_emul.cc revision 5958:2d9737bf3c2f
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             process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1),
57             process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 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        process->setSyscallReturn(tc, 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         process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 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()",
96                process->getSyscallArg(tc, 0) & 0xff);
97    }
98
99    return 1;
100}
101
102
103SyscallReturn
104getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
105{
106    return (int)VMPageSize;
107}
108
109
110SyscallReturn
111brkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
112{
113    // change brk addr to first arg
114    Addr new_brk = p->getSyscallArg(tc, 0);
115
116    // in Linux at least, brk(0) returns the current break value
117    // (note that the syscall and the glibc function have different behavior)
118    if (new_brk == 0)
119        return p->brk_point;
120
121    if (new_brk > p->brk_point) {
122        // might need to allocate some new pages
123        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
124                                VMPageSize); !gen.done(); gen.next()) {
125            if (!p->pTable->translate(gen.addr()))
126                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
127                                    VMPageSize);
128        }
129    }
130
131    p->brk_point = new_brk;
132    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
133    return p->brk_point;
134}
135
136
137SyscallReturn
138closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
139{
140    int target_fd = p->getSyscallArg(tc, 0);
141    int status = close(p->sim_fd(target_fd));
142    if (status >= 0)
143        p->free_fd(target_fd);
144    return status;
145}
146
147
148SyscallReturn
149readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
150{
151    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
152    int nbytes = p->getSyscallArg(tc, 2);
153    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
154
155    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
156
157    if (bytes_read != -1)
158        bufArg.copyOut(tc->getMemPort());
159
160    return bytes_read;
161}
162
163SyscallReturn
164writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
165{
166    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
167    int nbytes = p->getSyscallArg(tc, 2);
168    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
169
170    bufArg.copyIn(tc->getMemPort());
171
172    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
173
174    fsync(fd);
175
176    return bytes_written;
177}
178
179
180SyscallReturn
181lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
182{
183    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
184    uint64_t offs = p->getSyscallArg(tc, 1);
185    int whence = p->getSyscallArg(tc, 2);
186
187    off_t result = lseek(fd, offs, whence);
188
189    return (result == (off_t)-1) ? -errno : result;
190}
191
192
193SyscallReturn
194_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
195{
196    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
197    uint64_t offset_high = p->getSyscallArg(tc, 1);
198    uint32_t offset_low = p->getSyscallArg(tc, 2);
199    Addr result_ptr = p->getSyscallArg(tc, 3);
200    int whence = p->getSyscallArg(tc, 4);
201
202    uint64_t offset = (offset_high << 32) | offset_low;
203
204    uint64_t result = lseek(fd, offset, whence);
205    result = TheISA::htog(result);
206
207    if (result == (off_t)-1) {
208        //The seek failed.
209        return -errno;
210    } else {
211        //The seek succeeded.
212        //Copy "result" to "result_ptr"
213        //XXX We'll assume that the size of loff_t is 64 bits on the
214        //target platform
215        BufferArg result_buf(result_ptr, sizeof(result));
216        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
217        result_buf.copyOut(tc->getMemPort());
218        return 0;
219    }
220
221
222    return (result == (off_t)-1) ? -errno : result;
223}
224
225
226SyscallReturn
227munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
228{
229    // given that we don't really implement mmap, munmap is really easy
230    return 0;
231}
232
233
234const char *hostname = "m5.eecs.umich.edu";
235
236SyscallReturn
237gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
238{
239    int name_len = p->getSyscallArg(tc, 1);
240    BufferArg name(p->getSyscallArg(tc, 0), name_len);
241
242    strncpy((char *)name.bufferPtr(), hostname, name_len);
243
244    name.copyOut(tc->getMemPort());
245
246    return 0;
247}
248
249SyscallReturn
250getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
251{
252    int result = 0;
253    unsigned long size = p->getSyscallArg(tc, 1);
254    BufferArg buf(p->getSyscallArg(tc, 0), size);
255
256    // Is current working directory defined?
257    string cwd = p->getcwd();
258    if (!cwd.empty()) {
259        if (cwd.length() >= size) {
260            // Buffer too small
261            return -ERANGE;
262        }
263        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
264        result = cwd.length();
265    }
266    else {
267        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
268            result = strlen((char *)buf.bufferPtr());
269        }
270        else {
271            result = -1;
272        }
273    }
274
275    buf.copyOut(tc->getMemPort());
276
277    return (result == -1) ? -errno : result;
278}
279
280
281SyscallReturn
282readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
283{
284    string path;
285
286    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
287        return (TheISA::IntReg)-EFAULT;
288
289    // Adjust path for current working directory
290    path = p->fullPath(path);
291
292    size_t bufsiz = p->getSyscallArg(tc, 2);
293    BufferArg buf(p->getSyscallArg(tc, 1), bufsiz);
294
295    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
296
297    buf.copyOut(tc->getMemPort());
298
299    return (result == -1) ? -errno : result;
300}
301
302SyscallReturn
303unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
304{
305    string path;
306
307    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
308        return (TheISA::IntReg)-EFAULT;
309
310    // Adjust path for current working directory
311    path = p->fullPath(path);
312
313    int result = unlink(path.c_str());
314    return (result == -1) ? -errno : result;
315}
316
317
318SyscallReturn
319mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
320{
321    string path;
322
323    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
324        return (TheISA::IntReg)-EFAULT;
325
326    // Adjust path for current working directory
327    path = p->fullPath(path);
328
329    mode_t mode = p->getSyscallArg(tc, 1);
330
331    int result = mkdir(path.c_str(), mode);
332    return (result == -1) ? -errno : result;
333}
334
335SyscallReturn
336renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
337{
338    string old_name;
339
340    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0)))
341        return -EFAULT;
342
343    string new_name;
344
345    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1)))
346        return -EFAULT;
347
348    // Adjust path for current working directory
349    old_name = p->fullPath(old_name);
350    new_name = p->fullPath(new_name);
351
352    int64_t result = rename(old_name.c_str(), new_name.c_str());
353    return (result == -1) ? -errno : result;
354}
355
356SyscallReturn
357truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
358{
359    string path;
360
361    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
362        return -EFAULT;
363
364    off_t length = p->getSyscallArg(tc, 1);
365
366    // Adjust path for current working directory
367    path = p->fullPath(path);
368
369    int result = truncate(path.c_str(), length);
370    return (result == -1) ? -errno : result;
371}
372
373SyscallReturn
374ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
375{
376    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
377
378    if (fd < 0)
379        return -EBADF;
380
381    off_t length = process->getSyscallArg(tc, 1);
382
383    int result = ftruncate(fd, length);
384    return (result == -1) ? -errno : result;
385}
386
387SyscallReturn
388umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
389{
390    // Letting the simulated program change the simulator's umask seems like
391    // a bad idea.  Compromise by just returning the current umask but not
392    // changing anything.
393    mode_t oldMask = umask(0);
394    umask(oldMask);
395    return (int)oldMask;
396}
397
398SyscallReturn
399chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
400{
401    string path;
402
403    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
404        return -EFAULT;
405
406    /* XXX endianess */
407    uint32_t owner = p->getSyscallArg(tc, 1);
408    uid_t hostOwner = owner;
409    uint32_t group = p->getSyscallArg(tc, 2);
410    gid_t hostGroup = group;
411
412    // Adjust path for current working directory
413    path = p->fullPath(path);
414
415    int result = chown(path.c_str(), hostOwner, hostGroup);
416    return (result == -1) ? -errno : result;
417}
418
419SyscallReturn
420fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
421{
422    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
423
424    if (fd < 0)
425        return -EBADF;
426
427    /* XXX endianess */
428    uint32_t owner = process->getSyscallArg(tc, 1);
429    uid_t hostOwner = owner;
430    uint32_t group = process->getSyscallArg(tc, 2);
431    gid_t hostGroup = group;
432
433    int result = fchown(fd, hostOwner, hostGroup);
434    return (result == -1) ? -errno : result;
435}
436
437
438SyscallReturn
439dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
440{
441    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
442    if (fd < 0)
443        return -EBADF;
444
445    Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0));
446
447    int result = dup(fd);
448    return (result == -1) ? -errno : process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
449}
450
451
452SyscallReturn
453fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
454          ThreadContext *tc)
455{
456    int fd = process->getSyscallArg(tc, 0);
457
458    if (fd < 0 || process->sim_fd(fd) < 0)
459        return -EBADF;
460
461    int cmd = process->getSyscallArg(tc, 1);
462    switch (cmd) {
463      case 0: // F_DUPFD
464        // if we really wanted to support this, we'd need to do it
465        // in the target fd space.
466        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
467        return -EMFILE;
468
469      case 1: // F_GETFD (get close-on-exec flag)
470      case 2: // F_SETFD (set close-on-exec flag)
471        return 0;
472
473      case 3: // F_GETFL (get file flags)
474      case 4: // F_SETFL (set file flags)
475        // not sure if this is totally valid, but we'll pass it through
476        // to the underlying OS
477        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
478        return fcntl(process->sim_fd(fd), cmd);
479        // return 0;
480
481      case 7: // F_GETLK  (get lock)
482      case 8: // F_SETLK  (set lock)
483      case 9: // F_SETLKW (set lock and wait)
484        // don't mess with file locking... just act like it's OK
485        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
486        return 0;
487
488      default:
489        warn("Unknown fcntl command %d\n", cmd);
490        return 0;
491    }
492}
493
494SyscallReturn
495fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
496            ThreadContext *tc)
497{
498    int fd = process->getSyscallArg(tc, 0);
499
500    if (fd < 0 || process->sim_fd(fd) < 0)
501        return -EBADF;
502
503    int cmd = process->getSyscallArg(tc, 1);
504    switch (cmd) {
505      case 33: //F_GETLK64
506        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
507        return -EMFILE;
508
509      case 34: // F_SETLK64
510      case 35: // F_SETLKW64
511        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
512        return -EMFILE;
513
514      default:
515        // not sure if this is totally valid, but we'll pass it through
516        // to the underlying OS
517        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
518        return fcntl(process->sim_fd(fd), cmd);
519        // return 0;
520    }
521}
522
523SyscallReturn
524pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
525         ThreadContext *tc)
526{
527    int fds[2], sim_fds[2];
528    int pipe_retval = pipe(fds);
529
530    if (pipe_retval < 0) {
531        // error
532        return pipe_retval;
533    }
534
535    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
536    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
537
538    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
539    // Alpha Linux convention for pipe() is that fd[0] is returned as
540    // the return value of the function, and fd[1] is returned in r20.
541    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
542    return sim_fds[0];
543}
544
545
546SyscallReturn
547getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
548           ThreadContext *tc)
549{
550    // Make up a PID.  There's no interprocess communication in
551    // fake_syscall mode, so there's no way for a process to know it's
552    // not getting a unique value.
553
554    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
555    return process->pid();
556}
557
558
559SyscallReturn
560getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
561           ThreadContext *tc)
562{
563    // Make up a UID and EUID... it shouldn't matter, and we want the
564    // simulation to be deterministic.
565
566    // EUID goes in r20.
567    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
568    return process->uid();              // UID
569}
570
571
572SyscallReturn
573getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
574           ThreadContext *tc)
575{
576    // Get current group ID.  EGID goes in r20.
577    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
578    return process->gid();
579}
580
581
582SyscallReturn
583setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
584           ThreadContext *tc)
585{
586    // can't fathom why a benchmark would call this.
587    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0));
588    return 0;
589}
590
591SyscallReturn
592getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
593           ThreadContext *tc)
594{
595    // Make up a PID.  There's no interprocess communication in
596    // fake_syscall mode, so there's no way for a process to know it's
597    // not getting a unique value.
598
599    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
600    return process->pid();
601}
602
603SyscallReturn
604getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
605           ThreadContext *tc)
606{
607    return process->ppid();
608}
609
610SyscallReturn
611getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
612           ThreadContext *tc)
613{
614    return process->uid();              // UID
615}
616
617SyscallReturn
618geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
619           ThreadContext *tc)
620{
621    return process->euid();             // UID
622}
623
624SyscallReturn
625getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
626           ThreadContext *tc)
627{
628    return process->gid();
629}
630
631SyscallReturn
632getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
633           ThreadContext *tc)
634{
635    return process->egid();
636}
637
638
639