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