suspend.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/config.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/poll.h>
  16. #include <linux/delay.h>
  17. #include <linux/sysrq.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/irq.h>
  20. #include <linux/pm.h>
  21. #include <linux/device.h>
  22. #include <linux/suspend.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/acpi.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/io.h>
  27. #include <asm/proto.h>
  28. struct saved_context saved_context;
  29. unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
  30. unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
  31. unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
  32. unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
  33. unsigned long saved_context_eflags;
  34. void __save_processor_state(struct saved_context *ctxt)
  35. {
  36. kernel_fpu_begin();
  37. /*
  38. * descriptor tables
  39. */
  40. asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
  41. asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
  42. asm volatile ("str %0" : "=m" (ctxt->tr));
  43. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  44. /* EFER should be constant for kernel version, no need to handle it. */
  45. /*
  46. * segment registers
  47. */
  48. asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
  49. asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
  50. asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
  51. asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
  52. asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
  53. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  54. rdmsrl(MSR_GS_BASE, ctxt->gs_base);
  55. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  56. /*
  57. * control registers
  58. */
  59. asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
  60. asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
  61. asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
  62. asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
  63. asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
  64. }
  65. void save_processor_state(void)
  66. {
  67. __save_processor_state(&saved_context);
  68. }
  69. static void
  70. do_fpu_end(void)
  71. {
  72. /* restore FPU regs if necessary */
  73. /* Do it out of line so that gcc does not move cr0 load to some stupid place */
  74. kernel_fpu_end();
  75. mxcsr_feature_mask_init();
  76. }
  77. void __restore_processor_state(struct saved_context *ctxt)
  78. {
  79. /*
  80. * control registers
  81. */
  82. asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
  83. asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
  84. asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
  85. asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
  86. asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
  87. /*
  88. * now restore the descriptor tables to their proper values
  89. * ltr is done i fix_processor_context().
  90. */
  91. asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
  92. asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
  93. /*
  94. * segment registers
  95. */
  96. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  97. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  98. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  99. load_gs_index(ctxt->gs);
  100. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  101. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  102. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  103. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  104. fix_processor_context();
  105. do_fpu_end();
  106. mtrr_ap_init();
  107. }
  108. void restore_processor_state(void)
  109. {
  110. __restore_processor_state(&saved_context);
  111. }
  112. void fix_processor_context(void)
  113. {
  114. int cpu = smp_processor_id();
  115. struct tss_struct *t = &per_cpu(init_tss, cpu);
  116. 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. */
  117. cpu_gdt_table[cpu][GDT_ENTRY_TSS].type = 9;
  118. syscall_init(); /* This sets MSR_*STAR and related */
  119. load_TR_desc(); /* This does ltr */
  120. load_LDT(&current->active_mm->context); /* This does lldt */
  121. /*
  122. * Now maybe reload the debug registers
  123. */
  124. if (current->thread.debugreg7){
  125. loaddebug(&current->thread, 0);
  126. loaddebug(&current->thread, 1);
  127. loaddebug(&current->thread, 2);
  128. loaddebug(&current->thread, 3);
  129. /* no 4 and 5 */
  130. loaddebug(&current->thread, 6);
  131. loaddebug(&current->thread, 7);
  132. }
  133. }