cpu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #ifdef CONFIG_X86_32
  22. static struct saved_context saved_context;
  23. unsigned long saved_context_ebx;
  24. unsigned long saved_context_esp, saved_context_ebp;
  25. unsigned long saved_context_esi, saved_context_edi;
  26. unsigned long saved_context_eflags;
  27. #else
  28. /* CONFIG_X86_64 */
  29. struct saved_context saved_context;
  30. #endif
  31. /**
  32. * __save_processor_state - save CPU registers before creating a
  33. * hibernation image and before restoring the memory state from it
  34. * @ctxt - structure to store the registers contents in
  35. *
  36. * NOTE: If there is a CPU register the modification of which by the
  37. * boot kernel (ie. the kernel used for loading the hibernation image)
  38. * might affect the operations of the restored target kernel (ie. the one
  39. * saved in the hibernation image), then its contents must be saved by this
  40. * function. In other words, if kernel A is hibernated and different
  41. * kernel B is used for loading the hibernation image into memory, the
  42. * kernel A's __save_processor_state() function must save all registers
  43. * needed by kernel A, so that it can operate correctly after the resume
  44. * regardless of what kernel B does in the meantime.
  45. */
  46. static void __save_processor_state(struct saved_context *ctxt)
  47. {
  48. #ifdef CONFIG_X86_32
  49. mtrr_save_fixed_ranges(NULL);
  50. #endif
  51. kernel_fpu_begin();
  52. /*
  53. * descriptor tables
  54. */
  55. #ifdef CONFIG_X86_32
  56. store_gdt(&ctxt->gdt);
  57. store_idt(&ctxt->idt);
  58. #else
  59. /* CONFIG_X86_64 */
  60. store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
  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. 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. set_tss_desc(cpu, t); /*
  123. * This just modifies memory; should not be
  124. * necessary. But... This is necessary, because
  125. * 386 hardware has concept of busy TSS or some
  126. * similar stupidity.
  127. */
  128. #ifdef CONFIG_X86_64
  129. get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS].type = 9;
  130. syscall_init(); /* This sets MSR_*STAR and related */
  131. #endif
  132. load_TR_desc(); /* This does ltr */
  133. load_LDT(&current->active_mm->context); /* This does lldt */
  134. }
  135. /**
  136. * __restore_processor_state - restore the contents of CPU registers saved
  137. * by __save_processor_state()
  138. * @ctxt - structure to load the registers contents from
  139. */
  140. static void __restore_processor_state(struct saved_context *ctxt)
  141. {
  142. if (ctxt->misc_enable_saved)
  143. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  144. /*
  145. * control registers
  146. */
  147. /* cr4 was introduced in the Pentium CPU */
  148. #ifdef CONFIG_X86_32
  149. if (ctxt->cr4)
  150. write_cr4(ctxt->cr4);
  151. #else
  152. /* CONFIG X86_64 */
  153. wrmsrl(MSR_EFER, ctxt->efer);
  154. write_cr8(ctxt->cr8);
  155. write_cr4(ctxt->cr4);
  156. #endif
  157. write_cr3(ctxt->cr3);
  158. write_cr2(ctxt->cr2);
  159. write_cr0(ctxt->cr0);
  160. /*
  161. * now restore the descriptor tables to their proper values
  162. * ltr is done i fix_processor_context().
  163. */
  164. #ifdef CONFIG_X86_32
  165. load_gdt(&ctxt->gdt);
  166. load_idt(&ctxt->idt);
  167. #else
  168. /* CONFIG_X86_64 */
  169. load_gdt((const struct desc_ptr *)&ctxt->gdt_limit);
  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. mtrr_bp_restore();
  204. }
  205. /* Needed by apm.c */
  206. void restore_processor_state(void)
  207. {
  208. __restore_processor_state(&saved_context);
  209. restore_sched_clock_state();
  210. }
  211. #ifdef CONFIG_X86_32
  212. EXPORT_SYMBOL(restore_processor_state);
  213. #endif