xstate.c 33 KB

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