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 18 /** 19 * kexec_image_info - Print received image details 20 */ 21 static void 22 kexec_image_info(const struct kimage *image) 23 { 24 unsigned long i; 25 26 pr_debug("Kexec image info:\n"); 27 pr_debug("\ttype: %d\n", image->type); 28 pr_debug("\tstart: %lx\n", image->start); 29 pr_debug("\thead: %lx\n", image->head); 30 pr_debug("\tnr_segments: %lu\n", image->nr_segments); 31 32 for (i = 0; i < image->nr_segments; i++) { 33 pr_debug("\t segment[%lu]: %016lx - %016lx", i, 34 image->segment[i].mem, 35 image->segment[i].mem + image->segment[i].memsz); 36 pr_debug("\t\t0x%lx bytes, %lu pages\n", 37 (unsigned long) image->segment[i].memsz, 38 (unsigned long) image->segment[i].memsz / PAGE_SIZE); 39 } 40 } 41 42 /** 43 * machine_kexec_prepare - Initialize kexec 44 * 45 * This function is called from do_kexec_load, when the user has 46 * provided us with an image to be loaded. Its goal is to validate 47 * the image and prepare the control code buffer as needed. 48 * Note that kimage_alloc_init has already been called and the 49 * control buffer has already been allocated. 50 */ 51 int 52 machine_kexec_prepare(struct kimage *image) 53 { 54 struct kimage_arch *internal = &image->arch; 55 struct fdt_header fdt = {0}; 56 void *control_code_buffer = NULL; 57 unsigned int control_code_buffer_sz = 0; 58 int i = 0; 59 60 kexec_image_info(image); 61 62 /* Find the Flattened Device Tree and save its physical address */ 63 for (i = 0; i < image->nr_segments; i++) { 64 if (image->segment[i].memsz <= sizeof(fdt)) 65 continue; 66 67 if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt))) 68 continue; 69 70 if (fdt_check_header(&fdt)) 71 continue; 72 73 internal->fdt_addr = (unsigned long) image->segment[i].mem; 74 break; 75 } 76 77 if (!internal->fdt_addr) { 78 pr_err("Device tree not included in the provided image\n"); 79 return -EINVAL; 80 } 81 82 /* Copy the assembler code for relocation to the control page */ 83 if (image->type != KEXEC_TYPE_CRASH) { 84 control_code_buffer = page_address(image->control_code_page); 85 control_code_buffer_sz = page_size(image->control_code_page); 86 87 if (unlikely(riscv_kexec_relocate_size > control_code_buffer_sz)) { 88 pr_err("Relocation code doesn't fit within a control page\n"); 89 return -EINVAL; 90 } 91 92 memcpy(control_code_buffer, riscv_kexec_relocate, 93 riscv_kexec_relocate_size); 94 95 /* Mark the control page executable */ 96 set_memory_x((unsigned long) control_code_buffer, 1); 97 } 98 99 return 0; 100 } 101 102 103 /** 104 * machine_kexec_cleanup - Cleanup any leftovers from 105 * machine_kexec_prepare 106 * 107 * This function is called by kimage_free to handle any arch-specific 108 * allocations done on machine_kexec_prepare. Since we didn't do any 109 * allocations there, this is just an empty function. Note that the 110 * control buffer is freed by kimage_free. 111 */ 112 void 113 machine_kexec_cleanup(struct kimage *image) 114 { 115 } 116 117 118 /* 119 * machine_shutdown - Prepare for a kexec reboot 120 * 121 * This function is called by kernel_kexec just before machine_kexec 122 * below. Its goal is to prepare the rest of the system (the other 123 * harts and possibly devices etc) for a kexec reboot. 124 */ 125 void machine_shutdown(void) 126 { 127 /* 128 * No more interrupts on this hart 129 * until we are back up. 130 */ 131 local_irq_disable(); 132 133 #if defined(CONFIG_HOTPLUG_CPU) 134 smp_shutdown_nonboot_cpus(smp_processor_id()); 135 #endif 136 } 137 138 /** 139 * machine_crash_shutdown - Prepare to kexec after a kernel crash 140 * 141 * This function is called by crash_kexec just before machine_kexec 142 * below and its goal is similar to machine_shutdown, but in case of 143 * a kernel crash. Since we don't handle such cases yet, this function 144 * is empty. 145 */ 146 void 147 machine_crash_shutdown(struct pt_regs *regs) 148 { 149 crash_save_cpu(regs, smp_processor_id()); 150 machine_shutdown(); 151 pr_info("Starting crashdump kernel...\n"); 152 } 153 154 /** 155 * machine_kexec - Jump to the loaded kimage 156 * 157 * This function is called by kernel_kexec which is called by the 158 * reboot system call when the reboot cmd is LINUX_REBOOT_CMD_KEXEC, 159 * or by crash_kernel which is called by the kernel's arch-specific 160 * trap handler in case of a kernel panic. It's the final stage of 161 * the kexec process where the pre-loaded kimage is ready to be 162 * executed. We assume at this point that all other harts are 163 * suspended and this hart will be the new boot hart. 164 */ 165 void __noreturn 166 machine_kexec(struct kimage *image) 167 { 168 struct kimage_arch *internal = &image->arch; 169 unsigned long jump_addr = (unsigned long) image->start; 170 unsigned long first_ind_entry = (unsigned long) &image->head; 171 unsigned long this_hart_id = raw_smp_processor_id(); 172 unsigned long fdt_addr = internal->fdt_addr; 173 void *control_code_buffer = page_address(image->control_code_page); 174 riscv_kexec_method kexec_method = NULL; 175 176 if (image->type != KEXEC_TYPE_CRASH) 177 kexec_method = control_code_buffer; 178 else 179 kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate; 180 181 pr_notice("Will call new kernel at %08lx from hart id %lx\n", 182 jump_addr, this_hart_id); 183 pr_notice("FDT image at %08lx\n", fdt_addr); 184 185 /* Make sure the relocation code is visible to the hart */ 186 local_flush_icache_all(); 187 188 /* Jump to the relocation code */ 189 pr_notice("Bye...\n"); 190 kexec_method(first_ind_entry, jump_addr, fdt_addr, 191 this_hart_id, va_pa_offset); 192 unreachable(); 193 } 194