cpu.c 8.0 KB

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