init.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * x86 FPU boot time init code:
  3. */
  4. #include <asm/fpu/internal.h>
  5. #include <asm/tlbflush.h>
  6. /*
  7. * Initialize the TS bit in CR0 according to the style of context-switches
  8. * we are using:
  9. */
  10. static void fpu__init_cpu_ctx_switch(void)
  11. {
  12. if (!cpu_has_eager_fpu)
  13. stts();
  14. else
  15. clts();
  16. }
  17. /*
  18. * Initialize the registers found in all CPUs, CR0 and CR4:
  19. */
  20. static void fpu__init_cpu_generic(void)
  21. {
  22. unsigned long cr0;
  23. unsigned long cr4_mask = 0;
  24. if (cpu_has_fxsr)
  25. cr4_mask |= X86_CR4_OSFXSR;
  26. if (cpu_has_xmm)
  27. cr4_mask |= X86_CR4_OSXMMEXCPT;
  28. if (cr4_mask)
  29. cr4_set_bits(cr4_mask);
  30. cr0 = read_cr0();
  31. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  32. if (!cpu_has_fpu)
  33. cr0 |= X86_CR0_EM;
  34. write_cr0(cr0);
  35. /* Flush out any pending x87 state: */
  36. asm volatile ("fninit");
  37. }
  38. /*
  39. * Enable all supported FPU features. Called when a CPU is brought online:
  40. */
  41. void fpu__init_cpu(void)
  42. {
  43. fpu__init_cpu_generic();
  44. fpu__init_cpu_xstate();
  45. fpu__init_cpu_ctx_switch();
  46. }
  47. /*
  48. * The earliest FPU detection code.
  49. *
  50. * Set the X86_FEATURE_FPU CPU-capability bit based on
  51. * trying to execute an actual sequence of FPU instructions:
  52. */
  53. static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
  54. {
  55. unsigned long cr0;
  56. u16 fsw, fcw;
  57. fsw = fcw = 0xffff;
  58. cr0 = read_cr0();
  59. cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
  60. write_cr0(cr0);
  61. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  62. : "+m" (fsw), "+m" (fcw));
  63. if (fsw == 0 && (fcw & 0x103f) == 0x003f)
  64. set_cpu_cap(c, X86_FEATURE_FPU);
  65. else
  66. clear_cpu_cap(c, X86_FEATURE_FPU);
  67. #ifndef CONFIG_MATH_EMULATION
  68. if (!cpu_has_fpu) {
  69. pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
  70. for (;;)
  71. asm volatile("hlt");
  72. }
  73. #endif
  74. }
  75. /*
  76. * Boot time FPU feature detection code:
  77. */
  78. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  79. static void __init fpu__init_system_mxcsr(void)
  80. {
  81. unsigned int mask = 0;
  82. if (cpu_has_fxsr) {
  83. /* Static because GCC does not get 16-byte stack alignment right: */
  84. static struct fxregs_state fxregs __initdata;
  85. asm volatile("fxsave %0" : "+m" (fxregs));
  86. mask = fxregs.mxcsr_mask;
  87. /*
  88. * If zero then use the default features mask,
  89. * which has all features set, except the
  90. * denormals-are-zero feature bit:
  91. */
  92. if (mask == 0)
  93. mask = 0x0000ffbf;
  94. }
  95. mxcsr_feature_mask &= mask;
  96. }
  97. /*
  98. * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
  99. */
  100. static void __init fpu__init_system_generic(void)
  101. {
  102. /*
  103. * Set up the legacy init FPU context. (xstate init might overwrite this
  104. * with a more modern format, if the CPU supports it.)
  105. */
  106. fpstate_init_fxstate(&init_fpstate.fxsave);
  107. fpu__init_system_mxcsr();
  108. }
  109. /*
  110. * Size of the FPU context state. All tasks in the system use the
  111. * same context size, regardless of what portion they use.
  112. * This is inherent to the XSAVE architecture which puts all state
  113. * components into a single, continuous memory block:
  114. */
  115. unsigned int xstate_size;
  116. EXPORT_SYMBOL_GPL(xstate_size);
  117. /*
  118. * Set up the xstate_size based on the legacy FPU context size.
  119. *
  120. * We set this up first, and later it will be overwritten by
  121. * fpu__init_system_xstate() if the CPU knows about xstates.
  122. */
  123. static void __init fpu__init_system_xstate_size_legacy(void)
  124. {
  125. static int on_boot_cpu = 1;
  126. WARN_ON_FPU(!on_boot_cpu);
  127. on_boot_cpu = 0;
  128. /*
  129. * Note that xstate_size might be overwriten later during
  130. * fpu__init_system_xstate().
  131. */
  132. if (!cpu_has_fpu) {
  133. /*
  134. * Disable xsave as we do not support it if i387
  135. * emulation is enabled.
  136. */
  137. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  138. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  139. xstate_size = sizeof(struct swregs_state);
  140. } else {
  141. if (cpu_has_fxsr)
  142. xstate_size = sizeof(struct fxregs_state);
  143. else
  144. xstate_size = sizeof(struct fregs_state);
  145. }
  146. /*
  147. * Quirk: we don't yet handle the XSAVES* instructions
  148. * correctly, as we don't correctly convert between
  149. * standard and compacted format when interfacing
  150. * with user-space - so disable it for now.
  151. *
  152. * The difference is small: with recent CPUs the
  153. * compacted format is only marginally smaller than
  154. * the standard FPU state format.
  155. *
  156. * ( This is easy to backport while we are fixing
  157. * XSAVES* support. )
  158. */
  159. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  160. }
  161. /*
  162. * FPU context switching strategies:
  163. *
  164. * Against popular belief, we don't do lazy FPU saves, due to the
  165. * task migration complications it brings on SMP - we only do
  166. * lazy FPU restores.
  167. *
  168. * 'lazy' is the traditional strategy, which is based on setting
  169. * CR0::TS to 1 during context-switch (instead of doing a full
  170. * restore of the FPU state), which causes the first FPU instruction
  171. * after the context switch (whenever it is executed) to fault - at
  172. * which point we lazily restore the FPU state into FPU registers.
  173. *
  174. * Tasks are of course under no obligation to execute FPU instructions,
  175. * so it can easily happen that another context-switch occurs without
  176. * a single FPU instruction being executed. If we eventually switch
  177. * back to the original task (that still owns the FPU) then we have
  178. * not only saved the restores along the way, but we also have the
  179. * FPU ready to be used for the original task.
  180. *
  181. * 'eager' switching is used on modern CPUs, there we switch the FPU
  182. * state during every context switch, regardless of whether the task
  183. * has used FPU instructions in that time slice or not. This is done
  184. * because modern FPU context saving instructions are able to optimize
  185. * state saving and restoration in hardware: they can detect both
  186. * unused and untouched FPU state and optimize accordingly.
  187. *
  188. * [ Note that even in 'lazy' mode we might optimize context switches
  189. * to use 'eager' restores, if we detect that a task is using the FPU
  190. * frequently. See the fpu->counter logic in fpu/internal.h for that. ]
  191. */
  192. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  193. static int __init eager_fpu_setup(char *s)
  194. {
  195. if (!strcmp(s, "on"))
  196. eagerfpu = ENABLE;
  197. else if (!strcmp(s, "off"))
  198. eagerfpu = DISABLE;
  199. else if (!strcmp(s, "auto"))
  200. eagerfpu = AUTO;
  201. return 1;
  202. }
  203. __setup("eagerfpu=", eager_fpu_setup);
  204. /*
  205. * Pick the FPU context switching strategy:
  206. */
  207. static void __init fpu__init_system_ctx_switch(void)
  208. {
  209. static bool on_boot_cpu = 1;
  210. WARN_ON_FPU(!on_boot_cpu);
  211. on_boot_cpu = 0;
  212. WARN_ON_FPU(current->thread.fpu.fpstate_active);
  213. current_thread_info()->status = 0;
  214. /* Auto enable eagerfpu for xsaveopt */
  215. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  216. eagerfpu = ENABLE;
  217. if (xfeatures_mask & XSTATE_EAGER) {
  218. if (eagerfpu == DISABLE) {
  219. pr_err("x86/fpu: eagerfpu switching disabled, disabling the following xstate features: 0x%llx.\n",
  220. xfeatures_mask & XSTATE_EAGER);
  221. xfeatures_mask &= ~XSTATE_EAGER;
  222. } else {
  223. eagerfpu = ENABLE;
  224. }
  225. }
  226. if (eagerfpu == ENABLE)
  227. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  228. printk(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
  229. }
  230. /*
  231. * Called on the boot CPU once per system bootup, to set up the initial
  232. * FPU state that is later cloned into all processes:
  233. */
  234. void __init fpu__init_system(struct cpuinfo_x86 *c)
  235. {
  236. fpu__init_system_early_generic(c);
  237. /*
  238. * The FPU has to be operational for some of the
  239. * later FPU init activities:
  240. */
  241. fpu__init_cpu();
  242. /*
  243. * But don't leave CR0::TS set yet, as some of the FPU setup
  244. * methods depend on being able to execute FPU instructions
  245. * that will fault on a set TS, such as the FXSAVE in
  246. * fpu__init_system_mxcsr().
  247. */
  248. clts();
  249. fpu__init_system_generic();
  250. fpu__init_system_xstate_size_legacy();
  251. fpu__init_system_xstate();
  252. fpu__init_system_ctx_switch();
  253. }
  254. /*
  255. * Boot parameter to turn off FPU support and fall back to math-emu:
  256. */
  257. static int __init no_387(char *s)
  258. {
  259. setup_clear_cpu_cap(X86_FEATURE_FPU);
  260. return 1;
  261. }
  262. __setup("no387", no_387);
  263. /*
  264. * Disable all xstate CPU features:
  265. */
  266. static int __init x86_noxsave_setup(char *s)
  267. {
  268. if (strlen(s))
  269. return 0;
  270. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  271. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  272. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  273. setup_clear_cpu_cap(X86_FEATURE_AVX);
  274. setup_clear_cpu_cap(X86_FEATURE_AVX2);
  275. return 1;
  276. }
  277. __setup("noxsave", x86_noxsave_setup);
  278. /*
  279. * Disable the XSAVEOPT instruction specifically:
  280. */
  281. static int __init x86_noxsaveopt_setup(char *s)
  282. {
  283. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  284. return 1;
  285. }
  286. __setup("noxsaveopt", x86_noxsaveopt_setup);
  287. /*
  288. * Disable the XSAVES instruction:
  289. */
  290. static int __init x86_noxsaves_setup(char *s)
  291. {
  292. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  293. return 1;
  294. }
  295. __setup("noxsaves", x86_noxsaves_setup);
  296. /*
  297. * Disable FX save/restore and SSE support:
  298. */
  299. static int __init x86_nofxsr_setup(char *s)
  300. {
  301. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  302. setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
  303. setup_clear_cpu_cap(X86_FEATURE_XMM);
  304. return 1;
  305. }
  306. __setup("nofxsr", x86_nofxsr_setup);