init.c 9.8 KB

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