common.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. #include <linux/bootmem.h>
  2. #include <linux/linkage.h>
  3. #include <linux/bitops.h>
  4. #include <linux/kernel.h>
  5. #include <linux/export.h>
  6. #include <linux/percpu.h>
  7. #include <linux/string.h>
  8. #include <linux/ctype.h>
  9. #include <linux/delay.h>
  10. #include <linux/sched.h>
  11. #include <linux/init.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/kgdb.h>
  14. #include <linux/smp.h>
  15. #include <linux/io.h>
  16. #include <linux/syscore_ops.h>
  17. #include <asm/stackprotector.h>
  18. #include <asm/perf_event.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/archrandom.h>
  21. #include <asm/hypervisor.h>
  22. #include <asm/processor.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/debugreg.h>
  25. #include <asm/sections.h>
  26. #include <asm/vsyscall.h>
  27. #include <linux/topology.h>
  28. #include <linux/cpumask.h>
  29. #include <asm/pgtable.h>
  30. #include <linux/atomic.h>
  31. #include <asm/proto.h>
  32. #include <asm/setup.h>
  33. #include <asm/apic.h>
  34. #include <asm/desc.h>
  35. #include <asm/fpu/internal.h>
  36. #include <asm/mtrr.h>
  37. #include <linux/numa.h>
  38. #include <asm/asm.h>
  39. #include <asm/bugs.h>
  40. #include <asm/cpu.h>
  41. #include <asm/mce.h>
  42. #include <asm/msr.h>
  43. #include <asm/pat.h>
  44. #include <asm/microcode.h>
  45. #include <asm/microcode_intel.h>
  46. #include <asm/intel-family.h>
  47. #include <asm/cpu_device_id.h>
  48. #ifdef CONFIG_X86_LOCAL_APIC
  49. #include <asm/uv/uv.h>
  50. #endif
  51. #include "cpu.h"
  52. /* all of these masks are initialized in setup_cpu_local_masks() */
  53. cpumask_var_t cpu_initialized_mask;
  54. cpumask_var_t cpu_callout_mask;
  55. cpumask_var_t cpu_callin_mask;
  56. /* representing cpus for which sibling maps can be computed */
  57. cpumask_var_t cpu_sibling_setup_mask;
  58. /* correctly size the local cpu masks */
  59. void __init setup_cpu_local_masks(void)
  60. {
  61. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  62. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  63. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  64. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  65. }
  66. static void default_init(struct cpuinfo_x86 *c)
  67. {
  68. #ifdef CONFIG_X86_64
  69. cpu_detect_cache_sizes(c);
  70. #else
  71. /* Not much we can do here... */
  72. /* Check if at least it has cpuid */
  73. if (c->cpuid_level == -1) {
  74. /* No cpuid. It must be an ancient CPU */
  75. if (c->x86 == 4)
  76. strcpy(c->x86_model_id, "486");
  77. else if (c->x86 == 3)
  78. strcpy(c->x86_model_id, "386");
  79. }
  80. #endif
  81. }
  82. static const struct cpu_dev default_cpu = {
  83. .c_init = default_init,
  84. .c_vendor = "Unknown",
  85. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  86. };
  87. static const struct cpu_dev *this_cpu = &default_cpu;
  88. DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(struct gdt_page, gdt_page) = { .gdt = {
  89. #ifdef CONFIG_X86_64
  90. /*
  91. * We need valid kernel segments for data and code in long mode too
  92. * IRET will check the segment types kkeil 2000/10/28
  93. * Also sysret mandates a special GDT layout
  94. *
  95. * TLS descriptors are currently at a different place compared to i386.
  96. * Hopefully nobody expects them at a fixed place (Wine?)
  97. */
  98. [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
  99. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
  100. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
  101. [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
  102. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
  103. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
  104. #else
  105. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
  106. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  107. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
  108. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
  109. /*
  110. * Segments used for calling PnP BIOS have byte granularity.
  111. * They code segments and data segments have fixed 64k limits,
  112. * the transfer segment sizes are set at run time.
  113. */
  114. /* 32-bit code */
  115. [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
  116. /* 16-bit code */
  117. [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
  118. /* 16-bit data */
  119. [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
  120. /* 16-bit data */
  121. [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
  122. /* 16-bit data */
  123. [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
  124. /*
  125. * The APM segments have byte granularity and their bases
  126. * are set at run time. All have 64k limits.
  127. */
  128. /* 32-bit code */
  129. [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
  130. /* 16-bit code */
  131. [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
  132. /* data */
  133. [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
  134. [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  135. [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  136. GDT_STACK_CANARY_INIT
  137. #endif
  138. } };
  139. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  140. static int __init x86_mpx_setup(char *s)
  141. {
  142. /* require an exact match without trailing characters */
  143. if (strlen(s))
  144. return 0;
  145. /* do not emit a message if the feature is not present */
  146. if (!boot_cpu_has(X86_FEATURE_MPX))
  147. return 1;
  148. setup_clear_cpu_cap(X86_FEATURE_MPX);
  149. pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
  150. return 1;
  151. }
  152. __setup("nompx", x86_mpx_setup);
  153. #ifdef CONFIG_X86_64
  154. static int __init x86_pcid_setup(char *s)
  155. {
  156. /* require an exact match without trailing characters */
  157. if (strlen(s))
  158. return 0;
  159. /* do not emit a message if the feature is not present */
  160. if (!boot_cpu_has(X86_FEATURE_PCID))
  161. return 1;
  162. setup_clear_cpu_cap(X86_FEATURE_PCID);
  163. pr_info("nopcid: PCID feature disabled\n");
  164. return 1;
  165. }
  166. __setup("nopcid", x86_pcid_setup);
  167. #endif
  168. static int __init x86_noinvpcid_setup(char *s)
  169. {
  170. /* noinvpcid doesn't accept parameters */
  171. if (s)
  172. return -EINVAL;
  173. /* do not emit a message if the feature is not present */
  174. if (!boot_cpu_has(X86_FEATURE_INVPCID))
  175. return 0;
  176. setup_clear_cpu_cap(X86_FEATURE_INVPCID);
  177. pr_info("noinvpcid: INVPCID feature disabled\n");
  178. return 0;
  179. }
  180. early_param("noinvpcid", x86_noinvpcid_setup);
  181. #ifdef CONFIG_X86_32
  182. static int cachesize_override = -1;
  183. static int disable_x86_serial_nr = 1;
  184. static int __init cachesize_setup(char *str)
  185. {
  186. get_option(&str, &cachesize_override);
  187. return 1;
  188. }
  189. __setup("cachesize=", cachesize_setup);
  190. static int __init x86_sep_setup(char *s)
  191. {
  192. setup_clear_cpu_cap(X86_FEATURE_SEP);
  193. return 1;
  194. }
  195. __setup("nosep", x86_sep_setup);
  196. /* Standard macro to see if a specific flag is changeable */
  197. static inline int flag_is_changeable_p(u32 flag)
  198. {
  199. u32 f1, f2;
  200. /*
  201. * Cyrix and IDT cpus allow disabling of CPUID
  202. * so the code below may return different results
  203. * when it is executed before and after enabling
  204. * the CPUID. Add "volatile" to not allow gcc to
  205. * optimize the subsequent calls to this function.
  206. */
  207. asm volatile ("pushfl \n\t"
  208. "pushfl \n\t"
  209. "popl %0 \n\t"
  210. "movl %0, %1 \n\t"
  211. "xorl %2, %0 \n\t"
  212. "pushl %0 \n\t"
  213. "popfl \n\t"
  214. "pushfl \n\t"
  215. "popl %0 \n\t"
  216. "popfl \n\t"
  217. : "=&r" (f1), "=&r" (f2)
  218. : "ir" (flag));
  219. return ((f1^f2) & flag) != 0;
  220. }
  221. /* Probe for the CPUID instruction */
  222. int have_cpuid_p(void)
  223. {
  224. return flag_is_changeable_p(X86_EFLAGS_ID);
  225. }
  226. static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  227. {
  228. unsigned long lo, hi;
  229. if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
  230. return;
  231. /* Disable processor serial number: */
  232. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  233. lo |= 0x200000;
  234. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  235. pr_notice("CPU serial number disabled.\n");
  236. clear_cpu_cap(c, X86_FEATURE_PN);
  237. /* Disabling the serial number may affect the cpuid level */
  238. c->cpuid_level = cpuid_eax(0);
  239. }
  240. static int __init x86_serial_nr_setup(char *s)
  241. {
  242. disable_x86_serial_nr = 0;
  243. return 1;
  244. }
  245. __setup("serialnumber", x86_serial_nr_setup);
  246. #else
  247. static inline int flag_is_changeable_p(u32 flag)
  248. {
  249. return 1;
  250. }
  251. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  252. {
  253. }
  254. #endif
  255. static __init int setup_disable_smep(char *arg)
  256. {
  257. setup_clear_cpu_cap(X86_FEATURE_SMEP);
  258. /* Check for things that depend on SMEP being enabled: */
  259. check_mpx_erratum(&boot_cpu_data);
  260. return 1;
  261. }
  262. __setup("nosmep", setup_disable_smep);
  263. static __always_inline void setup_smep(struct cpuinfo_x86 *c)
  264. {
  265. if (cpu_has(c, X86_FEATURE_SMEP))
  266. cr4_set_bits(X86_CR4_SMEP);
  267. }
  268. static __init int setup_disable_smap(char *arg)
  269. {
  270. setup_clear_cpu_cap(X86_FEATURE_SMAP);
  271. return 1;
  272. }
  273. __setup("nosmap", setup_disable_smap);
  274. static __always_inline void setup_smap(struct cpuinfo_x86 *c)
  275. {
  276. unsigned long eflags = native_save_fl();
  277. /* This should have been cleared long ago */
  278. BUG_ON(eflags & X86_EFLAGS_AC);
  279. if (cpu_has(c, X86_FEATURE_SMAP)) {
  280. #ifdef CONFIG_X86_SMAP
  281. cr4_set_bits(X86_CR4_SMAP);
  282. #else
  283. cr4_clear_bits(X86_CR4_SMAP);
  284. #endif
  285. }
  286. }
  287. static void setup_pcid(struct cpuinfo_x86 *c)
  288. {
  289. if (cpu_has(c, X86_FEATURE_PCID)) {
  290. if (cpu_has(c, X86_FEATURE_PGE) || kaiser_enabled) {
  291. cr4_set_bits(X86_CR4_PCIDE);
  292. /*
  293. * INVPCID has two "groups" of types:
  294. * 1/2: Invalidate an individual address
  295. * 3/4: Invalidate all contexts
  296. *
  297. * 1/2 take a PCID, but 3/4 do not. So, 3/4
  298. * ignore the PCID argument in the descriptor.
  299. * But, we have to be careful not to call 1/2
  300. * with an actual non-zero PCID in them before
  301. * we do the above cr4_set_bits().
  302. */
  303. if (cpu_has(c, X86_FEATURE_INVPCID))
  304. set_cpu_cap(c, X86_FEATURE_INVPCID_SINGLE);
  305. } else {
  306. /*
  307. * flush_tlb_all(), as currently implemented, won't
  308. * work if PCID is on but PGE is not. Since that
  309. * combination doesn't exist on real hardware, there's
  310. * no reason to try to fully support it, but it's
  311. * polite to avoid corrupting data if we're on
  312. * an improperly configured VM.
  313. */
  314. clear_cpu_cap(c, X86_FEATURE_PCID);
  315. }
  316. }
  317. kaiser_setup_pcid();
  318. }
  319. /*
  320. * Protection Keys are not available in 32-bit mode.
  321. */
  322. static bool pku_disabled;
  323. static __always_inline void setup_pku(struct cpuinfo_x86 *c)
  324. {
  325. /* check the boot processor, plus compile options for PKU: */
  326. if (!cpu_feature_enabled(X86_FEATURE_PKU))
  327. return;
  328. /* checks the actual processor's cpuid bits: */
  329. if (!cpu_has(c, X86_FEATURE_PKU))
  330. return;
  331. if (pku_disabled)
  332. return;
  333. cr4_set_bits(X86_CR4_PKE);
  334. /*
  335. * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
  336. * cpuid bit to be set. We need to ensure that we
  337. * update that bit in this CPU's "cpu_info".
  338. */
  339. get_cpu_cap(c);
  340. }
  341. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  342. static __init int setup_disable_pku(char *arg)
  343. {
  344. /*
  345. * Do not clear the X86_FEATURE_PKU bit. All of the
  346. * runtime checks are against OSPKE so clearing the
  347. * bit does nothing.
  348. *
  349. * This way, we will see "pku" in cpuinfo, but not
  350. * "ospke", which is exactly what we want. It shows
  351. * that the CPU has PKU, but the OS has not enabled it.
  352. * This happens to be exactly how a system would look
  353. * if we disabled the config option.
  354. */
  355. pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
  356. pku_disabled = true;
  357. return 1;
  358. }
  359. __setup("nopku", setup_disable_pku);
  360. #endif /* CONFIG_X86_64 */
  361. /*
  362. * Some CPU features depend on higher CPUID levels, which may not always
  363. * be available due to CPUID level capping or broken virtualization
  364. * software. Add those features to this table to auto-disable them.
  365. */
  366. struct cpuid_dependent_feature {
  367. u32 feature;
  368. u32 level;
  369. };
  370. static const struct cpuid_dependent_feature
  371. cpuid_dependent_features[] = {
  372. { X86_FEATURE_MWAIT, 0x00000005 },
  373. { X86_FEATURE_DCA, 0x00000009 },
  374. { X86_FEATURE_XSAVE, 0x0000000d },
  375. { 0, 0 }
  376. };
  377. static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  378. {
  379. const struct cpuid_dependent_feature *df;
  380. for (df = cpuid_dependent_features; df->feature; df++) {
  381. if (!cpu_has(c, df->feature))
  382. continue;
  383. /*
  384. * Note: cpuid_level is set to -1 if unavailable, but
  385. * extended_extended_level is set to 0 if unavailable
  386. * and the legitimate extended levels are all negative
  387. * when signed; hence the weird messing around with
  388. * signs here...
  389. */
  390. if (!((s32)df->level < 0 ?
  391. (u32)df->level > (u32)c->extended_cpuid_level :
  392. (s32)df->level > (s32)c->cpuid_level))
  393. continue;
  394. clear_cpu_cap(c, df->feature);
  395. if (!warn)
  396. continue;
  397. pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
  398. x86_cap_flag(df->feature), df->level);
  399. }
  400. }
  401. /*
  402. * Naming convention should be: <Name> [(<Codename>)]
  403. * This table only is used unless init_<vendor>() below doesn't set it;
  404. * in particular, if CPUID levels 0x80000002..4 are supported, this
  405. * isn't used
  406. */
  407. /* Look up CPU names by table lookup. */
  408. static const char *table_lookup_model(struct cpuinfo_x86 *c)
  409. {
  410. #ifdef CONFIG_X86_32
  411. const struct legacy_cpu_model_info *info;
  412. if (c->x86_model >= 16)
  413. return NULL; /* Range check */
  414. if (!this_cpu)
  415. return NULL;
  416. info = this_cpu->legacy_models;
  417. while (info->family) {
  418. if (info->family == c->x86)
  419. return info->model_names[c->x86_model];
  420. info++;
  421. }
  422. #endif
  423. return NULL; /* Not found */
  424. }
  425. __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
  426. __u32 cpu_caps_set[NCAPINTS + NBUGINTS];
  427. void load_percpu_segment(int cpu)
  428. {
  429. #ifdef CONFIG_X86_32
  430. loadsegment(fs, __KERNEL_PERCPU);
  431. #else
  432. __loadsegment_simple(gs, 0);
  433. wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  434. #endif
  435. load_stack_canary_segment();
  436. }
  437. /*
  438. * Current gdt points %fs at the "master" per-cpu area: after this,
  439. * it's on the real one.
  440. */
  441. void switch_to_new_gdt(int cpu)
  442. {
  443. struct desc_ptr gdt_descr;
  444. gdt_descr.address = (long)get_cpu_gdt_table(cpu);
  445. gdt_descr.size = GDT_SIZE - 1;
  446. load_gdt(&gdt_descr);
  447. /* Reload the per-cpu base */
  448. load_percpu_segment(cpu);
  449. }
  450. static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  451. static void get_model_name(struct cpuinfo_x86 *c)
  452. {
  453. unsigned int *v;
  454. char *p, *q, *s;
  455. if (c->extended_cpuid_level < 0x80000004)
  456. return;
  457. v = (unsigned int *)c->x86_model_id;
  458. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  459. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  460. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  461. c->x86_model_id[48] = 0;
  462. /* Trim whitespace */
  463. p = q = s = &c->x86_model_id[0];
  464. while (*p == ' ')
  465. p++;
  466. while (*p) {
  467. /* Note the last non-whitespace index */
  468. if (!isspace(*p))
  469. s = q;
  470. *q++ = *p++;
  471. }
  472. *(s + 1) = '\0';
  473. }
  474. void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
  475. {
  476. unsigned int n, dummy, ebx, ecx, edx, l2size;
  477. n = c->extended_cpuid_level;
  478. if (n >= 0x80000005) {
  479. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  480. c->x86_cache_size = (ecx>>24) + (edx>>24);
  481. #ifdef CONFIG_X86_64
  482. /* On K8 L1 TLB is inclusive, so don't count it */
  483. c->x86_tlbsize = 0;
  484. #endif
  485. }
  486. if (n < 0x80000006) /* Some chips just has a large L1. */
  487. return;
  488. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  489. l2size = ecx >> 16;
  490. #ifdef CONFIG_X86_64
  491. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  492. #else
  493. /* do processor-specific cache resizing */
  494. if (this_cpu->legacy_cache_size)
  495. l2size = this_cpu->legacy_cache_size(c, l2size);
  496. /* Allow user to override all this if necessary. */
  497. if (cachesize_override != -1)
  498. l2size = cachesize_override;
  499. if (l2size == 0)
  500. return; /* Again, no L2 cache is possible */
  501. #endif
  502. c->x86_cache_size = l2size;
  503. }
  504. u16 __read_mostly tlb_lli_4k[NR_INFO];
  505. u16 __read_mostly tlb_lli_2m[NR_INFO];
  506. u16 __read_mostly tlb_lli_4m[NR_INFO];
  507. u16 __read_mostly tlb_lld_4k[NR_INFO];
  508. u16 __read_mostly tlb_lld_2m[NR_INFO];
  509. u16 __read_mostly tlb_lld_4m[NR_INFO];
  510. u16 __read_mostly tlb_lld_1g[NR_INFO];
  511. static void cpu_detect_tlb(struct cpuinfo_x86 *c)
  512. {
  513. if (this_cpu->c_detect_tlb)
  514. this_cpu->c_detect_tlb(c);
  515. pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
  516. tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
  517. tlb_lli_4m[ENTRIES]);
  518. pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
  519. tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
  520. tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
  521. }
  522. void detect_ht(struct cpuinfo_x86 *c)
  523. {
  524. #ifdef CONFIG_SMP
  525. u32 eax, ebx, ecx, edx;
  526. int index_msb, core_bits;
  527. static bool printed;
  528. if (!cpu_has(c, X86_FEATURE_HT))
  529. return;
  530. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  531. goto out;
  532. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  533. return;
  534. cpuid(1, &eax, &ebx, &ecx, &edx);
  535. smp_num_siblings = (ebx & 0xff0000) >> 16;
  536. if (smp_num_siblings == 1) {
  537. pr_info_once("CPU0: Hyper-Threading is disabled\n");
  538. goto out;
  539. }
  540. if (smp_num_siblings <= 1)
  541. goto out;
  542. index_msb = get_count_order(smp_num_siblings);
  543. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  544. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  545. index_msb = get_count_order(smp_num_siblings);
  546. core_bits = get_count_order(c->x86_max_cores);
  547. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  548. ((1 << core_bits) - 1);
  549. out:
  550. if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
  551. pr_info("CPU: Physical Processor ID: %d\n",
  552. c->phys_proc_id);
  553. pr_info("CPU: Processor Core ID: %d\n",
  554. c->cpu_core_id);
  555. printed = 1;
  556. }
  557. #endif
  558. }
  559. static void get_cpu_vendor(struct cpuinfo_x86 *c)
  560. {
  561. char *v = c->x86_vendor_id;
  562. int i;
  563. for (i = 0; i < X86_VENDOR_NUM; i++) {
  564. if (!cpu_devs[i])
  565. break;
  566. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  567. (cpu_devs[i]->c_ident[1] &&
  568. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  569. this_cpu = cpu_devs[i];
  570. c->x86_vendor = this_cpu->c_x86_vendor;
  571. return;
  572. }
  573. }
  574. pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
  575. "CPU: Your system may be unstable.\n", v);
  576. c->x86_vendor = X86_VENDOR_UNKNOWN;
  577. this_cpu = &default_cpu;
  578. }
  579. void cpu_detect(struct cpuinfo_x86 *c)
  580. {
  581. /* Get vendor name */
  582. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  583. (unsigned int *)&c->x86_vendor_id[0],
  584. (unsigned int *)&c->x86_vendor_id[8],
  585. (unsigned int *)&c->x86_vendor_id[4]);
  586. c->x86 = 4;
  587. /* Intel-defined flags: level 0x00000001 */
  588. if (c->cpuid_level >= 0x00000001) {
  589. u32 junk, tfms, cap0, misc;
  590. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  591. c->x86 = x86_family(tfms);
  592. c->x86_model = x86_model(tfms);
  593. c->x86_stepping = x86_stepping(tfms);
  594. if (cap0 & (1<<19)) {
  595. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  596. c->x86_cache_alignment = c->x86_clflush_size;
  597. }
  598. }
  599. }
  600. static void apply_forced_caps(struct cpuinfo_x86 *c)
  601. {
  602. int i;
  603. for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
  604. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  605. c->x86_capability[i] |= cpu_caps_set[i];
  606. }
  607. }
  608. static void init_speculation_control(struct cpuinfo_x86 *c)
  609. {
  610. /*
  611. * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
  612. * and they also have a different bit for STIBP support. Also,
  613. * a hypervisor might have set the individual AMD bits even on
  614. * Intel CPUs, for finer-grained selection of what's available.
  615. */
  616. if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
  617. set_cpu_cap(c, X86_FEATURE_IBRS);
  618. set_cpu_cap(c, X86_FEATURE_IBPB);
  619. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  620. }
  621. if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
  622. set_cpu_cap(c, X86_FEATURE_STIBP);
  623. if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
  624. cpu_has(c, X86_FEATURE_VIRT_SSBD))
  625. set_cpu_cap(c, X86_FEATURE_SSBD);
  626. if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
  627. set_cpu_cap(c, X86_FEATURE_IBRS);
  628. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  629. }
  630. if (cpu_has(c, X86_FEATURE_AMD_IBPB))
  631. set_cpu_cap(c, X86_FEATURE_IBPB);
  632. if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
  633. set_cpu_cap(c, X86_FEATURE_STIBP);
  634. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  635. }
  636. }
  637. void get_cpu_cap(struct cpuinfo_x86 *c)
  638. {
  639. u32 eax, ebx, ecx, edx;
  640. /* Intel-defined flags: level 0x00000001 */
  641. if (c->cpuid_level >= 0x00000001) {
  642. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  643. c->x86_capability[CPUID_1_ECX] = ecx;
  644. c->x86_capability[CPUID_1_EDX] = edx;
  645. }
  646. /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
  647. if (c->cpuid_level >= 0x00000006)
  648. c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
  649. /* Additional Intel-defined flags: level 0x00000007 */
  650. if (c->cpuid_level >= 0x00000007) {
  651. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  652. c->x86_capability[CPUID_7_0_EBX] = ebx;
  653. c->x86_capability[CPUID_7_ECX] = ecx;
  654. c->x86_capability[CPUID_7_EDX] = edx;
  655. }
  656. /* Extended state features: level 0x0000000d */
  657. if (c->cpuid_level >= 0x0000000d) {
  658. cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
  659. c->x86_capability[CPUID_D_1_EAX] = eax;
  660. }
  661. /* Additional Intel-defined flags: level 0x0000000F */
  662. if (c->cpuid_level >= 0x0000000F) {
  663. /* QoS sub-leaf, EAX=0Fh, ECX=0 */
  664. cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
  665. c->x86_capability[CPUID_F_0_EDX] = edx;
  666. if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
  667. /* will be overridden if occupancy monitoring exists */
  668. c->x86_cache_max_rmid = ebx;
  669. /* QoS sub-leaf, EAX=0Fh, ECX=1 */
  670. cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
  671. c->x86_capability[CPUID_F_1_EDX] = edx;
  672. if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
  673. ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
  674. (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
  675. c->x86_cache_max_rmid = ecx;
  676. c->x86_cache_occ_scale = ebx;
  677. }
  678. } else {
  679. c->x86_cache_max_rmid = -1;
  680. c->x86_cache_occ_scale = -1;
  681. }
  682. }
  683. /* AMD-defined flags: level 0x80000001 */
  684. eax = cpuid_eax(0x80000000);
  685. c->extended_cpuid_level = eax;
  686. if ((eax & 0xffff0000) == 0x80000000) {
  687. if (eax >= 0x80000001) {
  688. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  689. c->x86_capability[CPUID_8000_0001_ECX] = ecx;
  690. c->x86_capability[CPUID_8000_0001_EDX] = edx;
  691. }
  692. }
  693. if (c->extended_cpuid_level >= 0x80000007) {
  694. cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  695. c->x86_capability[CPUID_8000_0007_EBX] = ebx;
  696. c->x86_power = edx;
  697. }
  698. if (c->extended_cpuid_level >= 0x80000008) {
  699. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  700. c->x86_virt_bits = (eax >> 8) & 0xff;
  701. c->x86_phys_bits = eax & 0xff;
  702. c->x86_capability[CPUID_8000_0008_EBX] = ebx;
  703. }
  704. #ifdef CONFIG_X86_32
  705. else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  706. c->x86_phys_bits = 36;
  707. #endif
  708. if (c->extended_cpuid_level >= 0x8000000a)
  709. c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
  710. init_scattered_cpuid_features(c);
  711. init_speculation_control(c);
  712. /*
  713. * Clear/Set all flags overridden by options, after probe.
  714. * This needs to happen each time we re-probe, which may happen
  715. * several times during CPU initialization.
  716. */
  717. apply_forced_caps(c);
  718. }
  719. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  720. {
  721. #ifdef CONFIG_X86_32
  722. int i;
  723. /*
  724. * First of all, decide if this is a 486 or higher
  725. * It's a 486 if we can modify the AC flag
  726. */
  727. if (flag_is_changeable_p(X86_EFLAGS_AC))
  728. c->x86 = 4;
  729. else
  730. c->x86 = 3;
  731. for (i = 0; i < X86_VENDOR_NUM; i++)
  732. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  733. c->x86_vendor_id[0] = 0;
  734. cpu_devs[i]->c_identify(c);
  735. if (c->x86_vendor_id[0]) {
  736. get_cpu_vendor(c);
  737. break;
  738. }
  739. }
  740. #endif
  741. }
  742. static const __initconst struct x86_cpu_id cpu_no_speculation[] = {
  743. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CEDARVIEW, X86_FEATURE_ANY },
  744. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CLOVERVIEW, X86_FEATURE_ANY },
  745. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_LINCROFT, X86_FEATURE_ANY },
  746. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PENWELL, X86_FEATURE_ANY },
  747. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PINEVIEW, X86_FEATURE_ANY },
  748. { X86_VENDOR_CENTAUR, 5 },
  749. { X86_VENDOR_INTEL, 5 },
  750. { X86_VENDOR_NSC, 5 },
  751. { X86_VENDOR_ANY, 4 },
  752. {}
  753. };
  754. static const __initconst struct x86_cpu_id cpu_no_meltdown[] = {
  755. { X86_VENDOR_AMD },
  756. {}
  757. };
  758. static const __initconst struct x86_cpu_id cpu_no_spec_store_bypass[] = {
  759. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PINEVIEW },
  760. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_LINCROFT },
  761. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PENWELL },
  762. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CLOVERVIEW },
  763. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CEDARVIEW },
  764. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
  765. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
  766. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT2 },
  767. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_MERRIFIELD },
  768. { X86_VENDOR_INTEL, 6, INTEL_FAM6_CORE_YONAH },
  769. { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNL },
  770. { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNM },
  771. { X86_VENDOR_CENTAUR, 5, },
  772. { X86_VENDOR_INTEL, 5, },
  773. { X86_VENDOR_NSC, 5, },
  774. { X86_VENDOR_AMD, 0x12, },
  775. { X86_VENDOR_AMD, 0x11, },
  776. { X86_VENDOR_AMD, 0x10, },
  777. { X86_VENDOR_AMD, 0xf, },
  778. { X86_VENDOR_ANY, 4, },
  779. {}
  780. };
  781. static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
  782. {
  783. u64 ia32_cap = 0;
  784. if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
  785. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
  786. if (!x86_match_cpu(cpu_no_spec_store_bypass) &&
  787. !(ia32_cap & ARCH_CAP_SSB_NO))
  788. setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
  789. if (x86_match_cpu(cpu_no_speculation))
  790. return;
  791. setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
  792. setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
  793. if (x86_match_cpu(cpu_no_meltdown))
  794. return;
  795. /* Rogue Data Cache Load? No! */
  796. if (ia32_cap & ARCH_CAP_RDCL_NO)
  797. return;
  798. setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
  799. }
  800. /*
  801. * Do minimum CPU detection early.
  802. * Fields really needed: vendor, cpuid_level, family, model, mask,
  803. * cache alignment.
  804. * The others are not touched to avoid unwanted side effects.
  805. *
  806. * WARNING: this function is only called on the BP. Don't add code here
  807. * that is supposed to run on all CPUs.
  808. */
  809. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  810. {
  811. #ifdef CONFIG_X86_64
  812. c->x86_clflush_size = 64;
  813. c->x86_phys_bits = 36;
  814. c->x86_virt_bits = 48;
  815. #else
  816. c->x86_clflush_size = 32;
  817. c->x86_phys_bits = 32;
  818. c->x86_virt_bits = 32;
  819. #endif
  820. c->x86_cache_alignment = c->x86_clflush_size;
  821. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  822. c->extended_cpuid_level = 0;
  823. if (!have_cpuid_p())
  824. identify_cpu_without_cpuid(c);
  825. /* cyrix could have cpuid enabled via c_identify()*/
  826. if (have_cpuid_p()) {
  827. cpu_detect(c);
  828. get_cpu_vendor(c);
  829. get_cpu_cap(c);
  830. if (this_cpu->c_early_init)
  831. this_cpu->c_early_init(c);
  832. c->cpu_index = 0;
  833. filter_cpuid_features(c, false);
  834. if (this_cpu->c_bsp_init)
  835. this_cpu->c_bsp_init(c);
  836. }
  837. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  838. cpu_set_bug_bits(c);
  839. fpu__init_system(c);
  840. #ifdef CONFIG_X86_32
  841. /*
  842. * Regardless of whether PCID is enumerated, the SDM says
  843. * that it can't be enabled in 32-bit mode.
  844. */
  845. setup_clear_cpu_cap(X86_FEATURE_PCID);
  846. #endif
  847. }
  848. void __init early_cpu_init(void)
  849. {
  850. const struct cpu_dev *const *cdev;
  851. int count = 0;
  852. #ifdef CONFIG_PROCESSOR_SELECT
  853. pr_info("KERNEL supported cpus:\n");
  854. #endif
  855. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  856. const struct cpu_dev *cpudev = *cdev;
  857. if (count >= X86_VENDOR_NUM)
  858. break;
  859. cpu_devs[count] = cpudev;
  860. count++;
  861. #ifdef CONFIG_PROCESSOR_SELECT
  862. {
  863. unsigned int j;
  864. for (j = 0; j < 2; j++) {
  865. if (!cpudev->c_ident[j])
  866. continue;
  867. pr_info(" %s %s\n", cpudev->c_vendor,
  868. cpudev->c_ident[j]);
  869. }
  870. }
  871. #endif
  872. }
  873. early_identify_cpu(&boot_cpu_data);
  874. }
  875. /*
  876. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  877. * unfortunately, that's not true in practice because of early VIA
  878. * chips and (more importantly) broken virtualizers that are not easy
  879. * to detect. In the latter case it doesn't even *fail* reliably, so
  880. * probing for it doesn't even work. Disable it completely on 32-bit
  881. * unless we can find a reliable way to detect all the broken cases.
  882. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  883. */
  884. static void detect_nopl(struct cpuinfo_x86 *c)
  885. {
  886. #ifdef CONFIG_X86_32
  887. clear_cpu_cap(c, X86_FEATURE_NOPL);
  888. #else
  889. set_cpu_cap(c, X86_FEATURE_NOPL);
  890. #endif
  891. }
  892. static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
  893. {
  894. #ifdef CONFIG_X86_64
  895. /*
  896. * Empirically, writing zero to a segment selector on AMD does
  897. * not clear the base, whereas writing zero to a segment
  898. * selector on Intel does clear the base. Intel's behavior
  899. * allows slightly faster context switches in the common case
  900. * where GS is unused by the prev and next threads.
  901. *
  902. * Since neither vendor documents this anywhere that I can see,
  903. * detect it directly instead of hardcoding the choice by
  904. * vendor.
  905. *
  906. * I've designated AMD's behavior as the "bug" because it's
  907. * counterintuitive and less friendly.
  908. */
  909. unsigned long old_base, tmp;
  910. rdmsrl(MSR_FS_BASE, old_base);
  911. wrmsrl(MSR_FS_BASE, 1);
  912. loadsegment(fs, 0);
  913. rdmsrl(MSR_FS_BASE, tmp);
  914. if (tmp != 0)
  915. set_cpu_bug(c, X86_BUG_NULL_SEG);
  916. wrmsrl(MSR_FS_BASE, old_base);
  917. #endif
  918. }
  919. static void generic_identify(struct cpuinfo_x86 *c)
  920. {
  921. c->extended_cpuid_level = 0;
  922. if (!have_cpuid_p())
  923. identify_cpu_without_cpuid(c);
  924. /* cyrix could have cpuid enabled via c_identify()*/
  925. if (!have_cpuid_p())
  926. return;
  927. cpu_detect(c);
  928. get_cpu_vendor(c);
  929. get_cpu_cap(c);
  930. if (c->cpuid_level >= 0x00000001) {
  931. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  932. #ifdef CONFIG_X86_32
  933. # ifdef CONFIG_SMP
  934. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  935. # else
  936. c->apicid = c->initial_apicid;
  937. # endif
  938. #endif
  939. c->phys_proc_id = c->initial_apicid;
  940. }
  941. get_model_name(c); /* Default name */
  942. detect_nopl(c);
  943. detect_null_seg_behavior(c);
  944. /*
  945. * ESPFIX is a strange bug. All real CPUs have it. Paravirt
  946. * systems that run Linux at CPL > 0 may or may not have the
  947. * issue, but, even if they have the issue, there's absolutely
  948. * nothing we can do about it because we can't use the real IRET
  949. * instruction.
  950. *
  951. * NB: For the time being, only 32-bit kernels support
  952. * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
  953. * whether to apply espfix using paravirt hooks. If any
  954. * non-paravirt system ever shows up that does *not* have the
  955. * ESPFIX issue, we can change this.
  956. */
  957. #ifdef CONFIG_X86_32
  958. # ifdef CONFIG_PARAVIRT
  959. do {
  960. extern void native_iret(void);
  961. if (pv_cpu_ops.iret == native_iret)
  962. set_cpu_bug(c, X86_BUG_ESPFIX);
  963. } while (0);
  964. # else
  965. set_cpu_bug(c, X86_BUG_ESPFIX);
  966. # endif
  967. #endif
  968. }
  969. static void x86_init_cache_qos(struct cpuinfo_x86 *c)
  970. {
  971. /*
  972. * The heavy lifting of max_rmid and cache_occ_scale are handled
  973. * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
  974. * in case CQM bits really aren't there in this CPU.
  975. */
  976. if (c != &boot_cpu_data) {
  977. boot_cpu_data.x86_cache_max_rmid =
  978. min(boot_cpu_data.x86_cache_max_rmid,
  979. c->x86_cache_max_rmid);
  980. }
  981. }
  982. /*
  983. * Validate that ACPI/mptables have the same information about the
  984. * effective APIC id and update the package map.
  985. */
  986. static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
  987. {
  988. #ifdef CONFIG_SMP
  989. unsigned int apicid, cpu = smp_processor_id();
  990. apicid = apic->cpu_present_to_apicid(cpu);
  991. if (apicid != c->apicid) {
  992. pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
  993. cpu, apicid, c->initial_apicid);
  994. }
  995. BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
  996. #else
  997. c->logical_proc_id = 0;
  998. #endif
  999. }
  1000. /*
  1001. * This does the hard work of actually picking apart the CPU stuff...
  1002. */
  1003. static void identify_cpu(struct cpuinfo_x86 *c)
  1004. {
  1005. int i;
  1006. c->loops_per_jiffy = loops_per_jiffy;
  1007. c->x86_cache_size = 0;
  1008. c->x86_vendor = X86_VENDOR_UNKNOWN;
  1009. c->x86_model = c->x86_stepping = 0; /* So far unknown... */
  1010. c->x86_vendor_id[0] = '\0'; /* Unset */
  1011. c->x86_model_id[0] = '\0'; /* Unset */
  1012. c->x86_max_cores = 1;
  1013. c->x86_coreid_bits = 0;
  1014. c->cu_id = 0xff;
  1015. #ifdef CONFIG_X86_64
  1016. c->x86_clflush_size = 64;
  1017. c->x86_phys_bits = 36;
  1018. c->x86_virt_bits = 48;
  1019. #else
  1020. c->cpuid_level = -1; /* CPUID not detected */
  1021. c->x86_clflush_size = 32;
  1022. c->x86_phys_bits = 32;
  1023. c->x86_virt_bits = 32;
  1024. #endif
  1025. c->x86_cache_alignment = c->x86_clflush_size;
  1026. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  1027. generic_identify(c);
  1028. if (this_cpu->c_identify)
  1029. this_cpu->c_identify(c);
  1030. /* Clear/Set all flags overridden by options, after probe */
  1031. apply_forced_caps(c);
  1032. #ifdef CONFIG_X86_64
  1033. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  1034. #endif
  1035. /*
  1036. * Vendor-specific initialization. In this section we
  1037. * canonicalize the feature flags, meaning if there are
  1038. * features a certain CPU supports which CPUID doesn't
  1039. * tell us, CPUID claiming incorrect flags, or other bugs,
  1040. * we handle them here.
  1041. *
  1042. * At the end of this section, c->x86_capability better
  1043. * indicate the features this CPU genuinely supports!
  1044. */
  1045. if (this_cpu->c_init)
  1046. this_cpu->c_init(c);
  1047. /* Disable the PN if appropriate */
  1048. squash_the_stupid_serial_number(c);
  1049. /* Set up SMEP/SMAP */
  1050. setup_smep(c);
  1051. setup_smap(c);
  1052. /* Set up PCID */
  1053. setup_pcid(c);
  1054. /*
  1055. * The vendor-specific functions might have changed features.
  1056. * Now we do "generic changes."
  1057. */
  1058. /* Filter out anything that depends on CPUID levels we don't have */
  1059. filter_cpuid_features(c, true);
  1060. /* If the model name is still unset, do table lookup. */
  1061. if (!c->x86_model_id[0]) {
  1062. const char *p;
  1063. p = table_lookup_model(c);
  1064. if (p)
  1065. strcpy(c->x86_model_id, p);
  1066. else
  1067. /* Last resort... */
  1068. sprintf(c->x86_model_id, "%02x/%02x",
  1069. c->x86, c->x86_model);
  1070. }
  1071. #ifdef CONFIG_X86_64
  1072. detect_ht(c);
  1073. #endif
  1074. init_hypervisor(c);
  1075. x86_init_rdrand(c);
  1076. x86_init_cache_qos(c);
  1077. setup_pku(c);
  1078. /*
  1079. * Clear/Set all flags overridden by options, need do it
  1080. * before following smp all cpus cap AND.
  1081. */
  1082. apply_forced_caps(c);
  1083. /*
  1084. * On SMP, boot_cpu_data holds the common feature set between
  1085. * all CPUs; so make sure that we indicate which features are
  1086. * common between the CPUs. The first time this routine gets
  1087. * executed, c == &boot_cpu_data.
  1088. */
  1089. if (c != &boot_cpu_data) {
  1090. /* AND the already accumulated flags with these */
  1091. for (i = 0; i < NCAPINTS; i++)
  1092. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  1093. /* OR, i.e. replicate the bug flags */
  1094. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  1095. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  1096. }
  1097. /* Init Machine Check Exception if available. */
  1098. mcheck_cpu_init(c);
  1099. select_idle_routine(c);
  1100. #ifdef CONFIG_NUMA
  1101. numa_add_cpu(smp_processor_id());
  1102. #endif
  1103. }
  1104. /*
  1105. * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
  1106. * on 32-bit kernels:
  1107. */
  1108. #ifdef CONFIG_X86_32
  1109. void enable_sep_cpu(void)
  1110. {
  1111. struct tss_struct *tss;
  1112. int cpu;
  1113. if (!boot_cpu_has(X86_FEATURE_SEP))
  1114. return;
  1115. cpu = get_cpu();
  1116. tss = &per_cpu(cpu_tss, cpu);
  1117. /*
  1118. * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
  1119. * see the big comment in struct x86_hw_tss's definition.
  1120. */
  1121. tss->x86_tss.ss1 = __KERNEL_CS;
  1122. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  1123. wrmsr(MSR_IA32_SYSENTER_ESP,
  1124. (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
  1125. 0);
  1126. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
  1127. put_cpu();
  1128. }
  1129. #endif
  1130. void __init identify_boot_cpu(void)
  1131. {
  1132. identify_cpu(&boot_cpu_data);
  1133. init_amd_e400_c1e_mask();
  1134. #ifdef CONFIG_X86_32
  1135. sysenter_setup();
  1136. enable_sep_cpu();
  1137. #endif
  1138. cpu_detect_tlb(&boot_cpu_data);
  1139. }
  1140. void identify_secondary_cpu(struct cpuinfo_x86 *c)
  1141. {
  1142. BUG_ON(c == &boot_cpu_data);
  1143. identify_cpu(c);
  1144. #ifdef CONFIG_X86_32
  1145. enable_sep_cpu();
  1146. #endif
  1147. mtrr_ap_init();
  1148. validate_apic_and_package_id(c);
  1149. x86_spec_ctrl_setup_ap();
  1150. }
  1151. struct msr_range {
  1152. unsigned min;
  1153. unsigned max;
  1154. };
  1155. static const struct msr_range msr_range_array[] = {
  1156. { 0x00000000, 0x00000418},
  1157. { 0xc0000000, 0xc000040b},
  1158. { 0xc0010000, 0xc0010142},
  1159. { 0xc0011000, 0xc001103b},
  1160. };
  1161. static void __print_cpu_msr(void)
  1162. {
  1163. unsigned index_min, index_max;
  1164. unsigned index;
  1165. u64 val;
  1166. int i;
  1167. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  1168. index_min = msr_range_array[i].min;
  1169. index_max = msr_range_array[i].max;
  1170. for (index = index_min; index < index_max; index++) {
  1171. if (rdmsrl_safe(index, &val))
  1172. continue;
  1173. pr_info(" MSR%08x: %016llx\n", index, val);
  1174. }
  1175. }
  1176. }
  1177. static int show_msr;
  1178. static __init int setup_show_msr(char *arg)
  1179. {
  1180. int num;
  1181. get_option(&arg, &num);
  1182. if (num > 0)
  1183. show_msr = num;
  1184. return 1;
  1185. }
  1186. __setup("show_msr=", setup_show_msr);
  1187. static __init int setup_noclflush(char *arg)
  1188. {
  1189. setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
  1190. setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
  1191. return 1;
  1192. }
  1193. __setup("noclflush", setup_noclflush);
  1194. void print_cpu_info(struct cpuinfo_x86 *c)
  1195. {
  1196. const char *vendor = NULL;
  1197. if (c->x86_vendor < X86_VENDOR_NUM) {
  1198. vendor = this_cpu->c_vendor;
  1199. } else {
  1200. if (c->cpuid_level >= 0)
  1201. vendor = c->x86_vendor_id;
  1202. }
  1203. if (vendor && !strstr(c->x86_model_id, vendor))
  1204. pr_cont("%s ", vendor);
  1205. if (c->x86_model_id[0])
  1206. pr_cont("%s", c->x86_model_id);
  1207. else
  1208. pr_cont("%d86", c->x86);
  1209. pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
  1210. if (c->x86_stepping || c->cpuid_level >= 0)
  1211. pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
  1212. else
  1213. pr_cont(")\n");
  1214. print_cpu_msr(c);
  1215. }
  1216. void print_cpu_msr(struct cpuinfo_x86 *c)
  1217. {
  1218. if (c->cpu_index < show_msr)
  1219. __print_cpu_msr();
  1220. }
  1221. static __init int setup_disablecpuid(char *arg)
  1222. {
  1223. int bit;
  1224. if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
  1225. setup_clear_cpu_cap(bit);
  1226. else
  1227. return 0;
  1228. return 1;
  1229. }
  1230. __setup("clearcpuid=", setup_disablecpuid);
  1231. #ifdef CONFIG_X86_64
  1232. struct desc_ptr idt_descr __ro_after_init = {
  1233. .size = NR_VECTORS * 16 - 1,
  1234. .address = (unsigned long) idt_table,
  1235. };
  1236. const struct desc_ptr debug_idt_descr = {
  1237. .size = NR_VECTORS * 16 - 1,
  1238. .address = (unsigned long) debug_idt_table,
  1239. };
  1240. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  1241. irq_stack_union) __aligned(PAGE_SIZE) __visible;
  1242. /*
  1243. * The following percpu variables are hot. Align current_task to
  1244. * cacheline size such that they fall in the same cacheline.
  1245. */
  1246. DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
  1247. &init_task;
  1248. EXPORT_PER_CPU_SYMBOL(current_task);
  1249. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  1250. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
  1251. DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
  1252. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1253. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1254. /*
  1255. * Special IST stacks which the CPU switches to when it calls
  1256. * an IST-marked descriptor entry. Up to 7 stacks (hardware
  1257. * limit), all of them are 4K, except the debug stack which
  1258. * is 8K.
  1259. */
  1260. static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
  1261. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  1262. [DEBUG_STACK - 1] = DEBUG_STKSZ
  1263. };
  1264. DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(char, exception_stacks
  1265. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
  1266. /* May not be marked __init: used by software suspend */
  1267. void syscall_init(void)
  1268. {
  1269. wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
  1270. wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
  1271. #ifdef CONFIG_IA32_EMULATION
  1272. wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
  1273. /*
  1274. * This only works on Intel CPUs.
  1275. * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
  1276. * This does not cause SYSENTER to jump to the wrong location, because
  1277. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  1278. */
  1279. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  1280. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1281. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
  1282. #else
  1283. wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
  1284. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
  1285. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1286. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
  1287. #endif
  1288. /* Flags to clear on syscall */
  1289. wrmsrl(MSR_SYSCALL_MASK,
  1290. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
  1291. X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
  1292. }
  1293. /*
  1294. * Copies of the original ist values from the tss are only accessed during
  1295. * debugging, no special alignment required.
  1296. */
  1297. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  1298. static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
  1299. DEFINE_PER_CPU(int, debug_stack_usage);
  1300. int is_debug_stack(unsigned long addr)
  1301. {
  1302. return __this_cpu_read(debug_stack_usage) ||
  1303. (addr <= __this_cpu_read(debug_stack_addr) &&
  1304. addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
  1305. }
  1306. NOKPROBE_SYMBOL(is_debug_stack);
  1307. DEFINE_PER_CPU(u32, debug_idt_ctr);
  1308. void debug_stack_set_zero(void)
  1309. {
  1310. this_cpu_inc(debug_idt_ctr);
  1311. load_current_idt();
  1312. }
  1313. NOKPROBE_SYMBOL(debug_stack_set_zero);
  1314. void debug_stack_reset(void)
  1315. {
  1316. if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
  1317. return;
  1318. if (this_cpu_dec_return(debug_idt_ctr) == 0)
  1319. load_current_idt();
  1320. }
  1321. NOKPROBE_SYMBOL(debug_stack_reset);
  1322. #else /* CONFIG_X86_64 */
  1323. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  1324. EXPORT_PER_CPU_SYMBOL(current_task);
  1325. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1326. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1327. /*
  1328. * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
  1329. * the top of the kernel stack. Use an extra percpu variable to track the
  1330. * top of the kernel stack directly.
  1331. */
  1332. DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
  1333. (unsigned long)&init_thread_union + THREAD_SIZE;
  1334. EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
  1335. #ifdef CONFIG_CC_STACKPROTECTOR
  1336. DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
  1337. #endif
  1338. #endif /* CONFIG_X86_64 */
  1339. /*
  1340. * Clear all 6 debug registers:
  1341. */
  1342. static void clear_all_debug_regs(void)
  1343. {
  1344. int i;
  1345. for (i = 0; i < 8; i++) {
  1346. /* Ignore db4, db5 */
  1347. if ((i == 4) || (i == 5))
  1348. continue;
  1349. set_debugreg(0, i);
  1350. }
  1351. }
  1352. #ifdef CONFIG_KGDB
  1353. /*
  1354. * Restore debug regs if using kgdbwait and you have a kernel debugger
  1355. * connection established.
  1356. */
  1357. static void dbg_restore_debug_regs(void)
  1358. {
  1359. if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  1360. arch_kgdb_ops.correct_hw_break();
  1361. }
  1362. #else /* ! CONFIG_KGDB */
  1363. #define dbg_restore_debug_regs()
  1364. #endif /* ! CONFIG_KGDB */
  1365. static void wait_for_master_cpu(int cpu)
  1366. {
  1367. #ifdef CONFIG_SMP
  1368. /*
  1369. * wait for ACK from master CPU before continuing
  1370. * with AP initialization
  1371. */
  1372. WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
  1373. while (!cpumask_test_cpu(cpu, cpu_callout_mask))
  1374. cpu_relax();
  1375. #endif
  1376. }
  1377. /*
  1378. * cpu_init() initializes state that is per-CPU. Some data is already
  1379. * initialized (naturally) in the bootstrap process, such as the GDT
  1380. * and IDT. We reload them nevertheless, this function acts as a
  1381. * 'CPU state barrier', nothing should get across.
  1382. * A lot of state is already set up in PDA init for 64 bit
  1383. */
  1384. #ifdef CONFIG_X86_64
  1385. void cpu_init(void)
  1386. {
  1387. struct orig_ist *oist;
  1388. struct task_struct *me;
  1389. struct tss_struct *t;
  1390. unsigned long v;
  1391. int cpu = raw_smp_processor_id();
  1392. int i;
  1393. wait_for_master_cpu(cpu);
  1394. /*
  1395. * Initialize the CR4 shadow before doing anything that could
  1396. * try to read it.
  1397. */
  1398. cr4_init_shadow();
  1399. if (!kaiser_enabled) {
  1400. /*
  1401. * secondary_startup_64() deferred setting PGE in cr4:
  1402. * probe_page_size_mask() sets it on the boot cpu,
  1403. * but it needs to be set on each secondary cpu.
  1404. */
  1405. cr4_set_bits(X86_CR4_PGE);
  1406. }
  1407. /*
  1408. * Load microcode on this cpu if a valid microcode is available.
  1409. * This is early microcode loading procedure.
  1410. */
  1411. load_ucode_ap();
  1412. t = &per_cpu(cpu_tss, cpu);
  1413. oist = &per_cpu(orig_ist, cpu);
  1414. #ifdef CONFIG_NUMA
  1415. if (this_cpu_read(numa_node) == 0 &&
  1416. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1417. set_numa_node(early_cpu_to_node(cpu));
  1418. #endif
  1419. me = current;
  1420. pr_debug("Initializing CPU#%d\n", cpu);
  1421. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1422. /*
  1423. * Initialize the per-CPU GDT with the boot GDT,
  1424. * and set up the GDT descriptor:
  1425. */
  1426. switch_to_new_gdt(cpu);
  1427. loadsegment(fs, 0);
  1428. load_current_idt();
  1429. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1430. syscall_init();
  1431. wrmsrl(MSR_FS_BASE, 0);
  1432. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1433. barrier();
  1434. x86_configure_nx();
  1435. x2apic_setup();
  1436. /*
  1437. * set up and load the per-CPU TSS
  1438. */
  1439. if (!oist->ist[0]) {
  1440. char *estacks = per_cpu(exception_stacks, cpu);
  1441. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  1442. estacks += exception_stack_sizes[v];
  1443. oist->ist[v] = t->x86_tss.ist[v] =
  1444. (unsigned long)estacks;
  1445. if (v == DEBUG_STACK-1)
  1446. per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
  1447. }
  1448. }
  1449. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1450. /*
  1451. * <= is required because the CPU will access up to
  1452. * 8 bits beyond the end of the IO permission bitmap.
  1453. */
  1454. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  1455. t->io_bitmap[i] = ~0UL;
  1456. atomic_inc(&init_mm.mm_count);
  1457. me->active_mm = &init_mm;
  1458. BUG_ON(me->mm);
  1459. enter_lazy_tlb(&init_mm, me);
  1460. load_sp0(t, &current->thread);
  1461. set_tss_desc(cpu, t);
  1462. load_TR_desc();
  1463. load_mm_ldt(&init_mm);
  1464. clear_all_debug_regs();
  1465. dbg_restore_debug_regs();
  1466. fpu__init_cpu();
  1467. if (is_uv_system())
  1468. uv_cpu_init();
  1469. }
  1470. #else
  1471. void cpu_init(void)
  1472. {
  1473. int cpu = smp_processor_id();
  1474. struct task_struct *curr = current;
  1475. struct tss_struct *t = &per_cpu(cpu_tss, cpu);
  1476. struct thread_struct *thread = &curr->thread;
  1477. wait_for_master_cpu(cpu);
  1478. /*
  1479. * Initialize the CR4 shadow before doing anything that could
  1480. * try to read it.
  1481. */
  1482. cr4_init_shadow();
  1483. show_ucode_info_early();
  1484. pr_info("Initializing CPU#%d\n", cpu);
  1485. if (cpu_feature_enabled(X86_FEATURE_VME) ||
  1486. boot_cpu_has(X86_FEATURE_TSC) ||
  1487. boot_cpu_has(X86_FEATURE_DE))
  1488. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1489. load_current_idt();
  1490. switch_to_new_gdt(cpu);
  1491. /*
  1492. * Set up and load the per-CPU TSS and LDT
  1493. */
  1494. atomic_inc(&init_mm.mm_count);
  1495. curr->active_mm = &init_mm;
  1496. BUG_ON(curr->mm);
  1497. enter_lazy_tlb(&init_mm, curr);
  1498. load_sp0(t, thread);
  1499. set_tss_desc(cpu, t);
  1500. load_TR_desc();
  1501. load_mm_ldt(&init_mm);
  1502. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1503. #ifdef CONFIG_DOUBLEFAULT
  1504. /* Set up doublefault TSS pointer in the GDT */
  1505. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  1506. #endif
  1507. clear_all_debug_regs();
  1508. dbg_restore_debug_regs();
  1509. fpu__init_cpu();
  1510. }
  1511. #endif
  1512. static void bsp_resume(void)
  1513. {
  1514. if (this_cpu->c_bsp_resume)
  1515. this_cpu->c_bsp_resume(&boot_cpu_data);
  1516. }
  1517. static struct syscore_ops cpu_syscore_ops = {
  1518. .resume = bsp_resume,
  1519. };
  1520. static int __init init_cpu_syscore(void)
  1521. {
  1522. register_syscore_ops(&cpu_syscore_ops);
  1523. return 0;
  1524. }
  1525. core_initcall(init_cpu_syscore);