common.c 41 KB

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