cpu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Suspend support specific for i386/x86-64.
  4. *
  5. * Copyright (c) 2007 Rafael J. Wysocki <[email protected]>
  6. * Copyright (c) 2002 Pavel Machek <[email protected]>
  7. * Copyright (c) 2001 Patrick Mochel <[email protected]>
  8. */
  9. #include <linux/suspend.h>
  10. #include <linux/export.h>
  11. #include <linux/smp.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/tboot.h>
  14. #include <linux/dmi.h>
  15. #include <linux/pgtable.h>
  16. #include <asm/proto.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/page.h>
  19. #include <asm/mce.h>
  20. #include <asm/suspend.h>
  21. #include <asm/fpu/api.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/cpu.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/cpu_device_id.h>
  26. #include <asm/microcode.h>
  27. #ifdef CONFIG_X86_32
  28. __visible unsigned long saved_context_ebx;
  29. __visible unsigned long saved_context_esp, saved_context_ebp;
  30. __visible unsigned long saved_context_esi, saved_context_edi;
  31. __visible unsigned long saved_context_eflags;
  32. #endif
  33. struct saved_context saved_context;
  34. static void msr_save_context(struct saved_context *ctxt)
  35. {
  36. struct saved_msr *msr = ctxt->saved_msrs.array;
  37. struct saved_msr *end = msr + ctxt->saved_msrs.num;
  38. while (msr < end) {
  39. if (msr->valid)
  40. rdmsrl(msr->info.msr_no, msr->info.reg.q);
  41. msr++;
  42. }
  43. }
  44. static void msr_restore_context(struct saved_context *ctxt)
  45. {
  46. struct saved_msr *msr = ctxt->saved_msrs.array;
  47. struct saved_msr *end = msr + ctxt->saved_msrs.num;
  48. while (msr < end) {
  49. if (msr->valid)
  50. wrmsrl(msr->info.msr_no, msr->info.reg.q);
  51. msr++;
  52. }
  53. }
  54. /**
  55. * __save_processor_state() - Save CPU registers before creating a
  56. * hibernation image and before restoring
  57. * the memory state from it
  58. * @ctxt: Structure to store the registers contents in.
  59. *
  60. * NOTE: If there is a CPU register the modification of which by the
  61. * boot kernel (ie. the kernel used for loading the hibernation image)
  62. * might affect the operations of the restored target kernel (ie. the one
  63. * saved in the hibernation image), then its contents must be saved by this
  64. * function. In other words, if kernel A is hibernated and different
  65. * kernel B is used for loading the hibernation image into memory, the
  66. * kernel A's __save_processor_state() function must save all registers
  67. * needed by kernel A, so that it can operate correctly after the resume
  68. * regardless of what kernel B does in the meantime.
  69. */
  70. static void __save_processor_state(struct saved_context *ctxt)
  71. {
  72. #ifdef CONFIG_X86_32
  73. mtrr_save_fixed_ranges(NULL);
  74. #endif
  75. kernel_fpu_begin();
  76. /*
  77. * descriptor tables
  78. */
  79. store_idt(&ctxt->idt);
  80. /*
  81. * We save it here, but restore it only in the hibernate case.
  82. * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
  83. * mode in "secondary_startup_64". In 32-bit mode it is done via
  84. * 'pmode_gdt' in wakeup_start.
  85. */
  86. ctxt->gdt_desc.size = GDT_SIZE - 1;
  87. ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_rw(smp_processor_id());
  88. store_tr(ctxt->tr);
  89. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  90. /*
  91. * segment registers
  92. */
  93. savesegment(gs, ctxt->gs);
  94. #ifdef CONFIG_X86_64
  95. savesegment(fs, ctxt->fs);
  96. savesegment(ds, ctxt->ds);
  97. savesegment(es, ctxt->es);
  98. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  99. rdmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
  100. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
  101. mtrr_save_fixed_ranges(NULL);
  102. rdmsrl(MSR_EFER, ctxt->efer);
  103. #endif
  104. /*
  105. * control registers
  106. */
  107. ctxt->cr0 = read_cr0();
  108. ctxt->cr2 = read_cr2();
  109. ctxt->cr3 = __read_cr3();
  110. ctxt->cr4 = __read_cr4();
  111. ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
  112. &ctxt->misc_enable);
  113. msr_save_context(ctxt);
  114. }
  115. /* Needed by apm.c */
  116. void save_processor_state(void)
  117. {
  118. __save_processor_state(&saved_context);
  119. x86_platform.save_sched_clock_state();
  120. }
  121. #ifdef CONFIG_X86_32
  122. EXPORT_SYMBOL(save_processor_state);
  123. #endif
  124. static void do_fpu_end(void)
  125. {
  126. /*
  127. * Restore FPU regs if necessary.
  128. */
  129. kernel_fpu_end();
  130. }
  131. static void fix_processor_context(void)
  132. {
  133. int cpu = smp_processor_id();
  134. #ifdef CONFIG_X86_64
  135. struct desc_struct *desc = get_cpu_gdt_rw(cpu);
  136. tss_desc tss;
  137. #endif
  138. /*
  139. * We need to reload TR, which requires that we change the
  140. * GDT entry to indicate "available" first.
  141. *
  142. * XXX: This could probably all be replaced by a call to
  143. * force_reload_TR().
  144. */
  145. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  146. #ifdef CONFIG_X86_64
  147. memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
  148. tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
  149. write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
  150. syscall_init(); /* This sets MSR_*STAR and related */
  151. #else
  152. if (boot_cpu_has(X86_FEATURE_SEP))
  153. enable_sep_cpu();
  154. #endif
  155. load_TR_desc(); /* This does ltr */
  156. load_mm_ldt(current->active_mm); /* This does lldt */
  157. initialize_tlbstate_and_flush();
  158. fpu__resume_cpu();
  159. /* The processor is back on the direct GDT, load back the fixmap */
  160. load_fixmap_gdt(cpu);
  161. }
  162. /**
  163. * __restore_processor_state() - Restore the contents of CPU registers saved
  164. * by __save_processor_state()
  165. * @ctxt: Structure to load the registers contents from.
  166. *
  167. * The asm code that gets us here will have restored a usable GDT, although
  168. * it will be pointing to the wrong alias.
  169. */
  170. static void notrace __restore_processor_state(struct saved_context *ctxt)
  171. {
  172. struct cpuinfo_x86 *c;
  173. if (ctxt->misc_enable_saved)
  174. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  175. /*
  176. * control registers
  177. */
  178. /* cr4 was introduced in the Pentium CPU */
  179. #ifdef CONFIG_X86_32
  180. if (ctxt->cr4)
  181. __write_cr4(ctxt->cr4);
  182. #else
  183. /* CONFIG X86_64 */
  184. wrmsrl(MSR_EFER, ctxt->efer);
  185. __write_cr4(ctxt->cr4);
  186. #endif
  187. write_cr3(ctxt->cr3);
  188. write_cr2(ctxt->cr2);
  189. write_cr0(ctxt->cr0);
  190. /* Restore the IDT. */
  191. load_idt(&ctxt->idt);
  192. /*
  193. * Just in case the asm code got us here with the SS, DS, or ES
  194. * out of sync with the GDT, update them.
  195. */
  196. loadsegment(ss, __KERNEL_DS);
  197. loadsegment(ds, __USER_DS);
  198. loadsegment(es, __USER_DS);
  199. /*
  200. * Restore percpu access. Percpu access can happen in exception
  201. * handlers or in complicated helpers like load_gs_index().
  202. */
  203. #ifdef CONFIG_X86_64
  204. wrmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
  205. #else
  206. loadsegment(fs, __KERNEL_PERCPU);
  207. #endif
  208. /* Restore the TSS, RO GDT, LDT, and usermode-relevant MSRs. */
  209. fix_processor_context();
  210. /*
  211. * Now that we have descriptor tables fully restored and working
  212. * exception handling, restore the usermode segments.
  213. */
  214. #ifdef CONFIG_X86_64
  215. loadsegment(ds, ctxt->es);
  216. loadsegment(es, ctxt->es);
  217. loadsegment(fs, ctxt->fs);
  218. load_gs_index(ctxt->gs);
  219. /*
  220. * Restore FSBASE and GSBASE after restoring the selectors, since
  221. * restoring the selectors clobbers the bases. Keep in mind
  222. * that MSR_KERNEL_GS_BASE is horribly misnamed.
  223. */
  224. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  225. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
  226. #else
  227. loadsegment(gs, ctxt->gs);
  228. #endif
  229. do_fpu_end();
  230. tsc_verify_tsc_adjust(true);
  231. x86_platform.restore_sched_clock_state();
  232. mtrr_bp_restore();
  233. perf_restore_debug_store();
  234. c = &cpu_data(smp_processor_id());
  235. if (cpu_has(c, X86_FEATURE_MSR_IA32_FEAT_CTL))
  236. init_ia32_feat_ctl(c);
  237. microcode_bsp_resume();
  238. /*
  239. * This needs to happen after the microcode has been updated upon resume
  240. * because some of the MSRs are "emulated" in microcode.
  241. */
  242. msr_restore_context(ctxt);
  243. }
  244. /* Needed by apm.c */
  245. void notrace restore_processor_state(void)
  246. {
  247. __restore_processor_state(&saved_context);
  248. }
  249. #ifdef CONFIG_X86_32
  250. EXPORT_SYMBOL(restore_processor_state);
  251. #endif
  252. #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
  253. static void resume_play_dead(void)
  254. {
  255. play_dead_common();
  256. tboot_shutdown(TB_SHUTDOWN_WFS);
  257. hlt_play_dead();
  258. }
  259. int hibernate_resume_nonboot_cpu_disable(void)
  260. {
  261. void (*play_dead)(void) = smp_ops.play_dead;
  262. int ret;
  263. /*
  264. * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
  265. * during hibernate image restoration, because it is likely that the
  266. * monitored address will be actually written to at that time and then
  267. * the "dead" CPU will attempt to execute instructions again, but the
  268. * address in its instruction pointer may not be possible to resolve
  269. * any more at that point (the page tables used by it previously may
  270. * have been overwritten by hibernate image data).
  271. *
  272. * First, make sure that we wake up all the potentially disabled SMT
  273. * threads which have been initially brought up and then put into
  274. * mwait/cpuidle sleep.
  275. * Those will be put to proper (not interfering with hibernation
  276. * resume) sleep afterwards, and the resumed kernel will decide itself
  277. * what to do with them.
  278. */
  279. ret = cpuhp_smt_enable();
  280. if (ret)
  281. return ret;
  282. smp_ops.play_dead = resume_play_dead;
  283. ret = freeze_secondary_cpus(0);
  284. smp_ops.play_dead = play_dead;
  285. return ret;
  286. }
  287. #endif
  288. /*
  289. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  290. * is disabled already. So it's unnecessary to handle race condition between
  291. * cpumask query and cpu hotplug.
  292. */
  293. static int bsp_check(void)
  294. {
  295. if (cpumask_first(cpu_online_mask) != 0) {
  296. pr_warn("CPU0 is offline.\n");
  297. return -ENODEV;
  298. }
  299. return 0;
  300. }
  301. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  302. void *ptr)
  303. {
  304. int ret = 0;
  305. switch (action) {
  306. case PM_SUSPEND_PREPARE:
  307. case PM_HIBERNATION_PREPARE:
  308. ret = bsp_check();
  309. break;
  310. #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
  311. case PM_RESTORE_PREPARE:
  312. /*
  313. * When system resumes from hibernation, online CPU0 because
  314. * 1. it's required for resume and
  315. * 2. the CPU was online before hibernation
  316. */
  317. if (!cpu_online(0))
  318. _debug_hotplug_cpu(0, 1);
  319. break;
  320. case PM_POST_RESTORE:
  321. /*
  322. * When a resume really happens, this code won't be called.
  323. *
  324. * This code is called only when user space hibernation software
  325. * prepares for snapshot device during boot time. So we just
  326. * call _debug_hotplug_cpu() to restore to CPU0's state prior to
  327. * preparing the snapshot device.
  328. *
  329. * This works for normal boot case in our CPU0 hotplug debug
  330. * mode, i.e. CPU0 is offline and user mode hibernation
  331. * software initializes during boot time.
  332. *
  333. * If CPU0 is online and user application accesses snapshot
  334. * device after boot time, this will offline CPU0 and user may
  335. * see different CPU0 state before and after accessing
  336. * the snapshot device. But hopefully this is not a case when
  337. * user debugging CPU0 hotplug. Even if users hit this case,
  338. * they can easily online CPU0 back.
  339. *
  340. * To simplify this debug code, we only consider normal boot
  341. * case. Otherwise we need to remember CPU0's state and restore
  342. * to that state and resolve racy conditions etc.
  343. */
  344. _debug_hotplug_cpu(0, 0);
  345. break;
  346. #endif
  347. default:
  348. break;
  349. }
  350. return notifier_from_errno(ret);
  351. }
  352. static int __init bsp_pm_check_init(void)
  353. {
  354. /*
  355. * Set this bsp_pm_callback as lower priority than
  356. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  357. * earlier to disable cpu hotplug before bsp online check.
  358. */
  359. pm_notifier(bsp_pm_callback, -INT_MAX);
  360. return 0;
  361. }
  362. core_initcall(bsp_pm_check_init);
  363. static int msr_build_context(const u32 *msr_id, const int num)
  364. {
  365. struct saved_msrs *saved_msrs = &saved_context.saved_msrs;
  366. struct saved_msr *msr_array;
  367. int total_num;
  368. int i, j;
  369. total_num = saved_msrs->num + num;
  370. msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
  371. if (!msr_array) {
  372. pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
  373. return -ENOMEM;
  374. }
  375. if (saved_msrs->array) {
  376. /*
  377. * Multiple callbacks can invoke this function, so copy any
  378. * MSR save requests from previous invocations.
  379. */
  380. memcpy(msr_array, saved_msrs->array,
  381. sizeof(struct saved_msr) * saved_msrs->num);
  382. kfree(saved_msrs->array);
  383. }
  384. for (i = saved_msrs->num, j = 0; i < total_num; i++, j++) {
  385. u64 dummy;
  386. msr_array[i].info.msr_no = msr_id[j];
  387. msr_array[i].valid = !rdmsrl_safe(msr_id[j], &dummy);
  388. msr_array[i].info.reg.q = 0;
  389. }
  390. saved_msrs->num = total_num;
  391. saved_msrs->array = msr_array;
  392. return 0;
  393. }
  394. /*
  395. * The following sections are a quirk framework for problematic BIOSen:
  396. * Sometimes MSRs are modified by the BIOSen after suspended to
  397. * RAM, this might cause unexpected behavior after wakeup.
  398. * Thus we save/restore these specified MSRs across suspend/resume
  399. * in order to work around it.
  400. *
  401. * For any further problematic BIOSen/platforms,
  402. * please add your own function similar to msr_initialize_bdw.
  403. */
  404. static int msr_initialize_bdw(const struct dmi_system_id *d)
  405. {
  406. /* Add any extra MSR ids into this array. */
  407. u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
  408. pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
  409. return msr_build_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
  410. }
  411. static const struct dmi_system_id msr_save_dmi_table[] = {
  412. {
  413. .callback = msr_initialize_bdw,
  414. .ident = "BROADWELL BDX_EP",
  415. .matches = {
  416. DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
  417. DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
  418. },
  419. },
  420. {}
  421. };
  422. static int msr_save_cpuid_features(const struct x86_cpu_id *c)
  423. {
  424. u32 cpuid_msr_id[] = {
  425. MSR_AMD64_CPUID_FN_1,
  426. };
  427. pr_info("x86/pm: family %#hx cpu detected, MSR saving is needed during suspending.\n",
  428. c->family);
  429. return msr_build_context(cpuid_msr_id, ARRAY_SIZE(cpuid_msr_id));
  430. }
  431. static const struct x86_cpu_id msr_save_cpu_table[] = {
  432. X86_MATCH_VENDOR_FAM(AMD, 0x15, &msr_save_cpuid_features),
  433. X86_MATCH_VENDOR_FAM(AMD, 0x16, &msr_save_cpuid_features),
  434. {}
  435. };
  436. typedef int (*pm_cpu_match_t)(const struct x86_cpu_id *);
  437. static int pm_cpu_check(const struct x86_cpu_id *c)
  438. {
  439. const struct x86_cpu_id *m;
  440. int ret = 0;
  441. m = x86_match_cpu(msr_save_cpu_table);
  442. if (m) {
  443. pm_cpu_match_t fn;
  444. fn = (pm_cpu_match_t)m->driver_data;
  445. ret = fn(m);
  446. }
  447. return ret;
  448. }
  449. static void pm_save_spec_msr(void)
  450. {
  451. struct msr_enumeration {
  452. u32 msr_no;
  453. u32 feature;
  454. } msr_enum[] = {
  455. { MSR_IA32_SPEC_CTRL, X86_FEATURE_MSR_SPEC_CTRL },
  456. { MSR_IA32_TSX_CTRL, X86_FEATURE_MSR_TSX_CTRL },
  457. { MSR_TSX_FORCE_ABORT, X86_FEATURE_TSX_FORCE_ABORT },
  458. { MSR_IA32_MCU_OPT_CTRL, X86_FEATURE_SRBDS_CTRL },
  459. { MSR_AMD64_LS_CFG, X86_FEATURE_LS_CFG_SSBD },
  460. { MSR_AMD64_DE_CFG, X86_FEATURE_LFENCE_RDTSC },
  461. };
  462. int i;
  463. for (i = 0; i < ARRAY_SIZE(msr_enum); i++) {
  464. if (boot_cpu_has(msr_enum[i].feature))
  465. msr_build_context(&msr_enum[i].msr_no, 1);
  466. }
  467. }
  468. static int pm_check_save_msr(void)
  469. {
  470. dmi_check_system(msr_save_dmi_table);
  471. pm_cpu_check(msr_save_cpu_table);
  472. pm_save_spec_msr();
  473. return 0;
  474. }
  475. device_initcall(pm_check_save_msr);