suspend.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Suspend support specific for i386.
  3. *
  4. * Distribute under GPLv2
  5. *
  6. * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
  7. * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
  8. */
  9. #include <linux/smp.h>
  10. #include <linux/suspend.h>
  11. #include <asm/proto.h>
  12. #include <asm/page.h>
  13. #include <asm/pgtable.h>
  14. /* References to section boundaries */
  15. extern const void __nosave_begin, __nosave_end;
  16. struct saved_context saved_context;
  17. unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
  18. unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
  19. unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
  20. unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
  21. unsigned long saved_context_eflags;
  22. void __save_processor_state(struct saved_context *ctxt)
  23. {
  24. kernel_fpu_begin();
  25. /*
  26. * descriptor tables
  27. */
  28. asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
  29. asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
  30. asm volatile ("str %0" : "=m" (ctxt->tr));
  31. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  32. /*
  33. * segment registers
  34. */
  35. asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
  36. asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
  37. asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
  38. asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
  39. asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
  40. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  41. rdmsrl(MSR_GS_BASE, ctxt->gs_base);
  42. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  43. /*
  44. * control registers
  45. */
  46. rdmsrl(MSR_EFER, ctxt->efer);
  47. asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
  48. asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
  49. asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
  50. asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
  51. asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
  52. }
  53. void save_processor_state(void)
  54. {
  55. __save_processor_state(&saved_context);
  56. }
  57. static void do_fpu_end(void)
  58. {
  59. /*
  60. * Restore FPU regs if necessary
  61. */
  62. kernel_fpu_end();
  63. }
  64. void __restore_processor_state(struct saved_context *ctxt)
  65. {
  66. /*
  67. * control registers
  68. */
  69. wrmsrl(MSR_EFER, ctxt->efer);
  70. asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
  71. asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
  72. asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
  73. asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
  74. asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
  75. /*
  76. * now restore the descriptor tables to their proper values
  77. * ltr is done i fix_processor_context().
  78. */
  79. asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
  80. asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
  81. /*
  82. * segment registers
  83. */
  84. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  85. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  86. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  87. load_gs_index(ctxt->gs);
  88. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  89. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  90. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  91. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  92. fix_processor_context();
  93. do_fpu_end();
  94. mtrr_ap_init();
  95. }
  96. void restore_processor_state(void)
  97. {
  98. __restore_processor_state(&saved_context);
  99. }
  100. void fix_processor_context(void)
  101. {
  102. int cpu = smp_processor_id();
  103. struct tss_struct *t = &per_cpu(init_tss, cpu);
  104. set_tss_desc(cpu,t); /* This just modifies memory; should not be neccessary. But... This is neccessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
  105. cpu_gdt(cpu)[GDT_ENTRY_TSS].type = 9;
  106. syscall_init(); /* This sets MSR_*STAR and related */
  107. load_TR_desc(); /* This does ltr */
  108. load_LDT(&current->active_mm->context); /* This does lldt */
  109. /*
  110. * Now maybe reload the debug registers
  111. */
  112. if (current->thread.debugreg7){
  113. loaddebug(&current->thread, 0);
  114. loaddebug(&current->thread, 1);
  115. loaddebug(&current->thread, 2);
  116. loaddebug(&current->thread, 3);
  117. /* no 4 and 5 */
  118. loaddebug(&current->thread, 6);
  119. loaddebug(&current->thread, 7);
  120. }
  121. }
  122. #ifdef CONFIG_SOFTWARE_SUSPEND
  123. /* Defined in arch/x86_64/kernel/suspend_asm.S */
  124. extern int restore_image(void);
  125. pgd_t *temp_level4_pgt;
  126. static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
  127. {
  128. long i, j;
  129. i = pud_index(address);
  130. pud = pud + i;
  131. for (; i < PTRS_PER_PUD; pud++, i++) {
  132. unsigned long paddr;
  133. pmd_t *pmd;
  134. paddr = address + i*PUD_SIZE;
  135. if (paddr >= end)
  136. break;
  137. pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
  138. if (!pmd)
  139. return -ENOMEM;
  140. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  141. for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
  142. unsigned long pe;
  143. if (paddr >= end)
  144. break;
  145. pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr;
  146. pe &= __supported_pte_mask;
  147. set_pmd(pmd, __pmd(pe));
  148. }
  149. }
  150. return 0;
  151. }
  152. static int set_up_temporary_mappings(void)
  153. {
  154. unsigned long start, end, next;
  155. int error;
  156. temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
  157. if (!temp_level4_pgt)
  158. return -ENOMEM;
  159. /* It is safe to reuse the original kernel mapping */
  160. set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
  161. init_level4_pgt[pgd_index(__START_KERNEL_map)]);
  162. /* Set up the direct mapping from scratch */
  163. start = (unsigned long)pfn_to_kaddr(0);
  164. end = (unsigned long)pfn_to_kaddr(end_pfn);
  165. for (; start < end; start = next) {
  166. pud_t *pud = (pud_t *)get_safe_page(GFP_ATOMIC);
  167. if (!pud)
  168. return -ENOMEM;
  169. next = start + PGDIR_SIZE;
  170. if (next > end)
  171. next = end;
  172. if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
  173. return error;
  174. set_pgd(temp_level4_pgt + pgd_index(start),
  175. mk_kernel_pgd(__pa(pud)));
  176. }
  177. return 0;
  178. }
  179. int swsusp_arch_resume(void)
  180. {
  181. int error;
  182. /* We have got enough memory and from now on we cannot recover */
  183. if ((error = set_up_temporary_mappings()))
  184. return error;
  185. restore_image();
  186. return 0;
  187. }
  188. /*
  189. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  190. */
  191. int pfn_is_nosave(unsigned long pfn)
  192. {
  193. unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  194. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  195. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  196. }
  197. #endif /* CONFIG_SOFTWARE_SUSPEND */