syscall_emul.hh (2632:1bb2f91485ea) syscall_emul.hh (2665:a124942bacb8)
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.
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
27 */
28
29#ifndef __SIM_SYSCALL_EMUL_HH__
30#define __SIM_SYSCALL_EMUL_HH__
31
32#define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \
33 defined(__FreeBSD__))
34
35///
36/// @file syscall_emul.hh
37///
38/// This file defines objects used to emulate syscalls from the target
39/// application on the host machine.
40
41#include <errno.h>
42#include <string>
43#ifdef __CYGWIN32__
44#include <sys/fcntl.h> // for O_BINARY
45#endif
46#include <sys/uio.h>
47
48#include "arch/isa_traits.hh" // for Addr
49#include "base/chunk_generator.hh"
50#include "base/intmath.hh" // for RoundUp
51#include "base/misc.hh"
52#include "base/trace.hh"
53#include "cpu/base.hh"
54#include "cpu/exec_context.hh"
55#include "mem/translating_port.hh"
56#include "mem/page_table.hh"
57#include "sim/process.hh"
58
59///
60/// System call descriptor.
61///
62class SyscallDesc {
63
64 public:
65
66 /// Typedef for target syscall handler functions.
67 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
68 Process *, ExecContext *);
69
70 const char *name; //!< Syscall name (e.g., "open").
71 FuncPtr funcPtr; //!< Pointer to emulation function.
72 int flags; //!< Flags (see Flags enum).
73
74 /// Flag values for controlling syscall behavior.
75 enum Flags {
76 /// Don't set return regs according to funcPtr return value.
77 /// Used for syscalls with non-standard return conventions
78 /// that explicitly set the ExecContext regs (e.g.,
79 /// sigreturn).
80 SuppressReturnValue = 1
81 };
82
83 /// Constructor.
84 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
85 : name(_name), funcPtr(_funcPtr), flags(_flags)
86 {
87 }
88
89 /// Emulate the syscall. Public interface for calling through funcPtr.
90 void doSyscall(int callnum, Process *proc, ExecContext *xc);
91};
92
93
94class BaseBufferArg {
95
96 public:
97
98 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
99 {
100 bufPtr = new uint8_t[size];
101 // clear out buffer: in case we only partially populate this,
102 // and then do a copyOut(), we want to make sure we don't
103 // introduce any random junk into the simulated address space
104 memset(bufPtr, 0, size);
105 }
106
107 virtual ~BaseBufferArg() { delete [] bufPtr; }
108
109 //
110 // copy data into simulator space (read from target memory)
111 //
112 virtual bool copyIn(TranslatingPort *memport)
113 {
114 memport->readBlob(addr, bufPtr, size);
115 return true; // no EFAULT detection for now
116 }
117
118 //
119 // copy data out of simulator space (write to target memory)
120 //
121 virtual bool copyOut(TranslatingPort *memport)
122 {
123 memport->writeBlob(addr, bufPtr, size);
124 return true; // no EFAULT detection for now
125 }
126
127 protected:
128 Addr addr;
129 int size;
130 uint8_t *bufPtr;
131};
132
133
134class BufferArg : public BaseBufferArg
135{
136 public:
137 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
138 void *bufferPtr() { return bufPtr; }
139};
140
141template <class T>
142class TypedBufferArg : public BaseBufferArg
143{
144 public:
145 // user can optionally specify a specific number of bytes to
146 // allocate to deal with those structs that have variable-size
147 // arrays at the end
148 TypedBufferArg(Addr _addr, int _size = sizeof(T))
149 : BaseBufferArg(_addr, _size)
150 { }
151
152 // type case
153 operator T*() { return (T *)bufPtr; }
154
155 // dereference operators
156 T &operator*() { return *((T *)bufPtr); }
157 T* operator->() { return (T *)bufPtr; }
158 T &operator[](int i) { return ((T *)bufPtr)[i]; }
159};
160
161//////////////////////////////////////////////////////////////////////
162//
163// The following emulation functions are generic enough that they
164// don't need to be recompiled for different emulated OS's. They are
165// defined in sim/syscall_emul.cc.
166//
167//////////////////////////////////////////////////////////////////////
168
169
170/// Handler for unimplemented syscalls that we haven't thought about.
171SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
172 Process *p, ExecContext *xc);
173
174/// Handler for unimplemented syscalls that we never intend to
175/// implement (signal handling, etc.) and should not affect the correct
176/// behavior of the program. Print a warning only if the appropriate
177/// trace flag is enabled. Return success to the target program.
178SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
179 Process *p, ExecContext *xc);
180
181/// Target exit() handler: terminate simulation.
182SyscallReturn exitFunc(SyscallDesc *desc, int num,
183 Process *p, ExecContext *xc);
184
185/// Target getpagesize() handler.
186SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
187 Process *p, ExecContext *xc);
188
189/// Target obreak() handler: set brk address.
190SyscallReturn obreakFunc(SyscallDesc *desc, int num,
191 Process *p, ExecContext *xc);
192
193/// Target close() handler.
194SyscallReturn closeFunc(SyscallDesc *desc, int num,
195 Process *p, ExecContext *xc);
196
197/// Target read() handler.
198SyscallReturn readFunc(SyscallDesc *desc, int num,
199 Process *p, ExecContext *xc);
200
201/// Target write() handler.
202SyscallReturn writeFunc(SyscallDesc *desc, int num,
203 Process *p, ExecContext *xc);
204
205/// Target lseek() handler.
206SyscallReturn lseekFunc(SyscallDesc *desc, int num,
207 Process *p, ExecContext *xc);
208
209/// Target munmap() handler.
210SyscallReturn munmapFunc(SyscallDesc *desc, int num,
211 Process *p, ExecContext *xc);
212
213/// Target gethostname() handler.
214SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
215 Process *p, ExecContext *xc);
216
217/// Target unlink() handler.
218SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
219 Process *p, ExecContext *xc);
220
221/// Target rename() handler.
222SyscallReturn renameFunc(SyscallDesc *desc, int num,
223 Process *p, ExecContext *xc);
224
225
226/// Target truncate() handler.
227SyscallReturn truncateFunc(SyscallDesc *desc, int num,
228 Process *p, ExecContext *xc);
229
230
231/// Target ftruncate() handler.
232SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
233 Process *p, ExecContext *xc);
234
235
236/// Target chown() handler.
237SyscallReturn chownFunc(SyscallDesc *desc, int num,
238 Process *p, ExecContext *xc);
239
240
241/// Target fchown() handler.
242SyscallReturn fchownFunc(SyscallDesc *desc, int num,
243 Process *p, ExecContext *xc);
244
245/// Target fnctl() handler.
246SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
247 Process *process, ExecContext *xc);
248
249/// Target setuid() handler.
250SyscallReturn setuidFunc(SyscallDesc *desc, int num,
251 Process *p, ExecContext *xc);
252
253/// Target getpid() handler.
254SyscallReturn getpidFunc(SyscallDesc *desc, int num,
255 Process *p, ExecContext *xc);
256
257/// Target getuid() handler.
258SyscallReturn getuidFunc(SyscallDesc *desc, int num,
259 Process *p, ExecContext *xc);
260
261/// Target getgid() handler.
262SyscallReturn getgidFunc(SyscallDesc *desc, int num,
263 Process *p, ExecContext *xc);
264
265/// Target getppid() handler.
266SyscallReturn getppidFunc(SyscallDesc *desc, int num,
267 Process *p, ExecContext *xc);
268
269/// Target geteuid() handler.
270SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
271 Process *p, ExecContext *xc);
272
273/// Target getegid() handler.
274SyscallReturn getegidFunc(SyscallDesc *desc, int num,
275 Process *p, ExecContext *xc);
276
277
278
279/// Pseudo Funcs - These functions use a different return convension,
280/// returning a second value in a register other than the normal return register
281SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
282 Process *process, ExecContext *xc);
283
284/// Target getpidPseudo() handler.
285SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
286 Process *p, ExecContext *xc);
287
288/// Target getuidPseudo() handler.
289SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
290 Process *p, ExecContext *xc);
291
292/// Target getgidPseudo() handler.
293SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
294 Process *p, ExecContext *xc);
295
296
297/// This struct is used to build an target-OS-dependent table that
298/// maps the target's open() flags to the host open() flags.
299struct OpenFlagTransTable {
300 int tgtFlag; //!< Target system flag value.
301 int hostFlag; //!< Corresponding host system flag value.
302};
303
304
305
306/// A readable name for 1,000,000, for converting microseconds to seconds.
307const int one_million = 1000000;
308
309/// Approximate seconds since the epoch (1/1/1970). About a billion,
310/// by my reckoning. We want to keep this a constant (not use the
311/// real-world time) to keep simulations repeatable.
312const unsigned seconds_since_epoch = 1000000000;
313
314/// Helper function to convert current elapsed time to seconds and
315/// microseconds.
316template <class T1, class T2>
317void
318getElapsedTime(T1 &sec, T2 &usec)
319{
320 int elapsed_usecs = curTick / Clock::Int::us;
321 sec = elapsed_usecs / one_million;
322 usec = elapsed_usecs % one_million;
323}
324
325//////////////////////////////////////////////////////////////////////
326//
327// The following emulation functions are generic, but need to be
328// templated to account for differences in types, constants, etc.
329//
330//////////////////////////////////////////////////////////////////////
331
332/// Target ioctl() handler. For the most part, programs call ioctl()
333/// only to find out if their stdout is a tty, to determine whether to
334/// do line or block buffering.
335template <class OS>
336SyscallReturn
337ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
338 ExecContext *xc)
339{
340 int fd = xc->getSyscallArg(0);
341 unsigned req = xc->getSyscallArg(1);
342
343 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
344
345 if (fd < 0 || process->sim_fd(fd) < 0) {
346 // doesn't map to any simulator fd: not a valid target fd
347 return -EBADF;
348 }
349
350 switch (req) {
351 case OS::TIOCISATTY:
352 case OS::TIOCGETP:
353 case OS::TIOCSETP:
354 case OS::TIOCSETN:
355 case OS::TIOCSETC:
356 case OS::TIOCGETC:
357 case OS::TIOCGETS:
358 case OS::TIOCGETA:
359 return -ENOTTY;
360
361 default:
362 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
363 fd, req, xc->readPC());
364 }
365}
366
367/// Target open() handler.
368template <class OS>
369SyscallReturn
370openFunc(SyscallDesc *desc, int callnum, Process *process,
371 ExecContext *xc)
372{
373 std::string path;
374
375 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
376 return -EFAULT;
377
378 if (path == "/dev/sysdev0") {
379 // This is a memory-mapped high-resolution timer device on Alpha.
380 // We don't support it, so just punt.
381 warn("Ignoring open(%s, ...)\n", path);
382 return -ENOENT;
383 }
384
385 int tgtFlags = xc->getSyscallArg(1);
386 int mode = xc->getSyscallArg(2);
387 int hostFlags = 0;
388
389 // translate open flags
390 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
391 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
392 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
393 hostFlags |= OS::openFlagTable[i].hostFlag;
394 }
395 }
396
397 // any target flags left?
398 if (tgtFlags != 0)
399 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
400
401#ifdef __CYGWIN32__
402 hostFlags |= O_BINARY;
403#endif
404
405 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
406
407 // open the file
408 int fd = open(path.c_str(), hostFlags, mode);
409
410 return (fd == -1) ? -errno : process->alloc_fd(fd);
411}
412
413
414/// Target chmod() handler.
415template <class OS>
416SyscallReturn
417chmodFunc(SyscallDesc *desc, int callnum, Process *process,
418 ExecContext *xc)
419{
420 std::string path;
421
422 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
423 return -EFAULT;
424
425 uint32_t mode = xc->getSyscallArg(1);
426 mode_t hostMode = 0;
427
428 // XXX translate mode flags via OS::something???
429 hostMode = mode;
430
431 // do the chmod
432 int result = chmod(path.c_str(), hostMode);
433 if (result < 0)
434 return -errno;
435
436 return 0;
437}
438
439
440/// Target fchmod() handler.
441template <class OS>
442SyscallReturn
443fchmodFunc(SyscallDesc *desc, int callnum, Process *process,
444 ExecContext *xc)
445{
446 int fd = xc->getSyscallArg(0);
447 if (fd < 0 || process->sim_fd(fd) < 0) {
448 // doesn't map to any simulator fd: not a valid target fd
449 return -EBADF;
450 }
451
452 uint32_t mode = xc->getSyscallArg(1);
453 mode_t hostMode = 0;
454
455 // XXX translate mode flags via OS::someting???
456 hostMode = mode;
457
458 // do the fchmod
459 int result = fchmod(process->sim_fd(fd), hostMode);
460 if (result < 0)
461 return -errno;
462
463 return 0;
464}
465
466
467/// Target stat() handler.
468template <class OS>
469SyscallReturn
470statFunc(SyscallDesc *desc, int callnum, Process *process,
471 ExecContext *xc)
472{
473 std::string path;
474
475 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
476 return -EFAULT;
477
478 struct stat hostBuf;
479 int result = stat(path.c_str(), &hostBuf);
480
481 if (result < 0)
482 return -errno;
483
484 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
485
486 return 0;
487}
488
489
490/// Target fstat64() handler.
491template <class OS>
492SyscallReturn
493fstat64Func(SyscallDesc *desc, int callnum, Process *process,
494 ExecContext *xc)
495{
496 int fd = xc->getSyscallArg(0);
497 if (fd < 0 || process->sim_fd(fd) < 0) {
498 // doesn't map to any simulator fd: not a valid target fd
499 return -EBADF;
500 }
501
502#if BSD_HOST
503 struct stat hostBuf;
504 int result = fstat(process->sim_fd(fd), &hostBuf);
505#else
506 struct stat64 hostBuf;
507 int result = fstat64(process->sim_fd(fd), &hostBuf);
508#endif
509
510 if (result < 0)
511 return -errno;
512
513 OS::copyOutStat64Buf(xc->getMemPort(), fd, xc->getSyscallArg(1), &hostBuf);
514
515 return 0;
516}
517
518
519/// Target lstat() handler.
520template <class OS>
521SyscallReturn
522lstatFunc(SyscallDesc *desc, int callnum, Process *process,
523 ExecContext *xc)
524{
525 std::string path;
526
527 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
528 return -EFAULT;
529
530 struct stat hostBuf;
531 int result = lstat(path.c_str(), &hostBuf);
532
533 if (result < 0)
534 return -errno;
535
536 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
537
538 return 0;
539}
540
541/// Target lstat64() handler.
542template <class OS>
543SyscallReturn
544lstat64Func(SyscallDesc *desc, int callnum, Process *process,
545 ExecContext *xc)
546{
547 std::string path;
548
549 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
550 return -EFAULT;
551
552#if BSD_HOST
553 struct stat hostBuf;
554 int result = lstat(path.c_str(), &hostBuf);
555#else
556 struct stat64 hostBuf;
557 int result = lstat64(path.c_str(), &hostBuf);
558#endif
559
560 if (result < 0)
561 return -errno;
562
563 OS::copyOutStat64Buf(xc->getMemPort(), -1, xc->getSyscallArg(1), &hostBuf);
564
565 return 0;
566}
567
568/// Target fstat() handler.
569template <class OS>
570SyscallReturn
571fstatFunc(SyscallDesc *desc, int callnum, Process *process,
572 ExecContext *xc)
573{
574 int fd = process->sim_fd(xc->getSyscallArg(0));
575
576 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
577
578 if (fd < 0)
579 return -EBADF;
580
581 struct stat hostBuf;
582 int result = fstat(fd, &hostBuf);
583
584 if (result < 0)
585 return -errno;
586
587 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
588
589 return 0;
590}
591
592
593/// Target statfs() handler.
594template <class OS>
595SyscallReturn
596statfsFunc(SyscallDesc *desc, int callnum, Process *process,
597 ExecContext *xc)
598{
599 std::string path;
600
601 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
602 return -EFAULT;
603
604 struct statfs hostBuf;
605 int result = statfs(path.c_str(), &hostBuf);
606
607 if (result < 0)
608 return -errno;
609
610 OS::copyOutStatfsBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
611
612 return 0;
613}
614
615
616/// Target fstatfs() handler.
617template <class OS>
618SyscallReturn
619fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
620 ExecContext *xc)
621{
622 int fd = process->sim_fd(xc->getSyscallArg(0));
623
624 if (fd < 0)
625 return -EBADF;
626
627 struct statfs hostBuf;
628 int result = fstatfs(fd, &hostBuf);
629
630 if (result < 0)
631 return -errno;
632
633 OS::copyOutStatfsBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
634
635 return 0;
636}
637
638
639/// Target writev() handler.
640template <class OS>
641SyscallReturn
642writevFunc(SyscallDesc *desc, int callnum, Process *process,
643 ExecContext *xc)
644{
645 int fd = xc->getSyscallArg(0);
646 if (fd < 0 || process->sim_fd(fd) < 0) {
647 // doesn't map to any simulator fd: not a valid target fd
648 return -EBADF;
649 }
650
651 TranslatingPort *p = xc->getMemPort();
652 uint64_t tiov_base = xc->getSyscallArg(1);
653 size_t count = xc->getSyscallArg(2);
654 struct iovec hiov[count];
655 for (int i = 0; i < count; ++i)
656 {
657 typename OS::tgt_iovec tiov;
658
659 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
660 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
661 hiov[i].iov_len = gtoh(tiov.iov_len);
662 hiov[i].iov_base = new char [hiov[i].iov_len];
663 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
664 hiov[i].iov_len);
665 }
666
667 int result = writev(process->sim_fd(fd), hiov, count);
668
669 for (int i = 0; i < count; ++i)
670 {
671 delete [] (char *)hiov[i].iov_base;
672 }
673
674 if (result < 0)
675 return -errno;
676
677 return 0;
678}
679
680
681/// Target mmap() handler.
682///
683/// We don't really handle mmap(). If the target is mmaping an
684/// anonymous region or /dev/zero, we can get away with doing basically
685/// nothing (since memory is initialized to zero and the simulator
686/// doesn't really check addresses anyway). Always print a warning,
687/// since this could be seriously broken if we're not mapping
688/// /dev/zero.
689//
690/// Someday we should explicitly check for /dev/zero in open, flag the
691/// file descriptor, and fail (or implement!) a non-anonymous mmap to
692/// anything else.
693template <class OS>
694SyscallReturn
695mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
696{
697 Addr start = xc->getSyscallArg(0);
698 uint64_t length = xc->getSyscallArg(1);
699 // int prot = xc->getSyscallArg(2);
700 int flags = xc->getSyscallArg(3);
701 // int fd = p->sim_fd(xc->getSyscallArg(4));
702 // int offset = xc->getSyscallArg(5);
703
704 if ((start % TheISA::VMPageSize) != 0 ||
705 (length % TheISA::VMPageSize) != 0) {
706 warn("mmap failing: arguments not page-aligned: "
707 "start 0x%x length 0x%x",
708 start, length);
709 return -EINVAL;
710 }
711
712 if (start != 0) {
713 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
714 start, p->mmap_end);
715 }
716
717 // pick next address from our "mmap region"
718 start = p->mmap_end;
719 p->pTable->allocate(start, length);
720 p->mmap_end += length;
721
722 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
723 warn("allowing mmap of file @ fd %d. "
724 "This will break if not /dev/zero.", xc->getSyscallArg(4));
725 }
726
727 return start;
728}
729
730/// Target getrlimit() handler.
731template <class OS>
732SyscallReturn
733getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
734 ExecContext *xc)
735{
736 unsigned resource = xc->getSyscallArg(0);
737 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
738
739 switch (resource) {
740 case OS::TGT_RLIMIT_STACK:
741 // max stack size in bytes: make up a number (2MB for now)
742 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
743 rlp->rlim_cur = htog(rlp->rlim_cur);
744 rlp->rlim_max = htog(rlp->rlim_max);
745 break;
746
747 default:
748 std::cerr << "getrlimitFunc: unimplemented resource " << resource
749 << std::endl;
750 abort();
751 break;
752 }
753
754 rlp.copyOut(xc->getMemPort());
755 return 0;
756}
757
758/// Target gettimeofday() handler.
759template <class OS>
760SyscallReturn
761gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
762 ExecContext *xc)
763{
764 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
765
766 getElapsedTime(tp->tv_sec, tp->tv_usec);
767 tp->tv_sec += seconds_since_epoch;
768 tp->tv_sec = htog(tp->tv_sec);
769 tp->tv_usec = htog(tp->tv_usec);
770
771 tp.copyOut(xc->getMemPort());
772
773 return 0;
774}
775
776
777/// Target utimes() handler.
778template <class OS>
779SyscallReturn
780utimesFunc(SyscallDesc *desc, int callnum, Process *process,
781 ExecContext *xc)
782{
783 std::string path;
784
785 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
786 return -EFAULT;
787
788 TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1));
789 tp.copyIn(xc->getMemPort());
790
791 struct timeval hostTimeval[2];
792 for (int i = 0; i < 2; ++i)
793 {
794 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
795 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
796 }
797 int result = utimes(path.c_str(), hostTimeval);
798
799 if (result < 0)
800 return -errno;
801
802 return 0;
803}
804/// Target getrusage() function.
805template <class OS>
806SyscallReturn
807getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
808 ExecContext *xc)
809{
810 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
811 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
812
813 if (who != OS::TGT_RUSAGE_SELF) {
814 // don't really handle THREAD or CHILDREN, but just warn and
815 // plow ahead
816 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
817 who);
818 }
819
820 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
821 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
822 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
823
824 rup->ru_stime.tv_sec = 0;
825 rup->ru_stime.tv_usec = 0;
826 rup->ru_maxrss = 0;
827 rup->ru_ixrss = 0;
828 rup->ru_idrss = 0;
829 rup->ru_isrss = 0;
830 rup->ru_minflt = 0;
831 rup->ru_majflt = 0;
832 rup->ru_nswap = 0;
833 rup->ru_inblock = 0;
834 rup->ru_oublock = 0;
835 rup->ru_msgsnd = 0;
836 rup->ru_msgrcv = 0;
837 rup->ru_nsignals = 0;
838 rup->ru_nvcsw = 0;
839 rup->ru_nivcsw = 0;
840
841 rup.copyOut(xc->getMemPort());
842
843 return 0;
844}
845
846
847
848
849#endif // __SIM_SYSCALL_EMUL_HH__
30 */
31
32#ifndef __SIM_SYSCALL_EMUL_HH__
33#define __SIM_SYSCALL_EMUL_HH__
34
35#define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \
36 defined(__FreeBSD__))
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/uio.h>
50
51#include "arch/isa_traits.hh" // for Addr
52#include "base/chunk_generator.hh"
53#include "base/intmath.hh" // for RoundUp
54#include "base/misc.hh"
55#include "base/trace.hh"
56#include "cpu/base.hh"
57#include "cpu/exec_context.hh"
58#include "mem/translating_port.hh"
59#include "mem/page_table.hh"
60#include "sim/process.hh"
61
62///
63/// System call descriptor.
64///
65class SyscallDesc {
66
67 public:
68
69 /// Typedef for target syscall handler functions.
70 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
71 Process *, ExecContext *);
72
73 const char *name; //!< Syscall name (e.g., "open").
74 FuncPtr funcPtr; //!< Pointer to emulation function.
75 int flags; //!< Flags (see Flags enum).
76
77 /// Flag values for controlling syscall behavior.
78 enum Flags {
79 /// Don't set return regs according to funcPtr return value.
80 /// Used for syscalls with non-standard return conventions
81 /// that explicitly set the ExecContext regs (e.g.,
82 /// sigreturn).
83 SuppressReturnValue = 1
84 };
85
86 /// Constructor.
87 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
88 : name(_name), funcPtr(_funcPtr), flags(_flags)
89 {
90 }
91
92 /// Emulate the syscall. Public interface for calling through funcPtr.
93 void doSyscall(int callnum, Process *proc, ExecContext *xc);
94};
95
96
97class BaseBufferArg {
98
99 public:
100
101 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
102 {
103 bufPtr = new uint8_t[size];
104 // clear out buffer: in case we only partially populate this,
105 // and then do a copyOut(), we want to make sure we don't
106 // introduce any random junk into the simulated address space
107 memset(bufPtr, 0, size);
108 }
109
110 virtual ~BaseBufferArg() { delete [] bufPtr; }
111
112 //
113 // copy data into simulator space (read from target memory)
114 //
115 virtual bool copyIn(TranslatingPort *memport)
116 {
117 memport->readBlob(addr, bufPtr, size);
118 return true; // no EFAULT detection for now
119 }
120
121 //
122 // copy data out of simulator space (write to target memory)
123 //
124 virtual bool copyOut(TranslatingPort *memport)
125 {
126 memport->writeBlob(addr, bufPtr, size);
127 return true; // no EFAULT detection for now
128 }
129
130 protected:
131 Addr addr;
132 int size;
133 uint8_t *bufPtr;
134};
135
136
137class BufferArg : public BaseBufferArg
138{
139 public:
140 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
141 void *bufferPtr() { return bufPtr; }
142};
143
144template <class T>
145class TypedBufferArg : public BaseBufferArg
146{
147 public:
148 // user can optionally specify a specific number of bytes to
149 // allocate to deal with those structs that have variable-size
150 // arrays at the end
151 TypedBufferArg(Addr _addr, int _size = sizeof(T))
152 : BaseBufferArg(_addr, _size)
153 { }
154
155 // type case
156 operator T*() { return (T *)bufPtr; }
157
158 // dereference operators
159 T &operator*() { return *((T *)bufPtr); }
160 T* operator->() { return (T *)bufPtr; }
161 T &operator[](int i) { return ((T *)bufPtr)[i]; }
162};
163
164//////////////////////////////////////////////////////////////////////
165//
166// The following emulation functions are generic enough that they
167// don't need to be recompiled for different emulated OS's. They are
168// defined in sim/syscall_emul.cc.
169//
170//////////////////////////////////////////////////////////////////////
171
172
173/// Handler for unimplemented syscalls that we haven't thought about.
174SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
175 Process *p, ExecContext *xc);
176
177/// Handler for unimplemented syscalls that we never intend to
178/// implement (signal handling, etc.) and should not affect the correct
179/// behavior of the program. Print a warning only if the appropriate
180/// trace flag is enabled. Return success to the target program.
181SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
182 Process *p, ExecContext *xc);
183
184/// Target exit() handler: terminate simulation.
185SyscallReturn exitFunc(SyscallDesc *desc, int num,
186 Process *p, ExecContext *xc);
187
188/// Target getpagesize() handler.
189SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
190 Process *p, ExecContext *xc);
191
192/// Target obreak() handler: set brk address.
193SyscallReturn obreakFunc(SyscallDesc *desc, int num,
194 Process *p, ExecContext *xc);
195
196/// Target close() handler.
197SyscallReturn closeFunc(SyscallDesc *desc, int num,
198 Process *p, ExecContext *xc);
199
200/// Target read() handler.
201SyscallReturn readFunc(SyscallDesc *desc, int num,
202 Process *p, ExecContext *xc);
203
204/// Target write() handler.
205SyscallReturn writeFunc(SyscallDesc *desc, int num,
206 Process *p, ExecContext *xc);
207
208/// Target lseek() handler.
209SyscallReturn lseekFunc(SyscallDesc *desc, int num,
210 Process *p, ExecContext *xc);
211
212/// Target munmap() handler.
213SyscallReturn munmapFunc(SyscallDesc *desc, int num,
214 Process *p, ExecContext *xc);
215
216/// Target gethostname() handler.
217SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
218 Process *p, ExecContext *xc);
219
220/// Target unlink() handler.
221SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
222 Process *p, ExecContext *xc);
223
224/// Target rename() handler.
225SyscallReturn renameFunc(SyscallDesc *desc, int num,
226 Process *p, ExecContext *xc);
227
228
229/// Target truncate() handler.
230SyscallReturn truncateFunc(SyscallDesc *desc, int num,
231 Process *p, ExecContext *xc);
232
233
234/// Target ftruncate() handler.
235SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
236 Process *p, ExecContext *xc);
237
238
239/// Target chown() handler.
240SyscallReturn chownFunc(SyscallDesc *desc, int num,
241 Process *p, ExecContext *xc);
242
243
244/// Target fchown() handler.
245SyscallReturn fchownFunc(SyscallDesc *desc, int num,
246 Process *p, ExecContext *xc);
247
248/// Target fnctl() handler.
249SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
250 Process *process, ExecContext *xc);
251
252/// Target setuid() handler.
253SyscallReturn setuidFunc(SyscallDesc *desc, int num,
254 Process *p, ExecContext *xc);
255
256/// Target getpid() handler.
257SyscallReturn getpidFunc(SyscallDesc *desc, int num,
258 Process *p, ExecContext *xc);
259
260/// Target getuid() handler.
261SyscallReturn getuidFunc(SyscallDesc *desc, int num,
262 Process *p, ExecContext *xc);
263
264/// Target getgid() handler.
265SyscallReturn getgidFunc(SyscallDesc *desc, int num,
266 Process *p, ExecContext *xc);
267
268/// Target getppid() handler.
269SyscallReturn getppidFunc(SyscallDesc *desc, int num,
270 Process *p, ExecContext *xc);
271
272/// Target geteuid() handler.
273SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
274 Process *p, ExecContext *xc);
275
276/// Target getegid() handler.
277SyscallReturn getegidFunc(SyscallDesc *desc, int num,
278 Process *p, ExecContext *xc);
279
280
281
282/// Pseudo Funcs - These functions use a different return convension,
283/// returning a second value in a register other than the normal return register
284SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
285 Process *process, ExecContext *xc);
286
287/// Target getpidPseudo() handler.
288SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
289 Process *p, ExecContext *xc);
290
291/// Target getuidPseudo() handler.
292SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
293 Process *p, ExecContext *xc);
294
295/// Target getgidPseudo() handler.
296SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
297 Process *p, ExecContext *xc);
298
299
300/// This struct is used to build an target-OS-dependent table that
301/// maps the target's open() flags to the host open() flags.
302struct OpenFlagTransTable {
303 int tgtFlag; //!< Target system flag value.
304 int hostFlag; //!< Corresponding host system flag value.
305};
306
307
308
309/// A readable name for 1,000,000, for converting microseconds to seconds.
310const int one_million = 1000000;
311
312/// Approximate seconds since the epoch (1/1/1970). About a billion,
313/// by my reckoning. We want to keep this a constant (not use the
314/// real-world time) to keep simulations repeatable.
315const unsigned seconds_since_epoch = 1000000000;
316
317/// Helper function to convert current elapsed time to seconds and
318/// microseconds.
319template <class T1, class T2>
320void
321getElapsedTime(T1 &sec, T2 &usec)
322{
323 int elapsed_usecs = curTick / Clock::Int::us;
324 sec = elapsed_usecs / one_million;
325 usec = elapsed_usecs % one_million;
326}
327
328//////////////////////////////////////////////////////////////////////
329//
330// The following emulation functions are generic, but need to be
331// templated to account for differences in types, constants, etc.
332//
333//////////////////////////////////////////////////////////////////////
334
335/// Target ioctl() handler. For the most part, programs call ioctl()
336/// only to find out if their stdout is a tty, to determine whether to
337/// do line or block buffering.
338template <class OS>
339SyscallReturn
340ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
341 ExecContext *xc)
342{
343 int fd = xc->getSyscallArg(0);
344 unsigned req = xc->getSyscallArg(1);
345
346 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
347
348 if (fd < 0 || process->sim_fd(fd) < 0) {
349 // doesn't map to any simulator fd: not a valid target fd
350 return -EBADF;
351 }
352
353 switch (req) {
354 case OS::TIOCISATTY:
355 case OS::TIOCGETP:
356 case OS::TIOCSETP:
357 case OS::TIOCSETN:
358 case OS::TIOCSETC:
359 case OS::TIOCGETC:
360 case OS::TIOCGETS:
361 case OS::TIOCGETA:
362 return -ENOTTY;
363
364 default:
365 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
366 fd, req, xc->readPC());
367 }
368}
369
370/// Target open() handler.
371template <class OS>
372SyscallReturn
373openFunc(SyscallDesc *desc, int callnum, Process *process,
374 ExecContext *xc)
375{
376 std::string path;
377
378 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
379 return -EFAULT;
380
381 if (path == "/dev/sysdev0") {
382 // This is a memory-mapped high-resolution timer device on Alpha.
383 // We don't support it, so just punt.
384 warn("Ignoring open(%s, ...)\n", path);
385 return -ENOENT;
386 }
387
388 int tgtFlags = xc->getSyscallArg(1);
389 int mode = xc->getSyscallArg(2);
390 int hostFlags = 0;
391
392 // translate open flags
393 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
394 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
395 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
396 hostFlags |= OS::openFlagTable[i].hostFlag;
397 }
398 }
399
400 // any target flags left?
401 if (tgtFlags != 0)
402 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
403
404#ifdef __CYGWIN32__
405 hostFlags |= O_BINARY;
406#endif
407
408 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
409
410 // open the file
411 int fd = open(path.c_str(), hostFlags, mode);
412
413 return (fd == -1) ? -errno : process->alloc_fd(fd);
414}
415
416
417/// Target chmod() handler.
418template <class OS>
419SyscallReturn
420chmodFunc(SyscallDesc *desc, int callnum, Process *process,
421 ExecContext *xc)
422{
423 std::string path;
424
425 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
426 return -EFAULT;
427
428 uint32_t mode = xc->getSyscallArg(1);
429 mode_t hostMode = 0;
430
431 // XXX translate mode flags via OS::something???
432 hostMode = mode;
433
434 // do the chmod
435 int result = chmod(path.c_str(), hostMode);
436 if (result < 0)
437 return -errno;
438
439 return 0;
440}
441
442
443/// Target fchmod() handler.
444template <class OS>
445SyscallReturn
446fchmodFunc(SyscallDesc *desc, int callnum, Process *process,
447 ExecContext *xc)
448{
449 int fd = xc->getSyscallArg(0);
450 if (fd < 0 || process->sim_fd(fd) < 0) {
451 // doesn't map to any simulator fd: not a valid target fd
452 return -EBADF;
453 }
454
455 uint32_t mode = xc->getSyscallArg(1);
456 mode_t hostMode = 0;
457
458 // XXX translate mode flags via OS::someting???
459 hostMode = mode;
460
461 // do the fchmod
462 int result = fchmod(process->sim_fd(fd), hostMode);
463 if (result < 0)
464 return -errno;
465
466 return 0;
467}
468
469
470/// Target stat() handler.
471template <class OS>
472SyscallReturn
473statFunc(SyscallDesc *desc, int callnum, Process *process,
474 ExecContext *xc)
475{
476 std::string path;
477
478 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
479 return -EFAULT;
480
481 struct stat hostBuf;
482 int result = stat(path.c_str(), &hostBuf);
483
484 if (result < 0)
485 return -errno;
486
487 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
488
489 return 0;
490}
491
492
493/// Target fstat64() handler.
494template <class OS>
495SyscallReturn
496fstat64Func(SyscallDesc *desc, int callnum, Process *process,
497 ExecContext *xc)
498{
499 int fd = xc->getSyscallArg(0);
500 if (fd < 0 || process->sim_fd(fd) < 0) {
501 // doesn't map to any simulator fd: not a valid target fd
502 return -EBADF;
503 }
504
505#if BSD_HOST
506 struct stat hostBuf;
507 int result = fstat(process->sim_fd(fd), &hostBuf);
508#else
509 struct stat64 hostBuf;
510 int result = fstat64(process->sim_fd(fd), &hostBuf);
511#endif
512
513 if (result < 0)
514 return -errno;
515
516 OS::copyOutStat64Buf(xc->getMemPort(), fd, xc->getSyscallArg(1), &hostBuf);
517
518 return 0;
519}
520
521
522/// Target lstat() handler.
523template <class OS>
524SyscallReturn
525lstatFunc(SyscallDesc *desc, int callnum, Process *process,
526 ExecContext *xc)
527{
528 std::string path;
529
530 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
531 return -EFAULT;
532
533 struct stat hostBuf;
534 int result = lstat(path.c_str(), &hostBuf);
535
536 if (result < 0)
537 return -errno;
538
539 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
540
541 return 0;
542}
543
544/// Target lstat64() handler.
545template <class OS>
546SyscallReturn
547lstat64Func(SyscallDesc *desc, int callnum, Process *process,
548 ExecContext *xc)
549{
550 std::string path;
551
552 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
553 return -EFAULT;
554
555#if BSD_HOST
556 struct stat hostBuf;
557 int result = lstat(path.c_str(), &hostBuf);
558#else
559 struct stat64 hostBuf;
560 int result = lstat64(path.c_str(), &hostBuf);
561#endif
562
563 if (result < 0)
564 return -errno;
565
566 OS::copyOutStat64Buf(xc->getMemPort(), -1, xc->getSyscallArg(1), &hostBuf);
567
568 return 0;
569}
570
571/// Target fstat() handler.
572template <class OS>
573SyscallReturn
574fstatFunc(SyscallDesc *desc, int callnum, Process *process,
575 ExecContext *xc)
576{
577 int fd = process->sim_fd(xc->getSyscallArg(0));
578
579 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
580
581 if (fd < 0)
582 return -EBADF;
583
584 struct stat hostBuf;
585 int result = fstat(fd, &hostBuf);
586
587 if (result < 0)
588 return -errno;
589
590 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
591
592 return 0;
593}
594
595
596/// Target statfs() handler.
597template <class OS>
598SyscallReturn
599statfsFunc(SyscallDesc *desc, int callnum, Process *process,
600 ExecContext *xc)
601{
602 std::string path;
603
604 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
605 return -EFAULT;
606
607 struct statfs hostBuf;
608 int result = statfs(path.c_str(), &hostBuf);
609
610 if (result < 0)
611 return -errno;
612
613 OS::copyOutStatfsBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
614
615 return 0;
616}
617
618
619/// Target fstatfs() handler.
620template <class OS>
621SyscallReturn
622fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
623 ExecContext *xc)
624{
625 int fd = process->sim_fd(xc->getSyscallArg(0));
626
627 if (fd < 0)
628 return -EBADF;
629
630 struct statfs hostBuf;
631 int result = fstatfs(fd, &hostBuf);
632
633 if (result < 0)
634 return -errno;
635
636 OS::copyOutStatfsBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
637
638 return 0;
639}
640
641
642/// Target writev() handler.
643template <class OS>
644SyscallReturn
645writevFunc(SyscallDesc *desc, int callnum, Process *process,
646 ExecContext *xc)
647{
648 int fd = xc->getSyscallArg(0);
649 if (fd < 0 || process->sim_fd(fd) < 0) {
650 // doesn't map to any simulator fd: not a valid target fd
651 return -EBADF;
652 }
653
654 TranslatingPort *p = xc->getMemPort();
655 uint64_t tiov_base = xc->getSyscallArg(1);
656 size_t count = xc->getSyscallArg(2);
657 struct iovec hiov[count];
658 for (int i = 0; i < count; ++i)
659 {
660 typename OS::tgt_iovec tiov;
661
662 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
663 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
664 hiov[i].iov_len = gtoh(tiov.iov_len);
665 hiov[i].iov_base = new char [hiov[i].iov_len];
666 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
667 hiov[i].iov_len);
668 }
669
670 int result = writev(process->sim_fd(fd), hiov, count);
671
672 for (int i = 0; i < count; ++i)
673 {
674 delete [] (char *)hiov[i].iov_base;
675 }
676
677 if (result < 0)
678 return -errno;
679
680 return 0;
681}
682
683
684/// Target mmap() handler.
685///
686/// We don't really handle mmap(). If the target is mmaping an
687/// anonymous region or /dev/zero, we can get away with doing basically
688/// nothing (since memory is initialized to zero and the simulator
689/// doesn't really check addresses anyway). Always print a warning,
690/// since this could be seriously broken if we're not mapping
691/// /dev/zero.
692//
693/// Someday we should explicitly check for /dev/zero in open, flag the
694/// file descriptor, and fail (or implement!) a non-anonymous mmap to
695/// anything else.
696template <class OS>
697SyscallReturn
698mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
699{
700 Addr start = xc->getSyscallArg(0);
701 uint64_t length = xc->getSyscallArg(1);
702 // int prot = xc->getSyscallArg(2);
703 int flags = xc->getSyscallArg(3);
704 // int fd = p->sim_fd(xc->getSyscallArg(4));
705 // int offset = xc->getSyscallArg(5);
706
707 if ((start % TheISA::VMPageSize) != 0 ||
708 (length % TheISA::VMPageSize) != 0) {
709 warn("mmap failing: arguments not page-aligned: "
710 "start 0x%x length 0x%x",
711 start, length);
712 return -EINVAL;
713 }
714
715 if (start != 0) {
716 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
717 start, p->mmap_end);
718 }
719
720 // pick next address from our "mmap region"
721 start = p->mmap_end;
722 p->pTable->allocate(start, length);
723 p->mmap_end += length;
724
725 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
726 warn("allowing mmap of file @ fd %d. "
727 "This will break if not /dev/zero.", xc->getSyscallArg(4));
728 }
729
730 return start;
731}
732
733/// Target getrlimit() handler.
734template <class OS>
735SyscallReturn
736getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
737 ExecContext *xc)
738{
739 unsigned resource = xc->getSyscallArg(0);
740 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
741
742 switch (resource) {
743 case OS::TGT_RLIMIT_STACK:
744 // max stack size in bytes: make up a number (2MB for now)
745 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
746 rlp->rlim_cur = htog(rlp->rlim_cur);
747 rlp->rlim_max = htog(rlp->rlim_max);
748 break;
749
750 default:
751 std::cerr << "getrlimitFunc: unimplemented resource " << resource
752 << std::endl;
753 abort();
754 break;
755 }
756
757 rlp.copyOut(xc->getMemPort());
758 return 0;
759}
760
761/// Target gettimeofday() handler.
762template <class OS>
763SyscallReturn
764gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
765 ExecContext *xc)
766{
767 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
768
769 getElapsedTime(tp->tv_sec, tp->tv_usec);
770 tp->tv_sec += seconds_since_epoch;
771 tp->tv_sec = htog(tp->tv_sec);
772 tp->tv_usec = htog(tp->tv_usec);
773
774 tp.copyOut(xc->getMemPort());
775
776 return 0;
777}
778
779
780/// Target utimes() handler.
781template <class OS>
782SyscallReturn
783utimesFunc(SyscallDesc *desc, int callnum, Process *process,
784 ExecContext *xc)
785{
786 std::string path;
787
788 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
789 return -EFAULT;
790
791 TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1));
792 tp.copyIn(xc->getMemPort());
793
794 struct timeval hostTimeval[2];
795 for (int i = 0; i < 2; ++i)
796 {
797 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
798 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
799 }
800 int result = utimes(path.c_str(), hostTimeval);
801
802 if (result < 0)
803 return -errno;
804
805 return 0;
806}
807/// Target getrusage() function.
808template <class OS>
809SyscallReturn
810getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
811 ExecContext *xc)
812{
813 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
814 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
815
816 if (who != OS::TGT_RUSAGE_SELF) {
817 // don't really handle THREAD or CHILDREN, but just warn and
818 // plow ahead
819 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
820 who);
821 }
822
823 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
824 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
825 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
826
827 rup->ru_stime.tv_sec = 0;
828 rup->ru_stime.tv_usec = 0;
829 rup->ru_maxrss = 0;
830 rup->ru_ixrss = 0;
831 rup->ru_idrss = 0;
832 rup->ru_isrss = 0;
833 rup->ru_minflt = 0;
834 rup->ru_majflt = 0;
835 rup->ru_nswap = 0;
836 rup->ru_inblock = 0;
837 rup->ru_oublock = 0;
838 rup->ru_msgsnd = 0;
839 rup->ru_msgrcv = 0;
840 rup->ru_nsignals = 0;
841 rup->ru_nvcsw = 0;
842 rup->ru_nivcsw = 0;
843
844 rup.copyOut(xc->getMemPort());
845
846 return 0;
847}
848
849
850
851
852#endif // __SIM_SYSCALL_EMUL_HH__