cpu.c 3.2 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. store_gdt(&ctxt->gdt_limit);
  40. store_idt(&ctxt->idt_limit);
  41. store_tr(ctxt->tr);
  42. /*
  43. * segment registers
  44. */
  45. savesegment(es, ctxt->es);
  46. savesegment(fs, ctxt->fs);
  47. savesegment(gs, ctxt->gs);
  48. savesegment(ss, ctxt->ss);
  49. /*
  50. * control registers
  51. */
  52. ctxt->cr0 = read_cr0();
  53. ctxt->cr2 = read_cr2();
  54. ctxt->cr3 = read_cr3();
  55. ctxt->cr4 = read_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. load_TR_desc(); /* This does ltr */
  75. load_LDT(&current->active_mm->context); /* This does lldt */
  76. /*
  77. * Now maybe reload the debug registers
  78. */
  79. if (current->thread.debugreg[7]){
  80. set_debugreg(current->thread.debugreg[0], 0);
  81. set_debugreg(current->thread.debugreg[1], 1);
  82. set_debugreg(current->thread.debugreg[2], 2);
  83. set_debugreg(current->thread.debugreg[3], 3);
  84. /* no 4 and 5 */
  85. set_debugreg(current->thread.debugreg[6], 6);
  86. set_debugreg(current->thread.debugreg[7], 7);
  87. }
  88. }
  89. void __restore_processor_state(struct saved_context *ctxt)
  90. {
  91. /*
  92. * control registers
  93. */
  94. write_cr4(ctxt->cr4);
  95. write_cr3(ctxt->cr3);
  96. write_cr2(ctxt->cr2);
  97. write_cr2(ctxt->cr0);
  98. /*
  99. * now restore the descriptor tables to their proper values
  100. * ltr is done i fix_processor_context().
  101. */
  102. load_gdt(&ctxt->gdt_limit);
  103. load_idt(&ctxt->idt_limit);
  104. /*
  105. * segment registers
  106. */
  107. loadsegment(es, ctxt->es);
  108. loadsegment(fs, ctxt->fs);
  109. loadsegment(gs, ctxt->gs);
  110. loadsegment(ss, ctxt->ss);
  111. /*
  112. * sysenter MSRs
  113. */
  114. if (boot_cpu_has(X86_FEATURE_SEP))
  115. enable_sep_cpu();
  116. fix_processor_context();
  117. do_fpu_end();
  118. mtrr_ap_init();
  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);