xstate.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 <linux/mman.h>
  9. #include <linux/pkeys.h>
  10. #include <asm/fpu/api.h>
  11. #include <asm/fpu/internal.h>
  12. #include <asm/fpu/signal.h>
  13. #include <asm/fpu/regset.h>
  14. #include <asm/fpu/xstate.h>
  15. #include <asm/tlbflush.h>
  16. /*
  17. * Although we spell it out in here, the Processor Trace
  18. * xfeature is completely unused. We use other mechanisms
  19. * to save/restore PT state in Linux.
  20. */
  21. static const char *xfeature_names[] =
  22. {
  23. "x87 floating point registers" ,
  24. "SSE registers" ,
  25. "AVX registers" ,
  26. "MPX bounds registers" ,
  27. "MPX CSR" ,
  28. "AVX-512 opmask" ,
  29. "AVX-512 Hi256" ,
  30. "AVX-512 ZMM_Hi256" ,
  31. "Processor Trace (unused)" ,
  32. "Protection Keys User registers",
  33. "unknown xstate feature" ,
  34. };
  35. /*
  36. * Mask of xstate features supported by the CPU and the kernel:
  37. */
  38. u64 xfeatures_mask __read_mostly;
  39. static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
  40. static unsigned int xstate_sizes[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
  41. static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
  42. /*
  43. * The XSAVE area of kernel can be in standard or compacted format;
  44. * it is always in standard format for user mode. This is the user
  45. * mode standard format size used for signal and ptrace frames.
  46. */
  47. unsigned int fpu_user_xstate_size;
  48. /*
  49. * Clear all of the X86_FEATURE_* bits that are unavailable
  50. * when the CPU has no XSAVE support.
  51. */
  52. void fpu__xstate_clear_all_cpu_caps(void)
  53. {
  54. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  55. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  56. setup_clear_cpu_cap(X86_FEATURE_XSAVEC);
  57. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  58. setup_clear_cpu_cap(X86_FEATURE_AVX);
  59. setup_clear_cpu_cap(X86_FEATURE_AVX2);
  60. setup_clear_cpu_cap(X86_FEATURE_AVX512F);
  61. setup_clear_cpu_cap(X86_FEATURE_AVX512IFMA);
  62. setup_clear_cpu_cap(X86_FEATURE_AVX512PF);
  63. setup_clear_cpu_cap(X86_FEATURE_AVX512ER);
  64. setup_clear_cpu_cap(X86_FEATURE_AVX512CD);
  65. setup_clear_cpu_cap(X86_FEATURE_AVX512DQ);
  66. setup_clear_cpu_cap(X86_FEATURE_AVX512BW);
  67. setup_clear_cpu_cap(X86_FEATURE_AVX512VL);
  68. setup_clear_cpu_cap(X86_FEATURE_MPX);
  69. setup_clear_cpu_cap(X86_FEATURE_XGETBV1);
  70. setup_clear_cpu_cap(X86_FEATURE_AVX512VBMI);
  71. setup_clear_cpu_cap(X86_FEATURE_PKU);
  72. setup_clear_cpu_cap(X86_FEATURE_AVX512_4VNNIW);
  73. setup_clear_cpu_cap(X86_FEATURE_AVX512_4FMAPS);
  74. }
  75. /*
  76. * Return whether the system supports a given xfeature.
  77. *
  78. * Also return the name of the (most advanced) feature that the caller requested:
  79. */
  80. int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
  81. {
  82. u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
  83. if (unlikely(feature_name)) {
  84. long xfeature_idx, max_idx;
  85. u64 xfeatures_print;
  86. /*
  87. * So we use FLS here to be able to print the most advanced
  88. * feature that was requested but is missing. So if a driver
  89. * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
  90. * missing AVX feature - this is the most informative message
  91. * to users:
  92. */
  93. if (xfeatures_missing)
  94. xfeatures_print = xfeatures_missing;
  95. else
  96. xfeatures_print = xfeatures_needed;
  97. xfeature_idx = fls64(xfeatures_print)-1;
  98. max_idx = ARRAY_SIZE(xfeature_names)-1;
  99. xfeature_idx = min(xfeature_idx, max_idx);
  100. *feature_name = xfeature_names[xfeature_idx];
  101. }
  102. if (xfeatures_missing)
  103. return 0;
  104. return 1;
  105. }
  106. EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
  107. static int xfeature_is_supervisor(int xfeature_nr)
  108. {
  109. /*
  110. * We currently do not support supervisor states, but if
  111. * we did, we could find out like this.
  112. *
  113. * SDM says: If state component 'i' is a user state component,
  114. * ECX[0] return 0; if state component i is a supervisor
  115. * state component, ECX[0] returns 1.
  116. */
  117. u32 eax, ebx, ecx, edx;
  118. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  119. return !!(ecx & 1);
  120. }
  121. static int xfeature_is_user(int xfeature_nr)
  122. {
  123. return !xfeature_is_supervisor(xfeature_nr);
  124. }
  125. /*
  126. * When executing XSAVEOPT (or other optimized XSAVE instructions), if
  127. * a processor implementation detects that an FPU state component is still
  128. * (or is again) in its initialized state, it may clear the corresponding
  129. * bit in the header.xfeatures field, and can skip the writeout of registers
  130. * to the corresponding memory layout.
  131. *
  132. * This means that when the bit is zero, the state component might still contain
  133. * some previous - non-initialized register state.
  134. *
  135. * Before writing xstate information to user-space we sanitize those components,
  136. * to always ensure that the memory layout of a feature will be in the init state
  137. * if the corresponding header bit is zero. This is to ensure that user-space doesn't
  138. * see some stale state in the memory layout during signal handling, debugging etc.
  139. */
  140. void fpstate_sanitize_xstate(struct fpu *fpu)
  141. {
  142. struct fxregs_state *fx = &fpu->state.fxsave;
  143. int feature_bit;
  144. u64 xfeatures;
  145. if (!use_xsaveopt())
  146. return;
  147. xfeatures = fpu->state.xsave.header.xfeatures;
  148. /*
  149. * None of the feature bits are in init state. So nothing else
  150. * to do for us, as the memory layout is up to date.
  151. */
  152. if ((xfeatures & xfeatures_mask) == xfeatures_mask)
  153. return;
  154. /*
  155. * FP is in init state
  156. */
  157. if (!(xfeatures & XFEATURE_MASK_FP)) {
  158. fx->cwd = 0x37f;
  159. fx->swd = 0;
  160. fx->twd = 0;
  161. fx->fop = 0;
  162. fx->rip = 0;
  163. fx->rdp = 0;
  164. memset(&fx->st_space[0], 0, 128);
  165. }
  166. /*
  167. * SSE is in init state
  168. */
  169. if (!(xfeatures & XFEATURE_MASK_SSE))
  170. memset(&fx->xmm_space[0], 0, 256);
  171. /*
  172. * First two features are FPU and SSE, which above we handled
  173. * in a special way already:
  174. */
  175. feature_bit = 0x2;
  176. xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
  177. /*
  178. * Update all the remaining memory layouts according to their
  179. * standard xstate layout, if their header bit is in the init
  180. * state:
  181. */
  182. while (xfeatures) {
  183. if (xfeatures & 0x1) {
  184. int offset = xstate_comp_offsets[feature_bit];
  185. int size = xstate_sizes[feature_bit];
  186. memcpy((void *)fx + offset,
  187. (void *)&init_fpstate.xsave + offset,
  188. size);
  189. }
  190. xfeatures >>= 1;
  191. feature_bit++;
  192. }
  193. }
  194. /*
  195. * Enable the extended processor state save/restore feature.
  196. * Called once per CPU onlining.
  197. */
  198. void fpu__init_cpu_xstate(void)
  199. {
  200. if (!boot_cpu_has(X86_FEATURE_XSAVE) || !xfeatures_mask)
  201. return;
  202. /*
  203. * Make it clear that XSAVES supervisor states are not yet
  204. * implemented should anyone expect it to work by changing
  205. * bits in XFEATURE_MASK_* macros and XCR0.
  206. */
  207. WARN_ONCE((xfeatures_mask & XFEATURE_MASK_SUPERVISOR),
  208. "x86/fpu: XSAVES supervisor states are not yet implemented.\n");
  209. xfeatures_mask &= ~XFEATURE_MASK_SUPERVISOR;
  210. cr4_set_bits(X86_CR4_OSXSAVE);
  211. xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
  212. }
  213. /*
  214. * Note that in the future we will likely need a pair of
  215. * functions here: one for user xstates and the other for
  216. * system xstates. For now, they are the same.
  217. */
  218. static int xfeature_enabled(enum xfeature xfeature)
  219. {
  220. return !!(xfeatures_mask & (1UL << xfeature));
  221. }
  222. /*
  223. * Record the offsets and sizes of various xstates contained
  224. * in the XSAVE state memory layout.
  225. */
  226. static void __init setup_xstate_features(void)
  227. {
  228. u32 eax, ebx, ecx, edx, i;
  229. /* start at the beginnning of the "extended state" */
  230. unsigned int last_good_offset = offsetof(struct xregs_state,
  231. extended_state_area);
  232. /*
  233. * The FP xstates and SSE xstates are legacy states. They are always
  234. * in the fixed offsets in the xsave area in either compacted form
  235. * or standard form.
  236. */
  237. xstate_offsets[0] = 0;
  238. xstate_sizes[0] = offsetof(struct fxregs_state, xmm_space);
  239. xstate_offsets[1] = xstate_sizes[0];
  240. xstate_sizes[1] = FIELD_SIZEOF(struct fxregs_state, xmm_space);
  241. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  242. if (!xfeature_enabled(i))
  243. continue;
  244. cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
  245. /*
  246. * If an xfeature is supervisor state, the offset
  247. * in EBX is invalid. We leave it to -1.
  248. */
  249. if (xfeature_is_user(i))
  250. xstate_offsets[i] = ebx;
  251. xstate_sizes[i] = eax;
  252. /*
  253. * In our xstate size checks, we assume that the
  254. * highest-numbered xstate feature has the
  255. * highest offset in the buffer. Ensure it does.
  256. */
  257. WARN_ONCE(last_good_offset > xstate_offsets[i],
  258. "x86/fpu: misordered xstate at %d\n", last_good_offset);
  259. last_good_offset = xstate_offsets[i];
  260. }
  261. }
  262. static void __init print_xstate_feature(u64 xstate_mask)
  263. {
  264. const char *feature_name;
  265. if (cpu_has_xfeatures(xstate_mask, &feature_name))
  266. pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", xstate_mask, feature_name);
  267. }
  268. /*
  269. * Print out all the supported xstate features:
  270. */
  271. static void __init print_xstate_features(void)
  272. {
  273. print_xstate_feature(XFEATURE_MASK_FP);
  274. print_xstate_feature(XFEATURE_MASK_SSE);
  275. print_xstate_feature(XFEATURE_MASK_YMM);
  276. print_xstate_feature(XFEATURE_MASK_BNDREGS);
  277. print_xstate_feature(XFEATURE_MASK_BNDCSR);
  278. print_xstate_feature(XFEATURE_MASK_OPMASK);
  279. print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
  280. print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
  281. print_xstate_feature(XFEATURE_MASK_PKRU);
  282. }
  283. /*
  284. * This check is important because it is easy to get XSTATE_*
  285. * confused with XSTATE_BIT_*.
  286. */
  287. #define CHECK_XFEATURE(nr) do { \
  288. WARN_ON(nr < FIRST_EXTENDED_XFEATURE); \
  289. WARN_ON(nr >= XFEATURE_MAX); \
  290. } while (0)
  291. /*
  292. * We could cache this like xstate_size[], but we only use
  293. * it here, so it would be a waste of space.
  294. */
  295. static int xfeature_is_aligned(int xfeature_nr)
  296. {
  297. u32 eax, ebx, ecx, edx;
  298. CHECK_XFEATURE(xfeature_nr);
  299. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  300. /*
  301. * The value returned by ECX[1] indicates the alignment
  302. * of state component 'i' when the compacted format
  303. * of the extended region of an XSAVE area is used:
  304. */
  305. return !!(ecx & 2);
  306. }
  307. /*
  308. * This function sets up offsets and sizes of all extended states in
  309. * xsave area. This supports both standard format and compacted format
  310. * of the xsave aread.
  311. */
  312. static void __init setup_xstate_comp(void)
  313. {
  314. unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8];
  315. int i;
  316. /*
  317. * The FP xstates and SSE xstates are legacy states. They are always
  318. * in the fixed offsets in the xsave area in either compacted form
  319. * or standard form.
  320. */
  321. xstate_comp_offsets[0] = 0;
  322. xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space);
  323. if (!boot_cpu_has(X86_FEATURE_XSAVES)) {
  324. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  325. if (xfeature_enabled(i)) {
  326. xstate_comp_offsets[i] = xstate_offsets[i];
  327. xstate_comp_sizes[i] = xstate_sizes[i];
  328. }
  329. }
  330. return;
  331. }
  332. xstate_comp_offsets[FIRST_EXTENDED_XFEATURE] =
  333. FXSAVE_SIZE + XSAVE_HDR_SIZE;
  334. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  335. if (xfeature_enabled(i))
  336. xstate_comp_sizes[i] = xstate_sizes[i];
  337. else
  338. xstate_comp_sizes[i] = 0;
  339. if (i > FIRST_EXTENDED_XFEATURE) {
  340. xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
  341. + xstate_comp_sizes[i-1];
  342. if (xfeature_is_aligned(i))
  343. xstate_comp_offsets[i] =
  344. ALIGN(xstate_comp_offsets[i], 64);
  345. }
  346. }
  347. }
  348. /*
  349. * Print out xstate component offsets and sizes
  350. */
  351. static void __init print_xstate_offset_size(void)
  352. {
  353. int i;
  354. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  355. if (!xfeature_enabled(i))
  356. continue;
  357. pr_info("x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n",
  358. i, xstate_comp_offsets[i], i, xstate_sizes[i]);
  359. }
  360. }
  361. /*
  362. * setup the xstate image representing the init state
  363. */
  364. static void __init setup_init_fpu_buf(void)
  365. {
  366. static int on_boot_cpu __initdata = 1;
  367. WARN_ON_FPU(!on_boot_cpu);
  368. on_boot_cpu = 0;
  369. if (!boot_cpu_has(X86_FEATURE_XSAVE))
  370. return;
  371. setup_xstate_features();
  372. print_xstate_features();
  373. if (boot_cpu_has(X86_FEATURE_XSAVES))
  374. init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask;
  375. /*
  376. * Init all the features state with header.xfeatures being 0x0
  377. */
  378. copy_kernel_to_xregs_booting(&init_fpstate.xsave);
  379. /*
  380. * Dump the init state again. This is to identify the init state
  381. * of any feature which is not represented by all zero's.
  382. */
  383. copy_xregs_to_kernel_booting(&init_fpstate.xsave);
  384. }
  385. static int xfeature_uncompacted_offset(int xfeature_nr)
  386. {
  387. u32 eax, ebx, ecx, edx;
  388. /*
  389. * Only XSAVES supports supervisor states and it uses compacted
  390. * format. Checking a supervisor state's uncompacted offset is
  391. * an error.
  392. */
  393. if (XFEATURE_MASK_SUPERVISOR & (1 << xfeature_nr)) {
  394. WARN_ONCE(1, "No fixed offset for xstate %d\n", xfeature_nr);
  395. return -1;
  396. }
  397. CHECK_XFEATURE(xfeature_nr);
  398. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  399. return ebx;
  400. }
  401. static int xfeature_size(int xfeature_nr)
  402. {
  403. u32 eax, ebx, ecx, edx;
  404. CHECK_XFEATURE(xfeature_nr);
  405. cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
  406. return eax;
  407. }
  408. /*
  409. * 'XSAVES' implies two different things:
  410. * 1. saving of supervisor/system state
  411. * 2. using the compacted format
  412. *
  413. * Use this function when dealing with the compacted format so
  414. * that it is obvious which aspect of 'XSAVES' is being handled
  415. * by the calling code.
  416. */
  417. int using_compacted_format(void)
  418. {
  419. return boot_cpu_has(X86_FEATURE_XSAVES);
  420. }
  421. static void __xstate_dump_leaves(void)
  422. {
  423. int i;
  424. u32 eax, ebx, ecx, edx;
  425. static int should_dump = 1;
  426. if (!should_dump)
  427. return;
  428. should_dump = 0;
  429. /*
  430. * Dump out a few leaves past the ones that we support
  431. * just in case there are some goodies up there
  432. */
  433. for (i = 0; i < XFEATURE_MAX + 10; i++) {
  434. cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
  435. pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n",
  436. XSTATE_CPUID, i, eax, ebx, ecx, edx);
  437. }
  438. }
  439. #define XSTATE_WARN_ON(x) do { \
  440. if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \
  441. __xstate_dump_leaves(); \
  442. } \
  443. } while (0)
  444. #define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \
  445. if ((nr == nr_macro) && \
  446. WARN_ONCE(sz != sizeof(__struct), \
  447. "%s: struct is %zu bytes, cpu state %d bytes\n", \
  448. __stringify(nr_macro), sizeof(__struct), sz)) { \
  449. __xstate_dump_leaves(); \
  450. } \
  451. } while (0)
  452. /*
  453. * We have a C struct for each 'xstate'. We need to ensure
  454. * that our software representation matches what the CPU
  455. * tells us about the state's size.
  456. */
  457. static void check_xstate_against_struct(int nr)
  458. {
  459. /*
  460. * Ask the CPU for the size of the state.
  461. */
  462. int sz = xfeature_size(nr);
  463. /*
  464. * Match each CPU state with the corresponding software
  465. * structure.
  466. */
  467. XCHECK_SZ(sz, nr, XFEATURE_YMM, struct ymmh_struct);
  468. XCHECK_SZ(sz, nr, XFEATURE_BNDREGS, struct mpx_bndreg_state);
  469. XCHECK_SZ(sz, nr, XFEATURE_BNDCSR, struct mpx_bndcsr_state);
  470. XCHECK_SZ(sz, nr, XFEATURE_OPMASK, struct avx_512_opmask_state);
  471. XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state);
  472. XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state);
  473. XCHECK_SZ(sz, nr, XFEATURE_PKRU, struct pkru_state);
  474. /*
  475. * Make *SURE* to add any feature numbers in below if
  476. * there are "holes" in the xsave state component
  477. * numbers.
  478. */
  479. if ((nr < XFEATURE_YMM) ||
  480. (nr >= XFEATURE_MAX) ||
  481. (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR)) {
  482. WARN_ONCE(1, "no structure for xstate: %d\n", nr);
  483. XSTATE_WARN_ON(1);
  484. }
  485. }
  486. /*
  487. * This essentially double-checks what the cpu told us about
  488. * how large the XSAVE buffer needs to be. We are recalculating
  489. * it to be safe.
  490. */
  491. static void do_extra_xstate_size_checks(void)
  492. {
  493. int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
  494. int i;
  495. for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
  496. if (!xfeature_enabled(i))
  497. continue;
  498. check_xstate_against_struct(i);
  499. /*
  500. * Supervisor state components can be managed only by
  501. * XSAVES, which is compacted-format only.
  502. */
  503. if (!using_compacted_format())
  504. XSTATE_WARN_ON(xfeature_is_supervisor(i));
  505. /* Align from the end of the previous feature */
  506. if (xfeature_is_aligned(i))
  507. paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64);
  508. /*
  509. * The offset of a given state in the non-compacted
  510. * format is given to us in a CPUID leaf. We check
  511. * them for being ordered (increasing offsets) in
  512. * setup_xstate_features().
  513. */
  514. if (!using_compacted_format())
  515. paranoid_xstate_size = xfeature_uncompacted_offset(i);
  516. /*
  517. * The compacted-format offset always depends on where
  518. * the previous state ended.
  519. */
  520. paranoid_xstate_size += xfeature_size(i);
  521. }
  522. XSTATE_WARN_ON(paranoid_xstate_size != fpu_kernel_xstate_size);
  523. }
  524. /*
  525. * Get total size of enabled xstates in XCR0/xfeatures_mask.
  526. *
  527. * Note the SDM's wording here. "sub-function 0" only enumerates
  528. * the size of the *user* states. If we use it to size a buffer
  529. * that we use 'XSAVES' on, we could potentially overflow the
  530. * buffer because 'XSAVES' saves system states too.
  531. *
  532. * Note that we do not currently set any bits on IA32_XSS so
  533. * 'XCR0 | IA32_XSS == XCR0' for now.
  534. */
  535. static unsigned int __init get_xsaves_size(void)
  536. {
  537. unsigned int eax, ebx, ecx, edx;
  538. /*
  539. * - CPUID function 0DH, sub-function 1:
  540. * EBX enumerates the size (in bytes) required by
  541. * the XSAVES instruction for an XSAVE area
  542. * containing all the state components
  543. * corresponding to bits currently set in
  544. * XCR0 | IA32_XSS.
  545. */
  546. cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
  547. return ebx;
  548. }
  549. static unsigned int __init get_xsave_size(void)
  550. {
  551. unsigned int eax, ebx, ecx, edx;
  552. /*
  553. * - CPUID function 0DH, sub-function 0:
  554. * EBX enumerates the size (in bytes) required by
  555. * the XSAVE instruction for an XSAVE area
  556. * containing all the *user* state components
  557. * corresponding to bits currently set in XCR0.
  558. */
  559. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  560. return ebx;
  561. }
  562. /*
  563. * Will the runtime-enumerated 'xstate_size' fit in the init
  564. * task's statically-allocated buffer?
  565. */
  566. static bool is_supported_xstate_size(unsigned int test_xstate_size)
  567. {
  568. if (test_xstate_size <= sizeof(union fpregs_state))
  569. return true;
  570. pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
  571. sizeof(union fpregs_state), test_xstate_size);
  572. return false;
  573. }
  574. static int init_xstate_size(void)
  575. {
  576. /* Recompute the context size for enabled features: */
  577. unsigned int possible_xstate_size;
  578. unsigned int xsave_size;
  579. xsave_size = get_xsave_size();
  580. if (boot_cpu_has(X86_FEATURE_XSAVES))
  581. possible_xstate_size = get_xsaves_size();
  582. else
  583. possible_xstate_size = xsave_size;
  584. /* Ensure we have the space to store all enabled: */
  585. if (!is_supported_xstate_size(possible_xstate_size))
  586. return -EINVAL;
  587. /*
  588. * The size is OK, we are definitely going to use xsave,
  589. * make it known to the world that we need more space.
  590. */
  591. fpu_kernel_xstate_size = possible_xstate_size;
  592. do_extra_xstate_size_checks();
  593. /*
  594. * User space is always in standard format.
  595. */
  596. fpu_user_xstate_size = xsave_size;
  597. return 0;
  598. }
  599. /*
  600. * We enabled the XSAVE hardware, but something went wrong and
  601. * we can not use it. Disable it.
  602. */
  603. static void fpu__init_disable_system_xstate(void)
  604. {
  605. xfeatures_mask = 0;
  606. cr4_clear_bits(X86_CR4_OSXSAVE);
  607. fpu__xstate_clear_all_cpu_caps();
  608. }
  609. /*
  610. * Enable and initialize the xsave feature.
  611. * Called once per system bootup.
  612. */
  613. void __init fpu__init_system_xstate(void)
  614. {
  615. unsigned int eax, ebx, ecx, edx;
  616. static int on_boot_cpu __initdata = 1;
  617. int err;
  618. WARN_ON_FPU(!on_boot_cpu);
  619. on_boot_cpu = 0;
  620. if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
  621. pr_info("x86/fpu: Legacy x87 FPU detected.\n");
  622. return;
  623. }
  624. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  625. WARN_ON_FPU(1);
  626. return;
  627. }
  628. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  629. xfeatures_mask = eax + ((u64)edx << 32);
  630. if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
  631. /*
  632. * This indicates that something really unexpected happened
  633. * with the enumeration. Disable XSAVE and try to continue
  634. * booting without it. This is too early to BUG().
  635. */
  636. pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
  637. goto out_disable;
  638. }
  639. xfeatures_mask &= fpu__get_supported_xfeatures_mask();
  640. /* Enable xstate instructions to be able to continue with initialization: */
  641. fpu__init_cpu_xstate();
  642. err = init_xstate_size();
  643. if (err)
  644. goto out_disable;
  645. /*
  646. * Update info used for ptrace frames; use standard-format size and no
  647. * supervisor xstates:
  648. */
  649. update_regset_xstate_info(fpu_user_xstate_size, xfeatures_mask & ~XFEATURE_MASK_SUPERVISOR);
  650. fpu__init_prepare_fx_sw_frame();
  651. setup_init_fpu_buf();
  652. setup_xstate_comp();
  653. print_xstate_offset_size();
  654. pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
  655. xfeatures_mask,
  656. fpu_kernel_xstate_size,
  657. boot_cpu_has(X86_FEATURE_XSAVES) ? "compacted" : "standard");
  658. return;
  659. out_disable:
  660. /* something went wrong, try to boot without any XSAVE support */
  661. fpu__init_disable_system_xstate();
  662. }
  663. /*
  664. * Restore minimal FPU state after suspend:
  665. */
  666. void fpu__resume_cpu(void)
  667. {
  668. /*
  669. * Restore XCR0 on xsave capable CPUs:
  670. */
  671. if (boot_cpu_has(X86_FEATURE_XSAVE))
  672. xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
  673. }
  674. /*
  675. * Given an xstate feature mask, calculate where in the xsave
  676. * buffer the state is. Callers should ensure that the buffer
  677. * is valid.
  678. *
  679. * Note: does not work for compacted buffers.
  680. */
  681. void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
  682. {
  683. int feature_nr = fls64(xstate_feature_mask) - 1;
  684. if (!xfeature_enabled(feature_nr)) {
  685. WARN_ON_FPU(1);
  686. return NULL;
  687. }
  688. return (void *)xsave + xstate_comp_offsets[feature_nr];
  689. }
  690. /*
  691. * Given the xsave area and a state inside, this function returns the
  692. * address of the state.
  693. *
  694. * This is the API that is called to get xstate address in either
  695. * standard format or compacted format of xsave area.
  696. *
  697. * Note that if there is no data for the field in the xsave buffer
  698. * this will return NULL.
  699. *
  700. * Inputs:
  701. * xstate: the thread's storage area for all FPU data
  702. * xstate_feature: state which is defined in xsave.h (e.g.
  703. * XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...)
  704. * Output:
  705. * address of the state in the xsave area, or NULL if the
  706. * field is not present in the xsave buffer.
  707. */
  708. void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
  709. {
  710. /*
  711. * Do we even *have* xsave state?
  712. */
  713. if (!boot_cpu_has(X86_FEATURE_XSAVE))
  714. return NULL;
  715. /*
  716. * We should not ever be requesting features that we
  717. * have not enabled. Remember that pcntxt_mask is
  718. * what we write to the XCR0 register.
  719. */
  720. WARN_ONCE(!(xfeatures_mask & xstate_feature),
  721. "get of unsupported state");
  722. /*
  723. * This assumes the last 'xsave*' instruction to
  724. * have requested that 'xstate_feature' be saved.
  725. * If it did not, we might be seeing and old value
  726. * of the field in the buffer.
  727. *
  728. * This can happen because the last 'xsave' did not
  729. * request that this feature be saved (unlikely)
  730. * or because the "init optimization" caused it
  731. * to not be saved.
  732. */
  733. if (!(xsave->header.xfeatures & xstate_feature))
  734. return NULL;
  735. return __raw_xsave_addr(xsave, xstate_feature);
  736. }
  737. EXPORT_SYMBOL_GPL(get_xsave_addr);
  738. /*
  739. * This wraps up the common operations that need to occur when retrieving
  740. * data from xsave state. It first ensures that the current task was
  741. * using the FPU and retrieves the data in to a buffer. It then calculates
  742. * the offset of the requested field in the buffer.
  743. *
  744. * This function is safe to call whether the FPU is in use or not.
  745. *
  746. * Note that this only works on the current task.
  747. *
  748. * Inputs:
  749. * @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP,
  750. * XFEATURE_MASK_SSE, etc...)
  751. * Output:
  752. * address of the state in the xsave area or NULL if the state
  753. * is not present or is in its 'init state'.
  754. */
  755. const void *get_xsave_field_ptr(int xsave_state)
  756. {
  757. struct fpu *fpu = &current->thread.fpu;
  758. if (!fpu->fpstate_active)
  759. return NULL;
  760. /*
  761. * fpu__save() takes the CPU's xstate registers
  762. * and saves them off to the 'fpu memory buffer.
  763. */
  764. fpu__save(fpu);
  765. return get_xsave_addr(&fpu->state.xsave, xsave_state);
  766. }
  767. #ifdef CONFIG_ARCH_HAS_PKEYS
  768. #define NR_VALID_PKRU_BITS (CONFIG_NR_PROTECTION_KEYS * 2)
  769. #define PKRU_VALID_MASK (NR_VALID_PKRU_BITS - 1)
  770. /*
  771. * This will go out and modify PKRU register to set the access
  772. * rights for @pkey to @init_val.
  773. */
  774. int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  775. unsigned long init_val)
  776. {
  777. u32 old_pkru;
  778. int pkey_shift = (pkey * PKRU_BITS_PER_PKEY);
  779. u32 new_pkru_bits = 0;
  780. /*
  781. * This check implies XSAVE support. OSPKE only gets
  782. * set if we enable XSAVE and we enable PKU in XCR0.
  783. */
  784. if (!boot_cpu_has(X86_FEATURE_OSPKE))
  785. return -EINVAL;
  786. /* Set the bits we need in PKRU: */
  787. if (init_val & PKEY_DISABLE_ACCESS)
  788. new_pkru_bits |= PKRU_AD_BIT;
  789. if (init_val & PKEY_DISABLE_WRITE)
  790. new_pkru_bits |= PKRU_WD_BIT;
  791. /* Shift the bits in to the correct place in PKRU for pkey: */
  792. new_pkru_bits <<= pkey_shift;
  793. /* Get old PKRU and mask off any old bits in place: */
  794. old_pkru = read_pkru();
  795. old_pkru &= ~((PKRU_AD_BIT|PKRU_WD_BIT) << pkey_shift);
  796. /* Write old part along with new part: */
  797. write_pkru(old_pkru | new_pkru_bits);
  798. return 0;
  799. }
  800. #endif /* ! CONFIG_ARCH_HAS_PKEYS */
  801. /*
  802. * This is similar to user_regset_copyout(), but will not add offset to
  803. * the source data pointer or increment pos, count, kbuf, and ubuf.
  804. */
  805. static inline int xstate_copyout(unsigned int pos, unsigned int count,
  806. void *kbuf, void __user *ubuf,
  807. const void *data, const int start_pos,
  808. const int end_pos)
  809. {
  810. if ((count == 0) || (pos < start_pos))
  811. return 0;
  812. if (end_pos < 0 || pos < end_pos) {
  813. unsigned int copy = (end_pos < 0 ? count : min(count, end_pos - pos));
  814. if (kbuf) {
  815. memcpy(kbuf + pos, data, copy);
  816. } else {
  817. if (__copy_to_user(ubuf + pos, data, copy))
  818. return -EFAULT;
  819. }
  820. }
  821. return 0;
  822. }
  823. /*
  824. * Convert from kernel XSAVES compacted format to standard format and copy
  825. * to a ptrace buffer. It supports partial copy but pos always starts from
  826. * zero. This is called from xstateregs_get() and there we check the CPU
  827. * has XSAVES.
  828. */
  829. int copyout_from_xsaves(unsigned int pos, unsigned int count, void *kbuf,
  830. void __user *ubuf, struct xregs_state *xsave)
  831. {
  832. unsigned int offset, size;
  833. int ret, i;
  834. struct xstate_header header;
  835. /*
  836. * Currently copy_regset_to_user() starts from pos 0:
  837. */
  838. if (unlikely(pos != 0))
  839. return -EFAULT;
  840. /*
  841. * The destination is a ptrace buffer; we put in only user xstates:
  842. */
  843. memset(&header, 0, sizeof(header));
  844. header.xfeatures = xsave->header.xfeatures;
  845. header.xfeatures &= ~XFEATURE_MASK_SUPERVISOR;
  846. /*
  847. * Copy xregs_state->header:
  848. */
  849. offset = offsetof(struct xregs_state, header);
  850. size = sizeof(header);
  851. ret = xstate_copyout(offset, size, kbuf, ubuf, &header, 0, count);
  852. if (ret)
  853. return ret;
  854. for (i = 0; i < XFEATURE_MAX; i++) {
  855. /*
  856. * Copy only in-use xstates:
  857. */
  858. if ((header.xfeatures >> i) & 1) {
  859. void *src = __raw_xsave_addr(xsave, 1 << i);
  860. offset = xstate_offsets[i];
  861. size = xstate_sizes[i];
  862. ret = xstate_copyout(offset, size, kbuf, ubuf, src, 0, count);
  863. if (ret)
  864. return ret;
  865. if (offset + size >= count)
  866. break;
  867. }
  868. }
  869. /*
  870. * Fill xsave->i387.sw_reserved value for ptrace frame:
  871. */
  872. offset = offsetof(struct fxregs_state, sw_reserved);
  873. size = sizeof(xstate_fx_sw_bytes);
  874. ret = xstate_copyout(offset, size, kbuf, ubuf, xstate_fx_sw_bytes, 0, count);
  875. if (ret)
  876. return ret;
  877. return 0;
  878. }
  879. /*
  880. * Convert from a ptrace standard-format buffer to kernel XSAVES format
  881. * and copy to the target thread. This is called from xstateregs_set() and
  882. * there we check the CPU has XSAVES and a whole standard-sized buffer
  883. * exists.
  884. */
  885. int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
  886. struct xregs_state *xsave)
  887. {
  888. unsigned int offset, size;
  889. int i;
  890. u64 xfeatures;
  891. u64 allowed_features;
  892. offset = offsetof(struct xregs_state, header);
  893. size = sizeof(xfeatures);
  894. if (kbuf) {
  895. memcpy(&xfeatures, kbuf + offset, size);
  896. } else {
  897. if (__copy_from_user(&xfeatures, ubuf + offset, size))
  898. return -EFAULT;
  899. }
  900. /*
  901. * Reject if the user sets any disabled or supervisor features:
  902. */
  903. allowed_features = xfeatures_mask & ~XFEATURE_MASK_SUPERVISOR;
  904. if (xfeatures & ~allowed_features)
  905. return -EINVAL;
  906. for (i = 0; i < XFEATURE_MAX; i++) {
  907. u64 mask = ((u64)1 << i);
  908. if (xfeatures & mask) {
  909. void *dst = __raw_xsave_addr(xsave, 1 << i);
  910. offset = xstate_offsets[i];
  911. size = xstate_sizes[i];
  912. if (kbuf) {
  913. memcpy(dst, kbuf + offset, size);
  914. } else {
  915. if (__copy_from_user(dst, ubuf + offset, size))
  916. return -EFAULT;
  917. }
  918. }
  919. }
  920. /*
  921. * The state that came in from userspace was user-state only.
  922. * Mask all the user states out of 'xfeatures':
  923. */
  924. xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR;
  925. /*
  926. * Add back in the features that came in from userspace:
  927. */
  928. xsave->header.xfeatures |= xfeatures;
  929. return 0;
  930. }