cpu.c 8.1 KB

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