ia64/xen-unstable

view linux-2.6-xen-sparse/arch/x86_64/kernel/machine_kexec.c @ 13647:057c4c2991fa

32-on-64: Fix an one-off error in compat_addr_ok().
Signed-off-by: Jan Beulich <jbeulich@novell.com>
author kaf24@localhost.localdomain
date Fri Jan 26 13:36:52 2007 +0000 (2007-01-26)
parents 6f0f80aa817d
children
line source
1 /*
2 * machine_kexec.c - handle transition of Linux booting another kernel
3 * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
4 *
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
7 */
9 #include <linux/mm.h>
10 #include <linux/kexec.h>
11 #include <linux/string.h>
12 #include <linux/reboot.h>
13 #include <asm/pgtable.h>
14 #include <asm/tlbflush.h>
15 #include <asm/mmu_context.h>
16 #include <asm/io.h>
18 #define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
19 static u64 kexec_pgd[512] PAGE_ALIGNED;
20 static u64 kexec_pud0[512] PAGE_ALIGNED;
21 static u64 kexec_pmd0[512] PAGE_ALIGNED;
22 static u64 kexec_pte0[512] PAGE_ALIGNED;
23 static u64 kexec_pud1[512] PAGE_ALIGNED;
24 static u64 kexec_pmd1[512] PAGE_ALIGNED;
25 static u64 kexec_pte1[512] PAGE_ALIGNED;
27 #ifdef CONFIG_XEN
29 /* In the case of Xen, override hypervisor functions to be able to create
30 * a regular identity mapping page table...
31 */
33 #include <xen/interface/kexec.h>
34 #include <xen/interface/memory.h>
36 #define x__pmd(x) ((pmd_t) { (x) } )
37 #define x__pud(x) ((pud_t) { (x) } )
38 #define x__pgd(x) ((pgd_t) { (x) } )
40 #define x_pmd_val(x) ((x).pmd)
41 #define x_pud_val(x) ((x).pud)
42 #define x_pgd_val(x) ((x).pgd)
44 static inline void x_set_pmd(pmd_t *dst, pmd_t val)
45 {
46 x_pmd_val(*dst) = x_pmd_val(val);
47 }
49 static inline void x_set_pud(pud_t *dst, pud_t val)
50 {
51 x_pud_val(*dst) = phys_to_machine(x_pud_val(val));
52 }
54 static inline void x_pud_clear (pud_t *pud)
55 {
56 x_pud_val(*pud) = 0;
57 }
59 static inline void x_set_pgd(pgd_t *dst, pgd_t val)
60 {
61 x_pgd_val(*dst) = phys_to_machine(x_pgd_val(val));
62 }
64 static inline void x_pgd_clear (pgd_t * pgd)
65 {
66 x_pgd_val(*pgd) = 0;
67 }
69 #define X__PAGE_KERNEL_LARGE_EXEC \
70 _PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_PSE
71 #define X_KERNPG_TABLE _PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY
73 #define __ma(x) (pfn_to_mfn(__pa((x)) >> PAGE_SHIFT) << PAGE_SHIFT)
75 #if PAGES_NR > KEXEC_XEN_NO_PAGES
76 #error PAGES_NR is greater than KEXEC_XEN_NO_PAGES - Xen support will break
77 #endif
79 #if PA_CONTROL_PAGE != 0
80 #error PA_CONTROL_PAGE is non zero - Xen support will break
81 #endif
83 void machine_kexec_setup_load_arg(xen_kexec_image_t *xki, struct kimage *image)
84 {
85 void *control_page;
86 void *table_page;
88 memset(xki->page_list, 0, sizeof(xki->page_list));
90 control_page = page_address(image->control_code_page) + PAGE_SIZE;
91 memcpy(control_page, relocate_kernel, PAGE_SIZE);
93 table_page = page_address(image->control_code_page);
95 xki->page_list[PA_CONTROL_PAGE] = __ma(control_page);
96 xki->page_list[PA_TABLE_PAGE] = __ma(table_page);
98 xki->page_list[PA_PGD] = __ma(kexec_pgd);
99 xki->page_list[PA_PUD_0] = __ma(kexec_pud0);
100 xki->page_list[PA_PUD_1] = __ma(kexec_pud1);
101 xki->page_list[PA_PMD_0] = __ma(kexec_pmd0);
102 xki->page_list[PA_PMD_1] = __ma(kexec_pmd1);
103 xki->page_list[PA_PTE_0] = __ma(kexec_pte0);
104 xki->page_list[PA_PTE_1] = __ma(kexec_pte1);
105 }
107 #else /* CONFIG_XEN */
109 #define x__pmd(x) __pmd(x)
110 #define x__pud(x) __pud(x)
111 #define x__pgd(x) __pgd(x)
113 #define x_set_pmd(x, y) set_pmd(x, y)
114 #define x_set_pud(x, y) set_pud(x, y)
115 #define x_set_pgd(x, y) set_pgd(x, y)
117 #define x_pud_clear(x) pud_clear(x)
118 #define x_pgd_clear(x) pgd_clear(x)
120 #define X__PAGE_KERNEL_LARGE_EXEC __PAGE_KERNEL_LARGE_EXEC
121 #define X_KERNPG_TABLE _KERNPG_TABLE
123 #endif /* CONFIG_XEN */
125 static void init_level2_page(pmd_t *level2p, unsigned long addr)
126 {
127 unsigned long end_addr;
129 addr &= PAGE_MASK;
130 end_addr = addr + PUD_SIZE;
131 while (addr < end_addr) {
132 x_set_pmd(level2p++, x__pmd(addr | X__PAGE_KERNEL_LARGE_EXEC));
133 addr += PMD_SIZE;
134 }
135 }
137 static int init_level3_page(struct kimage *image, pud_t *level3p,
138 unsigned long addr, unsigned long last_addr)
139 {
140 unsigned long end_addr;
141 int result;
143 result = 0;
144 addr &= PAGE_MASK;
145 end_addr = addr + PGDIR_SIZE;
146 while ((addr < last_addr) && (addr < end_addr)) {
147 struct page *page;
148 pmd_t *level2p;
150 page = kimage_alloc_control_pages(image, 0);
151 if (!page) {
152 result = -ENOMEM;
153 goto out;
154 }
155 level2p = (pmd_t *)page_address(page);
156 init_level2_page(level2p, addr);
157 x_set_pud(level3p++, x__pud(__pa(level2p) | X_KERNPG_TABLE));
158 addr += PUD_SIZE;
159 }
160 /* clear the unused entries */
161 while (addr < end_addr) {
162 x_pud_clear(level3p++);
163 addr += PUD_SIZE;
164 }
165 out:
166 return result;
167 }
170 static int init_level4_page(struct kimage *image, pgd_t *level4p,
171 unsigned long addr, unsigned long last_addr)
172 {
173 unsigned long end_addr;
174 int result;
176 result = 0;
177 addr &= PAGE_MASK;
178 end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE);
179 while ((addr < last_addr) && (addr < end_addr)) {
180 struct page *page;
181 pud_t *level3p;
183 page = kimage_alloc_control_pages(image, 0);
184 if (!page) {
185 result = -ENOMEM;
186 goto out;
187 }
188 level3p = (pud_t *)page_address(page);
189 result = init_level3_page(image, level3p, addr, last_addr);
190 if (result) {
191 goto out;
192 }
193 x_set_pgd(level4p++, x__pgd(__pa(level3p) | X_KERNPG_TABLE));
194 addr += PGDIR_SIZE;
195 }
196 /* clear the unused entries */
197 while (addr < end_addr) {
198 x_pgd_clear(level4p++);
199 addr += PGDIR_SIZE;
200 }
201 out:
202 return result;
203 }
206 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
207 {
208 pgd_t *level4p;
209 unsigned long x_end_pfn = end_pfn;
211 #ifdef CONFIG_XEN
212 x_end_pfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
213 #endif
215 level4p = (pgd_t *)__va(start_pgtable);
216 return init_level4_page(image, level4p, 0, x_end_pfn << PAGE_SHIFT);
217 }
219 int machine_kexec_prepare(struct kimage *image)
220 {
221 unsigned long start_pgtable;
222 int result;
224 /* Calculate the offsets */
225 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
227 /* Setup the identity mapped 64bit page table */
228 result = init_pgtable(image, start_pgtable);
229 if (result)
230 return result;
232 return 0;
233 }
235 void machine_kexec_cleanup(struct kimage *image)
236 {
237 return;
238 }
240 #ifndef CONFIG_XEN
241 /*
242 * Do not allocate memory (or fail in any way) in machine_kexec().
243 * We are past the point of no return, committed to rebooting now.
244 */
245 NORET_TYPE void machine_kexec(struct kimage *image)
246 {
247 unsigned long page_list[PAGES_NR];
248 void *control_page;
250 /* Interrupts aren't acceptable while we reboot */
251 local_irq_disable();
253 control_page = page_address(image->control_code_page) + PAGE_SIZE;
254 memcpy(control_page, relocate_kernel, PAGE_SIZE);
256 page_list[PA_CONTROL_PAGE] = __pa(control_page);
257 page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel;
258 page_list[PA_PGD] = __pa(kexec_pgd);
259 page_list[VA_PGD] = (unsigned long)kexec_pgd;
260 page_list[PA_PUD_0] = __pa(kexec_pud0);
261 page_list[VA_PUD_0] = (unsigned long)kexec_pud0;
262 page_list[PA_PMD_0] = __pa(kexec_pmd0);
263 page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
264 page_list[PA_PTE_0] = __pa(kexec_pte0);
265 page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
266 page_list[PA_PUD_1] = __pa(kexec_pud1);
267 page_list[VA_PUD_1] = (unsigned long)kexec_pud1;
268 page_list[PA_PMD_1] = __pa(kexec_pmd1);
269 page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
270 page_list[PA_PTE_1] = __pa(kexec_pte1);
271 page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
273 page_list[PA_TABLE_PAGE] =
274 (unsigned long)__pa(page_address(image->control_code_page));
276 relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
277 image->start);
278 }
279 #endif