blob: 998fd3ec0d68a5d252b9dc4f1e1a41dee4777a89 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022void __save_processor_state(struct saved_context *ctxt)
23{
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 /*
43 * control registers
44 */
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 */
77 if (current->thread.debugreg[7]){
Vincent Hanquez1cc6f122005-06-23 00:08:43 -070078 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 Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87}
88
89void __restore_processor_state(struct saved_context *ctxt)
90{
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);