xsave.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. #ifdef CONFIG_IA32_EMULATION
  12. #include <asm/sigcontext32.h>
  13. #endif
  14. #include <asm/xcr.h>
  15. /*
  16. * Supported feature mask by the CPU and the kernel.
  17. */
  18. u64 pcntxt_mask;
  19. /*
  20. * Represents init state for the supported extended state.
  21. */
  22. static struct xsave_struct *init_xstate_buf;
  23. struct _fpx_sw_bytes fx_sw_reserved;
  24. #ifdef CONFIG_IA32_EMULATION
  25. struct _fpx_sw_bytes fx_sw_reserved_ia32;
  26. #endif
  27. static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
  28. /*
  29. * If a processor implementation discern that a processor state component is
  30. * in its initialized state it may modify the corresponding bit in the
  31. * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
  32. * layout in the case of xsaveopt. While presenting the xstate information to
  33. * the user, we always ensure that the memory layout of a feature will be in
  34. * the init state if the corresponding header bit is zero. This is to ensure
  35. * that the user doesn't see some stale state in the memory layout during
  36. * signal handling, debugging etc.
  37. */
  38. void __sanitize_i387_state(struct task_struct *tsk)
  39. {
  40. u64 xstate_bv;
  41. int feature_bit = 0x2;
  42. struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
  43. if (!fx)
  44. return;
  45. xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
  46. /*
  47. * None of the feature bits are in init state. So nothing else
  48. * to do for us, as the memory layout is up to date.
  49. */
  50. if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
  51. return;
  52. /*
  53. * FP is in init state
  54. */
  55. if (!(xstate_bv & XSTATE_FP)) {
  56. fx->cwd = 0x37f;
  57. fx->swd = 0;
  58. fx->twd = 0;
  59. fx->fop = 0;
  60. fx->rip = 0;
  61. fx->rdp = 0;
  62. memset(&fx->st_space[0], 0, 128);
  63. }
  64. /*
  65. * SSE is in init state
  66. */
  67. if (!(xstate_bv & XSTATE_SSE))
  68. memset(&fx->xmm_space[0], 0, 256);
  69. xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
  70. /*
  71. * Update all the other memory layouts for which the corresponding
  72. * header bit is in the init state.
  73. */
  74. while (xstate_bv) {
  75. if (xstate_bv & 0x1) {
  76. int offset = xstate_offsets[feature_bit];
  77. int size = xstate_sizes[feature_bit];
  78. memcpy(((void *) fx) + offset,
  79. ((void *) init_xstate_buf) + offset,
  80. size);
  81. }
  82. xstate_bv >>= 1;
  83. feature_bit++;
  84. }
  85. }
  86. /*
  87. * Check for the presence of extended state information in the
  88. * user fpstate pointer in the sigcontext.
  89. */
  90. int check_for_xstate(struct i387_fxsave_struct __user *buf,
  91. void __user *fpstate,
  92. struct _fpx_sw_bytes *fx_sw_user)
  93. {
  94. int min_xstate_size = sizeof(struct i387_fxsave_struct) +
  95. sizeof(struct xsave_hdr_struct);
  96. unsigned int magic2;
  97. int err;
  98. err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
  99. sizeof(struct _fpx_sw_bytes));
  100. if (err)
  101. return -EFAULT;
  102. /*
  103. * First Magic check failed.
  104. */
  105. if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
  106. return -EINVAL;
  107. /*
  108. * Check for error scenarios.
  109. */
  110. if (fx_sw_user->xstate_size < min_xstate_size ||
  111. fx_sw_user->xstate_size > xstate_size ||
  112. fx_sw_user->xstate_size > fx_sw_user->extended_size)
  113. return -EINVAL;
  114. err = __get_user(magic2, (__u32 __user *) (fpstate +
  115. fx_sw_user->extended_size -
  116. FP_XSTATE_MAGIC2_SIZE));
  117. if (err)
  118. return err;
  119. /*
  120. * Check for the presence of second magic word at the end of memory
  121. * layout. This detects the case where the user just copied the legacy
  122. * fpstate layout with out copying the extended state information
  123. * in the memory layout.
  124. */
  125. if (magic2 != FP_XSTATE_MAGIC2)
  126. return -EFAULT;
  127. return 0;
  128. }
  129. #ifdef CONFIG_X86_64
  130. /*
  131. * Signal frame handlers.
  132. */
  133. int save_i387_xstate(void __user *buf)
  134. {
  135. struct task_struct *tsk = current;
  136. int err = 0;
  137. if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
  138. return -EACCES;
  139. BUG_ON(sig_xstate_size < xstate_size);
  140. if ((unsigned long)buf % 64)
  141. pr_err("%s: bad fpstate %p\n", __func__, buf);
  142. if (!used_math())
  143. return 0;
  144. if (user_has_fpu()) {
  145. if (use_xsave())
  146. err = xsave_user(buf);
  147. else
  148. err = fxsave_user(buf);
  149. if (unlikely(err)) {
  150. __clear_user(buf, xstate_size);
  151. return err;
  152. }
  153. user_fpu_end();
  154. } else {
  155. sanitize_i387_state(tsk);
  156. if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
  157. xstate_size))
  158. return -1;
  159. }
  160. clear_used_math(); /* trigger finit */
  161. if (use_xsave()) {
  162. struct _fpstate __user *fx = buf;
  163. struct _xstate __user *x = buf;
  164. u64 xstate_bv;
  165. err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
  166. sizeof(struct _fpx_sw_bytes));
  167. err |= __put_user(FP_XSTATE_MAGIC2,
  168. (__u32 __user *) (buf + sig_xstate_size
  169. - FP_XSTATE_MAGIC2_SIZE));
  170. /*
  171. * Read the xstate_bv which we copied (directly from the cpu or
  172. * from the state in task struct) to the user buffers and
  173. * set the FP/SSE bits.
  174. */
  175. err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  176. /*
  177. * For legacy compatible, we always set FP/SSE bits in the bit
  178. * vector while saving the state to the user context. This will
  179. * enable us capturing any changes(during sigreturn) to
  180. * the FP/SSE bits by the legacy applications which don't touch
  181. * xstate_bv in the xsave header.
  182. *
  183. * xsave aware apps can change the xstate_bv in the xsave
  184. * header as well as change any contents in the memory layout.
  185. * xrestore as part of sigreturn will capture all the changes.
  186. */
  187. xstate_bv |= XSTATE_FPSSE;
  188. err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  189. if (err)
  190. return err;
  191. }
  192. return 1;
  193. }
  194. /*
  195. * Restore the extended state if present. Otherwise, restore the FP/SSE
  196. * state.
  197. */
  198. static int restore_user_xstate(void __user *buf)
  199. {
  200. struct _fpx_sw_bytes fx_sw_user;
  201. u64 mask;
  202. int err;
  203. if (((unsigned long)buf % 64) ||
  204. check_for_xstate(buf, buf, &fx_sw_user))
  205. goto fx_only;
  206. mask = fx_sw_user.xstate_bv;
  207. /*
  208. * restore the state passed by the user.
  209. */
  210. err = xrestore_user(buf, mask);
  211. if (err)
  212. return err;
  213. /*
  214. * init the state skipped by the user.
  215. */
  216. mask = pcntxt_mask & ~mask;
  217. if (unlikely(mask))
  218. xrstor_state(init_xstate_buf, mask);
  219. return 0;
  220. fx_only:
  221. /*
  222. * couldn't find the extended state information in the
  223. * memory layout. Restore just the FP/SSE and init all
  224. * the other extended state.
  225. */
  226. xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
  227. return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
  228. }
  229. /*
  230. * This restores directly out of user space. Exceptions are handled.
  231. */
  232. int restore_i387_xstate(void __user *buf)
  233. {
  234. struct task_struct *tsk = current;
  235. int err = 0;
  236. if (!buf) {
  237. if (used_math())
  238. goto clear;
  239. return 0;
  240. } else
  241. if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
  242. return -EACCES;
  243. if (!used_math()) {
  244. err = init_fpu(tsk);
  245. if (err)
  246. return err;
  247. }
  248. user_fpu_begin();
  249. if (use_xsave())
  250. err = restore_user_xstate(buf);
  251. else
  252. err = fxrstor_checking((__force struct i387_fxsave_struct *)
  253. buf);
  254. if (unlikely(err)) {
  255. /*
  256. * Encountered an error while doing the restore from the
  257. * user buffer, clear the fpu state.
  258. */
  259. clear:
  260. clear_fpu(tsk);
  261. clear_used_math();
  262. }
  263. return err;
  264. }
  265. #endif
  266. /*
  267. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  268. * the presence of the extended state information in the memory layout
  269. * pointed by the fpstate pointer in the sigcontext.
  270. * This will be saved when ever the FP and extended state context is
  271. * saved on the user stack during the signal handler delivery to the user.
  272. */
  273. static void prepare_fx_sw_frame(void)
  274. {
  275. int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
  276. FP_XSTATE_MAGIC2_SIZE;
  277. sig_xstate_size = sizeof(struct _fpstate) + size_extended;
  278. #ifdef CONFIG_IA32_EMULATION
  279. sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
  280. #endif
  281. memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
  282. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  283. fx_sw_reserved.extended_size = sig_xstate_size;
  284. fx_sw_reserved.xstate_bv = pcntxt_mask;
  285. fx_sw_reserved.xstate_size = xstate_size;
  286. #ifdef CONFIG_IA32_EMULATION
  287. memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
  288. sizeof(struct _fpx_sw_bytes));
  289. fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
  290. #endif
  291. }
  292. #ifdef CONFIG_X86_64
  293. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  294. #endif
  295. /*
  296. * Enable the extended processor state save/restore feature
  297. */
  298. static inline void xstate_enable(void)
  299. {
  300. set_in_cr4(X86_CR4_OSXSAVE);
  301. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  302. }
  303. /*
  304. * Record the offsets and sizes of different state managed by the xsave
  305. * memory layout.
  306. */
  307. static void __init setup_xstate_features(void)
  308. {
  309. int eax, ebx, ecx, edx, leaf = 0x2;
  310. xstate_features = fls64(pcntxt_mask);
  311. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  312. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  313. do {
  314. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  315. if (eax == 0)
  316. break;
  317. xstate_offsets[leaf] = ebx;
  318. xstate_sizes[leaf] = eax;
  319. leaf++;
  320. } while (1);
  321. }
  322. /*
  323. * setup the xstate image representing the init state
  324. */
  325. static void __init setup_xstate_init(void)
  326. {
  327. setup_xstate_features();
  328. /*
  329. * Setup init_xstate_buf to represent the init state of
  330. * all the features managed by the xsave
  331. */
  332. init_xstate_buf = alloc_bootmem_align(xstate_size,
  333. __alignof__(struct xsave_struct));
  334. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  335. clts();
  336. /*
  337. * Init all the features state with header_bv being 0x0
  338. */
  339. xrstor_state(init_xstate_buf, -1);
  340. /*
  341. * Dump the init state again. This is to identify the init state
  342. * of any feature which is not represented by all zero's.
  343. */
  344. xsave_state(init_xstate_buf, -1);
  345. stts();
  346. }
  347. /*
  348. * Enable and initialize the xsave feature.
  349. */
  350. static void __init xstate_enable_boot_cpu(void)
  351. {
  352. unsigned int eax, ebx, ecx, edx;
  353. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  354. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  355. return;
  356. }
  357. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  358. pcntxt_mask = eax + ((u64)edx << 32);
  359. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  360. pr_err("FP/SSE not shown under xsave features 0x%llx\n",
  361. pcntxt_mask);
  362. BUG();
  363. }
  364. /*
  365. * Support only the state known to OS.
  366. */
  367. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  368. xstate_enable();
  369. /*
  370. * Recompute the context size for enabled features
  371. */
  372. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  373. xstate_size = ebx;
  374. update_regset_xstate_info(xstate_size, pcntxt_mask);
  375. prepare_fx_sw_frame();
  376. setup_xstate_init();
  377. pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x\n",
  378. pcntxt_mask, xstate_size);
  379. }
  380. /*
  381. * For the very first instance, this calls xstate_enable_boot_cpu();
  382. * for all subsequent instances, this calls xstate_enable().
  383. *
  384. * This is somewhat obfuscated due to the lack of powerful enough
  385. * overrides for the section checks.
  386. */
  387. void __cpuinit xsave_init(void)
  388. {
  389. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  390. void (*this_func)(void);
  391. if (!cpu_has_xsave)
  392. return;
  393. this_func = next_func;
  394. next_func = xstate_enable;
  395. this_func();
  396. }