common.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  138. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  139. setup_clear_cpu_cap(X86_FEATURE_AVX);
  140. setup_clear_cpu_cap(X86_FEATURE_AVX2);
  141. return 1;
  142. }
  143. __setup("noxsave", x86_xsave_setup);
  144. static int __init x86_xsaveopt_setup(char *s)
  145. {
  146. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  147. return 1;
  148. }
  149. __setup("noxsaveopt", x86_xsaveopt_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_fxsr_setup(char *s)
  160. {
  161. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  162. setup_clear_cpu_cap(X86_FEATURE_XMM);
  163. return 1;
  164. }
  165. __setup("nofxsr", x86_fxsr_setup);
  166. static int __init x86_sep_setup(char *s)
  167. {
  168. setup_clear_cpu_cap(X86_FEATURE_SEP);
  169. return 1;
  170. }
  171. __setup("nosep", x86_sep_setup);
  172. /* Standard macro to see if a specific flag is changeable */
  173. static inline int flag_is_changeable_p(u32 flag)
  174. {
  175. u32 f1, f2;
  176. /*
  177. * Cyrix and IDT cpus allow disabling of CPUID
  178. * so the code below may return different results
  179. * when it is executed before and after enabling
  180. * the CPUID. Add "volatile" to not allow gcc to
  181. * optimize the subsequent calls to this function.
  182. */
  183. asm volatile ("pushfl \n\t"
  184. "pushfl \n\t"
  185. "popl %0 \n\t"
  186. "movl %0, %1 \n\t"
  187. "xorl %2, %0 \n\t"
  188. "pushl %0 \n\t"
  189. "popfl \n\t"
  190. "pushfl \n\t"
  191. "popl %0 \n\t"
  192. "popfl \n\t"
  193. : "=&r" (f1), "=&r" (f2)
  194. : "ir" (flag));
  195. return ((f1^f2) & flag) != 0;
  196. }
  197. /* Probe for the CPUID instruction */
  198. int have_cpuid_p(void)
  199. {
  200. return flag_is_changeable_p(X86_EFLAGS_ID);
  201. }
  202. static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  203. {
  204. unsigned long lo, hi;
  205. if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
  206. return;
  207. /* Disable processor serial number: */
  208. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  209. lo |= 0x200000;
  210. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  211. printk(KERN_NOTICE "CPU serial number disabled.\n");
  212. clear_cpu_cap(c, X86_FEATURE_PN);
  213. /* Disabling the serial number may affect the cpuid level */
  214. c->cpuid_level = cpuid_eax(0);
  215. }
  216. static int __init x86_serial_nr_setup(char *s)
  217. {
  218. disable_x86_serial_nr = 0;
  219. return 1;
  220. }
  221. __setup("serialnumber", x86_serial_nr_setup);
  222. #else
  223. static inline int flag_is_changeable_p(u32 flag)
  224. {
  225. return 1;
  226. }
  227. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  228. {
  229. }
  230. #endif
  231. static __init int setup_disable_smep(char *arg)
  232. {
  233. setup_clear_cpu_cap(X86_FEATURE_SMEP);
  234. return 1;
  235. }
  236. __setup("nosmep", setup_disable_smep);
  237. static __always_inline void setup_smep(struct cpuinfo_x86 *c)
  238. {
  239. if (cpu_has(c, X86_FEATURE_SMEP))
  240. set_in_cr4(X86_CR4_SMEP);
  241. }
  242. static __init int setup_disable_smap(char *arg)
  243. {
  244. setup_clear_cpu_cap(X86_FEATURE_SMAP);
  245. return 1;
  246. }
  247. __setup("nosmap", setup_disable_smap);
  248. static __always_inline void setup_smap(struct cpuinfo_x86 *c)
  249. {
  250. unsigned long eflags;
  251. /* This should have been cleared long ago */
  252. raw_local_save_flags(eflags);
  253. BUG_ON(eflags & X86_EFLAGS_AC);
  254. if (cpu_has(c, X86_FEATURE_SMAP)) {
  255. #ifdef CONFIG_X86_SMAP
  256. set_in_cr4(X86_CR4_SMAP);
  257. #else
  258. clear_in_cr4(X86_CR4_SMAP);
  259. #endif
  260. }
  261. }
  262. /*
  263. * Some CPU features depend on higher CPUID levels, which may not always
  264. * be available due to CPUID level capping or broken virtualization
  265. * software. Add those features to this table to auto-disable them.
  266. */
  267. struct cpuid_dependent_feature {
  268. u32 feature;
  269. u32 level;
  270. };
  271. static const struct cpuid_dependent_feature
  272. cpuid_dependent_features[] = {
  273. { X86_FEATURE_MWAIT, 0x00000005 },
  274. { X86_FEATURE_DCA, 0x00000009 },
  275. { X86_FEATURE_XSAVE, 0x0000000d },
  276. { 0, 0 }
  277. };
  278. static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  279. {
  280. const struct cpuid_dependent_feature *df;
  281. for (df = cpuid_dependent_features; df->feature; df++) {
  282. if (!cpu_has(c, df->feature))
  283. continue;
  284. /*
  285. * Note: cpuid_level is set to -1 if unavailable, but
  286. * extended_extended_level is set to 0 if unavailable
  287. * and the legitimate extended levels are all negative
  288. * when signed; hence the weird messing around with
  289. * signs here...
  290. */
  291. if (!((s32)df->level < 0 ?
  292. (u32)df->level > (u32)c->extended_cpuid_level :
  293. (s32)df->level > (s32)c->cpuid_level))
  294. continue;
  295. clear_cpu_cap(c, df->feature);
  296. if (!warn)
  297. continue;
  298. printk(KERN_WARNING
  299. "CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
  300. x86_cap_flags[df->feature], df->level);
  301. }
  302. }
  303. /*
  304. * Naming convention should be: <Name> [(<Codename>)]
  305. * This table only is used unless init_<vendor>() below doesn't set it;
  306. * in particular, if CPUID levels 0x80000002..4 are supported, this
  307. * isn't used
  308. */
  309. /* Look up CPU names by table lookup. */
  310. static const char *table_lookup_model(struct cpuinfo_x86 *c)
  311. {
  312. #ifdef CONFIG_X86_32
  313. const struct legacy_cpu_model_info *info;
  314. if (c->x86_model >= 16)
  315. return NULL; /* Range check */
  316. if (!this_cpu)
  317. return NULL;
  318. info = this_cpu->legacy_models;
  319. while (info->family) {
  320. if (info->family == c->x86)
  321. return info->model_names[c->x86_model];
  322. info++;
  323. }
  324. #endif
  325. return NULL; /* Not found */
  326. }
  327. __u32 cpu_caps_cleared[NCAPINTS];
  328. __u32 cpu_caps_set[NCAPINTS];
  329. void load_percpu_segment(int cpu)
  330. {
  331. #ifdef CONFIG_X86_32
  332. loadsegment(fs, __KERNEL_PERCPU);
  333. #else
  334. loadsegment(gs, 0);
  335. wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  336. #endif
  337. load_stack_canary_segment();
  338. }
  339. /*
  340. * Current gdt points %fs at the "master" per-cpu area: after this,
  341. * it's on the real one.
  342. */
  343. void switch_to_new_gdt(int cpu)
  344. {
  345. struct desc_ptr gdt_descr;
  346. gdt_descr.address = (long)get_cpu_gdt_table(cpu);
  347. gdt_descr.size = GDT_SIZE - 1;
  348. load_gdt(&gdt_descr);
  349. /* Reload the per-cpu base */
  350. load_percpu_segment(cpu);
  351. }
  352. static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  353. static void get_model_name(struct cpuinfo_x86 *c)
  354. {
  355. unsigned int *v;
  356. char *p, *q;
  357. if (c->extended_cpuid_level < 0x80000004)
  358. return;
  359. v = (unsigned int *)c->x86_model_id;
  360. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  361. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  362. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  363. c->x86_model_id[48] = 0;
  364. /*
  365. * Intel chips right-justify this string for some dumb reason;
  366. * undo that brain damage:
  367. */
  368. p = q = &c->x86_model_id[0];
  369. while (*p == ' ')
  370. p++;
  371. if (p != q) {
  372. while (*p)
  373. *q++ = *p++;
  374. while (q <= &c->x86_model_id[48])
  375. *q++ = '\0'; /* Zero-pad the rest */
  376. }
  377. }
  378. void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
  379. {
  380. unsigned int n, dummy, ebx, ecx, edx, l2size;
  381. n = c->extended_cpuid_level;
  382. if (n >= 0x80000005) {
  383. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  384. c->x86_cache_size = (ecx>>24) + (edx>>24);
  385. #ifdef CONFIG_X86_64
  386. /* On K8 L1 TLB is inclusive, so don't count it */
  387. c->x86_tlbsize = 0;
  388. #endif
  389. }
  390. if (n < 0x80000006) /* Some chips just has a large L1. */
  391. return;
  392. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  393. l2size = ecx >> 16;
  394. #ifdef CONFIG_X86_64
  395. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  396. #else
  397. /* do processor-specific cache resizing */
  398. if (this_cpu->legacy_cache_size)
  399. l2size = this_cpu->legacy_cache_size(c, l2size);
  400. /* Allow user to override all this if necessary. */
  401. if (cachesize_override != -1)
  402. l2size = cachesize_override;
  403. if (l2size == 0)
  404. return; /* Again, no L2 cache is possible */
  405. #endif
  406. c->x86_cache_size = l2size;
  407. }
  408. u16 __read_mostly tlb_lli_4k[NR_INFO];
  409. u16 __read_mostly tlb_lli_2m[NR_INFO];
  410. u16 __read_mostly tlb_lli_4m[NR_INFO];
  411. u16 __read_mostly tlb_lld_4k[NR_INFO];
  412. u16 __read_mostly tlb_lld_2m[NR_INFO];
  413. u16 __read_mostly tlb_lld_4m[NR_INFO];
  414. u16 __read_mostly tlb_lld_1g[NR_INFO];
  415. /*
  416. * tlb_flushall_shift shows the balance point in replacing cr3 write
  417. * with multiple 'invlpg'. It will do this replacement when
  418. * flush_tlb_lines <= active_lines/2^tlb_flushall_shift.
  419. * If tlb_flushall_shift is -1, means the replacement will be disabled.
  420. */
  421. s8 __read_mostly tlb_flushall_shift = -1;
  422. void cpu_detect_tlb(struct cpuinfo_x86 *c)
  423. {
  424. if (this_cpu->c_detect_tlb)
  425. this_cpu->c_detect_tlb(c);
  426. printk(KERN_INFO "Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n"
  427. "Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n"
  428. "tlb_flushall_shift: %d\n",
  429. tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
  430. tlb_lli_4m[ENTRIES], tlb_lld_4k[ENTRIES],
  431. tlb_lld_2m[ENTRIES], tlb_lld_4m[ENTRIES],
  432. tlb_lld_1g[ENTRIES], tlb_flushall_shift);
  433. }
  434. void detect_ht(struct cpuinfo_x86 *c)
  435. {
  436. #ifdef CONFIG_X86_HT
  437. u32 eax, ebx, ecx, edx;
  438. int index_msb, core_bits;
  439. static bool printed;
  440. if (!cpu_has(c, X86_FEATURE_HT))
  441. return;
  442. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  443. goto out;
  444. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  445. return;
  446. cpuid(1, &eax, &ebx, &ecx, &edx);
  447. smp_num_siblings = (ebx & 0xff0000) >> 16;
  448. if (smp_num_siblings == 1) {
  449. printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
  450. goto out;
  451. }
  452. if (smp_num_siblings <= 1)
  453. goto out;
  454. index_msb = get_count_order(smp_num_siblings);
  455. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  456. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  457. index_msb = get_count_order(smp_num_siblings);
  458. core_bits = get_count_order(c->x86_max_cores);
  459. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  460. ((1 << core_bits) - 1);
  461. out:
  462. if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
  463. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  464. c->phys_proc_id);
  465. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  466. c->cpu_core_id);
  467. printed = 1;
  468. }
  469. #endif
  470. }
  471. static void get_cpu_vendor(struct cpuinfo_x86 *c)
  472. {
  473. char *v = c->x86_vendor_id;
  474. int i;
  475. for (i = 0; i < X86_VENDOR_NUM; i++) {
  476. if (!cpu_devs[i])
  477. break;
  478. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  479. (cpu_devs[i]->c_ident[1] &&
  480. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  481. this_cpu = cpu_devs[i];
  482. c->x86_vendor = this_cpu->c_x86_vendor;
  483. return;
  484. }
  485. }
  486. printk_once(KERN_ERR
  487. "CPU: vendor_id '%s' unknown, using generic init.\n" \
  488. "CPU: Your system may be unstable.\n", v);
  489. c->x86_vendor = X86_VENDOR_UNKNOWN;
  490. this_cpu = &default_cpu;
  491. }
  492. void cpu_detect(struct cpuinfo_x86 *c)
  493. {
  494. /* Get vendor name */
  495. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  496. (unsigned int *)&c->x86_vendor_id[0],
  497. (unsigned int *)&c->x86_vendor_id[8],
  498. (unsigned int *)&c->x86_vendor_id[4]);
  499. c->x86 = 4;
  500. /* Intel-defined flags: level 0x00000001 */
  501. if (c->cpuid_level >= 0x00000001) {
  502. u32 junk, tfms, cap0, misc;
  503. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  504. c->x86 = (tfms >> 8) & 0xf;
  505. c->x86_model = (tfms >> 4) & 0xf;
  506. c->x86_mask = tfms & 0xf;
  507. if (c->x86 == 0xf)
  508. c->x86 += (tfms >> 20) & 0xff;
  509. if (c->x86 >= 0x6)
  510. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  511. if (cap0 & (1<<19)) {
  512. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  513. c->x86_cache_alignment = c->x86_clflush_size;
  514. }
  515. }
  516. }
  517. void get_cpu_cap(struct cpuinfo_x86 *c)
  518. {
  519. u32 tfms, xlvl;
  520. u32 ebx;
  521. /* Intel-defined flags: level 0x00000001 */
  522. if (c->cpuid_level >= 0x00000001) {
  523. u32 capability, excap;
  524. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  525. c->x86_capability[0] = capability;
  526. c->x86_capability[4] = excap;
  527. }
  528. /* Additional Intel-defined flags: level 0x00000007 */
  529. if (c->cpuid_level >= 0x00000007) {
  530. u32 eax, ebx, ecx, edx;
  531. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  532. c->x86_capability[9] = ebx;
  533. }
  534. /* AMD-defined flags: level 0x80000001 */
  535. xlvl = cpuid_eax(0x80000000);
  536. c->extended_cpuid_level = xlvl;
  537. if ((xlvl & 0xffff0000) == 0x80000000) {
  538. if (xlvl >= 0x80000001) {
  539. c->x86_capability[1] = cpuid_edx(0x80000001);
  540. c->x86_capability[6] = cpuid_ecx(0x80000001);
  541. }
  542. }
  543. if (c->extended_cpuid_level >= 0x80000008) {
  544. u32 eax = cpuid_eax(0x80000008);
  545. c->x86_virt_bits = (eax >> 8) & 0xff;
  546. c->x86_phys_bits = eax & 0xff;
  547. }
  548. #ifdef CONFIG_X86_32
  549. else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  550. c->x86_phys_bits = 36;
  551. #endif
  552. if (c->extended_cpuid_level >= 0x80000007)
  553. c->x86_power = cpuid_edx(0x80000007);
  554. init_scattered_cpuid_features(c);
  555. }
  556. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  557. {
  558. #ifdef CONFIG_X86_32
  559. int i;
  560. /*
  561. * First of all, decide if this is a 486 or higher
  562. * It's a 486 if we can modify the AC flag
  563. */
  564. if (flag_is_changeable_p(X86_EFLAGS_AC))
  565. c->x86 = 4;
  566. else
  567. c->x86 = 3;
  568. for (i = 0; i < X86_VENDOR_NUM; i++)
  569. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  570. c->x86_vendor_id[0] = 0;
  571. cpu_devs[i]->c_identify(c);
  572. if (c->x86_vendor_id[0]) {
  573. get_cpu_vendor(c);
  574. break;
  575. }
  576. }
  577. #endif
  578. }
  579. /*
  580. * Do minimum CPU detection early.
  581. * Fields really needed: vendor, cpuid_level, family, model, mask,
  582. * cache alignment.
  583. * The others are not touched to avoid unwanted side effects.
  584. *
  585. * WARNING: this function is only called on the BP. Don't add code here
  586. * that is supposed to run on all CPUs.
  587. */
  588. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  589. {
  590. #ifdef CONFIG_X86_64
  591. c->x86_clflush_size = 64;
  592. c->x86_phys_bits = 36;
  593. c->x86_virt_bits = 48;
  594. #else
  595. c->x86_clflush_size = 32;
  596. c->x86_phys_bits = 32;
  597. c->x86_virt_bits = 32;
  598. #endif
  599. c->x86_cache_alignment = c->x86_clflush_size;
  600. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  601. c->extended_cpuid_level = 0;
  602. if (!have_cpuid_p())
  603. identify_cpu_without_cpuid(c);
  604. /* cyrix could have cpuid enabled via c_identify()*/
  605. if (!have_cpuid_p())
  606. return;
  607. cpu_detect(c);
  608. get_cpu_vendor(c);
  609. get_cpu_cap(c);
  610. fpu_detect(c);
  611. if (this_cpu->c_early_init)
  612. this_cpu->c_early_init(c);
  613. c->cpu_index = 0;
  614. filter_cpuid_features(c, false);
  615. if (this_cpu->c_bsp_init)
  616. this_cpu->c_bsp_init(c);
  617. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  618. }
  619. void __init early_cpu_init(void)
  620. {
  621. const struct cpu_dev *const *cdev;
  622. int count = 0;
  623. #ifdef CONFIG_PROCESSOR_SELECT
  624. printk(KERN_INFO "KERNEL supported cpus:\n");
  625. #endif
  626. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  627. const struct cpu_dev *cpudev = *cdev;
  628. if (count >= X86_VENDOR_NUM)
  629. break;
  630. cpu_devs[count] = cpudev;
  631. count++;
  632. #ifdef CONFIG_PROCESSOR_SELECT
  633. {
  634. unsigned int j;
  635. for (j = 0; j < 2; j++) {
  636. if (!cpudev->c_ident[j])
  637. continue;
  638. printk(KERN_INFO " %s %s\n", cpudev->c_vendor,
  639. cpudev->c_ident[j]);
  640. }
  641. }
  642. #endif
  643. }
  644. early_identify_cpu(&boot_cpu_data);
  645. }
  646. /*
  647. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  648. * unfortunately, that's not true in practice because of early VIA
  649. * chips and (more importantly) broken virtualizers that are not easy
  650. * to detect. In the latter case it doesn't even *fail* reliably, so
  651. * probing for it doesn't even work. Disable it completely on 32-bit
  652. * unless we can find a reliable way to detect all the broken cases.
  653. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  654. */
  655. static void detect_nopl(struct cpuinfo_x86 *c)
  656. {
  657. #ifdef CONFIG_X86_32
  658. clear_cpu_cap(c, X86_FEATURE_NOPL);
  659. #else
  660. set_cpu_cap(c, X86_FEATURE_NOPL);
  661. #endif
  662. }
  663. static void generic_identify(struct cpuinfo_x86 *c)
  664. {
  665. c->extended_cpuid_level = 0;
  666. if (!have_cpuid_p())
  667. identify_cpu_without_cpuid(c);
  668. /* cyrix could have cpuid enabled via c_identify()*/
  669. if (!have_cpuid_p())
  670. return;
  671. cpu_detect(c);
  672. get_cpu_vendor(c);
  673. get_cpu_cap(c);
  674. if (c->cpuid_level >= 0x00000001) {
  675. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  676. #ifdef CONFIG_X86_32
  677. # ifdef CONFIG_X86_HT
  678. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  679. # else
  680. c->apicid = c->initial_apicid;
  681. # endif
  682. #endif
  683. c->phys_proc_id = c->initial_apicid;
  684. }
  685. get_model_name(c); /* Default name */
  686. detect_nopl(c);
  687. }
  688. /*
  689. * This does the hard work of actually picking apart the CPU stuff...
  690. */
  691. static void identify_cpu(struct cpuinfo_x86 *c)
  692. {
  693. int i;
  694. c->loops_per_jiffy = loops_per_jiffy;
  695. c->x86_cache_size = -1;
  696. c->x86_vendor = X86_VENDOR_UNKNOWN;
  697. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  698. c->x86_vendor_id[0] = '\0'; /* Unset */
  699. c->x86_model_id[0] = '\0'; /* Unset */
  700. c->x86_max_cores = 1;
  701. c->x86_coreid_bits = 0;
  702. #ifdef CONFIG_X86_64
  703. c->x86_clflush_size = 64;
  704. c->x86_phys_bits = 36;
  705. c->x86_virt_bits = 48;
  706. #else
  707. c->cpuid_level = -1; /* CPUID not detected */
  708. c->x86_clflush_size = 32;
  709. c->x86_phys_bits = 32;
  710. c->x86_virt_bits = 32;
  711. #endif
  712. c->x86_cache_alignment = c->x86_clflush_size;
  713. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  714. generic_identify(c);
  715. if (this_cpu->c_identify)
  716. this_cpu->c_identify(c);
  717. /* Clear/Set all flags overriden by options, after probe */
  718. for (i = 0; i < NCAPINTS; i++) {
  719. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  720. c->x86_capability[i] |= cpu_caps_set[i];
  721. }
  722. #ifdef CONFIG_X86_64
  723. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  724. #endif
  725. /*
  726. * Vendor-specific initialization. In this section we
  727. * canonicalize the feature flags, meaning if there are
  728. * features a certain CPU supports which CPUID doesn't
  729. * tell us, CPUID claiming incorrect flags, or other bugs,
  730. * we handle them here.
  731. *
  732. * At the end of this section, c->x86_capability better
  733. * indicate the features this CPU genuinely supports!
  734. */
  735. if (this_cpu->c_init)
  736. this_cpu->c_init(c);
  737. /* Disable the PN if appropriate */
  738. squash_the_stupid_serial_number(c);
  739. /* Set up SMEP/SMAP */
  740. setup_smep(c);
  741. setup_smap(c);
  742. /*
  743. * The vendor-specific functions might have changed features.
  744. * Now we do "generic changes."
  745. */
  746. /* Filter out anything that depends on CPUID levels we don't have */
  747. filter_cpuid_features(c, true);
  748. /* If the model name is still unset, do table lookup. */
  749. if (!c->x86_model_id[0]) {
  750. const char *p;
  751. p = table_lookup_model(c);
  752. if (p)
  753. strcpy(c->x86_model_id, p);
  754. else
  755. /* Last resort... */
  756. sprintf(c->x86_model_id, "%02x/%02x",
  757. c->x86, c->x86_model);
  758. }
  759. #ifdef CONFIG_X86_64
  760. detect_ht(c);
  761. #endif
  762. init_hypervisor(c);
  763. x86_init_rdrand(c);
  764. /*
  765. * Clear/Set all flags overriden by options, need do it
  766. * before following smp all cpus cap AND.
  767. */
  768. for (i = 0; i < NCAPINTS; i++) {
  769. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  770. c->x86_capability[i] |= cpu_caps_set[i];
  771. }
  772. /*
  773. * On SMP, boot_cpu_data holds the common feature set between
  774. * all CPUs; so make sure that we indicate which features are
  775. * common between the CPUs. The first time this routine gets
  776. * executed, c == &boot_cpu_data.
  777. */
  778. if (c != &boot_cpu_data) {
  779. /* AND the already accumulated flags with these */
  780. for (i = 0; i < NCAPINTS; i++)
  781. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  782. /* OR, i.e. replicate the bug flags */
  783. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  784. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  785. }
  786. /* Init Machine Check Exception if available. */
  787. mcheck_cpu_init(c);
  788. select_idle_routine(c);
  789. #ifdef CONFIG_NUMA
  790. numa_add_cpu(smp_processor_id());
  791. #endif
  792. }
  793. #ifdef CONFIG_X86_64
  794. static void vgetcpu_set_mode(void)
  795. {
  796. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  797. vgetcpu_mode = VGETCPU_RDTSCP;
  798. else
  799. vgetcpu_mode = VGETCPU_LSL;
  800. }
  801. /* May not be __init: called during resume */
  802. static void syscall32_cpu_init(void)
  803. {
  804. /* Load these always in case some future AMD CPU supports
  805. SYSENTER from compat mode too. */
  806. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  807. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  808. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  809. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  810. }
  811. #endif
  812. #ifdef CONFIG_X86_32
  813. void enable_sep_cpu(void)
  814. {
  815. int cpu = get_cpu();
  816. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  817. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  818. put_cpu();
  819. return;
  820. }
  821. tss->x86_tss.ss1 = __KERNEL_CS;
  822. tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
  823. wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
  824. wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
  825. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
  826. put_cpu();
  827. }
  828. #endif
  829. void __init identify_boot_cpu(void)
  830. {
  831. identify_cpu(&boot_cpu_data);
  832. init_amd_e400_c1e_mask();
  833. #ifdef CONFIG_X86_32
  834. sysenter_setup();
  835. enable_sep_cpu();
  836. #else
  837. vgetcpu_set_mode();
  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);
  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 __get_cpu_var(debug_stack_usage) ||
  993. (addr <= __get_cpu_var(debug_stack_addr) &&
  994. addr > (__get_cpu_var(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. /*
  1049. * cpu_init() initializes state that is per-CPU. Some data is already
  1050. * initialized (naturally) in the bootstrap process, such as the GDT
  1051. * and IDT. We reload them nevertheless, this function acts as a
  1052. * 'CPU state barrier', nothing should get across.
  1053. * A lot of state is already set up in PDA init for 64 bit
  1054. */
  1055. #ifdef CONFIG_X86_64
  1056. void cpu_init(void)
  1057. {
  1058. struct orig_ist *oist;
  1059. struct task_struct *me;
  1060. struct tss_struct *t;
  1061. unsigned long v;
  1062. int cpu;
  1063. int i;
  1064. /*
  1065. * Load microcode on this cpu if a valid microcode is available.
  1066. * This is early microcode loading procedure.
  1067. */
  1068. load_ucode_ap();
  1069. cpu = stack_smp_processor_id();
  1070. t = &per_cpu(init_tss, cpu);
  1071. oist = &per_cpu(orig_ist, cpu);
  1072. #ifdef CONFIG_NUMA
  1073. if (this_cpu_read(numa_node) == 0 &&
  1074. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1075. set_numa_node(early_cpu_to_node(cpu));
  1076. #endif
  1077. me = current;
  1078. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
  1079. panic("CPU#%d already initialized!\n", cpu);
  1080. pr_debug("Initializing CPU#%d\n", cpu);
  1081. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1082. /*
  1083. * Initialize the per-CPU GDT with the boot GDT,
  1084. * and set up the GDT descriptor:
  1085. */
  1086. switch_to_new_gdt(cpu);
  1087. loadsegment(fs, 0);
  1088. load_current_idt();
  1089. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1090. syscall_init();
  1091. wrmsrl(MSR_FS_BASE, 0);
  1092. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1093. barrier();
  1094. x86_configure_nx();
  1095. enable_x2apic();
  1096. /*
  1097. * set up and load the per-CPU TSS
  1098. */
  1099. if (!oist->ist[0]) {
  1100. char *estacks = per_cpu(exception_stacks, cpu);
  1101. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  1102. estacks += exception_stack_sizes[v];
  1103. oist->ist[v] = t->x86_tss.ist[v] =
  1104. (unsigned long)estacks;
  1105. if (v == DEBUG_STACK-1)
  1106. per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
  1107. }
  1108. }
  1109. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1110. /*
  1111. * <= is required because the CPU will access up to
  1112. * 8 bits beyond the end of the IO permission bitmap.
  1113. */
  1114. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  1115. t->io_bitmap[i] = ~0UL;
  1116. atomic_inc(&init_mm.mm_count);
  1117. me->active_mm = &init_mm;
  1118. BUG_ON(me->mm);
  1119. enter_lazy_tlb(&init_mm, me);
  1120. load_sp0(t, &current->thread);
  1121. set_tss_desc(cpu, t);
  1122. load_TR_desc();
  1123. load_LDT(&init_mm.context);
  1124. clear_all_debug_regs();
  1125. dbg_restore_debug_regs();
  1126. fpu_init();
  1127. if (is_uv_system())
  1128. uv_cpu_init();
  1129. }
  1130. #else
  1131. void cpu_init(void)
  1132. {
  1133. int cpu = smp_processor_id();
  1134. struct task_struct *curr = current;
  1135. struct tss_struct *t = &per_cpu(init_tss, cpu);
  1136. struct thread_struct *thread = &curr->thread;
  1137. show_ucode_info_early();
  1138. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
  1139. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  1140. for (;;)
  1141. local_irq_enable();
  1142. }
  1143. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  1144. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  1145. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1146. load_current_idt();
  1147. switch_to_new_gdt(cpu);
  1148. /*
  1149. * Set up and load the per-CPU TSS and LDT
  1150. */
  1151. atomic_inc(&init_mm.mm_count);
  1152. curr->active_mm = &init_mm;
  1153. BUG_ON(curr->mm);
  1154. enter_lazy_tlb(&init_mm, curr);
  1155. load_sp0(t, thread);
  1156. set_tss_desc(cpu, t);
  1157. load_TR_desc();
  1158. load_LDT(&init_mm.context);
  1159. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1160. #ifdef CONFIG_DOUBLEFAULT
  1161. /* Set up doublefault TSS pointer in the GDT */
  1162. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  1163. #endif
  1164. clear_all_debug_regs();
  1165. dbg_restore_debug_regs();
  1166. fpu_init();
  1167. }
  1168. #endif
  1169. #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
  1170. void warn_pre_alternatives(void)
  1171. {
  1172. WARN(1, "You're using static_cpu_has before alternatives have run!\n");
  1173. }
  1174. EXPORT_SYMBOL_GPL(warn_pre_alternatives);
  1175. #endif
  1176. inline bool __static_cpu_has_safe(u16 bit)
  1177. {
  1178. return boot_cpu_has(bit);
  1179. }
  1180. EXPORT_SYMBOL_GPL(__static_cpu_has_safe);