xsave.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. static 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. user_fpu_end();
  219. } else {
  220. sanitize_i387_state(tsk);
  221. if (__copy_to_user(buf_fx, xsave, xstate_size))
  222. return -1;
  223. }
  224. /* Save the fsave header for the 32-bit frames. */
  225. if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
  226. return -1;
  227. if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
  228. return -1;
  229. drop_fpu(tsk); /* trigger finit */
  230. return 0;
  231. }
  232. static inline void
  233. sanitize_restored_xstate(struct task_struct *tsk,
  234. struct user_i387_ia32_struct *ia32_env,
  235. u64 xstate_bv, int fx_only)
  236. {
  237. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  238. struct xsave_hdr_struct *xsave_hdr = &xsave->xsave_hdr;
  239. if (use_xsave()) {
  240. /* These bits must be zero. */
  241. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  242. /*
  243. * Init the state that is not present in the memory
  244. * layout and not enabled by the OS.
  245. */
  246. if (fx_only)
  247. xsave_hdr->xstate_bv = XSTATE_FPSSE;
  248. else
  249. xsave_hdr->xstate_bv &= (pcntxt_mask & xstate_bv);
  250. }
  251. if (use_fxsr()) {
  252. /*
  253. * mscsr reserved bits must be masked to zero for security
  254. * reasons.
  255. */
  256. xsave->i387.mxcsr &= mxcsr_feature_mask;
  257. convert_to_fxsr(tsk, ia32_env);
  258. }
  259. }
  260. /*
  261. * Restore the extended state if present. Otherwise, restore the FP/SSE state.
  262. */
  263. static inline int restore_user_xstate(void __user *buf, u64 xbv, int fx_only)
  264. {
  265. if (use_xsave()) {
  266. if ((unsigned long)buf % 64 || fx_only) {
  267. u64 init_bv = pcntxt_mask & ~XSTATE_FPSSE;
  268. xrstor_state(init_xstate_buf, init_bv);
  269. return fxrstor_checking((__force void *) buf);
  270. } else {
  271. u64 init_bv = pcntxt_mask & ~xbv;
  272. if (unlikely(init_bv))
  273. xrstor_state(init_xstate_buf, init_bv);
  274. return xrestore_user(buf, xbv);
  275. }
  276. } else if (use_fxsr()) {
  277. return fxrstor_checking((__force void *) buf);
  278. } else
  279. return frstor_checking((__force void *) buf);
  280. }
  281. int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)
  282. {
  283. int ia32_fxstate = (buf != buf_fx);
  284. struct task_struct *tsk = current;
  285. int state_size = xstate_size;
  286. u64 xstate_bv = 0;
  287. int fx_only = 0;
  288. ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
  289. config_enabled(CONFIG_IA32_EMULATION));
  290. if (!buf) {
  291. drop_fpu(tsk);
  292. return 0;
  293. }
  294. if (!access_ok(VERIFY_READ, buf, size))
  295. return -EACCES;
  296. if (!used_math() && init_fpu(tsk))
  297. return -1;
  298. if (!HAVE_HWFP) {
  299. return fpregs_soft_set(current, NULL,
  300. 0, sizeof(struct user_i387_ia32_struct),
  301. NULL, buf) != 0;
  302. }
  303. if (use_xsave()) {
  304. struct _fpx_sw_bytes fx_sw_user;
  305. if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
  306. /*
  307. * Couldn't find the extended state information in the
  308. * memory layout. Restore just the FP/SSE and init all
  309. * the other extended state.
  310. */
  311. state_size = sizeof(struct i387_fxsave_struct);
  312. fx_only = 1;
  313. } else {
  314. state_size = fx_sw_user.xstate_size;
  315. xstate_bv = fx_sw_user.xstate_bv;
  316. }
  317. }
  318. if (ia32_fxstate) {
  319. /*
  320. * For 32-bit frames with fxstate, copy the user state to the
  321. * thread's fpu state, reconstruct fxstate from the fsave
  322. * header. Sanitize the copied state etc.
  323. */
  324. struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;
  325. struct user_i387_ia32_struct env;
  326. drop_fpu(tsk);
  327. if (__copy_from_user(xsave, buf_fx, state_size) ||
  328. __copy_from_user(&env, buf, sizeof(env)))
  329. return -1;
  330. sanitize_restored_xstate(tsk, &env, xstate_bv, fx_only);
  331. set_used_math();
  332. } else {
  333. /*
  334. * For 64-bit frames and 32-bit fsave frames, restore the user
  335. * state to the registers directly (with exceptions handled).
  336. */
  337. user_fpu_begin();
  338. if (restore_user_xstate(buf_fx, xstate_bv, fx_only)) {
  339. drop_fpu(tsk);
  340. return -1;
  341. }
  342. }
  343. return 0;
  344. }
  345. /*
  346. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  347. * the presence of the extended state information in the memory layout
  348. * pointed by the fpstate pointer in the sigcontext.
  349. * This will be saved when ever the FP and extended state context is
  350. * saved on the user stack during the signal handler delivery to the user.
  351. */
  352. static void prepare_fx_sw_frame(void)
  353. {
  354. int fsave_header_size = sizeof(struct i387_fsave_struct);
  355. int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
  356. if (config_enabled(CONFIG_X86_32))
  357. size += fsave_header_size;
  358. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  359. fx_sw_reserved.extended_size = size;
  360. fx_sw_reserved.xstate_bv = pcntxt_mask;
  361. fx_sw_reserved.xstate_size = xstate_size;
  362. if (config_enabled(CONFIG_IA32_EMULATION)) {
  363. fx_sw_reserved_ia32 = fx_sw_reserved;
  364. fx_sw_reserved_ia32.extended_size += fsave_header_size;
  365. }
  366. }
  367. /*
  368. * Enable the extended processor state save/restore feature
  369. */
  370. static inline void xstate_enable(void)
  371. {
  372. set_in_cr4(X86_CR4_OSXSAVE);
  373. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  374. }
  375. /*
  376. * Record the offsets and sizes of different state managed by the xsave
  377. * memory layout.
  378. */
  379. static void __init setup_xstate_features(void)
  380. {
  381. int eax, ebx, ecx, edx, leaf = 0x2;
  382. xstate_features = fls64(pcntxt_mask);
  383. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  384. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  385. do {
  386. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  387. if (eax == 0)
  388. break;
  389. xstate_offsets[leaf] = ebx;
  390. xstate_sizes[leaf] = eax;
  391. leaf++;
  392. } while (1);
  393. }
  394. /*
  395. * setup the xstate image representing the init state
  396. */
  397. static void __init setup_xstate_init(void)
  398. {
  399. setup_xstate_features();
  400. /*
  401. * Setup init_xstate_buf to represent the init state of
  402. * all the features managed by the xsave
  403. */
  404. init_xstate_buf = alloc_bootmem_align(xstate_size,
  405. __alignof__(struct xsave_struct));
  406. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  407. clts();
  408. /*
  409. * Init all the features state with header_bv being 0x0
  410. */
  411. xrstor_state(init_xstate_buf, -1);
  412. /*
  413. * Dump the init state again. This is to identify the init state
  414. * of any feature which is not represented by all zero's.
  415. */
  416. xsave_state(init_xstate_buf, -1);
  417. stts();
  418. }
  419. /*
  420. * Enable and initialize the xsave feature.
  421. */
  422. static void __init xstate_enable_boot_cpu(void)
  423. {
  424. unsigned int eax, ebx, ecx, edx;
  425. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  426. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  427. return;
  428. }
  429. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  430. pcntxt_mask = eax + ((u64)edx << 32);
  431. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  432. pr_err("FP/SSE not shown under xsave features 0x%llx\n",
  433. pcntxt_mask);
  434. BUG();
  435. }
  436. /*
  437. * Support only the state known to OS.
  438. */
  439. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  440. xstate_enable();
  441. /*
  442. * Recompute the context size for enabled features
  443. */
  444. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  445. xstate_size = ebx;
  446. update_regset_xstate_info(xstate_size, pcntxt_mask);
  447. prepare_fx_sw_frame();
  448. setup_xstate_init();
  449. pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x\n",
  450. pcntxt_mask, xstate_size);
  451. }
  452. /*
  453. * For the very first instance, this calls xstate_enable_boot_cpu();
  454. * for all subsequent instances, this calls xstate_enable().
  455. *
  456. * This is somewhat obfuscated due to the lack of powerful enough
  457. * overrides for the section checks.
  458. */
  459. void __cpuinit xsave_init(void)
  460. {
  461. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  462. void (*this_func)(void);
  463. if (!cpu_has_xsave)
  464. return;
  465. this_func = next_func;
  466. next_func = xstate_enable;
  467. this_func();
  468. }