blob: ce702c5b3a2c13fded43ab9629c6210352b3772a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/suspend.h>
Alexey Dobriyan27b07da2006-06-23 02:04:18 -070012#include <asm/mtrr.h>
Alexey Dobriyana03a3e22006-06-23 02:04:20 -070013#include <asm/mce.h>
Suresh Siddha83b8e282008-08-27 14:57:36 -070014#include <asm/xcr.h>
Magnus Damma8af7892009-03-31 15:23:37 -070015#include <asm/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17static struct saved_context saved_context;
18
19unsigned long saved_context_ebx;
20unsigned long saved_context_esp, saved_context_ebp;
21unsigned long saved_context_esi, saved_context_edi;
22unsigned long saved_context_eflags;
23
Jan Beulichcae45952008-01-30 13:31:23 +010024static void __save_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Bernhard Kaindl3ebad592007-05-02 19:27:17 +020026 mtrr_save_fixed_ranges(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 kernel_fpu_begin();
28
29 /*
30 * descriptor tables
31 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010032 store_gdt(&ctxt->gdt);
33 store_idt(&ctxt->idt);
34 store_tr(ctxt->tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 /*
37 * segment registers
38 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010039 savesegment(es, ctxt->es);
40 savesegment(fs, ctxt->fs);
41 savesegment(gs, ctxt->gs);
42 savesegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 /*
Rafael J. Wysockic5759122008-02-09 23:24:09 +010045 * control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070047 ctxt->cr0 = read_cr0();
48 ctxt->cr2 = read_cr2();
49 ctxt->cr3 = read_cr3();
David Friese532c062008-08-17 23:03:40 -050050 ctxt->cr4 = read_cr4_safe();
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010053/* Needed by apm.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054void save_processor_state(void)
55{
56 __save_processor_state(&saved_context);
57}
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010058EXPORT_SYMBOL(save_processor_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Shaohua Li08967f92005-10-30 14:59:28 -080060static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Shaohua Li08967f92005-10-30 14:59:28 -080062 /*
63 * Restore FPU regs if necessary.
64 */
65 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void fix_processor_context(void)
69{
70 int cpu = smp_processor_id();
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010071 struct tss_struct *t = &per_cpu(init_tss, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010073 set_tss_desc(cpu, t); /*
74 * This just modifies memory; should not be
75 * necessary. But... This is necessary, because
76 * 386 hardware has concept of busy TSS or some
77 * similar stupidity.
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 load_TR_desc(); /* This does ltr */
81 load_LDT(&current->active_mm->context); /* This does lldt */
82
83 /*
84 * Now maybe reload the debug registers
85 */
Roland McGrath0f534092008-01-30 13:30:59 +010086 if (current->thread.debugreg7) {
87 set_debugreg(current->thread.debugreg0, 0);
88 set_debugreg(current->thread.debugreg1, 1);
89 set_debugreg(current->thread.debugreg2, 2);
90 set_debugreg(current->thread.debugreg3, 3);
Vincent Hanquez1cc6f122005-06-23 00:08:43 -070091 /* no 4 and 5 */
Roland McGrath0f534092008-01-30 13:30:59 +010092 set_debugreg(current->thread.debugreg6, 6);
93 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96}
97
Jan Beulichcae45952008-01-30 13:31:23 +010098static void __restore_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /*
101 * control registers
102 */
David Friese532c062008-08-17 23:03:40 -0500103 /* cr4 was introduced in the Pentium CPU */
104 if (ctxt->cr4)
105 write_cr4(ctxt->cr4);
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700106 write_cr3(ctxt->cr3);
107 write_cr2(ctxt->cr2);
Pavel Machek30d6b2f2006-05-22 22:35:29 -0700108 write_cr0(ctxt->cr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /*
Pavel Machek8d783b32005-06-25 14:55:14 -0700111 * now restore the descriptor tables to their proper values
112 * ltr is done i fix_processor_context().
113 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100114 load_gdt(&ctxt->gdt);
115 load_idt(&ctxt->idt);
Pavel Machek8d783b32005-06-25 14:55:14 -0700116
117 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * segment registers
119 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100120 loadsegment(es, ctxt->es);
121 loadsegment(fs, ctxt->fs);
122 loadsegment(gs, ctxt->gs);
123 loadsegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * sysenter MSRs
127 */
128 if (boot_cpu_has(X86_FEATURE_SEP))
Li Shaohua6fe940d2005-06-25 14:54:53 -0700129 enable_sep_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Suresh Siddha83b8e282008-08-27 14:57:36 -0700131 /*
132 * restore XCR0 for xsave capable cpu's.
133 */
134 if (cpu_has_xsave)
135 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 fix_processor_context();
138 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700139 mtrr_ap_init();
Shaohua Li31ab2692005-11-07 00:58:42 -0800140 mcheck_init(&boot_cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100143/* Needed by apm.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144void restore_processor_state(void)
145{
146 __restore_processor_state(&saved_context);
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148EXPORT_SYMBOL(restore_processor_state);