common.c 36 KB

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