common.c 36 KB

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