xsave.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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/fpu/api.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. struct fpu *fpu = &tsk->thread.fpu;
  288. int state_size = xstate_size;
  289. u64 xstate_bv = 0;
  290. int fx_only = 0;
  291. ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
  292. config_enabled(CONFIG_IA32_EMULATION));
  293. if (!buf) {
  294. fpu_reset_state(fpu);
  295. return 0;
  296. }
  297. if (!access_ok(VERIFY_READ, buf, size))
  298. return -EACCES;
  299. if (!fpu->fpstate_active && fpstate_alloc_init(fpu))
  300. return -1;
  301. if (!static_cpu_has(X86_FEATURE_FPU))
  302. return fpregs_soft_set(current, NULL,
  303. 0, sizeof(struct user_i387_ia32_struct),
  304. NULL, buf) != 0;
  305. if (use_xsave()) {
  306. struct _fpx_sw_bytes fx_sw_user;
  307. if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
  308. /*
  309. * Couldn't find the extended state information in the
  310. * memory layout. Restore just the FP/SSE and init all
  311. * the other extended state.
  312. */
  313. state_size = sizeof(struct i387_fxsave_struct);
  314. fx_only = 1;
  315. } else {
  316. state_size = fx_sw_user.xstate_size;
  317. xstate_bv = fx_sw_user.xstate_bv;
  318. }
  319. }
  320. if (ia32_fxstate) {
  321. /*
  322. * For 32-bit frames with fxstate, copy the user state to the
  323. * thread's fpu state, reconstruct fxstate from the fsave
  324. * header. Sanitize the copied state etc.
  325. */
  326. struct fpu *fpu = &tsk->thread.fpu;
  327. struct user_i387_ia32_struct env;
  328. int err = 0;
  329. /*
  330. * Drop the current fpu which clears fpu->fpstate_active. This ensures
  331. * that any context-switch during the copy of the new state,
  332. * avoids the intermediate state from getting restored/saved.
  333. * Thus avoiding the new restored state from getting corrupted.
  334. * We will be ready to restore/save the state only after
  335. * fpu->fpstate_active is again set.
  336. */
  337. drop_fpu(fpu);
  338. if (__copy_from_user(&fpu->state->xsave, buf_fx, state_size) ||
  339. __copy_from_user(&env, buf, sizeof(env))) {
  340. fpstate_init(fpu);
  341. err = -1;
  342. } else {
  343. sanitize_restored_xstate(tsk, &env, xstate_bv, fx_only);
  344. }
  345. fpu->fpstate_active = 1;
  346. if (use_eager_fpu()) {
  347. preempt_disable();
  348. fpu__restore();
  349. preempt_enable();
  350. }
  351. return err;
  352. } else {
  353. /*
  354. * For 64-bit frames and 32-bit fsave frames, restore the user
  355. * state to the registers directly (with exceptions handled).
  356. */
  357. user_fpu_begin();
  358. if (restore_user_xstate(buf_fx, xstate_bv, fx_only)) {
  359. fpu_reset_state(fpu);
  360. return -1;
  361. }
  362. }
  363. return 0;
  364. }
  365. /*
  366. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  367. * the presence of the extended state information in the memory layout
  368. * pointed by the fpstate pointer in the sigcontext.
  369. * This will be saved when ever the FP and extended state context is
  370. * saved on the user stack during the signal handler delivery to the user.
  371. */
  372. static void prepare_fx_sw_frame(void)
  373. {
  374. int fsave_header_size = sizeof(struct i387_fsave_struct);
  375. int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
  376. if (config_enabled(CONFIG_X86_32))
  377. size += fsave_header_size;
  378. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  379. fx_sw_reserved.extended_size = size;
  380. fx_sw_reserved.xstate_bv = pcntxt_mask;
  381. fx_sw_reserved.xstate_size = xstate_size;
  382. if (config_enabled(CONFIG_IA32_EMULATION)) {
  383. fx_sw_reserved_ia32 = fx_sw_reserved;
  384. fx_sw_reserved_ia32.extended_size += fsave_header_size;
  385. }
  386. }
  387. /*
  388. * Enable the extended processor state save/restore feature
  389. */
  390. static inline void xstate_enable(void)
  391. {
  392. cr4_set_bits(X86_CR4_OSXSAVE);
  393. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  394. }
  395. /*
  396. * Record the offsets and sizes of different state managed by the xsave
  397. * memory layout.
  398. */
  399. static void __init setup_xstate_features(void)
  400. {
  401. int eax, ebx, ecx, edx, leaf = 0x2;
  402. xstate_features = fls64(pcntxt_mask);
  403. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  404. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  405. do {
  406. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  407. if (eax == 0)
  408. break;
  409. xstate_offsets[leaf] = ebx;
  410. xstate_sizes[leaf] = eax;
  411. leaf++;
  412. } while (1);
  413. }
  414. /*
  415. * This function sets up offsets and sizes of all extended states in
  416. * xsave area. This supports both standard format and compacted format
  417. * of the xsave aread.
  418. *
  419. * Input: void
  420. * Output: void
  421. */
  422. void setup_xstate_comp(void)
  423. {
  424. unsigned int xstate_comp_sizes[sizeof(pcntxt_mask)*8];
  425. int i;
  426. /*
  427. * The FP xstates and SSE xstates are legacy states. They are always
  428. * in the fixed offsets in the xsave area in either compacted form
  429. * or standard form.
  430. */
  431. xstate_comp_offsets[0] = 0;
  432. xstate_comp_offsets[1] = offsetof(struct i387_fxsave_struct, xmm_space);
  433. if (!cpu_has_xsaves) {
  434. for (i = 2; i < xstate_features; i++) {
  435. if (test_bit(i, (unsigned long *)&pcntxt_mask)) {
  436. xstate_comp_offsets[i] = xstate_offsets[i];
  437. xstate_comp_sizes[i] = xstate_sizes[i];
  438. }
  439. }
  440. return;
  441. }
  442. xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE;
  443. for (i = 2; i < xstate_features; i++) {
  444. if (test_bit(i, (unsigned long *)&pcntxt_mask))
  445. xstate_comp_sizes[i] = xstate_sizes[i];
  446. else
  447. xstate_comp_sizes[i] = 0;
  448. if (i > 2)
  449. xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
  450. + xstate_comp_sizes[i-1];
  451. }
  452. }
  453. /*
  454. * setup the xstate image representing the init state
  455. */
  456. static void __init setup_init_fpu_buf(void)
  457. {
  458. /*
  459. * Setup init_xstate_buf to represent the init state of
  460. * all the features managed by the xsave
  461. */
  462. init_xstate_buf = alloc_bootmem_align(xstate_size,
  463. __alignof__(struct xsave_struct));
  464. fx_finit(&init_xstate_buf->i387);
  465. if (!cpu_has_xsave)
  466. return;
  467. setup_xstate_features();
  468. if (cpu_has_xsaves) {
  469. init_xstate_buf->xsave_hdr.xcomp_bv =
  470. (u64)1 << 63 | pcntxt_mask;
  471. init_xstate_buf->xsave_hdr.xstate_bv = pcntxt_mask;
  472. }
  473. /*
  474. * Init all the features state with header_bv being 0x0
  475. */
  476. xrstor_state_booting(init_xstate_buf, -1);
  477. /*
  478. * Dump the init state again. This is to identify the init state
  479. * of any feature which is not represented by all zero's.
  480. */
  481. xsave_state_booting(init_xstate_buf);
  482. }
  483. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  484. static int __init eager_fpu_setup(char *s)
  485. {
  486. if (!strcmp(s, "on"))
  487. eagerfpu = ENABLE;
  488. else if (!strcmp(s, "off"))
  489. eagerfpu = DISABLE;
  490. else if (!strcmp(s, "auto"))
  491. eagerfpu = AUTO;
  492. return 1;
  493. }
  494. __setup("eagerfpu=", eager_fpu_setup);
  495. /*
  496. * Calculate total size of enabled xstates in XCR0/pcntxt_mask.
  497. */
  498. static void __init init_xstate_size(void)
  499. {
  500. unsigned int eax, ebx, ecx, edx;
  501. int i;
  502. if (!cpu_has_xsaves) {
  503. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  504. xstate_size = ebx;
  505. return;
  506. }
  507. xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
  508. for (i = 2; i < 64; i++) {
  509. if (test_bit(i, (unsigned long *)&pcntxt_mask)) {
  510. cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
  511. xstate_size += eax;
  512. }
  513. }
  514. }
  515. /*
  516. * Enable and initialize the xsave feature.
  517. */
  518. static void __init xstate_enable_boot_cpu(void)
  519. {
  520. unsigned int eax, ebx, ecx, edx;
  521. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  522. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  523. return;
  524. }
  525. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  526. pcntxt_mask = eax + ((u64)edx << 32);
  527. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  528. pr_err("FP/SSE not shown under xsave features 0x%llx\n",
  529. pcntxt_mask);
  530. BUG();
  531. }
  532. /*
  533. * Support only the state known to OS.
  534. */
  535. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  536. xstate_enable();
  537. /*
  538. * Recompute the context size for enabled features
  539. */
  540. init_xstate_size();
  541. update_regset_xstate_info(xstate_size, pcntxt_mask);
  542. prepare_fx_sw_frame();
  543. setup_init_fpu_buf();
  544. /* Auto enable eagerfpu for xsaveopt */
  545. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  546. eagerfpu = ENABLE;
  547. if (pcntxt_mask & XSTATE_EAGER) {
  548. if (eagerfpu == DISABLE) {
  549. pr_err("eagerfpu not present, disabling some xstate features: 0x%llx\n",
  550. pcntxt_mask & XSTATE_EAGER);
  551. pcntxt_mask &= ~XSTATE_EAGER;
  552. } else {
  553. eagerfpu = ENABLE;
  554. }
  555. }
  556. pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x using %s\n",
  557. pcntxt_mask, xstate_size,
  558. cpu_has_xsaves ? "compacted form" : "standard form");
  559. }
  560. /*
  561. * For the very first instance, this calls xstate_enable_boot_cpu();
  562. * for all subsequent instances, this calls xstate_enable().
  563. *
  564. * This is somewhat obfuscated due to the lack of powerful enough
  565. * overrides for the section checks.
  566. */
  567. void xsave_init(void)
  568. {
  569. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  570. void (*this_func)(void);
  571. if (!cpu_has_xsave)
  572. return;
  573. this_func = next_func;
  574. next_func = xstate_enable;
  575. this_func();
  576. }
  577. /*
  578. * setup_init_fpu_buf() is __init and it is OK to call it here because
  579. * init_xstate_buf will be unset only once during boot.
  580. */
  581. void __init_refok eager_fpu_init(void)
  582. {
  583. WARN_ON(current->thread.fpu.fpstate_active);
  584. current_thread_info()->status = 0;
  585. if (eagerfpu == ENABLE)
  586. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  587. printk_once(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
  588. if (!cpu_has_eager_fpu) {
  589. stts();
  590. return;
  591. }
  592. if (!init_xstate_buf)
  593. setup_init_fpu_buf();
  594. }
  595. /*
  596. * Given the xsave area and a state inside, this function returns the
  597. * address of the state.
  598. *
  599. * This is the API that is called to get xstate address in either
  600. * standard format or compacted format of xsave area.
  601. *
  602. * Inputs:
  603. * xsave: base address of the xsave area;
  604. * xstate: state which is defined in xsave.h (e.g. XSTATE_FP, XSTATE_SSE,
  605. * etc.)
  606. * Output:
  607. * address of the state in the xsave area.
  608. */
  609. void *get_xsave_addr(struct xsave_struct *xsave, int xstate)
  610. {
  611. int feature = fls64(xstate) - 1;
  612. if (!test_bit(feature, (unsigned long *)&pcntxt_mask))
  613. return NULL;
  614. return (void *)xsave + xstate_comp_offsets[feature];
  615. }
  616. EXPORT_SYMBOL_GPL(get_xsave_addr);