c00edfdc618b48bea8f7a9d58aae1f1224bf244b
[gem5.git] / src / sim / syscall_emul.hh
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Steve Reinhardt
29 * Kevin Lim
30 */
31
32 #ifndef __SIM_SYSCALL_EMUL_HH__
33 #define __SIM_SYSCALL_EMUL_HH__
34
35 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
36 defined(__FreeBSD__) || defined(__CYGWIN__))
37
38 ///
39 /// @file syscall_emul.hh
40 ///
41 /// This file defines objects used to emulate syscalls from the target
42 /// application on the host machine.
43
44 #include <errno.h>
45 #include <string>
46 #ifdef __CYGWIN32__
47 #include <sys/fcntl.h> // for O_BINARY
48 #endif
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include <sys/uio.h>
52
53 #include "base/chunk_generator.hh"
54 #include "base/intmath.hh" // for RoundUp
55 #include "base/misc.hh"
56 #include "base/trace.hh"
57 #include "base/types.hh"
58 #include "cpu/base.hh"
59 #include "cpu/thread_context.hh"
60 #include "mem/translating_port.hh"
61 #include "mem/page_table.hh"
62 #include "sim/system.hh"
63 #include "sim/process.hh"
64
65 ///
66 /// System call descriptor.
67 ///
68 class SyscallDesc {
69
70 public:
71
72 /// Typedef for target syscall handler functions.
73 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
74 LiveProcess *, ThreadContext *);
75
76 const char *name; //!< Syscall name (e.g., "open").
77 FuncPtr funcPtr; //!< Pointer to emulation function.
78 int flags; //!< Flags (see Flags enum).
79
80 /// Flag values for controlling syscall behavior.
81 enum Flags {
82 /// Don't set return regs according to funcPtr return value.
83 /// Used for syscalls with non-standard return conventions
84 /// that explicitly set the ThreadContext regs (e.g.,
85 /// sigreturn).
86 SuppressReturnValue = 1
87 };
88
89 /// Constructor.
90 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
91 : name(_name), funcPtr(_funcPtr), flags(_flags)
92 {
93 }
94
95 /// Emulate the syscall. Public interface for calling through funcPtr.
96 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
97 };
98
99
100 class BaseBufferArg {
101
102 public:
103
104 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
105 {
106 bufPtr = new uint8_t[size];
107 // clear out buffer: in case we only partially populate this,
108 // and then do a copyOut(), we want to make sure we don't
109 // introduce any random junk into the simulated address space
110 memset(bufPtr, 0, size);
111 }
112
113 virtual ~BaseBufferArg() { delete [] bufPtr; }
114
115 //
116 // copy data into simulator space (read from target memory)
117 //
118 virtual bool copyIn(TranslatingPort *memport)
119 {
120 memport->readBlob(addr, bufPtr, size);
121 return true; // no EFAULT detection for now
122 }
123
124 //
125 // copy data out of simulator space (write to target memory)
126 //
127 virtual bool copyOut(TranslatingPort *memport)
128 {
129 memport->writeBlob(addr, bufPtr, size);
130 return true; // no EFAULT detection for now
131 }
132
133 protected:
134 Addr addr;
135 int size;
136 uint8_t *bufPtr;
137 };
138
139
140 class BufferArg : public BaseBufferArg
141 {
142 public:
143 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
144 void *bufferPtr() { return bufPtr; }
145 };
146
147 template <class T>
148 class TypedBufferArg : public BaseBufferArg
149 {
150 public:
151 // user can optionally specify a specific number of bytes to
152 // allocate to deal with those structs that have variable-size
153 // arrays at the end
154 TypedBufferArg(Addr _addr, int _size = sizeof(T))
155 : BaseBufferArg(_addr, _size)
156 { }
157
158 // type case
159 operator T*() { return (T *)bufPtr; }
160
161 // dereference operators
162 T &operator*() { return *((T *)bufPtr); }
163 T* operator->() { return (T *)bufPtr; }
164 T &operator[](int i) { return ((T *)bufPtr)[i]; }
165 };
166
167 //////////////////////////////////////////////////////////////////////
168 //
169 // The following emulation functions are generic enough that they
170 // don't need to be recompiled for different emulated OS's. They are
171 // defined in sim/syscall_emul.cc.
172 //
173 //////////////////////////////////////////////////////////////////////
174
175
176 /// Handler for unimplemented syscalls that we haven't thought about.
177 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
178 LiveProcess *p, ThreadContext *tc);
179
180 /// Handler for unimplemented syscalls that we never intend to
181 /// implement (signal handling, etc.) and should not affect the correct
182 /// behavior of the program. Print a warning only if the appropriate
183 /// trace flag is enabled. Return success to the target program.
184 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
185 LiveProcess *p, ThreadContext *tc);
186
187 /// Target exit() handler: terminate current context.
188 SyscallReturn exitFunc(SyscallDesc *desc, int num,
189 LiveProcess *p, ThreadContext *tc);
190
191 /// Target exit_group() handler: terminate simulation. (exit all threads)
192 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
193 LiveProcess *p, ThreadContext *tc);
194
195 /// Target getpagesize() handler.
196 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
197 LiveProcess *p, ThreadContext *tc);
198
199 /// Target brk() handler: set brk address.
200 SyscallReturn brkFunc(SyscallDesc *desc, int num,
201 LiveProcess *p, ThreadContext *tc);
202
203 /// Target close() handler.
204 SyscallReturn closeFunc(SyscallDesc *desc, int num,
205 LiveProcess *p, ThreadContext *tc);
206
207 /// Target read() handler.
208 SyscallReturn readFunc(SyscallDesc *desc, int num,
209 LiveProcess *p, ThreadContext *tc);
210
211 /// Target write() handler.
212 SyscallReturn writeFunc(SyscallDesc *desc, int num,
213 LiveProcess *p, ThreadContext *tc);
214
215 /// Target lseek() handler.
216 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
217 LiveProcess *p, ThreadContext *tc);
218
219 /// Target _llseek() handler.
220 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
221 LiveProcess *p, ThreadContext *tc);
222
223 /// Target munmap() handler.
224 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
225 LiveProcess *p, ThreadContext *tc);
226
227 /// Target gethostname() handler.
228 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
229 LiveProcess *p, ThreadContext *tc);
230
231 /// Target getcwd() handler.
232 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
233 LiveProcess *p, ThreadContext *tc);
234
235 /// Target unlink() handler.
236 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
237 LiveProcess *p, ThreadContext *tc);
238
239 /// Target unlink() handler.
240 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
241 LiveProcess *p, ThreadContext *tc);
242
243 /// Target mkdir() handler.
244 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
245 LiveProcess *p, ThreadContext *tc);
246
247 /// Target rename() handler.
248 SyscallReturn renameFunc(SyscallDesc *desc, int num,
249 LiveProcess *p, ThreadContext *tc);
250
251
252 /// Target truncate() handler.
253 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
254 LiveProcess *p, ThreadContext *tc);
255
256
257 /// Target ftruncate() handler.
258 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
259 LiveProcess *p, ThreadContext *tc);
260
261
262 /// Target umask() handler.
263 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
264 LiveProcess *p, ThreadContext *tc);
265
266
267 /// Target chown() handler.
268 SyscallReturn chownFunc(SyscallDesc *desc, int num,
269 LiveProcess *p, ThreadContext *tc);
270
271
272 /// Target fchown() handler.
273 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
274 LiveProcess *p, ThreadContext *tc);
275
276 /// Target dup() handler.
277 SyscallReturn dupFunc(SyscallDesc *desc, int num,
278 LiveProcess *process, ThreadContext *tc);
279
280 /// Target fnctl() handler.
281 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
282 LiveProcess *process, ThreadContext *tc);
283
284 /// Target fcntl64() handler.
285 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
286 LiveProcess *process, ThreadContext *tc);
287
288 /// Target setuid() handler.
289 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
290 LiveProcess *p, ThreadContext *tc);
291
292 /// Target getpid() handler.
293 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
294 LiveProcess *p, ThreadContext *tc);
295
296 /// Target getuid() handler.
297 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
298 LiveProcess *p, ThreadContext *tc);
299
300 /// Target getgid() handler.
301 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
302 LiveProcess *p, ThreadContext *tc);
303
304 /// Target getppid() handler.
305 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
306 LiveProcess *p, ThreadContext *tc);
307
308 /// Target geteuid() handler.
309 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
310 LiveProcess *p, ThreadContext *tc);
311
312 /// Target getegid() handler.
313 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
314 LiveProcess *p, ThreadContext *tc);
315
316 /// Target clone() handler.
317 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
318 LiveProcess *p, ThreadContext *tc);
319
320
321 /// Pseudo Funcs - These functions use a different return convension,
322 /// returning a second value in a register other than the normal return register
323 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
324 LiveProcess *process, ThreadContext *tc);
325
326 /// Target getpidPseudo() handler.
327 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
328 LiveProcess *p, ThreadContext *tc);
329
330 /// Target getuidPseudo() handler.
331 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
332 LiveProcess *p, ThreadContext *tc);
333
334 /// Target getgidPseudo() handler.
335 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
336 LiveProcess *p, ThreadContext *tc);
337
338
339 /// A readable name for 1,000,000, for converting microseconds to seconds.
340 const int one_million = 1000000;
341
342 /// Approximate seconds since the epoch (1/1/1970). About a billion,
343 /// by my reckoning. We want to keep this a constant (not use the
344 /// real-world time) to keep simulations repeatable.
345 const unsigned seconds_since_epoch = 1000000000;
346
347 /// Helper function to convert current elapsed time to seconds and
348 /// microseconds.
349 template <class T1, class T2>
350 void
351 getElapsedTime(T1 &sec, T2 &usec)
352 {
353 int elapsed_usecs = curTick / Clock::Int::us;
354 sec = elapsed_usecs / one_million;
355 usec = elapsed_usecs % one_million;
356 }
357
358 //////////////////////////////////////////////////////////////////////
359 //
360 // The following emulation functions are generic, but need to be
361 // templated to account for differences in types, constants, etc.
362 //
363 //////////////////////////////////////////////////////////////////////
364
365 #if NO_STAT64
366 typedef struct stat hst_stat;
367 typedef struct stat hst_stat64;
368 #else
369 typedef struct stat hst_stat;
370 typedef struct stat64 hst_stat64;
371 #endif
372
373 //// Helper function to convert a host stat buffer to a target stat
374 //// buffer. Also copies the target buffer out to the simulated
375 //// memory space. Used by stat(), fstat(), and lstat().
376
377 template <typename target_stat, typename host_stat>
378 static void
379 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
380 {
381 using namespace TheISA;
382
383 if (fakeTTY)
384 tgt->st_dev = 0xA;
385 else
386 tgt->st_dev = host->st_dev;
387 tgt->st_dev = htog(tgt->st_dev);
388 tgt->st_ino = host->st_ino;
389 tgt->st_ino = htog(tgt->st_ino);
390 tgt->st_mode = host->st_mode;
391 if (fakeTTY) {
392 // Claim to be a character device
393 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
394 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
395 }
396 tgt->st_mode = htog(tgt->st_mode);
397 tgt->st_nlink = host->st_nlink;
398 tgt->st_nlink = htog(tgt->st_nlink);
399 tgt->st_uid = host->st_uid;
400 tgt->st_uid = htog(tgt->st_uid);
401 tgt->st_gid = host->st_gid;
402 tgt->st_gid = htog(tgt->st_gid);
403 if (fakeTTY)
404 tgt->st_rdev = 0x880d;
405 else
406 tgt->st_rdev = host->st_rdev;
407 tgt->st_rdev = htog(tgt->st_rdev);
408 tgt->st_size = host->st_size;
409 tgt->st_size = htog(tgt->st_size);
410 tgt->st_atimeX = host->st_atime;
411 tgt->st_atimeX = htog(tgt->st_atimeX);
412 tgt->st_mtimeX = host->st_mtime;
413 tgt->st_mtimeX = htog(tgt->st_mtimeX);
414 tgt->st_ctimeX = host->st_ctime;
415 tgt->st_ctimeX = htog(tgt->st_ctimeX);
416 // Force the block size to be 8k. This helps to ensure buffered io works
417 // consistently across different hosts.
418 tgt->st_blksize = 0x2000;
419 tgt->st_blksize = htog(tgt->st_blksize);
420 tgt->st_blocks = host->st_blocks;
421 tgt->st_blocks = htog(tgt->st_blocks);
422 }
423
424 // Same for stat64
425
426 template <typename target_stat, typename host_stat64>
427 static void
428 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
429 {
430 using namespace TheISA;
431
432 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
433 #if defined(STAT_HAVE_NSEC)
434 tgt->st_atime_nsec = host->st_atime_nsec;
435 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
436 tgt->st_mtime_nsec = host->st_mtime_nsec;
437 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
438 tgt->st_ctime_nsec = host->st_ctime_nsec;
439 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
440 #else
441 tgt->st_atime_nsec = 0;
442 tgt->st_mtime_nsec = 0;
443 tgt->st_ctime_nsec = 0;
444 #endif
445 }
446
447 //Here are a couple convenience functions
448 template<class OS>
449 static void
450 copyOutStatBuf(TranslatingPort * mem, Addr addr,
451 hst_stat *host, bool fakeTTY = false)
452 {
453 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
454 tgt_stat_buf tgt(addr);
455 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
456 tgt.copyOut(mem);
457 }
458
459 template<class OS>
460 static void
461 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
462 hst_stat64 *host, bool fakeTTY = false)
463 {
464 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
465 tgt_stat_buf tgt(addr);
466 convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
467 tgt.copyOut(mem);
468 }
469
470 /// Target ioctl() handler. For the most part, programs call ioctl()
471 /// only to find out if their stdout is a tty, to determine whether to
472 /// do line or block buffering.
473 template <class OS>
474 SyscallReturn
475 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
476 ThreadContext *tc)
477 {
478 int fd = process->getSyscallArg(tc, 0);
479 unsigned req = process->getSyscallArg(tc, 1);
480
481 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
482
483 if (fd < 0 || process->sim_fd(fd) < 0) {
484 // doesn't map to any simulator fd: not a valid target fd
485 return -EBADF;
486 }
487
488 switch (req) {
489 case OS::TIOCISATTY_:
490 case OS::TIOCGETP_:
491 case OS::TIOCSETP_:
492 case OS::TIOCSETN_:
493 case OS::TIOCSETC_:
494 case OS::TIOCGETC_:
495 case OS::TIOCGETS_:
496 case OS::TIOCGETA_:
497 return -ENOTTY;
498
499 default:
500 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
501 fd, req, tc->readPC());
502 }
503 }
504
505 /// Target open() handler.
506 template <class OS>
507 SyscallReturn
508 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
509 ThreadContext *tc)
510 {
511 std::string path;
512
513 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
514 return -EFAULT;
515
516 if (path == "/dev/sysdev0") {
517 // This is a memory-mapped high-resolution timer device on Alpha.
518 // We don't support it, so just punt.
519 warn("Ignoring open(%s, ...)\n", path);
520 return -ENOENT;
521 }
522
523 int tgtFlags = process->getSyscallArg(tc, 1);
524 int mode = process->getSyscallArg(tc, 2);
525 int hostFlags = 0;
526
527 // translate open flags
528 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
529 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
530 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
531 hostFlags |= OS::openFlagTable[i].hostFlag;
532 }
533 }
534
535 // any target flags left?
536 if (tgtFlags != 0)
537 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
538
539 #ifdef __CYGWIN32__
540 hostFlags |= O_BINARY;
541 #endif
542
543 // Adjust path for current working directory
544 path = process->fullPath(path);
545
546 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
547
548 int fd;
549 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
550 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
551 // It's a proc/sys entery and requires special handling
552 fd = OS::openSpecialFile(path, process, tc);
553 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
554 } else {
555 // open the file
556 fd = open(path.c_str(), hostFlags, mode);
557 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
558 }
559
560 }
561
562 /// Target sysinfo() handler.
563 template <class OS>
564 SyscallReturn
565 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
566 ThreadContext *tc)
567 {
568
569 TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0));
570
571 sysinfo->uptime=seconds_since_epoch;
572 sysinfo->totalram=process->system->memSize();
573
574 sysinfo.copyOut(tc->getMemPort());
575
576 return 0;
577 }
578
579 /// Target chmod() handler.
580 template <class OS>
581 SyscallReturn
582 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
583 ThreadContext *tc)
584 {
585 std::string path;
586
587 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
588 return -EFAULT;
589
590 uint32_t mode = process->getSyscallArg(tc, 1);
591 mode_t hostMode = 0;
592
593 // XXX translate mode flags via OS::something???
594 hostMode = mode;
595
596 // Adjust path for current working directory
597 path = process->fullPath(path);
598
599 // do the chmod
600 int result = chmod(path.c_str(), hostMode);
601 if (result < 0)
602 return -errno;
603
604 return 0;
605 }
606
607
608 /// Target fchmod() handler.
609 template <class OS>
610 SyscallReturn
611 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
612 ThreadContext *tc)
613 {
614 int fd = process->getSyscallArg(tc, 0);
615 if (fd < 0 || process->sim_fd(fd) < 0) {
616 // doesn't map to any simulator fd: not a valid target fd
617 return -EBADF;
618 }
619
620 uint32_t mode = process->getSyscallArg(tc, 1);
621 mode_t hostMode = 0;
622
623 // XXX translate mode flags via OS::someting???
624 hostMode = mode;
625
626 // do the fchmod
627 int result = fchmod(process->sim_fd(fd), hostMode);
628 if (result < 0)
629 return -errno;
630
631 return 0;
632 }
633
634 /// Target mremap() handler.
635 template <class OS>
636 SyscallReturn
637 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
638 {
639 Addr start = process->getSyscallArg(tc, 0);
640 uint64_t old_length = process->getSyscallArg(tc, 1);
641 uint64_t new_length = process->getSyscallArg(tc, 2);
642 uint64_t flags = process->getSyscallArg(tc, 3);
643
644 if ((start % TheISA::VMPageSize != 0) ||
645 (new_length % TheISA::VMPageSize != 0)) {
646 warn("mremap failing: arguments not page aligned");
647 return -EINVAL;
648 }
649
650 if (new_length > old_length) {
651 if ((start + old_length) == process->mmap_end) {
652 uint64_t diff = new_length - old_length;
653 process->pTable->allocate(process->mmap_end, diff);
654 process->mmap_end += diff;
655 return start;
656 } else {
657 // sys/mman.h defined MREMAP_MAYMOVE
658 if (!(flags & 1)) {
659 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
660 return -ENOMEM;
661 } else {
662 process->pTable->remap(start, old_length, process->mmap_end);
663 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
664 process->mmap_end, process->mmap_end + new_length, new_length);
665 start = process->mmap_end;
666 // add on the remaining unallocated pages
667 process->pTable->allocate(start + old_length, new_length - old_length);
668 process->mmap_end += new_length;
669 warn("returning %08p as start\n", start);
670 return start;
671 }
672 }
673 } else {
674 process->pTable->deallocate(start + new_length, old_length -
675 new_length);
676 return start;
677 }
678 }
679
680 /// Target stat() handler.
681 template <class OS>
682 SyscallReturn
683 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
684 ThreadContext *tc)
685 {
686 std::string path;
687
688 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
689 return -EFAULT;
690
691 // Adjust path for current working directory
692 path = process->fullPath(path);
693
694 struct stat hostBuf;
695 int result = stat(path.c_str(), &hostBuf);
696
697 if (result < 0)
698 return -errno;
699
700 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
701 &hostBuf);
702
703 return 0;
704 }
705
706
707 /// Target stat64() handler.
708 template <class OS>
709 SyscallReturn
710 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
711 ThreadContext *tc)
712 {
713 std::string path;
714
715 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
716 return -EFAULT;
717
718 // Adjust path for current working directory
719 path = process->fullPath(path);
720
721 #if NO_STAT64
722 struct stat hostBuf;
723 int result = stat(path.c_str(), &hostBuf);
724 #else
725 struct stat64 hostBuf;
726 int result = stat64(path.c_str(), &hostBuf);
727 #endif
728
729 if (result < 0)
730 return -errno;
731
732 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
733 &hostBuf);
734
735 return 0;
736 }
737
738
739 /// Target fstat64() handler.
740 template <class OS>
741 SyscallReturn
742 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
743 ThreadContext *tc)
744 {
745 int fd = process->getSyscallArg(tc, 0);
746 if (fd < 0 || process->sim_fd(fd) < 0) {
747 // doesn't map to any simulator fd: not a valid target fd
748 return -EBADF;
749 }
750
751 #if NO_STAT64
752 struct stat hostBuf;
753 int result = fstat(process->sim_fd(fd), &hostBuf);
754 #else
755 struct stat64 hostBuf;
756 int result = fstat64(process->sim_fd(fd), &hostBuf);
757 #endif
758
759 if (result < 0)
760 return -errno;
761
762 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
763 &hostBuf, (fd == 1));
764
765 return 0;
766 }
767
768
769 /// Target lstat() handler.
770 template <class OS>
771 SyscallReturn
772 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
773 ThreadContext *tc)
774 {
775 std::string path;
776
777 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
778 return -EFAULT;
779
780 // Adjust path for current working directory
781 path = process->fullPath(path);
782
783 struct stat hostBuf;
784 int result = lstat(path.c_str(), &hostBuf);
785
786 if (result < 0)
787 return -errno;
788
789 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
790 &hostBuf);
791
792 return 0;
793 }
794
795 /// Target lstat64() handler.
796 template <class OS>
797 SyscallReturn
798 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
799 ThreadContext *tc)
800 {
801 std::string path;
802
803 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
804 return -EFAULT;
805
806 // Adjust path for current working directory
807 path = process->fullPath(path);
808
809 #if NO_STAT64
810 struct stat hostBuf;
811 int result = lstat(path.c_str(), &hostBuf);
812 #else
813 struct stat64 hostBuf;
814 int result = lstat64(path.c_str(), &hostBuf);
815 #endif
816
817 if (result < 0)
818 return -errno;
819
820 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
821 &hostBuf);
822
823 return 0;
824 }
825
826 /// Target fstat() handler.
827 template <class OS>
828 SyscallReturn
829 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
830 ThreadContext *tc)
831 {
832 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
833
834 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
835
836 if (fd < 0)
837 return -EBADF;
838
839 struct stat hostBuf;
840 int result = fstat(fd, &hostBuf);
841
842 if (result < 0)
843 return -errno;
844
845 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
846 &hostBuf, (fd == 1));
847
848 return 0;
849 }
850
851
852 /// Target statfs() handler.
853 template <class OS>
854 SyscallReturn
855 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
856 ThreadContext *tc)
857 {
858 std::string path;
859
860 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
861 return -EFAULT;
862
863 // Adjust path for current working directory
864 path = process->fullPath(path);
865
866 struct statfs hostBuf;
867 int result = statfs(path.c_str(), &hostBuf);
868
869 if (result < 0)
870 return -errno;
871
872 OS::copyOutStatfsBuf(tc->getMemPort(),
873 (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
874
875 return 0;
876 }
877
878
879 /// Target fstatfs() handler.
880 template <class OS>
881 SyscallReturn
882 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
883 ThreadContext *tc)
884 {
885 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
886
887 if (fd < 0)
888 return -EBADF;
889
890 struct statfs hostBuf;
891 int result = fstatfs(fd, &hostBuf);
892
893 if (result < 0)
894 return -errno;
895
896 OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
897 &hostBuf);
898
899 return 0;
900 }
901
902
903 /// Target writev() handler.
904 template <class OS>
905 SyscallReturn
906 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
907 ThreadContext *tc)
908 {
909 int fd = process->getSyscallArg(tc, 0);
910 if (fd < 0 || process->sim_fd(fd) < 0) {
911 // doesn't map to any simulator fd: not a valid target fd
912 return -EBADF;
913 }
914
915 TranslatingPort *p = tc->getMemPort();
916 uint64_t tiov_base = process->getSyscallArg(tc, 1);
917 size_t count = process->getSyscallArg(tc, 2);
918 struct iovec hiov[count];
919 for (size_t i = 0; i < count; ++i) {
920 typename OS::tgt_iovec tiov;
921
922 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
923 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
924 hiov[i].iov_len = gtoh(tiov.iov_len);
925 hiov[i].iov_base = new char [hiov[i].iov_len];
926 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
927 hiov[i].iov_len);
928 }
929
930 int result = writev(process->sim_fd(fd), hiov, count);
931
932 for (size_t i = 0; i < count; ++i)
933 delete [] (char *)hiov[i].iov_base;
934
935 if (result < 0)
936 return -errno;
937
938 return 0;
939 }
940
941
942 /// Target mmap() handler.
943 ///
944 /// We don't really handle mmap(). If the target is mmaping an
945 /// anonymous region or /dev/zero, we can get away with doing basically
946 /// nothing (since memory is initialized to zero and the simulator
947 /// doesn't really check addresses anyway). Always print a warning,
948 /// since this could be seriously broken if we're not mapping
949 /// /dev/zero.
950 //
951 /// Someday we should explicitly check for /dev/zero in open, flag the
952 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
953 /// anything else.
954 template <class OS>
955 SyscallReturn
956 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
957 {
958 Addr start = p->getSyscallArg(tc, 0);
959 uint64_t length = p->getSyscallArg(tc, 1);
960 // int prot = p->getSyscallArg(tc, 2);
961 int flags = p->getSyscallArg(tc, 3);
962 // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
963 // int offset = p->getSyscallArg(tc, 5);
964
965
966 if ((start % TheISA::VMPageSize) != 0 ||
967 (length % TheISA::VMPageSize) != 0) {
968 warn("mmap failing: arguments not page-aligned: "
969 "start 0x%x length 0x%x",
970 start, length);
971 return -EINVAL;
972 }
973
974 if (start != 0) {
975 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
976 start, p->mmap_end);
977 }
978
979 // pick next address from our "mmap region"
980 start = p->mmap_end;
981 p->pTable->allocate(start, length);
982 p->mmap_end += length;
983
984 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
985 warn("allowing mmap of file @ fd %d. "
986 "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
987 }
988
989 return start;
990 }
991
992 /// Target getrlimit() handler.
993 template <class OS>
994 SyscallReturn
995 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
996 ThreadContext *tc)
997 {
998 unsigned resource = process->getSyscallArg(tc, 0);
999 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
1000
1001 switch (resource) {
1002 case OS::TGT_RLIMIT_STACK:
1003 // max stack size in bytes: make up a number (8MB for now)
1004 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1005 rlp->rlim_cur = htog(rlp->rlim_cur);
1006 rlp->rlim_max = htog(rlp->rlim_max);
1007 break;
1008
1009 case OS::TGT_RLIMIT_DATA:
1010 // max data segment size in bytes: make up a number
1011 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1012 rlp->rlim_cur = htog(rlp->rlim_cur);
1013 rlp->rlim_max = htog(rlp->rlim_max);
1014 break;
1015
1016 default:
1017 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1018 << std::endl;
1019 abort();
1020 break;
1021 }
1022
1023 rlp.copyOut(tc->getMemPort());
1024 return 0;
1025 }
1026
1027 /// Target gettimeofday() handler.
1028 template <class OS>
1029 SyscallReturn
1030 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1031 ThreadContext *tc)
1032 {
1033 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1034
1035 getElapsedTime(tp->tv_sec, tp->tv_usec);
1036 tp->tv_sec += seconds_since_epoch;
1037 tp->tv_sec = TheISA::htog(tp->tv_sec);
1038 tp->tv_usec = TheISA::htog(tp->tv_usec);
1039
1040 tp.copyOut(tc->getMemPort());
1041
1042 return 0;
1043 }
1044
1045
1046 /// Target utimes() handler.
1047 template <class OS>
1048 SyscallReturn
1049 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1050 ThreadContext *tc)
1051 {
1052 std::string path;
1053
1054 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
1055 return -EFAULT;
1056
1057 TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
1058 tp.copyIn(tc->getMemPort());
1059
1060 struct timeval hostTimeval[2];
1061 for (int i = 0; i < 2; ++i)
1062 {
1063 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1064 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1065 }
1066
1067 // Adjust path for current working directory
1068 path = process->fullPath(path);
1069
1070 int result = utimes(path.c_str(), hostTimeval);
1071
1072 if (result < 0)
1073 return -errno;
1074
1075 return 0;
1076 }
1077 /// Target getrusage() function.
1078 template <class OS>
1079 SyscallReturn
1080 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1081 ThreadContext *tc)
1082 {
1083 int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN
1084 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1085
1086 rup->ru_utime.tv_sec = 0;
1087 rup->ru_utime.tv_usec = 0;
1088 rup->ru_stime.tv_sec = 0;
1089 rup->ru_stime.tv_usec = 0;
1090 rup->ru_maxrss = 0;
1091 rup->ru_ixrss = 0;
1092 rup->ru_idrss = 0;
1093 rup->ru_isrss = 0;
1094 rup->ru_minflt = 0;
1095 rup->ru_majflt = 0;
1096 rup->ru_nswap = 0;
1097 rup->ru_inblock = 0;
1098 rup->ru_oublock = 0;
1099 rup->ru_msgsnd = 0;
1100 rup->ru_msgrcv = 0;
1101 rup->ru_nsignals = 0;
1102 rup->ru_nvcsw = 0;
1103 rup->ru_nivcsw = 0;
1104
1105 switch (who) {
1106 case OS::TGT_RUSAGE_SELF:
1107 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1108 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1109 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1110 break;
1111
1112 case OS::TGT_RUSAGE_CHILDREN:
1113 // do nothing. We have no child processes, so they take no time.
1114 break;
1115
1116 default:
1117 // don't really handle THREAD or CHILDREN, but just warn and
1118 // plow ahead
1119 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1120 who);
1121 }
1122
1123 rup.copyOut(tc->getMemPort());
1124
1125 return 0;
1126 }
1127
1128
1129
1130
1131 #endif // __SIM_SYSCALL_EMUL_HH__