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