b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * machine_kexec.c for kexec |
| 4 | * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006 |
| 5 | */ |
| 6 | #include <linux/compiler.h> |
| 7 | #include <linux/kexec.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/libfdt.h> |
| 11 | |
| 12 | #include <asm/bootinfo.h> |
| 13 | #include <asm/cacheflush.h> |
| 14 | #include <asm/page.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include "machine_kexec.h" |
| 17 | |
| 18 | static unsigned long reboot_code_buffer; |
| 19 | |
| 20 | #ifdef CONFIG_SMP |
| 21 | static void (*relocated_kexec_smp_wait)(void *); |
| 22 | |
| 23 | atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0); |
| 24 | void (*_crash_smp_send_stop)(void) = NULL; |
| 25 | #endif |
| 26 | |
| 27 | void (*_machine_kexec_shutdown)(void) = NULL; |
| 28 | void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL; |
| 29 | |
| 30 | static void machine_kexec_print_args(void) |
| 31 | { |
| 32 | unsigned long argc = (int)kexec_args[0]; |
| 33 | int i; |
| 34 | |
| 35 | pr_info("kexec_args[0] (argc): %lu\n", argc); |
| 36 | pr_info("kexec_args[1] (argv): %p\n", (void *)kexec_args[1]); |
| 37 | pr_info("kexec_args[2] (env ): %p\n", (void *)kexec_args[2]); |
| 38 | pr_info("kexec_args[3] (desc): %p\n", (void *)kexec_args[3]); |
| 39 | |
| 40 | for (i = 0; i < argc; i++) { |
| 41 | pr_info("kexec_argv[%d] = %p, %s\n", |
| 42 | i, kexec_argv[i], kexec_argv[i]); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static void machine_kexec_init_argv(struct kimage *image) |
| 47 | { |
| 48 | void __user *buf = NULL; |
| 49 | size_t bufsz; |
| 50 | size_t size; |
| 51 | int i; |
| 52 | |
| 53 | bufsz = 0; |
| 54 | for (i = 0; i < image->nr_segments; i++) { |
| 55 | struct kexec_segment *seg; |
| 56 | |
| 57 | seg = &image->segment[i]; |
| 58 | if (seg->bufsz < 6) |
| 59 | continue; |
| 60 | |
| 61 | if (strncmp((char *) seg->buf, "kexec ", 6)) |
| 62 | continue; |
| 63 | |
| 64 | buf = seg->buf; |
| 65 | bufsz = seg->bufsz; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | if (!buf) |
| 70 | return; |
| 71 | |
| 72 | size = KEXEC_COMMAND_LINE_SIZE; |
| 73 | size = min(size, bufsz); |
| 74 | if (size < bufsz) |
| 75 | pr_warn("kexec command line truncated to %zd bytes\n", size); |
| 76 | |
| 77 | /* Copy to kernel space */ |
| 78 | if (copy_from_user(kexec_argv_buf, buf, size)) |
| 79 | pr_warn("kexec command line copy to kernel space failed\n"); |
| 80 | |
| 81 | kexec_argv_buf[size - 1] = 0; |
| 82 | } |
| 83 | |
| 84 | static void machine_kexec_parse_argv(struct kimage *image) |
| 85 | { |
| 86 | char *reboot_code_buffer; |
| 87 | int reloc_delta; |
| 88 | char *ptr; |
| 89 | int argc; |
| 90 | int i; |
| 91 | |
| 92 | ptr = kexec_argv_buf; |
| 93 | argc = 0; |
| 94 | |
| 95 | /* |
| 96 | * convert command line string to array of parameters |
| 97 | * (as bootloader does). |
| 98 | */ |
| 99 | while (ptr && *ptr && (KEXEC_MAX_ARGC > argc)) { |
| 100 | if (*ptr == ' ') { |
| 101 | *ptr++ = '\0'; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | kexec_argv[argc++] = ptr; |
| 106 | ptr = strchr(ptr, ' '); |
| 107 | } |
| 108 | |
| 109 | if (!argc) |
| 110 | return; |
| 111 | |
| 112 | kexec_args[0] = argc; |
| 113 | kexec_args[1] = (unsigned long)kexec_argv; |
| 114 | kexec_args[2] = 0; |
| 115 | kexec_args[3] = 0; |
| 116 | |
| 117 | reboot_code_buffer = page_address(image->control_code_page); |
| 118 | reloc_delta = reboot_code_buffer - (char *)kexec_relocate_new_kernel; |
| 119 | |
| 120 | kexec_args[1] += reloc_delta; |
| 121 | for (i = 0; i < argc; i++) |
| 122 | kexec_argv[i] += reloc_delta; |
| 123 | } |
| 124 | |
| 125 | static void kexec_image_info(const struct kimage *kimage) |
| 126 | { |
| 127 | unsigned long i; |
| 128 | |
| 129 | pr_debug("kexec kimage info:\n"); |
| 130 | pr_debug(" type: %d\n", kimage->type); |
| 131 | pr_debug(" start: %lx\n", kimage->start); |
| 132 | pr_debug(" head: %lx\n", kimage->head); |
| 133 | pr_debug(" nr_segments: %lu\n", kimage->nr_segments); |
| 134 | |
| 135 | for (i = 0; i < kimage->nr_segments; i++) { |
| 136 | pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n", |
| 137 | i, |
| 138 | kimage->segment[i].mem, |
| 139 | kimage->segment[i].mem + kimage->segment[i].memsz, |
| 140 | (unsigned long)kimage->segment[i].memsz, |
| 141 | (unsigned long)kimage->segment[i].memsz / PAGE_SIZE); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #ifdef CONFIG_UHI_BOOT |
| 146 | |
| 147 | static int uhi_machine_kexec_prepare(struct kimage *kimage) |
| 148 | { |
| 149 | int i; |
| 150 | |
| 151 | /* |
| 152 | * In case DTB file is not passed to the new kernel, a flat device |
| 153 | * tree will be created by kexec tool. It holds modified command |
| 154 | * line for the new kernel. |
| 155 | */ |
| 156 | for (i = 0; i < kimage->nr_segments; i++) { |
| 157 | struct fdt_header fdt; |
| 158 | |
| 159 | if (kimage->segment[i].memsz <= sizeof(fdt)) |
| 160 | continue; |
| 161 | |
| 162 | if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt))) |
| 163 | continue; |
| 164 | |
| 165 | if (fdt_check_header(&fdt)) |
| 166 | continue; |
| 167 | |
| 168 | kexec_args[0] = -2; |
| 169 | kexec_args[1] = (unsigned long) |
| 170 | phys_to_virt((unsigned long)kimage->segment[i].mem); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare; |
| 178 | |
| 179 | #else |
| 180 | |
| 181 | int (*_machine_kexec_prepare)(struct kimage *) = NULL; |
| 182 | |
| 183 | #endif /* CONFIG_UHI_BOOT */ |
| 184 | |
| 185 | int |
| 186 | machine_kexec_prepare(struct kimage *kimage) |
| 187 | { |
| 188 | #ifdef CONFIG_SMP |
| 189 | if (!kexec_nonboot_cpu_func()) |
| 190 | return -EINVAL; |
| 191 | #endif |
| 192 | |
| 193 | kexec_image_info(kimage); |
| 194 | /* |
| 195 | * Whenever arguments passed from kexec-tools, Init the arguments as |
| 196 | * the original ones to try avoiding booting failure. |
| 197 | */ |
| 198 | |
| 199 | kexec_args[0] = fw_arg0; |
| 200 | kexec_args[1] = fw_arg1; |
| 201 | kexec_args[2] = fw_arg2; |
| 202 | kexec_args[3] = fw_arg3; |
| 203 | |
| 204 | machine_kexec_init_argv(kimage); |
| 205 | machine_kexec_parse_argv(kimage); |
| 206 | |
| 207 | if (_machine_kexec_prepare) |
| 208 | return _machine_kexec_prepare(kimage); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | machine_kexec_cleanup(struct kimage *kimage) |
| 215 | { |
| 216 | } |
| 217 | |
| 218 | #ifdef CONFIG_SMP |
| 219 | static void kexec_shutdown_secondary(void *param) |
| 220 | { |
| 221 | int cpu = smp_processor_id(); |
| 222 | |
| 223 | if (!cpu_online(cpu)) |
| 224 | return; |
| 225 | |
| 226 | /* We won't be sent IPIs any more. */ |
| 227 | set_cpu_online(cpu, false); |
| 228 | |
| 229 | local_irq_disable(); |
| 230 | while (!atomic_read(&kexec_ready_to_reboot)) |
| 231 | cpu_relax(); |
| 232 | |
| 233 | kexec_reboot(); |
| 234 | |
| 235 | /* NOTREACHED */ |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | void |
| 240 | machine_shutdown(void) |
| 241 | { |
| 242 | if (_machine_kexec_shutdown) |
| 243 | _machine_kexec_shutdown(); |
| 244 | |
| 245 | #ifdef CONFIG_SMP |
| 246 | smp_call_function(kexec_shutdown_secondary, NULL, 0); |
| 247 | |
| 248 | while (num_online_cpus() > 1) { |
| 249 | cpu_relax(); |
| 250 | mdelay(1); |
| 251 | } |
| 252 | #endif |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | machine_crash_shutdown(struct pt_regs *regs) |
| 257 | { |
| 258 | if (_machine_crash_shutdown) |
| 259 | _machine_crash_shutdown(regs); |
| 260 | else |
| 261 | default_machine_crash_shutdown(regs); |
| 262 | } |
| 263 | |
| 264 | #ifdef CONFIG_SMP |
| 265 | void kexec_nonboot_cpu_jump(void) |
| 266 | { |
| 267 | local_flush_icache_range((unsigned long)relocated_kexec_smp_wait, |
| 268 | reboot_code_buffer + KEXEC_RELOCATE_NEW_KERNEL_SIZE); |
| 269 | |
| 270 | relocated_kexec_smp_wait(NULL); |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | void kexec_reboot(void) |
| 275 | { |
| 276 | void (*do_kexec)(void) __noreturn; |
| 277 | |
| 278 | /* |
| 279 | * We know we were online, and there will be no incoming IPIs at |
| 280 | * this point. Mark online again before rebooting so that the crash |
| 281 | * analysis tool will see us correctly. |
| 282 | */ |
| 283 | set_cpu_online(smp_processor_id(), true); |
| 284 | |
| 285 | /* Ensure remote CPUs observe that we're online before rebooting. */ |
| 286 | smp_mb__after_atomic(); |
| 287 | |
| 288 | #ifdef CONFIG_SMP |
| 289 | if (smp_processor_id() > 0) { |
| 290 | /* |
| 291 | * Instead of cpu_relax() or wait, this is needed for kexec |
| 292 | * smp reboot. Kdump usually doesn't require an smp new |
| 293 | * kernel, but kexec may do. |
| 294 | */ |
| 295 | kexec_nonboot_cpu(); |
| 296 | |
| 297 | /* NOTREACHED */ |
| 298 | } |
| 299 | #endif |
| 300 | |
| 301 | /* |
| 302 | * Make sure we get correct instructions written by the |
| 303 | * machine_kexec() CPU. |
| 304 | */ |
| 305 | local_flush_icache_range(reboot_code_buffer, |
| 306 | reboot_code_buffer + KEXEC_RELOCATE_NEW_KERNEL_SIZE); |
| 307 | |
| 308 | do_kexec = (void *)reboot_code_buffer; |
| 309 | do_kexec(); |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | machine_kexec(struct kimage *image) |
| 314 | { |
| 315 | unsigned long entry; |
| 316 | unsigned long *ptr; |
| 317 | |
| 318 | reboot_code_buffer = |
| 319 | (unsigned long)page_address(image->control_code_page); |
| 320 | pr_info("reboot_code_buffer = %p\n", (void *)reboot_code_buffer); |
| 321 | |
| 322 | kexec_start_address = |
| 323 | (unsigned long) phys_to_virt(image->start); |
| 324 | pr_info("kexec_start_address = %p\n", (void *)kexec_start_address); |
| 325 | |
| 326 | if (image->type == KEXEC_TYPE_DEFAULT) { |
| 327 | kexec_indirection_page = |
| 328 | (unsigned long) phys_to_virt(image->head & PAGE_MASK); |
| 329 | } else { |
| 330 | kexec_indirection_page = (unsigned long)&image->head; |
| 331 | } |
| 332 | pr_info("kexec_indirection_page = %p\n", (void *)kexec_indirection_page); |
| 333 | |
| 334 | pr_info("Where is memcpy: %p\n", memcpy); |
| 335 | pr_info("kexec_relocate_new_kernel = %p, kexec_relocate_new_kernel_end = %p\n", |
| 336 | (void *)kexec_relocate_new_kernel, &kexec_relocate_new_kernel_end); |
| 337 | pr_info("Copy %lu bytes from %p to %p\n", KEXEC_RELOCATE_NEW_KERNEL_SIZE, |
| 338 | (void *)kexec_relocate_new_kernel, (void *)reboot_code_buffer); |
| 339 | memcpy((void*)reboot_code_buffer, kexec_relocate_new_kernel, |
| 340 | KEXEC_RELOCATE_NEW_KERNEL_SIZE); |
| 341 | |
| 342 | pr_info("Before _print_args().\n"); |
| 343 | machine_kexec_print_args(); |
| 344 | pr_info("Before eval loop.\n"); |
| 345 | |
| 346 | /* |
| 347 | * The generic kexec code builds a page list with physical |
| 348 | * addresses. they are directly accessible through KSEG0 (or |
| 349 | * CKSEG0 or XPHYS if on 64bit system), hence the |
| 350 | * phys_to_virt() call. |
| 351 | */ |
| 352 | for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE); |
| 353 | ptr = (entry & IND_INDIRECTION) ? |
| 354 | phys_to_virt(entry & PAGE_MASK) : ptr + 1) { |
| 355 | if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION || |
| 356 | *ptr & IND_DESTINATION) |
| 357 | *ptr = (unsigned long) phys_to_virt(*ptr); |
| 358 | } |
| 359 | |
| 360 | /* Mark offline BEFORE disabling local irq. */ |
| 361 | set_cpu_online(smp_processor_id(), false); |
| 362 | |
| 363 | /* |
| 364 | * we do not want to be bothered. |
| 365 | */ |
| 366 | local_irq_disable(); |
| 367 | |
| 368 | printk("Will call new kernel at %08lx\n", image->start); |
| 369 | printk("Bye ...\n"); |
| 370 | /* Make reboot code buffer available to the boot CPU. */ |
| 371 | __flush_cache_all(); |
| 372 | #ifdef CONFIG_SMP |
| 373 | /* All secondary cpus now may jump to kexec_wait cycle */ |
| 374 | relocated_kexec_smp_wait = reboot_code_buffer + |
| 375 | (void *)(kexec_smp_wait - kexec_relocate_new_kernel); |
| 376 | smp_wmb(); |
| 377 | atomic_set(&kexec_ready_to_reboot, 1); |
| 378 | #endif |
| 379 | kexec_reboot(); |
| 380 | } |