4 * Copyright (C) Linaro.
5 * Copyright (C) Huawei Futurewei Technologies.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kexec.h>
13 #include <linux/page-flags.h>
14 #include <linux/smp.h>
16 #include <asm/cacheflush.h>
17 #include <asm/cpu_ops.h>
19 #include <asm/mmu_context.h>
22 #include "cpu-reset.h"
24 /* Global variables for the arm64_relocate_new_kernel routine. */
25 extern const unsigned char arm64_relocate_new_kernel[];
26 extern const unsigned long arm64_relocate_new_kernel_size;
29 * kexec_image_info - For debugging output.
31 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
32 static void _kexec_image_info(const char *func, int line,
33 const struct kimage *kimage)
37 pr_debug("%s:%d:\n", func, line);
38 pr_debug(" kexec kimage info:\n");
39 pr_debug(" type: %d\n", kimage->type);
40 pr_debug(" start: %lx\n", kimage->start);
41 pr_debug(" head: %lx\n", kimage->head);
42 pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
44 for (i = 0; i < kimage->nr_segments; i++) {
45 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
47 kimage->segment[i].mem,
48 kimage->segment[i].mem + kimage->segment[i].memsz,
49 kimage->segment[i].memsz,
50 kimage->segment[i].memsz / PAGE_SIZE);
54 void machine_kexec_cleanup(struct kimage *kimage)
56 /* Empty routine needed to avoid build errors. */
60 * machine_kexec_prepare - Prepare for a kexec reboot.
62 * Called from the core kexec code when a kernel image is loaded.
63 * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
64 * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
66 int machine_kexec_prepare(struct kimage *kimage)
68 kexec_image_info(kimage);
70 if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
71 pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
79 * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
81 static void kexec_list_flush(struct kimage *kimage)
83 kimage_entry_t *entry;
85 for (entry = &kimage->head; ; entry++) {
89 /* flush the list entries. */
90 __flush_dcache_area(entry, sizeof(kimage_entry_t));
92 flag = *entry & IND_FLAGS;
96 addr = phys_to_virt(*entry & PAGE_MASK);
100 /* Set entry point just before the new list page. */
101 entry = (kimage_entry_t *)addr - 1;
104 /* flush the source pages. */
105 __flush_dcache_area(addr, PAGE_SIZE);
107 case IND_DESTINATION:
116 * kexec_segment_flush - Helper to flush the kimage segments to PoC.
118 static void kexec_segment_flush(const struct kimage *kimage)
122 pr_debug("%s:\n", __func__);
124 for (i = 0; i < kimage->nr_segments; i++) {
125 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
127 kimage->segment[i].mem,
128 kimage->segment[i].mem + kimage->segment[i].memsz,
129 kimage->segment[i].memsz,
130 kimage->segment[i].memsz / PAGE_SIZE);
132 __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
133 kimage->segment[i].memsz);
138 * machine_kexec - Do the kexec reboot.
140 * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
142 void machine_kexec(struct kimage *kimage)
144 phys_addr_t reboot_code_buffer_phys;
145 void *reboot_code_buffer;
148 * New cpus may have become stuck_in_kernel after we loaded the image.
150 BUG_ON(cpus_are_stuck_in_kernel() || (num_online_cpus() > 1));
152 reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
153 reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
155 kexec_image_info(kimage);
157 pr_debug("%s:%d: control_code_page: %p\n", __func__, __LINE__,
158 kimage->control_code_page);
159 pr_debug("%s:%d: reboot_code_buffer_phys: %pa\n", __func__, __LINE__,
160 &reboot_code_buffer_phys);
161 pr_debug("%s:%d: reboot_code_buffer: %p\n", __func__, __LINE__,
163 pr_debug("%s:%d: relocate_new_kernel: %p\n", __func__, __LINE__,
164 arm64_relocate_new_kernel);
165 pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
166 __func__, __LINE__, arm64_relocate_new_kernel_size,
167 arm64_relocate_new_kernel_size);
170 * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
171 * after the kernel is shut down.
173 memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
174 arm64_relocate_new_kernel_size);
176 /* Flush the reboot_code_buffer in preparation for its execution. */
177 __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
178 flush_icache_range((uintptr_t)reboot_code_buffer,
179 arm64_relocate_new_kernel_size);
181 /* Flush the kimage list and its buffers. */
182 kexec_list_flush(kimage);
184 /* Flush the new image if already in place. */
185 if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
186 kexec_segment_flush(kimage);
190 /* Disable all DAIF exceptions. */
191 asm volatile ("msr daifset, #0xf" : : : "memory");
194 * cpu_soft_restart will shutdown the MMU, disable data caches, then
195 * transfer control to the reboot_code_buffer which contains a copy of
196 * the arm64_relocate_new_kernel routine. arm64_relocate_new_kernel
197 * uses physical addressing to relocate the new image to its final
198 * position and transfers control to the image entry point when the
199 * relocation is complete.
202 cpu_soft_restart(1, reboot_code_buffer_phys, kimage->head,
205 BUG(); /* Should never get here. */
208 void machine_crash_shutdown(struct pt_regs *regs)
210 /* Empty routine needed to avoid build errors. */
213 void arch_kexec_protect_crashkres(void)
217 kexec_segment_flush(kexec_crash_image);
219 for (i = 0; i < kexec_crash_image->nr_segments; i++)
221 __phys_to_virt(kexec_crash_image->segment[i].mem),
222 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
225 void arch_kexec_unprotect_crashkres(void)
229 for (i = 0; i < kexec_crash_image->nr_segments; i++)
231 __phys_to_virt(kexec_crash_image->segment[i].mem),
232 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
235 #ifdef CONFIG_HIBERNATION
237 * To preserve the crash dump kernel image, the relevant memory segments
238 * should be mapped again around the hibernation.
240 void crash_prepare_suspend(void)
242 if (kexec_crash_image)
243 arch_kexec_unprotect_crashkres();
246 void crash_post_resume(void)
248 if (kexec_crash_image)
249 arch_kexec_protect_crashkres();
255 * Return true only if a page is part of reserved memory for crash dump kernel,
256 * but does not hold any data of loaded kernel image.
258 * Note that all the pages in crash dump kernel memory have been initially
259 * marked as Reserved in kexec_reserve_crashkres_pages().
261 * In hibernation, the pages which are Reserved and yet "nosave" are excluded
262 * from the hibernation iamge. crash_is_nosave() does thich check for crash
263 * dump kernel and will reduce the total size of hibernation image.
266 bool crash_is_nosave(unsigned long pfn)
274 /* in reserved memory? */
275 addr = __pfn_to_phys(pfn);
276 if ((addr < crashk_res.start) || (crashk_res.end < addr))
279 if (!kexec_crash_image)
282 /* not part of loaded kernel image? */
283 for (i = 0; i < kexec_crash_image->nr_segments; i++)
284 if (addr >= kexec_crash_image->segment[i].mem &&
285 addr < (kexec_crash_image->segment[i].mem +
286 kexec_crash_image->segment[i].memsz))
292 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
297 for (addr = begin; addr < end; addr += PAGE_SIZE) {
298 page = phys_to_page(addr);
299 ClearPageReserved(page);
300 free_reserved_page(page);
303 #endif /* CONFIG_HIBERNATION */