common.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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_pcid_setup(char *s)
  157. {
  158. /* require an exact match without trailing characters */
  159. if (strlen(s))
  160. return 0;
  161. /* do not emit a message if the feature is not present */
  162. if (!boot_cpu_has(X86_FEATURE_PCID))
  163. return 1;
  164. setup_clear_cpu_cap(X86_FEATURE_PCID);
  165. pr_info("nopcid: PCID feature disabled\n");
  166. return 1;
  167. }
  168. __setup("nopcid", x86_pcid_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 void setup_pcid(struct cpuinfo_x86 *c)
  290. {
  291. if (cpu_has(c, X86_FEATURE_PCID)) {
  292. if (cpu_has(c, X86_FEATURE_PGE)) {
  293. cr4_set_bits(X86_CR4_PCIDE);
  294. } else {
  295. /*
  296. * flush_tlb_all(), as currently implemented, won't
  297. * work if PCID is on but PGE is not. Since that
  298. * combination doesn't exist on real hardware, there's
  299. * no reason to try to fully support it, but it's
  300. * polite to avoid corrupting data if we're on
  301. * an improperly configured VM.
  302. */
  303. clear_cpu_cap(c, X86_FEATURE_PCID);
  304. }
  305. }
  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];
  414. __u32 cpu_caps_set[NCAPINTS];
  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. /* Setup the fixmap mapping only once per-processor */
  426. static inline void setup_fixmap_gdt(int cpu)
  427. {
  428. #ifdef CONFIG_X86_64
  429. /* On 64-bit systems, we use a read-only fixmap GDT. */
  430. pgprot_t prot = PAGE_KERNEL_RO;
  431. #else
  432. /*
  433. * On native 32-bit systems, the GDT cannot be read-only because
  434. * our double fault handler uses a task gate, and entering through
  435. * a task gate needs to change an available TSS to busy. If the GDT
  436. * is read-only, that will triple fault.
  437. *
  438. * On Xen PV, the GDT must be read-only because the hypervisor requires
  439. * it.
  440. */
  441. pgprot_t prot = boot_cpu_has(X86_FEATURE_XENPV) ?
  442. PAGE_KERNEL_RO : PAGE_KERNEL;
  443. #endif
  444. __set_fixmap(get_cpu_gdt_ro_index(cpu), get_cpu_gdt_paddr(cpu), prot);
  445. }
  446. /* Load the original GDT from the per-cpu structure */
  447. void load_direct_gdt(int cpu)
  448. {
  449. struct desc_ptr gdt_descr;
  450. gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
  451. gdt_descr.size = GDT_SIZE - 1;
  452. load_gdt(&gdt_descr);
  453. }
  454. EXPORT_SYMBOL_GPL(load_direct_gdt);
  455. /* Load a fixmap remapping of the per-cpu GDT */
  456. void load_fixmap_gdt(int cpu)
  457. {
  458. struct desc_ptr gdt_descr;
  459. gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
  460. gdt_descr.size = GDT_SIZE - 1;
  461. load_gdt(&gdt_descr);
  462. }
  463. EXPORT_SYMBOL_GPL(load_fixmap_gdt);
  464. /*
  465. * Current gdt points %fs at the "master" per-cpu area: after this,
  466. * it's on the real one.
  467. */
  468. void switch_to_new_gdt(int cpu)
  469. {
  470. /* Load the original GDT */
  471. load_direct_gdt(cpu);
  472. /* Reload the per-cpu base */
  473. load_percpu_segment(cpu);
  474. }
  475. static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  476. static void get_model_name(struct cpuinfo_x86 *c)
  477. {
  478. unsigned int *v;
  479. char *p, *q, *s;
  480. if (c->extended_cpuid_level < 0x80000004)
  481. return;
  482. v = (unsigned int *)c->x86_model_id;
  483. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  484. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  485. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  486. c->x86_model_id[48] = 0;
  487. /* Trim whitespace */
  488. p = q = s = &c->x86_model_id[0];
  489. while (*p == ' ')
  490. p++;
  491. while (*p) {
  492. /* Note the last non-whitespace index */
  493. if (!isspace(*p))
  494. s = q;
  495. *q++ = *p++;
  496. }
  497. *(s + 1) = '\0';
  498. }
  499. void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
  500. {
  501. unsigned int n, dummy, ebx, ecx, edx, l2size;
  502. n = c->extended_cpuid_level;
  503. if (n >= 0x80000005) {
  504. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  505. c->x86_cache_size = (ecx>>24) + (edx>>24);
  506. #ifdef CONFIG_X86_64
  507. /* On K8 L1 TLB is inclusive, so don't count it */
  508. c->x86_tlbsize = 0;
  509. #endif
  510. }
  511. if (n < 0x80000006) /* Some chips just has a large L1. */
  512. return;
  513. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  514. l2size = ecx >> 16;
  515. #ifdef CONFIG_X86_64
  516. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  517. #else
  518. /* do processor-specific cache resizing */
  519. if (this_cpu->legacy_cache_size)
  520. l2size = this_cpu->legacy_cache_size(c, l2size);
  521. /* Allow user to override all this if necessary. */
  522. if (cachesize_override != -1)
  523. l2size = cachesize_override;
  524. if (l2size == 0)
  525. return; /* Again, no L2 cache is possible */
  526. #endif
  527. c->x86_cache_size = l2size;
  528. }
  529. u16 __read_mostly tlb_lli_4k[NR_INFO];
  530. u16 __read_mostly tlb_lli_2m[NR_INFO];
  531. u16 __read_mostly tlb_lli_4m[NR_INFO];
  532. u16 __read_mostly tlb_lld_4k[NR_INFO];
  533. u16 __read_mostly tlb_lld_2m[NR_INFO];
  534. u16 __read_mostly tlb_lld_4m[NR_INFO];
  535. u16 __read_mostly tlb_lld_1g[NR_INFO];
  536. static void cpu_detect_tlb(struct cpuinfo_x86 *c)
  537. {
  538. if (this_cpu->c_detect_tlb)
  539. this_cpu->c_detect_tlb(c);
  540. pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
  541. tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
  542. tlb_lli_4m[ENTRIES]);
  543. pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
  544. tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
  545. tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
  546. }
  547. void detect_ht(struct cpuinfo_x86 *c)
  548. {
  549. #ifdef CONFIG_SMP
  550. u32 eax, ebx, ecx, edx;
  551. int index_msb, core_bits;
  552. static bool printed;
  553. if (!cpu_has(c, X86_FEATURE_HT))
  554. return;
  555. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  556. goto out;
  557. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  558. return;
  559. cpuid(1, &eax, &ebx, &ecx, &edx);
  560. smp_num_siblings = (ebx & 0xff0000) >> 16;
  561. if (smp_num_siblings == 1) {
  562. pr_info_once("CPU0: Hyper-Threading is disabled\n");
  563. goto out;
  564. }
  565. if (smp_num_siblings <= 1)
  566. goto out;
  567. index_msb = get_count_order(smp_num_siblings);
  568. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  569. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  570. index_msb = get_count_order(smp_num_siblings);
  571. core_bits = get_count_order(c->x86_max_cores);
  572. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  573. ((1 << core_bits) - 1);
  574. out:
  575. if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
  576. pr_info("CPU: Physical Processor ID: %d\n",
  577. c->phys_proc_id);
  578. pr_info("CPU: Processor Core ID: %d\n",
  579. c->cpu_core_id);
  580. printed = 1;
  581. }
  582. #endif
  583. }
  584. static void get_cpu_vendor(struct cpuinfo_x86 *c)
  585. {
  586. char *v = c->x86_vendor_id;
  587. int i;
  588. for (i = 0; i < X86_VENDOR_NUM; i++) {
  589. if (!cpu_devs[i])
  590. break;
  591. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  592. (cpu_devs[i]->c_ident[1] &&
  593. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  594. this_cpu = cpu_devs[i];
  595. c->x86_vendor = this_cpu->c_x86_vendor;
  596. return;
  597. }
  598. }
  599. pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
  600. "CPU: Your system may be unstable.\n", v);
  601. c->x86_vendor = X86_VENDOR_UNKNOWN;
  602. this_cpu = &default_cpu;
  603. }
  604. void cpu_detect(struct cpuinfo_x86 *c)
  605. {
  606. /* Get vendor name */
  607. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  608. (unsigned int *)&c->x86_vendor_id[0],
  609. (unsigned int *)&c->x86_vendor_id[8],
  610. (unsigned int *)&c->x86_vendor_id[4]);
  611. c->x86 = 4;
  612. /* Intel-defined flags: level 0x00000001 */
  613. if (c->cpuid_level >= 0x00000001) {
  614. u32 junk, tfms, cap0, misc;
  615. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  616. c->x86 = x86_family(tfms);
  617. c->x86_model = x86_model(tfms);
  618. c->x86_mask = x86_stepping(tfms);
  619. if (cap0 & (1<<19)) {
  620. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  621. c->x86_cache_alignment = c->x86_clflush_size;
  622. }
  623. }
  624. }
  625. static void apply_forced_caps(struct cpuinfo_x86 *c)
  626. {
  627. int i;
  628. for (i = 0; i < NCAPINTS; i++) {
  629. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  630. c->x86_capability[i] |= cpu_caps_set[i];
  631. }
  632. }
  633. void get_cpu_cap(struct cpuinfo_x86 *c)
  634. {
  635. u32 eax, ebx, ecx, edx;
  636. /* Intel-defined flags: level 0x00000001 */
  637. if (c->cpuid_level >= 0x00000001) {
  638. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  639. c->x86_capability[CPUID_1_ECX] = ecx;
  640. c->x86_capability[CPUID_1_EDX] = edx;
  641. }
  642. /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
  643. if (c->cpuid_level >= 0x00000006)
  644. c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
  645. /* Additional Intel-defined flags: level 0x00000007 */
  646. if (c->cpuid_level >= 0x00000007) {
  647. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  648. c->x86_capability[CPUID_7_0_EBX] = ebx;
  649. c->x86_capability[CPUID_7_ECX] = ecx;
  650. }
  651. /* Extended state features: level 0x0000000d */
  652. if (c->cpuid_level >= 0x0000000d) {
  653. cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
  654. c->x86_capability[CPUID_D_1_EAX] = eax;
  655. }
  656. /* Additional Intel-defined flags: level 0x0000000F */
  657. if (c->cpuid_level >= 0x0000000F) {
  658. /* QoS sub-leaf, EAX=0Fh, ECX=0 */
  659. cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
  660. c->x86_capability[CPUID_F_0_EDX] = edx;
  661. if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
  662. /* will be overridden if occupancy monitoring exists */
  663. c->x86_cache_max_rmid = ebx;
  664. /* QoS sub-leaf, EAX=0Fh, ECX=1 */
  665. cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
  666. c->x86_capability[CPUID_F_1_EDX] = edx;
  667. if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
  668. ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
  669. (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
  670. c->x86_cache_max_rmid = ecx;
  671. c->x86_cache_occ_scale = ebx;
  672. }
  673. } else {
  674. c->x86_cache_max_rmid = -1;
  675. c->x86_cache_occ_scale = -1;
  676. }
  677. }
  678. /* AMD-defined flags: level 0x80000001 */
  679. eax = cpuid_eax(0x80000000);
  680. c->extended_cpuid_level = eax;
  681. if ((eax & 0xffff0000) == 0x80000000) {
  682. if (eax >= 0x80000001) {
  683. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  684. c->x86_capability[CPUID_8000_0001_ECX] = ecx;
  685. c->x86_capability[CPUID_8000_0001_EDX] = edx;
  686. }
  687. }
  688. if (c->extended_cpuid_level >= 0x80000007) {
  689. cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  690. c->x86_capability[CPUID_8000_0007_EBX] = ebx;
  691. c->x86_power = edx;
  692. }
  693. if (c->extended_cpuid_level >= 0x80000008) {
  694. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  695. c->x86_virt_bits = (eax >> 8) & 0xff;
  696. c->x86_phys_bits = eax & 0xff;
  697. c->x86_capability[CPUID_8000_0008_EBX] = ebx;
  698. }
  699. #ifdef CONFIG_X86_32
  700. else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  701. c->x86_phys_bits = 36;
  702. #endif
  703. if (c->extended_cpuid_level >= 0x8000000a)
  704. c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
  705. init_scattered_cpuid_features(c);
  706. /*
  707. * Clear/Set all flags overridden by options, after probe.
  708. * This needs to happen each time we re-probe, which may happen
  709. * several times during CPU initialization.
  710. */
  711. apply_forced_caps(c);
  712. }
  713. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  714. {
  715. #ifdef CONFIG_X86_32
  716. int i;
  717. /*
  718. * First of all, decide if this is a 486 or higher
  719. * It's a 486 if we can modify the AC flag
  720. */
  721. if (flag_is_changeable_p(X86_EFLAGS_AC))
  722. c->x86 = 4;
  723. else
  724. c->x86 = 3;
  725. for (i = 0; i < X86_VENDOR_NUM; i++)
  726. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  727. c->x86_vendor_id[0] = 0;
  728. cpu_devs[i]->c_identify(c);
  729. if (c->x86_vendor_id[0]) {
  730. get_cpu_vendor(c);
  731. break;
  732. }
  733. }
  734. #endif
  735. }
  736. /*
  737. * Do minimum CPU detection early.
  738. * Fields really needed: vendor, cpuid_level, family, model, mask,
  739. * cache alignment.
  740. * The others are not touched to avoid unwanted side effects.
  741. *
  742. * WARNING: this function is only called on the BP. Don't add code here
  743. * that is supposed to run on all CPUs.
  744. */
  745. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  746. {
  747. #ifdef CONFIG_X86_64
  748. c->x86_clflush_size = 64;
  749. c->x86_phys_bits = 36;
  750. c->x86_virt_bits = 48;
  751. #else
  752. c->x86_clflush_size = 32;
  753. c->x86_phys_bits = 32;
  754. c->x86_virt_bits = 32;
  755. #endif
  756. c->x86_cache_alignment = c->x86_clflush_size;
  757. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  758. c->extended_cpuid_level = 0;
  759. /* cyrix could have cpuid enabled via c_identify()*/
  760. if (have_cpuid_p()) {
  761. cpu_detect(c);
  762. get_cpu_vendor(c);
  763. get_cpu_cap(c);
  764. setup_force_cpu_cap(X86_FEATURE_CPUID);
  765. if (this_cpu->c_early_init)
  766. this_cpu->c_early_init(c);
  767. c->cpu_index = 0;
  768. filter_cpuid_features(c, false);
  769. if (this_cpu->c_bsp_init)
  770. this_cpu->c_bsp_init(c);
  771. } else {
  772. identify_cpu_without_cpuid(c);
  773. setup_clear_cpu_cap(X86_FEATURE_CPUID);
  774. }
  775. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  776. fpu__init_system(c);
  777. }
  778. void __init early_cpu_init(void)
  779. {
  780. const struct cpu_dev *const *cdev;
  781. int count = 0;
  782. #ifdef CONFIG_PROCESSOR_SELECT
  783. pr_info("KERNEL supported cpus:\n");
  784. #endif
  785. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  786. const struct cpu_dev *cpudev = *cdev;
  787. if (count >= X86_VENDOR_NUM)
  788. break;
  789. cpu_devs[count] = cpudev;
  790. count++;
  791. #ifdef CONFIG_PROCESSOR_SELECT
  792. {
  793. unsigned int j;
  794. for (j = 0; j < 2; j++) {
  795. if (!cpudev->c_ident[j])
  796. continue;
  797. pr_info(" %s %s\n", cpudev->c_vendor,
  798. cpudev->c_ident[j]);
  799. }
  800. }
  801. #endif
  802. }
  803. early_identify_cpu(&boot_cpu_data);
  804. }
  805. /*
  806. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  807. * unfortunately, that's not true in practice because of early VIA
  808. * chips and (more importantly) broken virtualizers that are not easy
  809. * to detect. In the latter case it doesn't even *fail* reliably, so
  810. * probing for it doesn't even work. Disable it completely on 32-bit
  811. * unless we can find a reliable way to detect all the broken cases.
  812. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  813. */
  814. static void detect_nopl(struct cpuinfo_x86 *c)
  815. {
  816. #ifdef CONFIG_X86_32
  817. clear_cpu_cap(c, X86_FEATURE_NOPL);
  818. #else
  819. set_cpu_cap(c, X86_FEATURE_NOPL);
  820. #endif
  821. }
  822. static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
  823. {
  824. #ifdef CONFIG_X86_64
  825. /*
  826. * Empirically, writing zero to a segment selector on AMD does
  827. * not clear the base, whereas writing zero to a segment
  828. * selector on Intel does clear the base. Intel's behavior
  829. * allows slightly faster context switches in the common case
  830. * where GS is unused by the prev and next threads.
  831. *
  832. * Since neither vendor documents this anywhere that I can see,
  833. * detect it directly instead of hardcoding the choice by
  834. * vendor.
  835. *
  836. * I've designated AMD's behavior as the "bug" because it's
  837. * counterintuitive and less friendly.
  838. */
  839. unsigned long old_base, tmp;
  840. rdmsrl(MSR_FS_BASE, old_base);
  841. wrmsrl(MSR_FS_BASE, 1);
  842. loadsegment(fs, 0);
  843. rdmsrl(MSR_FS_BASE, tmp);
  844. if (tmp != 0)
  845. set_cpu_bug(c, X86_BUG_NULL_SEG);
  846. wrmsrl(MSR_FS_BASE, old_base);
  847. #endif
  848. }
  849. static void generic_identify(struct cpuinfo_x86 *c)
  850. {
  851. c->extended_cpuid_level = 0;
  852. if (!have_cpuid_p())
  853. identify_cpu_without_cpuid(c);
  854. /* cyrix could have cpuid enabled via c_identify()*/
  855. if (!have_cpuid_p())
  856. return;
  857. cpu_detect(c);
  858. get_cpu_vendor(c);
  859. get_cpu_cap(c);
  860. if (c->cpuid_level >= 0x00000001) {
  861. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  862. #ifdef CONFIG_X86_32
  863. # ifdef CONFIG_SMP
  864. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  865. # else
  866. c->apicid = c->initial_apicid;
  867. # endif
  868. #endif
  869. c->phys_proc_id = c->initial_apicid;
  870. }
  871. get_model_name(c); /* Default name */
  872. detect_nopl(c);
  873. detect_null_seg_behavior(c);
  874. /*
  875. * ESPFIX is a strange bug. All real CPUs have it. Paravirt
  876. * systems that run Linux at CPL > 0 may or may not have the
  877. * issue, but, even if they have the issue, there's absolutely
  878. * nothing we can do about it because we can't use the real IRET
  879. * instruction.
  880. *
  881. * NB: For the time being, only 32-bit kernels support
  882. * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
  883. * whether to apply espfix using paravirt hooks. If any
  884. * non-paravirt system ever shows up that does *not* have the
  885. * ESPFIX issue, we can change this.
  886. */
  887. #ifdef CONFIG_X86_32
  888. # ifdef CONFIG_PARAVIRT
  889. do {
  890. extern void native_iret(void);
  891. if (pv_cpu_ops.iret == native_iret)
  892. set_cpu_bug(c, X86_BUG_ESPFIX);
  893. } while (0);
  894. # else
  895. set_cpu_bug(c, X86_BUG_ESPFIX);
  896. # endif
  897. #endif
  898. }
  899. static void x86_init_cache_qos(struct cpuinfo_x86 *c)
  900. {
  901. /*
  902. * The heavy lifting of max_rmid and cache_occ_scale are handled
  903. * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
  904. * in case CQM bits really aren't there in this CPU.
  905. */
  906. if (c != &boot_cpu_data) {
  907. boot_cpu_data.x86_cache_max_rmid =
  908. min(boot_cpu_data.x86_cache_max_rmid,
  909. c->x86_cache_max_rmid);
  910. }
  911. }
  912. /*
  913. * Validate that ACPI/mptables have the same information about the
  914. * effective APIC id and update the package map.
  915. */
  916. static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
  917. {
  918. #ifdef CONFIG_SMP
  919. unsigned int apicid, cpu = smp_processor_id();
  920. apicid = apic->cpu_present_to_apicid(cpu);
  921. if (apicid != c->apicid) {
  922. pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
  923. cpu, apicid, c->initial_apicid);
  924. }
  925. BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
  926. #else
  927. c->logical_proc_id = 0;
  928. #endif
  929. }
  930. /*
  931. * This does the hard work of actually picking apart the CPU stuff...
  932. */
  933. static void identify_cpu(struct cpuinfo_x86 *c)
  934. {
  935. int i;
  936. c->loops_per_jiffy = loops_per_jiffy;
  937. c->x86_cache_size = -1;
  938. c->x86_vendor = X86_VENDOR_UNKNOWN;
  939. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  940. c->x86_vendor_id[0] = '\0'; /* Unset */
  941. c->x86_model_id[0] = '\0'; /* Unset */
  942. c->x86_max_cores = 1;
  943. c->x86_coreid_bits = 0;
  944. c->cu_id = 0xff;
  945. #ifdef CONFIG_X86_64
  946. c->x86_clflush_size = 64;
  947. c->x86_phys_bits = 36;
  948. c->x86_virt_bits = 48;
  949. #else
  950. c->cpuid_level = -1; /* CPUID not detected */
  951. c->x86_clflush_size = 32;
  952. c->x86_phys_bits = 32;
  953. c->x86_virt_bits = 32;
  954. #endif
  955. c->x86_cache_alignment = c->x86_clflush_size;
  956. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  957. generic_identify(c);
  958. if (this_cpu->c_identify)
  959. this_cpu->c_identify(c);
  960. /* Clear/Set all flags overridden by options, after probe */
  961. apply_forced_caps(c);
  962. #ifdef CONFIG_X86_64
  963. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  964. #endif
  965. /*
  966. * Vendor-specific initialization. In this section we
  967. * canonicalize the feature flags, meaning if there are
  968. * features a certain CPU supports which CPUID doesn't
  969. * tell us, CPUID claiming incorrect flags, or other bugs,
  970. * we handle them here.
  971. *
  972. * At the end of this section, c->x86_capability better
  973. * indicate the features this CPU genuinely supports!
  974. */
  975. if (this_cpu->c_init)
  976. this_cpu->c_init(c);
  977. /* Disable the PN if appropriate */
  978. squash_the_stupid_serial_number(c);
  979. /* Set up SMEP/SMAP */
  980. setup_smep(c);
  981. setup_smap(c);
  982. /* Set up PCID */
  983. setup_pcid(c);
  984. /*
  985. * The vendor-specific functions might have changed features.
  986. * Now we do "generic changes."
  987. */
  988. /* Filter out anything that depends on CPUID levels we don't have */
  989. filter_cpuid_features(c, true);
  990. /* If the model name is still unset, do table lookup. */
  991. if (!c->x86_model_id[0]) {
  992. const char *p;
  993. p = table_lookup_model(c);
  994. if (p)
  995. strcpy(c->x86_model_id, p);
  996. else
  997. /* Last resort... */
  998. sprintf(c->x86_model_id, "%02x/%02x",
  999. c->x86, c->x86_model);
  1000. }
  1001. #ifdef CONFIG_X86_64
  1002. detect_ht(c);
  1003. #endif
  1004. x86_init_rdrand(c);
  1005. x86_init_cache_qos(c);
  1006. setup_pku(c);
  1007. /*
  1008. * Clear/Set all flags overridden by options, need do it
  1009. * before following smp all cpus cap AND.
  1010. */
  1011. apply_forced_caps(c);
  1012. /*
  1013. * On SMP, boot_cpu_data holds the common feature set between
  1014. * all CPUs; so make sure that we indicate which features are
  1015. * common between the CPUs. The first time this routine gets
  1016. * executed, c == &boot_cpu_data.
  1017. */
  1018. if (c != &boot_cpu_data) {
  1019. /* AND the already accumulated flags with these */
  1020. for (i = 0; i < NCAPINTS; i++)
  1021. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  1022. /* OR, i.e. replicate the bug flags */
  1023. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  1024. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  1025. }
  1026. /* Init Machine Check Exception if available. */
  1027. mcheck_cpu_init(c);
  1028. select_idle_routine(c);
  1029. #ifdef CONFIG_NUMA
  1030. numa_add_cpu(smp_processor_id());
  1031. #endif
  1032. }
  1033. /*
  1034. * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
  1035. * on 32-bit kernels:
  1036. */
  1037. #ifdef CONFIG_X86_32
  1038. void enable_sep_cpu(void)
  1039. {
  1040. struct tss_struct *tss;
  1041. int cpu;
  1042. if (!boot_cpu_has(X86_FEATURE_SEP))
  1043. return;
  1044. cpu = get_cpu();
  1045. tss = &per_cpu(cpu_tss, cpu);
  1046. /*
  1047. * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
  1048. * see the big comment in struct x86_hw_tss's definition.
  1049. */
  1050. tss->x86_tss.ss1 = __KERNEL_CS;
  1051. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  1052. wrmsr(MSR_IA32_SYSENTER_ESP,
  1053. (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
  1054. 0);
  1055. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
  1056. put_cpu();
  1057. }
  1058. #endif
  1059. void __init identify_boot_cpu(void)
  1060. {
  1061. identify_cpu(&boot_cpu_data);
  1062. #ifdef CONFIG_X86_32
  1063. sysenter_setup();
  1064. enable_sep_cpu();
  1065. #endif
  1066. cpu_detect_tlb(&boot_cpu_data);
  1067. }
  1068. void identify_secondary_cpu(struct cpuinfo_x86 *c)
  1069. {
  1070. BUG_ON(c == &boot_cpu_data);
  1071. identify_cpu(c);
  1072. #ifdef CONFIG_X86_32
  1073. enable_sep_cpu();
  1074. #endif
  1075. mtrr_ap_init();
  1076. validate_apic_and_package_id(c);
  1077. }
  1078. static __init int setup_noclflush(char *arg)
  1079. {
  1080. setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
  1081. setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
  1082. return 1;
  1083. }
  1084. __setup("noclflush", setup_noclflush);
  1085. void print_cpu_info(struct cpuinfo_x86 *c)
  1086. {
  1087. const char *vendor = NULL;
  1088. if (c->x86_vendor < X86_VENDOR_NUM) {
  1089. vendor = this_cpu->c_vendor;
  1090. } else {
  1091. if (c->cpuid_level >= 0)
  1092. vendor = c->x86_vendor_id;
  1093. }
  1094. if (vendor && !strstr(c->x86_model_id, vendor))
  1095. pr_cont("%s ", vendor);
  1096. if (c->x86_model_id[0])
  1097. pr_cont("%s", c->x86_model_id);
  1098. else
  1099. pr_cont("%d86", c->x86);
  1100. pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
  1101. if (c->x86_mask || c->cpuid_level >= 0)
  1102. pr_cont(", stepping: 0x%x)\n", c->x86_mask);
  1103. else
  1104. pr_cont(")\n");
  1105. }
  1106. static __init int setup_disablecpuid(char *arg)
  1107. {
  1108. int bit;
  1109. if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
  1110. setup_clear_cpu_cap(bit);
  1111. else
  1112. return 0;
  1113. return 1;
  1114. }
  1115. __setup("clearcpuid=", setup_disablecpuid);
  1116. #ifdef CONFIG_X86_64
  1117. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  1118. irq_stack_union) __aligned(PAGE_SIZE) __visible;
  1119. /*
  1120. * The following percpu variables are hot. Align current_task to
  1121. * cacheline size such that they fall in the same cacheline.
  1122. */
  1123. DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
  1124. &init_task;
  1125. EXPORT_PER_CPU_SYMBOL(current_task);
  1126. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  1127. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
  1128. DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
  1129. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1130. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1131. /*
  1132. * Special IST stacks which the CPU switches to when it calls
  1133. * an IST-marked descriptor entry. Up to 7 stacks (hardware
  1134. * limit), all of them are 4K, except the debug stack which
  1135. * is 8K.
  1136. */
  1137. static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
  1138. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  1139. [DEBUG_STACK - 1] = DEBUG_STKSZ
  1140. };
  1141. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  1142. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
  1143. /* May not be marked __init: used by software suspend */
  1144. void syscall_init(void)
  1145. {
  1146. wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
  1147. wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
  1148. #ifdef CONFIG_IA32_EMULATION
  1149. wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
  1150. /*
  1151. * This only works on Intel CPUs.
  1152. * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
  1153. * This does not cause SYSENTER to jump to the wrong location, because
  1154. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  1155. */
  1156. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  1157. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1158. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
  1159. #else
  1160. wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
  1161. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
  1162. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1163. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
  1164. #endif
  1165. /* Flags to clear on syscall */
  1166. wrmsrl(MSR_SYSCALL_MASK,
  1167. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
  1168. X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
  1169. }
  1170. /*
  1171. * Copies of the original ist values from the tss are only accessed during
  1172. * debugging, no special alignment required.
  1173. */
  1174. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  1175. static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
  1176. DEFINE_PER_CPU(int, debug_stack_usage);
  1177. int is_debug_stack(unsigned long addr)
  1178. {
  1179. return __this_cpu_read(debug_stack_usage) ||
  1180. (addr <= __this_cpu_read(debug_stack_addr) &&
  1181. addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
  1182. }
  1183. NOKPROBE_SYMBOL(is_debug_stack);
  1184. DEFINE_PER_CPU(u32, debug_idt_ctr);
  1185. void debug_stack_set_zero(void)
  1186. {
  1187. this_cpu_inc(debug_idt_ctr);
  1188. load_current_idt();
  1189. }
  1190. NOKPROBE_SYMBOL(debug_stack_set_zero);
  1191. void debug_stack_reset(void)
  1192. {
  1193. if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
  1194. return;
  1195. if (this_cpu_dec_return(debug_idt_ctr) == 0)
  1196. load_current_idt();
  1197. }
  1198. NOKPROBE_SYMBOL(debug_stack_reset);
  1199. #else /* CONFIG_X86_64 */
  1200. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  1201. EXPORT_PER_CPU_SYMBOL(current_task);
  1202. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1203. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1204. /*
  1205. * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
  1206. * the top of the kernel stack. Use an extra percpu variable to track the
  1207. * top of the kernel stack directly.
  1208. */
  1209. DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
  1210. (unsigned long)&init_thread_union + THREAD_SIZE;
  1211. EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
  1212. #ifdef CONFIG_CC_STACKPROTECTOR
  1213. DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
  1214. #endif
  1215. #endif /* CONFIG_X86_64 */
  1216. /*
  1217. * Clear all 6 debug registers:
  1218. */
  1219. static void clear_all_debug_regs(void)
  1220. {
  1221. int i;
  1222. for (i = 0; i < 8; i++) {
  1223. /* Ignore db4, db5 */
  1224. if ((i == 4) || (i == 5))
  1225. continue;
  1226. set_debugreg(0, i);
  1227. }
  1228. }
  1229. #ifdef CONFIG_KGDB
  1230. /*
  1231. * Restore debug regs if using kgdbwait and you have a kernel debugger
  1232. * connection established.
  1233. */
  1234. static void dbg_restore_debug_regs(void)
  1235. {
  1236. if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  1237. arch_kgdb_ops.correct_hw_break();
  1238. }
  1239. #else /* ! CONFIG_KGDB */
  1240. #define dbg_restore_debug_regs()
  1241. #endif /* ! CONFIG_KGDB */
  1242. static void wait_for_master_cpu(int cpu)
  1243. {
  1244. #ifdef CONFIG_SMP
  1245. /*
  1246. * wait for ACK from master CPU before continuing
  1247. * with AP initialization
  1248. */
  1249. WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
  1250. while (!cpumask_test_cpu(cpu, cpu_callout_mask))
  1251. cpu_relax();
  1252. #endif
  1253. }
  1254. /*
  1255. * cpu_init() initializes state that is per-CPU. Some data is already
  1256. * initialized (naturally) in the bootstrap process, such as the GDT
  1257. * and IDT. We reload them nevertheless, this function acts as a
  1258. * 'CPU state barrier', nothing should get across.
  1259. * A lot of state is already set up in PDA init for 64 bit
  1260. */
  1261. #ifdef CONFIG_X86_64
  1262. void cpu_init(void)
  1263. {
  1264. struct orig_ist *oist;
  1265. struct task_struct *me;
  1266. struct tss_struct *t;
  1267. unsigned long v;
  1268. int cpu = raw_smp_processor_id();
  1269. int i;
  1270. wait_for_master_cpu(cpu);
  1271. /*
  1272. * Initialize the CR4 shadow before doing anything that could
  1273. * try to read it.
  1274. */
  1275. cr4_init_shadow();
  1276. if (cpu)
  1277. load_ucode_ap();
  1278. t = &per_cpu(cpu_tss, cpu);
  1279. oist = &per_cpu(orig_ist, cpu);
  1280. #ifdef CONFIG_NUMA
  1281. if (this_cpu_read(numa_node) == 0 &&
  1282. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1283. set_numa_node(early_cpu_to_node(cpu));
  1284. #endif
  1285. me = current;
  1286. pr_debug("Initializing CPU#%d\n", cpu);
  1287. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1288. /*
  1289. * Initialize the per-CPU GDT with the boot GDT,
  1290. * and set up the GDT descriptor:
  1291. */
  1292. switch_to_new_gdt(cpu);
  1293. loadsegment(fs, 0);
  1294. load_current_idt();
  1295. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1296. syscall_init();
  1297. wrmsrl(MSR_FS_BASE, 0);
  1298. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1299. barrier();
  1300. x86_configure_nx();
  1301. x2apic_setup();
  1302. /*
  1303. * set up and load the per-CPU TSS
  1304. */
  1305. if (!oist->ist[0]) {
  1306. char *estacks = per_cpu(exception_stacks, cpu);
  1307. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  1308. estacks += exception_stack_sizes[v];
  1309. oist->ist[v] = t->x86_tss.ist[v] =
  1310. (unsigned long)estacks;
  1311. if (v == DEBUG_STACK-1)
  1312. per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
  1313. }
  1314. }
  1315. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1316. /*
  1317. * <= is required because the CPU will access up to
  1318. * 8 bits beyond the end of the IO permission bitmap.
  1319. */
  1320. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  1321. t->io_bitmap[i] = ~0UL;
  1322. mmgrab(&init_mm);
  1323. me->active_mm = &init_mm;
  1324. BUG_ON(me->mm);
  1325. enter_lazy_tlb(&init_mm, me);
  1326. load_sp0(t, &current->thread);
  1327. set_tss_desc(cpu, t);
  1328. load_TR_desc();
  1329. load_mm_ldt(&init_mm);
  1330. clear_all_debug_regs();
  1331. dbg_restore_debug_regs();
  1332. fpu__init_cpu();
  1333. if (is_uv_system())
  1334. uv_cpu_init();
  1335. setup_fixmap_gdt(cpu);
  1336. load_fixmap_gdt(cpu);
  1337. }
  1338. #else
  1339. void cpu_init(void)
  1340. {
  1341. int cpu = smp_processor_id();
  1342. struct task_struct *curr = current;
  1343. struct tss_struct *t = &per_cpu(cpu_tss, cpu);
  1344. struct thread_struct *thread = &curr->thread;
  1345. wait_for_master_cpu(cpu);
  1346. /*
  1347. * Initialize the CR4 shadow before doing anything that could
  1348. * try to read it.
  1349. */
  1350. cr4_init_shadow();
  1351. show_ucode_info_early();
  1352. pr_info("Initializing CPU#%d\n", cpu);
  1353. if (cpu_feature_enabled(X86_FEATURE_VME) ||
  1354. boot_cpu_has(X86_FEATURE_TSC) ||
  1355. boot_cpu_has(X86_FEATURE_DE))
  1356. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1357. load_current_idt();
  1358. switch_to_new_gdt(cpu);
  1359. /*
  1360. * Set up and load the per-CPU TSS and LDT
  1361. */
  1362. mmgrab(&init_mm);
  1363. curr->active_mm = &init_mm;
  1364. BUG_ON(curr->mm);
  1365. enter_lazy_tlb(&init_mm, curr);
  1366. load_sp0(t, thread);
  1367. set_tss_desc(cpu, t);
  1368. load_TR_desc();
  1369. load_mm_ldt(&init_mm);
  1370. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1371. #ifdef CONFIG_DOUBLEFAULT
  1372. /* Set up doublefault TSS pointer in the GDT */
  1373. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  1374. #endif
  1375. clear_all_debug_regs();
  1376. dbg_restore_debug_regs();
  1377. fpu__init_cpu();
  1378. setup_fixmap_gdt(cpu);
  1379. load_fixmap_gdt(cpu);
  1380. }
  1381. #endif
  1382. static void bsp_resume(void)
  1383. {
  1384. if (this_cpu->c_bsp_resume)
  1385. this_cpu->c_bsp_resume(&boot_cpu_data);
  1386. }
  1387. static struct syscore_ops cpu_syscore_ops = {
  1388. .resume = bsp_resume,
  1389. };
  1390. static int __init init_cpu_syscore(void)
  1391. {
  1392. register_syscore_ops(&cpu_syscore_ops);
  1393. return 0;
  1394. }
  1395. core_initcall(init_cpu_syscore);