common.c 40 KB

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