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 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 22 | int (*_machine_kexec_prepare)(struct kimage *) = NULL; |
| 23 | void (*_machine_kexec_shutdown)(void) = NULL; |
| 24 | void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL; |
| 25 | #ifdef CONFIG_SMP |
| 26 | void (*relocated_kexec_smp_wait) (void *); |
| 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 | |
Marcin Nowakowski | 856b0f5 | 2016-11-23 14:43:51 +0100 | [diff] [blame] | 31 | static void kexec_image_info(const struct kimage *kimage) |
| 32 | { |
| 33 | unsigned long i; |
| 34 | |
| 35 | pr_debug("kexec kimage info:\n"); |
| 36 | pr_debug(" type: %d\n", kimage->type); |
| 37 | pr_debug(" start: %lx\n", kimage->start); |
| 38 | pr_debug(" head: %lx\n", kimage->head); |
| 39 | pr_debug(" nr_segments: %lu\n", kimage->nr_segments); |
| 40 | |
| 41 | for (i = 0; i < kimage->nr_segments; i++) { |
| 42 | pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n", |
| 43 | i, |
| 44 | kimage->segment[i].mem, |
| 45 | kimage->segment[i].mem + kimage->segment[i].memsz, |
| 46 | (unsigned long)kimage->segment[i].memsz, |
| 47 | (unsigned long)kimage->segment[i].memsz / PAGE_SIZE); |
| 48 | } |
| 49 | } |
| 50 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 51 | int |
| 52 | machine_kexec_prepare(struct kimage *kimage) |
| 53 | { |
Marcin Nowakowski | 856b0f5 | 2016-11-23 14:43:51 +0100 | [diff] [blame] | 54 | kexec_image_info(kimage); |
| 55 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 56 | if (_machine_kexec_prepare) |
| 57 | return _machine_kexec_prepare(kimage); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | machine_kexec_cleanup(struct kimage *kimage) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | machine_shutdown(void) |
| 68 | { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 69 | if (_machine_kexec_shutdown) |
| 70 | _machine_kexec_shutdown(); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | machine_crash_shutdown(struct pt_regs *regs) |
| 75 | { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 76 | if (_machine_crash_shutdown) |
| 77 | _machine_crash_shutdown(regs); |
| 78 | else |
| 79 | default_machine_crash_shutdown(regs); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 82 | typedef void (*noretfun_t)(void) __noreturn; |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 83 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 84 | void |
| 85 | machine_kexec(struct kimage *image) |
| 86 | { |
| 87 | unsigned long reboot_code_buffer; |
| 88 | unsigned long entry; |
| 89 | unsigned long *ptr; |
| 90 | |
| 91 | reboot_code_buffer = |
| 92 | (unsigned long)page_address(image->control_code_page); |
| 93 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 94 | kexec_start_address = |
| 95 | (unsigned long) phys_to_virt(image->start); |
| 96 | |
Yang Wei | 91ffaa2 | 2014-07-31 19:42:29 +0800 | [diff] [blame] | 97 | if (image->type == KEXEC_TYPE_DEFAULT) { |
| 98 | kexec_indirection_page = |
| 99 | (unsigned long) phys_to_virt(image->head & PAGE_MASK); |
| 100 | } else { |
| 101 | kexec_indirection_page = (unsigned long)&image->head; |
| 102 | } |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 103 | |
| 104 | memcpy((void*)reboot_code_buffer, relocate_new_kernel, |
| 105 | relocate_new_kernel_size); |
| 106 | |
| 107 | /* |
| 108 | * The generic kexec code builds a page list with physical |
| 109 | * addresses. they are directly accessible through KSEG0 (or |
| 110 | * CKSEG0 or XPHYS if on 64bit system), hence the |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 111 | * phys_to_virt() call. |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 112 | */ |
| 113 | for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE); |
| 114 | ptr = (entry & IND_INDIRECTION) ? |
| 115 | phys_to_virt(entry & PAGE_MASK) : ptr + 1) { |
| 116 | if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION || |
| 117 | *ptr & IND_DESTINATION) |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 118 | *ptr = (unsigned long) phys_to_virt(*ptr); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Dengcheng Zhu | 806be82 | 2018-09-11 14:49:20 -0700 | [diff] [blame] | 121 | /* Mark offline BEFORE disabling local irq. */ |
| 122 | set_cpu_online(smp_processor_id(), false); |
| 123 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 124 | /* |
| 125 | * we do not want to be bothered. |
| 126 | */ |
| 127 | local_irq_disable(); |
| 128 | |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 129 | printk("Will call new kernel at %08lx\n", image->start); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 130 | printk("Bye ...\n"); |
Nicolas Schichan | 97ce9a8 | 2007-08-20 15:57:38 +0200 | [diff] [blame] | 131 | __flush_cache_all(); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 132 | #ifdef CONFIG_SMP |
| 133 | /* All secondary cpus now may jump to kexec_wait cycle */ |
| 134 | relocated_kexec_smp_wait = reboot_code_buffer + |
| 135 | (void *)(kexec_smp_wait - relocate_new_kernel); |
| 136 | smp_wmb(); |
| 137 | atomic_set(&kexec_ready_to_reboot, 1); |
| 138 | #endif |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 139 | ((noretfun_t) reboot_code_buffer)(); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 140 | } |