cpu_32.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/module.h>
  10. #include <linux/suspend.h>
  11. #include <asm/mtrr.h>
  12. #include <asm/mce.h>
  13. #include <asm/xcr.h>
  14. static struct saved_context saved_context;
  15. unsigned long saved_context_ebx;
  16. unsigned long saved_context_esp, saved_context_ebp;
  17. unsigned long saved_context_esi, saved_context_edi;
  18. unsigned long saved_context_eflags;
  19. static void __save_processor_state(struct saved_context *ctxt)
  20. {
  21. mtrr_save_fixed_ranges(NULL);
  22. kernel_fpu_begin();
  23. /*
  24. * descriptor tables
  25. */
  26. store_gdt(&ctxt->gdt);
  27. store_idt(&ctxt->idt);
  28. store_tr(ctxt->tr);
  29. /*
  30. * segment registers
  31. */
  32. savesegment(es, ctxt->es);
  33. savesegment(fs, ctxt->fs);
  34. savesegment(gs, ctxt->gs);
  35. savesegment(ss, ctxt->ss);
  36. /*
  37. * control registers
  38. */
  39. ctxt->cr0 = read_cr0();
  40. ctxt->cr2 = read_cr2();
  41. ctxt->cr3 = read_cr3();
  42. ctxt->cr4 = read_cr4_safe();
  43. }
  44. /* Needed by apm.c */
  45. void save_processor_state(void)
  46. {
  47. __save_processor_state(&saved_context);
  48. }
  49. EXPORT_SYMBOL(save_processor_state);
  50. static void do_fpu_end(void)
  51. {
  52. /*
  53. * Restore FPU regs if necessary.
  54. */
  55. kernel_fpu_end();
  56. }
  57. static void fix_processor_context(void)
  58. {
  59. int cpu = smp_processor_id();
  60. struct tss_struct *t = &per_cpu(init_tss, cpu);
  61. set_tss_desc(cpu, t); /*
  62. * This just modifies memory; should not be
  63. * necessary. But... This is necessary, because
  64. * 386 hardware has concept of busy TSS or some
  65. * similar stupidity.
  66. */
  67. load_TR_desc(); /* This does ltr */
  68. load_LDT(&current->active_mm->context); /* This does lldt */
  69. /*
  70. * Now maybe reload the debug registers
  71. */
  72. if (current->thread.debugreg7) {
  73. set_debugreg(current->thread.debugreg0, 0);
  74. set_debugreg(current->thread.debugreg1, 1);
  75. set_debugreg(current->thread.debugreg2, 2);
  76. set_debugreg(current->thread.debugreg3, 3);
  77. /* no 4 and 5 */
  78. set_debugreg(current->thread.debugreg6, 6);
  79. set_debugreg(current->thread.debugreg7, 7);
  80. }
  81. }
  82. static void __restore_processor_state(struct saved_context *ctxt)
  83. {
  84. /*
  85. * control registers
  86. */
  87. /* cr4 was introduced in the Pentium CPU */
  88. if (ctxt->cr4)
  89. write_cr4(ctxt->cr4);
  90. write_cr3(ctxt->cr3);
  91. write_cr2(ctxt->cr2);
  92. write_cr0(ctxt->cr0);
  93. /*
  94. * now restore the descriptor tables to their proper values
  95. * ltr is done i fix_processor_context().
  96. */
  97. load_gdt(&ctxt->gdt);
  98. load_idt(&ctxt->idt);
  99. /*
  100. * segment registers
  101. */
  102. loadsegment(es, ctxt->es);
  103. loadsegment(fs, ctxt->fs);
  104. loadsegment(gs, ctxt->gs);
  105. loadsegment(ss, ctxt->ss);
  106. /*
  107. * sysenter MSRs
  108. */
  109. if (boot_cpu_has(X86_FEATURE_SEP))
  110. enable_sep_cpu();
  111. /*
  112. * restore XCR0 for xsave capable cpu's.
  113. */
  114. if (cpu_has_xsave)
  115. xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
  116. fix_processor_context();
  117. do_fpu_end();
  118. mtrr_ap_init();
  119. mcheck_init(&boot_cpu_data);
  120. }
  121. /* Needed by apm.c */
  122. void restore_processor_state(void)
  123. {
  124. __restore_processor_state(&saved_context);
  125. }
  126. EXPORT_SYMBOL(restore_processor_state);