blob: 2e5efaaf8800ee2d402aca5b0b9bb1a00061458e [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
Al Viro55679ed2005-09-12 18:49:24 +020010#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/proto.h>
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +020013#include <asm/page.h>
14#include <asm/pgtable.h>
Bernhard Kaindl3ebad592007-05-02 19:27:17 +020015#include <asm/mtrr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Vivek Goyal49c3df62007-05-02 19:27:07 +020017/* References to section boundaries */
18extern const void __nosave_begin, __nosave_end;
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020struct saved_context saved_context;
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022void __save_processor_state(struct saved_context *ctxt)
23{
24 kernel_fpu_begin();
25
26 /*
27 * descriptor tables
28 */
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +020029 store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
30 store_idt((struct desc_ptr *)&ctxt->idt_limit);
31 store_tr(ctxt->tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 /*
35 * segment registers
36 */
37 asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
38 asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
39 asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
40 asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
41 asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
42
43 rdmsrl(MSR_FS_BASE, ctxt->fs_base);
44 rdmsrl(MSR_GS_BASE, ctxt->gs_base);
45 rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
Bernhard Kaindl3ebad592007-05-02 19:27:17 +020046 mtrr_save_fixed_ranges(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 /*
49 * control registers
50 */
Vivek Goyal3c321bc2007-05-02 19:27:07 +020051 rdmsrl(MSR_EFER, ctxt->efer);
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +020052 ctxt->cr0 = read_cr0();
53 ctxt->cr2 = read_cr2();
54 ctxt->cr3 = read_cr3();
55 ctxt->cr4 = read_cr4();
56 ctxt->cr8 = read_cr8();
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59void save_processor_state(void)
60{
61 __save_processor_state(&saved_context);
62}
63
Shaohua Li08967f92005-10-30 14:59:28 -080064static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Shaohua Li08967f92005-10-30 14:59:28 -080066 /*
67 * Restore FPU regs if necessary
68 */
69 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72void __restore_processor_state(struct saved_context *ctxt)
73{
74 /*
75 * control registers
76 */
Vivek Goyal3c321bc2007-05-02 19:27:07 +020077 wrmsrl(MSR_EFER, ctxt->efer);
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +020078 write_cr8(ctxt->cr8);
79 write_cr4(ctxt->cr4);
80 write_cr3(ctxt->cr3);
81 write_cr2(ctxt->cr2);
82 write_cr0(ctxt->cr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /*
Pavel Machek8d783b32005-06-25 14:55:14 -070085 * now restore the descriptor tables to their proper values
86 * ltr is done i fix_processor_context().
87 */
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +020088 load_gdt((const struct desc_ptr *)&ctxt->gdt_limit);
89 load_idt((const struct desc_ptr *)&ctxt->idt_limit);
90
Pavel Machek8d783b32005-06-25 14:55:14 -070091
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
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200121 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 -0700122
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100123 cpu_gdt(cpu)[GDT_ENTRY_TSS].type = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
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. Wysockib0cb1a12007-07-29 23:24:36 +0200144#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200145/* Defined in arch/x86_64/kernel/suspend_asm.S */
146extern int restore_image(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700148/*
149 * Address to jump to in the last phase of restore in order to get to the image
150 * kernel's text (this value is passed in the image header).
151 */
152unsigned long restore_jump_address;
153
Rafael J. Wysockic30bb682007-10-18 03:04:54 -0700154/*
155 * Value of the cr3 register from before the hibernation (this value is passed
156 * in the image header).
157 */
158unsigned long restore_cr3;
159
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200160pgd_t *temp_level4_pgt;
161
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700162void *relocated_restore_code;
163
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800164static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200165{
166 long i, j;
167
168 i = pud_index(address);
169 pud = pud + i;
170 for (; i < PTRS_PER_PUD; pud++, i++) {
171 unsigned long paddr;
172 pmd_t *pmd;
173
174 paddr = address + i*PUD_SIZE;
175 if (paddr >= end)
176 break;
177
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800178 pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
179 if (!pmd)
180 return -ENOMEM;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200181 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
182 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
183 unsigned long pe;
184
185 if (paddr >= end)
186 break;
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700187 pe = __PAGE_KERNEL_LARGE_EXEC | paddr;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200188 pe &= __supported_pte_mask;
189 set_pmd(pmd, __pmd(pe));
190 }
191 }
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800192 return 0;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200193}
194
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800195static int set_up_temporary_mappings(void)
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200196{
197 unsigned long start, end, next;
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800198 int error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200199
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800200 temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
201 if (!temp_level4_pgt)
202 return -ENOMEM;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200203
Andrew Morton5867a782007-12-17 16:19:45 -0800204 /* It is safe to reuse the original kernel mapping */
205 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
206 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
207
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200208 /* Set up the direct mapping from scratch */
209 start = (unsigned long)pfn_to_kaddr(0);
210 end = (unsigned long)pfn_to_kaddr(end_pfn);
211
212 for (; start < end; start = next) {
Andrew Morton5867a782007-12-17 16:19:45 -0800213 pud_t *pud = (pud_t *)get_safe_page(GFP_ATOMIC);
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800214 if (!pud)
215 return -ENOMEM;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200216 next = start + PGDIR_SIZE;
217 if (next > end)
218 next = end;
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800219 if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
220 return error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200221 set_pgd(temp_level4_pgt + pgd_index(start),
222 mk_kernel_pgd(__pa(pud)));
223 }
Andrew Morton5867a782007-12-17 16:19:45 -0800224 return 0;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200225}
226
227int swsusp_arch_resume(void)
228{
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800229 int error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200230
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200231 /* We have got enough memory and from now on we cannot recover */
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800232 if ((error = set_up_temporary_mappings()))
233 return error;
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700234
235 relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
236 if (!relocated_restore_code)
237 return -ENOMEM;
238 memcpy(relocated_restore_code, &core_restore_code,
239 &restore_registers - &core_restore_code);
240
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200241 restore_image();
242 return 0;
243}
Vivek Goyal49c3df62007-05-02 19:27:07 +0200244
245/*
246 * pfn_is_nosave - check if given pfn is in the 'nosave' section
247 */
248
249int pfn_is_nosave(unsigned long pfn)
250{
251 unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
252 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
253 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
254}
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700255
256struct restore_data_record {
257 unsigned long jump_address;
Rafael J. Wysockic30bb682007-10-18 03:04:54 -0700258 unsigned long cr3;
259 unsigned long magic;
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700260};
261
262#define RESTORE_MAGIC 0x0123456789ABCDEFUL
263
264/**
265 * arch_hibernation_header_save - populate the architecture specific part
266 * of a hibernation image header
267 * @addr: address to save the data at
268 */
269int arch_hibernation_header_save(void *addr, unsigned int max_size)
270{
271 struct restore_data_record *rdr = addr;
272
273 if (max_size < sizeof(struct restore_data_record))
274 return -EOVERFLOW;
275 rdr->jump_address = restore_jump_address;
Rafael J. Wysockic30bb682007-10-18 03:04:54 -0700276 rdr->cr3 = restore_cr3;
277 rdr->magic = RESTORE_MAGIC;
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700278 return 0;
279}
280
281/**
282 * arch_hibernation_header_restore - read the architecture specific data
283 * from the hibernation image header
284 * @addr: address to read the data from
285 */
286int arch_hibernation_header_restore(void *addr)
287{
288 struct restore_data_record *rdr = addr;
289
290 restore_jump_address = rdr->jump_address;
Rafael J. Wysockic30bb682007-10-18 03:04:54 -0700291 restore_cr3 = rdr->cr3;
292 return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700293}
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200294#endif /* CONFIG_HIBERNATION */