xsave.c 19 KB

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