common.c 32 KB

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