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