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