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