syscall_emul: [PATCH 15/22] add clone/execve for threading and multiprocess simulations
[gem5.git] / src / sim / syscall_emul.hh
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.
112 SyscallReturn 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.
119 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
120 Process *p, ThreadContext *tc);
121
122 // Target fallocateFunc() handler.
123 SyscallReturn fallocateFunc(SyscallDesc *desc, int num,
124 Process *p, ThreadContext *tc);
125
126 /// Target exit() handler: terminate current context.
127 SyscallReturn exitFunc(SyscallDesc *desc, int num,
128 Process *p, ThreadContext *tc);
129
130 /// Target exit_group() handler: terminate simulation. (exit all threads)
131 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
132 Process *p, ThreadContext *tc);
133
134 /// Target set_tid_address() handler.
135 SyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
136 Process *p, ThreadContext *tc);
137
138 /// Target getpagesize() handler.
139 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
140 Process *p, ThreadContext *tc);
141
142 /// Target brk() handler: set brk address.
143 SyscallReturn brkFunc(SyscallDesc *desc, int num,
144 Process *p, ThreadContext *tc);
145
146 /// Target close() handler.
147 SyscallReturn closeFunc(SyscallDesc *desc, int num,
148 Process *p, ThreadContext *tc);
149
150 // Target read() handler.
151 SyscallReturn readFunc(SyscallDesc *desc, int num,
152 Process *p, ThreadContext *tc);
153
154 /// Target write() handler.
155 SyscallReturn writeFunc(SyscallDesc *desc, int num,
156 Process *p, ThreadContext *tc);
157
158 /// Target lseek() handler.
159 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
160 Process *p, ThreadContext *tc);
161
162 /// Target _llseek() handler.
163 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
164 Process *p, ThreadContext *tc);
165
166 /// Target munmap() handler.
167 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
168 Process *p, ThreadContext *tc);
169
170 /// Target gethostname() handler.
171 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
172 Process *p, ThreadContext *tc);
173
174 /// Target getcwd() handler.
175 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
176 Process *p, ThreadContext *tc);
177
178 /// Target readlink() handler.
179 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
180 Process *p, ThreadContext *tc,
181 int index = 0);
182 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
183 Process *p, ThreadContext *tc);
184
185 /// Target unlink() handler.
186 SyscallReturn unlinkHelper(SyscallDesc *desc, int num,
187 Process *p, ThreadContext *tc,
188 int index);
189 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
190 Process *p, ThreadContext *tc);
191
192 /// Target mkdir() handler.
193 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
194 Process *p, ThreadContext *tc);
195
196 /// Target rename() handler.
197 SyscallReturn renameFunc(SyscallDesc *desc, int num,
198 Process *p, ThreadContext *tc);
199
200
201 /// Target truncate() handler.
202 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
203 Process *p, ThreadContext *tc);
204
205
206 /// Target ftruncate() handler.
207 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
208 Process *p, ThreadContext *tc);
209
210
211 /// Target truncate64() handler.
212 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
213 Process *p, ThreadContext *tc);
214
215 /// Target ftruncate64() handler.
216 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
217 Process *p, ThreadContext *tc);
218
219
220 /// Target umask() handler.
221 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
222 Process *p, ThreadContext *tc);
223
224 /// Target gettid() handler.
225 SyscallReturn gettidFunc(SyscallDesc *desc, int num,
226 Process *p, ThreadContext *tc);
227
228 /// Target chown() handler.
229 SyscallReturn chownFunc(SyscallDesc *desc, int num,
230 Process *p, ThreadContext *tc);
231
232 /// Target setpgid() handler.
233 SyscallReturn setpgidFunc(SyscallDesc *desc, int num,
234 Process *p, ThreadContext *tc);
235
236 /// Target fchown() handler.
237 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
238 Process *p, ThreadContext *tc);
239
240 /// Target dup() handler.
241 SyscallReturn dupFunc(SyscallDesc *desc, int num,
242 Process *process, ThreadContext *tc);
243
244 /// Target fcntl() handler.
245 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
246 Process *process, ThreadContext *tc);
247
248 /// Target fcntl64() handler.
249 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
250 Process *process, ThreadContext *tc);
251
252 /// Target setuid() handler.
253 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
254 Process *p, ThreadContext *tc);
255
256 /// Target getpid() handler.
257 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
258 Process *p, ThreadContext *tc);
259
260 /// Target getuid() handler.
261 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
262 Process *p, ThreadContext *tc);
263
264 /// Target getgid() handler.
265 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
266 Process *p, ThreadContext *tc);
267
268 /// Target getppid() handler.
269 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
270 Process *p, ThreadContext *tc);
271
272 /// Target geteuid() handler.
273 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
274 Process *p, ThreadContext *tc);
275
276 /// Target getegid() handler.
277 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
278 Process *p, ThreadContext *tc);
279
280 /// Target access() handler
281 SyscallReturn accessFunc(SyscallDesc *desc, int num,
282 Process *p, ThreadContext *tc);
283 SyscallReturn 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
290 template <class OS>
291 SyscallReturn
292 futexFunc(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
373 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
374 Process *process, ThreadContext *tc);
375
376 /// Target getpidPseudo() handler.
377 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
378 Process *p, ThreadContext *tc);
379
380 /// Target getuidPseudo() handler.
381 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
382 Process *p, ThreadContext *tc);
383
384 /// Target getgidPseudo() handler.
385 SyscallReturn 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.
390 const int one_million = 1000000;
391 /// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
392 const 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.
397 const unsigned seconds_since_epoch = 1000000000;
398
399 /// Helper function to convert current elapsed time to seconds and
400 /// microseconds.
401 template <class T1, class T2>
402 void
403 getElapsedTimeMicro(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.
412 template <class T1, class T2>
413 void
414 getElapsedTimeNano(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
441 template <typename target_stat, typename host_stat>
442 static void
443 convertStatBuf(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 8k. 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
490 template <typename target_stat, typename host_stat64>
491 static void
492 convertStat64Buf(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 convenience functions
512 template<class OS>
513 static void
514 copyOutStatBuf(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
523 template<class OS>
524 static void
525 copyOutStat64Buf(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
534 template <class OS>
535 static void
536 copyOutStatfsBuf(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.
580 template <class OS>
581 SyscallReturn
582 ioctlFunc(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
615 template <class OS>
616 static SyscallReturn
617 openFunc(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.
693 template <class OS>
694 SyscallReturn
695 openFunc(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.
702 template <class OS>
703 SyscallReturn
704 openatFunc(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.
715 template <class OS>
716 SyscallReturn
717 unlinkatFunc(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
729 template <class OS>
730 SyscallReturn
731 faccessatFunc(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
742 template <class OS>
743 SyscallReturn
744 readlinkatFunc(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.
755 template <class OS>
756 SyscallReturn
757 renameatFunc(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.
791 template <class OS>
792 SyscallReturn
793 sysinfoFunc(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.
811 template <class OS>
812 SyscallReturn
813 chmodFunc(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.
843 template <class OS>
844 SyscallReturn
845 fchmodFunc(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.
864 template <class OS>
865 SyscallReturn
866 mremapFunc(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 if ((start + old_length) == process->memState->mmapEnd &&
889 (!use_provided_address || provided_address == start)) {
890 uint64_t diff = new_length - old_length;
891 process->allocateMem(process->memState->mmapEnd, diff);
892 process->memState->mmapEnd += diff;
893 return start;
894 } else {
895 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
896 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
897 return -ENOMEM;
898 } else {
899 uint64_t new_start = use_provided_address ?
900 provided_address : process->memState->mmapEnd;
901 process->pTable->remap(start, old_length, new_start);
902 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
903 new_start, new_start + new_length,
904 new_length - old_length);
905 // add on the remaining unallocated pages
906 process->allocateMem(new_start + old_length,
907 new_length - old_length,
908 use_provided_address /* clobber */);
909 if (!use_provided_address)
910 process->memState->mmapEnd += new_length;
911 if (use_provided_address &&
912 new_start + new_length > process->memState->mmapEnd) {
913 // something fishy going on here, at least notify the user
914 // @todo: increase mmap_end?
915 warn("mmap region limit exceeded with MREMAP_FIXED\n");
916 }
917 warn("returning %08p as start\n", new_start);
918 return new_start;
919 }
920 }
921 } else {
922 if (use_provided_address && provided_address != start)
923 process->pTable->remap(start, new_length, provided_address);
924 process->pTable->unmap(start + new_length, old_length - new_length);
925 return use_provided_address ? provided_address : start;
926 }
927 }
928
929 /// Target stat() handler.
930 template <class OS>
931 SyscallReturn
932 statFunc(SyscallDesc *desc, int callnum, Process *process,
933 ThreadContext *tc)
934 {
935 std::string path;
936
937 int index = 0;
938 if (!tc->getMemProxy().tryReadString(path,
939 process->getSyscallArg(tc, index))) {
940 return -EFAULT;
941 }
942 Addr bufPtr = process->getSyscallArg(tc, index);
943
944 // Adjust path for current working directory
945 path = process->fullPath(path);
946
947 struct stat hostBuf;
948 int result = stat(path.c_str(), &hostBuf);
949
950 if (result < 0)
951 return -errno;
952
953 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
954
955 return 0;
956 }
957
958
959 /// Target stat64() handler.
960 template <class OS>
961 SyscallReturn
962 stat64Func(SyscallDesc *desc, int callnum, Process *process,
963 ThreadContext *tc)
964 {
965 std::string path;
966
967 int index = 0;
968 if (!tc->getMemProxy().tryReadString(path,
969 process->getSyscallArg(tc, index)))
970 return -EFAULT;
971 Addr bufPtr = process->getSyscallArg(tc, index);
972
973 // Adjust path for current working directory
974 path = process->fullPath(path);
975
976 #if NO_STAT64
977 struct stat hostBuf;
978 int result = stat(path.c_str(), &hostBuf);
979 #else
980 struct stat64 hostBuf;
981 int result = stat64(path.c_str(), &hostBuf);
982 #endif
983
984 if (result < 0)
985 return -errno;
986
987 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
988
989 return 0;
990 }
991
992
993 /// Target fstatat64() handler.
994 template <class OS>
995 SyscallReturn
996 fstatat64Func(SyscallDesc *desc, int callnum, Process *process,
997 ThreadContext *tc)
998 {
999 int index = 0;
1000 int dirfd = process->getSyscallArg(tc, index);
1001 if (dirfd != OS::TGT_AT_FDCWD)
1002 warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
1003
1004 std::string path;
1005 if (!tc->getMemProxy().tryReadString(path,
1006 process->getSyscallArg(tc, index)))
1007 return -EFAULT;
1008 Addr bufPtr = process->getSyscallArg(tc, index);
1009
1010 // Adjust path for current working directory
1011 path = process->fullPath(path);
1012
1013 #if NO_STAT64
1014 struct stat hostBuf;
1015 int result = stat(path.c_str(), &hostBuf);
1016 #else
1017 struct stat64 hostBuf;
1018 int result = stat64(path.c_str(), &hostBuf);
1019 #endif
1020
1021 if (result < 0)
1022 return -errno;
1023
1024 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1025
1026 return 0;
1027 }
1028
1029
1030 /// Target fstat64() handler.
1031 template <class OS>
1032 SyscallReturn
1033 fstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1034 {
1035 int index = 0;
1036 int tgt_fd = p->getSyscallArg(tc, index);
1037 Addr bufPtr = p->getSyscallArg(tc, index);
1038
1039 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1040 if (!ffdp)
1041 return -EBADF;
1042 int sim_fd = ffdp->getSimFD();
1043
1044 #if NO_STAT64
1045 struct stat hostBuf;
1046 int result = fstat(sim_fd, &hostBuf);
1047 #else
1048 struct stat64 hostBuf;
1049 int result = fstat64(sim_fd, &hostBuf);
1050 #endif
1051
1052 if (result < 0)
1053 return -errno;
1054
1055 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1056
1057 return 0;
1058 }
1059
1060
1061 /// Target lstat() handler.
1062 template <class OS>
1063 SyscallReturn
1064 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
1065 ThreadContext *tc)
1066 {
1067 std::string path;
1068
1069 int index = 0;
1070 if (!tc->getMemProxy().tryReadString(path,
1071 process->getSyscallArg(tc, index))) {
1072 return -EFAULT;
1073 }
1074 Addr bufPtr = process->getSyscallArg(tc, index);
1075
1076 // Adjust path for current working directory
1077 path = process->fullPath(path);
1078
1079 struct stat hostBuf;
1080 int result = lstat(path.c_str(), &hostBuf);
1081
1082 if (result < 0)
1083 return -errno;
1084
1085 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1086
1087 return 0;
1088 }
1089
1090 /// Target lstat64() handler.
1091 template <class OS>
1092 SyscallReturn
1093 lstat64Func(SyscallDesc *desc, int callnum, Process *process,
1094 ThreadContext *tc)
1095 {
1096 std::string path;
1097
1098 int index = 0;
1099 if (!tc->getMemProxy().tryReadString(path,
1100 process->getSyscallArg(tc, index))) {
1101 return -EFAULT;
1102 }
1103 Addr bufPtr = process->getSyscallArg(tc, index);
1104
1105 // Adjust path for current working directory
1106 path = process->fullPath(path);
1107
1108 #if NO_STAT64
1109 struct stat hostBuf;
1110 int result = lstat(path.c_str(), &hostBuf);
1111 #else
1112 struct stat64 hostBuf;
1113 int result = lstat64(path.c_str(), &hostBuf);
1114 #endif
1115
1116 if (result < 0)
1117 return -errno;
1118
1119 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1120
1121 return 0;
1122 }
1123
1124 /// Target fstat() handler.
1125 template <class OS>
1126 SyscallReturn
1127 fstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1128 {
1129 int index = 0;
1130 int tgt_fd = p->getSyscallArg(tc, index);
1131 Addr bufPtr = p->getSyscallArg(tc, index);
1132
1133 DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1134
1135 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1136 if (!ffdp)
1137 return -EBADF;
1138 int sim_fd = ffdp->getSimFD();
1139
1140 struct stat hostBuf;
1141 int result = fstat(sim_fd, &hostBuf);
1142
1143 if (result < 0)
1144 return -errno;
1145
1146 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1147
1148 return 0;
1149 }
1150
1151
1152 /// Target statfs() handler.
1153 template <class OS>
1154 SyscallReturn
1155 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
1156 ThreadContext *tc)
1157 {
1158 #if NO_STATFS
1159 warn("Host OS cannot support calls to statfs. Ignoring syscall");
1160 #else
1161 std::string path;
1162
1163 int index = 0;
1164 if (!tc->getMemProxy().tryReadString(path,
1165 process->getSyscallArg(tc, index))) {
1166 return -EFAULT;
1167 }
1168 Addr bufPtr = process->getSyscallArg(tc, index);
1169
1170 // Adjust path for current working directory
1171 path = process->fullPath(path);
1172
1173 struct statfs hostBuf;
1174 int result = statfs(path.c_str(), &hostBuf);
1175
1176 if (result < 0)
1177 return -errno;
1178
1179 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1180 #endif
1181 return 0;
1182 }
1183
1184 template <class OS>
1185 SyscallReturn
1186 cloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1187 {
1188 int index = 0;
1189 TheISA::IntReg flags = p->getSyscallArg(tc, index);
1190 TheISA::IntReg newStack = p->getSyscallArg(tc, index);
1191 Addr ptidPtr = p->getSyscallArg(tc, index);
1192 Addr ctidPtr = p->getSyscallArg(tc, index);
1193 Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
1194
1195 if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
1196 ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
1197 ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) ||
1198 ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) ||
1199 ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) ||
1200 ((flags & OS::TGT_CLONE_VM) && !(newStack)))
1201 return -EINVAL;
1202
1203 ThreadContext *ctc;
1204 if (!(ctc = p->findFreeContext()))
1205 fatal("clone: no spare thread context in system");
1206
1207 /**
1208 * Note that ProcessParams is generated by swig and there are no other
1209 * examples of how to create anything but this default constructor. The
1210 * fields are manually initialized instead of passing parameters to the
1211 * constructor.
1212 */
1213 ProcessParams *pp = new ProcessParams();
1214 pp->executable.assign(*(new std::string(p->progName())));
1215 pp->cmd.push_back(*(new std::string(p->progName())));
1216 pp->system = p->system;
1217 pp->maxStackSize = p->maxStackSize;
1218 pp->cwd.assign(p->getcwd());
1219 pp->input.assign("stdin");
1220 pp->output.assign("stdout");
1221 pp->errout.assign("stderr");
1222 pp->uid = p->uid();
1223 pp->euid = p->euid();
1224 pp->gid = p->gid();
1225 pp->egid = p->egid();
1226
1227 /* Find the first free PID that's less than the maximum */
1228 std::set<int> const& pids = p->system->PIDs;
1229 int temp_pid = *pids.begin();
1230 do {
1231 temp_pid++;
1232 } while (pids.find(temp_pid) != pids.end());
1233 if (temp_pid >= System::maxPID)
1234 fatal("temp_pid is too large: %d", temp_pid);
1235
1236 pp->pid = temp_pid;
1237 pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
1238 Process *cp = pp->create();
1239 delete pp;
1240
1241 Process *owner = ctc->getProcessPtr();
1242 ctc->setProcessPtr(cp);
1243 cp->assignThreadContext(ctc->contextId());
1244 owner->revokeThreadContext(ctc->contextId());
1245
1246 if (flags & OS::TGT_CLONE_PARENT_SETTID) {
1247 BufferArg ptidBuf(ptidPtr, sizeof(long));
1248 long *ptid = (long *)ptidBuf.bufferPtr();
1249 *ptid = cp->pid();
1250 ptidBuf.copyOut(tc->getMemProxy());
1251 }
1252
1253 cp->initState();
1254 p->clone(tc, ctc, cp, flags);
1255
1256 if (flags & OS::TGT_CLONE_CHILD_SETTID) {
1257 BufferArg ctidBuf(ctidPtr, sizeof(long));
1258 long *ctid = (long *)ctidBuf.bufferPtr();
1259 *ctid = cp->pid();
1260 ctidBuf.copyOut(ctc->getMemProxy());
1261 }
1262
1263 if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
1264 cp->childClearTID = (uint64_t)ctidPtr;
1265
1266 ctc->clearArchRegs();
1267
1268 #if THE_ISA == ALPHA_ISA
1269 TheISA::copyMiscRegs(tc, ctc);
1270 #elif THE_ISA == SPARC_ISA
1271 TheISA::copyRegs(tc, ctc);
1272 ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0);
1273 ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0);
1274 ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2);
1275 ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows);
1276 ctc->setMiscReg(TheISA::MISCREG_CWP, 0);
1277 ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0);
1278 ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0);
1279 ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY);
1280 for (int y = 8; y < 32; y++)
1281 ctc->setIntReg(y, tc->readIntReg(y));
1282 #elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA
1283 TheISA::copyRegs(tc, ctc);
1284 #endif
1285
1286 #if THE_ISA == X86_ISA
1287 if (flags & OS::TGT_CLONE_SETTLS) {
1288 ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr);
1289 ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr);
1290 }
1291 #endif
1292
1293 if (newStack)
1294 ctc->setIntReg(TheISA::StackPointerReg, newStack);
1295
1296 cp->setSyscallReturn(ctc, 0);
1297
1298 #if THE_ISA == ALPHA_ISA
1299 ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
1300 #elif THE_ISA == SPARC_ISA
1301 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
1302 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
1303 #endif
1304
1305 ctc->pcState(tc->nextInstAddr());
1306 ctc->activate();
1307
1308 return cp->pid();
1309 }
1310
1311 /// Target fstatfs() handler.
1312 template <class OS>
1313 SyscallReturn
1314 fstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1315 {
1316 int index = 0;
1317 int tgt_fd = p->getSyscallArg(tc, index);
1318 Addr bufPtr = p->getSyscallArg(tc, index);
1319
1320 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1321 if (!ffdp)
1322 return -EBADF;
1323 int sim_fd = ffdp->getSimFD();
1324
1325 struct statfs hostBuf;
1326 int result = fstatfs(sim_fd, &hostBuf);
1327
1328 if (result < 0)
1329 return -errno;
1330
1331 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1332
1333 return 0;
1334 }
1335
1336
1337 /// Target writev() handler.
1338 template <class OS>
1339 SyscallReturn
1340 writevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1341 {
1342 int index = 0;
1343 int tgt_fd = p->getSyscallArg(tc, index);
1344
1345 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
1346 if (!hbfdp)
1347 return -EBADF;
1348 int sim_fd = hbfdp->getSimFD();
1349
1350 SETranslatingPortProxy &prox = tc->getMemProxy();
1351 uint64_t tiov_base = p->getSyscallArg(tc, index);
1352 size_t count = p->getSyscallArg(tc, index);
1353 struct iovec hiov[count];
1354 for (size_t i = 0; i < count; ++i) {
1355 typename OS::tgt_iovec tiov;
1356
1357 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1358 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1359 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1360 hiov[i].iov_base = new char [hiov[i].iov_len];
1361 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1362 hiov[i].iov_len);
1363 }
1364
1365 int result = writev(sim_fd, hiov, count);
1366
1367 for (size_t i = 0; i < count; ++i)
1368 delete [] (char *)hiov[i].iov_base;
1369
1370 if (result < 0)
1371 return -errno;
1372
1373 return result;
1374 }
1375
1376 /// Real mmap handler.
1377 template <class OS>
1378 SyscallReturn
1379 mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
1380 bool is_mmap2)
1381 {
1382 int index = 0;
1383 Addr start = p->getSyscallArg(tc, index);
1384 uint64_t length = p->getSyscallArg(tc, index);
1385 int prot = p->getSyscallArg(tc, index);
1386 int tgt_flags = p->getSyscallArg(tc, index);
1387 int tgt_fd = p->getSyscallArg(tc, index);
1388 int offset = p->getSyscallArg(tc, index);
1389
1390 if (is_mmap2)
1391 offset *= TheISA::PageBytes;
1392
1393 if (start & (TheISA::PageBytes - 1) ||
1394 offset & (TheISA::PageBytes - 1) ||
1395 (tgt_flags & OS::TGT_MAP_PRIVATE &&
1396 tgt_flags & OS::TGT_MAP_SHARED) ||
1397 (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
1398 !(tgt_flags & OS::TGT_MAP_SHARED)) ||
1399 !length) {
1400 return -EINVAL;
1401 }
1402
1403 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
1404 // With shared mmaps, there are two cases to consider:
1405 // 1) anonymous: writes should modify the mapping and this should be
1406 // visible to observers who share the mapping. Currently, it's
1407 // difficult to update the shared mapping because there's no
1408 // structure which maintains information about the which virtual
1409 // memory areas are shared. If that structure existed, it would be
1410 // possible to make the translations point to the same frames.
1411 // 2) file-backed: writes should modify the mapping and the file
1412 // which is backed by the mapping. The shared mapping problem is the
1413 // same as what was mentioned about the anonymous mappings. For
1414 // file-backed mappings, the writes to the file are difficult
1415 // because it requires syncing what the mapping holds with the file
1416 // that resides on the host system. So, any write on a real system
1417 // would cause the change to be propagated to the file mapping at
1418 // some point in the future (the inode is tracked along with the
1419 // mapping). This isn't guaranteed to always happen, but it usually
1420 // works well enough. The guarantee is provided by the msync system
1421 // call. We could force the change through with shared mappings with
1422 // a call to msync, but that again would require more information
1423 // than we currently maintain.
1424 warn("mmap: writing to shared mmap region is currently "
1425 "unsupported. The write succeeds on the target, but it "
1426 "will not be propagated to the host or shared mappings");
1427 }
1428
1429 length = roundUp(length, TheISA::PageBytes);
1430
1431 int sim_fd = -1;
1432 uint8_t *pmap = nullptr;
1433 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
1434 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1435
1436 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
1437 if (dfdp) {
1438 EmulatedDriver *emul_driver = dfdp->getDriver();
1439 return emul_driver->mmap(p, tc, start, length, prot,
1440 tgt_flags, tgt_fd, offset);
1441 }
1442
1443 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
1444 if (!ffdp)
1445 return -EBADF;
1446 sim_fd = ffdp->getSimFD();
1447
1448 pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1449 sim_fd, offset);
1450
1451 if (pmap == (decltype(pmap))-1) {
1452 warn("mmap: failed to map file into host address space");
1453 return -errno;
1454 }
1455 }
1456
1457 // Extend global mmap region if necessary. Note that we ignore the
1458 // start address unless MAP_FIXED is specified.
1459 if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
1460 Addr *end = &p->memState->mmapEnd;
1461 start = p->mmapGrowsDown() ? *end - length : *end;
1462 *end = p->mmapGrowsDown() ? start : *end + length;
1463 }
1464
1465 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
1466 start, start + length - 1);
1467
1468 // We only allow mappings to overwrite existing mappings if
1469 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
1470 // because we ignore the start hint if TGT_MAP_FIXED is not set.
1471 int clobber = tgt_flags & OS::TGT_MAP_FIXED;
1472 if (clobber) {
1473 for (auto tc : p->system->threadContexts) {
1474 // If we might be overwriting old mappings, we need to
1475 // invalidate potentially stale mappings out of the TLBs.
1476 tc->getDTBPtr()->flushAll();
1477 tc->getITBPtr()->flushAll();
1478 }
1479 }
1480
1481 // Allocate physical memory and map it in. If the page table is already
1482 // mapped and clobber is not set, the simulator will issue throw a
1483 // fatal and bail out of the simulation.
1484 p->allocateMem(start, length, clobber);
1485
1486 // Transfer content into target address space.
1487 SETranslatingPortProxy &tp = tc->getMemProxy();
1488 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
1489 // In general, we should zero the mapped area for anonymous mappings,
1490 // with something like:
1491 // tp.memsetBlob(start, 0, length);
1492 // However, given that we don't support sparse mappings, and
1493 // some applications can map a couple of gigabytes of space
1494 // (intending sparse usage), that can get painfully expensive.
1495 // Fortunately, since we don't properly implement munmap either,
1496 // there's no danger of remapping used memory, so for now all
1497 // newly mapped memory should already be zeroed so we can skip it.
1498 } else {
1499 // It is possible to mmap an area larger than a file, however
1500 // accessing unmapped portions the system triggers a "Bus error"
1501 // on the host. We must know when to stop copying the file from
1502 // the host into the target address space.
1503 struct stat file_stat;
1504 if (fstat(sim_fd, &file_stat) > 0)
1505 fatal("mmap: cannot stat file");
1506
1507 // Copy the portion of the file that is resident. This requires
1508 // checking both the mmap size and the filesize that we are
1509 // trying to mmap into this space; the mmap size also depends
1510 // on the specified offset into the file.
1511 uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
1512 length);
1513 tp.writeBlob(start, pmap, size);
1514
1515 // Cleanup the mmap region before exiting this function.
1516 munmap(pmap, length);
1517
1518 // Maintain the symbol table for dynamic executables.
1519 // The loader will call mmap to map the images into its address
1520 // space and we intercept that here. We can verify that we are
1521 // executing inside the loader by checking the program counter value.
1522 // XXX: with multiprogrammed workloads or multi-node configurations,
1523 // this will not work since there is a single global symbol table.
1524 ObjectFile *interpreter = p->getInterpreter();
1525 if (interpreter) {
1526 Addr text_start = interpreter->textBase();
1527 Addr text_end = text_start + interpreter->textSize();
1528
1529 Addr pc = tc->pcState().pc();
1530
1531 if (pc >= text_start && pc < text_end) {
1532 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1533 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
1534 ObjectFile *lib = createObjectFile(ffdp->getFileName());
1535
1536 if (lib) {
1537 lib->loadAllSymbols(debugSymbolTable,
1538 lib->textBase(), start);
1539 }
1540 }
1541 }
1542
1543 // Note that we do not zero out the remainder of the mapping. This
1544 // is done by a real system, but it probably will not affect
1545 // execution (hopefully).
1546 }
1547
1548 return start;
1549 }
1550
1551 template <class OS>
1552 SyscallReturn
1553 pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1554 {
1555 int index = 0;
1556 int tgt_fd = p->getSyscallArg(tc, index);
1557 Addr bufPtr = p->getSyscallArg(tc, index);
1558 int nbytes = p->getSyscallArg(tc, index);
1559 int offset = p->getSyscallArg(tc, index);
1560
1561 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
1562 if (!ffdp)
1563 return -EBADF;
1564 int sim_fd = ffdp->getSimFD();
1565
1566 BufferArg bufArg(bufPtr, nbytes);
1567 bufArg.copyIn(tc->getMemProxy());
1568
1569 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
1570
1571 return (bytes_written == -1) ? -errno : bytes_written;
1572 }
1573
1574 /// Target mmap() handler.
1575 template <class OS>
1576 SyscallReturn
1577 mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1578 {
1579 return mmapImpl<OS>(desc, num, p, tc, false);
1580 }
1581
1582 /// Target mmap2() handler.
1583 template <class OS>
1584 SyscallReturn
1585 mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1586 {
1587 return mmapImpl<OS>(desc, num, p, tc, true);
1588 }
1589
1590 /// Target getrlimit() handler.
1591 template <class OS>
1592 SyscallReturn
1593 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
1594 ThreadContext *tc)
1595 {
1596 int index = 0;
1597 unsigned resource = process->getSyscallArg(tc, index);
1598 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1599
1600 switch (resource) {
1601 case OS::TGT_RLIMIT_STACK:
1602 // max stack size in bytes: make up a number (8MB for now)
1603 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1604 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1605 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1606 break;
1607
1608 case OS::TGT_RLIMIT_DATA:
1609 // max data segment size in bytes: make up a number
1610 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1611 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1612 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1613 break;
1614
1615 default:
1616 warn("getrlimit: unimplemented resource %d", resource);
1617 return -EINVAL;
1618 break;
1619 }
1620
1621 rlp.copyOut(tc->getMemProxy());
1622 return 0;
1623 }
1624
1625 /// Target clock_gettime() function.
1626 template <class OS>
1627 SyscallReturn
1628 clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1629 {
1630 int index = 1;
1631 //int clk_id = p->getSyscallArg(tc, index);
1632 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1633
1634 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
1635 tp->tv_sec += seconds_since_epoch;
1636 tp->tv_sec = TheISA::htog(tp->tv_sec);
1637 tp->tv_nsec = TheISA::htog(tp->tv_nsec);
1638
1639 tp.copyOut(tc->getMemProxy());
1640
1641 return 0;
1642 }
1643
1644 /// Target clock_getres() function.
1645 template <class OS>
1646 SyscallReturn
1647 clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1648 {
1649 int index = 1;
1650 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1651
1652 // Set resolution at ns, which is what clock_gettime() returns
1653 tp->tv_sec = 0;
1654 tp->tv_nsec = 1;
1655
1656 tp.copyOut(tc->getMemProxy());
1657
1658 return 0;
1659 }
1660
1661 /// Target gettimeofday() handler.
1662 template <class OS>
1663 SyscallReturn
1664 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
1665 ThreadContext *tc)
1666 {
1667 int index = 0;
1668 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1669
1670 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1671 tp->tv_sec += seconds_since_epoch;
1672 tp->tv_sec = TheISA::htog(tp->tv_sec);
1673 tp->tv_usec = TheISA::htog(tp->tv_usec);
1674
1675 tp.copyOut(tc->getMemProxy());
1676
1677 return 0;
1678 }
1679
1680
1681 /// Target utimes() handler.
1682 template <class OS>
1683 SyscallReturn
1684 utimesFunc(SyscallDesc *desc, int callnum, Process *process,
1685 ThreadContext *tc)
1686 {
1687 std::string path;
1688
1689 int index = 0;
1690 if (!tc->getMemProxy().tryReadString(path,
1691 process->getSyscallArg(tc, index))) {
1692 return -EFAULT;
1693 }
1694
1695 TypedBufferArg<typename OS::timeval [2]>
1696 tp(process->getSyscallArg(tc, index));
1697 tp.copyIn(tc->getMemProxy());
1698
1699 struct timeval hostTimeval[2];
1700 for (int i = 0; i < 2; ++i)
1701 {
1702 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1703 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1704 }
1705
1706 // Adjust path for current working directory
1707 path = process->fullPath(path);
1708
1709 int result = utimes(path.c_str(), hostTimeval);
1710
1711 if (result < 0)
1712 return -errno;
1713
1714 return 0;
1715 }
1716
1717 template <class OS>
1718 SyscallReturn
1719 execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1720 {
1721 desc->setFlags(0);
1722
1723 int index = 0;
1724 std::string path;
1725 SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
1726 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
1727 return -EFAULT;
1728
1729 if (access(path.c_str(), F_OK) == -1)
1730 return -EACCES;
1731
1732 auto read_in = [](std::vector<std::string> & vect,
1733 SETranslatingPortProxy & mem_proxy,
1734 Addr mem_loc)
1735 {
1736 for (int inc = 0; ; inc++) {
1737 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
1738 b.copyIn(mem_proxy);
1739
1740 if (!*(Addr*)b.bufferPtr())
1741 break;
1742
1743 vect.push_back(std::string());
1744 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
1745 }
1746 };
1747
1748 /**
1749 * Note that ProcessParams is generated by swig and there are no other
1750 * examples of how to create anything but this default constructor. The
1751 * fields are manually initialized instead of passing parameters to the
1752 * constructor.
1753 */
1754 ProcessParams *pp = new ProcessParams();
1755 pp->executable = path;
1756 Addr argv_mem_loc = p->getSyscallArg(tc, index);
1757 read_in(pp->cmd, mem_proxy, argv_mem_loc);
1758 Addr envp_mem_loc = p->getSyscallArg(tc, index);
1759 read_in(pp->env, mem_proxy, envp_mem_loc);
1760 pp->uid = p->uid();
1761 pp->egid = p->egid();
1762 pp->euid = p->euid();
1763 pp->gid = p->gid();
1764 pp->ppid = p->ppid();
1765 pp->pid = p->pid();
1766 pp->input.assign("cin");
1767 pp->output.assign("cout");
1768 pp->errout.assign("cerr");
1769 pp->cwd.assign(p->getcwd());
1770 pp->system = p->system;
1771 pp->maxStackSize = p->maxStackSize;
1772 /**
1773 * Prevent process object creation with identical PIDs (which will trip
1774 * a fatal check in Process constructor). The execve call is supposed to
1775 * take over the currently executing process' identity but replace
1776 * whatever it is doing with a new process image. Instead of hijacking
1777 * the process object in the simulator, we create a new process object
1778 * and bind to the previous process' thread below (hijacking the thread).
1779 */
1780 p->system->PIDs.erase(p->pid());
1781 Process *new_p = pp->create();
1782 delete pp;
1783
1784 /**
1785 * Work through the file descriptor array and close any files marked
1786 * close-on-exec.
1787 */
1788 new_p->fds = p->fds;
1789 for (int i = 0; i < new_p->fds->getSize(); i++) {
1790 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
1791 if (fdep && fdep->getCOE())
1792 new_p->fds->closeFDEntry(i);
1793 }
1794
1795 *new_p->sigchld = true;
1796
1797 delete p;
1798 tc->clearArchRegs();
1799 tc->setProcessPtr(new_p);
1800 new_p->assignThreadContext(tc->contextId());
1801 new_p->initState();
1802 tc->activate();
1803 TheISA::PCState pcState = tc->pcState();
1804 tc->setNPC(pcState.instAddr());
1805
1806 desc->setFlags(SyscallDesc::SuppressReturnValue);
1807 return 0;
1808 }
1809
1810 /// Target getrusage() function.
1811 template <class OS>
1812 SyscallReturn
1813 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
1814 ThreadContext *tc)
1815 {
1816 int index = 0;
1817 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1818 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1819
1820 rup->ru_utime.tv_sec = 0;
1821 rup->ru_utime.tv_usec = 0;
1822 rup->ru_stime.tv_sec = 0;
1823 rup->ru_stime.tv_usec = 0;
1824 rup->ru_maxrss = 0;
1825 rup->ru_ixrss = 0;
1826 rup->ru_idrss = 0;
1827 rup->ru_isrss = 0;
1828 rup->ru_minflt = 0;
1829 rup->ru_majflt = 0;
1830 rup->ru_nswap = 0;
1831 rup->ru_inblock = 0;
1832 rup->ru_oublock = 0;
1833 rup->ru_msgsnd = 0;
1834 rup->ru_msgrcv = 0;
1835 rup->ru_nsignals = 0;
1836 rup->ru_nvcsw = 0;
1837 rup->ru_nivcsw = 0;
1838
1839 switch (who) {
1840 case OS::TGT_RUSAGE_SELF:
1841 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1842 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1843 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1844 break;
1845
1846 case OS::TGT_RUSAGE_CHILDREN:
1847 // do nothing. We have no child processes, so they take no time.
1848 break;
1849
1850 default:
1851 // don't really handle THREAD or CHILDREN, but just warn and
1852 // plow ahead
1853 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1854 who);
1855 }
1856
1857 rup.copyOut(tc->getMemProxy());
1858
1859 return 0;
1860 }
1861
1862 /// Target times() function.
1863 template <class OS>
1864 SyscallReturn
1865 timesFunc(SyscallDesc *desc, int callnum, Process *process,
1866 ThreadContext *tc)
1867 {
1868 int index = 0;
1869 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1870
1871 // Fill in the time structure (in clocks)
1872 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1873 bufp->tms_utime = clocks;
1874 bufp->tms_stime = 0;
1875 bufp->tms_cutime = 0;
1876 bufp->tms_cstime = 0;
1877
1878 // Convert to host endianness
1879 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1880
1881 // Write back
1882 bufp.copyOut(tc->getMemProxy());
1883
1884 // Return clock ticks since system boot
1885 return clocks;
1886 }
1887
1888 /// Target time() function.
1889 template <class OS>
1890 SyscallReturn
1891 timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
1892 {
1893 typename OS::time_t sec, usec;
1894 getElapsedTimeMicro(sec, usec);
1895 sec += seconds_since_epoch;
1896
1897 int index = 0;
1898 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1899 if (taddr != 0) {
1900 typename OS::time_t t = sec;
1901 t = TheISA::htog(t);
1902 SETranslatingPortProxy &p = tc->getMemProxy();
1903 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1904 }
1905 return sec;
1906 }
1907
1908
1909 #endif // __SIM_SYSCALL_EMUL_HH__