xref: /linux/arch/arm64/kernel/machine_kexec.c (revision b059e957d07b3cab92eef4656988244043f5769a)
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/arm-rmi-cmds.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/kernel.h>
13 #include <linux/kexec.h>
14 #include <linux/page-flags.h>
15 #include <linux/reboot.h>
16 #include <linux/set_memory.h>
17 #include <linux/smp.h>
18 
19 #include <asm/cacheflush.h>
20 #include <asm/cpu_ops.h>
21 #include <asm/daifflags.h>
22 #include <asm/memory.h>
23 #include <asm/mmu.h>
24 #include <asm/mmu_context.h>
25 #include <asm/page.h>
26 #include <asm/sections.h>
27 #include <asm/trans_pgd.h>
28 
29 /**
30  * kexec_image_info - For debugging output.
31  */
32 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
33 static void _kexec_image_info(const char *func, int line,
34 	const struct kimage *kimage)
35 {
36 	kexec_dprintk("%s:%d:\n", func, line);
37 	kexec_dprintk("  kexec kimage info:\n");
38 	kexec_dprintk("    type:        %d\n", kimage->type);
39 	kexec_dprintk("    head:        %lx\n", kimage->head);
40 	kexec_dprintk("    kern_reloc: %pa\n", &kimage->arch.kern_reloc);
41 	kexec_dprintk("    el2_vectors: %pa\n", &kimage->arch.el2_vectors);
42 }
43 
44 void machine_kexec_cleanup(struct kimage *kimage)
45 {
46 	/* Empty routine needed to avoid build errors. */
47 }
48 
49 /**
50  * machine_kexec_prepare - Prepare for a kexec reboot.
51  *
52  * Called from the core kexec code when a kernel image is loaded.
53  * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
54  * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
55  */
56 int machine_kexec_prepare(struct kimage *kimage)
57 {
58 	if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
59 		pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
60 		return -EBUSY;
61 	}
62 
63 	/*
64 	 * We will be able to allow kdump to proceed, once we have the support
65 	 * for handling GPF from vmcore accesses to delegated pages. Until then
66 	 * block kexec completely.
67 	 */
68 	if (is_rmm_active()) {
69 		pr_err("Can't kexec: RMM is active.\n");
70 		return -EBUSY;
71 	}
72 
73 	return 0;
74 }
75 
76 /**
77  * kexec_segment_flush - Helper to flush the kimage segments to PoC.
78  */
79 static void kexec_segment_flush(const struct kimage *kimage)
80 {
81 	unsigned long i;
82 
83 	pr_debug("%s:\n", __func__);
84 
85 	for (i = 0; i < kimage->nr_segments; i++) {
86 		pr_debug("  segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
87 			i,
88 			kimage->segment[i].mem,
89 			kimage->segment[i].mem + kimage->segment[i].memsz,
90 			kimage->segment[i].memsz,
91 			kimage->segment[i].memsz /  PAGE_SIZE);
92 
93 		dcache_clean_inval_poc(
94 			(unsigned long)phys_to_virt(kimage->segment[i].mem),
95 			(unsigned long)phys_to_virt(kimage->segment[i].mem) +
96 				kimage->segment[i].memsz);
97 	}
98 }
99 
100 /* Allocates pages for kexec page table */
101 static void *kexec_page_alloc(void *arg)
102 {
103 	struct kimage *kimage = arg;
104 	struct page *page = kimage_alloc_control_pages(kimage, 0);
105 	void *vaddr = NULL;
106 
107 	if (!page)
108 		return NULL;
109 
110 	vaddr = page_address(page);
111 	memset(vaddr, 0, PAGE_SIZE);
112 
113 	return vaddr;
114 }
115 
116 int machine_kexec_post_load(struct kimage *kimage)
117 {
118 	int rc;
119 	pgd_t *trans_pgd;
120 	void *reloc_code = page_to_virt(kimage->control_code_page);
121 	long reloc_size;
122 	struct trans_pgd_info info = {
123 		.trans_alloc_page	= kexec_page_alloc,
124 		.trans_alloc_arg	= kimage,
125 	};
126 
127 	/* If in place, relocation is not used, only flush next kernel */
128 	if (kimage->head & IND_DONE) {
129 		kexec_segment_flush(kimage);
130 		kexec_image_info(kimage);
131 		return 0;
132 	}
133 
134 	kimage->arch.el2_vectors = 0;
135 	if (is_hyp_nvhe()) {
136 		rc = trans_pgd_copy_el2_vectors(&info,
137 						&kimage->arch.el2_vectors);
138 		if (rc)
139 			return rc;
140 	}
141 
142 	/* Create a copy of the linear map */
143 	rc = trans_pgd_create_copy(&info, &trans_pgd,
144 				   _PAGE_OFFSET(vabits_actual), PAGE_END);
145 	if (rc)
146 		return rc;
147 	kimage->arch.ttbr1 = __pa(trans_pgd);
148 	kimage->arch.zero_page = __pa_symbol(empty_zero_page);
149 
150 	/* Copy the whole section: a kCFI hash may precede the entry point */
151 	reloc_size = __relocate_new_kernel_end - __relocate_new_kernel_start;
152 	memcpy(reloc_code, __relocate_new_kernel_start, reloc_size);
153 	kimage->arch.kern_reloc = __pa(reloc_code) +
154 		((unsigned long)arm64_relocate_new_kernel -
155 		 (unsigned long)__relocate_new_kernel_start);
156 	rc = trans_pgd_idmap_page(&info, &kimage->arch.ttbr0,
157 				  &kimage->arch.t0sz, reloc_code);
158 	if (rc)
159 		return rc;
160 	kimage->arch.phys_offset = virt_to_phys(kimage) - (long)kimage;
161 
162 	/* Flush the reloc_code in preparation for its execution. */
163 	dcache_clean_inval_poc((unsigned long)reloc_code,
164 			       (unsigned long)reloc_code + reloc_size);
165 	icache_inval_pou((uintptr_t)reloc_code,
166 			 (uintptr_t)reloc_code + reloc_size);
167 	kexec_image_info(kimage);
168 
169 	return 0;
170 }
171 
172 /**
173  * machine_kexec - Do the kexec reboot.
174  *
175  * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
176  */
177 void machine_kexec(struct kimage *kimage)
178 {
179 	bool in_kexec_crash = (kimage == kexec_crash_image);
180 	bool stuck_cpus = cpus_are_stuck_in_kernel();
181 
182 	/*
183 	 * New cpus may have become stuck_in_kernel after we loaded the image.
184 	 */
185 	BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
186 	WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
187 		"Some CPUs may be stale, kdump will be unreliable.\n");
188 
189 	pr_info("Bye!\n");
190 
191 	local_daif_mask();
192 
193 	/*
194 	 * Both restart and kernel_reloc will shutdown the MMU, disable data
195 	 * caches. However, restart will start new kernel or purgatory directly,
196 	 * kernel_reloc contains the body of arm64_relocate_new_kernel
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 	if (kimage->head & IND_DONE) {
203 		typeof(cpu_soft_restart) *restart;
204 
205 		cpu_install_idmap();
206 		restart = (void *)__pa_symbol(cpu_soft_restart);
207 		restart(is_hyp_nvhe(), kimage->start, kimage->arch.dtb_mem,
208 			0, 0);
209 	} else {
210 		typeof(arm64_relocate_new_kernel) *kernel_reloc;
211 
212 		if (is_hyp_nvhe())
213 			__hyp_set_vectors(kimage->arch.el2_vectors);
214 		cpu_install_ttbr0(kimage->arch.ttbr0, kimage->arch.t0sz);
215 		kernel_reloc = (void *)kimage->arch.kern_reloc;
216 		kernel_reloc(kimage);
217 	}
218 
219 	BUG(); /* Should never get here. */
220 }
221 
222 /**
223  * machine_crash_shutdown - shutdown non-crashing cpus and save registers
224  */
225 void machine_crash_shutdown(struct pt_regs *regs)
226 {
227 	local_irq_disable();
228 
229 	/* shutdown non-crashing cpus */
230 	crash_smp_send_stop();
231 
232 	/* for crashing cpu */
233 	crash_save_cpu(regs, smp_processor_id());
234 	machine_kexec_mask_interrupts();
235 
236 	pr_info("Starting crashdump kernel...\n");
237 }
238 
239 #if defined(CONFIG_CRASH_DUMP) && defined(CONFIG_HIBERNATION)
240 /*
241  * To preserve the crash dump kernel image, the relevant memory segments
242  * should be mapped again around the hibernation.
243  */
244 void crash_prepare_suspend(void)
245 {
246 	if (kexec_crash_image)
247 		arch_kexec_unprotect_crashkres();
248 }
249 
250 void crash_post_resume(void)
251 {
252 	if (kexec_crash_image)
253 		arch_kexec_protect_crashkres();
254 }
255 
256 /*
257  * crash_is_nosave
258  *
259  * Return true only if a page is part of reserved memory for crash dump kernel,
260  * but does not hold any data of loaded kernel image.
261  *
262  * Note that all the pages in crash dump kernel memory have been initially
263  * marked as Reserved as memory was allocated via memblock_reserve().
264  *
265  * In hibernation, the pages which are Reserved and yet "nosave" are excluded
266  * from the hibernation image. crash_is_nosave() does thich check for crash
267  * dump kernel and will reduce the total size of hibernation image.
268  */
269 
270 bool crash_is_nosave(unsigned long pfn)
271 {
272 	int i;
273 	phys_addr_t addr;
274 
275 	if (!crashk_res.end)
276 		return false;
277 
278 	/* in reserved memory? */
279 	addr = __pfn_to_phys(pfn);
280 	if ((addr < crashk_res.start) || (crashk_res.end < addr)) {
281 		if (!crashk_low_res.end)
282 			return false;
283 
284 		if ((addr < crashk_low_res.start) || (crashk_low_res.end < addr))
285 			return false;
286 	}
287 
288 	if (!kexec_crash_image)
289 		return true;
290 
291 	/* not part of loaded kernel image? */
292 	for (i = 0; i < kexec_crash_image->nr_segments; i++)
293 		if (addr >= kexec_crash_image->segment[i].mem &&
294 				addr < (kexec_crash_image->segment[i].mem +
295 					kexec_crash_image->segment[i].memsz))
296 			return false;
297 
298 	return true;
299 }
300 
301 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
302 {
303 	unsigned long addr;
304 	struct page *page;
305 
306 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
307 		page = phys_to_page(addr);
308 		free_reserved_page(page);
309 	}
310 }
311 #endif /* CONFIG_HIBERNATION */
312