cpu.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Suspend support specific for i386/x86-64.
  3. *
  4. * Distribute under GPLv2
  5. *
  6. * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
  7. * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
  8. * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
  9. */
  10. #include <linux/suspend.h>
  11. #include <linux/export.h>
  12. #include <linux/smp.h>
  13. #include <linux/perf_event.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/proto.h>
  16. #include <asm/mtrr.h>
  17. #include <asm/page.h>
  18. #include <asm/mce.h>
  19. #include <asm/xcr.h>
  20. #include <asm/suspend.h>
  21. #include <asm/debugreg.h>
  22. #include <asm/fpu-internal.h> /* pcntxt_mask */
  23. #include <asm/cpu.h>
  24. #ifdef CONFIG_X86_32
  25. __visible unsigned long saved_context_ebx;
  26. __visible unsigned long saved_context_esp, saved_context_ebp;
  27. __visible unsigned long saved_context_esi, saved_context_edi;
  28. __visible unsigned long saved_context_eflags;
  29. #endif
  30. struct saved_context saved_context;
  31. /**
  32. * __save_processor_state - save CPU registers before creating a
  33. * hibernation image and before restoring the memory state from it
  34. * @ctxt - structure to store the registers contents in
  35. *
  36. * NOTE: If there is a CPU register the modification of which by the
  37. * boot kernel (ie. the kernel used for loading the hibernation image)
  38. * might affect the operations of the restored target kernel (ie. the one
  39. * saved in the hibernation image), then its contents must be saved by this
  40. * function. In other words, if kernel A is hibernated and different
  41. * kernel B is used for loading the hibernation image into memory, the
  42. * kernel A's __save_processor_state() function must save all registers
  43. * needed by kernel A, so that it can operate correctly after the resume
  44. * regardless of what kernel B does in the meantime.
  45. */
  46. static void __save_processor_state(struct saved_context *ctxt)
  47. {
  48. #ifdef CONFIG_X86_32
  49. mtrr_save_fixed_ranges(NULL);
  50. #endif
  51. kernel_fpu_begin();
  52. /*
  53. * descriptor tables
  54. */
  55. #ifdef CONFIG_X86_32
  56. store_idt(&ctxt->idt);
  57. #else
  58. /* CONFIG_X86_64 */
  59. store_idt((struct desc_ptr *)&ctxt->idt_limit);
  60. #endif
  61. /*
  62. * We save it here, but restore it only in the hibernate case.
  63. * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
  64. * mode in "secondary_startup_64". In 32-bit mode it is done via
  65. * 'pmode_gdt' in wakeup_start.
  66. */
  67. ctxt->gdt_desc.size = GDT_SIZE - 1;
  68. ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_table(smp_processor_id());
  69. store_tr(ctxt->tr);
  70. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  71. /*
  72. * segment registers
  73. */
  74. #ifdef CONFIG_X86_32
  75. savesegment(es, ctxt->es);
  76. savesegment(fs, ctxt->fs);
  77. savesegment(gs, ctxt->gs);
  78. savesegment(ss, ctxt->ss);
  79. #else
  80. /* CONFIG_X86_64 */
  81. asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
  82. asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
  83. asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
  84. asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
  85. asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
  86. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  87. rdmsrl(MSR_GS_BASE, ctxt->gs_base);
  88. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  89. mtrr_save_fixed_ranges(NULL);
  90. rdmsrl(MSR_EFER, ctxt->efer);
  91. #endif
  92. /*
  93. * control registers
  94. */
  95. ctxt->cr0 = read_cr0();
  96. ctxt->cr2 = read_cr2();
  97. ctxt->cr3 = read_cr3();
  98. ctxt->cr4 = __read_cr4_safe();
  99. #ifdef CONFIG_X86_64
  100. ctxt->cr8 = read_cr8();
  101. #endif
  102. ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
  103. &ctxt->misc_enable);
  104. }
  105. /* Needed by apm.c */
  106. void save_processor_state(void)
  107. {
  108. __save_processor_state(&saved_context);
  109. x86_platform.save_sched_clock_state();
  110. }
  111. #ifdef CONFIG_X86_32
  112. EXPORT_SYMBOL(save_processor_state);
  113. #endif
  114. static void do_fpu_end(void)
  115. {
  116. /*
  117. * Restore FPU regs if necessary.
  118. */
  119. kernel_fpu_end();
  120. }
  121. static void fix_processor_context(void)
  122. {
  123. int cpu = smp_processor_id();
  124. struct tss_struct *t = &per_cpu(cpu_tss, cpu);
  125. #ifdef CONFIG_X86_64
  126. struct desc_struct *desc = get_cpu_gdt_table(cpu);
  127. tss_desc tss;
  128. #endif
  129. set_tss_desc(cpu, t); /*
  130. * This just modifies memory; should not be
  131. * necessary. But... This is necessary, because
  132. * 386 hardware has concept of busy TSS or some
  133. * similar stupidity.
  134. */
  135. #ifdef CONFIG_X86_64
  136. memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
  137. tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
  138. write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
  139. syscall_init(); /* This sets MSR_*STAR and related */
  140. #endif
  141. load_TR_desc(); /* This does ltr */
  142. load_LDT(&current->active_mm->context); /* This does lldt */
  143. }
  144. /**
  145. * __restore_processor_state - restore the contents of CPU registers saved
  146. * by __save_processor_state()
  147. * @ctxt - structure to load the registers contents from
  148. */
  149. static void notrace __restore_processor_state(struct saved_context *ctxt)
  150. {
  151. if (ctxt->misc_enable_saved)
  152. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  153. /*
  154. * control registers
  155. */
  156. /* cr4 was introduced in the Pentium CPU */
  157. #ifdef CONFIG_X86_32
  158. if (ctxt->cr4)
  159. __write_cr4(ctxt->cr4);
  160. #else
  161. /* CONFIG X86_64 */
  162. wrmsrl(MSR_EFER, ctxt->efer);
  163. write_cr8(ctxt->cr8);
  164. __write_cr4(ctxt->cr4);
  165. #endif
  166. write_cr3(ctxt->cr3);
  167. write_cr2(ctxt->cr2);
  168. write_cr0(ctxt->cr0);
  169. /*
  170. * now restore the descriptor tables to their proper values
  171. * ltr is done i fix_processor_context().
  172. */
  173. #ifdef CONFIG_X86_32
  174. load_idt(&ctxt->idt);
  175. #else
  176. /* CONFIG_X86_64 */
  177. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  178. #endif
  179. /*
  180. * segment registers
  181. */
  182. #ifdef CONFIG_X86_32
  183. loadsegment(es, ctxt->es);
  184. loadsegment(fs, ctxt->fs);
  185. loadsegment(gs, ctxt->gs);
  186. loadsegment(ss, ctxt->ss);
  187. /*
  188. * sysenter MSRs
  189. */
  190. if (boot_cpu_has(X86_FEATURE_SEP))
  191. enable_sep_cpu();
  192. #else
  193. /* CONFIG_X86_64 */
  194. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  195. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  196. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  197. load_gs_index(ctxt->gs);
  198. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  199. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  200. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  201. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  202. #endif
  203. /*
  204. * restore XCR0 for xsave capable cpu's.
  205. */
  206. if (cpu_has_xsave)
  207. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  208. fix_processor_context();
  209. do_fpu_end();
  210. x86_platform.restore_sched_clock_state();
  211. mtrr_bp_restore();
  212. perf_restore_debug_store();
  213. }
  214. /* Needed by apm.c */
  215. void notrace restore_processor_state(void)
  216. {
  217. __restore_processor_state(&saved_context);
  218. }
  219. #ifdef CONFIG_X86_32
  220. EXPORT_SYMBOL(restore_processor_state);
  221. #endif
  222. /*
  223. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  224. * is disabled already. So it's unnessary to handle race condition between
  225. * cpumask query and cpu hotplug.
  226. */
  227. static int bsp_check(void)
  228. {
  229. if (cpumask_first(cpu_online_mask) != 0) {
  230. pr_warn("CPU0 is offline.\n");
  231. return -ENODEV;
  232. }
  233. return 0;
  234. }
  235. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  236. void *ptr)
  237. {
  238. int ret = 0;
  239. switch (action) {
  240. case PM_SUSPEND_PREPARE:
  241. case PM_HIBERNATION_PREPARE:
  242. ret = bsp_check();
  243. break;
  244. #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
  245. case PM_RESTORE_PREPARE:
  246. /*
  247. * When system resumes from hibernation, online CPU0 because
  248. * 1. it's required for resume and
  249. * 2. the CPU was online before hibernation
  250. */
  251. if (!cpu_online(0))
  252. _debug_hotplug_cpu(0, 1);
  253. break;
  254. case PM_POST_RESTORE:
  255. /*
  256. * When a resume really happens, this code won't be called.
  257. *
  258. * This code is called only when user space hibernation software
  259. * prepares for snapshot device during boot time. So we just
  260. * call _debug_hotplug_cpu() to restore to CPU0's state prior to
  261. * preparing the snapshot device.
  262. *
  263. * This works for normal boot case in our CPU0 hotplug debug
  264. * mode, i.e. CPU0 is offline and user mode hibernation
  265. * software initializes during boot time.
  266. *
  267. * If CPU0 is online and user application accesses snapshot
  268. * device after boot time, this will offline CPU0 and user may
  269. * see different CPU0 state before and after accessing
  270. * the snapshot device. But hopefully this is not a case when
  271. * user debugging CPU0 hotplug. Even if users hit this case,
  272. * they can easily online CPU0 back.
  273. *
  274. * To simplify this debug code, we only consider normal boot
  275. * case. Otherwise we need to remember CPU0's state and restore
  276. * to that state and resolve racy conditions etc.
  277. */
  278. _debug_hotplug_cpu(0, 0);
  279. break;
  280. #endif
  281. default:
  282. break;
  283. }
  284. return notifier_from_errno(ret);
  285. }
  286. static int __init bsp_pm_check_init(void)
  287. {
  288. /*
  289. * Set this bsp_pm_callback as lower priority than
  290. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  291. * earlier to disable cpu hotplug before bsp online check.
  292. */
  293. pm_notifier(bsp_pm_callback, -INT_MAX);
  294. return 0;
  295. }
  296. core_initcall(bsp_pm_check_init);