i387.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * General FPU state handling cleanups
  6. * Gareth Hughes <gareth@valinux.com>, May 2000
  7. */
  8. #include <linux/module.h>
  9. #include <linux/regset.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <asm/sigcontext.h>
  13. #include <asm/processor.h>
  14. #include <asm/math_emu.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/i387.h>
  18. #include <asm/fpu-internal.h>
  19. #include <asm/user.h>
  20. /*
  21. * Were we in an interrupt that interrupted kernel mode?
  22. *
  23. * We can do a kernel_fpu_begin/end() pair *ONLY* if that
  24. * pair does nothing at all: the thread must not have fpu (so
  25. * that we don't try to save the FPU state), and TS must
  26. * be set (so that the clts/stts pair does nothing that is
  27. * visible in the interrupted kernel thread).
  28. */
  29. static inline bool interrupted_kernel_fpu_idle(void)
  30. {
  31. return !__thread_has_fpu(current) &&
  32. (read_cr0() & X86_CR0_TS);
  33. }
  34. /*
  35. * Were we in user mode (or vm86 mode) when we were
  36. * interrupted?
  37. *
  38. * Doing kernel_fpu_begin/end() is ok if we are running
  39. * in an interrupt context from user mode - we'll just
  40. * save the FPU state as required.
  41. */
  42. static inline bool interrupted_user_mode(void)
  43. {
  44. struct pt_regs *regs = get_irq_regs();
  45. return regs && user_mode_vm(regs);
  46. }
  47. /*
  48. * Can we use the FPU in kernel mode with the
  49. * whole "kernel_fpu_begin/end()" sequence?
  50. *
  51. * It's always ok in process context (ie "not interrupt")
  52. * but it is sometimes ok even from an irq.
  53. */
  54. bool irq_fpu_usable(void)
  55. {
  56. return !in_interrupt() ||
  57. interrupted_user_mode() ||
  58. interrupted_kernel_fpu_idle();
  59. }
  60. EXPORT_SYMBOL(irq_fpu_usable);
  61. void kernel_fpu_begin(void)
  62. {
  63. struct task_struct *me = current;
  64. WARN_ON_ONCE(!irq_fpu_usable());
  65. preempt_disable();
  66. if (__thread_has_fpu(me)) {
  67. __save_init_fpu(me);
  68. __thread_clear_has_fpu(me);
  69. /* We do 'stts()' in kernel_fpu_end() */
  70. } else {
  71. this_cpu_write(fpu_owner_task, NULL);
  72. clts();
  73. }
  74. }
  75. EXPORT_SYMBOL(kernel_fpu_begin);
  76. void kernel_fpu_end(void)
  77. {
  78. stts();
  79. preempt_enable();
  80. }
  81. EXPORT_SYMBOL(kernel_fpu_end);
  82. void unlazy_fpu(struct task_struct *tsk)
  83. {
  84. preempt_disable();
  85. if (__thread_has_fpu(tsk)) {
  86. __save_init_fpu(tsk);
  87. __thread_fpu_end(tsk);
  88. } else
  89. tsk->fpu_counter = 0;
  90. preempt_enable();
  91. }
  92. EXPORT_SYMBOL(unlazy_fpu);
  93. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  94. unsigned int xstate_size;
  95. EXPORT_SYMBOL_GPL(xstate_size);
  96. static struct i387_fxsave_struct fx_scratch __cpuinitdata;
  97. static void __cpuinit mxcsr_feature_mask_init(void)
  98. {
  99. unsigned long mask = 0;
  100. clts();
  101. if (cpu_has_fxsr) {
  102. memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
  103. asm volatile("fxsave %0" : : "m" (fx_scratch));
  104. mask = fx_scratch.mxcsr_mask;
  105. if (mask == 0)
  106. mask = 0x0000ffbf;
  107. }
  108. mxcsr_feature_mask &= mask;
  109. stts();
  110. }
  111. static void __cpuinit init_thread_xstate(void)
  112. {
  113. /*
  114. * Note that xstate_size might be overwriten later during
  115. * xsave_init().
  116. */
  117. if (!HAVE_HWFP) {
  118. /*
  119. * Disable xsave as we do not support it if i387
  120. * emulation is enabled.
  121. */
  122. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  123. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  124. xstate_size = sizeof(struct i387_soft_struct);
  125. return;
  126. }
  127. if (cpu_has_fxsr)
  128. xstate_size = sizeof(struct i387_fxsave_struct);
  129. else
  130. xstate_size = sizeof(struct i387_fsave_struct);
  131. }
  132. /*
  133. * Called at bootup to set up the initial FPU state that is later cloned
  134. * into all processes.
  135. */
  136. void __cpuinit fpu_init(void)
  137. {
  138. unsigned long cr0;
  139. unsigned long cr4_mask = 0;
  140. if (cpu_has_fxsr)
  141. cr4_mask |= X86_CR4_OSFXSR;
  142. if (cpu_has_xmm)
  143. cr4_mask |= X86_CR4_OSXMMEXCPT;
  144. if (cr4_mask)
  145. set_in_cr4(cr4_mask);
  146. cr0 = read_cr0();
  147. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  148. if (!HAVE_HWFP)
  149. cr0 |= X86_CR0_EM;
  150. write_cr0(cr0);
  151. if (!smp_processor_id())
  152. init_thread_xstate();
  153. mxcsr_feature_mask_init();
  154. /* clean state in init */
  155. current_thread_info()->status = 0;
  156. clear_used_math();
  157. }
  158. void fpu_finit(struct fpu *fpu)
  159. {
  160. if (!HAVE_HWFP) {
  161. finit_soft_fpu(&fpu->state->soft);
  162. return;
  163. }
  164. if (cpu_has_fxsr) {
  165. struct i387_fxsave_struct *fx = &fpu->state->fxsave;
  166. memset(fx, 0, xstate_size);
  167. fx->cwd = 0x37f;
  168. if (cpu_has_xmm)
  169. fx->mxcsr = MXCSR_DEFAULT;
  170. } else {
  171. struct i387_fsave_struct *fp = &fpu->state->fsave;
  172. memset(fp, 0, xstate_size);
  173. fp->cwd = 0xffff037fu;
  174. fp->swd = 0xffff0000u;
  175. fp->twd = 0xffffffffu;
  176. fp->fos = 0xffff0000u;
  177. }
  178. }
  179. EXPORT_SYMBOL_GPL(fpu_finit);
  180. /*
  181. * The _current_ task is using the FPU for the first time
  182. * so initialize it and set the mxcsr to its default
  183. * value at reset if we support XMM instructions and then
  184. * remember the current task has used the FPU.
  185. */
  186. int init_fpu(struct task_struct *tsk)
  187. {
  188. int ret;
  189. if (tsk_used_math(tsk)) {
  190. if (HAVE_HWFP && tsk == current)
  191. unlazy_fpu(tsk);
  192. tsk->thread.fpu.last_cpu = ~0;
  193. return 0;
  194. }
  195. /*
  196. * Memory allocation at the first usage of the FPU and other state.
  197. */
  198. ret = fpu_alloc(&tsk->thread.fpu);
  199. if (ret)
  200. return ret;
  201. fpu_finit(&tsk->thread.fpu);
  202. set_stopped_child_used_math(tsk);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(init_fpu);
  206. /*
  207. * The xstateregs_active() routine is the same as the fpregs_active() routine,
  208. * as the "regset->n" for the xstate regset will be updated based on the feature
  209. * capabilites supported by the xsave.
  210. */
  211. int fpregs_active(struct task_struct *target, const struct user_regset *regset)
  212. {
  213. return tsk_used_math(target) ? regset->n : 0;
  214. }
  215. int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
  216. {
  217. return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
  218. }
  219. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  220. unsigned int pos, unsigned int count,
  221. void *kbuf, void __user *ubuf)
  222. {
  223. int ret;
  224. if (!cpu_has_fxsr)
  225. return -ENODEV;
  226. ret = init_fpu(target);
  227. if (ret)
  228. return ret;
  229. sanitize_i387_state(target);
  230. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  231. &target->thread.fpu.state->fxsave, 0, -1);
  232. }
  233. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  234. unsigned int pos, unsigned int count,
  235. const void *kbuf, const void __user *ubuf)
  236. {
  237. int ret;
  238. if (!cpu_has_fxsr)
  239. return -ENODEV;
  240. ret = init_fpu(target);
  241. if (ret)
  242. return ret;
  243. sanitize_i387_state(target);
  244. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  245. &target->thread.fpu.state->fxsave, 0, -1);
  246. /*
  247. * mxcsr reserved bits must be masked to zero for security reasons.
  248. */
  249. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  250. /*
  251. * update the header bits in the xsave header, indicating the
  252. * presence of FP and SSE state.
  253. */
  254. if (cpu_has_xsave)
  255. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
  256. return ret;
  257. }
  258. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  259. unsigned int pos, unsigned int count,
  260. void *kbuf, void __user *ubuf)
  261. {
  262. int ret;
  263. if (!cpu_has_xsave)
  264. return -ENODEV;
  265. ret = init_fpu(target);
  266. if (ret)
  267. return ret;
  268. /*
  269. * Copy the 48bytes defined by the software first into the xstate
  270. * memory layout in the thread struct, so that we can copy the entire
  271. * xstateregs to the user using one user_regset_copyout().
  272. */
  273. memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
  274. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  275. /*
  276. * Copy the xstate memory layout.
  277. */
  278. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  279. &target->thread.fpu.state->xsave, 0, -1);
  280. return ret;
  281. }
  282. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  283. unsigned int pos, unsigned int count,
  284. const void *kbuf, const void __user *ubuf)
  285. {
  286. int ret;
  287. struct xsave_hdr_struct *xsave_hdr;
  288. if (!cpu_has_xsave)
  289. return -ENODEV;
  290. ret = init_fpu(target);
  291. if (ret)
  292. return ret;
  293. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  294. &target->thread.fpu.state->xsave, 0, -1);
  295. /*
  296. * mxcsr reserved bits must be masked to zero for security reasons.
  297. */
  298. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  299. xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
  300. xsave_hdr->xstate_bv &= pcntxt_mask;
  301. /*
  302. * These bits must be zero.
  303. */
  304. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  305. return ret;
  306. }
  307. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  308. /*
  309. * FPU tag word conversions.
  310. */
  311. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  312. {
  313. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  314. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  315. tmp = ~twd;
  316. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  317. /* and move the valid bits to the lower byte. */
  318. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  319. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  320. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  321. return tmp;
  322. }
  323. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  324. #define FP_EXP_TAG_VALID 0
  325. #define FP_EXP_TAG_ZERO 1
  326. #define FP_EXP_TAG_SPECIAL 2
  327. #define FP_EXP_TAG_EMPTY 3
  328. static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  329. {
  330. struct _fpxreg *st;
  331. u32 tos = (fxsave->swd >> 11) & 7;
  332. u32 twd = (unsigned long) fxsave->twd;
  333. u32 tag;
  334. u32 ret = 0xffff0000u;
  335. int i;
  336. for (i = 0; i < 8; i++, twd >>= 1) {
  337. if (twd & 0x1) {
  338. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  339. switch (st->exponent & 0x7fff) {
  340. case 0x7fff:
  341. tag = FP_EXP_TAG_SPECIAL;
  342. break;
  343. case 0x0000:
  344. if (!st->significand[0] &&
  345. !st->significand[1] &&
  346. !st->significand[2] &&
  347. !st->significand[3])
  348. tag = FP_EXP_TAG_ZERO;
  349. else
  350. tag = FP_EXP_TAG_SPECIAL;
  351. break;
  352. default:
  353. if (st->significand[3] & 0x8000)
  354. tag = FP_EXP_TAG_VALID;
  355. else
  356. tag = FP_EXP_TAG_SPECIAL;
  357. break;
  358. }
  359. } else {
  360. tag = FP_EXP_TAG_EMPTY;
  361. }
  362. ret |= tag << (2 * i);
  363. }
  364. return ret;
  365. }
  366. /*
  367. * FXSR floating point environment conversions.
  368. */
  369. void
  370. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  371. {
  372. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  373. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  374. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  375. int i;
  376. env->cwd = fxsave->cwd | 0xffff0000u;
  377. env->swd = fxsave->swd | 0xffff0000u;
  378. env->twd = twd_fxsr_to_i387(fxsave);
  379. #ifdef CONFIG_X86_64
  380. env->fip = fxsave->rip;
  381. env->foo = fxsave->rdp;
  382. /*
  383. * should be actually ds/cs at fpu exception time, but
  384. * that information is not available in 64bit mode.
  385. */
  386. env->fcs = task_pt_regs(tsk)->cs;
  387. if (tsk == current) {
  388. savesegment(ds, env->fos);
  389. } else {
  390. env->fos = tsk->thread.ds;
  391. }
  392. env->fos |= 0xffff0000;
  393. #else
  394. env->fip = fxsave->fip;
  395. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  396. env->foo = fxsave->foo;
  397. env->fos = fxsave->fos;
  398. #endif
  399. for (i = 0; i < 8; ++i)
  400. memcpy(&to[i], &from[i], sizeof(to[0]));
  401. }
  402. void convert_to_fxsr(struct task_struct *tsk,
  403. const struct user_i387_ia32_struct *env)
  404. {
  405. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  406. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  407. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  408. int i;
  409. fxsave->cwd = env->cwd;
  410. fxsave->swd = env->swd;
  411. fxsave->twd = twd_i387_to_fxsr(env->twd);
  412. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  413. #ifdef CONFIG_X86_64
  414. fxsave->rip = env->fip;
  415. fxsave->rdp = env->foo;
  416. /* cs and ds ignored */
  417. #else
  418. fxsave->fip = env->fip;
  419. fxsave->fcs = (env->fcs & 0xffff);
  420. fxsave->foo = env->foo;
  421. fxsave->fos = env->fos;
  422. #endif
  423. for (i = 0; i < 8; ++i)
  424. memcpy(&to[i], &from[i], sizeof(from[0]));
  425. }
  426. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  427. unsigned int pos, unsigned int count,
  428. void *kbuf, void __user *ubuf)
  429. {
  430. struct user_i387_ia32_struct env;
  431. int ret;
  432. ret = init_fpu(target);
  433. if (ret)
  434. return ret;
  435. if (!HAVE_HWFP)
  436. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  437. if (!cpu_has_fxsr) {
  438. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  439. &target->thread.fpu.state->fsave, 0,
  440. -1);
  441. }
  442. sanitize_i387_state(target);
  443. if (kbuf && pos == 0 && count == sizeof(env)) {
  444. convert_from_fxsr(kbuf, target);
  445. return 0;
  446. }
  447. convert_from_fxsr(&env, target);
  448. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  449. }
  450. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  451. unsigned int pos, unsigned int count,
  452. const void *kbuf, const void __user *ubuf)
  453. {
  454. struct user_i387_ia32_struct env;
  455. int ret;
  456. ret = init_fpu(target);
  457. if (ret)
  458. return ret;
  459. sanitize_i387_state(target);
  460. if (!HAVE_HWFP)
  461. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  462. if (!cpu_has_fxsr) {
  463. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  464. &target->thread.fpu.state->fsave, 0, -1);
  465. }
  466. if (pos > 0 || count < sizeof(env))
  467. convert_from_fxsr(&env, target);
  468. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  469. if (!ret)
  470. convert_to_fxsr(target, &env);
  471. /*
  472. * update the header bit in the xsave header, indicating the
  473. * presence of FP.
  474. */
  475. if (cpu_has_xsave)
  476. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
  477. return ret;
  478. }
  479. /*
  480. * FPU state for core dumps.
  481. * This is only used for a.out dumps now.
  482. * It is declared generically using elf_fpregset_t (which is
  483. * struct user_i387_struct) but is in fact only used for 32-bit
  484. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  485. */
  486. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
  487. {
  488. struct task_struct *tsk = current;
  489. int fpvalid;
  490. fpvalid = !!used_math();
  491. if (fpvalid)
  492. fpvalid = !fpregs_get(tsk, NULL,
  493. 0, sizeof(struct user_i387_ia32_struct),
  494. fpu, NULL);
  495. return fpvalid;
  496. }
  497. EXPORT_SYMBOL(dump_fpu);
  498. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */