core.c 11 KB

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