cpu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. unsigned long saved_context_ebx;
  26. unsigned long saved_context_esp, saved_context_ebp;
  27. unsigned long saved_context_esi, saved_context_edi;
  28. 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. #ifdef CONFIG_X86_32
  99. ctxt->cr4 = read_cr4_safe();
  100. #else
  101. /* CONFIG_X86_64 */
  102. ctxt->cr4 = read_cr4();
  103. ctxt->cr8 = read_cr8();
  104. #endif
  105. ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
  106. &ctxt->misc_enable);
  107. }
  108. /* Needed by apm.c */
  109. void save_processor_state(void)
  110. {
  111. __save_processor_state(&saved_context);
  112. x86_platform.save_sched_clock_state();
  113. }
  114. #ifdef CONFIG_X86_32
  115. EXPORT_SYMBOL(save_processor_state);
  116. #endif
  117. static void do_fpu_end(void)
  118. {
  119. /*
  120. * Restore FPU regs if necessary.
  121. */
  122. kernel_fpu_end();
  123. }
  124. static void fix_processor_context(void)
  125. {
  126. int cpu = smp_processor_id();
  127. struct tss_struct *t = &per_cpu(init_tss, cpu);
  128. #ifdef CONFIG_X86_64
  129. struct desc_struct *desc = get_cpu_gdt_table(cpu);
  130. tss_desc tss;
  131. #endif
  132. set_tss_desc(cpu, t); /*
  133. * This just modifies memory; should not be
  134. * necessary. But... This is necessary, because
  135. * 386 hardware has concept of busy TSS or some
  136. * similar stupidity.
  137. */
  138. #ifdef CONFIG_X86_64
  139. memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
  140. tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
  141. write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
  142. syscall_init(); /* This sets MSR_*STAR and related */
  143. #endif
  144. load_TR_desc(); /* This does ltr */
  145. load_LDT(&current->active_mm->context); /* This does lldt */
  146. }
  147. /**
  148. * __restore_processor_state - restore the contents of CPU registers saved
  149. * by __save_processor_state()
  150. * @ctxt - structure to load the registers contents from
  151. */
  152. static void __restore_processor_state(struct saved_context *ctxt)
  153. {
  154. if (ctxt->misc_enable_saved)
  155. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  156. /*
  157. * control registers
  158. */
  159. /* cr4 was introduced in the Pentium CPU */
  160. #ifdef CONFIG_X86_32
  161. if (ctxt->cr4)
  162. write_cr4(ctxt->cr4);
  163. #else
  164. /* CONFIG X86_64 */
  165. wrmsrl(MSR_EFER, ctxt->efer);
  166. write_cr8(ctxt->cr8);
  167. write_cr4(ctxt->cr4);
  168. #endif
  169. write_cr3(ctxt->cr3);
  170. write_cr2(ctxt->cr2);
  171. write_cr0(ctxt->cr0);
  172. /*
  173. * now restore the descriptor tables to their proper values
  174. * ltr is done i fix_processor_context().
  175. */
  176. #ifdef CONFIG_X86_32
  177. load_idt(&ctxt->idt);
  178. #else
  179. /* CONFIG_X86_64 */
  180. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  181. #endif
  182. /*
  183. * segment registers
  184. */
  185. #ifdef CONFIG_X86_32
  186. loadsegment(es, ctxt->es);
  187. loadsegment(fs, ctxt->fs);
  188. loadsegment(gs, ctxt->gs);
  189. loadsegment(ss, ctxt->ss);
  190. /*
  191. * sysenter MSRs
  192. */
  193. if (boot_cpu_has(X86_FEATURE_SEP))
  194. enable_sep_cpu();
  195. #else
  196. /* CONFIG_X86_64 */
  197. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  198. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  199. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  200. load_gs_index(ctxt->gs);
  201. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  202. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  203. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  204. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  205. #endif
  206. /*
  207. * restore XCR0 for xsave capable cpu's.
  208. */
  209. if (cpu_has_xsave)
  210. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  211. fix_processor_context();
  212. do_fpu_end();
  213. x86_platform.restore_sched_clock_state();
  214. mtrr_bp_restore();
  215. perf_restore_debug_store();
  216. }
  217. /* Needed by apm.c */
  218. void restore_processor_state(void)
  219. {
  220. __restore_processor_state(&saved_context);
  221. }
  222. #ifdef CONFIG_X86_32
  223. EXPORT_SYMBOL(restore_processor_state);
  224. #endif
  225. /*
  226. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  227. * is disabled already. So it's unnessary to handle race condition between
  228. * cpumask query and cpu hotplug.
  229. */
  230. static int bsp_check(void)
  231. {
  232. if (cpumask_first(cpu_online_mask) != 0) {
  233. pr_warn("CPU0 is offline.\n");
  234. return -ENODEV;
  235. }
  236. return 0;
  237. }
  238. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  239. void *ptr)
  240. {
  241. int ret = 0;
  242. switch (action) {
  243. case PM_SUSPEND_PREPARE:
  244. case PM_HIBERNATION_PREPARE:
  245. ret = bsp_check();
  246. break;
  247. #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
  248. case PM_RESTORE_PREPARE:
  249. /*
  250. * When system resumes from hibernation, online CPU0 because
  251. * 1. it's required for resume and
  252. * 2. the CPU was online before hibernation
  253. */
  254. if (!cpu_online(0))
  255. _debug_hotplug_cpu(0, 1);
  256. break;
  257. case PM_POST_RESTORE:
  258. /*
  259. * When a resume really happens, this code won't be called.
  260. *
  261. * This code is called only when user space hibernation software
  262. * prepares for snapshot device during boot time. So we just
  263. * call _debug_hotplug_cpu() to restore to CPU0's state prior to
  264. * preparing the snapshot device.
  265. *
  266. * This works for normal boot case in our CPU0 hotplug debug
  267. * mode, i.e. CPU0 is offline and user mode hibernation
  268. * software initializes during boot time.
  269. *
  270. * If CPU0 is online and user application accesses snapshot
  271. * device after boot time, this will offline CPU0 and user may
  272. * see different CPU0 state before and after accessing
  273. * the snapshot device. But hopefully this is not a case when
  274. * user debugging CPU0 hotplug. Even if users hit this case,
  275. * they can easily online CPU0 back.
  276. *
  277. * To simplify this debug code, we only consider normal boot
  278. * case. Otherwise we need to remember CPU0's state and restore
  279. * to that state and resolve racy conditions etc.
  280. */
  281. _debug_hotplug_cpu(0, 0);
  282. break;
  283. #endif
  284. default:
  285. break;
  286. }
  287. return notifier_from_errno(ret);
  288. }
  289. static int __init bsp_pm_check_init(void)
  290. {
  291. /*
  292. * Set this bsp_pm_callback as lower priority than
  293. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  294. * earlier to disable cpu hotplug before bsp online check.
  295. */
  296. pm_notifier(bsp_pm_callback, -INT_MAX);
  297. return 0;
  298. }
  299. core_initcall(bsp_pm_check_init);