xsave.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * xsave/xrstor support.
  3. *
  4. * Author: Suresh Siddha <suresh.b.siddha@intel.com>
  5. */
  6. #include <linux/bootmem.h>
  7. #include <linux/compat.h>
  8. #include <asm/i387.h>
  9. #include <asm/fpu-internal.h>
  10. #ifdef CONFIG_IA32_EMULATION
  11. #include <asm/sigcontext32.h>
  12. #endif
  13. #include <asm/xcr.h>
  14. /*
  15. * Supported feature mask by the CPU and the kernel.
  16. */
  17. u64 pcntxt_mask;
  18. /*
  19. * Represents init state for the supported extended state.
  20. */
  21. static struct xsave_struct *init_xstate_buf;
  22. struct _fpx_sw_bytes fx_sw_reserved;
  23. #ifdef CONFIG_IA32_EMULATION
  24. struct _fpx_sw_bytes fx_sw_reserved_ia32;
  25. #endif
  26. static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
  27. /*
  28. * If a processor implementation discern that a processor state component is
  29. * in its initialized state it may modify the corresponding bit in the
  30. * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
  31. * layout in the case of xsaveopt. While presenting the xstate information to
  32. * the user, we always ensure that the memory layout of a feature will be in
  33. * the init state if the corresponding header bit is zero. This is to ensure
  34. * that the user doesn't see some stale state in the memory layout during
  35. * signal handling, debugging etc.
  36. */
  37. void __sanitize_i387_state(struct task_struct *tsk)
  38. {
  39. u64 xstate_bv;
  40. int feature_bit = 0x2;
  41. struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
  42. if (!fx)
  43. return;
  44. BUG_ON(__thread_has_fpu(tsk));
  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 *) (((void *)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. printk("save_i387_xstate: bad fpstate %p\n", 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 (err)
  150. return err;
  151. user_fpu_end();
  152. } else {
  153. sanitize_i387_state(tsk);
  154. if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
  155. xstate_size))
  156. return -1;
  157. }
  158. clear_used_math(); /* trigger finit */
  159. if (use_xsave()) {
  160. struct _fpstate __user *fx = buf;
  161. struct _xstate __user *x = buf;
  162. u64 xstate_bv;
  163. err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
  164. sizeof(struct _fpx_sw_bytes));
  165. err |= __put_user(FP_XSTATE_MAGIC2,
  166. (__u32 __user *) (buf + sig_xstate_size
  167. - FP_XSTATE_MAGIC2_SIZE));
  168. /*
  169. * Read the xstate_bv which we copied (directly from the cpu or
  170. * from the state in task struct) to the user buffers and
  171. * set the FP/SSE bits.
  172. */
  173. err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  174. /*
  175. * For legacy compatible, we always set FP/SSE bits in the bit
  176. * vector while saving the state to the user context. This will
  177. * enable us capturing any changes(during sigreturn) to
  178. * the FP/SSE bits by the legacy applications which don't touch
  179. * xstate_bv in the xsave header.
  180. *
  181. * xsave aware apps can change the xstate_bv in the xsave
  182. * header as well as change any contents in the memory layout.
  183. * xrestore as part of sigreturn will capture all the changes.
  184. */
  185. xstate_bv |= XSTATE_FPSSE;
  186. err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  187. if (err)
  188. return err;
  189. }
  190. return 1;
  191. }
  192. /*
  193. * Restore the extended state if present. Otherwise, restore the FP/SSE
  194. * state.
  195. */
  196. static int restore_user_xstate(void __user *buf)
  197. {
  198. struct _fpx_sw_bytes fx_sw_user;
  199. u64 mask;
  200. int err;
  201. if (((unsigned long)buf % 64) ||
  202. check_for_xstate(buf, buf, &fx_sw_user))
  203. goto fx_only;
  204. mask = fx_sw_user.xstate_bv;
  205. /*
  206. * restore the state passed by the user.
  207. */
  208. err = xrestore_user(buf, mask);
  209. if (err)
  210. return err;
  211. /*
  212. * init the state skipped by the user.
  213. */
  214. mask = pcntxt_mask & ~mask;
  215. if (unlikely(mask))
  216. xrstor_state(init_xstate_buf, mask);
  217. return 0;
  218. fx_only:
  219. /*
  220. * couldn't find the extended state information in the
  221. * memory layout. Restore just the FP/SSE and init all
  222. * the other extended state.
  223. */
  224. xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
  225. return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
  226. }
  227. /*
  228. * This restores directly out of user space. Exceptions are handled.
  229. */
  230. int restore_i387_xstate(void __user *buf)
  231. {
  232. struct task_struct *tsk = current;
  233. int err = 0;
  234. if (!buf) {
  235. if (used_math())
  236. goto clear;
  237. return 0;
  238. } else
  239. if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
  240. return -EACCES;
  241. if (!used_math()) {
  242. err = init_fpu(tsk);
  243. if (err)
  244. return err;
  245. }
  246. user_fpu_begin();
  247. if (use_xsave())
  248. err = restore_user_xstate(buf);
  249. else
  250. err = fxrstor_checking((__force struct i387_fxsave_struct *)
  251. buf);
  252. if (unlikely(err)) {
  253. /*
  254. * Encountered an error while doing the restore from the
  255. * user buffer, clear the fpu state.
  256. */
  257. clear:
  258. clear_fpu(tsk);
  259. clear_used_math();
  260. }
  261. return err;
  262. }
  263. #endif
  264. /*
  265. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  266. * the presence of the extended state information in the memory layout
  267. * pointed by the fpstate pointer in the sigcontext.
  268. * This will be saved when ever the FP and extended state context is
  269. * saved on the user stack during the signal handler delivery to the user.
  270. */
  271. static void prepare_fx_sw_frame(void)
  272. {
  273. int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
  274. FP_XSTATE_MAGIC2_SIZE;
  275. sig_xstate_size = sizeof(struct _fpstate) + size_extended;
  276. #ifdef CONFIG_IA32_EMULATION
  277. sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
  278. #endif
  279. memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
  280. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  281. fx_sw_reserved.extended_size = sig_xstate_size;
  282. fx_sw_reserved.xstate_bv = pcntxt_mask;
  283. fx_sw_reserved.xstate_size = xstate_size;
  284. #ifdef CONFIG_IA32_EMULATION
  285. memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
  286. sizeof(struct _fpx_sw_bytes));
  287. fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
  288. #endif
  289. }
  290. #ifdef CONFIG_X86_64
  291. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  292. #endif
  293. /*
  294. * Enable the extended processor state save/restore feature
  295. */
  296. static inline void xstate_enable(void)
  297. {
  298. set_in_cr4(X86_CR4_OSXSAVE);
  299. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  300. }
  301. /*
  302. * Record the offsets and sizes of different state managed by the xsave
  303. * memory layout.
  304. */
  305. static void __init setup_xstate_features(void)
  306. {
  307. int eax, ebx, ecx, edx, leaf = 0x2;
  308. xstate_features = fls64(pcntxt_mask);
  309. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  310. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  311. do {
  312. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  313. if (eax == 0)
  314. break;
  315. xstate_offsets[leaf] = ebx;
  316. xstate_sizes[leaf] = eax;
  317. leaf++;
  318. } while (1);
  319. }
  320. /*
  321. * setup the xstate image representing the init state
  322. */
  323. static void __init setup_xstate_init(void)
  324. {
  325. setup_xstate_features();
  326. /*
  327. * Setup init_xstate_buf to represent the init state of
  328. * all the features managed by the xsave
  329. */
  330. init_xstate_buf = alloc_bootmem_align(xstate_size,
  331. __alignof__(struct xsave_struct));
  332. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  333. clts();
  334. /*
  335. * Init all the features state with header_bv being 0x0
  336. */
  337. xrstor_state(init_xstate_buf, -1);
  338. /*
  339. * Dump the init state again. This is to identify the init state
  340. * of any feature which is not represented by all zero's.
  341. */
  342. xsave_state(init_xstate_buf, -1);
  343. stts();
  344. }
  345. /*
  346. * Enable and initialize the xsave feature.
  347. */
  348. static void __init xstate_enable_boot_cpu(void)
  349. {
  350. unsigned int eax, ebx, ecx, edx;
  351. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  352. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  353. return;
  354. }
  355. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  356. pcntxt_mask = eax + ((u64)edx << 32);
  357. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  358. printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
  359. pcntxt_mask);
  360. BUG();
  361. }
  362. /*
  363. * Support only the state known to OS.
  364. */
  365. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  366. xstate_enable();
  367. /*
  368. * Recompute the context size for enabled features
  369. */
  370. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  371. xstate_size = ebx;
  372. update_regset_xstate_info(xstate_size, pcntxt_mask);
  373. prepare_fx_sw_frame();
  374. setup_xstate_init();
  375. printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
  376. "cntxt size 0x%x\n",
  377. pcntxt_mask, xstate_size);
  378. }
  379. /*
  380. * For the very first instance, this calls xstate_enable_boot_cpu();
  381. * for all subsequent instances, this calls xstate_enable().
  382. *
  383. * This is somewhat obfuscated due to the lack of powerful enough
  384. * overrides for the section checks.
  385. */
  386. void __cpuinit xsave_init(void)
  387. {
  388. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  389. void (*this_func)(void);
  390. if (!cpu_has_xsave)
  391. return;
  392. this_func = next_func;
  393. next_func = xstate_enable;
  394. this_func();
  395. }