common.c 42 KB

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