common.c 40 KB

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