sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1 | |
sewardj | 331dc98 | 2005-12-22 20:14:57 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Support for doing system calls. syscall-ppc64-linux.S ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2005-2008 Paul Mackerras <paulus@samba.org> |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 11 | |
| 12 | This program is free software; you can redistribute it and/or |
| 13 | modify it under the terms of the GNU General Public License as |
| 14 | published by the Free Software Foundation; either version 2 of the |
| 15 | License, or (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but |
| 18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program; if not, write to the Free Software |
| 24 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 25 | 02111-1307, USA. |
| 26 | |
| 27 | The GNU General Public License is contained in the file COPYING. |
| 28 | */ |
| 29 | |
| 30 | #include "pub_core_basics_asm.h" |
sewardj | c95257a | 2006-10-14 19:51:19 +0000 | [diff] [blame] | 31 | #include "pub_core_vkiscnums.h" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 32 | #include "libvex_guest_offsets.h" |
| 33 | |
| 34 | |
| 35 | /*----------------------------------------------------------------*/ |
| 36 | /* |
| 37 | Perform a syscall for the client. This will run a syscall |
| 38 | with the client's specific per-thread signal mask. |
| 39 | |
| 40 | The structure of this function is such that, if the syscall is |
| 41 | interrupted by a signal, we can determine exactly what |
| 42 | execution state we were in with respect to the execution of |
| 43 | the syscall by examining the value of NIP in the signal |
| 44 | handler. This means that we can always do the appropriate |
| 45 | thing to precisely emulate the kernel's signal/syscall |
| 46 | interactions. |
| 47 | |
| 48 | The syscall number is taken from the argument, even though it |
| 49 | should also be in regs->m_gpr[0]. The syscall result is written |
| 50 | back to regs->m_gpr[3]/m_xer/m_result on completion. |
| 51 | |
| 52 | Returns 0 if the syscall was successfully called (even if the |
sewardj | 331dc98 | 2005-12-22 20:14:57 +0000 | [diff] [blame] | 53 | syscall itself failed), or a nonzero error code in the lowest |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 54 | 8 bits if one of the sigprocmasks failed (there's no way to |
| 55 | determine which one failed). And there's no obvious way to |
| 56 | recover from that either, but nevertheless we want to know. |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 57 | |
| 58 | VG_(fixup_guest_state_after_syscall_interrupted) does the |
| 59 | thread state fixup in the case where we were interrupted by a |
| 60 | signal. |
| 61 | |
| 62 | Prototype: |
| 63 | |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 64 | UWord ML_(do_syscall_for_client_WRK)( |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 65 | Int syscallno, // r3 |
| 66 | void* guest_state, // r4 |
| 67 | const vki_sigset_t *sysmask, // r5 |
| 68 | const vki_sigset_t *postmask, // r6 |
| 69 | Int nsigwords) // r7 |
| 70 | */ |
| 71 | /* from vki_arch.h */ |
| 72 | #define VKI_SIG_SETMASK 2 |
| 73 | |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 74 | .align 2 |
| 75 | .globl ML_(do_syscall_for_client_WRK) |
| 76 | .section ".opd","aw" |
| 77 | .align 3 |
| 78 | ML_(do_syscall_for_client_WRK): |
| 79 | .quad .ML_(do_syscall_for_client_WRK),.TOC.@tocbase,0 |
| 80 | .previous |
| 81 | .type .ML_(do_syscall_for_client_WRK),@function |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 82 | .globl .ML_(do_syscall_for_client_WRK) |
| 83 | .ML_(do_syscall_for_client_WRK): |
| 84 | /* make a stack frame */ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 85 | stdu 1,-80(1) |
| 86 | std 31,72(1) |
| 87 | std 30,64(1) |
| 88 | std 29,56(1) |
| 89 | std 28,48(1) |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 90 | mr 31,3 /* syscall number */ |
| 91 | mr 30,4 /* guest_state */ |
| 92 | mr 29,6 /* postmask */ |
| 93 | mr 28,7 /* nsigwords */ |
| 94 | |
| 95 | /* set the signal mask for doing the system call */ |
| 96 | /* set up for sigprocmask(SIG_SETMASK, sysmask, postmask) */ |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 97 | 1: li 0,__NR_rt_sigprocmask |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 98 | li 3,VKI_SIG_SETMASK |
| 99 | mr 4,5 |
| 100 | mr 5,6 |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 101 | mr 6,7 |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 102 | sc /* set the mask */ |
| 103 | bso 7f /* if the sigprocmask fails */ |
| 104 | |
| 105 | /* load up syscall args from the threadstate */ |
| 106 | ld 3,OFFSET_ppc64_GPR3(30) |
| 107 | ld 4,OFFSET_ppc64_GPR4(30) |
| 108 | ld 5,OFFSET_ppc64_GPR5(30) |
| 109 | ld 6,OFFSET_ppc64_GPR6(30) |
| 110 | ld 7,OFFSET_ppc64_GPR7(30) |
| 111 | ld 8,OFFSET_ppc64_GPR8(30) |
| 112 | mr 0,31 /* syscall number */ |
| 113 | 2: sc /* do the syscall */ |
| 114 | |
| 115 | /* put the result back in the threadstate */ |
| 116 | 3: std 3,OFFSET_ppc64_GPR3(30) /* gst->GPR3 = sc result */ |
| 117 | /* copy cr0.so back to simulated state */ |
| 118 | mfcr 5 /* r5 = CR */ |
| 119 | rlwinm 5,5,4,31,31 /* r5 = (CR >> 28) & 1 */ |
| 120 | stb 5,OFFSET_ppc64_CR0_0(30) /* gst->CR0.SO = cr0.so */ |
| 121 | |
| 122 | /* block signals again */ |
| 123 | /* set up for sigprocmask(SIG_SETMASK, postmask, NULL) */ |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 124 | 4: li 0,__NR_rt_sigprocmask |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 125 | li 3,VKI_SIG_SETMASK |
| 126 | mr 4,29 |
| 127 | li 5,0 |
| 128 | mr 6,28 |
| 129 | sc /* set the mask */ |
| 130 | bso 7f /* if the sigprocmask fails */ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 131 | /* now safe from signals */ |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 132 | li 3,0 /* SUCCESS */ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 133 | |
| 134 | /* pop off stack frame */ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 135 | 5: ld 28,48(1) |
| 136 | ld 29,56(1) |
| 137 | ld 30,64(1) |
| 138 | ld 31,72(1) |
| 139 | addi 1,1,80 |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 140 | blr |
| 141 | |
sewardj | 331dc98 | 2005-12-22 20:14:57 +0000 | [diff] [blame] | 142 | /* failure: return 0x8000 | error code */ |
sewardj | 2537616 | 2005-12-22 19:28:37 +0000 | [diff] [blame] | 143 | 7: ori 3,3,0x8000 /* FAILURE -- ensure return value is nonzero */ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 144 | b 5b |
| 145 | |
| 146 | .section .rodata |
| 147 | /* export the ranges so that |
| 148 | VG_(fixup_guest_state_after_syscall_interrupted) can do the |
| 149 | right thing */ |
| 150 | |
| 151 | .globl ML_(blksys_setup) |
| 152 | .globl ML_(blksys_restart) |
| 153 | .globl ML_(blksys_complete) |
| 154 | .globl ML_(blksys_committed) |
| 155 | .globl ML_(blksys_finished) |
sewardj | 4246fc6 | 2006-01-02 19:06:08 +0000 | [diff] [blame] | 156 | ML_(blksys_setup): .quad 1b |
| 157 | ML_(blksys_restart): .quad 2b |
| 158 | ML_(blksys_complete): .quad 3b |
| 159 | ML_(blksys_committed): .quad 4b |
| 160 | ML_(blksys_finished): .quad 5b |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 161 | |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 162 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 163 | /* Let the linker know we don't need an executable stack */ |
| 164 | .section .note.GNU-stack,"",@progbits |
| 165 | |
sewardj | 331dc98 | 2005-12-22 20:14:57 +0000 | [diff] [blame] | 166 | /*--------------------------------------------------------------------*/ |
| 167 | /*--- end ---*/ |
| 168 | /*--------------------------------------------------------------------*/ |