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; |
| 208 | compat_siginfo_t *si32 = (compat_siginfo_t *)compat_ptr(data); |
Andi Kleen | 2c87e2c | 2006-07-10 17:06:24 +0200 | [diff] [blame] | 209 | siginfo_t ssi; |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 210 | siginfo_t *si = compat_alloc_user_space(sizeof(siginfo_t)); |
| 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: |
| 246 | case PTRACE_SETOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return sys_ptrace(request, pid, addr, data); |
| 248 | |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 249 | default: |
| 250 | return -EINVAL; |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | case PTRACE_PEEKTEXT: |
| 253 | case PTRACE_PEEKDATA: |
| 254 | case PTRACE_POKEDATA: |
| 255 | case PTRACE_POKETEXT: |
| 256 | case PTRACE_POKEUSR: |
| 257 | case PTRACE_PEEKUSR: |
| 258 | case PTRACE_GETREGS: |
| 259 | case PTRACE_SETREGS: |
| 260 | case PTRACE_SETFPREGS: |
| 261 | case PTRACE_GETFPREGS: |
| 262 | case PTRACE_SETFPXREGS: |
| 263 | case PTRACE_GETFPXREGS: |
| 264 | case PTRACE_GETEVENTMSG: |
| 265 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 267 | case PTRACE_SETSIGINFO: |
| 268 | case PTRACE_GETSIGINFO: |
| 269 | return ptrace32_siginfo(request, pid, addr, data); |
| 270 | } |
Christoph Hellwig | 6b9c7ed8 | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 271 | |
| 272 | child = ptrace_get_task_struct(pid); |
| 273 | if (IS_ERR(child)) |
| 274 | return PTR_ERR(child); |
| 275 | |
| 276 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 277 | if (ret < 0) |
| 278 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 280 | childregs = task_pt_regs(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | switch (request) { |
| 283 | case PTRACE_PEEKDATA: |
| 284 | case PTRACE_PEEKTEXT: |
| 285 | ret = 0; |
| 286 | if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32)) |
| 287 | ret = -EIO; |
| 288 | else |
| 289 | ret = put_user(val, (unsigned int __user *)datap); |
| 290 | break; |
| 291 | |
| 292 | case PTRACE_POKEDATA: |
| 293 | case PTRACE_POKETEXT: |
| 294 | ret = 0; |
| 295 | if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32)) |
| 296 | ret = -EIO; |
| 297 | break; |
| 298 | |
| 299 | case PTRACE_PEEKUSR: |
| 300 | ret = getreg32(child, addr, &val); |
| 301 | if (ret == 0) |
| 302 | ret = put_user(val, (__u32 __user *)datap); |
| 303 | break; |
| 304 | |
| 305 | case PTRACE_POKEUSR: |
| 306 | ret = putreg32(child, addr, data); |
| 307 | break; |
| 308 | |
| 309 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
| 310 | int i; |
| 311 | if (!access_ok(VERIFY_WRITE, datap, 16*4)) { |
| 312 | ret = -EIO; |
| 313 | break; |
| 314 | } |
| 315 | ret = 0; |
| 316 | for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) { |
| 317 | getreg32(child, i, &val); |
| 318 | ret |= __put_user(val,(u32 __user *)datap); |
| 319 | datap += sizeof(u32); |
| 320 | } |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 325 | unsigned long tmp; |
| 326 | int i; |
| 327 | if (!access_ok(VERIFY_READ, datap, 16*4)) { |
| 328 | ret = -EIO; |
| 329 | break; |
| 330 | } |
| 331 | ret = 0; |
| 332 | for ( i = 0; i <= 16*4; i += sizeof(u32) ) { |
| 333 | ret |= __get_user(tmp, (u32 __user *)datap); |
| 334 | putreg32(child, i, tmp); |
| 335 | datap += sizeof(u32); |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | case PTRACE_GETFPREGS: |
| 341 | ret = -EIO; |
| 342 | if (!access_ok(VERIFY_READ, compat_ptr(data), |
| 343 | sizeof(struct user_i387_struct))) |
| 344 | break; |
| 345 | save_i387_ia32(child, datap, childregs, 1); |
| 346 | ret = 0; |
| 347 | break; |
| 348 | |
| 349 | case PTRACE_SETFPREGS: |
| 350 | ret = -EIO; |
| 351 | if (!access_ok(VERIFY_WRITE, datap, |
| 352 | sizeof(struct user_i387_struct))) |
| 353 | break; |
| 354 | ret = 0; |
| 355 | /* don't check EFAULT to be bug-to-bug compatible to i386 */ |
| 356 | restore_i387_ia32(child, datap, 1); |
| 357 | break; |
| 358 | |
| 359 | case PTRACE_GETFPXREGS: { |
| 360 | struct user32_fxsr_struct __user *u = datap; |
| 361 | init_fpu(child); |
| 362 | ret = -EIO; |
| 363 | if (!access_ok(VERIFY_WRITE, u, sizeof(*u))) |
| 364 | break; |
| 365 | ret = -EFAULT; |
| 366 | if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u))) |
| 367 | break; |
| 368 | ret = __put_user(childregs->cs, &u->fcs); |
| 369 | ret |= __put_user(child->thread.ds, &u->fos); |
| 370 | break; |
| 371 | } |
| 372 | case PTRACE_SETFPXREGS: { |
| 373 | struct user32_fxsr_struct __user *u = datap; |
| 374 | unlazy_fpu(child); |
| 375 | ret = -EIO; |
| 376 | if (!access_ok(VERIFY_READ, u, sizeof(*u))) |
| 377 | break; |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 378 | /* no checking to be bug-to-bug compatible with i386. */ |
| 379 | /* but silence warning */ |
| 380 | if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u))) |
| 381 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | set_stopped_child_used_math(child); |
| 383 | child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask; |
| 384 | ret = 0; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | case PTRACE_GETEVENTMSG: |
| 389 | ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data)); |
| 390 | break; |
| 391 | |
| 392 | default: |
Andi Kleen | f0f2d65 | 2006-06-26 13:56:34 +0200 | [diff] [blame] | 393 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Christoph Hellwig | 6b9c7ed8 | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 396 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | put_task_struct(child); |
| 398 | return ret; |
| 399 | } |
| 400 | |