cpu.c 8.2 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 <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_idt(&ctxt->idt);
  59. #else
  60. /* CONFIG_X86_64 */
  61. store_idt((struct desc_ptr *)&ctxt->idt_limit);
  62. #endif
  63. store_tr(ctxt->tr);
  64. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  65. /*
  66. * segment registers
  67. */
  68. #ifdef CONFIG_X86_32
  69. savesegment(es, ctxt->es);
  70. savesegment(fs, ctxt->fs);
  71. savesegment(gs, ctxt->gs);
  72. savesegment(ss, ctxt->ss);
  73. #else
  74. /* CONFIG_X86_64 */
  75. asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
  76. asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
  77. asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
  78. asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
  79. asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
  80. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  81. rdmsrl(MSR_GS_BASE, ctxt->gs_base);
  82. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  83. mtrr_save_fixed_ranges(NULL);
  84. rdmsrl(MSR_EFER, ctxt->efer);
  85. #endif
  86. /*
  87. * control registers
  88. */
  89. ctxt->cr0 = read_cr0();
  90. ctxt->cr2 = read_cr2();
  91. ctxt->cr3 = read_cr3();
  92. #ifdef CONFIG_X86_32
  93. ctxt->cr4 = read_cr4_safe();
  94. #else
  95. /* CONFIG_X86_64 */
  96. ctxt->cr4 = read_cr4();
  97. ctxt->cr8 = read_cr8();
  98. #endif
  99. ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
  100. &ctxt->misc_enable);
  101. }
  102. /* Needed by apm.c */
  103. void save_processor_state(void)
  104. {
  105. __save_processor_state(&saved_context);
  106. x86_platform.save_sched_clock_state();
  107. }
  108. #ifdef CONFIG_X86_32
  109. EXPORT_SYMBOL(save_processor_state);
  110. #endif
  111. static void do_fpu_end(void)
  112. {
  113. /*
  114. * Restore FPU regs if necessary.
  115. */
  116. kernel_fpu_end();
  117. }
  118. static void fix_processor_context(void)
  119. {
  120. int cpu = smp_processor_id();
  121. struct tss_struct *t = &per_cpu(init_tss, cpu);
  122. #ifdef CONFIG_X86_64
  123. struct desc_struct *desc = get_cpu_gdt_table(cpu);
  124. tss_desc tss;
  125. #endif
  126. set_tss_desc(cpu, t); /*
  127. * This just modifies memory; should not be
  128. * necessary. But... This is necessary, because
  129. * 386 hardware has concept of busy TSS or some
  130. * similar stupidity.
  131. */
  132. #ifdef CONFIG_X86_64
  133. memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
  134. tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
  135. write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
  136. syscall_init(); /* This sets MSR_*STAR and related */
  137. #endif
  138. load_TR_desc(); /* This does ltr */
  139. load_LDT(&current->active_mm->context); /* This does lldt */
  140. }
  141. /**
  142. * __restore_processor_state - restore the contents of CPU registers saved
  143. * by __save_processor_state()
  144. * @ctxt - structure to load the registers contents from
  145. */
  146. static void __restore_processor_state(struct saved_context *ctxt)
  147. {
  148. if (ctxt->misc_enable_saved)
  149. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  150. /*
  151. * control registers
  152. */
  153. /* cr4 was introduced in the Pentium CPU */
  154. #ifdef CONFIG_X86_32
  155. if (ctxt->cr4)
  156. write_cr4(ctxt->cr4);
  157. #else
  158. /* CONFIG X86_64 */
  159. wrmsrl(MSR_EFER, ctxt->efer);
  160. write_cr8(ctxt->cr8);
  161. write_cr4(ctxt->cr4);
  162. #endif
  163. write_cr3(ctxt->cr3);
  164. write_cr2(ctxt->cr2);
  165. write_cr0(ctxt->cr0);
  166. /*
  167. * now restore the descriptor tables to their proper values
  168. * ltr is done i fix_processor_context().
  169. */
  170. #ifdef CONFIG_X86_32
  171. load_idt(&ctxt->idt);
  172. #else
  173. /* CONFIG_X86_64 */
  174. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  175. #endif
  176. /*
  177. * segment registers
  178. */
  179. #ifdef CONFIG_X86_32
  180. loadsegment(es, ctxt->es);
  181. loadsegment(fs, ctxt->fs);
  182. loadsegment(gs, ctxt->gs);
  183. loadsegment(ss, ctxt->ss);
  184. /*
  185. * sysenter MSRs
  186. */
  187. if (boot_cpu_has(X86_FEATURE_SEP))
  188. enable_sep_cpu();
  189. #else
  190. /* CONFIG_X86_64 */
  191. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  192. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  193. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  194. load_gs_index(ctxt->gs);
  195. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  196. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  197. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  198. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  199. #endif
  200. /*
  201. * restore XCR0 for xsave capable cpu's.
  202. */
  203. if (cpu_has_xsave)
  204. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  205. fix_processor_context();
  206. do_fpu_end();
  207. x86_platform.restore_sched_clock_state();
  208. mtrr_bp_restore();
  209. }
  210. /* Needed by apm.c */
  211. void 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);