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)
_kexec_image_info(const char * func,int line,const struct kimage * kimage)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
machine_kexec_cleanup(struct kimage * kimage)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 */
machine_kexec_prepare(struct kimage * kimage)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 */
kexec_segment_flush(const struct kimage * kimage)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 */
kexec_page_alloc(void * arg)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
machine_kexec_post_load(struct kimage * kimage)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 reloc_size = __relocate_new_kernel_end - __relocate_new_kernel_start;
151 memcpy(reloc_code, __relocate_new_kernel_start, reloc_size);
152 kimage->arch.kern_reloc = __pa(reloc_code);
153 rc = trans_pgd_idmap_page(&info, &kimage->arch.ttbr0,
154 &kimage->arch.t0sz, reloc_code);
155 if (rc)
156 return rc;
157 kimage->arch.phys_offset = virt_to_phys(kimage) - (long)kimage;
158
159 /* Flush the reloc_code in preparation for its execution. */
160 dcache_clean_inval_poc((unsigned long)reloc_code,
161 (unsigned long)reloc_code + reloc_size);
162 icache_inval_pou((uintptr_t)reloc_code,
163 (uintptr_t)reloc_code + reloc_size);
164 kexec_image_info(kimage);
165
166 return 0;
167 }
168
169 /**
170 * machine_kexec - Do the kexec reboot.
171 *
172 * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
173 */
machine_kexec(struct kimage * kimage)174 void machine_kexec(struct kimage *kimage)
175 {
176 bool in_kexec_crash = (kimage == kexec_crash_image);
177 bool stuck_cpus = cpus_are_stuck_in_kernel();
178
179 /*
180 * New cpus may have become stuck_in_kernel after we loaded the image.
181 */
182 BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
183 WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
184 "Some CPUs may be stale, kdump will be unreliable.\n");
185
186 pr_info("Bye!\n");
187
188 local_daif_mask();
189
190 /*
191 * Both restart and kernel_reloc will shutdown the MMU, disable data
192 * caches. However, restart will start new kernel or purgatory directly,
193 * kernel_reloc contains the body of arm64_relocate_new_kernel
194 * In kexec case, kimage->start points to purgatory assuming that
195 * kernel entry and dtb address are embedded in purgatory by
196 * userspace (kexec-tools).
197 * In kexec_file case, the kernel starts directly without purgatory.
198 */
199 if (kimage->head & IND_DONE) {
200 typeof(cpu_soft_restart) *restart;
201
202 cpu_install_idmap();
203 restart = (void *)__pa_symbol(cpu_soft_restart);
204 restart(is_hyp_nvhe(), kimage->start, kimage->arch.dtb_mem,
205 0, 0);
206 } else {
207 void (*kernel_reloc)(struct kimage *kimage);
208
209 if (is_hyp_nvhe())
210 __hyp_set_vectors(kimage->arch.el2_vectors);
211 cpu_install_ttbr0(kimage->arch.ttbr0, kimage->arch.t0sz);
212 kernel_reloc = (void *)kimage->arch.kern_reloc;
213 kernel_reloc(kimage);
214 }
215
216 BUG(); /* Should never get here. */
217 }
218
219 /**
220 * machine_crash_shutdown - shutdown non-crashing cpus and save registers
221 */
machine_crash_shutdown(struct pt_regs * regs)222 void machine_crash_shutdown(struct pt_regs *regs)
223 {
224 local_irq_disable();
225
226 /* shutdown non-crashing cpus */
227 crash_smp_send_stop();
228
229 /* for crashing cpu */
230 crash_save_cpu(regs, smp_processor_id());
231 machine_kexec_mask_interrupts();
232
233 pr_info("Starting crashdump kernel...\n");
234 }
235
236 #if defined(CONFIG_CRASH_DUMP) && defined(CONFIG_HIBERNATION)
237 /*
238 * To preserve the crash dump kernel image, the relevant memory segments
239 * should be mapped again around the hibernation.
240 */
crash_prepare_suspend(void)241 void crash_prepare_suspend(void)
242 {
243 if (kexec_crash_image)
244 arch_kexec_unprotect_crashkres();
245 }
246
crash_post_resume(void)247 void crash_post_resume(void)
248 {
249 if (kexec_crash_image)
250 arch_kexec_protect_crashkres();
251 }
252
253 /*
254 * crash_is_nosave
255 *
256 * Return true only if a page is part of reserved memory for crash dump kernel,
257 * but does not hold any data of loaded kernel image.
258 *
259 * Note that all the pages in crash dump kernel memory have been initially
260 * marked as Reserved as memory was allocated via memblock_reserve().
261 *
262 * In hibernation, the pages which are Reserved and yet "nosave" are excluded
263 * from the hibernation image. crash_is_nosave() does thich check for crash
264 * dump kernel and will reduce the total size of hibernation image.
265 */
266
crash_is_nosave(unsigned long pfn)267 bool crash_is_nosave(unsigned long pfn)
268 {
269 int i;
270 phys_addr_t addr;
271
272 if (!crashk_res.end)
273 return false;
274
275 /* in reserved memory? */
276 addr = __pfn_to_phys(pfn);
277 if ((addr < crashk_res.start) || (crashk_res.end < addr)) {
278 if (!crashk_low_res.end)
279 return false;
280
281 if ((addr < crashk_low_res.start) || (crashk_low_res.end < addr))
282 return false;
283 }
284
285 if (!kexec_crash_image)
286 return true;
287
288 /* not part of loaded kernel image? */
289 for (i = 0; i < kexec_crash_image->nr_segments; i++)
290 if (addr >= kexec_crash_image->segment[i].mem &&
291 addr < (kexec_crash_image->segment[i].mem +
292 kexec_crash_image->segment[i].memsz))
293 return false;
294
295 return true;
296 }
297
crash_free_reserved_phys_range(unsigned long begin,unsigned long end)298 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
299 {
300 unsigned long addr;
301 struct page *page;
302
303 for (addr = begin; addr < end; addr += PAGE_SIZE) {
304 page = phys_to_page(addr);
305 free_reserved_page(page);
306 }
307 }
308 #endif /* CONFIG_HIBERNATION */
309