Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003 PathScale, Inc. |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 3 | * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the GPL |
| 6 | */ |
| 7 | |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 8 | #include <linux/mm.h> |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
| 10 | #include <linux/errno.h> |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 11 | #define __FRAME_OFFSETS |
| 12 | #include <asm/ptrace.h> |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 15 | /* |
| 16 | * determines which flags the user has access to. |
| 17 | * 1 = access 0 = no access |
| 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define FLAG_MASK 0x44dd5UL |
| 20 | |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 21 | static const int reg_offsets[] = |
| 22 | { |
| 23 | [R8 >> 3] = HOST_R8, |
| 24 | [R9 >> 3] = HOST_R9, |
| 25 | [R10 >> 3] = HOST_R10, |
| 26 | [R11 >> 3] = HOST_R11, |
| 27 | [R12 >> 3] = HOST_R12, |
| 28 | [R13 >> 3] = HOST_R13, |
| 29 | [R14 >> 3] = HOST_R14, |
| 30 | [R15 >> 3] = HOST_R15, |
| 31 | [RIP >> 3] = HOST_IP, |
| 32 | [RSP >> 3] = HOST_SP, |
Al Viro | 3579a38 | 2011-08-18 20:10:09 +0100 | [diff] [blame] | 33 | [RAX >> 3] = HOST_AX, |
| 34 | [RBX >> 3] = HOST_BX, |
| 35 | [RCX >> 3] = HOST_CX, |
| 36 | [RDX >> 3] = HOST_DX, |
| 37 | [RSI >> 3] = HOST_SI, |
| 38 | [RDI >> 3] = HOST_DI, |
| 39 | [RBP >> 3] = HOST_BP, |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 40 | [CS >> 3] = HOST_CS, |
| 41 | [SS >> 3] = HOST_SS, |
| 42 | [FS_BASE >> 3] = HOST_FS_BASE, |
| 43 | [GS_BASE >> 3] = HOST_GS_BASE, |
| 44 | [DS >> 3] = HOST_DS, |
| 45 | [ES >> 3] = HOST_ES, |
| 46 | [FS >> 3] = HOST_FS, |
| 47 | [GS >> 3] = HOST_GS, |
| 48 | [EFLAGS >> 3] = HOST_EFLAGS, |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 49 | [ORIG_RAX >> 3] = HOST_ORIG_AX, |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int putreg(struct task_struct *child, int regno, unsigned long value) |
| 53 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #ifdef TIF_IA32 |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 55 | /* |
| 56 | * Some code in the 64bit emulation may not be 64bit clean. |
| 57 | * Don't take any chances. |
| 58 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (test_tsk_thread_flag(child, TIF_IA32)) |
| 60 | value &= 0xffffffff; |
| 61 | #endif |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 62 | switch (regno) { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 63 | case R8: |
| 64 | case R9: |
| 65 | case R10: |
| 66 | case R11: |
| 67 | case R12: |
| 68 | case R13: |
| 69 | case R14: |
| 70 | case R15: |
| 71 | case RIP: |
| 72 | case RSP: |
| 73 | case RAX: |
| 74 | case RBX: |
| 75 | case RCX: |
| 76 | case RDX: |
| 77 | case RSI: |
| 78 | case RDI: |
| 79 | case RBP: |
| 80 | case ORIG_RAX: |
| 81 | break; |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | case FS: |
| 84 | case GS: |
| 85 | case DS: |
| 86 | case ES: |
| 87 | case SS: |
| 88 | case CS: |
| 89 | if (value && (value & 3) != 3) |
| 90 | return -EIO; |
| 91 | value &= 0xffff; |
| 92 | break; |
| 93 | |
| 94 | case FS_BASE: |
| 95 | case GS_BASE: |
| 96 | if (!((value >> 48) == 0 || (value >> 48) == 0xffff)) |
| 97 | return -EIO; |
| 98 | break; |
| 99 | |
| 100 | case EFLAGS: |
| 101 | value &= FLAG_MASK; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 102 | child->thread.regs.regs.gp[HOST_EFLAGS] |= value; |
| 103 | return 0; |
| 104 | |
| 105 | default: |
| 106 | panic("Bad register in putreg(): %d\n", regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 109 | child->thread.regs.regs.gp[reg_offsets[regno >> 3]] = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 113 | int poke_user(struct task_struct *child, long addr, long data) |
| 114 | { |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 115 | if ((addr & 3) || addr < 0) |
| 116 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 117 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 118 | if (addr < MAX_REG_OFFSET) |
| 119 | return putreg(child, addr, data); |
| 120 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 121 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 122 | addr -= offsetof(struct user, u_debugreg[0]); |
| 123 | addr = addr >> 2; |
| 124 | if ((addr == 4) || (addr == 5)) |
| 125 | return -EIO; |
| 126 | child->thread.arch.debugregs[addr] = data; |
| 127 | return 0; |
| 128 | } |
| 129 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | unsigned long getreg(struct task_struct *child, int regno) |
| 133 | { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 134 | unsigned long mask = ~0UL; |
| 135 | #ifdef TIF_IA32 |
| 136 | if (test_tsk_thread_flag(child, TIF_IA32)) |
| 137 | mask = 0xffffffff; |
| 138 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | switch (regno) { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 140 | case R8: |
| 141 | case R9: |
| 142 | case R10: |
| 143 | case R11: |
| 144 | case R12: |
| 145 | case R13: |
| 146 | case R14: |
| 147 | case R15: |
| 148 | case RIP: |
| 149 | case RSP: |
| 150 | case RAX: |
| 151 | case RBX: |
| 152 | case RCX: |
| 153 | case RDX: |
| 154 | case RSI: |
| 155 | case RDI: |
| 156 | case RBP: |
| 157 | case ORIG_RAX: |
| 158 | case EFLAGS: |
| 159 | case FS_BASE: |
| 160 | case GS_BASE: |
| 161 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | case FS: |
| 163 | case GS: |
| 164 | case DS: |
| 165 | case ES: |
| 166 | case SS: |
| 167 | case CS: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 168 | mask = 0xffff; |
| 169 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | default: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 171 | panic("Bad register in getreg: %d\n", regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 173 | return mask & child->thread.regs.regs.gp[reg_offsets[regno >> 3]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 176 | int peek_user(struct task_struct *child, long addr, long data) |
| 177 | { |
| 178 | /* read the word at location addr in the USER area. */ |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 179 | unsigned long tmp; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 180 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 181 | if ((addr & 3) || addr < 0) |
| 182 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 183 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 184 | tmp = 0; /* Default return condition */ |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 185 | if (addr < MAX_REG_OFFSET) |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 186 | tmp = getreg(child, addr); |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 187 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 188 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 189 | addr -= offsetof(struct user, u_debugreg[0]); |
| 190 | addr = addr >> 2; |
| 191 | tmp = child->thread.arch.debugregs[addr]; |
| 192 | } |
| 193 | return put_user(tmp, (unsigned long *) data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 196 | /* XXX Mostly copied from sys-i386 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | int is_syscall(unsigned long addr) |
| 198 | { |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 199 | unsigned short instr; |
| 200 | int n; |
| 201 | |
| 202 | n = copy_from_user(&instr, (void __user *) addr, sizeof(instr)); |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 203 | if (n) { |
| 204 | /* |
| 205 | * access_process_vm() grants access to vsyscall and stub, |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 206 | * while copy_from_user doesn't. Maybe access_process_vm is |
| 207 | * slow, but that doesn't matter, since it will be called only |
| 208 | * in case of singlestepping, if copy_from_user failed. |
| 209 | */ |
| 210 | n = access_process_vm(current, addr, &instr, sizeof(instr), 0); |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 211 | if (n != sizeof(instr)) { |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 212 | printk("is_syscall : failed to read instruction from " |
| 213 | "0x%lx\n", addr); |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 214 | return 1; |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | /* sysenter */ |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame] | 218 | return instr == 0x050f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 221 | static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 223 | int err, n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 224 | long fpregs[HOST_FP_SIZE]; |
| 225 | |
| 226 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
| 227 | err = save_fp_registers(userspace_pid[cpu], fpregs); |
| 228 | if (err) |
| 229 | return err; |
| 230 | |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 231 | n = copy_to_user(buf, fpregs, sizeof(fpregs)); |
Jeff Dike | 95906b2 | 2008-02-04 22:31:20 -0800 | [diff] [blame] | 232 | if (n > 0) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 233 | return -EFAULT; |
| 234 | |
| 235 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 238 | static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 240 | int n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 241 | long fpregs[HOST_FP_SIZE]; |
| 242 | |
| 243 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 244 | n = copy_from_user(fpregs, buf, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 245 | if (n > 0) |
| 246 | return -EFAULT; |
| 247 | |
| 248 | return restore_fp_registers(userspace_pid[cpu], fpregs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 251 | long subarch_ptrace(struct task_struct *child, long request, |
| 252 | unsigned long addr, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 254 | int ret = -EIO; |
| 255 | void __user *datap = (void __user *) data; |
| 256 | |
| 257 | switch (request) { |
| 258 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 259 | ret = get_fpregs(datap, child); |
| 260 | break; |
| 261 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 262 | ret = set_fpregs(datap, child); |
| 263 | break; |
| 264 | case PTRACE_ARCH_PRCTL: |
| 265 | /* XXX Calls ptrace on the host - needs some SMP thinking */ |
| 266 | ret = arch_prctl(child, data, (void __user *) addr); |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |