common.c 39 KB

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