cpu.c 8.3 KB

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