Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * 32bit ptrace for x86-64. |
| 3 | * |
| 4 | * Copyright 2001,2002 Andi Kleen, SuSE Labs. |
| 5 | * Some parts copied from arch/i386/kernel/ptrace.c. See that file for earlier |
| 6 | * copyright. |
| 7 | * |
| 8 | * This allows to access 64bit processes too; but there is no way to see the extended |
| 9 | * register contents. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/stddef.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/syscalls.h> |
| 16 | #include <linux/unistd.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <asm/ptrace.h> |
| 20 | #include <asm/compat.h> |
| 21 | #include <asm/uaccess.h> |
| 22 | #include <asm/user32.h> |
| 23 | #include <asm/user.h> |
| 24 | #include <asm/errno.h> |
| 25 | #include <asm/debugreg.h> |
| 26 | #include <asm/i387.h> |
| 27 | #include <asm/fpu32.h> |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 28 | #include <asm/ia32.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Chuck Ebbert | 60923df | 2006-01-11 22:46:03 +0100 | [diff] [blame] | 30 | /* |
| 31 | * Determines which flags the user has access to [1 = access, 0 = no access]. |
| 32 | * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9). |
| 33 | * Also masks reserved bits (31-22, 15, 5, 3, 1). |
| 34 | */ |
| 35 | #define FLAG_MASK 0x54dd5UL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #define R32(l,q) \ |
| 38 | case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break |
| 39 | |
| 40 | static int putreg32(struct task_struct *child, unsigned regno, u32 val) |
| 41 | { |
| 42 | int i; |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 43 | __u64 *stack = (__u64 *)task_pt_regs(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | switch (regno) { |
| 46 | case offsetof(struct user32, regs.fs): |
| 47 | if (val && (val & 3) != 3) return -EIO; |
Daniel Jacobowitz | e8ed11b | 2005-08-04 13:41:09 -0700 | [diff] [blame] | 48 | child->thread.fsindex = val & 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | break; |
| 50 | case offsetof(struct user32, regs.gs): |
| 51 | if (val && (val & 3) != 3) return -EIO; |
Daniel Jacobowitz | e8ed11b | 2005-08-04 13:41:09 -0700 | [diff] [blame] | 52 | child->thread.gsindex = val & 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | break; |
| 54 | case offsetof(struct user32, regs.ds): |
| 55 | if (val && (val & 3) != 3) return -EIO; |
| 56 | child->thread.ds = val & 0xffff; |
| 57 | break; |
| 58 | case offsetof(struct user32, regs.es): |
| 59 | child->thread.es = val & 0xffff; |
| 60 | break; |
| 61 | case offsetof(struct user32, regs.ss): |
| 62 | if ((val & 3) != 3) return -EIO; |
| 63 | stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff; |
| 64 | break; |
| 65 | case offsetof(struct user32, regs.cs): |
| 66 | if ((val & 3) != 3) return -EIO; |
| 67 | stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff; |
| 68 | break; |
| 69 | |
| 70 | R32(ebx, rbx); |
| 71 | R32(ecx, rcx); |
| 72 | R32(edx, rdx); |
| 73 | R32(edi, rdi); |
| 74 | R32(esi, rsi); |
| 75 | R32(ebp, rbp); |
| 76 | R32(eax, rax); |
| 77 | R32(orig_eax, orig_rax); |
| 78 | R32(eip, rip); |
| 79 | R32(esp, rsp); |
| 80 | |
| 81 | case offsetof(struct user32, regs.eflags): { |
| 82 | __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8]; |
| 83 | val &= FLAG_MASK; |
| 84 | *flags = val | (*flags & ~FLAG_MASK); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | case offsetof(struct user32, u_debugreg[4]): |
| 89 | case offsetof(struct user32, u_debugreg[5]): |
| 90 | return -EIO; |
| 91 | |
| 92 | case offsetof(struct user32, u_debugreg[0]): |
| 93 | child->thread.debugreg0 = val; |
| 94 | break; |
| 95 | |
| 96 | case offsetof(struct user32, u_debugreg[1]): |
| 97 | child->thread.debugreg1 = val; |
| 98 | break; |
| 99 | |
| 100 | case offsetof(struct user32, u_debugreg[2]): |
| 101 | child->thread.debugreg2 = val; |
| 102 | break; |
| 103 | |
| 104 | case offsetof(struct user32, u_debugreg[3]): |
| 105 | child->thread.debugreg3 = val; |
| 106 | break; |
| 107 | |
| 108 | case offsetof(struct user32, u_debugreg[6]): |
| 109 | child->thread.debugreg6 = val; |
| 110 | break; |
| 111 | |
| 112 | case offsetof(struct user32, u_debugreg[7]): |
| 113 | val &= ~DR_CONTROL_RESERVED; |
| 114 | /* See arch/i386/kernel/ptrace.c for an explanation of |
| 115 | * this awkward check.*/ |
| 116 | for(i=0; i<4; i++) |
| 117 | if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1) |
| 118 | return -EIO; |
| 119 | child->thread.debugreg7 = val; |
Stephane Eranian | d3a4f48 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 120 | if (val) |
| 121 | set_tsk_thread_flag(child, TIF_DEBUG); |
| 122 | else |
| 123 | clear_tsk_thread_flag(child, TIF_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | break; |
| 125 | |
| 126 | default: |
| 127 | if (regno > sizeof(struct user32) || (regno & 3)) |
| 128 | return -EIO; |
| 129 | |
| 130 | /* Other dummy fields in the virtual user structure are ignored */ |
| 131 | break; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | #undef R32 |
| 137 | |
| 138 | #define R32(l,q) \ |
| 139 | case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break |
| 140 | |
| 141 | static int getreg32(struct task_struct *child, unsigned regno, u32 *val) |
| 142 | { |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 143 | __u64 *stack = (__u64 *)task_pt_regs(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | switch (regno) { |
| 146 | case offsetof(struct user32, regs.fs): |
Daniel Jacobowitz | e8ed11b | 2005-08-04 13:41:09 -0700 | [diff] [blame] | 147 | *val = child->thread.fsindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | break; |
| 149 | case offsetof(struct user32, regs.gs): |
Daniel Jacobowitz | e8ed11b | 2005-08-04 13:41:09 -0700 | [diff] [blame] | 150 | *val = child->thread.gsindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | break; |
| 152 | case offsetof(struct user32, regs.ds): |
| 153 | *val = child->thread.ds; |
| 154 | break; |
| 155 | case offsetof(struct user32, regs.es): |
| 156 | *val = child->thread.es; |
| 157 | break; |
| 158 | |
| 159 | R32(cs, cs); |
| 160 | R32(ss, ss); |
| 161 | R32(ebx, rbx); |
| 162 | R32(ecx, rcx); |
| 163 | R32(edx, rdx); |
| 164 | R32(edi, rdi); |
| 165 | R32(esi, rsi); |
| 166 | R32(ebp, rbp); |
| 167 | R32(eax, rax); |
| 168 | R32(orig_eax, orig_rax); |
| 169 | R32(eip, rip); |
| 170 | R32(eflags, eflags); |
| 171 | R32(esp, rsp); |
| 172 | |
| 173 | case offsetof(struct user32, u_debugreg[0]): |
| 174 | *val = child->thread.debugreg0; |
| 175 | break; |
| 176 | case offsetof(struct user32, u_debugreg[1]): |
| 177 | *val = child->thread.debugreg1; |
| 178 | break; |
| 179 | case offsetof(struct user32, u_debugreg[2]): |
| 180 | *val = child->thread.debugreg2; |
| 181 | break; |
| 182 | case offsetof(struct user32, u_debugreg[3]): |
| 183 | *val = child->thread.debugreg3; |
| 184 | break; |
| 185 | case offsetof(struct user32, u_debugreg[6]): |
| 186 | *val = child->thread.debugreg6; |
| 187 | break; |
| 188 | case offsetof(struct user32, u_debugreg[7]): |
| 189 | *val = child->thread.debugreg7; |
| 190 | break; |
| 191 | |
| 192 | default: |
| 193 | if (regno > sizeof(struct user32) || (regno & 3)) |
| 194 | return -EIO; |
| 195 | |
| 196 | /* Other dummy fields in the virtual user structure are ignored */ |
| 197 | *val = 0; |
| 198 | break; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | #undef R32 |
| 204 | |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 205 | static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data) |
| 206 | { |
| 207 | int ret; |
Al Viro | 86f9333 | 2006-10-10 22:47:37 +0100 | [diff] [blame] | 208 | compat_siginfo_t __user *si32 = compat_ptr(data); |
Andi Kleen | 2c87e2c | 2006-07-10 17:06:24 +0200 | [diff] [blame] | 209 | siginfo_t ssi; |
Al Viro | 86f9333 | 2006-10-10 22:47:37 +0100 | [diff] [blame] | 210 | siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t)); |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 211 | if (request == PTRACE_SETSIGINFO) { |
Andi Kleen | 2c87e2c | 2006-07-10 17:06:24 +0200 | [diff] [blame] | 212 | memset(&ssi, 0, sizeof(siginfo_t)); |
| 213 | ret = copy_siginfo_from_user32(&ssi, si32); |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 214 | if (ret) |
| 215 | return ret; |
Andi Kleen | 2c87e2c | 2006-07-10 17:06:24 +0200 | [diff] [blame] | 216 | if (copy_to_user(si, &ssi, sizeof(siginfo_t))) |
| 217 | return -EFAULT; |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 218 | } |
| 219 | ret = sys_ptrace(request, pid, addr, (unsigned long)si); |
| 220 | if (ret) |
| 221 | return ret; |
Andi Kleen | 2c87e2c | 2006-07-10 17:06:24 +0200 | [diff] [blame] | 222 | if (request == PTRACE_GETSIGINFO) { |
| 223 | if (copy_from_user(&ssi, si, sizeof(siginfo_t))) |
| 224 | return -EFAULT; |
| 225 | ret = copy_siginfo_to_user32(si32, &ssi); |
| 226 | } |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 227 | return ret; |
| 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data) |
| 231 | { |
| 232 | struct task_struct *child; |
| 233 | struct pt_regs *childregs; |
| 234 | void __user *datap = compat_ptr(data); |
| 235 | int ret; |
| 236 | __u32 val; |
| 237 | |
| 238 | switch (request) { |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 239 | case PTRACE_TRACEME: |
| 240 | case PTRACE_ATTACH: |
| 241 | case PTRACE_KILL: |
| 242 | case PTRACE_CONT: |
| 243 | case PTRACE_SINGLESTEP: |
| 244 | case PTRACE_DETACH: |
| 245 | case PTRACE_SYSCALL: |
Paolo 'Blaisorblade' Giarrusso | 47c51df | 2007-03-07 20:41:17 -0800 | [diff] [blame] | 246 | case PTRACE_OLDSETOPTIONS: |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 247 | case PTRACE_SETOPTIONS: |
Andi Kleen | 14679eb | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 248 | case PTRACE_SET_THREAD_AREA: |
| 249 | case PTRACE_GET_THREAD_AREA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return sys_ptrace(request, pid, addr, data); |
| 251 | |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 252 | default: |
| 253 | return -EINVAL; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | case PTRACE_PEEKTEXT: |
| 256 | case PTRACE_PEEKDATA: |
| 257 | case PTRACE_POKEDATA: |
| 258 | case PTRACE_POKETEXT: |
| 259 | case PTRACE_POKEUSR: |
| 260 | case PTRACE_PEEKUSR: |
| 261 | case PTRACE_GETREGS: |
| 262 | case PTRACE_SETREGS: |
| 263 | case PTRACE_SETFPREGS: |
| 264 | case PTRACE_GETFPREGS: |
| 265 | case PTRACE_SETFPXREGS: |
| 266 | case PTRACE_GETFPXREGS: |
| 267 | case PTRACE_GETEVENTMSG: |
| 268 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 270 | case PTRACE_SETSIGINFO: |
| 271 | case PTRACE_GETSIGINFO: |
| 272 | return ptrace32_siginfo(request, pid, addr, data); |
| 273 | } |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 274 | |
| 275 | child = ptrace_get_task_struct(pid); |
| 276 | if (IS_ERR(child)) |
| 277 | return PTR_ERR(child); |
| 278 | |
| 279 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 280 | if (ret < 0) |
| 281 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 283 | childregs = task_pt_regs(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | switch (request) { |
| 286 | case PTRACE_PEEKDATA: |
| 287 | case PTRACE_PEEKTEXT: |
| 288 | ret = 0; |
| 289 | if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32)) |
| 290 | ret = -EIO; |
| 291 | else |
| 292 | ret = put_user(val, (unsigned int __user *)datap); |
| 293 | break; |
| 294 | |
| 295 | case PTRACE_POKEDATA: |
| 296 | case PTRACE_POKETEXT: |
| 297 | ret = 0; |
| 298 | if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32)) |
| 299 | ret = -EIO; |
| 300 | break; |
| 301 | |
| 302 | case PTRACE_PEEKUSR: |
| 303 | ret = getreg32(child, addr, &val); |
| 304 | if (ret == 0) |
| 305 | ret = put_user(val, (__u32 __user *)datap); |
| 306 | break; |
| 307 | |
| 308 | case PTRACE_POKEUSR: |
| 309 | ret = putreg32(child, addr, data); |
| 310 | break; |
| 311 | |
| 312 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
| 313 | int i; |
| 314 | if (!access_ok(VERIFY_WRITE, datap, 16*4)) { |
| 315 | ret = -EIO; |
| 316 | break; |
| 317 | } |
| 318 | ret = 0; |
| 319 | for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) { |
| 320 | getreg32(child, i, &val); |
| 321 | ret |= __put_user(val,(u32 __user *)datap); |
| 322 | datap += sizeof(u32); |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 328 | unsigned long tmp; |
| 329 | int i; |
| 330 | if (!access_ok(VERIFY_READ, datap, 16*4)) { |
| 331 | ret = -EIO; |
| 332 | break; |
| 333 | } |
| 334 | ret = 0; |
| 335 | for ( i = 0; i <= 16*4; i += sizeof(u32) ) { |
| 336 | ret |= __get_user(tmp, (u32 __user *)datap); |
| 337 | putreg32(child, i, tmp); |
| 338 | datap += sizeof(u32); |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | case PTRACE_GETFPREGS: |
| 344 | ret = -EIO; |
| 345 | if (!access_ok(VERIFY_READ, compat_ptr(data), |
| 346 | sizeof(struct user_i387_struct))) |
| 347 | break; |
| 348 | save_i387_ia32(child, datap, childregs, 1); |
| 349 | ret = 0; |
| 350 | break; |
| 351 | |
| 352 | case PTRACE_SETFPREGS: |
| 353 | ret = -EIO; |
| 354 | if (!access_ok(VERIFY_WRITE, datap, |
| 355 | sizeof(struct user_i387_struct))) |
| 356 | break; |
| 357 | ret = 0; |
| 358 | /* don't check EFAULT to be bug-to-bug compatible to i386 */ |
| 359 | restore_i387_ia32(child, datap, 1); |
| 360 | break; |
| 361 | |
| 362 | case PTRACE_GETFPXREGS: { |
| 363 | struct user32_fxsr_struct __user *u = datap; |
| 364 | init_fpu(child); |
| 365 | ret = -EIO; |
| 366 | if (!access_ok(VERIFY_WRITE, u, sizeof(*u))) |
| 367 | break; |
| 368 | ret = -EFAULT; |
| 369 | if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u))) |
| 370 | break; |
| 371 | ret = __put_user(childregs->cs, &u->fcs); |
| 372 | ret |= __put_user(child->thread.ds, &u->fos); |
| 373 | break; |
| 374 | } |
| 375 | case PTRACE_SETFPXREGS: { |
| 376 | struct user32_fxsr_struct __user *u = datap; |
| 377 | unlazy_fpu(child); |
| 378 | ret = -EIO; |
| 379 | if (!access_ok(VERIFY_READ, u, sizeof(*u))) |
| 380 | break; |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 381 | /* no checking to be bug-to-bug compatible with i386. */ |
| 382 | /* but silence warning */ |
| 383 | if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u))) |
| 384 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | set_stopped_child_used_math(child); |
| 386 | child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask; |
| 387 | ret = 0; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | case PTRACE_GETEVENTMSG: |
| 392 | ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data)); |
| 393 | break; |
| 394 | |
| 395 | default: |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 396 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 399 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | put_task_struct(child); |
| 401 | return ret; |
| 402 | } |
| 403 | |