blob: 6ce0eca847c35ff2b3274b5a068d449d3e87fe7e [file] [log] [blame]
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +01001/*
2 * Suspend and hibernation support for x86-64
3 *
4 * Distribute under GPLv2
5 *
6 * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
7 * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
8 * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
9 */
10
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010011#include <linux/suspend.h>
Sergio Luisf6783d22009-04-28 00:26:22 +020012#include <linux/smp.h>
13
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010014#include <asm/pgtable.h>
Sergio Luisf6783d22009-04-28 00:26:22 +020015#include <asm/proto.h>
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010016#include <asm/mtrr.h>
Sergio Luisf6783d22009-04-28 00:26:22 +020017#include <asm/page.h>
18#include <asm/mce.h>
Suresh Siddha83b8e282008-08-27 14:57:36 -070019#include <asm/xcr.h>
Magnus Damma8af7892009-03-31 15:23:37 -070020#include <asm/suspend.h>
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010021
Sergio Luis833b2ca2009-04-28 00:26:50 +020022#ifdef CONFIG_X86_32
23static struct saved_context saved_context;
24
25unsigned long saved_context_ebx;
26unsigned long saved_context_esp, saved_context_ebp;
27unsigned long saved_context_esi, saved_context_edi;
28unsigned long saved_context_eflags;
29#else
30/* CONFIG_X86_64 */
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010031static void fix_processor_context(void);
32
33struct saved_context saved_context;
Sergio Luis833b2ca2009-04-28 00:26:50 +020034#endif
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010035
36/**
37 * __save_processor_state - save CPU registers before creating a
38 * hibernation image and before restoring the memory state from it
39 * @ctxt - structure to store the registers contents in
40 *
41 * NOTE: If there is a CPU register the modification of which by the
42 * boot kernel (ie. the kernel used for loading the hibernation image)
43 * might affect the operations of the restored target kernel (ie. the one
44 * saved in the hibernation image), then its contents must be saved by this
45 * function. In other words, if kernel A is hibernated and different
46 * kernel B is used for loading the hibernation image into memory, the
47 * kernel A's __save_processor_state() function must save all registers
48 * needed by kernel A, so that it can operate correctly after the resume
49 * regardless of what kernel B does in the meantime.
50 */
51static void __save_processor_state(struct saved_context *ctxt)
52{
53 kernel_fpu_begin();
54
55 /*
56 * descriptor tables
57 */
58 store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
59 store_idt((struct desc_ptr *)&ctxt->idt_limit);
60 store_tr(ctxt->tr);
61
62 /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
63 /*
64 * segment registers
65 */
66 asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
67 asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
68 asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
69 asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
70 asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
71
72 rdmsrl(MSR_FS_BASE, ctxt->fs_base);
73 rdmsrl(MSR_GS_BASE, ctxt->gs_base);
74 rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
75 mtrr_save_fixed_ranges(NULL);
76
77 /*
78 * control registers
79 */
80 rdmsrl(MSR_EFER, ctxt->efer);
81 ctxt->cr0 = read_cr0();
82 ctxt->cr2 = read_cr2();
83 ctxt->cr3 = read_cr3();
84 ctxt->cr4 = read_cr4();
85 ctxt->cr8 = read_cr8();
86}
87
88void save_processor_state(void)
89{
90 __save_processor_state(&saved_context);
91}
92
93static void do_fpu_end(void)
94{
95 /*
96 * Restore FPU regs if necessary
97 */
98 kernel_fpu_end();
99}
100
101/**
102 * __restore_processor_state - restore the contents of CPU registers saved
103 * by __save_processor_state()
104 * @ctxt - structure to load the registers contents from
105 */
106static void __restore_processor_state(struct saved_context *ctxt)
107{
108 /*
109 * control registers
110 */
111 wrmsrl(MSR_EFER, ctxt->efer);
112 write_cr8(ctxt->cr8);
113 write_cr4(ctxt->cr4);
114 write_cr3(ctxt->cr3);
115 write_cr2(ctxt->cr2);
116 write_cr0(ctxt->cr0);
117
118 /*
119 * now restore the descriptor tables to their proper values
120 * ltr is done i fix_processor_context().
121 */
122 load_gdt((const struct desc_ptr *)&ctxt->gdt_limit);
123 load_idt((const struct desc_ptr *)&ctxt->idt_limit);
124
125
126 /*
127 * segment registers
128 */
129 asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
130 asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
131 asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
132 load_gs_index(ctxt->gs);
133 asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
134
135 wrmsrl(MSR_FS_BASE, ctxt->fs_base);
136 wrmsrl(MSR_GS_BASE, ctxt->gs_base);
137 wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
138
Suresh Siddha83b8e282008-08-27 14:57:36 -0700139 /*
140 * restore XCR0 for xsave capable cpu's.
141 */
142 if (cpu_has_xsave)
143 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
144
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +0100145 fix_processor_context();
146
147 do_fpu_end();
148 mtrr_ap_init();
149}
150
151void restore_processor_state(void)
152{
153 __restore_processor_state(&saved_context);
154}
155
156static void fix_processor_context(void)
157{
158 int cpu = smp_processor_id();
159 struct tss_struct *t = &per_cpu(init_tss, cpu);
160
161 /*
162 * This just modifies memory; should not be necessary. But... This
163 * is necessary, because 386 hardware has concept of busy TSS or some
164 * similar stupidity.
165 */
166 set_tss_desc(cpu, t);
167
168 get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS].type = 9;
169
170 syscall_init(); /* This sets MSR_*STAR and related */
171 load_TR_desc(); /* This does ltr */
172 load_LDT(&current->active_mm->context); /* This does lldt */
173
174 /*
175 * Now maybe reload the debug registers
176 */
177 if (current->thread.debugreg7){
178 loaddebug(&current->thread, 0);
179 loaddebug(&current->thread, 1);
180 loaddebug(&current->thread, 2);
181 loaddebug(&current->thread, 3);
182 /* no 4 and 5 */
183 loaddebug(&current->thread, 6);
184 loaddebug(&current->thread, 7);
185 }
186}