1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2019 FORTH-ICS/CARV 4 * Nick Kossifidis <mick@ics.forth.gr> 5 */ 6 7 #include <linux/kexec.h> 8 #include <asm/kexec.h> /* For riscv_kexec_* symbol defines */ 9 #include <linux/smp.h> /* For smp_send_stop () */ 10 #include <asm/cacheflush.h> /* For local_flush_icache_all() */ 11 #include <asm/barrier.h> /* For smp_wmb() */ 12 #include <asm/page.h> /* For PAGE_MASK */ 13 #include <linux/libfdt.h> /* For fdt_check_header() */ 14 #include <asm/set_memory.h> /* For set_memory_x() */ 15 #include <linux/compiler.h> /* For unreachable() */ 16 #include <linux/cpu.h> /* For cpu_down() */ 17 #include <linux/reboot.h> 18 #include <linux/interrupt.h> 19 #include <linux/irq.h> 20 21 /* 22 * machine_kexec_prepare - Initialize kexec 23 * 24 * This function is called from do_kexec_load, when the user has 25 * provided us with an image to be loaded. Its goal is to validate 26 * the image and prepare the control code buffer as needed. 27 * Note that kimage_alloc_init has already been called and the 28 * control buffer has already been allocated. 29 */ 30 int 31 machine_kexec_prepare(struct kimage *image) 32 { 33 struct kimage_arch *internal = &image->arch; 34 struct fdt_header fdt = {0}; 35 void *control_code_buffer = NULL; 36 unsigned int control_code_buffer_sz = 0; 37 int i = 0; 38 39 /* Find the Flattened Device Tree and save its physical address */ 40 for (i = 0; i < image->nr_segments; i++) { 41 if (image->segment[i].memsz <= sizeof(fdt)) 42 continue; 43 44 if (!image->segment[i].buf) 45 continue; 46 47 if (image->file_mode) 48 memcpy(&fdt, image->segment[i].buf, sizeof(fdt)); 49 else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt))) 50 continue; 51 52 if (fdt_check_header(&fdt)) 53 continue; 54 55 internal->fdt_addr = (unsigned long) image->segment[i].mem; 56 break; 57 } 58 59 if (!internal->fdt_addr) { 60 pr_err("Device tree not included in the provided image\n"); 61 return -EINVAL; 62 } 63 64 /* Copy the assembler code for relocation to the control page */ 65 if (image->type != KEXEC_TYPE_CRASH) { 66 control_code_buffer = page_address(image->control_code_page); 67 control_code_buffer_sz = page_size(image->control_code_page); 68 69 if (unlikely(riscv_kexec_relocate_size > control_code_buffer_sz)) { 70 pr_err("Relocation code doesn't fit within a control page\n"); 71 return -EINVAL; 72 } 73 74 memcpy(control_code_buffer, riscv_kexec_relocate, 75 riscv_kexec_relocate_size); 76 77 /* Mark the control page executable */ 78 set_memory_x((unsigned long) control_code_buffer, 1); 79 } 80 81 return 0; 82 } 83 84 85 /* 86 * machine_kexec_cleanup - Cleanup any leftovers from 87 * machine_kexec_prepare 88 * 89 * This function is called by kimage_free to handle any arch-specific 90 * allocations done on machine_kexec_prepare. Since we didn't do any 91 * allocations there, this is just an empty function. Note that the 92 * control buffer is freed by kimage_free. 93 */ 94 void 95 machine_kexec_cleanup(struct kimage *image) 96 { 97 } 98 99 100 /* 101 * machine_shutdown - Prepare for a kexec reboot 102 * 103 * This function is called by kernel_kexec just before machine_kexec 104 * below. Its goal is to prepare the rest of the system (the other 105 * harts and possibly devices etc) for a kexec reboot. 106 */ 107 void machine_shutdown(void) 108 { 109 /* 110 * No more interrupts on this hart 111 * until we are back up. 112 */ 113 local_irq_disable(); 114 115 #if defined(CONFIG_HOTPLUG_CPU) 116 smp_shutdown_nonboot_cpus(smp_processor_id()); 117 #endif 118 } 119 120 /* 121 * machine_crash_shutdown - Prepare to kexec after a kernel crash 122 * 123 * This function is called by crash_kexec just before machine_kexec 124 * and its goal is to shutdown non-crashing cpus and save registers. 125 */ 126 void 127 machine_crash_shutdown(struct pt_regs *regs) 128 { 129 local_irq_disable(); 130 131 /* shutdown non-crashing cpus */ 132 crash_smp_send_stop(); 133 134 crash_save_cpu(regs, smp_processor_id()); 135 machine_kexec_mask_interrupts(); 136 137 pr_info("Starting crashdump kernel...\n"); 138 } 139 140 /* 141 * machine_kexec - Jump to the loaded kimage 142 * 143 * This function is called by kernel_kexec which is called by the 144 * reboot system call when the reboot cmd is LINUX_REBOOT_CMD_KEXEC, 145 * or by crash_kernel which is called by the kernel's arch-specific 146 * trap handler in case of a kernel panic. It's the final stage of 147 * the kexec process where the pre-loaded kimage is ready to be 148 * executed. We assume at this point that all other harts are 149 * suspended and this hart will be the new boot hart. 150 */ 151 void __noreturn 152 machine_kexec(struct kimage *image) 153 { 154 struct kimage_arch *internal = &image->arch; 155 unsigned long jump_addr = (unsigned long) image->start; 156 unsigned long first_ind_entry = (unsigned long) &image->head; 157 unsigned long this_cpu_id = __smp_processor_id(); 158 unsigned long this_hart_id = cpuid_to_hartid_map(this_cpu_id); 159 unsigned long fdt_addr = internal->fdt_addr; 160 void *control_code_buffer = page_address(image->control_code_page); 161 riscv_kexec_method kexec_method = NULL; 162 163 #ifdef CONFIG_SMP 164 WARN(smp_crash_stop_failed(), 165 "Some CPUs may be stale, kdump will be unreliable.\n"); 166 #endif 167 168 if (image->type != KEXEC_TYPE_CRASH) 169 kexec_method = control_code_buffer; 170 else 171 kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate; 172 173 pr_notice("Will call new kernel at %08lx from hart id %lx\n", 174 jump_addr, this_hart_id); 175 pr_notice("FDT image at %08lx\n", fdt_addr); 176 177 /* Make sure the relocation code is visible to the hart */ 178 local_flush_icache_all(); 179 180 /* Jump to the relocation code */ 181 pr_notice("Bye...\n"); 182 kexec_method(first_ind_entry, jump_addr, fdt_addr, 183 this_hart_id, kernel_map.va_pa_offset); 184 unreachable(); 185 } 186