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