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