blob: d3e083dea7202c8b5c82e1d7378d947f9f2d42a3 [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 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010030 store_gdt(&ctxt->gdt);
31 store_idt(&ctxt->idt);
32 store_tr(ctxt->tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 /*
35 * segment registers
36 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010037 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();
David Friese532c062008-08-17 23:03:40 -050048 ctxt->cr4 = read_cr4_safe();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010051/* Needed by apm.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052void save_processor_state(void)
53{
54 __save_processor_state(&saved_context);
55}
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010056EXPORT_SYMBOL(save_processor_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Shaohua Li08967f92005-10-30 14:59:28 -080058static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Shaohua Li08967f92005-10-30 14:59:28 -080060 /*
61 * Restore FPU regs if necessary.
62 */
63 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void fix_processor_context(void)
67{
68 int cpu = smp_processor_id();
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010069 struct tss_struct *t = &per_cpu(init_tss, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010071 set_tss_desc(cpu, t); /*
72 * This just modifies memory; should not be
73 * necessary. But... This is necessary, because
74 * 386 hardware has concept of busy TSS or some
75 * similar stupidity.
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 load_TR_desc(); /* This does ltr */
79 load_LDT(&current->active_mm->context); /* This does lldt */
80
81 /*
82 * Now maybe reload the debug registers
83 */
Roland McGrath0f534092008-01-30 13:30:59 +010084 if (current->thread.debugreg7) {
85 set_debugreg(current->thread.debugreg0, 0);
86 set_debugreg(current->thread.debugreg1, 1);
87 set_debugreg(current->thread.debugreg2, 2);
88 set_debugreg(current->thread.debugreg3, 3);
Vincent Hanquez1cc6f122005-06-23 00:08:43 -070089 /* no 4 and 5 */
Roland McGrath0f534092008-01-30 13:30:59 +010090 set_debugreg(current->thread.debugreg6, 6);
91 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94}
95
Jan Beulichcae45952008-01-30 13:31:23 +010096static void __restore_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /*
99 * control registers
100 */
David Friese532c062008-08-17 23:03:40 -0500101 /* cr4 was introduced in the Pentium CPU */
102 if (ctxt->cr4)
103 write_cr4(ctxt->cr4);
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700104 write_cr3(ctxt->cr3);
105 write_cr2(ctxt->cr2);
Pavel Machek30d6b2f2006-05-22 22:35:29 -0700106 write_cr0(ctxt->cr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /*
Pavel Machek8d783b32005-06-25 14:55:14 -0700109 * now restore the descriptor tables to their proper values
110 * ltr is done i fix_processor_context().
111 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100112 load_gdt(&ctxt->gdt);
113 load_idt(&ctxt->idt);
Pavel Machek8d783b32005-06-25 14:55:14 -0700114
115 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * segment registers
117 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100118 loadsegment(es, ctxt->es);
119 loadsegment(fs, ctxt->fs);
120 loadsegment(gs, ctxt->gs);
121 loadsegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * sysenter MSRs
125 */
126 if (boot_cpu_has(X86_FEATURE_SEP))
Li Shaohua6fe940d2005-06-25 14:54:53 -0700127 enable_sep_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 fix_processor_context();
130 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700131 mtrr_ap_init();
Shaohua Li31ab2692005-11-07 00:58:42 -0800132 mcheck_init(&boot_cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100135/* Needed by apm.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136void restore_processor_state(void)
137{
138 __restore_processor_state(&saved_context);
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140EXPORT_SYMBOL(restore_processor_state);