sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | ##--------------------------------------------------------------------## |
| 3 | ##--- Support for doing system calls. ---## |
| 4 | ##--- vg_syscall.S ---## |
| 5 | ##--------------------------------------------------------------------## |
| 6 | |
| 7 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, an extensible x86 protected-mode |
| 9 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2003 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include "vg_constants.h" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame^] | 33 | #include "vg_unistd.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
| 35 | .globl VG_(do_syscall) |
| 36 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame^] | 37 | /* |
| 38 | Perform a Linux syscall with int 0x80 |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 39 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame^] | 40 | Syscall args are passed on the stack |
| 41 | Int VG_(do_syscall)(Int syscall_no, ...) |
| 42 | |
| 43 | This has no effect on the virtual machine; the expectation is |
| 44 | that the syscall mechanism makes no useful changes to any |
| 45 | register except %eax, which is returned. |
| 46 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 47 | VG_(do_syscall): |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame^] | 48 | push %esi |
| 49 | push %edi |
| 50 | push %ebx |
| 51 | push %ebp |
| 52 | movl 16+ 4(%esp),%eax |
| 53 | movl 16+ 8(%esp),%ebx |
| 54 | movl 16+12(%esp),%ecx |
| 55 | movl 16+16(%esp),%edx |
| 56 | movl 16+20(%esp),%esi |
| 57 | movl 16+24(%esp),%edi |
| 58 | movl 16+28(%esp),%ebp |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 59 | int $0x80 |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame^] | 60 | popl %ebp |
| 61 | popl %ebx |
| 62 | popl %edi |
| 63 | popl %esi |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 64 | ret |
| 65 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame^] | 66 | /* |
| 67 | Perform a clone system call. clone is strange because it has |
| 68 | fork()-like return-twice semantics, so it needs special |
| 69 | handling here. |
| 70 | |
| 71 | int VG_(clone)(int (*fn)(void *), void *child_stack, int flags, void *arg, |
| 72 | 0 4 8 12 |
| 73 | pid_t *child_tid, pid_t *parent_tid) |
| 74 | 16 20 |
| 75 | |
| 76 | */ |
| 77 | .globl VG_(clone) |
| 78 | VG_(clone): |
| 79 | #define FSZ (4+4+4) /* frame size = retaddr+ebx+edi */ |
| 80 | push %ebx |
| 81 | push %edi |
| 82 | /* set up child stack with function and arg */ |
| 83 | movl 4+FSZ(%esp), %ecx /* child stack */ |
| 84 | movl 12+FSZ(%esp), %ebx /* fn arg */ |
| 85 | movl 0+FSZ(%esp), %eax /* fn */ |
| 86 | lea -8(%ecx), %ecx /* make space on stack */ |
| 87 | movl %ebx, 4(%ecx) /* fn arg */ |
| 88 | movl %eax, 0(%ecx) /* fn */ |
| 89 | |
| 90 | /* get other args to clone */ |
| 91 | movl 8+FSZ(%esp), %ebx /* flags */ |
| 92 | movl 20+FSZ(%esp), %edx /* parent tid * */ |
| 93 | movl 16+FSZ(%esp), %edi /* child tid * */ |
| 94 | movl $__NR_clone, %eax |
| 95 | int $0x80 |
| 96 | testl %eax, %eax |
| 97 | jnz 1f |
| 98 | |
| 99 | /* CHILD - call thread function */ |
| 100 | popl %eax |
| 101 | call *%eax |
| 102 | |
| 103 | /* exit with result */ |
| 104 | movl %eax, %ebx |
| 105 | movl $__NR_exit, %eax |
| 106 | int $0x80 |
| 107 | |
| 108 | /* Hm, exit returned */ |
| 109 | ud2 |
| 110 | |
| 111 | 1: /* PARENT or ERROR */ |
| 112 | pop %edi |
| 113 | pop %ebx |
| 114 | ret |
| 115 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 116 | ##--------------------------------------------------------------------## |
| 117 | ##--- end vg_syscall.S ---## |
| 118 | ##--------------------------------------------------------------------## |