core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 <asm/fpu/internal.h>
  9. #include <asm/fpu/regset.h>
  10. #include <asm/fpu/signal.h>
  11. #include <asm/fpu/types.h>
  12. #include <asm/traps.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/pkeys.h>
  15. #define CREATE_TRACE_POINTS
  16. #include <asm/trace/fpu.h>
  17. /*
  18. * Represents the initial FPU state. It's mostly (but not completely) zeroes,
  19. * depending on the FPU hardware format:
  20. */
  21. union fpregs_state init_fpstate __read_mostly;
  22. /*
  23. * Track whether the kernel is using the FPU state
  24. * currently.
  25. *
  26. * This flag is used:
  27. *
  28. * - by IRQ context code to potentially use the FPU
  29. * if it's unused.
  30. *
  31. * - to debug kernel_fpu_begin()/end() correctness
  32. */
  33. static DEFINE_PER_CPU(bool, in_kernel_fpu);
  34. /*
  35. * Track which context is using the FPU on the CPU:
  36. */
  37. DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
  38. static void kernel_fpu_disable(void)
  39. {
  40. WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
  41. this_cpu_write(in_kernel_fpu, true);
  42. }
  43. static void kernel_fpu_enable(void)
  44. {
  45. WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
  46. this_cpu_write(in_kernel_fpu, false);
  47. }
  48. static bool kernel_fpu_disabled(void)
  49. {
  50. return this_cpu_read(in_kernel_fpu);
  51. }
  52. static bool interrupted_kernel_fpu_idle(void)
  53. {
  54. return !kernel_fpu_disabled();
  55. }
  56. /*
  57. * Were we in user mode (or vm86 mode) when we were
  58. * interrupted?
  59. *
  60. * Doing kernel_fpu_begin/end() is ok if we are running
  61. * in an interrupt context from user mode - we'll just
  62. * save the FPU state as required.
  63. */
  64. static bool interrupted_user_mode(void)
  65. {
  66. struct pt_regs *regs = get_irq_regs();
  67. return regs && user_mode(regs);
  68. }
  69. /*
  70. * Can we use the FPU in kernel mode with the
  71. * whole "kernel_fpu_begin/end()" sequence?
  72. *
  73. * It's always ok in process context (ie "not interrupt")
  74. * but it is sometimes ok even from an irq.
  75. */
  76. bool irq_fpu_usable(void)
  77. {
  78. return !in_interrupt() ||
  79. interrupted_user_mode() ||
  80. interrupted_kernel_fpu_idle();
  81. }
  82. EXPORT_SYMBOL(irq_fpu_usable);
  83. void __kernel_fpu_begin(void)
  84. {
  85. struct fpu *fpu = &current->thread.fpu;
  86. WARN_ON_FPU(!irq_fpu_usable());
  87. kernel_fpu_disable();
  88. if (fpu->fpregs_active) {
  89. /*
  90. * Ignore return value -- we don't care if reg state
  91. * is clobbered.
  92. */
  93. copy_fpregs_to_fpstate(fpu);
  94. } else {
  95. __cpu_invalidate_fpregs_state();
  96. }
  97. }
  98. EXPORT_SYMBOL(__kernel_fpu_begin);
  99. void __kernel_fpu_end(void)
  100. {
  101. struct fpu *fpu = &current->thread.fpu;
  102. if (fpu->fpregs_active)
  103. copy_kernel_to_fpregs(&fpu->state);
  104. kernel_fpu_enable();
  105. }
  106. EXPORT_SYMBOL(__kernel_fpu_end);
  107. void kernel_fpu_begin(void)
  108. {
  109. preempt_disable();
  110. __kernel_fpu_begin();
  111. }
  112. EXPORT_SYMBOL_GPL(kernel_fpu_begin);
  113. void kernel_fpu_end(void)
  114. {
  115. __kernel_fpu_end();
  116. preempt_enable();
  117. }
  118. EXPORT_SYMBOL_GPL(kernel_fpu_end);
  119. /*
  120. * Save the FPU state (mark it for reload if necessary):
  121. *
  122. * This only ever gets called for the current task.
  123. */
  124. void fpu__save(struct fpu *fpu)
  125. {
  126. WARN_ON_FPU(fpu != &current->thread.fpu);
  127. preempt_disable();
  128. trace_x86_fpu_before_save(fpu);
  129. if (fpu->fpregs_active) {
  130. if (!copy_fpregs_to_fpstate(fpu)) {
  131. copy_kernel_to_fpregs(&fpu->state);
  132. }
  133. }
  134. trace_x86_fpu_after_save(fpu);
  135. preempt_enable();
  136. }
  137. EXPORT_SYMBOL_GPL(fpu__save);
  138. /*
  139. * Legacy x87 fpstate state init:
  140. */
  141. static inline void fpstate_init_fstate(struct fregs_state *fp)
  142. {
  143. fp->cwd = 0xffff037fu;
  144. fp->swd = 0xffff0000u;
  145. fp->twd = 0xffffffffu;
  146. fp->fos = 0xffff0000u;
  147. }
  148. void fpstate_init(union fpregs_state *state)
  149. {
  150. if (!static_cpu_has(X86_FEATURE_FPU)) {
  151. fpstate_init_soft(&state->soft);
  152. return;
  153. }
  154. memset(state, 0, fpu_kernel_xstate_size);
  155. /*
  156. * XRSTORS requires that this bit is set in xcomp_bv, or
  157. * it will #GP. Make sure it is replaced after the memset().
  158. */
  159. if (static_cpu_has(X86_FEATURE_XSAVES))
  160. state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT;
  161. if (static_cpu_has(X86_FEATURE_FXSR))
  162. fpstate_init_fxstate(&state->fxsave);
  163. else
  164. fpstate_init_fstate(&state->fsave);
  165. }
  166. EXPORT_SYMBOL_GPL(fpstate_init);
  167. int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
  168. {
  169. dst_fpu->fpregs_active = 0;
  170. dst_fpu->last_cpu = -1;
  171. if (!src_fpu->fpstate_active || !static_cpu_has(X86_FEATURE_FPU))
  172. return 0;
  173. WARN_ON_FPU(src_fpu != &current->thread.fpu);
  174. /*
  175. * Don't let 'init optimized' areas of the XSAVE area
  176. * leak into the child task:
  177. */
  178. memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
  179. /*
  180. * Save current FPU registers directly into the child
  181. * FPU context, without any memory-to-memory copying.
  182. * In lazy mode, if the FPU context isn't loaded into
  183. * fpregs, CR0.TS will be set and do_device_not_available
  184. * will load the FPU context.
  185. *
  186. * We have to do all this with preemption disabled,
  187. * mostly because of the FNSAVE case, because in that
  188. * case we must not allow preemption in the window
  189. * between the FNSAVE and us marking the context lazy.
  190. *
  191. * It shouldn't be an issue as even FNSAVE is plenty
  192. * fast in terms of critical section length.
  193. */
  194. preempt_disable();
  195. if (!copy_fpregs_to_fpstate(dst_fpu)) {
  196. memcpy(&src_fpu->state, &dst_fpu->state,
  197. fpu_kernel_xstate_size);
  198. copy_kernel_to_fpregs(&src_fpu->state);
  199. }
  200. preempt_enable();
  201. trace_x86_fpu_copy_src(src_fpu);
  202. trace_x86_fpu_copy_dst(dst_fpu);
  203. return 0;
  204. }
  205. /*
  206. * Activate the current task's in-memory FPU context,
  207. * if it has not been used before:
  208. */
  209. void fpu__activate_curr(struct fpu *fpu)
  210. {
  211. WARN_ON_FPU(fpu != &current->thread.fpu);
  212. if (!fpu->fpstate_active) {
  213. fpstate_init(&fpu->state);
  214. trace_x86_fpu_init_state(fpu);
  215. trace_x86_fpu_activate_state(fpu);
  216. /* Safe to do for the current task: */
  217. fpu->fpstate_active = 1;
  218. }
  219. }
  220. EXPORT_SYMBOL_GPL(fpu__activate_curr);
  221. /*
  222. * This function must be called before we read a task's fpstate.
  223. *
  224. * If the task has not used the FPU before then initialize its
  225. * fpstate.
  226. *
  227. * If the task has used the FPU before then save it.
  228. */
  229. void fpu__activate_fpstate_read(struct fpu *fpu)
  230. {
  231. /*
  232. * If fpregs are active (in the current CPU), then
  233. * copy them to the fpstate:
  234. */
  235. if (fpu->fpregs_active) {
  236. fpu__save(fpu);
  237. } else {
  238. if (!fpu->fpstate_active) {
  239. fpstate_init(&fpu->state);
  240. trace_x86_fpu_init_state(fpu);
  241. trace_x86_fpu_activate_state(fpu);
  242. /* Safe to do for current and for stopped child tasks: */
  243. fpu->fpstate_active = 1;
  244. }
  245. }
  246. }
  247. /*
  248. * This function must be called before we write a task's fpstate.
  249. *
  250. * If the task has used the FPU before then unlazy it.
  251. * If the task has not used the FPU before then initialize its fpstate.
  252. *
  253. * After this function call, after registers in the fpstate are
  254. * modified and the child task has woken up, the child task will
  255. * restore the modified FPU state from the modified context. If we
  256. * didn't clear its lazy status here then the lazy in-registers
  257. * state pending on its former CPU could be restored, corrupting
  258. * the modifications.
  259. */
  260. void fpu__activate_fpstate_write(struct fpu *fpu)
  261. {
  262. /*
  263. * Only stopped child tasks can be used to modify the FPU
  264. * state in the fpstate buffer:
  265. */
  266. WARN_ON_FPU(fpu == &current->thread.fpu);
  267. if (fpu->fpstate_active) {
  268. /* Invalidate any lazy state: */
  269. __fpu_invalidate_fpregs_state(fpu);
  270. } else {
  271. fpstate_init(&fpu->state);
  272. trace_x86_fpu_init_state(fpu);
  273. trace_x86_fpu_activate_state(fpu);
  274. /* Safe to do for stopped child tasks: */
  275. fpu->fpstate_active = 1;
  276. }
  277. }
  278. /*
  279. * This function must be called before we write the current
  280. * task's fpstate.
  281. *
  282. * This call gets the current FPU register state and moves
  283. * it in to the 'fpstate'. Preemption is disabled so that
  284. * no writes to the 'fpstate' can occur from context
  285. * swiches.
  286. *
  287. * Must be followed by a fpu__current_fpstate_write_end().
  288. */
  289. void fpu__current_fpstate_write_begin(void)
  290. {
  291. struct fpu *fpu = &current->thread.fpu;
  292. /*
  293. * Ensure that the context-switching code does not write
  294. * over the fpstate while we are doing our update.
  295. */
  296. preempt_disable();
  297. /*
  298. * Move the fpregs in to the fpu's 'fpstate'.
  299. */
  300. fpu__activate_fpstate_read(fpu);
  301. /*
  302. * The caller is about to write to 'fpu'. Ensure that no
  303. * CPU thinks that its fpregs match the fpstate. This
  304. * ensures we will not be lazy and skip a XRSTOR in the
  305. * future.
  306. */
  307. __fpu_invalidate_fpregs_state(fpu);
  308. }
  309. /*
  310. * This function must be paired with fpu__current_fpstate_write_begin()
  311. *
  312. * This will ensure that the modified fpstate gets placed back in
  313. * the fpregs if necessary.
  314. *
  315. * Note: This function may be called whether or not an _actual_
  316. * write to the fpstate occurred.
  317. */
  318. void fpu__current_fpstate_write_end(void)
  319. {
  320. struct fpu *fpu = &current->thread.fpu;
  321. /*
  322. * 'fpu' now has an updated copy of the state, but the
  323. * registers may still be out of date. Update them with
  324. * an XRSTOR if they are active.
  325. */
  326. if (fpregs_active())
  327. copy_kernel_to_fpregs(&fpu->state);
  328. /*
  329. * Our update is done and the fpregs/fpstate are in sync
  330. * if necessary. Context switches can happen again.
  331. */
  332. preempt_enable();
  333. }
  334. /*
  335. * 'fpu__restore()' is called to copy FPU registers from
  336. * the FPU fpstate to the live hw registers and to activate
  337. * access to the hardware registers, so that FPU instructions
  338. * can be used afterwards.
  339. *
  340. * Must be called with kernel preemption disabled (for example
  341. * with local interrupts disabled, as it is in the case of
  342. * do_device_not_available()).
  343. */
  344. void fpu__restore(struct fpu *fpu)
  345. {
  346. fpu__activate_curr(fpu);
  347. /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
  348. kernel_fpu_disable();
  349. trace_x86_fpu_before_restore(fpu);
  350. fpregs_activate(fpu);
  351. copy_kernel_to_fpregs(&fpu->state);
  352. trace_x86_fpu_after_restore(fpu);
  353. kernel_fpu_enable();
  354. }
  355. EXPORT_SYMBOL_GPL(fpu__restore);
  356. /*
  357. * Drops current FPU state: deactivates the fpregs and
  358. * the fpstate. NOTE: it still leaves previous contents
  359. * in the fpregs in the eager-FPU case.
  360. *
  361. * This function can be used in cases where we know that
  362. * a state-restore is coming: either an explicit one,
  363. * or a reschedule.
  364. */
  365. void fpu__drop(struct fpu *fpu)
  366. {
  367. preempt_disable();
  368. if (fpu->fpregs_active) {
  369. /* Ignore delayed exceptions from user space */
  370. asm volatile("1: fwait\n"
  371. "2:\n"
  372. _ASM_EXTABLE(1b, 2b));
  373. fpregs_deactivate(fpu);
  374. }
  375. fpu->fpstate_active = 0;
  376. trace_x86_fpu_dropped(fpu);
  377. preempt_enable();
  378. }
  379. /*
  380. * Clear FPU registers by setting them up from
  381. * the init fpstate:
  382. */
  383. static inline void copy_init_fpstate_to_fpregs(void)
  384. {
  385. if (use_xsave())
  386. copy_kernel_to_xregs(&init_fpstate.xsave, -1);
  387. else if (static_cpu_has(X86_FEATURE_FXSR))
  388. copy_kernel_to_fxregs(&init_fpstate.fxsave);
  389. else
  390. copy_kernel_to_fregs(&init_fpstate.fsave);
  391. if (boot_cpu_has(X86_FEATURE_OSPKE))
  392. copy_init_pkru_to_fpregs();
  393. }
  394. /*
  395. * Clear the FPU state back to init state.
  396. *
  397. * Called by sys_execve(), by the signal handler code and by various
  398. * error paths.
  399. */
  400. void fpu__clear(struct fpu *fpu)
  401. {
  402. WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
  403. fpu__drop(fpu);
  404. /*
  405. * Make sure fpstate is cleared and initialized.
  406. */
  407. if (static_cpu_has(X86_FEATURE_FPU)) {
  408. fpu__activate_curr(fpu);
  409. user_fpu_begin();
  410. copy_init_fpstate_to_fpregs();
  411. }
  412. }
  413. /*
  414. * x87 math exception handling:
  415. */
  416. int fpu__exception_code(struct fpu *fpu, int trap_nr)
  417. {
  418. int err;
  419. if (trap_nr == X86_TRAP_MF) {
  420. unsigned short cwd, swd;
  421. /*
  422. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  423. * status. 0x3f is the exception bits in these regs, 0x200 is the
  424. * C1 reg you need in case of a stack fault, 0x040 is the stack
  425. * fault bit. We should only be taking one exception at a time,
  426. * so if this combination doesn't produce any single exception,
  427. * then we have a bad program that isn't synchronizing its FPU usage
  428. * and it will suffer the consequences since we won't be able to
  429. * fully reproduce the context of the exception.
  430. */
  431. if (boot_cpu_has(X86_FEATURE_FXSR)) {
  432. cwd = fpu->state.fxsave.cwd;
  433. swd = fpu->state.fxsave.swd;
  434. } else {
  435. cwd = (unsigned short)fpu->state.fsave.cwd;
  436. swd = (unsigned short)fpu->state.fsave.swd;
  437. }
  438. err = swd & ~cwd;
  439. } else {
  440. /*
  441. * The SIMD FPU exceptions are handled a little differently, as there
  442. * is only a single status/control register. Thus, to determine which
  443. * unmasked exception was caught we must mask the exception mask bits
  444. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  445. */
  446. unsigned short mxcsr = MXCSR_DEFAULT;
  447. if (boot_cpu_has(X86_FEATURE_XMM))
  448. mxcsr = fpu->state.fxsave.mxcsr;
  449. err = ~(mxcsr >> 7) & mxcsr;
  450. }
  451. if (err & 0x001) { /* Invalid op */
  452. /*
  453. * swd & 0x240 == 0x040: Stack Underflow
  454. * swd & 0x240 == 0x240: Stack Overflow
  455. * User must clear the SF bit (0x40) if set
  456. */
  457. return FPE_FLTINV;
  458. } else if (err & 0x004) { /* Divide by Zero */
  459. return FPE_FLTDIV;
  460. } else if (err & 0x008) { /* Overflow */
  461. return FPE_FLTOVF;
  462. } else if (err & 0x012) { /* Denormal, Underflow */
  463. return FPE_FLTUND;
  464. } else if (err & 0x020) { /* Precision */
  465. return FPE_FLTRES;
  466. }
  467. /*
  468. * If we're using IRQ 13, or supposedly even some trap
  469. * X86_TRAP_MF implementations, it's possible
  470. * we get a spurious trap, which is not an error.
  471. */
  472. return 0;
  473. }