cpu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifdef CONFIG_X86_32
  23. static struct saved_context saved_context;
  24. unsigned long saved_context_ebx;
  25. unsigned long saved_context_esp, saved_context_ebp;
  26. unsigned long saved_context_esi, saved_context_edi;
  27. unsigned long saved_context_eflags;
  28. #else
  29. /* CONFIG_X86_64 */
  30. struct saved_context saved_context;
  31. #endif
  32. /**
  33. * __save_processor_state - save CPU registers before creating a
  34. * hibernation image and before restoring the memory state from it
  35. * @ctxt - structure to store the registers contents in
  36. *
  37. * NOTE: If there is a CPU register the modification of which by the
  38. * boot kernel (ie. the kernel used for loading the hibernation image)
  39. * might affect the operations of the restored target kernel (ie. the one
  40. * saved in the hibernation image), then its contents must be saved by this
  41. * function. In other words, if kernel A is hibernated and different
  42. * kernel B is used for loading the hibernation image into memory, the
  43. * kernel A's __save_processor_state() function must save all registers
  44. * needed by kernel A, so that it can operate correctly after the resume
  45. * regardless of what kernel B does in the meantime.
  46. */
  47. static void __save_processor_state(struct saved_context *ctxt)
  48. {
  49. #ifdef CONFIG_X86_32
  50. mtrr_save_fixed_ranges(NULL);
  51. #endif
  52. kernel_fpu_begin();
  53. /*
  54. * descriptor tables
  55. */
  56. #ifdef CONFIG_X86_32
  57. store_gdt(&ctxt->gdt);
  58. store_idt(&ctxt->idt);
  59. #else
  60. /* CONFIG_X86_64 */
  61. store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
  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_gdt((const struct desc_ptr *)&ctxt->gdt_limit);
  171. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  172. #endif
  173. /*
  174. * segment registers
  175. */
  176. #ifdef CONFIG_X86_32
  177. loadsegment(es, ctxt->es);
  178. loadsegment(fs, ctxt->fs);
  179. loadsegment(gs, ctxt->gs);
  180. loadsegment(ss, ctxt->ss);
  181. /*
  182. * sysenter MSRs
  183. */
  184. if (boot_cpu_has(X86_FEATURE_SEP))
  185. enable_sep_cpu();
  186. #else
  187. /* CONFIG_X86_64 */
  188. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  189. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  190. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  191. load_gs_index(ctxt->gs);
  192. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  193. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  194. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  195. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  196. #endif
  197. /*
  198. * restore XCR0 for xsave capable cpu's.
  199. */
  200. if (cpu_has_xsave)
  201. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  202. fix_processor_context();
  203. do_fpu_end();
  204. x86_platform.restore_sched_clock_state();
  205. mtrr_bp_restore();
  206. }
  207. /* Needed by apm.c */
  208. void restore_processor_state(void)
  209. {
  210. __restore_processor_state(&saved_context);
  211. }
  212. #ifdef CONFIG_X86_32
  213. EXPORT_SYMBOL(restore_processor_state);
  214. #endif