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