suspend.c 5.8 KB

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