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