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