blob: 7f9c6da04a4c143a01f9529125b775052cfb35d1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15static struct saved_context saved_context;
16
17unsigned long saved_context_ebx;
18unsigned long saved_context_esp, saved_context_ebp;
19unsigned long saved_context_esi, saved_context_edi;
20unsigned long saved_context_eflags;
21
Jan Beulichcae45952008-01-30 13:31:23 +010022static void __save_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Bernhard Kaindl3ebad592007-05-02 19:27:17 +020024 mtrr_save_fixed_ranges(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 kernel_fpu_begin();
26
27 /*
28 * descriptor tables
29 */
Rusty Russelld3561b72006-12-07 02:14:07 +010030 store_gdt(&ctxt->gdt);
31 store_idt(&ctxt->idt);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -070032 store_tr(ctxt->tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 /*
35 * segment registers
36 */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -070037 savesegment(es, ctxt->es);
38 savesegment(fs, ctxt->fs);
39 savesegment(gs, ctxt->gs);
40 savesegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 /*
Rafael J. Wysockic5759122008-02-09 23:24:09 +010043 * control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070045 ctxt->cr0 = read_cr0();
46 ctxt->cr2 = read_cr2();
47 ctxt->cr3 = read_cr3();
48 ctxt->cr4 = read_cr4();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51void save_processor_state(void)
52{
53 __save_processor_state(&saved_context);
54}
55
Shaohua Li08967f92005-10-30 14:59:28 -080056static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Shaohua Li08967f92005-10-30 14:59:28 -080058 /*
59 * Restore FPU regs if necessary.
60 */
61 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static 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 Torvalds1da177e2005-04-16 15:20:36 -070070
71 load_TR_desc(); /* This does ltr */
72 load_LDT(&current->active_mm->context); /* This does lldt */
73
74 /*
75 * Now maybe reload the debug registers
76 */
Roland McGrath0f534092008-01-30 13:30:59 +010077 if (current->thread.debugreg7) {
78 set_debugreg(current->thread.debugreg0, 0);
79 set_debugreg(current->thread.debugreg1, 1);
80 set_debugreg(current->thread.debugreg2, 2);
81 set_debugreg(current->thread.debugreg3, 3);
Vincent Hanquez1cc6f122005-06-23 00:08:43 -070082 /* no 4 and 5 */
Roland McGrath0f534092008-01-30 13:30:59 +010083 set_debugreg(current->thread.debugreg6, 6);
84 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87}
88
Jan Beulichcae45952008-01-30 13:31:23 +010089static void __restore_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /*
92 * control registers
93 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070094 write_cr4(ctxt->cr4);
95 write_cr3(ctxt->cr3);
96 write_cr2(ctxt->cr2);
Pavel Machek30d6b2f2006-05-22 22:35:29 -070097 write_cr0(ctxt->cr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /*
Pavel Machek8d783b32005-06-25 14:55:14 -0700100 * now restore the descriptor tables to their proper values
101 * ltr is done i fix_processor_context().
102 */
Rusty Russelld3561b72006-12-07 02:14:07 +0100103 load_gdt(&ctxt->gdt);
104 load_idt(&ctxt->idt);
Pavel Machek8d783b32005-06-25 14:55:14 -0700105
106 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * segment registers
108 */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700109 loadsegment(es, ctxt->es);
110 loadsegment(fs, ctxt->fs);
111 loadsegment(gs, ctxt->gs);
112 loadsegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * sysenter MSRs
116 */
117 if (boot_cpu_has(X86_FEATURE_SEP))
Li Shaohua6fe940d2005-06-25 14:54:53 -0700118 enable_sep_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 fix_processor_context();
121 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700122 mtrr_ap_init();
Shaohua Li31ab2692005-11-07 00:58:42 -0800123 mcheck_init(&boot_cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126void restore_processor_state(void)
127{
128 __restore_processor_state(&saved_context);
129}
130
131/* Needed by apm.c */
132EXPORT_SYMBOL(save_processor_state);
133EXPORT_SYMBOL(restore_processor_state);