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