init.c 10 KB

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