xsave.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. struct _fpx_sw_bytes fx_sw_reserved;
  18. #ifdef CONFIG_IA32_EMULATION
  19. struct _fpx_sw_bytes fx_sw_reserved_ia32;
  20. #endif
  21. static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
  22. /*
  23. * If a processor implementation discern that a processor state component is
  24. * in its initialized state it may modify the corresponding bit in the
  25. * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
  26. * layout in the case of xsaveopt. While presenting the xstate information to
  27. * the user, we always ensure that the memory layout of a feature will be in
  28. * the init state if the corresponding header bit is zero. This is to ensure
  29. * that the user doesn't see some stale state in the memory layout during
  30. * signal handling, debugging etc.
  31. */
  32. void __sanitize_i387_state(struct task_struct *tsk)
  33. {
  34. u64 xstate_bv;
  35. int feature_bit = 0x2;
  36. struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
  37. if (!fx)
  38. return;
  39. BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU);
  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 upto 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. int check_for_xstate(struct i387_fxsave_struct __user *buf,
  86. void __user *fpstate,
  87. struct _fpx_sw_bytes *fx_sw_user)
  88. {
  89. int min_xstate_size = sizeof(struct i387_fxsave_struct) +
  90. sizeof(struct xsave_hdr_struct);
  91. unsigned int magic2;
  92. int err;
  93. err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
  94. sizeof(struct _fpx_sw_bytes));
  95. if (err)
  96. return -EFAULT;
  97. /*
  98. * First Magic check failed.
  99. */
  100. if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
  101. return -EINVAL;
  102. /*
  103. * Check for error scenarios.
  104. */
  105. if (fx_sw_user->xstate_size < min_xstate_size ||
  106. fx_sw_user->xstate_size > xstate_size ||
  107. fx_sw_user->xstate_size > fx_sw_user->extended_size)
  108. return -EINVAL;
  109. err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
  110. fx_sw_user->extended_size -
  111. FP_XSTATE_MAGIC2_SIZE));
  112. if (err)
  113. return err;
  114. /*
  115. * Check for the presence of second magic word at the end of memory
  116. * layout. This detects the case where the user just copied the legacy
  117. * fpstate layout with out copying the extended state information
  118. * in the memory layout.
  119. */
  120. if (magic2 != FP_XSTATE_MAGIC2)
  121. return -EFAULT;
  122. return 0;
  123. }
  124. #ifdef CONFIG_X86_64
  125. /*
  126. * Signal frame handlers.
  127. */
  128. int save_i387_xstate(void __user *buf)
  129. {
  130. struct task_struct *tsk = current;
  131. int err = 0;
  132. if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
  133. return -EACCES;
  134. BUG_ON(sig_xstate_size < xstate_size);
  135. if ((unsigned long)buf % 64)
  136. printk("save_i387_xstate: bad fpstate %p\n", buf);
  137. if (!used_math())
  138. return 0;
  139. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  140. /*
  141. * Start with clearing the user buffer. This will present a
  142. * clean context for the bytes not touched by the fxsave/xsave.
  143. */
  144. err = __clear_user(buf, sig_xstate_size);
  145. if (err)
  146. return err;
  147. if (use_xsave())
  148. err = xsave_user(buf);
  149. else
  150. err = fxsave_user(buf);
  151. if (err)
  152. return err;
  153. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  154. stts();
  155. } else {
  156. sanitize_i387_state(tsk);
  157. if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
  158. xstate_size))
  159. return -1;
  160. }
  161. clear_used_math(); /* trigger finit */
  162. if (use_xsave()) {
  163. struct _fpstate __user *fx = buf;
  164. struct _xstate __user *x = buf;
  165. u64 xstate_bv;
  166. err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
  167. sizeof(struct _fpx_sw_bytes));
  168. err |= __put_user(FP_XSTATE_MAGIC2,
  169. (__u32 __user *) (buf + sig_xstate_size
  170. - FP_XSTATE_MAGIC2_SIZE));
  171. /*
  172. * Read the xstate_bv which we copied (directly from the cpu or
  173. * from the state in task struct) to the user buffers and
  174. * set the FP/SSE bits.
  175. */
  176. err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  177. /*
  178. * For legacy compatible, we always set FP/SSE bits in the bit
  179. * vector while saving the state to the user context. This will
  180. * enable us capturing any changes(during sigreturn) to
  181. * the FP/SSE bits by the legacy applications which don't touch
  182. * xstate_bv in the xsave header.
  183. *
  184. * xsave aware apps can change the xstate_bv in the xsave
  185. * header as well as change any contents in the memory layout.
  186. * xrestore as part of sigreturn will capture all the changes.
  187. */
  188. xstate_bv |= XSTATE_FPSSE;
  189. err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  190. if (err)
  191. return err;
  192. }
  193. return 1;
  194. }
  195. /*
  196. * Restore the extended state if present. Otherwise, restore the FP/SSE
  197. * state.
  198. */
  199. static int restore_user_xstate(void __user *buf)
  200. {
  201. struct _fpx_sw_bytes fx_sw_user;
  202. u64 mask;
  203. int err;
  204. if (((unsigned long)buf % 64) ||
  205. check_for_xstate(buf, buf, &fx_sw_user))
  206. goto fx_only;
  207. mask = fx_sw_user.xstate_bv;
  208. /*
  209. * restore the state passed by the user.
  210. */
  211. err = xrestore_user(buf, mask);
  212. if (err)
  213. return err;
  214. /*
  215. * init the state skipped by the user.
  216. */
  217. mask = pcntxt_mask & ~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. if (!(task_thread_info(current)->status & TS_USEDFPU)) {
  249. clts();
  250. task_thread_info(current)->status |= TS_USEDFPU;
  251. }
  252. if (use_xsave())
  253. err = restore_user_xstate(buf);
  254. else
  255. err = fxrstor_checking((__force struct i387_fxsave_struct *)
  256. buf);
  257. if (unlikely(err)) {
  258. /*
  259. * Encountered an error while doing the restore from the
  260. * user buffer, clear the fpu state.
  261. */
  262. clear:
  263. clear_fpu(tsk);
  264. clear_used_math();
  265. }
  266. return err;
  267. }
  268. #endif
  269. /*
  270. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  271. * the presence of the extended state information in the memory layout
  272. * pointed by the fpstate pointer in the sigcontext.
  273. * This will be saved when ever the FP and extended state context is
  274. * saved on the user stack during the signal handler delivery to the user.
  275. */
  276. static void prepare_fx_sw_frame(void)
  277. {
  278. int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
  279. FP_XSTATE_MAGIC2_SIZE;
  280. sig_xstate_size = sizeof(struct _fpstate) + size_extended;
  281. #ifdef CONFIG_IA32_EMULATION
  282. sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
  283. #endif
  284. memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
  285. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  286. fx_sw_reserved.extended_size = sig_xstate_size;
  287. fx_sw_reserved.xstate_bv = pcntxt_mask;
  288. fx_sw_reserved.xstate_size = xstate_size;
  289. #ifdef CONFIG_IA32_EMULATION
  290. memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
  291. sizeof(struct _fpx_sw_bytes));
  292. fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
  293. #endif
  294. }
  295. /*
  296. * Represents init state for the supported extended state.
  297. */
  298. struct xsave_struct *init_xstate_buf;
  299. #ifdef CONFIG_X86_64
  300. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  301. #endif
  302. /*
  303. * Enable the extended processor state save/restore feature
  304. */
  305. void __cpuinit xsave_init(void)
  306. {
  307. if (!cpu_has_xsave)
  308. return;
  309. set_in_cr4(X86_CR4_OSXSAVE);
  310. /*
  311. * Enable all the features that the HW is capable of
  312. * and the Linux kernel is aware of.
  313. */
  314. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  315. }
  316. /*
  317. * Record the offsets and sizes of different state managed by the xsave
  318. * memory layout.
  319. */
  320. static void setup_xstate_features(void)
  321. {
  322. int eax, ebx, ecx, edx, leaf = 0x2;
  323. xstate_features = fls64(pcntxt_mask);
  324. xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
  325. xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
  326. do {
  327. cpuid_count(0xd, leaf, &eax, &ebx, &ecx, &edx);
  328. if (eax == 0)
  329. break;
  330. xstate_offsets[leaf] = ebx;
  331. xstate_sizes[leaf] = eax;
  332. leaf++;
  333. } while (1);
  334. }
  335. /*
  336. * setup the xstate image representing the init state
  337. */
  338. static void __init setup_xstate_init(void)
  339. {
  340. setup_xstate_features();
  341. /*
  342. * Setup init_xstate_buf to represent the init state of
  343. * all the features managed by the xsave
  344. */
  345. init_xstate_buf = alloc_bootmem(xstate_size);
  346. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  347. clts();
  348. /*
  349. * Init all the features state with header_bv being 0x0
  350. */
  351. xrstor_state(init_xstate_buf, -1);
  352. /*
  353. * Dump the init state again. This is to identify the init state
  354. * of any feature which is not represented by all zero's.
  355. */
  356. xsave_state(init_xstate_buf, -1);
  357. stts();
  358. }
  359. /*
  360. * Enable and initialize the xsave feature.
  361. */
  362. void __ref xsave_cntxt_init(void)
  363. {
  364. unsigned int eax, ebx, ecx, edx;
  365. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  366. pcntxt_mask = eax + ((u64)edx << 32);
  367. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  368. printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
  369. pcntxt_mask);
  370. BUG();
  371. }
  372. /*
  373. * Support only the state known to OS.
  374. */
  375. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  376. xsave_init();
  377. /*
  378. * Recompute the context size for enabled features
  379. */
  380. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  381. xstate_size = ebx;
  382. update_regset_xstate_info(xstate_size, pcntxt_mask);
  383. prepare_fx_sw_frame();
  384. setup_xstate_init();
  385. printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
  386. "cntxt size 0x%x\n",
  387. pcntxt_mask, xstate_size);
  388. }