init.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 registers found in all CPUs, CR0 and CR4:
  12. */
  13. static void fpu__init_cpu_generic(void)
  14. {
  15. unsigned long cr0;
  16. unsigned long cr4_mask = 0;
  17. if (boot_cpu_has(X86_FEATURE_FXSR))
  18. cr4_mask |= X86_CR4_OSFXSR;
  19. if (boot_cpu_has(X86_FEATURE_XMM))
  20. cr4_mask |= X86_CR4_OSXMMEXCPT;
  21. if (cr4_mask)
  22. cr4_set_bits(cr4_mask);
  23. cr0 = read_cr0();
  24. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  25. if (!boot_cpu_has(X86_FEATURE_FPU))
  26. cr0 |= X86_CR0_EM;
  27. write_cr0(cr0);
  28. /* Flush out any pending x87 state: */
  29. #ifdef CONFIG_MATH_EMULATION
  30. if (!boot_cpu_has(X86_FEATURE_FPU))
  31. fpstate_init_soft(&current->thread.fpu.state.soft);
  32. else
  33. #endif
  34. asm volatile ("fninit");
  35. }
  36. /*
  37. * Enable all supported FPU features. Called when a CPU is brought online:
  38. */
  39. void fpu__init_cpu(void)
  40. {
  41. fpu__init_cpu_generic();
  42. fpu__init_cpu_xstate();
  43. }
  44. /*
  45. * The earliest FPU detection code.
  46. *
  47. * Set the X86_FEATURE_FPU CPU-capability bit based on
  48. * trying to execute an actual sequence of FPU instructions:
  49. */
  50. static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
  51. {
  52. unsigned long cr0;
  53. u16 fsw, fcw;
  54. fsw = fcw = 0xffff;
  55. cr0 = read_cr0();
  56. cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
  57. write_cr0(cr0);
  58. if (!test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
  59. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  60. : "+m" (fsw), "+m" (fcw));
  61. if (fsw == 0 && (fcw & 0x103f) == 0x003f)
  62. set_cpu_cap(c, X86_FEATURE_FPU);
  63. else
  64. clear_cpu_cap(c, X86_FEATURE_FPU);
  65. }
  66. #ifndef CONFIG_MATH_EMULATION
  67. if (!boot_cpu_has(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. static void __init fpu__init_system_mxcsr(void)
  79. {
  80. unsigned int mask = 0;
  81. if (boot_cpu_has(X86_FEATURE_FXSR)) {
  82. /* Static because GCC does not get 16-byte stack alignment right: */
  83. static struct fxregs_state fxregs __initdata;
  84. asm volatile("fxsave %0" : "+m" (fxregs));
  85. mask = fxregs.mxcsr_mask;
  86. /*
  87. * If zero then use the default features mask,
  88. * which has all features set, except the
  89. * denormals-are-zero feature bit:
  90. */
  91. if (mask == 0)
  92. mask = 0x0000ffbf;
  93. }
  94. mxcsr_feature_mask &= mask;
  95. }
  96. /*
  97. * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
  98. */
  99. static void __init fpu__init_system_generic(void)
  100. {
  101. /*
  102. * Set up the legacy init FPU context. (xstate init might overwrite this
  103. * with a more modern format, if the CPU supports it.)
  104. */
  105. fpstate_init(&init_fpstate);
  106. fpu__init_system_mxcsr();
  107. }
  108. /*
  109. * Size of the FPU context state. All tasks in the system use the
  110. * same context size, regardless of what portion they use.
  111. * This is inherent to the XSAVE architecture which puts all state
  112. * components into a single, continuous memory block:
  113. */
  114. unsigned int fpu_kernel_xstate_size;
  115. EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size);
  116. /* Get alignment of the TYPE. */
  117. #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
  118. /*
  119. * Enforce that 'MEMBER' is the last field of 'TYPE'.
  120. *
  121. * Align the computed size with alignment of the TYPE,
  122. * because that's how C aligns structs.
  123. */
  124. #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
  125. BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
  126. TYPE_ALIGN(TYPE)))
  127. /*
  128. * We append the 'struct fpu' to the task_struct:
  129. */
  130. static void __init fpu__init_task_struct_size(void)
  131. {
  132. int task_size = sizeof(struct task_struct);
  133. /*
  134. * Subtract off the static size of the register state.
  135. * It potentially has a bunch of padding.
  136. */
  137. task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
  138. /*
  139. * Add back the dynamically-calculated register state
  140. * size.
  141. */
  142. task_size += fpu_kernel_xstate_size;
  143. /*
  144. * We dynamically size 'struct fpu', so we require that
  145. * it be at the end of 'thread_struct' and that
  146. * 'thread_struct' be at the end of 'task_struct'. If
  147. * you hit a compile error here, check the structure to
  148. * see if something got added to the end.
  149. */
  150. CHECK_MEMBER_AT_END_OF(struct fpu, state);
  151. CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
  152. CHECK_MEMBER_AT_END_OF(struct task_struct, thread);
  153. arch_task_struct_size = task_size;
  154. }
  155. /*
  156. * Set up the user and kernel xstate sizes based on the legacy FPU context size.
  157. *
  158. * We set this up first, and later it will be overwritten by
  159. * fpu__init_system_xstate() if the CPU knows about xstates.
  160. */
  161. static void __init fpu__init_system_xstate_size_legacy(void)
  162. {
  163. static int on_boot_cpu __initdata = 1;
  164. WARN_ON_FPU(!on_boot_cpu);
  165. on_boot_cpu = 0;
  166. /*
  167. * Note that xstate sizes might be overwritten later during
  168. * fpu__init_system_xstate().
  169. */
  170. if (!boot_cpu_has(X86_FEATURE_FPU)) {
  171. /*
  172. * Disable xsave as we do not support it if i387
  173. * emulation is enabled.
  174. */
  175. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  176. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  177. fpu_kernel_xstate_size = sizeof(struct swregs_state);
  178. } else {
  179. if (boot_cpu_has(X86_FEATURE_FXSR))
  180. fpu_kernel_xstate_size =
  181. sizeof(struct fxregs_state);
  182. else
  183. fpu_kernel_xstate_size =
  184. sizeof(struct fregs_state);
  185. }
  186. fpu_user_xstate_size = fpu_kernel_xstate_size;
  187. }
  188. /*
  189. * Find supported xfeatures based on cpu features and command-line input.
  190. * This must be called after fpu__init_parse_early_param() is called and
  191. * xfeatures_mask is enumerated.
  192. */
  193. u64 __init fpu__get_supported_xfeatures_mask(void)
  194. {
  195. return XCNTXT_MASK;
  196. }
  197. /* Legacy code to initialize eager fpu mode. */
  198. static void __init fpu__init_system_ctx_switch(void)
  199. {
  200. static bool on_boot_cpu __initdata = 1;
  201. WARN_ON_FPU(!on_boot_cpu);
  202. on_boot_cpu = 0;
  203. WARN_ON_FPU(current->thread.fpu.fpstate_active);
  204. }
  205. /*
  206. * We parse fpu parameters early because fpu__init_system() is executed
  207. * before parse_early_param().
  208. */
  209. static void __init fpu__init_parse_early_param(void)
  210. {
  211. if (cmdline_find_option_bool(boot_command_line, "no387"))
  212. setup_clear_cpu_cap(X86_FEATURE_FPU);
  213. if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
  214. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  215. setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
  216. setup_clear_cpu_cap(X86_FEATURE_XMM);
  217. }
  218. if (cmdline_find_option_bool(boot_command_line, "noxsave"))
  219. fpu__xstate_clear_all_cpu_caps();
  220. if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
  221. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  222. if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
  223. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  224. }
  225. /*
  226. * Called on the boot CPU once per system bootup, to set up the initial
  227. * FPU state that is later cloned into all processes:
  228. */
  229. void __init fpu__init_system(struct cpuinfo_x86 *c)
  230. {
  231. fpu__init_parse_early_param();
  232. fpu__init_system_early_generic(c);
  233. /*
  234. * The FPU has to be operational for some of the
  235. * later FPU init activities:
  236. */
  237. fpu__init_cpu();
  238. fpu__init_system_generic();
  239. fpu__init_system_xstate_size_legacy();
  240. fpu__init_system_xstate();
  241. fpu__init_task_struct_size();
  242. fpu__init_system_ctx_switch();
  243. }