cpu.c 5.9 KB

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