xsave.c 7.8 KB

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