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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/suspend.h> |
Alexey Dobriyan | 27b07da | 2006-06-23 02:04:18 -0700 | [diff] [blame] | 12 | #include <asm/mtrr.h> |
Alexey Dobriyan | a03a3e2 | 2006-06-23 02:04:20 -0700 | [diff] [blame] | 13 | #include <asm/mce.h> |
Suresh Siddha | 83b8e28 | 2008-08-27 14:57:36 -0700 | [diff] [blame] | 14 | #include <asm/xcr.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 | |
Jan Beulich | cae4595 | 2008-01-30 13:31:23 +0100 | [diff] [blame] | 23 | static void __save_processor_state(struct saved_context *ctxt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
Bernhard Kaindl | 3ebad59 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 25 | mtrr_save_fixed_ranges(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | kernel_fpu_begin(); |
| 27 | |
| 28 | /* |
| 29 | * descriptor tables |
| 30 | */ |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 31 | store_gdt(&ctxt->gdt); |
| 32 | store_idt(&ctxt->idt); |
| 33 | store_tr(ctxt->tr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * segment registers |
| 37 | */ |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 38 | savesegment(es, ctxt->es); |
| 39 | savesegment(fs, ctxt->fs); |
| 40 | savesegment(gs, ctxt->gs); |
| 41 | savesegment(ss, ctxt->ss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
Rafael J. Wysocki | c575912 | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 44 | * control registers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 46 | ctxt->cr0 = read_cr0(); |
| 47 | ctxt->cr2 = read_cr2(); |
| 48 | ctxt->cr3 = read_cr3(); |
David Fries | e532c06 | 2008-08-17 23:03:40 -0500 | [diff] [blame] | 49 | ctxt->cr4 = read_cr4_safe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 52 | /* Needed by apm.c */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | void save_processor_state(void) |
| 54 | { |
| 55 | __save_processor_state(&saved_context); |
| 56 | } |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 57 | EXPORT_SYMBOL(save_processor_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Shaohua Li | 08967f9 | 2005-10-30 14:59:28 -0800 | [diff] [blame] | 59 | static void do_fpu_end(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Shaohua Li | 08967f9 | 2005-10-30 14:59:28 -0800 | [diff] [blame] | 61 | /* |
| 62 | * Restore FPU regs if necessary. |
| 63 | */ |
| 64 | kernel_fpu_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static void fix_processor_context(void) |
| 68 | { |
| 69 | int cpu = smp_processor_id(); |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 70 | struct tss_struct *t = &per_cpu(init_tss, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 72 | set_tss_desc(cpu, t); /* |
| 73 | * This just modifies memory; should not be |
| 74 | * necessary. But... This is necessary, because |
| 75 | * 386 hardware has concept of busy TSS or some |
| 76 | * similar stupidity. |
| 77 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | load_TR_desc(); /* This does ltr */ |
| 80 | load_LDT(¤t->active_mm->context); /* This does lldt */ |
| 81 | |
| 82 | /* |
| 83 | * Now maybe reload the debug registers |
| 84 | */ |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 85 | if (current->thread.debugreg7) { |
| 86 | set_debugreg(current->thread.debugreg0, 0); |
| 87 | set_debugreg(current->thread.debugreg1, 1); |
| 88 | set_debugreg(current->thread.debugreg2, 2); |
| 89 | set_debugreg(current->thread.debugreg3, 3); |
Vincent Hanquez | 1cc6f12 | 2005-06-23 00:08:43 -0700 | [diff] [blame] | 90 | /* no 4 and 5 */ |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 91 | set_debugreg(current->thread.debugreg6, 6); |
| 92 | set_debugreg(current->thread.debugreg7, 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } |
| 96 | |
Jan Beulich | cae4595 | 2008-01-30 13:31:23 +0100 | [diff] [blame] | 97 | static void __restore_processor_state(struct saved_context *ctxt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* |
| 100 | * control registers |
| 101 | */ |
David Fries | e532c06 | 2008-08-17 23:03:40 -0500 | [diff] [blame] | 102 | /* cr4 was introduced in the Pentium CPU */ |
| 103 | if (ctxt->cr4) |
| 104 | write_cr4(ctxt->cr4); |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 105 | write_cr3(ctxt->cr3); |
| 106 | write_cr2(ctxt->cr2); |
Pavel Machek | 30d6b2f | 2006-05-22 22:35:29 -0700 | [diff] [blame] | 107 | write_cr0(ctxt->cr0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
Pavel Machek | 8d783b3 | 2005-06-25 14:55:14 -0700 | [diff] [blame] | 110 | * now restore the descriptor tables to their proper values |
| 111 | * ltr is done i fix_processor_context(). |
| 112 | */ |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 113 | load_gdt(&ctxt->gdt); |
| 114 | load_idt(&ctxt->idt); |
Pavel Machek | 8d783b3 | 2005-06-25 14:55:14 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * segment registers |
| 118 | */ |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 119 | loadsegment(es, ctxt->es); |
| 120 | loadsegment(fs, ctxt->fs); |
| 121 | loadsegment(gs, ctxt->gs); |
| 122 | loadsegment(ss, ctxt->ss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * sysenter MSRs |
| 126 | */ |
| 127 | if (boot_cpu_has(X86_FEATURE_SEP)) |
Li Shaohua | 6fe940d | 2005-06-25 14:54:53 -0700 | [diff] [blame] | 128 | enable_sep_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Suresh Siddha | 83b8e28 | 2008-08-27 14:57:36 -0700 | [diff] [blame] | 130 | /* |
| 131 | * restore XCR0 for xsave capable cpu's. |
| 132 | */ |
| 133 | if (cpu_has_xsave) |
| 134 | xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask); |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | fix_processor_context(); |
| 137 | do_fpu_end(); |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 138 | mtrr_ap_init(); |
Shaohua Li | 31ab269 | 2005-11-07 00:58:42 -0800 | [diff] [blame] | 139 | mcheck_init(&boot_cpu_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Paolo Ciarrocchi | db96598 | 2008-02-24 11:57:22 +0100 | [diff] [blame] | 142 | /* Needed by apm.c */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | void restore_processor_state(void) |
| 144 | { |
| 145 | __restore_processor_state(&saved_context); |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | EXPORT_SYMBOL(restore_processor_state); |