2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * Copyright (c) 2015 Advanced Micro Devices, Inc.
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.
15 * Copyright (c) 2003-2005 The Regents of The University of Michigan
16 * All rights reserved.
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.
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.
41 * Authors: Steve Reinhardt
45 #ifndef __SIM_SYSCALL_EMUL_HH__
46 #define __SIM_SYSCALL_EMUL_HH__
48 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
49 defined(__FreeBSD__) || defined(__CYGWIN__) || \
53 /// @file syscall_emul.hh
55 /// This file defines objects used to emulate syscalls from the target
56 /// application on the host machine.
59 #include <sys/fcntl.h> // for O_BINARY
65 #include <sys/statfs.h>
73 #include "base/chunk_generator.hh"
74 #include "base/intmath.hh" // for RoundUp
75 #include "base/loader/object_file.hh"
76 #include "base/misc.hh"
77 #include "base/trace.hh"
78 #include "base/types.hh"
79 #include "config/the_isa.hh"
80 #include "cpu/base.hh"
81 #include "cpu/thread_context.hh"
82 #include "mem/page_table.hh"
83 #include "sim/byteswap.hh"
84 #include "sim/emul_driver.hh"
85 #include "sim/process.hh"
86 #include "sim/syscall_debug_macros.hh"
87 #include "sim/syscall_emul_buf.hh"
88 #include "sim/syscall_return.hh"
89 #include "sim/system.hh"
93 //////////////////////////////////////////////////////////////////////
95 // The following emulation functions are generic enough that they
96 // don't need to be recompiled for different emulated OS's. They are
97 // defined in sim/syscall_emul.cc.
99 //////////////////////////////////////////////////////////////////////
102 /// Handler for unimplemented syscalls that we haven't thought about.
103 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
104 LiveProcess *p, ThreadContext *tc);
106 /// Handler for unimplemented syscalls that we never intend to
107 /// implement (signal handling, etc.) and should not affect the correct
108 /// behavior of the program. Print a warning only if the appropriate
109 /// trace flag is enabled. Return success to the target program.
110 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
111 LiveProcess *p, ThreadContext *tc);
113 // Target fallocateFunc() handler.
114 SyscallReturn fallocateFunc(SyscallDesc *desc, int num,
115 LiveProcess *p, ThreadContext *tc);
117 /// Target exit() handler: terminate current context.
118 SyscallReturn exitFunc(SyscallDesc *desc, int num,
119 LiveProcess *p, ThreadContext *tc);
121 /// Target exit_group() handler: terminate simulation. (exit all threads)
122 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
123 LiveProcess *p, ThreadContext *tc);
125 /// Target getpagesize() handler.
126 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
127 LiveProcess *p, ThreadContext *tc);
129 /// Target brk() handler: set brk address.
130 SyscallReturn brkFunc(SyscallDesc *desc, int num,
131 LiveProcess *p, ThreadContext *tc);
133 /// Target close() handler.
134 SyscallReturn closeFunc(SyscallDesc *desc, int num,
135 LiveProcess *p, ThreadContext *tc);
137 /// Target read() handler.
138 SyscallReturn readFunc(SyscallDesc *desc, int num,
139 LiveProcess *p, ThreadContext *tc);
141 /// Target write() handler.
142 SyscallReturn writeFunc(SyscallDesc *desc, int num,
143 LiveProcess *p, ThreadContext *tc);
145 /// Target lseek() handler.
146 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
147 LiveProcess *p, ThreadContext *tc);
149 /// Target _llseek() handler.
150 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
151 LiveProcess *p, ThreadContext *tc);
153 /// Target munmap() handler.
154 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
155 LiveProcess *p, ThreadContext *tc);
157 /// Target gethostname() handler.
158 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
159 LiveProcess *p, ThreadContext *tc);
161 /// Target getcwd() handler.
162 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
163 LiveProcess *p, ThreadContext *tc);
165 /// Target readlink() handler.
166 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
167 LiveProcess *p, ThreadContext *tc,
169 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
170 LiveProcess *p, ThreadContext *tc);
172 /// Target unlink() handler.
173 SyscallReturn unlinkHelper(SyscallDesc *desc, int num,
174 LiveProcess *p, ThreadContext *tc,
176 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
177 LiveProcess *p, ThreadContext *tc);
179 /// Target mkdir() handler.
180 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
181 LiveProcess *p, ThreadContext *tc);
183 /// Target rename() handler.
184 SyscallReturn renameFunc(SyscallDesc *desc, int num,
185 LiveProcess *p, ThreadContext *tc);
188 /// Target truncate() handler.
189 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
190 LiveProcess *p, ThreadContext *tc);
193 /// Target ftruncate() handler.
194 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
195 LiveProcess *p, ThreadContext *tc);
198 /// Target truncate64() handler.
199 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
200 LiveProcess *p, ThreadContext *tc);
202 /// Target ftruncate64() handler.
203 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
204 LiveProcess *p, ThreadContext *tc);
207 /// Target umask() handler.
208 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
209 LiveProcess *p, ThreadContext *tc);
212 /// Target chown() handler.
213 SyscallReturn chownFunc(SyscallDesc *desc, int num,
214 LiveProcess *p, ThreadContext *tc);
217 /// Target fchown() handler.
218 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
219 LiveProcess *p, ThreadContext *tc);
221 /// Target dup() handler.
222 SyscallReturn dupFunc(SyscallDesc *desc, int num,
223 LiveProcess *process, ThreadContext *tc);
225 /// Target fnctl() handler.
226 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
227 LiveProcess *process, ThreadContext *tc);
229 /// Target fcntl64() handler.
230 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
231 LiveProcess *process, ThreadContext *tc);
233 /// Target setuid() handler.
234 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
235 LiveProcess *p, ThreadContext *tc);
237 /// Target getpid() handler.
238 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
239 LiveProcess *p, ThreadContext *tc);
241 /// Target getuid() handler.
242 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
243 LiveProcess *p, ThreadContext *tc);
245 /// Target getgid() handler.
246 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
247 LiveProcess *p, ThreadContext *tc);
249 /// Target getppid() handler.
250 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
251 LiveProcess *p, ThreadContext *tc);
253 /// Target geteuid() handler.
254 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
255 LiveProcess *p, ThreadContext *tc);
257 /// Target getegid() handler.
258 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
259 LiveProcess *p, ThreadContext *tc);
261 /// Target clone() handler.
262 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
263 LiveProcess *p, ThreadContext *tc);
265 /// Target access() handler
266 SyscallReturn accessFunc(SyscallDesc *desc, int num,
267 LiveProcess *p, ThreadContext *tc);
268 SyscallReturn accessFunc(SyscallDesc *desc, int num,
269 LiveProcess *p, ThreadContext *tc,
272 /// Futex system call
273 /// Implemented by Daniel Sanchez
274 /// Used by printf's in multi-threaded apps
277 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
283 int index_timeout = 3;
285 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
286 int op = process->getSyscallArg(tc, index_op);
287 int val = process->getSyscallArg(tc, index_val);
288 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
290 std::map<uint64_t, std::list<ThreadContext *> * >
291 &futex_map = tc->getSystemPtr()->futexMap;
293 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
296 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
298 if (op == OS::TGT_FUTEX_WAIT) {
300 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
301 "we'll wait indefinitely");
304 uint8_t *buf = new uint8_t[sizeof(int)];
305 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
306 int mem_val = *((int *)buf);
309 if (val != mem_val) {
310 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
311 "expected: %d\n", mem_val, val);
312 return -OS::TGT_EWOULDBLOCK;
315 // Queue the thread context
316 std::list<ThreadContext *> * tcWaitList;
317 if (futex_map.count(uaddr)) {
318 tcWaitList = futex_map.find(uaddr)->second;
320 tcWaitList = new std::list<ThreadContext *>();
321 futex_map.insert(std::pair< uint64_t,
322 std::list<ThreadContext *> * >(uaddr, tcWaitList));
324 tcWaitList->push_back(tc);
325 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
329 } else if (op == OS::TGT_FUTEX_WAKE){
331 std::list<ThreadContext *> * tcWaitList;
332 if (futex_map.count(uaddr)) {
333 tcWaitList = futex_map.find(uaddr)->second;
334 while (tcWaitList->size() > 0 && wokenUp < val) {
335 tcWaitList->front()->activate();
336 tcWaitList->pop_front();
339 if (tcWaitList->empty()) {
340 futex_map.erase(uaddr);
344 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
345 "thread contexts\n", wokenUp);
348 warn("sys_futex: op %d is not implemented, just returning...", op);
355 /// Pseudo Funcs - These functions use a different return convension,
356 /// returning a second value in a register other than the normal return register
357 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
358 LiveProcess *process, ThreadContext *tc);
360 /// Target getpidPseudo() handler.
361 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
362 LiveProcess *p, ThreadContext *tc);
364 /// Target getuidPseudo() handler.
365 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
366 LiveProcess *p, ThreadContext *tc);
368 /// Target getgidPseudo() handler.
369 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
370 LiveProcess *p, ThreadContext *tc);
373 /// A readable name for 1,000,000, for converting microseconds to seconds.
374 const int one_million = 1000000;
375 /// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
376 const int one_billion = 1000000000;
378 /// Approximate seconds since the epoch (1/1/1970). About a billion,
379 /// by my reckoning. We want to keep this a constant (not use the
380 /// real-world time) to keep simulations repeatable.
381 const unsigned seconds_since_epoch = 1000000000;
383 /// Helper function to convert current elapsed time to seconds and
385 template <class T1, class T2>
387 getElapsedTimeMicro(T1 &sec, T2 &usec)
389 uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
390 sec = elapsed_usecs / one_million;
391 usec = elapsed_usecs % one_million;
394 /// Helper function to convert current elapsed time to seconds and
396 template <class T1, class T2>
398 getElapsedTimeNano(T1 &sec, T2 &nsec)
400 uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
401 sec = elapsed_nsecs / one_billion;
402 nsec = elapsed_nsecs % one_billion;
405 //////////////////////////////////////////////////////////////////////
407 // The following emulation functions are generic, but need to be
408 // templated to account for differences in types, constants, etc.
410 //////////////////////////////////////////////////////////////////////
412 typedef struct statfs hst_statfs;
414 typedef struct stat hst_stat;
415 typedef struct stat hst_stat64;
417 typedef struct stat hst_stat;
418 typedef struct stat64 hst_stat64;
421 //// Helper function to convert a host stat buffer to a target stat
422 //// buffer. Also copies the target buffer out to the simulated
423 //// memory space. Used by stat(), fstat(), and lstat().
425 template <typename target_stat, typename host_stat>
427 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
429 using namespace TheISA;
434 tgt->st_dev = host->st_dev;
435 tgt->st_dev = TheISA::htog(tgt->st_dev);
436 tgt->st_ino = host->st_ino;
437 tgt->st_ino = TheISA::htog(tgt->st_ino);
438 tgt->st_mode = host->st_mode;
440 // Claim to be a character device
441 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
442 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
444 tgt->st_mode = TheISA::htog(tgt->st_mode);
445 tgt->st_nlink = host->st_nlink;
446 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
447 tgt->st_uid = host->st_uid;
448 tgt->st_uid = TheISA::htog(tgt->st_uid);
449 tgt->st_gid = host->st_gid;
450 tgt->st_gid = TheISA::htog(tgt->st_gid);
452 tgt->st_rdev = 0x880d;
454 tgt->st_rdev = host->st_rdev;
455 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
456 tgt->st_size = host->st_size;
457 tgt->st_size = TheISA::htog(tgt->st_size);
458 tgt->st_atimeX = host->st_atime;
459 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
460 tgt->st_mtimeX = host->st_mtime;
461 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
462 tgt->st_ctimeX = host->st_ctime;
463 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
464 // Force the block size to be 8k. This helps to ensure buffered io works
465 // consistently across different hosts.
466 tgt->st_blksize = 0x2000;
467 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
468 tgt->st_blocks = host->st_blocks;
469 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
474 template <typename target_stat, typename host_stat64>
476 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
478 using namespace TheISA;
480 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
481 #if defined(STAT_HAVE_NSEC)
482 tgt->st_atime_nsec = host->st_atime_nsec;
483 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
484 tgt->st_mtime_nsec = host->st_mtime_nsec;
485 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
486 tgt->st_ctime_nsec = host->st_ctime_nsec;
487 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
489 tgt->st_atime_nsec = 0;
490 tgt->st_mtime_nsec = 0;
491 tgt->st_ctime_nsec = 0;
495 //Here are a couple convenience functions
498 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
499 hst_stat *host, bool fakeTTY = false)
501 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
502 tgt_stat_buf tgt(addr);
503 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
509 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
510 hst_stat64 *host, bool fakeTTY = false)
512 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
513 tgt_stat_buf tgt(addr);
514 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
520 copyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
523 TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
525 #if defined(__OpenBSD__) || defined(__APPLE__) || defined(__FreeBSD__)
528 tgt->f_type = TheISA::htog(host->f_type);
530 tgt->f_bsize = TheISA::htog(host->f_bsize);
531 tgt->f_blocks = TheISA::htog(host->f_blocks);
532 tgt->f_bfree = TheISA::htog(host->f_bfree);
533 tgt->f_bavail = TheISA::htog(host->f_bavail);
534 tgt->f_files = TheISA::htog(host->f_files);
535 tgt->f_ffree = TheISA::htog(host->f_ffree);
536 memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
537 tgt->f_namelen = TheISA::htog(host->f_namelen);
538 tgt->f_frsize = TheISA::htog(host->f_frsize);
539 memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
544 /// Target ioctl() handler. For the most part, programs call ioctl()
545 /// only to find out if their stdout is a tty, to determine whether to
546 /// do line or block buffering. We always claim that output fds are
547 /// not TTYs to provide repeatable results.
550 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
554 int tgt_fd = process->getSyscallArg(tc, index);
555 unsigned req = process->getSyscallArg(tc, index);
557 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
559 FDEntry *fde = process->getFDEntry(tgt_fd);
562 // doesn't map to any simulator fd: not a valid target fd
566 if (fde->driver != NULL) {
567 return fde->driver->ioctl(process, tc, req);
570 if (OS::isTtyReq(req)) {
574 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
575 tgt_fd, req, tc->pcState());
581 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
582 ThreadContext *tc, int index)
586 if (!tc->getMemProxy().tryReadString(path,
587 process->getSyscallArg(tc, index)))
590 int tgtFlags = process->getSyscallArg(tc, index);
591 int mode = process->getSyscallArg(tc, index);
594 // translate open flags
595 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
596 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
597 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
598 hostFlags |= OS::openFlagTable[i].hostFlag;
602 // any target flags left?
604 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
607 hostFlags |= O_BINARY;
610 // Adjust path for current working directory
611 path = process->fullPath(path);
613 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
615 if (startswith(path, "/dev/")) {
616 std::string filename = path.substr(strlen("/dev/"));
617 if (filename == "sysdev0") {
618 // This is a memory-mapped high-resolution timer device on Alpha.
619 // We don't support it, so just punt.
620 warn("Ignoring open(%s, ...)\n", path);
624 EmulatedDriver *drv = process->findDriver(filename);
626 // the driver's open method will allocate a fd from the
627 // process if necessary.
628 return drv->open(process, tc, mode, hostFlags);
631 // fall through here for pass through to host devices, such as
637 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
638 startswith(path, "/platform/") || startswith(path, "/sys/")) {
639 // It's a proc/sys entry and requires special handling
640 fd = OS::openSpecialFile(path, process, tc);
641 local_errno = ENOENT;
644 fd = open(path.c_str(), hostFlags, mode);
651 return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
654 /// Target open() handler.
657 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
660 return openFunc<OS>(desc, callnum, process, tc, 0);
663 /// Target openat() handler.
666 openatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
670 int dirfd = process->getSyscallArg(tc, index);
671 if (dirfd != OS::TGT_AT_FDCWD)
672 warn("openat: first argument not AT_FDCWD; unlikely to work");
673 return openFunc<OS>(desc, callnum, process, tc, 1);
676 /// Target unlinkat() handler.
679 unlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
683 int dirfd = process->getSyscallArg(tc, index);
684 if (dirfd != OS::TGT_AT_FDCWD)
685 warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
687 return unlinkHelper(desc, callnum, process, tc, 1);
690 /// Target facessat() handler
693 faccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
697 int dirfd = process->getSyscallArg(tc, index);
698 if (dirfd != OS::TGT_AT_FDCWD)
699 warn("faccessat: first argument not AT_FDCWD; unlikely to work");
700 return accessFunc(desc, callnum, process, tc, 1);
703 /// Target readlinkat() handler
706 readlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
710 int dirfd = process->getSyscallArg(tc, index);
711 if (dirfd != OS::TGT_AT_FDCWD)
712 warn("openat: first argument not AT_FDCWD; unlikely to work");
713 return readlinkFunc(desc, callnum, process, tc, 1);
716 /// Target renameat() handler.
719 renameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
724 int olddirfd = process->getSyscallArg(tc, index);
725 if (olddirfd != OS::TGT_AT_FDCWD)
726 warn("renameat: first argument not AT_FDCWD; unlikely to work");
728 std::string old_name;
730 if (!tc->getMemProxy().tryReadString(old_name,
731 process->getSyscallArg(tc, index)))
734 int newdirfd = process->getSyscallArg(tc, index);
735 if (newdirfd != OS::TGT_AT_FDCWD)
736 warn("renameat: third argument not AT_FDCWD; unlikely to work");
738 std::string new_name;
740 if (!tc->getMemProxy().tryReadString(new_name,
741 process->getSyscallArg(tc, index)))
744 // Adjust path for current working directory
745 old_name = process->fullPath(old_name);
746 new_name = process->fullPath(new_name);
748 int result = rename(old_name.c_str(), new_name.c_str());
749 return (result == -1) ? -errno : result;
752 /// Target sysinfo() handler.
755 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
760 TypedBufferArg<typename OS::tgt_sysinfo>
761 sysinfo(process->getSyscallArg(tc, index));
763 sysinfo->uptime = seconds_since_epoch;
764 sysinfo->totalram = process->system->memSize();
765 sysinfo->mem_unit = 1;
767 sysinfo.copyOut(tc->getMemProxy());
772 /// Target chmod() handler.
775 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
781 if (!tc->getMemProxy().tryReadString(path,
782 process->getSyscallArg(tc, index))) {
786 uint32_t mode = process->getSyscallArg(tc, index);
789 // XXX translate mode flags via OS::something???
792 // Adjust path for current working directory
793 path = process->fullPath(path);
796 int result = chmod(path.c_str(), hostMode);
804 /// Target fchmod() handler.
807 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
811 int tgt_fd = process->getSyscallArg(tc, index);
812 uint32_t mode = process->getSyscallArg(tc, index);
814 int sim_fd = process->getSimFD(tgt_fd);
820 // XXX translate mode flags via OS::someting???
824 int result = fchmod(sim_fd, hostMode);
831 /// Target mremap() handler.
834 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
837 Addr start = process->getSyscallArg(tc, index);
838 uint64_t old_length = process->getSyscallArg(tc, index);
839 uint64_t new_length = process->getSyscallArg(tc, index);
840 uint64_t flags = process->getSyscallArg(tc, index);
841 uint64_t provided_address = 0;
842 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
844 if (use_provided_address)
845 provided_address = process->getSyscallArg(tc, index);
847 if ((start % TheISA::PageBytes != 0) ||
848 (provided_address % TheISA::PageBytes != 0)) {
849 warn("mremap failing: arguments not page aligned");
853 new_length = roundUp(new_length, TheISA::PageBytes);
855 if (new_length > old_length) {
856 if ((start + old_length) == process->mmap_end &&
857 (!use_provided_address || provided_address == start)) {
858 uint64_t diff = new_length - old_length;
859 process->allocateMem(process->mmap_end, diff);
860 process->mmap_end += diff;
863 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
864 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
867 uint64_t new_start = use_provided_address ?
868 provided_address : process->mmap_end;
869 process->pTable->remap(start, old_length, new_start);
870 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
871 new_start, new_start + new_length,
872 new_length - old_length);
873 // add on the remaining unallocated pages
874 process->allocateMem(new_start + old_length,
875 new_length - old_length,
876 use_provided_address /* clobber */);
877 if (!use_provided_address)
878 process->mmap_end += new_length;
879 if (use_provided_address &&
880 new_start + new_length > process->mmap_end) {
881 // something fishy going on here, at least notify the user
882 // @todo: increase mmap_end?
883 warn("mmap region limit exceeded with MREMAP_FIXED\n");
885 warn("returning %08p as start\n", new_start);
890 if (use_provided_address && provided_address != start)
891 process->pTable->remap(start, new_length, provided_address);
892 process->pTable->unmap(start + new_length, old_length - new_length);
893 return use_provided_address ? provided_address : start;
897 /// Target stat() handler.
900 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
906 if (!tc->getMemProxy().tryReadString(path,
907 process->getSyscallArg(tc, index))) {
910 Addr bufPtr = process->getSyscallArg(tc, index);
912 // Adjust path for current working directory
913 path = process->fullPath(path);
916 int result = stat(path.c_str(), &hostBuf);
921 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
927 /// Target stat64() handler.
930 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
936 if (!tc->getMemProxy().tryReadString(path,
937 process->getSyscallArg(tc, index)))
939 Addr bufPtr = process->getSyscallArg(tc, index);
941 // Adjust path for current working directory
942 path = process->fullPath(path);
946 int result = stat(path.c_str(), &hostBuf);
948 struct stat64 hostBuf;
949 int result = stat64(path.c_str(), &hostBuf);
955 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
961 /// Target fstatat64() handler.
964 fstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
968 int dirfd = process->getSyscallArg(tc, index);
969 if (dirfd != OS::TGT_AT_FDCWD)
970 warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
973 if (!tc->getMemProxy().tryReadString(path,
974 process->getSyscallArg(tc, index)))
976 Addr bufPtr = process->getSyscallArg(tc, index);
978 // Adjust path for current working directory
979 path = process->fullPath(path);
983 int result = stat(path.c_str(), &hostBuf);
985 struct stat64 hostBuf;
986 int result = stat64(path.c_str(), &hostBuf);
992 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
998 /// Target fstat64() handler.
1001 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1005 int tgt_fd = process->getSyscallArg(tc, index);
1006 Addr bufPtr = process->getSyscallArg(tc, index);
1008 int sim_fd = process->getSimFD(tgt_fd);
1013 struct stat hostBuf;
1014 int result = fstat(sim_fd, &hostBuf);
1016 struct stat64 hostBuf;
1017 int result = fstat64(sim_fd, &hostBuf);
1023 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1029 /// Target lstat() handler.
1032 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1038 if (!tc->getMemProxy().tryReadString(path,
1039 process->getSyscallArg(tc, index))) {
1042 Addr bufPtr = process->getSyscallArg(tc, index);
1044 // Adjust path for current working directory
1045 path = process->fullPath(path);
1047 struct stat hostBuf;
1048 int result = lstat(path.c_str(), &hostBuf);
1053 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1058 /// Target lstat64() handler.
1061 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1067 if (!tc->getMemProxy().tryReadString(path,
1068 process->getSyscallArg(tc, index))) {
1071 Addr bufPtr = process->getSyscallArg(tc, index);
1073 // Adjust path for current working directory
1074 path = process->fullPath(path);
1077 struct stat hostBuf;
1078 int result = lstat(path.c_str(), &hostBuf);
1080 struct stat64 hostBuf;
1081 int result = lstat64(path.c_str(), &hostBuf);
1087 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1092 /// Target fstat() handler.
1095 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1099 int tgt_fd = process->getSyscallArg(tc, index);
1100 Addr bufPtr = process->getSyscallArg(tc, index);
1102 DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1104 int sim_fd = process->getSimFD(tgt_fd);
1108 struct stat hostBuf;
1109 int result = fstat(sim_fd, &hostBuf);
1114 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1120 /// Target statfs() handler.
1123 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1129 if (!tc->getMemProxy().tryReadString(path,
1130 process->getSyscallArg(tc, index))) {
1133 Addr bufPtr = process->getSyscallArg(tc, index);
1135 // Adjust path for current working directory
1136 path = process->fullPath(path);
1138 struct statfs hostBuf;
1139 int result = statfs(path.c_str(), &hostBuf);
1144 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1150 /// Target fstatfs() handler.
1153 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1157 int tgt_fd = process->getSyscallArg(tc, index);
1158 Addr bufPtr = process->getSyscallArg(tc, index);
1160 int sim_fd = process->getSimFD(tgt_fd);
1164 struct statfs hostBuf;
1165 int result = fstatfs(sim_fd, &hostBuf);
1170 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1176 /// Target writev() handler.
1179 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1183 int tgt_fd = process->getSyscallArg(tc, index);
1185 int sim_fd = process->getSimFD(tgt_fd);
1189 SETranslatingPortProxy &p = tc->getMemProxy();
1190 uint64_t tiov_base = process->getSyscallArg(tc, index);
1191 size_t count = process->getSyscallArg(tc, index);
1192 struct iovec hiov[count];
1193 for (size_t i = 0; i < count; ++i) {
1194 typename OS::tgt_iovec tiov;
1196 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1197 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1198 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1199 hiov[i].iov_base = new char [hiov[i].iov_len];
1200 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1204 int result = writev(sim_fd, hiov, count);
1206 for (size_t i = 0; i < count; ++i)
1207 delete [] (char *)hiov[i].iov_base;
1215 /// Real mmap handler.
1218 mmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
1222 Addr start = p->getSyscallArg(tc, index);
1223 uint64_t length = p->getSyscallArg(tc, index);
1224 int prot = p->getSyscallArg(tc, index);
1225 int tgt_flags = p->getSyscallArg(tc, index);
1226 int tgt_fd = p->getSyscallArg(tc, index);
1227 int offset = p->getSyscallArg(tc, index);
1230 offset *= TheISA::PageBytes;
1232 if (start & (TheISA::PageBytes - 1) ||
1233 offset & (TheISA::PageBytes - 1) ||
1234 (tgt_flags & OS::TGT_MAP_PRIVATE &&
1235 tgt_flags & OS::TGT_MAP_SHARED) ||
1236 (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
1237 !(tgt_flags & OS::TGT_MAP_SHARED)) ||
1242 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
1243 // With shared mmaps, there are two cases to consider:
1244 // 1) anonymous: writes should modify the mapping and this should be
1245 // visible to observers who share the mapping. Currently, it's
1246 // difficult to update the shared mapping because there's no
1247 // structure which maintains information about the which virtual
1248 // memory areas are shared. If that structure existed, it would be
1249 // possible to make the translations point to the same frames.
1250 // 2) file-backed: writes should modify the mapping and the file
1251 // which is backed by the mapping. The shared mapping problem is the
1252 // same as what was mentioned about the anonymous mappings. For
1253 // file-backed mappings, the writes to the file are difficult
1254 // because it requires syncing what the mapping holds with the file
1255 // that resides on the host system. So, any write on a real system
1256 // would cause the change to be propagated to the file mapping at
1257 // some point in the future (the inode is tracked along with the
1258 // mapping). This isn't guaranteed to always happen, but it usually
1259 // works well enough. The guarantee is provided by the msync system
1260 // call. We could force the change through with shared mappings with
1261 // a call to msync, but that again would require more information
1262 // than we currently maintain.
1263 warn("mmap: writing to shared mmap region is currently "
1264 "unsupported. The write succeeds on the target, but it "
1265 "will not be propagated to the host or shared mappings");
1268 length = roundUp(length, TheISA::PageBytes);
1271 uint8_t *pmap = nullptr;
1272 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
1273 // Check for EmulatedDriver mmap
1274 FDEntry *fde = p->getFDEntry(tgt_fd);
1278 if (fde->driver != NULL) {
1279 return fde->driver->mmap(p, tc, start, length, prot,
1280 tgt_flags, tgt_fd, offset);
1287 pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1290 if (pmap == (decltype(pmap))-1) {
1291 warn("mmap: failed to map file into host address space");
1296 // Extend global mmap region if necessary. Note that we ignore the
1297 // start address unless MAP_FIXED is specified.
1298 if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
1299 start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end;
1300 p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length;
1303 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
1304 start, start + length - 1);
1306 // We only allow mappings to overwrite existing mappings if
1307 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
1308 // because we ignore the start hint if TGT_MAP_FIXED is not set.
1309 int clobber = tgt_flags & OS::TGT_MAP_FIXED;
1311 for (auto tc : p->system->threadContexts) {
1312 // If we might be overwriting old mappings, we need to
1313 // invalidate potentially stale mappings out of the TLBs.
1314 tc->getDTBPtr()->flushAll();
1315 tc->getITBPtr()->flushAll();
1319 // Allocate physical memory and map it in. If the page table is already
1320 // mapped and clobber is not set, the simulator will issue throw a
1321 // fatal and bail out of the simulation.
1322 p->allocateMem(start, length, clobber);
1324 // Transfer content into target address space.
1325 SETranslatingPortProxy &tp = tc->getMemProxy();
1326 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
1327 // In general, we should zero the mapped area for anonymous mappings,
1328 // with something like:
1329 // tp.memsetBlob(start, 0, length);
1330 // However, given that we don't support sparse mappings, and
1331 // some applications can map a couple of gigabytes of space
1332 // (intending sparse usage), that can get painfully expensive.
1333 // Fortunately, since we don't properly implement munmap either,
1334 // there's no danger of remapping used memory, so for now all
1335 // newly mapped memory should already be zeroed so we can skip it.
1337 // It is possible to mmap an area larger than a file, however
1338 // accessing unmapped portions the system triggers a "Bus error"
1339 // on the host. We must know when to stop copying the file from
1340 // the host into the target address space.
1341 struct stat file_stat;
1342 if (fstat(sim_fd, &file_stat) > 0)
1343 fatal("mmap: cannot stat file");
1345 // Copy the portion of the file that is resident. This requires
1346 // checking both the mmap size and the filesize that we are
1347 // trying to mmap into this space; the mmap size also depends
1348 // on the specified offset into the file.
1349 uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
1351 tp.writeBlob(start, pmap, size);
1353 // Cleanup the mmap region before exiting this function.
1354 munmap(pmap, length);
1356 // Maintain the symbol table for dynamic executables.
1357 // The loader will call mmap to map the images into its address
1358 // space and we intercept that here. We can verify that we are
1359 // executing inside the loader by checking the program counter value.
1360 // XXX: with multiprogrammed workloads or multi-node configurations,
1361 // this will not work since there is a single global symbol table.
1362 ObjectFile *interpreter = p->getInterpreter();
1364 Addr text_start = interpreter->textBase();
1365 Addr text_end = text_start + interpreter->textSize();
1367 Addr pc = tc->pcState().pc();
1369 if (pc >= text_start && pc < text_end) {
1370 FDEntry *fde = p->getFDEntry(tgt_fd);
1372 ObjectFile *lib = createObjectFile(fde->filename);
1375 lib->loadAllSymbols(debugSymbolTable,
1376 lib->textBase(), start);
1381 // Note that we do not zero out the remainder of the mapping. This
1382 // is done by a real system, but it probably will not affect
1383 // execution (hopefully).
1391 pwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1394 int tgt_fd = p->getSyscallArg(tc, index);
1395 Addr bufPtr = p->getSyscallArg(tc, index);
1396 int nbytes = p->getSyscallArg(tc, index);
1397 int offset = p->getSyscallArg(tc, index);
1399 int sim_fd = p->getSimFD(tgt_fd);
1403 BufferArg bufArg(bufPtr, nbytes);
1404 bufArg.copyIn(tc->getMemProxy());
1406 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
1408 return (bytes_written == -1) ? -errno : bytes_written;
1411 /// Target mmap() handler.
1414 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1416 return mmapImpl<OS>(desc, num, p, tc, false);
1419 /// Target mmap2() handler.
1422 mmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1424 return mmapImpl<OS>(desc, num, p, tc, true);
1427 /// Target getrlimit() handler.
1430 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1434 unsigned resource = process->getSyscallArg(tc, index);
1435 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1438 case OS::TGT_RLIMIT_STACK:
1439 // max stack size in bytes: make up a number (8MB for now)
1440 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1441 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1442 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1445 case OS::TGT_RLIMIT_DATA:
1446 // max data segment size in bytes: make up a number
1447 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1448 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1449 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1453 warn("getrlimit: unimplemented resource %d", resource);
1458 rlp.copyOut(tc->getMemProxy());
1462 /// Target clock_gettime() function.
1465 clock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1468 //int clk_id = p->getSyscallArg(tc, index);
1469 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1471 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
1472 tp->tv_sec += seconds_since_epoch;
1473 tp->tv_sec = TheISA::htog(tp->tv_sec);
1474 tp->tv_nsec = TheISA::htog(tp->tv_nsec);
1476 tp.copyOut(tc->getMemProxy());
1481 /// Target clock_getres() function.
1484 clock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1487 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1489 // Set resolution at ns, which is what clock_gettime() returns
1493 tp.copyOut(tc->getMemProxy());
1498 /// Target gettimeofday() handler.
1501 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1505 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1507 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1508 tp->tv_sec += seconds_since_epoch;
1509 tp->tv_sec = TheISA::htog(tp->tv_sec);
1510 tp->tv_usec = TheISA::htog(tp->tv_usec);
1512 tp.copyOut(tc->getMemProxy());
1518 /// Target utimes() handler.
1521 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1527 if (!tc->getMemProxy().tryReadString(path,
1528 process->getSyscallArg(tc, index))) {
1532 TypedBufferArg<typename OS::timeval [2]>
1533 tp(process->getSyscallArg(tc, index));
1534 tp.copyIn(tc->getMemProxy());
1536 struct timeval hostTimeval[2];
1537 for (int i = 0; i < 2; ++i)
1539 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1540 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1543 // Adjust path for current working directory
1544 path = process->fullPath(path);
1546 int result = utimes(path.c_str(), hostTimeval);
1553 /// Target getrusage() function.
1556 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1560 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1561 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1563 rup->ru_utime.tv_sec = 0;
1564 rup->ru_utime.tv_usec = 0;
1565 rup->ru_stime.tv_sec = 0;
1566 rup->ru_stime.tv_usec = 0;
1574 rup->ru_inblock = 0;
1575 rup->ru_oublock = 0;
1578 rup->ru_nsignals = 0;
1583 case OS::TGT_RUSAGE_SELF:
1584 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1585 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1586 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1589 case OS::TGT_RUSAGE_CHILDREN:
1590 // do nothing. We have no child processes, so they take no time.
1594 // don't really handle THREAD or CHILDREN, but just warn and
1596 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1600 rup.copyOut(tc->getMemProxy());
1605 /// Target times() function.
1608 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1612 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1614 // Fill in the time structure (in clocks)
1615 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1616 bufp->tms_utime = clocks;
1617 bufp->tms_stime = 0;
1618 bufp->tms_cutime = 0;
1619 bufp->tms_cstime = 0;
1621 // Convert to host endianness
1622 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1625 bufp.copyOut(tc->getMemProxy());
1627 // Return clock ticks since system boot
1631 /// Target time() function.
1634 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1637 typename OS::time_t sec, usec;
1638 getElapsedTimeMicro(sec, usec);
1639 sec += seconds_since_epoch;
1642 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1644 typename OS::time_t t = sec;
1645 t = TheISA::htog(t);
1646 SETranslatingPortProxy &p = tc->getMemProxy();
1647 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1653 #endif // __SIM_SYSCALL_EMUL_HH__