1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * machine_kexec.c for kexec 4 * 5 * Copyright (C) 2022 Loongson Technology Corporation Limited 6 */ 7 #include <linux/compiler.h> 8 #include <linux/cpu.h> 9 #include <linux/kexec.h> 10 #include <linux/crash_dump.h> 11 #include <linux/delay.h> 12 #include <linux/irq.h> 13 #include <linux/libfdt.h> 14 #include <linux/mm.h> 15 #include <linux/of_fdt.h> 16 #include <linux/reboot.h> 17 #include <linux/slab.h> 18 #include <linux/sched.h> 19 #include <linux/sched/task_stack.h> 20 21 #include <asm/bootinfo.h> 22 #include <asm/cacheflush.h> 23 #include <asm/page.h> 24 25 /* 0x100000 ~ 0x200000 is safe */ 26 #define KEXEC_CONTROL_CODE TO_CACHE(0x100000UL) 27 #define KEXEC_CMDLINE_ADDR TO_CACHE(0x108000UL) 28 29 static unsigned long reboot_code_buffer; 30 static cpumask_t cpus_in_crash = CPU_MASK_NONE; 31 32 #ifdef CONFIG_SMP 33 static void (*relocated_kexec_smp_wait)(void *); 34 atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0); 35 #endif 36 37 static unsigned long efi_boot; 38 static unsigned long cmdline_ptr; 39 static unsigned long systable_ptr; 40 static unsigned long start_addr; 41 static unsigned long first_ind_entry; 42 43 int machine_kexec_prepare(struct kimage *kimage) 44 { 45 int i; 46 char head[8]; 47 char *bootloader = "kexec"; 48 void *cmdline_ptr = (void *)KEXEC_CMDLINE_ADDR; 49 50 kimage->arch.efi_boot = fw_arg0; 51 kimage->arch.systable_ptr = fw_arg2; 52 53 if (kimage->file_mode == 1) { 54 /* 55 * kimage->cmdline_buf will be released in kexec_file_load, so copy 56 * to the KEXEC_CMDLINE_ADDR safe area. 57 */ 58 memcpy((void *)KEXEC_CMDLINE_ADDR, (void *)kimage->arch.cmdline_ptr, 59 strlen((char *)kimage->arch.cmdline_ptr) + 1); 60 kfree((void *)kimage->arch.cmdline_ptr); 61 kimage->arch.cmdline_ptr = (unsigned long)KEXEC_CMDLINE_ADDR; 62 } else { 63 /* Find the command line */ 64 for (i = 0; i < kimage->nr_segments; i++) { 65 if (copy_from_user(head, kimage->segment[i].buf, strlen(bootloader))) 66 continue; 67 if (!strncmp(bootloader, head, strlen(bootloader))) { 68 if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE)) 69 kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr; 70 break; 71 } 72 } 73 74 if (!kimage->arch.cmdline_ptr) { 75 pr_err("Command line not included in the provided image\n"); 76 return -EINVAL; 77 } 78 } 79 80 /* kexec/kdump need a safe page to save reboot_code_buffer */ 81 kimage->control_code_page = virt_to_page((void *)KEXEC_CONTROL_CODE); 82 83 reboot_code_buffer = (unsigned long)page_address(kimage->control_code_page); 84 memcpy((void *)reboot_code_buffer, relocate_new_kernel, relocate_new_kernel_size); 85 86 #ifdef CONFIG_SMP 87 /* All secondary cpus now may jump to kexec_smp_wait cycle */ 88 relocated_kexec_smp_wait = reboot_code_buffer + (void *)(kexec_smp_wait - relocate_new_kernel); 89 #endif 90 91 return 0; 92 } 93 94 void machine_kexec_cleanup(struct kimage *kimage) 95 { 96 } 97 98 void kexec_reboot(void) 99 { 100 do_kexec_t do_kexec = NULL; 101 102 /* 103 * We know we were online, and there will be no incoming IPIs at 104 * this point. Mark online again before rebooting so that the crash 105 * analysis tool will see us correctly. 106 */ 107 set_cpu_online(smp_processor_id(), true); 108 109 /* Ensure remote CPUs observe that we're online before rebooting. */ 110 smp_mb__after_atomic(); 111 112 /* 113 * Make sure we get correct instructions written by the 114 * machine_kexec_prepare() CPU. 115 */ 116 __asm__ __volatile__ ("\tibar 0\n"::); 117 118 #ifdef CONFIG_SMP 119 /* All secondary cpus go to kexec_smp_wait */ 120 if (smp_processor_id() > 0) { 121 relocated_kexec_smp_wait(NULL); 122 BUG(); 123 } 124 #endif 125 126 do_kexec = (void *)reboot_code_buffer; 127 do_kexec(efi_boot, cmdline_ptr, systable_ptr, start_addr, first_ind_entry); 128 129 BUG(); 130 } 131 132 133 #ifdef CONFIG_SMP 134 static void kexec_shutdown_secondary(void *regs) 135 { 136 int cpu = smp_processor_id(); 137 138 if (!cpu_online(cpu)) 139 return; 140 141 /* We won't be sent IPIs any more. */ 142 set_cpu_online(cpu, false); 143 144 local_irq_disable(); 145 while (!atomic_read(&kexec_ready_to_reboot)) 146 cpu_relax(); 147 148 kexec_reboot(); 149 } 150 151 static void crash_shutdown_secondary(void *passed_regs) 152 { 153 int cpu = smp_processor_id(); 154 struct pt_regs *regs = passed_regs; 155 156 /* 157 * If we are passed registers, use those. Otherwise get the 158 * regs from the last interrupt, which should be correct, as 159 * we are in an interrupt. But if the regs are not there, 160 * pull them from the top of the stack. They are probably 161 * wrong, but we need something to keep from crashing again. 162 */ 163 if (!regs) 164 regs = get_irq_regs(); 165 if (!regs) 166 regs = task_pt_regs(current); 167 168 if (!cpu_online(cpu)) 169 return; 170 171 /* We won't be sent IPIs any more. */ 172 set_cpu_online(cpu, false); 173 174 local_irq_disable(); 175 if (!cpumask_test_cpu(cpu, &cpus_in_crash)) 176 crash_save_cpu(regs, cpu); 177 cpumask_set_cpu(cpu, &cpus_in_crash); 178 179 while (!atomic_read(&kexec_ready_to_reboot)) 180 cpu_relax(); 181 182 kexec_reboot(); 183 } 184 185 void crash_smp_send_stop(void) 186 { 187 unsigned int ncpus; 188 unsigned long timeout; 189 static int cpus_stopped; 190 191 /* 192 * This function can be called twice in panic path, but obviously 193 * we should execute this only once. 194 */ 195 if (cpus_stopped) 196 return; 197 198 cpus_stopped = 1; 199 200 /* Excluding the panic cpu */ 201 ncpus = num_online_cpus() - 1; 202 203 smp_call_function(crash_shutdown_secondary, NULL, 0); 204 smp_wmb(); 205 206 /* 207 * The crash CPU sends an IPI and wait for other CPUs to 208 * respond. Delay of at least 10 seconds. 209 */ 210 timeout = MSEC_PER_SEC * 10; 211 pr_emerg("Sending IPI to other cpus...\n"); 212 while ((cpumask_weight(&cpus_in_crash) < ncpus) && timeout--) { 213 mdelay(1); 214 cpu_relax(); 215 } 216 } 217 #endif /* defined(CONFIG_SMP) */ 218 219 void machine_shutdown(void) 220 { 221 #ifdef CONFIG_SMP 222 int cpu; 223 224 /* All CPUs go to reboot_code_buffer */ 225 for_each_possible_cpu(cpu) 226 if (!cpu_online(cpu)) 227 cpu_device_up(get_cpu_device(cpu)); 228 229 smp_call_function(kexec_shutdown_secondary, NULL, 0); 230 #endif 231 } 232 233 void machine_crash_shutdown(struct pt_regs *regs) 234 { 235 int crashing_cpu; 236 237 local_irq_disable(); 238 239 crashing_cpu = smp_processor_id(); 240 crash_save_cpu(regs, crashing_cpu); 241 242 #ifdef CONFIG_SMP 243 crash_smp_send_stop(); 244 #endif 245 machine_kexec_mask_interrupts(); 246 cpumask_set_cpu(crashing_cpu, &cpus_in_crash); 247 248 pr_info("Starting crashdump kernel...\n"); 249 } 250 251 void machine_kexec(struct kimage *image) 252 { 253 unsigned long entry, *ptr; 254 struct kimage_arch *internal = &image->arch; 255 256 efi_boot = internal->efi_boot; 257 cmdline_ptr = internal->cmdline_ptr; 258 systable_ptr = internal->systable_ptr; 259 260 start_addr = (unsigned long)phys_to_virt(image->start); 261 262 first_ind_entry = (image->type == KEXEC_TYPE_DEFAULT) ? 263 (unsigned long)phys_to_virt(image->head & PAGE_MASK) : 0; 264 265 /* 266 * The generic kexec code builds a page list with physical 267 * addresses. they are directly accessible through XKPRANGE 268 * hence the phys_to_virt() call. 269 */ 270 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); 271 ptr = (entry & IND_INDIRECTION) ? 272 phys_to_virt(entry & PAGE_MASK) : ptr + 1) { 273 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION || 274 *ptr & IND_DESTINATION) 275 *ptr = (unsigned long) phys_to_virt(*ptr); 276 } 277 278 /* Mark offline before disabling local irq. */ 279 set_cpu_online(smp_processor_id(), false); 280 281 /* We do not want to be bothered. */ 282 local_irq_disable(); 283 machine_kexec_mask_interrupts(); 284 285 pr_notice("EFI boot flag: 0x%lx\n", efi_boot); 286 pr_notice("Command line addr: 0x%lx\n", cmdline_ptr); 287 pr_notice("Command line string: %s\n", (char *)cmdline_ptr); 288 pr_notice("System table addr: 0x%lx\n", systable_ptr); 289 pr_notice("We will call new kernel at 0x%lx\n", start_addr); 290 pr_notice("Bye ...\n"); 291 292 /* Make reboot code buffer available to the boot CPU. */ 293 flush_cache_all(); 294 295 #ifdef CONFIG_SMP 296 atomic_set(&kexec_ready_to_reboot, 1); 297 #endif 298 299 kexec_reboot(); 300 } 301