This source file includes following definitions.
- _kexec_image_info
- machine_kexec_cleanup
- machine_kexec_prepare
- kexec_list_flush
- kexec_segment_flush
- machine_kexec
- machine_kexec_mask_interrupts
- machine_crash_shutdown
- arch_kexec_protect_crashkres
- arch_kexec_unprotect_crashkres
- crash_prepare_suspend
- crash_post_resume
- crash_is_nosave
- crash_free_reserved_phys_range
1
2
3
4
5
6
7
8
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/kernel.h>
12 #include <linux/kexec.h>
13 #include <linux/page-flags.h>
14 #include <linux/smp.h>
15
16 #include <asm/cacheflush.h>
17 #include <asm/cpu_ops.h>
18 #include <asm/daifflags.h>
19 #include <asm/memory.h>
20 #include <asm/mmu.h>
21 #include <asm/mmu_context.h>
22 #include <asm/page.h>
23
24 #include "cpu-reset.h"
25
26
27 extern const unsigned char arm64_relocate_new_kernel[];
28 extern const unsigned long arm64_relocate_new_kernel_size;
29
30
31
32
33 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
34 static void _kexec_image_info(const char *func, int line,
35 const struct kimage *kimage)
36 {
37 unsigned long i;
38
39 pr_debug("%s:%d:\n", func, line);
40 pr_debug(" kexec kimage info:\n");
41 pr_debug(" type: %d\n", kimage->type);
42 pr_debug(" start: %lx\n", kimage->start);
43 pr_debug(" head: %lx\n", kimage->head);
44 pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
45
46 for (i = 0; i < kimage->nr_segments; i++) {
47 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
48 i,
49 kimage->segment[i].mem,
50 kimage->segment[i].mem + kimage->segment[i].memsz,
51 kimage->segment[i].memsz,
52 kimage->segment[i].memsz / PAGE_SIZE);
53 }
54 }
55
56 void machine_kexec_cleanup(struct kimage *kimage)
57 {
58
59 }
60
61
62
63
64
65
66
67
68 int machine_kexec_prepare(struct kimage *kimage)
69 {
70 kexec_image_info(kimage);
71
72 if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
73 pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
74 return -EBUSY;
75 }
76
77 return 0;
78 }
79
80
81
82
83 static void kexec_list_flush(struct kimage *kimage)
84 {
85 kimage_entry_t *entry;
86
87 for (entry = &kimage->head; ; entry++) {
88 unsigned int flag;
89 void *addr;
90
91
92 __flush_dcache_area(entry, sizeof(kimage_entry_t));
93
94 flag = *entry & IND_FLAGS;
95 if (flag == IND_DONE)
96 break;
97
98 addr = phys_to_virt(*entry & PAGE_MASK);
99
100 switch (flag) {
101 case IND_INDIRECTION:
102
103 entry = (kimage_entry_t *)addr - 1;
104 break;
105 case IND_SOURCE:
106
107 __flush_dcache_area(addr, PAGE_SIZE);
108 break;
109 case IND_DESTINATION:
110 break;
111 default:
112 BUG();
113 }
114 }
115 }
116
117
118
119
120 static void kexec_segment_flush(const struct kimage *kimage)
121 {
122 unsigned long i;
123
124 pr_debug("%s:\n", __func__);
125
126 for (i = 0; i < kimage->nr_segments; i++) {
127 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
128 i,
129 kimage->segment[i].mem,
130 kimage->segment[i].mem + kimage->segment[i].memsz,
131 kimage->segment[i].memsz,
132 kimage->segment[i].memsz / PAGE_SIZE);
133
134 __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
135 kimage->segment[i].memsz);
136 }
137 }
138
139
140
141
142
143
144 void machine_kexec(struct kimage *kimage)
145 {
146 phys_addr_t reboot_code_buffer_phys;
147 void *reboot_code_buffer;
148 bool in_kexec_crash = (kimage == kexec_crash_image);
149 bool stuck_cpus = cpus_are_stuck_in_kernel();
150
151
152
153
154 BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
155 WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
156 "Some CPUs may be stale, kdump will be unreliable.\n");
157
158 reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
159 reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
160
161 kexec_image_info(kimage);
162
163 pr_debug("%s:%d: control_code_page: %p\n", __func__, __LINE__,
164 kimage->control_code_page);
165 pr_debug("%s:%d: reboot_code_buffer_phys: %pa\n", __func__, __LINE__,
166 &reboot_code_buffer_phys);
167 pr_debug("%s:%d: reboot_code_buffer: %p\n", __func__, __LINE__,
168 reboot_code_buffer);
169 pr_debug("%s:%d: relocate_new_kernel: %p\n", __func__, __LINE__,
170 arm64_relocate_new_kernel);
171 pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
172 __func__, __LINE__, arm64_relocate_new_kernel_size,
173 arm64_relocate_new_kernel_size);
174
175
176
177
178
179 memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
180 arm64_relocate_new_kernel_size);
181
182
183 __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
184
185
186
187
188
189
190
191 __flush_icache_range((uintptr_t)reboot_code_buffer,
192 (uintptr_t)reboot_code_buffer +
193 arm64_relocate_new_kernel_size);
194
195
196 kexec_list_flush(kimage);
197
198
199 if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
200 kexec_segment_flush(kimage);
201
202 pr_info("Bye!\n");
203
204 local_daif_mask();
205
206
207
208
209
210
211
212
213
214
215
216
217
218 cpu_soft_restart(reboot_code_buffer_phys, kimage->head, kimage->start,
219 #ifdef CONFIG_KEXEC_FILE
220 kimage->arch.dtb_mem);
221 #else
222 0);
223 #endif
224
225 BUG();
226 }
227
228 static void machine_kexec_mask_interrupts(void)
229 {
230 unsigned int i;
231 struct irq_desc *desc;
232
233 for_each_irq_desc(i, desc) {
234 struct irq_chip *chip;
235 int ret;
236
237 chip = irq_desc_get_chip(desc);
238 if (!chip)
239 continue;
240
241
242
243
244
245 ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
246
247 if (ret && irqd_irq_inprogress(&desc->irq_data) &&
248 chip->irq_eoi)
249 chip->irq_eoi(&desc->irq_data);
250
251 if (chip->irq_mask)
252 chip->irq_mask(&desc->irq_data);
253
254 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
255 chip->irq_disable(&desc->irq_data);
256 }
257 }
258
259
260
261
262 void machine_crash_shutdown(struct pt_regs *regs)
263 {
264 local_irq_disable();
265
266
267 crash_smp_send_stop();
268
269
270 crash_save_cpu(regs, smp_processor_id());
271 machine_kexec_mask_interrupts();
272
273 pr_info("Starting crashdump kernel...\n");
274 }
275
276 void arch_kexec_protect_crashkres(void)
277 {
278 int i;
279
280 kexec_segment_flush(kexec_crash_image);
281
282 for (i = 0; i < kexec_crash_image->nr_segments; i++)
283 set_memory_valid(
284 __phys_to_virt(kexec_crash_image->segment[i].mem),
285 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
286 }
287
288 void arch_kexec_unprotect_crashkres(void)
289 {
290 int i;
291
292 for (i = 0; i < kexec_crash_image->nr_segments; i++)
293 set_memory_valid(
294 __phys_to_virt(kexec_crash_image->segment[i].mem),
295 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
296 }
297
298 #ifdef CONFIG_HIBERNATION
299
300
301
302
303 void crash_prepare_suspend(void)
304 {
305 if (kexec_crash_image)
306 arch_kexec_unprotect_crashkres();
307 }
308
309 void crash_post_resume(void)
310 {
311 if (kexec_crash_image)
312 arch_kexec_protect_crashkres();
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 bool crash_is_nosave(unsigned long pfn)
330 {
331 int i;
332 phys_addr_t addr;
333
334 if (!crashk_res.end)
335 return false;
336
337
338 addr = __pfn_to_phys(pfn);
339 if ((addr < crashk_res.start) || (crashk_res.end < addr))
340 return false;
341
342 if (!kexec_crash_image)
343 return true;
344
345
346 for (i = 0; i < kexec_crash_image->nr_segments; i++)
347 if (addr >= kexec_crash_image->segment[i].mem &&
348 addr < (kexec_crash_image->segment[i].mem +
349 kexec_crash_image->segment[i].memsz))
350 return false;
351
352 return true;
353 }
354
355 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
356 {
357 unsigned long addr;
358 struct page *page;
359
360 for (addr = begin; addr < end; addr += PAGE_SIZE) {
361 page = phys_to_page(addr);
362 free_reserved_page(page);
363 }
364 }
365 #endif