xsave.c 7.2 KB

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