Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 6 | #include <linux/mm.h> |
| 7 | #include <linux/sched.h> |
| 8 | #include <asm/uaccess.h> |
Richard Weinberger | da028d5 | 2015-06-25 22:44:11 +0200 | [diff] [blame] | 9 | #include <asm/ptrace-abi.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 10 | #include <skas.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Karol Swietlicki | 291248f | 2008-02-04 22:30:49 -0800 | [diff] [blame] | 12 | extern int arch_switch_tls(struct task_struct *to); |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 13 | |
Karol Swietlicki | 291248f | 2008-02-04 22:30:49 -0800 | [diff] [blame] | 14 | void arch_switch_to(struct task_struct *to) |
Paolo 'Blaisorblade' Giarrusso | 972410b | 2006-03-31 02:30:21 -0800 | [diff] [blame] | 15 | { |
Karol Swietlicki | 291248f | 2008-02-04 22:30:49 -0800 | [diff] [blame] | 16 | int err = arch_switch_tls(to); |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 17 | if (!err) |
| 18 | return; |
| 19 | |
| 20 | if (err != -EINVAL) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 21 | printk(KERN_WARNING "arch_switch_tls failed, errno %d, " |
| 22 | "not EINVAL\n", -err); |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 23 | else |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 24 | printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | int is_syscall(unsigned long addr) |
| 28 | { |
| 29 | unsigned short instr; |
| 30 | int n; |
| 31 | |
| 32 | n = copy_from_user(&instr, (void __user *) addr, sizeof(instr)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 33 | if (n) { |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 34 | /* access_process_vm() grants access to vsyscall and stub, |
| 35 | * while copy_from_user doesn't. Maybe access_process_vm is |
| 36 | * slow, but that doesn't matter, since it will be called only |
| 37 | * in case of singlestepping, if copy_from_user failed. |
| 38 | */ |
| 39 | n = access_process_vm(current, addr, &instr, sizeof(instr), 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 40 | if (n != sizeof(instr)) { |
| 41 | printk(KERN_ERR "is_syscall : failed to read " |
| 42 | "instruction from 0x%lx\n", addr); |
| 43 | return 1; |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 44 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | /* int 0x80 or sysenter */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 47 | return (instr == 0x80cd) || (instr == 0x340f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* determines which flags the user has access to. */ |
| 51 | /* 1 = access 0 = no access */ |
| 52 | #define FLAG_MASK 0x00044dd5 |
| 53 | |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 54 | static const int reg_offsets[] = { |
Al Viro | 3579a38 | 2011-08-18 20:10:09 +0100 | [diff] [blame] | 55 | [EBX] = HOST_BX, |
| 56 | [ECX] = HOST_CX, |
| 57 | [EDX] = HOST_DX, |
| 58 | [ESI] = HOST_SI, |
| 59 | [EDI] = HOST_DI, |
| 60 | [EBP] = HOST_BP, |
| 61 | [EAX] = HOST_AX, |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 62 | [DS] = HOST_DS, |
| 63 | [ES] = HOST_ES, |
| 64 | [FS] = HOST_FS, |
| 65 | [GS] = HOST_GS, |
| 66 | [EIP] = HOST_IP, |
| 67 | [CS] = HOST_CS, |
| 68 | [EFL] = HOST_EFLAGS, |
| 69 | [UESP] = HOST_SP, |
| 70 | [SS] = HOST_SS, |
| 71 | }; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | int putreg(struct task_struct *child, int regno, unsigned long value) |
| 74 | { |
| 75 | regno >>= 2; |
| 76 | switch (regno) { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 77 | case EBX: |
| 78 | case ECX: |
| 79 | case EDX: |
| 80 | case ESI: |
| 81 | case EDI: |
| 82 | case EBP: |
| 83 | case EAX: |
| 84 | case EIP: |
| 85 | case UESP: |
| 86 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | case FS: |
| 88 | if (value && (value & 3) != 3) |
| 89 | return -EIO; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 90 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | case GS: |
| 92 | if (value && (value & 3) != 3) |
| 93 | return -EIO; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 94 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | case DS: |
| 96 | case ES: |
| 97 | if (value && (value & 3) != 3) |
| 98 | return -EIO; |
| 99 | value &= 0xffff; |
| 100 | break; |
| 101 | case SS: |
| 102 | case CS: |
| 103 | if ((value & 3) != 3) |
| 104 | return -EIO; |
| 105 | value &= 0xffff; |
| 106 | break; |
| 107 | case EFL: |
| 108 | value &= FLAG_MASK; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 109 | child->thread.regs.regs.gp[HOST_EFLAGS] |= value; |
| 110 | return 0; |
| 111 | case ORIG_EAX: |
| 112 | child->thread.regs.regs.syscall = value; |
| 113 | return 0; |
| 114 | default : |
| 115 | panic("Bad register in putreg() : %d\n", regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 117 | child->thread.regs.regs.gp[reg_offsets[regno]] = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 121 | int poke_user(struct task_struct *child, long addr, long data) |
| 122 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 123 | if ((addr & 3) || addr < 0) |
| 124 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 125 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 126 | if (addr < MAX_REG_OFFSET) |
| 127 | return putreg(child, addr, data); |
| 128 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 129 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
| 130 | addr -= offsetof(struct user, u_debugreg[0]); |
| 131 | addr = addr >> 2; |
| 132 | if ((addr == 4) || (addr == 5)) |
| 133 | return -EIO; |
| 134 | child->thread.arch.debugregs[addr] = data; |
| 135 | return 0; |
| 136 | } |
| 137 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | unsigned long getreg(struct task_struct *child, int regno) |
| 141 | { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 142 | unsigned long mask = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | regno >>= 2; |
| 145 | switch (regno) { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 146 | case ORIG_EAX: |
| 147 | return child->thread.regs.regs.syscall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | case FS: |
| 149 | case GS: |
| 150 | case DS: |
| 151 | case ES: |
| 152 | case SS: |
| 153 | case CS: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 154 | mask = 0xffff; |
| 155 | break; |
| 156 | case EIP: |
| 157 | case UESP: |
| 158 | case EAX: |
| 159 | case EBX: |
| 160 | case ECX: |
| 161 | case EDX: |
| 162 | case ESI: |
| 163 | case EDI: |
| 164 | case EBP: |
| 165 | case EFL: |
| 166 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | default: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 168 | panic("Bad register in getreg() : %d\n", regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 170 | return mask & child->thread.regs.regs.gp[reg_offsets[regno]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 173 | /* read the word at location addr in the USER area. */ |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 174 | int peek_user(struct task_struct *child, long addr, long data) |
| 175 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 176 | unsigned long tmp; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 177 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 178 | if ((addr & 3) || addr < 0) |
| 179 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 180 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 181 | tmp = 0; /* Default return condition */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 182 | if (addr < MAX_REG_OFFSET) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 183 | tmp = getreg(child, addr); |
| 184 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 185 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 186 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 187 | addr -= offsetof(struct user, u_debugreg[0]); |
| 188 | addr = addr >> 2; |
| 189 | tmp = child->thread.arch.debugregs[addr]; |
| 190 | } |
| 191 | return put_user(tmp, (unsigned long __user *) data); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 194 | 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] | 195 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 196 | int err, n, cpu = ((struct thread_info *) child->stack)->cpu; |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 197 | struct user_i387_struct fpregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 199 | err = save_fp_registers(userspace_pid[cpu], (unsigned long *) &fpregs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 200 | if (err) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 201 | return err; |
| 202 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 203 | n = copy_to_user(buf, &fpregs, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 204 | if(n > 0) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 205 | return -EFAULT; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 206 | |
| 207 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 210 | 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] | 211 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 212 | int n, cpu = ((struct thread_info *) child->stack)->cpu; |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 213 | struct user_i387_struct fpregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 215 | n = copy_from_user(&fpregs, buf, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 216 | if (n > 0) |
| 217 | return -EFAULT; |
| 218 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 219 | return restore_fp_registers(userspace_pid[cpu], |
| 220 | (unsigned long *) &fpregs); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 223 | static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 224 | { |
| 225 | int err, n, cpu = ((struct thread_info *) child->stack)->cpu; |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 226 | struct user_fxsr_struct fpregs; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 227 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 228 | err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 229 | if (err) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 230 | return err; |
| 231 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 232 | n = copy_to_user(buf, &fpregs, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 233 | if(n > 0) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 234 | return -EFAULT; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 235 | |
| 236 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 239 | static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 241 | int n, cpu = ((struct thread_info *) child->stack)->cpu; |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 242 | struct user_fxsr_struct fpregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [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 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 248 | return restore_fpx_registers(userspace_pid[cpu], |
| 249 | (unsigned long *) &fpregs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 252 | long subarch_ptrace(struct task_struct *child, long request, |
| 253 | unsigned long addr, unsigned long data) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 254 | { |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 255 | int ret = -EIO; |
| 256 | void __user *datap = (void __user *) data; |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 257 | switch (request) { |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 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; |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 264 | case PTRACE_GETFPXREGS: /* Get the child FPU state. */ |
| 265 | ret = get_fpxregs(datap, child); |
| 266 | break; |
| 267 | case PTRACE_SETFPXREGS: /* Set the child FPU state. */ |
| 268 | ret = set_fpxregs(datap, child); |
| 269 | break; |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 270 | default: |
| 271 | ret = -EIO; |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 272 | } |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 273 | return ret; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 274 | } |