GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / arm64 / kernel / machine_kexec.c
1 /*
2  * kexec for arm64
3  *
4  * Copyright (C) Linaro.
5  * Copyright (C) Huawei Futurewei Technologies.
6  *
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.
10  */
11
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/kexec.h>
16 #include <linux/page-flags.h>
17 #include <linux/smp.h>
18
19 #include <asm/cacheflush.h>
20 #include <asm/cpu_ops.h>
21 #include <asm/memory.h>
22 #include <asm/mmu.h>
23 #include <asm/mmu_context.h>
24 #include <asm/page.h>
25
26 #include "cpu-reset.h"
27
28 /* Global variables for the arm64_relocate_new_kernel routine. */
29 extern const unsigned char arm64_relocate_new_kernel[];
30 extern const unsigned long arm64_relocate_new_kernel_size;
31
32 /**
33  * kexec_image_info - For debugging output.
34  */
35 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
36 static void _kexec_image_info(const char *func, int line,
37         const struct kimage *kimage)
38 {
39         unsigned long i;
40
41         pr_debug("%s:%d:\n", func, line);
42         pr_debug("  kexec kimage info:\n");
43         pr_debug("    type:        %d\n", kimage->type);
44         pr_debug("    start:       %lx\n", kimage->start);
45         pr_debug("    head:        %lx\n", kimage->head);
46         pr_debug("    nr_segments: %lu\n", kimage->nr_segments);
47
48         for (i = 0; i < kimage->nr_segments; i++) {
49                 pr_debug("      segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
50                         i,
51                         kimage->segment[i].mem,
52                         kimage->segment[i].mem + kimage->segment[i].memsz,
53                         kimage->segment[i].memsz,
54                         kimage->segment[i].memsz /  PAGE_SIZE);
55         }
56 }
57
58 void machine_kexec_cleanup(struct kimage *kimage)
59 {
60         /* Empty routine needed to avoid build errors. */
61 }
62
63 /**
64  * machine_kexec_prepare - Prepare for a kexec reboot.
65  *
66  * Called from the core kexec code when a kernel image is loaded.
67  * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
68  * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
69  */
70 int machine_kexec_prepare(struct kimage *kimage)
71 {
72         kexec_image_info(kimage);
73
74         if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
75                 pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
76                 return -EBUSY;
77         }
78
79         return 0;
80 }
81
82 /**
83  * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
84  */
85 static void kexec_list_flush(struct kimage *kimage)
86 {
87         kimage_entry_t *entry;
88
89         for (entry = &kimage->head; ; entry++) {
90                 unsigned int flag;
91                 void *addr;
92
93                 /* flush the list entries. */
94                 __flush_dcache_area(entry, sizeof(kimage_entry_t));
95
96                 flag = *entry & IND_FLAGS;
97                 if (flag == IND_DONE)
98                         break;
99
100                 addr = phys_to_virt(*entry & PAGE_MASK);
101
102                 switch (flag) {
103                 case IND_INDIRECTION:
104                         /* Set entry point just before the new list page. */
105                         entry = (kimage_entry_t *)addr - 1;
106                         break;
107                 case IND_SOURCE:
108                         /* flush the source pages. */
109                         __flush_dcache_area(addr, PAGE_SIZE);
110                         break;
111                 case IND_DESTINATION:
112                         break;
113                 default:
114                         BUG();
115                 }
116         }
117 }
118
119 /**
120  * kexec_segment_flush - Helper to flush the kimage segments to PoC.
121  */
122 static void kexec_segment_flush(const struct kimage *kimage)
123 {
124         unsigned long i;
125
126         pr_debug("%s:\n", __func__);
127
128         for (i = 0; i < kimage->nr_segments; i++) {
129                 pr_debug("  segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
130                         i,
131                         kimage->segment[i].mem,
132                         kimage->segment[i].mem + kimage->segment[i].memsz,
133                         kimage->segment[i].memsz,
134                         kimage->segment[i].memsz /  PAGE_SIZE);
135
136                 __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
137                         kimage->segment[i].memsz);
138         }
139 }
140
141 /**
142  * machine_kexec - Do the kexec reboot.
143  *
144  * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
145  */
146 void machine_kexec(struct kimage *kimage)
147 {
148         phys_addr_t reboot_code_buffer_phys;
149         void *reboot_code_buffer;
150         bool in_kexec_crash = (kimage == kexec_crash_image);
151         bool stuck_cpus = cpus_are_stuck_in_kernel();
152
153         /*
154          * New cpus may have become stuck_in_kernel after we loaded the image.
155          */
156         BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
157         WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
158                 "Some CPUs may be stale, kdump will be unreliable.\n");
159
160         reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
161         reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
162
163         kexec_image_info(kimage);
164
165         pr_debug("%s:%d: control_code_page:        %p\n", __func__, __LINE__,
166                 kimage->control_code_page);
167         pr_debug("%s:%d: reboot_code_buffer_phys:  %pa\n", __func__, __LINE__,
168                 &reboot_code_buffer_phys);
169         pr_debug("%s:%d: reboot_code_buffer:       %p\n", __func__, __LINE__,
170                 reboot_code_buffer);
171         pr_debug("%s:%d: relocate_new_kernel:      %p\n", __func__, __LINE__,
172                 arm64_relocate_new_kernel);
173         pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
174                 __func__, __LINE__, arm64_relocate_new_kernel_size,
175                 arm64_relocate_new_kernel_size);
176
177         /*
178          * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
179          * after the kernel is shut down.
180          */
181         memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
182                 arm64_relocate_new_kernel_size);
183
184         /* Flush the reboot_code_buffer in preparation for its execution. */
185         __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
186         flush_icache_range((uintptr_t)reboot_code_buffer,
187                            (uintptr_t)reboot_code_buffer +
188                            arm64_relocate_new_kernel_size);
189
190         /* Flush the kimage list and its buffers. */
191         kexec_list_flush(kimage);
192
193         /* Flush the new image if already in place. */
194         if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
195                 kexec_segment_flush(kimage);
196
197         pr_info("Bye!\n");
198
199         /* Disable all DAIF exceptions. */
200         asm volatile ("msr daifset, #0xf" : : : "memory");
201
202         /*
203          * cpu_soft_restart will shutdown the MMU, disable data caches, then
204          * transfer control to the reboot_code_buffer which contains a copy of
205          * the arm64_relocate_new_kernel routine.  arm64_relocate_new_kernel
206          * uses physical addressing to relocate the new image to its final
207          * position and transfers control to the image entry point when the
208          * relocation is complete.
209          */
210
211         cpu_soft_restart(kimage != kexec_crash_image,
212                 reboot_code_buffer_phys, kimage->head, kimage->start, 0);
213
214         BUG(); /* Should never get here. */
215 }
216
217 static void machine_kexec_mask_interrupts(void)
218 {
219         unsigned int i;
220         struct irq_desc *desc;
221
222         for_each_irq_desc(i, desc) {
223                 struct irq_chip *chip;
224                 int ret;
225
226                 chip = irq_desc_get_chip(desc);
227                 if (!chip)
228                         continue;
229
230                 /*
231                  * First try to remove the active state. If this
232                  * fails, try to EOI the interrupt.
233                  */
234                 ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
235
236                 if (ret && irqd_irq_inprogress(&desc->irq_data) &&
237                     chip->irq_eoi)
238                         chip->irq_eoi(&desc->irq_data);
239
240                 if (chip->irq_mask)
241                         chip->irq_mask(&desc->irq_data);
242
243                 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
244                         chip->irq_disable(&desc->irq_data);
245         }
246 }
247
248 /**
249  * machine_crash_shutdown - shutdown non-crashing cpus and save registers
250  */
251 void machine_crash_shutdown(struct pt_regs *regs)
252 {
253         local_irq_disable();
254
255         /* shutdown non-crashing cpus */
256         crash_smp_send_stop();
257
258         /* for crashing cpu */
259         crash_save_cpu(regs, smp_processor_id());
260         machine_kexec_mask_interrupts();
261
262         pr_info("Starting crashdump kernel...\n");
263 }
264
265 void arch_kexec_protect_crashkres(void)
266 {
267         int i;
268
269         kexec_segment_flush(kexec_crash_image);
270
271         for (i = 0; i < kexec_crash_image->nr_segments; i++)
272                 set_memory_valid(
273                         __phys_to_virt(kexec_crash_image->segment[i].mem),
274                         kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
275 }
276
277 void arch_kexec_unprotect_crashkres(void)
278 {
279         int i;
280
281         for (i = 0; i < kexec_crash_image->nr_segments; i++)
282                 set_memory_valid(
283                         __phys_to_virt(kexec_crash_image->segment[i].mem),
284                         kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
285 }
286
287 #ifdef CONFIG_HIBERNATION
288 /*
289  * To preserve the crash dump kernel image, the relevant memory segments
290  * should be mapped again around the hibernation.
291  */
292 void crash_prepare_suspend(void)
293 {
294         if (kexec_crash_image)
295                 arch_kexec_unprotect_crashkres();
296 }
297
298 void crash_post_resume(void)
299 {
300         if (kexec_crash_image)
301                 arch_kexec_protect_crashkres();
302 }
303
304 /*
305  * crash_is_nosave
306  *
307  * Return true only if a page is part of reserved memory for crash dump kernel,
308  * but does not hold any data of loaded kernel image.
309  *
310  * Note that all the pages in crash dump kernel memory have been initially
311  * marked as Reserved in kexec_reserve_crashkres_pages().
312  *
313  * In hibernation, the pages which are Reserved and yet "nosave" are excluded
314  * from the hibernation iamge. crash_is_nosave() does thich check for crash
315  * dump kernel and will reduce the total size of hibernation image.
316  */
317
318 bool crash_is_nosave(unsigned long pfn)
319 {
320         int i;
321         phys_addr_t addr;
322
323         if (!crashk_res.end)
324                 return false;
325
326         /* in reserved memory? */
327         addr = __pfn_to_phys(pfn);
328         if ((addr < crashk_res.start) || (crashk_res.end < addr))
329                 return false;
330
331         if (!kexec_crash_image)
332                 return true;
333
334         /* not part of loaded kernel image? */
335         for (i = 0; i < kexec_crash_image->nr_segments; i++)
336                 if (addr >= kexec_crash_image->segment[i].mem &&
337                                 addr < (kexec_crash_image->segment[i].mem +
338                                         kexec_crash_image->segment[i].memsz))
339                         return false;
340
341         return true;
342 }
343
344 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
345 {
346         unsigned long addr;
347         struct page *page;
348
349         for (addr = begin; addr < end; addr += PAGE_SIZE) {
350                 page = phys_to_page(addr);
351                 ClearPageReserved(page);
352                 free_reserved_page(page);
353         }
354 }
355 #endif /* CONFIG_HIBERNATION */
356
357 void arch_crash_save_vmcoreinfo(void)
358 {
359         VMCOREINFO_NUMBER(VA_BITS);
360         /* Please note VMCOREINFO_NUMBER() uses "%d", not "%x" */
361         vmcoreinfo_append_str("NUMBER(kimage_voffset)=0x%llx\n",
362                                                 kimage_voffset);
363         vmcoreinfo_append_str("NUMBER(PHYS_OFFSET)=0x%llx\n",
364                                                 PHYS_OFFSET);
365 }