common.c 34 KB

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