init.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. 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. if (!test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
  68. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  69. : "+m" (fsw), "+m" (fcw));
  70. if (fsw == 0 && (fcw & 0x103f) == 0x003f)
  71. set_cpu_cap(c, X86_FEATURE_FPU);
  72. else
  73. clear_cpu_cap(c, X86_FEATURE_FPU);
  74. }
  75. #ifndef CONFIG_MATH_EMULATION
  76. if (!cpu_has_fpu) {
  77. pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
  78. for (;;)
  79. asm volatile("hlt");
  80. }
  81. #endif
  82. }
  83. /*
  84. * Boot time FPU feature detection code:
  85. */
  86. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  87. EXPORT_SYMBOL_GPL(mxcsr_feature_mask);
  88. static void __init fpu__init_system_mxcsr(void)
  89. {
  90. unsigned int mask = 0;
  91. if (cpu_has_fxsr) {
  92. /* Static because GCC does not get 16-byte stack alignment right: */
  93. static struct fxregs_state fxregs __initdata;
  94. asm volatile("fxsave %0" : "+m" (fxregs));
  95. mask = fxregs.mxcsr_mask;
  96. /*
  97. * If zero then use the default features mask,
  98. * which has all features set, except the
  99. * denormals-are-zero feature bit:
  100. */
  101. if (mask == 0)
  102. mask = 0x0000ffbf;
  103. }
  104. mxcsr_feature_mask &= mask;
  105. }
  106. /*
  107. * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
  108. */
  109. static void __init fpu__init_system_generic(void)
  110. {
  111. /*
  112. * Set up the legacy init FPU context. (xstate init might overwrite this
  113. * with a more modern format, if the CPU supports it.)
  114. */
  115. fpstate_init(&init_fpstate);
  116. fpu__init_system_mxcsr();
  117. }
  118. /*
  119. * Size of the FPU context state. All tasks in the system use the
  120. * same context size, regardless of what portion they use.
  121. * This is inherent to the XSAVE architecture which puts all state
  122. * components into a single, continuous memory block:
  123. */
  124. unsigned int xstate_size;
  125. EXPORT_SYMBOL_GPL(xstate_size);
  126. /* Enforce that 'MEMBER' is the last field of 'TYPE': */
  127. #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
  128. BUILD_BUG_ON(sizeof(TYPE) != offsetofend(TYPE, MEMBER))
  129. /*
  130. * We append the 'struct fpu' to the task_struct:
  131. */
  132. static void __init fpu__init_task_struct_size(void)
  133. {
  134. int task_size = sizeof(struct task_struct);
  135. /*
  136. * Subtract off the static size of the register state.
  137. * It potentially has a bunch of padding.
  138. */
  139. task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
  140. /*
  141. * Add back the dynamically-calculated register state
  142. * size.
  143. */
  144. task_size += xstate_size;
  145. /*
  146. * We dynamically size 'struct fpu', so we require that
  147. * it be at the end of 'thread_struct' and that
  148. * 'thread_struct' be at the end of 'task_struct'. If
  149. * you hit a compile error here, check the structure to
  150. * see if something got added to the end.
  151. */
  152. CHECK_MEMBER_AT_END_OF(struct fpu, state);
  153. CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
  154. CHECK_MEMBER_AT_END_OF(struct task_struct, thread);
  155. arch_task_struct_size = task_size;
  156. }
  157. /*
  158. * Set up the xstate_size based on the legacy FPU context size.
  159. *
  160. * We set this up first, and later it will be overwritten by
  161. * fpu__init_system_xstate() if the CPU knows about xstates.
  162. */
  163. static void __init fpu__init_system_xstate_size_legacy(void)
  164. {
  165. static int on_boot_cpu = 1;
  166. WARN_ON_FPU(!on_boot_cpu);
  167. on_boot_cpu = 0;
  168. /*
  169. * Note that xstate_size might be overwriten later during
  170. * fpu__init_system_xstate().
  171. */
  172. if (!cpu_has_fpu) {
  173. /*
  174. * Disable xsave as we do not support it if i387
  175. * emulation is enabled.
  176. */
  177. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  178. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  179. xstate_size = sizeof(struct swregs_state);
  180. } else {
  181. if (cpu_has_fxsr)
  182. xstate_size = sizeof(struct fxregs_state);
  183. else
  184. xstate_size = sizeof(struct fregs_state);
  185. }
  186. /*
  187. * Quirk: we don't yet handle the XSAVES* instructions
  188. * correctly, as we don't correctly convert between
  189. * standard and compacted format when interfacing
  190. * with user-space - so disable it for now.
  191. *
  192. * The difference is small: with recent CPUs the
  193. * compacted format is only marginally smaller than
  194. * the standard FPU state format.
  195. *
  196. * ( This is easy to backport while we are fixing
  197. * XSAVES* support. )
  198. */
  199. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  200. }
  201. /*
  202. * Find supported xfeatures based on cpu features and command-line input.
  203. * This must be called after fpu__init_parse_early_param() is called and
  204. * xfeatures_mask is enumerated.
  205. */
  206. u64 __init fpu__get_supported_xfeatures_mask(void)
  207. {
  208. return XCNTXT_MASK;
  209. }
  210. /* Legacy code to initialize eager fpu mode. */
  211. static void __init fpu__init_system_ctx_switch(void)
  212. {
  213. static bool on_boot_cpu = 1;
  214. WARN_ON_FPU(!on_boot_cpu);
  215. on_boot_cpu = 0;
  216. WARN_ON_FPU(current->thread.fpu.fpstate_active);
  217. current_thread_info()->status = 0;
  218. }
  219. /*
  220. * We parse fpu parameters early because fpu__init_system() is executed
  221. * before parse_early_param().
  222. */
  223. static void __init fpu__init_parse_early_param(void)
  224. {
  225. if (cmdline_find_option_bool(boot_command_line, "no387"))
  226. setup_clear_cpu_cap(X86_FEATURE_FPU);
  227. if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
  228. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  229. setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
  230. setup_clear_cpu_cap(X86_FEATURE_XMM);
  231. }
  232. if (cmdline_find_option_bool(boot_command_line, "noxsave"))
  233. fpu__xstate_clear_all_cpu_caps();
  234. if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
  235. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  236. if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
  237. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  238. }
  239. /*
  240. * Called on the boot CPU once per system bootup, to set up the initial
  241. * FPU state that is later cloned into all processes:
  242. */
  243. void __init fpu__init_system(struct cpuinfo_x86 *c)
  244. {
  245. fpu__init_parse_early_param();
  246. fpu__init_system_early_generic(c);
  247. /*
  248. * The FPU has to be operational for some of the
  249. * later FPU init activities:
  250. */
  251. fpu__init_cpu();
  252. /*
  253. * But don't leave CR0::TS set yet, as some of the FPU setup
  254. * methods depend on being able to execute FPU instructions
  255. * that will fault on a set TS, such as the FXSAVE in
  256. * fpu__init_system_mxcsr().
  257. */
  258. clts();
  259. fpu__init_system_generic();
  260. fpu__init_system_xstate_size_legacy();
  261. fpu__init_system_xstate();
  262. fpu__init_task_struct_size();
  263. fpu__init_system_ctx_switch();
  264. }