^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * machine_kexec.c - handle transition of Linux booting another kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/of_fdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/kexec-internal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/fncpy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/mach-types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/smp_plat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/system_misc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/set_memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) extern void relocate_new_kernel(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) extern const unsigned int relocate_new_kernel_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static atomic_t waiting_for_crash_ipi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Provide a dummy crash_notes definition while crash dump arrives to arm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * This prevents breakage of crash_notes attribute in kernel/ksysfs.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int machine_kexec_prepare(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct kexec_segment *current_segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __be32 header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) image->arch.kernel_r2 = image->start - KEXEC_ARM_ZIMAGE_OFFSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) + KEXEC_ARM_ATAGS_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Validate that if the current HW supports SMP, then the SW supports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * and implements CPU hotplug for the current HW. If not, we won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * able to kexec reliably, so fail the prepare operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (num_possible_cpus() > 1 && platform_can_secondary_boot() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) !platform_can_cpu_hotplug())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return -EINVAL;
^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) * No segment at default ATAGs address. try to locate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * a dtb using magic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) for (i = 0; i < image->nr_segments; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) current_segment = &image->segment[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!memblock_is_region_memory(idmap_to_phys(current_segment->mem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) current_segment->memsz))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) err = get_user(header, (__be32*)current_segment->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (header == cpu_to_be32(OF_DT_HEADER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) image->arch.kernel_r2 = current_segment->mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) void machine_kexec_cleanup(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) void machine_crash_nonpanic_core(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct pt_regs regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) crash_setup_regs(®s, get_irq_regs());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) printk(KERN_DEBUG "CPU %u will stop doing anything useful since another CPU has crashed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) crash_save_cpu(®s, smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) flush_cache_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) set_cpu_online(smp_processor_id(), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) atomic_dec(&waiting_for_crash_ipi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) wfe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void crash_smp_send_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int cpus_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned long msecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (cpus_stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) smp_call_function(machine_crash_nonpanic_core, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) msecs = 1000; /* Wait at most a second for the other cpus to stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) mdelay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) msecs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (atomic_read(&waiting_for_crash_ipi) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pr_warn("Non-crashing CPUs did not react to IPI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) cpus_stopped = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void machine_kexec_mask_interrupts(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for_each_irq_desc(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct irq_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) chip = irq_desc_get_chip(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) chip->irq_eoi(&desc->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (chip->irq_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) chip->irq_mask(&desc->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) chip->irq_disable(&desc->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void machine_crash_shutdown(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) crash_smp_send_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) crash_save_cpu(regs, smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) machine_kexec_mask_interrupts();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) pr_info("Loading crashdump kernel...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Function pointer to optional machine-specific reinitialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void (*kexec_reinit)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) void machine_kexec(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long page_list, reboot_entry_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct kexec_relocate_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void (*reboot_entry)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void *reboot_code_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * This can only happen if machine_shutdown() failed to disable some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * CPU, and that can only happen if the checks in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * machine_kexec_prepare() were not correct. If this fails, we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * reliably kexec anyway, so BUG_ON is appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) BUG_ON(num_online_cpus() > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) page_list = image->head & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) reboot_code_buffer = page_address(image->control_code_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* copy our kernel relocation code to the control code page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) reboot_entry = fncpy(reboot_code_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) &relocate_new_kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) relocate_new_kernel_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) data = reboot_code_buffer + relocate_new_kernel_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) data->kexec_start_address = image->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) data->kexec_indirection_page = page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) data->kexec_mach_type = machine_arch_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) data->kexec_r2 = image->arch.kernel_r2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* get the identity mapping physical address for the reboot code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) reboot_entry_phys = virt_to_idmap(reboot_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) pr_info("Bye!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (kexec_reinit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) kexec_reinit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) soft_restart(reboot_entry_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void arch_crash_save_vmcoreinfo(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #ifdef CONFIG_ARM_LPAE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) VMCOREINFO_CONFIG(ARM_LPAE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }