2 * machine_kexec.c - handle transition of Linux booting another kernel
3 * Copyright (C) 2002-2004 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/delay.h>
12 #include <linux/string.h>
13 #include <linux/reboot.h>
15 #include <asm/pgtable.h>
16 #include <asm/pgalloc.h>
17 #include <asm/tlbflush.h>
18 #include <asm/mmu_context.h>
21 #include <asm/cpufeature.h>
22 #include <asm/hw_irq.h>
24 #define LEVEL0_SIZE (1UL << 12UL)
25 #define LEVEL1_SIZE (1UL << 21UL)
26 #define LEVEL2_SIZE (1UL << 30UL)
27 #define LEVEL3_SIZE (1UL << 39UL)
28 #define LEVEL4_SIZE (1UL << 48UL)
30 #define L0_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
31 #define L1_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE)
32 #define L2_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
33 #define L3_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
35 static void init_level2_page(
36 uint64_t *level2p, unsigned long addr)
38 unsigned long end_addr;
40 end_addr = addr + LEVEL2_SIZE;
41 while(addr < end_addr) {
42 *(level2p++) = addr | L1_ATTR;
47 static int init_level3_page(struct kimage *image,
48 uint64_t *level3p, unsigned long addr, unsigned long last_addr)
50 unsigned long end_addr;
54 end_addr = addr + LEVEL3_SIZE;
55 while((addr < last_addr) && (addr < end_addr)) {
58 page = kimage_alloc_control_pages(image, 0);
63 level2p = (uint64_t *)page_address(page);
64 init_level2_page(level2p, addr);
65 *(level3p++) = __pa(level2p) | L2_ATTR;
68 /* clear the unused entries */
69 while(addr < end_addr) {
78 static int init_level4_page(struct kimage *image,
79 uint64_t *level4p, unsigned long addr, unsigned long last_addr)
81 unsigned long end_addr;
85 end_addr = addr + LEVEL4_SIZE;
86 while((addr < last_addr) && (addr < end_addr)) {
89 page = kimage_alloc_control_pages(image, 0);
94 level3p = (uint64_t *)page_address(page);
95 result = init_level3_page(image, level3p, addr, last_addr);
99 *(level4p++) = __pa(level3p) | L3_ATTR;
102 /* clear the unused entries */
103 while(addr < end_addr) {
112 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
115 level4p = (uint64_t *)__va(start_pgtable);
116 return init_level4_page(image, level4p, 0, end_pfn << PAGE_SHIFT);
119 static void set_idt(void *newidt, __u16 limit)
121 unsigned char curidt[10];
123 /* x86-64 supports unaliged loads & stores */
124 (*(__u16 *)(curidt)) = limit;
125 (*(__u64 *)(curidt +2)) = (unsigned long)(newidt);
127 __asm__ __volatile__ (
134 static void set_gdt(void *newgdt, __u16 limit)
136 unsigned char curgdt[10];
138 /* x86-64 supports unaligned loads & stores */
139 (*(__u16 *)(curgdt)) = limit;
140 (*(__u64 *)(curgdt +2)) = (unsigned long)(newgdt);
142 __asm__ __volatile__ (
148 static void load_segments(void)
150 __asm__ __volatile__ (
151 "\tmovl $"STR(__KERNEL_DS)",%eax\n"
162 typedef void (*relocate_new_kernel_t)(
163 unsigned long indirection_page, unsigned long control_code_buffer,
164 unsigned long start_address, unsigned long pgtable);
166 const extern unsigned char relocate_new_kernel[];
167 extern void relocate_new_kernel_end(void);
168 const extern unsigned long relocate_new_kernel_size;
170 int machine_kexec_prepare(struct kimage *image)
172 unsigned long start_pgtable, control_code_buffer;
175 /* Calculate the offsets */
176 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
177 control_code_buffer = start_pgtable + 4096UL;
179 /* Setup the identity mapped 64bit page table */
180 result = init_pgtable(image, start_pgtable);
185 /* Place the code in the reboot code buffer */
186 memcpy(__va(control_code_buffer), relocate_new_kernel, relocate_new_kernel_size);
191 void machine_kexec_cleanup(struct kimage *image)
197 * Do not allocate memory (or fail in any way) in machine_kexec().
198 * We are past the point of no return, committed to rebooting now.
200 void machine_kexec(struct kimage *image)
202 unsigned long indirection_page;
203 unsigned long control_code_buffer;
204 unsigned long start_pgtable;
205 relocate_new_kernel_t rnk;
207 /* Interrupts aren't acceptable while we reboot */
210 /* Calculate the offsets */
211 indirection_page = image->head & PAGE_MASK;
212 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
213 control_code_buffer = start_pgtable + 4096UL;
215 /* Set the low half of the page table to my identity mapped
216 * page table for kexec. Leave the high half pointing at the
217 * kernel pages. Don't bother to flush the global pages
218 * as that will happen when I fully switch to my identity mapped
221 // memcpy(current->active_mm->pml4, __va(start_pgtable), PAGE_SIZE/2);
225 /* The segment registers are funny things, they are
226 * automatically loaded from a table, in memory wherever you
227 * set them to a specific selector, but this table is never
228 * accessed again unless you set the segment to a different selector.
230 * The more common model are caches where the behide
231 * the scenes work is done, but is also dropped at arbitrary
234 * I take advantage of this here by force loading the
235 * segments, before I zap the gdt with an invalid value.
238 /* The gdt & idt are now invalid.
239 * If you want to load them you must set up your own idt & gdt.
241 set_gdt(phys_to_virt(0),0);
242 set_idt(phys_to_virt(0),0);
244 rnk = (relocate_new_kernel_t) control_code_buffer;
245 (*rnk)(indirection_page, control_code_buffer, image->start, start_pgtable);