syscall_emul.cc revision 11851
1955SN/A/*
2955SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
31762SN/A * All rights reserved.
4955SN/A *
5955SN/A * Redistribution and use in source and binary forms, with or without
6955SN/A * modification, are permitted provided that the following conditions are
7955SN/A * met: redistributions of source code must retain the above copyright
8955SN/A * notice, this list of conditions and the following disclaimer;
9955SN/A * redistributions in binary form must reproduce the above copyright
10955SN/A * notice, this list of conditions and the following disclaimer in the
11955SN/A * documentation and/or other materials provided with the distribution;
12955SN/A * neither the name of the copyright holders nor the names of its
13955SN/A * contributors may be used to endorse or promote products derived from
14955SN/A * this software without specific prior written permission.
15955SN/A *
16955SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17955SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18955SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19955SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20955SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21955SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22955SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23955SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24955SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25955SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26955SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27955SN/A *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
30955SN/A */
31955SN/A
32955SN/A#include "sim/syscall_emul.hh"
331608SN/A
34955SN/A#include <fcntl.h>
35955SN/A#include <unistd.h>
36955SN/A
37955SN/A#include <iostream>
38955SN/A#include <string>
39955SN/A
40955SN/A#include "arch/utility.hh"
41955SN/A#include "base/chunk_generator.hh"
42955SN/A#include "base/trace.hh"
43955SN/A#include "config/the_isa.hh"
44955SN/A#include "cpu/thread_context.hh"
45955SN/A#include "mem/page_table.hh"
46955SN/A#include "sim/process.hh"
47955SN/A#include "sim/sim_exit.hh"
482023SN/A#include "sim/syscall_debug_macros.hh"
49955SN/A#include "sim/syscall_desc.hh"
50955SN/A#include "sim/system.hh"
51955SN/A
52955SN/Ausing namespace std;
53955SN/Ausing namespace TheISA;
54955SN/A
55955SN/ASyscallReturn
56955SN/AunimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
57955SN/A                  ThreadContext *tc)
581031SN/A{
59955SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
601388SN/A
61955SN/A    return 1;
62955SN/A}
631296SN/A
64955SN/A
65955SN/ASyscallReturn
66955SN/AignoreFunc(SyscallDesc *desc, int callnum, Process *process,
67955SN/A           ThreadContext *tc)
68955SN/A{
69955SN/A    if (desc->needWarning()) {
70955SN/A        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
71955SN/A             "\n      (further warnings will be suppressed)" : "");
72955SN/A    }
73955SN/A
74955SN/A    return 0;
75955SN/A}
76955SN/A
77955SN/A
78955SN/ASyscallReturn
79955SN/AexitFunc(SyscallDesc *desc, int callnum, Process *process,
80955SN/A         ThreadContext *tc)
81955SN/A{
82955SN/A    if (process->system->numRunningContexts() == 1) {
832325SN/A        // Last running context... exit simulator
841717SN/A        int index = 0;
852652Ssaidi@eecs.umich.edu        exitSimLoop("target called exit()",
86955SN/A                    process->getSyscallArg(tc, index) & 0xff);
872736Sktlim@umich.edu    } else {
882410SN/A        // other running threads... just halt this one
89955SN/A        tc->halt();
902290SN/A    }
91955SN/A
922683Sktlim@umich.edu    return 1;
932683Sktlim@umich.edu}
942669Sktlim@umich.edu
952568SN/A
962568SN/ASyscallReturn
972462SN/AexitGroupFunc(SyscallDesc *desc, int callnum, Process *process,
982568SN/A              ThreadContext *tc)
992395SN/A{
1002405SN/A    // halt all threads belonging to this process
101955SN/A    for (auto i: process->contextIds) {
1022811Srdreslin@umich.edu        process->system->getThreadContext(i)->halt();
1032811Srdreslin@umich.edu    }
1042811Srdreslin@umich.edu
1052811Srdreslin@umich.edu    if (!process->system->numRunningContexts()) {
1062811Srdreslin@umich.edu        // all threads belonged to this process... exit simulator
1072811Srdreslin@umich.edu        int index = 0;
1082811Srdreslin@umich.edu        exitSimLoop("target called exit()",
1092811Srdreslin@umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1102811Srdreslin@umich.edu    }
1112811Srdreslin@umich.edu
1122811Srdreslin@umich.edu    return 1;
1132811Srdreslin@umich.edu}
1142811Srdreslin@umich.edu
1152811Srdreslin@umich.edu
1162811Srdreslin@umich.eduSyscallReturn
1172811Srdreslin@umich.edugetpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1182814Srdreslin@umich.edu{
1192811Srdreslin@umich.edu    return (int)PageBytes;
1202811Srdreslin@umich.edu}
1212811Srdreslin@umich.edu
1222811Srdreslin@umich.edu
1232811Srdreslin@umich.eduSyscallReturn
1242811Srdreslin@umich.edubrkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1252811Srdreslin@umich.edu{
1262813Srdreslin@umich.edu    // change brk addr to first arg
1272813Srdreslin@umich.edu    int index = 0;
128955SN/A    Addr new_brk = p->getSyscallArg(tc, index);
129955SN/A
130955SN/A    // in Linux at least, brk(0) returns the current break value
1312090SN/A    // (note that the syscall and the glibc function have different behavior)
132955SN/A    if (new_brk == 0)
1332763Sstever@eecs.umich.edu        return p->brk_point;
134955SN/A
1351696SN/A    if (new_brk > p->brk_point) {
136955SN/A        // might need to allocate some new pages
137955SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
138955SN/A                                PageBytes); !gen.done(); gen.next()) {
1391127SN/A            if (!p->pTable->translate(gen.addr()))
140955SN/A                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
141955SN/A
1422379SN/A            // if the address is already there, zero it out
143955SN/A            else {
144955SN/A                uint8_t zero  = 0;
145955SN/A                SETranslatingPortProxy &tp = tc->getMemProxy();
1462155SN/A
1472155SN/A                // split non-page aligned accesses
1482155SN/A                Addr next_page = roundUp(gen.addr(), PageBytes);
1492155SN/A                uint32_t size_needed = next_page - gen.addr();
1502155SN/A                tp.memsetBlob(gen.addr(), zero, size_needed);
1512155SN/A                if (gen.addr() + PageBytes > next_page &&
1522155SN/A                    next_page < new_brk &&
1532155SN/A                    p->pTable->translate(next_page))
1542155SN/A                {
1552155SN/A                    size_needed = PageBytes - size_needed;
1562155SN/A                    tp.memsetBlob(next_page, zero, size_needed);
1572155SN/A                }
1582155SN/A            }
1592155SN/A        }
1602155SN/A    }
1612155SN/A
1622155SN/A    p->brk_point = new_brk;
1632155SN/A    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
1642155SN/A                    p->brk_point);
1652155SN/A    return p->brk_point;
1662155SN/A}
1672155SN/A
1682155SN/A
1692155SN/ASyscallReturn
1702155SN/AcloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1712155SN/A{
1722155SN/A    int index = 0;
1732155SN/A    int tgt_fd = p->getSyscallArg(tc, index);
1742155SN/A
1752155SN/A    int sim_fd = p->getSimFD(tgt_fd);
1762155SN/A    if (sim_fd < 0)
1772155SN/A        return -EBADF;
1782155SN/A
1792155SN/A    int status = 0;
1802155SN/A    if (sim_fd > 2)
1812155SN/A        status = close(sim_fd);
1822155SN/A    if (status >= 0)
1832155SN/A        p->resetFDEntry(tgt_fd);
1842155SN/A    return status;
1852422SN/A}
1862422SN/A
1872422SN/A
1882422SN/ASyscallReturn
1892422SN/AreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1902422SN/A{
1912422SN/A    int index = 0;
1922397SN/A    int tgt_fd = p->getSyscallArg(tc, index);
1932397SN/A    Addr bufPtr = p->getSyscallArg(tc, index);
1942422SN/A    int nbytes = p->getSyscallArg(tc, index);
1952422SN/A    BufferArg bufArg(bufPtr, nbytes);
196955SN/A
197955SN/A    int sim_fd = p->getSimFD(tgt_fd);
198955SN/A    if (sim_fd < 0)
199955SN/A        return -EBADF;
200955SN/A
201955SN/A    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
202955SN/A
203955SN/A    if (bytes_read > 0)
2041078SN/A        bufArg.copyOut(tc->getMemProxy());
205955SN/A
206955SN/A    return bytes_read;
207955SN/A}
208955SN/A
2091917SN/ASyscallReturn
210955SN/AwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
211955SN/A{
212955SN/A    int index = 0;
213955SN/A    int tgt_fd = p->getSyscallArg(tc, index);
214974SN/A    Addr bufPtr = p->getSyscallArg(tc, index);
215955SN/A    int nbytes = p->getSyscallArg(tc, index);
216955SN/A    BufferArg bufArg(bufPtr, nbytes);
217955SN/A
218955SN/A    int sim_fd = p->getSimFD(tgt_fd);
2192566SN/A    if (sim_fd < 0)
2202566SN/A        return -EBADF;
221955SN/A
222955SN/A    bufArg.copyIn(tc->getMemProxy());
2232539SN/A
224955SN/A    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
225955SN/A
226955SN/A    fsync(sim_fd);
2271817SN/A
2281154SN/A    return bytes_written;
2291840SN/A}
2302522SN/A
2312522SN/A
2322629SN/ASyscallReturn
233955SN/AlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
234955SN/A{
235955SN/A    int index = 0;
2362539SN/A    int tgt_fd = p->getSyscallArg(tc, index);
237955SN/A    uint64_t offs = p->getSyscallArg(tc, index);
2382539SN/A    int whence = p->getSyscallArg(tc, index);
239955SN/A
2401730SN/A    int sim_fd = p->getSimFD(tgt_fd);
241955SN/A    if (sim_fd < 0)
242955SN/A        return -EBADF;
243955SN/A
2442212SN/A    off_t result = lseek(sim_fd, offs, whence);
245955SN/A
2461040SN/A    return (result == (off_t)-1) ? -errno : result;
2472507SN/A}
2482521SN/A
2492521SN/A
2502507SN/ASyscallReturn
2512507SN/A_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2522507SN/A{
2532521SN/A    int index = 0;
2542507SN/A    int tgt_fd = p->getSyscallArg(tc, index);
2552507SN/A    uint64_t offset_high = p->getSyscallArg(tc, index);
256955SN/A    uint32_t offset_low = p->getSyscallArg(tc, index);
257955SN/A    Addr result_ptr = p->getSyscallArg(tc, index);
258955SN/A    int whence = p->getSyscallArg(tc, index);
259955SN/A
260955SN/A    int sim_fd = p->getSimFD(tgt_fd);
261955SN/A    if (sim_fd < 0)
2621742SN/A        return -EBADF;
2631742SN/A
2641742SN/A    uint64_t offset = (offset_high << 32) | offset_low;
2651742SN/A
2661742SN/A    uint64_t result = lseek(sim_fd, offset, whence);
2671742SN/A    result = TheISA::htog(result);
2681742SN/A
2691742SN/A    if (result == (off_t)-1)
2701742SN/A        return -errno;
2711742SN/A    // Assuming that the size of loff_t is 64 bits on the target platform
2721742SN/A    BufferArg result_buf(result_ptr, sizeof(result));
2731742SN/A    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2741742SN/A    result_buf.copyOut(tc->getMemProxy());
2751742SN/A    return 0;
2761742SN/A}
2771742SN/A
2781742SN/A
2791742SN/ASyscallReturn
2801742SN/AmunmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2811742SN/A{
282955SN/A    // With mmap more fully implemented, it might be worthwhile to bite
283955SN/A    // the bullet and implement munmap. Should allow us to reuse simulated
2842520SN/A    // memory.
2852517SN/A    return 0;
2862253SN/A}
2872253SN/A
2882253SN/A
2892253SN/Aconst char *hostname = "m5.eecs.umich.edu";
2902553SN/A
2912553SN/ASyscallReturn
2922553SN/AgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2932553SN/A{
2942507SN/A    int index = 0;
2952470SN/A    Addr bufPtr = p->getSyscallArg(tc, index);
2961744SN/A    int name_len = p->getSyscallArg(tc, index);
2971744SN/A    BufferArg name(bufPtr, name_len);
2982470SN/A
2992470SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
3002470SN/A
3012919Sktlim@umich.edu    name.copyOut(tc->getMemProxy());
3022470SN/A
3032470SN/A    return 0;
3042400SN/A}
3052400SN/A
306955SN/ASyscallReturn
307955SN/AgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3082667Sstever@eecs.umich.edu{
3092667Sstever@eecs.umich.edu    int result = 0;
3102667Sstever@eecs.umich.edu    int index = 0;
3112667Sstever@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3122667Sstever@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3132667Sstever@eecs.umich.edu    BufferArg buf(bufPtr, size);
3142037SN/A
3152037SN/A    // Is current working directory defined?
3162037SN/A    string cwd = p->getcwd();
3172667Sstever@eecs.umich.edu    if (!cwd.empty()) {
3182139SN/A        if (cwd.length() >= size) {
3192667Sstever@eecs.umich.edu            // Buffer too small
3202155SN/A            return -ERANGE;
3212155SN/A        }
3222155SN/A        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3232155SN/A        result = cwd.length();
3242155SN/A    } else {
3252155SN/A        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
326955SN/A            result = strlen((char *)buf.bufferPtr());
3272155SN/A        } else {
328955SN/A            result = -1;
329955SN/A        }
330955SN/A    }
3311742SN/A
3321742SN/A    buf.copyOut(tc->getMemProxy());
333955SN/A
334955SN/A    return (result == -1) ? -errno : result;
335955SN/A}
3361858SN/A
337955SN/A/// Target open() handler.
3381858SN/ASyscallReturn
3391858SN/AreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
3401858SN/A             ThreadContext *tc)
3411085SN/A{
342955SN/A    return readlinkFunc(desc, callnum, process, tc, 0);
343955SN/A}
344955SN/A
345955SN/ASyscallReturn
346955SN/AreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
347955SN/A             int index)
348955SN/A{
349955SN/A    string path;
350955SN/A
351955SN/A    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
352955SN/A        return -EFAULT;
353955SN/A
3542667Sstever@eecs.umich.edu    // Adjust path for current working directory
3551045SN/A    path = p->fullPath(path);
356955SN/A
357955SN/A    Addr bufPtr = p->getSyscallArg(tc, index);
358955SN/A    size_t bufsiz = p->getSyscallArg(tc, index);
359955SN/A
3601108SN/A    BufferArg buf(bufPtr, bufsiz);
361955SN/A
362955SN/A    int result = -1;
363955SN/A    if (path != "/proc/self/exe") {
364955SN/A        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
365955SN/A    } else {
366955SN/A        // Emulate readlink() called on '/proc/self/exe' should return the
367955SN/A        // absolute path of the binary running in the simulated system (the
368955SN/A        // Process' executable). It is possible that using this path in
369955SN/A        // the simulated system will result in unexpected behavior if:
370955SN/A        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
371955SN/A        //     called binary calls readlink().
372955SN/A        //  2) The host's full path to the running benchmark changes from one
373955SN/A        //     simulation to another. This can result in different simulated
374955SN/A        //     performance since the simulated system will process the binary
375955SN/A        //     path differently, even if the binary itself does not change.
376955SN/A
3772655Sstever@eecs.umich.edu        // Get the absolute canonical path to the running application
3782655Sstever@eecs.umich.edu        char real_path[PATH_MAX];
3792655Sstever@eecs.umich.edu        char *check_real_path = realpath(p->progName(), real_path);
3802655Sstever@eecs.umich.edu        if (!check_real_path) {
3812655Sstever@eecs.umich.edu            fatal("readlink('/proc/self/exe') unable to resolve path to "
3822655Sstever@eecs.umich.edu                  "executable: %s", p->progName());
3832655Sstever@eecs.umich.edu        }
3842655Sstever@eecs.umich.edu        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
3852655Sstever@eecs.umich.edu        size_t real_path_len = strlen(real_path);
3862655Sstever@eecs.umich.edu        if (real_path_len > bufsiz) {
3872655Sstever@eecs.umich.edu            // readlink will truncate the contents of the
3882655Sstever@eecs.umich.edu            // path to ensure it is no more than bufsiz
3892655Sstever@eecs.umich.edu            result = bufsiz;
3902655Sstever@eecs.umich.edu        } else {
3912655Sstever@eecs.umich.edu            result = real_path_len;
3922655Sstever@eecs.umich.edu        }
3932655Sstever@eecs.umich.edu
3942655Sstever@eecs.umich.edu        // Issue a warning about potential unexpected results
3952655Sstever@eecs.umich.edu        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
3962655Sstever@eecs.umich.edu                  "results in various settings.\n      Returning '%s'\n",
3972655Sstever@eecs.umich.edu                  (char*)buf.bufferPtr());
3982655Sstever@eecs.umich.edu    }
399955SN/A
4002655Sstever@eecs.umich.edu    buf.copyOut(tc->getMemProxy());
4012655Sstever@eecs.umich.edu
4022655Sstever@eecs.umich.edu    return (result == -1) ? -errno : result;
403955SN/A}
404955SN/A
4052655Sstever@eecs.umich.eduSyscallReturn
4062655Sstever@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
407955SN/A{
408955SN/A    return unlinkHelper(desc, num, p, tc, 0);
4092655Sstever@eecs.umich.edu}
4102655Sstever@eecs.umich.edu
4112655Sstever@eecs.umich.eduSyscallReturn
412955SN/AunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
413955SN/A             int index)
4142655Sstever@eecs.umich.edu{
4152655Sstever@eecs.umich.edu    string path;
4162655Sstever@eecs.umich.edu
4171869SN/A    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4181869SN/A        return -EFAULT;
419
420    // Adjust path for current working directory
421    path = p->fullPath(path);
422
423    int result = unlink(path.c_str());
424    return (result == -1) ? -errno : result;
425}
426
427
428SyscallReturn
429mkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
430{
431    string path;
432
433    int index = 0;
434    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
435        return -EFAULT;
436
437    // Adjust path for current working directory
438    path = p->fullPath(path);
439
440    mode_t mode = p->getSyscallArg(tc, index);
441
442    int result = mkdir(path.c_str(), mode);
443    return (result == -1) ? -errno : result;
444}
445
446SyscallReturn
447renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
448{
449    string old_name;
450
451    int index = 0;
452    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
453        return -EFAULT;
454
455    string new_name;
456
457    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
458        return -EFAULT;
459
460    // Adjust path for current working directory
461    old_name = p->fullPath(old_name);
462    new_name = p->fullPath(new_name);
463
464    int64_t result = rename(old_name.c_str(), new_name.c_str());
465    return (result == -1) ? -errno : result;
466}
467
468SyscallReturn
469truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
470{
471    string path;
472
473    int index = 0;
474    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
475        return -EFAULT;
476
477    off_t length = p->getSyscallArg(tc, index);
478
479    // Adjust path for current working directory
480    path = p->fullPath(path);
481
482    int result = truncate(path.c_str(), length);
483    return (result == -1) ? -errno : result;
484}
485
486SyscallReturn
487ftruncateFunc(SyscallDesc *desc, int num,
488              Process *process, ThreadContext *tc)
489{
490    int index = 0;
491    int tgt_fd = process->getSyscallArg(tc, index);
492    off_t length = process->getSyscallArg(tc, index);
493
494    int sim_fd = process->getSimFD(tgt_fd);
495    if (sim_fd < 0)
496        return -EBADF;
497
498    int result = ftruncate(sim_fd, length);
499    return (result == -1) ? -errno : result;
500}
501
502SyscallReturn
503truncate64Func(SyscallDesc *desc, int num,
504               Process *process, ThreadContext *tc)
505{
506    int index = 0;
507    string path;
508
509    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
510       return -EFAULT;
511
512    int64_t length = process->getSyscallArg(tc, index, 64);
513
514    // Adjust path for current working directory
515    path = process->fullPath(path);
516
517#if NO_STAT64
518    int result = truncate(path.c_str(), length);
519#else
520    int result = truncate64(path.c_str(), length);
521#endif
522    return (result == -1) ? -errno : result;
523}
524
525SyscallReturn
526ftruncate64Func(SyscallDesc *desc, int num,
527                Process *process, ThreadContext *tc)
528{
529    int index = 0;
530    int tgt_fd = process->getSyscallArg(tc, index);
531    int64_t length = process->getSyscallArg(tc, index, 64);
532
533    int sim_fd = process->getSimFD(tgt_fd);
534    if (sim_fd < 0)
535        return -EBADF;
536
537#if NO_STAT64
538    int result = ftruncate(sim_fd, length);
539#else
540    int result = ftruncate64(sim_fd, length);
541#endif
542    return (result == -1) ? -errno : result;
543}
544
545SyscallReturn
546umaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
547{
548    // Letting the simulated program change the simulator's umask seems like
549    // a bad idea.  Compromise by just returning the current umask but not
550    // changing anything.
551    mode_t oldMask = umask(0);
552    umask(oldMask);
553    return (int)oldMask;
554}
555
556SyscallReturn
557chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
558{
559    string path;
560
561    int index = 0;
562    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
563        return -EFAULT;
564
565    /* XXX endianess */
566    uint32_t owner = p->getSyscallArg(tc, index);
567    uid_t hostOwner = owner;
568    uint32_t group = p->getSyscallArg(tc, index);
569    gid_t hostGroup = group;
570
571    // Adjust path for current working directory
572    path = p->fullPath(path);
573
574    int result = chown(path.c_str(), hostOwner, hostGroup);
575    return (result == -1) ? -errno : result;
576}
577
578SyscallReturn
579fchownFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
580{
581    int index = 0;
582    int tgt_fd = process->getSyscallArg(tc, index);
583
584    int sim_fd = process->getSimFD(tgt_fd);
585    if (sim_fd < 0)
586        return -EBADF;
587
588    /* XXX endianess */
589    uint32_t owner = process->getSyscallArg(tc, index);
590    uid_t hostOwner = owner;
591    uint32_t group = process->getSyscallArg(tc, index);
592    gid_t hostGroup = group;
593
594    int result = fchown(sim_fd, hostOwner, hostGroup);
595    return (result == -1) ? -errno : result;
596}
597
598
599SyscallReturn
600dupFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
601{
602    int index = 0;
603    int tgt_fd = process->getSyscallArg(tc, index);
604
605    int sim_fd = process->getSimFD(tgt_fd);
606    if (sim_fd < 0)
607        return -EBADF;
608
609    FDEntry *fde = process->getFDEntry(tgt_fd);
610
611    int result = dup(sim_fd);
612    return (result == -1) ? -errno :
613        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
614}
615
616
617SyscallReturn
618fcntlFunc(SyscallDesc *desc, int num, Process *process,
619          ThreadContext *tc)
620{
621    int index = 0;
622    int tgt_fd = process->getSyscallArg(tc, index);
623
624    int sim_fd = process->getSimFD(tgt_fd);
625    if (sim_fd < 0)
626        return -EBADF;
627
628    int cmd = process->getSyscallArg(tc, index);
629    switch (cmd) {
630      case 0: // F_DUPFD
631        // if we really wanted to support this, we'd need to do it
632        // in the target fd space.
633        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
634        return -EMFILE;
635
636      case 1: // F_GETFD (get close-on-exec flag)
637      case 2: // F_SETFD (set close-on-exec flag)
638        return 0;
639
640      case 3: // F_GETFL (get file flags)
641      case 4: // F_SETFL (set file flags)
642        // not sure if this is totally valid, but we'll pass it through
643        // to the underlying OS
644        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
645        return fcntl(sim_fd, cmd);
646        // return 0;
647
648      case 7: // F_GETLK  (get lock)
649      case 8: // F_SETLK  (set lock)
650      case 9: // F_SETLKW (set lock and wait)
651        // don't mess with file locking... just act like it's OK
652        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
653        return 0;
654
655      default:
656        warn("Unknown fcntl command %d\n", cmd);
657        return 0;
658    }
659}
660
661SyscallReturn
662fcntl64Func(SyscallDesc *desc, int num, Process *process,
663            ThreadContext *tc)
664{
665    int index = 0;
666    int tgt_fd = process->getSyscallArg(tc, index);
667
668    int sim_fd = process->getSimFD(tgt_fd);
669    if (sim_fd < 0)
670        return -EBADF;
671
672    int cmd = process->getSyscallArg(tc, index);
673    switch (cmd) {
674      case 33: //F_GETLK64
675        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
676        return -EMFILE;
677
678      case 34: // F_SETLK64
679      case 35: // F_SETLKW64
680        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
681             tgt_fd);
682        return -EMFILE;
683
684      default:
685        // not sure if this is totally valid, but we'll pass it through
686        // to the underlying OS
687        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
688        return fcntl(sim_fd, cmd);
689        // return 0;
690    }
691}
692
693SyscallReturn
694pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
695               ThreadContext *tc)
696{
697    int fds[2], sim_fds[2];
698    int pipe_retval = pipe(fds);
699
700    if (pipe_retval < 0) {
701        // error
702        return pipe_retval;
703    }
704
705    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
706    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
707
708    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
709    // Alpha Linux convention for pipe() is that fd[0] is returned as
710    // the return value of the function, and fd[1] is returned in r20.
711    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
712    return sim_fds[0];
713}
714
715
716SyscallReturn
717getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
718                 ThreadContext *tc)
719{
720    // Make up a PID.  There's no interprocess communication in
721    // fake_syscall mode, so there's no way for a process to know it's
722    // not getting a unique value.
723
724    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
725    return process->pid();
726}
727
728
729SyscallReturn
730getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
731                 ThreadContext *tc)
732{
733    // Make up a UID and EUID... it shouldn't matter, and we want the
734    // simulation to be deterministic.
735
736    // EUID goes in r20.
737    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
738    return process->uid();              // UID
739}
740
741
742SyscallReturn
743getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
744                 ThreadContext *tc)
745{
746    // Get current group ID.  EGID goes in r20.
747    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
748    return process->gid();
749}
750
751
752SyscallReturn
753setuidFunc(SyscallDesc *desc, int callnum, Process *process,
754           ThreadContext *tc)
755{
756    // can't fathom why a benchmark would call this.
757    int index = 0;
758    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
759    return 0;
760}
761
762SyscallReturn
763getpidFunc(SyscallDesc *desc, int callnum, Process *process,
764           ThreadContext *tc)
765{
766    // Make up a PID.  There's no interprocess communication in
767    // fake_syscall mode, so there's no way for a process to know it's
768    // not getting a unique value.
769
770    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
771    return process->pid();
772}
773
774SyscallReturn
775getppidFunc(SyscallDesc *desc, int callnum, Process *process,
776            ThreadContext *tc)
777{
778    return process->ppid();
779}
780
781SyscallReturn
782getuidFunc(SyscallDesc *desc, int callnum, Process *process,
783           ThreadContext *tc)
784{
785    return process->uid();              // UID
786}
787
788SyscallReturn
789geteuidFunc(SyscallDesc *desc, int callnum, Process *process,
790            ThreadContext *tc)
791{
792    return process->euid();             // UID
793}
794
795SyscallReturn
796getgidFunc(SyscallDesc *desc, int callnum, Process *process,
797           ThreadContext *tc)
798{
799    return process->gid();
800}
801
802SyscallReturn
803getegidFunc(SyscallDesc *desc, int callnum, Process *process,
804            ThreadContext *tc)
805{
806    return process->egid();
807}
808
809
810SyscallReturn
811cloneFunc(SyscallDesc *desc, int callnum, Process *process,
812          ThreadContext *tc)
813{
814    int index = 0;
815    IntReg flags = process->getSyscallArg(tc, index);
816    IntReg newStack = process->getSyscallArg(tc, index);
817
818    DPRINTF(SyscallVerbose, "In sys_clone:\n");
819    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
820    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
821
822
823    if (flags != 0x10f00) {
824        warn("This sys_clone implementation assumes flags "
825             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
826             "(0x10f00), and may not work correctly with given flags "
827             "0x%llx\n", flags);
828    }
829
830    ThreadContext* ctc; // child thread context
831    if ( ( ctc = process->findFreeContext() ) != NULL ) {
832        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
833
834        ctc->clearArchRegs();
835
836        // Arch-specific cloning code
837        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
838            // Cloning the misc. regs for these archs is enough
839            TheISA::copyMiscRegs(tc, ctc);
840        #elif THE_ISA == SPARC_ISA
841            TheISA::copyRegs(tc, ctc);
842
843            // TODO: Explain what this code actually does :-)
844            ctc->setIntReg(NumIntArchRegs + 6, 0);
845            ctc->setIntReg(NumIntArchRegs + 4, 0);
846            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
847            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
848            ctc->setMiscReg(MISCREG_CWP, 0);
849            ctc->setIntReg(NumIntArchRegs + 7, 0);
850            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
851            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
852
853            for (int y = 8; y < 32; y++)
854                ctc->setIntReg(y, tc->readIntReg(y));
855        #elif THE_ISA == ARM_ISA
856            TheISA::copyRegs(tc, ctc);
857        #else
858            fatal("sys_clone is not implemented for this ISA\n");
859        #endif
860
861        // Set up stack register
862        ctc->setIntReg(TheISA::StackPointerReg, newStack);
863
864        // Set up syscall return values in parent and child
865        ctc->setIntReg(ReturnValueReg, 0); // return value, child
866
867        // Alpha needs SyscallSuccessReg=0 in child
868        #if THE_ISA == ALPHA_ISA
869            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
870        #endif
871
872        // In SPARC/Linux, clone returns 0 on pseudo-return register if
873        // parent, non-zero if child
874        #if THE_ISA == SPARC_ISA
875            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
876            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
877        #endif
878
879        ctc->pcState(tc->nextInstAddr());
880
881        ctc->activate();
882
883        // Should return nonzero child TID in parent's syscall return register,
884        // but for our pthread library any non-zero value will work
885        return 1;
886    } else {
887        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
888        return 0;
889    }
890}
891
892SyscallReturn
893fallocateFunc(SyscallDesc *desc, int callnum, Process *process,
894              ThreadContext *tc)
895{
896#if NO_FALLOCATE
897    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
898#else
899    int index = 0;
900    int tgt_fd = process->getSyscallArg(tc, index);
901    int mode = process->getSyscallArg(tc, index);
902    off_t offset = process->getSyscallArg(tc, index);
903    off_t len = process->getSyscallArg(tc, index);
904
905    int sim_fd = process->getSimFD(tgt_fd);
906    if (sim_fd < 0)
907        return -EBADF;
908
909    int result = fallocate(sim_fd, mode, offset, len);
910    if (result < 0)
911        return -errno;
912#endif
913    return 0;
914}
915
916SyscallReturn
917accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
918           int index)
919{
920    string path;
921    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
922        return -EFAULT;
923
924    // Adjust path for current working directory
925    path = p->fullPath(path);
926
927    mode_t mode = p->getSyscallArg(tc, index);
928
929    int result = access(path.c_str(), mode);
930    return (result == -1) ? -errno : result;
931}
932
933SyscallReturn
934accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
935{
936    return accessFunc(desc, callnum, p, tc, 0);
937}
938
939