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.
9 #define pr_fmt(fmt) "kexec: " fmt
12 #include <linux/kexec.h>
13 #include <linux/string.h>
14 #include <linux/gfp.h>
15 #include <linux/reboot.h>
16 #include <linux/numa.h>
17 #include <linux/ftrace.h>
19 #include <linux/suspend.h>
20 #include <linux/vmalloc.h>
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
25 #include <asm/mmu_context.h>
26 #include <asm/io_apic.h>
27 #include <asm/debugreg.h>
28 #include <asm/kexec-bzimage64.h>
29 #include <asm/setup.h>
30 #include <asm/set_memory.h>
32 #ifdef CONFIG_KEXEC_FILE
33 const struct kexec_file_ops * const kexec_file_loaders[] = {
39 static void free_transition_pgtable(struct kimage *image)
41 free_page((unsigned long)image->arch.p4d);
42 free_page((unsigned long)image->arch.pud);
43 free_page((unsigned long)image->arch.pmd);
44 free_page((unsigned long)image->arch.pte);
47 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
53 unsigned long vaddr, paddr;
56 vaddr = (unsigned long)relocate_kernel;
57 paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
58 pgd += pgd_index(vaddr);
59 if (!pgd_present(*pgd)) {
60 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
63 image->arch.p4d = p4d;
64 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE));
66 p4d = p4d_offset(pgd, vaddr);
67 if (!p4d_present(*p4d)) {
68 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
71 image->arch.pud = pud;
72 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
74 pud = pud_offset(p4d, vaddr);
75 if (!pud_present(*pud)) {
76 pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
79 image->arch.pmd = pmd;
80 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
82 pmd = pmd_offset(pud, vaddr);
83 if (!pmd_present(*pmd)) {
84 pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
87 image->arch.pte = pte;
88 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
90 pte = pte_offset_kernel(pmd, vaddr);
91 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC_NOENC));
94 free_transition_pgtable(image);
98 static void *alloc_pgt_page(void *data)
100 struct kimage *image = (struct kimage *)data;
104 page = kimage_alloc_control_pages(image, 0);
106 p = page_address(page);
113 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
115 struct x86_mapping_info info = {
116 .alloc_pgt_page = alloc_pgt_page,
118 .page_flag = __PAGE_KERNEL_LARGE_EXEC,
119 .kernpg_flag = _KERNPG_TABLE_NOENC,
121 unsigned long mstart, mend;
126 level4p = (pgd_t *)__va(start_pgtable);
130 info.direct_gbpages = true;
132 for (i = 0; i < nr_pfn_mapped; i++) {
133 mstart = pfn_mapped[i].start << PAGE_SHIFT;
134 mend = pfn_mapped[i].end << PAGE_SHIFT;
136 result = kernel_ident_mapping_init(&info,
137 level4p, mstart, mend);
143 * segments's mem ranges could be outside 0 ~ max_pfn,
144 * for example when jump back to original kernel from kexeced kernel.
145 * or first kernel is booted with user mem map, and second kernel
146 * could be loaded out of that range.
148 for (i = 0; i < image->nr_segments; i++) {
149 mstart = image->segment[i].mem;
150 mend = mstart + image->segment[i].memsz;
152 result = kernel_ident_mapping_init(&info,
153 level4p, mstart, mend);
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 #ifdef CONFIG_KEXEC_FILE
204 /* Update purgatory as needed after various image segments have been prepared */
205 static int arch_update_purgatory(struct kimage *image)
209 if (!image->file_mode)
212 /* Setup copying of backup region */
213 if (image->type == KEXEC_TYPE_CRASH) {
214 ret = kexec_purgatory_get_set_symbol(image,
215 "purgatory_backup_dest",
216 &image->arch.backup_load_addr,
217 sizeof(image->arch.backup_load_addr), 0);
221 ret = kexec_purgatory_get_set_symbol(image,
222 "purgatory_backup_src",
223 &image->arch.backup_src_start,
224 sizeof(image->arch.backup_src_start), 0);
228 ret = kexec_purgatory_get_set_symbol(image,
229 "purgatory_backup_sz",
230 &image->arch.backup_src_sz,
231 sizeof(image->arch.backup_src_sz), 0);
238 #else /* !CONFIG_KEXEC_FILE */
239 static inline int arch_update_purgatory(struct kimage *image)
243 #endif /* CONFIG_KEXEC_FILE */
245 int machine_kexec_prepare(struct kimage *image)
247 unsigned long start_pgtable;
250 /* Calculate the offsets */
251 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
253 /* Setup the identity mapped 64bit page table */
254 result = init_pgtable(image, start_pgtable);
258 /* update purgatory as needed */
259 result = arch_update_purgatory(image);
266 void machine_kexec_cleanup(struct kimage *image)
268 free_transition_pgtable(image);
272 * Do not allocate memory (or fail in any way) in machine_kexec().
273 * We are past the point of no return, committed to rebooting now.
275 void machine_kexec(struct kimage *image)
277 unsigned long page_list[PAGES_NR];
279 int save_ftrace_enabled;
281 #ifdef CONFIG_KEXEC_JUMP
282 if (image->preserve_context)
283 save_processor_state();
286 save_ftrace_enabled = __ftrace_enabled_save();
288 /* Interrupts aren't acceptable while we reboot */
290 hw_breakpoint_disable();
292 if (image->preserve_context) {
293 #ifdef CONFIG_X86_IO_APIC
295 * We need to put APICs in legacy mode so that we can
296 * get timer interrupts in second kernel. kexec/kdump
297 * paths already have calls to restore_boot_irq_mode()
298 * in one form or other. kexec jump path also need one.
301 restore_boot_irq_mode();
305 control_page = page_address(image->control_code_page) + PAGE_SIZE;
306 memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
308 page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
309 page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
310 page_list[PA_TABLE_PAGE] =
311 (unsigned long)__pa(page_address(image->control_code_page));
313 if (image->type == KEXEC_TYPE_DEFAULT)
314 page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
318 * The segment registers are funny things, they have both a
319 * visible and an invisible part. Whenever the visible part is
320 * set to a specific selector, the invisible part is loaded
321 * with from a table in memory. At no other time is the
322 * descriptor table in memory accessed.
324 * I take advantage of this here by force loading the
325 * segments, before I zap the gdt with an invalid value.
329 * The gdt & idt are now invalid.
330 * If you want to load them you must set up your own idt & gdt.
332 set_gdt(phys_to_virt(0), 0);
333 set_idt(phys_to_virt(0), 0);
336 image->start = relocate_kernel((unsigned long)image->head,
337 (unsigned long)page_list,
339 image->preserve_context,
342 #ifdef CONFIG_KEXEC_JUMP
343 if (image->preserve_context)
344 restore_processor_state();
347 __ftrace_enabled_restore(save_ftrace_enabled);
350 void arch_crash_save_vmcoreinfo(void)
352 VMCOREINFO_NUMBER(phys_base);
353 VMCOREINFO_SYMBOL(init_top_pgt);
354 VMCOREINFO_NUMBER(pgtable_l5_enabled);
357 VMCOREINFO_SYMBOL(node_data);
358 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
360 vmcoreinfo_append_str("KERNELOFFSET=%lx\n",
362 VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
365 /* arch-dependent functionality related to kexec file-based syscall */
367 #ifdef CONFIG_KEXEC_FILE
368 void *arch_kexec_kernel_image_load(struct kimage *image)
370 vfree(image->arch.elf_headers);
371 image->arch.elf_headers = NULL;
373 if (!image->fops || !image->fops->load)
374 return ERR_PTR(-ENOEXEC);
376 return image->fops->load(image, image->kernel_buf,
377 image->kernel_buf_len, image->initrd_buf,
378 image->initrd_buf_len, image->cmdline_buf,
379 image->cmdline_buf_len);
383 * Apply purgatory relocations.
385 * @pi: Purgatory to be relocated.
386 * @section: Section relocations applying to.
387 * @relsec: Section containing RELAs.
388 * @symtabsec: Corresponding symtab.
390 * TODO: Some of the code belongs to generic code. Move that in kexec.c.
392 int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
393 Elf_Shdr *section, const Elf_Shdr *relsec,
394 const Elf_Shdr *symtabsec)
400 unsigned long address, sec_base, value;
401 const char *strtab, *name, *shstrtab;
402 const Elf_Shdr *sechdrs;
404 /* String & section header string table */
405 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
406 strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset;
407 shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
409 rel = (void *)pi->ehdr + relsec->sh_offset;
411 pr_debug("Applying relocate section %s to %u\n",
412 shstrtab + relsec->sh_name, relsec->sh_info);
414 for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) {
417 * rel[i].r_offset contains byte offset from beginning
418 * of section to the storage unit affected.
420 * This is location to update. This is temporary buffer
421 * where section is currently loaded. This will finally be
422 * loaded to a different address later, pointed to by
423 * ->sh_addr. kexec takes care of moving it
424 * (kexec_load_segment()).
426 location = pi->purgatory_buf;
427 location += section->sh_offset;
428 location += rel[i].r_offset;
430 /* Final address of the location */
431 address = section->sh_addr + rel[i].r_offset;
434 * rel[i].r_info contains information about symbol table index
435 * w.r.t which relocation must be made and type of relocation
436 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
437 * these respectively.
439 sym = (void *)pi->ehdr + symtabsec->sh_offset;
440 sym += ELF64_R_SYM(rel[i].r_info);
443 name = strtab + sym->st_name;
445 name = shstrtab + sechdrs[sym->st_shndx].sh_name;
447 pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
448 name, sym->st_info, sym->st_shndx, sym->st_value,
451 if (sym->st_shndx == SHN_UNDEF) {
452 pr_err("Undefined symbol: %s\n", name);
456 if (sym->st_shndx == SHN_COMMON) {
457 pr_err("symbol '%s' in common section\n", name);
461 if (sym->st_shndx == SHN_ABS)
463 else if (sym->st_shndx >= pi->ehdr->e_shnum) {
464 pr_err("Invalid section %d for symbol %s\n",
465 sym->st_shndx, name);
468 sec_base = pi->sechdrs[sym->st_shndx].sh_addr;
470 value = sym->st_value;
472 value += rel[i].r_addend;
474 switch (ELF64_R_TYPE(rel[i].r_info)) {
478 *(u64 *)location = value;
481 *(u32 *)location = value;
482 if (value != *(u32 *)location)
486 *(s32 *)location = value;
487 if ((s64)value != *(s32 *)location)
492 value -= (u64)address;
493 *(u32 *)location = value;
496 pr_err("Unknown rela relocation: %llu\n",
497 ELF64_R_TYPE(rel[i].r_info));
504 pr_err("Overflow in relocation type %d value 0x%lx\n",
505 (int)ELF64_R_TYPE(rel[i].r_info), value);
508 #endif /* CONFIG_KEXEC_FILE */
511 kexec_mark_range(unsigned long start, unsigned long end, bool protect)
514 unsigned int nr_pages;
517 * For physical range: [start, end]. We must skip the unassigned
518 * crashk resource with zero-valued "end" member.
520 if (!end || start > end)
523 page = pfn_to_page(start >> PAGE_SHIFT);
524 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
526 return set_pages_ro(page, nr_pages);
528 return set_pages_rw(page, nr_pages);
531 static void kexec_mark_crashkres(bool protect)
533 unsigned long control;
535 kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect);
537 /* Don't touch the control code page used in crash_kexec().*/
538 control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page));
539 /* Control code page is located in the 2nd page. */
540 kexec_mark_range(crashk_res.start, control + PAGE_SIZE - 1, protect);
541 control += KEXEC_CONTROL_PAGE_SIZE;
542 kexec_mark_range(control, crashk_res.end, protect);
545 void arch_kexec_protect_crashkres(void)
547 kexec_mark_crashkres(true);
550 void arch_kexec_unprotect_crashkres(void)
552 kexec_mark_crashkres(false);
555 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
558 * If SME is active we need to be sure that kexec pages are
559 * not encrypted because when we boot to the new kernel the
560 * pages won't be accessed encrypted (initially).
562 return set_memory_decrypted((unsigned long)vaddr, pages);
565 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
568 * If SME is active we need to reset the pages back to being
569 * an encrypted mapping before freeing them.
571 set_memory_encrypted((unsigned long)vaddr, pages);