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