1
2
3
4
5
6
7
8
9
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
29extern const unsigned char arm64_relocate_new_kernel[];
30extern const unsigned long arm64_relocate_new_kernel_size;
31
32
33
34
35#define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
36static 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
58void machine_kexec_cleanup(struct kimage *kimage)
59{
60
61}
62
63
64
65
66
67
68
69
70int 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
84
85static 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
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
105 entry = (kimage_entry_t *)addr - 1;
106 break;
107 case IND_SOURCE:
108
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
121
122static 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
143
144
145
146void 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
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
179
180
181 memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
182 arm64_relocate_new_kernel_size);
183
184
185 __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
186 flush_icache_range((uintptr_t)reboot_code_buffer,
187 arm64_relocate_new_kernel_size);
188
189
190 kexec_list_flush(kimage);
191
192
193 if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
194 kexec_segment_flush(kimage);
195
196 pr_info("Bye!\n");
197
198
199 asm volatile ("msr daifset, #0xf" : : : "memory");
200
201
202
203
204
205
206
207
208
209
210 cpu_soft_restart(kimage != kexec_crash_image,
211 reboot_code_buffer_phys, kimage->head, kimage->start, 0);
212
213 BUG();
214}
215
216static void machine_kexec_mask_interrupts(void)
217{
218 unsigned int i;
219 struct irq_desc *desc;
220
221 for_each_irq_desc(i, desc) {
222 struct irq_chip *chip;
223 int ret;
224
225 chip = irq_desc_get_chip(desc);
226 if (!chip)
227 continue;
228
229
230
231
232
233 ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
234
235 if (ret && irqd_irq_inprogress(&desc->irq_data) &&
236 chip->irq_eoi)
237 chip->irq_eoi(&desc->irq_data);
238
239 if (chip->irq_mask)
240 chip->irq_mask(&desc->irq_data);
241
242 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
243 chip->irq_disable(&desc->irq_data);
244 }
245}
246
247
248
249
250void machine_crash_shutdown(struct pt_regs *regs)
251{
252 local_irq_disable();
253
254
255 smp_send_crash_stop();
256
257
258 crash_save_cpu(regs, smp_processor_id());
259 machine_kexec_mask_interrupts();
260
261 pr_info("Starting crashdump kernel...\n");
262}
263
264void arch_kexec_protect_crashkres(void)
265{
266 int i;
267
268 kexec_segment_flush(kexec_crash_image);
269
270 for (i = 0; i < kexec_crash_image->nr_segments; i++)
271 set_memory_valid(
272 __phys_to_virt(kexec_crash_image->segment[i].mem),
273 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
274}
275
276void arch_kexec_unprotect_crashkres(void)
277{
278 int i;
279
280 for (i = 0; i < kexec_crash_image->nr_segments; i++)
281 set_memory_valid(
282 __phys_to_virt(kexec_crash_image->segment[i].mem),
283 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
284}
285
286#ifdef CONFIG_HIBERNATION
287
288
289
290
291void crash_prepare_suspend(void)
292{
293 if (kexec_crash_image)
294 arch_kexec_unprotect_crashkres();
295}
296
297void crash_post_resume(void)
298{
299 if (kexec_crash_image)
300 arch_kexec_protect_crashkres();
301}
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317bool crash_is_nosave(unsigned long pfn)
318{
319 int i;
320 phys_addr_t addr;
321
322 if (!crashk_res.end)
323 return false;
324
325
326 addr = __pfn_to_phys(pfn);
327 if ((addr < crashk_res.start) || (crashk_res.end < addr))
328 return false;
329
330 if (!kexec_crash_image)
331 return true;
332
333
334 for (i = 0; i < kexec_crash_image->nr_segments; i++)
335 if (addr >= kexec_crash_image->segment[i].mem &&
336 addr < (kexec_crash_image->segment[i].mem +
337 kexec_crash_image->segment[i].memsz))
338 return false;
339
340 return true;
341}
342
343void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
344{
345 unsigned long addr;
346 struct page *page;
347
348 for (addr = begin; addr < end; addr += PAGE_SIZE) {
349 page = phys_to_page(addr);
350 ClearPageReserved(page);
351 free_reserved_page(page);
352 }
353}
354#endif
355
356void arch_crash_save_vmcoreinfo(void)
357{
358 VMCOREINFO_NUMBER(VA_BITS);
359
360 vmcoreinfo_append_str("NUMBER(kimage_voffset)=0x%llx\n",
361 kimage_voffset);
362 vmcoreinfo_append_str("NUMBER(PHYS_OFFSET)=0x%llx\n",
363 PHYS_OFFSET);
364}
365