xsave.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /*
  10. * Supported feature mask by the CPU and the kernel.
  11. */
  12. unsigned int pcntxt_hmask, pcntxt_lmask;
  13. /*
  14. * Represents init state for the supported extended state.
  15. */
  16. struct xsave_struct *init_xstate_buf;
  17. #ifdef CONFIG_X86_64
  18. unsigned int sig_xstate_size = sizeof(struct _fpstate);
  19. #endif
  20. /*
  21. * Enable the extended processor state save/restore feature
  22. */
  23. void __cpuinit xsave_init(void)
  24. {
  25. if (!cpu_has_xsave)
  26. return;
  27. set_in_cr4(X86_CR4_OSXSAVE);
  28. /*
  29. * Enable all the features that the HW is capable of
  30. * and the Linux kernel is aware of.
  31. *
  32. * xsetbv();
  33. */
  34. asm volatile(".byte 0x0f,0x01,0xd1" : : "c" (0),
  35. "a" (pcntxt_lmask), "d" (pcntxt_hmask));
  36. }
  37. /*
  38. * setup the xstate image representing the init state
  39. */
  40. void setup_xstate_init(void)
  41. {
  42. init_xstate_buf = alloc_bootmem(xstate_size);
  43. init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
  44. }
  45. /*
  46. * Enable and initialize the xsave feature.
  47. */
  48. void __init xsave_cntxt_init(void)
  49. {
  50. unsigned int eax, ebx, ecx, edx;
  51. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  52. pcntxt_lmask = eax;
  53. pcntxt_hmask = edx;
  54. if ((pcntxt_lmask & XSTATE_FPSSE) != XSTATE_FPSSE) {
  55. printk(KERN_ERR "FP/SSE not shown under xsave features %x\n",
  56. pcntxt_lmask);
  57. BUG();
  58. }
  59. /*
  60. * for now OS knows only about FP/SSE
  61. */
  62. pcntxt_lmask = pcntxt_lmask & XCNTXT_LMASK;
  63. pcntxt_hmask = pcntxt_hmask & XCNTXT_HMASK;
  64. xsave_init();
  65. /*
  66. * Recompute the context size for enabled features
  67. */
  68. cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
  69. xstate_size = ebx;
  70. setup_xstate_init();
  71. printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%Lx, "
  72. "cntxt size 0x%x\n",
  73. (pcntxt_lmask | ((u64) pcntxt_hmask << 32)), xstate_size);
  74. }