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 <asm/pgtable.h>
17 #include <asm/tlbflush.h>
18 #include <asm/mmu_context.h>
21 static void init_level2_page(pmd_t *level2p, unsigned long addr)
23 unsigned long end_addr;
26 end_addr = addr + PUD_SIZE;
27 while (addr < end_addr) {
28 set_pmd(level2p++, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
33 static int init_level3_page(struct kimage *image, pud_t *level3p,
34 unsigned long addr, unsigned long last_addr)
36 unsigned long end_addr;
41 end_addr = addr + PGDIR_SIZE;
42 while ((addr < last_addr) && (addr < end_addr)) {
46 page = kimage_alloc_control_pages(image, 0);
51 level2p = (pmd_t *)page_address(page);
52 init_level2_page(level2p, addr);
53 set_pud(level3p++, __pud(__pa(level2p) | _KERNPG_TABLE));
56 /* clear the unused entries */
57 while (addr < end_addr) {
66 static int init_level4_page(struct kimage *image, pgd_t *level4p,
67 unsigned long addr, unsigned long last_addr)
69 unsigned long end_addr;
74 end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE);
75 while ((addr < last_addr) && (addr < end_addr)) {
79 page = kimage_alloc_control_pages(image, 0);
84 level3p = (pud_t *)page_address(page);
85 result = init_level3_page(image, level3p, addr, last_addr);
89 set_pgd(level4p++, __pgd(__pa(level3p) | _KERNPG_TABLE));
92 /* clear the unused entries */
93 while (addr < end_addr) {
101 static void free_transition_pgtable(struct kimage *image)
103 free_page((unsigned long)image->arch.pud);
104 free_page((unsigned long)image->arch.pmd);
105 free_page((unsigned long)image->arch.pte);
108 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
113 unsigned long vaddr, paddr;
114 int result = -ENOMEM;
116 vaddr = (unsigned long)relocate_kernel;
117 paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
118 pgd += pgd_index(vaddr);
119 if (!pgd_present(*pgd)) {
120 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
123 image->arch.pud = pud;
124 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
126 pud = pud_offset(pgd, vaddr);
127 if (!pud_present(*pud)) {
128 pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
131 image->arch.pmd = pmd;
132 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
134 pmd = pmd_offset(pud, vaddr);
135 if (!pmd_present(*pmd)) {
136 pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
139 image->arch.pte = pte;
140 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
142 pte = pte_offset_kernel(pmd, vaddr);
143 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
146 free_transition_pgtable(image);
151 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
155 level4p = (pgd_t *)__va(start_pgtable);
156 result = init_level4_page(image, level4p, 0, max_pfn << PAGE_SHIFT);
159 return init_transition_pgtable(image, level4p);
162 static void set_idt(void *newidt, u16 limit)
164 struct desc_ptr curidt;
166 /* x86-64 supports unaliged loads & stores */
168 curidt.address = (unsigned long)newidt;
170 __asm__ __volatile__ (
177 static void set_gdt(void *newgdt, u16 limit)
179 struct desc_ptr curgdt;
181 /* x86-64 supports unaligned loads & stores */
183 curgdt.address = (unsigned long)newgdt;
185 __asm__ __volatile__ (
191 static void load_segments(void)
193 __asm__ __volatile__ (
199 : : "a" (__KERNEL_DS) : "memory"
203 int machine_kexec_prepare(struct kimage *image)
205 unsigned long start_pgtable;
208 /* Calculate the offsets */
209 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
211 /* Setup the identity mapped 64bit page table */
212 result = init_pgtable(image, start_pgtable);
219 void machine_kexec_cleanup(struct kimage *image)
221 free_transition_pgtable(image);
225 * Do not allocate memory (or fail in any way) in machine_kexec().
226 * We are past the point of no return, committed to rebooting now.
228 void machine_kexec(struct kimage *image)
230 unsigned long page_list[PAGES_NR];
235 /* Interrupts aren't acceptable while we reboot */
238 control_page = page_address(image->control_code_page) + PAGE_SIZE;
239 memcpy(control_page, relocate_kernel, PAGE_SIZE);
241 page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
242 page_list[PA_TABLE_PAGE] =
243 (unsigned long)__pa(page_address(image->control_code_page));
245 /* The segment registers are funny things, they have both a
246 * visible and an invisible part. Whenever the visible part is
247 * set to a specific selector, the invisible part is loaded
248 * with from a table in memory. At no other time is the
249 * descriptor table in memory accessed.
251 * I take advantage of this here by force loading the
252 * segments, before I zap the gdt with an invalid value.
255 /* The gdt & idt are now invalid.
256 * If you want to load them you must set up your own idt & gdt.
258 set_gdt(phys_to_virt(0),0);
259 set_idt(phys_to_virt(0),0);
262 relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
266 void arch_crash_save_vmcoreinfo(void)
268 VMCOREINFO_SYMBOL(phys_base);
269 VMCOREINFO_SYMBOL(init_level4_pgt);
272 VMCOREINFO_SYMBOL(node_data);
273 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);