Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * machine_kexec.c for kexec
0004  * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006
0005  */
0006 #include <linux/compiler.h>
0007 #include <linux/kexec.h>
0008 #include <linux/mm.h>
0009 #include <linux/delay.h>
0010 #include <linux/libfdt.h>
0011 
0012 #include <asm/cacheflush.h>
0013 #include <asm/page.h>
0014 
0015 extern const unsigned char relocate_new_kernel[];
0016 extern const size_t relocate_new_kernel_size;
0017 
0018 extern unsigned long kexec_start_address;
0019 extern unsigned long kexec_indirection_page;
0020 
0021 static unsigned long reboot_code_buffer;
0022 
0023 #ifdef CONFIG_SMP
0024 static void (*relocated_kexec_smp_wait)(void *);
0025 
0026 atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
0027 void (*_crash_smp_send_stop)(void) = NULL;
0028 #endif
0029 
0030 void (*_machine_kexec_shutdown)(void) = NULL;
0031 void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
0032 
0033 static void kexec_image_info(const struct kimage *kimage)
0034 {
0035     unsigned long i;
0036 
0037     pr_debug("kexec kimage info:\n");
0038     pr_debug("  type:        %d\n", kimage->type);
0039     pr_debug("  start:       %lx\n", kimage->start);
0040     pr_debug("  head:        %lx\n", kimage->head);
0041     pr_debug("  nr_segments: %lu\n", kimage->nr_segments);
0042 
0043     for (i = 0; i < kimage->nr_segments; i++) {
0044         pr_debug("    segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
0045             i,
0046             kimage->segment[i].mem,
0047             kimage->segment[i].mem + kimage->segment[i].memsz,
0048             (unsigned long)kimage->segment[i].memsz,
0049             (unsigned long)kimage->segment[i].memsz /  PAGE_SIZE);
0050     }
0051 }
0052 
0053 #ifdef CONFIG_UHI_BOOT
0054 
0055 static int uhi_machine_kexec_prepare(struct kimage *kimage)
0056 {
0057     int i;
0058 
0059     /*
0060      * In case DTB file is not passed to the new kernel, a flat device
0061      * tree will be created by kexec tool. It holds modified command
0062      * line for the new kernel.
0063      */
0064     for (i = 0; i < kimage->nr_segments; i++) {
0065         struct fdt_header fdt;
0066 
0067         if (kimage->segment[i].memsz <= sizeof(fdt))
0068             continue;
0069 
0070         if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt)))
0071             continue;
0072 
0073         if (fdt_check_header(&fdt))
0074             continue;
0075 
0076         kexec_args[0] = -2;
0077         kexec_args[1] = (unsigned long)
0078             phys_to_virt((unsigned long)kimage->segment[i].mem);
0079         break;
0080     }
0081 
0082     return 0;
0083 }
0084 
0085 int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare;
0086 
0087 #else
0088 
0089 int (*_machine_kexec_prepare)(struct kimage *) = NULL;
0090 
0091 #endif /* CONFIG_UHI_BOOT */
0092 
0093 int
0094 machine_kexec_prepare(struct kimage *kimage)
0095 {
0096 #ifdef CONFIG_SMP
0097     if (!kexec_nonboot_cpu_func())
0098         return -EINVAL;
0099 #endif
0100 
0101     kexec_image_info(kimage);
0102 
0103     if (_machine_kexec_prepare)
0104         return _machine_kexec_prepare(kimage);
0105 
0106     return 0;
0107 }
0108 
0109 void
0110 machine_kexec_cleanup(struct kimage *kimage)
0111 {
0112 }
0113 
0114 #ifdef CONFIG_SMP
0115 static void kexec_shutdown_secondary(void *param)
0116 {
0117     int cpu = smp_processor_id();
0118 
0119     if (!cpu_online(cpu))
0120         return;
0121 
0122     /* We won't be sent IPIs any more. */
0123     set_cpu_online(cpu, false);
0124 
0125     local_irq_disable();
0126     while (!atomic_read(&kexec_ready_to_reboot))
0127         cpu_relax();
0128 
0129     kexec_reboot();
0130 
0131     /* NOTREACHED */
0132 }
0133 #endif
0134 
0135 void
0136 machine_shutdown(void)
0137 {
0138     if (_machine_kexec_shutdown)
0139         _machine_kexec_shutdown();
0140 
0141 #ifdef CONFIG_SMP
0142     smp_call_function(kexec_shutdown_secondary, NULL, 0);
0143 
0144     while (num_online_cpus() > 1) {
0145         cpu_relax();
0146         mdelay(1);
0147     }
0148 #endif
0149 }
0150 
0151 void
0152 machine_crash_shutdown(struct pt_regs *regs)
0153 {
0154     if (_machine_crash_shutdown)
0155         _machine_crash_shutdown(regs);
0156     else
0157         default_machine_crash_shutdown(regs);
0158 }
0159 
0160 #ifdef CONFIG_SMP
0161 void kexec_nonboot_cpu_jump(void)
0162 {
0163     local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
0164                  reboot_code_buffer + relocate_new_kernel_size);
0165 
0166     relocated_kexec_smp_wait(NULL);
0167 }
0168 #endif
0169 
0170 void kexec_reboot(void)
0171 {
0172     void (*do_kexec)(void) __noreturn;
0173 
0174     /*
0175      * We know we were online, and there will be no incoming IPIs at
0176      * this point. Mark online again before rebooting so that the crash
0177      * analysis tool will see us correctly.
0178      */
0179     set_cpu_online(smp_processor_id(), true);
0180 
0181     /* Ensure remote CPUs observe that we're online before rebooting. */
0182     smp_mb__after_atomic();
0183 
0184 #ifdef CONFIG_SMP
0185     if (smp_processor_id() > 0) {
0186         /*
0187          * Instead of cpu_relax() or wait, this is needed for kexec
0188          * smp reboot. Kdump usually doesn't require an smp new
0189          * kernel, but kexec may do.
0190          */
0191         kexec_nonboot_cpu();
0192 
0193         /* NOTREACHED */
0194     }
0195 #endif
0196 
0197     /*
0198      * Make sure we get correct instructions written by the
0199      * machine_kexec() CPU.
0200      */
0201     local_flush_icache_range(reboot_code_buffer,
0202                  reboot_code_buffer + relocate_new_kernel_size);
0203 
0204     do_kexec = (void *)reboot_code_buffer;
0205     do_kexec();
0206 }
0207 
0208 void
0209 machine_kexec(struct kimage *image)
0210 {
0211     unsigned long entry;
0212     unsigned long *ptr;
0213 
0214     reboot_code_buffer =
0215       (unsigned long)page_address(image->control_code_page);
0216 
0217     kexec_start_address =
0218         (unsigned long) phys_to_virt(image->start);
0219 
0220     if (image->type == KEXEC_TYPE_DEFAULT) {
0221         kexec_indirection_page =
0222             (unsigned long) phys_to_virt(image->head & PAGE_MASK);
0223     } else {
0224         kexec_indirection_page = (unsigned long)&image->head;
0225     }
0226 
0227     memcpy((void*)reboot_code_buffer, relocate_new_kernel,
0228            relocate_new_kernel_size);
0229 
0230     /*
0231      * The generic kexec code builds a page list with physical
0232      * addresses. they are directly accessible through KSEG0 (or
0233      * CKSEG0 or XPHYS if on 64bit system), hence the
0234      * phys_to_virt() call.
0235      */
0236     for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
0237          ptr = (entry & IND_INDIRECTION) ?
0238            phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
0239         if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
0240             *ptr & IND_DESTINATION)
0241             *ptr = (unsigned long) phys_to_virt(*ptr);
0242     }
0243 
0244     /* Mark offline BEFORE disabling local irq. */
0245     set_cpu_online(smp_processor_id(), false);
0246 
0247     /*
0248      * we do not want to be bothered.
0249      */
0250     local_irq_disable();
0251 
0252     printk("Will call new kernel at %08lx\n", image->start);
0253     printk("Bye ...\n");
0254     /* Make reboot code buffer available to the boot CPU. */
0255     __flush_cache_all();
0256 #ifdef CONFIG_SMP
0257     /* All secondary cpus now may jump to kexec_wait cycle */
0258     relocated_kexec_smp_wait = reboot_code_buffer +
0259         (void *)(kexec_smp_wait - relocate_new_kernel);
0260     smp_wmb();
0261     atomic_set(&kexec_ready_to_reboot, 1);
0262 #endif
0263     kexec_reboot();
0264 }