cpu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/suspend.h>
  20. #include <asm/fpu/internal.h>
  21. #include <asm/debugreg.h>
  22. #include <asm/cpu.h>
  23. #include <asm/mmu_context.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_mm_ldt(current->active_mm); /* This does lldt */
  143. fpu__resume_cpu();
  144. }
  145. /**
  146. * __restore_processor_state - restore the contents of CPU registers saved
  147. * by __save_processor_state()
  148. * @ctxt - structure to load the registers contents from
  149. */
  150. static void notrace __restore_processor_state(struct saved_context *ctxt)
  151. {
  152. if (ctxt->misc_enable_saved)
  153. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  154. /*
  155. * control registers
  156. */
  157. /* cr4 was introduced in the Pentium CPU */
  158. #ifdef CONFIG_X86_32
  159. if (ctxt->cr4)
  160. __write_cr4(ctxt->cr4);
  161. #else
  162. /* CONFIG X86_64 */
  163. wrmsrl(MSR_EFER, ctxt->efer);
  164. write_cr8(ctxt->cr8);
  165. __write_cr4(ctxt->cr4);
  166. #endif
  167. write_cr3(ctxt->cr3);
  168. write_cr2(ctxt->cr2);
  169. write_cr0(ctxt->cr0);
  170. /*
  171. * now restore the descriptor tables to their proper values
  172. * ltr is done i fix_processor_context().
  173. */
  174. #ifdef CONFIG_X86_32
  175. load_idt(&ctxt->idt);
  176. #else
  177. /* CONFIG_X86_64 */
  178. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  179. #endif
  180. /*
  181. * segment registers
  182. */
  183. #ifdef CONFIG_X86_32
  184. loadsegment(es, ctxt->es);
  185. loadsegment(fs, ctxt->fs);
  186. loadsegment(gs, ctxt->gs);
  187. loadsegment(ss, ctxt->ss);
  188. /*
  189. * sysenter MSRs
  190. */
  191. if (boot_cpu_has(X86_FEATURE_SEP))
  192. enable_sep_cpu();
  193. #else
  194. /* CONFIG_X86_64 */
  195. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  196. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  197. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  198. load_gs_index(ctxt->gs);
  199. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  200. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  201. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  202. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  203. #endif
  204. fix_processor_context();
  205. do_fpu_end();
  206. x86_platform.restore_sched_clock_state();
  207. mtrr_bp_restore();
  208. perf_restore_debug_store();
  209. }
  210. /* Needed by apm.c */
  211. void notrace restore_processor_state(void)
  212. {
  213. __restore_processor_state(&saved_context);
  214. }
  215. #ifdef CONFIG_X86_32
  216. EXPORT_SYMBOL(restore_processor_state);
  217. #endif
  218. /*
  219. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  220. * is disabled already. So it's unnessary to handle race condition between
  221. * cpumask query and cpu hotplug.
  222. */
  223. static int bsp_check(void)
  224. {
  225. if (cpumask_first(cpu_online_mask) != 0) {
  226. pr_warn("CPU0 is offline.\n");
  227. return -ENODEV;
  228. }
  229. return 0;
  230. }
  231. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  232. void *ptr)
  233. {
  234. int ret = 0;
  235. switch (action) {
  236. case PM_SUSPEND_PREPARE:
  237. case PM_HIBERNATION_PREPARE:
  238. ret = bsp_check();
  239. break;
  240. #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
  241. case PM_RESTORE_PREPARE:
  242. /*
  243. * When system resumes from hibernation, online CPU0 because
  244. * 1. it's required for resume and
  245. * 2. the CPU was online before hibernation
  246. */
  247. if (!cpu_online(0))
  248. _debug_hotplug_cpu(0, 1);
  249. break;
  250. case PM_POST_RESTORE:
  251. /*
  252. * When a resume really happens, this code won't be called.
  253. *
  254. * This code is called only when user space hibernation software
  255. * prepares for snapshot device during boot time. So we just
  256. * call _debug_hotplug_cpu() to restore to CPU0's state prior to
  257. * preparing the snapshot device.
  258. *
  259. * This works for normal boot case in our CPU0 hotplug debug
  260. * mode, i.e. CPU0 is offline and user mode hibernation
  261. * software initializes during boot time.
  262. *
  263. * If CPU0 is online and user application accesses snapshot
  264. * device after boot time, this will offline CPU0 and user may
  265. * see different CPU0 state before and after accessing
  266. * the snapshot device. But hopefully this is not a case when
  267. * user debugging CPU0 hotplug. Even if users hit this case,
  268. * they can easily online CPU0 back.
  269. *
  270. * To simplify this debug code, we only consider normal boot
  271. * case. Otherwise we need to remember CPU0's state and restore
  272. * to that state and resolve racy conditions etc.
  273. */
  274. _debug_hotplug_cpu(0, 0);
  275. break;
  276. #endif
  277. default:
  278. break;
  279. }
  280. return notifier_from_errno(ret);
  281. }
  282. static int __init bsp_pm_check_init(void)
  283. {
  284. /*
  285. * Set this bsp_pm_callback as lower priority than
  286. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  287. * earlier to disable cpu hotplug before bsp online check.
  288. */
  289. pm_notifier(bsp_pm_callback, -INT_MAX);
  290. return 0;
  291. }
  292. core_initcall(bsp_pm_check_init);