2 * handle transition of Linux booting another kernel
3 * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
10 #include <linux/kexec.h>
11 #include <linux/string.h>
12 #include <linux/reboot.h>
13 #include <linux/numa.h>
14 #include <linux/ftrace.h>
16 #include <linux/suspend.h>
18 #include <asm/pgtable.h>
19 #include <asm/tlbflush.h>
20 #include <asm/mmu_context.h>
22 static int init_one_level2_page(struct kimage
*image
, pgd_t
*pgd
,
31 pgd
+= pgd_index(addr
);
32 if (!pgd_present(*pgd
)) {
33 page
= kimage_alloc_control_pages(image
, 0);
36 pud
= (pud_t
*)page_address(page
);
37 memset(pud
, 0, PAGE_SIZE
);
38 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
));
40 pud
= pud_offset(pgd
, addr
);
41 if (!pud_present(*pud
)) {
42 page
= kimage_alloc_control_pages(image
, 0);
45 pmd
= (pmd_t
*)page_address(page
);
46 memset(pmd
, 0, PAGE_SIZE
);
47 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
));
49 pmd
= pmd_offset(pud
, addr
);
50 if (!pmd_present(*pmd
))
51 set_pmd(pmd
, __pmd(addr
| __PAGE_KERNEL_LARGE_EXEC
));
57 static void init_level2_page(pmd_t
*level2p
, unsigned long addr
)
59 unsigned long end_addr
;
62 end_addr
= addr
+ PUD_SIZE
;
63 while (addr
< end_addr
) {
64 set_pmd(level2p
++, __pmd(addr
| __PAGE_KERNEL_LARGE_EXEC
));
69 static int init_level3_page(struct kimage
*image
, pud_t
*level3p
,
70 unsigned long addr
, unsigned long last_addr
)
72 unsigned long end_addr
;
77 end_addr
= addr
+ PGDIR_SIZE
;
78 while ((addr
< last_addr
) && (addr
< end_addr
)) {
82 page
= kimage_alloc_control_pages(image
, 0);
87 level2p
= (pmd_t
*)page_address(page
);
88 init_level2_page(level2p
, addr
);
89 set_pud(level3p
++, __pud(__pa(level2p
) | _KERNPG_TABLE
));
92 /* clear the unused entries */
93 while (addr
< end_addr
) {
102 static int init_level4_page(struct kimage
*image
, pgd_t
*level4p
,
103 unsigned long addr
, unsigned long last_addr
)
105 unsigned long end_addr
;
110 end_addr
= addr
+ (PTRS_PER_PGD
* PGDIR_SIZE
);
111 while ((addr
< last_addr
) && (addr
< end_addr
)) {
115 page
= kimage_alloc_control_pages(image
, 0);
120 level3p
= (pud_t
*)page_address(page
);
121 result
= init_level3_page(image
, level3p
, addr
, last_addr
);
124 set_pgd(level4p
++, __pgd(__pa(level3p
) | _KERNPG_TABLE
));
127 /* clear the unused entries */
128 while (addr
< end_addr
) {
129 pgd_clear(level4p
++);
136 static void free_transition_pgtable(struct kimage
*image
)
138 free_page((unsigned long)image
->arch
.pud
);
139 free_page((unsigned long)image
->arch
.pmd
);
140 free_page((unsigned long)image
->arch
.pte
);
143 static int init_transition_pgtable(struct kimage
*image
, pgd_t
*pgd
)
148 unsigned long vaddr
, paddr
;
149 int result
= -ENOMEM
;
151 vaddr
= (unsigned long)relocate_kernel
;
152 paddr
= __pa(page_address(image
->control_code_page
)+PAGE_SIZE
);
153 pgd
+= pgd_index(vaddr
);
154 if (!pgd_present(*pgd
)) {
155 pud
= (pud_t
*)get_zeroed_page(GFP_KERNEL
);
158 image
->arch
.pud
= pud
;
159 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
));
161 pud
= pud_offset(pgd
, vaddr
);
162 if (!pud_present(*pud
)) {
163 pmd
= (pmd_t
*)get_zeroed_page(GFP_KERNEL
);
166 image
->arch
.pmd
= pmd
;
167 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
));
169 pmd
= pmd_offset(pud
, vaddr
);
170 if (!pmd_present(*pmd
)) {
171 pte
= (pte_t
*)get_zeroed_page(GFP_KERNEL
);
174 image
->arch
.pte
= pte
;
175 set_pmd(pmd
, __pmd(__pa(pte
) | _KERNPG_TABLE
));
177 pte
= pte_offset_kernel(pmd
, vaddr
);
178 set_pte(pte
, pfn_pte(paddr
>> PAGE_SHIFT
, PAGE_KERNEL_EXEC
));
181 free_transition_pgtable(image
);
186 static int init_pgtable(struct kimage
*image
, unsigned long start_pgtable
)
190 level4p
= (pgd_t
*)__va(start_pgtable
);
191 result
= init_level4_page(image
, level4p
, 0, max_pfn
<< PAGE_SHIFT
);
195 * image->start may be outside 0 ~ max_pfn, for example when
196 * jump back to original kernel from kexeced kernel
198 result
= init_one_level2_page(image
, level4p
, image
->start
);
201 return init_transition_pgtable(image
, level4p
);
204 static void set_idt(void *newidt
, u16 limit
)
206 struct desc_ptr curidt
;
208 /* x86-64 supports unaliged loads & stores */
210 curidt
.address
= (unsigned long)newidt
;
212 __asm__
__volatile__ (
219 static void set_gdt(void *newgdt
, u16 limit
)
221 struct desc_ptr curgdt
;
223 /* x86-64 supports unaligned loads & stores */
225 curgdt
.address
= (unsigned long)newgdt
;
227 __asm__
__volatile__ (
233 static void load_segments(void)
235 __asm__
__volatile__ (
241 : : "a" (__KERNEL_DS
) : "memory"
245 int machine_kexec_prepare(struct kimage
*image
)
247 unsigned long start_pgtable
;
250 /* Calculate the offsets */
251 start_pgtable
= page_to_pfn(image
->control_code_page
) << PAGE_SHIFT
;
253 /* Setup the identity mapped 64bit page table */
254 result
= init_pgtable(image
, start_pgtable
);
261 void machine_kexec_cleanup(struct kimage
*image
)
263 free_transition_pgtable(image
);
267 * Do not allocate memory (or fail in any way) in machine_kexec().
268 * We are past the point of no return, committed to rebooting now.
270 void machine_kexec(struct kimage
*image
)
272 unsigned long page_list
[PAGES_NR
];
274 int save_ftrace_enabled
;
276 #ifdef CONFIG_KEXEC_JUMP
277 if (image
->preserve_context
)
278 save_processor_state();
281 save_ftrace_enabled
= __ftrace_enabled_save();
283 /* Interrupts aren't acceptable while we reboot */
286 if (image
->preserve_context
) {
287 #ifdef CONFIG_X86_IO_APIC
289 * We need to put APICs in legacy mode so that we can
290 * get timer interrupts in second kernel. kexec/kdump
291 * paths already have calls to disable_IO_APIC() in
292 * one form or other. kexec jump path also need
299 control_page
= page_address(image
->control_code_page
) + PAGE_SIZE
;
300 memcpy(control_page
, relocate_kernel
, KEXEC_CONTROL_CODE_MAX_SIZE
);
302 page_list
[PA_CONTROL_PAGE
] = virt_to_phys(control_page
);
303 page_list
[VA_CONTROL_PAGE
] = (unsigned long)control_page
;
304 page_list
[PA_TABLE_PAGE
] =
305 (unsigned long)__pa(page_address(image
->control_code_page
));
307 if (image
->type
== KEXEC_TYPE_DEFAULT
)
308 page_list
[PA_SWAP_PAGE
] = (page_to_pfn(image
->swap_page
)
312 * The segment registers are funny things, they have both a
313 * visible and an invisible part. Whenever the visible part is
314 * set to a specific selector, the invisible part is loaded
315 * with from a table in memory. At no other time is the
316 * descriptor table in memory accessed.
318 * I take advantage of this here by force loading the
319 * segments, before I zap the gdt with an invalid value.
323 * The gdt & idt are now invalid.
324 * If you want to load them you must set up your own idt & gdt.
326 set_gdt(phys_to_virt(0), 0);
327 set_idt(phys_to_virt(0), 0);
330 image
->start
= relocate_kernel((unsigned long)image
->head
,
331 (unsigned long)page_list
,
333 image
->preserve_context
);
335 #ifdef CONFIG_KEXEC_JUMP
336 if (image
->preserve_context
)
337 restore_processor_state();
340 __ftrace_enabled_restore(save_ftrace_enabled
);
343 void arch_crash_save_vmcoreinfo(void)
345 VMCOREINFO_SYMBOL(phys_base
);
346 VMCOREINFO_SYMBOL(init_level4_pgt
);
349 VMCOREINFO_SYMBOL(node_data
);
350 VMCOREINFO_LENGTH(node_data
, MAX_NUMNODES
);