syscall_emul.cc revision 6029
12817Sksewell@umich.edu/*
22817Sksewell@umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
32817Sksewell@umich.edu * All rights reserved.
42817Sksewell@umich.edu *
52817Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
62817Sksewell@umich.edu * modification, are permitted provided that the following conditions are
72817Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
82817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
92817Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
102817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
112817Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
122817Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
132817Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
142817Sksewell@umich.edu * this software without specific prior written permission.
152817Sksewell@umich.edu *
162817Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172817Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182817Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192817Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202817Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212817Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222817Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232817Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242817Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252817Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262817Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272817Sksewell@umich.edu *
282817Sksewell@umich.edu * Authors: Steve Reinhardt
292817Sksewell@umich.edu *          Ali Saidi
302817Sksewell@umich.edu */
312817Sksewell@umich.edu
322817Sksewell@umich.edu#include <fcntl.h>
332817Sksewell@umich.edu#include <unistd.h>
342935Sksewell@umich.edu
352817Sksewell@umich.edu#include <string>
362817Sksewell@umich.edu#include <iostream>
372834Sksewell@umich.edu
382834Sksewell@umich.edu#include "sim/syscall_emul.hh"
392834Sksewell@umich.edu#include "base/chunk_generator.hh"
402834Sksewell@umich.edu#include "base/trace.hh"
412834Sksewell@umich.edu#include "cpu/thread_context.hh"
422834Sksewell@umich.edu#include "cpu/base.hh"
432834Sksewell@umich.edu#include "mem/page_table.hh"
442817Sksewell@umich.edu#include "sim/process.hh"
452817Sksewell@umich.edu#include "sim/system.hh"
462817Sksewell@umich.edu
472817Sksewell@umich.edu#include "sim/sim_exit.hh"
482817Sksewell@umich.edu
492817Sksewell@umich.eduusing namespace std;
502817Sksewell@umich.eduusing namespace TheISA;
512817Sksewell@umich.edu
522817Sksewell@umich.eduvoid
532817Sksewell@umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
542817Sksewell@umich.edu{
552817Sksewell@umich.edu    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
562817Sksewell@umich.edu             curTick,tc->getCpuPtr()->name(), name,
572817Sksewell@umich.edu             process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1),
582817Sksewell@umich.edu             process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 3));
592817Sksewell@umich.edu
602817Sksewell@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
612817Sksewell@umich.edu
622817Sksewell@umich.edu    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
632817Sksewell@umich.edu             curTick,tc->getCpuPtr()->name(), name, retval.value());
642817Sksewell@umich.edu
652817Sksewell@umich.edu    if (!(flags & SyscallDesc::SuppressReturnValue))
662817Sksewell@umich.edu        process->setSyscallReturn(tc, retval);
672817Sksewell@umich.edu}
682817Sksewell@umich.edu
692817Sksewell@umich.edu
702817Sksewell@umich.eduSyscallReturn
712817Sksewell@umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
722817Sksewell@umich.edu                  ThreadContext *tc)
732817Sksewell@umich.edu{
742817Sksewell@umich.edu    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
752817Sksewell@umich.edu
762817Sksewell@umich.edu    return 1;
772817Sksewell@umich.edu}
782817Sksewell@umich.edu
792817Sksewell@umich.edu
802817Sksewell@umich.eduSyscallReturn
812817Sksewell@umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
822817Sksewell@umich.edu           ThreadContext *tc)
832817Sksewell@umich.edu{
842817Sksewell@umich.edu    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
852817Sksewell@umich.edu         process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1));
862817Sksewell@umich.edu
872817Sksewell@umich.edu    return 0;
882817Sksewell@umich.edu}
892817Sksewell@umich.edu
902817Sksewell@umich.edu
912817Sksewell@umich.eduSyscallReturn
922817Sksewell@umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
932817Sksewell@umich.edu         ThreadContext *tc)
942817Sksewell@umich.edu{
952817Sksewell@umich.edu    if (process->system->numRunningContexts() == 1) {
962817Sksewell@umich.edu        // Last running context... exit simulator
972817Sksewell@umich.edu        exitSimLoop("target called exit()",
982817Sksewell@umich.edu                    process->getSyscallArg(tc, 0) & 0xff);
992817Sksewell@umich.edu    } else {
1002817Sksewell@umich.edu        // other running threads... just halt this one
1012817Sksewell@umich.edu        tc->halt();
1022817Sksewell@umich.edu    }
1032817Sksewell@umich.edu
1042817Sksewell@umich.edu    return 1;
1052817Sksewell@umich.edu}
1062817Sksewell@umich.edu
1072817Sksewell@umich.edu
1082817Sksewell@umich.eduSyscallReturn
1092817Sksewell@umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1102817Sksewell@umich.edu{
1112817Sksewell@umich.edu    return (int)VMPageSize;
1122817Sksewell@umich.edu}
1132817Sksewell@umich.edu
1142817Sksewell@umich.edu
1152875Sksewell@umich.eduSyscallReturn
1162817Sksewell@umich.edubrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1172817Sksewell@umich.edu{
1182817Sksewell@umich.edu    // change brk addr to first arg
1192817Sksewell@umich.edu    Addr new_brk = p->getSyscallArg(tc, 0);
1202817Sksewell@umich.edu
1212817Sksewell@umich.edu    // in Linux at least, brk(0) returns the current break value
1222817Sksewell@umich.edu    // (note that the syscall and the glibc function have different behavior)
1232817Sksewell@umich.edu    if (new_brk == 0)
1242817Sksewell@umich.edu        return p->brk_point;
1252817Sksewell@umich.edu
1262817Sksewell@umich.edu    if (new_brk > p->brk_point) {
1272817Sksewell@umich.edu        // might need to allocate some new pages
1282817Sksewell@umich.edu        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1292817Sksewell@umich.edu                                VMPageSize); !gen.done(); gen.next()) {
1302817Sksewell@umich.edu            if (!p->pTable->translate(gen.addr()))
1312817Sksewell@umich.edu                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1322817Sksewell@umich.edu                                    VMPageSize);
1332817Sksewell@umich.edu        }
1342817Sksewell@umich.edu    }
1352817Sksewell@umich.edu
1362817Sksewell@umich.edu    p->brk_point = new_brk;
1372817Sksewell@umich.edu    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1382817Sksewell@umich.edu    return p->brk_point;
1392817Sksewell@umich.edu}
1402817Sksewell@umich.edu
1412817Sksewell@umich.edu
1422817Sksewell@umich.eduSyscallReturn
1432817Sksewell@umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1442817Sksewell@umich.edu{
1452817Sksewell@umich.edu    int target_fd = p->getSyscallArg(tc, 0);
1462817Sksewell@umich.edu    int status = close(p->sim_fd(target_fd));
1472817Sksewell@umich.edu    if (status >= 0)
1482817Sksewell@umich.edu        p->free_fd(target_fd);
1492817Sksewell@umich.edu    return status;
1502817Sksewell@umich.edu}
1512817Sksewell@umich.edu
1522817Sksewell@umich.edu
1532817Sksewell@umich.eduSyscallReturn
1542817Sksewell@umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1552817Sksewell@umich.edu{
1562817Sksewell@umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1572817Sksewell@umich.edu    int nbytes = p->getSyscallArg(tc, 2);
1582817Sksewell@umich.edu    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
1592817Sksewell@umich.edu
1602817Sksewell@umich.edu    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
1612817Sksewell@umich.edu
1622817Sksewell@umich.edu    if (bytes_read != -1)
1632817Sksewell@umich.edu        bufArg.copyOut(tc->getMemPort());
1642817Sksewell@umich.edu
1652817Sksewell@umich.edu    return bytes_read;
1662817Sksewell@umich.edu}
1672817Sksewell@umich.edu
1682817Sksewell@umich.eduSyscallReturn
1692817Sksewell@umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1702817Sksewell@umich.edu{
1712817Sksewell@umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1722817Sksewell@umich.edu    int nbytes = p->getSyscallArg(tc, 2);
1732817Sksewell@umich.edu    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
1742817Sksewell@umich.edu
1752817Sksewell@umich.edu    bufArg.copyIn(tc->getMemPort());
1762817Sksewell@umich.edu
1772817Sksewell@umich.edu    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
1782817Sksewell@umich.edu
1792817Sksewell@umich.edu    fsync(fd);
1802817Sksewell@umich.edu
1812817Sksewell@umich.edu    return bytes_written;
1822817Sksewell@umich.edu}
1832817Sksewell@umich.edu
1842817Sksewell@umich.edu
1852817Sksewell@umich.eduSyscallReturn
1862817Sksewell@umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1872817Sksewell@umich.edu{
1882817Sksewell@umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1892817Sksewell@umich.edu    uint64_t offs = p->getSyscallArg(tc, 1);
1902817Sksewell@umich.edu    int whence = p->getSyscallArg(tc, 2);
1912817Sksewell@umich.edu
1922817Sksewell@umich.edu    off_t result = lseek(fd, offs, whence);
1932817Sksewell@umich.edu
1942817Sksewell@umich.edu    return (result == (off_t)-1) ? -errno : result;
1952817Sksewell@umich.edu}
1962817Sksewell@umich.edu
1972817Sksewell@umich.edu
1982817Sksewell@umich.eduSyscallReturn
1992817Sksewell@umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2002817Sksewell@umich.edu{
2012817Sksewell@umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
2022817Sksewell@umich.edu    uint64_t offset_high = p->getSyscallArg(tc, 1);
2032817Sksewell@umich.edu    uint32_t offset_low = p->getSyscallArg(tc, 2);
2042817Sksewell@umich.edu    Addr result_ptr = p->getSyscallArg(tc, 3);
2052817Sksewell@umich.edu    int whence = p->getSyscallArg(tc, 4);
2062817Sksewell@umich.edu
2072817Sksewell@umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2082817Sksewell@umich.edu
2092817Sksewell@umich.edu    uint64_t result = lseek(fd, offset, whence);
2102817Sksewell@umich.edu    result = TheISA::htog(result);
2112817Sksewell@umich.edu
2122817Sksewell@umich.edu    if (result == (off_t)-1) {
2132817Sksewell@umich.edu        //The seek failed.
2142817Sksewell@umich.edu        return -errno;
2152817Sksewell@umich.edu    } else {
2162817Sksewell@umich.edu        //The seek succeeded.
2172817Sksewell@umich.edu        //Copy "result" to "result_ptr"
2182817Sksewell@umich.edu        //XXX We'll assume that the size of loff_t is 64 bits on the
2192817Sksewell@umich.edu        //target platform
2202817Sksewell@umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2212817Sksewell@umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2222817Sksewell@umich.edu        result_buf.copyOut(tc->getMemPort());
2232817Sksewell@umich.edu        return 0;
2242817Sksewell@umich.edu    }
2252817Sksewell@umich.edu
2262817Sksewell@umich.edu
2272817Sksewell@umich.edu    return (result == (off_t)-1) ? -errno : result;
2282817Sksewell@umich.edu}
2292817Sksewell@umich.edu
2302817Sksewell@umich.edu
2312817Sksewell@umich.eduSyscallReturn
2322817Sksewell@umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2332817Sksewell@umich.edu{
2342817Sksewell@umich.edu    // given that we don't really implement mmap, munmap is really easy
2352817Sksewell@umich.edu    return 0;
2362817Sksewell@umich.edu}
2372817Sksewell@umich.edu
2382817Sksewell@umich.edu
2392817Sksewell@umich.educonst char *hostname = "m5.eecs.umich.edu";
2402817Sksewell@umich.edu
2412817Sksewell@umich.eduSyscallReturn
2422817Sksewell@umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2432817Sksewell@umich.edu{
2442817Sksewell@umich.edu    int name_len = p->getSyscallArg(tc, 1);
2452817Sksewell@umich.edu    BufferArg name(p->getSyscallArg(tc, 0), name_len);
2462817Sksewell@umich.edu
2472817Sksewell@umich.edu    strncpy((char *)name.bufferPtr(), hostname, name_len);
2482817Sksewell@umich.edu
2492817Sksewell@umich.edu    name.copyOut(tc->getMemPort());
2502817Sksewell@umich.edu
251    return 0;
252}
253
254SyscallReturn
255getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
256{
257    int result = 0;
258    unsigned long size = p->getSyscallArg(tc, 1);
259    BufferArg buf(p->getSyscallArg(tc, 0), size);
260
261    // Is current working directory defined?
262    string cwd = p->getcwd();
263    if (!cwd.empty()) {
264        if (cwd.length() >= size) {
265            // Buffer too small
266            return -ERANGE;
267        }
268        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
269        result = cwd.length();
270    }
271    else {
272        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
273            result = strlen((char *)buf.bufferPtr());
274        }
275        else {
276            result = -1;
277        }
278    }
279
280    buf.copyOut(tc->getMemPort());
281
282    return (result == -1) ? -errno : result;
283}
284
285
286SyscallReturn
287readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
288{
289    string path;
290
291    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
292        return (TheISA::IntReg)-EFAULT;
293
294    // Adjust path for current working directory
295    path = p->fullPath(path);
296
297    size_t bufsiz = p->getSyscallArg(tc, 2);
298    BufferArg buf(p->getSyscallArg(tc, 1), bufsiz);
299
300    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
301
302    buf.copyOut(tc->getMemPort());
303
304    return (result == -1) ? -errno : result;
305}
306
307SyscallReturn
308unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
309{
310    string path;
311
312    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
313        return (TheISA::IntReg)-EFAULT;
314
315    // Adjust path for current working directory
316    path = p->fullPath(path);
317
318    int result = unlink(path.c_str());
319    return (result == -1) ? -errno : result;
320}
321
322
323SyscallReturn
324mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
325{
326    string path;
327
328    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
329        return (TheISA::IntReg)-EFAULT;
330
331    // Adjust path for current working directory
332    path = p->fullPath(path);
333
334    mode_t mode = p->getSyscallArg(tc, 1);
335
336    int result = mkdir(path.c_str(), mode);
337    return (result == -1) ? -errno : result;
338}
339
340SyscallReturn
341renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
342{
343    string old_name;
344
345    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0)))
346        return -EFAULT;
347
348    string new_name;
349
350    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1)))
351        return -EFAULT;
352
353    // Adjust path for current working directory
354    old_name = p->fullPath(old_name);
355    new_name = p->fullPath(new_name);
356
357    int64_t result = rename(old_name.c_str(), new_name.c_str());
358    return (result == -1) ? -errno : result;
359}
360
361SyscallReturn
362truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
363{
364    string path;
365
366    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
367        return -EFAULT;
368
369    off_t length = p->getSyscallArg(tc, 1);
370
371    // Adjust path for current working directory
372    path = p->fullPath(path);
373
374    int result = truncate(path.c_str(), length);
375    return (result == -1) ? -errno : result;
376}
377
378SyscallReturn
379ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
380{
381    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
382
383    if (fd < 0)
384        return -EBADF;
385
386    off_t length = process->getSyscallArg(tc, 1);
387
388    int result = ftruncate(fd, length);
389    return (result == -1) ? -errno : result;
390}
391
392SyscallReturn
393umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
394{
395    // Letting the simulated program change the simulator's umask seems like
396    // a bad idea.  Compromise by just returning the current umask but not
397    // changing anything.
398    mode_t oldMask = umask(0);
399    umask(oldMask);
400    return (int)oldMask;
401}
402
403SyscallReturn
404chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
405{
406    string path;
407
408    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
409        return -EFAULT;
410
411    /* XXX endianess */
412    uint32_t owner = p->getSyscallArg(tc, 1);
413    uid_t hostOwner = owner;
414    uint32_t group = p->getSyscallArg(tc, 2);
415    gid_t hostGroup = group;
416
417    // Adjust path for current working directory
418    path = p->fullPath(path);
419
420    int result = chown(path.c_str(), hostOwner, hostGroup);
421    return (result == -1) ? -errno : result;
422}
423
424SyscallReturn
425fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
426{
427    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
428
429    if (fd < 0)
430        return -EBADF;
431
432    /* XXX endianess */
433    uint32_t owner = process->getSyscallArg(tc, 1);
434    uid_t hostOwner = owner;
435    uint32_t group = process->getSyscallArg(tc, 2);
436    gid_t hostGroup = group;
437
438    int result = fchown(fd, hostOwner, hostGroup);
439    return (result == -1) ? -errno : result;
440}
441
442
443SyscallReturn
444dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
445{
446    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
447    if (fd < 0)
448        return -EBADF;
449
450    Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0));
451
452    int result = dup(fd);
453    return (result == -1) ? -errno : process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
454}
455
456
457SyscallReturn
458fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
459          ThreadContext *tc)
460{
461    int fd = process->getSyscallArg(tc, 0);
462
463    if (fd < 0 || process->sim_fd(fd) < 0)
464        return -EBADF;
465
466    int cmd = process->getSyscallArg(tc, 1);
467    switch (cmd) {
468      case 0: // F_DUPFD
469        // if we really wanted to support this, we'd need to do it
470        // in the target fd space.
471        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
472        return -EMFILE;
473
474      case 1: // F_GETFD (get close-on-exec flag)
475      case 2: // F_SETFD (set close-on-exec flag)
476        return 0;
477
478      case 3: // F_GETFL (get file flags)
479      case 4: // F_SETFL (set file flags)
480        // not sure if this is totally valid, but we'll pass it through
481        // to the underlying OS
482        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
483        return fcntl(process->sim_fd(fd), cmd);
484        // return 0;
485
486      case 7: // F_GETLK  (get lock)
487      case 8: // F_SETLK  (set lock)
488      case 9: // F_SETLKW (set lock and wait)
489        // don't mess with file locking... just act like it's OK
490        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
491        return 0;
492
493      default:
494        warn("Unknown fcntl command %d\n", cmd);
495        return 0;
496    }
497}
498
499SyscallReturn
500fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
501            ThreadContext *tc)
502{
503    int fd = process->getSyscallArg(tc, 0);
504
505    if (fd < 0 || process->sim_fd(fd) < 0)
506        return -EBADF;
507
508    int cmd = process->getSyscallArg(tc, 1);
509    switch (cmd) {
510      case 33: //F_GETLK64
511        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
512        return -EMFILE;
513
514      case 34: // F_SETLK64
515      case 35: // F_SETLKW64
516        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
517        return -EMFILE;
518
519      default:
520        // not sure if this is totally valid, but we'll pass it through
521        // to the underlying OS
522        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
523        return fcntl(process->sim_fd(fd), cmd);
524        // return 0;
525    }
526}
527
528SyscallReturn
529pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
530         ThreadContext *tc)
531{
532    int fds[2], sim_fds[2];
533    int pipe_retval = pipe(fds);
534
535    if (pipe_retval < 0) {
536        // error
537        return pipe_retval;
538    }
539
540    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
541    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
542
543    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
544    // Alpha Linux convention for pipe() is that fd[0] is returned as
545    // the return value of the function, and fd[1] is returned in r20.
546    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
547    return sim_fds[0];
548}
549
550
551SyscallReturn
552getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
553           ThreadContext *tc)
554{
555    // Make up a PID.  There's no interprocess communication in
556    // fake_syscall mode, so there's no way for a process to know it's
557    // not getting a unique value.
558
559    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
560    return process->pid();
561}
562
563
564SyscallReturn
565getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
566           ThreadContext *tc)
567{
568    // Make up a UID and EUID... it shouldn't matter, and we want the
569    // simulation to be deterministic.
570
571    // EUID goes in r20.
572    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
573    return process->uid();              // UID
574}
575
576
577SyscallReturn
578getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
579           ThreadContext *tc)
580{
581    // Get current group ID.  EGID goes in r20.
582    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
583    return process->gid();
584}
585
586
587SyscallReturn
588setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
589           ThreadContext *tc)
590{
591    // can't fathom why a benchmark would call this.
592    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0));
593    return 0;
594}
595
596SyscallReturn
597getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
598           ThreadContext *tc)
599{
600    // Make up a PID.  There's no interprocess communication in
601    // fake_syscall mode, so there's no way for a process to know it's
602    // not getting a unique value.
603
604    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
605    return process->pid();
606}
607
608SyscallReturn
609getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
610           ThreadContext *tc)
611{
612    return process->ppid();
613}
614
615SyscallReturn
616getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
617           ThreadContext *tc)
618{
619    return process->uid();              // UID
620}
621
622SyscallReturn
623geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
624           ThreadContext *tc)
625{
626    return process->euid();             // UID
627}
628
629SyscallReturn
630getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
631           ThreadContext *tc)
632{
633    return process->gid();
634}
635
636SyscallReturn
637getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
638           ThreadContext *tc)
639{
640    return process->egid();
641}
642
643
644