common.c 38 KB

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