Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel support for the ptrace() and syscall tracing interfaces. |
| 3 | * |
| 4 | * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc. |
| 5 | * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx> |
| 6 | * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org> |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008 Helge Deller <deller@gmx.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/ptrace.h> |
Kyle McMartin | d6b5877 | 2009-04-26 23:53:34 -0400 | [diff] [blame] | 16 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/user.h> |
| 18 | #include <linux/personality.h> |
| 19 | #include <linux/security.h> |
| 20 | #include <linux/compat.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 21 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/system.h> |
| 26 | #include <asm/processor.h> |
Sam Ravnborg | 0013a85 | 2005-09-09 20:57:26 +0200 | [diff] [blame] | 27 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /* PSW bits we allow the debugger to modify */ |
| 30 | #define USER_PSW_BITS (PSW_N | PSW_V | PSW_CB) |
| 31 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Called by kernel/ptrace.c when detaching.. |
| 34 | * |
| 35 | * Make sure single step bits etc are not set. |
| 36 | */ |
| 37 | void ptrace_disable(struct task_struct *task) |
| 38 | { |
| 39 | task->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 41 | /* make sure the trap bits are not set */ |
| 42 | pa_psw(task)->r = 0; |
| 43 | pa_psw(task)->t = 0; |
| 44 | pa_psw(task)->h = 0; |
| 45 | pa_psw(task)->l = 0; |
| 46 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 48 | /* |
| 49 | * The following functions are called by ptrace_resume() when |
| 50 | * enabling or disabling single/block tracing. |
| 51 | */ |
| 52 | void user_disable_single_step(struct task_struct *task) |
| 53 | { |
| 54 | ptrace_disable(task); |
| 55 | } |
| 56 | |
| 57 | void user_enable_single_step(struct task_struct *task) |
| 58 | { |
| 59 | task->ptrace &= ~PT_BLOCKSTEP; |
| 60 | task->ptrace |= PT_SINGLESTEP; |
| 61 | |
| 62 | if (pa_psw(task)->n) { |
| 63 | struct siginfo si; |
| 64 | |
| 65 | /* Nullified, just crank over the queue. */ |
| 66 | task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1]; |
| 67 | task_regs(task)->iasq[0] = task_regs(task)->iasq[1]; |
| 68 | task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4; |
| 69 | pa_psw(task)->n = 0; |
| 70 | pa_psw(task)->x = 0; |
| 71 | pa_psw(task)->y = 0; |
| 72 | pa_psw(task)->z = 0; |
| 73 | pa_psw(task)->b = 0; |
| 74 | ptrace_disable(task); |
| 75 | /* Don't wake up the task, but let the |
| 76 | parent know something happened. */ |
| 77 | si.si_code = TRAP_TRACE; |
| 78 | si.si_addr = (void __user *) (task_regs(task)->iaoq[0] & ~3); |
| 79 | si.si_signo = SIGTRAP; |
| 80 | si.si_errno = 0; |
| 81 | force_sig_info(SIGTRAP, &si, task); |
| 82 | /* notify_parent(task, SIGCHLD); */ |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | /* Enable recovery counter traps. The recovery counter |
| 87 | * itself will be set to zero on a task switch. If the |
| 88 | * task is suspended on a syscall then the syscall return |
| 89 | * path will overwrite the recovery counter with a suitable |
| 90 | * value such that it traps once back in user space. We |
| 91 | * disable interrupts in the tasks PSW here also, to avoid |
| 92 | * interrupts while the recovery counter is decrementing. |
| 93 | */ |
| 94 | pa_psw(task)->r = 1; |
| 95 | pa_psw(task)->t = 0; |
| 96 | pa_psw(task)->h = 0; |
| 97 | pa_psw(task)->l = 0; |
| 98 | } |
| 99 | |
| 100 | void user_enable_block_step(struct task_struct *task) |
| 101 | { |
| 102 | task->ptrace &= ~PT_SINGLESTEP; |
| 103 | task->ptrace |= PT_BLOCKSTEP; |
| 104 | |
| 105 | /* Enable taken branch trap. */ |
| 106 | pa_psw(task)->r = 0; |
| 107 | pa_psw(task)->t = 1; |
| 108 | pa_psw(task)->h = 0; |
| 109 | pa_psw(task)->l = 0; |
| 110 | } |
| 111 | |
| 112 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
| 113 | { |
| 114 | unsigned long tmp; |
| 115 | long ret = -EIO; |
| 116 | |
| 117 | switch (request) { |
| 118 | |
| 119 | /* Read the word at location addr in the USER area. For ptraced |
| 120 | processes, the kernel saves all regs on a syscall. */ |
| 121 | case PTRACE_PEEKUSR: |
| 122 | if ((addr & (sizeof(long)-1)) || |
| 123 | (unsigned long) addr >= sizeof(struct pt_regs)) |
| 124 | break; |
| 125 | tmp = *(unsigned long *) ((char *) task_regs(child) + addr); |
| 126 | ret = put_user(tmp, (unsigned long *) data); |
| 127 | break; |
| 128 | |
| 129 | /* Write the word at location addr in the USER area. This will need |
| 130 | to change when the kernel no longer saves all regs on a syscall. |
| 131 | FIXME. There is a problem at the moment in that r3-r18 are only |
| 132 | saved if the process is ptraced on syscall entry, and even then |
| 133 | those values are overwritten by actual register values on syscall |
| 134 | exit. */ |
| 135 | case PTRACE_POKEUSR: |
| 136 | /* Some register values written here may be ignored in |
| 137 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with |
| 138 | * r31/r31+4, and not with the values in pt_regs. |
| 139 | */ |
| 140 | if (addr == PT_PSW) { |
| 141 | /* Allow writing to Nullify, Divide-step-correction, |
| 142 | * and carry/borrow bits. |
| 143 | * BEWARE, if you set N, and then single step, it won't |
| 144 | * stop on the nullified instruction. |
| 145 | */ |
| 146 | data &= USER_PSW_BITS; |
| 147 | task_regs(child)->gr[0] &= ~USER_PSW_BITS; |
| 148 | task_regs(child)->gr[0] |= data; |
| 149 | ret = 0; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | if ((addr & (sizeof(long)-1)) || |
| 154 | (unsigned long) addr >= sizeof(struct pt_regs)) |
| 155 | break; |
| 156 | if ((addr >= PT_GR1 && addr <= PT_GR31) || |
| 157 | addr == PT_IAOQ0 || addr == PT_IAOQ1 || |
| 158 | (addr >= PT_FR0 && addr <= PT_FR31 + 4) || |
| 159 | addr == PT_SAR) { |
| 160 | *(unsigned long *) ((char *) task_regs(child) + addr) = data; |
| 161 | ret = 0; |
| 162 | } |
| 163 | break; |
| 164 | |
| 165 | default: |
| 166 | ret = ptrace_request(child, request, addr, data); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | #ifdef CONFIG_COMPAT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* This function is needed to translate 32 bit pt_regs offsets in to |
| 177 | * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel |
| 178 | * will request offset 12 if it wants gr3, but the lower 32 bits of |
| 179 | * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4). |
| 180 | * This code relies on a 32 bit pt_regs being comprised of 32 bit values |
| 181 | * except for the fp registers which (a) are 64 bits, and (b) follow |
| 182 | * the gr registers at the start of pt_regs. The 32 bit pt_regs should |
| 183 | * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[] |
| 184 | * being 64 bit in both cases. |
| 185 | */ |
| 186 | |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 187 | static compat_ulong_t translate_usr_offset(compat_ulong_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | if (offset < 0) |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 190 | return sizeof(struct pt_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | else if (offset <= 32*4) /* gr[0..31] */ |
| 192 | return offset * 2 + 4; |
| 193 | else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */ |
| 194 | return offset + 32*4; |
| 195 | else if (offset < sizeof(struct pt_regs)/2 + 32*4) |
| 196 | return offset * 2 + 4 - 32*8; |
| 197 | else |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 198 | return sizeof(struct pt_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 201 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
| 202 | compat_ulong_t addr, compat_ulong_t data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 204 | compat_uint_t tmp; |
| 205 | long ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | switch (request) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 209 | case PTRACE_PEEKUSR: |
| 210 | if (addr & (sizeof(compat_uint_t)-1)) |
| 211 | break; |
| 212 | addr = translate_usr_offset(addr); |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 213 | if (addr >= sizeof(struct pt_regs)) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 214 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 216 | tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr); |
| 217 | ret = put_user(tmp, (compat_uint_t *) (unsigned long) data); |
| 218 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /* Write the word at location addr in the USER area. This will need |
| 221 | to change when the kernel no longer saves all regs on a syscall. |
| 222 | FIXME. There is a problem at the moment in that r3-r18 are only |
| 223 | saved if the process is ptraced on syscall entry, and even then |
| 224 | those values are overwritten by actual register values on syscall |
| 225 | exit. */ |
| 226 | case PTRACE_POKEUSR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /* Some register values written here may be ignored in |
| 228 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with |
| 229 | * r31/r31+4, and not with the values in pt_regs. |
| 230 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (addr == PT_PSW) { |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 232 | /* Since PT_PSW==0, it is valid for 32 bit processes |
| 233 | * under 64 bit kernels as well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | */ |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 235 | ret = arch_ptrace(child, request, addr, data); |
| 236 | } else { |
| 237 | if (addr & (sizeof(compat_uint_t)-1)) |
| 238 | break; |
| 239 | addr = translate_usr_offset(addr); |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 240 | if (addr >= sizeof(struct pt_regs)) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 241 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (addr >= PT_FR0 && addr <= PT_FR31 + 4) { |
| 243 | /* Special case, fp regs are 64 bits anyway */ |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 244 | *(__u64 *) ((char *) task_regs(child) + addr) = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | ret = 0; |
| 246 | } |
| 247 | else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) || |
| 248 | addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 || |
| 249 | addr == PT_SAR+4) { |
| 250 | /* Zero the top 32 bits */ |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 251 | *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0; |
| 252 | *(__u32 *) ((char *) task_regs(child) + addr) = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | ret = 0; |
| 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 256 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | default: |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 259 | ret = compat_ptrace_request(child, request, addr, data); |
| 260 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return ret; |
| 264 | } |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 265 | #endif |
| 266 | |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame^] | 267 | long do_syscall_trace_enter(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame^] | 269 | if (test_thread_flag(TIF_SYSCALL_TRACE) && |
| 270 | tracehook_report_syscall_entry(regs)) |
| 271 | return -1L; |
Kyle McMartin | d6b5877 | 2009-04-26 23:53:34 -0400 | [diff] [blame] | 272 | |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame^] | 273 | return regs->gr[20]; |
| 274 | } |
| 275 | |
| 276 | void do_syscall_trace_exit(struct pt_regs *regs) |
| 277 | { |
| 278 | int stepping = !!(current->ptrace & (PT_SINGLESTEP|PT_BLOCKSTEP)); |
| 279 | |
| 280 | if (stepping || test_thread_flag(TIF_SYSCALL_TRACE)) |
| 281 | tracehook_report_syscall_exit(regs, stepping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |