Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 10 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/suspend.h> |
Alexey Dobriyan | 27b07da | 2006-06-23 02:04:18 -0700 | [diff] [blame] | 13 | #include <asm/mtrr.h> |
Alexey Dobriyan | a03a3e2 | 2006-06-23 02:04:20 -0700 | [diff] [blame] | 14 | #include <asm/mce.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | static struct saved_context saved_context; |
| 17 | |
| 18 | unsigned long saved_context_ebx; |
| 19 | unsigned long saved_context_esp, saved_context_ebp; |
| 20 | unsigned long saved_context_esi, saved_context_edi; |
| 21 | unsigned long saved_context_eflags; |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | void __save_processor_state(struct saved_context *ctxt) |
| 24 | { |
| 25 | kernel_fpu_begin(); |
| 26 | |
| 27 | /* |
| 28 | * descriptor tables |
| 29 | */ |
Zachary Amsden | 4d37e7e | 2005-09-03 15:56:38 -0700 | [diff] [blame] | 30 | store_gdt(&ctxt->gdt_limit); |
| 31 | store_idt(&ctxt->idt_limit); |
| 32 | store_tr(ctxt->tr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * segment registers |
| 36 | */ |
Zachary Amsden | 4d37e7e | 2005-09-03 15:56:38 -0700 | [diff] [blame] | 37 | savesegment(es, ctxt->es); |
| 38 | savesegment(fs, ctxt->fs); |
| 39 | savesegment(gs, ctxt->gs); |
| 40 | savesegment(ss, ctxt->ss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * control registers |
| 44 | */ |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 45 | ctxt->cr0 = read_cr0(); |
| 46 | ctxt->cr2 = read_cr2(); |
| 47 | ctxt->cr3 = read_cr3(); |
| 48 | ctxt->cr4 = read_cr4(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void save_processor_state(void) |
| 52 | { |
| 53 | __save_processor_state(&saved_context); |
| 54 | } |
| 55 | |
Shaohua Li | 08967f9 | 2005-10-30 14:59:28 -0800 | [diff] [blame] | 56 | static void do_fpu_end(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Shaohua Li | 08967f9 | 2005-10-30 14:59:28 -0800 | [diff] [blame] | 58 | /* |
| 59 | * Restore FPU regs if necessary. |
| 60 | */ |
| 61 | kernel_fpu_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static void fix_processor_context(void) |
| 65 | { |
| 66 | int cpu = smp_processor_id(); |
| 67 | struct tss_struct * t = &per_cpu(init_tss, cpu); |
| 68 | |
| 69 | 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. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | load_TR_desc(); /* This does ltr */ |
| 72 | load_LDT(¤t->active_mm->context); /* This does lldt */ |
| 73 | |
| 74 | /* |
| 75 | * Now maybe reload the debug registers |
| 76 | */ |
| 77 | if (current->thread.debugreg[7]){ |
Vincent Hanquez | 1cc6f12 | 2005-06-23 00:08:43 -0700 | [diff] [blame] | 78 | set_debugreg(current->thread.debugreg[0], 0); |
| 79 | set_debugreg(current->thread.debugreg[1], 1); |
| 80 | set_debugreg(current->thread.debugreg[2], 2); |
| 81 | set_debugreg(current->thread.debugreg[3], 3); |
| 82 | /* no 4 and 5 */ |
| 83 | set_debugreg(current->thread.debugreg[6], 6); |
| 84 | set_debugreg(current->thread.debugreg[7], 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | void __restore_processor_state(struct saved_context *ctxt) |
| 90 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* |
| 92 | * control registers |
| 93 | */ |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 94 | write_cr4(ctxt->cr4); |
| 95 | write_cr3(ctxt->cr3); |
| 96 | write_cr2(ctxt->cr2); |
Pavel Machek | 30d6b2f | 2006-05-22 22:35:29 -0700 | [diff] [blame] | 97 | write_cr0(ctxt->cr0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
Pavel Machek | 8d783b3 | 2005-06-25 14:55:14 -0700 | [diff] [blame] | 100 | * now restore the descriptor tables to their proper values |
| 101 | * ltr is done i fix_processor_context(). |
| 102 | */ |
Zachary Amsden | 4d37e7e | 2005-09-03 15:56:38 -0700 | [diff] [blame] | 103 | load_gdt(&ctxt->gdt_limit); |
| 104 | load_idt(&ctxt->idt_limit); |
Pavel Machek | 8d783b3 | 2005-06-25 14:55:14 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * segment registers |
| 108 | */ |
Zachary Amsden | 4d37e7e | 2005-09-03 15:56:38 -0700 | [diff] [blame] | 109 | loadsegment(es, ctxt->es); |
| 110 | loadsegment(fs, ctxt->fs); |
| 111 | loadsegment(gs, ctxt->gs); |
| 112 | loadsegment(ss, ctxt->ss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * sysenter MSRs |
| 116 | */ |
| 117 | if (boot_cpu_has(X86_FEATURE_SEP)) |
Li Shaohua | 6fe940d | 2005-06-25 14:54:53 -0700 | [diff] [blame] | 118 | enable_sep_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | fix_processor_context(); |
| 121 | do_fpu_end(); |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 122 | mtrr_ap_init(); |
Shaohua Li | 31ab269 | 2005-11-07 00:58:42 -0800 | [diff] [blame] | 123 | mcheck_init(&boot_cpu_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void restore_processor_state(void) |
| 127 | { |
| 128 | __restore_processor_state(&saved_context); |
| 129 | } |
| 130 | |
| 131 | /* Needed by apm.c */ |
| 132 | EXPORT_SYMBOL(save_processor_state); |
| 133 | EXPORT_SYMBOL(restore_processor_state); |