common.c 39 KB

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