Commit | Line | Data |
---|---|---|
5234f5eb | 1 | /* |
835c34a1 | 2 | * handle transition of Linux booting another kernel |
5234f5eb EB |
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 | */ | |
8 | ||
9 | #include <linux/mm.h> | |
10 | #include <linux/kexec.h> | |
5234f5eb EB |
11 | #include <linux/string.h> |
12 | #include <linux/reboot.h> | |
fd59d231 | 13 | #include <linux/numa.h> |
5234f5eb | 14 | #include <asm/pgtable.h> |
5234f5eb EB |
15 | #include <asm/tlbflush.h> |
16 | #include <asm/mmu_context.h> | |
17 | #include <asm/io.h> | |
8bf27556 | 18 | |
4bfaaef0 MD |
19 | #define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE))) |
20 | static u64 kexec_pgd[512] PAGE_ALIGNED; | |
21 | static u64 kexec_pud0[512] PAGE_ALIGNED; | |
22 | static u64 kexec_pmd0[512] PAGE_ALIGNED; | |
23 | static u64 kexec_pte0[512] PAGE_ALIGNED; | |
24 | static u64 kexec_pud1[512] PAGE_ALIGNED; | |
25 | static u64 kexec_pmd1[512] PAGE_ALIGNED; | |
26 | static u64 kexec_pte1[512] PAGE_ALIGNED; | |
27 | ||
8bf27556 | 28 | static void init_level2_page(pmd_t *level2p, unsigned long addr) |
5234f5eb EB |
29 | { |
30 | unsigned long end_addr; | |
72414d3f | 31 | |
5234f5eb | 32 | addr &= PAGE_MASK; |
8bf27556 | 33 | end_addr = addr + PUD_SIZE; |
72414d3f | 34 | while (addr < end_addr) { |
8bf27556 EB |
35 | set_pmd(level2p++, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC)); |
36 | addr += PMD_SIZE; | |
5234f5eb EB |
37 | } |
38 | } | |
39 | ||
8bf27556 | 40 | static int init_level3_page(struct kimage *image, pud_t *level3p, |
72414d3f | 41 | unsigned long addr, unsigned long last_addr) |
5234f5eb EB |
42 | { |
43 | unsigned long end_addr; | |
44 | int result; | |
72414d3f | 45 | |
5234f5eb EB |
46 | result = 0; |
47 | addr &= PAGE_MASK; | |
8bf27556 | 48 | end_addr = addr + PGDIR_SIZE; |
72414d3f | 49 | while ((addr < last_addr) && (addr < end_addr)) { |
5234f5eb | 50 | struct page *page; |
8bf27556 | 51 | pmd_t *level2p; |
72414d3f | 52 | |
5234f5eb EB |
53 | page = kimage_alloc_control_pages(image, 0); |
54 | if (!page) { | |
55 | result = -ENOMEM; | |
56 | goto out; | |
57 | } | |
8bf27556 | 58 | level2p = (pmd_t *)page_address(page); |
5234f5eb | 59 | init_level2_page(level2p, addr); |
8bf27556 EB |
60 | set_pud(level3p++, __pud(__pa(level2p) | _KERNPG_TABLE)); |
61 | addr += PUD_SIZE; | |
5234f5eb EB |
62 | } |
63 | /* clear the unused entries */ | |
72414d3f | 64 | while (addr < end_addr) { |
8bf27556 EB |
65 | pud_clear(level3p++); |
66 | addr += PUD_SIZE; | |
5234f5eb EB |
67 | } |
68 | out: | |
69 | return result; | |
70 | } | |
71 | ||
72 | ||
8bf27556 | 73 | static int init_level4_page(struct kimage *image, pgd_t *level4p, |
72414d3f | 74 | unsigned long addr, unsigned long last_addr) |
5234f5eb EB |
75 | { |
76 | unsigned long end_addr; | |
77 | int result; | |
72414d3f | 78 | |
5234f5eb EB |
79 | result = 0; |
80 | addr &= PAGE_MASK; | |
8bf27556 | 81 | end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE); |
72414d3f | 82 | while ((addr < last_addr) && (addr < end_addr)) { |
5234f5eb | 83 | struct page *page; |
8bf27556 | 84 | pud_t *level3p; |
72414d3f | 85 | |
5234f5eb EB |
86 | page = kimage_alloc_control_pages(image, 0); |
87 | if (!page) { | |
88 | result = -ENOMEM; | |
89 | goto out; | |
90 | } | |
8bf27556 | 91 | level3p = (pud_t *)page_address(page); |
5234f5eb EB |
92 | result = init_level3_page(image, level3p, addr, last_addr); |
93 | if (result) { | |
94 | goto out; | |
95 | } | |
8bf27556 EB |
96 | set_pgd(level4p++, __pgd(__pa(level3p) | _KERNPG_TABLE)); |
97 | addr += PGDIR_SIZE; | |
5234f5eb EB |
98 | } |
99 | /* clear the unused entries */ | |
72414d3f | 100 | while (addr < end_addr) { |
8bf27556 EB |
101 | pgd_clear(level4p++); |
102 | addr += PGDIR_SIZE; | |
5234f5eb | 103 | } |
72414d3f | 104 | out: |
5234f5eb EB |
105 | return result; |
106 | } | |
107 | ||
108 | ||
109 | static int init_pgtable(struct kimage *image, unsigned long start_pgtable) | |
110 | { | |
8bf27556 EB |
111 | pgd_t *level4p; |
112 | level4p = (pgd_t *)__va(start_pgtable); | |
72414d3f | 113 | return init_level4_page(image, level4p, 0, end_pfn << PAGE_SHIFT); |
5234f5eb EB |
114 | } |
115 | ||
116 | static void set_idt(void *newidt, u16 limit) | |
117 | { | |
36c4fd23 | 118 | struct desc_ptr curidt; |
5234f5eb EB |
119 | |
120 | /* x86-64 supports unaliged loads & stores */ | |
36c4fd23 EB |
121 | curidt.size = limit; |
122 | curidt.address = (unsigned long)newidt; | |
5234f5eb EB |
123 | |
124 | __asm__ __volatile__ ( | |
36c4fd23 EB |
125 | "lidtq %0\n" |
126 | : : "m" (curidt) | |
5234f5eb EB |
127 | ); |
128 | }; | |
129 | ||
130 | ||
131 | static void set_gdt(void *newgdt, u16 limit) | |
132 | { | |
36c4fd23 | 133 | struct desc_ptr curgdt; |
5234f5eb EB |
134 | |
135 | /* x86-64 supports unaligned loads & stores */ | |
36c4fd23 EB |
136 | curgdt.size = limit; |
137 | curgdt.address = (unsigned long)newgdt; | |
5234f5eb EB |
138 | |
139 | __asm__ __volatile__ ( | |
36c4fd23 EB |
140 | "lgdtq %0\n" |
141 | : : "m" (curgdt) | |
5234f5eb EB |
142 | ); |
143 | }; | |
144 | ||
145 | static void load_segments(void) | |
146 | { | |
147 | __asm__ __volatile__ ( | |
36c4fd23 EB |
148 | "\tmovl %0,%%ds\n" |
149 | "\tmovl %0,%%es\n" | |
150 | "\tmovl %0,%%ss\n" | |
151 | "\tmovl %0,%%fs\n" | |
152 | "\tmovl %0,%%gs\n" | |
2ec5e3a8 | 153 | : : "a" (__KERNEL_DS) : "memory" |
5234f5eb | 154 | ); |
5234f5eb EB |
155 | } |
156 | ||
5234f5eb EB |
157 | int machine_kexec_prepare(struct kimage *image) |
158 | { | |
4bfaaef0 | 159 | unsigned long start_pgtable; |
5234f5eb EB |
160 | int result; |
161 | ||
162 | /* Calculate the offsets */ | |
72414d3f | 163 | start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT; |
5234f5eb EB |
164 | |
165 | /* Setup the identity mapped 64bit page table */ | |
166 | result = init_pgtable(image, start_pgtable); | |
72414d3f | 167 | if (result) |
5234f5eb | 168 | return result; |
5234f5eb | 169 | |
5234f5eb EB |
170 | return 0; |
171 | } | |
172 | ||
173 | void machine_kexec_cleanup(struct kimage *image) | |
174 | { | |
175 | return; | |
176 | } | |
177 | ||
178 | /* | |
179 | * Do not allocate memory (or fail in any way) in machine_kexec(). | |
180 | * We are past the point of no return, committed to rebooting now. | |
181 | */ | |
182 | NORET_TYPE void machine_kexec(struct kimage *image) | |
183 | { | |
4bfaaef0 MD |
184 | unsigned long page_list[PAGES_NR]; |
185 | void *control_page; | |
5234f5eb EB |
186 | |
187 | /* Interrupts aren't acceptable while we reboot */ | |
188 | local_irq_disable(); | |
189 | ||
4bfaaef0 MD |
190 | control_page = page_address(image->control_code_page) + PAGE_SIZE; |
191 | memcpy(control_page, relocate_kernel, PAGE_SIZE); | |
192 | ||
e3ebadd9 | 193 | page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page); |
4bfaaef0 | 194 | page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel; |
e3ebadd9 | 195 | page_list[PA_PGD] = virt_to_phys(&kexec_pgd); |
4bfaaef0 | 196 | page_list[VA_PGD] = (unsigned long)kexec_pgd; |
e3ebadd9 | 197 | page_list[PA_PUD_0] = virt_to_phys(&kexec_pud0); |
4bfaaef0 | 198 | page_list[VA_PUD_0] = (unsigned long)kexec_pud0; |
e3ebadd9 | 199 | page_list[PA_PMD_0] = virt_to_phys(&kexec_pmd0); |
4bfaaef0 | 200 | page_list[VA_PMD_0] = (unsigned long)kexec_pmd0; |
e3ebadd9 | 201 | page_list[PA_PTE_0] = virt_to_phys(&kexec_pte0); |
4bfaaef0 | 202 | page_list[VA_PTE_0] = (unsigned long)kexec_pte0; |
e3ebadd9 | 203 | page_list[PA_PUD_1] = virt_to_phys(&kexec_pud1); |
4bfaaef0 | 204 | page_list[VA_PUD_1] = (unsigned long)kexec_pud1; |
e3ebadd9 | 205 | page_list[PA_PMD_1] = virt_to_phys(&kexec_pmd1); |
4bfaaef0 | 206 | page_list[VA_PMD_1] = (unsigned long)kexec_pmd1; |
e3ebadd9 | 207 | page_list[PA_PTE_1] = virt_to_phys(&kexec_pte1); |
4bfaaef0 MD |
208 | page_list[VA_PTE_1] = (unsigned long)kexec_pte1; |
209 | ||
210 | page_list[PA_TABLE_PAGE] = | |
211 | (unsigned long)__pa(page_address(image->control_code_page)); | |
5234f5eb | 212 | |
2a8a3d5b EB |
213 | /* The segment registers are funny things, they have both a |
214 | * visible and an invisible part. Whenever the visible part is | |
215 | * set to a specific selector, the invisible part is loaded | |
216 | * with from a table in memory. At no other time is the | |
217 | * descriptor table in memory accessed. | |
5234f5eb EB |
218 | * |
219 | * I take advantage of this here by force loading the | |
220 | * segments, before I zap the gdt with an invalid value. | |
221 | */ | |
222 | load_segments(); | |
223 | /* The gdt & idt are now invalid. | |
224 | * If you want to load them you must set up your own idt & gdt. | |
225 | */ | |
226 | set_gdt(phys_to_virt(0),0); | |
227 | set_idt(phys_to_virt(0),0); | |
4bfaaef0 | 228 | |
5234f5eb | 229 | /* now call it */ |
4bfaaef0 MD |
230 | relocate_kernel((unsigned long)image->head, (unsigned long)page_list, |
231 | image->start); | |
5234f5eb | 232 | } |
2c8c0e6b AK |
233 | |
234 | /* crashkernel=size@addr specifies the location to reserve for | |
235 | * a crash kernel. By reserving this memory we guarantee | |
236 | * that linux never set's it up as a DMA target. | |
237 | * Useful for holding code to do something appropriate | |
238 | * after a kernel panic. | |
239 | */ | |
240 | static int __init setup_crashkernel(char *arg) | |
241 | { | |
242 | unsigned long size, base; | |
243 | char *p; | |
244 | if (!arg) | |
245 | return -EINVAL; | |
246 | size = memparse(arg, &p); | |
247 | if (arg == p) | |
248 | return -EINVAL; | |
249 | if (*p == '@') { | |
250 | base = memparse(p+1, &p); | |
251 | /* FIXME: Do I want a sanity check to validate the | |
252 | * memory range? Yes you do, but it's too early for | |
253 | * e820 -AK */ | |
254 | crashk_res.start = base; | |
255 | crashk_res.end = base + size - 1; | |
256 | } | |
257 | return 0; | |
258 | } | |
259 | early_param("crashkernel", setup_crashkernel); | |
260 | ||
fd59d231 KO |
261 | void arch_crash_save_vmcoreinfo(void) |
262 | { | |
263 | #ifdef CONFIG_ARCH_DISCONTIGMEM_ENABLE | |
bcbba6c1 KO |
264 | VMCOREINFO_SYMBOL(node_data); |
265 | VMCOREINFO_LENGTH(node_data, MAX_NUMNODES); | |
fd59d231 KO |
266 | #endif |
267 | } | |
268 |