init.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/sched/task.h>
  10. #include <linux/init.h>
  11. /*
  12. * Initialize the registers found in all CPUs, CR0 and CR4:
  13. */
  14. static void fpu__init_cpu_generic(void)
  15. {
  16. unsigned long cr0;
  17. unsigned long cr4_mask = 0;
  18. if (boot_cpu_has(X86_FEATURE_FXSR))
  19. cr4_mask |= X86_CR4_OSFXSR;
  20. if (boot_cpu_has(X86_FEATURE_XMM))
  21. cr4_mask |= X86_CR4_OSXMMEXCPT;
  22. if (cr4_mask)
  23. cr4_set_bits(cr4_mask);
  24. cr0 = read_cr0();
  25. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  26. if (!boot_cpu_has(X86_FEATURE_FPU))
  27. cr0 |= X86_CR0_EM;
  28. write_cr0(cr0);
  29. /* Flush out any pending x87 state: */
  30. #ifdef CONFIG_MATH_EMULATION
  31. if (!boot_cpu_has(X86_FEATURE_FPU))
  32. fpstate_init_soft(&current->thread.fpu.state.soft);
  33. else
  34. #endif
  35. asm volatile ("fninit");
  36. }
  37. /*
  38. * Enable all supported FPU features. Called when a CPU is brought online:
  39. */
  40. void fpu__init_cpu(void)
  41. {
  42. fpu__init_cpu_generic();
  43. fpu__init_cpu_xstate();
  44. }
  45. static bool fpu__probe_without_cpuid(void)
  46. {
  47. unsigned long cr0;
  48. u16 fsw, fcw;
  49. fsw = fcw = 0xffff;
  50. cr0 = read_cr0();
  51. cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
  52. write_cr0(cr0);
  53. asm volatile("fninit ; fnstsw %0 ; fnstcw %1" : "+m" (fsw), "+m" (fcw));
  54. pr_info("x86/fpu: Probing for FPU: FSW=0x%04hx FCW=0x%04hx\n", fsw, fcw);
  55. return fsw == 0 && (fcw & 0x103f) == 0x003f;
  56. }
  57. static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
  58. {
  59. if (!boot_cpu_has(X86_FEATURE_CPUID) &&
  60. !test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
  61. if (fpu__probe_without_cpuid())
  62. setup_force_cpu_cap(X86_FEATURE_FPU);
  63. else
  64. setup_clear_cpu_cap(X86_FEATURE_FPU);
  65. }
  66. #ifndef CONFIG_MATH_EMULATION
  67. if (!test_cpu_cap(&boot_cpu_data, X86_FEATURE_FPU)) {
  68. pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
  69. for (;;)
  70. asm volatile("hlt");
  71. }
  72. #endif
  73. }
  74. /*
  75. * Boot time FPU feature detection code:
  76. */
  77. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  78. EXPORT_SYMBOL_GPL(mxcsr_feature_mask);
  79. static void __init fpu__init_system_mxcsr(void)
  80. {
  81. unsigned int mask = 0;
  82. if (boot_cpu_has(X86_FEATURE_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(&init_fpstate);
  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 fpu_kernel_xstate_size;
  116. EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size);
  117. /* Get alignment of the TYPE. */
  118. #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
  119. /*
  120. * Enforce that 'MEMBER' is the last field of 'TYPE'.
  121. *
  122. * Align the computed size with alignment of the TYPE,
  123. * because that's how C aligns structs.
  124. */
  125. #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
  126. BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
  127. TYPE_ALIGN(TYPE)))
  128. /*
  129. * We append the 'struct fpu' to the task_struct:
  130. */
  131. static void __init fpu__init_task_struct_size(void)
  132. {
  133. int task_size = sizeof(struct task_struct);
  134. /*
  135. * Subtract off the static size of the register state.
  136. * It potentially has a bunch of padding.
  137. */
  138. task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
  139. /*
  140. * Add back the dynamically-calculated register state
  141. * size.
  142. */
  143. task_size += fpu_kernel_xstate_size;
  144. /*
  145. * We dynamically size 'struct fpu', so we require that
  146. * it be at the end of 'thread_struct' and that
  147. * 'thread_struct' be at the end of 'task_struct'. If
  148. * you hit a compile error here, check the structure to
  149. * see if something got added to the end.
  150. */
  151. CHECK_MEMBER_AT_END_OF(struct fpu, state);
  152. CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
  153. CHECK_MEMBER_AT_END_OF(struct task_struct, thread);
  154. arch_task_struct_size = task_size;
  155. }
  156. /*
  157. * Set up the user and kernel xstate sizes based on the legacy FPU context size.
  158. *
  159. * We set this up first, and later it will be overwritten by
  160. * fpu__init_system_xstate() if the CPU knows about xstates.
  161. */
  162. static void __init fpu__init_system_xstate_size_legacy(void)
  163. {
  164. static int on_boot_cpu __initdata = 1;
  165. WARN_ON_FPU(!on_boot_cpu);
  166. on_boot_cpu = 0;
  167. /*
  168. * Note that xstate sizes might be overwritten later during
  169. * fpu__init_system_xstate().
  170. */
  171. if (!boot_cpu_has(X86_FEATURE_FPU)) {
  172. /*
  173. * Disable xsave as we do not support it if i387
  174. * emulation is enabled.
  175. */
  176. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  177. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  178. fpu_kernel_xstate_size = sizeof(struct swregs_state);
  179. } else {
  180. if (boot_cpu_has(X86_FEATURE_FXSR))
  181. fpu_kernel_xstate_size =
  182. sizeof(struct fxregs_state);
  183. else
  184. fpu_kernel_xstate_size =
  185. sizeof(struct fregs_state);
  186. }
  187. fpu_user_xstate_size = fpu_kernel_xstate_size;
  188. }
  189. /*
  190. * Find supported xfeatures based on cpu features and command-line input.
  191. * This must be called after fpu__init_parse_early_param() is called and
  192. * xfeatures_mask is enumerated.
  193. */
  194. u64 __init fpu__get_supported_xfeatures_mask(void)
  195. {
  196. return XCNTXT_MASK;
  197. }
  198. /* Legacy code to initialize eager fpu mode. */
  199. static void __init fpu__init_system_ctx_switch(void)
  200. {
  201. static bool on_boot_cpu __initdata = 1;
  202. WARN_ON_FPU(!on_boot_cpu);
  203. on_boot_cpu = 0;
  204. WARN_ON_FPU(current->thread.fpu.initialized);
  205. }
  206. /*
  207. * We parse fpu parameters early because fpu__init_system() is executed
  208. * before parse_early_param().
  209. */
  210. static void __init fpu__init_parse_early_param(void)
  211. {
  212. char arg[32];
  213. char *argptr = arg;
  214. int bit;
  215. if (cmdline_find_option_bool(boot_command_line, "no387"))
  216. setup_clear_cpu_cap(X86_FEATURE_FPU);
  217. if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
  218. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  219. setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
  220. setup_clear_cpu_cap(X86_FEATURE_XMM);
  221. }
  222. if (cmdline_find_option_bool(boot_command_line, "noxsave"))
  223. fpu__xstate_clear_all_cpu_caps();
  224. if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
  225. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  226. if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
  227. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  228. if (cmdline_find_option(boot_command_line, "clearcpuid", arg,
  229. sizeof(arg)) &&
  230. get_option(&argptr, &bit) &&
  231. bit >= 0 &&
  232. bit < NCAPINTS * 32)
  233. setup_clear_cpu_cap(bit);
  234. }
  235. /*
  236. * Called on the boot CPU once per system bootup, to set up the initial
  237. * FPU state that is later cloned into all processes:
  238. */
  239. void __init fpu__init_system(struct cpuinfo_x86 *c)
  240. {
  241. fpu__init_parse_early_param();
  242. fpu__init_system_early_generic(c);
  243. /*
  244. * The FPU has to be operational for some of the
  245. * later FPU init activities:
  246. */
  247. fpu__init_cpu();
  248. fpu__init_system_generic();
  249. fpu__init_system_xstate_size_legacy();
  250. fpu__init_system_xstate();
  251. fpu__init_task_struct_size();
  252. fpu__init_system_ctx_switch();
  253. }