blob: 02516823f514bd299dcf6c925d701367d2aa85ef [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
10#include <linux/config.h>
Al Viro55679ed2005-09-12 18:49:24 +020011#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/proto.h>
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +020014#include <asm/page.h>
15#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct saved_context saved_context;
18
19unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
20unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
21unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
22unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
23unsigned long saved_context_eflags;
24
25void __save_processor_state(struct saved_context *ctxt)
26{
27 kernel_fpu_begin();
28
29 /*
30 * descriptor tables
31 */
32 asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
33 asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 asm volatile ("str %0" : "=m" (ctxt->tr));
35
36 /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
37 /* EFER should be constant for kernel version, no need to handle it. */
38 /*
39 * segment registers
40 */
41 asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
42 asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
43 asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
44 asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
45 asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
46
47 rdmsrl(MSR_FS_BASE, ctxt->fs_base);
48 rdmsrl(MSR_GS_BASE, ctxt->gs_base);
49 rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
50
51 /*
52 * control registers
53 */
54 asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
55 asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
56 asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
57 asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
Pavel Machek8d783b32005-06-25 14:55:14 -070058 asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61void save_processor_state(void)
62{
63 __save_processor_state(&saved_context);
64}
65
Shaohua Li08967f92005-10-30 14:59:28 -080066static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Shaohua Li08967f92005-10-30 14:59:28 -080068 /*
69 * Restore FPU regs if necessary
70 */
71 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74void __restore_processor_state(struct saved_context *ctxt)
75{
76 /*
77 * control registers
78 */
Pavel Machek8d783b32005-06-25 14:55:14 -070079 asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
81 asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
82 asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
83 asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
84
85 /*
Pavel Machek8d783b32005-06-25 14:55:14 -070086 * now restore the descriptor tables to their proper values
87 * ltr is done i fix_processor_context().
88 */
89 asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
90 asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
91
92 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * segment registers
94 */
95 asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
96 asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
97 asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
98 load_gs_index(ctxt->gs);
99 asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
100
101 wrmsrl(MSR_FS_BASE, ctxt->fs_base);
102 wrmsrl(MSR_GS_BASE, ctxt->gs_base);
103 wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 fix_processor_context();
106
107 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700108 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111void restore_processor_state(void)
112{
113 __restore_processor_state(&saved_context);
114}
115
116void fix_processor_context(void)
117{
118 int cpu = smp_processor_id();
119 struct tss_struct *t = &per_cpu(init_tss, cpu);
120
121 set_tss_desc(cpu,t); /* This just modifies memory; should not be neccessary. But... This is neccessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
122
123 cpu_gdt_table[cpu][GDT_ENTRY_TSS].type = 9;
124
125 syscall_init(); /* This sets MSR_*STAR and related */
126 load_TR_desc(); /* This does ltr */
127 load_LDT(&current->active_mm->context); /* This does lldt */
128
129 /*
130 * Now maybe reload the debug registers
131 */
132 if (current->thread.debugreg7){
133 loaddebug(&current->thread, 0);
134 loaddebug(&current->thread, 1);
135 loaddebug(&current->thread, 2);
136 loaddebug(&current->thread, 3);
137 /* no 4 and 5 */
138 loaddebug(&current->thread, 6);
139 loaddebug(&current->thread, 7);
140 }
141
142}
143
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200144#ifdef CONFIG_SOFTWARE_SUSPEND
145/* Defined in arch/x86_64/kernel/suspend_asm.S */
146extern int restore_image(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200148pgd_t *temp_level4_pgt;
149
150static void **pages;
151
152static inline void *__add_page(void)
153{
154 void **c;
155
156 c = (void **)get_usable_page(GFP_ATOMIC);
157 if (c) {
158 *c = pages;
159 pages = c;
160 }
161 return c;
162}
163
164static inline void *__next_page(void)
165{
166 void **c;
167
168 c = pages;
169 if (c) {
170 pages = *c;
171 *c = NULL;
172 }
173 return c;
174}
175
176/*
177 * Try to allocate as many usable pages as needed and daisy chain them.
178 * If one allocation fails, free the pages allocated so far
179 */
180static int alloc_usable_pages(unsigned long n)
181{
182 void *p;
183
184 pages = NULL;
185 do
186 if (!__add_page())
187 break;
188 while (--n);
189 if (n) {
190 p = __next_page();
191 while (p) {
192 free_page((unsigned long)p);
193 p = __next_page();
194 }
195 return -ENOMEM;
196 }
197 return 0;
198}
199
200static void res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
201{
202 long i, j;
203
204 i = pud_index(address);
205 pud = pud + i;
206 for (; i < PTRS_PER_PUD; pud++, i++) {
207 unsigned long paddr;
208 pmd_t *pmd;
209
210 paddr = address + i*PUD_SIZE;
211 if (paddr >= end)
212 break;
213
214 pmd = (pmd_t *)__next_page();
215 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
216 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
217 unsigned long pe;
218
219 if (paddr >= end)
220 break;
221 pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr;
222 pe &= __supported_pte_mask;
223 set_pmd(pmd, __pmd(pe));
224 }
225 }
226}
227
228static void set_up_temporary_mappings(void)
229{
230 unsigned long start, end, next;
231
232 temp_level4_pgt = (pgd_t *)__next_page();
233
234 /* It is safe to reuse the original kernel mapping */
235 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
236 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
237
238 /* Set up the direct mapping from scratch */
239 start = (unsigned long)pfn_to_kaddr(0);
240 end = (unsigned long)pfn_to_kaddr(end_pfn);
241
242 for (; start < end; start = next) {
243 pud_t *pud = (pud_t *)__next_page();
244 next = start + PGDIR_SIZE;
245 if (next > end)
246 next = end;
247 res_phys_pud_init(pud, __pa(start), __pa(next));
248 set_pgd(temp_level4_pgt + pgd_index(start),
249 mk_kernel_pgd(__pa(pud)));
250 }
251}
252
253int swsusp_arch_resume(void)
254{
255 unsigned long n;
256
257 n = ((end_pfn << PAGE_SHIFT) + PUD_SIZE - 1) >> PUD_SHIFT;
258 n += (n + PTRS_PER_PUD - 1) / PTRS_PER_PUD + 1;
259 pr_debug("swsusp_arch_resume(): pages needed = %lu\n", n);
260 if (alloc_usable_pages(n)) {
261 free_eaten_memory();
262 return -ENOMEM;
263 }
264 /* We have got enough memory and from now on we cannot recover */
265 set_up_temporary_mappings();
266 restore_image();
267 return 0;
268}
269#endif /* CONFIG_SOFTWARE_SUSPEND */