syscall_emul.hh revision 14020
1/*
2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * Copyright (c) 2015 Advanced Micro Devices, Inc.
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder.  You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2003-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Steve Reinhardt
42 *          Kevin Lim
43 */
44
45#ifndef __SIM_SYSCALL_EMUL_HH__
46#define __SIM_SYSCALL_EMUL_HH__
47
48#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
49     defined(__FreeBSD__) || defined(__CYGWIN__) ||     \
50     defined(__NetBSD__))
51#define NO_STAT64 1
52#else
53#define NO_STAT64 0
54#endif
55
56///
57/// @file syscall_emul.hh
58///
59/// This file defines objects used to emulate syscalls from the target
60/// application on the host machine.
61
62#if defined(__linux__)
63#include <sys/eventfd.h>
64#include <sys/statfs.h>
65
66#else
67#include <sys/mount.h>
68
69#endif
70
71#ifdef __CYGWIN32__
72#include <sys/fcntl.h>
73
74#endif
75#include <fcntl.h>
76#include <net/if.h>
77#include <poll.h>
78#include <sys/ioctl.h>
79#include <sys/mman.h>
80#include <sys/socket.h>
81#include <sys/stat.h>
82#include <sys/time.h>
83#include <sys/types.h>
84#include <sys/uio.h>
85#include <unistd.h>
86
87#include <cerrno>
88#include <memory>
89#include <string>
90
91#include "arch/generic/tlb.hh"
92#include "arch/utility.hh"
93#include "base/intmath.hh"
94#include "base/loader/object_file.hh"
95#include "base/logging.hh"
96#include "base/trace.hh"
97#include "base/types.hh"
98#include "config/the_isa.hh"
99#include "cpu/base.hh"
100#include "cpu/thread_context.hh"
101#include "mem/page_table.hh"
102#include "params/Process.hh"
103#include "sim/emul_driver.hh"
104#include "sim/futex_map.hh"
105#include "sim/process.hh"
106#include "sim/syscall_debug_macros.hh"
107#include "sim/syscall_desc.hh"
108#include "sim/syscall_emul_buf.hh"
109#include "sim/syscall_return.hh"
110
111#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN)
112#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
113#endif
114
115//////////////////////////////////////////////////////////////////////
116//
117// The following emulation functions are generic enough that they
118// don't need to be recompiled for different emulated OS's.  They are
119// defined in sim/syscall_emul.cc.
120//
121//////////////////////////////////////////////////////////////////////
122
123void warnUnsupportedOS(std::string syscall_name);
124
125/// Handler for unimplemented syscalls that we haven't thought about.
126SyscallReturn unimplementedFunc(SyscallDesc *desc, int num, ThreadContext *tc);
127
128/// Handler for unimplemented syscalls that we never intend to
129/// implement (signal handling, etc.) and should not affect the correct
130/// behavior of the program.  Print a warning only if the appropriate
131/// trace flag is enabled.  Return success to the target program.
132SyscallReturn ignoreFunc(SyscallDesc *desc, int num, ThreadContext *tc);
133
134// Target fallocateFunc() handler.
135SyscallReturn fallocateFunc(SyscallDesc *desc, int num, ThreadContext *tc);
136
137/// Target exit() handler: terminate current context.
138SyscallReturn exitFunc(SyscallDesc *desc, int num, ThreadContext *tc);
139
140/// Target exit_group() handler: terminate simulation. (exit all threads)
141SyscallReturn exitGroupFunc(SyscallDesc *desc, int num, ThreadContext *tc);
142
143/// Target set_tid_address() handler.
144SyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, ThreadContext *tc);
145
146/// Target getpagesize() handler.
147SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, ThreadContext *tc);
148
149/// Target brk() handler: set brk address.
150SyscallReturn brkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
151
152/// Target close() handler.
153SyscallReturn closeFunc(SyscallDesc *desc, int num, ThreadContext *tc);
154
155/// Target lseek() handler.
156SyscallReturn lseekFunc(SyscallDesc *desc, int num, ThreadContext *tc);
157
158/// Target _llseek() handler.
159SyscallReturn _llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc);
160
161/// Target munmap() handler.
162SyscallReturn munmapFunc(SyscallDesc *desc, int num, ThreadContext *tc);
163
164/// Target shutdown() handler.
165SyscallReturn shutdownFunc(SyscallDesc *desc, int num, ThreadContext *tc);
166
167/// Target gethostname() handler.
168SyscallReturn gethostnameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
169
170/// Target getcwd() handler.
171SyscallReturn getcwdFunc(SyscallDesc *desc, int num, ThreadContext *tc);
172
173/// Target readlink() handler.
174SyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc,
175                           int index = 0);
176SyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
177
178/// Target unlink() handler.
179SyscallReturn unlinkHelper(SyscallDesc *desc, int num, ThreadContext *tc,
180                           int index);
181SyscallReturn unlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
182
183/// Target link() handler
184SyscallReturn linkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
185
186/// Target symlink() handler.
187SyscallReturn symlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
188
189/// Target mkdir() handler.
190SyscallReturn mkdirFunc(SyscallDesc *desc, int num, ThreadContext *tc);
191
192/// Target mknod() handler.
193SyscallReturn mknodFunc(SyscallDesc *desc, int num, ThreadContext *tc);
194
195/// Target chdir() handler.
196SyscallReturn chdirFunc(SyscallDesc *desc, int num, ThreadContext *tc);
197
198// Target rmdir() handler.
199SyscallReturn rmdirFunc(SyscallDesc *desc, int num, ThreadContext *tc);
200
201/// Target rename() handler.
202SyscallReturn renameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
203
204
205/// Target truncate() handler.
206SyscallReturn truncateFunc(SyscallDesc *desc, int num, ThreadContext *tc);
207
208
209/// Target ftruncate() handler.
210SyscallReturn ftruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc);
211
212
213/// Target truncate64() handler.
214SyscallReturn truncate64Func(SyscallDesc *desc, int num, ThreadContext *tc);
215
216/// Target ftruncate64() handler.
217SyscallReturn ftruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc);
218
219
220/// Target umask() handler.
221SyscallReturn umaskFunc(SyscallDesc *desc, int num, ThreadContext *tc);
222
223/// Target gettid() handler.
224SyscallReturn gettidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
225
226/// Target chown() handler.
227SyscallReturn chownFunc(SyscallDesc *desc, int num, ThreadContext *tc);
228
229/// Target setpgid() handler.
230SyscallReturn setpgidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
231
232/// Target fchown() handler.
233SyscallReturn fchownFunc(SyscallDesc *desc, int num, ThreadContext *tc);
234
235/// Target dup() handler.
236SyscallReturn dupFunc(SyscallDesc *desc, int num, ThreadContext *tc);
237
238/// Target dup2() handler.
239SyscallReturn dup2Func(SyscallDesc *desc, int num, ThreadContext *tc);
240
241/// Target fcntl() handler.
242SyscallReturn fcntlFunc(SyscallDesc *desc, int num, ThreadContext *tc);
243
244/// Target fcntl64() handler.
245SyscallReturn fcntl64Func(SyscallDesc *desc, int num, ThreadContext *tc);
246
247/// Target setuid() handler.
248SyscallReturn setuidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
249
250/// Target pipe() handler.
251SyscallReturn pipeFunc(SyscallDesc *desc, int num, ThreadContext *tc);
252
253/// Internal pipe() handler.
254SyscallReturn pipeImpl(SyscallDesc *desc, int num, ThreadContext *tc,
255                       bool pseudoPipe);
256
257/// Target getpid() handler.
258SyscallReturn getpidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
259
260// Target getpeername() handler.
261SyscallReturn getpeernameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
262
263// Target bind() handler.
264SyscallReturn bindFunc(SyscallDesc *desc, int num, ThreadContext *tc);
265
266// Target listen() handler.
267SyscallReturn listenFunc(SyscallDesc *desc, int num, ThreadContext *tc);
268
269// Target connect() handler.
270SyscallReturn connectFunc(SyscallDesc *desc, int num, ThreadContext *tc);
271
272#if defined(SYS_getdents)
273// Target getdents() handler.
274SyscallReturn getdentsFunc(SyscallDesc *desc, int num, ThreadContext *tc);
275#endif
276
277#if defined(SYS_getdents64)
278// Target getdents() handler.
279SyscallReturn getdents64Func(SyscallDesc *desc, int num, ThreadContext *tc);
280#endif
281
282// Target sendto() handler.
283SyscallReturn sendtoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
284
285// Target recvfrom() handler.
286SyscallReturn recvfromFunc(SyscallDesc *desc, int num, ThreadContext *tc);
287
288// Target recvmsg() handler.
289SyscallReturn recvmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc);
290
291// Target sendmsg() handler.
292SyscallReturn sendmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc);
293
294// Target getuid() handler.
295SyscallReturn getuidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
296
297/// Target getgid() handler.
298SyscallReturn getgidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
299
300/// Target getppid() handler.
301SyscallReturn getppidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
302
303/// Target geteuid() handler.
304SyscallReturn geteuidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
305
306/// Target getegid() handler.
307SyscallReturn getegidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
308
309/// Target access() handler
310SyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc);
311SyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc,
312                         int index);
313
314// Target getsockopt() handler.
315SyscallReturn getsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc);
316
317// Target setsockopt() handler.
318SyscallReturn setsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc);
319
320// Target getsockname() handler.
321SyscallReturn getsocknameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
322
323/// Futex system call
324/// Implemented by Daniel Sanchez
325/// Used by printf's in multi-threaded apps
326template <class OS>
327SyscallReturn
328futexFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
329{
330    using namespace std;
331
332    int index = 0;
333    auto process = tc->getProcessPtr();
334
335    Addr uaddr = process->getSyscallArg(tc, index);
336    int op = process->getSyscallArg(tc, index);
337    int val = process->getSyscallArg(tc, index);
338    int timeout M5_VAR_USED = process->getSyscallArg(tc, index);
339    Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index);
340    int val3 = process->getSyscallArg(tc, index);
341
342    /*
343     * Unsupported option that does not affect the correctness of the
344     * application. This is a performance optimization utilized by Linux.
345     */
346    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
347    op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG;
348
349    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
350
351    if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) {
352        // Ensure futex system call accessed atomically.
353        BufferArg buf(uaddr, sizeof(int));
354        buf.copyIn(tc->getMemProxy());
355        int mem_val = *(int*)buf.bufferPtr();
356
357        /*
358         * The value in memory at uaddr is not equal with the expected val
359         * (a different thread must have changed it before the system call was
360         * invoked). In this case, we need to throw an error.
361         */
362        if (val != mem_val)
363            return -OS::TGT_EWOULDBLOCK;
364
365        if (OS::TGT_FUTEX_WAIT) {
366            futex_map.suspend(uaddr, process->tgid(), tc);
367        } else {
368            futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3);
369        }
370
371        return 0;
372    } else if (OS::TGT_FUTEX_WAKE == op) {
373        return futex_map.wakeup(uaddr, process->tgid(), val);
374    } else if (OS::TGT_FUTEX_WAKE_BITSET == op) {
375        return futex_map.wakeup_bitset(uaddr, process->tgid(), val3);
376    } else if (OS::TGT_FUTEX_REQUEUE == op ||
377               OS::TGT_FUTEX_CMP_REQUEUE == op) {
378
379        // Ensure futex system call accessed atomically.
380        BufferArg buf(uaddr, sizeof(int));
381        buf.copyIn(tc->getMemProxy());
382        int mem_val = *(int*)buf.bufferPtr();
383        /*
384         * For CMP_REQUEUE, the whole operation is only started only if
385         * val3 is still the value of the futex pointed to by uaddr.
386         */
387        if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val)
388            return -OS::TGT_EWOULDBLOCK;
389        return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2);
390    } else if (OS::TGT_FUTEX_WAKE_OP == op) {
391        /*
392         * The FUTEX_WAKE_OP operation is equivalent to executing the
393         * following code atomically and totally ordered with respect to
394         * other futex operations on any of the two supplied futex words:
395         *
396         *   int oldval = *(int *) addr2;
397         *   *(int *) addr2 = oldval op oparg;
398         *   futex(addr1, FUTEX_WAKE, val, 0, 0, 0);
399         *   if (oldval cmp cmparg)
400         *        futex(addr2, FUTEX_WAKE, val2, 0, 0, 0);
401         *
402         * (op, oparg, cmp, cmparg are encoded in val3)
403         *
404         * +---+---+-----------+-----------+
405         * |op |cmp|   oparg   |  cmparg   |
406         * +---+---+-----------+-----------+
407         *   4   4       12          12    <== # of bits
408         *
409         * reference: http://man7.org/linux/man-pages/man2/futex.2.html
410         *
411         */
412        // get value from simulated-space
413        BufferArg buf(uaddr2, sizeof(int));
414        buf.copyIn(tc->getMemProxy());
415        int oldval = *(int*)buf.bufferPtr();
416        int newval = oldval;
417        // extract op, oparg, cmp, cmparg from val3
418        int wake_cmparg =  val3 & 0xfff;
419        int wake_oparg  = (val3 & 0xfff000)   >> 12;
420        int wake_cmp    = (val3 & 0xf000000)  >> 24;
421        int wake_op     = (val3 & 0xf0000000) >> 28;
422        if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1)
423            wake_oparg = (1 << wake_oparg);
424        wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT;
425        // perform operation on the value of the second futex
426        if (wake_op == OS::TGT_FUTEX_OP_SET)
427            newval = wake_oparg;
428        else if (wake_op == OS::TGT_FUTEX_OP_ADD)
429            newval += wake_oparg;
430        else if (wake_op == OS::TGT_FUTEX_OP_OR)
431            newval |= wake_oparg;
432        else if (wake_op == OS::TGT_FUTEX_OP_ANDN)
433            newval &= ~wake_oparg;
434        else if (wake_op == OS::TGT_FUTEX_OP_XOR)
435            newval ^= wake_oparg;
436        // copy updated value back to simulated-space
437        *(int*)buf.bufferPtr() = newval;
438        buf.copyOut(tc->getMemProxy());
439        // perform the first wake-up
440        int woken1 = futex_map.wakeup(uaddr, process->tgid(), val);
441        int woken2 = 0;
442        // calculate the condition of the second wake-up
443        bool is_wake2 = false;
444        if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ)
445            is_wake2 = oldval == wake_cmparg;
446        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE)
447            is_wake2 = oldval != wake_cmparg;
448        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT)
449            is_wake2 = oldval < wake_cmparg;
450        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE)
451            is_wake2 = oldval <= wake_cmparg;
452        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT)
453            is_wake2 = oldval > wake_cmparg;
454        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE)
455            is_wake2 = oldval >= wake_cmparg;
456        // perform the second wake-up
457        if (is_wake2)
458            woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout);
459
460        return woken1 + woken2;
461    }
462    warn("futex: op %d not implemented; ignoring.", op);
463    return -ENOSYS;
464}
465
466
467/// Pseudo Funcs  - These functions use a different return convension,
468/// returning a second value in a register other than the normal return register
469SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
470
471/// Target getpidPseudo() handler.
472SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
473
474/// Target getuidPseudo() handler.
475SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
476
477/// Target getgidPseudo() handler.
478SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
479
480
481/// A readable name for 1,000,000, for converting microseconds to seconds.
482const int one_million = 1000000;
483/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
484const int one_billion = 1000000000;
485
486/// Approximate seconds since the epoch (1/1/1970).  About a billion,
487/// by my reckoning.  We want to keep this a constant (not use the
488/// real-world time) to keep simulations repeatable.
489const unsigned seconds_since_epoch = 1000000000;
490
491/// Helper function to convert current elapsed time to seconds and
492/// microseconds.
493template <class T1, class T2>
494void
495getElapsedTimeMicro(T1 &sec, T2 &usec)
496{
497    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
498    sec = elapsed_usecs / one_million;
499    usec = elapsed_usecs % one_million;
500}
501
502/// Helper function to convert current elapsed time to seconds and
503/// nanoseconds.
504template <class T1, class T2>
505void
506getElapsedTimeNano(T1 &sec, T2 &nsec)
507{
508    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
509    sec = elapsed_nsecs / one_billion;
510    nsec = elapsed_nsecs % one_billion;
511}
512
513//////////////////////////////////////////////////////////////////////
514//
515// The following emulation functions are generic, but need to be
516// templated to account for differences in types, constants, etc.
517//
518//////////////////////////////////////////////////////////////////////
519
520    typedef struct statfs hst_statfs;
521#if NO_STAT64
522    typedef struct stat hst_stat;
523    typedef struct stat hst_stat64;
524#else
525    typedef struct stat hst_stat;
526    typedef struct stat64 hst_stat64;
527#endif
528
529//// Helper function to convert a host stat buffer to a target stat
530//// buffer.  Also copies the target buffer out to the simulated
531//// memory space.  Used by stat(), fstat(), and lstat().
532
533template <typename target_stat, typename host_stat>
534void
535convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
536{
537    using namespace TheISA;
538
539    if (fakeTTY)
540        tgt->st_dev = 0xA;
541    else
542        tgt->st_dev = host->st_dev;
543    tgt->st_dev = TheISA::htog(tgt->st_dev);
544    tgt->st_ino = host->st_ino;
545    tgt->st_ino = TheISA::htog(tgt->st_ino);
546    tgt->st_mode = host->st_mode;
547    if (fakeTTY) {
548        // Claim to be a character device
549        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
550        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
551    }
552    tgt->st_mode = TheISA::htog(tgt->st_mode);
553    tgt->st_nlink = host->st_nlink;
554    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
555    tgt->st_uid = host->st_uid;
556    tgt->st_uid = TheISA::htog(tgt->st_uid);
557    tgt->st_gid = host->st_gid;
558    tgt->st_gid = TheISA::htog(tgt->st_gid);
559    if (fakeTTY)
560        tgt->st_rdev = 0x880d;
561    else
562        tgt->st_rdev = host->st_rdev;
563    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
564    tgt->st_size = host->st_size;
565    tgt->st_size = TheISA::htog(tgt->st_size);
566    tgt->st_atimeX = host->st_atime;
567    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
568    tgt->st_mtimeX = host->st_mtime;
569    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
570    tgt->st_ctimeX = host->st_ctime;
571    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
572    // Force the block size to be 8KB. This helps to ensure buffered io works
573    // consistently across different hosts.
574    tgt->st_blksize = 0x2000;
575    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
576    tgt->st_blocks = host->st_blocks;
577    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
578}
579
580// Same for stat64
581
582template <typename target_stat, typename host_stat64>
583void
584convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
585{
586    using namespace TheISA;
587
588    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
589#if defined(STAT_HAVE_NSEC)
590    tgt->st_atime_nsec = host->st_atime_nsec;
591    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
592    tgt->st_mtime_nsec = host->st_mtime_nsec;
593    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
594    tgt->st_ctime_nsec = host->st_ctime_nsec;
595    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
596#else
597    tgt->st_atime_nsec = 0;
598    tgt->st_mtime_nsec = 0;
599    tgt->st_ctime_nsec = 0;
600#endif
601}
602
603// Here are a couple of convenience functions
604template<class OS>
605void
606copyOutStatBuf(PortProxy &mem, Addr addr,
607               hst_stat *host, bool fakeTTY = false)
608{
609    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
610    tgt_stat_buf tgt(addr);
611    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
612    tgt.copyOut(mem);
613}
614
615template<class OS>
616void
617copyOutStat64Buf(PortProxy &mem, Addr addr,
618                 hst_stat64 *host, bool fakeTTY = false)
619{
620    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
621    tgt_stat_buf tgt(addr);
622    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
623    tgt.copyOut(mem);
624}
625
626template <class OS>
627void
628copyOutStatfsBuf(PortProxy &mem, Addr addr,
629                 hst_statfs *host)
630{
631    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
632
633    tgt->f_type = TheISA::htog(host->f_type);
634#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
635    tgt->f_bsize = TheISA::htog(host->f_iosize);
636#else
637    tgt->f_bsize = TheISA::htog(host->f_bsize);
638#endif
639    tgt->f_blocks = TheISA::htog(host->f_blocks);
640    tgt->f_bfree = TheISA::htog(host->f_bfree);
641    tgt->f_bavail = TheISA::htog(host->f_bavail);
642    tgt->f_files = TheISA::htog(host->f_files);
643    tgt->f_ffree = TheISA::htog(host->f_ffree);
644    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
645#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
646    tgt->f_namelen = TheISA::htog(host->f_namemax);
647    tgt->f_frsize = TheISA::htog(host->f_bsize);
648#elif defined(__APPLE__)
649    tgt->f_namelen = 0;
650    tgt->f_frsize = 0;
651#else
652    tgt->f_namelen = TheISA::htog(host->f_namelen);
653    tgt->f_frsize = TheISA::htog(host->f_frsize);
654#endif
655#if defined(__linux__)
656    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
657#else
658    /*
659     * The fields are different sizes per OS. Don't bother with
660     * f_spare or f_reserved on non-Linux for now.
661     */
662    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
663#endif
664
665    tgt.copyOut(mem);
666}
667
668/// Target ioctl() handler.  For the most part, programs call ioctl()
669/// only to find out if their stdout is a tty, to determine whether to
670/// do line or block buffering.  We always claim that output fds are
671/// not TTYs to provide repeatable results.
672template <class OS>
673SyscallReturn
674ioctlFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
675{
676    int index = 0;
677    auto p = tc->getProcessPtr();
678
679    int tgt_fd = p->getSyscallArg(tc, index);
680    unsigned req = p->getSyscallArg(tc, index);
681
682    DPRINTF_SYSCALL(Verbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
683
684    if (OS::isTtyReq(req))
685        return -ENOTTY;
686
687    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
688    if (dfdp) {
689        EmulatedDriver *emul_driver = dfdp->getDriver();
690        if (emul_driver)
691            return emul_driver->ioctl(tc, req);
692    }
693
694    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
695    if (sfdp) {
696        int status;
697
698        switch (req) {
699          case SIOCGIFCONF: {
700            Addr conf_addr = p->getSyscallArg(tc, index);
701            BufferArg conf_arg(conf_addr, sizeof(ifconf));
702            conf_arg.copyIn(tc->getMemProxy());
703
704            ifconf *conf = (ifconf*)conf_arg.bufferPtr();
705            Addr ifc_buf_addr = (Addr)conf->ifc_buf;
706            BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len);
707            ifc_buf_arg.copyIn(tc->getMemProxy());
708
709            conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr();
710
711            status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr());
712            if (status != -1) {
713                conf->ifc_buf = (char*)ifc_buf_addr;
714                ifc_buf_arg.copyOut(tc->getMemProxy());
715                conf_arg.copyOut(tc->getMemProxy());
716            }
717
718            return status;
719          }
720          case SIOCGIFFLAGS:
721#if defined(__linux__)
722          case SIOCGIFINDEX:
723#endif
724          case SIOCGIFNETMASK:
725          case SIOCGIFADDR:
726#if defined(__linux__)
727          case SIOCGIFHWADDR:
728#endif
729          case SIOCGIFMTU: {
730            Addr req_addr = p->getSyscallArg(tc, index);
731            BufferArg req_arg(req_addr, sizeof(ifreq));
732            req_arg.copyIn(tc->getMemProxy());
733
734            status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr());
735            if (status != -1)
736                req_arg.copyOut(tc->getMemProxy());
737            return status;
738          }
739        }
740    }
741
742    /**
743     * For lack of a better return code, return ENOTTY. Ideally, we should
744     * return something better here, but at least we issue the warning.
745     */
746    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
747         tgt_fd, req, tc->pcState());
748    return -ENOTTY;
749}
750
751template <class OS>
752SyscallReturn
753openImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool isopenat)
754{
755    int index = 0;
756    auto p = tc->getProcessPtr();
757    int tgt_dirfd = -1;
758
759    /**
760     * If using the openat variant, read in the target directory file
761     * descriptor from the simulated process.
762     */
763    if (isopenat)
764        tgt_dirfd = p->getSyscallArg(tc, index);
765
766    /**
767     * Retrieve the simulated process' memory proxy and then read in the path
768     * string from that memory space into the host's working memory space.
769     */
770    std::string path;
771    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
772        return -EFAULT;
773
774#ifdef __CYGWIN32__
775    int host_flags = O_BINARY;
776#else
777    int host_flags = 0;
778#endif
779    /**
780     * Translate target flags into host flags. Flags exist which are not
781     * ported between architectures which can cause check failures.
782     */
783    int tgt_flags = p->getSyscallArg(tc, index);
784    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
785        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
786            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
787            host_flags |= OS::openFlagTable[i].hostFlag;
788        }
789    }
790    if (tgt_flags) {
791        warn("open%s: cannot decode flags 0x%x",
792             isopenat ? "at" : "", tgt_flags);
793    }
794#ifdef __CYGWIN32__
795    host_flags |= O_BINARY;
796#endif
797
798    int mode = p->getSyscallArg(tc, index);
799
800    /**
801     * If the simulated process called open or openat with AT_FDCWD specified,
802     * take the current working directory value which was passed into the
803     * process class as a Python parameter and append the current path to
804     * create a full path.
805     * Otherwise, openat with a valid target directory file descriptor has
806     * been called. If the path option, which was passed in as a parameter,
807     * is not absolute, retrieve the directory file descriptor's path and
808     * prepend it to the path passed in as a parameter.
809     * In every case, we should have a full path (which is relevant to the
810     * host) to work with after this block has been passed.
811     */
812    std::string redir_path = path;
813    std::string abs_path = path;
814    if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) {
815        abs_path = p->absolutePath(path, true);
816        redir_path = p->checkPathRedirect(path);
817    } else if (!startswith(path, "/")) {
818        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
819        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
820        if (!ffdp)
821            return -EBADF;
822        abs_path = ffdp->getFileName() + path;
823        redir_path = p->checkPathRedirect(abs_path);
824    }
825
826    /**
827     * Since this is an emulated environment, we create pseudo file
828     * descriptors for device requests that have been registered with
829     * the process class through Python; this allows us to create a file
830     * descriptor for subsequent ioctl or mmap calls.
831     */
832    if (startswith(abs_path, "/dev/")) {
833        std::string filename = abs_path.substr(strlen("/dev/"));
834        EmulatedDriver *drv = p->findDriver(filename);
835        if (drv) {
836            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
837                            "driver open with path[%s]\n",
838                            isopenat ? "at" : "", abs_path.c_str());
839            return drv->open(tc, mode, host_flags);
840        }
841        /**
842         * Fall through here for pass through to host devices, such
843         * as /dev/zero
844         */
845    }
846
847    /**
848     * We make several attempts resolve a call to open.
849     *
850     * 1) Resolve any path redirection before hand. This will set the path
851     * up with variable 'redir_path' which may contain a modified path or
852     * the original path value. This should already be done in prior code.
853     * 2) Try to handle the access using 'special_paths'. Some special_paths
854     * and files cannot be called on the host and need to be handled as
855     * special cases inside the simulator. These special_paths are handled by
856     * C++ routines to provide output back to userspace.
857     * 3) If the full path that was created above does not match any of the
858     * special cases, pass it through to the open call on the __HOST__ to let
859     * the host open the file on our behalf. Again, the openImpl tries to
860     * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the
861     * faux-filesystem files). The faux-filesystem is dynamically created
862     * during simulator configuration using Python functions.
863     * 4) If the host cannot open the file, the open attempt failed in "3)".
864     * Return the host's error code back through the system call to the
865     * simulated process. If running a debug trace, also notify the user that
866     * the open call failed.
867     *
868     * Any success will set sim_fd to something other than -1 and skip the
869     * next conditions effectively bypassing them.
870     */
871    int sim_fd = -1;
872    std::string used_path;
873    std::vector<std::string> special_paths =
874            { "/proc/meminfo/", "/system/", "/platform/", "/etc/passwd" };
875    for (auto entry : special_paths) {
876        if (startswith(path, entry)) {
877            sim_fd = OS::openSpecialFile(abs_path, p, tc);
878            used_path = abs_path;
879        }
880    }
881    if (sim_fd == -1) {
882        sim_fd = open(redir_path.c_str(), host_flags, mode);
883        used_path = redir_path;
884    }
885    if (sim_fd == -1) {
886        int local = -errno;
887        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s "
888                        "(inferred from:%s)\n", isopenat ? "at" : "",
889                        used_path.c_str(), path.c_str());
890        return local;
891    }
892
893    /**
894     * The file was opened successfully and needs to be recorded in the
895     * process' file descriptor array so that it can be retrieved later.
896     * The target file descriptor that is chosen will be the lowest unused
897     * file descriptor.
898     * Return the indirect target file descriptor back to the simulated
899     * process to act as a handle for the opened file.
900     */
901    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
902    int tgt_fd = p->fds->allocFD(ffdp);
903    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n"
904                    "(inferred from:%s)\n", isopenat ? "at" : "",
905                    sim_fd, tgt_fd, used_path.c_str(), path.c_str());
906    return tgt_fd;
907}
908
909/// Target open() handler.
910template <class OS>
911SyscallReturn
912openFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
913{
914    return openImpl<OS>(desc, callnum, tc, false);
915}
916
917/// Target openat() handler.
918template <class OS>
919SyscallReturn
920openatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
921{
922    return openImpl<OS>(desc, callnum, tc, true);
923}
924
925/// Target unlinkat() handler.
926template <class OS>
927SyscallReturn
928unlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
929{
930    int index = 0;
931    auto process = tc->getProcessPtr();
932    int dirfd = process->getSyscallArg(tc, index);
933    if (dirfd != OS::TGT_AT_FDCWD)
934        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
935
936    return unlinkHelper(desc, callnum, tc, 1);
937}
938
939/// Target facessat() handler
940template <class OS>
941SyscallReturn
942faccessatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
943{
944    int index = 0;
945    auto process = tc->getProcessPtr();
946    int dirfd = process->getSyscallArg(tc, index);
947    if (dirfd != OS::TGT_AT_FDCWD)
948        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
949    return accessFunc(desc, callnum, tc, 1);
950}
951
952/// Target readlinkat() handler
953template <class OS>
954SyscallReturn
955readlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
956{
957    int index = 0;
958    auto process = tc->getProcessPtr();
959    int dirfd = process->getSyscallArg(tc, index);
960    if (dirfd != OS::TGT_AT_FDCWD)
961        warn("openat: first argument not AT_FDCWD; unlikely to work");
962    return readlinkFunc(desc, callnum, tc, 1);
963}
964
965/// Target renameat() handler.
966template <class OS>
967SyscallReturn
968renameatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
969{
970    int index = 0;
971    auto process = tc->getProcessPtr();
972
973    int olddirfd = process->getSyscallArg(tc, index);
974    if (olddirfd != OS::TGT_AT_FDCWD)
975        warn("renameat: first argument not AT_FDCWD; unlikely to work");
976
977    std::string old_name;
978
979    if (!tc->getMemProxy().tryReadString(old_name,
980                                         process->getSyscallArg(tc, index)))
981        return -EFAULT;
982
983    int newdirfd = process->getSyscallArg(tc, index);
984    if (newdirfd != OS::TGT_AT_FDCWD)
985        warn("renameat: third argument not AT_FDCWD; unlikely to work");
986
987    std::string new_name;
988
989    if (!tc->getMemProxy().tryReadString(new_name,
990                                         process->getSyscallArg(tc, index)))
991        return -EFAULT;
992
993    // Adjust path for cwd and redirection
994    old_name = process->checkPathRedirect(old_name);
995    new_name = process->checkPathRedirect(new_name);
996
997    int result = rename(old_name.c_str(), new_name.c_str());
998    return (result == -1) ? -errno : result;
999}
1000
1001/// Target sysinfo() handler.
1002template <class OS>
1003SyscallReturn
1004sysinfoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1005{
1006    int index = 0;
1007    auto process = tc->getProcessPtr();
1008
1009    TypedBufferArg<typename OS::tgt_sysinfo>
1010        sysinfo(process->getSyscallArg(tc, index));
1011
1012    sysinfo->uptime = seconds_since_epoch;
1013    sysinfo->totalram = process->system->memSize();
1014    sysinfo->mem_unit = 1;
1015
1016    sysinfo.copyOut(tc->getMemProxy());
1017
1018    return 0;
1019}
1020
1021/// Target chmod() handler.
1022template <class OS>
1023SyscallReturn
1024chmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1025{
1026    std::string path;
1027    auto process = tc->getProcessPtr();
1028
1029    int index = 0;
1030    if (!tc->getMemProxy().tryReadString(path,
1031                process->getSyscallArg(tc, index))) {
1032        return -EFAULT;
1033    }
1034
1035    uint32_t mode = process->getSyscallArg(tc, index);
1036    mode_t hostMode = 0;
1037
1038    // XXX translate mode flags via OS::something???
1039    hostMode = mode;
1040
1041    // Adjust path for cwd and redirection
1042    path = process->checkPathRedirect(path);
1043
1044    // do the chmod
1045    int result = chmod(path.c_str(), hostMode);
1046    if (result < 0)
1047        return -errno;
1048
1049    return 0;
1050}
1051
1052template <class OS>
1053SyscallReturn
1054pollFunc(SyscallDesc *desc, int num, ThreadContext *tc)
1055{
1056    int index = 0;
1057    auto p = tc->getProcessPtr();
1058    Addr fdsPtr = p->getSyscallArg(tc, index);
1059    int nfds = p->getSyscallArg(tc, index);
1060    int tmout = p->getSyscallArg(tc, index);
1061
1062    BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds);
1063    fdsBuf.copyIn(tc->getMemProxy());
1064
1065    /**
1066     * Record the target file descriptors in a local variable. We need to
1067     * replace them with host file descriptors but we need a temporary copy
1068     * for later. Afterwards, replace each target file descriptor in the
1069     * poll_fd array with its host_fd.
1070     */
1071    int temp_tgt_fds[nfds];
1072    for (index = 0; index < nfds; index++) {
1073        temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd;
1074        auto tgt_fd = temp_tgt_fds[index];
1075        auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
1076        if (!hbfdp)
1077            return -EBADF;
1078        auto host_fd = hbfdp->getSimFD();
1079        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd;
1080    }
1081
1082    /**
1083     * We cannot allow an infinite poll to occur or it will inevitably cause
1084     * a deadlock in the gem5 simulator with clone. We must pass in tmout with
1085     * a non-negative value, however it also makes no sense to poll on the
1086     * underlying host for any other time than tmout a zero timeout.
1087     */
1088    int status;
1089    if (tmout < 0) {
1090        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
1091        if (status == 0) {
1092            /**
1093             * If blocking indefinitely, check the signal list to see if a
1094             * signal would break the poll out of the retry cycle and try
1095             * to return the signal interrupt instead.
1096             */
1097            System *sysh = tc->getSystemPtr();
1098            std::list<BasicSignal>::iterator it;
1099            for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++)
1100                if (it->receiver == p)
1101                    return -EINTR;
1102            return SyscallReturn::retry();
1103        }
1104    } else
1105        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
1106
1107    if (status == -1)
1108        return -errno;
1109
1110    /**
1111     * Replace each host_fd in the returned poll_fd array with its original
1112     * target file descriptor.
1113     */
1114    for (index = 0; index < nfds; index++) {
1115        auto tgt_fd = temp_tgt_fds[index];
1116        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd;
1117    }
1118
1119    /**
1120     * Copy out the pollfd struct because the host may have updated fields
1121     * in the structure.
1122     */
1123    fdsBuf.copyOut(tc->getMemProxy());
1124
1125    return status;
1126}
1127
1128/// Target fchmod() handler.
1129template <class OS>
1130SyscallReturn
1131fchmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1132{
1133    int index = 0;
1134    auto p = tc->getProcessPtr();
1135    int tgt_fd = p->getSyscallArg(tc, index);
1136    uint32_t mode = p->getSyscallArg(tc, index);
1137
1138    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1139    if (!ffdp)
1140        return -EBADF;
1141    int sim_fd = ffdp->getSimFD();
1142
1143    mode_t hostMode = mode;
1144
1145    int result = fchmod(sim_fd, hostMode);
1146
1147    return (result < 0) ? -errno : 0;
1148}
1149
1150/// Target mremap() handler.
1151template <class OS>
1152SyscallReturn
1153mremapFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1154{
1155    int index = 0;
1156    auto process = tc->getProcessPtr();
1157    Addr start = process->getSyscallArg(tc, index);
1158    uint64_t old_length = process->getSyscallArg(tc, index);
1159    uint64_t new_length = process->getSyscallArg(tc, index);
1160    uint64_t flags = process->getSyscallArg(tc, index);
1161    uint64_t provided_address = 0;
1162    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
1163
1164    if (use_provided_address)
1165        provided_address = process->getSyscallArg(tc, index);
1166
1167    if ((start % TheISA::PageBytes != 0) ||
1168        (provided_address % TheISA::PageBytes != 0)) {
1169        warn("mremap failing: arguments not page aligned");
1170        return -EINVAL;
1171    }
1172
1173    new_length = roundUp(new_length, TheISA::PageBytes);
1174
1175    if (new_length > old_length) {
1176        std::shared_ptr<MemState> mem_state = process->memState;
1177        Addr mmap_end = mem_state->getMmapEnd();
1178
1179        if ((start + old_length) == mmap_end &&
1180            (!use_provided_address || provided_address == start)) {
1181            // This case cannot occur when growing downward, as
1182            // start is greater than or equal to mmap_end.
1183            uint64_t diff = new_length - old_length;
1184            process->allocateMem(mmap_end, diff);
1185            mem_state->setMmapEnd(mmap_end + diff);
1186            return start;
1187        } else {
1188            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
1189                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
1190                return -ENOMEM;
1191            } else {
1192                uint64_t new_start = provided_address;
1193                if (!use_provided_address) {
1194                    new_start = process->mmapGrowsDown() ?
1195                                mmap_end - new_length : mmap_end;
1196                    mmap_end = process->mmapGrowsDown() ?
1197                               new_start : mmap_end + new_length;
1198                    mem_state->setMmapEnd(mmap_end);
1199                }
1200
1201                process->pTable->remap(start, old_length, new_start);
1202                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
1203                     new_start, new_start + new_length,
1204                     new_length - old_length);
1205                // add on the remaining unallocated pages
1206                process->allocateMem(new_start + old_length,
1207                                     new_length - old_length,
1208                                     use_provided_address /* clobber */);
1209                if (use_provided_address &&
1210                    ((new_start + new_length > mem_state->getMmapEnd() &&
1211                      !process->mmapGrowsDown()) ||
1212                    (new_start < mem_state->getMmapEnd() &&
1213                      process->mmapGrowsDown()))) {
1214                    // something fishy going on here, at least notify the user
1215                    // @todo: increase mmap_end?
1216                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
1217                }
1218                warn("returning %08p as start\n", new_start);
1219                return new_start;
1220            }
1221        }
1222    } else {
1223        if (use_provided_address && provided_address != start)
1224            process->pTable->remap(start, new_length, provided_address);
1225        process->pTable->unmap(start + new_length, old_length - new_length);
1226        return use_provided_address ? provided_address : start;
1227    }
1228}
1229
1230/// Target stat() handler.
1231template <class OS>
1232SyscallReturn
1233statFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1234{
1235    std::string path;
1236    auto process = tc->getProcessPtr();
1237
1238    int index = 0;
1239    if (!tc->getMemProxy().tryReadString(path,
1240                process->getSyscallArg(tc, index))) {
1241        return -EFAULT;
1242    }
1243    Addr bufPtr = process->getSyscallArg(tc, index);
1244
1245    // Adjust path for cwd and redirection
1246    path = process->checkPathRedirect(path);
1247
1248    struct stat hostBuf;
1249    int result = stat(path.c_str(), &hostBuf);
1250
1251    if (result < 0)
1252        return -errno;
1253
1254    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1255
1256    return 0;
1257}
1258
1259
1260/// Target stat64() handler.
1261template <class OS>
1262SyscallReturn
1263stat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
1264{
1265    std::string path;
1266    auto process = tc->getProcessPtr();
1267
1268    int index = 0;
1269    if (!tc->getMemProxy().tryReadString(path,
1270                process->getSyscallArg(tc, index)))
1271        return -EFAULT;
1272    Addr bufPtr = process->getSyscallArg(tc, index);
1273
1274    // Adjust path for cwd and redirection
1275    path = process->checkPathRedirect(path);
1276
1277#if NO_STAT64
1278    struct stat  hostBuf;
1279    int result = stat(path.c_str(), &hostBuf);
1280#else
1281    struct stat64 hostBuf;
1282    int result = stat64(path.c_str(), &hostBuf);
1283#endif
1284
1285    if (result < 0)
1286        return -errno;
1287
1288    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1289
1290    return 0;
1291}
1292
1293
1294/// Target fstatat64() handler.
1295template <class OS>
1296SyscallReturn
1297fstatat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
1298{
1299    int index = 0;
1300    auto process = tc->getProcessPtr();
1301    int dirfd = process->getSyscallArg(tc, index);
1302    if (dirfd != OS::TGT_AT_FDCWD)
1303        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
1304
1305    std::string path;
1306    if (!tc->getMemProxy().tryReadString(path,
1307                process->getSyscallArg(tc, index)))
1308        return -EFAULT;
1309    Addr bufPtr = process->getSyscallArg(tc, index);
1310
1311    // Adjust path for cwd and redirection
1312    path = process->checkPathRedirect(path);
1313
1314#if NO_STAT64
1315    struct stat  hostBuf;
1316    int result = stat(path.c_str(), &hostBuf);
1317#else
1318    struct stat64 hostBuf;
1319    int result = stat64(path.c_str(), &hostBuf);
1320#endif
1321
1322    if (result < 0)
1323        return -errno;
1324
1325    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1326
1327    return 0;
1328}
1329
1330
1331/// Target fstat64() handler.
1332template <class OS>
1333SyscallReturn
1334fstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
1335{
1336    int index = 0;
1337    auto p = tc->getProcessPtr();
1338    int tgt_fd = p->getSyscallArg(tc, index);
1339    Addr bufPtr = p->getSyscallArg(tc, index);
1340
1341    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1342    if (!ffdp)
1343        return -EBADF;
1344    int sim_fd = ffdp->getSimFD();
1345
1346#if NO_STAT64
1347    struct stat  hostBuf;
1348    int result = fstat(sim_fd, &hostBuf);
1349#else
1350    struct stat64  hostBuf;
1351    int result = fstat64(sim_fd, &hostBuf);
1352#endif
1353
1354    if (result < 0)
1355        return -errno;
1356
1357    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1358
1359    return 0;
1360}
1361
1362
1363/// Target lstat() handler.
1364template <class OS>
1365SyscallReturn
1366lstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1367{
1368    std::string path;
1369    auto process = tc->getProcessPtr();
1370
1371    int index = 0;
1372    if (!tc->getMemProxy().tryReadString(path,
1373                process->getSyscallArg(tc, index))) {
1374        return -EFAULT;
1375    }
1376    Addr bufPtr = process->getSyscallArg(tc, index);
1377
1378    // Adjust path for cwd and redirection
1379    path = process->checkPathRedirect(path);
1380
1381    struct stat hostBuf;
1382    int result = lstat(path.c_str(), &hostBuf);
1383
1384    if (result < 0)
1385        return -errno;
1386
1387    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1388
1389    return 0;
1390}
1391
1392/// Target lstat64() handler.
1393template <class OS>
1394SyscallReturn
1395lstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
1396{
1397    std::string path;
1398    auto process = tc->getProcessPtr();
1399
1400    int index = 0;
1401    if (!tc->getMemProxy().tryReadString(path,
1402                process->getSyscallArg(tc, index))) {
1403        return -EFAULT;
1404    }
1405    Addr bufPtr = process->getSyscallArg(tc, index);
1406
1407    // Adjust path for cwd and redirection
1408    path = process->checkPathRedirect(path);
1409
1410#if NO_STAT64
1411    struct stat hostBuf;
1412    int result = lstat(path.c_str(), &hostBuf);
1413#else
1414    struct stat64 hostBuf;
1415    int result = lstat64(path.c_str(), &hostBuf);
1416#endif
1417
1418    if (result < 0)
1419        return -errno;
1420
1421    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1422
1423    return 0;
1424}
1425
1426/// Target fstat() handler.
1427template <class OS>
1428SyscallReturn
1429fstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1430{
1431    int index = 0;
1432    auto p = tc->getProcessPtr();
1433    int tgt_fd = p->getSyscallArg(tc, index);
1434    Addr bufPtr = p->getSyscallArg(tc, index);
1435
1436    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1437
1438    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1439    if (!ffdp)
1440        return -EBADF;
1441    int sim_fd = ffdp->getSimFD();
1442
1443    struct stat hostBuf;
1444    int result = fstat(sim_fd, &hostBuf);
1445
1446    if (result < 0)
1447        return -errno;
1448
1449    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1450
1451    return 0;
1452}
1453
1454/// Target statfs() handler.
1455template <class OS>
1456SyscallReturn
1457statfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1458{
1459#if defined(__linux__)
1460    std::string path;
1461    auto process = tc->getProcessPtr();
1462
1463    int index = 0;
1464    if (!tc->getMemProxy().tryReadString(path,
1465                process->getSyscallArg(tc, index))) {
1466        return -EFAULT;
1467    }
1468    Addr bufPtr = process->getSyscallArg(tc, index);
1469
1470    // Adjust path for cwd and redirection
1471    path = process->checkPathRedirect(path);
1472
1473    struct statfs hostBuf;
1474    int result = statfs(path.c_str(), &hostBuf);
1475
1476    if (result < 0)
1477        return -errno;
1478
1479    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1480    return 0;
1481#else
1482    warnUnsupportedOS("statfs");
1483    return -1;
1484#endif
1485}
1486
1487template <class OS>
1488SyscallReturn
1489cloneFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1490{
1491    int index = 0;
1492
1493    auto p = tc->getProcessPtr();
1494    RegVal flags = p->getSyscallArg(tc, index);
1495    RegVal newStack = p->getSyscallArg(tc, index);
1496    Addr ptidPtr = p->getSyscallArg(tc, index);
1497
1498#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
1499    /**
1500     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
1501     * The flag defines the list of clone() arguments in the following
1502     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
1503     */
1504    Addr tlsPtr = p->getSyscallArg(tc, index);
1505    Addr ctidPtr = p->getSyscallArg(tc, index);
1506#else
1507    Addr ctidPtr = p->getSyscallArg(tc, index);
1508    Addr tlsPtr = p->getSyscallArg(tc, index);
1509#endif
1510
1511    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
1512        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
1513        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
1514        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
1515        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
1516        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
1517        return -EINVAL;
1518
1519    ThreadContext *ctc;
1520    if (!(ctc = p->findFreeContext())) {
1521        DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system"
1522                        "[cpu %d, thread %d]", tc->cpuId(), tc->threadId());
1523        return -EAGAIN;
1524    }
1525
1526    /**
1527     * Note that ProcessParams is generated by swig and there are no other
1528     * examples of how to create anything but this default constructor. The
1529     * fields are manually initialized instead of passing parameters to the
1530     * constructor.
1531     */
1532    ProcessParams *pp = new ProcessParams();
1533    pp->executable.assign(*(new std::string(p->progName())));
1534    pp->cmd.push_back(*(new std::string(p->progName())));
1535    pp->system = p->system;
1536    pp->cwd.assign(p->tgtCwd);
1537    pp->input.assign("stdin");
1538    pp->output.assign("stdout");
1539    pp->errout.assign("stderr");
1540    pp->uid = p->uid();
1541    pp->euid = p->euid();
1542    pp->gid = p->gid();
1543    pp->egid = p->egid();
1544
1545    /* Find the first free PID that's less than the maximum */
1546    std::set<int> const& pids = p->system->PIDs;
1547    int temp_pid = *pids.begin();
1548    do {
1549        temp_pid++;
1550    } while (pids.find(temp_pid) != pids.end());
1551    if (temp_pid >= System::maxPID)
1552        fatal("temp_pid is too large: %d", temp_pid);
1553
1554    pp->pid = temp_pid;
1555    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
1556    pp->useArchPT = p->useArchPT;
1557    pp->kvmInSE = p->kvmInSE;
1558    Process *cp = pp->create();
1559    delete pp;
1560
1561    Process *owner = ctc->getProcessPtr();
1562    ctc->setProcessPtr(cp);
1563    cp->assignThreadContext(ctc->contextId());
1564    owner->revokeThreadContext(ctc->contextId());
1565
1566    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
1567        BufferArg ptidBuf(ptidPtr, sizeof(long));
1568        long *ptid = (long *)ptidBuf.bufferPtr();
1569        *ptid = cp->pid();
1570        ptidBuf.copyOut(tc->getMemProxy());
1571    }
1572
1573    if (flags & OS::TGT_CLONE_THREAD) {
1574        cp->pTable->shared = true;
1575        cp->useForClone = true;
1576    }
1577    cp->initState();
1578    p->clone(tc, ctc, cp, flags);
1579
1580    if (flags & OS::TGT_CLONE_THREAD) {
1581        delete cp->sigchld;
1582        cp->sigchld = p->sigchld;
1583    } else if (flags & OS::TGT_SIGCHLD) {
1584        *cp->sigchld = true;
1585    }
1586
1587    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
1588        BufferArg ctidBuf(ctidPtr, sizeof(long));
1589        long *ctid = (long *)ctidBuf.bufferPtr();
1590        *ctid = cp->pid();
1591        ctidBuf.copyOut(ctc->getMemProxy());
1592    }
1593
1594    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
1595        cp->childClearTID = (uint64_t)ctidPtr;
1596
1597    ctc->clearArchRegs();
1598
1599    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
1600
1601    cp->setSyscallReturn(ctc, 0);
1602
1603#if THE_ISA == ALPHA_ISA
1604    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
1605#elif THE_ISA == SPARC_ISA
1606    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
1607    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
1608#endif
1609
1610    if (p->kvmInSE) {
1611#if THE_ISA == X86_ISA
1612        ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX));
1613#else
1614        panic("KVM CPU model is not supported for this ISA");
1615#endif
1616    } else {
1617        TheISA::PCState cpc = tc->pcState();
1618        cpc.advance();
1619        ctc->pcState(cpc);
1620    }
1621    ctc->activate();
1622
1623    return cp->pid();
1624}
1625
1626/// Target fstatfs() handler.
1627template <class OS>
1628SyscallReturn
1629fstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1630{
1631    int index = 0;
1632    auto p = tc->getProcessPtr();
1633    int tgt_fd = p->getSyscallArg(tc, index);
1634    Addr bufPtr = p->getSyscallArg(tc, index);
1635
1636    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1637    if (!ffdp)
1638        return -EBADF;
1639    int sim_fd = ffdp->getSimFD();
1640
1641    struct statfs hostBuf;
1642    int result = fstatfs(sim_fd, &hostBuf);
1643
1644    if (result < 0)
1645        return -errno;
1646
1647    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1648
1649    return 0;
1650}
1651
1652/// Target readv() handler.
1653template <class OS>
1654SyscallReturn
1655readvFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1656{
1657    int index = 0;
1658    auto p = tc->getProcessPtr();
1659    int tgt_fd = p->getSyscallArg(tc, index);
1660
1661    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1662    if (!ffdp)
1663        return -EBADF;
1664    int sim_fd = ffdp->getSimFD();
1665
1666    PortProxy &prox = tc->getMemProxy();
1667    uint64_t tiov_base = p->getSyscallArg(tc, index);
1668    size_t count = p->getSyscallArg(tc, index);
1669    typename OS::tgt_iovec tiov[count];
1670    struct iovec hiov[count];
1671    for (size_t i = 0; i < count; ++i) {
1672        prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)),
1673                      &tiov[i], sizeof(typename OS::tgt_iovec));
1674        hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len);
1675        hiov[i].iov_base = new char [hiov[i].iov_len];
1676    }
1677
1678    int result = readv(sim_fd, hiov, count);
1679    int local_errno = errno;
1680
1681    for (size_t i = 0; i < count; ++i) {
1682        if (result != -1) {
1683            prox.writeBlob(TheISA::htog(tiov[i].iov_base),
1684                           hiov[i].iov_base, hiov[i].iov_len);
1685        }
1686        delete [] (char *)hiov[i].iov_base;
1687    }
1688
1689    return (result == -1) ? -local_errno : result;
1690}
1691
1692/// Target writev() handler.
1693template <class OS>
1694SyscallReturn
1695writevFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1696{
1697    int index = 0;
1698    auto p = tc->getProcessPtr();
1699    int tgt_fd = p->getSyscallArg(tc, index);
1700
1701    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
1702    if (!hbfdp)
1703        return -EBADF;
1704    int sim_fd = hbfdp->getSimFD();
1705
1706    PortProxy &prox = tc->getMemProxy();
1707    uint64_t tiov_base = p->getSyscallArg(tc, index);
1708    size_t count = p->getSyscallArg(tc, index);
1709    struct iovec hiov[count];
1710    for (size_t i = 0; i < count; ++i) {
1711        typename OS::tgt_iovec tiov;
1712
1713        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1714                      &tiov, sizeof(typename OS::tgt_iovec));
1715        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1716        hiov[i].iov_base = new char [hiov[i].iov_len];
1717        prox.readBlob(TheISA::gtoh(tiov.iov_base), hiov[i].iov_base,
1718                      hiov[i].iov_len);
1719    }
1720
1721    int result = writev(sim_fd, hiov, count);
1722
1723    for (size_t i = 0; i < count; ++i)
1724        delete [] (char *)hiov[i].iov_base;
1725
1726    return (result == -1) ? -errno : result;
1727}
1728
1729/// Real mmap handler.
1730template <class OS>
1731SyscallReturn
1732mmapImpl(SyscallDesc *desc, int num, ThreadContext *tc, bool is_mmap2)
1733{
1734    int index = 0;
1735    auto p = tc->getProcessPtr();
1736    Addr start = p->getSyscallArg(tc, index);
1737    uint64_t length = p->getSyscallArg(tc, index);
1738    int prot = p->getSyscallArg(tc, index);
1739    int tgt_flags = p->getSyscallArg(tc, index);
1740    int tgt_fd = p->getSyscallArg(tc, index);
1741    int offset = p->getSyscallArg(tc, index);
1742
1743    if (is_mmap2)
1744        offset *= TheISA::PageBytes;
1745
1746    if (start & (TheISA::PageBytes - 1) ||
1747        offset & (TheISA::PageBytes - 1) ||
1748        (tgt_flags & OS::TGT_MAP_PRIVATE &&
1749         tgt_flags & OS::TGT_MAP_SHARED) ||
1750        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
1751         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
1752        !length) {
1753        return -EINVAL;
1754    }
1755
1756    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
1757        // With shared mmaps, there are two cases to consider:
1758        // 1) anonymous: writes should modify the mapping and this should be
1759        // visible to observers who share the mapping. Currently, it's
1760        // difficult to update the shared mapping because there's no
1761        // structure which maintains information about the which virtual
1762        // memory areas are shared. If that structure existed, it would be
1763        // possible to make the translations point to the same frames.
1764        // 2) file-backed: writes should modify the mapping and the file
1765        // which is backed by the mapping. The shared mapping problem is the
1766        // same as what was mentioned about the anonymous mappings. For
1767        // file-backed mappings, the writes to the file are difficult
1768        // because it requires syncing what the mapping holds with the file
1769        // that resides on the host system. So, any write on a real system
1770        // would cause the change to be propagated to the file mapping at
1771        // some point in the future (the inode is tracked along with the
1772        // mapping). This isn't guaranteed to always happen, but it usually
1773        // works well enough. The guarantee is provided by the msync system
1774        // call. We could force the change through with shared mappings with
1775        // a call to msync, but that again would require more information
1776        // than we currently maintain.
1777        warn("mmap: writing to shared mmap region is currently "
1778             "unsupported. The write succeeds on the target, but it "
1779             "will not be propagated to the host or shared mappings");
1780    }
1781
1782    length = roundUp(length, TheISA::PageBytes);
1783
1784    int sim_fd = -1;
1785    uint8_t *pmap = nullptr;
1786    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
1787        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1788
1789        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
1790        if (dfdp) {
1791            EmulatedDriver *emul_driver = dfdp->getDriver();
1792            return emul_driver->mmap(tc, start, length, prot, tgt_flags,
1793                                     tgt_fd, offset);
1794        }
1795
1796        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
1797        if (!ffdp)
1798            return -EBADF;
1799        sim_fd = ffdp->getSimFD();
1800
1801        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
1802                                    sim_fd, offset);
1803
1804        if (pmap == (decltype(pmap))-1) {
1805            warn("mmap: failed to map file into host address space");
1806            return -errno;
1807        }
1808    }
1809
1810    // Extend global mmap region if necessary. Note that we ignore the
1811    // start address unless MAP_FIXED is specified.
1812    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
1813        std::shared_ptr<MemState> mem_state = p->memState;
1814        Addr mmap_end = mem_state->getMmapEnd();
1815
1816        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
1817        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
1818
1819        mem_state->setMmapEnd(mmap_end);
1820    }
1821
1822    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
1823                    start, start + length - 1);
1824
1825    // We only allow mappings to overwrite existing mappings if
1826    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
1827    // because we ignore the start hint if TGT_MAP_FIXED is not set.
1828    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
1829    if (clobber) {
1830        for (auto tc : p->system->threadContexts) {
1831            // If we might be overwriting old mappings, we need to
1832            // invalidate potentially stale mappings out of the TLBs.
1833            tc->getDTBPtr()->flushAll();
1834            tc->getITBPtr()->flushAll();
1835        }
1836    }
1837
1838    // Allocate physical memory and map it in. If the page table is already
1839    // mapped and clobber is not set, the simulator will issue throw a
1840    // fatal and bail out of the simulation.
1841    p->allocateMem(start, length, clobber);
1842
1843    // Transfer content into target address space.
1844    PortProxy &tp = tc->getMemProxy();
1845    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
1846        // In general, we should zero the mapped area for anonymous mappings,
1847        // with something like:
1848        //     tp.memsetBlob(start, 0, length);
1849        // However, given that we don't support sparse mappings, and
1850        // some applications can map a couple of gigabytes of space
1851        // (intending sparse usage), that can get painfully expensive.
1852        // Fortunately, since we don't properly implement munmap either,
1853        // there's no danger of remapping used memory, so for now all
1854        // newly mapped memory should already be zeroed so we can skip it.
1855    } else {
1856        // It is possible to mmap an area larger than a file, however
1857        // accessing unmapped portions the system triggers a "Bus error"
1858        // on the host. We must know when to stop copying the file from
1859        // the host into the target address space.
1860        struct stat file_stat;
1861        if (fstat(sim_fd, &file_stat) > 0)
1862            fatal("mmap: cannot stat file");
1863
1864        // Copy the portion of the file that is resident. This requires
1865        // checking both the mmap size and the filesize that we are
1866        // trying to mmap into this space; the mmap size also depends
1867        // on the specified offset into the file.
1868        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
1869                                 length);
1870        tp.writeBlob(start, pmap, size);
1871
1872        // Cleanup the mmap region before exiting this function.
1873        munmap(pmap, length);
1874
1875        // Maintain the symbol table for dynamic executables.
1876        // The loader will call mmap to map the images into its address
1877        // space and we intercept that here. We can verify that we are
1878        // executing inside the loader by checking the program counter value.
1879        // XXX: with multiprogrammed workloads or multi-node configurations,
1880        // this will not work since there is a single global symbol table.
1881        ObjectFile *interpreter = p->getInterpreter();
1882        if (interpreter) {
1883            Addr text_start = interpreter->textBase();
1884            Addr text_end = text_start + interpreter->textSize();
1885
1886            Addr pc = tc->pcState().pc();
1887
1888            if (pc >= text_start && pc < text_end) {
1889                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1890                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
1891                ObjectFile *lib = createObjectFile(ffdp->getFileName());
1892
1893                if (lib) {
1894                    lib->loadAllSymbols(debugSymbolTable,
1895                                        lib->textBase(), start);
1896                }
1897            }
1898        }
1899
1900        // Note that we do not zero out the remainder of the mapping. This
1901        // is done by a real system, but it probably will not affect
1902        // execution (hopefully).
1903    }
1904
1905    return start;
1906}
1907
1908template <class OS>
1909SyscallReturn
1910pwrite64Func(SyscallDesc *desc, int num, ThreadContext *tc)
1911{
1912    int index = 0;
1913    auto p = tc->getProcessPtr();
1914    int tgt_fd = p->getSyscallArg(tc, index);
1915    Addr bufPtr = p->getSyscallArg(tc, index);
1916    int nbytes = p->getSyscallArg(tc, index);
1917    int offset = p->getSyscallArg(tc, index);
1918
1919    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1920    if (!ffdp)
1921        return -EBADF;
1922    int sim_fd = ffdp->getSimFD();
1923
1924    BufferArg bufArg(bufPtr, nbytes);
1925    bufArg.copyIn(tc->getMemProxy());
1926
1927    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
1928
1929    return (bytes_written == -1) ? -errno : bytes_written;
1930}
1931
1932/// Target mmap() handler.
1933template <class OS>
1934SyscallReturn
1935mmapFunc(SyscallDesc *desc, int num, ThreadContext *tc)
1936{
1937    return mmapImpl<OS>(desc, num, tc, false);
1938}
1939
1940/// Target mmap2() handler.
1941template <class OS>
1942SyscallReturn
1943mmap2Func(SyscallDesc *desc, int num, ThreadContext *tc)
1944{
1945    return mmapImpl<OS>(desc, num, tc, true);
1946}
1947
1948/// Target getrlimit() handler.
1949template <class OS>
1950SyscallReturn
1951getrlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1952{
1953    int index = 0;
1954    auto process = tc->getProcessPtr();
1955    unsigned resource = process->getSyscallArg(tc, index);
1956    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1957
1958    switch (resource) {
1959      case OS::TGT_RLIMIT_STACK:
1960        // max stack size in bytes: make up a number (8MB for now)
1961        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1962        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1963        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1964        break;
1965
1966      case OS::TGT_RLIMIT_DATA:
1967        // max data segment size in bytes: make up a number
1968        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1969        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1970        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1971        break;
1972
1973      default:
1974        warn("getrlimit: unimplemented resource %d", resource);
1975        return -EINVAL;
1976        break;
1977    }
1978
1979    rlp.copyOut(tc->getMemProxy());
1980    return 0;
1981}
1982
1983template <class OS>
1984SyscallReturn
1985prlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1986{
1987    int index = 0;
1988    auto process = tc->getProcessPtr();
1989    if (process->getSyscallArg(tc, index) != 0)
1990    {
1991        warn("prlimit: ignoring rlimits for nonzero pid");
1992        return -EPERM;
1993    }
1994    int resource = process->getSyscallArg(tc, index);
1995    Addr n = process->getSyscallArg(tc, index);
1996    if (n != 0)
1997        warn("prlimit: ignoring new rlimit");
1998    Addr o = process->getSyscallArg(tc, index);
1999    if (o != 0)
2000    {
2001        TypedBufferArg<typename OS::rlimit> rlp(o);
2002        switch (resource) {
2003          case OS::TGT_RLIMIT_STACK:
2004            // max stack size in bytes: make up a number (8MB for now)
2005            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
2006            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
2007            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
2008            break;
2009          case OS::TGT_RLIMIT_DATA:
2010            // max data segment size in bytes: make up a number
2011            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
2012            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
2013            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
2014            break;
2015          default:
2016            warn("prlimit: unimplemented resource %d", resource);
2017            return -EINVAL;
2018            break;
2019        }
2020        rlp.copyOut(tc->getMemProxy());
2021    }
2022    return 0;
2023}
2024
2025/// Target clock_gettime() function.
2026template <class OS>
2027SyscallReturn
2028clock_gettimeFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2029{
2030    int index = 1;
2031    auto p = tc->getProcessPtr();
2032    //int clk_id = p->getSyscallArg(tc, index);
2033    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
2034
2035    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
2036    tp->tv_sec += seconds_since_epoch;
2037    tp->tv_sec = TheISA::htog(tp->tv_sec);
2038    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
2039
2040    tp.copyOut(tc->getMemProxy());
2041
2042    return 0;
2043}
2044
2045/// Target clock_getres() function.
2046template <class OS>
2047SyscallReturn
2048clock_getresFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2049{
2050    int index = 1;
2051    auto p = tc->getProcessPtr();
2052    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
2053
2054    // Set resolution at ns, which is what clock_gettime() returns
2055    tp->tv_sec = 0;
2056    tp->tv_nsec = 1;
2057
2058    tp.copyOut(tc->getMemProxy());
2059
2060    return 0;
2061}
2062
2063/// Target gettimeofday() handler.
2064template <class OS>
2065SyscallReturn
2066gettimeofdayFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2067{
2068    int index = 0;
2069    auto process = tc->getProcessPtr();
2070    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
2071
2072    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
2073    tp->tv_sec += seconds_since_epoch;
2074    tp->tv_sec = TheISA::htog(tp->tv_sec);
2075    tp->tv_usec = TheISA::htog(tp->tv_usec);
2076
2077    tp.copyOut(tc->getMemProxy());
2078
2079    return 0;
2080}
2081
2082
2083/// Target utimes() handler.
2084template <class OS>
2085SyscallReturn
2086utimesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2087{
2088    std::string path;
2089    auto process = tc->getProcessPtr();
2090
2091    int index = 0;
2092    if (!tc->getMemProxy().tryReadString(path,
2093                process->getSyscallArg(tc, index))) {
2094        return -EFAULT;
2095    }
2096
2097    TypedBufferArg<typename OS::timeval [2]>
2098        tp(process->getSyscallArg(tc, index));
2099    tp.copyIn(tc->getMemProxy());
2100
2101    struct timeval hostTimeval[2];
2102    for (int i = 0; i < 2; ++i) {
2103        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
2104        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
2105    }
2106
2107    // Adjust path for cwd and redirection
2108    path = process->checkPathRedirect(path);
2109
2110    int result = utimes(path.c_str(), hostTimeval);
2111
2112    if (result < 0)
2113        return -errno;
2114
2115    return 0;
2116}
2117
2118template <class OS>
2119SyscallReturn
2120execveFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2121{
2122    desc->setFlags(0);
2123    auto p = tc->getProcessPtr();
2124
2125    int index = 0;
2126    std::string path;
2127    PortProxy & mem_proxy = tc->getMemProxy();
2128    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
2129        return -EFAULT;
2130
2131    if (access(path.c_str(), F_OK) == -1)
2132        return -EACCES;
2133
2134    auto read_in = [](std::vector<std::string> &vect,
2135                      PortProxy &mem_proxy, Addr mem_loc)
2136    {
2137        for (int inc = 0; ; inc++) {
2138            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
2139            b.copyIn(mem_proxy);
2140
2141            if (!*(Addr*)b.bufferPtr())
2142                break;
2143
2144            vect.push_back(std::string());
2145            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
2146        }
2147    };
2148
2149    /**
2150     * Note that ProcessParams is generated by swig and there are no other
2151     * examples of how to create anything but this default constructor. The
2152     * fields are manually initialized instead of passing parameters to the
2153     * constructor.
2154     */
2155    ProcessParams *pp = new ProcessParams();
2156    pp->executable = path;
2157    Addr argv_mem_loc = p->getSyscallArg(tc, index);
2158    read_in(pp->cmd, mem_proxy, argv_mem_loc);
2159    Addr envp_mem_loc = p->getSyscallArg(tc, index);
2160    read_in(pp->env, mem_proxy, envp_mem_loc);
2161    pp->uid = p->uid();
2162    pp->egid = p->egid();
2163    pp->euid = p->euid();
2164    pp->gid = p->gid();
2165    pp->ppid = p->ppid();
2166    pp->pid = p->pid();
2167    pp->input.assign("cin");
2168    pp->output.assign("cout");
2169    pp->errout.assign("cerr");
2170    pp->cwd.assign(p->tgtCwd);
2171    pp->system = p->system;
2172    /**
2173     * Prevent process object creation with identical PIDs (which will trip
2174     * a fatal check in Process constructor). The execve call is supposed to
2175     * take over the currently executing process' identity but replace
2176     * whatever it is doing with a new process image. Instead of hijacking
2177     * the process object in the simulator, we create a new process object
2178     * and bind to the previous process' thread below (hijacking the thread).
2179     */
2180    p->system->PIDs.erase(p->pid());
2181    Process *new_p = pp->create();
2182    delete pp;
2183
2184    /**
2185     * Work through the file descriptor array and close any files marked
2186     * close-on-exec.
2187     */
2188    new_p->fds = p->fds;
2189    for (int i = 0; i < new_p->fds->getSize(); i++) {
2190        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
2191        if (fdep && fdep->getCOE())
2192            new_p->fds->closeFDEntry(i);
2193    }
2194
2195    *new_p->sigchld = true;
2196
2197    delete p;
2198    tc->clearArchRegs();
2199    tc->setProcessPtr(new_p);
2200    new_p->assignThreadContext(tc->contextId());
2201    new_p->initState();
2202    tc->activate();
2203    TheISA::PCState pcState = tc->pcState();
2204    tc->setNPC(pcState.instAddr());
2205
2206    desc->setFlags(SyscallDesc::SuppressReturnValue);
2207    return 0;
2208}
2209
2210/// Target getrusage() function.
2211template <class OS>
2212SyscallReturn
2213getrusageFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2214{
2215    int index = 0;
2216    auto process = tc->getProcessPtr();
2217    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
2218    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
2219
2220    rup->ru_utime.tv_sec = 0;
2221    rup->ru_utime.tv_usec = 0;
2222    rup->ru_stime.tv_sec = 0;
2223    rup->ru_stime.tv_usec = 0;
2224    rup->ru_maxrss = 0;
2225    rup->ru_ixrss = 0;
2226    rup->ru_idrss = 0;
2227    rup->ru_isrss = 0;
2228    rup->ru_minflt = 0;
2229    rup->ru_majflt = 0;
2230    rup->ru_nswap = 0;
2231    rup->ru_inblock = 0;
2232    rup->ru_oublock = 0;
2233    rup->ru_msgsnd = 0;
2234    rup->ru_msgrcv = 0;
2235    rup->ru_nsignals = 0;
2236    rup->ru_nvcsw = 0;
2237    rup->ru_nivcsw = 0;
2238
2239    switch (who) {
2240      case OS::TGT_RUSAGE_SELF:
2241        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
2242        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
2243        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
2244        break;
2245
2246      case OS::TGT_RUSAGE_CHILDREN:
2247        // do nothing.  We have no child processes, so they take no time.
2248        break;
2249
2250      default:
2251        // don't really handle THREAD or CHILDREN, but just warn and
2252        // plow ahead
2253        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
2254             who);
2255    }
2256
2257    rup.copyOut(tc->getMemProxy());
2258
2259    return 0;
2260}
2261
2262/// Target times() function.
2263template <class OS>
2264SyscallReturn
2265timesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2266{
2267    int index = 0;
2268    auto process = tc->getProcessPtr();
2269    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
2270
2271    // Fill in the time structure (in clocks)
2272    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
2273    bufp->tms_utime = clocks;
2274    bufp->tms_stime = 0;
2275    bufp->tms_cutime = 0;
2276    bufp->tms_cstime = 0;
2277
2278    // Convert to host endianness
2279    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
2280
2281    // Write back
2282    bufp.copyOut(tc->getMemProxy());
2283
2284    // Return clock ticks since system boot
2285    return clocks;
2286}
2287
2288/// Target time() function.
2289template <class OS>
2290SyscallReturn
2291timeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2292{
2293    typename OS::time_t sec, usec;
2294    getElapsedTimeMicro(sec, usec);
2295    sec += seconds_since_epoch;
2296
2297    int index = 0;
2298    auto process = tc->getProcessPtr();
2299    Addr taddr = (Addr)process->getSyscallArg(tc, index);
2300    if (taddr != 0) {
2301        typename OS::time_t t = sec;
2302        t = TheISA::htog(t);
2303        PortProxy &p = tc->getMemProxy();
2304        p.writeBlob(taddr, &t, (int)sizeof(typename OS::time_t));
2305    }
2306    return sec;
2307}
2308
2309template <class OS>
2310SyscallReturn
2311tgkillFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2312{
2313    int index = 0;
2314    auto process = tc->getProcessPtr();
2315    int tgid = process->getSyscallArg(tc, index);
2316    int tid = process->getSyscallArg(tc, index);
2317    int sig = process->getSyscallArg(tc, index);
2318
2319    /**
2320     * This system call is intended to allow killing a specific thread
2321     * within an arbitrary thread group if sanctioned with permission checks.
2322     * It's usually true that threads share the termination signal as pointed
2323     * out by the pthread_kill man page and this seems to be the intended
2324     * usage. Due to this being an emulated environment, assume the following:
2325     * Threads are allowed to call tgkill because the EUID for all threads
2326     * should be the same. There is no signal handling mechanism for kernel
2327     * registration of signal handlers since signals are poorly supported in
2328     * emulation mode. Since signal handlers cannot be registered, all
2329     * threads within in a thread group must share the termination signal.
2330     * We never exhaust PIDs so there's no chance of finding the wrong one
2331     * due to PID rollover.
2332     */
2333
2334    System *sys = tc->getSystemPtr();
2335    Process *tgt_proc = nullptr;
2336    for (int i = 0; i < sys->numContexts(); i++) {
2337        Process *temp = sys->threadContexts[i]->getProcessPtr();
2338        if (temp->pid() == tid) {
2339            tgt_proc = temp;
2340            break;
2341        }
2342    }
2343
2344    if (sig != 0 || sig != OS::TGT_SIGABRT)
2345        return -EINVAL;
2346
2347    if (tgt_proc == nullptr)
2348        return -ESRCH;
2349
2350    if (tgid != -1 && tgt_proc->tgid() != tgid)
2351        return -ESRCH;
2352
2353    if (sig == OS::TGT_SIGABRT)
2354        exitGroupFunc(desc, 252, tc);
2355
2356    return 0;
2357}
2358
2359template <class OS>
2360SyscallReturn
2361socketFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2362{
2363    int index = 0;
2364    auto p = tc->getProcessPtr();
2365    int domain = p->getSyscallArg(tc, index);
2366    int type = p->getSyscallArg(tc, index);
2367    int prot = p->getSyscallArg(tc, index);
2368
2369    int sim_fd = socket(domain, type, prot);
2370    if (sim_fd == -1)
2371        return -errno;
2372
2373    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
2374    int tgt_fd = p->fds->allocFD(sfdp);
2375
2376    return tgt_fd;
2377}
2378
2379template <class OS>
2380SyscallReturn
2381socketpairFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2382{
2383    int index = 0;
2384    auto p = tc->getProcessPtr();
2385    int domain = p->getSyscallArg(tc, index);
2386    int type = p->getSyscallArg(tc, index);
2387    int prot = p->getSyscallArg(tc, index);
2388    Addr svPtr = p->getSyscallArg(tc, index);
2389
2390    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
2391    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
2392    if (status == -1)
2393        return -errno;
2394
2395    int *fds = (int *)svBuf.bufferPtr();
2396
2397    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
2398    fds[0] = p->fds->allocFD(sfdp1);
2399    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
2400    fds[1] = p->fds->allocFD(sfdp2);
2401    svBuf.copyOut(tc->getMemProxy());
2402
2403    return status;
2404}
2405
2406template <class OS>
2407SyscallReturn
2408selectFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2409{
2410    int retval;
2411
2412    int index = 0;
2413    auto p = tc->getProcessPtr();
2414    int nfds_t = p->getSyscallArg(tc, index);
2415    Addr fds_read_ptr = p->getSyscallArg(tc, index);
2416    Addr fds_writ_ptr = p->getSyscallArg(tc, index);
2417    Addr fds_excp_ptr = p->getSyscallArg(tc, index);
2418    Addr time_val_ptr = p->getSyscallArg(tc, index);
2419
2420    TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr);
2421    TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr);
2422    TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr);
2423    TypedBufferArg<typename OS::timeval> tp(time_val_ptr);
2424
2425    /**
2426     * Host fields. Notice that these use the definitions from the system
2427     * headers instead of the gem5 headers and libraries. If the host and
2428     * target have different header file definitions, this will not work.
2429     */
2430    fd_set rd_h;
2431    FD_ZERO(&rd_h);
2432    fd_set wr_h;
2433    FD_ZERO(&wr_h);
2434    fd_set ex_h;
2435    FD_ZERO(&ex_h);
2436
2437    /**
2438     * Copy in the fd_set from the target.
2439     */
2440    if (fds_read_ptr)
2441        rd_t.copyIn(tc->getMemProxy());
2442    if (fds_writ_ptr)
2443        wr_t.copyIn(tc->getMemProxy());
2444    if (fds_excp_ptr)
2445        ex_t.copyIn(tc->getMemProxy());
2446
2447    /**
2448     * We need to translate the target file descriptor set into a host file
2449     * descriptor set. This involves both our internal process fd array
2450     * and the fd_set defined in Linux header files. The nfds field also
2451     * needs to be updated as it will be only target specific after
2452     * retrieving it from the target; the nfds value is expected to be the
2453     * highest file descriptor that needs to be checked, so we need to extend
2454     * it out for nfds_h when we do the update.
2455     */
2456    int nfds_h = 0;
2457    std::map<int, int> trans_map;
2458    auto try_add_host_set = [&](fd_set *tgt_set_entry,
2459                                fd_set *hst_set_entry,
2460                                int iter) -> bool
2461    {
2462        /**
2463         * By this point, we know that we are looking at a valid file
2464         * descriptor set on the target. We need to check if the target file
2465         * descriptor value passed in as iter is part of the set.
2466         */
2467        if (FD_ISSET(iter, tgt_set_entry)) {
2468            /**
2469             * We know that the target file descriptor belongs to the set,
2470             * but we do not yet know if the file descriptor is valid or
2471             * that we have a host mapping. Check that now.
2472             */
2473            auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]);
2474            if (!hbfdp)
2475                return true;
2476            auto sim_fd = hbfdp->getSimFD();
2477
2478            /**
2479             * Add the sim_fd to tgt_fd translation into trans_map for use
2480             * later when we need to zero the target fd_set structures and
2481             * then update them with hits returned from the host select call.
2482             */
2483            trans_map[sim_fd] = iter;
2484
2485            /**
2486             * We know that the host file descriptor exists so now we check
2487             * if we need to update the max count for nfds_h before passing
2488             * the duplicated structure into the host.
2489             */
2490            nfds_h = std::max(nfds_h - 1, sim_fd + 1);
2491
2492            /**
2493             * Add the host file descriptor to the set that we are going to
2494             * pass into the host.
2495             */
2496            FD_SET(sim_fd, hst_set_entry);
2497        }
2498        return false;
2499    };
2500
2501    for (int i = 0; i < nfds_t; i++) {
2502        if (fds_read_ptr) {
2503            bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i);
2504            if (ebadf) return -EBADF;
2505        }
2506        if (fds_writ_ptr) {
2507            bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i);
2508            if (ebadf) return -EBADF;
2509        }
2510        if (fds_excp_ptr) {
2511            bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i);
2512            if (ebadf) return -EBADF;
2513        }
2514    }
2515
2516    if (time_val_ptr) {
2517        /**
2518         * It might be possible to decrement the timeval based on some
2519         * derivation of wall clock determined from elapsed simulator ticks
2520         * but that seems like overkill. Rather, we just set the timeval with
2521         * zero timeout. (There is no reason to block during the simulation
2522         * as it only decreases simulator performance.)
2523         */
2524        tp->tv_sec = 0;
2525        tp->tv_usec = 0;
2526
2527        retval = select(nfds_h,
2528                        fds_read_ptr ? &rd_h : nullptr,
2529                        fds_writ_ptr ? &wr_h : nullptr,
2530                        fds_excp_ptr ? &ex_h : nullptr,
2531                        (timeval*)&*tp);
2532    } else {
2533        /**
2534         * If the timeval pointer is null, setup a new timeval structure to
2535         * pass into the host select call. Unfortunately, we will need to
2536         * manually check the return value and throw a retry fault if the
2537         * return value is zero. Allowing the system call to block will
2538         * likely deadlock the event queue.
2539         */
2540        struct timeval tv = { 0, 0 };
2541
2542        retval = select(nfds_h,
2543                        fds_read_ptr ? &rd_h : nullptr,
2544                        fds_writ_ptr ? &wr_h : nullptr,
2545                        fds_excp_ptr ? &ex_h : nullptr,
2546                        &tv);
2547
2548        if (retval == 0) {
2549            /**
2550             * If blocking indefinitely, check the signal list to see if a
2551             * signal would break the poll out of the retry cycle and try to
2552             * return the signal interrupt instead.
2553             */
2554            for (auto sig : tc->getSystemPtr()->signalList)
2555                if (sig.receiver == p)
2556                    return -EINTR;
2557            return SyscallReturn::retry();
2558        }
2559    }
2560
2561    if (retval == -1)
2562        return -errno;
2563
2564    FD_ZERO((fd_set*)&*rd_t);
2565    FD_ZERO((fd_set*)&*wr_t);
2566    FD_ZERO((fd_set*)&*ex_t);
2567
2568    /**
2569     * We need to translate the host file descriptor set into a target file
2570     * descriptor set. This involves both our internal process fd array
2571     * and the fd_set defined in header files.
2572     */
2573    for (int i = 0; i < nfds_h; i++) {
2574        if (fds_read_ptr) {
2575            if (FD_ISSET(i, &rd_h))
2576                FD_SET(trans_map[i], (fd_set*)&*rd_t);
2577        }
2578
2579        if (fds_writ_ptr) {
2580            if (FD_ISSET(i, &wr_h))
2581                FD_SET(trans_map[i], (fd_set*)&*wr_t);
2582        }
2583
2584        if (fds_excp_ptr) {
2585            if (FD_ISSET(i, &ex_h))
2586                FD_SET(trans_map[i], (fd_set*)&*ex_t);
2587        }
2588    }
2589
2590    if (fds_read_ptr)
2591        rd_t.copyOut(tc->getMemProxy());
2592    if (fds_writ_ptr)
2593        wr_t.copyOut(tc->getMemProxy());
2594    if (fds_excp_ptr)
2595        ex_t.copyOut(tc->getMemProxy());
2596    if (time_val_ptr)
2597        tp.copyOut(tc->getMemProxy());
2598
2599    return retval;
2600}
2601
2602template <class OS>
2603SyscallReturn
2604readFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2605{
2606    int index = 0;
2607    auto p = tc->getProcessPtr();
2608    int tgt_fd = p->getSyscallArg(tc, index);
2609    Addr buf_ptr = p->getSyscallArg(tc, index);
2610    int nbytes = p->getSyscallArg(tc, index);
2611
2612    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
2613    if (!hbfdp)
2614        return -EBADF;
2615    int sim_fd = hbfdp->getSimFD();
2616
2617    struct pollfd pfd;
2618    pfd.fd = sim_fd;
2619    pfd.events = POLLIN | POLLPRI;
2620    if ((poll(&pfd, 1, 0) == 0)
2621        && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK))
2622        return SyscallReturn::retry();
2623
2624    BufferArg buf_arg(buf_ptr, nbytes);
2625    int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes);
2626
2627    if (bytes_read > 0)
2628        buf_arg.copyOut(tc->getMemProxy());
2629
2630    return (bytes_read == -1) ? -errno : bytes_read;
2631}
2632
2633template <class OS>
2634SyscallReturn
2635writeFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2636{
2637    int index = 0;
2638    auto p = tc->getProcessPtr();
2639    int tgt_fd = p->getSyscallArg(tc, index);
2640    Addr buf_ptr = p->getSyscallArg(tc, index);
2641    int nbytes = p->getSyscallArg(tc, index);
2642
2643    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
2644    if (!hbfdp)
2645        return -EBADF;
2646    int sim_fd = hbfdp->getSimFD();
2647
2648    BufferArg buf_arg(buf_ptr, nbytes);
2649    buf_arg.copyIn(tc->getMemProxy());
2650
2651    struct pollfd pfd;
2652    pfd.fd = sim_fd;
2653    pfd.events = POLLOUT;
2654
2655    /**
2656     * We don't want to poll on /dev/random. The kernel will not enable the
2657     * file descriptor for writing unless the entropy in the system falls
2658     * below write_wakeup_threshold. This is not guaranteed to happen
2659     * depending on host settings.
2660     */
2661    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp);
2662    if (ffdp && (ffdp->getFileName() != "/dev/random")) {
2663        if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK))
2664            return SyscallReturn::retry();
2665    }
2666
2667    int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes);
2668
2669    if (bytes_written != -1)
2670        fsync(sim_fd);
2671
2672    return (bytes_written == -1) ? -errno : bytes_written;
2673}
2674
2675template <class OS>
2676SyscallReturn
2677wait4Func(SyscallDesc *desc, int num, ThreadContext *tc)
2678{
2679    int index = 0;
2680    auto p = tc->getProcessPtr();
2681    pid_t pid = p->getSyscallArg(tc, index);
2682    Addr statPtr = p->getSyscallArg(tc, index);
2683    int options = p->getSyscallArg(tc, index);
2684    Addr rusagePtr = p->getSyscallArg(tc, index);
2685
2686    if (rusagePtr)
2687        DPRINTF_SYSCALL(Verbose, "wait4: rusage pointer provided %lx, however "
2688                 "functionality not supported. Ignoring rusage pointer.\n",
2689                 rusagePtr);
2690
2691    /**
2692     * Currently, wait4 is only implemented so that it will wait for children
2693     * exit conditions which are denoted by a SIGCHLD signals posted into the
2694     * system signal list. We return no additional information via any of the
2695     * parameters supplied to wait4. If nothing is found in the system signal
2696     * list, we will wait indefinitely for SIGCHLD to post by retrying the
2697     * call.
2698     */
2699    System *sysh = tc->getSystemPtr();
2700    std::list<BasicSignal>::iterator iter;
2701    for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) {
2702        if (iter->receiver == p) {
2703            if (pid < -1) {
2704                if ((iter->sender->pgid() == -pid)
2705                    && (iter->signalValue == OS::TGT_SIGCHLD))
2706                    goto success;
2707            } else if (pid == -1) {
2708                if (iter->signalValue == OS::TGT_SIGCHLD)
2709                    goto success;
2710            } else if (pid == 0) {
2711                if ((iter->sender->pgid() == p->pgid())
2712                    && (iter->signalValue == OS::TGT_SIGCHLD))
2713                    goto success;
2714            } else {
2715                if ((iter->sender->pid() == pid)
2716                    && (iter->signalValue == OS::TGT_SIGCHLD))
2717                    goto success;
2718            }
2719        }
2720    }
2721
2722    return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry();
2723
2724success:
2725    // Set status to EXITED for WIFEXITED evaluations.
2726    const int EXITED = 0;
2727    BufferArg statusBuf(statPtr, sizeof(int));
2728    *(int *)statusBuf.bufferPtr() = EXITED;
2729    statusBuf.copyOut(tc->getMemProxy());
2730
2731    // Return the child PID.
2732    pid_t retval = iter->sender->pid();
2733    sysh->signalList.erase(iter);
2734    return retval;
2735}
2736
2737template <class OS>
2738SyscallReturn
2739acceptFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2740{
2741    struct sockaddr sa;
2742    socklen_t addrLen;
2743    int host_fd;
2744    int index = 0;
2745    auto p = tc->getProcessPtr();
2746    int tgt_fd = p->getSyscallArg(tc, index);
2747    Addr addrPtr = p->getSyscallArg(tc, index);
2748    Addr lenPtr = p->getSyscallArg(tc, index);
2749
2750    BufferArg *lenBufPtr = nullptr;
2751    BufferArg *addrBufPtr = nullptr;
2752
2753    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
2754    if (!sfdp)
2755        return -EBADF;
2756    int sim_fd = sfdp->getSimFD();
2757
2758    /**
2759     * We poll the socket file descriptor first to guarantee that we do not
2760     * block on our accept call. The socket can be opened without the
2761     * non-blocking flag (it blocks). This will cause deadlocks between
2762     * communicating processes.
2763     */
2764    struct pollfd pfd;
2765    pfd.fd = sim_fd;
2766    pfd.events = POLLIN | POLLPRI;
2767    if ((poll(&pfd, 1, 0) == 0)
2768        && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK))
2769        return SyscallReturn::retry();
2770
2771    if (lenPtr) {
2772        lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t));
2773        lenBufPtr->copyIn(tc->getMemProxy());
2774        memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(),
2775               sizeof(socklen_t));
2776    }
2777
2778    if (addrPtr) {
2779        addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr));
2780        addrBufPtr->copyIn(tc->getMemProxy());
2781        memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(),
2782               sizeof(struct sockaddr));
2783    }
2784
2785    host_fd = accept(sim_fd, &sa, &addrLen);
2786
2787    if (host_fd == -1)
2788        return -errno;
2789
2790    if (addrPtr) {
2791        memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa));
2792        addrBufPtr->copyOut(tc->getMemProxy());
2793        delete(addrBufPtr);
2794    }
2795
2796    if (lenPtr) {
2797        *(socklen_t *)lenBufPtr->bufferPtr() = addrLen;
2798        lenBufPtr->copyOut(tc->getMemProxy());
2799        delete(lenBufPtr);
2800    }
2801
2802    auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain,
2803                                                sfdp->_type, sfdp->_protocol);
2804    return p->fds->allocFD(afdp);
2805}
2806
2807/// Target eventfd() function.
2808template <class OS>
2809SyscallReturn
2810eventfdFunc(SyscallDesc *desc, int num, ThreadContext *tc)
2811{
2812#if defined(__linux__)
2813    int index = 0;
2814    auto p = tc->getProcessPtr();
2815    unsigned initval = p->getSyscallArg(tc, index);
2816    int in_flags = p->getSyscallArg(tc, index);
2817
2818    int sim_fd = eventfd(initval, in_flags);
2819    if (sim_fd == -1)
2820        return -errno;
2821
2822    bool cloexec = in_flags & OS::TGT_O_CLOEXEC;
2823
2824    int flags = cloexec ? OS::TGT_O_CLOEXEC : 0;
2825    flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0;
2826
2827    auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec);
2828    int tgt_fd = p->fds->allocFD(hbfdp);
2829    return tgt_fd;
2830#else
2831    warnUnsupportedOS("eventfd");
2832    return -1;
2833#endif
2834}
2835
2836#endif // __SIM_SYSCALL_EMUL_HH__
2837