cpu.c 2.7 KB

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