|
| 1 | +/* |
| 2 | + * kexec for arm64 |
| 3 | + * |
| 4 | + * Copyright (C) Linaro. |
| 5 | + * Copyright (C) Huawei Futurewei Technologies. |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License version 2 as |
| 9 | + * published by the Free Software Foundation. |
| 10 | + */ |
| 11 | + |
| 12 | +#include <linux/kexec.h> |
| 13 | +#include <linux/smp.h> |
| 14 | + |
| 15 | +#include <asm/cacheflush.h> |
| 16 | +#include <asm/cpu_ops.h> |
| 17 | +#include <asm/mmu_context.h> |
| 18 | + |
| 19 | +#include "cpu-reset.h" |
| 20 | + |
| 21 | +/* Global variables for the arm64_relocate_new_kernel routine. */ |
| 22 | +extern const unsigned char arm64_relocate_new_kernel[]; |
| 23 | +extern const unsigned long arm64_relocate_new_kernel_size; |
| 24 | + |
| 25 | +static unsigned long kimage_start; |
| 26 | + |
| 27 | +void machine_kexec_cleanup(struct kimage *kimage) |
| 28 | +{ |
| 29 | + /* Empty routine needed to avoid build errors. */ |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * machine_kexec_prepare - Prepare for a kexec reboot. |
| 34 | + * |
| 35 | + * Called from the core kexec code when a kernel image is loaded. |
| 36 | + * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus |
| 37 | + * are stuck in the kernel. This avoids a panic once we hit machine_kexec(). |
| 38 | + */ |
| 39 | +int machine_kexec_prepare(struct kimage *kimage) |
| 40 | +{ |
| 41 | + kimage_start = kimage->start; |
| 42 | + |
| 43 | + if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) { |
| 44 | + pr_err("Can't kexec: CPUs are stuck in the kernel.\n"); |
| 45 | + return -EBUSY; |
| 46 | + } |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * kexec_list_flush - Helper to flush the kimage list and source pages to PoC. |
| 53 | + */ |
| 54 | +static void kexec_list_flush(struct kimage *kimage) |
| 55 | +{ |
| 56 | + kimage_entry_t *entry; |
| 57 | + |
| 58 | + for (entry = &kimage->head; ; entry++) { |
| 59 | + unsigned int flag; |
| 60 | + void *addr; |
| 61 | + |
| 62 | + /* flush the list entries. */ |
| 63 | + __flush_dcache_area(entry, sizeof(kimage_entry_t)); |
| 64 | + |
| 65 | + flag = *entry & IND_FLAGS; |
| 66 | + if (flag == IND_DONE) |
| 67 | + break; |
| 68 | + |
| 69 | + addr = phys_to_virt(*entry & PAGE_MASK); |
| 70 | + |
| 71 | + switch (flag) { |
| 72 | + case IND_INDIRECTION: |
| 73 | + /* Set entry point just before the new list page. */ |
| 74 | + entry = (kimage_entry_t *)addr - 1; |
| 75 | + break; |
| 76 | + case IND_SOURCE: |
| 77 | + /* flush the source pages. */ |
| 78 | + __flush_dcache_area(addr, PAGE_SIZE); |
| 79 | + break; |
| 80 | + case IND_DESTINATION: |
| 81 | + break; |
| 82 | + default: |
| 83 | + BUG(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * kexec_segment_flush - Helper to flush the kimage segments to PoC. |
| 90 | + */ |
| 91 | +static void kexec_segment_flush(const struct kimage *kimage) |
| 92 | +{ |
| 93 | + unsigned long i; |
| 94 | + |
| 95 | + pr_debug("%s:\n", __func__); |
| 96 | + |
| 97 | + for (i = 0; i < kimage->nr_segments; i++) { |
| 98 | + pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n", |
| 99 | + i, |
| 100 | + kimage->segment[i].mem, |
| 101 | + kimage->segment[i].mem + kimage->segment[i].memsz, |
| 102 | + kimage->segment[i].memsz, |
| 103 | + kimage->segment[i].memsz / PAGE_SIZE); |
| 104 | + |
| 105 | + __flush_dcache_area(phys_to_virt(kimage->segment[i].mem), |
| 106 | + kimage->segment[i].memsz); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * machine_kexec - Do the kexec reboot. |
| 112 | + * |
| 113 | + * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC. |
| 114 | + */ |
| 115 | +void machine_kexec(struct kimage *kimage) |
| 116 | +{ |
| 117 | + phys_addr_t reboot_code_buffer_phys; |
| 118 | + void *reboot_code_buffer; |
| 119 | + |
| 120 | + /* |
| 121 | + * New cpus may have become stuck_in_kernel after we loaded the image. |
| 122 | + */ |
| 123 | + BUG_ON(cpus_are_stuck_in_kernel() || (num_online_cpus() > 1)); |
| 124 | + |
| 125 | + reboot_code_buffer_phys = page_to_phys(kimage->control_code_page); |
| 126 | + reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys); |
| 127 | + |
| 128 | + /* |
| 129 | + * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use |
| 130 | + * after the kernel is shut down. |
| 131 | + */ |
| 132 | + memcpy(reboot_code_buffer, arm64_relocate_new_kernel, |
| 133 | + arm64_relocate_new_kernel_size); |
| 134 | + |
| 135 | + /* Flush the reboot_code_buffer in preparation for its execution. */ |
| 136 | + __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size); |
| 137 | + flush_icache_range((uintptr_t)reboot_code_buffer, |
| 138 | + arm64_relocate_new_kernel_size); |
| 139 | + |
| 140 | + /* Flush the kimage list and its buffers. */ |
| 141 | + kexec_list_flush(kimage); |
| 142 | + |
| 143 | + /* Flush the new image if already in place. */ |
| 144 | + if (kimage->head & IND_DONE) |
| 145 | + kexec_segment_flush(kimage); |
| 146 | + |
| 147 | + pr_info("Bye!\n"); |
| 148 | + |
| 149 | + /* Disable all DAIF exceptions. */ |
| 150 | + asm volatile ("msr daifset, #0xf" : : : "memory"); |
| 151 | + |
| 152 | + /* |
| 153 | + * cpu_soft_restart will shutdown the MMU, disable data caches, then |
| 154 | + * transfer control to the reboot_code_buffer which contains a copy of |
| 155 | + * the arm64_relocate_new_kernel routine. arm64_relocate_new_kernel |
| 156 | + * uses physical addressing to relocate the new image to its final |
| 157 | + * position and transfers control to the image entry point when the |
| 158 | + * relocation is complete. |
| 159 | + */ |
| 160 | + |
| 161 | + cpu_soft_restart(1, reboot_code_buffer_phys, kimage->head, |
| 162 | + kimage_start, 0); |
| 163 | + |
| 164 | + BUG(); /* Should never get here. */ |
| 165 | +} |
| 166 | + |
| 167 | +void machine_crash_shutdown(struct pt_regs *regs) |
| 168 | +{ |
| 169 | + /* Empty routine needed to avoid build errors. */ |
| 170 | +} |
0 commit comments