Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mackerras | b123923 | 2005-10-20 09:11:29 +1000 | [diff] [blame] | 2 | * ptrace for 32-bit processes running on a 64-bit kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * PowerPC version |
| 5 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
| 6 | * |
| 7 | * Derived from "arch/m68k/kernel/ptrace.c" |
| 8 | * Copyright (C) 1994 by Hamish Macdonald |
| 9 | * Taken from linux/kernel/ptrace.c and modified for M680x0. |
| 10 | * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds |
| 11 | * |
| 12 | * Modified by Cort Dougan (cort@hq.fsmlabs.com) |
Paul Mackerras | b123923 | 2005-10-20 09:11:29 +1000 | [diff] [blame] | 13 | * and Paul Mackerras (paulus@samba.org). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * This file is subject to the terms and conditions of the GNU General |
Paul Mackerras | b123923 | 2005-10-20 09:11:29 +1000 | [diff] [blame] | 16 | * Public License. See the file COPYING in the main directory of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * this archive for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/smp.h> |
| 24 | #include <linux/smp_lock.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/ptrace.h> |
| 27 | #include <linux/user.h> |
| 28 | #include <linux/security.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 29 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/page.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | #include <asm/system.h> |
Paul Mackerras | 21a6290 | 2005-11-19 20:47:22 +1100 | [diff] [blame] | 35 | |
Benjamin Herrenschmidt | acd8982 | 2007-06-04 15:15:41 +1000 | [diff] [blame] | 36 | #include "ptrace-ppc64.h" |
Paul Mackerras | 21a6290 | 2005-11-19 20:47:22 +1100 | [diff] [blame] | 37 | #include "ptrace-common.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * does not yet catch signals sent when the child dies. |
| 41 | * in exit.c or in signal.c. |
| 42 | */ |
| 43 | |
Benjamin Herrenschmidt | e17666b | 2007-06-04 15:15:43 +1000 | [diff] [blame^] | 44 | /* |
| 45 | * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls, |
| 46 | * we mark them as obsolete now, they will be removed in a future version |
| 47 | */ |
| 48 | static long compat_ptrace_old(struct task_struct *child, long request, |
| 49 | long addr, long data) |
| 50 | { |
| 51 | int ret = -EPERM; |
| 52 | |
| 53 | switch(request) { |
| 54 | case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */ |
| 55 | int i; |
| 56 | unsigned long *reg = &((unsigned long *)child->thread.regs)[0]; |
| 57 | unsigned int __user *tmp = (unsigned int __user *)addr; |
| 58 | |
| 59 | for (i = 0; i < 32; i++) { |
| 60 | ret = put_user(*reg, tmp); |
| 61 | if (ret) |
| 62 | break; |
| 63 | reg++; |
| 64 | tmp++; |
| 65 | } |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */ |
| 70 | int i; |
| 71 | unsigned long *reg = &((unsigned long *)child->thread.regs)[0]; |
| 72 | unsigned int __user *tmp = (unsigned int __user *)addr; |
| 73 | |
| 74 | for (i = 0; i < 32; i++) { |
| 75 | ret = get_user(*reg, tmp); |
| 76 | if (ret) |
| 77 | break; |
| 78 | reg++; |
| 79 | tmp++; |
| 80 | } |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | return ret; |
| 86 | } |
| 87 | |
Paul Mackerras | 734d652 | 2005-10-31 13:57:01 +1100 | [diff] [blame] | 88 | long compat_sys_ptrace(int request, int pid, unsigned long addr, |
| 89 | unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | struct task_struct *child; |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 92 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | lock_kernel(); |
| 95 | if (request == PTRACE_TRACEME) { |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 96 | ret = ptrace_traceme(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | goto out; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 100 | child = ptrace_get_task_struct(pid); |
| 101 | if (IS_ERR(child)) { |
| 102 | ret = PTR_ERR(child); |
| 103 | goto out; |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | if (request == PTRACE_ATTACH) { |
| 107 | ret = ptrace_attach(child); |
| 108 | goto out_tsk; |
| 109 | } |
| 110 | |
| 111 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 112 | if (ret < 0) |
| 113 | goto out_tsk; |
| 114 | |
| 115 | switch (request) { |
| 116 | /* when I and D space are separate, these will need to be fixed. */ |
| 117 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
| 118 | case PTRACE_PEEKDATA: { |
| 119 | unsigned int tmp; |
| 120 | int copied; |
| 121 | |
| 122 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); |
| 123 | ret = -EIO; |
| 124 | if (copied != sizeof(tmp)) |
| 125 | break; |
| 126 | ret = put_user(tmp, (u32 __user *)data); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Read 4 bytes of the other process' storage |
| 132 | * data is a pointer specifying where the user wants the |
| 133 | * 4 bytes copied into |
| 134 | * addr is a pointer in the user's storage that contains an 8 byte |
| 135 | * address in the other process of the 4 bytes that is to be read |
| 136 | * (this is run in a 32-bit process looking at a 64-bit process) |
| 137 | * when I and D space are separate, these will need to be fixed. |
| 138 | */ |
| 139 | case PPC_PTRACE_PEEKTEXT_3264: |
| 140 | case PPC_PTRACE_PEEKDATA_3264: { |
| 141 | u32 tmp; |
| 142 | int copied; |
| 143 | u32 __user * addrOthers; |
| 144 | |
| 145 | ret = -EIO; |
| 146 | |
| 147 | /* Get the addr in the other process that we want to read */ |
| 148 | if (get_user(addrOthers, (u32 __user * __user *)addr) != 0) |
| 149 | break; |
| 150 | |
| 151 | copied = access_process_vm(child, (u64)addrOthers, &tmp, |
| 152 | sizeof(tmp), 0); |
| 153 | if (copied != sizeof(tmp)) |
| 154 | break; |
| 155 | ret = put_user(tmp, (u32 __user *)data); |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | /* Read a register (specified by ADDR) out of the "user area" */ |
| 160 | case PTRACE_PEEKUSR: { |
| 161 | int index; |
| 162 | unsigned long tmp; |
| 163 | |
| 164 | ret = -EIO; |
| 165 | /* convert to index and check */ |
| 166 | index = (unsigned long) addr >> 2; |
| 167 | if ((addr & 3) || (index > PT_FPSCR32)) |
| 168 | break; |
| 169 | |
| 170 | if (index < PT_FPR0) { |
| 171 | tmp = get_reg(child, index); |
| 172 | } else { |
| 173 | flush_fp_to_thread(child); |
| 174 | /* |
| 175 | * the user space code considers the floating point |
| 176 | * to be an array of unsigned int (32 bits) - the |
| 177 | * index passed in is based on this assumption. |
| 178 | */ |
| 179 | tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0]; |
| 180 | } |
| 181 | ret = put_user((unsigned int)tmp, (u32 __user *)data); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Read 4 bytes out of the other process' pt_regs area |
| 187 | * data is a pointer specifying where the user wants the |
| 188 | * 4 bytes copied into |
| 189 | * addr is the offset into the other process' pt_regs structure |
| 190 | * that is to be read |
| 191 | * (this is run in a 32-bit process looking at a 64-bit process) |
| 192 | */ |
| 193 | case PPC_PTRACE_PEEKUSR_3264: { |
| 194 | u32 index; |
| 195 | u32 reg32bits; |
| 196 | u64 tmp; |
| 197 | u32 numReg; |
| 198 | u32 part; |
| 199 | |
| 200 | ret = -EIO; |
| 201 | /* Determine which register the user wants */ |
| 202 | index = (u64)addr >> 2; |
| 203 | numReg = index / 2; |
| 204 | /* Determine which part of the register the user wants */ |
| 205 | if (index % 2) |
| 206 | part = 1; /* want the 2nd half of the register (right-most). */ |
| 207 | else |
| 208 | part = 0; /* want the 1st half of the register (left-most). */ |
| 209 | |
| 210 | /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */ |
| 211 | if ((addr & 3) || numReg > PT_FPSCR) |
| 212 | break; |
| 213 | |
| 214 | if (numReg >= PT_FPR0) { |
| 215 | flush_fp_to_thread(child); |
| 216 | tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0]; |
| 217 | } else { /* register within PT_REGS struct */ |
| 218 | tmp = get_reg(child, numReg); |
| 219 | } |
| 220 | reg32bits = ((u32*)&tmp)[part]; |
| 221 | ret = put_user(reg32bits, (u32 __user *)data); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | /* If I and D space are separate, this will have to be fixed. */ |
| 226 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 227 | case PTRACE_POKEDATA: { |
| 228 | unsigned int tmp; |
| 229 | tmp = data; |
| 230 | ret = 0; |
| 231 | if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) |
| 232 | == sizeof(tmp)) |
| 233 | break; |
| 234 | ret = -EIO; |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Write 4 bytes into the other process' storage |
| 240 | * data is the 4 bytes that the user wants written |
| 241 | * addr is a pointer in the user's storage that contains an |
| 242 | * 8 byte address in the other process where the 4 bytes |
| 243 | * that is to be written |
| 244 | * (this is run in a 32-bit process looking at a 64-bit process) |
| 245 | * when I and D space are separate, these will need to be fixed. |
| 246 | */ |
| 247 | case PPC_PTRACE_POKETEXT_3264: |
| 248 | case PPC_PTRACE_POKEDATA_3264: { |
| 249 | u32 tmp = data; |
| 250 | u32 __user * addrOthers; |
| 251 | |
| 252 | /* Get the addr in the other process that we want to write into */ |
| 253 | ret = -EIO; |
| 254 | if (get_user(addrOthers, (u32 __user * __user *)addr) != 0) |
| 255 | break; |
| 256 | ret = 0; |
| 257 | if (access_process_vm(child, (u64)addrOthers, &tmp, |
| 258 | sizeof(tmp), 1) == sizeof(tmp)) |
| 259 | break; |
| 260 | ret = -EIO; |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | /* write the word at location addr in the USER area */ |
| 265 | case PTRACE_POKEUSR: { |
| 266 | unsigned long index; |
| 267 | |
| 268 | ret = -EIO; |
| 269 | /* convert to index and check */ |
| 270 | index = (unsigned long) addr >> 2; |
| 271 | if ((addr & 3) || (index > PT_FPSCR32)) |
| 272 | break; |
| 273 | |
| 274 | if (index == PT_ORIG_R3) |
| 275 | break; |
| 276 | if (index < PT_FPR0) { |
| 277 | ret = put_reg(child, index, data); |
| 278 | } else { |
| 279 | flush_fp_to_thread(child); |
| 280 | /* |
| 281 | * the user space code considers the floating point |
| 282 | * to be an array of unsigned int (32 bits) - the |
| 283 | * index passed in is based on this assumption. |
| 284 | */ |
| 285 | ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data; |
| 286 | ret = 0; |
| 287 | } |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Write 4 bytes into the other process' pt_regs area |
| 293 | * data is the 4 bytes that the user wants written |
| 294 | * addr is the offset into the other process' pt_regs structure |
| 295 | * that is to be written into |
| 296 | * (this is run in a 32-bit process looking at a 64-bit process) |
| 297 | */ |
| 298 | case PPC_PTRACE_POKEUSR_3264: { |
| 299 | u32 index; |
| 300 | u32 numReg; |
| 301 | |
| 302 | ret = -EIO; |
| 303 | /* Determine which register the user wants */ |
| 304 | index = (u64)addr >> 2; |
| 305 | numReg = index / 2; |
| 306 | /* |
| 307 | * Validate the input - check to see if address is on the |
| 308 | * wrong boundary or beyond the end of the user area |
| 309 | */ |
| 310 | if ((addr & 3) || (numReg > PT_FPSCR)) |
| 311 | break; |
| 312 | /* Insure it is a register we let them change */ |
| 313 | if ((numReg == PT_ORIG_R3) |
| 314 | || ((numReg > PT_CCR) && (numReg < PT_FPR0))) |
| 315 | break; |
| 316 | if (numReg >= PT_FPR0) { |
| 317 | flush_fp_to_thread(child); |
| 318 | } |
| 319 | if (numReg == PT_MSR) |
| 320 | data = (data & MSR_DEBUGCHANGE) |
| 321 | | (child->thread.regs->msr & ~MSR_DEBUGCHANGE); |
| 322 | ((u32*)child->thread.regs)[index] = data; |
| 323 | ret = 0; |
| 324 | break; |
| 325 | } |
| 326 | |
Anton Blanchard | fd9648d | 2005-09-10 16:01:11 +1000 | [diff] [blame] | 327 | case PTRACE_GET_DEBUGREG: { |
| 328 | ret = -EINVAL; |
| 329 | /* We only support one DABR and no IABRS at the moment */ |
| 330 | if (addr > 0) |
| 331 | break; |
| 332 | ret = put_user(child->thread.dabr, (u32 __user *)data); |
| 333 | break; |
| 334 | } |
| 335 | |
Anton Blanchard | df09ce4 | 2005-09-10 16:01:09 +1000 | [diff] [blame] | 336 | case PTRACE_GETEVENTMSG: |
| 337 | ret = put_user(child->ptrace_message, (unsigned int __user *) data); |
| 338 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Benjamin Herrenschmidt | e17666b | 2007-06-04 15:15:43 +1000 | [diff] [blame^] | 340 | case PTRACE_GETREGS: { /* Get all pt_regs from the child. */ |
| 341 | int ui; |
| 342 | if (!access_ok(VERIFY_WRITE, (void __user *)data, |
| 343 | PT_REGS_COUNT * sizeof(int))) { |
| 344 | ret = -EIO; |
| 345 | break; |
| 346 | } |
| 347 | ret = 0; |
| 348 | for (ui = 0; ui < PT_REGS_COUNT; ui ++) { |
| 349 | ret |= __put_user(get_reg(child, ui), |
| 350 | (unsigned int __user *) data); |
| 351 | data += sizeof(int); |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 357 | unsigned long tmp; |
| 358 | int ui; |
| 359 | if (!access_ok(VERIFY_READ, (void __user *)data, |
| 360 | PT_REGS_COUNT * sizeof(int))) { |
| 361 | ret = -EIO; |
| 362 | break; |
| 363 | } |
| 364 | ret = 0; |
| 365 | for (ui = 0; ui < PT_REGS_COUNT; ui ++) { |
| 366 | ret = __get_user(tmp, (unsigned int __user *) data); |
| 367 | if (ret) |
| 368 | break; |
| 369 | put_reg(child, ui, tmp); |
| 370 | data += sizeof(int); |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | case PTRACE_GETFPREGS: |
| 376 | case PTRACE_SETFPREGS: |
Robert Jennings | 962bca7 | 2005-09-10 16:01:07 +1000 | [diff] [blame] | 377 | case PTRACE_GETVRREGS: |
Benjamin Herrenschmidt | e17666b | 2007-06-04 15:15:43 +1000 | [diff] [blame^] | 378 | case PTRACE_SETVRREGS: |
| 379 | case PTRACE_GETREGS64: |
| 380 | case PTRACE_SETREGS64: |
| 381 | case PPC_PTRACE_GETFPREGS: |
| 382 | case PPC_PTRACE_SETFPREGS: |
| 383 | case PTRACE_KILL: |
| 384 | case PTRACE_SINGLESTEP: |
| 385 | case PTRACE_DETACH: |
| 386 | case PTRACE_SET_DEBUGREG: |
| 387 | case PTRACE_SYSCALL: |
| 388 | case PTRACE_CONT: |
| 389 | ret = arch_ptrace(child, request, addr, data); |
Robert Jennings | 962bca7 | 2005-09-10 16:01:07 +1000 | [diff] [blame] | 390 | break; |
| 391 | |
Benjamin Herrenschmidt | e17666b | 2007-06-04 15:15:43 +1000 | [diff] [blame^] | 392 | /* Old reverse args ptrace callss */ |
| 393 | case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */ |
| 394 | case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */ |
| 395 | ret = compat_ptrace_old(child, request, addr, data); |
Robert Jennings | 962bca7 | 2005-09-10 16:01:07 +1000 | [diff] [blame] | 396 | break; |
Robert Jennings | 962bca7 | 2005-09-10 16:01:07 +1000 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | default: |
| 399 | ret = ptrace_request(child, request, addr, data); |
| 400 | break; |
| 401 | } |
| 402 | out_tsk: |
| 403 | put_task_struct(child); |
| 404 | out: |
| 405 | unlock_kernel(); |
| 406 | return ret; |
| 407 | } |