xstate.c 29 KB

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