core.c 18 KB

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