init.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * x86 FPU boot time init code
  3. */
  4. #include <asm/fpu/internal.h>
  5. #include <asm/tlbflush.h>
  6. /*
  7. * Boot time CPU/FPU FDIV bug detection code:
  8. */
  9. static double __initdata x = 4195835.0;
  10. static double __initdata y = 3145727.0;
  11. /*
  12. * This used to check for exceptions..
  13. * However, it turns out that to support that,
  14. * the XMM trap handlers basically had to
  15. * be buggy. So let's have a correct XMM trap
  16. * handler, and forget about printing out
  17. * some status at boot.
  18. *
  19. * We should really only care about bugs here
  20. * anyway. Not features.
  21. */
  22. static void __init check_fpu(void)
  23. {
  24. s32 fdiv_bug;
  25. kernel_fpu_begin();
  26. /*
  27. * trap_init() enabled FXSR and company _before_ testing for FP
  28. * problems here.
  29. *
  30. * Test for the divl bug: http://en.wikipedia.org/wiki/Fdiv_bug
  31. */
  32. __asm__("fninit\n\t"
  33. "fldl %1\n\t"
  34. "fdivl %2\n\t"
  35. "fmull %2\n\t"
  36. "fldl %1\n\t"
  37. "fsubp %%st,%%st(1)\n\t"
  38. "fistpl %0\n\t"
  39. "fwait\n\t"
  40. "fninit"
  41. : "=m" (*&fdiv_bug)
  42. : "m" (*&x), "m" (*&y));
  43. kernel_fpu_end();
  44. if (fdiv_bug) {
  45. set_cpu_bug(&boot_cpu_data, X86_BUG_FDIV);
  46. pr_warn("Hmm, FPU with FDIV bug\n");
  47. }
  48. }
  49. void fpu__init_check_bugs(void)
  50. {
  51. /*
  52. * kernel_fpu_begin/end() in check_fpu() relies on the patched
  53. * alternative instructions.
  54. */
  55. if (cpu_has_fpu)
  56. check_fpu();
  57. }
  58. /*
  59. * The earliest FPU detection code:
  60. */
  61. static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
  62. {
  63. unsigned long cr0;
  64. u16 fsw, fcw;
  65. fsw = fcw = 0xffff;
  66. cr0 = read_cr0();
  67. cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
  68. write_cr0(cr0);
  69. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  70. : "+m" (fsw), "+m" (fcw));
  71. if (fsw == 0 && (fcw & 0x103f) == 0x003f)
  72. set_cpu_cap(c, X86_FEATURE_FPU);
  73. else
  74. clear_cpu_cap(c, X86_FEATURE_FPU);
  75. }
  76. /*
  77. * Boot time FPU feature detection code:
  78. */
  79. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  80. unsigned int xstate_size;
  81. EXPORT_SYMBOL_GPL(xstate_size);
  82. static void fpu__init_system_mxcsr(void)
  83. {
  84. unsigned int mask = 0;
  85. if (cpu_has_fxsr) {
  86. struct i387_fxsave_struct fx_tmp __aligned(32) = { };
  87. asm volatile("fxsave %0" : "+m" (fx_tmp));
  88. mask = fx_tmp.mxcsr_mask;
  89. /*
  90. * If zero then use the default features mask,
  91. * which has all features set, except the
  92. * denormals-are-zero feature bit:
  93. */
  94. if (mask == 0)
  95. mask = 0x0000ffbf;
  96. }
  97. mxcsr_feature_mask &= mask;
  98. }
  99. /*
  100. * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
  101. */
  102. static void fpu__init_system_generic(void)
  103. {
  104. /*
  105. * Set up the legacy init FPU context. (xstate init might overwrite this
  106. * with a more modern format, if the CPU supports it.)
  107. */
  108. fx_finit(&init_xstate_ctx.i387);
  109. fpu__init_system_mxcsr();
  110. }
  111. static void fpstate_xstate_init_size(void)
  112. {
  113. static bool on_boot_cpu = 1;
  114. if (!on_boot_cpu)
  115. return;
  116. on_boot_cpu = 0;
  117. /*
  118. * Note that xstate_size might be overwriten later during
  119. * fpu__init_system_xstate().
  120. */
  121. if (!cpu_has_fpu) {
  122. /*
  123. * Disable xsave as we do not support it if i387
  124. * emulation is enabled.
  125. */
  126. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  127. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  128. xstate_size = sizeof(struct i387_soft_struct);
  129. } else {
  130. if (cpu_has_fxsr)
  131. xstate_size = sizeof(struct i387_fxsave_struct);
  132. else
  133. xstate_size = sizeof(struct i387_fsave_struct);
  134. }
  135. }
  136. /*
  137. * Initialize the TS bit in CR0 according to the style of context-switches
  138. * we are using:
  139. */
  140. static void fpu__init_cpu_ctx_switch(void)
  141. {
  142. if (!cpu_has_eager_fpu)
  143. stts();
  144. else
  145. clts();
  146. }
  147. /*
  148. * Initialize the registers found in all CPUs, CR0 and CR4:
  149. */
  150. static void fpu__init_cpu_generic(void)
  151. {
  152. unsigned long cr0;
  153. unsigned long cr4_mask = 0;
  154. #ifndef CONFIG_MATH_EMULATION
  155. if (!cpu_has_fpu) {
  156. pr_emerg("No FPU found and no math emulation present\n");
  157. pr_emerg("Giving up\n");
  158. for (;;)
  159. asm volatile("hlt");
  160. }
  161. #endif
  162. if (cpu_has_fxsr)
  163. cr4_mask |= X86_CR4_OSFXSR;
  164. if (cpu_has_xmm)
  165. cr4_mask |= X86_CR4_OSXMMEXCPT;
  166. if (cr4_mask)
  167. cr4_set_bits(cr4_mask);
  168. cr0 = read_cr0();
  169. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  170. if (!cpu_has_fpu)
  171. cr0 |= X86_CR0_EM;
  172. write_cr0(cr0);
  173. }
  174. /*
  175. * Enable all supported FPU features. Called when a CPU is brought online.
  176. */
  177. void fpu__init_cpu(void)
  178. {
  179. fpu__init_cpu_generic();
  180. fpu__init_cpu_xstate();
  181. fpu__init_cpu_ctx_switch();
  182. }
  183. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  184. static int __init eager_fpu_setup(char *s)
  185. {
  186. if (!strcmp(s, "on"))
  187. eagerfpu = ENABLE;
  188. else if (!strcmp(s, "off"))
  189. eagerfpu = DISABLE;
  190. else if (!strcmp(s, "auto"))
  191. eagerfpu = AUTO;
  192. return 1;
  193. }
  194. __setup("eagerfpu=", eager_fpu_setup);
  195. /*
  196. * setup_init_fpu_buf() is __init and it is OK to call it here because
  197. * init_xstate_ctx will be unset only once during boot.
  198. */
  199. static void fpu__init_system_ctx_switch(void)
  200. {
  201. WARN_ON(current->thread.fpu.fpstate_active);
  202. current_thread_info()->status = 0;
  203. /* Auto enable eagerfpu for xsaveopt */
  204. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  205. eagerfpu = ENABLE;
  206. if (xfeatures_mask & XSTATE_EAGER) {
  207. if (eagerfpu == DISABLE) {
  208. pr_err("x86/fpu: eagerfpu switching disabled, disabling the following xstate features: 0x%llx.\n",
  209. xfeatures_mask & XSTATE_EAGER);
  210. xfeatures_mask &= ~XSTATE_EAGER;
  211. } else {
  212. eagerfpu = ENABLE;
  213. }
  214. }
  215. if (eagerfpu == ENABLE)
  216. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  217. printk_once(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
  218. }
  219. /*
  220. * Called on the boot CPU once per system bootup, to set up the initial FPU state that
  221. * is later cloned into all processes.
  222. */
  223. void fpu__init_system(void)
  224. {
  225. /* The FPU has to be operational for some of the later FPU init activities: */
  226. fpu__init_cpu();
  227. /*
  228. * But don't leave CR0::TS set yet, as some of the FPU setup methods depend
  229. * on being able to execute FPU instructions that will fault on a set TS,
  230. * such as the FXSAVE in fpu__init_system_mxcsr().
  231. */
  232. clts();
  233. fpu__init_system_generic();
  234. fpstate_xstate_init_size();
  235. fpu__init_system_xstate();
  236. fpu__init_system_ctx_switch();
  237. }
  238. static int __init no_387(char *s)
  239. {
  240. setup_clear_cpu_cap(X86_FEATURE_FPU);
  241. return 1;
  242. }
  243. __setup("no387", no_387);
  244. /*
  245. * Set the X86_FEATURE_FPU CPU-capability bit based on
  246. * trying to execute an actual sequence of FPU instructions:
  247. */
  248. void fpu__detect(struct cpuinfo_x86 *c)
  249. {
  250. fpu__init_system_early_generic(c);
  251. fpu__init_system();
  252. /* The final cr0 value is set later, in fpu_init() */
  253. }