blob: 274d06082f4851bfcf71c621f7a62db447e54876 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16static struct saved_context saved_context;
17
18unsigned long saved_context_ebx;
19unsigned long saved_context_esp, saved_context_ebp;
20unsigned long saved_context_esi, saved_context_edi;
21unsigned long saved_context_eflags;
22
Jan Beulichcae45952008-01-30 13:31:23 +010023static void __save_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Bernhard Kaindl3ebad592007-05-02 19:27:17 +020025 mtrr_save_fixed_ranges(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 kernel_fpu_begin();
27
28 /*
29 * descriptor tables
30 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010031 store_gdt(&ctxt->gdt);
32 store_idt(&ctxt->idt);
33 store_tr(ctxt->tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 /*
36 * segment registers
37 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010038 savesegment(es, ctxt->es);
39 savesegment(fs, ctxt->fs);
40 savesegment(gs, ctxt->gs);
41 savesegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 /*
Rafael J. Wysockic5759122008-02-09 23:24:09 +010044 * control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070046 ctxt->cr0 = read_cr0();
47 ctxt->cr2 = read_cr2();
48 ctxt->cr3 = read_cr3();
David Friese532c062008-08-17 23:03:40 -050049 ctxt->cr4 = read_cr4_safe();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010052/* Needed by apm.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053void save_processor_state(void)
54{
55 __save_processor_state(&saved_context);
56}
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010057EXPORT_SYMBOL(save_processor_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Shaohua Li08967f92005-10-30 14:59:28 -080059static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Shaohua Li08967f92005-10-30 14:59:28 -080061 /*
62 * Restore FPU regs if necessary.
63 */
64 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void fix_processor_context(void)
68{
69 int cpu = smp_processor_id();
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010070 struct tss_struct *t = &per_cpu(init_tss, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Paolo Ciarrocchidb965982008-02-24 11:57:22 +010072 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 Torvalds1da177e2005-04-16 15:20:36 -070078
79 load_TR_desc(); /* This does ltr */
80 load_LDT(&current->active_mm->context); /* This does lldt */
81
82 /*
83 * Now maybe reload the debug registers
84 */
Roland McGrath0f534092008-01-30 13:30:59 +010085 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 Hanquez1cc6f122005-06-23 00:08:43 -070090 /* no 4 and 5 */
Roland McGrath0f534092008-01-30 13:30:59 +010091 set_debugreg(current->thread.debugreg6, 6);
92 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
95}
96
Jan Beulichcae45952008-01-30 13:31:23 +010097static void __restore_processor_state(struct saved_context *ctxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /*
100 * control registers
101 */
David Friese532c062008-08-17 23:03:40 -0500102 /* cr4 was introduced in the Pentium CPU */
103 if (ctxt->cr4)
104 write_cr4(ctxt->cr4);
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700105 write_cr3(ctxt->cr3);
106 write_cr2(ctxt->cr2);
Pavel Machek30d6b2f2006-05-22 22:35:29 -0700107 write_cr0(ctxt->cr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /*
Pavel Machek8d783b32005-06-25 14:55:14 -0700110 * now restore the descriptor tables to their proper values
111 * ltr is done i fix_processor_context().
112 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100113 load_gdt(&ctxt->gdt);
114 load_idt(&ctxt->idt);
Pavel Machek8d783b32005-06-25 14:55:14 -0700115
116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * segment registers
118 */
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100119 loadsegment(es, ctxt->es);
120 loadsegment(fs, ctxt->fs);
121 loadsegment(gs, ctxt->gs);
122 loadsegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * sysenter MSRs
126 */
127 if (boot_cpu_has(X86_FEATURE_SEP))
Li Shaohua6fe940d2005-06-25 14:54:53 -0700128 enable_sep_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Suresh Siddha83b8e282008-08-27 14:57:36 -0700130 /*
131 * restore XCR0 for xsave capable cpu's.
132 */
133 if (cpu_has_xsave)
134 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 fix_processor_context();
137 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700138 mtrr_ap_init();
Shaohua Li31ab2692005-11-07 00:58:42 -0800139 mcheck_init(&boot_cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Paolo Ciarrocchidb965982008-02-24 11:57:22 +0100142/* Needed by apm.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143void restore_processor_state(void)
144{
145 __restore_processor_state(&saved_context);
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147EXPORT_SYMBOL(restore_processor_state);