cpu.c 2.8 KB

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