init.c 11 KB

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