^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) IBM Corporation, 2004. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) Red Hat Inc., 2014. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Vivek Goyal <vgoyal@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define pr_fmt(fmt) "kexec: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/elfcore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <asm/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <asm/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <asm/hw_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <asm/apic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <asm/e820/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <asm/io_apic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <asm/hpet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <asm/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <asm/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <asm/virtext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <asm/intel_pt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <asm/crash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <asm/cmdline.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Used while preparing memory map entries for second kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct crash_memmap_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct boot_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* Type of memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * This is used to VMCLEAR all VMCSs loaded on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * processor. And when loading kvm_intel module, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * callback function pointer will be assigned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * protected by rcu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) EXPORT_SYMBOL_GPL(crash_vmclear_loaded_vmcss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static inline void cpu_crash_vmclear_loaded_vmcss(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) crash_vmclear_fn *do_vmclear_operation = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) do_vmclear_operation = rcu_dereference(crash_vmclear_loaded_vmcss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (do_vmclear_operation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) do_vmclear_operation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * When the crashkernel option is specified, only use the low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * 1M for the real mode trampoline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) void __init crash_reserve_low_1M(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (cmdline_find_option(boot_command_line, "crashkernel", NULL, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) memblock_reserve(0, 1<<20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_info("Reserving the low 1M of memory for crashkernel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) crash_save_cpu(regs, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * VMCLEAR VMCSs loaded on all cpus if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) cpu_crash_vmclear_loaded_vmcss();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Disable VMX or SVM if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * We need to disable virtualization on all CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Having VMX or SVM enabled on any CPU may break rebooting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * after the kdump kernel has finished its task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cpu_emergency_vmxoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) cpu_emergency_svm_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * Disable Intel PT to stop its logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) cpu_emergency_stop_pt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) disable_local_APIC();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void kdump_nmi_shootdown_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) nmi_shootdown_cpus(kdump_nmi_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) disable_local_APIC();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* Override the weak function in kernel/panic.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void crash_smp_send_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int cpus_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (cpus_stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (smp_ops.crash_stop_other_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) smp_ops.crash_stop_other_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) smp_send_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) cpus_stopped = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void crash_smp_send_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* There are no cpus to shootdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void native_machine_crash_shutdown(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* This function is only called after the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * has panicked or is otherwise in a critical state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * The minimum amount of code to allow a kexec'd kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * to run successfully needs to happen here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * In practice this means shooting down the other cpus in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * an SMP system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* The kernel is broken so disable interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) crash_smp_send_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * VMCLEAR VMCSs loaded on this cpu if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) cpu_crash_vmclear_loaded_vmcss();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Booting kdump kernel with VMX or SVM enabled won't work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * because (among other limitations) we can't disable paging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * with the virt flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) cpu_emergency_vmxoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) cpu_emergency_svm_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * Disable Intel PT to stop its logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) cpu_emergency_stop_pt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #ifdef CONFIG_X86_IO_APIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Prevent crash_kexec() from deadlocking on ioapic_lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ioapic_zap_locks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) clear_IO_APIC();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) lapic_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) restore_boot_irq_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #ifdef CONFIG_HPET_TIMER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) hpet_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) crash_save_cpu(regs, safe_smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #ifdef CONFIG_KEXEC_FILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned int *nr_ranges = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) (*nr_ranges)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* Gather all the required information to prepare elf headers for ram regions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static struct crash_mem *fill_up_crash_elf_data(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int nr_ranges = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct crash_mem *cmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!nr_ranges)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Exclusion of crash region and/or crashk_low_res may cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * another range split. So add extra two slots here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) nr_ranges += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) cmem = vzalloc(struct_size(cmem, ranges, nr_ranges));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!cmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) cmem->max_nr_ranges = nr_ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) cmem->nr_ranges = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return cmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Look for any unwanted ranges between mstart, mend and remove them. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * might lead to split and split ranges are put in cmem->ranges[] array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int elf_header_exclude_ranges(struct crash_mem *cmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Exclude the low 1M because it is always reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ret = crash_exclude_mem_range(cmem, 0, (1<<20)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* Exclude crashkernel region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (crashk_low_res.end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ret = crash_exclude_mem_range(cmem, crashk_low_res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) crashk_low_res.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct crash_mem *cmem = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) cmem->ranges[cmem->nr_ranges].start = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) cmem->ranges[cmem->nr_ranges].end = res->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) cmem->nr_ranges++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* Prepare elf headers. Return addr and size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int prepare_elf_headers(struct kimage *image, void **addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned long *sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct crash_mem *cmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) cmem = fill_up_crash_elf_data();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!cmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Exclude unwanted mem ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ret = elf_header_exclude_ranges(cmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* By default prepare 64bit headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = crash_prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) vfree(cmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static int add_e820_entry(struct boot_params *params, struct e820_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned int nr_e820_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) nr_e820_entries = params->e820_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) memcpy(¶ms->e820_table[nr_e820_entries], entry, sizeof(struct e820_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) params->e820_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int memmap_entry_callback(struct resource *res, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct crash_memmap_data *cmd = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct boot_params *params = cmd->params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct e820_entry ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ei.addr = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ei.size = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ei.type = cmd->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) add_e820_entry(params, &ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) unsigned long long mstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) unsigned long long mend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) cmem->ranges[0].start = mstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) cmem->ranges[0].end = mend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) cmem->nr_ranges = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* Exclude elf header region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) start = image->arch.elf_load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) end = start + image->arch.elf_headers_sz - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return crash_exclude_mem_range(cmem, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /* Prepare memory map for crash dump kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct e820_entry ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct crash_memmap_data cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct crash_mem *cmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) cmem = vzalloc(struct_size(cmem, ranges, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (!cmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) memset(&cmd, 0, sizeof(struct crash_memmap_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) cmd.params = params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* Add the low 1M */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) cmd.type = E820_TYPE_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) walk_iomem_res_desc(IORES_DESC_NONE, flags, 0, (1<<20)-1, &cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) memmap_entry_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* Add ACPI tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) cmd.type = E820_TYPE_ACPI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) flags = IORESOURCE_MEM | IORESOURCE_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) memmap_entry_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* Add ACPI Non-volatile Storage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) cmd.type = E820_TYPE_NVS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) memmap_entry_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /* Add e820 reserved ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) cmd.type = E820_TYPE_RESERVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) memmap_entry_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /* Add crashk_low_res region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (crashk_low_res.end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ei.addr = crashk_low_res.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) ei.size = resource_size(&crashk_low_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ei.type = E820_TYPE_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) add_e820_entry(params, &ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* Exclude some ranges from crashk_res and add rest to memmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ret = memmap_exclude_ranges(image, cmem, crashk_res.start, crashk_res.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) for (i = 0; i < cmem->nr_ranges; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* If entry is less than a page, skip it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (ei.size < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ei.addr = cmem->ranges[i].start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ei.type = E820_TYPE_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) add_e820_entry(params, &ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) vfree(cmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) int crash_load_segments(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct kexec_buf kbuf = { .image = image, .buf_min = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .buf_max = ULONG_MAX, .top_down = false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* Prepare elf headers and add a segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ret = prepare_elf_headers(image, &kbuf.buffer, &kbuf.bufsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) image->arch.elf_headers = kbuf.buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) image->arch.elf_headers_sz = kbuf.bufsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) kbuf.memsz = kbuf.bufsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = kexec_add_buffer(&kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) vfree((void *)image->arch.elf_headers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) image->arch.elf_load_addr = kbuf.mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) image->arch.elf_load_addr, kbuf.bufsz, kbuf.bufsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) #endif /* CONFIG_KEXEC_FILE */