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