xsave.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * xsave/xrstor support.
  3. *
  4. * Author: Suresh Siddha <suresh.b.siddha@intel.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/bootmem.h>
  8. #include <linux/compat.h>
  9. #include <linux/cpu.h>
  10. #include <asm/i387.h>
  11. #include <asm/fpu-internal.h>
  12. #include <asm/sigframe.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/xcr.h>
  15. /*
  16. * Supported feature mask by the CPU and the kernel.
  17. */
  18. u64 pcntxt_mask;
  19. /*
  20. * Represents init state for the supported extended state.
  21. */
  22. struct xsave_struct *init_xstate_buf;
  23. static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
  24. static unsigned int *xstate_offsets, *xstate_sizes;
  25. static unsigned int xstate_comp_offsets[sizeof(pcntxt_mask)*8];
  26. static unsigned int xstate_features;
  27. /*
  28. * If a processor implementation discern that a processor state component is
  29. * in its initialized state it may modify the corresponding bit in the
  30. * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
  31. * layout in the case of xsaveopt. While presenting the xstate information to
  32. * the user, we always ensure that the memory layout of a feature will be in
  33. * the init state if the corresponding header bit is zero. This is to ensure
  34. * that the user doesn't see some stale state in the memory layout during
  35. * signal handling, debugging etc.
  36. */
  37. void __sanitize_i387_state(struct task_struct *tsk)
  38. {
  39. struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
  40. int feature_bit = 0x2;
  41. u64 xstate_bv;
  42. if (!fx)
  43. return;
  44. xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
  45. /*
  46. * None of the feature bits are in init state. So nothing else
  47. * to do for us, as the memory layout is up to date.
  48. */
  49. if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
  50. return;
  51. /*
  52. * FP is in init state
  53. */
  54. if (!(xstate_bv & XSTATE_FP)) {
  55. fx->cwd = 0x37f;
  56. fx->swd = 0;
  57. fx->twd = 0;
  58. fx->fop = 0;
  59. fx->rip = 0;
  60. fx->rdp = 0;
  61. memset(&fx->st_space[0], 0, 128);
  62. }
  63. /*
  64. * SSE is in init state
  65. */
  66. if (!(xstate_bv & XSTATE_SSE))
  67. memset(&fx->xmm_space[0], 0, 256);
  68. xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
  69. /*
  70. * Update all the other memory layouts for which the corresponding
  71. * header bit is in the init state.
  72. */
  73. while (xstate_bv) {
  74. if (xstate_bv & 0x1) {
  75. int offset = xstate_offsets[feature_bit];
  76. int size = xstate_sizes[feature_bit];
  77. memcpy(((void *) fx) + offset,
  78. ((void *) init_xstate_buf) + offset,
  79. size);
  80. }
  81. xstate_bv >>= 1;
  82. feature_bit++;
  83. }
  84. }
  85. /*
  86. * Check for the presence of extended state information in the
  87. * user fpstate pointer in the sigcontext.
  88. */
  89. static inline int check_for_xstate(struct i387_fxsave_struct __user *buf,
  90. void __user *fpstate,
  91. struct _fpx_sw_bytes *fx_sw)
  92. {
  93. int min_xstate_size = sizeof(struct i387_fxsave_struct) +
  94. sizeof(struct xsave_hdr_struct);
  95. unsigned int magic2;
  96. if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
  97. return -1;
  98. /* Check for the first magic field and other error scenarios. */
  99. if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
  100. fx_sw->xstate_size < min_xstate_size ||
  101. fx_sw->xstate_size > xstate_size ||
  102. fx_sw->xstate_size > fx_sw->extended_size)
  103. return -1;
  104. /*
  105. * Check for the presence of second magic word at the end of memory
  106. * layout. This detects the case where the user just copied the legacy
  107. * fpstate layout with out copying the extended state information
  108. * in the memory layout.
  109. */
  110. if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
  111. || magic2 != FP_XSTATE_MAGIC2)
  112. return -1;
  113. return 0;
  114. }
  115. /*
  116. * Signal frame handlers.
  117. */
  118. static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
  119. {
  120. if (use_fxsr()) {
  121. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  122. struct user_i387_ia32_struct env;
  123. struct _fpstate_ia32 __user *fp = buf;
  124. convert_from_fxsr(&env, tsk);
  125. if (__copy_to_user(buf, &env, sizeof(env)) ||
  126. __put_user(xsave->i387.swd, &fp->status) ||
  127. __put_user(X86_FXSR_MAGIC, &fp->magic))
  128. return -1;
  129. } else {
  130. struct i387_fsave_struct __user *fp = buf;
  131. u32 swd;
  132. if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
  138. {
  139. struct xsave_struct __user *x = buf;
  140. struct _fpx_sw_bytes *sw_bytes;
  141. u32 xstate_bv;
  142. int err;
  143. /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
  144. sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
  145. err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
  146. if (!use_xsave())
  147. return err;
  148. err |= __put_user(FP_XSTATE_MAGIC2, (__u32 *)(buf + xstate_size));
  149. /*
  150. * Read the xstate_bv which we copied (directly from the cpu or
  151. * from the state in task struct) to the user buffers.
  152. */
  153. err |= __get_user(xstate_bv, (__u32 *)&x->xsave_hdr.xstate_bv);
  154. /*
  155. * For legacy compatible, we always set FP/SSE bits in the bit
  156. * vector while saving the state to the user context. This will
  157. * enable us capturing any changes(during sigreturn) to
  158. * the FP/SSE bits by the legacy applications which don't touch
  159. * xstate_bv in the xsave header.
  160. *
  161. * xsave aware apps can change the xstate_bv in the xsave
  162. * header as well as change any contents in the memory layout.
  163. * xrestore as part of sigreturn will capture all the changes.
  164. */
  165. xstate_bv |= XSTATE_FPSSE;
  166. err |= __put_user(xstate_bv, (__u32 *)&x->xsave_hdr.xstate_bv);
  167. return err;
  168. }
  169. static inline int save_user_xstate(struct xsave_struct __user *buf)
  170. {
  171. int err;
  172. if (use_xsave())
  173. err = xsave_user(buf);
  174. else if (use_fxsr())
  175. err = fxsave_user((struct i387_fxsave_struct __user *) buf);
  176. else
  177. err = fsave_user((struct i387_fsave_struct __user *) buf);
  178. if (unlikely(err) && __clear_user(buf, xstate_size))
  179. err = -EFAULT;
  180. return err;
  181. }
  182. /*
  183. * Save the fpu, extended register state to the user signal frame.
  184. *
  185. * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
  186. * state is copied.
  187. * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
  188. *
  189. * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
  190. * buf != buf_fx for 32-bit frames with fxstate.
  191. *
  192. * If the fpu, extended register state is live, save the state directly
  193. * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
  194. * copy the thread's fpu state to the user frame starting at 'buf_fx'.
  195. *
  196. * If this is a 32-bit frame with fxstate, put a fsave header before
  197. * the aligned state at 'buf_fx'.
  198. *
  199. * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
  200. * indicating the absence/presence of the extended state to the user.
  201. */
  202. int save_xstate_sig(void __user *buf, void __user *buf_fx, int size)
  203. {
  204. struct xsave_struct *xsave = &current->thread.fpu.state->xsave;
  205. struct task_struct *tsk = current;
  206. int ia32_fxstate = (buf != buf_fx);
  207. ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
  208. config_enabled(CONFIG_IA32_EMULATION));
  209. if (!access_ok(VERIFY_WRITE, buf, size))
  210. return -EACCES;
  211. if (!static_cpu_has(X86_FEATURE_FPU))
  212. return fpregs_soft_get(current, NULL, 0,
  213. sizeof(struct user_i387_ia32_struct), NULL,
  214. (struct _fpstate_ia32 __user *) buf) ? -1 : 1;
  215. if (user_has_fpu()) {
  216. /* Save the live register state to the user directly. */
  217. if (save_user_xstate(buf_fx))
  218. return -1;
  219. /* Update the thread's fxstate to save the fsave header. */
  220. if (ia32_fxstate)
  221. fpu_fxsave(&tsk->thread.fpu);
  222. } else {
  223. sanitize_i387_state(tsk);
  224. if (__copy_to_user(buf_fx, xsave, xstate_size))
  225. return -1;
  226. }
  227. /* Save the fsave header for the 32-bit frames. */
  228. if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
  229. return -1;
  230. if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
  231. return -1;
  232. return 0;
  233. }
  234. static inline void
  235. sanitize_restored_xstate(struct task_struct *tsk,
  236. struct user_i387_ia32_struct *ia32_env,
  237. u64 xstate_bv, int fx_only)
  238. {
  239. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  240. struct xsave_hdr_struct *xsave_hdr = &xsave->xsave_hdr;
  241. if (use_xsave()) {
  242. /* These bits must be zero. */
  243. memset(xsave_hdr->reserved, 0, 48);
  244. /*
  245. * Init the state that is not present in the memory
  246. * layout and not enabled by the OS.
  247. */
  248. if (fx_only)
  249. xsave_hdr->xstate_bv = XSTATE_FPSSE;
  250. else
  251. xsave_hdr->xstate_bv &= (pcntxt_mask & xstate_bv);
  252. }
  253. if (use_fxsr()) {
  254. /*
  255. * mscsr reserved bits must be masked to zero for security
  256. * reasons.
  257. */
  258. xsave->i387.mxcsr &= mxcsr_feature_mask;
  259. convert_to_fxsr(tsk, ia32_env);
  260. }
  261. }
  262. /*
  263. * Restore the extended state if present. Otherwise, restore the FP/SSE state.
  264. */
  265. static inline int restore_user_xstate(void __user *buf, u64 xbv, int fx_only)
  266. {
  267. if (use_xsave()) {
  268. if ((unsigned long)buf % 64 || fx_only) {
  269. u64 init_bv = pcntxt_mask & ~XSTATE_FPSSE;
  270. xrstor_state(init_xstate_buf, init_bv);
  271. return fxrstor_user(buf);
  272. } else {
  273. u64 init_bv = pcntxt_mask & ~xbv;
  274. if (unlikely(init_bv))
  275. xrstor_state(init_xstate_buf, init_bv);
  276. return xrestore_user(buf, xbv);
  277. }
  278. } else if (use_fxsr()) {
  279. return fxrstor_user(buf);
  280. } else
  281. return frstor_user(buf);
  282. }
  283. int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)
  284. {
  285. int ia32_fxstate = (buf != buf_fx);
  286. struct task_struct *tsk = current;
  287. int state_size = xstate_size;
  288. u64 xstate_bv = 0;
  289. int fx_only = 0;
  290. ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
  291. config_enabled(CONFIG_IA32_EMULATION));
  292. if (!buf) {
  293. fpu_reset_state(tsk);
  294. return 0;
  295. }
  296. if (!access_ok(VERIFY_READ, buf, size))
  297. return -EACCES;
  298. if (!used_math() && init_fpu(tsk))
  299. return -1;
  300. if (!static_cpu_has(X86_FEATURE_FPU))
  301. return fpregs_soft_set(current, NULL,
  302. 0, sizeof(struct user_i387_ia32_struct),
  303. NULL, buf) != 0;
  304. if (use_xsave()) {
  305. struct _fpx_sw_bytes fx_sw_user;
  306. if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
  307. /*
  308. * Couldn't find the extended state information in the
  309. * memory layout. Restore just the FP/SSE and init all
  310. * the other extended state.
  311. */
  312. state_size = sizeof(struct i387_fxsave_struct);
  313. fx_only = 1;
  314. } else {
  315. state_size = fx_sw_user.xstate_size;
  316. xstate_bv = fx_sw_user.xstate_bv;
  317. }
  318. }
  319. if (ia32_fxstate) {
  320. /*
  321. * For 32-bit frames with fxstate, copy the user state to the
  322. * thread's fpu state, reconstruct fxstate from the fsave
  323. * header. Sanitize the copied state etc.
  324. */
  325. struct fpu *fpu = &tsk->thread.fpu;
  326. struct user_i387_ia32_struct env;
  327. int err = 0;
  328. /*
  329. * Drop the current fpu which clears used_math(). This ensures
  330. * that any context-switch during the copy of the new state,
  331. * avoids the intermediate state from getting restored/saved.
  332. * Thus avoiding the new restored state from getting corrupted.
  333. * We will be ready to restore/save the state only after
  334. * set_used_math() is again set.
  335. */
  336. drop_fpu(tsk);
  337. if (__copy_from_user(&fpu->state->xsave, buf_fx, state_size) ||
  338. __copy_from_user(&env, buf, sizeof(env))) {
  339. fpu_finit(fpu);
  340. err = -1;
  341. } else {
  342. sanitize_restored_xstate(tsk, &env, xstate_bv, fx_only);
  343. }
  344. set_used_math();
  345. if (use_eager_fpu()) {
  346. preempt_disable();
  347. math_state_restore();
  348. preempt_enable();
  349. }
  350. return err;
  351. } else {
  352. /*
  353. * For 64-bit frames and 32-bit fsave frames, restore the user
  354. * state to the registers directly (with exceptions handled).
  355. */
  356. user_fpu_begin();
  357. if (restore_user_xstate(buf_fx, xstate_bv, fx_only)) {
  358. fpu_reset_state(tsk);
  359. return -1;
  360. }
  361. }
  362. return 0;
  363. }
  364. /*
  365. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  366. * the presence of the extended state information in the memory layout
  367. * pointed by the fpstate pointer in the sigcontext.
  368. * This will be saved when ever the FP and extended state context is
  369. * saved on the user stack during the signal handler delivery to the user.
  370. */
  371. static void prepare_fx_sw_frame(void)
  372. {
  373. int fsave_header_size = sizeof(struct i387_fsave_struct);
  374. int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
  375. if (config_enabled(CONFIG_X86_32))
  376. size += fsave_header_size;
  377. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  378. fx_sw_reserved.extended_size = size;
  379. fx_sw_reserved.xstate_bv = pcntxt_mask;
  380. fx_sw_reserved.xstate_size = xstate_size;
  381. if (config_enabled(CONFIG_IA32_EMULATION)) {
  382. fx_sw_reserved_ia32 = fx_sw_reserved;
  383. fx_sw_reserved_ia32.extended_size += fsave_header_size;
  384. }
  385. }
  386. /*
  387. * Enable the extended processor state save/restore feature
  388. */
  389. static inline void xstate_enable(void)
  390. {
  391. cr4_set_bits(X86_CR4_OSXSAVE);
  392. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  393. }
  394. /*
  395. * Record the offsets and sizes of different state managed by the xsave
  396. * memory layout.
  397. */
  398. static void __init setup_xstate_features(void)
  399. {
  400. int eax, ebx, ecx, edx, leaf = 0x2;
  401. xstate_features = fls64(pcntxt_mask);
  402. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  403. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  404. do {
  405. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  406. if (eax == 0)
  407. break;
  408. xstate_offsets[leaf] = ebx;
  409. xstate_sizes[leaf] = eax;
  410. leaf++;
  411. } while (1);
  412. }
  413. /*
  414. * This function sets up offsets and sizes of all extended states in
  415. * xsave area. This supports both standard format and compacted format
  416. * of the xsave aread.
  417. *
  418. * Input: void
  419. * Output: void
  420. */
  421. void setup_xstate_comp(void)
  422. {
  423. unsigned int xstate_comp_sizes[sizeof(pcntxt_mask)*8];
  424. int i;
  425. /*
  426. * The FP xstates and SSE xstates are legacy states. They are always
  427. * in the fixed offsets in the xsave area in either compacted form
  428. * or standard form.
  429. */
  430. xstate_comp_offsets[0] = 0;
  431. xstate_comp_offsets[1] = offsetof(struct i387_fxsave_struct, xmm_space);
  432. if (!cpu_has_xsaves) {
  433. for (i = 2; i < xstate_features; i++) {
  434. if (test_bit(i, (unsigned long *)&pcntxt_mask)) {
  435. xstate_comp_offsets[i] = xstate_offsets[i];
  436. xstate_comp_sizes[i] = xstate_sizes[i];
  437. }
  438. }
  439. return;
  440. }
  441. xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE;
  442. for (i = 2; i < xstate_features; i++) {
  443. if (test_bit(i, (unsigned long *)&pcntxt_mask))
  444. xstate_comp_sizes[i] = xstate_sizes[i];
  445. else
  446. xstate_comp_sizes[i] = 0;
  447. if (i > 2)
  448. xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
  449. + xstate_comp_sizes[i-1];
  450. }
  451. }
  452. /*
  453. * setup the xstate image representing the init state
  454. */
  455. static void __init setup_init_fpu_buf(void)
  456. {
  457. /*
  458. * Setup init_xstate_buf to represent the init state of
  459. * all the features managed by the xsave
  460. */
  461. init_xstate_buf = alloc_bootmem_align(xstate_size,
  462. __alignof__(struct xsave_struct));
  463. fx_finit(&init_xstate_buf->i387);
  464. if (!cpu_has_xsave)
  465. return;
  466. setup_xstate_features();
  467. if (cpu_has_xsaves) {
  468. init_xstate_buf->xsave_hdr.xcomp_bv =
  469. (u64)1 << 63 | pcntxt_mask;
  470. init_xstate_buf->xsave_hdr.xstate_bv = pcntxt_mask;
  471. }
  472. /*
  473. * Init all the features state with header_bv being 0x0
  474. */
  475. xrstor_state_booting(init_xstate_buf, -1);
  476. /*
  477. * Dump the init state again. This is to identify the init state
  478. * of any feature which is not represented by all zero's.
  479. */
  480. xsave_state_booting(init_xstate_buf, -1);
  481. }
  482. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  483. static int __init eager_fpu_setup(char *s)
  484. {
  485. if (!strcmp(s, "on"))
  486. eagerfpu = ENABLE;
  487. else if (!strcmp(s, "off"))
  488. eagerfpu = DISABLE;
  489. else if (!strcmp(s, "auto"))
  490. eagerfpu = AUTO;
  491. return 1;
  492. }
  493. __setup("eagerfpu=", eager_fpu_setup);
  494. /*
  495. * Calculate total size of enabled xstates in XCR0/pcntxt_mask.
  496. */
  497. static void __init init_xstate_size(void)
  498. {
  499. unsigned int eax, ebx, ecx, edx;
  500. int i;
  501. if (!cpu_has_xsaves) {
  502. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  503. xstate_size = ebx;
  504. return;
  505. }
  506. xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
  507. for (i = 2; i < 64; i++) {
  508. if (test_bit(i, (unsigned long *)&pcntxt_mask)) {
  509. cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
  510. xstate_size += eax;
  511. }
  512. }
  513. }
  514. /*
  515. * Enable and initialize the xsave feature.
  516. */
  517. static void __init xstate_enable_boot_cpu(void)
  518. {
  519. unsigned int eax, ebx, ecx, edx;
  520. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  521. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  522. return;
  523. }
  524. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  525. pcntxt_mask = eax + ((u64)edx << 32);
  526. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  527. pr_err("FP/SSE not shown under xsave features 0x%llx\n",
  528. pcntxt_mask);
  529. BUG();
  530. }
  531. /*
  532. * Support only the state known to OS.
  533. */
  534. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  535. xstate_enable();
  536. /*
  537. * Recompute the context size for enabled features
  538. */
  539. init_xstate_size();
  540. update_regset_xstate_info(xstate_size, pcntxt_mask);
  541. prepare_fx_sw_frame();
  542. setup_init_fpu_buf();
  543. /* Auto enable eagerfpu for xsaveopt */
  544. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  545. eagerfpu = ENABLE;
  546. if (pcntxt_mask & XSTATE_EAGER) {
  547. if (eagerfpu == DISABLE) {
  548. pr_err("eagerfpu not present, disabling some xstate features: 0x%llx\n",
  549. pcntxt_mask & XSTATE_EAGER);
  550. pcntxt_mask &= ~XSTATE_EAGER;
  551. } else {
  552. eagerfpu = ENABLE;
  553. }
  554. }
  555. pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x using %s\n",
  556. pcntxt_mask, xstate_size,
  557. cpu_has_xsaves ? "compacted form" : "standard form");
  558. }
  559. /*
  560. * For the very first instance, this calls xstate_enable_boot_cpu();
  561. * for all subsequent instances, this calls xstate_enable().
  562. *
  563. * This is somewhat obfuscated due to the lack of powerful enough
  564. * overrides for the section checks.
  565. */
  566. void xsave_init(void)
  567. {
  568. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  569. void (*this_func)(void);
  570. if (!cpu_has_xsave)
  571. return;
  572. this_func = next_func;
  573. next_func = xstate_enable;
  574. this_func();
  575. }
  576. /*
  577. * setup_init_fpu_buf() is __init and it is OK to call it here because
  578. * init_xstate_buf will be unset only once during boot.
  579. */
  580. void __init_refok eager_fpu_init(void)
  581. {
  582. WARN_ON(used_math());
  583. current_thread_info()->status = 0;
  584. if (eagerfpu == ENABLE)
  585. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  586. if (!cpu_has_eager_fpu) {
  587. stts();
  588. return;
  589. }
  590. if (!init_xstate_buf)
  591. setup_init_fpu_buf();
  592. }
  593. /*
  594. * Given the xsave area and a state inside, this function returns the
  595. * address of the state.
  596. *
  597. * This is the API that is called to get xstate address in either
  598. * standard format or compacted format of xsave area.
  599. *
  600. * Inputs:
  601. * xsave: base address of the xsave area;
  602. * xstate: state which is defined in xsave.h (e.g. XSTATE_FP, XSTATE_SSE,
  603. * etc.)
  604. * Output:
  605. * address of the state in the xsave area.
  606. */
  607. void *get_xsave_addr(struct xsave_struct *xsave, int xstate)
  608. {
  609. int feature = fls64(xstate) - 1;
  610. if (!test_bit(feature, (unsigned long *)&pcntxt_mask))
  611. return NULL;
  612. return (void *)xsave + xstate_comp_offsets[feature];
  613. }
  614. EXPORT_SYMBOL_GPL(get_xsave_addr);