xsave.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /*
  22. * Check for the presence of extended state information in the
  23. * user fpstate pointer in the sigcontext.
  24. */
  25. int check_for_xstate(struct i387_fxsave_struct __user *buf,
  26. void __user *fpstate,
  27. struct _fpx_sw_bytes *fx_sw_user)
  28. {
  29. int min_xstate_size = sizeof(struct i387_fxsave_struct) +
  30. sizeof(struct xsave_hdr_struct);
  31. unsigned int magic2;
  32. int err;
  33. err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
  34. sizeof(struct _fpx_sw_bytes));
  35. if (err)
  36. return err;
  37. /*
  38. * First Magic check failed.
  39. */
  40. if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
  41. return -1;
  42. /*
  43. * Check for error scenarios.
  44. */
  45. if (fx_sw_user->xstate_size < min_xstate_size ||
  46. fx_sw_user->xstate_size > xstate_size ||
  47. fx_sw_user->xstate_size > fx_sw_user->extended_size)
  48. return -1;
  49. err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
  50. fx_sw_user->extended_size -
  51. FP_XSTATE_MAGIC2_SIZE));
  52. /*
  53. * Check for the presence of second magic word at the end of memory
  54. * layout. This detects the case where the user just copied the legacy
  55. * fpstate layout with out copying the extended state information
  56. * in the memory layout.
  57. */
  58. if (err || magic2 != FP_XSTATE_MAGIC2)
  59. return -1;
  60. return 0;
  61. }
  62. #ifdef CONFIG_X86_64
  63. /*
  64. * Signal frame handlers.
  65. */
  66. int save_i387_xstate(void __user *buf)
  67. {
  68. struct task_struct *tsk = current;
  69. int err = 0;
  70. if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
  71. return -EACCES;
  72. BUG_ON(sig_xstate_size < xstate_size);
  73. if ((unsigned long)buf % 64)
  74. printk("save_i387_xstate: bad fpstate %p\n", buf);
  75. if (!used_math())
  76. return 0;
  77. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  78. /*
  79. * Start with clearing the user buffer. This will present a
  80. * clean context for the bytes not touched by the fxsave/xsave.
  81. */
  82. err = __clear_user(buf, sig_xstate_size);
  83. if (err)
  84. return err;
  85. if (task_thread_info(tsk)->status & TS_XSAVE)
  86. err = xsave_user(buf);
  87. else
  88. err = fxsave_user(buf);
  89. if (err)
  90. return err;
  91. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  92. stts();
  93. } else {
  94. if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
  95. xstate_size))
  96. return -1;
  97. }
  98. clear_used_math(); /* trigger finit */
  99. if (task_thread_info(tsk)->status & TS_XSAVE) {
  100. struct _fpstate __user *fx = buf;
  101. struct _xstate __user *x = buf;
  102. u64 xstate_bv;
  103. err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
  104. sizeof(struct _fpx_sw_bytes));
  105. err |= __put_user(FP_XSTATE_MAGIC2,
  106. (__u32 __user *) (buf + sig_xstate_size
  107. - FP_XSTATE_MAGIC2_SIZE));
  108. /*
  109. * Read the xstate_bv which we copied (directly from the cpu or
  110. * from the state in task struct) to the user buffers and
  111. * set the FP/SSE bits.
  112. */
  113. err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  114. /*
  115. * For legacy compatible, we always set FP/SSE bits in the bit
  116. * vector while saving the state to the user context. This will
  117. * enable us capturing any changes(during sigreturn) to
  118. * the FP/SSE bits by the legacy applications which don't touch
  119. * xstate_bv in the xsave header.
  120. *
  121. * xsave aware apps can change the xstate_bv in the xsave
  122. * header as well as change any contents in the memory layout.
  123. * xrestore as part of sigreturn will capture all the changes.
  124. */
  125. xstate_bv |= XSTATE_FPSSE;
  126. err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
  127. if (err)
  128. return err;
  129. }
  130. return 1;
  131. }
  132. /*
  133. * Restore the extended state if present. Otherwise, restore the FP/SSE
  134. * state.
  135. */
  136. static int restore_user_xstate(void __user *buf)
  137. {
  138. struct _fpx_sw_bytes fx_sw_user;
  139. u64 mask;
  140. int err;
  141. if (((unsigned long)buf % 64) ||
  142. check_for_xstate(buf, buf, &fx_sw_user))
  143. goto fx_only;
  144. mask = fx_sw_user.xstate_bv;
  145. /*
  146. * restore the state passed by the user.
  147. */
  148. err = xrestore_user(buf, mask);
  149. if (err)
  150. return err;
  151. /*
  152. * init the state skipped by the user.
  153. */
  154. mask = pcntxt_mask & ~mask;
  155. xrstor_state(init_xstate_buf, mask);
  156. return 0;
  157. fx_only:
  158. /*
  159. * couldn't find the extended state information in the
  160. * memory layout. Restore just the FP/SSE and init all
  161. * the other extended state.
  162. */
  163. xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
  164. return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
  165. }
  166. /*
  167. * This restores directly out of user space. Exceptions are handled.
  168. */
  169. int restore_i387_xstate(void __user *buf)
  170. {
  171. struct task_struct *tsk = current;
  172. int err = 0;
  173. if (!buf) {
  174. if (used_math())
  175. goto clear;
  176. return 0;
  177. } else
  178. if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
  179. return -EACCES;
  180. if (!used_math()) {
  181. err = init_fpu(tsk);
  182. if (err)
  183. return err;
  184. }
  185. if (!(task_thread_info(current)->status & TS_USEDFPU)) {
  186. clts();
  187. task_thread_info(current)->status |= TS_USEDFPU;
  188. }
  189. if (task_thread_info(tsk)->status & TS_XSAVE)
  190. err = restore_user_xstate(buf);
  191. else
  192. err = fxrstor_checking((__force struct i387_fxsave_struct *)
  193. buf);
  194. if (unlikely(err)) {
  195. /*
  196. * Encountered an error while doing the restore from the
  197. * user buffer, clear the fpu state.
  198. */
  199. clear:
  200. clear_fpu(tsk);
  201. clear_used_math();
  202. }
  203. return err;
  204. }
  205. #endif
  206. /*
  207. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  208. * the presence of the extended state information in the memory layout
  209. * pointed by the fpstate pointer in the sigcontext.
  210. * This will be saved when ever the FP and extended state context is
  211. * saved on the user stack during the signal handler delivery to the user.
  212. */
  213. static void prepare_fx_sw_frame(void)
  214. {
  215. int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
  216. FP_XSTATE_MAGIC2_SIZE;
  217. sig_xstate_size = sizeof(struct _fpstate) + size_extended;
  218. #ifdef CONFIG_IA32_EMULATION
  219. sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
  220. #endif
  221. memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
  222. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  223. fx_sw_reserved.extended_size = sig_xstate_size;
  224. fx_sw_reserved.xstate_bv = pcntxt_mask;
  225. fx_sw_reserved.xstate_size = xstate_size;
  226. #ifdef CONFIG_IA32_EMULATION
  227. memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
  228. sizeof(struct _fpx_sw_bytes));
  229. fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
  230. #endif
  231. }
  232. /*
  233. * Represents init state for the supported extended state.
  234. */
  235. struct xsave_struct *init_xstate_buf;
  236. #ifdef CONFIG_X86_64
  237. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  238. #endif
  239. /*
  240. * Enable the extended processor state save/restore feature
  241. */
  242. void __cpuinit xsave_init(void)
  243. {
  244. if (!cpu_has_xsave)
  245. return;
  246. set_in_cr4(X86_CR4_OSXSAVE);
  247. /*
  248. * Enable all the features that the HW is capable of
  249. * and the Linux kernel is aware of.
  250. */
  251. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  252. }
  253. /*
  254. * setup the xstate image representing the init state
  255. */
  256. static void __init setup_xstate_init(void)
  257. {
  258. init_xstate_buf = alloc_bootmem(xstate_size);
  259. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  260. }
  261. /*
  262. * Enable and initialize the xsave feature.
  263. */
  264. void __ref xsave_cntxt_init(void)
  265. {
  266. unsigned int eax, ebx, ecx, edx;
  267. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  268. pcntxt_mask = eax + ((u64)edx << 32);
  269. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  270. printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
  271. pcntxt_mask);
  272. BUG();
  273. }
  274. /*
  275. * Support only the state known to OS.
  276. */
  277. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  278. xsave_init();
  279. /*
  280. * Recompute the context size for enabled features
  281. */
  282. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  283. xstate_size = ebx;
  284. prepare_fx_sw_frame();
  285. setup_xstate_init();
  286. printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
  287. "cntxt size 0x%x\n",
  288. pcntxt_mask, xstate_size);
  289. }