xsave.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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(task_thread_info(tsk)->status & TS_USEDFPU);
  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 (task_thread_info(tsk)->status & TS_USEDFPU) {
  144. if (use_xsave())
  145. err = xsave_user(buf);
  146. else
  147. err = fxsave_user(buf);
  148. if (err)
  149. return err;
  150. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  151. stts();
  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. if (!(task_thread_info(current)->status & TS_USEDFPU)) {
  247. clts();
  248. task_thread_info(current)->status |= TS_USEDFPU;
  249. }
  250. if (use_xsave())
  251. err = restore_user_xstate(buf);
  252. else
  253. err = fxrstor_checking((__force struct i387_fxsave_struct *)
  254. buf);
  255. if (unlikely(err)) {
  256. /*
  257. * Encountered an error while doing the restore from the
  258. * user buffer, clear the fpu state.
  259. */
  260. clear:
  261. clear_fpu(tsk);
  262. clear_used_math();
  263. }
  264. return err;
  265. }
  266. #endif
  267. /*
  268. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  269. * the presence of the extended state information in the memory layout
  270. * pointed by the fpstate pointer in the sigcontext.
  271. * This will be saved when ever the FP and extended state context is
  272. * saved on the user stack during the signal handler delivery to the user.
  273. */
  274. static void prepare_fx_sw_frame(void)
  275. {
  276. int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
  277. FP_XSTATE_MAGIC2_SIZE;
  278. sig_xstate_size = sizeof(struct _fpstate) + size_extended;
  279. #ifdef CONFIG_IA32_EMULATION
  280. sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
  281. #endif
  282. memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
  283. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  284. fx_sw_reserved.extended_size = sig_xstate_size;
  285. fx_sw_reserved.xstate_bv = pcntxt_mask;
  286. fx_sw_reserved.xstate_size = xstate_size;
  287. #ifdef CONFIG_IA32_EMULATION
  288. memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
  289. sizeof(struct _fpx_sw_bytes));
  290. fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
  291. #endif
  292. }
  293. #ifdef CONFIG_X86_64
  294. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  295. #endif
  296. /*
  297. * Enable the extended processor state save/restore feature
  298. */
  299. static inline void xstate_enable(void)
  300. {
  301. set_in_cr4(X86_CR4_OSXSAVE);
  302. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  303. }
  304. /*
  305. * Record the offsets and sizes of different state managed by the xsave
  306. * memory layout.
  307. */
  308. static void __init setup_xstate_features(void)
  309. {
  310. int eax, ebx, ecx, edx, leaf = 0x2;
  311. xstate_features = fls64(pcntxt_mask);
  312. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  313. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  314. do {
  315. cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
  316. if (eax == 0)
  317. break;
  318. xstate_offsets[leaf] = ebx;
  319. xstate_sizes[leaf] = eax;
  320. leaf++;
  321. } while (1);
  322. }
  323. /*
  324. * setup the xstate image representing the init state
  325. */
  326. static void __init setup_xstate_init(void)
  327. {
  328. setup_xstate_features();
  329. /*
  330. * Setup init_xstate_buf to represent the init state of
  331. * all the features managed by the xsave
  332. */
  333. init_xstate_buf = alloc_bootmem_align(xstate_size,
  334. __alignof__(struct xsave_struct));
  335. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  336. clts();
  337. /*
  338. * Init all the features state with header_bv being 0x0
  339. */
  340. xrstor_state(init_xstate_buf, -1);
  341. /*
  342. * Dump the init state again. This is to identify the init state
  343. * of any feature which is not represented by all zero's.
  344. */
  345. xsave_state(init_xstate_buf, -1);
  346. stts();
  347. }
  348. /*
  349. * Enable and initialize the xsave feature.
  350. */
  351. static void __init xstate_enable_boot_cpu(void)
  352. {
  353. unsigned int eax, ebx, ecx, edx;
  354. if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
  355. WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
  356. return;
  357. }
  358. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  359. pcntxt_mask = eax + ((u64)edx << 32);
  360. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  361. printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
  362. pcntxt_mask);
  363. BUG();
  364. }
  365. /*
  366. * Support only the state known to OS.
  367. */
  368. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  369. xstate_enable();
  370. /*
  371. * Recompute the context size for enabled features
  372. */
  373. cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
  374. xstate_size = ebx;
  375. update_regset_xstate_info(xstate_size, pcntxt_mask);
  376. prepare_fx_sw_frame();
  377. setup_xstate_init();
  378. printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
  379. "cntxt size 0x%x\n",
  380. pcntxt_mask, xstate_size);
  381. }
  382. /*
  383. * For the very first instance, this calls xstate_enable_boot_cpu();
  384. * for all subsequent instances, this calls xstate_enable().
  385. *
  386. * This is somewhat obfuscated due to the lack of powerful enough
  387. * overrides for the section checks.
  388. */
  389. void __cpuinit xsave_init(void)
  390. {
  391. static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
  392. void (*this_func)(void);
  393. if (!cpu_has_xsave)
  394. return;
  395. this_func = next_func;
  396. next_func = xstate_enable;
  397. this_func();
  398. }