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 | */ |
Lorenzo Stoakes | f307ab6 | 2016-10-13 01:20:20 +0100 | [diff] [blame] | 39 | n = access_process_vm(current, addr, &instr, sizeof(instr), |
| 40 | FOLL_FORCE); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 41 | if (n != sizeof(instr)) { |
| 42 | printk(KERN_ERR "is_syscall : failed to read " |
| 43 | "instruction from 0x%lx\n", addr); |
| 44 | return 1; |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 45 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | /* int 0x80 or sysenter */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 48 | return (instr == 0x80cd) || (instr == 0x340f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* determines which flags the user has access to. */ |
| 52 | /* 1 = access 0 = no access */ |
| 53 | #define FLAG_MASK 0x00044dd5 |
| 54 | |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 55 | static const int reg_offsets[] = { |
Al Viro | 3579a38 | 2011-08-18 20:10:09 +0100 | [diff] [blame] | 56 | [EBX] = HOST_BX, |
| 57 | [ECX] = HOST_CX, |
| 58 | [EDX] = HOST_DX, |
| 59 | [ESI] = HOST_SI, |
| 60 | [EDI] = HOST_DI, |
| 61 | [EBP] = HOST_BP, |
| 62 | [EAX] = HOST_AX, |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 63 | [DS] = HOST_DS, |
| 64 | [ES] = HOST_ES, |
| 65 | [FS] = HOST_FS, |
| 66 | [GS] = HOST_GS, |
| 67 | [EIP] = HOST_IP, |
| 68 | [CS] = HOST_CS, |
| 69 | [EFL] = HOST_EFLAGS, |
| 70 | [UESP] = HOST_SP, |
| 71 | [SS] = HOST_SS, |
Mickaël Salaün | e04c989 | 2015-12-29 21:35:44 +0100 | [diff] [blame] | 72 | [ORIG_EAX] = HOST_ORIG_AX, |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int putreg(struct task_struct *child, int regno, unsigned long value) |
| 76 | { |
| 77 | regno >>= 2; |
| 78 | switch (regno) { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 79 | case EBX: |
| 80 | case ECX: |
| 81 | case EDX: |
| 82 | case ESI: |
| 83 | case EDI: |
| 84 | case EBP: |
| 85 | case EAX: |
| 86 | case EIP: |
| 87 | case UESP: |
Mickaël Salaün | ce29856 | 2016-08-01 23:01:56 +0200 | [diff] [blame] | 88 | break; |
Mickaël Salaün | e04c989 | 2015-12-29 21:35:44 +0100 | [diff] [blame] | 89 | case ORIG_EAX: |
Mickaël Salaün | ce29856 | 2016-08-01 23:01:56 +0200 | [diff] [blame] | 90 | /* Update the syscall number. */ |
| 91 | UPT_SYSCALL_NR(&child->thread.regs.regs) = value; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 92 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | case FS: |
| 94 | if (value && (value & 3) != 3) |
| 95 | return -EIO; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 96 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | case GS: |
| 98 | if (value && (value & 3) != 3) |
| 99 | return -EIO; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 100 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | case DS: |
| 102 | case ES: |
| 103 | if (value && (value & 3) != 3) |
| 104 | return -EIO; |
| 105 | value &= 0xffff; |
| 106 | break; |
| 107 | case SS: |
| 108 | case CS: |
| 109 | if ((value & 3) != 3) |
| 110 | return -EIO; |
| 111 | value &= 0xffff; |
| 112 | break; |
| 113 | case EFL: |
| 114 | value &= FLAG_MASK; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 115 | child->thread.regs.regs.gp[HOST_EFLAGS] |= value; |
| 116 | return 0; |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 117 | default : |
| 118 | panic("Bad register in putreg() : %d\n", regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 120 | child->thread.regs.regs.gp[reg_offsets[regno]] = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 124 | int poke_user(struct task_struct *child, long addr, long data) |
| 125 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 126 | if ((addr & 3) || addr < 0) |
| 127 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 128 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 129 | if (addr < MAX_REG_OFFSET) |
| 130 | return putreg(child, addr, data); |
| 131 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 132 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
| 133 | addr -= offsetof(struct user, u_debugreg[0]); |
| 134 | addr = addr >> 2; |
| 135 | if ((addr == 4) || (addr == 5)) |
| 136 | return -EIO; |
| 137 | child->thread.arch.debugregs[addr] = data; |
| 138 | return 0; |
| 139 | } |
| 140 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | unsigned long getreg(struct task_struct *child, int regno) |
| 144 | { |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 145 | unsigned long mask = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | regno >>= 2; |
| 148 | switch (regno) { |
| 149 | case FS: |
| 150 | case GS: |
| 151 | case DS: |
| 152 | case ES: |
| 153 | case SS: |
| 154 | case CS: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 155 | mask = 0xffff; |
| 156 | break; |
| 157 | case EIP: |
| 158 | case UESP: |
| 159 | case EAX: |
| 160 | case EBX: |
| 161 | case ECX: |
| 162 | case EDX: |
| 163 | case ESI: |
| 164 | case EDI: |
| 165 | case EBP: |
| 166 | case EFL: |
Mickaël Salaün | e04c989 | 2015-12-29 21:35:44 +0100 | [diff] [blame] | 167 | case ORIG_EAX: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 168 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | default: |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 170 | panic("Bad register in getreg() : %d\n", regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
Al Viro | 412f90e | 2011-08-18 20:02:29 +0100 | [diff] [blame] | 172 | return mask & child->thread.regs.regs.gp[reg_offsets[regno]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 175 | /* read the word at location addr in the USER area. */ |
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 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 178 | unsigned long tmp; |
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 | if ((addr & 3) || addr < 0) |
| 181 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 182 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 183 | tmp = 0; /* Default return condition */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 184 | if (addr < MAX_REG_OFFSET) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 185 | tmp = getreg(child, addr); |
| 186 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 187 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 188 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [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 __user *) data); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 196 | 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] | 197 | { |
Linus Torvalds | d896fa2 | 2016-09-13 14:29:23 -0700 | [diff] [blame] | 198 | int err, n, cpu = task_cpu(child); |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 199 | struct user_i387_struct fpregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Eli Cooper | a78ff11 | 2016-03-20 00:58:41 +0800 | [diff] [blame] | 201 | err = save_i387_registers(userspace_pid[cpu], |
| 202 | (unsigned long *) &fpregs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 203 | if (err) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 204 | return err; |
| 205 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 206 | n = copy_to_user(buf, &fpregs, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 207 | if(n > 0) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 208 | return -EFAULT; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 209 | |
| 210 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 213 | 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] | 214 | { |
Linus Torvalds | d896fa2 | 2016-09-13 14:29:23 -0700 | [diff] [blame] | 215 | int n, cpu = task_cpu(child); |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 216 | struct user_i387_struct fpregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 218 | n = copy_from_user(&fpregs, buf, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 219 | if (n > 0) |
| 220 | return -EFAULT; |
| 221 | |
Eli Cooper | a78ff11 | 2016-03-20 00:58:41 +0800 | [diff] [blame] | 222 | return restore_i387_registers(userspace_pid[cpu], |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 223 | (unsigned long *) &fpregs); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 226 | 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] | 227 | { |
Linus Torvalds | d896fa2 | 2016-09-13 14:29:23 -0700 | [diff] [blame] | 228 | int err, n, cpu = task_cpu(child); |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 229 | struct user_fxsr_struct fpregs; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 230 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 231 | err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 232 | if (err) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 233 | return err; |
| 234 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 235 | n = copy_to_user(buf, &fpregs, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 236 | if(n > 0) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 237 | return -EFAULT; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 238 | |
| 239 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 242 | 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] | 243 | { |
Linus Torvalds | d896fa2 | 2016-09-13 14:29:23 -0700 | [diff] [blame] | 244 | int n, cpu = task_cpu(child); |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 245 | struct user_fxsr_struct fpregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 247 | n = copy_from_user(&fpregs, buf, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 248 | if (n > 0) |
| 249 | return -EFAULT; |
| 250 | |
Jeff Dike | 47906dd | 2008-05-12 14:01:50 -0700 | [diff] [blame] | 251 | return restore_fpx_registers(userspace_pid[cpu], |
| 252 | (unsigned long *) &fpregs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 255 | long subarch_ptrace(struct task_struct *child, long request, |
| 256 | unsigned long addr, unsigned long data) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 257 | { |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 258 | int ret = -EIO; |
| 259 | void __user *datap = (void __user *) data; |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 260 | switch (request) { |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 261 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 262 | ret = get_fpregs(datap, child); |
| 263 | break; |
| 264 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 265 | ret = set_fpregs(datap, child); |
| 266 | break; |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 267 | case PTRACE_GETFPXREGS: /* Get the child FPU state. */ |
| 268 | ret = get_fpxregs(datap, child); |
| 269 | break; |
| 270 | case PTRACE_SETFPXREGS: /* Set the child FPU state. */ |
| 271 | ret = set_fpxregs(datap, child); |
| 272 | break; |
Al Viro | f2833ae | 2011-09-14 16:21:37 -0700 | [diff] [blame] | 273 | default: |
| 274 | ret = -EIO; |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 275 | } |
Al Viro | 01599cd | 2011-09-14 16:21:34 -0700 | [diff] [blame] | 276 | return ret; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 277 | } |