xstate.c 28 KB

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