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