syscall_emul.hh revision 13629:0b83d5382336
13101Sstever@eecs.umich.edu/* 27534Ssteve.reinhardt@amd.com * Copyright (c) 2012-2013, 2015 ARM Limited 33101Sstever@eecs.umich.edu * Copyright (c) 2015 Advanced Micro Devices, Inc. 43101Sstever@eecs.umich.edu * All rights reserved 53101Sstever@eecs.umich.edu * 63101Sstever@eecs.umich.edu * The license below extends only to copyright in the software and shall 73101Sstever@eecs.umich.edu * not be construed as granting a license to any other intellectual 83101Sstever@eecs.umich.edu * property including but not limited to intellectual property relating 93101Sstever@eecs.umich.edu * to a hardware implementation of the functionality of the software 103101Sstever@eecs.umich.edu * licensed hereunder. You may use the software subject to the license 113101Sstever@eecs.umich.edu * terms below provided that you ensure that this notice is replicated 123101Sstever@eecs.umich.edu * unmodified and in its entirety in all distributions of the software, 133101Sstever@eecs.umich.edu * modified or unmodified, in source code or in binary form. 143101Sstever@eecs.umich.edu * 153101Sstever@eecs.umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan 163101Sstever@eecs.umich.edu * All rights reserved. 173101Sstever@eecs.umich.edu * 183101Sstever@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 193101Sstever@eecs.umich.edu * modification, are permitted provided that the following conditions are 203101Sstever@eecs.umich.edu * met: redistributions of source code must retain the above copyright 213101Sstever@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 223101Sstever@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 233101Sstever@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 243101Sstever@eecs.umich.edu * documentation and/or other materials provided with the distribution; 253101Sstever@eecs.umich.edu * neither the name of the copyright holders nor the names of its 263101Sstever@eecs.umich.edu * contributors may be used to endorse or promote products derived from 273101Sstever@eecs.umich.edu * this software without specific prior written permission. 283101Sstever@eecs.umich.edu * 293101Sstever@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 303101Sstever@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 313101Sstever@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 323101Sstever@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 333101Sstever@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 343101Sstever@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 353101Sstever@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 363101Sstever@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 373101Sstever@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 383101Sstever@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 393101Sstever@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 403101Sstever@eecs.umich.edu * 413101Sstever@eecs.umich.edu * Authors: Steve Reinhardt 423101Sstever@eecs.umich.edu * Kevin Lim 433101Sstever@eecs.umich.edu */ 443101Sstever@eecs.umich.edu 453101Sstever@eecs.umich.edu#ifndef __SIM_SYSCALL_EMUL_HH__ 463101Sstever@eecs.umich.edu#define __SIM_SYSCALL_EMUL_HH__ 473101Sstever@eecs.umich.edu 483885Sbinkertn@umich.edu#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 493885Sbinkertn@umich.edu defined(__FreeBSD__) || defined(__CYGWIN__) || \ 504762Snate@binkert.org defined(__NetBSD__)) 513885Sbinkertn@umich.edu#define NO_STAT64 1 523885Sbinkertn@umich.edu#else 537528Ssteve.reinhardt@amd.com#define NO_STAT64 0 543885Sbinkertn@umich.edu#endif 554380Sbinkertn@umich.edu 564167Sbinkertn@umich.edu#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 573102Sstever@eecs.umich.edu defined(__FreeBSD__) || defined(__NetBSD__)) 583101Sstever@eecs.umich.edu#define NO_STATFS 1 594762Snate@binkert.org#else 604762Snate@binkert.org#define NO_STATFS 0 614762Snate@binkert.org#endif 624762Snate@binkert.org 634762Snate@binkert.org#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 644762Snate@binkert.org defined(__FreeBSD__) || defined(__NetBSD__)) 654762Snate@binkert.org#define NO_FALLOCATE 1 664762Snate@binkert.org#else 674762Snate@binkert.org#define NO_FALLOCATE 0 685033Smilesck@eecs.umich.edu#endif 695033Smilesck@eecs.umich.edu 705033Smilesck@eecs.umich.edu/// 715033Smilesck@eecs.umich.edu/// @file syscall_emul.hh 725033Smilesck@eecs.umich.edu/// 735033Smilesck@eecs.umich.edu/// This file defines objects used to emulate syscalls from the target 745033Smilesck@eecs.umich.edu/// application on the host machine. 755033Smilesck@eecs.umich.edu 765033Smilesck@eecs.umich.edu#ifdef __CYGWIN32__ 775033Smilesck@eecs.umich.edu#include <sys/fcntl.h> 783101Sstever@eecs.umich.edu 793101Sstever@eecs.umich.edu#endif 803101Sstever@eecs.umich.edu#include <fcntl.h> 815033Smilesck@eecs.umich.edu#include <poll.h> 823101Sstever@eecs.umich.edu#include <sys/mman.h> 837673Snate@binkert.org#include <sys/socket.h> 847673Snate@binkert.org#include <sys/stat.h> 857673Snate@binkert.org#if (NO_STATFS == 0) 867673Snate@binkert.org#include <sys/statfs.h> 877673Snate@binkert.org#else 887673Snate@binkert.org#include <sys/mount.h> 897673Snate@binkert.org#endif 903101Sstever@eecs.umich.edu#include <sys/time.h> 913101Sstever@eecs.umich.edu#include <sys/types.h> 923101Sstever@eecs.umich.edu#include <sys/uio.h> 933101Sstever@eecs.umich.edu#include <unistd.h> 943101Sstever@eecs.umich.edu 953101Sstever@eecs.umich.edu#include <cerrno> 963101Sstever@eecs.umich.edu#include <memory> 973101Sstever@eecs.umich.edu#include <string> 983101Sstever@eecs.umich.edu 993101Sstever@eecs.umich.edu#include "arch/generic/tlb.hh" 1003101Sstever@eecs.umich.edu#include "arch/utility.hh" 1013101Sstever@eecs.umich.edu#include "base/intmath.hh" 1023101Sstever@eecs.umich.edu#include "base/loader/object_file.hh" 1036656Snate@binkert.org#include "base/logging.hh" 1046656Snate@binkert.org#include "base/trace.hh" 1053101Sstever@eecs.umich.edu#include "base/types.hh" 1063101Sstever@eecs.umich.edu#include "config/the_isa.hh" 1073101Sstever@eecs.umich.edu#include "cpu/base.hh" 1083101Sstever@eecs.umich.edu#include "cpu/thread_context.hh" 1093101Sstever@eecs.umich.edu#include "mem/page_table.hh" 1103101Sstever@eecs.umich.edu#include "params/Process.hh" 1113101Sstever@eecs.umich.edu#include "sim/emul_driver.hh" 1123101Sstever@eecs.umich.edu#include "sim/futex_map.hh" 1133101Sstever@eecs.umich.edu#include "sim/process.hh" 1143101Sstever@eecs.umich.edu#include "sim/syscall_debug_macros.hh" 1153101Sstever@eecs.umich.edu#include "sim/syscall_desc.hh" 1163101Sstever@eecs.umich.edu#include "sim/syscall_emul_buf.hh" 1173101Sstever@eecs.umich.edu#include "sim/syscall_return.hh" 1183101Sstever@eecs.umich.edu 1193101Sstever@eecs.umich.edu#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN) 1203101Sstever@eecs.umich.edu#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) 1213101Sstever@eecs.umich.edu#endif 1223101Sstever@eecs.umich.edu 1233101Sstever@eecs.umich.edu////////////////////////////////////////////////////////////////////// 1243101Sstever@eecs.umich.edu// 1253101Sstever@eecs.umich.edu// The following emulation functions are generic enough that they 1263101Sstever@eecs.umich.edu// don't need to be recompiled for different emulated OS's. They are 1273101Sstever@eecs.umich.edu// defined in sim/syscall_emul.cc. 1283101Sstever@eecs.umich.edu// 1293101Sstever@eecs.umich.edu////////////////////////////////////////////////////////////////////// 1303101Sstever@eecs.umich.edu 1313101Sstever@eecs.umich.edu 1323101Sstever@eecs.umich.edu/// Handler for unimplemented syscalls that we haven't thought about. 1333101Sstever@eecs.umich.eduSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1343101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1353101Sstever@eecs.umich.edu 1363101Sstever@eecs.umich.edu/// Handler for unimplemented syscalls that we never intend to 1373101Sstever@eecs.umich.edu/// implement (signal handling, etc.) and should not affect the correct 1385033Smilesck@eecs.umich.edu/// behavior of the program. Print a warning only if the appropriate 1396656Snate@binkert.org/// trace flag is enabled. Return success to the target program. 1405033Smilesck@eecs.umich.eduSyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1415033Smilesck@eecs.umich.edu Process *p, ThreadContext *tc); 1425033Smilesck@eecs.umich.edu 1433101Sstever@eecs.umich.edu// Target fallocateFunc() handler. 1443101Sstever@eecs.umich.eduSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 1453101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1463101Sstever@eecs.umich.edu 1473101Sstever@eecs.umich.edu/// Target exit() handler: terminate current context. 1483101Sstever@eecs.umich.eduSyscallReturn exitFunc(SyscallDesc *desc, int num, 1493101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1503101Sstever@eecs.umich.edu 1513101Sstever@eecs.umich.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1523101Sstever@eecs.umich.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1533101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1543101Sstever@eecs.umich.edu 1553101Sstever@eecs.umich.edu/// Target set_tid_address() handler. 1563102Sstever@eecs.umich.eduSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1573101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1583101Sstever@eecs.umich.edu 1593101Sstever@eecs.umich.edu/// Target getpagesize() handler. 1607673Snate@binkert.orgSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1617673Snate@binkert.org Process *p, ThreadContext *tc); 1623101Sstever@eecs.umich.edu 1637673Snate@binkert.org/// Target brk() handler: set brk address. 1647673Snate@binkert.orgSyscallReturn brkFunc(SyscallDesc *desc, int num, 1653101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1667673Snate@binkert.org 1677673Snate@binkert.org/// Target close() handler. 1683101Sstever@eecs.umich.eduSyscallReturn closeFunc(SyscallDesc *desc, int num, 1693101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1703101Sstever@eecs.umich.edu 1713101Sstever@eecs.umich.edu/// Target lseek() handler. 1723101Sstever@eecs.umich.eduSyscallReturn lseekFunc(SyscallDesc *desc, int num, 1733101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1745033Smilesck@eecs.umich.edu 1755475Snate@binkert.org/// Target _llseek() handler. 1765475Snate@binkert.orgSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1775475Snate@binkert.org Process *p, ThreadContext *tc); 1785475Snate@binkert.org 1793101Sstever@eecs.umich.edu/// Target munmap() handler. 1803101Sstever@eecs.umich.eduSyscallReturn munmapFunc(SyscallDesc *desc, int num, 1813101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1824762Snate@binkert.org 1834762Snate@binkert.org/// Target shutdown() handler. 1844762Snate@binkert.orgSyscallReturn shutdownFunc(SyscallDesc *desc, int num, 1853101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 1863101Sstever@eecs.umich.edu 1873101Sstever@eecs.umich.edu/// Target gethostname() handler. 1887528Ssteve.reinhardt@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 1897528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 1907528Ssteve.reinhardt@amd.com 1917528Ssteve.reinhardt@amd.com/// Target getcwd() handler. 1927528Ssteve.reinhardt@amd.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1937528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 1943101Sstever@eecs.umich.edu 1957528Ssteve.reinhardt@amd.com/// Target readlink() handler. 1967528Ssteve.reinhardt@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1977528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc, 1987528Ssteve.reinhardt@amd.com int index = 0); 1997528Ssteve.reinhardt@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2007528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 2017528Ssteve.reinhardt@amd.com 2027528Ssteve.reinhardt@amd.com/// Target unlink() handler. 2037528Ssteve.reinhardt@amd.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2047528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc, 2057528Ssteve.reinhardt@amd.com int index); 2067528Ssteve.reinhardt@amd.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2077528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 2087528Ssteve.reinhardt@amd.com 2097528Ssteve.reinhardt@amd.com/// Target link() handler 2107528Ssteve.reinhardt@amd.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p, 2117528Ssteve.reinhardt@amd.com ThreadContext *tc); 2127528Ssteve.reinhardt@amd.com 2137528Ssteve.reinhardt@amd.com/// Target symlink() handler. 2147528Ssteve.reinhardt@amd.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p, 2157528Ssteve.reinhardt@amd.com ThreadContext *tc); 2167528Ssteve.reinhardt@amd.com 2177528Ssteve.reinhardt@amd.com/// Target mkdir() handler. 2187528Ssteve.reinhardt@amd.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2197528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 2207528Ssteve.reinhardt@amd.com 2217528Ssteve.reinhardt@amd.com/// Target mknod() handler. 2227528Ssteve.reinhardt@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, 2237528Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 2243101Sstever@eecs.umich.edu 2253101Sstever@eecs.umich.edu/// Target chdir() handler. 2266656Snate@binkert.orgSyscallReturn chdirFunc(SyscallDesc *desc, int num, 2276656Snate@binkert.org Process *p, ThreadContext *tc); 2283101Sstever@eecs.umich.edu 2293101Sstever@eecs.umich.edu// Target rmdir() handler. 2303101Sstever@eecs.umich.eduSyscallReturn rmdirFunc(SyscallDesc *desc, int num, 2313101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 2323101Sstever@eecs.umich.edu 2333101Sstever@eecs.umich.edu/// Target rename() handler. 2343101Sstever@eecs.umich.eduSyscallReturn renameFunc(SyscallDesc *desc, int num, 2354762Snate@binkert.org Process *p, ThreadContext *tc); 2364762Snate@binkert.org 2374762Snate@binkert.org 2384762Snate@binkert.org/// Target truncate() handler. 2397528Ssteve.reinhardt@amd.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2404762Snate@binkert.org Process *p, ThreadContext *tc); 2414762Snate@binkert.org 2424762Snate@binkert.org 2437673Snate@binkert.org/// Target ftruncate() handler. 2447677Snate@binkert.orgSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2454762Snate@binkert.org Process *p, ThreadContext *tc); 2467673Snate@binkert.org 2477675Snate@binkert.org 2487675Snate@binkert.org/// Target truncate64() handler. 2497675Snate@binkert.orgSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2507675Snate@binkert.org Process *p, ThreadContext *tc); 2517675Snate@binkert.org 2527675Snate@binkert.org/// Target ftruncate64() handler. 2537673Snate@binkert.orgSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2547675Snate@binkert.org Process *p, ThreadContext *tc); 2557675Snate@binkert.org 2567675Snate@binkert.org 2577675Snate@binkert.org/// Target umask() handler. 2587675Snate@binkert.orgSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2597673Snate@binkert.org Process *p, ThreadContext *tc); 2607675Snate@binkert.org 2617675Snate@binkert.org/// Target gettid() handler. 2627675Snate@binkert.orgSyscallReturn gettidFunc(SyscallDesc *desc, int num, 2637675Snate@binkert.org Process *p, ThreadContext *tc); 2647675Snate@binkert.org 2657675Snate@binkert.org/// Target chown() handler. 2667675Snate@binkert.orgSyscallReturn chownFunc(SyscallDesc *desc, int num, 2677675Snate@binkert.org Process *p, ThreadContext *tc); 2687675Snate@binkert.org 2697675Snate@binkert.org/// Target setpgid() handler. 2707675Snate@binkert.orgSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2717675Snate@binkert.org Process *p, ThreadContext *tc); 2727675Snate@binkert.org 2737675Snate@binkert.org/// Target fchown() handler. 2747675Snate@binkert.orgSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2757675Snate@binkert.org Process *p, ThreadContext *tc); 2767673Snate@binkert.org 2777673Snate@binkert.org/// Target dup() handler. 2783101Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2797675Snate@binkert.org Process *process, ThreadContext *tc); 2807675Snate@binkert.org 2817673Snate@binkert.org/// Target dup2() handler. 2827673Snate@binkert.orgSyscallReturn dup2Func(SyscallDesc *desc, int num, 2837673Snate@binkert.org Process *process, ThreadContext *tc); 2843101Sstever@eecs.umich.edu 2857673Snate@binkert.org/// Target fcntl() handler. 2867673Snate@binkert.orgSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2873101Sstever@eecs.umich.edu Process *process, ThreadContext *tc); 2883101Sstever@eecs.umich.edu 2893101Sstever@eecs.umich.edu/// Target fcntl64() handler. 2903101Sstever@eecs.umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2913101Sstever@eecs.umich.edu Process *process, ThreadContext *tc); 2923101Sstever@eecs.umich.edu 2933101Sstever@eecs.umich.edu/// Target setuid() handler. 2943101Sstever@eecs.umich.eduSyscallReturn setuidFunc(SyscallDesc *desc, int num, 2953101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 2963101Sstever@eecs.umich.edu 2973101Sstever@eecs.umich.edu/// Target pipe() handler. 2983101Sstever@eecs.umich.eduSyscallReturn pipeFunc(SyscallDesc *desc, int num, 2993101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3003101Sstever@eecs.umich.edu 3013101Sstever@eecs.umich.edu/// Internal pipe() handler. 3025033Smilesck@eecs.umich.eduSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 3035033Smilesck@eecs.umich.edu ThreadContext *tc, bool pseudoPipe); 3043101Sstever@eecs.umich.edu 3053101Sstever@eecs.umich.edu/// Target getpid() handler. 3063101Sstever@eecs.umich.eduSyscallReturn getpidFunc(SyscallDesc *desc, int num, 3073101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3083101Sstever@eecs.umich.edu 3093101Sstever@eecs.umich.edu// Target getpeername() handler. 3103101Sstever@eecs.umich.eduSyscallReturn getpeernameFunc(SyscallDesc *desc, int num, 3113101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3123101Sstever@eecs.umich.edu 3133101Sstever@eecs.umich.edu// Target bind() handler. 3143101Sstever@eecs.umich.eduSyscallReturn bindFunc(SyscallDesc *desc, int num, 3153101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3163101Sstever@eecs.umich.edu 3173101Sstever@eecs.umich.edu// Target listen() handler. 3183101Sstever@eecs.umich.eduSyscallReturn listenFunc(SyscallDesc *desc, int num, 3193101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3203101Sstever@eecs.umich.edu 3213101Sstever@eecs.umich.edu// Target connect() handler. 3223101Sstever@eecs.umich.eduSyscallReturn connectFunc(SyscallDesc *desc, int num, 3233101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3243101Sstever@eecs.umich.edu 3253101Sstever@eecs.umich.edu#if defined(SYS_getdents) 3263101Sstever@eecs.umich.edu// Target getdents() handler. 3273101Sstever@eecs.umich.eduSyscallReturn getdentsFunc(SyscallDesc *desc, int num, 3283101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3297673Snate@binkert.org#endif 3307673Snate@binkert.org 3317673Snate@binkert.org#if defined(SYS_getdents64) 3327673Snate@binkert.org// Target getdents() handler. 3337673Snate@binkert.orgSyscallReturn getdents64Func(SyscallDesc *desc, int num, 3347673Snate@binkert.org Process *p, ThreadContext *tc); 3357673Snate@binkert.org#endif 3367673Snate@binkert.org 3374762Snate@binkert.org// Target sendto() handler. 3384762Snate@binkert.orgSyscallReturn sendtoFunc(SyscallDesc *desc, int num, 3394762Snate@binkert.org Process *p, ThreadContext *tc); 3403101Sstever@eecs.umich.edu 3413101Sstever@eecs.umich.edu// Target recvfrom() handler. 3423101Sstever@eecs.umich.eduSyscallReturn recvfromFunc(SyscallDesc *desc, int num, 3433101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3443101Sstever@eecs.umich.edu 3453101Sstever@eecs.umich.edu// Target recvmsg() handler. 3463101Sstever@eecs.umich.eduSyscallReturn recvmsgFunc(SyscallDesc *desc, int num, 3473101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3483101Sstever@eecs.umich.edu 3493101Sstever@eecs.umich.edu// Target sendmsg() handler. 3503101Sstever@eecs.umich.eduSyscallReturn sendmsgFunc(SyscallDesc *desc, int num, 3513714Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3523714Sstever@eecs.umich.edu 3533714Sstever@eecs.umich.edu// Target getuid() handler. 3543714Sstever@eecs.umich.eduSyscallReturn getuidFunc(SyscallDesc *desc, int num, 3553714Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3563714Sstever@eecs.umich.edu 3573101Sstever@eecs.umich.edu/// Target getgid() handler. 3583101Sstever@eecs.umich.eduSyscallReturn getgidFunc(SyscallDesc *desc, int num, 3593101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3603101Sstever@eecs.umich.edu 3613101Sstever@eecs.umich.edu/// Target getppid() handler. 3623101Sstever@eecs.umich.eduSyscallReturn getppidFunc(SyscallDesc *desc, int num, 3633101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3643101Sstever@eecs.umich.edu 3653101Sstever@eecs.umich.edu/// Target geteuid() handler. 3663101Sstever@eecs.umich.eduSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3673101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3683101Sstever@eecs.umich.edu 3693101Sstever@eecs.umich.edu/// Target getegid() handler. 3703101Sstever@eecs.umich.eduSyscallReturn getegidFunc(SyscallDesc *desc, int num, 3713101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3723101Sstever@eecs.umich.edu 3733101Sstever@eecs.umich.edu/// Target access() handler 3743101Sstever@eecs.umich.eduSyscallReturn accessFunc(SyscallDesc *desc, int num, 3753101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3763101Sstever@eecs.umich.eduSyscallReturn accessFunc(SyscallDesc *desc, int num, 3773101Sstever@eecs.umich.edu Process *p, ThreadContext *tc, 3783101Sstever@eecs.umich.edu int index); 3793101Sstever@eecs.umich.edu 3803101Sstever@eecs.umich.edu// Target getsockopt() handler. 3813101Sstever@eecs.umich.eduSyscallReturn getsockoptFunc(SyscallDesc *desc, int num, 3825033Smilesck@eecs.umich.edu Process *p, ThreadContext *tc); 3833101Sstever@eecs.umich.edu 3843101Sstever@eecs.umich.edu// Target setsockopt() handler. 3853101Sstever@eecs.umich.eduSyscallReturn setsockoptFunc(SyscallDesc *desc, int num, 3863101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3873101Sstever@eecs.umich.edu 3883101Sstever@eecs.umich.edu// Target getsockname() handler. 3893101Sstever@eecs.umich.eduSyscallReturn getsocknameFunc(SyscallDesc *desc, int num, 3903101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 3913101Sstever@eecs.umich.edu 3923101Sstever@eecs.umich.edu/// Futex system call 3933101Sstever@eecs.umich.edu/// Implemented by Daniel Sanchez 3943101Sstever@eecs.umich.edu/// Used by printf's in multi-threaded apps 3955822Ssaidi@eecs.umich.edutemplate <class OS> 3965822Ssaidi@eecs.umich.eduSyscallReturn 3973101Sstever@eecs.umich.edufutexFunc(SyscallDesc *desc, int callnum, Process *process, 3983101Sstever@eecs.umich.edu ThreadContext *tc) 3993101Sstever@eecs.umich.edu{ 4003101Sstever@eecs.umich.edu using namespace std; 4013101Sstever@eecs.umich.edu 4023101Sstever@eecs.umich.edu int index = 0; 4033101Sstever@eecs.umich.edu Addr uaddr = process->getSyscallArg(tc, index); 4043101Sstever@eecs.umich.edu int op = process->getSyscallArg(tc, index); 4053101Sstever@eecs.umich.edu int val = process->getSyscallArg(tc, index); 4063101Sstever@eecs.umich.edu 4073101Sstever@eecs.umich.edu /* 4083101Sstever@eecs.umich.edu * Unsupported option that does not affect the correctness of the 4093101Sstever@eecs.umich.edu * application. This is a performance optimization utilized by Linux. 4103101Sstever@eecs.umich.edu */ 4113101Sstever@eecs.umich.edu op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 4123101Sstever@eecs.umich.edu 4133101Sstever@eecs.umich.edu FutexMap &futex_map = tc->getSystemPtr()->futexMap; 4143101Sstever@eecs.umich.edu 4153101Sstever@eecs.umich.edu if (OS::TGT_FUTEX_WAIT == op) { 4163101Sstever@eecs.umich.edu // Ensure futex system call accessed atomically. 4173101Sstever@eecs.umich.edu BufferArg buf(uaddr, sizeof(int)); 4183102Sstever@eecs.umich.edu buf.copyIn(tc->getMemProxy()); 4193714Sstever@eecs.umich.edu int mem_val = *(int*)buf.bufferPtr(); 4203101Sstever@eecs.umich.edu 4213714Sstever@eecs.umich.edu /* 4223714Sstever@eecs.umich.edu * The value in memory at uaddr is not equal with the expected val 4233714Sstever@eecs.umich.edu * (a different thread must have changed it before the system call was 4243101Sstever@eecs.umich.edu * invoked). In this case, we need to throw an error. 4253101Sstever@eecs.umich.edu */ 4267673Snate@binkert.org if (val != mem_val) 4277673Snate@binkert.org return -OS::TGT_EWOULDBLOCK; 4287673Snate@binkert.org 4297673Snate@binkert.org futex_map.suspend(uaddr, process->tgid(), tc); 4307673Snate@binkert.org 4317673Snate@binkert.org return 0; 4327673Snate@binkert.org } else if (OS::TGT_FUTEX_WAKE == op) { 4337673Snate@binkert.org return futex_map.wakeup(uaddr, process->tgid(), val); 4347673Snate@binkert.org } 4357673Snate@binkert.org 4367673Snate@binkert.org warn("futex: op %d not implemented; ignoring.", op); 4374762Snate@binkert.org return -ENOSYS; 4384762Snate@binkert.org} 4394762Snate@binkert.org 4403101Sstever@eecs.umich.edu 4413101Sstever@eecs.umich.edu/// Pseudo Funcs - These functions use a different return convension, 4423101Sstever@eecs.umich.edu/// returning a second value in a register other than the normal return register 4433101Sstever@eecs.umich.eduSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 4443101Sstever@eecs.umich.edu Process *process, ThreadContext *tc); 4453101Sstever@eecs.umich.edu 4463101Sstever@eecs.umich.edu/// Target getpidPseudo() handler. 4473101Sstever@eecs.umich.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 4483101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 4493101Sstever@eecs.umich.edu 4503101Sstever@eecs.umich.edu/// Target getuidPseudo() handler. 4513101Sstever@eecs.umich.eduSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 4523101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 4533101Sstever@eecs.umich.edu 4543101Sstever@eecs.umich.edu/// Target getgidPseudo() handler. 4553101Sstever@eecs.umich.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 4563101Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 4573101Sstever@eecs.umich.edu 4583101Sstever@eecs.umich.edu 4593101Sstever@eecs.umich.edu/// A readable name for 1,000,000, for converting microseconds to seconds. 4604446Sbinkertn@umich.educonst int one_million = 1000000; 4613101Sstever@eecs.umich.edu/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 4625468Snate@binkert.orgconst int one_billion = 1000000000; 4635468Snate@binkert.org 4645468Snate@binkert.org/// Approximate seconds since the epoch (1/1/1970). About a billion, 4655468Snate@binkert.org/// by my reckoning. We want to keep this a constant (not use the 4665468Snate@binkert.org/// real-world time) to keep simulations repeatable. 4675468Snate@binkert.orgconst unsigned seconds_since_epoch = 1000000000; 4685468Snate@binkert.org 4694762Snate@binkert.org/// Helper function to convert current elapsed time to seconds and 4704762Snate@binkert.org/// microseconds. 4714762Snate@binkert.orgtemplate <class T1, class T2> 4723101Sstever@eecs.umich.eduvoid 4733101Sstever@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec) 4743101Sstever@eecs.umich.edu{ 4753101Sstever@eecs.umich.edu uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 4763101Sstever@eecs.umich.edu sec = elapsed_usecs / one_million; 4773101Sstever@eecs.umich.edu usec = elapsed_usecs % one_million; 4783101Sstever@eecs.umich.edu} 4793101Sstever@eecs.umich.edu 4803102Sstever@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 4813101Sstever@eecs.umich.edu/// nanoseconds. 4823101Sstever@eecs.umich.edutemplate <class T1, class T2> 4833101Sstever@eecs.umich.eduvoid 4844168Sbinkertn@umich.edugetElapsedTimeNano(T1 &sec, T2 &nsec) 4853101Sstever@eecs.umich.edu{ 4863101Sstever@eecs.umich.edu uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 4873101Sstever@eecs.umich.edu sec = elapsed_nsecs / one_billion; 4883101Sstever@eecs.umich.edu nsec = elapsed_nsecs % one_billion; 4893101Sstever@eecs.umich.edu} 4903101Sstever@eecs.umich.edu 4913102Sstever@eecs.umich.edu////////////////////////////////////////////////////////////////////// 4923101Sstever@eecs.umich.edu// 4933101Sstever@eecs.umich.edu// The following emulation functions are generic, but need to be 4943101Sstever@eecs.umich.edu// templated to account for differences in types, constants, etc. 4953101Sstever@eecs.umich.edu// 4963101Sstever@eecs.umich.edu////////////////////////////////////////////////////////////////////// 4973101Sstever@eecs.umich.edu 4983101Sstever@eecs.umich.edu typedef struct statfs hst_statfs; 4993101Sstever@eecs.umich.edu#if NO_STAT64 5003101Sstever@eecs.umich.edu typedef struct stat hst_stat; 5013101Sstever@eecs.umich.edu typedef struct stat hst_stat64; 5023101Sstever@eecs.umich.edu#else 5033102Sstever@eecs.umich.edu typedef struct stat hst_stat; 5043101Sstever@eecs.umich.edu typedef struct stat64 hst_stat64; 5053101Sstever@eecs.umich.edu#endif 5063101Sstever@eecs.umich.edu 5073584Ssaidi@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 5083584Ssaidi@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 5093584Ssaidi@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 5103584Ssaidi@eecs.umich.edu 5113584Ssaidi@eecs.umich.edutemplate <typename target_stat, typename host_stat> 5123101Sstever@eecs.umich.eduvoid 5133101Sstever@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 5145033Smilesck@eecs.umich.edu{ 5153101Sstever@eecs.umich.edu using namespace TheISA; 5163101Sstever@eecs.umich.edu 5173101Sstever@eecs.umich.edu if (fakeTTY) 5183101Sstever@eecs.umich.edu tgt->st_dev = 0xA; 5193101Sstever@eecs.umich.edu else 5203101Sstever@eecs.umich.edu tgt->st_dev = host->st_dev; 5213101Sstever@eecs.umich.edu tgt->st_dev = TheISA::htog(tgt->st_dev); 5223101Sstever@eecs.umich.edu tgt->st_ino = host->st_ino; 5233101Sstever@eecs.umich.edu tgt->st_ino = TheISA::htog(tgt->st_ino); 5243101Sstever@eecs.umich.edu tgt->st_mode = host->st_mode; 5253101Sstever@eecs.umich.edu if (fakeTTY) { 5263101Sstever@eecs.umich.edu // Claim to be a character device 5273101Sstever@eecs.umich.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 5283101Sstever@eecs.umich.edu tgt->st_mode |= S_IFCHR; // Set S_IFCHR 5293101Sstever@eecs.umich.edu } 5303101Sstever@eecs.umich.edu tgt->st_mode = TheISA::htog(tgt->st_mode); 5313101Sstever@eecs.umich.edu tgt->st_nlink = host->st_nlink; 5323101Sstever@eecs.umich.edu tgt->st_nlink = TheISA::htog(tgt->st_nlink); 5333101Sstever@eecs.umich.edu tgt->st_uid = host->st_uid; 5343101Sstever@eecs.umich.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 5353101Sstever@eecs.umich.edu tgt->st_gid = host->st_gid; 5363101Sstever@eecs.umich.edu tgt->st_gid = TheISA::htog(tgt->st_gid); 5373101Sstever@eecs.umich.edu if (fakeTTY) 5383101Sstever@eecs.umich.edu tgt->st_rdev = 0x880d; 5393101Sstever@eecs.umich.edu else 5403101Sstever@eecs.umich.edu tgt->st_rdev = host->st_rdev; 5413101Sstever@eecs.umich.edu tgt->st_rdev = TheISA::htog(tgt->st_rdev); 5423101Sstever@eecs.umich.edu tgt->st_size = host->st_size; 5433101Sstever@eecs.umich.edu tgt->st_size = TheISA::htog(tgt->st_size); 5445219Ssaidi@eecs.umich.edu tgt->st_atimeX = host->st_atime; 5455219Ssaidi@eecs.umich.edu tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 5465219Ssaidi@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 5473101Sstever@eecs.umich.edu tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 5483101Sstever@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 5493101Sstever@eecs.umich.edu tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 5503101Sstever@eecs.umich.edu // Force the block size to be 8KB. This helps to ensure buffered io works 5513101Sstever@eecs.umich.edu // consistently across different hosts. 5523101Sstever@eecs.umich.edu tgt->st_blksize = 0x2000; 5533101Sstever@eecs.umich.edu tgt->st_blksize = TheISA::htog(tgt->st_blksize); 5543101Sstever@eecs.umich.edu tgt->st_blocks = host->st_blocks; 5553101Sstever@eecs.umich.edu tgt->st_blocks = TheISA::htog(tgt->st_blocks); 5563101Sstever@eecs.umich.edu} 5573101Sstever@eecs.umich.edu 5583101Sstever@eecs.umich.edu// Same for stat64 5593101Sstever@eecs.umich.edu 5603101Sstever@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 5613101Sstever@eecs.umich.eduvoid 5623101Sstever@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 5637673Snate@binkert.org{ 5647673Snate@binkert.org using namespace TheISA; 5657675Snate@binkert.org 5667673Snate@binkert.org convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 5677675Snate@binkert.org#if defined(STAT_HAVE_NSEC) 5687675Snate@binkert.org tgt->st_atime_nsec = host->st_atime_nsec; 5697675Snate@binkert.org tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 5707675Snate@binkert.org tgt->st_mtime_nsec = host->st_mtime_nsec; 5717675Snate@binkert.org tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 5727673Snate@binkert.org tgt->st_ctime_nsec = host->st_ctime_nsec; 5733101Sstever@eecs.umich.edu tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 5743101Sstever@eecs.umich.edu#else 5757673Snate@binkert.org tgt->st_atime_nsec = 0; 5764762Snate@binkert.org tgt->st_mtime_nsec = 0; 5777675Snate@binkert.org tgt->st_ctime_nsec = 0; 5784762Snate@binkert.org#endif 5794762Snate@binkert.org} 5804762Snate@binkert.org 5814762Snate@binkert.org// Here are a couple of convenience functions 5824762Snate@binkert.orgtemplate<class OS> 5833101Sstever@eecs.umich.eduvoid 5843101Sstever@eecs.umich.educopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 5853101Sstever@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 5867673Snate@binkert.org{ 5874762Snate@binkert.org typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5887675Snate@binkert.org tgt_stat_buf tgt(addr); 5894762Snate@binkert.org convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5904762Snate@binkert.org tgt.copyOut(mem); 5914762Snate@binkert.org} 5924762Snate@binkert.org 5934762Snate@binkert.orgtemplate<class OS> 5943101Sstever@eecs.umich.eduvoid 5953101Sstever@eecs.umich.educopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5963101Sstever@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5973101Sstever@eecs.umich.edu{ 5983101Sstever@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5993101Sstever@eecs.umich.edu tgt_stat_buf tgt(addr); 6003101Sstever@eecs.umich.edu convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 6013101Sstever@eecs.umich.edu tgt.copyOut(mem); 6023102Sstever@eecs.umich.edu} 6033101Sstever@eecs.umich.edu 6043101Sstever@eecs.umich.edutemplate <class OS> 6053101Sstever@eecs.umich.eduvoid 6064762Snate@binkert.orgcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 6074762Snate@binkert.org hst_statfs *host) 6084762Snate@binkert.org{ 6093101Sstever@eecs.umich.edu TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 6103101Sstever@eecs.umich.edu 6113101Sstever@eecs.umich.edu tgt->f_type = TheISA::htog(host->f_type); 6123101Sstever@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 6133101Sstever@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_iosize); 6143101Sstever@eecs.umich.edu#else 6153101Sstever@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_bsize); 6163101Sstever@eecs.umich.edu#endif 6173101Sstever@eecs.umich.edu tgt->f_blocks = TheISA::htog(host->f_blocks); 6183101Sstever@eecs.umich.edu tgt->f_bfree = TheISA::htog(host->f_bfree); 6193101Sstever@eecs.umich.edu tgt->f_bavail = TheISA::htog(host->f_bavail); 6203101Sstever@eecs.umich.edu tgt->f_files = TheISA::htog(host->f_files); 6213101Sstever@eecs.umich.edu tgt->f_ffree = TheISA::htog(host->f_ffree); 6223101Sstever@eecs.umich.edu memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 6233101Sstever@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 6243101Sstever@eecs.umich.edu tgt->f_namelen = TheISA::htog(host->f_namemax); 6253101Sstever@eecs.umich.edu tgt->f_frsize = TheISA::htog(host->f_bsize); 6263101Sstever@eecs.umich.edu#elif defined(__APPLE__) 6273101Sstever@eecs.umich.edu tgt->f_namelen = 0; 6283101Sstever@eecs.umich.edu tgt->f_frsize = 0; 6294380Sbinkertn@umich.edu#else 6304380Sbinkertn@umich.edu tgt->f_namelen = TheISA::htog(host->f_namelen); 6314380Sbinkertn@umich.edu tgt->f_frsize = TheISA::htog(host->f_frsize); 6323101Sstever@eecs.umich.edu#endif 6334380Sbinkertn@umich.edu#if defined(__linux__) 6344380Sbinkertn@umich.edu memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 6354380Sbinkertn@umich.edu#else 6363101Sstever@eecs.umich.edu /* 6373101Sstever@eecs.umich.edu * The fields are different sizes per OS. Don't bother with 6383101Sstever@eecs.umich.edu * f_spare or f_reserved on non-Linux for now. 6397673Snate@binkert.org */ 6407673Snate@binkert.org memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 6417673Snate@binkert.org#endif 6427673Snate@binkert.org 6437673Snate@binkert.org tgt.copyOut(mem); 6447673Snate@binkert.org} 6457673Snate@binkert.org 6467673Snate@binkert.org/// Target ioctl() handler. For the most part, programs call ioctl() 6477673Snate@binkert.org/// only to find out if their stdout is a tty, to determine whether to 6483101Sstever@eecs.umich.edu/// do line or block buffering. We always claim that output fds are 6493101Sstever@eecs.umich.edu/// not TTYs to provide repeatable results. 6503101Sstever@eecs.umich.edutemplate <class OS> 6513101Sstever@eecs.umich.eduSyscallReturn 6523101Sstever@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 6533101Sstever@eecs.umich.edu{ 6543101Sstever@eecs.umich.edu int index = 0; 6553101Sstever@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 6563101Sstever@eecs.umich.edu unsigned req = p->getSyscallArg(tc, index); 6573101Sstever@eecs.umich.edu 6583101Sstever@eecs.umich.edu DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 6593101Sstever@eecs.umich.edu 6603101Sstever@eecs.umich.edu if (OS::isTtyReq(req)) 6617743Sgblack@eecs.umich.edu return -ENOTTY; 6623101Sstever@eecs.umich.edu 6633101Sstever@eecs.umich.edu auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 6643101Sstever@eecs.umich.edu if (!dfdp) 6653101Sstever@eecs.umich.edu return -EBADF; 6663101Sstever@eecs.umich.edu 6673101Sstever@eecs.umich.edu /** 6684380Sbinkertn@umich.edu * If the driver is valid, issue the ioctl through it. Otherwise, 6693101Sstever@eecs.umich.edu * there's an implicit assumption that the device is a TTY type and we 6703101Sstever@eecs.umich.edu * return that we do not have a valid TTY. 6714762Snate@binkert.org */ 6727677Snate@binkert.org EmulatedDriver *emul_driver = dfdp->getDriver(); 6734762Snate@binkert.org if (emul_driver) 6744762Snate@binkert.org return emul_driver->ioctl(p, tc, req); 6754380Sbinkertn@umich.edu 6764380Sbinkertn@umich.edu /** 6773101Sstever@eecs.umich.edu * For lack of a better return code, return ENOTTY. Ideally, we should 6787777Sgblack@eecs.umich.edu * return something better here, but at least we issue the warning. 6797777Sgblack@eecs.umich.edu */ 6807777Sgblack@eecs.umich.edu warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 6817777Sgblack@eecs.umich.edu tgt_fd, req, tc->pcState()); 6827777Sgblack@eecs.umich.edu return -ENOTTY; 6837777Sgblack@eecs.umich.edu} 6847777Sgblack@eecs.umich.edu 6857777Sgblack@eecs.umich.edutemplate <class OS> 6867777Sgblack@eecs.umich.eduSyscallReturn 6877777Sgblack@eecs.umich.eduopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 6887777Sgblack@eecs.umich.edu bool isopenat) 6897777Sgblack@eecs.umich.edu{ 6907777Sgblack@eecs.umich.edu int index = 0; 6917777Sgblack@eecs.umich.edu int tgt_dirfd = -1; 6927777Sgblack@eecs.umich.edu 6937777Sgblack@eecs.umich.edu /** 6947777Sgblack@eecs.umich.edu * If using the openat variant, read in the target directory file 6957777Sgblack@eecs.umich.edu * descriptor from the simulated process. 6967777Sgblack@eecs.umich.edu */ 6977777Sgblack@eecs.umich.edu if (isopenat) 6987777Sgblack@eecs.umich.edu tgt_dirfd = p->getSyscallArg(tc, index); 6997777Sgblack@eecs.umich.edu 7007777Sgblack@eecs.umich.edu /** 7017777Sgblack@eecs.umich.edu * Retrieve the simulated process' memory proxy and then read in the path 7027777Sgblack@eecs.umich.edu * string from that memory space into the host's working memory space. 7037777Sgblack@eecs.umich.edu */ 7047777Sgblack@eecs.umich.edu std::string path; 7057777Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 7067777Sgblack@eecs.umich.edu return -EFAULT; 7077777Sgblack@eecs.umich.edu 7087777Sgblack@eecs.umich.edu#ifdef __CYGWIN32__ 7097777Sgblack@eecs.umich.edu int host_flags = O_BINARY; 7107777Sgblack@eecs.umich.edu#else 7117777Sgblack@eecs.umich.edu int host_flags = 0; 7127777Sgblack@eecs.umich.edu#endif 7137777Sgblack@eecs.umich.edu /** 7147777Sgblack@eecs.umich.edu * Translate target flags into host flags. Flags exist which are not 7157777Sgblack@eecs.umich.edu * ported between architectures which can cause check failures. 7167777Sgblack@eecs.umich.edu */ 7177777Sgblack@eecs.umich.edu int tgt_flags = p->getSyscallArg(tc, index); 7187777Sgblack@eecs.umich.edu for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 7197777Sgblack@eecs.umich.edu if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 7207777Sgblack@eecs.umich.edu tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 7217777Sgblack@eecs.umich.edu host_flags |= OS::openFlagTable[i].hostFlag; 7227777Sgblack@eecs.umich.edu } 7237777Sgblack@eecs.umich.edu } 7247777Sgblack@eecs.umich.edu if (tgt_flags) { 7257777Sgblack@eecs.umich.edu warn("open%s: cannot decode flags 0x%x", 7267777Sgblack@eecs.umich.edu isopenat ? "at" : "", tgt_flags); 7277777Sgblack@eecs.umich.edu } 7287777Sgblack@eecs.umich.edu#ifdef __CYGWIN32__ 7297777Sgblack@eecs.umich.edu host_flags |= O_BINARY; 7307777Sgblack@eecs.umich.edu#endif 7317777Sgblack@eecs.umich.edu 7327777Sgblack@eecs.umich.edu int mode = p->getSyscallArg(tc, index); 7337777Sgblack@eecs.umich.edu 7347777Sgblack@eecs.umich.edu /** 7357777Sgblack@eecs.umich.edu * If the simulated process called open or openat with AT_FDCWD specified, 7367777Sgblack@eecs.umich.edu * take the current working directory value which was passed into the 7377777Sgblack@eecs.umich.edu * process class as a Python parameter and append the current path to 7387777Sgblack@eecs.umich.edu * create a full path. 7397777Sgblack@eecs.umich.edu * Otherwise, openat with a valid target directory file descriptor has 7407777Sgblack@eecs.umich.edu * been called. If the path option, which was passed in as a parameter, 7417777Sgblack@eecs.umich.edu * is not absolute, retrieve the directory file descriptor's path and 7427777Sgblack@eecs.umich.edu * prepend it to the path passed in as a parameter. 7437777Sgblack@eecs.umich.edu * In every case, we should have a full path (which is relevant to the 7447777Sgblack@eecs.umich.edu * host) to work with after this block has been passed. 7457777Sgblack@eecs.umich.edu */ 7467777Sgblack@eecs.umich.edu if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 7477777Sgblack@eecs.umich.edu path = p->fullPath(path); 7487777Sgblack@eecs.umich.edu } else if (!startswith(path, "/")) { 7497777Sgblack@eecs.umich.edu std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 7507777Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 7517777Sgblack@eecs.umich.edu if (!ffdp) 7527777Sgblack@eecs.umich.edu return -EBADF; 7537777Sgblack@eecs.umich.edu path.insert(0, ffdp->getFileName() + "/"); 7547777Sgblack@eecs.umich.edu } 7557777Sgblack@eecs.umich.edu 7567777Sgblack@eecs.umich.edu /** 7577777Sgblack@eecs.umich.edu * Since this is an emulated environment, we create pseudo file 7587777Sgblack@eecs.umich.edu * descriptors for device requests that have been registered with 7597777Sgblack@eecs.umich.edu * the process class through Python; this allows us to create a file 7607777Sgblack@eecs.umich.edu * descriptor for subsequent ioctl or mmap calls. 7617777Sgblack@eecs.umich.edu */ 7627777Sgblack@eecs.umich.edu if (startswith(path, "/dev/")) { 7637777Sgblack@eecs.umich.edu std::string filename = path.substr(strlen("/dev/")); 7647777Sgblack@eecs.umich.edu EmulatedDriver *drv = p->findDriver(filename); 7657777Sgblack@eecs.umich.edu if (drv) { 7667777Sgblack@eecs.umich.edu DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 7677777Sgblack@eecs.umich.edu "driver open with path[%s]\n", 7687777Sgblack@eecs.umich.edu isopenat ? "at" : "", path.c_str()); 7697777Sgblack@eecs.umich.edu return drv->open(p, tc, mode, host_flags); 7707777Sgblack@eecs.umich.edu } 7717777Sgblack@eecs.umich.edu /** 7727777Sgblack@eecs.umich.edu * Fall through here for pass through to host devices, such 7737777Sgblack@eecs.umich.edu * as /dev/zero 7747777Sgblack@eecs.umich.edu */ 7757777Sgblack@eecs.umich.edu } 7767777Sgblack@eecs.umich.edu 7777777Sgblack@eecs.umich.edu /** 7787777Sgblack@eecs.umich.edu * Some special paths and files cannot be called on the host and need 7797777Sgblack@eecs.umich.edu * to be handled as special cases inside the simulator. 7807777Sgblack@eecs.umich.edu * If the full path that was created above does not match any of the 7817777Sgblack@eecs.umich.edu * special cases, pass it through to the open call on the host to let 7827777Sgblack@eecs.umich.edu * the host open the file on our behalf. 7837777Sgblack@eecs.umich.edu * If the host cannot open the file, return the host's error code back 7847777Sgblack@eecs.umich.edu * through the system call to the simulated process. 7857777Sgblack@eecs.umich.edu */ 7867777Sgblack@eecs.umich.edu int sim_fd = -1; 7877777Sgblack@eecs.umich.edu std::vector<std::string> special_paths = 7887777Sgblack@eecs.umich.edu { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 7897777Sgblack@eecs.umich.edu for (auto entry : special_paths) { 7907777Sgblack@eecs.umich.edu if (startswith(path, entry)) 7917777Sgblack@eecs.umich.edu sim_fd = OS::openSpecialFile(path, p, tc); 7927777Sgblack@eecs.umich.edu } 7937777Sgblack@eecs.umich.edu if (sim_fd == -1) { 7947777Sgblack@eecs.umich.edu sim_fd = open(path.c_str(), host_flags, mode); 7957777Sgblack@eecs.umich.edu } 7967777Sgblack@eecs.umich.edu if (sim_fd == -1) { 7977777Sgblack@eecs.umich.edu int local = -errno; 7987777Sgblack@eecs.umich.edu DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 7997777Sgblack@eecs.umich.edu isopenat ? "at" : "", path.c_str()); 8007777Sgblack@eecs.umich.edu return local; 8017777Sgblack@eecs.umich.edu } 8027777Sgblack@eecs.umich.edu 8037777Sgblack@eecs.umich.edu /** 8047777Sgblack@eecs.umich.edu * The file was opened successfully and needs to be recorded in the 8057777Sgblack@eecs.umich.edu * process' file descriptor array so that it can be retrieved later. 8067777Sgblack@eecs.umich.edu * The target file descriptor that is chosen will be the lowest unused 8077777Sgblack@eecs.umich.edu * file descriptor. 8087777Sgblack@eecs.umich.edu * Return the indirect target file descriptor back to the simulated 8097777Sgblack@eecs.umich.edu * process to act as a handle for the opened file. 8107777Sgblack@eecs.umich.edu */ 8117777Sgblack@eecs.umich.edu auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 8127777Sgblack@eecs.umich.edu int tgt_fd = p->fds->allocFD(ffdp); 8137777Sgblack@eecs.umich.edu DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 8147777Sgblack@eecs.umich.edu isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 8157777Sgblack@eecs.umich.edu return tgt_fd; 8167777Sgblack@eecs.umich.edu} 8177777Sgblack@eecs.umich.edu 8187777Sgblack@eecs.umich.edu/// Target open() handler. 8197777Sgblack@eecs.umich.edutemplate <class OS> 8207777Sgblack@eecs.umich.eduSyscallReturn 8217777Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, Process *process, 8227777Sgblack@eecs.umich.edu ThreadContext *tc) 8237777Sgblack@eecs.umich.edu{ 8247777Sgblack@eecs.umich.edu return openImpl<OS>(desc, callnum, process, tc, false); 8257777Sgblack@eecs.umich.edu} 8267777Sgblack@eecs.umich.edu 8277777Sgblack@eecs.umich.edu/// Target openat() handler. 8287777Sgblack@eecs.umich.edutemplate <class OS> 8297777Sgblack@eecs.umich.eduSyscallReturn 8307777Sgblack@eecs.umich.eduopenatFunc(SyscallDesc *desc, int callnum, Process *process, 8317777Sgblack@eecs.umich.edu ThreadContext *tc) 8327777Sgblack@eecs.umich.edu{ 8337777Sgblack@eecs.umich.edu return openImpl<OS>(desc, callnum, process, tc, true); 8347777Sgblack@eecs.umich.edu} 8353932Sbinkertn@umich.edu 8363932Sbinkertn@umich.edu/// Target unlinkat() handler. 8373932Sbinkertn@umich.edutemplate <class OS> 8383932Sbinkertn@umich.eduSyscallReturn 8393932Sbinkertn@umich.eduunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 8403932Sbinkertn@umich.edu ThreadContext *tc) 8413932Sbinkertn@umich.edu{ 8423932Sbinkertn@umich.edu int index = 0; 8433932Sbinkertn@umich.edu int dirfd = process->getSyscallArg(tc, index); 8443932Sbinkertn@umich.edu if (dirfd != OS::TGT_AT_FDCWD) 8453932Sbinkertn@umich.edu warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 8463932Sbinkertn@umich.edu 8473932Sbinkertn@umich.edu return unlinkHelper(desc, callnum, process, tc, 1); 8483885Sbinkertn@umich.edu} 8493932Sbinkertn@umich.edu 8503932Sbinkertn@umich.edu/// Target facessat() handler 8513885Sbinkertn@umich.edutemplate <class OS> 8523932Sbinkertn@umich.eduSyscallReturn 8533932Sbinkertn@umich.edufaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 8543932Sbinkertn@umich.edu ThreadContext *tc) 8553932Sbinkertn@umich.edu{ 8563932Sbinkertn@umich.edu int index = 0; 8573932Sbinkertn@umich.edu int dirfd = process->getSyscallArg(tc, index); 8583932Sbinkertn@umich.edu if (dirfd != OS::TGT_AT_FDCWD) 8593932Sbinkertn@umich.edu warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 8603932Sbinkertn@umich.edu return accessFunc(desc, callnum, process, tc, 1); 8613932Sbinkertn@umich.edu} 8623932Sbinkertn@umich.edu 8633932Sbinkertn@umich.edu/// Target readlinkat() handler 8643932Sbinkertn@umich.edutemplate <class OS> 8653932Sbinkertn@umich.eduSyscallReturn 8663932Sbinkertn@umich.edureadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 8673932Sbinkertn@umich.edu ThreadContext *tc) 8683932Sbinkertn@umich.edu{ 8693932Sbinkertn@umich.edu int index = 0; 8703885Sbinkertn@umich.edu int dirfd = process->getSyscallArg(tc, index); 8713885Sbinkertn@umich.edu if (dirfd != OS::TGT_AT_FDCWD) 8723885Sbinkertn@umich.edu warn("openat: first argument not AT_FDCWD; unlikely to work"); 8733885Sbinkertn@umich.edu return readlinkFunc(desc, callnum, process, tc, 1); 8744762Snate@binkert.org} 8757673Snate@binkert.org 8767673Snate@binkert.org/// Target renameat() handler. 8777673Snate@binkert.orgtemplate <class OS> 8787673Snate@binkert.orgSyscallReturn 8797673Snate@binkert.orgrenameatFunc(SyscallDesc *desc, int callnum, Process *process, 8807673Snate@binkert.org ThreadContext *tc) 8817673Snate@binkert.org{ 8827673Snate@binkert.org int index = 0; 8837673Snate@binkert.org 8843885Sbinkertn@umich.edu int olddirfd = process->getSyscallArg(tc, index); 8853932Sbinkertn@umich.edu if (olddirfd != OS::TGT_AT_FDCWD) 8863885Sbinkertn@umich.edu warn("renameat: first argument not AT_FDCWD; unlikely to work"); 8874762Snate@binkert.org 8887677Snate@binkert.org std::string old_name; 8894762Snate@binkert.org 8904762Snate@binkert.org if (!tc->getMemProxy().tryReadString(old_name, 8914762Snate@binkert.org process->getSyscallArg(tc, index))) 8924762Snate@binkert.org return -EFAULT; 8934762Snate@binkert.org 8944762Snate@binkert.org int newdirfd = process->getSyscallArg(tc, index); 8954762Snate@binkert.org if (newdirfd != OS::TGT_AT_FDCWD) 8964762Snate@binkert.org warn("renameat: third argument not AT_FDCWD; unlikely to work"); 8974762Snate@binkert.org 8984762Snate@binkert.org std::string new_name; 8994762Snate@binkert.org 9004762Snate@binkert.org if (!tc->getMemProxy().tryReadString(new_name, 9014762Snate@binkert.org process->getSyscallArg(tc, index))) 9024762Snate@binkert.org return -EFAULT; 9034762Snate@binkert.org 9044762Snate@binkert.org // Adjust path for current working directory 9054762Snate@binkert.org old_name = process->fullPath(old_name); 9064762Snate@binkert.org new_name = process->fullPath(new_name); 9074762Snate@binkert.org 9084762Snate@binkert.org int result = rename(old_name.c_str(), new_name.c_str()); 9094762Snate@binkert.org return (result == -1) ? -errno : result; 9104762Snate@binkert.org} 9114762Snate@binkert.org 9124762Snate@binkert.org/// Target sysinfo() handler. 9133885Sbinkertn@umich.edutemplate <class OS> 9144762Snate@binkert.orgSyscallReturn 9153885Sbinkertn@umich.edusysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 9163885Sbinkertn@umich.edu ThreadContext *tc) 9173932Sbinkertn@umich.edu{ 9183885Sbinkertn@umich.edu 9193101Sstever@eecs.umich.edu int index = 0; 9203101Sstever@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 9213101Sstever@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 9223101Sstever@eecs.umich.edu 9233101Sstever@eecs.umich.edu sysinfo->uptime = seconds_since_epoch; 9243101Sstever@eecs.umich.edu sysinfo->totalram = process->system->memSize(); 9253101Sstever@eecs.umich.edu sysinfo->mem_unit = 1; 9263101Sstever@eecs.umich.edu 9273101Sstever@eecs.umich.edu sysinfo.copyOut(tc->getMemProxy()); 9283101Sstever@eecs.umich.edu 9293101Sstever@eecs.umich.edu return 0; 9303101Sstever@eecs.umich.edu} 9313101Sstever@eecs.umich.edu 9323101Sstever@eecs.umich.edu/// Target chmod() handler. 9334762Snate@binkert.orgtemplate <class OS> 9343101Sstever@eecs.umich.eduSyscallReturn 9355033Smilesck@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, Process *process, 9364762Snate@binkert.org ThreadContext *tc) 9374762Snate@binkert.org{ 9384762Snate@binkert.org std::string path; 9394762Snate@binkert.org 9404762Snate@binkert.org int index = 0; 9414762Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, 9424762Snate@binkert.org process->getSyscallArg(tc, index))) { 9433101Sstever@eecs.umich.edu return -EFAULT; 9443101Sstever@eecs.umich.edu } 9453101Sstever@eecs.umich.edu 9463101Sstever@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 9473101Sstever@eecs.umich.edu mode_t hostMode = 0; 9483101Sstever@eecs.umich.edu 9493101Sstever@eecs.umich.edu // XXX translate mode flags via OS::something??? 9503101Sstever@eecs.umich.edu hostMode = mode; 9513101Sstever@eecs.umich.edu 9523101Sstever@eecs.umich.edu // Adjust path for current working directory 9533101Sstever@eecs.umich.edu path = process->fullPath(path); 9543101Sstever@eecs.umich.edu 9553101Sstever@eecs.umich.edu // do the chmod 9563101Sstever@eecs.umich.edu int result = chmod(path.c_str(), hostMode); 9573101Sstever@eecs.umich.edu if (result < 0) 9583101Sstever@eecs.umich.edu return -errno; 9593101Sstever@eecs.umich.edu 9603101Sstever@eecs.umich.edu return 0; 9613101Sstever@eecs.umich.edu} 9623101Sstever@eecs.umich.edu 9634762Snate@binkert.orgtemplate <class OS> 9643101Sstever@eecs.umich.eduSyscallReturn 9653101Sstever@eecs.umich.edupollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 9663101Sstever@eecs.umich.edu{ 9673101Sstever@eecs.umich.edu int index = 0; 9683101Sstever@eecs.umich.edu Addr fdsPtr = p->getSyscallArg(tc, index); 9693101Sstever@eecs.umich.edu int nfds = p->getSyscallArg(tc, index); 9707673Snate@binkert.org int tmout = p->getSyscallArg(tc, index); 9716654Snate@binkert.org 9727673Snate@binkert.org BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 9737673Snate@binkert.org fdsBuf.copyIn(tc->getMemProxy()); 9747673Snate@binkert.org 9757673Snate@binkert.org /** 9767673Snate@binkert.org * Record the target file descriptors in a local variable. We need to 9777673Snate@binkert.org * replace them with host file descriptors but we need a temporary copy 9787673Snate@binkert.org * for later. Afterwards, replace each target file descriptor in the 9797673Snate@binkert.org * poll_fd array with its host_fd. 9804762Snate@binkert.org */ 9817673Snate@binkert.org int temp_tgt_fds[nfds]; 9827673Snate@binkert.org for (index = 0; index < nfds; index++) { 9837673Snate@binkert.org temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 9847673Snate@binkert.org auto tgt_fd = temp_tgt_fds[index]; 9857673Snate@binkert.org auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 9867673Snate@binkert.org if (!hbfdp) 9877673Snate@binkert.org return -EBADF; 9884762Snate@binkert.org auto host_fd = hbfdp->getSimFD(); 9897673Snate@binkert.org ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 9907673Snate@binkert.org } 9917673Snate@binkert.org 9927673Snate@binkert.org /** 9936654Snate@binkert.org * We cannot allow an infinite poll to occur or it will inevitably cause 9947673Snate@binkert.org * a deadlock in the gem5 simulator with clone. We must pass in tmout with 9957677Snate@binkert.org * a non-negative value, however it also makes no sense to poll on the 9967673Snate@binkert.org * underlying host for any other time than tmout a zero timeout. 9977673Snate@binkert.org */ 9987673Snate@binkert.org int status; 9997673Snate@binkert.org if (tmout < 0) { 10007673Snate@binkert.org status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 10014762Snate@binkert.org if (status == 0) { 10027673Snate@binkert.org /** 10037673Snate@binkert.org * If blocking indefinitely, check the signal list to see if a 10047673Snate@binkert.org * signal would break the poll out of the retry cycle and try 10057673Snate@binkert.org * to return the signal interrupt instead. 10067673Snate@binkert.org */ 10077673Snate@binkert.org System *sysh = tc->getSystemPtr(); 10083101Sstever@eecs.umich.edu std::list<BasicSignal>::iterator it; 10093101Sstever@eecs.umich.edu for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 10103101Sstever@eecs.umich.edu if (it->receiver == p) 10113101Sstever@eecs.umich.edu return -EINTR; 10123101Sstever@eecs.umich.edu return SyscallReturn::retry(); 10133101Sstever@eecs.umich.edu } 10143101Sstever@eecs.umich.edu } else 10153101Sstever@eecs.umich.edu status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 10163101Sstever@eecs.umich.edu 10173101Sstever@eecs.umich.edu if (status == -1) 10183101Sstever@eecs.umich.edu return -errno; 10193101Sstever@eecs.umich.edu 10207675Snate@binkert.org /** 10217675Snate@binkert.org * Replace each host_fd in the returned poll_fd array with its original 10227675Snate@binkert.org * target file descriptor. 10237675Snate@binkert.org */ 10247675Snate@binkert.org for (index = 0; index < nfds; index++) { 10257675Snate@binkert.org auto tgt_fd = temp_tgt_fds[index]; 10267677Snate@binkert.org ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 10277675Snate@binkert.org } 10284762Snate@binkert.org 10294762Snate@binkert.org /** 10304762Snate@binkert.org * Copy out the pollfd struct because the host may have updated fields 10313101Sstever@eecs.umich.edu * in the structure. 10323101Sstever@eecs.umich.edu */ 10333101Sstever@eecs.umich.edu fdsBuf.copyOut(tc->getMemProxy()); 10343101Sstever@eecs.umich.edu 10353101Sstever@eecs.umich.edu return status; 10363101Sstever@eecs.umich.edu} 10374167Sbinkertn@umich.edu 10383101Sstever@eecs.umich.edu/// Target fchmod() handler. 10397673Snate@binkert.orgtemplate <class OS> 10407673Snate@binkert.orgSyscallReturn 10417673Snate@binkert.orgfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 10427673Snate@binkert.org{ 10437673Snate@binkert.org int index = 0; 10447673Snate@binkert.org int tgt_fd = p->getSyscallArg(tc, index); 10457673Snate@binkert.org uint32_t mode = p->getSyscallArg(tc, index); 10467673Snate@binkert.org 10477673Snate@binkert.org auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 10484167Sbinkertn@umich.edu if (!ffdp) 10494762Snate@binkert.org return -EBADF; 10504762Snate@binkert.org int sim_fd = ffdp->getSimFD(); 10514762Snate@binkert.org 10524167Sbinkertn@umich.edu mode_t hostMode = mode; 10533101Sstever@eecs.umich.edu 10544167Sbinkertn@umich.edu int result = fchmod(sim_fd, hostMode); 10554167Sbinkertn@umich.edu 10564167Sbinkertn@umich.edu return (result < 0) ? -errno : 0; 10574167Sbinkertn@umich.edu} 10584167Sbinkertn@umich.edu 10594167Sbinkertn@umich.edu/// Target mremap() handler. 10604167Sbinkertn@umich.edutemplate <class OS> 10614167Sbinkertn@umich.eduSyscallReturn 10624167Sbinkertn@umich.edumremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 10634167Sbinkertn@umich.edu{ 10644167Sbinkertn@umich.edu int index = 0; 10654167Sbinkertn@umich.edu Addr start = process->getSyscallArg(tc, index); 10663101Sstever@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 10673101Sstever@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 10683101Sstever@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 10693101Sstever@eecs.umich.edu uint64_t provided_address = 0; 10703101Sstever@eecs.umich.edu bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 10713101Sstever@eecs.umich.edu 10723101Sstever@eecs.umich.edu if (use_provided_address) 10733101Sstever@eecs.umich.edu provided_address = process->getSyscallArg(tc, index); 10744762Snate@binkert.org 10754762Snate@binkert.org if ((start % TheISA::PageBytes != 0) || 10764762Snate@binkert.org (provided_address % TheISA::PageBytes != 0)) { 10774762Snate@binkert.org warn("mremap failing: arguments not page aligned"); 10784762Snate@binkert.org return -EINVAL; 10794762Snate@binkert.org } 10804762Snate@binkert.org 10813101Sstever@eecs.umich.edu new_length = roundUp(new_length, TheISA::PageBytes); 10823101Sstever@eecs.umich.edu 10834762Snate@binkert.org if (new_length > old_length) { 10843101Sstever@eecs.umich.edu std::shared_ptr<MemState> mem_state = process->memState; 10854167Sbinkertn@umich.edu Addr mmap_end = mem_state->getMmapEnd(); 10863101Sstever@eecs.umich.edu 10874167Sbinkertn@umich.edu if ((start + old_length) == mmap_end && 10884167Sbinkertn@umich.edu (!use_provided_address || provided_address == start)) { 10894167Sbinkertn@umich.edu // This case cannot occur when growing downward, as 10904167Sbinkertn@umich.edu // start is greater than or equal to mmap_end. 10914167Sbinkertn@umich.edu uint64_t diff = new_length - old_length; 10924167Sbinkertn@umich.edu process->allocateMem(mmap_end, diff); 10934167Sbinkertn@umich.edu mem_state->setMmapEnd(mmap_end + diff); 10944167Sbinkertn@umich.edu return start; 10954167Sbinkertn@umich.edu } else { 10964167Sbinkertn@umich.edu if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 10974167Sbinkertn@umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 10984167Sbinkertn@umich.edu return -ENOMEM; 10993101Sstever@eecs.umich.edu } else { 11003101Sstever@eecs.umich.edu uint64_t new_start = provided_address; 11013101Sstever@eecs.umich.edu if (!use_provided_address) { 11023101Sstever@eecs.umich.edu new_start = process->mmapGrowsDown() ? 11033101Sstever@eecs.umich.edu mmap_end - new_length : mmap_end; 11043101Sstever@eecs.umich.edu mmap_end = process->mmapGrowsDown() ? 11053101Sstever@eecs.umich.edu new_start : mmap_end + new_length; 11063101Sstever@eecs.umich.edu mem_state->setMmapEnd(mmap_end); 11074167Sbinkertn@umich.edu } 11084762Snate@binkert.org 11094762Snate@binkert.org process->pTable->remap(start, old_length, new_start); 11104762Snate@binkert.org warn("mremapping to new vaddr %08p-%08p, adding %d\n", 11114762Snate@binkert.org new_start, new_start + new_length, 11124762Snate@binkert.org new_length - old_length); 11134762Snate@binkert.org // add on the remaining unallocated pages 11144762Snate@binkert.org process->allocateMem(new_start + old_length, 11153101Sstever@eecs.umich.edu new_length - old_length, 11164762Snate@binkert.org use_provided_address /* clobber */); 11173101Sstever@eecs.umich.edu if (use_provided_address && 11183101Sstever@eecs.umich.edu ((new_start + new_length > mem_state->getMmapEnd() && 11193101Sstever@eecs.umich.edu !process->mmapGrowsDown()) || 11203101Sstever@eecs.umich.edu (new_start < mem_state->getMmapEnd() && 11213101Sstever@eecs.umich.edu process->mmapGrowsDown()))) { 11223101Sstever@eecs.umich.edu // something fishy going on here, at least notify the user 11237673Snate@binkert.org // @todo: increase mmap_end? 11247673Snate@binkert.org warn("mmap region limit exceeded with MREMAP_FIXED\n"); 11257673Snate@binkert.org } 11267673Snate@binkert.org warn("returning %08p as start\n", new_start); 11277673Snate@binkert.org return new_start; 11287673Snate@binkert.org } 11297673Snate@binkert.org } 11307673Snate@binkert.org } else { 11317673Snate@binkert.org if (use_provided_address && provided_address != start) 11327673Snate@binkert.org process->pTable->remap(start, new_length, provided_address); 11333101Sstever@eecs.umich.edu process->pTable->unmap(start + new_length, old_length - new_length); 11344167Sbinkertn@umich.edu return use_provided_address ? provided_address : start; 11354167Sbinkertn@umich.edu } 11364167Sbinkertn@umich.edu} 11374167Sbinkertn@umich.edu 11384167Sbinkertn@umich.edu/// Target stat() handler. 11394167Sbinkertn@umich.edutemplate <class OS> 11404167Sbinkertn@umich.eduSyscallReturn 11414167Sbinkertn@umich.edustatFunc(SyscallDesc *desc, int callnum, Process *process, 11424167Sbinkertn@umich.edu ThreadContext *tc) 11434167Sbinkertn@umich.edu{ 11444167Sbinkertn@umich.edu std::string path; 11454167Sbinkertn@umich.edu 11463101Sstever@eecs.umich.edu int index = 0; 11473101Sstever@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 11483101Sstever@eecs.umich.edu process->getSyscallArg(tc, index))) { 11493101Sstever@eecs.umich.edu return -EFAULT; 11503101Sstever@eecs.umich.edu } 11513101Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11523101Sstever@eecs.umich.edu 11533101Sstever@eecs.umich.edu // Adjust path for current working directory 11544762Snate@binkert.org path = process->fullPath(path); 11554762Snate@binkert.org 11564762Snate@binkert.org struct stat hostBuf; 11573101Sstever@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 11583101Sstever@eecs.umich.edu 11593101Sstever@eecs.umich.edu if (result < 0) 11603101Sstever@eecs.umich.edu return -errno; 11613101Sstever@eecs.umich.edu 11623101Sstever@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11634167Sbinkertn@umich.edu 11644167Sbinkertn@umich.edu return 0; 11653101Sstever@eecs.umich.edu} 11663101Sstever@eecs.umich.edu 11673101Sstever@eecs.umich.edu 11683101Sstever@eecs.umich.edu/// Target stat64() handler. 11693101Sstever@eecs.umich.edutemplate <class OS> 11704762Snate@binkert.orgSyscallReturn 11714167Sbinkertn@umich.edustat64Func(SyscallDesc *desc, int callnum, Process *process, 11724167Sbinkertn@umich.edu ThreadContext *tc) 11734167Sbinkertn@umich.edu{ 11744762Snate@binkert.org std::string path; 11754762Snate@binkert.org 11764762Snate@binkert.org int index = 0; 11774762Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, 11784762Snate@binkert.org process->getSyscallArg(tc, index))) 11793101Sstever@eecs.umich.edu return -EFAULT; 11803101Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11813101Sstever@eecs.umich.edu 11825469Snate@binkert.org // Adjust path for current working directory 11837743Sgblack@eecs.umich.edu path = process->fullPath(path); 11843102Sstever@eecs.umich.edu 11853101Sstever@eecs.umich.edu#if NO_STAT64 11863101Sstever@eecs.umich.edu struct stat hostBuf; 11873101Sstever@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 11883101Sstever@eecs.umich.edu#else 11893101Sstever@eecs.umich.edu struct stat64 hostBuf; 11904762Snate@binkert.org int result = stat64(path.c_str(), &hostBuf); 11914167Sbinkertn@umich.edu#endif 11925468Snate@binkert.org 11935468Snate@binkert.org if (result < 0) 11945468Snate@binkert.org return -errno; 11954167Sbinkertn@umich.edu 11964762Snate@binkert.org copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11974762Snate@binkert.org 11984762Snate@binkert.org return 0; 11994762Snate@binkert.org} 12004762Snate@binkert.org 12013101Sstever@eecs.umich.edu 12023101Sstever@eecs.umich.edu/// Target fstatat64() handler. 12033101Sstever@eecs.umich.edutemplate <class OS> 12043101Sstever@eecs.umich.eduSyscallReturn 12053101Sstever@eecs.umich.edufstatat64Func(SyscallDesc *desc, int callnum, Process *process, 12063102Sstever@eecs.umich.edu ThreadContext *tc) 12073102Sstever@eecs.umich.edu{ 12083102Sstever@eecs.umich.edu int index = 0; 12093102Sstever@eecs.umich.edu int dirfd = process->getSyscallArg(tc, index); 12103102Sstever@eecs.umich.edu if (dirfd != OS::TGT_AT_FDCWD) 12113102Sstever@eecs.umich.edu warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 12123102Sstever@eecs.umich.edu 12133102Sstever@eecs.umich.edu std::string path; 12143102Sstever@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 12153102Sstever@eecs.umich.edu process->getSyscallArg(tc, index))) 12163102Sstever@eecs.umich.edu return -EFAULT; 12173102Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 12183102Sstever@eecs.umich.edu 12193102Sstever@eecs.umich.edu // Adjust path for current working directory 12203102Sstever@eecs.umich.edu path = process->fullPath(path); 12213102Sstever@eecs.umich.edu 12223102Sstever@eecs.umich.edu#if NO_STAT64 12233102Sstever@eecs.umich.edu struct stat hostBuf; 12243102Sstever@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 12253102Sstever@eecs.umich.edu#else 12263102Sstever@eecs.umich.edu struct stat64 hostBuf; 12274762Snate@binkert.org int result = stat64(path.c_str(), &hostBuf); 12283102Sstever@eecs.umich.edu#endif 12293102Sstever@eecs.umich.edu 12303102Sstever@eecs.umich.edu if (result < 0) 12314762Snate@binkert.org return -errno; 12324762Snate@binkert.org 12334762Snate@binkert.org copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12343102Sstever@eecs.umich.edu 12353102Sstever@eecs.umich.edu return 0; 12363102Sstever@eecs.umich.edu} 12373102Sstever@eecs.umich.edu 12383102Sstever@eecs.umich.edu 12393102Sstever@eecs.umich.edu/// Target fstat64() handler. 12403101Sstever@eecs.umich.edutemplate <class OS> 12413101Sstever@eecs.umich.eduSyscallReturn 12423101Sstever@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 12433101Sstever@eecs.umich.edu{ 12443101Sstever@eecs.umich.edu int index = 0; 12453101Sstever@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 12463101Sstever@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 12473101Sstever@eecs.umich.edu 12483101Sstever@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 12493101Sstever@eecs.umich.edu if (!ffdp) 12503101Sstever@eecs.umich.edu return -EBADF; 12513101Sstever@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 12523101Sstever@eecs.umich.edu 12533101Sstever@eecs.umich.edu#if NO_STAT64 12543101Sstever@eecs.umich.edu struct stat hostBuf; 12553101Sstever@eecs.umich.edu int result = fstat(sim_fd, &hostBuf); 12563101Sstever@eecs.umich.edu#else 12573105Sstever@eecs.umich.edu struct stat64 hostBuf; 12583105Sstever@eecs.umich.edu int result = fstat64(sim_fd, &hostBuf); 12593101Sstever@eecs.umich.edu#endif 12603101Sstever@eecs.umich.edu 12613101Sstever@eecs.umich.edu if (result < 0) 12623101Sstever@eecs.umich.edu return -errno; 12633105Sstever@eecs.umich.edu 12643101Sstever@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12653103Sstever@eecs.umich.edu 12663105Sstever@eecs.umich.edu return 0; 12673103Sstever@eecs.umich.edu} 12683105Sstever@eecs.umich.edu 12693105Sstever@eecs.umich.edu 12703105Sstever@eecs.umich.edu/// Target lstat() handler. 12713105Sstever@eecs.umich.edutemplate <class OS> 12723105Sstever@eecs.umich.eduSyscallReturn 12733105Sstever@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, Process *process, 12743105Sstever@eecs.umich.edu ThreadContext *tc) 12753105Sstever@eecs.umich.edu{ 12763105Sstever@eecs.umich.edu std::string path; 12773105Sstever@eecs.umich.edu 12783105Sstever@eecs.umich.edu int index = 0; 12793105Sstever@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 12803105Sstever@eecs.umich.edu process->getSyscallArg(tc, index))) { 12813109Sstever@eecs.umich.edu return -EFAULT; 12823105Sstever@eecs.umich.edu } 12833105Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 12843105Sstever@eecs.umich.edu 12853105Sstever@eecs.umich.edu // Adjust path for current working directory 12863105Sstever@eecs.umich.edu path = process->fullPath(path); 12873105Sstever@eecs.umich.edu 12883105Sstever@eecs.umich.edu struct stat hostBuf; 12893105Sstever@eecs.umich.edu int result = lstat(path.c_str(), &hostBuf); 12905578SSteve.Reinhardt@amd.com 12913101Sstever@eecs.umich.edu if (result < 0) 12923109Sstever@eecs.umich.edu return -errno; 12933109Sstever@eecs.umich.edu 12943109Sstever@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12953109Sstever@eecs.umich.edu 12963109Sstever@eecs.umich.edu return 0; 12973109Sstever@eecs.umich.edu} 12983109Sstever@eecs.umich.edu 12993109Sstever@eecs.umich.edu/// Target lstat64() handler. 13003109Sstever@eecs.umich.edutemplate <class OS> 13013101Sstever@eecs.umich.eduSyscallReturn 13023105Sstever@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, Process *process, 13033105Sstever@eecs.umich.edu ThreadContext *tc) 13043105Sstever@eecs.umich.edu{ 13053101Sstever@eecs.umich.edu std::string path; 13063105Sstever@eecs.umich.edu 13073105Sstever@eecs.umich.edu int index = 0; 13083101Sstever@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 13093105Sstever@eecs.umich.edu process->getSyscallArg(tc, index))) { 13103179Sstever@eecs.umich.edu return -EFAULT; 13113105Sstever@eecs.umich.edu } 13123105Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 13133101Sstever@eecs.umich.edu 13143101Sstever@eecs.umich.edu // Adjust path for current working directory 13153105Sstever@eecs.umich.edu path = process->fullPath(path); 13163105Sstever@eecs.umich.edu 13173105Sstever@eecs.umich.edu#if NO_STAT64 13183105Sstever@eecs.umich.edu struct stat hostBuf; 13193105Sstever@eecs.umich.edu int result = lstat(path.c_str(), &hostBuf); 13203105Sstever@eecs.umich.edu#else 13213105Sstever@eecs.umich.edu struct stat64 hostBuf; 13223105Sstever@eecs.umich.edu int result = lstat64(path.c_str(), &hostBuf); 13233105Sstever@eecs.umich.edu#endif 13243105Sstever@eecs.umich.edu 13253105Sstever@eecs.umich.edu if (result < 0) 13263101Sstever@eecs.umich.edu return -errno; 13273101Sstever@eecs.umich.edu 13287677Snate@binkert.org copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13294762Snate@binkert.org 13303101Sstever@eecs.umich.edu return 0; 13313101Sstever@eecs.umich.edu} 13323101Sstever@eecs.umich.edu 13335578SSteve.Reinhardt@amd.com/// Target fstat() handler. 13345578SSteve.Reinhardt@amd.comtemplate <class OS> 13357526Ssteve.reinhardt@amd.comSyscallReturn 13367526Ssteve.reinhardt@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 13377526Ssteve.reinhardt@amd.com{ 13387526Ssteve.reinhardt@amd.com int index = 0; 13397526Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 13407526Ssteve.reinhardt@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 13417526Ssteve.reinhardt@amd.com 13427526Ssteve.reinhardt@amd.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 13433101Sstever@eecs.umich.edu 13443101Sstever@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 13453101Sstever@eecs.umich.edu if (!ffdp) 13463105Sstever@eecs.umich.edu return -EBADF; 13473105Sstever@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 13483105Sstever@eecs.umich.edu 13493105Sstever@eecs.umich.edu struct stat hostBuf; 13503105Sstever@eecs.umich.edu int result = fstat(sim_fd, &hostBuf); 13513105Sstever@eecs.umich.edu 13523105Sstever@eecs.umich.edu if (result < 0) 13533105Sstever@eecs.umich.edu return -errno; 13543105Sstever@eecs.umich.edu 13553105Sstever@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 13563105Sstever@eecs.umich.edu 13573105Sstever@eecs.umich.edu return 0; 13583105Sstever@eecs.umich.edu} 13593105Sstever@eecs.umich.edu 13603105Sstever@eecs.umich.edu/// Target statfs() handler. 13613105Sstever@eecs.umich.edutemplate <class OS> 13623105Sstever@eecs.umich.eduSyscallReturn 13633105Sstever@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, Process *process, 13643105Sstever@eecs.umich.edu ThreadContext *tc) 13653109Sstever@eecs.umich.edu{ 13663109Sstever@eecs.umich.edu#if NO_STATFS 13673109Sstever@eecs.umich.edu warn("Host OS cannot support calls to statfs. Ignoring syscall"); 13683105Sstever@eecs.umich.edu#else 13693105Sstever@eecs.umich.edu std::string path; 13703105Sstever@eecs.umich.edu 13713105Sstever@eecs.umich.edu int index = 0; 13723105Sstever@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 13733105Sstever@eecs.umich.edu process->getSyscallArg(tc, index))) { 13743105Sstever@eecs.umich.edu return -EFAULT; 13753105Sstever@eecs.umich.edu } 13763105Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 13773105Sstever@eecs.umich.edu 13783105Sstever@eecs.umich.edu // Adjust path for current working directory 13793105Sstever@eecs.umich.edu path = process->fullPath(path); 13803105Sstever@eecs.umich.edu 13813105Sstever@eecs.umich.edu struct statfs hostBuf; 13823105Sstever@eecs.umich.edu int result = statfs(path.c_str(), &hostBuf); 13833105Sstever@eecs.umich.edu 13843105Sstever@eecs.umich.edu if (result < 0) 13853105Sstever@eecs.umich.edu return -errno; 13863105Sstever@eecs.umich.edu 13873105Sstever@eecs.umich.edu copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13883105Sstever@eecs.umich.edu#endif 13893105Sstever@eecs.umich.edu return 0; 13903105Sstever@eecs.umich.edu} 13913109Sstever@eecs.umich.edu 13923109Sstever@eecs.umich.edutemplate <class OS> 13933109Sstever@eecs.umich.eduSyscallReturn 13943109Sstever@eecs.umich.educloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 13953109Sstever@eecs.umich.edu{ 13963109Sstever@eecs.umich.edu int index = 0; 13973109Sstever@eecs.umich.edu 13983109Sstever@eecs.umich.edu RegVal flags = p->getSyscallArg(tc, index); 13993109Sstever@eecs.umich.edu RegVal newStack = p->getSyscallArg(tc, index); 14003109Sstever@eecs.umich.edu Addr ptidPtr = p->getSyscallArg(tc, index); 14013109Sstever@eecs.umich.edu 14023109Sstever@eecs.umich.edu#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 14033109Sstever@eecs.umich.edu /** 14043109Sstever@eecs.umich.edu * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 14053109Sstever@eecs.umich.edu * The flag defines the list of clone() arguments in the following 14063109Sstever@eecs.umich.edu * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 14073109Sstever@eecs.umich.edu */ 14083109Sstever@eecs.umich.edu Addr tlsPtr = p->getSyscallArg(tc, index); 14093109Sstever@eecs.umich.edu Addr ctidPtr = p->getSyscallArg(tc, index); 14103105Sstever@eecs.umich.edu#else 14113105Sstever@eecs.umich.edu Addr ctidPtr = p->getSyscallArg(tc, index); 14123105Sstever@eecs.umich.edu Addr tlsPtr = p->getSyscallArg(tc, index); 14133105Sstever@eecs.umich.edu#endif 14143105Sstever@eecs.umich.edu 14153105Sstever@eecs.umich.edu if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 14163105Sstever@eecs.umich.edu ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 14173101Sstever@eecs.umich.edu ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 14183101Sstever@eecs.umich.edu ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 14193101Sstever@eecs.umich.edu ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 14203101Sstever@eecs.umich.edu ((flags & OS::TGT_CLONE_VM) && !(newStack))) 14213105Sstever@eecs.umich.edu return -EINVAL; 14223105Sstever@eecs.umich.edu 14233105Sstever@eecs.umich.edu ThreadContext *ctc; 14243105Sstever@eecs.umich.edu if (!(ctc = p->findFreeContext())) 14253105Sstever@eecs.umich.edu fatal("clone: no spare thread context in system"); 14263105Sstever@eecs.umich.edu 14273105Sstever@eecs.umich.edu /** 14283105Sstever@eecs.umich.edu * Note that ProcessParams is generated by swig and there are no other 14293105Sstever@eecs.umich.edu * examples of how to create anything but this default constructor. The 14303105Sstever@eecs.umich.edu * fields are manually initialized instead of passing parameters to the 14313105Sstever@eecs.umich.edu * constructor. 14323101Sstever@eecs.umich.edu */ 14333101Sstever@eecs.umich.edu ProcessParams *pp = new ProcessParams(); 14343101Sstever@eecs.umich.edu pp->executable.assign(*(new std::string(p->progName()))); 14353105Sstever@eecs.umich.edu pp->cmd.push_back(*(new std::string(p->progName()))); 14363105Sstever@eecs.umich.edu pp->system = p->system; 14373101Sstever@eecs.umich.edu pp->cwd.assign(p->getcwd()); 14383101Sstever@eecs.umich.edu pp->input.assign("stdin"); 14393101Sstever@eecs.umich.edu pp->output.assign("stdout"); 14403105Sstever@eecs.umich.edu pp->errout.assign("stderr"); 14413105Sstever@eecs.umich.edu pp->uid = p->uid(); 14423101Sstever@eecs.umich.edu pp->euid = p->euid(); 14433101Sstever@eecs.umich.edu pp->gid = p->gid(); 14443101Sstever@eecs.umich.edu pp->egid = p->egid(); 14453101Sstever@eecs.umich.edu 14463105Sstever@eecs.umich.edu /* Find the first free PID that's less than the maximum */ 14473105Sstever@eecs.umich.edu std::set<int> const& pids = p->system->PIDs; 14483101Sstever@eecs.umich.edu int temp_pid = *pids.begin(); 14493101Sstever@eecs.umich.edu do { 14503105Sstever@eecs.umich.edu temp_pid++; 14513105Sstever@eecs.umich.edu } while (pids.find(temp_pid) != pids.end()); 14523105Sstever@eecs.umich.edu if (temp_pid >= System::maxPID) 14533109Sstever@eecs.umich.edu fatal("temp_pid is too large: %d", temp_pid); 14543109Sstever@eecs.umich.edu 14553109Sstever@eecs.umich.edu pp->pid = temp_pid; 14563109Sstever@eecs.umich.edu pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 14573109Sstever@eecs.umich.edu Process *cp = pp->create(); 14583109Sstever@eecs.umich.edu delete pp; 14593109Sstever@eecs.umich.edu 14603109Sstever@eecs.umich.edu Process *owner = ctc->getProcessPtr(); 14613105Sstever@eecs.umich.edu ctc->setProcessPtr(cp); 14626654Snate@binkert.org cp->assignThreadContext(ctc->contextId()); 14636654Snate@binkert.org owner->revokeThreadContext(ctc->contextId()); 14646654Snate@binkert.org 14656654Snate@binkert.org if (flags & OS::TGT_CLONE_PARENT_SETTID) { 14666654Snate@binkert.org BufferArg ptidBuf(ptidPtr, sizeof(long)); 14676654Snate@binkert.org long *ptid = (long *)ptidBuf.bufferPtr(); 14686654Snate@binkert.org *ptid = cp->pid(); 14696654Snate@binkert.org ptidBuf.copyOut(tc->getMemProxy()); 14706654Snate@binkert.org } 14713101Sstever@eecs.umich.edu 14723101Sstever@eecs.umich.edu cp->initState(); 14733101Sstever@eecs.umich.edu p->clone(tc, ctc, cp, flags); 14743101Sstever@eecs.umich.edu 14753101Sstever@eecs.umich.edu if (flags & OS::TGT_CLONE_THREAD) { 14763101Sstever@eecs.umich.edu delete cp->sigchld; 14777777Sgblack@eecs.umich.edu cp->sigchld = p->sigchld; 14783101Sstever@eecs.umich.edu } else if (flags & OS::TGT_SIGCHLD) { 14794167Sbinkertn@umich.edu *cp->sigchld = true; 14803101Sstever@eecs.umich.edu } 14813101Sstever@eecs.umich.edu 14823101Sstever@eecs.umich.edu if (flags & OS::TGT_CLONE_CHILD_SETTID) { 14833885Sbinkertn@umich.edu BufferArg ctidBuf(ctidPtr, sizeof(long)); 14843102Sstever@eecs.umich.edu long *ctid = (long *)ctidBuf.bufferPtr(); 14853101Sstever@eecs.umich.edu *ctid = cp->pid(); 14866654Snate@binkert.org ctidBuf.copyOut(ctc->getMemProxy()); 14876654Snate@binkert.org } 1488 1489 if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 1490 cp->childClearTID = (uint64_t)ctidPtr; 1491 1492 ctc->clearArchRegs(); 1493 1494 OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 1495 1496 cp->setSyscallReturn(ctc, 0); 1497 1498#if THE_ISA == ALPHA_ISA 1499 ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 1500#elif THE_ISA == SPARC_ISA 1501 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1502 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 1503#endif 1504 1505 TheISA::PCState cpc = tc->pcState(); 1506 cpc.advance(); 1507 ctc->pcState(cpc); 1508 ctc->activate(); 1509 1510 return cp->pid(); 1511} 1512 1513/// Target fstatfs() handler. 1514template <class OS> 1515SyscallReturn 1516fstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1517{ 1518 int index = 0; 1519 int tgt_fd = p->getSyscallArg(tc, index); 1520 Addr bufPtr = p->getSyscallArg(tc, index); 1521 1522 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1523 if (!ffdp) 1524 return -EBADF; 1525 int sim_fd = ffdp->getSimFD(); 1526 1527 struct statfs hostBuf; 1528 int result = fstatfs(sim_fd, &hostBuf); 1529 1530 if (result < 0) 1531 return -errno; 1532 1533 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1534 1535 return 0; 1536} 1537 1538/// Target readv() handler. 1539template <class OS> 1540SyscallReturn 1541readvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1542{ 1543 int index = 0; 1544 int tgt_fd = p->getSyscallArg(tc, index); 1545 1546 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1547 if (!ffdp) 1548 return -EBADF; 1549 int sim_fd = ffdp->getSimFD(); 1550 1551 SETranslatingPortProxy &prox = tc->getMemProxy(); 1552 uint64_t tiov_base = p->getSyscallArg(tc, index); 1553 size_t count = p->getSyscallArg(tc, index); 1554 typename OS::tgt_iovec tiov[count]; 1555 struct iovec hiov[count]; 1556 for (size_t i = 0; i < count; ++i) { 1557 prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)), 1558 (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec)); 1559 hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len); 1560 hiov[i].iov_base = new char [hiov[i].iov_len]; 1561 } 1562 1563 int result = readv(sim_fd, hiov, count); 1564 int local_errno = errno; 1565 1566 for (size_t i = 0; i < count; ++i) { 1567 if (result != -1) { 1568 prox.writeBlob(TheISA::htog(tiov[i].iov_base), 1569 (uint8_t*)hiov[i].iov_base, hiov[i].iov_len); 1570 } 1571 delete [] (char *)hiov[i].iov_base; 1572 } 1573 1574 return (result == -1) ? -local_errno : result; 1575} 1576 1577/// Target writev() handler. 1578template <class OS> 1579SyscallReturn 1580writevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1581{ 1582 int index = 0; 1583 int tgt_fd = p->getSyscallArg(tc, index); 1584 1585 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 1586 if (!hbfdp) 1587 return -EBADF; 1588 int sim_fd = hbfdp->getSimFD(); 1589 1590 SETranslatingPortProxy &prox = tc->getMemProxy(); 1591 uint64_t tiov_base = p->getSyscallArg(tc, index); 1592 size_t count = p->getSyscallArg(tc, index); 1593 struct iovec hiov[count]; 1594 for (size_t i = 0; i < count; ++i) { 1595 typename OS::tgt_iovec tiov; 1596 1597 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 1598 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 1599 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1600 hiov[i].iov_base = new char [hiov[i].iov_len]; 1601 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 1602 hiov[i].iov_len); 1603 } 1604 1605 int result = writev(sim_fd, hiov, count); 1606 1607 for (size_t i = 0; i < count; ++i) 1608 delete [] (char *)hiov[i].iov_base; 1609 1610 return (result == -1) ? -errno : result; 1611} 1612 1613/// Real mmap handler. 1614template <class OS> 1615SyscallReturn 1616mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1617 bool is_mmap2) 1618{ 1619 int index = 0; 1620 Addr start = p->getSyscallArg(tc, index); 1621 uint64_t length = p->getSyscallArg(tc, index); 1622 int prot = p->getSyscallArg(tc, index); 1623 int tgt_flags = p->getSyscallArg(tc, index); 1624 int tgt_fd = p->getSyscallArg(tc, index); 1625 int offset = p->getSyscallArg(tc, index); 1626 1627 if (is_mmap2) 1628 offset *= TheISA::PageBytes; 1629 1630 if (start & (TheISA::PageBytes - 1) || 1631 offset & (TheISA::PageBytes - 1) || 1632 (tgt_flags & OS::TGT_MAP_PRIVATE && 1633 tgt_flags & OS::TGT_MAP_SHARED) || 1634 (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 1635 !(tgt_flags & OS::TGT_MAP_SHARED)) || 1636 !length) { 1637 return -EINVAL; 1638 } 1639 1640 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1641 // With shared mmaps, there are two cases to consider: 1642 // 1) anonymous: writes should modify the mapping and this should be 1643 // visible to observers who share the mapping. Currently, it's 1644 // difficult to update the shared mapping because there's no 1645 // structure which maintains information about the which virtual 1646 // memory areas are shared. If that structure existed, it would be 1647 // possible to make the translations point to the same frames. 1648 // 2) file-backed: writes should modify the mapping and the file 1649 // which is backed by the mapping. The shared mapping problem is the 1650 // same as what was mentioned about the anonymous mappings. For 1651 // file-backed mappings, the writes to the file are difficult 1652 // because it requires syncing what the mapping holds with the file 1653 // that resides on the host system. So, any write on a real system 1654 // would cause the change to be propagated to the file mapping at 1655 // some point in the future (the inode is tracked along with the 1656 // mapping). This isn't guaranteed to always happen, but it usually 1657 // works well enough. The guarantee is provided by the msync system 1658 // call. We could force the change through with shared mappings with 1659 // a call to msync, but that again would require more information 1660 // than we currently maintain. 1661 warn("mmap: writing to shared mmap region is currently " 1662 "unsupported. The write succeeds on the target, but it " 1663 "will not be propagated to the host or shared mappings"); 1664 } 1665 1666 length = roundUp(length, TheISA::PageBytes); 1667 1668 int sim_fd = -1; 1669 uint8_t *pmap = nullptr; 1670 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1671 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1672 1673 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1674 if (dfdp) { 1675 EmulatedDriver *emul_driver = dfdp->getDriver(); 1676 return emul_driver->mmap(p, tc, start, length, prot, 1677 tgt_flags, tgt_fd, offset); 1678 } 1679 1680 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1681 if (!ffdp) 1682 return -EBADF; 1683 sim_fd = ffdp->getSimFD(); 1684 1685 pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 1686 sim_fd, offset); 1687 1688 if (pmap == (decltype(pmap))-1) { 1689 warn("mmap: failed to map file into host address space"); 1690 return -errno; 1691 } 1692 } 1693 1694 // Extend global mmap region if necessary. Note that we ignore the 1695 // start address unless MAP_FIXED is specified. 1696 if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 1697 std::shared_ptr<MemState> mem_state = p->memState; 1698 Addr mmap_end = mem_state->getMmapEnd(); 1699 1700 start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1701 mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 1702 1703 mem_state->setMmapEnd(mmap_end); 1704 } 1705 1706 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1707 start, start + length - 1); 1708 1709 // We only allow mappings to overwrite existing mappings if 1710 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 1711 // because we ignore the start hint if TGT_MAP_FIXED is not set. 1712 int clobber = tgt_flags & OS::TGT_MAP_FIXED; 1713 if (clobber) { 1714 for (auto tc : p->system->threadContexts) { 1715 // If we might be overwriting old mappings, we need to 1716 // invalidate potentially stale mappings out of the TLBs. 1717 tc->getDTBPtr()->flushAll(); 1718 tc->getITBPtr()->flushAll(); 1719 } 1720 } 1721 1722 // Allocate physical memory and map it in. If the page table is already 1723 // mapped and clobber is not set, the simulator will issue throw a 1724 // fatal and bail out of the simulation. 1725 p->allocateMem(start, length, clobber); 1726 1727 // Transfer content into target address space. 1728 SETranslatingPortProxy &tp = tc->getMemProxy(); 1729 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1730 // In general, we should zero the mapped area for anonymous mappings, 1731 // with something like: 1732 // tp.memsetBlob(start, 0, length); 1733 // However, given that we don't support sparse mappings, and 1734 // some applications can map a couple of gigabytes of space 1735 // (intending sparse usage), that can get painfully expensive. 1736 // Fortunately, since we don't properly implement munmap either, 1737 // there's no danger of remapping used memory, so for now all 1738 // newly mapped memory should already be zeroed so we can skip it. 1739 } else { 1740 // It is possible to mmap an area larger than a file, however 1741 // accessing unmapped portions the system triggers a "Bus error" 1742 // on the host. We must know when to stop copying the file from 1743 // the host into the target address space. 1744 struct stat file_stat; 1745 if (fstat(sim_fd, &file_stat) > 0) 1746 fatal("mmap: cannot stat file"); 1747 1748 // Copy the portion of the file that is resident. This requires 1749 // checking both the mmap size and the filesize that we are 1750 // trying to mmap into this space; the mmap size also depends 1751 // on the specified offset into the file. 1752 uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1753 length); 1754 tp.writeBlob(start, pmap, size); 1755 1756 // Cleanup the mmap region before exiting this function. 1757 munmap(pmap, length); 1758 1759 // Maintain the symbol table for dynamic executables. 1760 // The loader will call mmap to map the images into its address 1761 // space and we intercept that here. We can verify that we are 1762 // executing inside the loader by checking the program counter value. 1763 // XXX: with multiprogrammed workloads or multi-node configurations, 1764 // this will not work since there is a single global symbol table. 1765 ObjectFile *interpreter = p->getInterpreter(); 1766 if (interpreter) { 1767 Addr text_start = interpreter->textBase(); 1768 Addr text_end = text_start + interpreter->textSize(); 1769 1770 Addr pc = tc->pcState().pc(); 1771 1772 if (pc >= text_start && pc < text_end) { 1773 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1774 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1775 ObjectFile *lib = createObjectFile(ffdp->getFileName()); 1776 1777 if (lib) { 1778 lib->loadAllSymbols(debugSymbolTable, 1779 lib->textBase(), start); 1780 } 1781 } 1782 } 1783 1784 // Note that we do not zero out the remainder of the mapping. This 1785 // is done by a real system, but it probably will not affect 1786 // execution (hopefully). 1787 } 1788 1789 return start; 1790} 1791 1792template <class OS> 1793SyscallReturn 1794pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1795{ 1796 int index = 0; 1797 int tgt_fd = p->getSyscallArg(tc, index); 1798 Addr bufPtr = p->getSyscallArg(tc, index); 1799 int nbytes = p->getSyscallArg(tc, index); 1800 int offset = p->getSyscallArg(tc, index); 1801 1802 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1803 if (!ffdp) 1804 return -EBADF; 1805 int sim_fd = ffdp->getSimFD(); 1806 1807 BufferArg bufArg(bufPtr, nbytes); 1808 bufArg.copyIn(tc->getMemProxy()); 1809 1810 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1811 1812 return (bytes_written == -1) ? -errno : bytes_written; 1813} 1814 1815/// Target mmap() handler. 1816template <class OS> 1817SyscallReturn 1818mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1819{ 1820 return mmapImpl<OS>(desc, num, p, tc, false); 1821} 1822 1823/// Target mmap2() handler. 1824template <class OS> 1825SyscallReturn 1826mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1827{ 1828 return mmapImpl<OS>(desc, num, p, tc, true); 1829} 1830 1831/// Target getrlimit() handler. 1832template <class OS> 1833SyscallReturn 1834getrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1835 ThreadContext *tc) 1836{ 1837 int index = 0; 1838 unsigned resource = process->getSyscallArg(tc, index); 1839 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1840 1841 switch (resource) { 1842 case OS::TGT_RLIMIT_STACK: 1843 // max stack size in bytes: make up a number (8MB for now) 1844 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1845 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1846 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1847 break; 1848 1849 case OS::TGT_RLIMIT_DATA: 1850 // max data segment size in bytes: make up a number 1851 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1852 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1853 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1854 break; 1855 1856 default: 1857 warn("getrlimit: unimplemented resource %d", resource); 1858 return -EINVAL; 1859 break; 1860 } 1861 1862 rlp.copyOut(tc->getMemProxy()); 1863 return 0; 1864} 1865 1866template <class OS> 1867SyscallReturn 1868prlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1869 ThreadContext *tc) 1870{ 1871 int index = 0; 1872 if (process->getSyscallArg(tc, index) != 0) 1873 { 1874 warn("prlimit: ignoring rlimits for nonzero pid"); 1875 return -EPERM; 1876 } 1877 int resource = process->getSyscallArg(tc, index); 1878 Addr n = process->getSyscallArg(tc, index); 1879 if (n != 0) 1880 warn("prlimit: ignoring new rlimit"); 1881 Addr o = process->getSyscallArg(tc, index); 1882 if (o != 0) 1883 { 1884 TypedBufferArg<typename OS::rlimit> rlp(o); 1885 switch (resource) { 1886 case OS::TGT_RLIMIT_STACK: 1887 // max stack size in bytes: make up a number (8MB for now) 1888 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1889 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1890 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1891 break; 1892 case OS::TGT_RLIMIT_DATA: 1893 // max data segment size in bytes: make up a number 1894 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 1895 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1896 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1897 break; 1898 default: 1899 warn("prlimit: unimplemented resource %d", resource); 1900 return -EINVAL; 1901 break; 1902 } 1903 rlp.copyOut(tc->getMemProxy()); 1904 } 1905 return 0; 1906} 1907 1908/// Target clock_gettime() function. 1909template <class OS> 1910SyscallReturn 1911clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1912{ 1913 int index = 1; 1914 //int clk_id = p->getSyscallArg(tc, index); 1915 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1916 1917 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 1918 tp->tv_sec += seconds_since_epoch; 1919 tp->tv_sec = TheISA::htog(tp->tv_sec); 1920 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 1921 1922 tp.copyOut(tc->getMemProxy()); 1923 1924 return 0; 1925} 1926 1927/// Target clock_getres() function. 1928template <class OS> 1929SyscallReturn 1930clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1931{ 1932 int index = 1; 1933 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1934 1935 // Set resolution at ns, which is what clock_gettime() returns 1936 tp->tv_sec = 0; 1937 tp->tv_nsec = 1; 1938 1939 tp.copyOut(tc->getMemProxy()); 1940 1941 return 0; 1942} 1943 1944/// Target gettimeofday() handler. 1945template <class OS> 1946SyscallReturn 1947gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 1948 ThreadContext *tc) 1949{ 1950 int index = 0; 1951 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1952 1953 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1954 tp->tv_sec += seconds_since_epoch; 1955 tp->tv_sec = TheISA::htog(tp->tv_sec); 1956 tp->tv_usec = TheISA::htog(tp->tv_usec); 1957 1958 tp.copyOut(tc->getMemProxy()); 1959 1960 return 0; 1961} 1962 1963 1964/// Target utimes() handler. 1965template <class OS> 1966SyscallReturn 1967utimesFunc(SyscallDesc *desc, int callnum, Process *process, 1968 ThreadContext *tc) 1969{ 1970 std::string path; 1971 1972 int index = 0; 1973 if (!tc->getMemProxy().tryReadString(path, 1974 process->getSyscallArg(tc, index))) { 1975 return -EFAULT; 1976 } 1977 1978 TypedBufferArg<typename OS::timeval [2]> 1979 tp(process->getSyscallArg(tc, index)); 1980 tp.copyIn(tc->getMemProxy()); 1981 1982 struct timeval hostTimeval[2]; 1983 for (int i = 0; i < 2; ++i) { 1984 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 1985 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 1986 } 1987 1988 // Adjust path for current working directory 1989 path = process->fullPath(path); 1990 1991 int result = utimes(path.c_str(), hostTimeval); 1992 1993 if (result < 0) 1994 return -errno; 1995 1996 return 0; 1997} 1998 1999template <class OS> 2000SyscallReturn 2001execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 2002{ 2003 desc->setFlags(0); 2004 2005 int index = 0; 2006 std::string path; 2007 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 2008 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 2009 return -EFAULT; 2010 2011 if (access(path.c_str(), F_OK) == -1) 2012 return -EACCES; 2013 2014 auto read_in = [](std::vector<std::string> & vect, 2015 SETranslatingPortProxy & mem_proxy, 2016 Addr mem_loc) 2017 { 2018 for (int inc = 0; ; inc++) { 2019 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 2020 b.copyIn(mem_proxy); 2021 2022 if (!*(Addr*)b.bufferPtr()) 2023 break; 2024 2025 vect.push_back(std::string()); 2026 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 2027 } 2028 }; 2029 2030 /** 2031 * Note that ProcessParams is generated by swig and there are no other 2032 * examples of how to create anything but this default constructor. The 2033 * fields are manually initialized instead of passing parameters to the 2034 * constructor. 2035 */ 2036 ProcessParams *pp = new ProcessParams(); 2037 pp->executable = path; 2038 Addr argv_mem_loc = p->getSyscallArg(tc, index); 2039 read_in(pp->cmd, mem_proxy, argv_mem_loc); 2040 Addr envp_mem_loc = p->getSyscallArg(tc, index); 2041 read_in(pp->env, mem_proxy, envp_mem_loc); 2042 pp->uid = p->uid(); 2043 pp->egid = p->egid(); 2044 pp->euid = p->euid(); 2045 pp->gid = p->gid(); 2046 pp->ppid = p->ppid(); 2047 pp->pid = p->pid(); 2048 pp->input.assign("cin"); 2049 pp->output.assign("cout"); 2050 pp->errout.assign("cerr"); 2051 pp->cwd.assign(p->getcwd()); 2052 pp->system = p->system; 2053 /** 2054 * Prevent process object creation with identical PIDs (which will trip 2055 * a fatal check in Process constructor). The execve call is supposed to 2056 * take over the currently executing process' identity but replace 2057 * whatever it is doing with a new process image. Instead of hijacking 2058 * the process object in the simulator, we create a new process object 2059 * and bind to the previous process' thread below (hijacking the thread). 2060 */ 2061 p->system->PIDs.erase(p->pid()); 2062 Process *new_p = pp->create(); 2063 delete pp; 2064 2065 /** 2066 * Work through the file descriptor array and close any files marked 2067 * close-on-exec. 2068 */ 2069 new_p->fds = p->fds; 2070 for (int i = 0; i < new_p->fds->getSize(); i++) { 2071 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 2072 if (fdep && fdep->getCOE()) 2073 new_p->fds->closeFDEntry(i); 2074 } 2075 2076 *new_p->sigchld = true; 2077 2078 delete p; 2079 tc->clearArchRegs(); 2080 tc->setProcessPtr(new_p); 2081 new_p->assignThreadContext(tc->contextId()); 2082 new_p->initState(); 2083 tc->activate(); 2084 TheISA::PCState pcState = tc->pcState(); 2085 tc->setNPC(pcState.instAddr()); 2086 2087 desc->setFlags(SyscallDesc::SuppressReturnValue); 2088 return 0; 2089} 2090 2091/// Target getrusage() function. 2092template <class OS> 2093SyscallReturn 2094getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 2095 ThreadContext *tc) 2096{ 2097 int index = 0; 2098 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 2099 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2100 2101 rup->ru_utime.tv_sec = 0; 2102 rup->ru_utime.tv_usec = 0; 2103 rup->ru_stime.tv_sec = 0; 2104 rup->ru_stime.tv_usec = 0; 2105 rup->ru_maxrss = 0; 2106 rup->ru_ixrss = 0; 2107 rup->ru_idrss = 0; 2108 rup->ru_isrss = 0; 2109 rup->ru_minflt = 0; 2110 rup->ru_majflt = 0; 2111 rup->ru_nswap = 0; 2112 rup->ru_inblock = 0; 2113 rup->ru_oublock = 0; 2114 rup->ru_msgsnd = 0; 2115 rup->ru_msgrcv = 0; 2116 rup->ru_nsignals = 0; 2117 rup->ru_nvcsw = 0; 2118 rup->ru_nivcsw = 0; 2119 2120 switch (who) { 2121 case OS::TGT_RUSAGE_SELF: 2122 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 2123 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 2124 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 2125 break; 2126 2127 case OS::TGT_RUSAGE_CHILDREN: 2128 // do nothing. We have no child processes, so they take no time. 2129 break; 2130 2131 default: 2132 // don't really handle THREAD or CHILDREN, but just warn and 2133 // plow ahead 2134 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 2135 who); 2136 } 2137 2138 rup.copyOut(tc->getMemProxy()); 2139 2140 return 0; 2141} 2142 2143/// Target times() function. 2144template <class OS> 2145SyscallReturn 2146timesFunc(SyscallDesc *desc, int callnum, Process *process, 2147 ThreadContext *tc) 2148{ 2149 int index = 0; 2150 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 2151 2152 // Fill in the time structure (in clocks) 2153 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 2154 bufp->tms_utime = clocks; 2155 bufp->tms_stime = 0; 2156 bufp->tms_cutime = 0; 2157 bufp->tms_cstime = 0; 2158 2159 // Convert to host endianness 2160 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 2161 2162 // Write back 2163 bufp.copyOut(tc->getMemProxy()); 2164 2165 // Return clock ticks since system boot 2166 return clocks; 2167} 2168 2169/// Target time() function. 2170template <class OS> 2171SyscallReturn 2172timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 2173{ 2174 typename OS::time_t sec, usec; 2175 getElapsedTimeMicro(sec, usec); 2176 sec += seconds_since_epoch; 2177 2178 int index = 0; 2179 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2180 if (taddr != 0) { 2181 typename OS::time_t t = sec; 2182 t = TheISA::htog(t); 2183 SETranslatingPortProxy &p = tc->getMemProxy(); 2184 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2185 } 2186 return sec; 2187} 2188 2189template <class OS> 2190SyscallReturn 2191tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 2192{ 2193 int index = 0; 2194 int tgid = process->getSyscallArg(tc, index); 2195 int tid = process->getSyscallArg(tc, index); 2196 int sig = process->getSyscallArg(tc, index); 2197 2198 /** 2199 * This system call is intended to allow killing a specific thread 2200 * within an arbitrary thread group if sanctioned with permission checks. 2201 * It's usually true that threads share the termination signal as pointed 2202 * out by the pthread_kill man page and this seems to be the intended 2203 * usage. Due to this being an emulated environment, assume the following: 2204 * Threads are allowed to call tgkill because the EUID for all threads 2205 * should be the same. There is no signal handling mechanism for kernel 2206 * registration of signal handlers since signals are poorly supported in 2207 * emulation mode. Since signal handlers cannot be registered, all 2208 * threads within in a thread group must share the termination signal. 2209 * We never exhaust PIDs so there's no chance of finding the wrong one 2210 * due to PID rollover. 2211 */ 2212 2213 System *sys = tc->getSystemPtr(); 2214 Process *tgt_proc = nullptr; 2215 for (int i = 0; i < sys->numContexts(); i++) { 2216 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2217 if (temp->pid() == tid) { 2218 tgt_proc = temp; 2219 break; 2220 } 2221 } 2222 2223 if (sig != 0 || sig != OS::TGT_SIGABRT) 2224 return -EINVAL; 2225 2226 if (tgt_proc == nullptr) 2227 return -ESRCH; 2228 2229 if (tgid != -1 && tgt_proc->tgid() != tgid) 2230 return -ESRCH; 2231 2232 if (sig == OS::TGT_SIGABRT) 2233 exitGroupFunc(desc, 252, process, tc); 2234 2235 return 0; 2236} 2237 2238template <class OS> 2239SyscallReturn 2240socketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2241{ 2242 int index = 0; 2243 int domain = p->getSyscallArg(tc, index); 2244 int type = p->getSyscallArg(tc, index); 2245 int prot = p->getSyscallArg(tc, index); 2246 2247 int sim_fd = socket(domain, type, prot); 2248 if (sim_fd == -1) 2249 return -errno; 2250 2251 auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 2252 int tgt_fd = p->fds->allocFD(sfdp); 2253 2254 return tgt_fd; 2255} 2256 2257template <class OS> 2258SyscallReturn 2259socketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2260{ 2261 int index = 0; 2262 int domain = p->getSyscallArg(tc, index); 2263 int type = p->getSyscallArg(tc, index); 2264 int prot = p->getSyscallArg(tc, index); 2265 Addr svPtr = p->getSyscallArg(tc, index); 2266 2267 BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 2268 int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 2269 if (status == -1) 2270 return -errno; 2271 2272 int *fds = (int *)svBuf.bufferPtr(); 2273 2274 auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 2275 fds[0] = p->fds->allocFD(sfdp1); 2276 auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 2277 fds[1] = p->fds->allocFD(sfdp2); 2278 svBuf.copyOut(tc->getMemProxy()); 2279 2280 return status; 2281} 2282 2283template <class OS> 2284SyscallReturn 2285selectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 2286{ 2287 int retval; 2288 2289 int index = 0; 2290 int nfds_t = p->getSyscallArg(tc, index); 2291 Addr fds_read_ptr = p->getSyscallArg(tc, index); 2292 Addr fds_writ_ptr = p->getSyscallArg(tc, index); 2293 Addr fds_excp_ptr = p->getSyscallArg(tc, index); 2294 Addr time_val_ptr = p->getSyscallArg(tc, index); 2295 2296 TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 2297 TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 2298 TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 2299 TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 2300 2301 /** 2302 * Host fields. Notice that these use the definitions from the system 2303 * headers instead of the gem5 headers and libraries. If the host and 2304 * target have different header file definitions, this will not work. 2305 */ 2306 fd_set rd_h; 2307 FD_ZERO(&rd_h); 2308 fd_set wr_h; 2309 FD_ZERO(&wr_h); 2310 fd_set ex_h; 2311 FD_ZERO(&ex_h); 2312 2313 /** 2314 * Copy in the fd_set from the target. 2315 */ 2316 if (fds_read_ptr) 2317 rd_t.copyIn(tc->getMemProxy()); 2318 if (fds_writ_ptr) 2319 wr_t.copyIn(tc->getMemProxy()); 2320 if (fds_excp_ptr) 2321 ex_t.copyIn(tc->getMemProxy()); 2322 2323 /** 2324 * We need to translate the target file descriptor set into a host file 2325 * descriptor set. This involves both our internal process fd array 2326 * and the fd_set defined in Linux header files. The nfds field also 2327 * needs to be updated as it will be only target specific after 2328 * retrieving it from the target; the nfds value is expected to be the 2329 * highest file descriptor that needs to be checked, so we need to extend 2330 * it out for nfds_h when we do the update. 2331 */ 2332 int nfds_h = 0; 2333 std::map<int, int> trans_map; 2334 auto try_add_host_set = [&](fd_set *tgt_set_entry, 2335 fd_set *hst_set_entry, 2336 int iter) -> bool 2337 { 2338 /** 2339 * By this point, we know that we are looking at a valid file 2340 * descriptor set on the target. We need to check if the target file 2341 * descriptor value passed in as iter is part of the set. 2342 */ 2343 if (FD_ISSET(iter, tgt_set_entry)) { 2344 /** 2345 * We know that the target file descriptor belongs to the set, 2346 * but we do not yet know if the file descriptor is valid or 2347 * that we have a host mapping. Check that now. 2348 */ 2349 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 2350 if (!hbfdp) 2351 return true; 2352 auto sim_fd = hbfdp->getSimFD(); 2353 2354 /** 2355 * Add the sim_fd to tgt_fd translation into trans_map for use 2356 * later when we need to zero the target fd_set structures and 2357 * then update them with hits returned from the host select call. 2358 */ 2359 trans_map[sim_fd] = iter; 2360 2361 /** 2362 * We know that the host file descriptor exists so now we check 2363 * if we need to update the max count for nfds_h before passing 2364 * the duplicated structure into the host. 2365 */ 2366 nfds_h = std::max(nfds_h - 1, sim_fd + 1); 2367 2368 /** 2369 * Add the host file descriptor to the set that we are going to 2370 * pass into the host. 2371 */ 2372 FD_SET(sim_fd, hst_set_entry); 2373 } 2374 return false; 2375 }; 2376 2377 for (int i = 0; i < nfds_t; i++) { 2378 if (fds_read_ptr) { 2379 bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 2380 if (ebadf) return -EBADF; 2381 } 2382 if (fds_writ_ptr) { 2383 bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 2384 if (ebadf) return -EBADF; 2385 } 2386 if (fds_excp_ptr) { 2387 bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 2388 if (ebadf) return -EBADF; 2389 } 2390 } 2391 2392 if (time_val_ptr) { 2393 /** 2394 * It might be possible to decrement the timeval based on some 2395 * derivation of wall clock determined from elapsed simulator ticks 2396 * but that seems like overkill. Rather, we just set the timeval with 2397 * zero timeout. (There is no reason to block during the simulation 2398 * as it only decreases simulator performance.) 2399 */ 2400 tp->tv_sec = 0; 2401 tp->tv_usec = 0; 2402 2403 retval = select(nfds_h, 2404 fds_read_ptr ? &rd_h : nullptr, 2405 fds_writ_ptr ? &wr_h : nullptr, 2406 fds_excp_ptr ? &ex_h : nullptr, 2407 (timeval*)&*tp); 2408 } else { 2409 /** 2410 * If the timeval pointer is null, setup a new timeval structure to 2411 * pass into the host select call. Unfortunately, we will need to 2412 * manually check the return value and throw a retry fault if the 2413 * return value is zero. Allowing the system call to block will 2414 * likely deadlock the event queue. 2415 */ 2416 struct timeval tv = { 0, 0 }; 2417 2418 retval = select(nfds_h, 2419 fds_read_ptr ? &rd_h : nullptr, 2420 fds_writ_ptr ? &wr_h : nullptr, 2421 fds_excp_ptr ? &ex_h : nullptr, 2422 &tv); 2423 2424 if (retval == 0) { 2425 /** 2426 * If blocking indefinitely, check the signal list to see if a 2427 * signal would break the poll out of the retry cycle and try to 2428 * return the signal interrupt instead. 2429 */ 2430 for (auto sig : tc->getSystemPtr()->signalList) 2431 if (sig.receiver == p) 2432 return -EINTR; 2433 return SyscallReturn::retry(); 2434 } 2435 } 2436 2437 if (retval == -1) 2438 return -errno; 2439 2440 FD_ZERO((fd_set*)&*rd_t); 2441 FD_ZERO((fd_set*)&*wr_t); 2442 FD_ZERO((fd_set*)&*ex_t); 2443 2444 /** 2445 * We need to translate the host file descriptor set into a target file 2446 * descriptor set. This involves both our internal process fd array 2447 * and the fd_set defined in header files. 2448 */ 2449 for (int i = 0; i < nfds_h; i++) { 2450 if (fds_read_ptr) { 2451 if (FD_ISSET(i, &rd_h)) 2452 FD_SET(trans_map[i], (fd_set*)&*rd_t); 2453 } 2454 2455 if (fds_writ_ptr) { 2456 if (FD_ISSET(i, &wr_h)) 2457 FD_SET(trans_map[i], (fd_set*)&*wr_t); 2458 } 2459 2460 if (fds_excp_ptr) { 2461 if (FD_ISSET(i, &ex_h)) 2462 FD_SET(trans_map[i], (fd_set*)&*ex_t); 2463 } 2464 } 2465 2466 if (fds_read_ptr) 2467 rd_t.copyOut(tc->getMemProxy()); 2468 if (fds_writ_ptr) 2469 wr_t.copyOut(tc->getMemProxy()); 2470 if (fds_excp_ptr) 2471 ex_t.copyOut(tc->getMemProxy()); 2472 if (time_val_ptr) 2473 tp.copyOut(tc->getMemProxy()); 2474 2475 return retval; 2476} 2477 2478template <class OS> 2479SyscallReturn 2480readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2481{ 2482 int index = 0; 2483 int tgt_fd = p->getSyscallArg(tc, index); 2484 Addr buf_ptr = p->getSyscallArg(tc, index); 2485 int nbytes = p->getSyscallArg(tc, index); 2486 2487 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2488 if (!hbfdp) 2489 return -EBADF; 2490 int sim_fd = hbfdp->getSimFD(); 2491 2492 struct pollfd pfd; 2493 pfd.fd = sim_fd; 2494 pfd.events = POLLIN | POLLPRI; 2495 if ((poll(&pfd, 1, 0) == 0) 2496 && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2497 return SyscallReturn::retry(); 2498 2499 BufferArg buf_arg(buf_ptr, nbytes); 2500 int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 2501 2502 if (bytes_read > 0) 2503 buf_arg.copyOut(tc->getMemProxy()); 2504 2505 return (bytes_read == -1) ? -errno : bytes_read; 2506} 2507 2508template <class OS> 2509SyscallReturn 2510writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2511{ 2512 int index = 0; 2513 int tgt_fd = p->getSyscallArg(tc, index); 2514 Addr buf_ptr = p->getSyscallArg(tc, index); 2515 int nbytes = p->getSyscallArg(tc, index); 2516 2517 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2518 if (!hbfdp) 2519 return -EBADF; 2520 int sim_fd = hbfdp->getSimFD(); 2521 2522 BufferArg buf_arg(buf_ptr, nbytes); 2523 buf_arg.copyIn(tc->getMemProxy()); 2524 2525 struct pollfd pfd; 2526 pfd.fd = sim_fd; 2527 pfd.events = POLLOUT; 2528 2529 /** 2530 * We don't want to poll on /dev/random. The kernel will not enable the 2531 * file descriptor for writing unless the entropy in the system falls 2532 * below write_wakeup_threshold. This is not guaranteed to happen 2533 * depending on host settings. 2534 */ 2535 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 2536 if (ffdp && (ffdp->getFileName() != "/dev/random")) { 2537 if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 2538 return SyscallReturn::retry(); 2539 } 2540 2541 int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 2542 2543 if (bytes_written != -1) 2544 fsync(sim_fd); 2545 2546 return (bytes_written == -1) ? -errno : bytes_written; 2547} 2548 2549template <class OS> 2550SyscallReturn 2551wait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2552{ 2553 int index = 0; 2554 pid_t pid = p->getSyscallArg(tc, index); 2555 Addr statPtr = p->getSyscallArg(tc, index); 2556 int options = p->getSyscallArg(tc, index); 2557 Addr rusagePtr = p->getSyscallArg(tc, index); 2558 2559 if (rusagePtr) 2560 DPRINTFR(SyscallVerbose, 2561 "%d: %s: syscall wait4: rusage pointer provided however " 2562 "functionality not supported. Ignoring rusage pointer.\n", 2563 curTick(), tc->getCpuPtr()->name()); 2564 2565 /** 2566 * Currently, wait4 is only implemented so that it will wait for children 2567 * exit conditions which are denoted by a SIGCHLD signals posted into the 2568 * system signal list. We return no additional information via any of the 2569 * parameters supplied to wait4. If nothing is found in the system signal 2570 * list, we will wait indefinitely for SIGCHLD to post by retrying the 2571 * call. 2572 */ 2573 System *sysh = tc->getSystemPtr(); 2574 std::list<BasicSignal>::iterator iter; 2575 for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 2576 if (iter->receiver == p) { 2577 if (pid < -1) { 2578 if ((iter->sender->pgid() == -pid) 2579 && (iter->signalValue == OS::TGT_SIGCHLD)) 2580 goto success; 2581 } else if (pid == -1) { 2582 if (iter->signalValue == OS::TGT_SIGCHLD) 2583 goto success; 2584 } else if (pid == 0) { 2585 if ((iter->sender->pgid() == p->pgid()) 2586 && (iter->signalValue == OS::TGT_SIGCHLD)) 2587 goto success; 2588 } else { 2589 if ((iter->sender->pid() == pid) 2590 && (iter->signalValue == OS::TGT_SIGCHLD)) 2591 goto success; 2592 } 2593 } 2594 } 2595 2596 return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 2597 2598success: 2599 // Set status to EXITED for WIFEXITED evaluations. 2600 const int EXITED = 0; 2601 BufferArg statusBuf(statPtr, sizeof(int)); 2602 *(int *)statusBuf.bufferPtr() = EXITED; 2603 statusBuf.copyOut(tc->getMemProxy()); 2604 2605 // Return the child PID. 2606 pid_t retval = iter->sender->pid(); 2607 sysh->signalList.erase(iter); 2608 return retval; 2609} 2610 2611template <class OS> 2612SyscallReturn 2613acceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2614{ 2615 struct sockaddr sa; 2616 socklen_t addrLen; 2617 int host_fd; 2618 int index = 0; 2619 int tgt_fd = p->getSyscallArg(tc, index); 2620 Addr addrPtr = p->getSyscallArg(tc, index); 2621 Addr lenPtr = p->getSyscallArg(tc, index); 2622 2623 BufferArg *lenBufPtr = nullptr; 2624 BufferArg *addrBufPtr = nullptr; 2625 2626 auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 2627 if (!sfdp) 2628 return -EBADF; 2629 int sim_fd = sfdp->getSimFD(); 2630 2631 /** 2632 * We poll the socket file descriptor first to guarantee that we do not 2633 * block on our accept call. The socket can be opened without the 2634 * non-blocking flag (it blocks). This will cause deadlocks between 2635 * communicating processes. 2636 */ 2637 struct pollfd pfd; 2638 pfd.fd = sim_fd; 2639 pfd.events = POLLIN | POLLPRI; 2640 if ((poll(&pfd, 1, 0) == 0) 2641 && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2642 return SyscallReturn::retry(); 2643 2644 if (lenPtr) { 2645 lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 2646 lenBufPtr->copyIn(tc->getMemProxy()); 2647 memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 2648 sizeof(socklen_t)); 2649 } 2650 2651 if (addrPtr) { 2652 addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 2653 addrBufPtr->copyIn(tc->getMemProxy()); 2654 memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 2655 sizeof(struct sockaddr)); 2656 } 2657 2658 host_fd = accept(sim_fd, &sa, &addrLen); 2659 2660 if (host_fd == -1) 2661 return -errno; 2662 2663 if (addrPtr) { 2664 memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 2665 addrBufPtr->copyOut(tc->getMemProxy()); 2666 delete(addrBufPtr); 2667 } 2668 2669 if (lenPtr) { 2670 *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 2671 lenBufPtr->copyOut(tc->getMemProxy()); 2672 delete(lenBufPtr); 2673 } 2674 2675 auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 2676 sfdp->_type, sfdp->_protocol); 2677 return p->fds->allocFD(afdp); 2678} 2679 2680#endif // __SIM_SYSCALL_EMUL_HH__ 2681