blob: 91f7e678bae72e7f5e951919633ff15d6f12451a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16struct saved_context saved_context;
17
18unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
19unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
20unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
21unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
22unsigned long saved_context_eflags;
23
24void __save_processor_state(struct saved_context *ctxt)
25{
26 kernel_fpu_begin();
27
28 /*
29 * descriptor tables
30 */
31 asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
32 asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 asm volatile ("str %0" : "=m" (ctxt->tr));
34
35 /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
36 /* EFER should be constant for kernel version, no need to handle it. */
37 /*
38 * segment registers
39 */
40 asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
41 asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
42 asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
43 asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
44 asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
45
46 rdmsrl(MSR_FS_BASE, ctxt->fs_base);
47 rdmsrl(MSR_GS_BASE, ctxt->gs_base);
48 rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
49
50 /*
51 * control registers
52 */
53 asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
54 asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
55 asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
56 asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
Pavel Machek8d783b32005-06-25 14:55:14 -070057 asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60void save_processor_state(void)
61{
62 __save_processor_state(&saved_context);
63}
64
Shaohua Li08967f92005-10-30 14:59:28 -080065static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Shaohua Li08967f92005-10-30 14:59:28 -080067 /*
68 * Restore FPU regs if necessary
69 */
70 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73void __restore_processor_state(struct saved_context *ctxt)
74{
75 /*
76 * control registers
77 */
Pavel Machek8d783b32005-06-25 14:55:14 -070078 asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
80 asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
81 asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
82 asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
83
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 */
88 asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
89 asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
90
91 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * segment registers
93 */
94 asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
95 asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
96 asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
97 load_gs_index(ctxt->gs);
98 asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
99
100 wrmsrl(MSR_FS_BASE, ctxt->fs_base);
101 wrmsrl(MSR_GS_BASE, ctxt->gs_base);
102 wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 fix_processor_context();
105
106 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700107 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110void restore_processor_state(void)
111{
112 __restore_processor_state(&saved_context);
113}
114
115void fix_processor_context(void)
116{
117 int cpu = smp_processor_id();
118 struct tss_struct *t = &per_cpu(init_tss, cpu);
119
120 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. */
121
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100122 cpu_gdt(cpu)[GDT_ENTRY_TSS].type = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 syscall_init(); /* This sets MSR_*STAR and related */
125 load_TR_desc(); /* This does ltr */
126 load_LDT(&current->active_mm->context); /* This does lldt */
127
128 /*
129 * Now maybe reload the debug registers
130 */
131 if (current->thread.debugreg7){
132 loaddebug(&current->thread, 0);
133 loaddebug(&current->thread, 1);
134 loaddebug(&current->thread, 2);
135 loaddebug(&current->thread, 3);
136 /* no 4 and 5 */
137 loaddebug(&current->thread, 6);
138 loaddebug(&current->thread, 7);
139 }
140
141}
142
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200143#ifdef CONFIG_SOFTWARE_SUSPEND
144/* Defined in arch/x86_64/kernel/suspend_asm.S */
145extern int restore_image(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200147pgd_t *temp_level4_pgt;
148
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800149static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200150{
151 long i, j;
152
153 i = pud_index(address);
154 pud = pud + i;
155 for (; i < PTRS_PER_PUD; pud++, i++) {
156 unsigned long paddr;
157 pmd_t *pmd;
158
159 paddr = address + i*PUD_SIZE;
160 if (paddr >= end)
161 break;
162
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800163 pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
164 if (!pmd)
165 return -ENOMEM;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200166 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
167 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
168 unsigned long pe;
169
170 if (paddr >= end)
171 break;
172 pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr;
173 pe &= __supported_pte_mask;
174 set_pmd(pmd, __pmd(pe));
175 }
176 }
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800177 return 0;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200178}
179
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800180static int set_up_temporary_mappings(void)
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200181{
182 unsigned long start, end, next;
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800183 int error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200184
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800185 temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
186 if (!temp_level4_pgt)
187 return -ENOMEM;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200188
189 /* It is safe to reuse the original kernel mapping */
190 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
191 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
192
193 /* Set up the direct mapping from scratch */
194 start = (unsigned long)pfn_to_kaddr(0);
195 end = (unsigned long)pfn_to_kaddr(end_pfn);
196
197 for (; start < end; start = next) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800198 pud_t *pud = (pud_t *)get_safe_page(GFP_ATOMIC);
199 if (!pud)
200 return -ENOMEM;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200201 next = start + PGDIR_SIZE;
202 if (next > end)
203 next = end;
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800204 if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
205 return error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200206 set_pgd(temp_level4_pgt + pgd_index(start),
207 mk_kernel_pgd(__pa(pud)));
208 }
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800209 return 0;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200210}
211
212int swsusp_arch_resume(void)
213{
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800214 int error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200215
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200216 /* We have got enough memory and from now on we cannot recover */
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800217 if ((error = set_up_temporary_mappings()))
218 return error;
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200219 restore_image();
220 return 0;
221}
222#endif /* CONFIG_SOFTWARE_SUSPEND */