xsave.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 (!HAVE_HWFP)
  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 (!HAVE_HWFP) {
  298. return fpregs_soft_set(current, NULL,
  299. 0, sizeof(struct user_i387_ia32_struct),
  300. NULL, buf) != 0;
  301. }
  302. if (use_xsave()) {
  303. struct _fpx_sw_bytes fx_sw_user;
  304. if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
  305. /*
  306. * Couldn't find the extended state information in the
  307. * memory layout. Restore just the FP/SSE and init all
  308. * the other extended state.
  309. */
  310. state_size = sizeof(struct i387_fxsave_struct);
  311. fx_only = 1;
  312. } else {
  313. state_size = fx_sw_user.xstate_size;
  314. xstate_bv = fx_sw_user.xstate_bv;
  315. }
  316. }
  317. if (ia32_fxstate) {
  318. /*
  319. * For 32-bit frames with fxstate, copy the user state to the
  320. * thread's fpu state, reconstruct fxstate from the fsave
  321. * header. Sanitize the copied state etc.
  322. */
  323. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  324. struct user_i387_ia32_struct env;
  325. int err = 0;
  326. /*
  327. * Drop the current fpu which clears used_math(). This ensures
  328. * that any context-switch during the copy of the new state,
  329. * avoids the intermediate state from getting restored/saved.
  330. * Thus avoiding the new restored state from getting corrupted.
  331. * We will be ready to restore/save the state only after
  332. * set_used_math() is again set.
  333. */
  334. drop_fpu(tsk);
  335. if (__copy_from_user(xsave, buf_fx, state_size) ||
  336. __copy_from_user(&env, buf, sizeof(env))) {
  337. err = -1;
  338. } else {
  339. sanitize_restored_xstate(tsk, &env, xstate_bv, fx_only);
  340. set_used_math();
  341. }
  342. if (use_eager_fpu())
  343. math_state_restore();
  344. return err;
  345. } else {
  346. /*
  347. * For 64-bit frames and 32-bit fsave frames, restore the user
  348. * state to the registers directly (with exceptions handled).
  349. */
  350. user_fpu_begin();
  351. if (restore_user_xstate(buf_fx, xstate_bv, fx_only)) {
  352. drop_init_fpu(tsk);
  353. return -1;
  354. }
  355. }
  356. return 0;
  357. }
  358. /*
  359. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  360. * the presence of the extended state information in the memory layout
  361. * pointed by the fpstate pointer in the sigcontext.
  362. * This will be saved when ever the FP and extended state context is
  363. * saved on the user stack during the signal handler delivery to the user.
  364. */
  365. static void prepare_fx_sw_frame(void)
  366. {
  367. int fsave_header_size = sizeof(struct i387_fsave_struct);
  368. int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
  369. if (config_enabled(CONFIG_X86_32))
  370. size += fsave_header_size;
  371. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  372. fx_sw_reserved.extended_size = size;
  373. fx_sw_reserved.xstate_bv = pcntxt_mask;
  374. fx_sw_reserved.xstate_size = xstate_size;
  375. if (config_enabled(CONFIG_IA32_EMULATION)) {
  376. fx_sw_reserved_ia32 = fx_sw_reserved;
  377. fx_sw_reserved_ia32.extended_size += fsave_header_size;
  378. }
  379. }
  380. /*
  381. * Enable the extended processor state save/restore feature
  382. */
  383. static inline void xstate_enable(void)
  384. {
  385. set_in_cr4(X86_CR4_OSXSAVE);
  386. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  387. }
  388. /*
  389. * Record the offsets and sizes of different state managed by the xsave
  390. * memory layout.
  391. */
  392. static void __init setup_xstate_features(void)
  393. {
  394. int eax, ebx, ecx, edx, leaf = 0x2;
  395. xstate_features = fls64(pcntxt_mask);
  396. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  397. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  398. do {
  399. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  400. if (eax == 0)
  401. break;
  402. xstate_offsets[leaf] = ebx;
  403. xstate_sizes[leaf] = eax;
  404. leaf++;
  405. } while (1);
  406. }
  407. /*
  408. * setup the xstate image representing the init state
  409. */
  410. static void __init setup_init_fpu_buf(void)
  411. {
  412. /*
  413. * Setup init_xstate_buf to represent the init state of
  414. * all the features managed by the xsave
  415. */
  416. init_xstate_buf = alloc_bootmem_align(xstate_size,
  417. __alignof__(struct xsave_struct));
  418. fx_finit(&init_xstate_buf->i387);
  419. if (!cpu_has_xsave)
  420. return;
  421. setup_xstate_features();
  422. /*
  423. * Init all the features state with header_bv being 0x0
  424. */
  425. xrstor_state(init_xstate_buf, -1);
  426. /*
  427. * Dump the init state again. This is to identify the init state
  428. * of any feature which is not represented by all zero's.
  429. */
  430. xsave_state(init_xstate_buf, -1);
  431. }
  432. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  433. static int __init eager_fpu_setup(char *s)
  434. {
  435. if (!strcmp(s, "on"))
  436. eagerfpu = ENABLE;
  437. else if (!strcmp(s, "off"))
  438. eagerfpu = DISABLE;
  439. else if (!strcmp(s, "auto"))
  440. eagerfpu = AUTO;
  441. return 1;
  442. }
  443. __setup("eagerfpu=", eager_fpu_setup);
  444. /*
  445. * Enable and initialize the xsave feature.
  446. */
  447. static void __init xstate_enable_boot_cpu(void)
  448. {
  449. unsigned int eax, ebx, ecx, edx;
  450. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  451. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  452. return;
  453. }
  454. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  455. pcntxt_mask = eax + ((u64)edx << 32);
  456. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  457. pr_err("FP/SSE not shown under xsave features 0x%llx\n",
  458. pcntxt_mask);
  459. BUG();
  460. }
  461. /*
  462. * Support only the state known to OS.
  463. */
  464. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  465. xstate_enable();
  466. /*
  467. * Recompute the context size for enabled features
  468. */
  469. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  470. xstate_size = ebx;
  471. update_regset_xstate_info(xstate_size, pcntxt_mask);
  472. prepare_fx_sw_frame();
  473. setup_init_fpu_buf();
  474. /* Auto enable eagerfpu for xsaveopt */
  475. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  476. eagerfpu = ENABLE;
  477. pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x\n",
  478. pcntxt_mask, xstate_size);
  479. }
  480. /*
  481. * For the very first instance, this calls xstate_enable_boot_cpu();
  482. * for all subsequent instances, this calls xstate_enable().
  483. *
  484. * This is somewhat obfuscated due to the lack of powerful enough
  485. * overrides for the section checks.
  486. */
  487. void __cpuinit xsave_init(void)
  488. {
  489. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  490. void (*this_func)(void);
  491. if (!cpu_has_xsave)
  492. return;
  493. this_func = next_func;
  494. next_func = xstate_enable;
  495. this_func();
  496. }
  497. static inline void __init eager_fpu_init_bp(void)
  498. {
  499. current->thread.fpu.state =
  500. alloc_bootmem_align(xstate_size, __alignof__(struct xsave_struct));
  501. if (!init_xstate_buf)
  502. setup_init_fpu_buf();
  503. }
  504. void __cpuinit eager_fpu_init(void)
  505. {
  506. static __refdata void (*boot_func)(void) = eager_fpu_init_bp;
  507. clear_used_math();
  508. current_thread_info()->status = 0;
  509. if (eagerfpu == ENABLE)
  510. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  511. if (!cpu_has_eager_fpu) {
  512. stts();
  513. return;
  514. }
  515. if (boot_func) {
  516. boot_func();
  517. boot_func = NULL;
  518. }
  519. /*
  520. * This is same as math_state_restore(). But use_xsave() is
  521. * not yet patched to use math_state_restore().
  522. */
  523. init_fpu(current);
  524. __thread_fpu_begin(current);
  525. if (cpu_has_xsave)
  526. xrstor_state(init_xstate_buf, -1);
  527. else
  528. fxrstor_checking(&init_xstate_buf->i387);
  529. }