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