xsave.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. clear_used_math(); /* trigger finit */
  78. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  79. /*
  80. * Start with clearing the user buffer. This will present a
  81. * clean context for the bytes not touched by the fxsave/xsave.
  82. */
  83. __clear_user(buf, sig_xstate_size);
  84. if (task_thread_info(tsk)->status & TS_XSAVE)
  85. err = xsave_user(buf);
  86. else
  87. err = fxsave_user(buf);
  88. if (err)
  89. return err;
  90. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  91. stts();
  92. } else {
  93. if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
  94. xstate_size))
  95. return -1;
  96. }
  97. if (task_thread_info(tsk)->status & TS_XSAVE) {
  98. struct _fpstate __user *fx = buf;
  99. err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
  100. sizeof(struct _fpx_sw_bytes));
  101. err |= __put_user(FP_XSTATE_MAGIC2,
  102. (__u32 __user *) (buf + sig_xstate_size
  103. - FP_XSTATE_MAGIC2_SIZE));
  104. }
  105. return 1;
  106. }
  107. /*
  108. * Restore the extended state if present. Otherwise, restore the FP/SSE
  109. * state.
  110. */
  111. int restore_user_xstate(void __user *buf)
  112. {
  113. struct _fpx_sw_bytes fx_sw_user;
  114. u64 mask;
  115. int err;
  116. if (((unsigned long)buf % 64) ||
  117. check_for_xstate(buf, buf, &fx_sw_user))
  118. goto fx_only;
  119. mask = fx_sw_user.xstate_bv;
  120. /*
  121. * restore the state passed by the user.
  122. */
  123. err = xrestore_user(buf, mask);
  124. if (err)
  125. return err;
  126. /*
  127. * init the state skipped by the user.
  128. */
  129. mask = pcntxt_mask & ~mask;
  130. xrstor_state(init_xstate_buf, mask);
  131. return 0;
  132. fx_only:
  133. /*
  134. * couldn't find the extended state information in the
  135. * memory layout. Restore just the FP/SSE and init all
  136. * the other extended state.
  137. */
  138. xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
  139. return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
  140. }
  141. /*
  142. * This restores directly out of user space. Exceptions are handled.
  143. */
  144. int restore_i387_xstate(void __user *buf)
  145. {
  146. struct task_struct *tsk = current;
  147. int err = 0;
  148. if (!buf) {
  149. if (used_math())
  150. goto clear;
  151. return 0;
  152. } else
  153. if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
  154. return -EACCES;
  155. if (!used_math()) {
  156. err = init_fpu(tsk);
  157. if (err)
  158. return err;
  159. }
  160. if (!(task_thread_info(current)->status & TS_USEDFPU)) {
  161. clts();
  162. task_thread_info(current)->status |= TS_USEDFPU;
  163. }
  164. if (task_thread_info(tsk)->status & TS_XSAVE)
  165. err = restore_user_xstate(buf);
  166. else
  167. err = fxrstor_checking((__force struct i387_fxsave_struct *)
  168. buf);
  169. if (unlikely(err)) {
  170. /*
  171. * Encountered an error while doing the restore from the
  172. * user buffer, clear the fpu state.
  173. */
  174. clear:
  175. clear_fpu(tsk);
  176. clear_used_math();
  177. }
  178. return err;
  179. }
  180. #endif
  181. /*
  182. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  183. * the presence of the extended state information in the memory layout
  184. * pointed by the fpstate pointer in the sigcontext.
  185. * This will be saved when ever the FP and extended state context is
  186. * saved on the user stack during the signal handler delivery to the user.
  187. */
  188. void prepare_fx_sw_frame(void)
  189. {
  190. int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
  191. FP_XSTATE_MAGIC2_SIZE;
  192. sig_xstate_size = sizeof(struct _fpstate) + size_extended;
  193. #ifdef CONFIG_IA32_EMULATION
  194. sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
  195. #endif
  196. memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
  197. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  198. fx_sw_reserved.extended_size = sig_xstate_size;
  199. fx_sw_reserved.xstate_bv = pcntxt_mask;
  200. fx_sw_reserved.xstate_size = xstate_size;
  201. #ifdef CONFIG_IA32_EMULATION
  202. memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
  203. sizeof(struct _fpx_sw_bytes));
  204. fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
  205. #endif
  206. }
  207. /*
  208. * Represents init state for the supported extended state.
  209. */
  210. struct xsave_struct *init_xstate_buf;
  211. #ifdef CONFIG_X86_64
  212. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  213. #endif
  214. /*
  215. * Enable the extended processor state save/restore feature
  216. */
  217. void __cpuinit xsave_init(void)
  218. {
  219. if (!cpu_has_xsave)
  220. return;
  221. set_in_cr4(X86_CR4_OSXSAVE);
  222. /*
  223. * Enable all the features that the HW is capable of
  224. * and the Linux kernel is aware of.
  225. */
  226. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  227. }
  228. /*
  229. * setup the xstate image representing the init state
  230. */
  231. void setup_xstate_init(void)
  232. {
  233. init_xstate_buf = alloc_bootmem(xstate_size);
  234. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  235. }
  236. /*
  237. * Enable and initialize the xsave feature.
  238. */
  239. void __init xsave_cntxt_init(void)
  240. {
  241. unsigned int eax, ebx, ecx, edx;
  242. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  243. pcntxt_mask = eax + ((u64)edx << 32);
  244. if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  245. printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
  246. pcntxt_mask);
  247. BUG();
  248. }
  249. /*
  250. * for now OS knows only about FP/SSE
  251. */
  252. pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
  253. xsave_init();
  254. /*
  255. * Recompute the context size for enabled features
  256. */
  257. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  258. xstate_size = ebx;
  259. prepare_fx_sw_frame();
  260. setup_xstate_init();
  261. printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
  262. "cntxt size 0x%x\n",
  263. pcntxt_mask, xstate_size);
  264. }