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