syscall_emul.cc revision 7823:dac01f14f20f
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 *          Ali Saidi
30 */
31
32#include <fcntl.h>
33#include <unistd.h>
34
35#include <cstdio>
36#include <iostream>
37#include <string>
38
39#include "arch/utility.hh"
40#include "sim/syscall_emul.hh"
41#include "base/chunk_generator.hh"
42#include "base/trace.hh"
43#include "config/the_isa.hh"
44#include "cpu/thread_context.hh"
45#include "cpu/base.hh"
46#include "mem/page_table.hh"
47#include "sim/process.hh"
48#include "sim/system.hh"
49#include "sim/sim_exit.hh"
50
51using namespace std;
52using namespace TheISA;
53
54void
55SyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
56{
57#if TRACING_ON
58    int index = 0;
59#endif
60    DPRINTFR(SyscallVerbose,
61             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
62             curTick(), tc->getCpuPtr()->name(), name,
63             process->getSyscallArg(tc, index),
64             process->getSyscallArg(tc, index),
65             process->getSyscallArg(tc, index),
66             process->getSyscallArg(tc, index));
67
68    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
69
70    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
71             curTick(),tc->getCpuPtr()->name(), name, retval.value());
72
73    if (!(flags & SyscallDesc::SuppressReturnValue))
74        process->setSyscallReturn(tc, retval);
75}
76
77
78SyscallReturn
79unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
80                  ThreadContext *tc)
81{
82    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
83
84    return 1;
85}
86
87
88SyscallReturn
89ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
90           ThreadContext *tc)
91{
92    int index = 0;
93    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
94         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
95
96    return 0;
97}
98
99
100SyscallReturn
101exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
102         ThreadContext *tc)
103{
104    if (process->system->numRunningContexts() == 1) {
105        // Last running context... exit simulator
106        int index = 0;
107        exitSimLoop("target called exit()",
108                    process->getSyscallArg(tc, index) & 0xff);
109    } else {
110        // other running threads... just halt this one
111        tc->halt();
112    }
113
114    return 1;
115}
116
117
118SyscallReturn
119exitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
120              ThreadContext *tc)
121{
122    // really should just halt all thread contexts belonging to this
123    // process in case there's another process running...
124    int index = 0;
125    exitSimLoop("target called exit()",
126                process->getSyscallArg(tc, index) & 0xff);
127
128    return 1;
129}
130
131
132SyscallReturn
133getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
134{
135    return (int)VMPageSize;
136}
137
138
139SyscallReturn
140brkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
141{
142    // change brk addr to first arg
143    int index = 0;
144    Addr new_brk = p->getSyscallArg(tc, index);
145
146    // in Linux at least, brk(0) returns the current break value
147    // (note that the syscall and the glibc function have different behavior)
148    if (new_brk == 0)
149        return p->brk_point;
150
151    if (new_brk > p->brk_point) {
152        // might need to allocate some new pages
153        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
154                                VMPageSize); !gen.done(); gen.next()) {
155            if (!p->pTable->translate(gen.addr()))
156                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
157                                    VMPageSize);
158
159            // if the address is already there, zero it out
160            else {
161                uint8_t zero  = 0;
162                TranslatingPort *tp = tc->getMemPort();
163
164                // split non-page aligned accesses
165                Addr next_page = roundUp(gen.addr(), VMPageSize);
166                uint32_t size_needed = next_page - gen.addr();
167                tp->memsetBlob(gen.addr(), zero, size_needed);
168                if (gen.addr() + VMPageSize > next_page &&
169                    next_page < new_brk &&
170                    p->pTable->translate(next_page))
171                {
172                    size_needed = VMPageSize - size_needed;
173                    tp->memsetBlob(next_page, zero, size_needed);
174                }
175            }
176        }
177    }
178
179    p->brk_point = new_brk;
180    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
181    return p->brk_point;
182}
183
184
185SyscallReturn
186closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
187{
188    int index = 0;
189    int target_fd = p->getSyscallArg(tc, index);
190    int sim_fd = p->sim_fd(target_fd);
191    int status = 0;
192    if (sim_fd > 2)
193        status = close(sim_fd);
194    if (status >= 0)
195        p->free_fd(target_fd);
196    return status;
197}
198
199
200SyscallReturn
201readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
202{
203    int index = 0;
204    int fd = p->sim_fd(p->getSyscallArg(tc, index));
205    Addr bufPtr = p->getSyscallArg(tc, index);
206    int nbytes = p->getSyscallArg(tc, index);
207    BufferArg bufArg(bufPtr, nbytes);
208
209    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
210
211    if (bytes_read != -1)
212        bufArg.copyOut(tc->getMemPort());
213
214    return bytes_read;
215}
216
217SyscallReturn
218writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
219{
220    int index = 0;
221    int fd = p->sim_fd(p->getSyscallArg(tc, index));
222    Addr bufPtr = p->getSyscallArg(tc, index);
223    int nbytes = p->getSyscallArg(tc, index);
224    BufferArg bufArg(bufPtr, nbytes);
225
226    bufArg.copyIn(tc->getMemPort());
227
228    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
229
230    fsync(fd);
231
232    return bytes_written;
233}
234
235
236SyscallReturn
237lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
238{
239    int index = 0;
240    int fd = p->sim_fd(p->getSyscallArg(tc, index));
241    uint64_t offs = p->getSyscallArg(tc, index);
242    int whence = p->getSyscallArg(tc, index);
243
244    off_t result = lseek(fd, offs, whence);
245
246    return (result == (off_t)-1) ? -errno : result;
247}
248
249
250SyscallReturn
251_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
252{
253    int index = 0;
254    int fd = p->sim_fd(p->getSyscallArg(tc, index));
255    uint64_t offset_high = p->getSyscallArg(tc, index);
256    uint32_t offset_low = p->getSyscallArg(tc, index);
257    Addr result_ptr = p->getSyscallArg(tc, index);
258    int whence = p->getSyscallArg(tc, index);
259
260    uint64_t offset = (offset_high << 32) | offset_low;
261
262    uint64_t result = lseek(fd, offset, whence);
263    result = TheISA::htog(result);
264
265    if (result == (off_t)-1) {
266        //The seek failed.
267        return -errno;
268    } else {
269        // The seek succeeded.
270        // Copy "result" to "result_ptr"
271        // XXX We'll assume that the size of loff_t is 64 bits on the
272        // target platform
273        BufferArg result_buf(result_ptr, sizeof(result));
274        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
275        result_buf.copyOut(tc->getMemPort());
276        return 0;
277    }
278
279
280    return (result == (off_t)-1) ? -errno : result;
281}
282
283
284SyscallReturn
285munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
286{
287    // given that we don't really implement mmap, munmap is really easy
288    return 0;
289}
290
291
292const char *hostname = "m5.eecs.umich.edu";
293
294SyscallReturn
295gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
296{
297    int index = 0;
298    Addr bufPtr = p->getSyscallArg(tc, index);
299    int name_len = p->getSyscallArg(tc, index);
300    BufferArg name(bufPtr, name_len);
301
302    strncpy((char *)name.bufferPtr(), hostname, name_len);
303
304    name.copyOut(tc->getMemPort());
305
306    return 0;
307}
308
309SyscallReturn
310getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
311{
312    int result = 0;
313    int index = 0;
314    Addr bufPtr = p->getSyscallArg(tc, index);
315    unsigned long size = p->getSyscallArg(tc, index);
316    BufferArg buf(bufPtr, size);
317
318    // Is current working directory defined?
319    string cwd = p->getcwd();
320    if (!cwd.empty()) {
321        if (cwd.length() >= size) {
322            // Buffer too small
323            return -ERANGE;
324        }
325        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
326        result = cwd.length();
327    }
328    else {
329        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
330            result = strlen((char *)buf.bufferPtr());
331        }
332        else {
333            result = -1;
334        }
335    }
336
337    buf.copyOut(tc->getMemPort());
338
339    return (result == -1) ? -errno : result;
340}
341
342
343SyscallReturn
344readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
345{
346    string path;
347
348    int index = 0;
349    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
350        return (TheISA::IntReg)-EFAULT;
351
352    // Adjust path for current working directory
353    path = p->fullPath(path);
354
355    Addr bufPtr = p->getSyscallArg(tc, index);
356    size_t bufsiz = p->getSyscallArg(tc, index);
357
358    BufferArg buf(bufPtr, bufsiz);
359
360    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
361
362    buf.copyOut(tc->getMemPort());
363
364    return (result == -1) ? -errno : result;
365}
366
367SyscallReturn
368unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
369{
370    string path;
371
372    int index = 0;
373    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
374        return (TheISA::IntReg)-EFAULT;
375
376    // Adjust path for current working directory
377    path = p->fullPath(path);
378
379    int result = unlink(path.c_str());
380    return (result == -1) ? -errno : result;
381}
382
383
384SyscallReturn
385mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
386{
387    string path;
388
389    int index = 0;
390    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
391        return (TheISA::IntReg)-EFAULT;
392
393    // Adjust path for current working directory
394    path = p->fullPath(path);
395
396    mode_t mode = p->getSyscallArg(tc, index);
397
398    int result = mkdir(path.c_str(), mode);
399    return (result == -1) ? -errno : result;
400}
401
402SyscallReturn
403renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
404{
405    string old_name;
406
407    int index = 0;
408    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index)))
409        return -EFAULT;
410
411    string new_name;
412
413    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index)))
414        return -EFAULT;
415
416    // Adjust path for current working directory
417    old_name = p->fullPath(old_name);
418    new_name = p->fullPath(new_name);
419
420    int64_t result = rename(old_name.c_str(), new_name.c_str());
421    return (result == -1) ? -errno : result;
422}
423
424SyscallReturn
425truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
426{
427    string path;
428
429    int index = 0;
430    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
431        return -EFAULT;
432
433    off_t length = p->getSyscallArg(tc, index);
434
435    // Adjust path for current working directory
436    path = p->fullPath(path);
437
438    int result = truncate(path.c_str(), length);
439    return (result == -1) ? -errno : result;
440}
441
442SyscallReturn
443ftruncateFunc(SyscallDesc *desc, int num,
444              LiveProcess *process, ThreadContext *tc)
445{
446    int index = 0;
447    int fd = process->sim_fd(process->getSyscallArg(tc, index));
448
449    if (fd < 0)
450        return -EBADF;
451
452    off_t length = process->getSyscallArg(tc, index);
453
454    int result = ftruncate(fd, length);
455    return (result == -1) ? -errno : result;
456}
457
458SyscallReturn
459truncate64Func(SyscallDesc *desc, int num,
460                LiveProcess *process, ThreadContext *tc)
461{
462    int index = 0;
463    string path;
464
465    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index)))
466       return -EFAULT;
467
468    int64_t length = process->getSyscallArg(tc, index, 64);
469
470    // Adjust path for current working directory
471    path = process->fullPath(path);
472
473#if NO_STAT64
474    int result = truncate(path.c_str(), length);
475#else
476    int result = truncate64(path.c_str(), length);
477#endif
478    return (result == -1) ? -errno : result;
479}
480
481SyscallReturn
482ftruncate64Func(SyscallDesc *desc, int num,
483                LiveProcess *process, ThreadContext *tc)
484{
485    int index = 0;
486    int fd = process->sim_fd(process->getSyscallArg(tc, index));
487
488    if (fd < 0)
489        return -EBADF;
490
491    int64_t length = process->getSyscallArg(tc, index, 64);
492
493#if NO_STAT64
494    int result = ftruncate(fd, length);
495#else
496    int result = ftruncate64(fd, length);
497#endif
498    return (result == -1) ? -errno : result;
499}
500
501SyscallReturn
502umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
503{
504    // Letting the simulated program change the simulator's umask seems like
505    // a bad idea.  Compromise by just returning the current umask but not
506    // changing anything.
507    mode_t oldMask = umask(0);
508    umask(oldMask);
509    return (int)oldMask;
510}
511
512SyscallReturn
513chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
514{
515    string path;
516
517    int index = 0;
518    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
519        return -EFAULT;
520
521    /* XXX endianess */
522    uint32_t owner = p->getSyscallArg(tc, index);
523    uid_t hostOwner = owner;
524    uint32_t group = p->getSyscallArg(tc, index);
525    gid_t hostGroup = group;
526
527    // Adjust path for current working directory
528    path = p->fullPath(path);
529
530    int result = chown(path.c_str(), hostOwner, hostGroup);
531    return (result == -1) ? -errno : result;
532}
533
534SyscallReturn
535fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
536{
537    int index = 0;
538    int fd = process->sim_fd(process->getSyscallArg(tc, index));
539
540    if (fd < 0)
541        return -EBADF;
542
543    /* XXX endianess */
544    uint32_t owner = process->getSyscallArg(tc, index);
545    uid_t hostOwner = owner;
546    uint32_t group = process->getSyscallArg(tc, index);
547    gid_t hostGroup = group;
548
549    int result = fchown(fd, hostOwner, hostGroup);
550    return (result == -1) ? -errno : result;
551}
552
553
554SyscallReturn
555dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
556{
557    int index = 0;
558    int fd = process->sim_fd(process->getSyscallArg(tc, index));
559    if (fd < 0)
560        return -EBADF;
561
562    Process::FdMap *fdo = process->sim_fd_obj(fd);
563
564    int result = dup(fd);
565    return (result == -1) ? -errno :
566        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
567}
568
569
570SyscallReturn
571fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
572          ThreadContext *tc)
573{
574    int index = 0;
575    int fd = process->getSyscallArg(tc, index);
576
577    if (fd < 0 || process->sim_fd(fd) < 0)
578        return -EBADF;
579
580    int cmd = process->getSyscallArg(tc, index);
581    switch (cmd) {
582      case 0: // F_DUPFD
583        // if we really wanted to support this, we'd need to do it
584        // in the target fd space.
585        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
586        return -EMFILE;
587
588      case 1: // F_GETFD (get close-on-exec flag)
589      case 2: // F_SETFD (set close-on-exec flag)
590        return 0;
591
592      case 3: // F_GETFL (get file flags)
593      case 4: // F_SETFL (set file flags)
594        // not sure if this is totally valid, but we'll pass it through
595        // to the underlying OS
596        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
597        return fcntl(process->sim_fd(fd), cmd);
598        // return 0;
599
600      case 7: // F_GETLK  (get lock)
601      case 8: // F_SETLK  (set lock)
602      case 9: // F_SETLKW (set lock and wait)
603        // don't mess with file locking... just act like it's OK
604        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
605        return 0;
606
607      default:
608        warn("Unknown fcntl command %d\n", cmd);
609        return 0;
610    }
611}
612
613SyscallReturn
614fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
615            ThreadContext *tc)
616{
617    int index = 0;
618    int fd = process->getSyscallArg(tc, index);
619
620    if (fd < 0 || process->sim_fd(fd) < 0)
621        return -EBADF;
622
623    int cmd = process->getSyscallArg(tc, index);
624    switch (cmd) {
625      case 33: //F_GETLK64
626        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
627        return -EMFILE;
628
629      case 34: // F_SETLK64
630      case 35: // F_SETLKW64
631        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
632        return -EMFILE;
633
634      default:
635        // not sure if this is totally valid, but we'll pass it through
636        // to the underlying OS
637        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
638        return fcntl(process->sim_fd(fd), cmd);
639        // return 0;
640    }
641}
642
643SyscallReturn
644pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
645         ThreadContext *tc)
646{
647    int fds[2], sim_fds[2];
648    int pipe_retval = pipe(fds);
649
650    if (pipe_retval < 0) {
651        // error
652        return pipe_retval;
653    }
654
655    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
656    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
657
658    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
659    // Alpha Linux convention for pipe() is that fd[0] is returned as
660    // the return value of the function, and fd[1] is returned in r20.
661    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
662    return sim_fds[0];
663}
664
665
666SyscallReturn
667getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
668           ThreadContext *tc)
669{
670    // Make up a PID.  There's no interprocess communication in
671    // fake_syscall mode, so there's no way for a process to know it's
672    // not getting a unique value.
673
674    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
675    return process->pid();
676}
677
678
679SyscallReturn
680getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
681           ThreadContext *tc)
682{
683    // Make up a UID and EUID... it shouldn't matter, and we want the
684    // simulation to be deterministic.
685
686    // EUID goes in r20.
687    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
688    return process->uid();              // UID
689}
690
691
692SyscallReturn
693getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
694           ThreadContext *tc)
695{
696    // Get current group ID.  EGID goes in r20.
697    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
698    return process->gid();
699}
700
701
702SyscallReturn
703setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
704           ThreadContext *tc)
705{
706    // can't fathom why a benchmark would call this.
707    int index = 0;
708    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
709    return 0;
710}
711
712SyscallReturn
713getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
714           ThreadContext *tc)
715{
716    // Make up a PID.  There's no interprocess communication in
717    // fake_syscall mode, so there's no way for a process to know it's
718    // not getting a unique value.
719
720    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
721    return process->pid();
722}
723
724SyscallReturn
725getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
726           ThreadContext *tc)
727{
728    return process->ppid();
729}
730
731SyscallReturn
732getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
733           ThreadContext *tc)
734{
735    return process->uid();              // UID
736}
737
738SyscallReturn
739geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
740           ThreadContext *tc)
741{
742    return process->euid();             // UID
743}
744
745SyscallReturn
746getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
747           ThreadContext *tc)
748{
749    return process->gid();
750}
751
752SyscallReturn
753getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
754           ThreadContext *tc)
755{
756    return process->egid();
757}
758
759
760SyscallReturn
761cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
762           ThreadContext *tc)
763{
764    int index = 0;
765    IntReg flags = process->getSyscallArg(tc, index);
766    IntReg newStack = process->getSyscallArg(tc, index);
767
768    DPRINTF(SyscallVerbose, "In sys_clone:\n");
769    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
770    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
771
772
773    if (flags != 0x10f00) {
774        warn("This sys_clone implementation assumes flags "
775             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
776             "(0x10f00), and may not work correctly with given flags "
777             "0x%llx\n", flags);
778    }
779
780    ThreadContext* ctc; // child thread context
781    if ( ( ctc = process->findFreeContext() ) != NULL ) {
782        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
783
784        ctc->clearArchRegs();
785
786        // Arch-specific cloning code
787        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
788            // Cloning the misc. regs for these archs is enough
789            TheISA::copyMiscRegs(tc, ctc);
790        #elif THE_ISA == SPARC_ISA
791            TheISA::copyRegs(tc, ctc);
792
793            // TODO: Explain what this code actually does :-)
794            ctc->setIntReg(NumIntArchRegs + 6, 0);
795            ctc->setIntReg(NumIntArchRegs + 4, 0);
796            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
797            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
798            ctc->setMiscReg(MISCREG_CWP, 0);
799            ctc->setIntReg(NumIntArchRegs + 7, 0);
800            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
801            ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
802
803            for (int y = 8; y < 32; y++)
804                ctc->setIntReg(y, tc->readIntReg(y));
805        #else
806            fatal("sys_clone is not implemented for this ISA\n");
807        #endif
808
809        // Set up stack register
810        ctc->setIntReg(TheISA::StackPointerReg, newStack);
811
812        // Set up syscall return values in parent and child
813        ctc->setIntReg(ReturnValueReg, 0); // return value, child
814
815        // Alpha needs SyscallSuccessReg=0 in child
816        #if THE_ISA == ALPHA_ISA
817            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
818        #endif
819
820        // In SPARC/Linux, clone returns 0 on pseudo-return register if
821        // parent, non-zero if child
822        #if THE_ISA == SPARC_ISA
823            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
824            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
825        #endif
826
827        ctc->pcState(tc->nextInstAddr());
828
829        ctc->activate();
830
831        // Should return nonzero child TID in parent's syscall return register,
832        // but for our pthread library any non-zero value will work
833        return 1;
834    } else {
835        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
836        return 0;
837    }
838}
839
840