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