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