2 * Suspend support specific for i386.
4 * Distribute under GPLv2
6 * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
7 * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
10 #include <linux/smp.h>
11 #include <linux/suspend.h>
12 #include <asm/proto.h>
14 #include <asm/pgtable.h>
17 /* References to section boundaries */
18 extern const void __nosave_begin, __nosave_end;
20 struct saved_context saved_context;
22 unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
23 unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
24 unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
25 unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
26 unsigned long saved_context_eflags;
28 void __save_processor_state(struct saved_context *ctxt)
35 store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
36 store_idt((struct desc_ptr *)&ctxt->idt_limit);
39 /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
43 asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
44 asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
45 asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
46 asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
47 asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
49 rdmsrl(MSR_FS_BASE, ctxt->fs_base);
50 rdmsrl(MSR_GS_BASE, ctxt->gs_base);
51 rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
52 mtrr_save_fixed_ranges(NULL);
57 rdmsrl(MSR_EFER, ctxt->efer);
58 ctxt->cr0 = read_cr0();
59 ctxt->cr2 = read_cr2();
60 ctxt->cr3 = read_cr3();
61 ctxt->cr4 = read_cr4();
62 ctxt->cr8 = read_cr8();
65 void save_processor_state(void)
67 __save_processor_state(&saved_context);
70 static void do_fpu_end(void)
73 * Restore FPU regs if necessary
78 void __restore_processor_state(struct saved_context *ctxt)
83 wrmsrl(MSR_EFER, ctxt->efer);
91 * now restore the descriptor tables to their proper values
92 * ltr is done i fix_processor_context().
94 load_gdt((const struct desc_ptr *)&ctxt->gdt_limit);
95 load_idt((const struct desc_ptr *)&ctxt->idt_limit);
101 asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
102 asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
103 asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
104 load_gs_index(ctxt->gs);
105 asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
107 wrmsrl(MSR_FS_BASE, ctxt->fs_base);
108 wrmsrl(MSR_GS_BASE, ctxt->gs_base);
109 wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
111 fix_processor_context();
117 void restore_processor_state(void)
119 __restore_processor_state(&saved_context);
122 void fix_processor_context(void)
124 int cpu = smp_processor_id();
125 struct tss_struct *t = &per_cpu(init_tss, cpu);
127 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. */
129 cpu_gdt(cpu)[GDT_ENTRY_TSS].type = 9;
131 syscall_init(); /* This sets MSR_*STAR and related */
132 load_TR_desc(); /* This does ltr */
133 load_LDT(¤t->active_mm->context); /* This does lldt */
136 * Now maybe reload the debug registers
138 if (current->thread.debugreg7){
139 loaddebug(¤t->thread, 0);
140 loaddebug(¤t->thread, 1);
141 loaddebug(¤t->thread, 2);
142 loaddebug(¤t->thread, 3);
144 loaddebug(¤t->thread, 6);
145 loaddebug(¤t->thread, 7);
150 #ifdef CONFIG_HIBERNATION
151 /* Defined in arch/x86_64/kernel/suspend_asm.S */
152 extern int restore_image(void);
155 * Address to jump to in the last phase of restore in order to get to the image
156 * kernel's text (this value is passed in the image header).
158 unsigned long restore_jump_address;
161 * Value of the cr3 register from before the hibernation (this value is passed
162 * in the image header).
164 unsigned long restore_cr3;
166 pgd_t *temp_level4_pgt;
168 void *relocated_restore_code;
170 static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
174 i = pud_index(address);
176 for (; i < PTRS_PER_PUD; pud++, i++) {
180 paddr = address + i*PUD_SIZE;
184 pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
187 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
188 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
193 pe = __PAGE_KERNEL_LARGE_EXEC | paddr;
194 pe &= __supported_pte_mask;
195 set_pmd(pmd, __pmd(pe));
201 static int res_kernel_text_pud_init(pud_t *pud, unsigned long start)
206 pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
209 set_pud(pud + pud_index(start), __pud(__pa(pmd) | _KERNPG_TABLE));
210 for (paddr = 0; paddr < KERNEL_TEXT_SIZE; pmd++, paddr += PMD_SIZE) {
213 pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr;
214 pe &= __supported_pte_mask;
215 set_pmd(pmd, __pmd(pe));
221 static int set_up_temporary_mappings(void)
223 unsigned long start, end, next;
227 temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
228 if (!temp_level4_pgt)
231 /* Set up the direct mapping from scratch */
232 start = (unsigned long)pfn_to_kaddr(0);
233 end = (unsigned long)pfn_to_kaddr(end_pfn);
235 for (; start < end; start = next) {
236 pud = (pud_t *)get_safe_page(GFP_ATOMIC);
239 next = start + PGDIR_SIZE;
242 if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
244 set_pgd(temp_level4_pgt + pgd_index(start),
245 mk_kernel_pgd(__pa(pud)));
248 /* Set up the kernel text mapping from scratch */
249 pud = (pud_t *)get_safe_page(GFP_ATOMIC);
252 error = res_kernel_text_pud_init(pud, __START_KERNEL_map);
254 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
255 __pgd(__pa(pud) | _PAGE_TABLE));
260 int swsusp_arch_resume(void)
264 /* We have got enough memory and from now on we cannot recover */
265 if ((error = set_up_temporary_mappings()))
268 relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
269 if (!relocated_restore_code)
271 memcpy(relocated_restore_code, &core_restore_code,
272 &restore_registers - &core_restore_code);
279 * pfn_is_nosave - check if given pfn is in the 'nosave' section
282 int pfn_is_nosave(unsigned long pfn)
284 unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
285 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
286 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
289 struct restore_data_record {
290 unsigned long jump_address;
295 #define RESTORE_MAGIC 0x0123456789ABCDEFUL
298 * arch_hibernation_header_save - populate the architecture specific part
299 * of a hibernation image header
300 * @addr: address to save the data at
302 int arch_hibernation_header_save(void *addr, unsigned int max_size)
304 struct restore_data_record *rdr = addr;
306 if (max_size < sizeof(struct restore_data_record))
308 rdr->jump_address = restore_jump_address;
309 rdr->cr3 = restore_cr3;
310 rdr->magic = RESTORE_MAGIC;
315 * arch_hibernation_header_restore - read the architecture specific data
316 * from the hibernation image header
317 * @addr: address to read the data from
319 int arch_hibernation_header_restore(void *addr)
321 struct restore_data_record *rdr = addr;
323 restore_jump_address = rdr->jump_address;
324 restore_cr3 = rdr->cr3;
325 return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;
327 #endif /* CONFIG_HIBERNATION */