core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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->initialized) {
  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->initialized)
  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->initialized) {
  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->last_cpu = -1;
  166. if (!src_fpu->initialized || !static_cpu_has(X86_FEATURE_FPU))
  167. return 0;
  168. WARN_ON_FPU(src_fpu != &current->thread.fpu);
  169. /*
  170. * Don't let 'init optimized' areas of the XSAVE area
  171. * leak into the child task:
  172. */
  173. memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
  174. /*
  175. * Save current FPU registers directly into the child
  176. * FPU context, without any memory-to-memory copying.
  177. *
  178. * ( The function 'fails' in the FNSAVE case, which destroys
  179. * register contents so we have to copy them back. )
  180. */
  181. if (!copy_fpregs_to_fpstate(dst_fpu)) {
  182. memcpy(&src_fpu->state, &dst_fpu->state, fpu_kernel_xstate_size);
  183. copy_kernel_to_fpregs(&src_fpu->state);
  184. }
  185. trace_x86_fpu_copy_src(src_fpu);
  186. trace_x86_fpu_copy_dst(dst_fpu);
  187. return 0;
  188. }
  189. /*
  190. * Activate the current task's in-memory FPU context,
  191. * if it has not been used before:
  192. */
  193. void fpu__initialize(struct fpu *fpu)
  194. {
  195. WARN_ON_FPU(fpu != &current->thread.fpu);
  196. if (!fpu->initialized) {
  197. fpstate_init(&fpu->state);
  198. trace_x86_fpu_init_state(fpu);
  199. trace_x86_fpu_activate_state(fpu);
  200. /* Safe to do for the current task: */
  201. fpu->initialized = 1;
  202. }
  203. }
  204. EXPORT_SYMBOL_GPL(fpu__initialize);
  205. /*
  206. * This function must be called before we read a task's fpstate.
  207. *
  208. * There's two cases where this gets called:
  209. *
  210. * - for the current task (when coredumping), in which case we have
  211. * to save the latest FPU registers into the fpstate,
  212. *
  213. * - or it's called for stopped tasks (ptrace), in which case the
  214. * registers were already saved by the context-switch code when
  215. * the task scheduled out - we only have to initialize the registers
  216. * if they've never been initialized.
  217. *
  218. * If the task has used the FPU before then save it.
  219. */
  220. void fpu__prepare_read(struct fpu *fpu)
  221. {
  222. if (fpu == &current->thread.fpu) {
  223. fpu__save(fpu);
  224. } else {
  225. if (!fpu->initialized) {
  226. fpstate_init(&fpu->state);
  227. trace_x86_fpu_init_state(fpu);
  228. trace_x86_fpu_activate_state(fpu);
  229. /* Safe to do for current and for stopped child tasks: */
  230. fpu->initialized = 1;
  231. }
  232. }
  233. }
  234. /*
  235. * This function must be called before we write a task's fpstate.
  236. *
  237. * If the task has used the FPU before then invalidate any cached FPU registers.
  238. * If the task has not used the FPU before then initialize its fpstate.
  239. *
  240. * After this function call, after registers in the fpstate are
  241. * modified and the child task has woken up, the child task will
  242. * restore the modified FPU state from the modified context. If we
  243. * didn't clear its cached status here then the cached in-registers
  244. * state pending on its former CPU could be restored, corrupting
  245. * the modifications.
  246. */
  247. void fpu__prepare_write(struct fpu *fpu)
  248. {
  249. /*
  250. * Only stopped child tasks can be used to modify the FPU
  251. * state in the fpstate buffer:
  252. */
  253. WARN_ON_FPU(fpu == &current->thread.fpu);
  254. if (fpu->initialized) {
  255. /* Invalidate any cached state: */
  256. __fpu_invalidate_fpregs_state(fpu);
  257. } else {
  258. fpstate_init(&fpu->state);
  259. trace_x86_fpu_init_state(fpu);
  260. trace_x86_fpu_activate_state(fpu);
  261. /* Safe to do for stopped child tasks: */
  262. fpu->initialized = 1;
  263. }
  264. }
  265. /*
  266. * 'fpu__restore()' is called to copy FPU registers from
  267. * the FPU fpstate to the live hw registers and to activate
  268. * access to the hardware registers, so that FPU instructions
  269. * can be used afterwards.
  270. *
  271. * Must be called with kernel preemption disabled (for example
  272. * with local interrupts disabled, as it is in the case of
  273. * do_device_not_available()).
  274. */
  275. void fpu__restore(struct fpu *fpu)
  276. {
  277. fpu__initialize(fpu);
  278. /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
  279. kernel_fpu_disable();
  280. trace_x86_fpu_before_restore(fpu);
  281. fpregs_activate(fpu);
  282. copy_kernel_to_fpregs(&fpu->state);
  283. trace_x86_fpu_after_restore(fpu);
  284. kernel_fpu_enable();
  285. }
  286. EXPORT_SYMBOL_GPL(fpu__restore);
  287. /*
  288. * Drops current FPU state: deactivates the fpregs and
  289. * the fpstate. NOTE: it still leaves previous contents
  290. * in the fpregs in the eager-FPU case.
  291. *
  292. * This function can be used in cases where we know that
  293. * a state-restore is coming: either an explicit one,
  294. * or a reschedule.
  295. */
  296. void fpu__drop(struct fpu *fpu)
  297. {
  298. preempt_disable();
  299. if (fpu == &current->thread.fpu) {
  300. if (fpu->initialized) {
  301. /* Ignore delayed exceptions from user space */
  302. asm volatile("1: fwait\n"
  303. "2:\n"
  304. _ASM_EXTABLE(1b, 2b));
  305. fpregs_deactivate(fpu);
  306. }
  307. }
  308. fpu->initialized = 0;
  309. trace_x86_fpu_dropped(fpu);
  310. preempt_enable();
  311. }
  312. /*
  313. * Clear FPU registers by setting them up from
  314. * the init fpstate:
  315. */
  316. static inline void copy_init_fpstate_to_fpregs(void)
  317. {
  318. if (use_xsave())
  319. copy_kernel_to_xregs(&init_fpstate.xsave, -1);
  320. else if (static_cpu_has(X86_FEATURE_FXSR))
  321. copy_kernel_to_fxregs(&init_fpstate.fxsave);
  322. else
  323. copy_kernel_to_fregs(&init_fpstate.fsave);
  324. if (boot_cpu_has(X86_FEATURE_OSPKE))
  325. copy_init_pkru_to_fpregs();
  326. }
  327. /*
  328. * Clear the FPU state back to init state.
  329. *
  330. * Called by sys_execve(), by the signal handler code and by various
  331. * error paths.
  332. */
  333. void fpu__clear(struct fpu *fpu)
  334. {
  335. WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
  336. fpu__drop(fpu);
  337. /*
  338. * Make sure fpstate is cleared and initialized.
  339. */
  340. if (static_cpu_has(X86_FEATURE_FPU)) {
  341. preempt_disable();
  342. fpu__initialize(fpu);
  343. user_fpu_begin();
  344. copy_init_fpstate_to_fpregs();
  345. preempt_enable();
  346. }
  347. }
  348. /*
  349. * x87 math exception handling:
  350. */
  351. int fpu__exception_code(struct fpu *fpu, int trap_nr)
  352. {
  353. int err;
  354. if (trap_nr == X86_TRAP_MF) {
  355. unsigned short cwd, swd;
  356. /*
  357. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  358. * status. 0x3f is the exception bits in these regs, 0x200 is the
  359. * C1 reg you need in case of a stack fault, 0x040 is the stack
  360. * fault bit. We should only be taking one exception at a time,
  361. * so if this combination doesn't produce any single exception,
  362. * then we have a bad program that isn't synchronizing its FPU usage
  363. * and it will suffer the consequences since we won't be able to
  364. * fully reproduce the context of the exception.
  365. */
  366. if (boot_cpu_has(X86_FEATURE_FXSR)) {
  367. cwd = fpu->state.fxsave.cwd;
  368. swd = fpu->state.fxsave.swd;
  369. } else {
  370. cwd = (unsigned short)fpu->state.fsave.cwd;
  371. swd = (unsigned short)fpu->state.fsave.swd;
  372. }
  373. err = swd & ~cwd;
  374. } else {
  375. /*
  376. * The SIMD FPU exceptions are handled a little differently, as there
  377. * is only a single status/control register. Thus, to determine which
  378. * unmasked exception was caught we must mask the exception mask bits
  379. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  380. */
  381. unsigned short mxcsr = MXCSR_DEFAULT;
  382. if (boot_cpu_has(X86_FEATURE_XMM))
  383. mxcsr = fpu->state.fxsave.mxcsr;
  384. err = ~(mxcsr >> 7) & mxcsr;
  385. }
  386. if (err & 0x001) { /* Invalid op */
  387. /*
  388. * swd & 0x240 == 0x040: Stack Underflow
  389. * swd & 0x240 == 0x240: Stack Overflow
  390. * User must clear the SF bit (0x40) if set
  391. */
  392. return FPE_FLTINV;
  393. } else if (err & 0x004) { /* Divide by Zero */
  394. return FPE_FLTDIV;
  395. } else if (err & 0x008) { /* Overflow */
  396. return FPE_FLTOVF;
  397. } else if (err & 0x012) { /* Denormal, Underflow */
  398. return FPE_FLTUND;
  399. } else if (err & 0x020) { /* Precision */
  400. return FPE_FLTRES;
  401. }
  402. /*
  403. * If we're using IRQ 13, or supposedly even some trap
  404. * X86_TRAP_MF implementations, it's possible
  405. * we get a spurious trap, which is not an error.
  406. */
  407. return 0;
  408. }