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