OSDN Git Service

x86: kexec_file: lift CRASH_MAX_RANGES limit on crash_mem buffer
[uclinux-h8/linux.git] / arch / x86 / kernel / crash.c
1 /*
2  * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
3  *
4  * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
5  *
6  * Copyright (C) IBM Corporation, 2004. All rights reserved.
7  * Copyright (C) Red Hat Inc., 2014. All rights reserved.
8  * Authors:
9  *      Vivek Goyal <vgoyal@redhat.com>
10  *
11  */
12
13 #define pr_fmt(fmt)     "kexec: " fmt
14
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/smp.h>
18 #include <linux/reboot.h>
19 #include <linux/kexec.h>
20 #include <linux/delay.h>
21 #include <linux/elf.h>
22 #include <linux/elfcore.h>
23 #include <linux/export.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26
27 #include <asm/processor.h>
28 #include <asm/hardirq.h>
29 #include <asm/nmi.h>
30 #include <asm/hw_irq.h>
31 #include <asm/apic.h>
32 #include <asm/e820/types.h>
33 #include <asm/io_apic.h>
34 #include <asm/hpet.h>
35 #include <linux/kdebug.h>
36 #include <asm/cpu.h>
37 #include <asm/reboot.h>
38 #include <asm/virtext.h>
39 #include <asm/intel_pt.h>
40
41 /* Alignment required for elf header segment */
42 #define ELF_CORE_HEADER_ALIGN   4096
43
44 struct crash_mem_range {
45         u64 start, end;
46 };
47
48 struct crash_mem {
49         unsigned int max_nr_ranges;
50         unsigned int nr_ranges;
51         struct crash_mem_range ranges[0];
52 };
53
54 /* Used while preparing memory map entries for second kernel */
55 struct crash_memmap_data {
56         struct boot_params *params;
57         /* Type of memory */
58         unsigned int type;
59 };
60
61 /*
62  * This is used to VMCLEAR all VMCSs loaded on the
63  * processor. And when loading kvm_intel module, the
64  * callback function pointer will be assigned.
65  *
66  * protected by rcu.
67  */
68 crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss = NULL;
69 EXPORT_SYMBOL_GPL(crash_vmclear_loaded_vmcss);
70 unsigned long crash_zero_bytes;
71
72 static inline void cpu_crash_vmclear_loaded_vmcss(void)
73 {
74         crash_vmclear_fn *do_vmclear_operation = NULL;
75
76         rcu_read_lock();
77         do_vmclear_operation = rcu_dereference(crash_vmclear_loaded_vmcss);
78         if (do_vmclear_operation)
79                 do_vmclear_operation();
80         rcu_read_unlock();
81 }
82
83 #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
84
85 static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
86 {
87 #ifdef CONFIG_X86_32
88         struct pt_regs fixed_regs;
89
90         if (!user_mode(regs)) {
91                 crash_fixup_ss_esp(&fixed_regs, regs);
92                 regs = &fixed_regs;
93         }
94 #endif
95         crash_save_cpu(regs, cpu);
96
97         /*
98          * VMCLEAR VMCSs loaded on all cpus if needed.
99          */
100         cpu_crash_vmclear_loaded_vmcss();
101
102         /* Disable VMX or SVM if needed.
103          *
104          * We need to disable virtualization on all CPUs.
105          * Having VMX or SVM enabled on any CPU may break rebooting
106          * after the kdump kernel has finished its task.
107          */
108         cpu_emergency_vmxoff();
109         cpu_emergency_svm_disable();
110
111         /*
112          * Disable Intel PT to stop its logging
113          */
114         cpu_emergency_stop_pt();
115
116         disable_local_APIC();
117 }
118
119 void kdump_nmi_shootdown_cpus(void)
120 {
121         nmi_shootdown_cpus(kdump_nmi_callback);
122
123         disable_local_APIC();
124 }
125
126 /* Override the weak function in kernel/panic.c */
127 void crash_smp_send_stop(void)
128 {
129         static int cpus_stopped;
130
131         if (cpus_stopped)
132                 return;
133
134         if (smp_ops.crash_stop_other_cpus)
135                 smp_ops.crash_stop_other_cpus();
136         else
137                 smp_send_stop();
138
139         cpus_stopped = 1;
140 }
141
142 #else
143 void crash_smp_send_stop(void)
144 {
145         /* There are no cpus to shootdown */
146 }
147 #endif
148
149 void native_machine_crash_shutdown(struct pt_regs *regs)
150 {
151         /* This function is only called after the system
152          * has panicked or is otherwise in a critical state.
153          * The minimum amount of code to allow a kexec'd kernel
154          * to run successfully needs to happen here.
155          *
156          * In practice this means shooting down the other cpus in
157          * an SMP system.
158          */
159         /* The kernel is broken so disable interrupts */
160         local_irq_disable();
161
162         crash_smp_send_stop();
163
164         /*
165          * VMCLEAR VMCSs loaded on this cpu if needed.
166          */
167         cpu_crash_vmclear_loaded_vmcss();
168
169         /* Booting kdump kernel with VMX or SVM enabled won't work,
170          * because (among other limitations) we can't disable paging
171          * with the virt flags.
172          */
173         cpu_emergency_vmxoff();
174         cpu_emergency_svm_disable();
175
176         /*
177          * Disable Intel PT to stop its logging
178          */
179         cpu_emergency_stop_pt();
180
181 #ifdef CONFIG_X86_IO_APIC
182         /* Prevent crash_kexec() from deadlocking on ioapic_lock. */
183         ioapic_zap_locks();
184         clear_IO_APIC();
185 #endif
186         lapic_shutdown();
187         restore_boot_irq_mode();
188 #ifdef CONFIG_HPET_TIMER
189         hpet_disable();
190 #endif
191         crash_save_cpu(regs, safe_smp_processor_id());
192 }
193
194 #ifdef CONFIG_KEXEC_FILE
195 static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
196 {
197         unsigned int *nr_ranges = arg;
198
199         (*nr_ranges)++;
200         return 0;
201 }
202
203 /* Gather all the required information to prepare elf headers for ram regions */
204 static struct crash_mem *fill_up_crash_elf_data(void)
205 {
206         unsigned int nr_ranges = 0;
207         struct crash_mem *cmem;
208
209         walk_system_ram_res(0, -1, &nr_ranges,
210                                 get_nr_ram_ranges_callback);
211         if (!nr_ranges)
212                 return NULL;
213
214         /*
215          * Exclusion of crash region and/or crashk_low_res may cause
216          * another range split. So add extra two slots here.
217          */
218         nr_ranges += 2;
219         cmem = vzalloc(sizeof(struct crash_mem) +
220                         sizeof(struct crash_mem_range) * nr_ranges);
221         if (!cmem)
222                 return NULL;
223
224         cmem->max_nr_ranges = nr_ranges;
225         cmem->nr_ranges = 0;
226
227         return cmem;
228 }
229
230 static int exclude_mem_range(struct crash_mem *mem,
231                 unsigned long long mstart, unsigned long long mend)
232 {
233         int i, j;
234         unsigned long long start, end;
235         struct crash_mem_range temp_range = {0, 0};
236
237         for (i = 0; i < mem->nr_ranges; i++) {
238                 start = mem->ranges[i].start;
239                 end = mem->ranges[i].end;
240
241                 if (mstart > end || mend < start)
242                         continue;
243
244                 /* Truncate any area outside of range */
245                 if (mstart < start)
246                         mstart = start;
247                 if (mend > end)
248                         mend = end;
249
250                 /* Found completely overlapping range */
251                 if (mstart == start && mend == end) {
252                         mem->ranges[i].start = 0;
253                         mem->ranges[i].end = 0;
254                         if (i < mem->nr_ranges - 1) {
255                                 /* Shift rest of the ranges to left */
256                                 for (j = i; j < mem->nr_ranges - 1; j++) {
257                                         mem->ranges[j].start =
258                                                 mem->ranges[j+1].start;
259                                         mem->ranges[j].end =
260                                                         mem->ranges[j+1].end;
261                                 }
262                         }
263                         mem->nr_ranges--;
264                         return 0;
265                 }
266
267                 if (mstart > start && mend < end) {
268                         /* Split original range */
269                         mem->ranges[i].end = mstart - 1;
270                         temp_range.start = mend + 1;
271                         temp_range.end = end;
272                 } else if (mstart != start)
273                         mem->ranges[i].end = mstart - 1;
274                 else
275                         mem->ranges[i].start = mend + 1;
276                 break;
277         }
278
279         /* If a split happend, add the split to array */
280         if (!temp_range.end)
281                 return 0;
282
283         /* Split happened */
284         if (i == mem->max_nr_ranges - 1)
285                 return -ENOMEM;
286
287         /* Location where new range should go */
288         j = i + 1;
289         if (j < mem->nr_ranges) {
290                 /* Move over all ranges one slot towards the end */
291                 for (i = mem->nr_ranges - 1; i >= j; i--)
292                         mem->ranges[i + 1] = mem->ranges[i];
293         }
294
295         mem->ranges[j].start = temp_range.start;
296         mem->ranges[j].end = temp_range.end;
297         mem->nr_ranges++;
298         return 0;
299 }
300
301 /*
302  * Look for any unwanted ranges between mstart, mend and remove them. This
303  * might lead to split and split ranges are put in cmem->ranges[] array
304  */
305 static int elf_header_exclude_ranges(struct crash_mem *cmem)
306 {
307         int ret = 0;
308
309         /* Exclude crashkernel region */
310         ret = exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
311         if (ret)
312                 return ret;
313
314         if (crashk_low_res.end) {
315                 ret = exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end);
316                 if (ret)
317                         return ret;
318         }
319
320         return ret;
321 }
322
323 static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
324 {
325         struct crash_mem *cmem = arg;
326
327         cmem->ranges[cmem->nr_ranges].start = res->start;
328         cmem->ranges[cmem->nr_ranges].end = res->end;
329         cmem->nr_ranges++;
330
331         return 0;
332 }
333
334 static int prepare_elf64_headers(struct crash_mem *cmem, bool kernel_map,
335                 void **addr, unsigned long *sz)
336 {
337         Elf64_Ehdr *ehdr;
338         Elf64_Phdr *phdr;
339         unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
340         unsigned char *buf, *bufp;
341         unsigned int cpu, i;
342         unsigned long long notes_addr;
343         unsigned long mstart, mend;
344
345         /* extra phdr for vmcoreinfo elf note */
346         nr_phdr = nr_cpus + 1;
347         nr_phdr += cmem->nr_ranges;
348
349         /*
350          * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
351          * area on x86_64 (ffffffff80000000 - ffffffffa0000000).
352          * I think this is required by tools like gdb. So same physical
353          * memory will be mapped in two elf headers. One will contain kernel
354          * text virtual addresses and other will have __va(physical) addresses.
355          */
356
357         nr_phdr++;
358         elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
359         elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
360
361         buf = vzalloc(elf_sz);
362         if (!buf)
363                 return -ENOMEM;
364
365         bufp = buf;
366         ehdr = (Elf64_Ehdr *)bufp;
367         bufp += sizeof(Elf64_Ehdr);
368         memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
369         ehdr->e_ident[EI_CLASS] = ELFCLASS64;
370         ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
371         ehdr->e_ident[EI_VERSION] = EV_CURRENT;
372         ehdr->e_ident[EI_OSABI] = ELF_OSABI;
373         memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
374         ehdr->e_type = ET_CORE;
375         ehdr->e_machine = ELF_ARCH;
376         ehdr->e_version = EV_CURRENT;
377         ehdr->e_phoff = sizeof(Elf64_Ehdr);
378         ehdr->e_ehsize = sizeof(Elf64_Ehdr);
379         ehdr->e_phentsize = sizeof(Elf64_Phdr);
380
381         /* Prepare one phdr of type PT_NOTE for each present cpu */
382         for_each_present_cpu(cpu) {
383                 phdr = (Elf64_Phdr *)bufp;
384                 bufp += sizeof(Elf64_Phdr);
385                 phdr->p_type = PT_NOTE;
386                 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
387                 phdr->p_offset = phdr->p_paddr = notes_addr;
388                 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
389                 (ehdr->e_phnum)++;
390         }
391
392         /* Prepare one PT_NOTE header for vmcoreinfo */
393         phdr = (Elf64_Phdr *)bufp;
394         bufp += sizeof(Elf64_Phdr);
395         phdr->p_type = PT_NOTE;
396         phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
397         phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
398         (ehdr->e_phnum)++;
399
400         /* Prepare PT_LOAD type program header for kernel text region */
401         if (kernel_map) {
402                 phdr = (Elf64_Phdr *)bufp;
403                 bufp += sizeof(Elf64_Phdr);
404                 phdr->p_type = PT_LOAD;
405                 phdr->p_flags = PF_R|PF_W|PF_X;
406                 phdr->p_vaddr = (Elf64_Addr)_text;
407                 phdr->p_filesz = phdr->p_memsz = _end - _text;
408                 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
409                 (ehdr->e_phnum)++;
410         }
411
412         /* Go through all the ranges in cmem->ranges[] and prepare phdr */
413         for (i = 0; i < cmem->nr_ranges; i++) {
414                 mstart = cmem->ranges[i].start;
415                 mend = cmem->ranges[i].end;
416
417                 phdr->p_type = PT_LOAD;
418                 phdr->p_flags = PF_R|PF_W|PF_X;
419                 phdr->p_offset  = mstart;
420
421                 phdr->p_paddr = mstart;
422                 phdr->p_vaddr = (unsigned long long) __va(mstart);
423                 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
424                 phdr->p_align = 0;
425                 ehdr->e_phnum++;
426                 phdr++;
427                 pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
428                         phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
429                         ehdr->e_phnum, phdr->p_offset);
430         }
431
432         *addr = buf;
433         *sz = elf_sz;
434         return 0;
435 }
436
437 /* Prepare elf headers. Return addr and size */
438 static int prepare_elf_headers(struct kimage *image, void **addr,
439                                         unsigned long *sz)
440 {
441         struct crash_mem *cmem;
442         Elf64_Ehdr *ehdr;
443         Elf64_Phdr *phdr;
444         int ret, i;
445
446         cmem = fill_up_crash_elf_data();
447         if (!cmem)
448                 return -ENOMEM;
449
450         ret = walk_system_ram_res(0, -1, cmem,
451                                 prepare_elf64_ram_headers_callback);
452         if (ret)
453                 goto out;
454
455         /* Exclude unwanted mem ranges */
456         ret = elf_header_exclude_ranges(cmem);
457         if (ret)
458                 goto out;
459
460         /* By default prepare 64bit headers */
461         ret =  prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz);
462         if (ret)
463                 goto out;
464
465         /*
466          * If a range matches backup region, adjust offset to backup
467          * segment.
468          */
469         ehdr = (Elf64_Ehdr *)*addr;
470         phdr = (Elf64_Phdr *)(ehdr + 1);
471         for (i = 0; i < ehdr->e_phnum; phdr++, i++)
472                 if (phdr->p_type == PT_LOAD &&
473                                 phdr->p_paddr == image->arch.backup_src_start &&
474                                 phdr->p_memsz == image->arch.backup_src_sz) {
475                         phdr->p_offset = image->arch.backup_load_addr;
476                         break;
477                 }
478 out:
479         vfree(cmem);
480         return ret;
481 }
482
483 static int add_e820_entry(struct boot_params *params, struct e820_entry *entry)
484 {
485         unsigned int nr_e820_entries;
486
487         nr_e820_entries = params->e820_entries;
488         if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE)
489                 return 1;
490
491         memcpy(&params->e820_table[nr_e820_entries], entry,
492                         sizeof(struct e820_entry));
493         params->e820_entries++;
494         return 0;
495 }
496
497 static int memmap_entry_callback(struct resource *res, void *arg)
498 {
499         struct crash_memmap_data *cmd = arg;
500         struct boot_params *params = cmd->params;
501         struct e820_entry ei;
502
503         ei.addr = res->start;
504         ei.size = resource_size(res);
505         ei.type = cmd->type;
506         add_e820_entry(params, &ei);
507
508         return 0;
509 }
510
511 static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
512                                  unsigned long long mstart,
513                                  unsigned long long mend)
514 {
515         unsigned long start, end;
516         int ret = 0;
517
518         cmem->ranges[0].start = mstart;
519         cmem->ranges[0].end = mend;
520         cmem->nr_ranges = 1;
521
522         /* Exclude Backup region */
523         start = image->arch.backup_load_addr;
524         end = start + image->arch.backup_src_sz - 1;
525         ret = exclude_mem_range(cmem, start, end);
526         if (ret)
527                 return ret;
528
529         /* Exclude elf header region */
530         start = image->arch.elf_load_addr;
531         end = start + image->arch.elf_headers_sz - 1;
532         return exclude_mem_range(cmem, start, end);
533 }
534
535 /* Prepare memory map for crash dump kernel */
536 int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
537 {
538         int i, ret = 0;
539         unsigned long flags;
540         struct e820_entry ei;
541         struct crash_memmap_data cmd;
542         struct crash_mem *cmem;
543
544         cmem = vzalloc(sizeof(struct crash_mem));
545         if (!cmem)
546                 return -ENOMEM;
547
548         memset(&cmd, 0, sizeof(struct crash_memmap_data));
549         cmd.params = params;
550
551         /* Add first 640K segment */
552         ei.addr = image->arch.backup_src_start;
553         ei.size = image->arch.backup_src_sz;
554         ei.type = E820_TYPE_RAM;
555         add_e820_entry(params, &ei);
556
557         /* Add ACPI tables */
558         cmd.type = E820_TYPE_ACPI;
559         flags = IORESOURCE_MEM | IORESOURCE_BUSY;
560         walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd,
561                        memmap_entry_callback);
562
563         /* Add ACPI Non-volatile Storage */
564         cmd.type = E820_TYPE_NVS;
565         walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
566                         memmap_entry_callback);
567
568         /* Add crashk_low_res region */
569         if (crashk_low_res.end) {
570                 ei.addr = crashk_low_res.start;
571                 ei.size = crashk_low_res.end - crashk_low_res.start + 1;
572                 ei.type = E820_TYPE_RAM;
573                 add_e820_entry(params, &ei);
574         }
575
576         /* Exclude some ranges from crashk_res and add rest to memmap */
577         ret = memmap_exclude_ranges(image, cmem, crashk_res.start,
578                                                 crashk_res.end);
579         if (ret)
580                 goto out;
581
582         for (i = 0; i < cmem->nr_ranges; i++) {
583                 ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
584
585                 /* If entry is less than a page, skip it */
586                 if (ei.size < PAGE_SIZE)
587                         continue;
588                 ei.addr = cmem->ranges[i].start;
589                 ei.type = E820_TYPE_RAM;
590                 add_e820_entry(params, &ei);
591         }
592
593 out:
594         vfree(cmem);
595         return ret;
596 }
597
598 static int determine_backup_region(struct resource *res, void *arg)
599 {
600         struct kimage *image = arg;
601
602         image->arch.backup_src_start = res->start;
603         image->arch.backup_src_sz = resource_size(res);
604
605         /* Expecting only one range for backup region */
606         return 1;
607 }
608
609 int crash_load_segments(struct kimage *image)
610 {
611         int ret;
612         struct kexec_buf kbuf = { .image = image, .buf_min = 0,
613                                   .buf_max = ULONG_MAX, .top_down = false };
614
615         /*
616          * Determine and load a segment for backup area. First 640K RAM
617          * region is backup source
618          */
619
620         ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
621                                 image, determine_backup_region);
622
623         /* Zero or postive return values are ok */
624         if (ret < 0)
625                 return ret;
626
627         /* Add backup segment. */
628         if (image->arch.backup_src_sz) {
629                 kbuf.buffer = &crash_zero_bytes;
630                 kbuf.bufsz = sizeof(crash_zero_bytes);
631                 kbuf.memsz = image->arch.backup_src_sz;
632                 kbuf.buf_align = PAGE_SIZE;
633                 /*
634                  * Ideally there is no source for backup segment. This is
635                  * copied in purgatory after crash. Just add a zero filled
636                  * segment for now to make sure checksum logic works fine.
637                  */
638                 ret = kexec_add_buffer(&kbuf);
639                 if (ret)
640                         return ret;
641                 image->arch.backup_load_addr = kbuf.mem;
642                 pr_debug("Loaded backup region at 0x%lx backup_start=0x%lx memsz=0x%lx\n",
643                          image->arch.backup_load_addr,
644                          image->arch.backup_src_start, kbuf.memsz);
645         }
646
647         /* Prepare elf headers and add a segment */
648         ret = prepare_elf_headers(image, &kbuf.buffer, &kbuf.bufsz);
649         if (ret)
650                 return ret;
651
652         image->arch.elf_headers = kbuf.buffer;
653         image->arch.elf_headers_sz = kbuf.bufsz;
654
655         kbuf.memsz = kbuf.bufsz;
656         kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
657         ret = kexec_add_buffer(&kbuf);
658         if (ret) {
659                 vfree((void *)image->arch.elf_headers);
660                 return ret;
661         }
662         image->arch.elf_load_addr = kbuf.mem;
663         pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
664                  image->arch.elf_load_addr, kbuf.bufsz, kbuf.bufsz);
665
666         return ret;
667 }
668 #endif /* CONFIG_KEXEC_FILE */