xstate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * xsave/xrstor support.
  3. *
  4. * Author: Suresh Siddha <suresh.b.siddha@intel.com>
  5. */
  6. #include <linux/compat.h>
  7. #include <linux/cpu.h>
  8. #include <asm/fpu/api.h>
  9. #include <asm/fpu/internal.h>
  10. #include <asm/fpu/signal.h>
  11. #include <asm/fpu/regset.h>
  12. #include <asm/tlbflush.h>
  13. static const char *xfeature_names[] =
  14. {
  15. "x87 floating point registers" ,
  16. "SSE registers" ,
  17. "AVX registers" ,
  18. "MPX bounds registers" ,
  19. "MPX CSR" ,
  20. "AVX-512 opmask" ,
  21. "AVX-512 Hi256" ,
  22. "AVX-512 ZMM_Hi256" ,
  23. "unknown xstate feature" ,
  24. };
  25. /*
  26. * Mask of xstate features supported by the CPU and the kernel:
  27. */
  28. u64 xfeatures_mask __read_mostly;
  29. static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
  30. static unsigned int xstate_sizes[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
  31. static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
  32. /*
  33. * Clear all of the X86_FEATURE_* bits that are unavailable
  34. * when the CPU has no XSAVE support.
  35. */
  36. void fpu__xstate_clear_all_cpu_caps(void)
  37. {
  38. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  39. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  40. setup_clear_cpu_cap(X86_FEATURE_XSAVEC);
  41. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  42. setup_clear_cpu_cap(X86_FEATURE_AVX);
  43. setup_clear_cpu_cap(X86_FEATURE_AVX2);
  44. setup_clear_cpu_cap(X86_FEATURE_AVX512F);
  45. setup_clear_cpu_cap(X86_FEATURE_AVX512PF);
  46. setup_clear_cpu_cap(X86_FEATURE_AVX512ER);
  47. setup_clear_cpu_cap(X86_FEATURE_AVX512CD);
  48. setup_clear_cpu_cap(X86_FEATURE_MPX);
  49. setup_clear_cpu_cap(X86_FEATURE_XGETBV1);
  50. }
  51. /*
  52. * Return whether the system supports a given xfeature.
  53. *
  54. * Also return the name of the (most advanced) feature that the caller requested:
  55. */
  56. int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
  57. {
  58. u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
  59. if (unlikely(feature_name)) {
  60. long xfeature_idx, max_idx;
  61. u64 xfeatures_print;
  62. /*
  63. * So we use FLS here to be able to print the most advanced
  64. * feature that was requested but is missing. So if a driver
  65. * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
  66. * missing AVX feature - this is the most informative message
  67. * to users:
  68. */
  69. if (xfeatures_missing)
  70. xfeatures_print = xfeatures_missing;
  71. else
  72. xfeatures_print = xfeatures_needed;
  73. xfeature_idx = fls64(xfeatures_print)-1;
  74. max_idx = ARRAY_SIZE(xfeature_names)-1;
  75. xfeature_idx = min(xfeature_idx, max_idx);
  76. *feature_name = xfeature_names[xfeature_idx];
  77. }
  78. if (xfeatures_missing)
  79. return 0;
  80. return 1;
  81. }
  82. EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
  83. /*
  84. * When executing XSAVEOPT (or other optimized XSAVE instructions), if
  85. * a processor implementation detects that an FPU state component is still
  86. * (or is again) in its initialized state, it may clear the corresponding
  87. * bit in the header.xfeatures field, and can skip the writeout of registers
  88. * to the corresponding memory layout.
  89. *
  90. * This means that when the bit is zero, the state component might still contain
  91. * some previous - non-initialized register state.
  92. *
  93. * Before writing xstate information to user-space we sanitize those components,
  94. * to always ensure that the memory layout of a feature will be in the init state
  95. * if the corresponding header bit is zero. This is to ensure that user-space doesn't
  96. * see some stale state in the memory layout during signal handling, debugging etc.
  97. */
  98. void fpstate_sanitize_xstate(struct fpu *fpu)
  99. {
  100. struct fxregs_state *fx = &fpu->state.fxsave;
  101. int feature_bit;
  102. u64 xfeatures;
  103. if (!use_xsaveopt())
  104. return;
  105. xfeatures = fpu->state.xsave.header.xfeatures;
  106. /*
  107. * None of the feature bits are in init state. So nothing else
  108. * to do for us, as the memory layout is up to date.
  109. */
  110. if ((xfeatures & xfeatures_mask) == xfeatures_mask)
  111. return;
  112. /*
  113. * FP is in init state
  114. */
  115. if (!(xfeatures & XFEATURE_MASK_FP)) {
  116. fx->cwd = 0x37f;
  117. fx->swd = 0;
  118. fx->twd = 0;
  119. fx->fop = 0;
  120. fx->rip = 0;
  121. fx->rdp = 0;
  122. memset(&fx->st_space[0], 0, 128);
  123. }
  124. /*
  125. * SSE is in init state
  126. */
  127. if (!(xfeatures & XFEATURE_MASK_SSE))
  128. memset(&fx->xmm_space[0], 0, 256);
  129. /*
  130. * First two features are FPU and SSE, which above we handled
  131. * in a special way already:
  132. */
  133. feature_bit = 0x2;
  134. xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
  135. /*
  136. * Update all the remaining memory layouts according to their
  137. * standard xstate layout, if their header bit is in the init
  138. * state:
  139. */
  140. while (xfeatures) {
  141. if (xfeatures & 0x1) {
  142. int offset = xstate_offsets[feature_bit];
  143. int size = xstate_sizes[feature_bit];
  144. memcpy((void *)fx + offset,
  145. (void *)&init_fpstate.xsave + offset,
  146. size);
  147. }
  148. xfeatures >>= 1;
  149. feature_bit++;
  150. }
  151. }
  152. /*
  153. * Enable the extended processor state save/restore feature.
  154. * Called once per CPU onlining.
  155. */
  156. void fpu__init_cpu_xstate(void)
  157. {
  158. if (!cpu_has_xsave || !xfeatures_mask)
  159. return;
  160. cr4_set_bits(X86_CR4_OSXSAVE);
  161. xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
  162. }
  163. /*
  164. * Note that in the future we will likely need a pair of
  165. * functions here: one for user xstates and the other for
  166. * system xstates. For now, they are the same.
  167. */
  168. static int xfeature_enabled(enum xfeature xfeature)
  169. {
  170. return !!(xfeatures_mask & (1UL << xfeature));
  171. }
  172. /*
  173. * Record the offsets and sizes of various xstates contained
  174. * in the XSAVE state memory layout.
  175. */
  176. static void __init setup_xstate_features(void)
  177. {
  178. u32 eax, ebx, ecx, edx, i;
  179. /* start at the beginnning of the "extended state" */
  180. unsigned int last_good_offset = offsetof(struct xregs_state,
  181. extended_state_area);
  182. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  183. if (!xfeature_enabled(i))
  184. continue;
  185. cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
  186. xstate_offsets[i] = ebx;
  187. xstate_sizes[i] = eax;
  188. /*
  189. * In our xstate size checks, we assume that the
  190. * highest-numbered xstate feature has the
  191. * highest offset in the buffer. Ensure it does.
  192. */
  193. WARN_ONCE(last_good_offset > xstate_offsets[i],
  194. "x86/fpu: misordered xstate at %d\n", last_good_offset);
  195. last_good_offset = xstate_offsets[i];
  196. printk(KERN_INFO "x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n", i, ebx, i, eax);
  197. }
  198. }
  199. static void __init print_xstate_feature(u64 xstate_mask)
  200. {
  201. const char *feature_name;
  202. if (cpu_has_xfeatures(xstate_mask, &feature_name))
  203. pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask, feature_name);
  204. }
  205. /*
  206. * Print out all the supported xstate features:
  207. */
  208. static void __init print_xstate_features(void)
  209. {
  210. print_xstate_feature(XFEATURE_MASK_FP);
  211. print_xstate_feature(XFEATURE_MASK_SSE);
  212. print_xstate_feature(XFEATURE_MASK_YMM);
  213. print_xstate_feature(XFEATURE_MASK_BNDREGS);
  214. print_xstate_feature(XFEATURE_MASK_BNDCSR);
  215. print_xstate_feature(XFEATURE_MASK_OPMASK);
  216. print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
  217. print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
  218. }
  219. /*
  220. * This function sets up offsets and sizes of all extended states in
  221. * xsave area. This supports both standard format and compacted format
  222. * of the xsave aread.
  223. */
  224. static void __init setup_xstate_comp(void)
  225. {
  226. unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8];
  227. int i;
  228. /*
  229. * The FP xstates and SSE xstates are legacy states. They are always
  230. * in the fixed offsets in the xsave area in either compacted form
  231. * or standard form.
  232. */
  233. xstate_comp_offsets[0] = 0;
  234. xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space);
  235. if (!cpu_has_xsaves) {
  236. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  237. if (xfeature_enabled(i)) {
  238. xstate_comp_offsets[i] = xstate_offsets[i];
  239. xstate_comp_sizes[i] = xstate_sizes[i];
  240. }
  241. }
  242. return;
  243. }
  244. xstate_comp_offsets[FIRST_EXTENDED_XFEATURE] =
  245. FXSAVE_SIZE + XSAVE_HDR_SIZE;
  246. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  247. if (xfeature_enabled(i))
  248. xstate_comp_sizes[i] = xstate_sizes[i];
  249. else
  250. xstate_comp_sizes[i] = 0;
  251. if (i > FIRST_EXTENDED_XFEATURE)
  252. xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
  253. + xstate_comp_sizes[i-1];
  254. }
  255. }
  256. /*
  257. * setup the xstate image representing the init state
  258. */
  259. static void __init setup_init_fpu_buf(void)
  260. {
  261. static int on_boot_cpu __initdata = 1;
  262. WARN_ON_FPU(!on_boot_cpu);
  263. on_boot_cpu = 0;
  264. if (!cpu_has_xsave)
  265. return;
  266. setup_xstate_features();
  267. print_xstate_features();
  268. if (cpu_has_xsaves) {
  269. init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask;
  270. init_fpstate.xsave.header.xfeatures = xfeatures_mask;
  271. }
  272. /*
  273. * Init all the features state with header_bv being 0x0
  274. */
  275. copy_kernel_to_xregs_booting(&init_fpstate.xsave);
  276. /*
  277. * Dump the init state again. This is to identify the init state
  278. * of any feature which is not represented by all zero's.
  279. */
  280. copy_xregs_to_kernel_booting(&init_fpstate.xsave);
  281. }
  282. static int xfeature_is_supervisor(int xfeature_nr)
  283. {
  284. /*
  285. * We currently do not support supervisor states, but if
  286. * we did, we could find out like this.
  287. *
  288. * SDM says: If state component i is a user state component,
  289. * ECX[0] return 0; if state component i is a supervisor
  290. * state component, ECX[0] returns 1.
  291. u32 eax, ebx, ecx, edx;
  292. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx;
  293. return !!(ecx & 1);
  294. */
  295. return 0;
  296. }
  297. /*
  298. static int xfeature_is_user(int xfeature_nr)
  299. {
  300. return !xfeature_is_supervisor(xfeature_nr);
  301. }
  302. */
  303. /*
  304. * This check is important because it is easy to get XSTATE_*
  305. * confused with XSTATE_BIT_*.
  306. */
  307. #define CHECK_XFEATURE(nr) do { \
  308. WARN_ON(nr < FIRST_EXTENDED_XFEATURE); \
  309. WARN_ON(nr >= XFEATURE_MAX); \
  310. } while (0)
  311. /*
  312. * We could cache this like xstate_size[], but we only use
  313. * it here, so it would be a waste of space.
  314. */
  315. static int xfeature_is_aligned(int xfeature_nr)
  316. {
  317. u32 eax, ebx, ecx, edx;
  318. CHECK_XFEATURE(xfeature_nr);
  319. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  320. /*
  321. * The value returned by ECX[1] indicates the alignment
  322. * of state component i when the compacted format
  323. * of the extended region of an XSAVE area is used
  324. */
  325. return !!(ecx & 2);
  326. }
  327. static int xfeature_uncompacted_offset(int xfeature_nr)
  328. {
  329. u32 eax, ebx, ecx, edx;
  330. CHECK_XFEATURE(xfeature_nr);
  331. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  332. return ebx;
  333. }
  334. static int xfeature_size(int xfeature_nr)
  335. {
  336. u32 eax, ebx, ecx, edx;
  337. CHECK_XFEATURE(xfeature_nr);
  338. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  339. return eax;
  340. }
  341. /*
  342. * 'XSAVES' implies two different things:
  343. * 1. saving of supervisor/system state
  344. * 2. using the compacted format
  345. *
  346. * Use this function when dealing with the compacted format so
  347. * that it is obvious which aspect of 'XSAVES' is being handled
  348. * by the calling code.
  349. */
  350. static int using_compacted_format(void)
  351. {
  352. return cpu_has_xsaves;
  353. }
  354. static void __xstate_dump_leaves(void)
  355. {
  356. int i;
  357. u32 eax, ebx, ecx, edx;
  358. static int should_dump = 1;
  359. if (!should_dump)
  360. return;
  361. should_dump = 0;
  362. /*
  363. * Dump out a few leaves past the ones that we support
  364. * just in case there are some goodies up there
  365. */
  366. for (i = 0; i < XFEATURE_MAX + 10; i++) {
  367. cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
  368. pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n",
  369. XSTATE_CPUID, i, eax, ebx, ecx, edx);
  370. }
  371. }
  372. #define XSTATE_WARN_ON(x) do { \
  373. if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \
  374. __xstate_dump_leaves(); \
  375. } \
  376. } while (0)
  377. #define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \
  378. if ((nr == nr_macro) && \
  379. WARN_ONCE(sz != sizeof(__struct), \
  380. "%s: struct is %zu bytes, cpu state %d bytes\n", \
  381. __stringify(nr_macro), sizeof(__struct), sz)) { \
  382. __xstate_dump_leaves(); \
  383. } \
  384. } while (0)
  385. /*
  386. * We have a C struct for each 'xstate'. We need to ensure
  387. * that our software representation matches what the CPU
  388. * tells us about the state's size.
  389. */
  390. static void check_xstate_against_struct(int nr)
  391. {
  392. /*
  393. * Ask the CPU for the size of the state.
  394. */
  395. int sz = xfeature_size(nr);
  396. /*
  397. * Match each CPU state with the corresponding software
  398. * structure.
  399. */
  400. XCHECK_SZ(sz, nr, XFEATURE_YMM, struct ymmh_struct);
  401. XCHECK_SZ(sz, nr, XFEATURE_BNDREGS, struct mpx_bndreg_state);
  402. XCHECK_SZ(sz, nr, XFEATURE_BNDCSR, struct mpx_bndcsr_state);
  403. XCHECK_SZ(sz, nr, XFEATURE_OPMASK, struct avx_512_opmask_state);
  404. XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state);
  405. XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state);
  406. /*
  407. * Make *SURE* to add any feature numbers in below if
  408. * there are "holes" in the xsave state component
  409. * numbers.
  410. */
  411. if ((nr < XFEATURE_YMM) ||
  412. (nr >= XFEATURE_MAX)) {
  413. WARN_ONCE(1, "no structure for xstate: %d\n", nr);
  414. XSTATE_WARN_ON(1);
  415. }
  416. }
  417. /*
  418. * This essentially double-checks what the cpu told us about
  419. * how large the XSAVE buffer needs to be. We are recalculating
  420. * it to be safe.
  421. */
  422. static void do_extra_xstate_size_checks(void)
  423. {
  424. int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
  425. int i;
  426. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  427. if (!xfeature_enabled(i))
  428. continue;
  429. check_xstate_against_struct(i);
  430. /*
  431. * Supervisor state components can be managed only by
  432. * XSAVES, which is compacted-format only.
  433. */
  434. if (!using_compacted_format())
  435. XSTATE_WARN_ON(xfeature_is_supervisor(i));
  436. /* Align from the end of the previous feature */
  437. if (xfeature_is_aligned(i))
  438. paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64);
  439. /*
  440. * The offset of a given state in the non-compacted
  441. * format is given to us in a CPUID leaf. We check
  442. * them for being ordered (increasing offsets) in
  443. * setup_xstate_features().
  444. */
  445. if (!using_compacted_format())
  446. paranoid_xstate_size = xfeature_uncompacted_offset(i);
  447. /*
  448. * The compacted-format offset always depends on where
  449. * the previous state ended.
  450. */
  451. paranoid_xstate_size += xfeature_size(i);
  452. }
  453. XSTATE_WARN_ON(paranoid_xstate_size != xstate_size);
  454. }
  455. /*
  456. * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
  457. *
  458. * Note the SDM's wording here. "sub-function 0" only enumerates
  459. * the size of the *user* states. If we use it to size a buffer
  460. * that we use 'XSAVES' on, we could potentially overflow the
  461. * buffer because 'XSAVES' saves system states too.
  462. *
  463. * Note that we do not currently set any bits on IA32_XSS so
  464. * 'XCR0 | IA32_XSS == XCR0' for now.
  465. */
  466. static unsigned int __init calculate_xstate_size(void)
  467. {
  468. unsigned int eax, ebx, ecx, edx;
  469. unsigned int calculated_xstate_size;
  470. if (!cpu_has_xsaves) {
  471. /*
  472. * - CPUID function 0DH, sub-function 0:
  473. * EBX enumerates the size (in bytes) required by
  474. * the XSAVE instruction for an XSAVE area
  475. * containing all the *user* state components
  476. * corresponding to bits currently set in XCR0.
  477. */
  478. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  479. calculated_xstate_size = ebx;
  480. } else {
  481. /*
  482. * - CPUID function 0DH, sub-function 1:
  483. * EBX enumerates the size (in bytes) required by
  484. * the XSAVES instruction for an XSAVE area
  485. * containing all the state components
  486. * corresponding to bits currently set in
  487. * XCR0 | IA32_XSS.
  488. */
  489. cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
  490. calculated_xstate_size = ebx;
  491. }
  492. return calculated_xstate_size;
  493. }
  494. /*
  495. * Will the runtime-enumerated 'xstate_size' fit in the init
  496. * task's statically-allocated buffer?
  497. */
  498. static bool is_supported_xstate_size(unsigned int test_xstate_size)
  499. {
  500. if (test_xstate_size <= sizeof(union fpregs_state))
  501. return true;
  502. pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
  503. sizeof(union fpregs_state), test_xstate_size);
  504. return false;
  505. }
  506. static int init_xstate_size(void)
  507. {
  508. /* Recompute the context size for enabled features: */
  509. unsigned int possible_xstate_size = calculate_xstate_size();
  510. /* Ensure we have the space to store all enabled: */
  511. if (!is_supported_xstate_size(possible_xstate_size))
  512. return -EINVAL;
  513. /*
  514. * The size is OK, we are definitely going to use xsave,
  515. * make it known to the world that we need more space.
  516. */
  517. xstate_size = possible_xstate_size;
  518. do_extra_xstate_size_checks();
  519. return 0;
  520. }
  521. /*
  522. * We enabled the XSAVE hardware, but something went wrong and
  523. * we can not use it. Disable it.
  524. */
  525. static void fpu__init_disable_system_xstate(void)
  526. {
  527. xfeatures_mask = 0;
  528. cr4_clear_bits(X86_CR4_OSXSAVE);
  529. fpu__xstate_clear_all_cpu_caps();
  530. }
  531. /*
  532. * Enable and initialize the xsave feature.
  533. * Called once per system bootup.
  534. */
  535. void __init fpu__init_system_xstate(void)
  536. {
  537. unsigned int eax, ebx, ecx, edx;
  538. static int on_boot_cpu __initdata = 1;
  539. int err;
  540. WARN_ON_FPU(!on_boot_cpu);
  541. on_boot_cpu = 0;
  542. if (!cpu_has_xsave) {
  543. pr_info("x86/fpu: Legacy x87 FPU detected.\n");
  544. return;
  545. }
  546. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  547. WARN_ON_FPU(1);
  548. return;
  549. }
  550. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  551. xfeatures_mask = eax + ((u64)edx << 32);
  552. if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
  553. pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
  554. BUG();
  555. }
  556. xfeatures_mask &= fpu__get_supported_xfeatures_mask();
  557. /* Enable xstate instructions to be able to continue with initialization: */
  558. fpu__init_cpu_xstate();
  559. err = init_xstate_size();
  560. if (err) {
  561. /* something went wrong, boot without any XSAVE support */
  562. fpu__init_disable_system_xstate();
  563. return;
  564. }
  565. update_regset_xstate_info(xstate_size, xfeatures_mask);
  566. fpu__init_prepare_fx_sw_frame();
  567. setup_init_fpu_buf();
  568. setup_xstate_comp();
  569. pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
  570. xfeatures_mask,
  571. xstate_size,
  572. cpu_has_xsaves ? "compacted" : "standard");
  573. }
  574. /*
  575. * Restore minimal FPU state after suspend:
  576. */
  577. void fpu__resume_cpu(void)
  578. {
  579. /*
  580. * Restore XCR0 on xsave capable CPUs:
  581. */
  582. if (cpu_has_xsave)
  583. xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
  584. }
  585. /*
  586. * Given the xsave area and a state inside, this function returns the
  587. * address of the state.
  588. *
  589. * This is the API that is called to get xstate address in either
  590. * standard format or compacted format of xsave area.
  591. *
  592. * Note that if there is no data for the field in the xsave buffer
  593. * this will return NULL.
  594. *
  595. * Inputs:
  596. * xstate: the thread's storage area for all FPU data
  597. * xstate_feature: state which is defined in xsave.h (e.g.
  598. * XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...)
  599. * Output:
  600. * address of the state in the xsave area, or NULL if the
  601. * field is not present in the xsave buffer.
  602. */
  603. void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
  604. {
  605. int feature_nr = fls64(xstate_feature) - 1;
  606. /*
  607. * Do we even *have* xsave state?
  608. */
  609. if (!boot_cpu_has(X86_FEATURE_XSAVE))
  610. return NULL;
  611. /*
  612. * We should not ever be requesting features that we
  613. * have not enabled. Remember that pcntxt_mask is
  614. * what we write to the XCR0 register.
  615. */
  616. WARN_ONCE(!(xfeatures_mask & xstate_feature),
  617. "get of unsupported state");
  618. /*
  619. * This assumes the last 'xsave*' instruction to
  620. * have requested that 'xstate_feature' be saved.
  621. * If it did not, we might be seeing and old value
  622. * of the field in the buffer.
  623. *
  624. * This can happen because the last 'xsave' did not
  625. * request that this feature be saved (unlikely)
  626. * or because the "init optimization" caused it
  627. * to not be saved.
  628. */
  629. if (!(xsave->header.xfeatures & xstate_feature))
  630. return NULL;
  631. return (void *)xsave + xstate_comp_offsets[feature_nr];
  632. }
  633. EXPORT_SYMBOL_GPL(get_xsave_addr);
  634. /*
  635. * This wraps up the common operations that need to occur when retrieving
  636. * data from xsave state. It first ensures that the current task was
  637. * using the FPU and retrieves the data in to a buffer. It then calculates
  638. * the offset of the requested field in the buffer.
  639. *
  640. * This function is safe to call whether the FPU is in use or not.
  641. *
  642. * Note that this only works on the current task.
  643. *
  644. * Inputs:
  645. * @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP,
  646. * XFEATURE_MASK_SSE, etc...)
  647. * Output:
  648. * address of the state in the xsave area or NULL if the state
  649. * is not present or is in its 'init state'.
  650. */
  651. const void *get_xsave_field_ptr(int xsave_state)
  652. {
  653. struct fpu *fpu = &current->thread.fpu;
  654. if (!fpu->fpstate_active)
  655. return NULL;
  656. /*
  657. * fpu__save() takes the CPU's xstate registers
  658. * and saves them off to the 'fpu memory buffer.
  659. */
  660. fpu__save(fpu);
  661. return get_xsave_addr(&fpu->state.xsave, xsave_state);
  662. }