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/gfp.h>
13 #include <linux/reboot.h>
14 #include <linux/numa.h>
15 #include <linux/ftrace.h>
17 #include <linux/suspend.h>
20 #include <asm/pgtable.h>
21 #include <asm/tlbflush.h>
22 #include <asm/mmu_context.h>
23 #include <asm/debugreg.h>
25 static void free_transition_pgtable(struct kimage
*image
)
27 free_page((unsigned long)image
->arch
.pud
);
28 free_page((unsigned long)image
->arch
.pmd
);
29 free_page((unsigned long)image
->arch
.pte
);
32 static int init_transition_pgtable(struct kimage
*image
, pgd_t
*pgd
)
37 unsigned long vaddr
, paddr
;
40 vaddr
= (unsigned long)relocate_kernel
;
41 paddr
= __pa(page_address(image
->control_code_page
)+PAGE_SIZE
);
42 pgd
+= pgd_index(vaddr
);
43 if (!pgd_present(*pgd
)) {
44 pud
= (pud_t
*)get_zeroed_page(GFP_KERNEL
);
47 image
->arch
.pud
= pud
;
48 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
));
50 pud
= pud_offset(pgd
, vaddr
);
51 if (!pud_present(*pud
)) {
52 pmd
= (pmd_t
*)get_zeroed_page(GFP_KERNEL
);
55 image
->arch
.pmd
= pmd
;
56 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
));
58 pmd
= pmd_offset(pud
, vaddr
);
59 if (!pmd_present(*pmd
)) {
60 pte
= (pte_t
*)get_zeroed_page(GFP_KERNEL
);
63 image
->arch
.pte
= pte
;
64 set_pmd(pmd
, __pmd(__pa(pte
) | _KERNPG_TABLE
));
66 pte
= pte_offset_kernel(pmd
, vaddr
);
67 set_pte(pte
, pfn_pte(paddr
>> PAGE_SHIFT
, PAGE_KERNEL_EXEC
));
70 free_transition_pgtable(image
);
74 static void *alloc_pgt_page(void *data
)
76 struct kimage
*image
= (struct kimage
*)data
;
80 page
= kimage_alloc_control_pages(image
, 0);
82 p
= page_address(page
);
89 static int init_pgtable(struct kimage
*image
, unsigned long start_pgtable
)
91 struct x86_mapping_info info
= {
92 .alloc_pgt_page
= alloc_pgt_page
,
94 .pmd_flag
= __PAGE_KERNEL_LARGE_EXEC
,
96 unsigned long mstart
, mend
;
101 level4p
= (pgd_t
*)__va(start_pgtable
);
103 result
= kernel_ident_mapping_init(&info
, level4p
,
104 0, max_pfn
<< PAGE_SHIFT
);
109 * segments's mem ranges could be outside 0 ~ max_pfn,
110 * for example when jump back to original kernel from kexeced kernel.
111 * or first kernel is booted with user mem map, and second kernel
112 * could be loaded out of that range.
114 for (i
= 0; i
< image
->nr_segments
; i
++) {
115 mstart
= image
->segment
[i
].mem
;
116 mend
= mstart
+ image
->segment
[i
].memsz
;
118 result
= kernel_ident_mapping_init(&info
,
119 level4p
, mstart
, mend
);
125 return init_transition_pgtable(image
, level4p
);
128 static void set_idt(void *newidt
, u16 limit
)
130 struct desc_ptr curidt
;
132 /* x86-64 supports unaliged loads & stores */
134 curidt
.address
= (unsigned long)newidt
;
136 __asm__
__volatile__ (
143 static void set_gdt(void *newgdt
, u16 limit
)
145 struct desc_ptr curgdt
;
147 /* x86-64 supports unaligned loads & stores */
149 curgdt
.address
= (unsigned long)newgdt
;
151 __asm__
__volatile__ (
157 static void load_segments(void)
159 __asm__
__volatile__ (
165 : : "a" (__KERNEL_DS
) : "memory"
169 int machine_kexec_prepare(struct kimage
*image
)
171 unsigned long start_pgtable
;
174 /* Calculate the offsets */
175 start_pgtable
= page_to_pfn(image
->control_code_page
) << PAGE_SHIFT
;
177 /* Setup the identity mapped 64bit page table */
178 result
= init_pgtable(image
, start_pgtable
);
185 void machine_kexec_cleanup(struct kimage
*image
)
187 free_transition_pgtable(image
);
191 * Do not allocate memory (or fail in any way) in machine_kexec().
192 * We are past the point of no return, committed to rebooting now.
194 void machine_kexec(struct kimage
*image
)
196 unsigned long page_list
[PAGES_NR
];
198 int save_ftrace_enabled
;
200 #ifdef CONFIG_KEXEC_JUMP
201 if (image
->preserve_context
)
202 save_processor_state();
205 save_ftrace_enabled
= __ftrace_enabled_save();
207 /* Interrupts aren't acceptable while we reboot */
209 hw_breakpoint_disable();
211 if (image
->preserve_context
) {
212 #ifdef CONFIG_X86_IO_APIC
214 * We need to put APICs in legacy mode so that we can
215 * get timer interrupts in second kernel. kexec/kdump
216 * paths already have calls to disable_IO_APIC() in
217 * one form or other. kexec jump path also need
224 control_page
= page_address(image
->control_code_page
) + PAGE_SIZE
;
225 memcpy(control_page
, relocate_kernel
, KEXEC_CONTROL_CODE_MAX_SIZE
);
227 page_list
[PA_CONTROL_PAGE
] = virt_to_phys(control_page
);
228 page_list
[VA_CONTROL_PAGE
] = (unsigned long)control_page
;
229 page_list
[PA_TABLE_PAGE
] =
230 (unsigned long)__pa(page_address(image
->control_code_page
));
232 if (image
->type
== KEXEC_TYPE_DEFAULT
)
233 page_list
[PA_SWAP_PAGE
] = (page_to_pfn(image
->swap_page
)
237 * The segment registers are funny things, they have both a
238 * visible and an invisible part. Whenever the visible part is
239 * set to a specific selector, the invisible part is loaded
240 * with from a table in memory. At no other time is the
241 * descriptor table in memory accessed.
243 * I take advantage of this here by force loading the
244 * segments, before I zap the gdt with an invalid value.
248 * The gdt & idt are now invalid.
249 * If you want to load them you must set up your own idt & gdt.
251 set_gdt(phys_to_virt(0), 0);
252 set_idt(phys_to_virt(0), 0);
255 image
->start
= relocate_kernel((unsigned long)image
->head
,
256 (unsigned long)page_list
,
258 image
->preserve_context
);
260 #ifdef CONFIG_KEXEC_JUMP
261 if (image
->preserve_context
)
262 restore_processor_state();
265 __ftrace_enabled_restore(save_ftrace_enabled
);
268 void arch_crash_save_vmcoreinfo(void)
270 VMCOREINFO_SYMBOL(phys_base
);
271 VMCOREINFO_SYMBOL(init_level4_pgt
);
274 VMCOREINFO_SYMBOL(node_data
);
275 VMCOREINFO_LENGTH(node_data
, MAX_NUMNODES
);