xref: /linux/arch/riscv/kernel/machine_kexec.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
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
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
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
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  */
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 
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 		int ret;
151 
152 		chip = irq_desc_get_chip(desc);
153 		if (!chip)
154 			continue;
155 
156 		/*
157 		 * First try to remove the active state. If this
158 		 * fails, try to EOI the interrupt.
159 		 */
160 		ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
161 
162 		if (ret && irqd_irq_inprogress(&desc->irq_data) &&
163 		    chip->irq_eoi)
164 			chip->irq_eoi(&desc->irq_data);
165 
166 		if (chip->irq_mask)
167 			chip->irq_mask(&desc->irq_data);
168 
169 		if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
170 			chip->irq_disable(&desc->irq_data);
171 	}
172 }
173 
174 /*
175  * machine_crash_shutdown - Prepare to kexec after a kernel crash
176  *
177  * This function is called by crash_kexec just before machine_kexec
178  * and its goal is to shutdown non-crashing cpus and save registers.
179  */
180 void
181 machine_crash_shutdown(struct pt_regs *regs)
182 {
183 	local_irq_disable();
184 
185 	/* shutdown non-crashing cpus */
186 	crash_smp_send_stop();
187 
188 	crash_save_cpu(regs, smp_processor_id());
189 	machine_kexec_mask_interrupts();
190 
191 	pr_info("Starting crashdump kernel...\n");
192 }
193 
194 /*
195  * machine_kexec - Jump to the loaded kimage
196  *
197  * This function is called by kernel_kexec which is called by the
198  * reboot system call when the reboot cmd is LINUX_REBOOT_CMD_KEXEC,
199  * or by crash_kernel which is called by the kernel's arch-specific
200  * trap handler in case of a kernel panic. It's the final stage of
201  * the kexec process where the pre-loaded kimage is ready to be
202  * executed. We assume at this point that all other harts are
203  * suspended and this hart will be the new boot hart.
204  */
205 void __noreturn
206 machine_kexec(struct kimage *image)
207 {
208 	struct kimage_arch *internal = &image->arch;
209 	unsigned long jump_addr = (unsigned long) image->start;
210 	unsigned long first_ind_entry = (unsigned long) &image->head;
211 	unsigned long this_cpu_id = __smp_processor_id();
212 	unsigned long this_hart_id = cpuid_to_hartid_map(this_cpu_id);
213 	unsigned long fdt_addr = internal->fdt_addr;
214 	void *control_code_buffer = page_address(image->control_code_page);
215 	riscv_kexec_method kexec_method = NULL;
216 
217 #ifdef CONFIG_SMP
218 	WARN(smp_crash_stop_failed(),
219 		"Some CPUs may be stale, kdump will be unreliable.\n");
220 #endif
221 
222 	if (image->type != KEXEC_TYPE_CRASH)
223 		kexec_method = control_code_buffer;
224 	else
225 		kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
226 
227 	pr_notice("Will call new kernel at %08lx from hart id %lx\n",
228 		  jump_addr, this_hart_id);
229 	pr_notice("FDT image at %08lx\n", fdt_addr);
230 
231 	/* Make sure the relocation code is visible to the hart */
232 	local_flush_icache_all();
233 
234 	/* Jump to the relocation code */
235 	pr_notice("Bye...\n");
236 	kexec_method(first_ind_entry, jump_addr, fdt_addr,
237 		     this_hart_id, kernel_map.va_pa_offset);
238 	unreachable();
239 }
240