xsave.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * xsave/xrstor support.
  3. *
  4. * Author: Suresh Siddha <suresh.b.siddha@intel.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/bootmem.h>
  8. #include <linux/compat.h>
  9. #include <asm/i387.h>
  10. #include <asm/fpu-internal.h>
  11. #include <asm/sigframe.h>
  12. #include <asm/xcr.h>
  13. /*
  14. * Supported feature mask by the CPU and the kernel.
  15. */
  16. u64 pcntxt_mask;
  17. /*
  18. * Represents init state for the supported extended state.
  19. */
  20. struct xsave_struct *init_xstate_buf;
  21. static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
  22. static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
  23. /*
  24. * If a processor implementation discern that a processor state component is
  25. * in its initialized state it may modify the corresponding bit in the
  26. * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
  27. * layout in the case of xsaveopt. While presenting the xstate information to
  28. * the user, we always ensure that the memory layout of a feature will be in
  29. * the init state if the corresponding header bit is zero. This is to ensure
  30. * that the user doesn't see some stale state in the memory layout during
  31. * signal handling, debugging etc.
  32. */
  33. void __sanitize_i387_state(struct task_struct *tsk)
  34. {
  35. struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
  36. int feature_bit = 0x2;
  37. u64 xstate_bv;
  38. if (!fx)
  39. return;
  40. xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
  41. /*
  42. * None of the feature bits are in init state. So nothing else
  43. * to do for us, as the memory layout is up to date.
  44. */
  45. if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
  46. return;
  47. /*
  48. * FP is in init state
  49. */
  50. if (!(xstate_bv & XSTATE_FP)) {
  51. fx->cwd = 0x37f;
  52. fx->swd = 0;
  53. fx->twd = 0;
  54. fx->fop = 0;
  55. fx->rip = 0;
  56. fx->rdp = 0;
  57. memset(&fx->st_space[0], 0, 128);
  58. }
  59. /*
  60. * SSE is in init state
  61. */
  62. if (!(xstate_bv & XSTATE_SSE))
  63. memset(&fx->xmm_space[0], 0, 256);
  64. xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
  65. /*
  66. * Update all the other memory layouts for which the corresponding
  67. * header bit is in the init state.
  68. */
  69. while (xstate_bv) {
  70. if (xstate_bv & 0x1) {
  71. int offset = xstate_offsets[feature_bit];
  72. int size = xstate_sizes[feature_bit];
  73. memcpy(((void *) fx) + offset,
  74. ((void *) init_xstate_buf) + offset,
  75. size);
  76. }
  77. xstate_bv >>= 1;
  78. feature_bit++;
  79. }
  80. }
  81. /*
  82. * Check for the presence of extended state information in the
  83. * user fpstate pointer in the sigcontext.
  84. */
  85. static inline int check_for_xstate(struct i387_fxsave_struct __user *buf,
  86. void __user *fpstate,
  87. struct _fpx_sw_bytes *fx_sw)
  88. {
  89. int min_xstate_size = sizeof(struct i387_fxsave_struct) +
  90. sizeof(struct xsave_hdr_struct);
  91. unsigned int magic2;
  92. if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
  93. return -1;
  94. /* Check for the first magic field and other error scenarios. */
  95. if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
  96. fx_sw->xstate_size < min_xstate_size ||
  97. fx_sw->xstate_size > xstate_size ||
  98. fx_sw->xstate_size > fx_sw->extended_size)
  99. return -1;
  100. /*
  101. * Check for the presence of second magic word at the end of memory
  102. * layout. This detects the case where the user just copied the legacy
  103. * fpstate layout with out copying the extended state information
  104. * in the memory layout.
  105. */
  106. if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
  107. || magic2 != FP_XSTATE_MAGIC2)
  108. return -1;
  109. return 0;
  110. }
  111. /*
  112. * Signal frame handlers.
  113. */
  114. static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
  115. {
  116. if (use_fxsr()) {
  117. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  118. struct user_i387_ia32_struct env;
  119. struct _fpstate_ia32 __user *fp = buf;
  120. convert_from_fxsr(&env, tsk);
  121. if (__copy_to_user(buf, &env, sizeof(env)) ||
  122. __put_user(xsave->i387.swd, &fp->status) ||
  123. __put_user(X86_FXSR_MAGIC, &fp->magic))
  124. return -1;
  125. } else {
  126. struct i387_fsave_struct __user *fp = buf;
  127. u32 swd;
  128. if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
  134. {
  135. struct xsave_struct __user *x = buf;
  136. struct _fpx_sw_bytes *sw_bytes;
  137. u32 xstate_bv;
  138. int err;
  139. /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
  140. sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
  141. err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
  142. if (!use_xsave())
  143. return err;
  144. err |= __put_user(FP_XSTATE_MAGIC2, (__u32 *)(buf + xstate_size));
  145. /*
  146. * Read the xstate_bv which we copied (directly from the cpu or
  147. * from the state in task struct) to the user buffers.
  148. */
  149. err |= __get_user(xstate_bv, (__u32 *)&x->xsave_hdr.xstate_bv);
  150. /*
  151. * For legacy compatible, we always set FP/SSE bits in the bit
  152. * vector while saving the state to the user context. This will
  153. * enable us capturing any changes(during sigreturn) to
  154. * the FP/SSE bits by the legacy applications which don't touch
  155. * xstate_bv in the xsave header.
  156. *
  157. * xsave aware apps can change the xstate_bv in the xsave
  158. * header as well as change any contents in the memory layout.
  159. * xrestore as part of sigreturn will capture all the changes.
  160. */
  161. xstate_bv |= XSTATE_FPSSE;
  162. err |= __put_user(xstate_bv, (__u32 *)&x->xsave_hdr.xstate_bv);
  163. return err;
  164. }
  165. static inline int save_user_xstate(struct xsave_struct __user *buf)
  166. {
  167. int err;
  168. if (use_xsave())
  169. err = xsave_user(buf);
  170. else if (use_fxsr())
  171. err = fxsave_user((struct i387_fxsave_struct __user *) buf);
  172. else
  173. err = fsave_user((struct i387_fsave_struct __user *) buf);
  174. if (unlikely(err) && __clear_user(buf, xstate_size))
  175. err = -EFAULT;
  176. return err;
  177. }
  178. /*
  179. * Save the fpu, extended register state to the user signal frame.
  180. *
  181. * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
  182. * state is copied.
  183. * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
  184. *
  185. * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
  186. * buf != buf_fx for 32-bit frames with fxstate.
  187. *
  188. * If the fpu, extended register state is live, save the state directly
  189. * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
  190. * copy the thread's fpu state to the user frame starting at 'buf_fx'.
  191. *
  192. * If this is a 32-bit frame with fxstate, put a fsave header before
  193. * the aligned state at 'buf_fx'.
  194. *
  195. * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
  196. * indicating the absence/presence of the extended state to the user.
  197. */
  198. int save_xstate_sig(void __user *buf, void __user *buf_fx, int size)
  199. {
  200. struct xsave_struct *xsave = &current->thread.fpu.state->xsave;
  201. struct task_struct *tsk = current;
  202. int ia32_fxstate = (buf != buf_fx);
  203. ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
  204. config_enabled(CONFIG_IA32_EMULATION));
  205. if (!access_ok(VERIFY_WRITE, buf, size))
  206. return -EACCES;
  207. if (!static_cpu_has(X86_FEATURE_FPU))
  208. return fpregs_soft_get(current, NULL, 0,
  209. sizeof(struct user_i387_ia32_struct), NULL,
  210. (struct _fpstate_ia32 __user *) buf) ? -1 : 1;
  211. if (user_has_fpu()) {
  212. /* Save the live register state to the user directly. */
  213. if (save_user_xstate(buf_fx))
  214. return -1;
  215. /* Update the thread's fxstate to save the fsave header. */
  216. if (ia32_fxstate)
  217. fpu_fxsave(&tsk->thread.fpu);
  218. } else {
  219. sanitize_i387_state(tsk);
  220. if (__copy_to_user(buf_fx, xsave, xstate_size))
  221. return -1;
  222. }
  223. /* Save the fsave header for the 32-bit frames. */
  224. if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
  225. return -1;
  226. if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
  227. return -1;
  228. drop_init_fpu(tsk); /* trigger finit */
  229. return 0;
  230. }
  231. static inline void
  232. sanitize_restored_xstate(struct task_struct *tsk,
  233. struct user_i387_ia32_struct *ia32_env,
  234. u64 xstate_bv, int fx_only)
  235. {
  236. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  237. struct xsave_hdr_struct *xsave_hdr = &xsave->xsave_hdr;
  238. if (use_xsave()) {
  239. /* These bits must be zero. */
  240. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  241. /*
  242. * Init the state that is not present in the memory
  243. * layout and not enabled by the OS.
  244. */
  245. if (fx_only)
  246. xsave_hdr->xstate_bv = XSTATE_FPSSE;
  247. else
  248. xsave_hdr->xstate_bv &= (pcntxt_mask & xstate_bv);
  249. }
  250. if (use_fxsr()) {
  251. /*
  252. * mscsr reserved bits must be masked to zero for security
  253. * reasons.
  254. */
  255. xsave->i387.mxcsr &= mxcsr_feature_mask;
  256. convert_to_fxsr(tsk, ia32_env);
  257. }
  258. }
  259. /*
  260. * Restore the extended state if present. Otherwise, restore the FP/SSE state.
  261. */
  262. static inline int restore_user_xstate(void __user *buf, u64 xbv, int fx_only)
  263. {
  264. if (use_xsave()) {
  265. if ((unsigned long)buf % 64 || fx_only) {
  266. u64 init_bv = pcntxt_mask & ~XSTATE_FPSSE;
  267. xrstor_state(init_xstate_buf, init_bv);
  268. return fxrstor_user(buf);
  269. } else {
  270. u64 init_bv = pcntxt_mask & ~xbv;
  271. if (unlikely(init_bv))
  272. xrstor_state(init_xstate_buf, init_bv);
  273. return xrestore_user(buf, xbv);
  274. }
  275. } else if (use_fxsr()) {
  276. return fxrstor_user(buf);
  277. } else
  278. return frstor_user(buf);
  279. }
  280. int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)
  281. {
  282. int ia32_fxstate = (buf != buf_fx);
  283. struct task_struct *tsk = current;
  284. int state_size = xstate_size;
  285. u64 xstate_bv = 0;
  286. int fx_only = 0;
  287. ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
  288. config_enabled(CONFIG_IA32_EMULATION));
  289. if (!buf) {
  290. drop_init_fpu(tsk);
  291. return 0;
  292. }
  293. if (!access_ok(VERIFY_READ, buf, size))
  294. return -EACCES;
  295. if (!used_math() && init_fpu(tsk))
  296. return -1;
  297. if (!static_cpu_has(X86_FEATURE_FPU))
  298. return fpregs_soft_set(current, NULL,
  299. 0, sizeof(struct user_i387_ia32_struct),
  300. NULL, buf) != 0;
  301. if (use_xsave()) {
  302. struct _fpx_sw_bytes fx_sw_user;
  303. if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
  304. /*
  305. * Couldn't find the extended state information in the
  306. * memory layout. Restore just the FP/SSE and init all
  307. * the other extended state.
  308. */
  309. state_size = sizeof(struct i387_fxsave_struct);
  310. fx_only = 1;
  311. } else {
  312. state_size = fx_sw_user.xstate_size;
  313. xstate_bv = fx_sw_user.xstate_bv;
  314. }
  315. }
  316. if (ia32_fxstate) {
  317. /*
  318. * For 32-bit frames with fxstate, copy the user state to the
  319. * thread's fpu state, reconstruct fxstate from the fsave
  320. * header. Sanitize the copied state etc.
  321. */
  322. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  323. struct user_i387_ia32_struct env;
  324. int err = 0;
  325. /*
  326. * Drop the current fpu which clears used_math(). This ensures
  327. * that any context-switch during the copy of the new state,
  328. * avoids the intermediate state from getting restored/saved.
  329. * Thus avoiding the new restored state from getting corrupted.
  330. * We will be ready to restore/save the state only after
  331. * set_used_math() is again set.
  332. */
  333. drop_fpu(tsk);
  334. if (__copy_from_user(xsave, buf_fx, state_size) ||
  335. __copy_from_user(&env, buf, sizeof(env))) {
  336. err = -1;
  337. } else {
  338. sanitize_restored_xstate(tsk, &env, xstate_bv, fx_only);
  339. set_used_math();
  340. }
  341. if (use_eager_fpu())
  342. math_state_restore();
  343. return err;
  344. } else {
  345. /*
  346. * For 64-bit frames and 32-bit fsave frames, restore the user
  347. * state to the registers directly (with exceptions handled).
  348. */
  349. user_fpu_begin();
  350. if (restore_user_xstate(buf_fx, xstate_bv, fx_only)) {
  351. drop_init_fpu(tsk);
  352. return -1;
  353. }
  354. }
  355. return 0;
  356. }
  357. /*
  358. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  359. * the presence of the extended state information in the memory layout
  360. * pointed by the fpstate pointer in the sigcontext.
  361. * This will be saved when ever the FP and extended state context is
  362. * saved on the user stack during the signal handler delivery to the user.
  363. */
  364. static void prepare_fx_sw_frame(void)
  365. {
  366. int fsave_header_size = sizeof(struct i387_fsave_struct);
  367. int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
  368. if (config_enabled(CONFIG_X86_32))
  369. size += fsave_header_size;
  370. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  371. fx_sw_reserved.extended_size = size;
  372. fx_sw_reserved.xstate_bv = pcntxt_mask;
  373. fx_sw_reserved.xstate_size = xstate_size;
  374. if (config_enabled(CONFIG_IA32_EMULATION)) {
  375. fx_sw_reserved_ia32 = fx_sw_reserved;
  376. fx_sw_reserved_ia32.extended_size += fsave_header_size;
  377. }
  378. }
  379. /*
  380. * Enable the extended processor state save/restore feature
  381. */
  382. static inline void xstate_enable(void)
  383. {
  384. set_in_cr4(X86_CR4_OSXSAVE);
  385. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  386. }
  387. /*
  388. * Record the offsets and sizes of different state managed by the xsave
  389. * memory layout.
  390. */
  391. static void __init setup_xstate_features(void)
  392. {
  393. int eax, ebx, ecx, edx, leaf = 0x2;
  394. xstate_features = fls64(pcntxt_mask);
  395. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  396. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  397. do {
  398. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  399. if (eax == 0)
  400. break;
  401. xstate_offsets[leaf] = ebx;
  402. xstate_sizes[leaf] = eax;
  403. leaf++;
  404. } while (1);
  405. }
  406. /*
  407. * setup the xstate image representing the init state
  408. */
  409. static void __init setup_init_fpu_buf(void)
  410. {
  411. /*
  412. * Setup init_xstate_buf to represent the init state of
  413. * all the features managed by the xsave
  414. */
  415. init_xstate_buf = alloc_bootmem_align(xstate_size,
  416. __alignof__(struct xsave_struct));
  417. fx_finit(&init_xstate_buf->i387);
  418. if (!cpu_has_xsave)
  419. return;
  420. setup_xstate_features();
  421. /*
  422. * Init all the features state with header_bv being 0x0
  423. */
  424. xrstor_state(init_xstate_buf, -1);
  425. /*
  426. * Dump the init state again. This is to identify the init state
  427. * of any feature which is not represented by all zero's.
  428. */
  429. xsave_state(init_xstate_buf, -1);
  430. }
  431. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  432. static int __init eager_fpu_setup(char *s)
  433. {
  434. if (!strcmp(s, "on"))
  435. eagerfpu = ENABLE;
  436. else if (!strcmp(s, "off"))
  437. eagerfpu = DISABLE;
  438. else if (!strcmp(s, "auto"))
  439. eagerfpu = AUTO;
  440. return 1;
  441. }
  442. __setup("eagerfpu=", eager_fpu_setup);
  443. /*
  444. * Enable and initialize the xsave feature.
  445. */
  446. static void __init xstate_enable_boot_cpu(void)
  447. {
  448. unsigned int eax, ebx, ecx, edx;
  449. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  450. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  451. return;
  452. }
  453. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  454. pcntxt_mask = eax + ((u64)edx << 32);
  455. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  456. pr_err("FP/SSE not shown under xsave features 0x%llx\n",
  457. pcntxt_mask);
  458. BUG();
  459. }
  460. /*
  461. * Support only the state known to OS.
  462. */
  463. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  464. xstate_enable();
  465. /*
  466. * Recompute the context size for enabled features
  467. */
  468. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  469. xstate_size = ebx;
  470. update_regset_xstate_info(xstate_size, pcntxt_mask);
  471. prepare_fx_sw_frame();
  472. setup_init_fpu_buf();
  473. /* Auto enable eagerfpu for xsaveopt */
  474. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  475. eagerfpu = ENABLE;
  476. pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x\n",
  477. pcntxt_mask, xstate_size);
  478. }
  479. /*
  480. * For the very first instance, this calls xstate_enable_boot_cpu();
  481. * for all subsequent instances, this calls xstate_enable().
  482. *
  483. * This is somewhat obfuscated due to the lack of powerful enough
  484. * overrides for the section checks.
  485. */
  486. void xsave_init(void)
  487. {
  488. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  489. void (*this_func)(void);
  490. if (!cpu_has_xsave)
  491. return;
  492. this_func = next_func;
  493. next_func = xstate_enable;
  494. this_func();
  495. }
  496. static inline void __init eager_fpu_init_bp(void)
  497. {
  498. current->thread.fpu.state =
  499. alloc_bootmem_align(xstate_size, __alignof__(struct xsave_struct));
  500. if (!init_xstate_buf)
  501. setup_init_fpu_buf();
  502. }
  503. void eager_fpu_init(void)
  504. {
  505. static __refdata void (*boot_func)(void) = eager_fpu_init_bp;
  506. clear_used_math();
  507. current_thread_info()->status = 0;
  508. if (eagerfpu == ENABLE)
  509. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  510. if (!cpu_has_eager_fpu) {
  511. stts();
  512. return;
  513. }
  514. if (boot_func) {
  515. boot_func();
  516. boot_func = NULL;
  517. }
  518. /*
  519. * This is same as math_state_restore(). But use_xsave() is
  520. * not yet patched to use math_state_restore().
  521. */
  522. init_fpu(current);
  523. __thread_fpu_begin(current);
  524. if (cpu_has_xsave)
  525. xrstor_state(init_xstate_buf, -1);
  526. else
  527. fxrstor_checking(&init_xstate_buf->i387);
  528. }