core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. /*
  10. * Track whether the kernel is using the FPU state
  11. * currently.
  12. *
  13. * This flag is used:
  14. *
  15. * - by IRQ context code to potentially use the FPU
  16. * if it's unused.
  17. *
  18. * - to debug kernel_fpu_begin()/end() correctness
  19. */
  20. static DEFINE_PER_CPU(bool, in_kernel_fpu);
  21. /*
  22. * Track which context is using the FPU on the CPU:
  23. */
  24. DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
  25. static void kernel_fpu_disable(void)
  26. {
  27. WARN_ON(this_cpu_read(in_kernel_fpu));
  28. this_cpu_write(in_kernel_fpu, true);
  29. }
  30. static void kernel_fpu_enable(void)
  31. {
  32. WARN_ON_ONCE(!this_cpu_read(in_kernel_fpu));
  33. this_cpu_write(in_kernel_fpu, false);
  34. }
  35. static bool kernel_fpu_disabled(void)
  36. {
  37. return this_cpu_read(in_kernel_fpu);
  38. }
  39. /*
  40. * Were we in an interrupt that interrupted kernel mode?
  41. *
  42. * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
  43. * pair does nothing at all: the thread must not have fpu (so
  44. * that we don't try to save the FPU state), and TS must
  45. * be set (so that the clts/stts pair does nothing that is
  46. * visible in the interrupted kernel thread).
  47. *
  48. * Except for the eagerfpu case when we return true; in the likely case
  49. * the thread has FPU but we are not going to set/clear TS.
  50. */
  51. static bool interrupted_kernel_fpu_idle(void)
  52. {
  53. if (kernel_fpu_disabled())
  54. return false;
  55. if (use_eager_fpu())
  56. return true;
  57. return !current->thread.fpu.has_fpu && (read_cr0() & X86_CR0_TS);
  58. }
  59. /*
  60. * Were we in user mode (or vm86 mode) when we were
  61. * interrupted?
  62. *
  63. * Doing kernel_fpu_begin/end() is ok if we are running
  64. * in an interrupt context from user mode - we'll just
  65. * save the FPU state as required.
  66. */
  67. static bool interrupted_user_mode(void)
  68. {
  69. struct pt_regs *regs = get_irq_regs();
  70. return regs && user_mode(regs);
  71. }
  72. /*
  73. * Can we use the FPU in kernel mode with the
  74. * whole "kernel_fpu_begin/end()" sequence?
  75. *
  76. * It's always ok in process context (ie "not interrupt")
  77. * but it is sometimes ok even from an irq.
  78. */
  79. bool irq_fpu_usable(void)
  80. {
  81. return !in_interrupt() ||
  82. interrupted_user_mode() ||
  83. interrupted_kernel_fpu_idle();
  84. }
  85. EXPORT_SYMBOL(irq_fpu_usable);
  86. void __kernel_fpu_begin(void)
  87. {
  88. struct fpu *fpu = &current->thread.fpu;
  89. kernel_fpu_disable();
  90. if (fpu->has_fpu) {
  91. fpu_save_init(fpu);
  92. } else {
  93. this_cpu_write(fpu_fpregs_owner_ctx, NULL);
  94. if (!use_eager_fpu())
  95. clts();
  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->has_fpu) {
  103. if (WARN_ON(restore_fpu_checking(fpu)))
  104. fpu_reset_state(fpu);
  105. } else if (!use_eager_fpu()) {
  106. stts();
  107. }
  108. kernel_fpu_enable();
  109. }
  110. EXPORT_SYMBOL(__kernel_fpu_end);
  111. static void __save_fpu(struct fpu *fpu)
  112. {
  113. if (use_xsave()) {
  114. if (unlikely(system_state == SYSTEM_BOOTING))
  115. xsave_state_booting(&fpu->state->xsave);
  116. else
  117. xsave_state(&fpu->state->xsave);
  118. } else {
  119. fpu_fxsave(fpu);
  120. }
  121. }
  122. /*
  123. * Save the FPU state (initialize it if necessary):
  124. *
  125. * This only ever gets called for the current task.
  126. */
  127. void fpu__save(struct fpu *fpu)
  128. {
  129. WARN_ON(fpu != &current->thread.fpu);
  130. preempt_disable();
  131. if (fpu->has_fpu) {
  132. if (use_eager_fpu()) {
  133. __save_fpu(fpu);
  134. } else {
  135. fpu_save_init(fpu);
  136. __thread_fpu_end(fpu);
  137. }
  138. }
  139. preempt_enable();
  140. }
  141. EXPORT_SYMBOL_GPL(fpu__save);
  142. void fpstate_init(struct fpu *fpu)
  143. {
  144. if (!cpu_has_fpu) {
  145. finit_soft_fpu(&fpu->state->soft);
  146. return;
  147. }
  148. memset(fpu->state, 0, xstate_size);
  149. if (cpu_has_fxsr) {
  150. fx_finit(&fpu->state->fxsave);
  151. } else {
  152. struct i387_fsave_struct *fp = &fpu->state->fsave;
  153. fp->cwd = 0xffff037fu;
  154. fp->swd = 0xffff0000u;
  155. fp->twd = 0xffffffffu;
  156. fp->fos = 0xffff0000u;
  157. }
  158. }
  159. EXPORT_SYMBOL_GPL(fpstate_init);
  160. /*
  161. * FPU state allocation:
  162. */
  163. static struct kmem_cache *task_xstate_cachep;
  164. void fpstate_cache_init(void)
  165. {
  166. task_xstate_cachep =
  167. kmem_cache_create("task_xstate", xstate_size,
  168. __alignof__(union thread_xstate),
  169. SLAB_PANIC | SLAB_NOTRACK, NULL);
  170. setup_xstate_comp();
  171. }
  172. int fpstate_alloc(struct fpu *fpu)
  173. {
  174. if (fpu->state)
  175. return 0;
  176. fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
  177. if (!fpu->state)
  178. return -ENOMEM;
  179. /* The CPU requires the FPU state to be aligned to 16 byte boundaries: */
  180. WARN_ON((unsigned long)fpu->state & 15);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL_GPL(fpstate_alloc);
  184. void fpstate_free(struct fpu *fpu)
  185. {
  186. if (fpu->state) {
  187. kmem_cache_free(task_xstate_cachep, fpu->state);
  188. fpu->state = NULL;
  189. }
  190. }
  191. EXPORT_SYMBOL_GPL(fpstate_free);
  192. /*
  193. * Copy the current task's FPU state to a new task's FPU context.
  194. *
  195. * In the 'eager' case we just save to the destination context.
  196. *
  197. * In the 'lazy' case we save to the source context, mark the FPU lazy
  198. * via stts() and copy the source context into the destination context.
  199. */
  200. static void fpu_copy(struct fpu *dst_fpu, struct fpu *src_fpu)
  201. {
  202. WARN_ON(src_fpu != &current->thread.fpu);
  203. if (use_eager_fpu()) {
  204. memset(&dst_fpu->state->xsave, 0, xstate_size);
  205. __save_fpu(dst_fpu);
  206. } else {
  207. fpu__save(src_fpu);
  208. memcpy(dst_fpu->state, src_fpu->state, xstate_size);
  209. }
  210. }
  211. int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
  212. {
  213. dst_fpu->counter = 0;
  214. dst_fpu->has_fpu = 0;
  215. dst_fpu->state = NULL;
  216. dst_fpu->last_cpu = -1;
  217. if (src_fpu->fpstate_active) {
  218. int err = fpstate_alloc(dst_fpu);
  219. if (err)
  220. return err;
  221. fpu_copy(dst_fpu, src_fpu);
  222. }
  223. return 0;
  224. }
  225. /*
  226. * Allocate the backing store for the current task's FPU registers
  227. * and initialize the registers themselves as well.
  228. *
  229. * Can fail.
  230. */
  231. int fpstate_alloc_init(struct fpu *fpu)
  232. {
  233. int ret;
  234. if (WARN_ON_ONCE(fpu != &current->thread.fpu))
  235. return -EINVAL;
  236. if (WARN_ON_ONCE(fpu->fpstate_active))
  237. return -EINVAL;
  238. /*
  239. * Memory allocation at the first usage of the FPU and other state.
  240. */
  241. ret = fpstate_alloc(fpu);
  242. if (ret)
  243. return ret;
  244. fpstate_init(fpu);
  245. /* Safe to do for the current task: */
  246. fpu->fpstate_active = 1;
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(fpstate_alloc_init);
  250. /*
  251. * This function is called before we modify a stopped child's
  252. * FPU state context.
  253. *
  254. * If the child has not used the FPU before then initialize its
  255. * FPU context.
  256. *
  257. * If the child has used the FPU before then unlazy it.
  258. *
  259. * [ After this function call, after the context is modified and
  260. * the child task is woken up, the child task will restore
  261. * the modified FPU state from the modified context. If we
  262. * didn't clear its lazy status here then the lazy in-registers
  263. * state pending on its former CPU could be restored, losing
  264. * the modifications. ]
  265. *
  266. * This function is also called before we read a stopped child's
  267. * FPU state - to make sure it's modified.
  268. *
  269. * TODO: A future optimization would be to skip the unlazying in
  270. * the read-only case, it's not strictly necessary for
  271. * read-only access to the context.
  272. */
  273. static int fpu__unlazy_stopped(struct fpu *child_fpu)
  274. {
  275. int ret;
  276. if (WARN_ON_ONCE(child_fpu == &current->thread.fpu))
  277. return -EINVAL;
  278. if (child_fpu->fpstate_active) {
  279. child_fpu->last_cpu = -1;
  280. return 0;
  281. }
  282. /*
  283. * Memory allocation at the first usage of the FPU and other state.
  284. */
  285. ret = fpstate_alloc(child_fpu);
  286. if (ret)
  287. return ret;
  288. fpstate_init(child_fpu);
  289. /* Safe to do for stopped child tasks: */
  290. child_fpu->fpstate_active = 1;
  291. return 0;
  292. }
  293. /*
  294. * 'fpu__restore()' saves the current math information in the
  295. * old math state array, and gets the new ones from the current task
  296. *
  297. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  298. * Don't touch unless you *really* know how it works.
  299. *
  300. * Must be called with kernel preemption disabled (eg with local
  301. * local interrupts as in the case of do_device_not_available).
  302. */
  303. void fpu__restore(void)
  304. {
  305. struct task_struct *tsk = current;
  306. struct fpu *fpu = &tsk->thread.fpu;
  307. if (!fpu->fpstate_active) {
  308. local_irq_enable();
  309. /*
  310. * does a slab alloc which can sleep
  311. */
  312. if (fpstate_alloc_init(fpu)) {
  313. /*
  314. * ran out of memory!
  315. */
  316. do_group_exit(SIGKILL);
  317. return;
  318. }
  319. local_irq_disable();
  320. }
  321. /* Avoid __kernel_fpu_begin() right after __thread_fpu_begin() */
  322. kernel_fpu_disable();
  323. __thread_fpu_begin(fpu);
  324. if (unlikely(restore_fpu_checking(fpu))) {
  325. fpu_reset_state(fpu);
  326. force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
  327. } else {
  328. tsk->thread.fpu.counter++;
  329. }
  330. kernel_fpu_enable();
  331. }
  332. EXPORT_SYMBOL_GPL(fpu__restore);
  333. void fpu__clear(struct task_struct *tsk)
  334. {
  335. struct fpu *fpu = &tsk->thread.fpu;
  336. WARN_ON_ONCE(tsk != current); /* Almost certainly an anomaly */
  337. if (!use_eager_fpu()) {
  338. /* FPU state will be reallocated lazily at the first use. */
  339. drop_fpu(fpu);
  340. fpstate_free(fpu);
  341. } else {
  342. if (!fpu->fpstate_active) {
  343. /* kthread execs. TODO: cleanup this horror. */
  344. if (WARN_ON(fpstate_alloc_init(fpu)))
  345. force_sig(SIGKILL, tsk);
  346. user_fpu_begin();
  347. }
  348. restore_init_xstate();
  349. }
  350. }
  351. /*
  352. * The xstateregs_active() routine is the same as the fpregs_active() routine,
  353. * as the "regset->n" for the xstate regset will be updated based on the feature
  354. * capabilites supported by the xsave.
  355. */
  356. int fpregs_active(struct task_struct *target, const struct user_regset *regset)
  357. {
  358. struct fpu *target_fpu = &target->thread.fpu;
  359. return target_fpu->fpstate_active ? regset->n : 0;
  360. }
  361. int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
  362. {
  363. struct fpu *target_fpu = &target->thread.fpu;
  364. return (cpu_has_fxsr && target_fpu->fpstate_active) ? regset->n : 0;
  365. }
  366. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  367. unsigned int pos, unsigned int count,
  368. void *kbuf, void __user *ubuf)
  369. {
  370. struct fpu *fpu = &target->thread.fpu;
  371. int ret;
  372. if (!cpu_has_fxsr)
  373. return -ENODEV;
  374. ret = fpu__unlazy_stopped(fpu);
  375. if (ret)
  376. return ret;
  377. sanitize_i387_state(target);
  378. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  379. &fpu->state->fxsave, 0, -1);
  380. }
  381. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  382. unsigned int pos, unsigned int count,
  383. const void *kbuf, const void __user *ubuf)
  384. {
  385. struct fpu *fpu = &target->thread.fpu;
  386. int ret;
  387. if (!cpu_has_fxsr)
  388. return -ENODEV;
  389. ret = fpu__unlazy_stopped(fpu);
  390. if (ret)
  391. return ret;
  392. sanitize_i387_state(target);
  393. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  394. &fpu->state->fxsave, 0, -1);
  395. /*
  396. * mxcsr reserved bits must be masked to zero for security reasons.
  397. */
  398. fpu->state->fxsave.mxcsr &= mxcsr_feature_mask;
  399. /*
  400. * update the header bits in the xsave header, indicating the
  401. * presence of FP and SSE state.
  402. */
  403. if (cpu_has_xsave)
  404. fpu->state->xsave.header.xstate_bv |= XSTATE_FPSSE;
  405. return ret;
  406. }
  407. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  408. unsigned int pos, unsigned int count,
  409. void *kbuf, void __user *ubuf)
  410. {
  411. struct fpu *fpu = &target->thread.fpu;
  412. struct xsave_struct *xsave;
  413. int ret;
  414. if (!cpu_has_xsave)
  415. return -ENODEV;
  416. ret = fpu__unlazy_stopped(fpu);
  417. if (ret)
  418. return ret;
  419. xsave = &fpu->state->xsave;
  420. /*
  421. * Copy the 48bytes defined by the software first into the xstate
  422. * memory layout in the thread struct, so that we can copy the entire
  423. * xstateregs to the user using one user_regset_copyout().
  424. */
  425. memcpy(&xsave->i387.sw_reserved,
  426. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  427. /*
  428. * Copy the xstate memory layout.
  429. */
  430. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
  431. return ret;
  432. }
  433. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  434. unsigned int pos, unsigned int count,
  435. const void *kbuf, const void __user *ubuf)
  436. {
  437. struct fpu *fpu = &target->thread.fpu;
  438. struct xsave_struct *xsave;
  439. int ret;
  440. if (!cpu_has_xsave)
  441. return -ENODEV;
  442. ret = fpu__unlazy_stopped(fpu);
  443. if (ret)
  444. return ret;
  445. xsave = &fpu->state->xsave;
  446. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
  447. /*
  448. * mxcsr reserved bits must be masked to zero for security reasons.
  449. */
  450. xsave->i387.mxcsr &= mxcsr_feature_mask;
  451. xsave->header.xstate_bv &= xfeatures_mask;
  452. /*
  453. * These bits must be zero.
  454. */
  455. memset(&xsave->header.reserved, 0, 48);
  456. return ret;
  457. }
  458. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  459. /*
  460. * FPU tag word conversions.
  461. */
  462. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  463. {
  464. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  465. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  466. tmp = ~twd;
  467. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  468. /* and move the valid bits to the lower byte. */
  469. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  470. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  471. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  472. return tmp;
  473. }
  474. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  475. #define FP_EXP_TAG_VALID 0
  476. #define FP_EXP_TAG_ZERO 1
  477. #define FP_EXP_TAG_SPECIAL 2
  478. #define FP_EXP_TAG_EMPTY 3
  479. static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  480. {
  481. struct _fpxreg *st;
  482. u32 tos = (fxsave->swd >> 11) & 7;
  483. u32 twd = (unsigned long) fxsave->twd;
  484. u32 tag;
  485. u32 ret = 0xffff0000u;
  486. int i;
  487. for (i = 0; i < 8; i++, twd >>= 1) {
  488. if (twd & 0x1) {
  489. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  490. switch (st->exponent & 0x7fff) {
  491. case 0x7fff:
  492. tag = FP_EXP_TAG_SPECIAL;
  493. break;
  494. case 0x0000:
  495. if (!st->significand[0] &&
  496. !st->significand[1] &&
  497. !st->significand[2] &&
  498. !st->significand[3])
  499. tag = FP_EXP_TAG_ZERO;
  500. else
  501. tag = FP_EXP_TAG_SPECIAL;
  502. break;
  503. default:
  504. if (st->significand[3] & 0x8000)
  505. tag = FP_EXP_TAG_VALID;
  506. else
  507. tag = FP_EXP_TAG_SPECIAL;
  508. break;
  509. }
  510. } else {
  511. tag = FP_EXP_TAG_EMPTY;
  512. }
  513. ret |= tag << (2 * i);
  514. }
  515. return ret;
  516. }
  517. /*
  518. * FXSR floating point environment conversions.
  519. */
  520. void
  521. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  522. {
  523. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  524. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  525. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  526. int i;
  527. env->cwd = fxsave->cwd | 0xffff0000u;
  528. env->swd = fxsave->swd | 0xffff0000u;
  529. env->twd = twd_fxsr_to_i387(fxsave);
  530. #ifdef CONFIG_X86_64
  531. env->fip = fxsave->rip;
  532. env->foo = fxsave->rdp;
  533. /*
  534. * should be actually ds/cs at fpu exception time, but
  535. * that information is not available in 64bit mode.
  536. */
  537. env->fcs = task_pt_regs(tsk)->cs;
  538. if (tsk == current) {
  539. savesegment(ds, env->fos);
  540. } else {
  541. env->fos = tsk->thread.ds;
  542. }
  543. env->fos |= 0xffff0000;
  544. #else
  545. env->fip = fxsave->fip;
  546. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  547. env->foo = fxsave->foo;
  548. env->fos = fxsave->fos;
  549. #endif
  550. for (i = 0; i < 8; ++i)
  551. memcpy(&to[i], &from[i], sizeof(to[0]));
  552. }
  553. void convert_to_fxsr(struct task_struct *tsk,
  554. const struct user_i387_ia32_struct *env)
  555. {
  556. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  557. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  558. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  559. int i;
  560. fxsave->cwd = env->cwd;
  561. fxsave->swd = env->swd;
  562. fxsave->twd = twd_i387_to_fxsr(env->twd);
  563. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  564. #ifdef CONFIG_X86_64
  565. fxsave->rip = env->fip;
  566. fxsave->rdp = env->foo;
  567. /* cs and ds ignored */
  568. #else
  569. fxsave->fip = env->fip;
  570. fxsave->fcs = (env->fcs & 0xffff);
  571. fxsave->foo = env->foo;
  572. fxsave->fos = env->fos;
  573. #endif
  574. for (i = 0; i < 8; ++i)
  575. memcpy(&to[i], &from[i], sizeof(from[0]));
  576. }
  577. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  578. unsigned int pos, unsigned int count,
  579. void *kbuf, void __user *ubuf)
  580. {
  581. struct fpu *fpu = &target->thread.fpu;
  582. struct user_i387_ia32_struct env;
  583. int ret;
  584. ret = fpu__unlazy_stopped(fpu);
  585. if (ret)
  586. return ret;
  587. if (!static_cpu_has(X86_FEATURE_FPU))
  588. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  589. if (!cpu_has_fxsr)
  590. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  591. &fpu->state->fsave, 0,
  592. -1);
  593. sanitize_i387_state(target);
  594. if (kbuf && pos == 0 && count == sizeof(env)) {
  595. convert_from_fxsr(kbuf, target);
  596. return 0;
  597. }
  598. convert_from_fxsr(&env, target);
  599. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  600. }
  601. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  602. unsigned int pos, unsigned int count,
  603. const void *kbuf, const void __user *ubuf)
  604. {
  605. struct fpu *fpu = &target->thread.fpu;
  606. struct user_i387_ia32_struct env;
  607. int ret;
  608. ret = fpu__unlazy_stopped(fpu);
  609. if (ret)
  610. return ret;
  611. sanitize_i387_state(target);
  612. if (!static_cpu_has(X86_FEATURE_FPU))
  613. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  614. if (!cpu_has_fxsr)
  615. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  616. &fpu->state->fsave, 0,
  617. -1);
  618. if (pos > 0 || count < sizeof(env))
  619. convert_from_fxsr(&env, target);
  620. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  621. if (!ret)
  622. convert_to_fxsr(target, &env);
  623. /*
  624. * update the header bit in the xsave header, indicating the
  625. * presence of FP.
  626. */
  627. if (cpu_has_xsave)
  628. fpu->state->xsave.header.xstate_bv |= XSTATE_FP;
  629. return ret;
  630. }
  631. /*
  632. * FPU state for core dumps.
  633. * This is only used for a.out dumps now.
  634. * It is declared generically using elf_fpregset_t (which is
  635. * struct user_i387_struct) but is in fact only used for 32-bit
  636. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  637. */
  638. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
  639. {
  640. struct task_struct *tsk = current;
  641. struct fpu *fpu = &tsk->thread.fpu;
  642. int fpvalid;
  643. fpvalid = fpu->fpstate_active;
  644. if (fpvalid)
  645. fpvalid = !fpregs_get(tsk, NULL,
  646. 0, sizeof(struct user_i387_ia32_struct),
  647. ufpu, NULL);
  648. return fpvalid;
  649. }
  650. EXPORT_SYMBOL(dump_fpu);
  651. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */