core.c 11 KB

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