2 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3 * dump with assistance from firmware. This approach does not use kexec,
4 * instead firmware assists in booting the kdump kernel while preserving
5 * memory contents. The most of the code implementation has been adapted
6 * from phyp assisted dump implementation written by Linas Vepstas and
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright 2011 IBM Corporation
24 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
28 #define pr_fmt(fmt) "fadump: " fmt
30 #include <linux/string.h>
31 #include <linux/memblock.h>
32 #include <linux/delay.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/crash_dump.h>
40 #include <asm/fadump.h>
42 static struct fw_dump fw_dump;
43 static struct fadump_mem_struct fdm;
44 static const struct fadump_mem_struct *fdm_active;
46 static DEFINE_MUTEX(fadump_mutex);
47 struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
50 /* Scan the Firmware Assisted dump configuration details. */
51 int __init early_init_dt_scan_fw_dump(unsigned long node,
52 const char *uname, int depth, void *data)
59 if (depth != 1 || strcmp(uname, "rtas") != 0)
63 * Check if Firmware Assisted dump is supported. if yes, check
64 * if dump has been initiated on last reboot.
66 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
70 fw_dump.fadump_supported = 1;
71 fw_dump.ibm_configure_kernel_dump = *token;
74 * The 'ibm,kernel-dump' rtas node is present only if there is
75 * dump data waiting for us.
77 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
79 fw_dump.dump_active = 1;
81 /* Get the sizes required to store dump data for the firmware provided
83 * For each dump section type supported, a 32bit cell which defines
84 * the ID of a supported section followed by two 32 bit cells which
85 * gives teh size of the section in bytes.
87 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
93 num_sections = size / (3 * sizeof(u32));
95 for (i = 0; i < num_sections; i++, sections += 3) {
96 u32 type = (u32)of_read_number(sections, 1);
99 case FADUMP_CPU_STATE_DATA:
100 fw_dump.cpu_state_data_size =
101 of_read_ulong(§ions[1], 2);
103 case FADUMP_HPTE_REGION:
104 fw_dump.hpte_region_size =
105 of_read_ulong(§ions[1], 2);
112 int is_fadump_active(void)
114 return fw_dump.dump_active;
117 /* Print firmware assisted dump configurations for debugging purpose. */
118 static void fadump_show_config(void)
120 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
121 (fw_dump.fadump_supported ? "present" : "no support"));
123 if (!fw_dump.fadump_supported)
126 pr_debug("Fadump enabled : %s\n",
127 (fw_dump.fadump_enabled ? "yes" : "no"));
128 pr_debug("Dump Active : %s\n",
129 (fw_dump.dump_active ? "yes" : "no"));
130 pr_debug("Dump section sizes:\n");
131 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
132 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
133 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
136 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
142 memset(fdm, 0, sizeof(struct fadump_mem_struct));
143 addr = addr & PAGE_MASK;
145 fdm->header.dump_format_version = 0x00000001;
146 fdm->header.dump_num_sections = 3;
147 fdm->header.dump_status_flag = 0;
148 fdm->header.offset_first_dump_section =
149 (u32)offsetof(struct fadump_mem_struct, cpu_state_data);
152 * Fields for disk dump option.
153 * We are not using disk dump option, hence set these fields to 0.
155 fdm->header.dd_block_size = 0;
156 fdm->header.dd_block_offset = 0;
157 fdm->header.dd_num_blocks = 0;
158 fdm->header.dd_offset_disk_path = 0;
160 /* set 0 to disable an automatic dump-reboot. */
161 fdm->header.max_time_auto = 0;
163 /* Kernel dump sections */
164 /* cpu state data section. */
165 fdm->cpu_state_data.request_flag = FADUMP_REQUEST_FLAG;
166 fdm->cpu_state_data.source_data_type = FADUMP_CPU_STATE_DATA;
167 fdm->cpu_state_data.source_address = 0;
168 fdm->cpu_state_data.source_len = fw_dump.cpu_state_data_size;
169 fdm->cpu_state_data.destination_address = addr;
170 addr += fw_dump.cpu_state_data_size;
172 /* hpte region section */
173 fdm->hpte_region.request_flag = FADUMP_REQUEST_FLAG;
174 fdm->hpte_region.source_data_type = FADUMP_HPTE_REGION;
175 fdm->hpte_region.source_address = 0;
176 fdm->hpte_region.source_len = fw_dump.hpte_region_size;
177 fdm->hpte_region.destination_address = addr;
178 addr += fw_dump.hpte_region_size;
180 /* RMA region section */
181 fdm->rmr_region.request_flag = FADUMP_REQUEST_FLAG;
182 fdm->rmr_region.source_data_type = FADUMP_REAL_MODE_REGION;
183 fdm->rmr_region.source_address = RMA_START;
184 fdm->rmr_region.source_len = fw_dump.boot_memory_size;
185 fdm->rmr_region.destination_address = addr;
186 addr += fw_dump.boot_memory_size;
192 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
194 * Function to find the largest memory size we need to reserve during early
195 * boot process. This will be the size of the memory that is required for a
196 * kernel to boot successfully.
198 * This function has been taken from phyp-assisted dump feature implementation.
200 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
202 * TODO: Come up with better approach to find out more accurate memory size
203 * that is required for a kernel to boot successfully.
206 static inline unsigned long fadump_calculate_reserve_size(void)
211 * Check if the size is specified through fadump_reserve_mem= cmdline
212 * option. If yes, then use that.
214 if (fw_dump.reserve_bootvar)
215 return fw_dump.reserve_bootvar;
217 /* divide by 20 to get 5% of value */
218 size = memblock_end_of_DRAM() / 20;
220 /* round it down in multiples of 256 */
221 size = size & ~0x0FFFFFFFUL;
223 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
224 if (memory_limit && size > memory_limit)
227 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
231 * Calculate the total memory size required to be reserved for
232 * firmware-assisted dump registration.
234 static unsigned long get_fadump_area_size(void)
236 unsigned long size = 0;
238 size += fw_dump.cpu_state_data_size;
239 size += fw_dump.hpte_region_size;
240 size += fw_dump.boot_memory_size;
241 size += sizeof(struct fadump_crash_info_header);
242 size += sizeof(struct elfhdr); /* ELF core header.*/
243 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
244 /* Program headers for crash memory regions. */
245 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
247 size = PAGE_ALIGN(size);
251 int __init fadump_reserve_mem(void)
253 unsigned long base, size, memory_boundary;
255 if (!fw_dump.fadump_enabled)
258 if (!fw_dump.fadump_supported) {
259 printk(KERN_INFO "Firmware-assisted dump is not supported on"
261 fw_dump.fadump_enabled = 0;
265 * Initialize boot memory size
266 * If dump is active then we have already calculated the size during
270 fw_dump.boot_memory_size = fdm_active->rmr_region.source_len;
272 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
275 * Calculate the memory boundary.
276 * If memory_limit is less than actual memory boundary then reserve
277 * the memory for fadump beyond the memory_limit and adjust the
278 * memory_limit accordingly, so that the running kernel can run with
279 * specified memory_limit.
281 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
282 size = get_fadump_area_size();
283 if ((memory_limit + size) < memblock_end_of_DRAM())
284 memory_limit += size;
286 memory_limit = memblock_end_of_DRAM();
287 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
288 " dump, now %#016llx\n",
289 (unsigned long long)memory_limit);
292 memory_boundary = memory_limit;
294 memory_boundary = memblock_end_of_DRAM();
296 if (fw_dump.dump_active) {
297 printk(KERN_INFO "Firmware-assisted dump is active.\n");
299 * If last boot has crashed then reserve all the memory
300 * above boot_memory_size so that we don't touch it until
301 * dump is written to disk by userspace tool. This memory
302 * will be released for general use once the dump is saved.
304 base = fw_dump.boot_memory_size;
305 size = memory_boundary - base;
306 memblock_reserve(base, size);
307 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
308 "for saving crash dump\n",
309 (unsigned long)(size >> 20),
310 (unsigned long)(base >> 20));
312 fw_dump.fadumphdr_addr =
313 fdm_active->rmr_region.destination_address +
314 fdm_active->rmr_region.source_len;
315 pr_debug("fadumphdr_addr = %p\n",
316 (void *) fw_dump.fadumphdr_addr);
318 /* Reserve the memory at the top of memory. */
319 size = get_fadump_area_size();
320 base = memory_boundary - size;
321 memblock_reserve(base, size);
322 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
323 "for firmware-assisted dump\n",
324 (unsigned long)(size >> 20),
325 (unsigned long)(base >> 20));
327 fw_dump.reserve_dump_area_start = base;
328 fw_dump.reserve_dump_area_size = size;
332 /* Look for fadump= cmdline option. */
333 static int __init early_fadump_param(char *p)
338 if (strncmp(p, "on", 2) == 0)
339 fw_dump.fadump_enabled = 1;
340 else if (strncmp(p, "off", 3) == 0)
341 fw_dump.fadump_enabled = 0;
345 early_param("fadump", early_fadump_param);
347 /* Look for fadump_reserve_mem= cmdline option */
348 static int __init early_fadump_reserve_mem(char *p)
351 fw_dump.reserve_bootvar = memparse(p, &p);
354 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
356 static void register_fw_dump(struct fadump_mem_struct *fdm)
359 unsigned int wait_time;
361 pr_debug("Registering for firmware-assisted kernel dump...\n");
363 /* TODO: Add upper time limit for the delay */
365 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
366 FADUMP_REGISTER, fdm,
367 sizeof(struct fadump_mem_struct));
369 wait_time = rtas_busy_delay_time(rc);
377 printk(KERN_ERR "Failed to register firmware-assisted kernel"
378 " dump. Hardware Error(%d).\n", rc);
381 printk(KERN_ERR "Failed to register firmware-assisted kernel"
382 " dump. Parameter Error(%d).\n", rc);
385 printk(KERN_ERR "firmware-assisted kernel dump is already "
387 fw_dump.dump_registered = 1;
390 printk(KERN_INFO "firmware-assisted kernel dump registration"
392 fw_dump.dump_registered = 1;
397 void crash_fadump(struct pt_regs *regs, const char *str)
399 struct fadump_crash_info_header *fdh = NULL;
401 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
404 fdh = __va(fw_dump.fadumphdr_addr);
405 crashing_cpu = smp_processor_id();
406 fdh->crashing_cpu = crashing_cpu;
407 crash_save_vmcoreinfo();
412 ppc_save_regs(&fdh->regs);
414 fdh->cpu_online_mask = *cpu_online_mask;
416 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
417 rtas_os_term((char *)str);
420 #define GPR_MASK 0xffffff0000000000
421 static inline int fadump_gpr_index(u64 id)
426 if ((id & GPR_MASK) == REG_ID("GPR")) {
427 /* get the digits at the end */
432 str[0] = (id >> 8) & 0xff;
433 sscanf(str, "%d", &i);
440 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
445 i = fadump_gpr_index(reg_id);
447 regs->gpr[i] = (unsigned long)reg_val;
448 else if (reg_id == REG_ID("NIA"))
449 regs->nip = (unsigned long)reg_val;
450 else if (reg_id == REG_ID("MSR"))
451 regs->msr = (unsigned long)reg_val;
452 else if (reg_id == REG_ID("CTR"))
453 regs->ctr = (unsigned long)reg_val;
454 else if (reg_id == REG_ID("LR"))
455 regs->link = (unsigned long)reg_val;
456 else if (reg_id == REG_ID("XER"))
457 regs->xer = (unsigned long)reg_val;
458 else if (reg_id == REG_ID("CR"))
459 regs->ccr = (unsigned long)reg_val;
460 else if (reg_id == REG_ID("DAR"))
461 regs->dar = (unsigned long)reg_val;
462 else if (reg_id == REG_ID("DSISR"))
463 regs->dsisr = (unsigned long)reg_val;
466 static struct fadump_reg_entry*
467 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
469 memset(regs, 0, sizeof(struct pt_regs));
471 while (reg_entry->reg_id != REG_ID("CPUEND")) {
472 fadump_set_regval(regs, reg_entry->reg_id,
473 reg_entry->reg_value);
480 static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
481 void *data, size_t data_len)
483 struct elf_note note;
485 note.n_namesz = strlen(name) + 1;
486 note.n_descsz = data_len;
488 memcpy(buf, ¬e, sizeof(note));
489 buf += (sizeof(note) + 3)/4;
490 memcpy(buf, name, note.n_namesz);
491 buf += (note.n_namesz + 3)/4;
492 memcpy(buf, data, note.n_descsz);
493 buf += (note.n_descsz + 3)/4;
498 static void fadump_final_note(u32 *buf)
500 struct elf_note note;
505 memcpy(buf, ¬e, sizeof(note));
508 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
510 struct elf_prstatus prstatus;
512 memset(&prstatus, 0, sizeof(prstatus));
514 * FIXME: How do i get PID? Do I really need it?
515 * prstatus.pr_pid = ????
517 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
518 buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
519 &prstatus, sizeof(prstatus));
523 static void fadump_update_elfcore_header(char *bufp)
526 struct elf_phdr *phdr;
528 elf = (struct elfhdr *)bufp;
529 bufp += sizeof(struct elfhdr);
531 /* First note is a place holder for cpu notes info. */
532 phdr = (struct elf_phdr *)bufp;
534 if (phdr->p_type == PT_NOTE) {
535 phdr->p_paddr = fw_dump.cpu_notes_buf;
536 phdr->p_offset = phdr->p_paddr;
537 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
538 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
543 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
547 unsigned long order, count, i;
549 order = get_order(size);
550 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
555 page = virt_to_page(vaddr);
556 for (i = 0; i < count; i++)
557 SetPageReserved(page + i);
561 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
564 unsigned long order, count, i;
566 order = get_order(size);
568 page = virt_to_page(vaddr);
569 for (i = 0; i < count; i++)
570 ClearPageReserved(page + i);
571 __free_pages(page, order);
575 * Read CPU state dump data and convert it into ELF notes.
576 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
577 * used to access the data to allow for additional fields to be added without
578 * affecting compatibility. Each list of registers for a CPU starts with
579 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
580 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
581 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
582 * of register value. For more details refer to PAPR document.
584 * Only for the crashing cpu we ignore the CPU dump data and get exact
585 * state from fadump crash info structure populated by first kernel at the
588 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
590 struct fadump_reg_save_area_header *reg_header;
591 struct fadump_reg_entry *reg_entry;
592 struct fadump_crash_info_header *fdh = NULL;
595 u32 num_cpus, *note_buf;
597 int i, rc = 0, cpu = 0;
599 if (!fdm->cpu_state_data.bytes_dumped)
602 addr = fdm->cpu_state_data.destination_address;
606 if (reg_header->magic_number != REGSAVE_AREA_MAGIC) {
607 printk(KERN_ERR "Unable to read register save area.\n");
610 pr_debug("--------CPU State Data------------\n");
611 pr_debug("Magic Number: %llx\n", reg_header->magic_number);
612 pr_debug("NumCpuOffset: %x\n", reg_header->num_cpu_offset);
614 vaddr += reg_header->num_cpu_offset;
615 num_cpus = *((u32 *)(vaddr));
616 pr_debug("NumCpus : %u\n", num_cpus);
617 vaddr += sizeof(u32);
618 reg_entry = (struct fadump_reg_entry *)vaddr;
620 /* Allocate buffer to hold cpu crash notes. */
621 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
622 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
623 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
625 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
626 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
629 fw_dump.cpu_notes_buf = __pa(note_buf);
631 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
632 (num_cpus * sizeof(note_buf_t)), note_buf);
634 if (fw_dump.fadumphdr_addr)
635 fdh = __va(fw_dump.fadumphdr_addr);
637 for (i = 0; i < num_cpus; i++) {
638 if (reg_entry->reg_id != REG_ID("CPUSTRT")) {
639 printk(KERN_ERR "Unable to read CPU state data\n");
643 /* Lower 4 bytes of reg_value contains logical cpu id */
644 cpu = reg_entry->reg_value & FADUMP_CPU_ID_MASK;
645 if (!cpumask_test_cpu(cpu, &fdh->cpu_online_mask)) {
646 SKIP_TO_NEXT_CPU(reg_entry);
649 pr_debug("Reading register data for cpu %d...\n", cpu);
650 if (fdh && fdh->crashing_cpu == cpu) {
652 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
653 SKIP_TO_NEXT_CPU(reg_entry);
656 reg_entry = fadump_read_registers(reg_entry, ®s);
657 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
660 fadump_final_note(note_buf);
662 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
663 fdh->elfcorehdr_addr);
664 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
668 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
669 fw_dump.cpu_notes_buf_size);
670 fw_dump.cpu_notes_buf = 0;
671 fw_dump.cpu_notes_buf_size = 0;
677 * Validate and process the dump data stored by firmware before exporting
678 * it through '/proc/vmcore'.
680 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
682 struct fadump_crash_info_header *fdh;
685 if (!fdm_active || !fw_dump.fadumphdr_addr)
688 /* Check if the dump data is valid. */
689 if ((fdm_active->header.dump_status_flag == FADUMP_ERROR_FLAG) ||
690 (fdm_active->cpu_state_data.error_flags != 0) ||
691 (fdm_active->rmr_region.error_flags != 0)) {
692 printk(KERN_ERR "Dump taken by platform is not valid\n");
695 if ((fdm_active->rmr_region.bytes_dumped !=
696 fdm_active->rmr_region.source_len) ||
697 !fdm_active->cpu_state_data.bytes_dumped) {
698 printk(KERN_ERR "Dump taken by platform is incomplete\n");
702 /* Validate the fadump crash info header */
703 fdh = __va(fw_dump.fadumphdr_addr);
704 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
705 printk(KERN_ERR "Crash info header is not valid.\n");
709 rc = fadump_build_cpu_notes(fdm_active);
714 * We are done validating dump info and elfcore header is now ready
715 * to be exported. set elfcorehdr_addr so that vmcore module will
716 * export the elfcore header through '/proc/vmcore'.
718 elfcorehdr_addr = fdh->elfcorehdr_addr;
723 static inline void fadump_add_crash_memory(unsigned long long base,
724 unsigned long long end)
729 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
730 crash_mem_ranges, base, end - 1, (end - base));
731 crash_memory_ranges[crash_mem_ranges].base = base;
732 crash_memory_ranges[crash_mem_ranges].size = end - base;
736 static void fadump_exclude_reserved_area(unsigned long long start,
737 unsigned long long end)
739 unsigned long long ra_start, ra_end;
741 ra_start = fw_dump.reserve_dump_area_start;
742 ra_end = ra_start + fw_dump.reserve_dump_area_size;
744 if ((ra_start < end) && (ra_end > start)) {
745 if ((start < ra_start) && (end > ra_end)) {
746 fadump_add_crash_memory(start, ra_start);
747 fadump_add_crash_memory(ra_end, end);
748 } else if (start < ra_start) {
749 fadump_add_crash_memory(start, ra_start);
750 } else if (ra_end < end) {
751 fadump_add_crash_memory(ra_end, end);
754 fadump_add_crash_memory(start, end);
757 static int fadump_init_elfcore_header(char *bufp)
761 elf = (struct elfhdr *) bufp;
762 bufp += sizeof(struct elfhdr);
763 memcpy(elf->e_ident, ELFMAG, SELFMAG);
764 elf->e_ident[EI_CLASS] = ELF_CLASS;
765 elf->e_ident[EI_DATA] = ELF_DATA;
766 elf->e_ident[EI_VERSION] = EV_CURRENT;
767 elf->e_ident[EI_OSABI] = ELF_OSABI;
768 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
769 elf->e_type = ET_CORE;
770 elf->e_machine = ELF_ARCH;
771 elf->e_version = EV_CURRENT;
773 elf->e_phoff = sizeof(struct elfhdr);
775 elf->e_flags = ELF_CORE_EFLAGS;
776 elf->e_ehsize = sizeof(struct elfhdr);
777 elf->e_phentsize = sizeof(struct elf_phdr);
779 elf->e_shentsize = 0;
787 * Traverse through memblock structure and setup crash memory ranges. These
788 * ranges will be used create PT_LOAD program headers in elfcore header.
790 static void fadump_setup_crash_memory_ranges(void)
792 struct memblock_region *reg;
793 unsigned long long start, end;
795 pr_debug("Setup crash memory ranges.\n");
796 crash_mem_ranges = 0;
798 * add the first memory chunk (RMA_START through boot_memory_size) as
799 * a separate memory chunk. The reason is, at the time crash firmware
800 * will move the content of this memory chunk to different location
801 * specified during fadump registration. We need to create a separate
802 * program header for this chunk with the correct offset.
804 fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
806 for_each_memblock(memory, reg) {
807 start = (unsigned long long)reg->base;
808 end = start + (unsigned long long)reg->size;
809 if (start == RMA_START && end >= fw_dump.boot_memory_size)
810 start = fw_dump.boot_memory_size;
812 /* add this range excluding the reserved dump area. */
813 fadump_exclude_reserved_area(start, end);
818 * If the given physical address falls within the boot memory region then
819 * return the relocated address that points to the dump region reserved
820 * for saving initial boot memory contents.
822 static inline unsigned long fadump_relocate(unsigned long paddr)
824 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
825 return fdm.rmr_region.destination_address + paddr;
830 static int fadump_create_elfcore_headers(char *bufp)
833 struct elf_phdr *phdr;
836 fadump_init_elfcore_header(bufp);
837 elf = (struct elfhdr *)bufp;
838 bufp += sizeof(struct elfhdr);
841 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
842 * will be populated during second kernel boot after crash. Hence
843 * this PT_NOTE will always be the first elf note.
845 * NOTE: Any new ELF note addition should be placed after this note.
847 phdr = (struct elf_phdr *)bufp;
848 bufp += sizeof(struct elf_phdr);
849 phdr->p_type = PT_NOTE;
861 /* setup ELF PT_NOTE for vmcoreinfo */
862 phdr = (struct elf_phdr *)bufp;
863 bufp += sizeof(struct elf_phdr);
864 phdr->p_type = PT_NOTE;
869 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
870 phdr->p_offset = phdr->p_paddr;
871 phdr->p_memsz = vmcoreinfo_max_size;
872 phdr->p_filesz = vmcoreinfo_max_size;
874 /* Increment number of program headers. */
877 /* setup PT_LOAD sections. */
879 for (i = 0; i < crash_mem_ranges; i++) {
880 unsigned long long mbase, msize;
881 mbase = crash_memory_ranges[i].base;
882 msize = crash_memory_ranges[i].size;
887 phdr = (struct elf_phdr *)bufp;
888 bufp += sizeof(struct elf_phdr);
889 phdr->p_type = PT_LOAD;
890 phdr->p_flags = PF_R|PF_W|PF_X;
891 phdr->p_offset = mbase;
893 if (mbase == RMA_START) {
895 * The entire RMA region will be moved by firmware
896 * to the specified destination_address. Hence set
897 * the correct offset.
899 phdr->p_offset = fdm.rmr_region.destination_address;
902 phdr->p_paddr = mbase;
903 phdr->p_vaddr = (unsigned long)__va(mbase);
904 phdr->p_filesz = msize;
905 phdr->p_memsz = msize;
908 /* Increment number of program headers. */
914 static unsigned long init_fadump_header(unsigned long addr)
916 struct fadump_crash_info_header *fdh;
921 fw_dump.fadumphdr_addr = addr;
923 addr += sizeof(struct fadump_crash_info_header);
925 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
926 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
927 fdh->elfcorehdr_addr = addr;
928 /* We will set the crashing cpu id in crash_fadump() during crash. */
929 fdh->crashing_cpu = CPU_UNKNOWN;
934 static void register_fadump(void)
940 * If no memory is reserved then we can not register for firmware-
943 if (!fw_dump.reserve_dump_area_size)
946 fadump_setup_crash_memory_ranges();
948 addr = fdm.rmr_region.destination_address + fdm.rmr_region.source_len;
949 /* Initialize fadump crash info header. */
950 addr = init_fadump_header(addr);
953 pr_debug("Creating ELF core headers at %#016lx\n", addr);
954 fadump_create_elfcore_headers(vaddr);
956 /* register the future kernel dump with firmware. */
957 register_fw_dump(&fdm);
960 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
963 unsigned int wait_time;
965 pr_debug("Un-register firmware-assisted dump\n");
967 /* TODO: Add upper time limit for the delay */
969 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
970 FADUMP_UNREGISTER, fdm,
971 sizeof(struct fadump_mem_struct));
973 wait_time = rtas_busy_delay_time(rc);
979 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
980 " unexpected error(%d).\n", rc);
983 fw_dump.dump_registered = 0;
987 static ssize_t fadump_enabled_show(struct kobject *kobj,
988 struct kobj_attribute *attr,
991 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
994 static ssize_t fadump_register_show(struct kobject *kobj,
995 struct kobj_attribute *attr,
998 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1001 static ssize_t fadump_register_store(struct kobject *kobj,
1002 struct kobj_attribute *attr,
1003 const char *buf, size_t count)
1007 if (!fw_dump.fadump_enabled || fdm_active)
1010 mutex_lock(&fadump_mutex);
1014 if (fw_dump.dump_registered == 0) {
1018 /* Un-register Firmware-assisted dump */
1019 fadump_unregister_dump(&fdm);
1022 if (fw_dump.dump_registered == 1) {
1026 /* Register Firmware-assisted dump */
1035 mutex_unlock(&fadump_mutex);
1036 return ret < 0 ? ret : count;
1039 static int fadump_region_show(struct seq_file *m, void *private)
1041 const struct fadump_mem_struct *fdm_ptr;
1043 if (!fw_dump.fadump_enabled)
1047 fdm_ptr = fdm_active;
1052 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1054 fdm_ptr->cpu_state_data.destination_address,
1055 fdm_ptr->cpu_state_data.destination_address +
1056 fdm_ptr->cpu_state_data.source_len - 1,
1057 fdm_ptr->cpu_state_data.source_len,
1058 fdm_ptr->cpu_state_data.bytes_dumped);
1060 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1062 fdm_ptr->hpte_region.destination_address,
1063 fdm_ptr->hpte_region.destination_address +
1064 fdm_ptr->hpte_region.source_len - 1,
1065 fdm_ptr->hpte_region.source_len,
1066 fdm_ptr->hpte_region.bytes_dumped);
1068 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1070 fdm_ptr->rmr_region.destination_address,
1071 fdm_ptr->rmr_region.destination_address +
1072 fdm_ptr->rmr_region.source_len - 1,
1073 fdm_ptr->rmr_region.source_len,
1074 fdm_ptr->rmr_region.bytes_dumped);
1077 (fw_dump.reserve_dump_area_start ==
1078 fdm_ptr->cpu_state_data.destination_address))
1081 /* Dump is active. Show reserved memory region. */
1083 " : [%#016llx-%#016llx] %#llx bytes, "
1085 (unsigned long long)fw_dump.reserve_dump_area_start,
1086 fdm_ptr->cpu_state_data.destination_address - 1,
1087 fdm_ptr->cpu_state_data.destination_address -
1088 fw_dump.reserve_dump_area_start,
1089 fdm_ptr->cpu_state_data.destination_address -
1090 fw_dump.reserve_dump_area_start);
1094 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1095 0444, fadump_enabled_show,
1097 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1098 0644, fadump_register_show,
1099 fadump_register_store);
1101 static int fadump_region_open(struct inode *inode, struct file *file)
1103 return single_open(file, fadump_region_show, inode->i_private);
1106 static const struct file_operations fadump_region_fops = {
1107 .open = fadump_region_open,
1109 .llseek = seq_lseek,
1110 .release = single_release,
1113 static void fadump_init_files(void)
1115 struct dentry *debugfs_file;
1118 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1120 printk(KERN_ERR "fadump: unable to create sysfs file"
1121 " fadump_enabled (%d)\n", rc);
1123 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1125 printk(KERN_ERR "fadump: unable to create sysfs file"
1126 " fadump_registered (%d)\n", rc);
1128 debugfs_file = debugfs_create_file("fadump_region", 0444,
1129 powerpc_debugfs_root, NULL,
1130 &fadump_region_fops);
1132 printk(KERN_ERR "fadump: unable to create debugfs file"
1133 " fadump_region\n");
1138 * Prepare for firmware-assisted dump.
1140 int __init setup_fadump(void)
1142 if (!fw_dump.fadump_enabled)
1145 if (!fw_dump.fadump_supported) {
1146 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1147 " this hardware\n");
1151 fadump_show_config();
1153 * If dump data is available then see if it is valid and prepare for
1154 * saving it to the disk.
1156 if (fw_dump.dump_active)
1157 process_fadump(fdm_active);
1158 /* Initialize the kernel dump memory structure for FAD registration. */
1159 else if (fw_dump.reserve_dump_area_size)
1160 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1161 fadump_init_files();
1165 subsys_initcall(setup_fadump);