syscall_emul.cc (10930:ddc3d96d6313) syscall_emul.cc (10931:42d846318962)
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;

--- 196 unchanged lines hidden (view full) ---

205 return p->brk_point;
206}
207
208
209SyscallReturn
210closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
211{
212 int index = 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;

--- 196 unchanged lines hidden (view full) ---

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);
213 int tgt_fd = p->getSyscallArg(tc, index);
214
215 int sim_fd = p->sim_fd(tgt_fd);
216 if (sim_fd < 0)
217 return -EBADF;
218
215 int status = 0;
216 if (sim_fd > 2)
217 status = close(sim_fd);
218 if (status >= 0)
219 int status = 0;
220 if (sim_fd > 2)
221 status = close(sim_fd);
222 if (status >= 0)
219 p->reset_fd_entry(target_fd);
223 p->reset_fd_entry(tgt_fd);
220 return status;
221}
222
223
224SyscallReturn
225readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
226{
227 int index = 0;
224 return status;
225}
226
227
228SyscallReturn
229readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
230{
231 int index = 0;
228 int fd = p->sim_fd(p->getSyscallArg(tc, index));
229 assert(fd >= 0);
232 int tgt_fd = p->getSyscallArg(tc, index);
230 Addr bufPtr = p->getSyscallArg(tc, index);
231 int nbytes = p->getSyscallArg(tc, index);
232 BufferArg bufArg(bufPtr, nbytes);
233
233 Addr bufPtr = p->getSyscallArg(tc, index);
234 int nbytes = p->getSyscallArg(tc, index);
235 BufferArg bufArg(bufPtr, nbytes);
236
234 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
237 int sim_fd = p->sim_fd(tgt_fd);
238 if (sim_fd < 0)
239 return -EBADF;
235
240
241 int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
242
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;
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;
246 int fd = p->sim_fd(p->getSyscallArg(tc, index));
253 int tgt_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
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->sim_fd(tgt_fd);
259 if (sim_fd < 0)
260 return -EBADF;
261
251 bufArg.copyIn(tc->getMemProxy());
252
262 bufArg.copyIn(tc->getMemProxy());
263
253 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
264 int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
254
265
255 fsync(fd);
266 fsync(sim_fd);
256
257 return bytes_written;
258}
259
260
261SyscallReturn
262lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
263{
264 int index = 0;
267
268 return bytes_written;
269}
270
271
272SyscallReturn
273lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
274{
275 int index = 0;
265 int fd = p->sim_fd(p->getSyscallArg(tc, index));
266 assert(fd >= 0);
276 int tgt_fd = p->getSyscallArg(tc, index);
267 uint64_t offs = p->getSyscallArg(tc, index);
268 int whence = p->getSyscallArg(tc, index);
269
277 uint64_t offs = p->getSyscallArg(tc, index);
278 int whence = p->getSyscallArg(tc, index);
279
270 off_t result = lseek(fd, offs, whence);
280 int sim_fd = p->sim_fd(tgt_fd);
281 if (sim_fd < 0)
282 return -EBADF;
271
283
284 off_t result = lseek(sim_fd, offs, whence);
285
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;
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;
280 int fd = p->sim_fd(p->getSyscallArg(tc, index));
281 assert(fd >= 0);
294 int tgt_fd = p->getSyscallArg(tc, index);
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
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->sim_fd(tgt_fd);
301 if (sim_fd < 0)
302 return -EBADF;
303
287 uint64_t offset = (offset_high << 32) | offset_low;
288
304 uint64_t offset = (offset_high << 32) | offset_low;
305
289 uint64_t result = lseek(fd, offset, whence);
306 uint64_t result = lseek(sim_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"

--- 178 unchanged lines hidden (view full) ---

476 return (result == -1) ? -errno : result;
477}
478
479SyscallReturn
480ftruncateFunc(SyscallDesc *desc, int num,
481 LiveProcess *process, ThreadContext *tc)
482{
483 int index = 0;
307 result = TheISA::htog(result);
308
309 if (result == (off_t)-1) {
310 //The seek failed.
311 return -errno;
312 } else {
313 // The seek succeeded.
314 // Copy "result" to "result_ptr"

--- 178 unchanged lines hidden (view full) ---

493 return (result == -1) ? -errno : result;
494}
495
496SyscallReturn
497ftruncateFunc(SyscallDesc *desc, int num,
498 LiveProcess *process, ThreadContext *tc)
499{
500 int index = 0;
484 int fd = process->sim_fd(process->getSyscallArg(tc, index));
501 int tgt_fd = process->getSyscallArg(tc, index);
502 off_t length = process->getSyscallArg(tc, index);
485
503
486 if (fd < 0)
504 int sim_fd = process->sim_fd(tgt_fd);
505 if (sim_fd < 0)
487 return -EBADF;
488
506 return -EBADF;
507
489 off_t length = process->getSyscallArg(tc, index);
490
491 int result = ftruncate(fd, length);
508 int result = ftruncate(sim_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;

--- 15 unchanged lines hidden (view full) ---

515 return (result == -1) ? -errno : result;
516}
517
518SyscallReturn
519ftruncate64Func(SyscallDesc *desc, int num,
520 LiveProcess *process, ThreadContext *tc)
521{
522 int index = 0;
509 return (result == -1) ? -errno : result;
510}
511
512SyscallReturn
513truncate64Func(SyscallDesc *desc, int num,
514 LiveProcess *process, ThreadContext *tc)
515{
516 int index = 0;

--- 15 unchanged lines hidden (view full) ---

532 return (result == -1) ? -errno : result;
533}
534
535SyscallReturn
536ftruncate64Func(SyscallDesc *desc, int num,
537 LiveProcess *process, ThreadContext *tc)
538{
539 int index = 0;
523 int fd = process->sim_fd(process->getSyscallArg(tc, index));
540 int tgt_fd = process->getSyscallArg(tc, index);
541 int64_t length = process->getSyscallArg(tc, index, 64);
524
542
525 if (fd < 0)
543 int sim_fd = process->sim_fd(tgt_fd);
544 if (sim_fd < 0)
526 return -EBADF;
527
545 return -EBADF;
546
528 int64_t length = process->getSyscallArg(tc, index, 64);
529
530#if NO_STAT64
547#if NO_STAT64
531 int result = ftruncate(fd, length);
548 int result = ftruncate(sim_fd, length);
532#else
549#else
533 int result = ftruncate64(fd, length);
550 int result = ftruncate64(sim_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

--- 25 unchanged lines hidden (view full) ---

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;
551#endif
552 return (result == -1) ? -errno : result;
553}
554
555SyscallReturn
556umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
557{
558 // Letting the simulated program change the simulator's umask seems like

--- 25 unchanged lines hidden (view full) ---

584 int result = chown(path.c_str(), hostOwner, hostGroup);
585 return (result == -1) ? -errno : result;
586}
587
588SyscallReturn
589fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
590{
591 int index = 0;
575 int fd = process->sim_fd(process->getSyscallArg(tc, index));
592 int tgt_fd = process->getSyscallArg(tc, index);
576
593
577 if (fd < 0)
594 int sim_fd = process->sim_fd(tgt_fd);
595 if (sim_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
596 return -EBADF;
597
598 /* XXX endianess */
599 uint32_t owner = process->getSyscallArg(tc, index);
600 uid_t hostOwner = owner;
601 uint32_t group = process->getSyscallArg(tc, index);
602 gid_t hostGroup = group;
603
586 int result = fchown(fd, hostOwner, hostGroup);
604 int result = fchown(sim_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);
605 return (result == -1) ? -errno : result;
606}
607
608
609SyscallReturn
610dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
611{
612 int index = 0;
613 int tgt_fd = process->getSyscallArg(tc, index);
614
596 int sim_fd = process->sim_fd(tgt_fd);
597 if (sim_fd < 0)
598 return -EBADF;
599
600 FDEntry *fde = process->get_fd_entry(tgt_fd);
601
602 int result = dup(sim_fd);
603 return (result == -1) ? -errno :
604 process->alloc_fd(result, fde->filename, fde->flags, fde->mode, false);
605}
606
607
608SyscallReturn
609fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
610 ThreadContext *tc)
611{
612 int index = 0;
615 int sim_fd = process->sim_fd(tgt_fd);
616 if (sim_fd < 0)
617 return -EBADF;
618
619 FDEntry *fde = process->get_fd_entry(tgt_fd);
620
621 int result = dup(sim_fd);
622 return (result == -1) ? -errno :
623 process->alloc_fd(result, fde->filename, fde->flags, fde->mode, false);
624}
625
626
627SyscallReturn
628fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
629 ThreadContext *tc)
630{
631 int index = 0;
613 int fd = process->getSyscallArg(tc, index);
632 int tgt_fd = process->getSyscallArg(tc, index);
614
633
615 if (fd < 0 || process->sim_fd(fd) < 0)
634 int sim_fd = process->sim_fd(tgt_fd);
635 if (sim_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.
636 return -EBADF;
637
638 int cmd = process->getSyscallArg(tc, index);
639 switch (cmd) {
640 case 0: // F_DUPFD
641 // if we really wanted to support this, we'd need to do it
642 // in the target fd space.
623 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
643 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_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
644 return -EMFILE;
645
646 case 1: // F_GETFD (get close-on-exec flag)
647 case 2: // F_SETFD (set close-on-exec flag)
648 return 0;
649
650 case 3: // F_GETFL (get file flags)
651 case 4: // F_SETFL (set file flags)
652 // not sure if this is totally valid, but we'll pass it through
653 // to the underlying OS
634 warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
635 return fcntl(process->sim_fd(fd), cmd);
654 warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
655 return fcntl(sim_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
656 // return 0;
657
658 case 7: // F_GETLK (get lock)
659 case 8: // F_SETLK (set lock)
660 case 9: // F_SETLKW (set lock and wait)
661 // don't mess with file locking... just act like it's OK
642 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
662 warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_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;
663 return 0;
664
665 default:
666 warn("Unknown fcntl command %d\n", cmd);
667 return 0;
668 }
669}
670
671SyscallReturn
672fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
673 ThreadContext *tc)
674{
675 int index = 0;
656 int fd = process->getSyscallArg(tc, index);
676 int tgt_fd = process->getSyscallArg(tc, index);
657
677
658 if (fd < 0 || process->sim_fd(fd) < 0)
678 int sim_fd = process->sim_fd(tgt_fd);
679 if (sim_fd < 0)
659 return -EBADF;
660
661 int cmd = process->getSyscallArg(tc, index);
662 switch (cmd) {
663 case 33: //F_GETLK64
680 return -EBADF;
681
682 int cmd = process->getSyscallArg(tc, index);
683 switch (cmd) {
684 case 33: //F_GETLK64
664 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
685 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
665 return -EMFILE;
666
667 case 34: // F_SETLK64
668 case 35: // F_SETLKW64
686 return -EMFILE;
687
688 case 34: // F_SETLK64
689 case 35: // F_SETLKW64
669 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
690 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
691 tgt_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
692 return -EMFILE;
693
694 default:
695 // not sure if this is totally valid, but we'll pass it through
696 // to the underlying OS
675 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
676 return fcntl(process->sim_fd(fd), cmd);
697 warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
698 return fcntl(sim_fd, cmd);
677 // return 0;
678 }
679}
680
681SyscallReturn
682pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
683 ThreadContext *tc)
684{

--- 218 unchanged lines hidden ---
699 // return 0;
700 }
701}
702
703SyscallReturn
704pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
705 ThreadContext *tc)
706{

--- 218 unchanged lines hidden ---