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