cpu.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. static struct saved_context saved_context;
  14. unsigned long saved_context_ebx;
  15. unsigned long saved_context_esp, saved_context_ebp;
  16. unsigned long saved_context_esi, saved_context_edi;
  17. unsigned long saved_context_eflags;
  18. void __save_processor_state(struct saved_context *ctxt)
  19. {
  20. mtrr_save_fixed_ranges(NULL);
  21. kernel_fpu_begin();
  22. /*
  23. * descriptor tables
  24. */
  25. store_gdt(&ctxt->gdt);
  26. store_idt(&ctxt->idt);
  27. store_tr(ctxt->tr);
  28. /*
  29. * segment registers
  30. */
  31. savesegment(es, ctxt->es);
  32. savesegment(fs, ctxt->fs);
  33. savesegment(gs, ctxt->gs);
  34. savesegment(ss, ctxt->ss);
  35. /*
  36. * control registers
  37. */
  38. ctxt->cr0 = read_cr0();
  39. ctxt->cr2 = read_cr2();
  40. ctxt->cr3 = read_cr3();
  41. ctxt->cr4 = read_cr4();
  42. }
  43. void save_processor_state(void)
  44. {
  45. __save_processor_state(&saved_context);
  46. }
  47. static void do_fpu_end(void)
  48. {
  49. /*
  50. * Restore FPU regs if necessary.
  51. */
  52. kernel_fpu_end();
  53. }
  54. static void fix_processor_context(void)
  55. {
  56. int cpu = smp_processor_id();
  57. struct tss_struct * t = &per_cpu(init_tss, cpu);
  58. 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. */
  59. load_TR_desc(); /* This does ltr */
  60. load_LDT(&current->active_mm->context); /* This does lldt */
  61. /*
  62. * Now maybe reload the debug registers
  63. */
  64. if (current->thread.debugreg[7]){
  65. set_debugreg(current->thread.debugreg[0], 0);
  66. set_debugreg(current->thread.debugreg[1], 1);
  67. set_debugreg(current->thread.debugreg[2], 2);
  68. set_debugreg(current->thread.debugreg[3], 3);
  69. /* no 4 and 5 */
  70. set_debugreg(current->thread.debugreg[6], 6);
  71. set_debugreg(current->thread.debugreg[7], 7);
  72. }
  73. }
  74. void __restore_processor_state(struct saved_context *ctxt)
  75. {
  76. /*
  77. * control registers
  78. */
  79. write_cr4(ctxt->cr4);
  80. write_cr3(ctxt->cr3);
  81. write_cr2(ctxt->cr2);
  82. write_cr0(ctxt->cr0);
  83. /*
  84. * now restore the descriptor tables to their proper values
  85. * ltr is done i fix_processor_context().
  86. */
  87. load_gdt(&ctxt->gdt);
  88. load_idt(&ctxt->idt);
  89. /*
  90. * segment registers
  91. */
  92. loadsegment(es, ctxt->es);
  93. loadsegment(fs, ctxt->fs);
  94. loadsegment(gs, ctxt->gs);
  95. loadsegment(ss, ctxt->ss);
  96. /*
  97. * sysenter MSRs
  98. */
  99. if (boot_cpu_has(X86_FEATURE_SEP))
  100. enable_sep_cpu();
  101. fix_processor_context();
  102. do_fpu_end();
  103. mtrr_ap_init();
  104. mcheck_init(&boot_cpu_data);
  105. }
  106. void restore_processor_state(void)
  107. {
  108. __restore_processor_state(&saved_context);
  109. }
  110. /* Needed by apm.c */
  111. EXPORT_SYMBOL(save_processor_state);
  112. EXPORT_SYMBOL(restore_processor_state);