syscall_emul.hh revision 11906:4b99c1bb3b72
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#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
49  defined(__FreeBSD__) || defined(__CYGWIN__) || \
50  defined(__NetBSD__))
51
52#define NO_STATFS (defined(__APPLE__) || defined(__OpenBSD__) || \
53  defined(__FreeBSD__) || defined(__NetBSD__))
54
55#define NO_FALLOCATE (defined(__APPLE__) || defined(__OpenBSD__) || \
56  defined(__FreeBSD__) || defined(__NetBSD__))
57
58///
59/// @file syscall_emul.hh
60///
61/// This file defines objects used to emulate syscalls from the target
62/// application on the host machine.
63
64#ifdef __CYGWIN32__
65#include <sys/fcntl.h>
66
67#endif
68#include <fcntl.h>
69#include <sys/mman.h>
70#include <sys/stat.h>
71#if (NO_STATFS == 0)
72#include <sys/statfs.h>
73#else
74#include <sys/mount.h>
75#endif
76#include <sys/time.h>
77#include <sys/uio.h>
78#include <unistd.h>
79
80#include <cerrno>
81#include <memory>
82#include <string>
83
84#include "arch/utility.hh"
85#include "base/intmath.hh"
86#include "base/loader/object_file.hh"
87#include "base/misc.hh"
88#include "base/trace.hh"
89#include "base/types.hh"
90#include "config/the_isa.hh"
91#include "cpu/base.hh"
92#include "cpu/thread_context.hh"
93#include "mem/page_table.hh"
94#include "params/Process.hh"
95#include "sim/emul_driver.hh"
96#include "sim/process.hh"
97#include "sim/syscall_debug_macros.hh"
98#include "sim/syscall_desc.hh"
99#include "sim/syscall_emul_buf.hh"
100#include "sim/syscall_return.hh"
101
102//////////////////////////////////////////////////////////////////////
103//
104// The following emulation functions are generic enough that they
105// don't need to be recompiled for different emulated OS's.  They are
106// defined in sim/syscall_emul.cc.
107//
108//////////////////////////////////////////////////////////////////////
109
110
111/// Handler for unimplemented syscalls that we haven't thought about.
112SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
113                                Process *p, ThreadContext *tc);
114
115/// Handler for unimplemented syscalls that we never intend to
116/// implement (signal handling, etc.) and should not affect the correct
117/// behavior of the program.  Print a warning only if the appropriate
118/// trace flag is enabled.  Return success to the target program.
119SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
120                         Process *p, ThreadContext *tc);
121
122// Target fallocateFunc() handler.
123SyscallReturn fallocateFunc(SyscallDesc *desc, int num,
124                            Process *p, ThreadContext *tc);
125
126/// Target exit() handler: terminate current context.
127SyscallReturn exitFunc(SyscallDesc *desc, int num,
128                       Process *p, ThreadContext *tc);
129
130/// Target exit_group() handler: terminate simulation. (exit all threads)
131SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
132                       Process *p, ThreadContext *tc);
133
134/// Target set_tid_address() handler.
135SyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
136                                Process *p, ThreadContext *tc);
137
138/// Target getpagesize() handler.
139SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
140                              Process *p, ThreadContext *tc);
141
142/// Target brk() handler: set brk address.
143SyscallReturn brkFunc(SyscallDesc *desc, int num,
144                      Process *p, ThreadContext *tc);
145
146/// Target close() handler.
147SyscallReturn closeFunc(SyscallDesc *desc, int num,
148                        Process *p, ThreadContext *tc);
149
150// Target read() handler.
151SyscallReturn readFunc(SyscallDesc *desc, int num,
152                       Process *p, ThreadContext *tc);
153
154/// Target write() handler.
155SyscallReturn writeFunc(SyscallDesc *desc, int num,
156                        Process *p, ThreadContext *tc);
157
158/// Target lseek() handler.
159SyscallReturn lseekFunc(SyscallDesc *desc, int num,
160                        Process *p, ThreadContext *tc);
161
162/// Target _llseek() handler.
163SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
164                          Process *p, ThreadContext *tc);
165
166/// Target munmap() handler.
167SyscallReturn munmapFunc(SyscallDesc *desc, int num,
168                         Process *p, ThreadContext *tc);
169
170/// Target gethostname() handler.
171SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
172                              Process *p, ThreadContext *tc);
173
174/// Target getcwd() handler.
175SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
176                         Process *p, ThreadContext *tc);
177
178/// Target readlink() handler.
179SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
180                           Process *p, ThreadContext *tc,
181                           int index = 0);
182SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
183                           Process *p, ThreadContext *tc);
184
185/// Target unlink() handler.
186SyscallReturn unlinkHelper(SyscallDesc *desc, int num,
187                           Process *p, ThreadContext *tc,
188                           int index);
189SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
190                         Process *p, ThreadContext *tc);
191
192/// Target mkdir() handler.
193SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
194                        Process *p, ThreadContext *tc);
195
196/// Target rename() handler.
197SyscallReturn renameFunc(SyscallDesc *desc, int num,
198                         Process *p, ThreadContext *tc);
199
200
201/// Target truncate() handler.
202SyscallReturn truncateFunc(SyscallDesc *desc, int num,
203                           Process *p, ThreadContext *tc);
204
205
206/// Target ftruncate() handler.
207SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
208                            Process *p, ThreadContext *tc);
209
210
211/// Target truncate64() handler.
212SyscallReturn truncate64Func(SyscallDesc *desc, int num,
213                             Process *p, ThreadContext *tc);
214
215/// Target ftruncate64() handler.
216SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
217                              Process *p, ThreadContext *tc);
218
219
220/// Target umask() handler.
221SyscallReturn umaskFunc(SyscallDesc *desc, int num,
222                        Process *p, ThreadContext *tc);
223
224/// Target gettid() handler.
225SyscallReturn gettidFunc(SyscallDesc *desc, int num,
226                         Process *p, ThreadContext *tc);
227
228/// Target chown() handler.
229SyscallReturn chownFunc(SyscallDesc *desc, int num,
230                        Process *p, ThreadContext *tc);
231
232/// Target setpgid() handler.
233SyscallReturn setpgidFunc(SyscallDesc *desc, int num,
234                          Process *p, ThreadContext *tc);
235
236/// Target fchown() handler.
237SyscallReturn fchownFunc(SyscallDesc *desc, int num,
238                         Process *p, ThreadContext *tc);
239
240/// Target dup() handler.
241SyscallReturn dupFunc(SyscallDesc *desc, int num,
242                      Process *process, ThreadContext *tc);
243
244/// Target fcntl() handler.
245SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
246                        Process *process, ThreadContext *tc);
247
248/// Target fcntl64() handler.
249SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
250                          Process *process, ThreadContext *tc);
251
252/// Target setuid() handler.
253SyscallReturn setuidFunc(SyscallDesc *desc, int num,
254                         Process *p, ThreadContext *tc);
255
256/// Target getpid() handler.
257SyscallReturn getpidFunc(SyscallDesc *desc, int num,
258                         Process *p, ThreadContext *tc);
259
260/// Target getuid() handler.
261SyscallReturn getuidFunc(SyscallDesc *desc, int num,
262                         Process *p, ThreadContext *tc);
263
264/// Target getgid() handler.
265SyscallReturn getgidFunc(SyscallDesc *desc, int num,
266                         Process *p, ThreadContext *tc);
267
268/// Target getppid() handler.
269SyscallReturn getppidFunc(SyscallDesc *desc, int num,
270                          Process *p, ThreadContext *tc);
271
272/// Target geteuid() handler.
273SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
274                          Process *p, ThreadContext *tc);
275
276/// Target getegid() handler.
277SyscallReturn getegidFunc(SyscallDesc *desc, int num,
278                          Process *p, ThreadContext *tc);
279
280/// Target access() handler
281SyscallReturn accessFunc(SyscallDesc *desc, int num,
282                         Process *p, ThreadContext *tc);
283SyscallReturn accessFunc(SyscallDesc *desc, int num,
284                         Process *p, ThreadContext *tc,
285                         int index);
286
287/// Futex system call
288/// Implemented by Daniel Sanchez
289/// Used by printf's in multi-threaded apps
290template <class OS>
291SyscallReturn
292futexFunc(SyscallDesc *desc, int callnum, Process *process,
293          ThreadContext *tc)
294{
295    int index_uaddr = 0;
296    int index_op = 1;
297    int index_val = 2;
298    int index_timeout = 3;
299
300    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
301    int op = process->getSyscallArg(tc, index_op);
302    int val = process->getSyscallArg(tc, index_val);
303    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
304
305    std::map<uint64_t, std::list<ThreadContext *> * >
306        &futex_map = tc->getSystemPtr()->futexMap;
307
308    DPRINTF(SyscallVerbose, "futex: Address=%llx, op=%d, val=%d\n",
309            uaddr, op, val);
310
311    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
312
313    if (op == OS::TGT_FUTEX_WAIT) {
314        if (timeout != 0) {
315            warn("futex: FUTEX_WAIT with non-null timeout unimplemented;"
316                 "we'll wait indefinitely");
317        }
318
319        uint8_t *buf = new uint8_t[sizeof(int)];
320        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
321        int mem_val = *((int *)buf);
322        delete[] buf;
323
324        if (val != mem_val) {
325            DPRINTF(SyscallVerbose, "futex: FUTEX_WAKE, read: %d, "
326                                    "expected: %d\n", mem_val, val);
327            return -OS::TGT_EWOULDBLOCK;
328        }
329
330        // Queue the thread context
331        std::list<ThreadContext *> * tcWaitList;
332        if (futex_map.count(uaddr)) {
333            tcWaitList = futex_map.find(uaddr)->second;
334        } else {
335            tcWaitList = new std::list<ThreadContext *>();
336            futex_map.insert(std::pair< uint64_t,
337                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
338        }
339        tcWaitList->push_back(tc);
340        DPRINTF(SyscallVerbose, "futex: FUTEX_WAIT, suspending calling thread "
341                                "context on address 0x%lx\n", uaddr);
342        tc->suspend();
343        return 0;
344    } else if (op == OS::TGT_FUTEX_WAKE){
345        int wokenUp = 0;
346        std::list<ThreadContext *> * tcWaitList;
347        if (futex_map.count(uaddr)) {
348            tcWaitList = futex_map.find(uaddr)->second;
349            while (tcWaitList->size() > 0 && wokenUp < val) {
350                tcWaitList->front()->activate();
351                tcWaitList->pop_front();
352                wokenUp++;
353            }
354            if (tcWaitList->empty()) {
355                futex_map.erase(uaddr);
356                delete tcWaitList;
357            }
358        }
359        DPRINTF(SyscallVerbose, "futex: FUTEX_WAKE, activated %d waiting "
360                                "thread context on address 0x%lx\n",
361                                wokenUp, uaddr);
362        return wokenUp;
363    } else {
364        warn("futex: op %d is not implemented, just returning...", op);
365        return 0;
366    }
367
368}
369
370
371/// Pseudo Funcs  - These functions use a different return convension,
372/// returning a second value in a register other than the normal return register
373SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
374                             Process *process, ThreadContext *tc);
375
376/// Target getpidPseudo() handler.
377SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
378                               Process *p, ThreadContext *tc);
379
380/// Target getuidPseudo() handler.
381SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
382                               Process *p, ThreadContext *tc);
383
384/// Target getgidPseudo() handler.
385SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
386                               Process *p, ThreadContext *tc);
387
388
389/// A readable name for 1,000,000, for converting microseconds to seconds.
390const int one_million = 1000000;
391/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
392const int one_billion = 1000000000;
393
394/// Approximate seconds since the epoch (1/1/1970).  About a billion,
395/// by my reckoning.  We want to keep this a constant (not use the
396/// real-world time) to keep simulations repeatable.
397const unsigned seconds_since_epoch = 1000000000;
398
399/// Helper function to convert current elapsed time to seconds and
400/// microseconds.
401template <class T1, class T2>
402void
403getElapsedTimeMicro(T1 &sec, T2 &usec)
404{
405    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
406    sec = elapsed_usecs / one_million;
407    usec = elapsed_usecs % one_million;
408}
409
410/// Helper function to convert current elapsed time to seconds and
411/// nanoseconds.
412template <class T1, class T2>
413void
414getElapsedTimeNano(T1 &sec, T2 &nsec)
415{
416    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
417    sec = elapsed_nsecs / one_billion;
418    nsec = elapsed_nsecs % one_billion;
419}
420
421//////////////////////////////////////////////////////////////////////
422//
423// The following emulation functions are generic, but need to be
424// templated to account for differences in types, constants, etc.
425//
426//////////////////////////////////////////////////////////////////////
427
428    typedef struct statfs hst_statfs;
429#if NO_STAT64
430    typedef struct stat hst_stat;
431    typedef struct stat hst_stat64;
432#else
433    typedef struct stat hst_stat;
434    typedef struct stat64 hst_stat64;
435#endif
436
437//// Helper function to convert a host stat buffer to a target stat
438//// buffer.  Also copies the target buffer out to the simulated
439//// memory space.  Used by stat(), fstat(), and lstat().
440
441template <typename target_stat, typename host_stat>
442static void
443convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
444{
445    using namespace TheISA;
446
447    if (fakeTTY)
448        tgt->st_dev = 0xA;
449    else
450        tgt->st_dev = host->st_dev;
451    tgt->st_dev = TheISA::htog(tgt->st_dev);
452    tgt->st_ino = host->st_ino;
453    tgt->st_ino = TheISA::htog(tgt->st_ino);
454    tgt->st_mode = host->st_mode;
455    if (fakeTTY) {
456        // Claim to be a character device
457        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
458        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
459    }
460    tgt->st_mode = TheISA::htog(tgt->st_mode);
461    tgt->st_nlink = host->st_nlink;
462    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
463    tgt->st_uid = host->st_uid;
464    tgt->st_uid = TheISA::htog(tgt->st_uid);
465    tgt->st_gid = host->st_gid;
466    tgt->st_gid = TheISA::htog(tgt->st_gid);
467    if (fakeTTY)
468        tgt->st_rdev = 0x880d;
469    else
470        tgt->st_rdev = host->st_rdev;
471    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
472    tgt->st_size = host->st_size;
473    tgt->st_size = TheISA::htog(tgt->st_size);
474    tgt->st_atimeX = host->st_atime;
475    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
476    tgt->st_mtimeX = host->st_mtime;
477    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
478    tgt->st_ctimeX = host->st_ctime;
479    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
480    // Force the block size to be 8KB. This helps to ensure buffered io works
481    // consistently across different hosts.
482    tgt->st_blksize = 0x2000;
483    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
484    tgt->st_blocks = host->st_blocks;
485    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
486}
487
488// Same for stat64
489
490template <typename target_stat, typename host_stat64>
491static void
492convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
493{
494    using namespace TheISA;
495
496    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
497#if defined(STAT_HAVE_NSEC)
498    tgt->st_atime_nsec = host->st_atime_nsec;
499    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
500    tgt->st_mtime_nsec = host->st_mtime_nsec;
501    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
502    tgt->st_ctime_nsec = host->st_ctime_nsec;
503    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
504#else
505    tgt->st_atime_nsec = 0;
506    tgt->st_mtime_nsec = 0;
507    tgt->st_ctime_nsec = 0;
508#endif
509}
510
511// Here are a couple of convenience functions
512template<class OS>
513static void
514copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
515               hst_stat *host, bool fakeTTY = false)
516{
517    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
518    tgt_stat_buf tgt(addr);
519    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
520    tgt.copyOut(mem);
521}
522
523template<class OS>
524static void
525copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
526                 hst_stat64 *host, bool fakeTTY = false)
527{
528    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
529    tgt_stat_buf tgt(addr);
530    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
531    tgt.copyOut(mem);
532}
533
534template <class OS>
535static void
536copyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
537                 hst_statfs *host)
538{
539    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
540
541    tgt->f_type = TheISA::htog(host->f_type);
542#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
543    tgt->f_bsize = TheISA::htog(host->f_iosize);
544#else
545    tgt->f_bsize = TheISA::htog(host->f_bsize);
546#endif
547    tgt->f_blocks = TheISA::htog(host->f_blocks);
548    tgt->f_bfree = TheISA::htog(host->f_bfree);
549    tgt->f_bavail = TheISA::htog(host->f_bavail);
550    tgt->f_files = TheISA::htog(host->f_files);
551    tgt->f_ffree = TheISA::htog(host->f_ffree);
552    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
553#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
554    tgt->f_namelen = TheISA::htog(host->f_namemax);
555    tgt->f_frsize = TheISA::htog(host->f_bsize);
556#elif defined(__APPLE__)
557    tgt->f_namelen = 0;
558    tgt->f_frsize = 0;
559#else
560    tgt->f_namelen = TheISA::htog(host->f_namelen);
561    tgt->f_frsize = TheISA::htog(host->f_frsize);
562#endif
563#if defined(__linux__)
564    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
565#else
566    /*
567     * The fields are different sizes per OS. Don't bother with
568     * f_spare or f_reserved on non-Linux for now.
569     */
570    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
571#endif
572
573    tgt.copyOut(mem);
574}
575
576/// Target ioctl() handler.  For the most part, programs call ioctl()
577/// only to find out if their stdout is a tty, to determine whether to
578/// do line or block buffering.  We always claim that output fds are
579/// not TTYs to provide repeatable results.
580template <class OS>
581SyscallReturn
582ioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
583{
584    int index = 0;
585    int tgt_fd = p->getSyscallArg(tc, index);
586    unsigned req = p->getSyscallArg(tc, index);
587
588    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
589
590    if (OS::isTtyReq(req))
591        return -ENOTTY;
592
593    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
594    if (!dfdp)
595        return -EBADF;
596
597    /**
598     * If the driver is valid, issue the ioctl through it. Otherwise,
599     * there's an implicit assumption that the device is a TTY type and we
600     * return that we do not have a valid TTY.
601     */
602    EmulatedDriver *emul_driver = dfdp->getDriver();
603    if (emul_driver)
604        return emul_driver->ioctl(p, tc, req);
605
606    /**
607     * For lack of a better return code, return ENOTTY. Ideally, we should
608     * return something better here, but at least we issue the warning.
609     */
610    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
611         tgt_fd, req, tc->pcState());
612    return -ENOTTY;
613}
614
615template <class OS>
616static SyscallReturn
617openFunc(SyscallDesc *desc, int callnum, Process *process,
618         ThreadContext *tc, int index)
619{
620    std::string path;
621
622    if (!tc->getMemProxy().tryReadString(path,
623                process->getSyscallArg(tc, index)))
624        return -EFAULT;
625
626    int tgtFlags = process->getSyscallArg(tc, index);
627    int mode = process->getSyscallArg(tc, index);
628    int hostFlags = 0;
629
630    // translate open flags
631    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
632        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
633            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
634            hostFlags |= OS::openFlagTable[i].hostFlag;
635        }
636    }
637
638    // any target flags left?
639    if (tgtFlags != 0)
640        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
641
642#ifdef __CYGWIN32__
643    hostFlags |= O_BINARY;
644#endif
645
646    // Adjust path for current working directory
647    path = process->fullPath(path);
648
649    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
650
651    if (startswith(path, "/dev/")) {
652        std::string filename = path.substr(strlen("/dev/"));
653        if (filename == "sysdev0") {
654            // This is a memory-mapped high-resolution timer device on Alpha.
655            // We don't support it, so just punt.
656            warn("Ignoring open(%s, ...)\n", path);
657            return -ENOENT;
658        }
659
660        EmulatedDriver *drv = process->findDriver(filename);
661        if (drv) {
662            // the driver's open method will allocate a fd from the
663            // process if necessary.
664            return drv->open(process, tc, mode, hostFlags);
665        }
666
667        // fall through here for pass through to host devices, such as
668        // /dev/zero
669    }
670
671    int fd;
672    int local_errno;
673    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
674        startswith(path, "/platform/") || startswith(path, "/sys/")) {
675        // It's a proc/sys entry and requires special handling
676        fd = OS::openSpecialFile(path, process, tc);
677        local_errno = ENOENT;
678     } else {
679        // open the file
680        fd = open(path.c_str(), hostFlags, mode);
681        local_errno = errno;
682     }
683
684    if (fd == -1)
685        return -local_errno;
686
687    std::shared_ptr<FileFDEntry> ffdp =
688        std::make_shared<FileFDEntry>(fd, hostFlags, path.c_str(), false);
689    return process->fds->allocFD(ffdp);
690}
691
692/// Target open() handler.
693template <class OS>
694SyscallReturn
695openFunc(SyscallDesc *desc, int callnum, Process *process,
696         ThreadContext *tc)
697{
698    return openFunc<OS>(desc, callnum, process, tc, 0);
699}
700
701/// Target openat() handler.
702template <class OS>
703SyscallReturn
704openatFunc(SyscallDesc *desc, int callnum, Process *process,
705           ThreadContext *tc)
706{
707    int index = 0;
708    int dirfd = process->getSyscallArg(tc, index);
709    if (dirfd != OS::TGT_AT_FDCWD)
710        warn("openat: first argument not AT_FDCWD; unlikely to work");
711    return openFunc<OS>(desc, callnum, process, tc, 1);
712}
713
714/// Target unlinkat() handler.
715template <class OS>
716SyscallReturn
717unlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
718             ThreadContext *tc)
719{
720    int index = 0;
721    int dirfd = process->getSyscallArg(tc, index);
722    if (dirfd != OS::TGT_AT_FDCWD)
723        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
724
725    return unlinkHelper(desc, callnum, process, tc, 1);
726}
727
728/// Target facessat() handler
729template <class OS>
730SyscallReturn
731faccessatFunc(SyscallDesc *desc, int callnum, Process *process,
732              ThreadContext *tc)
733{
734    int index = 0;
735    int dirfd = process->getSyscallArg(tc, index);
736    if (dirfd != OS::TGT_AT_FDCWD)
737        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
738    return accessFunc(desc, callnum, process, tc, 1);
739}
740
741/// Target readlinkat() handler
742template <class OS>
743SyscallReturn
744readlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
745               ThreadContext *tc)
746{
747    int index = 0;
748    int dirfd = process->getSyscallArg(tc, index);
749    if (dirfd != OS::TGT_AT_FDCWD)
750        warn("openat: first argument not AT_FDCWD; unlikely to work");
751    return readlinkFunc(desc, callnum, process, tc, 1);
752}
753
754/// Target renameat() handler.
755template <class OS>
756SyscallReturn
757renameatFunc(SyscallDesc *desc, int callnum, Process *process,
758             ThreadContext *tc)
759{
760    int index = 0;
761
762    int olddirfd = process->getSyscallArg(tc, index);
763    if (olddirfd != OS::TGT_AT_FDCWD)
764        warn("renameat: first argument not AT_FDCWD; unlikely to work");
765
766    std::string old_name;
767
768    if (!tc->getMemProxy().tryReadString(old_name,
769                                         process->getSyscallArg(tc, index)))
770        return -EFAULT;
771
772    int newdirfd = process->getSyscallArg(tc, index);
773    if (newdirfd != OS::TGT_AT_FDCWD)
774        warn("renameat: third argument not AT_FDCWD; unlikely to work");
775
776    std::string new_name;
777
778    if (!tc->getMemProxy().tryReadString(new_name,
779                                         process->getSyscallArg(tc, index)))
780        return -EFAULT;
781
782    // Adjust path for current working directory
783    old_name = process->fullPath(old_name);
784    new_name = process->fullPath(new_name);
785
786    int result = rename(old_name.c_str(), new_name.c_str());
787    return (result == -1) ? -errno : result;
788}
789
790/// Target sysinfo() handler.
791template <class OS>
792SyscallReturn
793sysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
794            ThreadContext *tc)
795{
796
797    int index = 0;
798    TypedBufferArg<typename OS::tgt_sysinfo>
799        sysinfo(process->getSyscallArg(tc, index));
800
801    sysinfo->uptime = seconds_since_epoch;
802    sysinfo->totalram = process->system->memSize();
803    sysinfo->mem_unit = 1;
804
805    sysinfo.copyOut(tc->getMemProxy());
806
807    return 0;
808}
809
810/// Target chmod() handler.
811template <class OS>
812SyscallReturn
813chmodFunc(SyscallDesc *desc, int callnum, Process *process,
814          ThreadContext *tc)
815{
816    std::string path;
817
818    int index = 0;
819    if (!tc->getMemProxy().tryReadString(path,
820                process->getSyscallArg(tc, index))) {
821        return -EFAULT;
822    }
823
824    uint32_t mode = process->getSyscallArg(tc, index);
825    mode_t hostMode = 0;
826
827    // XXX translate mode flags via OS::something???
828    hostMode = mode;
829
830    // Adjust path for current working directory
831    path = process->fullPath(path);
832
833    // do the chmod
834    int result = chmod(path.c_str(), hostMode);
835    if (result < 0)
836        return -errno;
837
838    return 0;
839}
840
841
842/// Target fchmod() handler.
843template <class OS>
844SyscallReturn
845fchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
846{
847    int index = 0;
848    int tgt_fd = p->getSyscallArg(tc, index);
849    uint32_t mode = p->getSyscallArg(tc, index);
850
851    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
852    if (!ffdp)
853        return -EBADF;
854    int sim_fd = ffdp->getSimFD();
855
856    mode_t hostMode = mode;
857
858    int result = fchmod(sim_fd, hostMode);
859
860    return (result < 0) ? -errno : 0;
861}
862
863/// Target mremap() handler.
864template <class OS>
865SyscallReturn
866mremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
867{
868    int index = 0;
869    Addr start = process->getSyscallArg(tc, index);
870    uint64_t old_length = process->getSyscallArg(tc, index);
871    uint64_t new_length = process->getSyscallArg(tc, index);
872    uint64_t flags = process->getSyscallArg(tc, index);
873    uint64_t provided_address = 0;
874    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
875
876    if (use_provided_address)
877        provided_address = process->getSyscallArg(tc, index);
878
879    if ((start % TheISA::PageBytes != 0) ||
880        (provided_address % TheISA::PageBytes != 0)) {
881        warn("mremap failing: arguments not page aligned");
882        return -EINVAL;
883    }
884
885    new_length = roundUp(new_length, TheISA::PageBytes);
886
887    if (new_length > old_length) {
888        std::shared_ptr<MemState> mem_state = process->memState;
889        Addr mmap_end = mem_state->getMmapEnd();
890
891        if ((start + old_length) == mmap_end &&
892            (!use_provided_address || provided_address == start)) {
893            uint64_t diff = new_length - old_length;
894            process->allocateMem(mmap_end, diff);
895            mem_state->setMmapEnd(mmap_end + diff);
896            return start;
897        } else {
898            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
899                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
900                return -ENOMEM;
901            } else {
902                uint64_t new_start = use_provided_address ?
903                    provided_address : mmap_end;
904                process->pTable->remap(start, old_length, new_start);
905                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
906                     new_start, new_start + new_length,
907                     new_length - old_length);
908                // add on the remaining unallocated pages
909                process->allocateMem(new_start + old_length,
910                                     new_length - old_length,
911                                     use_provided_address /* clobber */);
912                if (!use_provided_address)
913                    mem_state->setMmapEnd(mmap_end + new_length);
914                if (use_provided_address &&
915                    new_start + new_length > mem_state->getMmapEnd()) {
916                    // something fishy going on here, at least notify the user
917                    // @todo: increase mmap_end?
918                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
919                }
920                warn("returning %08p as start\n", new_start);
921                return new_start;
922            }
923        }
924    } else {
925        if (use_provided_address && provided_address != start)
926            process->pTable->remap(start, new_length, provided_address);
927        process->pTable->unmap(start + new_length, old_length - new_length);
928        return use_provided_address ? provided_address : start;
929    }
930}
931
932/// Target stat() handler.
933template <class OS>
934SyscallReturn
935statFunc(SyscallDesc *desc, int callnum, Process *process,
936         ThreadContext *tc)
937{
938    std::string path;
939
940    int index = 0;
941    if (!tc->getMemProxy().tryReadString(path,
942                process->getSyscallArg(tc, index))) {
943        return -EFAULT;
944    }
945    Addr bufPtr = process->getSyscallArg(tc, index);
946
947    // Adjust path for current working directory
948    path = process->fullPath(path);
949
950    struct stat hostBuf;
951    int result = stat(path.c_str(), &hostBuf);
952
953    if (result < 0)
954        return -errno;
955
956    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
957
958    return 0;
959}
960
961
962/// Target stat64() handler.
963template <class OS>
964SyscallReturn
965stat64Func(SyscallDesc *desc, int callnum, Process *process,
966           ThreadContext *tc)
967{
968    std::string path;
969
970    int index = 0;
971    if (!tc->getMemProxy().tryReadString(path,
972                process->getSyscallArg(tc, index)))
973        return -EFAULT;
974    Addr bufPtr = process->getSyscallArg(tc, index);
975
976    // Adjust path for current working directory
977    path = process->fullPath(path);
978
979#if NO_STAT64
980    struct stat  hostBuf;
981    int result = stat(path.c_str(), &hostBuf);
982#else
983    struct stat64 hostBuf;
984    int result = stat64(path.c_str(), &hostBuf);
985#endif
986
987    if (result < 0)
988        return -errno;
989
990    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
991
992    return 0;
993}
994
995
996/// Target fstatat64() handler.
997template <class OS>
998SyscallReturn
999fstatat64Func(SyscallDesc *desc, int callnum, Process *process,
1000              ThreadContext *tc)
1001{
1002    int index = 0;
1003    int dirfd = process->getSyscallArg(tc, index);
1004    if (dirfd != OS::TGT_AT_FDCWD)
1005        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
1006
1007    std::string path;
1008    if (!tc->getMemProxy().tryReadString(path,
1009                process->getSyscallArg(tc, index)))
1010        return -EFAULT;
1011    Addr bufPtr = process->getSyscallArg(tc, index);
1012
1013    // Adjust path for current working directory
1014    path = process->fullPath(path);
1015
1016#if NO_STAT64
1017    struct stat  hostBuf;
1018    int result = stat(path.c_str(), &hostBuf);
1019#else
1020    struct stat64 hostBuf;
1021    int result = stat64(path.c_str(), &hostBuf);
1022#endif
1023
1024    if (result < 0)
1025        return -errno;
1026
1027    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1028
1029    return 0;
1030}
1031
1032
1033/// Target fstat64() handler.
1034template <class OS>
1035SyscallReturn
1036fstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1037{
1038    int index = 0;
1039    int tgt_fd = p->getSyscallArg(tc, index);
1040    Addr bufPtr = p->getSyscallArg(tc, index);
1041
1042    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1043    if (!ffdp)
1044        return -EBADF;
1045    int sim_fd = ffdp->getSimFD();
1046
1047#if NO_STAT64
1048    struct stat  hostBuf;
1049    int result = fstat(sim_fd, &hostBuf);
1050#else
1051    struct stat64  hostBuf;
1052    int result = fstat64(sim_fd, &hostBuf);
1053#endif
1054
1055    if (result < 0)
1056        return -errno;
1057
1058    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1059
1060    return 0;
1061}
1062
1063
1064/// Target lstat() handler.
1065template <class OS>
1066SyscallReturn
1067lstatFunc(SyscallDesc *desc, int callnum, Process *process,
1068          ThreadContext *tc)
1069{
1070    std::string path;
1071
1072    int index = 0;
1073    if (!tc->getMemProxy().tryReadString(path,
1074                process->getSyscallArg(tc, index))) {
1075        return -EFAULT;
1076    }
1077    Addr bufPtr = process->getSyscallArg(tc, index);
1078
1079    // Adjust path for current working directory
1080    path = process->fullPath(path);
1081
1082    struct stat hostBuf;
1083    int result = lstat(path.c_str(), &hostBuf);
1084
1085    if (result < 0)
1086        return -errno;
1087
1088    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1089
1090    return 0;
1091}
1092
1093/// Target lstat64() handler.
1094template <class OS>
1095SyscallReturn
1096lstat64Func(SyscallDesc *desc, int callnum, Process *process,
1097            ThreadContext *tc)
1098{
1099    std::string path;
1100
1101    int index = 0;
1102    if (!tc->getMemProxy().tryReadString(path,
1103                process->getSyscallArg(tc, index))) {
1104        return -EFAULT;
1105    }
1106    Addr bufPtr = process->getSyscallArg(tc, index);
1107
1108    // Adjust path for current working directory
1109    path = process->fullPath(path);
1110
1111#if NO_STAT64
1112    struct stat hostBuf;
1113    int result = lstat(path.c_str(), &hostBuf);
1114#else
1115    struct stat64 hostBuf;
1116    int result = lstat64(path.c_str(), &hostBuf);
1117#endif
1118
1119    if (result < 0)
1120        return -errno;
1121
1122    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1123
1124    return 0;
1125}
1126
1127/// Target fstat() handler.
1128template <class OS>
1129SyscallReturn
1130fstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1131{
1132    int index = 0;
1133    int tgt_fd = p->getSyscallArg(tc, index);
1134    Addr bufPtr = p->getSyscallArg(tc, index);
1135
1136    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1137
1138    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1139    if (!ffdp)
1140        return -EBADF;
1141    int sim_fd = ffdp->getSimFD();
1142
1143    struct stat hostBuf;
1144    int result = fstat(sim_fd, &hostBuf);
1145
1146    if (result < 0)
1147        return -errno;
1148
1149    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1150
1151    return 0;
1152}
1153
1154
1155/// Target statfs() handler.
1156template <class OS>
1157SyscallReturn
1158statfsFunc(SyscallDesc *desc, int callnum, Process *process,
1159           ThreadContext *tc)
1160{
1161#if NO_STATFS
1162    warn("Host OS cannot support calls to statfs. Ignoring syscall");
1163#else
1164    std::string path;
1165
1166    int index = 0;
1167    if (!tc->getMemProxy().tryReadString(path,
1168                process->getSyscallArg(tc, index))) {
1169        return -EFAULT;
1170    }
1171    Addr bufPtr = process->getSyscallArg(tc, index);
1172
1173    // Adjust path for current working directory
1174    path = process->fullPath(path);
1175
1176    struct statfs hostBuf;
1177    int result = statfs(path.c_str(), &hostBuf);
1178
1179    if (result < 0)
1180        return -errno;
1181
1182    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1183#endif
1184    return 0;
1185}
1186
1187template <class OS>
1188SyscallReturn
1189cloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1190{
1191    int index = 0;
1192    TheISA::IntReg flags = p->getSyscallArg(tc, index);
1193    TheISA::IntReg newStack = p->getSyscallArg(tc, index);
1194    Addr ptidPtr = p->getSyscallArg(tc, index);
1195    Addr ctidPtr = p->getSyscallArg(tc, index);
1196    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
1197
1198    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
1199        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
1200        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
1201        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
1202        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
1203        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
1204        return -EINVAL;
1205
1206    ThreadContext *ctc;
1207    if (!(ctc = p->findFreeContext()))
1208        fatal("clone: no spare thread context in system");
1209
1210    /**
1211     * Note that ProcessParams is generated by swig and there are no other
1212     * examples of how to create anything but this default constructor. The
1213     * fields are manually initialized instead of passing parameters to the
1214     * constructor.
1215     */
1216    ProcessParams *pp = new ProcessParams();
1217    pp->executable.assign(*(new std::string(p->progName())));
1218    pp->cmd.push_back(*(new std::string(p->progName())));
1219    pp->system = p->system;
1220    pp->cwd.assign(p->getcwd());
1221    pp->input.assign("stdin");
1222    pp->output.assign("stdout");
1223    pp->errout.assign("stderr");
1224    pp->uid = p->uid();
1225    pp->euid = p->euid();
1226    pp->gid = p->gid();
1227    pp->egid = p->egid();
1228
1229    /* Find the first free PID that's less than the maximum */
1230    std::set<int> const& pids = p->system->PIDs;
1231    int temp_pid = *pids.begin();
1232    do {
1233        temp_pid++;
1234    } while (pids.find(temp_pid) != pids.end());
1235    if (temp_pid >= System::maxPID)
1236        fatal("temp_pid is too large: %d", temp_pid);
1237
1238    pp->pid = temp_pid;
1239    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
1240    Process *cp = pp->create();
1241    delete pp;
1242
1243    Process *owner = ctc->getProcessPtr();
1244    ctc->setProcessPtr(cp);
1245    cp->assignThreadContext(ctc->contextId());
1246    owner->revokeThreadContext(ctc->contextId());
1247
1248    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
1249        BufferArg ptidBuf(ptidPtr, sizeof(long));
1250        long *ptid = (long *)ptidBuf.bufferPtr();
1251        *ptid = cp->pid();
1252        ptidBuf.copyOut(tc->getMemProxy());
1253    }
1254
1255    cp->initState();
1256    p->clone(tc, ctc, cp, flags);
1257
1258    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
1259        BufferArg ctidBuf(ctidPtr, sizeof(long));
1260        long *ctid = (long *)ctidBuf.bufferPtr();
1261        *ctid = cp->pid();
1262        ctidBuf.copyOut(ctc->getMemProxy());
1263    }
1264
1265    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
1266        cp->childClearTID = (uint64_t)ctidPtr;
1267
1268    ctc->clearArchRegs();
1269
1270#if THE_ISA == ALPHA_ISA
1271    TheISA::copyMiscRegs(tc, ctc);
1272#elif THE_ISA == SPARC_ISA
1273    TheISA::copyRegs(tc, ctc);
1274    ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0);
1275    ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0);
1276    ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2);
1277    ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows);
1278    ctc->setMiscReg(TheISA::MISCREG_CWP, 0);
1279    ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0);
1280    ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0);
1281    ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY);
1282    for (int y = 8; y < 32; y++)
1283        ctc->setIntReg(y, tc->readIntReg(y));
1284#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA
1285    TheISA::copyRegs(tc, ctc);
1286#endif
1287
1288#if THE_ISA == X86_ISA
1289    if (flags & OS::TGT_CLONE_SETTLS) {
1290        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr);
1291        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr);
1292    }
1293#endif
1294
1295    if (newStack)
1296        ctc->setIntReg(TheISA::StackPointerReg, newStack);
1297
1298    cp->setSyscallReturn(ctc, 0);
1299
1300#if THE_ISA == ALPHA_ISA
1301    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
1302#elif THE_ISA == SPARC_ISA
1303    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
1304    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
1305#endif
1306
1307    ctc->pcState(tc->nextInstAddr());
1308    ctc->activate();
1309
1310    return cp->pid();
1311}
1312
1313/// Target fstatfs() handler.
1314template <class OS>
1315SyscallReturn
1316fstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1317{
1318    int index = 0;
1319    int tgt_fd = p->getSyscallArg(tc, index);
1320    Addr bufPtr = p->getSyscallArg(tc, index);
1321
1322    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1323    if (!ffdp)
1324        return -EBADF;
1325    int sim_fd = ffdp->getSimFD();
1326
1327    struct statfs hostBuf;
1328    int result = fstatfs(sim_fd, &hostBuf);
1329
1330    if (result < 0)
1331        return -errno;
1332
1333    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1334
1335    return 0;
1336}
1337
1338
1339/// Target writev() handler.
1340template <class OS>
1341SyscallReturn
1342writevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1343{
1344    int index = 0;
1345    int tgt_fd = p->getSyscallArg(tc, index);
1346
1347    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
1348    if (!hbfdp)
1349        return -EBADF;
1350    int sim_fd = hbfdp->getSimFD();
1351
1352    SETranslatingPortProxy &prox = tc->getMemProxy();
1353    uint64_t tiov_base = p->getSyscallArg(tc, index);
1354    size_t count = p->getSyscallArg(tc, index);
1355    struct iovec hiov[count];
1356    for (size_t i = 0; i < count; ++i) {
1357        typename OS::tgt_iovec tiov;
1358
1359        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1360                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1361        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1362        hiov[i].iov_base = new char [hiov[i].iov_len];
1363        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1364                      hiov[i].iov_len);
1365    }
1366
1367    int result = writev(sim_fd, hiov, count);
1368
1369    for (size_t i = 0; i < count; ++i)
1370        delete [] (char *)hiov[i].iov_base;
1371
1372    if (result < 0)
1373        return -errno;
1374
1375    return result;
1376}
1377
1378/// Real mmap handler.
1379template <class OS>
1380SyscallReturn
1381mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
1382         bool is_mmap2)
1383{
1384    int index = 0;
1385    Addr start = p->getSyscallArg(tc, index);
1386    uint64_t length = p->getSyscallArg(tc, index);
1387    int prot = p->getSyscallArg(tc, index);
1388    int tgt_flags = p->getSyscallArg(tc, index);
1389    int tgt_fd = p->getSyscallArg(tc, index);
1390    int offset = p->getSyscallArg(tc, index);
1391
1392    if (is_mmap2)
1393        offset *= TheISA::PageBytes;
1394
1395    if (start & (TheISA::PageBytes - 1) ||
1396        offset & (TheISA::PageBytes - 1) ||
1397        (tgt_flags & OS::TGT_MAP_PRIVATE &&
1398         tgt_flags & OS::TGT_MAP_SHARED) ||
1399        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
1400         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
1401        !length) {
1402        return -EINVAL;
1403    }
1404
1405    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
1406        // With shared mmaps, there are two cases to consider:
1407        // 1) anonymous: writes should modify the mapping and this should be
1408        // visible to observers who share the mapping. Currently, it's
1409        // difficult to update the shared mapping because there's no
1410        // structure which maintains information about the which virtual
1411        // memory areas are shared. If that structure existed, it would be
1412        // possible to make the translations point to the same frames.
1413        // 2) file-backed: writes should modify the mapping and the file
1414        // which is backed by the mapping. The shared mapping problem is the
1415        // same as what was mentioned about the anonymous mappings. For
1416        // file-backed mappings, the writes to the file are difficult
1417        // because it requires syncing what the mapping holds with the file
1418        // that resides on the host system. So, any write on a real system
1419        // would cause the change to be propagated to the file mapping at
1420        // some point in the future (the inode is tracked along with the
1421        // mapping). This isn't guaranteed to always happen, but it usually
1422        // works well enough. The guarantee is provided by the msync system
1423        // call. We could force the change through with shared mappings with
1424        // a call to msync, but that again would require more information
1425        // than we currently maintain.
1426        warn("mmap: writing to shared mmap region is currently "
1427             "unsupported. The write succeeds on the target, but it "
1428             "will not be propagated to the host or shared mappings");
1429    }
1430
1431    length = roundUp(length, TheISA::PageBytes);
1432
1433    int sim_fd = -1;
1434    uint8_t *pmap = nullptr;
1435    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
1436        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1437
1438        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
1439        if (dfdp) {
1440            EmulatedDriver *emul_driver = dfdp->getDriver();
1441            return emul_driver->mmap(p, tc, start, length, prot,
1442                                     tgt_flags, tgt_fd, offset);
1443        }
1444
1445        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
1446        if (!ffdp)
1447            return -EBADF;
1448        sim_fd = ffdp->getSimFD();
1449
1450        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1451                                    sim_fd, offset);
1452
1453        if (pmap == (decltype(pmap))-1) {
1454            warn("mmap: failed to map file into host address space");
1455            return -errno;
1456        }
1457    }
1458
1459    // Extend global mmap region if necessary. Note that we ignore the
1460    // start address unless MAP_FIXED is specified.
1461    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
1462        std::shared_ptr<MemState> mem_state = p->memState;
1463        Addr mmap_end = mem_state->getMmapEnd();
1464
1465        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
1466        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
1467
1468        mem_state->setMmapEnd(mmap_end);
1469    }
1470
1471    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
1472                    start, start + length - 1);
1473
1474    // We only allow mappings to overwrite existing mappings if
1475    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
1476    // because we ignore the start hint if TGT_MAP_FIXED is not set.
1477    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
1478    if (clobber) {
1479        for (auto tc : p->system->threadContexts) {
1480            // If we might be overwriting old mappings, we need to
1481            // invalidate potentially stale mappings out of the TLBs.
1482            tc->getDTBPtr()->flushAll();
1483            tc->getITBPtr()->flushAll();
1484        }
1485    }
1486
1487    // Allocate physical memory and map it in. If the page table is already
1488    // mapped and clobber is not set, the simulator will issue throw a
1489    // fatal and bail out of the simulation.
1490    p->allocateMem(start, length, clobber);
1491
1492    // Transfer content into target address space.
1493    SETranslatingPortProxy &tp = tc->getMemProxy();
1494    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
1495        // In general, we should zero the mapped area for anonymous mappings,
1496        // with something like:
1497        //     tp.memsetBlob(start, 0, length);
1498        // However, given that we don't support sparse mappings, and
1499        // some applications can map a couple of gigabytes of space
1500        // (intending sparse usage), that can get painfully expensive.
1501        // Fortunately, since we don't properly implement munmap either,
1502        // there's no danger of remapping used memory, so for now all
1503        // newly mapped memory should already be zeroed so we can skip it.
1504    } else {
1505        // It is possible to mmap an area larger than a file, however
1506        // accessing unmapped portions the system triggers a "Bus error"
1507        // on the host. We must know when to stop copying the file from
1508        // the host into the target address space.
1509        struct stat file_stat;
1510        if (fstat(sim_fd, &file_stat) > 0)
1511            fatal("mmap: cannot stat file");
1512
1513        // Copy the portion of the file that is resident. This requires
1514        // checking both the mmap size and the filesize that we are
1515        // trying to mmap into this space; the mmap size also depends
1516        // on the specified offset into the file.
1517        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
1518                                 length);
1519        tp.writeBlob(start, pmap, size);
1520
1521        // Cleanup the mmap region before exiting this function.
1522        munmap(pmap, length);
1523
1524        // Maintain the symbol table for dynamic executables.
1525        // The loader will call mmap to map the images into its address
1526        // space and we intercept that here. We can verify that we are
1527        // executing inside the loader by checking the program counter value.
1528        // XXX: with multiprogrammed workloads or multi-node configurations,
1529        // this will not work since there is a single global symbol table.
1530        ObjectFile *interpreter = p->getInterpreter();
1531        if (interpreter) {
1532            Addr text_start = interpreter->textBase();
1533            Addr text_end = text_start + interpreter->textSize();
1534
1535            Addr pc = tc->pcState().pc();
1536
1537            if (pc >= text_start && pc < text_end) {
1538                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1539                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
1540                ObjectFile *lib = createObjectFile(ffdp->getFileName());
1541
1542                if (lib) {
1543                    lib->loadAllSymbols(debugSymbolTable,
1544                                        lib->textBase(), start);
1545                }
1546            }
1547        }
1548
1549        // Note that we do not zero out the remainder of the mapping. This
1550        // is done by a real system, but it probably will not affect
1551        // execution (hopefully).
1552    }
1553
1554    return start;
1555}
1556
1557template <class OS>
1558SyscallReturn
1559pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1560{
1561    int index = 0;
1562    int tgt_fd = p->getSyscallArg(tc, index);
1563    Addr bufPtr = p->getSyscallArg(tc, index);
1564    int nbytes = p->getSyscallArg(tc, index);
1565    int offset = p->getSyscallArg(tc, index);
1566
1567    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1568    if (!ffdp)
1569        return -EBADF;
1570    int sim_fd = ffdp->getSimFD();
1571
1572    BufferArg bufArg(bufPtr, nbytes);
1573    bufArg.copyIn(tc->getMemProxy());
1574
1575    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
1576
1577    return (bytes_written == -1) ? -errno : bytes_written;
1578}
1579
1580/// Target mmap() handler.
1581template <class OS>
1582SyscallReturn
1583mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1584{
1585    return mmapImpl<OS>(desc, num, p, tc, false);
1586}
1587
1588/// Target mmap2() handler.
1589template <class OS>
1590SyscallReturn
1591mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1592{
1593    return mmapImpl<OS>(desc, num, p, tc, true);
1594}
1595
1596/// Target getrlimit() handler.
1597template <class OS>
1598SyscallReturn
1599getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
1600              ThreadContext *tc)
1601{
1602    int index = 0;
1603    unsigned resource = process->getSyscallArg(tc, index);
1604    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1605
1606    switch (resource) {
1607      case OS::TGT_RLIMIT_STACK:
1608        // max stack size in bytes: make up a number (8MB for now)
1609        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1610        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1611        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1612        break;
1613
1614      case OS::TGT_RLIMIT_DATA:
1615        // max data segment size in bytes: make up a number
1616        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1617        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1618        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1619        break;
1620
1621      default:
1622        warn("getrlimit: unimplemented resource %d", resource);
1623        return -EINVAL;
1624        break;
1625    }
1626
1627    rlp.copyOut(tc->getMemProxy());
1628    return 0;
1629}
1630
1631/// Target clock_gettime() function.
1632template <class OS>
1633SyscallReturn
1634clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1635{
1636    int index = 1;
1637    //int clk_id = p->getSyscallArg(tc, index);
1638    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1639
1640    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
1641    tp->tv_sec += seconds_since_epoch;
1642    tp->tv_sec = TheISA::htog(tp->tv_sec);
1643    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
1644
1645    tp.copyOut(tc->getMemProxy());
1646
1647    return 0;
1648}
1649
1650/// Target clock_getres() function.
1651template <class OS>
1652SyscallReturn
1653clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1654{
1655    int index = 1;
1656    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1657
1658    // Set resolution at ns, which is what clock_gettime() returns
1659    tp->tv_sec = 0;
1660    tp->tv_nsec = 1;
1661
1662    tp.copyOut(tc->getMemProxy());
1663
1664    return 0;
1665}
1666
1667/// Target gettimeofday() handler.
1668template <class OS>
1669SyscallReturn
1670gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
1671                 ThreadContext *tc)
1672{
1673    int index = 0;
1674    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1675
1676    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1677    tp->tv_sec += seconds_since_epoch;
1678    tp->tv_sec = TheISA::htog(tp->tv_sec);
1679    tp->tv_usec = TheISA::htog(tp->tv_usec);
1680
1681    tp.copyOut(tc->getMemProxy());
1682
1683    return 0;
1684}
1685
1686
1687/// Target utimes() handler.
1688template <class OS>
1689SyscallReturn
1690utimesFunc(SyscallDesc *desc, int callnum, Process *process,
1691           ThreadContext *tc)
1692{
1693    std::string path;
1694
1695    int index = 0;
1696    if (!tc->getMemProxy().tryReadString(path,
1697                process->getSyscallArg(tc, index))) {
1698        return -EFAULT;
1699    }
1700
1701    TypedBufferArg<typename OS::timeval [2]>
1702        tp(process->getSyscallArg(tc, index));
1703    tp.copyIn(tc->getMemProxy());
1704
1705    struct timeval hostTimeval[2];
1706    for (int i = 0; i < 2; ++i) {
1707        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1708        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1709    }
1710
1711    // Adjust path for current working directory
1712    path = process->fullPath(path);
1713
1714    int result = utimes(path.c_str(), hostTimeval);
1715
1716    if (result < 0)
1717        return -errno;
1718
1719    return 0;
1720}
1721
1722template <class OS>
1723SyscallReturn
1724execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1725{
1726    desc->setFlags(0);
1727
1728    int index = 0;
1729    std::string path;
1730    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
1731    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
1732        return -EFAULT;
1733
1734    if (access(path.c_str(), F_OK) == -1)
1735        return -EACCES;
1736
1737    auto read_in = [](std::vector<std::string> & vect,
1738                      SETranslatingPortProxy & mem_proxy,
1739                      Addr mem_loc)
1740    {
1741        for (int inc = 0; ; inc++) {
1742            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
1743            b.copyIn(mem_proxy);
1744
1745            if (!*(Addr*)b.bufferPtr())
1746                break;
1747
1748            vect.push_back(std::string());
1749            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
1750        }
1751    };
1752
1753    /**
1754     * Note that ProcessParams is generated by swig and there are no other
1755     * examples of how to create anything but this default constructor. The
1756     * fields are manually initialized instead of passing parameters to the
1757     * constructor.
1758     */
1759    ProcessParams *pp = new ProcessParams();
1760    pp->executable = path;
1761    Addr argv_mem_loc = p->getSyscallArg(tc, index);
1762    read_in(pp->cmd, mem_proxy, argv_mem_loc);
1763    Addr envp_mem_loc = p->getSyscallArg(tc, index);
1764    read_in(pp->env, mem_proxy, envp_mem_loc);
1765    pp->uid = p->uid();
1766    pp->egid = p->egid();
1767    pp->euid = p->euid();
1768    pp->gid = p->gid();
1769    pp->ppid = p->ppid();
1770    pp->pid = p->pid();
1771    pp->input.assign("cin");
1772    pp->output.assign("cout");
1773    pp->errout.assign("cerr");
1774    pp->cwd.assign(p->getcwd());
1775    pp->system = p->system;
1776    /**
1777     * Prevent process object creation with identical PIDs (which will trip
1778     * a fatal check in Process constructor). The execve call is supposed to
1779     * take over the currently executing process' identity but replace
1780     * whatever it is doing with a new process image. Instead of hijacking
1781     * the process object in the simulator, we create a new process object
1782     * and bind to the previous process' thread below (hijacking the thread).
1783     */
1784    p->system->PIDs.erase(p->pid());
1785    Process *new_p = pp->create();
1786    delete pp;
1787
1788    /**
1789     * Work through the file descriptor array and close any files marked
1790     * close-on-exec.
1791     */
1792    new_p->fds = p->fds;
1793    for (int i = 0; i < new_p->fds->getSize(); i++) {
1794        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
1795        if (fdep && fdep->getCOE())
1796            new_p->fds->closeFDEntry(i);
1797    }
1798
1799    *new_p->sigchld = true;
1800
1801    delete p;
1802    tc->clearArchRegs();
1803    tc->setProcessPtr(new_p);
1804    new_p->assignThreadContext(tc->contextId());
1805    new_p->initState();
1806    tc->activate();
1807    TheISA::PCState pcState = tc->pcState();
1808    tc->setNPC(pcState.instAddr());
1809
1810    desc->setFlags(SyscallDesc::SuppressReturnValue);
1811    return 0;
1812}
1813
1814/// Target getrusage() function.
1815template <class OS>
1816SyscallReturn
1817getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
1818              ThreadContext *tc)
1819{
1820    int index = 0;
1821    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1822    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1823
1824    rup->ru_utime.tv_sec = 0;
1825    rup->ru_utime.tv_usec = 0;
1826    rup->ru_stime.tv_sec = 0;
1827    rup->ru_stime.tv_usec = 0;
1828    rup->ru_maxrss = 0;
1829    rup->ru_ixrss = 0;
1830    rup->ru_idrss = 0;
1831    rup->ru_isrss = 0;
1832    rup->ru_minflt = 0;
1833    rup->ru_majflt = 0;
1834    rup->ru_nswap = 0;
1835    rup->ru_inblock = 0;
1836    rup->ru_oublock = 0;
1837    rup->ru_msgsnd = 0;
1838    rup->ru_msgrcv = 0;
1839    rup->ru_nsignals = 0;
1840    rup->ru_nvcsw = 0;
1841    rup->ru_nivcsw = 0;
1842
1843    switch (who) {
1844      case OS::TGT_RUSAGE_SELF:
1845        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1846        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1847        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1848        break;
1849
1850      case OS::TGT_RUSAGE_CHILDREN:
1851        // do nothing.  We have no child processes, so they take no time.
1852        break;
1853
1854      default:
1855        // don't really handle THREAD or CHILDREN, but just warn and
1856        // plow ahead
1857        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
1858             who);
1859    }
1860
1861    rup.copyOut(tc->getMemProxy());
1862
1863    return 0;
1864}
1865
1866/// Target times() function.
1867template <class OS>
1868SyscallReturn
1869timesFunc(SyscallDesc *desc, int callnum, Process *process,
1870          ThreadContext *tc)
1871{
1872    int index = 0;
1873    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1874
1875    // Fill in the time structure (in clocks)
1876    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1877    bufp->tms_utime = clocks;
1878    bufp->tms_stime = 0;
1879    bufp->tms_cutime = 0;
1880    bufp->tms_cstime = 0;
1881
1882    // Convert to host endianness
1883    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1884
1885    // Write back
1886    bufp.copyOut(tc->getMemProxy());
1887
1888    // Return clock ticks since system boot
1889    return clocks;
1890}
1891
1892/// Target time() function.
1893template <class OS>
1894SyscallReturn
1895timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
1896{
1897    typename OS::time_t sec, usec;
1898    getElapsedTimeMicro(sec, usec);
1899    sec += seconds_since_epoch;
1900
1901    int index = 0;
1902    Addr taddr = (Addr)process->getSyscallArg(tc, index);
1903    if (taddr != 0) {
1904        typename OS::time_t t = sec;
1905        t = TheISA::htog(t);
1906        SETranslatingPortProxy &p = tc->getMemProxy();
1907        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1908    }
1909    return sec;
1910}
1911
1912
1913#endif // __SIM_SYSCALL_EMUL_HH__
1914