1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kexec for arm64
4  *
5  * Copyright (C) Linaro.
6  * Copyright (C) Huawei Futurewei Technologies.
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/kernel.h>
12 #include <linux/kexec.h>
13 #include <linux/page-flags.h>
14 #include <linux/smp.h>
15 
16 #include <asm/cacheflush.h>
17 #include <asm/cpu_ops.h>
18 #include <asm/daifflags.h>
19 #include <asm/memory.h>
20 #include <asm/mmu.h>
21 #include <asm/mmu_context.h>
22 #include <asm/page.h>
23 
24 #include "cpu-reset.h"
25 
26 /* Global variables for the arm64_relocate_new_kernel routine. */
27 extern const unsigned char arm64_relocate_new_kernel[];
28 extern const unsigned long arm64_relocate_new_kernel_size;
29 
30 /**
31  * kexec_image_info - For debugging output.
32  */
33 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
34 static void _kexec_image_info(const char *func, int line,
35 	const struct kimage *kimage)
36 {
37 	unsigned long i;
38 
39 	pr_debug("%s:%d:\n", func, line);
40 	pr_debug("  kexec kimage info:\n");
41 	pr_debug("    type:        %d\n", kimage->type);
42 	pr_debug("    start:       %lx\n", kimage->start);
43 	pr_debug("    head:        %lx\n", kimage->head);
44 	pr_debug("    nr_segments: %lu\n", kimage->nr_segments);
45 	pr_debug("    kern_reloc: %pa\n", &kimage->arch.kern_reloc);
46 
47 	for (i = 0; i < kimage->nr_segments; i++) {
48 		pr_debug("      segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
49 			i,
50 			kimage->segment[i].mem,
51 			kimage->segment[i].mem + kimage->segment[i].memsz,
52 			kimage->segment[i].memsz,
53 			kimage->segment[i].memsz /  PAGE_SIZE);
54 	}
55 }
56 
57 void machine_kexec_cleanup(struct kimage *kimage)
58 {
59 	/* Empty routine needed to avoid build errors. */
60 }
61 
62 int machine_kexec_post_load(struct kimage *kimage)
63 {
64 	void *reloc_code = page_to_virt(kimage->control_code_page);
65 
66 	memcpy(reloc_code, arm64_relocate_new_kernel,
67 	       arm64_relocate_new_kernel_size);
68 	kimage->arch.kern_reloc = __pa(reloc_code);
69 	kexec_image_info(kimage);
70 
71 	/*
72 	 * For execution with the MMU off, reloc_code needs to be cleaned to the
73 	 * PoC and invalidated from the I-cache.
74 	 */
75 	__flush_dcache_area(reloc_code, arm64_relocate_new_kernel_size);
76 	invalidate_icache_range((uintptr_t)reloc_code,
77 				(uintptr_t)reloc_code +
78 					arm64_relocate_new_kernel_size);
79 
80 	return 0;
81 }
82 
83 /**
84  * machine_kexec_prepare - Prepare for a kexec reboot.
85  *
86  * Called from the core kexec code when a kernel image is loaded.
87  * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
88  * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
89  */
90 int machine_kexec_prepare(struct kimage *kimage)
91 {
92 	if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
93 		pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
94 		return -EBUSY;
95 	}
96 
97 	return 0;
98 }
99 
100 /**
101  * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
102  */
103 static void kexec_list_flush(struct kimage *kimage)
104 {
105 	kimage_entry_t *entry;
106 
107 	for (entry = &kimage->head; ; entry++) {
108 		unsigned int flag;
109 		void *addr;
110 
111 		/* flush the list entries. */
112 		__flush_dcache_area(entry, sizeof(kimage_entry_t));
113 
114 		flag = *entry & IND_FLAGS;
115 		if (flag == IND_DONE)
116 			break;
117 
118 		addr = phys_to_virt(*entry & PAGE_MASK);
119 
120 		switch (flag) {
121 		case IND_INDIRECTION:
122 			/* Set entry point just before the new list page. */
123 			entry = (kimage_entry_t *)addr - 1;
124 			break;
125 		case IND_SOURCE:
126 			/* flush the source pages. */
127 			__flush_dcache_area(addr, PAGE_SIZE);
128 			break;
129 		case IND_DESTINATION:
130 			break;
131 		default:
132 			BUG();
133 		}
134 	}
135 }
136 
137 /**
138  * kexec_segment_flush - Helper to flush the kimage segments to PoC.
139  */
140 static void kexec_segment_flush(const struct kimage *kimage)
141 {
142 	unsigned long i;
143 
144 	pr_debug("%s:\n", __func__);
145 
146 	for (i = 0; i < kimage->nr_segments; i++) {
147 		pr_debug("  segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
148 			i,
149 			kimage->segment[i].mem,
150 			kimage->segment[i].mem + kimage->segment[i].memsz,
151 			kimage->segment[i].memsz,
152 			kimage->segment[i].memsz /  PAGE_SIZE);
153 
154 		__flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
155 			kimage->segment[i].memsz);
156 	}
157 }
158 
159 /**
160  * machine_kexec - Do the kexec reboot.
161  *
162  * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
163  */
164 void machine_kexec(struct kimage *kimage)
165 {
166 	bool in_kexec_crash = (kimage == kexec_crash_image);
167 	bool stuck_cpus = cpus_are_stuck_in_kernel();
168 
169 	/*
170 	 * New cpus may have become stuck_in_kernel after we loaded the image.
171 	 */
172 	BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
173 	WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
174 		"Some CPUs may be stale, kdump will be unreliable.\n");
175 
176 	/* Flush the kimage list and its buffers. */
177 	kexec_list_flush(kimage);
178 
179 	/* Flush the new image if already in place. */
180 	if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
181 		kexec_segment_flush(kimage);
182 
183 	pr_info("Bye!\n");
184 
185 	local_daif_mask();
186 
187 	/*
188 	 * cpu_soft_restart will shutdown the MMU, disable data caches, then
189 	 * transfer control to the kern_reloc which contains a copy of
190 	 * the arm64_relocate_new_kernel routine.  arm64_relocate_new_kernel
191 	 * uses physical addressing to relocate the new image to its final
192 	 * position and transfers control to the image entry point when the
193 	 * relocation is complete.
194 	 * In kexec case, kimage->start points to purgatory assuming that
195 	 * kernel entry and dtb address are embedded in purgatory by
196 	 * userspace (kexec-tools).
197 	 * In kexec_file case, the kernel starts directly without purgatory.
198 	 */
199 	cpu_soft_restart(kimage->arch.kern_reloc, kimage->head, kimage->start,
200 			 kimage->arch.dtb_mem);
201 
202 	BUG(); /* Should never get here. */
203 }
204 
205 static void machine_kexec_mask_interrupts(void)
206 {
207 	unsigned int i;
208 	struct irq_desc *desc;
209 
210 	for_each_irq_desc(i, desc) {
211 		struct irq_chip *chip;
212 		int ret;
213 
214 		chip = irq_desc_get_chip(desc);
215 		if (!chip)
216 			continue;
217 
218 		/*
219 		 * First try to remove the active state. If this
220 		 * fails, try to EOI the interrupt.
221 		 */
222 		ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
223 
224 		if (ret && irqd_irq_inprogress(&desc->irq_data) &&
225 		    chip->irq_eoi)
226 			chip->irq_eoi(&desc->irq_data);
227 
228 		if (chip->irq_mask)
229 			chip->irq_mask(&desc->irq_data);
230 
231 		if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
232 			chip->irq_disable(&desc->irq_data);
233 	}
234 }
235 
236 /**
237  * machine_crash_shutdown - shutdown non-crashing cpus and save registers
238  */
239 void machine_crash_shutdown(struct pt_regs *regs)
240 {
241 	local_irq_disable();
242 
243 	/* shutdown non-crashing cpus */
244 	crash_smp_send_stop();
245 
246 	/* for crashing cpu */
247 	crash_save_cpu(regs, smp_processor_id());
248 	machine_kexec_mask_interrupts();
249 
250 	pr_info("Starting crashdump kernel...\n");
251 }
252 
253 void arch_kexec_protect_crashkres(void)
254 {
255 	int i;
256 
257 	kexec_segment_flush(kexec_crash_image);
258 
259 	for (i = 0; i < kexec_crash_image->nr_segments; i++)
260 		set_memory_valid(
261 			__phys_to_virt(kexec_crash_image->segment[i].mem),
262 			kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
263 }
264 
265 void arch_kexec_unprotect_crashkres(void)
266 {
267 	int i;
268 
269 	for (i = 0; i < kexec_crash_image->nr_segments; i++)
270 		set_memory_valid(
271 			__phys_to_virt(kexec_crash_image->segment[i].mem),
272 			kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
273 }
274 
275 #ifdef CONFIG_HIBERNATION
276 /*
277  * To preserve the crash dump kernel image, the relevant memory segments
278  * should be mapped again around the hibernation.
279  */
280 void crash_prepare_suspend(void)
281 {
282 	if (kexec_crash_image)
283 		arch_kexec_unprotect_crashkres();
284 }
285 
286 void crash_post_resume(void)
287 {
288 	if (kexec_crash_image)
289 		arch_kexec_protect_crashkres();
290 }
291 
292 /*
293  * crash_is_nosave
294  *
295  * Return true only if a page is part of reserved memory for crash dump kernel,
296  * but does not hold any data of loaded kernel image.
297  *
298  * Note that all the pages in crash dump kernel memory have been initially
299  * marked as Reserved as memory was allocated via memblock_reserve().
300  *
301  * In hibernation, the pages which are Reserved and yet "nosave" are excluded
302  * from the hibernation iamge. crash_is_nosave() does thich check for crash
303  * dump kernel and will reduce the total size of hibernation image.
304  */
305 
306 bool crash_is_nosave(unsigned long pfn)
307 {
308 	int i;
309 	phys_addr_t addr;
310 
311 	if (!crashk_res.end)
312 		return false;
313 
314 	/* in reserved memory? */
315 	addr = __pfn_to_phys(pfn);
316 	if ((addr < crashk_res.start) || (crashk_res.end < addr))
317 		return false;
318 
319 	if (!kexec_crash_image)
320 		return true;
321 
322 	/* not part of loaded kernel image? */
323 	for (i = 0; i < kexec_crash_image->nr_segments; i++)
324 		if (addr >= kexec_crash_image->segment[i].mem &&
325 				addr < (kexec_crash_image->segment[i].mem +
326 					kexec_crash_image->segment[i].memsz))
327 			return false;
328 
329 	return true;
330 }
331 
332 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
333 {
334 	unsigned long addr;
335 	struct page *page;
336 
337 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
338 		page = phys_to_page(addr);
339 		free_reserved_page(page);
340 	}
341 }
342 #endif /* CONFIG_HIBERNATION */
343