core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. if (static_cpu_has(X86_FEATURE_XSAVES))
  156. fpstate_init_xstate(&state->xsave);
  157. if (static_cpu_has(X86_FEATURE_FXSR))
  158. fpstate_init_fxstate(&state->fxsave);
  159. else
  160. fpstate_init_fstate(&state->fsave);
  161. }
  162. EXPORT_SYMBOL_GPL(fpstate_init);
  163. int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
  164. {
  165. dst_fpu->fpregs_active = 0;
  166. dst_fpu->last_cpu = -1;
  167. if (!src_fpu->fpstate_active || !static_cpu_has(X86_FEATURE_FPU))
  168. return 0;
  169. WARN_ON_FPU(src_fpu != &current->thread.fpu);
  170. /*
  171. * Don't let 'init optimized' areas of the XSAVE area
  172. * leak into the child task:
  173. */
  174. memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
  175. /*
  176. * Save current FPU registers directly into the child
  177. * FPU context, without any memory-to-memory copying.
  178. * In lazy mode, if the FPU context isn't loaded into
  179. * fpregs, CR0.TS will be set and do_device_not_available
  180. * will load the FPU context.
  181. *
  182. * We have to do all this with preemption disabled,
  183. * mostly because of the FNSAVE case, because in that
  184. * case we must not allow preemption in the window
  185. * between the FNSAVE and us marking the context lazy.
  186. *
  187. * It shouldn't be an issue as even FNSAVE is plenty
  188. * fast in terms of critical section length.
  189. */
  190. preempt_disable();
  191. if (!copy_fpregs_to_fpstate(dst_fpu)) {
  192. memcpy(&src_fpu->state, &dst_fpu->state,
  193. fpu_kernel_xstate_size);
  194. copy_kernel_to_fpregs(&src_fpu->state);
  195. }
  196. preempt_enable();
  197. trace_x86_fpu_copy_src(src_fpu);
  198. trace_x86_fpu_copy_dst(dst_fpu);
  199. return 0;
  200. }
  201. /*
  202. * Activate the current task's in-memory FPU context,
  203. * if it has not been used before:
  204. */
  205. void fpu__activate_curr(struct fpu *fpu)
  206. {
  207. WARN_ON_FPU(fpu != &current->thread.fpu);
  208. if (!fpu->fpstate_active) {
  209. fpstate_init(&fpu->state);
  210. trace_x86_fpu_init_state(fpu);
  211. trace_x86_fpu_activate_state(fpu);
  212. /* Safe to do for the current task: */
  213. fpu->fpstate_active = 1;
  214. }
  215. }
  216. EXPORT_SYMBOL_GPL(fpu__activate_curr);
  217. /*
  218. * This function must be called before we read a task's fpstate.
  219. *
  220. * If the task has not used the FPU before then initialize its
  221. * fpstate.
  222. *
  223. * If the task has used the FPU before then save it.
  224. */
  225. void fpu__activate_fpstate_read(struct fpu *fpu)
  226. {
  227. /*
  228. * If fpregs are active (in the current CPU), then
  229. * copy them to the fpstate:
  230. */
  231. if (fpu->fpregs_active) {
  232. fpu__save(fpu);
  233. } else {
  234. if (!fpu->fpstate_active) {
  235. fpstate_init(&fpu->state);
  236. trace_x86_fpu_init_state(fpu);
  237. trace_x86_fpu_activate_state(fpu);
  238. /* Safe to do for current and for stopped child tasks: */
  239. fpu->fpstate_active = 1;
  240. }
  241. }
  242. }
  243. /*
  244. * This function must be called before we write a task's fpstate.
  245. *
  246. * If the task has used the FPU before then unlazy it.
  247. * If the task has not used the FPU before then initialize its fpstate.
  248. *
  249. * After this function call, after registers in the fpstate are
  250. * modified and the child task has woken up, the child task will
  251. * restore the modified FPU state from the modified context. If we
  252. * didn't clear its lazy status here then the lazy in-registers
  253. * state pending on its former CPU could be restored, corrupting
  254. * the modifications.
  255. */
  256. void fpu__activate_fpstate_write(struct fpu *fpu)
  257. {
  258. /*
  259. * Only stopped child tasks can be used to modify the FPU
  260. * state in the fpstate buffer:
  261. */
  262. WARN_ON_FPU(fpu == &current->thread.fpu);
  263. if (fpu->fpstate_active) {
  264. /* Invalidate any lazy state: */
  265. __fpu_invalidate_fpregs_state(fpu);
  266. } else {
  267. fpstate_init(&fpu->state);
  268. trace_x86_fpu_init_state(fpu);
  269. trace_x86_fpu_activate_state(fpu);
  270. /* Safe to do for stopped child tasks: */
  271. fpu->fpstate_active = 1;
  272. }
  273. }
  274. /*
  275. * This function must be called before we write the current
  276. * task's fpstate.
  277. *
  278. * This call gets the current FPU register state and moves
  279. * it in to the 'fpstate'. Preemption is disabled so that
  280. * no writes to the 'fpstate' can occur from context
  281. * swiches.
  282. *
  283. * Must be followed by a fpu__current_fpstate_write_end().
  284. */
  285. void fpu__current_fpstate_write_begin(void)
  286. {
  287. struct fpu *fpu = &current->thread.fpu;
  288. /*
  289. * Ensure that the context-switching code does not write
  290. * over the fpstate while we are doing our update.
  291. */
  292. preempt_disable();
  293. /*
  294. * Move the fpregs in to the fpu's 'fpstate'.
  295. */
  296. fpu__activate_fpstate_read(fpu);
  297. /*
  298. * The caller is about to write to 'fpu'. Ensure that no
  299. * CPU thinks that its fpregs match the fpstate. This
  300. * ensures we will not be lazy and skip a XRSTOR in the
  301. * future.
  302. */
  303. __fpu_invalidate_fpregs_state(fpu);
  304. }
  305. /*
  306. * This function must be paired with fpu__current_fpstate_write_begin()
  307. *
  308. * This will ensure that the modified fpstate gets placed back in
  309. * the fpregs if necessary.
  310. *
  311. * Note: This function may be called whether or not an _actual_
  312. * write to the fpstate occurred.
  313. */
  314. void fpu__current_fpstate_write_end(void)
  315. {
  316. struct fpu *fpu = &current->thread.fpu;
  317. /*
  318. * 'fpu' now has an updated copy of the state, but the
  319. * registers may still be out of date. Update them with
  320. * an XRSTOR if they are active.
  321. */
  322. if (fpregs_active())
  323. copy_kernel_to_fpregs(&fpu->state);
  324. /*
  325. * Our update is done and the fpregs/fpstate are in sync
  326. * if necessary. Context switches can happen again.
  327. */
  328. preempt_enable();
  329. }
  330. /*
  331. * 'fpu__restore()' is called to copy FPU registers from
  332. * the FPU fpstate to the live hw registers and to activate
  333. * access to the hardware registers, so that FPU instructions
  334. * can be used afterwards.
  335. *
  336. * Must be called with kernel preemption disabled (for example
  337. * with local interrupts disabled, as it is in the case of
  338. * do_device_not_available()).
  339. */
  340. void fpu__restore(struct fpu *fpu)
  341. {
  342. fpu__activate_curr(fpu);
  343. /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
  344. kernel_fpu_disable();
  345. trace_x86_fpu_before_restore(fpu);
  346. fpregs_activate(fpu);
  347. copy_kernel_to_fpregs(&fpu->state);
  348. trace_x86_fpu_after_restore(fpu);
  349. kernel_fpu_enable();
  350. }
  351. EXPORT_SYMBOL_GPL(fpu__restore);
  352. /*
  353. * Drops current FPU state: deactivates the fpregs and
  354. * the fpstate. NOTE: it still leaves previous contents
  355. * in the fpregs in the eager-FPU case.
  356. *
  357. * This function can be used in cases where we know that
  358. * a state-restore is coming: either an explicit one,
  359. * or a reschedule.
  360. */
  361. void fpu__drop(struct fpu *fpu)
  362. {
  363. preempt_disable();
  364. if (fpu->fpregs_active) {
  365. /* Ignore delayed exceptions from user space */
  366. asm volatile("1: fwait\n"
  367. "2:\n"
  368. _ASM_EXTABLE(1b, 2b));
  369. fpregs_deactivate(fpu);
  370. }
  371. fpu->fpstate_active = 0;
  372. trace_x86_fpu_dropped(fpu);
  373. preempt_enable();
  374. }
  375. /*
  376. * Clear FPU registers by setting them up from
  377. * the init fpstate:
  378. */
  379. static inline void copy_init_fpstate_to_fpregs(void)
  380. {
  381. if (use_xsave())
  382. copy_kernel_to_xregs(&init_fpstate.xsave, -1);
  383. else if (static_cpu_has(X86_FEATURE_FXSR))
  384. copy_kernel_to_fxregs(&init_fpstate.fxsave);
  385. else
  386. copy_kernel_to_fregs(&init_fpstate.fsave);
  387. if (boot_cpu_has(X86_FEATURE_OSPKE))
  388. copy_init_pkru_to_fpregs();
  389. }
  390. /*
  391. * Clear the FPU state back to init state.
  392. *
  393. * Called by sys_execve(), by the signal handler code and by various
  394. * error paths.
  395. */
  396. void fpu__clear(struct fpu *fpu)
  397. {
  398. WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
  399. fpu__drop(fpu);
  400. /*
  401. * Make sure fpstate is cleared and initialized.
  402. */
  403. if (static_cpu_has(X86_FEATURE_FPU)) {
  404. fpu__activate_curr(fpu);
  405. user_fpu_begin();
  406. copy_init_fpstate_to_fpregs();
  407. }
  408. }
  409. /*
  410. * x87 math exception handling:
  411. */
  412. int fpu__exception_code(struct fpu *fpu, int trap_nr)
  413. {
  414. int err;
  415. if (trap_nr == X86_TRAP_MF) {
  416. unsigned short cwd, swd;
  417. /*
  418. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  419. * status. 0x3f is the exception bits in these regs, 0x200 is the
  420. * C1 reg you need in case of a stack fault, 0x040 is the stack
  421. * fault bit. We should only be taking one exception at a time,
  422. * so if this combination doesn't produce any single exception,
  423. * then we have a bad program that isn't synchronizing its FPU usage
  424. * and it will suffer the consequences since we won't be able to
  425. * fully reproduce the context of the exception.
  426. */
  427. if (boot_cpu_has(X86_FEATURE_FXSR)) {
  428. cwd = fpu->state.fxsave.cwd;
  429. swd = fpu->state.fxsave.swd;
  430. } else {
  431. cwd = (unsigned short)fpu->state.fsave.cwd;
  432. swd = (unsigned short)fpu->state.fsave.swd;
  433. }
  434. err = swd & ~cwd;
  435. } else {
  436. /*
  437. * The SIMD FPU exceptions are handled a little differently, as there
  438. * is only a single status/control register. Thus, to determine which
  439. * unmasked exception was caught we must mask the exception mask bits
  440. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  441. */
  442. unsigned short mxcsr = MXCSR_DEFAULT;
  443. if (boot_cpu_has(X86_FEATURE_XMM))
  444. mxcsr = fpu->state.fxsave.mxcsr;
  445. err = ~(mxcsr >> 7) & mxcsr;
  446. }
  447. if (err & 0x001) { /* Invalid op */
  448. /*
  449. * swd & 0x240 == 0x040: Stack Underflow
  450. * swd & 0x240 == 0x240: Stack Overflow
  451. * User must clear the SF bit (0x40) if set
  452. */
  453. return FPE_FLTINV;
  454. } else if (err & 0x004) { /* Divide by Zero */
  455. return FPE_FLTDIV;
  456. } else if (err & 0x008) { /* Overflow */
  457. return FPE_FLTOVF;
  458. } else if (err & 0x012) { /* Denormal, Underflow */
  459. return FPE_FLTUND;
  460. } else if (err & 0x020) { /* Precision */
  461. return FPE_FLTRES;
  462. }
  463. /*
  464. * If we're using IRQ 13, or supposedly even some trap
  465. * X86_TRAP_MF implementations, it's possible
  466. * we get a spurious trap, which is not an error.
  467. */
  468. return 0;
  469. }