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 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 11 | extern int arch_switch_tls(struct task_struct *from, struct task_struct *to); |
| 12 | |
| 13 | void arch_switch_to(struct task_struct *from, struct task_struct *to) |
Paolo 'Blaisorblade' Giarrusso | 972410b | 2006-03-31 02:30:21 -0800 | [diff] [blame] | 14 | { |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 15 | int err = arch_switch_tls(from, 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 | |
| 53 | int putreg(struct task_struct *child, int regno, unsigned long value) |
| 54 | { |
| 55 | regno >>= 2; |
| 56 | switch (regno) { |
| 57 | case FS: |
| 58 | if (value && (value & 3) != 3) |
| 59 | return -EIO; |
| 60 | PT_REGS_FS(&child->thread.regs) = value; |
| 61 | return 0; |
| 62 | case GS: |
| 63 | if (value && (value & 3) != 3) |
| 64 | return -EIO; |
| 65 | PT_REGS_GS(&child->thread.regs) = value; |
| 66 | return 0; |
| 67 | case DS: |
| 68 | case ES: |
| 69 | if (value && (value & 3) != 3) |
| 70 | return -EIO; |
| 71 | value &= 0xffff; |
| 72 | break; |
| 73 | case SS: |
| 74 | case CS: |
| 75 | if ((value & 3) != 3) |
| 76 | return -EIO; |
| 77 | value &= 0xffff; |
| 78 | break; |
| 79 | case EFL: |
| 80 | value &= FLAG_MASK; |
| 81 | value |= PT_REGS_EFLAGS(&child->thread.regs); |
| 82 | break; |
| 83 | } |
| 84 | PT_REGS_SET(&child->thread.regs, regno, value); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 88 | int poke_user(struct task_struct *child, long addr, long data) |
| 89 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 90 | if ((addr & 3) || addr < 0) |
| 91 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 92 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 93 | if (addr < MAX_REG_OFFSET) |
| 94 | return putreg(child, addr, data); |
| 95 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 96 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
| 97 | addr -= offsetof(struct user, u_debugreg[0]); |
| 98 | addr = addr >> 2; |
| 99 | if ((addr == 4) || (addr == 5)) |
| 100 | return -EIO; |
| 101 | child->thread.arch.debugregs[addr] = data; |
| 102 | return 0; |
| 103 | } |
| 104 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | unsigned long getreg(struct task_struct *child, int regno) |
| 108 | { |
| 109 | unsigned long retval = ~0UL; |
| 110 | |
| 111 | regno >>= 2; |
| 112 | switch (regno) { |
| 113 | case FS: |
| 114 | case GS: |
| 115 | case DS: |
| 116 | case ES: |
| 117 | case SS: |
| 118 | case CS: |
| 119 | retval = 0xffff; |
| 120 | /* fall through */ |
| 121 | default: |
| 122 | retval &= PT_REG(&child->thread.regs, regno); |
| 123 | } |
| 124 | return retval; |
| 125 | } |
| 126 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 127 | /* read the word at location addr in the USER area. */ |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 128 | int peek_user(struct task_struct *child, long addr, long data) |
| 129 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 130 | unsigned long tmp; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 131 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 132 | if ((addr & 3) || addr < 0) |
| 133 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 134 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 135 | tmp = 0; /* Default return condition */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 136 | if (addr < MAX_REG_OFFSET) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 137 | tmp = getreg(child, addr); |
| 138 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 139 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 140 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 141 | addr -= offsetof(struct user, u_debugreg[0]); |
| 142 | addr = addr >> 2; |
| 143 | tmp = child->thread.arch.debugregs[addr]; |
| 144 | } |
| 145 | return put_user(tmp, (unsigned long __user *) data); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 148 | 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] | 149 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 150 | int err, n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 151 | long fpregs[HOST_FP_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 153 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
| 154 | err = save_fp_registers(userspace_pid[cpu], fpregs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 155 | if (err) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 156 | return err; |
| 157 | |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 158 | n = copy_to_user(buf, fpregs, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 159 | if(n > 0) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 160 | return -EFAULT; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 161 | |
| 162 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 165 | 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] | 166 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 167 | int n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 168 | long fpregs[HOST_FP_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 170 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 171 | n = copy_from_user(fpregs, buf, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 172 | if (n > 0) |
| 173 | return -EFAULT; |
| 174 | |
| 175 | return restore_fp_registers(userspace_pid[cpu], fpregs); |
| 176 | } |
| 177 | |
| 178 | int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child) |
| 179 | { |
| 180 | int err, n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 181 | long fpregs[HOST_XFP_SIZE]; |
| 182 | |
| 183 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
| 184 | err = save_fpx_registers(userspace_pid[cpu], fpregs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 185 | if (err) |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 186 | return err; |
| 187 | |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 188 | n = copy_to_user(buf, fpregs, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 189 | if(n > 0) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 190 | return -EFAULT; |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 191 | |
| 192 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 195 | 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] | 196 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 197 | int n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 198 | long fpregs[HOST_XFP_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 200 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 201 | n = copy_from_user(fpregs, buf, sizeof(fpregs)); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 202 | if (n > 0) |
| 203 | return -EFAULT; |
| 204 | |
| 205 | return restore_fpx_registers(userspace_pid[cpu], fpregs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 208 | long subarch_ptrace(struct task_struct *child, long request, long addr, |
| 209 | long data) |
| 210 | { |
| 211 | return -EIO; |
| 212 | } |