^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) * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * LANDISK/sh4 supported by kogiidena
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/numa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/sh_bios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) typedef void (*relocate_new_kernel_t)(unsigned long indirection_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long reboot_code_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned long start_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) extern const unsigned char relocate_new_kernel[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) extern const unsigned int relocate_new_kernel_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) extern void *vbr_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void native_machine_crash_shutdown(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Nothing to do for UP, but definitely broken for SMP.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Do what every setup is needed on image and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * reboot code buffer to allow us to avoid allocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int machine_kexec_prepare(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void machine_kexec_cleanup(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void kexec_info(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) printk("kexec information\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) for (i = 0; i < image->nr_segments; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) printk(" segment[%d]: 0x%08x - 0x%08x (0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) (unsigned int)image->segment[i].mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) (unsigned int)image->segment[i].mem +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) image->segment[i].memsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) (unsigned int)image->segment[i].memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) printk(" start : 0x%08x\n\n", (unsigned int)image->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Do not allocate memory (or fail in any way) in machine_kexec().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * We are past the point of no return, committed to rebooting now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) void machine_kexec(struct kimage *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned long page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long reboot_code_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) relocate_new_kernel_t rnk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned long entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned long *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int save_ftrace_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Nicked from the mips version of machine_kexec():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * The generic kexec code builds a page list with physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * addresses. Use phys_to_virt() to convert them to virtual.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ptr = (entry & IND_INDIRECTION) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *ptr & IND_DESTINATION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *ptr = (unsigned long) phys_to_virt(*ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #ifdef CONFIG_KEXEC_JUMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (image->preserve_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) save_processor_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) save_ftrace_enabled = __ftrace_enabled_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Interrupts aren't acceptable while we reboot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) page_list = image->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* we need both effective and real address here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) reboot_code_buffer =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (unsigned long)page_address(image->control_code_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* copy our kernel relocation code to the control code page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) memcpy((void *)reboot_code_buffer, relocate_new_kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) relocate_new_kernel_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kexec_info(image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) flush_cache_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) sh_bios_vbr_reload();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* now call it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) rnk = (relocate_new_kernel_t) reboot_code_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) (*rnk)(page_list, reboot_code_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) (unsigned long)phys_to_virt(image->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #ifdef CONFIG_KEXEC_JUMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) asm volatile("ldc %0, vbr" : : "r" (&vbr_base) : "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (image->preserve_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) restore_processor_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* Convert page list back to physical addresses, what a mess. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ptr = (*ptr & IND_INDIRECTION) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) phys_to_virt(*ptr & PAGE_MASK) : ptr + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) *ptr & IND_DESTINATION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *ptr = virt_to_phys(*ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) __ftrace_enabled_restore(save_ftrace_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) void arch_crash_save_vmcoreinfo(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) VMCOREINFO_SYMBOL(node_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #ifdef CONFIG_X2TLB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) VMCOREINFO_CONFIG(X2TLB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #endif
^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) void __init reserve_crashkernel(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long long crash_size, crash_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) &crash_size, &crash_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret == 0 && crash_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) crashk_res.start = crash_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) crashk_res.end = crash_base + crash_size - 1;
^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) if (crashk_res.end == crashk_res.start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) crash_size = PAGE_ALIGN(resource_size(&crashk_res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (!crashk_res.start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) unsigned long max = memblock_end_of_DRAM() - memory_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) crashk_res.start = memblock_phys_alloc_range(crash_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) PAGE_SIZE, 0, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!crashk_res.start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pr_err("crashkernel allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ret = memblock_reserve(crashk_res.start, crash_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (unlikely(ret < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pr_err("crashkernel reservation failed - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) "memory is in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) crashk_res.end = crashk_res.start + crash_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * Crash kernel trumps memory limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if ((memblock_end_of_DRAM() - memory_limit) <= crashk_res.end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) memory_limit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) pr_info("Disabled memory limit for crashkernel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pr_info("Reserving %ldMB of memory at 0x%08lx "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) "for crashkernel (System RAM: %ldMB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) (unsigned long)(crash_size >> 20),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) (unsigned long)(crashk_res.start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) (unsigned long)(memblock_phys_mem_size() >> 20));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) disable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) crashk_res.start = crashk_res.end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }