common.c 39 KB

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