blob: b3ea9db39db6f7ee9f9dab00632f754d8a75827d [file] [log] [blame]
Eric W. Biederman5234f5e2005-06-25 14:58:02 -07001/*
Dave Jones835c34a2007-10-12 21:10:53 -04002 * handle transition of Linux booting another kernel
Eric W. Biederman5234f5e2005-06-25 14:58:02 -07003 * 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>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070011#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070013#include <linux/reboot.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070014#include <linux/numa.h>
Ingo Molnarf43fdad2008-05-12 21:20:43 +020015#include <linux/ftrace.h>
Huang Yingfef3a7a2009-03-10 10:56:57 +080016#include <linux/io.h>
Huang Yingfee7b0d2009-03-10 10:57:16 +080017#include <linux/suspend.h>
Ingo Molnarf43fdad2008-05-12 21:20:43 +020018
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070019#include <asm/pgtable.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070020#include <asm/tlbflush.h>
21#include <asm/mmu_context.h>
K.Prasad17f557e2009-06-01 23:46:03 +053022#include <asm/debugreg.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070023
Huang Ying53594542009-03-10 10:57:04 +080024static int init_one_level2_page(struct kimage *image, pgd_t *pgd,
25 unsigned long addr)
26{
27 pud_t *pud;
28 pmd_t *pmd;
29 struct page *page;
30 int result = -ENOMEM;
31
32 addr &= PMD_MASK;
33 pgd += pgd_index(addr);
34 if (!pgd_present(*pgd)) {
35 page = kimage_alloc_control_pages(image, 0);
36 if (!page)
37 goto out;
38 pud = (pud_t *)page_address(page);
Jan Beulich234bb542010-09-02 13:46:34 +010039 clear_page(pud);
Huang Ying53594542009-03-10 10:57:04 +080040 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
41 }
42 pud = pud_offset(pgd, addr);
43 if (!pud_present(*pud)) {
44 page = kimage_alloc_control_pages(image, 0);
45 if (!page)
46 goto out;
47 pmd = (pmd_t *)page_address(page);
Jan Beulich234bb542010-09-02 13:46:34 +010048 clear_page(pmd);
Huang Ying53594542009-03-10 10:57:04 +080049 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
50 }
51 pmd = pmd_offset(pud, addr);
52 if (!pmd_present(*pmd))
53 set_pmd(pmd, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
54 result = 0;
55out:
56 return result;
57}
58
Eric W. Biederman8bf27552005-07-29 13:25:28 -060059static void init_level2_page(pmd_t *level2p, unsigned long addr)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070060{
61 unsigned long end_addr;
Maneesh Soni72414d32005-06-25 14:58:28 -070062
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070063 addr &= PAGE_MASK;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060064 end_addr = addr + PUD_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -070065 while (addr < end_addr) {
Eric W. Biederman8bf27552005-07-29 13:25:28 -060066 set_pmd(level2p++, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
67 addr += PMD_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070068 }
69}
70
Eric W. Biederman8bf27552005-07-29 13:25:28 -060071static int init_level3_page(struct kimage *image, pud_t *level3p,
Maneesh Soni72414d32005-06-25 14:58:28 -070072 unsigned long addr, unsigned long last_addr)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070073{
74 unsigned long end_addr;
75 int result;
Maneesh Soni72414d32005-06-25 14:58:28 -070076
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070077 result = 0;
78 addr &= PAGE_MASK;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060079 end_addr = addr + PGDIR_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -070080 while ((addr < last_addr) && (addr < end_addr)) {
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070081 struct page *page;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060082 pmd_t *level2p;
Maneesh Soni72414d32005-06-25 14:58:28 -070083
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070084 page = kimage_alloc_control_pages(image, 0);
85 if (!page) {
86 result = -ENOMEM;
87 goto out;
88 }
Eric W. Biederman8bf27552005-07-29 13:25:28 -060089 level2p = (pmd_t *)page_address(page);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070090 init_level2_page(level2p, addr);
Eric W. Biederman8bf27552005-07-29 13:25:28 -060091 set_pud(level3p++, __pud(__pa(level2p) | _KERNPG_TABLE));
92 addr += PUD_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070093 }
94 /* clear the unused entries */
Maneesh Soni72414d32005-06-25 14:58:28 -070095 while (addr < end_addr) {
Eric W. Biederman8bf27552005-07-29 13:25:28 -060096 pud_clear(level3p++);
97 addr += PUD_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070098 }
99out:
100 return result;
101}
102
103
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600104static int init_level4_page(struct kimage *image, pgd_t *level4p,
Maneesh Soni72414d32005-06-25 14:58:28 -0700105 unsigned long addr, unsigned long last_addr)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700106{
107 unsigned long end_addr;
108 int result;
Maneesh Soni72414d32005-06-25 14:58:28 -0700109
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700110 result = 0;
111 addr &= PAGE_MASK;
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600112 end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700113 while ((addr < last_addr) && (addr < end_addr)) {
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700114 struct page *page;
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600115 pud_t *level3p;
Maneesh Soni72414d32005-06-25 14:58:28 -0700116
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700117 page = kimage_alloc_control_pages(image, 0);
118 if (!page) {
119 result = -ENOMEM;
120 goto out;
121 }
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600122 level3p = (pud_t *)page_address(page);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700123 result = init_level3_page(image, level3p, addr, last_addr);
Huang Yingfef3a7a2009-03-10 10:56:57 +0800124 if (result)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700125 goto out;
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600126 set_pgd(level4p++, __pgd(__pa(level3p) | _KERNPG_TABLE));
127 addr += PGDIR_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700128 }
129 /* clear the unused entries */
Maneesh Soni72414d32005-06-25 14:58:28 -0700130 while (addr < end_addr) {
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600131 pgd_clear(level4p++);
132 addr += PGDIR_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700133 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700134out:
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700135 return result;
136}
137
Huang Yingf5deb792009-02-03 14:22:48 +0800138static void free_transition_pgtable(struct kimage *image)
139{
140 free_page((unsigned long)image->arch.pud);
141 free_page((unsigned long)image->arch.pmd);
142 free_page((unsigned long)image->arch.pte);
143}
144
145static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
146{
147 pud_t *pud;
148 pmd_t *pmd;
149 pte_t *pte;
150 unsigned long vaddr, paddr;
151 int result = -ENOMEM;
152
153 vaddr = (unsigned long)relocate_kernel;
154 paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
155 pgd += pgd_index(vaddr);
156 if (!pgd_present(*pgd)) {
157 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
158 if (!pud)
159 goto err;
160 image->arch.pud = pud;
161 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
162 }
163 pud = pud_offset(pgd, vaddr);
164 if (!pud_present(*pud)) {
165 pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
166 if (!pmd)
167 goto err;
168 image->arch.pmd = pmd;
169 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
170 }
171 pmd = pmd_offset(pud, vaddr);
172 if (!pmd_present(*pmd)) {
173 pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
174 if (!pte)
175 goto err;
176 image->arch.pte = pte;
177 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
178 }
179 pte = pte_offset_kernel(pmd, vaddr);
180 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
181 return 0;
182err:
183 free_transition_pgtable(image);
184 return result;
185}
186
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700187
188static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
189{
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600190 pgd_t *level4p;
Huang Yingf5deb792009-02-03 14:22:48 +0800191 int result;
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600192 level4p = (pgd_t *)__va(start_pgtable);
Huang Yingf5deb792009-02-03 14:22:48 +0800193 result = init_level4_page(image, level4p, 0, max_pfn << PAGE_SHIFT);
194 if (result)
195 return result;
Huang Ying53594542009-03-10 10:57:04 +0800196 /*
197 * image->start may be outside 0 ~ max_pfn, for example when
198 * jump back to original kernel from kexeced kernel
199 */
200 result = init_one_level2_page(image, level4p, image->start);
201 if (result)
202 return result;
Huang Yingf5deb792009-02-03 14:22:48 +0800203 return init_transition_pgtable(image, level4p);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700204}
205
206static void set_idt(void *newidt, u16 limit)
207{
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600208 struct desc_ptr curidt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700209
210 /* x86-64 supports unaliged loads & stores */
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600211 curidt.size = limit;
212 curidt.address = (unsigned long)newidt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700213
214 __asm__ __volatile__ (
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600215 "lidtq %0\n"
216 : : "m" (curidt)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700217 );
218};
219
220
221static void set_gdt(void *newgdt, u16 limit)
222{
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600223 struct desc_ptr curgdt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700224
225 /* x86-64 supports unaligned loads & stores */
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600226 curgdt.size = limit;
227 curgdt.address = (unsigned long)newgdt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700228
229 __asm__ __volatile__ (
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600230 "lgdtq %0\n"
231 : : "m" (curgdt)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700232 );
233};
234
235static void load_segments(void)
236{
237 __asm__ __volatile__ (
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600238 "\tmovl %0,%%ds\n"
239 "\tmovl %0,%%es\n"
240 "\tmovl %0,%%ss\n"
241 "\tmovl %0,%%fs\n"
242 "\tmovl %0,%%gs\n"
Michael Matz2ec5e3a2006-03-07 21:55:48 -0800243 : : "a" (__KERNEL_DS) : "memory"
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700244 );
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700245}
246
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700247int machine_kexec_prepare(struct kimage *image)
248{
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200249 unsigned long start_pgtable;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700250 int result;
251
252 /* Calculate the offsets */
Maneesh Soni72414d32005-06-25 14:58:28 -0700253 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700254
255 /* Setup the identity mapped 64bit page table */
256 result = init_pgtable(image, start_pgtable);
Maneesh Soni72414d32005-06-25 14:58:28 -0700257 if (result)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700258 return result;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700259
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700260 return 0;
261}
262
263void machine_kexec_cleanup(struct kimage *image)
264{
Huang Yingf5deb792009-02-03 14:22:48 +0800265 free_transition_pgtable(image);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700266}
267
268/*
269 * Do not allocate memory (or fail in any way) in machine_kexec().
270 * We are past the point of no return, committed to rebooting now.
271 */
Huang Ying3ab83522008-07-25 19:45:07 -0700272void machine_kexec(struct kimage *image)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700273{
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200274 unsigned long page_list[PAGES_NR];
275 void *control_page;
Huang Yingfee7b0d2009-03-10 10:57:16 +0800276 int save_ftrace_enabled;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700277
Huang Yingfee7b0d2009-03-10 10:57:16 +0800278#ifdef CONFIG_KEXEC_JUMP
Huang Ying6407df52009-05-08 10:51:41 +0800279 if (image->preserve_context)
Huang Yingfee7b0d2009-03-10 10:57:16 +0800280 save_processor_state();
281#endif
282
283 save_ftrace_enabled = __ftrace_enabled_save();
Ingo Molnarf43fdad2008-05-12 21:20:43 +0200284
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700285 /* Interrupts aren't acceptable while we reboot */
286 local_irq_disable();
K.Prasad17f557e2009-06-01 23:46:03 +0530287 hw_breakpoint_disable();
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700288
Huang Yingfee7b0d2009-03-10 10:57:16 +0800289 if (image->preserve_context) {
290#ifdef CONFIG_X86_IO_APIC
291 /*
292 * We need to put APICs in legacy mode so that we can
293 * get timer interrupts in second kernel. kexec/kdump
294 * paths already have calls to disable_IO_APIC() in
295 * one form or other. kexec jump path also need
296 * one.
297 */
298 disable_IO_APIC();
299#endif
300 }
301
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200302 control_page = page_address(image->control_code_page) + PAGE_SIZE;
Huang Yingfee7b0d2009-03-10 10:57:16 +0800303 memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700304
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700305 page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
Huang Yingfee7b0d2009-03-10 10:57:16 +0800306 page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200307 page_list[PA_TABLE_PAGE] =
308 (unsigned long)__pa(page_address(image->control_code_page));
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700309
Huang Yingfee7b0d2009-03-10 10:57:16 +0800310 if (image->type == KEXEC_TYPE_DEFAULT)
311 page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
312 << PAGE_SHIFT);
313
Huang Yingfef3a7a2009-03-10 10:56:57 +0800314 /*
315 * The segment registers are funny things, they have both a
Eric W. Biederman2a8a3d52006-07-30 03:03:20 -0700316 * visible and an invisible part. Whenever the visible part is
317 * set to a specific selector, the invisible part is loaded
318 * with from a table in memory. At no other time is the
319 * descriptor table in memory accessed.
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700320 *
321 * I take advantage of this here by force loading the
322 * segments, before I zap the gdt with an invalid value.
323 */
324 load_segments();
Huang Yingfef3a7a2009-03-10 10:56:57 +0800325 /*
326 * The gdt & idt are now invalid.
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700327 * If you want to load them you must set up your own idt & gdt.
328 */
Huang Yingfef3a7a2009-03-10 10:56:57 +0800329 set_gdt(phys_to_virt(0), 0);
330 set_idt(phys_to_virt(0), 0);
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200331
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700332 /* now call it */
Huang Yingfee7b0d2009-03-10 10:57:16 +0800333 image->start = relocate_kernel((unsigned long)image->head,
334 (unsigned long)page_list,
335 image->start,
336 image->preserve_context);
337
338#ifdef CONFIG_KEXEC_JUMP
Huang Ying6407df52009-05-08 10:51:41 +0800339 if (image->preserve_context)
Huang Yingfee7b0d2009-03-10 10:57:16 +0800340 restore_processor_state();
341#endif
342
343 __ftrace_enabled_restore(save_ftrace_enabled);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700344}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200345
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700346void arch_crash_save_vmcoreinfo(void)
347{
Ken'ichi Ohmichi629c8b42008-04-02 13:04:50 -0700348 VMCOREINFO_SYMBOL(phys_base);
Ken'ichi Ohmichi69243f92007-10-26 14:19:26 +0900349 VMCOREINFO_SYMBOL(init_level4_pgt);
Ken'ichi Ohmichi92df5c32008-02-07 00:15:23 -0800350
351#ifdef CONFIG_NUMA
352 VMCOREINFO_SYMBOL(node_data);
353 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
354#endif
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700355}
356