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 * ehdr: Pointer to elf headers
386 * sechdrs: Pointer to section headers.
387 * relsec: section index of SHT_RELA section.
389 * TODO: Some of the code belongs to generic code. Move that in kexec.c.
391 int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
392 Elf64_Shdr *sechdrs, unsigned int relsec)
398 Elf64_Shdr *section, *symtabsec;
399 unsigned long address, sec_base, value;
400 const char *strtab, *name, *shstrtab;
403 * ->sh_offset has been modified to keep the pointer to section
406 rel = (void *)sechdrs[relsec].sh_offset;
408 /* Section to which relocations apply */
409 section = &sechdrs[sechdrs[relsec].sh_info];
411 pr_debug("Applying relocate section %u to %u\n", relsec,
412 sechdrs[relsec].sh_info);
414 /* Associated symbol table */
415 symtabsec = &sechdrs[sechdrs[relsec].sh_link];
418 if (symtabsec->sh_link >= ehdr->e_shnum) {
419 /* Invalid strtab section number */
420 pr_err("Invalid string table section index %d\n",
425 strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
427 /* section header string table */
428 shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
430 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
433 * rel[i].r_offset contains byte offset from beginning
434 * of section to the storage unit affected.
436 * This is location to update (->sh_offset). This is temporary
437 * buffer where section is currently loaded. This will finally
438 * be loaded to a different address later, pointed to by
439 * ->sh_addr. kexec takes care of moving it
440 * (kexec_load_segment()).
442 location = (void *)(section->sh_offset + rel[i].r_offset);
444 /* Final address of the location */
445 address = section->sh_addr + rel[i].r_offset;
448 * rel[i].r_info contains information about symbol table index
449 * w.r.t which relocation must be made and type of relocation
450 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
451 * these respectively.
453 sym = (Elf64_Sym *)symtabsec->sh_offset +
454 ELF64_R_SYM(rel[i].r_info);
457 name = strtab + sym->st_name;
459 name = shstrtab + sechdrs[sym->st_shndx].sh_name;
461 pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
462 name, sym->st_info, sym->st_shndx, sym->st_value,
465 if (sym->st_shndx == SHN_UNDEF) {
466 pr_err("Undefined symbol: %s\n", name);
470 if (sym->st_shndx == SHN_COMMON) {
471 pr_err("symbol '%s' in common section\n", name);
475 if (sym->st_shndx == SHN_ABS)
477 else if (sym->st_shndx >= ehdr->e_shnum) {
478 pr_err("Invalid section %d for symbol %s\n",
479 sym->st_shndx, name);
482 sec_base = sechdrs[sym->st_shndx].sh_addr;
484 value = sym->st_value;
486 value += rel[i].r_addend;
488 switch (ELF64_R_TYPE(rel[i].r_info)) {
492 *(u64 *)location = value;
495 *(u32 *)location = value;
496 if (value != *(u32 *)location)
500 *(s32 *)location = value;
501 if ((s64)value != *(s32 *)location)
506 value -= (u64)address;
507 *(u32 *)location = value;
510 pr_err("Unknown rela relocation: %llu\n",
511 ELF64_R_TYPE(rel[i].r_info));
518 pr_err("Overflow in relocation type %d value 0x%lx\n",
519 (int)ELF64_R_TYPE(rel[i].r_info), value);
522 #endif /* CONFIG_KEXEC_FILE */
525 kexec_mark_range(unsigned long start, unsigned long end, bool protect)
528 unsigned int nr_pages;
531 * For physical range: [start, end]. We must skip the unassigned
532 * crashk resource with zero-valued "end" member.
534 if (!end || start > end)
537 page = pfn_to_page(start >> PAGE_SHIFT);
538 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
540 return set_pages_ro(page, nr_pages);
542 return set_pages_rw(page, nr_pages);
545 static void kexec_mark_crashkres(bool protect)
547 unsigned long control;
549 kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect);
551 /* Don't touch the control code page used in crash_kexec().*/
552 control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page));
553 /* Control code page is located in the 2nd page. */
554 kexec_mark_range(crashk_res.start, control + PAGE_SIZE - 1, protect);
555 control += KEXEC_CONTROL_PAGE_SIZE;
556 kexec_mark_range(control, crashk_res.end, protect);
559 void arch_kexec_protect_crashkres(void)
561 kexec_mark_crashkres(true);
564 void arch_kexec_unprotect_crashkres(void)
566 kexec_mark_crashkres(false);
569 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
572 * If SME is active we need to be sure that kexec pages are
573 * not encrypted because when we boot to the new kernel the
574 * pages won't be accessed encrypted (initially).
576 return set_memory_decrypted((unsigned long)vaddr, pages);
579 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
582 * If SME is active we need to reset the pages back to being
583 * an encrypted mapping before freeing them.
585 set_memory_encrypted((unsigned long)vaddr, pages);