cpu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <linux/acpi.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/acpi.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/processor.h>
  28. static struct saved_context saved_context;
  29. unsigned long saved_context_ebx;
  30. unsigned long saved_context_esp, saved_context_ebp;
  31. unsigned long saved_context_esi, saved_context_edi;
  32. unsigned long saved_context_eflags;
  33. void __save_processor_state(struct saved_context *ctxt)
  34. {
  35. kernel_fpu_begin();
  36. /*
  37. * descriptor tables
  38. */
  39. asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
  40. asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
  41. asm volatile ("str %0" : "=m" (ctxt->tr));
  42. /*
  43. * segment registers
  44. */
  45. asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
  46. asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
  47. asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
  48. asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
  49. /*
  50. * control registers
  51. */
  52. asm volatile ("movl %%cr0, %0" : "=r" (ctxt->cr0));
  53. asm volatile ("movl %%cr2, %0" : "=r" (ctxt->cr2));
  54. asm volatile ("movl %%cr3, %0" : "=r" (ctxt->cr3));
  55. asm volatile ("movl %%cr4, %0" : "=r" (ctxt->cr4));
  56. }
  57. void save_processor_state(void)
  58. {
  59. __save_processor_state(&saved_context);
  60. }
  61. static void
  62. do_fpu_end(void)
  63. {
  64. /* restore FPU regs if necessary */
  65. /* Do it out of line so that gcc does not move cr0 load to some stupid place */
  66. kernel_fpu_end();
  67. mxcsr_feature_mask_init();
  68. }
  69. static void fix_processor_context(void)
  70. {
  71. int cpu = smp_processor_id();
  72. struct tss_struct * t = &per_cpu(init_tss, cpu);
  73. set_tss_desc(cpu,t); /* This just modifies memory; should not be necessary. But... This is necessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
  74. per_cpu(cpu_gdt_table, cpu)[GDT_ENTRY_TSS].b &= 0xfffffdff;
  75. load_TR_desc(); /* This does ltr */
  76. load_LDT(&current->active_mm->context); /* This does lldt */
  77. /*
  78. * Now maybe reload the debug registers
  79. */
  80. if (current->thread.debugreg[7]){
  81. set_debugreg(current->thread.debugreg[0], 0);
  82. set_debugreg(current->thread.debugreg[1], 1);
  83. set_debugreg(current->thread.debugreg[2], 2);
  84. set_debugreg(current->thread.debugreg[3], 3);
  85. /* no 4 and 5 */
  86. set_debugreg(current->thread.debugreg[6], 6);
  87. set_debugreg(current->thread.debugreg[7], 7);
  88. }
  89. }
  90. void __restore_processor_state(struct saved_context *ctxt)
  91. {
  92. /*
  93. * control registers
  94. */
  95. asm volatile ("movl %0, %%cr4" :: "r" (ctxt->cr4));
  96. asm volatile ("movl %0, %%cr3" :: "r" (ctxt->cr3));
  97. asm volatile ("movl %0, %%cr2" :: "r" (ctxt->cr2));
  98. asm volatile ("movl %0, %%cr0" :: "r" (ctxt->cr0));
  99. /*
  100. * now restore the descriptor tables to their proper values
  101. * ltr is done i fix_processor_context().
  102. */
  103. asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
  104. asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
  105. /*
  106. * segment registers
  107. */
  108. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  109. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  110. asm volatile ("movw %0, %%gs" :: "r" (ctxt->gs));
  111. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  112. /*
  113. * sysenter MSRs
  114. */
  115. if (boot_cpu_has(X86_FEATURE_SEP))
  116. enable_sep_cpu();
  117. fix_processor_context();
  118. do_fpu_end();
  119. }
  120. void restore_processor_state(void)
  121. {
  122. __restore_processor_state(&saved_context);
  123. }
  124. /* Needed by apm.c */
  125. EXPORT_SYMBOL(save_processor_state);
  126. EXPORT_SYMBOL(restore_processor_state);