syscall_emul.cc revision 6134
16184SN/A/*
26184SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
36184SN/A * All rights reserved.
46184SN/A *
56184SN/A * Redistribution and use in source and binary forms, with or without
66184SN/A * modification, are permitted provided that the following conditions are
76184SN/A * met: redistributions of source code must retain the above copyright
86184SN/A * notice, this list of conditions and the following disclaimer;
96184SN/A * redistributions in binary form must reproduce the above copyright
106184SN/A * notice, this list of conditions and the following disclaimer in the
116184SN/A * documentation and/or other materials provided with the distribution;
126184SN/A * neither the name of the copyright holders nor the names of its
136184SN/A * contributors may be used to endorse or promote products derived from
146184SN/A * this software without specific prior written permission.
156184SN/A *
166184SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176184SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186184SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196184SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206184SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216184SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226184SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236184SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246184SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256184SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266184SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276184SN/A *
286184SN/A * Authors: Steve Reinhardt
296184SN/A *          Ali Saidi
306184SN/A */
316184SN/A
326226Snate@binkert.org#include <fcntl.h>
336184SN/A#include <unistd.h>
346184SN/A
356184SN/A#include <string>
366184SN/A#include <iostream>
376184SN/A
386184SN/A#include "sim/syscall_emul.hh"
396184SN/A#include "base/chunk_generator.hh"
406184SN/A#include "base/trace.hh"
416184SN/A#include "cpu/thread_context.hh"
426184SN/A#include "cpu/base.hh"
436184SN/A#include "mem/page_table.hh"
446184SN/A#include "sim/process.hh"
456184SN/A#include "sim/system.hh"
466184SN/A
476184SN/A#include "sim/sim_exit.hh"
486184SN/A
496184SN/Ausing namespace std;
506184SN/Ausing namespace TheISA;
516184SN/A
526184SN/Avoid
536184SN/ASyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
546184SN/A{
556184SN/A    DPRINTFR(SyscallVerbose,
566184SN/A             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
576184SN/A             curTick, tc->getCpuPtr()->name(), name,
586184SN/A             process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1),
596184SN/A             process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 3));
606184SN/A
616184SN/A    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
626184SN/A
636184SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
646184SN/A             curTick,tc->getCpuPtr()->name(), name, retval.value());
656184SN/A
666184SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
676184SN/A        process->setSyscallReturn(tc, retval);
686184SN/A}
696184SN/A
706184SN/A
716184SN/ASyscallReturn
726184SN/AunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
736184SN/A                  ThreadContext *tc)
746184SN/A{
756184SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
766184SN/A
776184SN/A    return 1;
786184SN/A}
796184SN/A
806184SN/A
816184SN/ASyscallReturn
826184SN/AignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
836184SN/A           ThreadContext *tc)
846184SN/A{
856184SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
866184SN/A         process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1));
876184SN/A
886184SN/A    return 0;
896184SN/A}
906184SN/A
916184SN/A
926184SN/ASyscallReturn
936184SN/AexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
946184SN/A         ThreadContext *tc)
956184SN/A{
966184SN/A    if (process->system->numRunningContexts() == 1) {
976184SN/A        // Last running context... exit simulator
986184SN/A        exitSimLoop("target called exit()",
996184SN/A                    process->getSyscallArg(tc, 0) & 0xff);
1006184SN/A    } else {
1016184SN/A        // other running threads... just halt this one
1026184SN/A        tc->halt();
1036184SN/A    }
1046184SN/A
1056184SN/A    return 1;
1066184SN/A}
1076184SN/A
1086184SN/A
1096184SN/ASyscallReturn
1106184SN/AexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1116184SN/A              ThreadContext *tc)
1126184SN/A{
1136184SN/A    // really should just halt all thread contexts belonging to this
1146184SN/A    // process in case there's another process running...
1156184SN/A    exitSimLoop("target called exit()",
1166184SN/A                process->getSyscallArg(tc, 0) & 0xff);
1176184SN/A
1186184SN/A    return 1;
1196184SN/A}
1206184SN/A
1216184SN/A
1226184SN/ASyscallReturn
1236184SN/AgetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1246184SN/A{
1256184SN/A    return (int)VMPageSize;
1266184SN/A}
1276184SN/A
1286184SN/A
1296184SN/ASyscallReturn
1306184SN/AbrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1316184SN/A{
1326184SN/A    // change brk addr to first arg
1336184SN/A    Addr new_brk = p->getSyscallArg(tc, 0);
1346184SN/A
1356184SN/A    // in Linux at least, brk(0) returns the current break value
1366184SN/A    // (note that the syscall and the glibc function have different behavior)
1376184SN/A    if (new_brk == 0)
1386184SN/A        return p->brk_point;
1396184SN/A
1406184SN/A    if (new_brk > p->brk_point) {
1416184SN/A        // might need to allocate some new pages
1426184SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1436184SN/A                                VMPageSize); !gen.done(); gen.next()) {
1446184SN/A            if (!p->pTable->translate(gen.addr()))
1456184SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1466184SN/A                                    VMPageSize);
1476184SN/A        }
1486184SN/A    }
1496184SN/A
1506184SN/A    p->brk_point = new_brk;
1516184SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1526184SN/A    return p->brk_point;
1536184SN/A}
1546184SN/A
1556184SN/A
1566184SN/ASyscallReturn
1576184SN/AcloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1586184SN/A{
1596184SN/A    int target_fd = p->getSyscallArg(tc, 0);
1606184SN/A    int status = close(p->sim_fd(target_fd));
1616184SN/A    if (status >= 0)
1626184SN/A        p->free_fd(target_fd);
1636184SN/A    return status;
1646184SN/A}
1656184SN/A
1666184SN/A
1676184SN/ASyscallReturn
1686184SN/AreadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1696184SN/A{
1706184SN/A    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1716184SN/A    int nbytes = p->getSyscallArg(tc, 2);
1726184SN/A    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
1736184SN/A
1746184SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
1756184SN/A
1766184SN/A    if (bytes_read != -1)
1776184SN/A        bufArg.copyOut(tc->getMemPort());
1786184SN/A
1796184SN/A    return bytes_read;
1806184SN/A}
1816184SN/A
1826184SN/ASyscallReturn
1836184SN/AwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1846184SN/A{
1856184SN/A    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1866184SN/A    int nbytes = p->getSyscallArg(tc, 2);
1876184SN/A    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
1886184SN/A
1896184SN/A    bufArg.copyIn(tc->getMemPort());
1906184SN/A
1916184SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
1926184SN/A
1936184SN/A    fsync(fd);
1946184SN/A
1956184SN/A    return bytes_written;
1966184SN/A}
1976184SN/A
1986184SN/A
1996184SN/ASyscallReturn
2006184SN/AlseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2016184SN/A{
2026184SN/A    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
2036184SN/A    uint64_t offs = p->getSyscallArg(tc, 1);
2046184SN/A    int whence = p->getSyscallArg(tc, 2);
2056184SN/A
2066184SN/A    off_t result = lseek(fd, offs, whence);
2076184SN/A
2086184SN/A    return (result == (off_t)-1) ? -errno : result;
2096184SN/A}
2106184SN/A
2116184SN/A
2126184SN/ASyscallReturn
2136184SN/A_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2146184SN/A{
2156184SN/A    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
2166184SN/A    uint64_t offset_high = p->getSyscallArg(tc, 1);
2176184SN/A    uint32_t offset_low = p->getSyscallArg(tc, 2);
2186184SN/A    Addr result_ptr = p->getSyscallArg(tc, 3);
2196184SN/A    int whence = p->getSyscallArg(tc, 4);
2206184SN/A
2216184SN/A    uint64_t offset = (offset_high << 32) | offset_low;
2226184SN/A
2236184SN/A    uint64_t result = lseek(fd, offset, whence);
2246184SN/A    result = TheISA::htog(result);
2256184SN/A
2266184SN/A    if (result == (off_t)-1) {
2276184SN/A        //The seek failed.
2286184SN/A        return -errno;
2296184SN/A    } else {
2306184SN/A        // The seek succeeded.
2316184SN/A        // Copy "result" to "result_ptr"
2326184SN/A        // XXX We'll assume that the size of loff_t is 64 bits on the
2336184SN/A        // target platform
2346184SN/A        BufferArg result_buf(result_ptr, sizeof(result));
2356184SN/A        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2366184SN/A        result_buf.copyOut(tc->getMemPort());
2376184SN/A        return 0;
2386184SN/A    }
2396184SN/A
2406184SN/A
2416184SN/A    return (result == (off_t)-1) ? -errno : result;
2426184SN/A}
2436184SN/A
2446184SN/A
2456184SN/ASyscallReturn
2466184SN/AmunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2476184SN/A{
2486184SN/A    // given that we don't really implement mmap, munmap is really easy
2496184SN/A    return 0;
2506184SN/A}
2516184SN/A
2526184SN/A
2536184SN/Aconst char *hostname = "m5.eecs.umich.edu";
2546184SN/A
2556184SN/ASyscallReturn
2566184SN/AgethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2576184SN/A{
2586184SN/A    int name_len = p->getSyscallArg(tc, 1);
2596184SN/A    BufferArg name(p->getSyscallArg(tc, 0), name_len);
2606184SN/A
2616184SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
2626184SN/A
2636184SN/A    name.copyOut(tc->getMemPort());
2646184SN/A
2656184SN/A    return 0;
2666184SN/A}
2676184SN/A
2686184SN/ASyscallReturn
2696184SN/AgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2706184SN/A{
2716184SN/A    int result = 0;
2726184SN/A    unsigned long size = p->getSyscallArg(tc, 1);
2736184SN/A    BufferArg buf(p->getSyscallArg(tc, 0), size);
2746184SN/A
2756184SN/A    // Is current working directory defined?
2766184SN/A    string cwd = p->getcwd();
2776184SN/A    if (!cwd.empty()) {
2786184SN/A        if (cwd.length() >= size) {
2796184SN/A            // Buffer too small
2806184SN/A            return -ERANGE;
2816184SN/A        }
2826184SN/A        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
2836184SN/A        result = cwd.length();
2846184SN/A    }
2856184SN/A    else {
2866184SN/A        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
2876184SN/A            result = strlen((char *)buf.bufferPtr());
2886184SN/A        }
2896184SN/A        else {
2906184SN/A            result = -1;
2916184SN/A        }
2926184SN/A    }
2936184SN/A
2946184SN/A    buf.copyOut(tc->getMemPort());
2956184SN/A
2966184SN/A    return (result == -1) ? -errno : result;
2976184SN/A}
298
299
300SyscallReturn
301readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
302{
303    string path;
304
305    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
306        return (TheISA::IntReg)-EFAULT;
307
308    // Adjust path for current working directory
309    path = p->fullPath(path);
310
311    size_t bufsiz = p->getSyscallArg(tc, 2);
312    BufferArg buf(p->getSyscallArg(tc, 1), bufsiz);
313
314    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
315
316    buf.copyOut(tc->getMemPort());
317
318    return (result == -1) ? -errno : result;
319}
320
321SyscallReturn
322unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
323{
324    string path;
325
326    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
327        return (TheISA::IntReg)-EFAULT;
328
329    // Adjust path for current working directory
330    path = p->fullPath(path);
331
332    int result = unlink(path.c_str());
333    return (result == -1) ? -errno : result;
334}
335
336
337SyscallReturn
338mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
339{
340    string path;
341
342    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
343        return (TheISA::IntReg)-EFAULT;
344
345    // Adjust path for current working directory
346    path = p->fullPath(path);
347
348    mode_t mode = p->getSyscallArg(tc, 1);
349
350    int result = mkdir(path.c_str(), mode);
351    return (result == -1) ? -errno : result;
352}
353
354SyscallReturn
355renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
356{
357    string old_name;
358
359    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0)))
360        return -EFAULT;
361
362    string new_name;
363
364    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1)))
365        return -EFAULT;
366
367    // Adjust path for current working directory
368    old_name = p->fullPath(old_name);
369    new_name = p->fullPath(new_name);
370
371    int64_t result = rename(old_name.c_str(), new_name.c_str());
372    return (result == -1) ? -errno : result;
373}
374
375SyscallReturn
376truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
377{
378    string path;
379
380    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
381        return -EFAULT;
382
383    off_t length = p->getSyscallArg(tc, 1);
384
385    // Adjust path for current working directory
386    path = p->fullPath(path);
387
388    int result = truncate(path.c_str(), length);
389    return (result == -1) ? -errno : result;
390}
391
392SyscallReturn
393ftruncateFunc(SyscallDesc *desc, int num,
394              LiveProcess *process, ThreadContext *tc)
395{
396    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
397
398    if (fd < 0)
399        return -EBADF;
400
401    off_t length = process->getSyscallArg(tc, 1);
402
403    int result = ftruncate(fd, length);
404    return (result == -1) ? -errno : result;
405}
406
407SyscallReturn
408umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
409{
410    // Letting the simulated program change the simulator's umask seems like
411    // a bad idea.  Compromise by just returning the current umask but not
412    // changing anything.
413    mode_t oldMask = umask(0);
414    umask(oldMask);
415    return (int)oldMask;
416}
417
418SyscallReturn
419chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
420{
421    string path;
422
423    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
424        return -EFAULT;
425
426    /* XXX endianess */
427    uint32_t owner = p->getSyscallArg(tc, 1);
428    uid_t hostOwner = owner;
429    uint32_t group = p->getSyscallArg(tc, 2);
430    gid_t hostGroup = group;
431
432    // Adjust path for current working directory
433    path = p->fullPath(path);
434
435    int result = chown(path.c_str(), hostOwner, hostGroup);
436    return (result == -1) ? -errno : result;
437}
438
439SyscallReturn
440fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
441{
442    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
443
444    if (fd < 0)
445        return -EBADF;
446
447    /* XXX endianess */
448    uint32_t owner = process->getSyscallArg(tc, 1);
449    uid_t hostOwner = owner;
450    uint32_t group = process->getSyscallArg(tc, 2);
451    gid_t hostGroup = group;
452
453    int result = fchown(fd, hostOwner, hostGroup);
454    return (result == -1) ? -errno : result;
455}
456
457
458SyscallReturn
459dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
460{
461    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
462    if (fd < 0)
463        return -EBADF;
464
465    Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0));
466
467    int result = dup(fd);
468    return (result == -1) ? -errno :
469        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
470}
471
472
473SyscallReturn
474fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
475          ThreadContext *tc)
476{
477    int fd = process->getSyscallArg(tc, 0);
478
479    if (fd < 0 || process->sim_fd(fd) < 0)
480        return -EBADF;
481
482    int cmd = process->getSyscallArg(tc, 1);
483    switch (cmd) {
484      case 0: // F_DUPFD
485        // if we really wanted to support this, we'd need to do it
486        // in the target fd space.
487        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
488        return -EMFILE;
489
490      case 1: // F_GETFD (get close-on-exec flag)
491      case 2: // F_SETFD (set close-on-exec flag)
492        return 0;
493
494      case 3: // F_GETFL (get file flags)
495      case 4: // F_SETFL (set file flags)
496        // not sure if this is totally valid, but we'll pass it through
497        // to the underlying OS
498        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
499        return fcntl(process->sim_fd(fd), cmd);
500        // return 0;
501
502      case 7: // F_GETLK  (get lock)
503      case 8: // F_SETLK  (set lock)
504      case 9: // F_SETLKW (set lock and wait)
505        // don't mess with file locking... just act like it's OK
506        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
507        return 0;
508
509      default:
510        warn("Unknown fcntl command %d\n", cmd);
511        return 0;
512    }
513}
514
515SyscallReturn
516fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
517            ThreadContext *tc)
518{
519    int fd = process->getSyscallArg(tc, 0);
520
521    if (fd < 0 || process->sim_fd(fd) < 0)
522        return -EBADF;
523
524    int cmd = process->getSyscallArg(tc, 1);
525    switch (cmd) {
526      case 33: //F_GETLK64
527        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
528        return -EMFILE;
529
530      case 34: // F_SETLK64
531      case 35: // F_SETLKW64
532        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
533        return -EMFILE;
534
535      default:
536        // not sure if this is totally valid, but we'll pass it through
537        // to the underlying OS
538        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
539        return fcntl(process->sim_fd(fd), cmd);
540        // return 0;
541    }
542}
543
544SyscallReturn
545pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
546         ThreadContext *tc)
547{
548    int fds[2], sim_fds[2];
549    int pipe_retval = pipe(fds);
550
551    if (pipe_retval < 0) {
552        // error
553        return pipe_retval;
554    }
555
556    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
557    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
558
559    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
560    // Alpha Linux convention for pipe() is that fd[0] is returned as
561    // the return value of the function, and fd[1] is returned in r20.
562    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
563    return sim_fds[0];
564}
565
566
567SyscallReturn
568getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
569           ThreadContext *tc)
570{
571    // Make up a PID.  There's no interprocess communication in
572    // fake_syscall mode, so there's no way for a process to know it's
573    // not getting a unique value.
574
575    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
576    return process->pid();
577}
578
579
580SyscallReturn
581getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
582           ThreadContext *tc)
583{
584    // Make up a UID and EUID... it shouldn't matter, and we want the
585    // simulation to be deterministic.
586
587    // EUID goes in r20.
588    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
589    return process->uid();              // UID
590}
591
592
593SyscallReturn
594getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
595           ThreadContext *tc)
596{
597    // Get current group ID.  EGID goes in r20.
598    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
599    return process->gid();
600}
601
602
603SyscallReturn
604setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
605           ThreadContext *tc)
606{
607    // can't fathom why a benchmark would call this.
608    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0));
609    return 0;
610}
611
612SyscallReturn
613getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
614           ThreadContext *tc)
615{
616    // Make up a PID.  There's no interprocess communication in
617    // fake_syscall mode, so there's no way for a process to know it's
618    // not getting a unique value.
619
620    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
621    return process->pid();
622}
623
624SyscallReturn
625getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
626           ThreadContext *tc)
627{
628    return process->ppid();
629}
630
631SyscallReturn
632getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
633           ThreadContext *tc)
634{
635    return process->uid();              // UID
636}
637
638SyscallReturn
639geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
640           ThreadContext *tc)
641{
642    return process->euid();             // UID
643}
644
645SyscallReturn
646getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
647           ThreadContext *tc)
648{
649    return process->gid();
650}
651
652SyscallReturn
653getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
654           ThreadContext *tc)
655{
656    return process->egid();
657}
658
659
660SyscallReturn
661cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
662           ThreadContext *tc)
663{
664    DPRINTF(SyscallVerbose, "In sys_clone:\n");
665    DPRINTF(SyscallVerbose, " Flags=%llx\n", process->getSyscallArg(tc, 0));
666    DPRINTF(SyscallVerbose, " Child stack=%llx\n",
667            process->getSyscallArg(tc, 1));
668
669
670    if (process->getSyscallArg(tc, 0) != 0x10f00) {
671        warn("This sys_clone implementation assumes flags "
672             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
673             "(0x10f00), and may not work correctly with given flags "
674             "0x%llx\n", process->getSyscallArg(tc, 0));
675    }
676
677    ThreadContext* ctc; // child thread context
678    if ( ( ctc = process->findFreeContext() ) != NULL ) {
679        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
680
681        ctc->clearArchRegs();
682
683        // Arch-specific cloning code
684        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
685            // Cloning the misc. regs for these archs is enough
686            TheISA::copyMiscRegs(tc, ctc);
687        #elif THE_ISA == SPARC_ISA
688            TheISA::copyRegs(tc, ctc);
689
690            // TODO: Explain what this code actually does :-)
691            ctc->setIntReg(NumIntArchRegs + 6, 0);
692            ctc->setIntReg(NumIntArchRegs + 4, 0);
693            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
694            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
695            ctc->setMiscRegNoEffect(MISCREG_CWP, 0);
696            ctc->setIntReg(NumIntArchRegs + 7, 0);
697            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
698            ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
699
700            for (int y = 8; y < 32; y++)
701                ctc->setIntReg(y, tc->readIntReg(y));
702        #else
703            fatal("sys_clone is not implemented for this ISA\n");
704        #endif
705
706        // Set up stack register
707        ctc->setIntReg(TheISA::StackPointerReg, process->getSyscallArg(tc, 1));
708
709        // Set up syscall return values in parent and child
710        ctc->setIntReg(ReturnValueReg, 0); // return value, child
711
712        // Alpha needs SyscallSuccessReg=0 in child
713        #if THE_ISA == ALPHA_ISA
714            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
715        #endif
716
717        // In SPARC/Linux, clone returns 0 on pseudo-return register if
718        // parent, non-zero if child
719        #if THE_ISA == SPARC_ISA
720            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
721            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
722        #endif
723
724        ctc->setPC(tc->readNextPC());
725        ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
726        ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst));
727
728        ctc->activate();
729
730        // Should return nonzero child TID in parent's syscall return register,
731        // but for our pthread library any non-zero value will work
732        return 1;
733    } else {
734        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
735        return 0;
736    }
737}
738
739