| 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> | 
|  | 7 | */ | 
|  | 8 |  | 
|  | 9 | #include <linux/kernel.h> | 
|  | 10 | #include <linux/sched.h> | 
|  | 11 | #include <linux/mm.h> | 
|  | 12 | #include <linux/smp.h> | 
|  | 13 | #include <linux/smp_lock.h> | 
|  | 14 | #include <linux/errno.h> | 
|  | 15 | #include <linux/ptrace.h> | 
|  | 16 | #include <linux/user.h> | 
|  | 17 | #include <linux/personality.h> | 
|  | 18 | #include <linux/security.h> | 
|  | 19 | #include <linux/compat.h> | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 20 | #include <linux/signal.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 |  | 
|  | 22 | #include <asm/uaccess.h> | 
|  | 23 | #include <asm/pgtable.h> | 
|  | 24 | #include <asm/system.h> | 
|  | 25 | #include <asm/processor.h> | 
| Sam Ravnborg | 0013a85 | 2005-09-09 20:57:26 +0200 | [diff] [blame] | 26 | #include <asm/asm-offsets.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | /* PSW bits we allow the debugger to modify */ | 
|  | 29 | #define USER_PSW_BITS	(PSW_N | PSW_V | PSW_CB) | 
|  | 30 |  | 
|  | 31 | #undef DEBUG_PTRACE | 
|  | 32 |  | 
|  | 33 | #ifdef DEBUG_PTRACE | 
|  | 34 | #define DBG(x...)	printk(x) | 
|  | 35 | #else | 
|  | 36 | #define DBG(x...) | 
|  | 37 | #endif | 
|  | 38 |  | 
|  | 39 | #ifdef __LP64__ | 
|  | 40 |  | 
|  | 41 | /* This function is needed to translate 32 bit pt_regs offsets in to | 
|  | 42 | * 64 bit pt_regs offsets.  For example, a 32 bit gdb under a 64 bit kernel | 
|  | 43 | * will request offset 12 if it wants gr3, but the lower 32 bits of | 
|  | 44 | * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4). | 
|  | 45 | * This code relies on a 32 bit pt_regs being comprised of 32 bit values | 
|  | 46 | * except for the fp registers which (a) are 64 bits, and (b) follow | 
|  | 47 | * the gr registers at the start of pt_regs.  The 32 bit pt_regs should | 
|  | 48 | * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[] | 
|  | 49 | * being 64 bit in both cases. | 
|  | 50 | */ | 
|  | 51 |  | 
|  | 52 | static long translate_usr_offset(long offset) | 
|  | 53 | { | 
|  | 54 | if (offset < 0) | 
|  | 55 | return -1; | 
|  | 56 | else if (offset <= 32*4)	/* gr[0..31] */ | 
|  | 57 | return offset * 2 + 4; | 
|  | 58 | else if (offset <= 32*4+32*8)	/* gr[0..31] + fr[0..31] */ | 
|  | 59 | return offset + 32*4; | 
|  | 60 | else if (offset < sizeof(struct pt_regs)/2 + 32*4) | 
|  | 61 | return offset * 2 + 4 - 32*8; | 
|  | 62 | else | 
|  | 63 | return -1; | 
|  | 64 | } | 
|  | 65 | #endif | 
|  | 66 |  | 
|  | 67 | /* | 
|  | 68 | * Called by kernel/ptrace.c when detaching.. | 
|  | 69 | * | 
|  | 70 | * Make sure single step bits etc are not set. | 
|  | 71 | */ | 
|  | 72 | void ptrace_disable(struct task_struct *child) | 
|  | 73 | { | 
|  | 74 | /* make sure the trap bits are not set */ | 
|  | 75 | pa_psw(child)->r = 0; | 
|  | 76 | pa_psw(child)->t = 0; | 
|  | 77 | pa_psw(child)->h = 0; | 
|  | 78 | pa_psw(child)->l = 0; | 
|  | 79 | } | 
|  | 80 |  | 
| Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 81 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | long ret; | 
|  | 84 | #ifdef DEBUG_PTRACE | 
|  | 85 | long oaddr=addr, odata=data; | 
|  | 86 | #endif | 
|  | 87 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | switch (request) { | 
|  | 89 | case PTRACE_PEEKTEXT: /* read word at location addr. */ | 
|  | 90 | case PTRACE_PEEKDATA: { | 
|  | 91 | int copied; | 
|  | 92 |  | 
|  | 93 | #ifdef __LP64__ | 
| Kyle McMartin | a3ea84f | 2006-06-16 19:10:02 +0000 | [diff] [blame] | 94 | if (__is_compat_task(child)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | unsigned int tmp; | 
|  | 96 |  | 
|  | 97 | addr &= 0xffffffffL; | 
|  | 98 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); | 
|  | 99 | ret = -EIO; | 
|  | 100 | if (copied != sizeof(tmp)) | 
|  | 101 | goto out_tsk; | 
|  | 102 | ret = put_user(tmp,(unsigned int *) data); | 
|  | 103 | DBG("sys_ptrace(PEEK%s, %d, %lx, %lx) returning %ld, data %x\n", | 
|  | 104 | request == PTRACE_PEEKTEXT ? "TEXT" : "DATA", | 
|  | 105 | pid, oaddr, odata, ret, tmp); | 
|  | 106 | } | 
|  | 107 | else | 
|  | 108 | #endif | 
|  | 109 | { | 
|  | 110 | unsigned long tmp; | 
|  | 111 |  | 
|  | 112 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); | 
|  | 113 | ret = -EIO; | 
|  | 114 | if (copied != sizeof(tmp)) | 
|  | 115 | goto out_tsk; | 
|  | 116 | ret = put_user(tmp,(unsigned long *) data); | 
|  | 117 | } | 
|  | 118 | goto out_tsk; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /* when I and D space are separate, this will have to be fixed. */ | 
|  | 122 | case PTRACE_POKETEXT: /* write the word at location addr. */ | 
|  | 123 | case PTRACE_POKEDATA: | 
|  | 124 | ret = 0; | 
|  | 125 | #ifdef __LP64__ | 
| Kyle McMartin | a3ea84f | 2006-06-16 19:10:02 +0000 | [diff] [blame] | 126 | if (__is_compat_task(child)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | unsigned int tmp = (unsigned int)data; | 
|  | 128 | DBG("sys_ptrace(POKE%s, %d, %lx, %lx)\n", | 
|  | 129 | request == PTRACE_POKETEXT ? "TEXT" : "DATA", | 
|  | 130 | pid, oaddr, odata); | 
|  | 131 | addr &= 0xffffffffL; | 
|  | 132 | if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp)) | 
|  | 133 | goto out_tsk; | 
|  | 134 | } | 
|  | 135 | else | 
|  | 136 | #endif | 
|  | 137 | { | 
|  | 138 | if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) | 
|  | 139 | goto out_tsk; | 
|  | 140 | } | 
|  | 141 | ret = -EIO; | 
|  | 142 | goto out_tsk; | 
|  | 143 |  | 
|  | 144 | /* Read the word at location addr in the USER area.  For ptraced | 
|  | 145 | processes, the kernel saves all regs on a syscall. */ | 
|  | 146 | case PTRACE_PEEKUSR: { | 
|  | 147 | ret = -EIO; | 
|  | 148 | #ifdef __LP64__ | 
| Kyle McMartin | a3ea84f | 2006-06-16 19:10:02 +0000 | [diff] [blame] | 149 | if (__is_compat_task(child)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | unsigned int tmp; | 
|  | 151 |  | 
|  | 152 | if (addr & (sizeof(int)-1)) | 
|  | 153 | goto out_tsk; | 
|  | 154 | if ((addr = translate_usr_offset(addr)) < 0) | 
|  | 155 | goto out_tsk; | 
|  | 156 |  | 
|  | 157 | tmp = *(unsigned int *) ((char *) task_regs(child) + addr); | 
|  | 158 | ret = put_user(tmp, (unsigned int *) data); | 
|  | 159 | DBG("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n", | 
|  | 160 | pid, oaddr, odata, ret, addr, tmp); | 
|  | 161 | } | 
|  | 162 | else | 
|  | 163 | #endif | 
|  | 164 | { | 
|  | 165 | unsigned long tmp; | 
|  | 166 |  | 
|  | 167 | if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs)) | 
|  | 168 | goto out_tsk; | 
|  | 169 | tmp = *(unsigned long *) ((char *) task_regs(child) + addr); | 
|  | 170 | ret = put_user(tmp, (unsigned long *) data); | 
|  | 171 | } | 
|  | 172 | goto out_tsk; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /* Write the word at location addr in the USER area.  This will need | 
|  | 176 | to change when the kernel no longer saves all regs on a syscall. | 
|  | 177 | FIXME.  There is a problem at the moment in that r3-r18 are only | 
|  | 178 | saved if the process is ptraced on syscall entry, and even then | 
|  | 179 | those values are overwritten by actual register values on syscall | 
|  | 180 | exit. */ | 
|  | 181 | case PTRACE_POKEUSR: | 
|  | 182 | ret = -EIO; | 
|  | 183 | /* Some register values written here may be ignored in | 
|  | 184 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with | 
|  | 185 | * r31/r31+4, and not with the values in pt_regs. | 
|  | 186 | */ | 
|  | 187 | /* PT_PSW=0, so this is valid for 32 bit processes under 64 | 
|  | 188 | * bit kernels. | 
|  | 189 | */ | 
|  | 190 | if (addr == PT_PSW) { | 
|  | 191 | /* PT_PSW=0, so this is valid for 32 bit processes | 
|  | 192 | * under 64 bit kernels. | 
|  | 193 | * | 
|  | 194 | * Allow writing to Nullify, Divide-step-correction, | 
|  | 195 | * and carry/borrow bits. | 
|  | 196 | * BEWARE, if you set N, and then single step, it won't | 
|  | 197 | * stop on the nullified instruction. | 
|  | 198 | */ | 
|  | 199 | DBG("sys_ptrace(POKEUSR, %d, %lx, %lx)\n", | 
|  | 200 | pid, oaddr, odata); | 
|  | 201 | data &= USER_PSW_BITS; | 
|  | 202 | task_regs(child)->gr[0] &= ~USER_PSW_BITS; | 
|  | 203 | task_regs(child)->gr[0] |= data; | 
|  | 204 | ret = 0; | 
|  | 205 | goto out_tsk; | 
|  | 206 | } | 
|  | 207 | #ifdef __LP64__ | 
| Kyle McMartin | a3ea84f | 2006-06-16 19:10:02 +0000 | [diff] [blame] | 208 | if (__is_compat_task(child)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | if (addr & (sizeof(int)-1)) | 
|  | 210 | goto out_tsk; | 
|  | 211 | if ((addr = translate_usr_offset(addr)) < 0) | 
|  | 212 | goto out_tsk; | 
|  | 213 | DBG("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n", | 
|  | 214 | pid, oaddr, odata, addr); | 
|  | 215 | if (addr >= PT_FR0 && addr <= PT_FR31 + 4) { | 
|  | 216 | /* Special case, fp regs are 64 bits anyway */ | 
|  | 217 | *(unsigned int *) ((char *) task_regs(child) + addr) = data; | 
|  | 218 | ret = 0; | 
|  | 219 | } | 
|  | 220 | else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) || | 
|  | 221 | addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 || | 
|  | 222 | addr == PT_SAR+4) { | 
|  | 223 | /* Zero the top 32 bits */ | 
|  | 224 | *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0; | 
|  | 225 | *(unsigned int *) ((char *) task_regs(child) + addr) = data; | 
|  | 226 | ret = 0; | 
|  | 227 | } | 
|  | 228 | goto out_tsk; | 
|  | 229 | } | 
|  | 230 | else | 
|  | 231 | #endif | 
|  | 232 | { | 
|  | 233 | if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs)) | 
|  | 234 | goto out_tsk; | 
|  | 235 | if ((addr >= PT_GR1 && addr <= PT_GR31) || | 
|  | 236 | addr == PT_IAOQ0 || addr == PT_IAOQ1 || | 
|  | 237 | (addr >= PT_FR0 && addr <= PT_FR31 + 4) || | 
|  | 238 | addr == PT_SAR) { | 
|  | 239 | *(unsigned long *) ((char *) task_regs(child) + addr) = data; | 
|  | 240 | ret = 0; | 
|  | 241 | } | 
|  | 242 | goto out_tsk; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ | 
|  | 246 | case PTRACE_CONT: | 
|  | 247 | ret = -EIO; | 
|  | 248 | DBG("sys_ptrace(%s)\n", | 
|  | 249 | request == PTRACE_SYSCALL ? "SYSCALL" : "CONT"); | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 250 | if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | goto out_tsk; | 
|  | 252 | child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP); | 
|  | 253 | if (request == PTRACE_SYSCALL) { | 
|  | 254 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
|  | 255 | } else { | 
|  | 256 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
|  | 257 | } | 
|  | 258 | child->exit_code = data; | 
|  | 259 | goto out_wake_notrap; | 
|  | 260 |  | 
|  | 261 | case PTRACE_KILL: | 
|  | 262 | /* | 
|  | 263 | * make the child exit.  Best I can do is send it a | 
|  | 264 | * sigkill.  perhaps it should be put in the status | 
|  | 265 | * that it wants to exit. | 
|  | 266 | */ | 
| Matthew Wilcox | 83aceb5 | 2005-11-17 16:37:24 -0500 | [diff] [blame] | 267 | ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | DBG("sys_ptrace(KILL)\n"); | 
|  | 269 | if (child->exit_state == EXIT_ZOMBIE)	/* already dead */ | 
|  | 270 | goto out_tsk; | 
|  | 271 | child->exit_code = SIGKILL; | 
|  | 272 | goto out_wake_notrap; | 
|  | 273 |  | 
|  | 274 | case PTRACE_SINGLEBLOCK: | 
|  | 275 | DBG("sys_ptrace(SINGLEBLOCK)\n"); | 
|  | 276 | ret = -EIO; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 277 | if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | goto out_tsk; | 
|  | 279 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
|  | 280 | child->ptrace &= ~PT_SINGLESTEP; | 
|  | 281 | child->ptrace |= PT_BLOCKSTEP; | 
|  | 282 | child->exit_code = data; | 
|  | 283 |  | 
|  | 284 | /* Enable taken branch trap. */ | 
|  | 285 | pa_psw(child)->r = 0; | 
|  | 286 | pa_psw(child)->t = 1; | 
|  | 287 | pa_psw(child)->h = 0; | 
|  | 288 | pa_psw(child)->l = 0; | 
|  | 289 | goto out_wake; | 
|  | 290 |  | 
|  | 291 | case PTRACE_SINGLESTEP: | 
|  | 292 | DBG("sys_ptrace(SINGLESTEP)\n"); | 
|  | 293 | ret = -EIO; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 294 | if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | goto out_tsk; | 
|  | 296 |  | 
|  | 297 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
|  | 298 | child->ptrace &= ~PT_BLOCKSTEP; | 
|  | 299 | child->ptrace |= PT_SINGLESTEP; | 
|  | 300 | child->exit_code = data; | 
|  | 301 |  | 
|  | 302 | if (pa_psw(child)->n) { | 
|  | 303 | struct siginfo si; | 
|  | 304 |  | 
|  | 305 | /* Nullified, just crank over the queue. */ | 
|  | 306 | task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1]; | 
|  | 307 | task_regs(child)->iasq[0] = task_regs(child)->iasq[1]; | 
|  | 308 | task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4; | 
|  | 309 | pa_psw(child)->n = 0; | 
|  | 310 | pa_psw(child)->x = 0; | 
|  | 311 | pa_psw(child)->y = 0; | 
|  | 312 | pa_psw(child)->z = 0; | 
|  | 313 | pa_psw(child)->b = 0; | 
|  | 314 | ptrace_disable(child); | 
|  | 315 | /* Don't wake up the child, but let the | 
|  | 316 | parent know something happened. */ | 
|  | 317 | si.si_code = TRAP_TRACE; | 
|  | 318 | si.si_addr = (void __user *) (task_regs(child)->iaoq[0] & ~3); | 
|  | 319 | si.si_signo = SIGTRAP; | 
|  | 320 | si.si_errno = 0; | 
|  | 321 | force_sig_info(SIGTRAP, &si, child); | 
|  | 322 | //notify_parent(child, SIGCHLD); | 
|  | 323 | //ret = 0; | 
|  | 324 | goto out_wake; | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | /* Enable recovery counter traps.  The recovery counter | 
|  | 328 | * itself will be set to zero on a task switch.  If the | 
|  | 329 | * task is suspended on a syscall then the syscall return | 
|  | 330 | * path will overwrite the recovery counter with a suitable | 
|  | 331 | * value such that it traps once back in user space.  We | 
|  | 332 | * disable interrupts in the childs PSW here also, to avoid | 
|  | 333 | * interrupts while the recovery counter is decrementing. | 
|  | 334 | */ | 
|  | 335 | pa_psw(child)->r = 1; | 
|  | 336 | pa_psw(child)->t = 0; | 
|  | 337 | pa_psw(child)->h = 0; | 
|  | 338 | pa_psw(child)->l = 0; | 
|  | 339 | /* give it a chance to run. */ | 
|  | 340 | goto out_wake; | 
|  | 341 |  | 
|  | 342 | case PTRACE_DETACH: | 
|  | 343 | ret = ptrace_detach(child, data); | 
|  | 344 | goto out_tsk; | 
|  | 345 |  | 
|  | 346 | case PTRACE_GETEVENTMSG: | 
|  | 347 | ret = put_user(child->ptrace_message, (unsigned int __user *) data); | 
| Matthew Wilcox | 83aceb5 | 2005-11-17 16:37:24 -0500 | [diff] [blame] | 348 | goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 |  | 
|  | 350 | default: | 
|  | 351 | ret = ptrace_request(child, request, addr, data); | 
| Matthew Wilcox | 83aceb5 | 2005-11-17 16:37:24 -0500 | [diff] [blame] | 352 | goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
|  | 355 | out_wake_notrap: | 
|  | 356 | ptrace_disable(child); | 
|  | 357 | out_wake: | 
|  | 358 | wake_up_process(child); | 
|  | 359 | ret = 0; | 
|  | 360 | out_tsk: | 
| Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 361 | DBG("arch_ptrace(%ld, %d, %lx, %lx) returning %ld\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | request, pid, oaddr, odata, ret); | 
|  | 363 | return ret; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | void syscall_trace(void) | 
|  | 367 | { | 
|  | 368 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) | 
|  | 369 | return; | 
|  | 370 | if (!(current->ptrace & PT_PTRACED)) | 
|  | 371 | return; | 
|  | 372 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | 
|  | 373 | ? 0x80 : 0)); | 
|  | 374 | /* | 
|  | 375 | * this isn't the same as continuing with a signal, but it will do | 
|  | 376 | * for normal use.  strace only continues with a signal if the | 
|  | 377 | * stopping signal is not SIGTRAP.  -brl | 
|  | 378 | */ | 
|  | 379 | if (current->exit_code) { | 
|  | 380 | send_sig(current->exit_code, current, 1); | 
|  | 381 | current->exit_code = 0; | 
|  | 382 | } | 
|  | 383 | } |