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