Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * machine_kexec.c for kexec |
| 3 | * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006 |
| 4 | * |
| 5 | * This source code is licensed under the GNU General Public License, |
| 6 | * Version 2. See the file COPYING for more details. |
| 7 | */ |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 9 | #include <linux/kexec.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/delay.h> |
| 12 | |
| 13 | #include <asm/cacheflush.h> |
| 14 | #include <asm/page.h> |
| 15 | |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 16 | extern const unsigned char relocate_new_kernel[]; |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 17 | extern const size_t relocate_new_kernel_size; |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 18 | |
| 19 | extern unsigned long kexec_start_address; |
| 20 | extern unsigned long kexec_indirection_page; |
| 21 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 22 | static unsigned long reboot_code_buffer; |
| 23 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 24 | #ifdef CONFIG_SMP |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 25 | static void (*relocated_kexec_smp_wait)(void *); |
| 26 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 27 | atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0); |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 28 | void (*_crash_smp_send_stop)(void) = NULL; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 29 | #endif |
| 30 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 31 | int (*_machine_kexec_prepare)(struct kimage *) = NULL; |
| 32 | void (*_machine_kexec_shutdown)(void) = NULL; |
| 33 | void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL; |
| 34 | |
Marcin Nowakowski | 856b0f5 | 2016-11-23 14:43:51 +0100 | [diff] [blame] | 35 | static void kexec_image_info(const struct kimage *kimage) |
| 36 | { |
| 37 | unsigned long i; |
| 38 | |
| 39 | pr_debug("kexec kimage info:\n"); |
| 40 | pr_debug(" type: %d\n", kimage->type); |
| 41 | pr_debug(" start: %lx\n", kimage->start); |
| 42 | pr_debug(" head: %lx\n", kimage->head); |
| 43 | pr_debug(" nr_segments: %lu\n", kimage->nr_segments); |
| 44 | |
| 45 | for (i = 0; i < kimage->nr_segments; i++) { |
| 46 | pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n", |
| 47 | i, |
| 48 | kimage->segment[i].mem, |
| 49 | kimage->segment[i].mem + kimage->segment[i].memsz, |
| 50 | (unsigned long)kimage->segment[i].memsz, |
| 51 | (unsigned long)kimage->segment[i].memsz / PAGE_SIZE); |
| 52 | } |
| 53 | } |
| 54 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 55 | int |
| 56 | machine_kexec_prepare(struct kimage *kimage) |
| 57 | { |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 58 | #ifdef CONFIG_SMP |
| 59 | if (!kexec_nonboot_cpu_func()) |
| 60 | return -EINVAL; |
| 61 | #endif |
| 62 | |
Marcin Nowakowski | 856b0f5 | 2016-11-23 14:43:51 +0100 | [diff] [blame] | 63 | kexec_image_info(kimage); |
| 64 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 65 | if (_machine_kexec_prepare) |
| 66 | return _machine_kexec_prepare(kimage); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 67 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | machine_kexec_cleanup(struct kimage *kimage) |
| 73 | { |
| 74 | } |
| 75 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 76 | #ifdef CONFIG_SMP |
| 77 | static void kexec_shutdown_secondary(void *param) |
| 78 | { |
| 79 | int cpu = smp_processor_id(); |
| 80 | |
| 81 | if (!cpu_online(cpu)) |
| 82 | return; |
| 83 | |
| 84 | /* We won't be sent IPIs any more. */ |
| 85 | set_cpu_online(cpu, false); |
| 86 | |
| 87 | local_irq_disable(); |
| 88 | while (!atomic_read(&kexec_ready_to_reboot)) |
| 89 | cpu_relax(); |
| 90 | |
| 91 | kexec_reboot(); |
| 92 | |
| 93 | /* NOTREACHED */ |
| 94 | } |
| 95 | #endif |
| 96 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 97 | void |
| 98 | machine_shutdown(void) |
| 99 | { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 100 | if (_machine_kexec_shutdown) |
| 101 | _machine_kexec_shutdown(); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 102 | |
| 103 | #ifdef CONFIG_SMP |
| 104 | smp_call_function(kexec_shutdown_secondary, NULL, 0); |
| 105 | |
| 106 | while (num_online_cpus() > 1) { |
| 107 | cpu_relax(); |
| 108 | mdelay(1); |
| 109 | } |
| 110 | #endif |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
| 114 | machine_crash_shutdown(struct pt_regs *regs) |
| 115 | { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 116 | if (_machine_crash_shutdown) |
| 117 | _machine_crash_shutdown(regs); |
| 118 | else |
| 119 | default_machine_crash_shutdown(regs); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 122 | #ifdef CONFIG_SMP |
| 123 | void kexec_nonboot_cpu_jump(void) |
| 124 | { |
| 125 | local_flush_icache_range((unsigned long)relocated_kexec_smp_wait, |
| 126 | reboot_code_buffer + relocate_new_kernel_size); |
| 127 | |
| 128 | relocated_kexec_smp_wait(NULL); |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | void kexec_reboot(void) |
| 133 | { |
| 134 | void (*do_kexec)(void) __noreturn; |
| 135 | |
| 136 | #ifdef CONFIG_SMP |
| 137 | if (smp_processor_id() > 0) { |
| 138 | /* |
| 139 | * Instead of cpu_relax() or wait, this is needed for kexec |
| 140 | * smp reboot. Kdump usually doesn't require an smp new |
| 141 | * kernel, but kexec may do. |
| 142 | */ |
| 143 | kexec_nonboot_cpu(); |
| 144 | |
| 145 | /* NOTREACHED */ |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | /* |
| 150 | * Make sure we get correct instructions written by the |
| 151 | * machine_kexec() CPU. |
| 152 | */ |
| 153 | local_flush_icache_range(reboot_code_buffer, |
| 154 | reboot_code_buffer + relocate_new_kernel_size); |
| 155 | |
| 156 | do_kexec = (void *)reboot_code_buffer; |
| 157 | do_kexec(); |
| 158 | } |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 159 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 160 | void |
| 161 | machine_kexec(struct kimage *image) |
| 162 | { |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 163 | unsigned long entry; |
| 164 | unsigned long *ptr; |
| 165 | |
| 166 | reboot_code_buffer = |
| 167 | (unsigned long)page_address(image->control_code_page); |
| 168 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 169 | kexec_start_address = |
| 170 | (unsigned long) phys_to_virt(image->start); |
| 171 | |
Yang Wei | 91ffaa2 | 2014-07-31 19:42:29 +0800 | [diff] [blame] | 172 | if (image->type == KEXEC_TYPE_DEFAULT) { |
| 173 | kexec_indirection_page = |
| 174 | (unsigned long) phys_to_virt(image->head & PAGE_MASK); |
| 175 | } else { |
| 176 | kexec_indirection_page = (unsigned long)&image->head; |
| 177 | } |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 178 | |
| 179 | memcpy((void*)reboot_code_buffer, relocate_new_kernel, |
| 180 | relocate_new_kernel_size); |
| 181 | |
| 182 | /* |
| 183 | * The generic kexec code builds a page list with physical |
| 184 | * addresses. they are directly accessible through KSEG0 (or |
| 185 | * CKSEG0 or XPHYS if on 64bit system), hence the |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 186 | * phys_to_virt() call. |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 187 | */ |
| 188 | for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE); |
| 189 | ptr = (entry & IND_INDIRECTION) ? |
| 190 | phys_to_virt(entry & PAGE_MASK) : ptr + 1) { |
| 191 | if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION || |
| 192 | *ptr & IND_DESTINATION) |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 193 | *ptr = (unsigned long) phys_to_virt(*ptr); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Dengcheng Zhu | dc57aaf | 2018-09-11 14:49:20 -0700 | [diff] [blame] | 196 | /* Mark offline BEFORE disabling local irq. */ |
| 197 | set_cpu_online(smp_processor_id(), false); |
| 198 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 199 | /* |
| 200 | * we do not want to be bothered. |
| 201 | */ |
| 202 | local_irq_disable(); |
| 203 | |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 204 | printk("Will call new kernel at %08lx\n", image->start); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 205 | printk("Bye ...\n"); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 206 | /* Make reboot code buffer available to the boot CPU. */ |
Nicolas Schichan | 97ce9a8 | 2007-08-20 15:57:38 +0200 | [diff] [blame] | 207 | __flush_cache_all(); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 208 | #ifdef CONFIG_SMP |
| 209 | /* All secondary cpus now may jump to kexec_wait cycle */ |
| 210 | relocated_kexec_smp_wait = reboot_code_buffer + |
| 211 | (void *)(kexec_smp_wait - relocate_new_kernel); |
| 212 | smp_wmb(); |
| 213 | atomic_set(&kexec_ready_to_reboot, 1); |
| 214 | #endif |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame^] | 215 | kexec_reboot(); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 216 | } |