cpu_32.c 3.0 KB

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