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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 10 | extern int arch_switch_tls(struct task_struct *from, struct task_struct *to); |
| 11 | |
| 12 | 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] | 13 | { |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 14 | int err = arch_switch_tls(from, to); |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 15 | if (!err) |
| 16 | return; |
| 17 | |
| 18 | if (err != -EINVAL) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 19 | printk(KERN_WARNING "arch_switch_tls failed, errno %d, " |
| 20 | "not EINVAL\n", -err); |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 21 | else |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 22 | printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int is_syscall(unsigned long addr) |
| 26 | { |
| 27 | unsigned short instr; |
| 28 | int n; |
| 29 | |
| 30 | n = copy_from_user(&instr, (void __user *) addr, sizeof(instr)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 31 | if (n) { |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 32 | /* access_process_vm() grants access to vsyscall and stub, |
| 33 | * while copy_from_user doesn't. Maybe access_process_vm is |
| 34 | * slow, but that doesn't matter, since it will be called only |
| 35 | * in case of singlestepping, if copy_from_user failed. |
| 36 | */ |
| 37 | n = access_process_vm(current, addr, &instr, sizeof(instr), 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 38 | if (n != sizeof(instr)) { |
| 39 | printk(KERN_ERR "is_syscall : failed to read " |
| 40 | "instruction from 0x%lx\n", addr); |
| 41 | return 1; |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 42 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | /* int 0x80 or sysenter */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 45 | return (instr == 0x80cd) || (instr == 0x340f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /* determines which flags the user has access to. */ |
| 49 | /* 1 = access 0 = no access */ |
| 50 | #define FLAG_MASK 0x00044dd5 |
| 51 | |
| 52 | int putreg(struct task_struct *child, int regno, unsigned long value) |
| 53 | { |
| 54 | regno >>= 2; |
| 55 | switch (regno) { |
| 56 | case FS: |
| 57 | if (value && (value & 3) != 3) |
| 58 | return -EIO; |
| 59 | PT_REGS_FS(&child->thread.regs) = value; |
| 60 | return 0; |
| 61 | case GS: |
| 62 | if (value && (value & 3) != 3) |
| 63 | return -EIO; |
| 64 | PT_REGS_GS(&child->thread.regs) = value; |
| 65 | return 0; |
| 66 | case DS: |
| 67 | case ES: |
| 68 | if (value && (value & 3) != 3) |
| 69 | return -EIO; |
| 70 | value &= 0xffff; |
| 71 | break; |
| 72 | case SS: |
| 73 | case CS: |
| 74 | if ((value & 3) != 3) |
| 75 | return -EIO; |
| 76 | value &= 0xffff; |
| 77 | break; |
| 78 | case EFL: |
| 79 | value &= FLAG_MASK; |
| 80 | value |= PT_REGS_EFLAGS(&child->thread.regs); |
| 81 | break; |
| 82 | } |
| 83 | PT_REGS_SET(&child->thread.regs, regno, value); |
| 84 | return 0; |
| 85 | } |
| 86 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 87 | int poke_user(struct task_struct *child, long addr, long data) |
| 88 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 89 | if ((addr & 3) || addr < 0) |
| 90 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 91 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 92 | if (addr < MAX_REG_OFFSET) |
| 93 | return putreg(child, addr, data); |
| 94 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 95 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
| 96 | addr -= offsetof(struct user, u_debugreg[0]); |
| 97 | addr = addr >> 2; |
| 98 | if ((addr == 4) || (addr == 5)) |
| 99 | return -EIO; |
| 100 | child->thread.arch.debugregs[addr] = data; |
| 101 | return 0; |
| 102 | } |
| 103 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | unsigned long getreg(struct task_struct *child, int regno) |
| 107 | { |
| 108 | unsigned long retval = ~0UL; |
| 109 | |
| 110 | regno >>= 2; |
| 111 | switch (regno) { |
| 112 | case FS: |
| 113 | case GS: |
| 114 | case DS: |
| 115 | case ES: |
| 116 | case SS: |
| 117 | case CS: |
| 118 | retval = 0xffff; |
| 119 | /* fall through */ |
| 120 | default: |
| 121 | retval &= PT_REG(&child->thread.regs, regno); |
| 122 | } |
| 123 | return retval; |
| 124 | } |
| 125 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 126 | /* read the word at location addr in the USER area. */ |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 127 | int peek_user(struct task_struct *child, long addr, long data) |
| 128 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 129 | unsigned long tmp; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 130 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 131 | if ((addr & 3) || addr < 0) |
| 132 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 133 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 134 | tmp = 0; /* Default return condition */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 135 | if (addr < MAX_REG_OFFSET) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 136 | tmp = getreg(child, addr); |
| 137 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 138 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 139 | (addr <= offsetof(struct user, u_debugreg[7]))) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 140 | addr -= offsetof(struct user, u_debugreg[0]); |
| 141 | addr = addr >> 2; |
| 142 | tmp = child->thread.arch.debugregs[addr]; |
| 143 | } |
| 144 | return put_user(tmp, (unsigned long __user *) data); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | struct i387_fxsave_struct { |
| 148 | unsigned short cwd; |
| 149 | unsigned short swd; |
| 150 | unsigned short twd; |
| 151 | unsigned short fop; |
| 152 | long fip; |
| 153 | long fcs; |
| 154 | long foo; |
| 155 | long fos; |
| 156 | long mxcsr; |
| 157 | long reserved; |
| 158 | long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 159 | long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 160 | long padding[56]; |
| 161 | }; |
| 162 | |
| 163 | /* |
| 164 | * FPU tag word conversions. |
| 165 | */ |
| 166 | |
| 167 | static inline unsigned short twd_i387_to_fxsr( unsigned short twd ) |
| 168 | { |
| 169 | unsigned int tmp; /* to avoid 16 bit prefixes in the code */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* Transform each pair of bits into 01 (valid) or 00 (empty) */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 172 | tmp = ~twd; |
| 173 | tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */ |
| 174 | /* and move the valid bits to the lower byte. */ |
| 175 | tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */ |
| 176 | tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */ |
| 177 | tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */ |
| 178 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave ) |
| 182 | { |
| 183 | struct _fpxreg *st = NULL; |
| 184 | unsigned long twd = (unsigned long) fxsave->twd; |
| 185 | unsigned long tag; |
| 186 | unsigned long ret = 0xffff0000; |
| 187 | int i; |
| 188 | |
| 189 | #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16); |
| 190 | |
| 191 | for ( i = 0 ; i < 8 ; i++ ) { |
| 192 | if ( twd & 0x1 ) { |
| 193 | st = (struct _fpxreg *) FPREG_ADDR( fxsave, i ); |
| 194 | |
| 195 | switch ( st->exponent & 0x7fff ) { |
| 196 | case 0x7fff: |
| 197 | tag = 2; /* Special */ |
| 198 | break; |
| 199 | case 0x0000: |
| 200 | if ( !st->significand[0] && |
| 201 | !st->significand[1] && |
| 202 | !st->significand[2] && |
| 203 | !st->significand[3] ) { |
| 204 | tag = 1; /* Zero */ |
| 205 | } else { |
| 206 | tag = 2; /* Special */ |
| 207 | } |
| 208 | break; |
| 209 | default: |
| 210 | if ( st->significand[3] & 0x8000 ) { |
| 211 | tag = 0; /* Valid */ |
| 212 | } else { |
| 213 | tag = 2; /* Special */ |
| 214 | } |
| 215 | break; |
| 216 | } |
| 217 | } else { |
| 218 | tag = 3; /* Empty */ |
| 219 | } |
| 220 | ret |= (tag << (2 * i)); |
| 221 | twd = twd >> 1; |
| 222 | } |
| 223 | return ret; |
| 224 | } |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | static inline int convert_fxsr_to_user(struct _fpstate __user *buf, |
| 227 | struct pt_regs *regs) |
| 228 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 229 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 232 | static inline int convert_fxsr_from_user(struct pt_regs *regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | struct _fpstate __user *buf) |
| 234 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 235 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | int get_fpregs(unsigned long buf, struct task_struct *child) |
| 239 | { |
| 240 | int err; |
| 241 | |
| 242 | err = convert_fxsr_to_user((struct _fpstate __user *) buf, |
| 243 | &child->thread.regs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 244 | if (err) |
| 245 | return -EFAULT; |
| 246 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int set_fpregs(unsigned long buf, struct task_struct *child) |
| 250 | { |
| 251 | int err; |
| 252 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 253 | err = convert_fxsr_from_user(&child->thread.regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | (struct _fpstate __user *) buf); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 255 | if (err) |
| 256 | return -EFAULT; |
| 257 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | int get_fpxregs(unsigned long buf, struct task_struct *tsk) |
| 261 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 262 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int set_fpxregs(unsigned long buf, struct task_struct *tsk) |
| 266 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 267 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | #ifdef notdef |
| 271 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu) |
| 272 | { |
| 273 | fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) | |
| 274 | (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff)); |
| 275 | fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff; |
| 276 | fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs)); |
| 277 | fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff; |
| 278 | fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs)); |
| 279 | fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs)); |
| 280 | fpu->fos = 0; |
| 281 | memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)), |
| 282 | sizeof(fpu->st_space)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 283 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | #endif |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu ) |
| 288 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 289 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |