syscall_emul.cc (10831:fbdaa08aaa42) syscall_emul.cc (10929:b2bbfec74eca)
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 target_fd = p->getSyscallArg(tc, index);
214 int sim_fd = p->sim_fd(target_fd);
215 int status = 0;
216 if (sim_fd > 2)
217 status = close(sim_fd);
218 if (status >= 0)
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 target_fd = p->getSyscallArg(tc, index);
214 int sim_fd = p->sim_fd(target_fd);
215 int status = 0;
216 if (sim_fd > 2)
217 status = close(sim_fd);
218 if (status >= 0)
219 p->free_fd(target_fd);
219 p->free_fdmap_entry(target_fd);
220 return status;
221}
222
223
224SyscallReturn
225readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
226{
227 int index = 0;
228 int fd = p->sim_fd(p->getSyscallArg(tc, index));
229 assert(fd >= 0);
230 Addr bufPtr = p->getSyscallArg(tc, index);
231 int nbytes = p->getSyscallArg(tc, index);
232 BufferArg bufArg(bufPtr, nbytes);
233
234 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
235
236 if (bytes_read != -1)
237 bufArg.copyOut(tc->getMemProxy());
238
239 return bytes_read;
240}
241
242SyscallReturn
243writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
244{
245 int index = 0;
246 int fd = p->sim_fd(p->getSyscallArg(tc, index));
247 Addr bufPtr = p->getSyscallArg(tc, index);
248 int nbytes = p->getSyscallArg(tc, index);
249 BufferArg bufArg(bufPtr, nbytes);
250
251 bufArg.copyIn(tc->getMemProxy());
252
253 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
254
255 fsync(fd);
256
257 return bytes_written;
258}
259
260
261SyscallReturn
262lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
263{
264 int index = 0;
265 int fd = p->sim_fd(p->getSyscallArg(tc, index));
266 assert(fd >= 0);
267 uint64_t offs = p->getSyscallArg(tc, index);
268 int whence = p->getSyscallArg(tc, index);
269
270 off_t result = lseek(fd, offs, whence);
271
272 return (result == (off_t)-1) ? -errno : result;
273}
274
275
276SyscallReturn
277_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
278{
279 int index = 0;
280 int fd = p->sim_fd(p->getSyscallArg(tc, index));
281 assert(fd >= 0);
282 uint64_t offset_high = p->getSyscallArg(tc, index);
283 uint32_t offset_low = p->getSyscallArg(tc, index);
284 Addr result_ptr = p->getSyscallArg(tc, index);
285 int whence = p->getSyscallArg(tc, index);
286
287 uint64_t offset = (offset_high << 32) | offset_low;
288
289 uint64_t result = lseek(fd, offset, whence);
290 result = TheISA::htog(result);
291
292 if (result == (off_t)-1) {
293 //The seek failed.
294 return -errno;
295 } else {
296 // The seek succeeded.
297 // Copy "result" to "result_ptr"
298 // XXX We'll assume that the size of loff_t is 64 bits on the
299 // target platform
300 BufferArg result_buf(result_ptr, sizeof(result));
301 memcpy(result_buf.bufferPtr(), &result, sizeof(result));
302 result_buf.copyOut(tc->getMemProxy());
303 return 0;
304 }
305}
306
307
308SyscallReturn
309munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
310{
311 // given that we don't really implement mmap, munmap is really easy
312 return 0;
313}
314
315
316const char *hostname = "m5.eecs.umich.edu";
317
318SyscallReturn
319gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
320{
321 int index = 0;
322 Addr bufPtr = p->getSyscallArg(tc, index);
323 int name_len = p->getSyscallArg(tc, index);
324 BufferArg name(bufPtr, name_len);
325
326 strncpy((char *)name.bufferPtr(), hostname, name_len);
327
328 name.copyOut(tc->getMemProxy());
329
330 return 0;
331}
332
333SyscallReturn
334getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
335{
336 int result = 0;
337 int index = 0;
338 Addr bufPtr = p->getSyscallArg(tc, index);
339 unsigned long size = p->getSyscallArg(tc, index);
340 BufferArg buf(bufPtr, size);
341
342 // Is current working directory defined?
343 string cwd = p->getcwd();
344 if (!cwd.empty()) {
345 if (cwd.length() >= size) {
346 // Buffer too small
347 return -ERANGE;
348 }
349 strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
350 result = cwd.length();
351 }
352 else {
353 if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
354 result = strlen((char *)buf.bufferPtr());
355 }
356 else {
357 result = -1;
358 }
359 }
360
361 buf.copyOut(tc->getMemProxy());
362
363 return (result == -1) ? -errno : result;
364}
365
366/// Target open() handler.
367SyscallReturn
368readlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
369 ThreadContext *tc)
370{
371 return readlinkFunc(desc, callnum, process, tc, 0);
372}
373
374SyscallReturn
375readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
376 int index)
377{
378 string path;
379
380 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
381 return -EFAULT;
382
383 // Adjust path for current working directory
384 path = p->fullPath(path);
385
386 Addr bufPtr = p->getSyscallArg(tc, index);
387 size_t bufsiz = p->getSyscallArg(tc, index);
388
389 BufferArg buf(bufPtr, bufsiz);
390
391 int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
392
393 buf.copyOut(tc->getMemProxy());
394
395 return (result == -1) ? -errno : result;
396}
397
398SyscallReturn
399unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
400{
401 return unlinkHelper(desc, num, p, tc, 0);
402}
403
404SyscallReturn
405unlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
406 int index)
407{
408 string path;
409
410 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
411 return -EFAULT;
412
413 // Adjust path for current working directory
414 path = p->fullPath(path);
415
416 int result = unlink(path.c_str());
417 return (result == -1) ? -errno : result;
418}
419
420
421SyscallReturn
422mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
423{
424 string path;
425
426 int index = 0;
427 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
428 return -EFAULT;
429
430 // Adjust path for current working directory
431 path = p->fullPath(path);
432
433 mode_t mode = p->getSyscallArg(tc, index);
434
435 int result = mkdir(path.c_str(), mode);
436 return (result == -1) ? -errno : result;
437}
438
439SyscallReturn
440renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
441{
442 string old_name;
443
444 int index = 0;
445 if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
446 return -EFAULT;
447
448 string new_name;
449
450 if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
451 return -EFAULT;
452
453 // Adjust path for current working directory
454 old_name = p->fullPath(old_name);
455 new_name = p->fullPath(new_name);
456
457 int64_t result = rename(old_name.c_str(), new_name.c_str());
458 return (result == -1) ? -errno : result;
459}
460
461SyscallReturn
462truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
463{
464 string path;
465
466 int index = 0;
467 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
468 return -EFAULT;
469
470 off_t length = p->getSyscallArg(tc, index);
471
472 // Adjust path for current working directory
473 path = p->fullPath(path);
474
475 int result = truncate(path.c_str(), length);
476 return (result == -1) ? -errno : result;
477}
478
479SyscallReturn
480ftruncateFunc(SyscallDesc *desc, int num,
481 LiveProcess *process, ThreadContext *tc)
482{
483 int index = 0;
484 int fd = process->sim_fd(process->getSyscallArg(tc, index));
485
486 if (fd < 0)
487 return -EBADF;
488
489 off_t length = process->getSyscallArg(tc, index);
490
491 int result = ftruncate(fd, length);
492 return (result == -1) ? -errno : result;
493}
494
495SyscallReturn
496truncate64Func(SyscallDesc *desc, int num,
497 LiveProcess *process, ThreadContext *tc)
498{
499 int index = 0;
500 string path;
501
502 if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
503 return -EFAULT;
504
505 int64_t length = process->getSyscallArg(tc, index, 64);
506
507 // Adjust path for current working directory
508 path = process->fullPath(path);
509
510#if NO_STAT64
511 int result = truncate(path.c_str(), length);
512#else
513 int result = truncate64(path.c_str(), length);
514#endif
515 return (result == -1) ? -errno : result;
516}
517
518SyscallReturn
519ftruncate64Func(SyscallDesc *desc, int num,
520 LiveProcess *process, ThreadContext *tc)
521{
522 int index = 0;
523 int fd = process->sim_fd(process->getSyscallArg(tc, index));
524
525 if (fd < 0)
526 return -EBADF;
527
528 int64_t length = process->getSyscallArg(tc, index, 64);
529
530#if NO_STAT64
531 int result = ftruncate(fd, length);
532#else
533 int result = ftruncate64(fd, length);
534#endif
535 return (result == -1) ? -errno : result;
536}
537
538SyscallReturn
539umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
540{
541 // Letting the simulated program change the simulator's umask seems like
542 // a bad idea. Compromise by just returning the current umask but not
543 // changing anything.
544 mode_t oldMask = umask(0);
545 umask(oldMask);
546 return (int)oldMask;
547}
548
549SyscallReturn
550chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
551{
552 string path;
553
554 int index = 0;
555 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
556 return -EFAULT;
557
558 /* XXX endianess */
559 uint32_t owner = p->getSyscallArg(tc, index);
560 uid_t hostOwner = owner;
561 uint32_t group = p->getSyscallArg(tc, index);
562 gid_t hostGroup = group;
563
564 // Adjust path for current working directory
565 path = p->fullPath(path);
566
567 int result = chown(path.c_str(), hostOwner, hostGroup);
568 return (result == -1) ? -errno : result;
569}
570
571SyscallReturn
572fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
573{
574 int index = 0;
575 int fd = process->sim_fd(process->getSyscallArg(tc, index));
576
577 if (fd < 0)
578 return -EBADF;
579
580 /* XXX endianess */
581 uint32_t owner = process->getSyscallArg(tc, index);
582 uid_t hostOwner = owner;
583 uint32_t group = process->getSyscallArg(tc, index);
584 gid_t hostGroup = group;
585
586 int result = fchown(fd, hostOwner, hostGroup);
587 return (result == -1) ? -errno : result;
588}
589
590
591SyscallReturn
592dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
593{
594 int index = 0;
595 int tgt_fd = process->getSyscallArg(tc, index);
596 int sim_fd = process->sim_fd(tgt_fd);
597 if (sim_fd < 0)
598 return -EBADF;
599
600 Process::FdMap *fdo = process->sim_fd_obj(tgt_fd);
601
602 int result = dup(sim_fd);
603 return (result == -1) ? -errno :
604 process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
605}
606
607
608SyscallReturn
609fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
610 ThreadContext *tc)
611{
612 int index = 0;
613 int fd = process->getSyscallArg(tc, index);
614
615 if (fd < 0 || process->sim_fd(fd) < 0)
616 return -EBADF;
617
618 int cmd = process->getSyscallArg(tc, index);
619 switch (cmd) {
620 case 0: // F_DUPFD
621 // if we really wanted to support this, we'd need to do it
622 // in the target fd space.
623 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
624 return -EMFILE;
625
626 case 1: // F_GETFD (get close-on-exec flag)
627 case 2: // F_SETFD (set close-on-exec flag)
628 return 0;
629
630 case 3: // F_GETFL (get file flags)
631 case 4: // F_SETFL (set file flags)
632 // not sure if this is totally valid, but we'll pass it through
633 // to the underlying OS
634 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
635 return fcntl(process->sim_fd(fd), cmd);
636 // return 0;
637
638 case 7: // F_GETLK (get lock)
639 case 8: // F_SETLK (set lock)
640 case 9: // F_SETLKW (set lock and wait)
641 // don't mess with file locking... just act like it's OK
642 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
643 return 0;
644
645 default:
646 warn("Unknown fcntl command %d\n", cmd);
647 return 0;
648 }
649}
650
651SyscallReturn
652fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
653 ThreadContext *tc)
654{
655 int index = 0;
656 int fd = process->getSyscallArg(tc, index);
657
658 if (fd < 0 || process->sim_fd(fd) < 0)
659 return -EBADF;
660
661 int cmd = process->getSyscallArg(tc, index);
662 switch (cmd) {
663 case 33: //F_GETLK64
664 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
665 return -EMFILE;
666
667 case 34: // F_SETLK64
668 case 35: // F_SETLKW64
669 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
670 return -EMFILE;
671
672 default:
673 // not sure if this is totally valid, but we'll pass it through
674 // to the underlying OS
675 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
676 return fcntl(process->sim_fd(fd), cmd);
677 // return 0;
678 }
679}
680
681SyscallReturn
682pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
683 ThreadContext *tc)
684{
685 int fds[2], sim_fds[2];
686 int pipe_retval = pipe(fds);
687
688 if (pipe_retval < 0) {
689 // error
690 return pipe_retval;
691 }
692
693 sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
694 sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
695
696 process->setReadPipeSource(sim_fds[0], sim_fds[1]);
697 // Alpha Linux convention for pipe() is that fd[0] is returned as
698 // the return value of the function, and fd[1] is returned in r20.
699 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
700 return sim_fds[0];
701}
702
703
704SyscallReturn
705getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
706 ThreadContext *tc)
707{
708 // Make up a PID. There's no interprocess communication in
709 // fake_syscall mode, so there's no way for a process to know it's
710 // not getting a unique value.
711
712 tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
713 return process->pid();
714}
715
716
717SyscallReturn
718getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
719 ThreadContext *tc)
720{
721 // Make up a UID and EUID... it shouldn't matter, and we want the
722 // simulation to be deterministic.
723
724 // EUID goes in r20.
725 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
726 return process->uid(); // UID
727}
728
729
730SyscallReturn
731getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
732 ThreadContext *tc)
733{
734 // Get current group ID. EGID goes in r20.
735 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
736 return process->gid();
737}
738
739
740SyscallReturn
741setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
742 ThreadContext *tc)
743{
744 // can't fathom why a benchmark would call this.
745 int index = 0;
746 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
747 return 0;
748}
749
750SyscallReturn
751getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
752 ThreadContext *tc)
753{
754 // Make up a PID. There's no interprocess communication in
755 // fake_syscall mode, so there's no way for a process to know it's
756 // not getting a unique value.
757
758 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
759 return process->pid();
760}
761
762SyscallReturn
763getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
764 ThreadContext *tc)
765{
766 return process->ppid();
767}
768
769SyscallReturn
770getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
771 ThreadContext *tc)
772{
773 return process->uid(); // UID
774}
775
776SyscallReturn
777geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
778 ThreadContext *tc)
779{
780 return process->euid(); // UID
781}
782
783SyscallReturn
784getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
785 ThreadContext *tc)
786{
787 return process->gid();
788}
789
790SyscallReturn
791getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
792 ThreadContext *tc)
793{
794 return process->egid();
795}
796
797
798SyscallReturn
799cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
800 ThreadContext *tc)
801{
802 int index = 0;
803 IntReg flags = process->getSyscallArg(tc, index);
804 IntReg newStack = process->getSyscallArg(tc, index);
805
806 DPRINTF(SyscallVerbose, "In sys_clone:\n");
807 DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
808 DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
809
810
811 if (flags != 0x10f00) {
812 warn("This sys_clone implementation assumes flags "
813 "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
814 "(0x10f00), and may not work correctly with given flags "
815 "0x%llx\n", flags);
816 }
817
818 ThreadContext* ctc; // child thread context
819 if ( ( ctc = process->findFreeContext() ) != NULL ) {
820 DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
821
822 ctc->clearArchRegs();
823
824 // Arch-specific cloning code
825 #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
826 // Cloning the misc. regs for these archs is enough
827 TheISA::copyMiscRegs(tc, ctc);
828 #elif THE_ISA == SPARC_ISA
829 TheISA::copyRegs(tc, ctc);
830
831 // TODO: Explain what this code actually does :-)
832 ctc->setIntReg(NumIntArchRegs + 6, 0);
833 ctc->setIntReg(NumIntArchRegs + 4, 0);
834 ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
835 ctc->setIntReg(NumIntArchRegs + 5, NWindows);
836 ctc->setMiscReg(MISCREG_CWP, 0);
837 ctc->setIntReg(NumIntArchRegs + 7, 0);
838 ctc->setMiscRegNoEffect(MISCREG_TL, 0);
839 ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
840
841 for (int y = 8; y < 32; y++)
842 ctc->setIntReg(y, tc->readIntReg(y));
843 #elif THE_ISA == ARM_ISA
844 TheISA::copyRegs(tc, ctc);
845 #else
846 fatal("sys_clone is not implemented for this ISA\n");
847 #endif
848
849 // Set up stack register
850 ctc->setIntReg(TheISA::StackPointerReg, newStack);
851
852 // Set up syscall return values in parent and child
853 ctc->setIntReg(ReturnValueReg, 0); // return value, child
854
855 // Alpha needs SyscallSuccessReg=0 in child
856 #if THE_ISA == ALPHA_ISA
857 ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
858 #endif
859
860 // In SPARC/Linux, clone returns 0 on pseudo-return register if
861 // parent, non-zero if child
862 #if THE_ISA == SPARC_ISA
863 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
864 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
865 #endif
866
867 ctc->pcState(tc->nextInstAddr());
868
869 ctc->activate();
870
871 // Should return nonzero child TID in parent's syscall return register,
872 // but for our pthread library any non-zero value will work
873 return 1;
874 } else {
875 fatal("Called sys_clone, but no unallocated thread contexts found!\n");
876 return 0;
877 }
878}
879
880SyscallReturn
881accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
882 int index)
883{
884 string path;
885 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
886 return -EFAULT;
887
888 // Adjust path for current working directory
889 path = p->fullPath(path);
890
891 mode_t mode = p->getSyscallArg(tc, index);
892
893 int result = access(path.c_str(), mode);
894 return (result == -1) ? -errno : result;
895}
896
897SyscallReturn
898accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
899{
900 return accessFunc(desc, callnum, p, tc, 0);
901}
902
220 return status;
221}
222
223
224SyscallReturn
225readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
226{
227 int index = 0;
228 int fd = p->sim_fd(p->getSyscallArg(tc, index));
229 assert(fd >= 0);
230 Addr bufPtr = p->getSyscallArg(tc, index);
231 int nbytes = p->getSyscallArg(tc, index);
232 BufferArg bufArg(bufPtr, nbytes);
233
234 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
235
236 if (bytes_read != -1)
237 bufArg.copyOut(tc->getMemProxy());
238
239 return bytes_read;
240}
241
242SyscallReturn
243writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
244{
245 int index = 0;
246 int fd = p->sim_fd(p->getSyscallArg(tc, index));
247 Addr bufPtr = p->getSyscallArg(tc, index);
248 int nbytes = p->getSyscallArg(tc, index);
249 BufferArg bufArg(bufPtr, nbytes);
250
251 bufArg.copyIn(tc->getMemProxy());
252
253 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
254
255 fsync(fd);
256
257 return bytes_written;
258}
259
260
261SyscallReturn
262lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
263{
264 int index = 0;
265 int fd = p->sim_fd(p->getSyscallArg(tc, index));
266 assert(fd >= 0);
267 uint64_t offs = p->getSyscallArg(tc, index);
268 int whence = p->getSyscallArg(tc, index);
269
270 off_t result = lseek(fd, offs, whence);
271
272 return (result == (off_t)-1) ? -errno : result;
273}
274
275
276SyscallReturn
277_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
278{
279 int index = 0;
280 int fd = p->sim_fd(p->getSyscallArg(tc, index));
281 assert(fd >= 0);
282 uint64_t offset_high = p->getSyscallArg(tc, index);
283 uint32_t offset_low = p->getSyscallArg(tc, index);
284 Addr result_ptr = p->getSyscallArg(tc, index);
285 int whence = p->getSyscallArg(tc, index);
286
287 uint64_t offset = (offset_high << 32) | offset_low;
288
289 uint64_t result = lseek(fd, offset, whence);
290 result = TheISA::htog(result);
291
292 if (result == (off_t)-1) {
293 //The seek failed.
294 return -errno;
295 } else {
296 // The seek succeeded.
297 // Copy "result" to "result_ptr"
298 // XXX We'll assume that the size of loff_t is 64 bits on the
299 // target platform
300 BufferArg result_buf(result_ptr, sizeof(result));
301 memcpy(result_buf.bufferPtr(), &result, sizeof(result));
302 result_buf.copyOut(tc->getMemProxy());
303 return 0;
304 }
305}
306
307
308SyscallReturn
309munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
310{
311 // given that we don't really implement mmap, munmap is really easy
312 return 0;
313}
314
315
316const char *hostname = "m5.eecs.umich.edu";
317
318SyscallReturn
319gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
320{
321 int index = 0;
322 Addr bufPtr = p->getSyscallArg(tc, index);
323 int name_len = p->getSyscallArg(tc, index);
324 BufferArg name(bufPtr, name_len);
325
326 strncpy((char *)name.bufferPtr(), hostname, name_len);
327
328 name.copyOut(tc->getMemProxy());
329
330 return 0;
331}
332
333SyscallReturn
334getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
335{
336 int result = 0;
337 int index = 0;
338 Addr bufPtr = p->getSyscallArg(tc, index);
339 unsigned long size = p->getSyscallArg(tc, index);
340 BufferArg buf(bufPtr, size);
341
342 // Is current working directory defined?
343 string cwd = p->getcwd();
344 if (!cwd.empty()) {
345 if (cwd.length() >= size) {
346 // Buffer too small
347 return -ERANGE;
348 }
349 strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
350 result = cwd.length();
351 }
352 else {
353 if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
354 result = strlen((char *)buf.bufferPtr());
355 }
356 else {
357 result = -1;
358 }
359 }
360
361 buf.copyOut(tc->getMemProxy());
362
363 return (result == -1) ? -errno : result;
364}
365
366/// Target open() handler.
367SyscallReturn
368readlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
369 ThreadContext *tc)
370{
371 return readlinkFunc(desc, callnum, process, tc, 0);
372}
373
374SyscallReturn
375readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
376 int index)
377{
378 string path;
379
380 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
381 return -EFAULT;
382
383 // Adjust path for current working directory
384 path = p->fullPath(path);
385
386 Addr bufPtr = p->getSyscallArg(tc, index);
387 size_t bufsiz = p->getSyscallArg(tc, index);
388
389 BufferArg buf(bufPtr, bufsiz);
390
391 int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
392
393 buf.copyOut(tc->getMemProxy());
394
395 return (result == -1) ? -errno : result;
396}
397
398SyscallReturn
399unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
400{
401 return unlinkHelper(desc, num, p, tc, 0);
402}
403
404SyscallReturn
405unlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
406 int index)
407{
408 string path;
409
410 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
411 return -EFAULT;
412
413 // Adjust path for current working directory
414 path = p->fullPath(path);
415
416 int result = unlink(path.c_str());
417 return (result == -1) ? -errno : result;
418}
419
420
421SyscallReturn
422mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
423{
424 string path;
425
426 int index = 0;
427 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
428 return -EFAULT;
429
430 // Adjust path for current working directory
431 path = p->fullPath(path);
432
433 mode_t mode = p->getSyscallArg(tc, index);
434
435 int result = mkdir(path.c_str(), mode);
436 return (result == -1) ? -errno : result;
437}
438
439SyscallReturn
440renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
441{
442 string old_name;
443
444 int index = 0;
445 if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
446 return -EFAULT;
447
448 string new_name;
449
450 if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
451 return -EFAULT;
452
453 // Adjust path for current working directory
454 old_name = p->fullPath(old_name);
455 new_name = p->fullPath(new_name);
456
457 int64_t result = rename(old_name.c_str(), new_name.c_str());
458 return (result == -1) ? -errno : result;
459}
460
461SyscallReturn
462truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
463{
464 string path;
465
466 int index = 0;
467 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
468 return -EFAULT;
469
470 off_t length = p->getSyscallArg(tc, index);
471
472 // Adjust path for current working directory
473 path = p->fullPath(path);
474
475 int result = truncate(path.c_str(), length);
476 return (result == -1) ? -errno : result;
477}
478
479SyscallReturn
480ftruncateFunc(SyscallDesc *desc, int num,
481 LiveProcess *process, ThreadContext *tc)
482{
483 int index = 0;
484 int fd = process->sim_fd(process->getSyscallArg(tc, index));
485
486 if (fd < 0)
487 return -EBADF;
488
489 off_t length = process->getSyscallArg(tc, index);
490
491 int result = ftruncate(fd, length);
492 return (result == -1) ? -errno : result;
493}
494
495SyscallReturn
496truncate64Func(SyscallDesc *desc, int num,
497 LiveProcess *process, ThreadContext *tc)
498{
499 int index = 0;
500 string path;
501
502 if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
503 return -EFAULT;
504
505 int64_t length = process->getSyscallArg(tc, index, 64);
506
507 // Adjust path for current working directory
508 path = process->fullPath(path);
509
510#if NO_STAT64
511 int result = truncate(path.c_str(), length);
512#else
513 int result = truncate64(path.c_str(), length);
514#endif
515 return (result == -1) ? -errno : result;
516}
517
518SyscallReturn
519ftruncate64Func(SyscallDesc *desc, int num,
520 LiveProcess *process, ThreadContext *tc)
521{
522 int index = 0;
523 int fd = process->sim_fd(process->getSyscallArg(tc, index));
524
525 if (fd < 0)
526 return -EBADF;
527
528 int64_t length = process->getSyscallArg(tc, index, 64);
529
530#if NO_STAT64
531 int result = ftruncate(fd, length);
532#else
533 int result = ftruncate64(fd, length);
534#endif
535 return (result == -1) ? -errno : result;
536}
537
538SyscallReturn
539umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
540{
541 // Letting the simulated program change the simulator's umask seems like
542 // a bad idea. Compromise by just returning the current umask but not
543 // changing anything.
544 mode_t oldMask = umask(0);
545 umask(oldMask);
546 return (int)oldMask;
547}
548
549SyscallReturn
550chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
551{
552 string path;
553
554 int index = 0;
555 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
556 return -EFAULT;
557
558 /* XXX endianess */
559 uint32_t owner = p->getSyscallArg(tc, index);
560 uid_t hostOwner = owner;
561 uint32_t group = p->getSyscallArg(tc, index);
562 gid_t hostGroup = group;
563
564 // Adjust path for current working directory
565 path = p->fullPath(path);
566
567 int result = chown(path.c_str(), hostOwner, hostGroup);
568 return (result == -1) ? -errno : result;
569}
570
571SyscallReturn
572fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
573{
574 int index = 0;
575 int fd = process->sim_fd(process->getSyscallArg(tc, index));
576
577 if (fd < 0)
578 return -EBADF;
579
580 /* XXX endianess */
581 uint32_t owner = process->getSyscallArg(tc, index);
582 uid_t hostOwner = owner;
583 uint32_t group = process->getSyscallArg(tc, index);
584 gid_t hostGroup = group;
585
586 int result = fchown(fd, hostOwner, hostGroup);
587 return (result == -1) ? -errno : result;
588}
589
590
591SyscallReturn
592dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
593{
594 int index = 0;
595 int tgt_fd = process->getSyscallArg(tc, index);
596 int sim_fd = process->sim_fd(tgt_fd);
597 if (sim_fd < 0)
598 return -EBADF;
599
600 Process::FdMap *fdo = process->sim_fd_obj(tgt_fd);
601
602 int result = dup(sim_fd);
603 return (result == -1) ? -errno :
604 process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
605}
606
607
608SyscallReturn
609fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
610 ThreadContext *tc)
611{
612 int index = 0;
613 int fd = process->getSyscallArg(tc, index);
614
615 if (fd < 0 || process->sim_fd(fd) < 0)
616 return -EBADF;
617
618 int cmd = process->getSyscallArg(tc, index);
619 switch (cmd) {
620 case 0: // F_DUPFD
621 // if we really wanted to support this, we'd need to do it
622 // in the target fd space.
623 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
624 return -EMFILE;
625
626 case 1: // F_GETFD (get close-on-exec flag)
627 case 2: // F_SETFD (set close-on-exec flag)
628 return 0;
629
630 case 3: // F_GETFL (get file flags)
631 case 4: // F_SETFL (set file flags)
632 // not sure if this is totally valid, but we'll pass it through
633 // to the underlying OS
634 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
635 return fcntl(process->sim_fd(fd), cmd);
636 // return 0;
637
638 case 7: // F_GETLK (get lock)
639 case 8: // F_SETLK (set lock)
640 case 9: // F_SETLKW (set lock and wait)
641 // don't mess with file locking... just act like it's OK
642 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
643 return 0;
644
645 default:
646 warn("Unknown fcntl command %d\n", cmd);
647 return 0;
648 }
649}
650
651SyscallReturn
652fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
653 ThreadContext *tc)
654{
655 int index = 0;
656 int fd = process->getSyscallArg(tc, index);
657
658 if (fd < 0 || process->sim_fd(fd) < 0)
659 return -EBADF;
660
661 int cmd = process->getSyscallArg(tc, index);
662 switch (cmd) {
663 case 33: //F_GETLK64
664 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
665 return -EMFILE;
666
667 case 34: // F_SETLK64
668 case 35: // F_SETLKW64
669 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
670 return -EMFILE;
671
672 default:
673 // not sure if this is totally valid, but we'll pass it through
674 // to the underlying OS
675 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
676 return fcntl(process->sim_fd(fd), cmd);
677 // return 0;
678 }
679}
680
681SyscallReturn
682pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
683 ThreadContext *tc)
684{
685 int fds[2], sim_fds[2];
686 int pipe_retval = pipe(fds);
687
688 if (pipe_retval < 0) {
689 // error
690 return pipe_retval;
691 }
692
693 sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
694 sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
695
696 process->setReadPipeSource(sim_fds[0], sim_fds[1]);
697 // Alpha Linux convention for pipe() is that fd[0] is returned as
698 // the return value of the function, and fd[1] is returned in r20.
699 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
700 return sim_fds[0];
701}
702
703
704SyscallReturn
705getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
706 ThreadContext *tc)
707{
708 // Make up a PID. There's no interprocess communication in
709 // fake_syscall mode, so there's no way for a process to know it's
710 // not getting a unique value.
711
712 tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
713 return process->pid();
714}
715
716
717SyscallReturn
718getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
719 ThreadContext *tc)
720{
721 // Make up a UID and EUID... it shouldn't matter, and we want the
722 // simulation to be deterministic.
723
724 // EUID goes in r20.
725 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
726 return process->uid(); // UID
727}
728
729
730SyscallReturn
731getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
732 ThreadContext *tc)
733{
734 // Get current group ID. EGID goes in r20.
735 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
736 return process->gid();
737}
738
739
740SyscallReturn
741setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
742 ThreadContext *tc)
743{
744 // can't fathom why a benchmark would call this.
745 int index = 0;
746 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
747 return 0;
748}
749
750SyscallReturn
751getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
752 ThreadContext *tc)
753{
754 // Make up a PID. There's no interprocess communication in
755 // fake_syscall mode, so there's no way for a process to know it's
756 // not getting a unique value.
757
758 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
759 return process->pid();
760}
761
762SyscallReturn
763getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
764 ThreadContext *tc)
765{
766 return process->ppid();
767}
768
769SyscallReturn
770getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
771 ThreadContext *tc)
772{
773 return process->uid(); // UID
774}
775
776SyscallReturn
777geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
778 ThreadContext *tc)
779{
780 return process->euid(); // UID
781}
782
783SyscallReturn
784getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
785 ThreadContext *tc)
786{
787 return process->gid();
788}
789
790SyscallReturn
791getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
792 ThreadContext *tc)
793{
794 return process->egid();
795}
796
797
798SyscallReturn
799cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
800 ThreadContext *tc)
801{
802 int index = 0;
803 IntReg flags = process->getSyscallArg(tc, index);
804 IntReg newStack = process->getSyscallArg(tc, index);
805
806 DPRINTF(SyscallVerbose, "In sys_clone:\n");
807 DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
808 DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
809
810
811 if (flags != 0x10f00) {
812 warn("This sys_clone implementation assumes flags "
813 "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
814 "(0x10f00), and may not work correctly with given flags "
815 "0x%llx\n", flags);
816 }
817
818 ThreadContext* ctc; // child thread context
819 if ( ( ctc = process->findFreeContext() ) != NULL ) {
820 DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
821
822 ctc->clearArchRegs();
823
824 // Arch-specific cloning code
825 #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
826 // Cloning the misc. regs for these archs is enough
827 TheISA::copyMiscRegs(tc, ctc);
828 #elif THE_ISA == SPARC_ISA
829 TheISA::copyRegs(tc, ctc);
830
831 // TODO: Explain what this code actually does :-)
832 ctc->setIntReg(NumIntArchRegs + 6, 0);
833 ctc->setIntReg(NumIntArchRegs + 4, 0);
834 ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
835 ctc->setIntReg(NumIntArchRegs + 5, NWindows);
836 ctc->setMiscReg(MISCREG_CWP, 0);
837 ctc->setIntReg(NumIntArchRegs + 7, 0);
838 ctc->setMiscRegNoEffect(MISCREG_TL, 0);
839 ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
840
841 for (int y = 8; y < 32; y++)
842 ctc->setIntReg(y, tc->readIntReg(y));
843 #elif THE_ISA == ARM_ISA
844 TheISA::copyRegs(tc, ctc);
845 #else
846 fatal("sys_clone is not implemented for this ISA\n");
847 #endif
848
849 // Set up stack register
850 ctc->setIntReg(TheISA::StackPointerReg, newStack);
851
852 // Set up syscall return values in parent and child
853 ctc->setIntReg(ReturnValueReg, 0); // return value, child
854
855 // Alpha needs SyscallSuccessReg=0 in child
856 #if THE_ISA == ALPHA_ISA
857 ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
858 #endif
859
860 // In SPARC/Linux, clone returns 0 on pseudo-return register if
861 // parent, non-zero if child
862 #if THE_ISA == SPARC_ISA
863 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
864 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
865 #endif
866
867 ctc->pcState(tc->nextInstAddr());
868
869 ctc->activate();
870
871 // Should return nonzero child TID in parent's syscall return register,
872 // but for our pthread library any non-zero value will work
873 return 1;
874 } else {
875 fatal("Called sys_clone, but no unallocated thread contexts found!\n");
876 return 0;
877 }
878}
879
880SyscallReturn
881accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
882 int index)
883{
884 string path;
885 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
886 return -EFAULT;
887
888 // Adjust path for current working directory
889 path = p->fullPath(path);
890
891 mode_t mode = p->getSyscallArg(tc, index);
892
893 int result = access(path.c_str(), mode);
894 return (result == -1) ? -errno : result;
895}
896
897SyscallReturn
898accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
899{
900 return accessFunc(desc, callnum, p, tc, 0);
901}
902