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/seq_file.h>
34 #include <linux/crash_dump.h>
35 #include <linux/kobject.h>
36 #include <linux/sysfs.h>
38 #include <asm/debugfs.h>
42 #include <asm/fadump.h>
43 #include <asm/setup.h>
45 static struct fw_dump fw_dump;
46 static struct fadump_mem_struct fdm;
47 static const struct fadump_mem_struct *fdm_active;
49 static DEFINE_MUTEX(fadump_mutex);
50 struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
53 /* Scan the Firmware Assisted dump configuration details. */
54 int __init early_init_dt_scan_fw_dump(unsigned long node,
55 const char *uname, int depth, void *data)
57 const __be32 *sections;
62 if (depth != 1 || strcmp(uname, "rtas") != 0)
66 * Check if Firmware Assisted dump is supported. if yes, check
67 * if dump has been initiated on last reboot.
69 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
73 fw_dump.fadump_supported = 1;
74 fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
77 * The 'ibm,kernel-dump' rtas node is present only if there is
78 * dump data waiting for us.
80 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
82 fw_dump.dump_active = 1;
84 /* Get the sizes required to store dump data for the firmware provided
86 * For each dump section type supported, a 32bit cell which defines
87 * the ID of a supported section followed by two 32 bit cells which
88 * gives teh size of the section in bytes.
90 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
96 num_sections = size / (3 * sizeof(u32));
98 for (i = 0; i < num_sections; i++, sections += 3) {
99 u32 type = (u32)of_read_number(sections, 1);
102 case FADUMP_CPU_STATE_DATA:
103 fw_dump.cpu_state_data_size =
104 of_read_ulong(§ions[1], 2);
106 case FADUMP_HPTE_REGION:
107 fw_dump.hpte_region_size =
108 of_read_ulong(§ions[1], 2);
116 int is_fadump_active(void)
118 return fw_dump.dump_active;
121 /* Print firmware assisted dump configurations for debugging purpose. */
122 static void fadump_show_config(void)
124 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
125 (fw_dump.fadump_supported ? "present" : "no support"));
127 if (!fw_dump.fadump_supported)
130 pr_debug("Fadump enabled : %s\n",
131 (fw_dump.fadump_enabled ? "yes" : "no"));
132 pr_debug("Dump Active : %s\n",
133 (fw_dump.dump_active ? "yes" : "no"));
134 pr_debug("Dump section sizes:\n");
135 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
136 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
137 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
140 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
146 memset(fdm, 0, sizeof(struct fadump_mem_struct));
147 addr = addr & PAGE_MASK;
149 fdm->header.dump_format_version = cpu_to_be32(0x00000001);
150 fdm->header.dump_num_sections = cpu_to_be16(3);
151 fdm->header.dump_status_flag = 0;
152 fdm->header.offset_first_dump_section =
153 cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
156 * Fields for disk dump option.
157 * We are not using disk dump option, hence set these fields to 0.
159 fdm->header.dd_block_size = 0;
160 fdm->header.dd_block_offset = 0;
161 fdm->header.dd_num_blocks = 0;
162 fdm->header.dd_offset_disk_path = 0;
164 /* set 0 to disable an automatic dump-reboot. */
165 fdm->header.max_time_auto = 0;
167 /* Kernel dump sections */
168 /* cpu state data section. */
169 fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
170 fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
171 fdm->cpu_state_data.source_address = 0;
172 fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
173 fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
174 addr += fw_dump.cpu_state_data_size;
176 /* hpte region section */
177 fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
178 fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
179 fdm->hpte_region.source_address = 0;
180 fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
181 fdm->hpte_region.destination_address = cpu_to_be64(addr);
182 addr += fw_dump.hpte_region_size;
184 /* RMA region section */
185 fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
186 fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
187 fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
188 fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
189 fdm->rmr_region.destination_address = cpu_to_be64(addr);
190 addr += fw_dump.boot_memory_size;
196 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
198 * Function to find the largest memory size we need to reserve during early
199 * boot process. This will be the size of the memory that is required for a
200 * kernel to boot successfully.
202 * This function has been taken from phyp-assisted dump feature implementation.
204 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
206 * TODO: Come up with better approach to find out more accurate memory size
207 * that is required for a kernel to boot successfully.
210 static inline unsigned long fadump_calculate_reserve_size(void)
213 unsigned long long base, size;
215 if (fw_dump.reserve_bootvar)
216 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
219 * Check if the size is specified through crashkernel= cmdline
220 * option. If yes, then use that but ignore base as fadump reserves
221 * memory at a predefined offset.
223 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
225 if (ret == 0 && size > 0) {
226 unsigned long max_size;
228 if (fw_dump.reserve_bootvar)
229 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
231 fw_dump.reserve_bootvar = (unsigned long)size;
234 * Adjust if the boot memory size specified is above
237 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
238 if (fw_dump.reserve_bootvar > max_size) {
239 fw_dump.reserve_bootvar = max_size;
240 pr_info("Adjusted boot memory size to %luMB\n",
241 (fw_dump.reserve_bootvar >> 20));
244 return fw_dump.reserve_bootvar;
245 } else if (fw_dump.reserve_bootvar) {
247 * 'fadump_reserve_mem=' is being used to reserve memory
248 * for firmware-assisted dump.
250 return fw_dump.reserve_bootvar;
253 /* divide by 20 to get 5% of value */
254 size = memblock_phys_mem_size() / 20;
256 /* round it down in multiples of 256 */
257 size = size & ~0x0FFFFFFFUL;
259 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
260 if (memory_limit && size > memory_limit)
263 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
267 * Calculate the total memory size required to be reserved for
268 * firmware-assisted dump registration.
270 static unsigned long get_fadump_area_size(void)
272 unsigned long size = 0;
274 size += fw_dump.cpu_state_data_size;
275 size += fw_dump.hpte_region_size;
276 size += fw_dump.boot_memory_size;
277 size += sizeof(struct fadump_crash_info_header);
278 size += sizeof(struct elfhdr); /* ELF core header.*/
279 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
280 /* Program headers for crash memory regions. */
281 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
283 size = PAGE_ALIGN(size);
287 int __init fadump_reserve_mem(void)
289 unsigned long base, size, memory_boundary;
291 if (!fw_dump.fadump_enabled)
294 if (!fw_dump.fadump_supported) {
295 printk(KERN_INFO "Firmware-assisted dump is not supported on"
297 fw_dump.fadump_enabled = 0;
301 * Initialize boot memory size
302 * If dump is active then we have already calculated the size during
306 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
308 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
311 * Calculate the memory boundary.
312 * If memory_limit is less than actual memory boundary then reserve
313 * the memory for fadump beyond the memory_limit and adjust the
314 * memory_limit accordingly, so that the running kernel can run with
315 * specified memory_limit.
317 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
318 size = get_fadump_area_size();
319 if ((memory_limit + size) < memblock_end_of_DRAM())
320 memory_limit += size;
322 memory_limit = memblock_end_of_DRAM();
323 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
324 " dump, now %#016llx\n", memory_limit);
327 memory_boundary = memory_limit;
329 memory_boundary = memblock_end_of_DRAM();
331 if (fw_dump.dump_active) {
332 printk(KERN_INFO "Firmware-assisted dump is active.\n");
334 * If last boot has crashed then reserve all the memory
335 * above boot_memory_size so that we don't touch it until
336 * dump is written to disk by userspace tool. This memory
337 * will be released for general use once the dump is saved.
339 base = fw_dump.boot_memory_size;
340 size = memory_boundary - base;
341 memblock_reserve(base, size);
342 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
343 "for saving crash dump\n",
344 (unsigned long)(size >> 20),
345 (unsigned long)(base >> 20));
347 fw_dump.fadumphdr_addr =
348 be64_to_cpu(fdm_active->rmr_region.destination_address) +
349 be64_to_cpu(fdm_active->rmr_region.source_len);
350 pr_debug("fadumphdr_addr = %p\n",
351 (void *) fw_dump.fadumphdr_addr);
353 size = get_fadump_area_size();
356 * Reserve memory at an offset closer to bottom of the RAM to
357 * minimize the impact of memory hot-remove operation. We can't
358 * use memblock_find_in_range() here since it doesn't allocate
359 * from bottom to top.
361 for (base = fw_dump.boot_memory_size;
362 base <= (memory_boundary - size);
364 if (memblock_is_region_memory(base, size) &&
365 !memblock_is_region_reserved(base, size))
368 if ((base > (memory_boundary - size)) ||
369 memblock_reserve(base, size)) {
370 pr_err("Failed to reserve memory\n");
374 pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
375 "assisted dump (System RAM: %ldMB)\n",
376 (unsigned long)(size >> 20),
377 (unsigned long)(base >> 20),
378 (unsigned long)(memblock_phys_mem_size() >> 20));
381 fw_dump.reserve_dump_area_start = base;
382 fw_dump.reserve_dump_area_size = size;
386 unsigned long __init arch_reserved_kernel_pages(void)
388 return memblock_reserved_size() / PAGE_SIZE;
391 /* Look for fadump= cmdline option. */
392 static int __init early_fadump_param(char *p)
397 if (strncmp(p, "on", 2) == 0)
398 fw_dump.fadump_enabled = 1;
399 else if (strncmp(p, "off", 3) == 0)
400 fw_dump.fadump_enabled = 0;
404 early_param("fadump", early_fadump_param);
407 * Look for fadump_reserve_mem= cmdline option
408 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
409 * the sooner 'crashkernel=' parameter is accustomed to.
411 static int __init early_fadump_reserve_mem(char *p)
414 fw_dump.reserve_bootvar = memparse(p, &p);
417 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
419 static int register_fw_dump(struct fadump_mem_struct *fdm)
422 unsigned int wait_time;
424 pr_debug("Registering for firmware-assisted kernel dump...\n");
426 /* TODO: Add upper time limit for the delay */
428 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
429 FADUMP_REGISTER, fdm,
430 sizeof(struct fadump_mem_struct));
432 wait_time = rtas_busy_delay_time(rc);
441 pr_err("Failed to register. Unknown Error(%d).\n", rc);
444 printk(KERN_ERR "Failed to register firmware-assisted kernel"
445 " dump. Hardware Error(%d).\n", rc);
448 printk(KERN_ERR "Failed to register firmware-assisted kernel"
449 " dump. Parameter Error(%d).\n", rc);
453 printk(KERN_ERR "firmware-assisted kernel dump is already "
455 fw_dump.dump_registered = 1;
459 printk(KERN_INFO "firmware-assisted kernel dump registration"
461 fw_dump.dump_registered = 1;
468 void crash_fadump(struct pt_regs *regs, const char *str)
470 struct fadump_crash_info_header *fdh = NULL;
471 int old_cpu, this_cpu;
473 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
477 * old_cpu == -1 means this is the first CPU which has come here,
478 * go ahead and trigger fadump.
480 * old_cpu != -1 means some other CPU has already on it's way
481 * to trigger fadump, just keep looping here.
483 this_cpu = smp_processor_id();
484 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
488 * We can't loop here indefinitely. Wait as long as fadump
489 * is in force. If we race with fadump un-registration this
490 * loop will break and then we go down to normal panic path
491 * and reboot. If fadump is in force the first crashing
492 * cpu will definitely trigger fadump.
494 while (fw_dump.dump_registered)
499 fdh = __va(fw_dump.fadumphdr_addr);
500 fdh->crashing_cpu = crashing_cpu;
501 crash_save_vmcoreinfo();
506 ppc_save_regs(&fdh->regs);
508 fdh->online_mask = *cpu_online_mask;
510 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
511 rtas_os_term((char *)str);
514 #define GPR_MASK 0xffffff0000000000
515 static inline int fadump_gpr_index(u64 id)
520 if ((id & GPR_MASK) == REG_ID("GPR")) {
521 /* get the digits at the end */
526 str[0] = (id >> 8) & 0xff;
527 sscanf(str, "%d", &i);
534 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
539 i = fadump_gpr_index(reg_id);
541 regs->gpr[i] = (unsigned long)reg_val;
542 else if (reg_id == REG_ID("NIA"))
543 regs->nip = (unsigned long)reg_val;
544 else if (reg_id == REG_ID("MSR"))
545 regs->msr = (unsigned long)reg_val;
546 else if (reg_id == REG_ID("CTR"))
547 regs->ctr = (unsigned long)reg_val;
548 else if (reg_id == REG_ID("LR"))
549 regs->link = (unsigned long)reg_val;
550 else if (reg_id == REG_ID("XER"))
551 regs->xer = (unsigned long)reg_val;
552 else if (reg_id == REG_ID("CR"))
553 regs->ccr = (unsigned long)reg_val;
554 else if (reg_id == REG_ID("DAR"))
555 regs->dar = (unsigned long)reg_val;
556 else if (reg_id == REG_ID("DSISR"))
557 regs->dsisr = (unsigned long)reg_val;
560 static struct fadump_reg_entry*
561 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
563 memset(regs, 0, sizeof(struct pt_regs));
565 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
566 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
567 be64_to_cpu(reg_entry->reg_value));
574 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
576 struct elf_prstatus prstatus;
578 memset(&prstatus, 0, sizeof(prstatus));
580 * FIXME: How do i get PID? Do I really need it?
581 * prstatus.pr_pid = ????
583 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
584 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
585 &prstatus, sizeof(prstatus));
589 static void fadump_update_elfcore_header(char *bufp)
592 struct elf_phdr *phdr;
594 elf = (struct elfhdr *)bufp;
595 bufp += sizeof(struct elfhdr);
597 /* First note is a place holder for cpu notes info. */
598 phdr = (struct elf_phdr *)bufp;
600 if (phdr->p_type == PT_NOTE) {
601 phdr->p_paddr = fw_dump.cpu_notes_buf;
602 phdr->p_offset = phdr->p_paddr;
603 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
604 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
609 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
613 unsigned long order, count, i;
615 order = get_order(size);
616 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
621 page = virt_to_page(vaddr);
622 for (i = 0; i < count; i++)
623 SetPageReserved(page + i);
627 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
630 unsigned long order, count, i;
632 order = get_order(size);
634 page = virt_to_page(vaddr);
635 for (i = 0; i < count; i++)
636 ClearPageReserved(page + i);
637 __free_pages(page, order);
641 * Read CPU state dump data and convert it into ELF notes.
642 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
643 * used to access the data to allow for additional fields to be added without
644 * affecting compatibility. Each list of registers for a CPU starts with
645 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
646 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
647 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
648 * of register value. For more details refer to PAPR document.
650 * Only for the crashing cpu we ignore the CPU dump data and get exact
651 * state from fadump crash info structure populated by first kernel at the
654 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
656 struct fadump_reg_save_area_header *reg_header;
657 struct fadump_reg_entry *reg_entry;
658 struct fadump_crash_info_header *fdh = NULL;
661 u32 num_cpus, *note_buf;
663 int i, rc = 0, cpu = 0;
665 if (!fdm->cpu_state_data.bytes_dumped)
668 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
672 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
673 printk(KERN_ERR "Unable to read register save area.\n");
676 pr_debug("--------CPU State Data------------\n");
677 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
678 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
680 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
681 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
682 pr_debug("NumCpus : %u\n", num_cpus);
683 vaddr += sizeof(u32);
684 reg_entry = (struct fadump_reg_entry *)vaddr;
686 /* Allocate buffer to hold cpu crash notes. */
687 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
688 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
689 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
691 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
692 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
695 fw_dump.cpu_notes_buf = __pa(note_buf);
697 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
698 (num_cpus * sizeof(note_buf_t)), note_buf);
700 if (fw_dump.fadumphdr_addr)
701 fdh = __va(fw_dump.fadumphdr_addr);
703 for (i = 0; i < num_cpus; i++) {
704 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
705 printk(KERN_ERR "Unable to read CPU state data\n");
709 /* Lower 4 bytes of reg_value contains logical cpu id */
710 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
711 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
712 SKIP_TO_NEXT_CPU(reg_entry);
715 pr_debug("Reading register data for cpu %d...\n", cpu);
716 if (fdh && fdh->crashing_cpu == cpu) {
718 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
719 SKIP_TO_NEXT_CPU(reg_entry);
722 reg_entry = fadump_read_registers(reg_entry, ®s);
723 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
726 final_note(note_buf);
729 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
730 fdh->elfcorehdr_addr);
731 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
736 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
737 fw_dump.cpu_notes_buf_size);
738 fw_dump.cpu_notes_buf = 0;
739 fw_dump.cpu_notes_buf_size = 0;
745 * Validate and process the dump data stored by firmware before exporting
746 * it through '/proc/vmcore'.
748 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
750 struct fadump_crash_info_header *fdh;
753 if (!fdm_active || !fw_dump.fadumphdr_addr)
756 /* Check if the dump data is valid. */
757 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
758 (fdm_active->cpu_state_data.error_flags != 0) ||
759 (fdm_active->rmr_region.error_flags != 0)) {
760 printk(KERN_ERR "Dump taken by platform is not valid\n");
763 if ((fdm_active->rmr_region.bytes_dumped !=
764 fdm_active->rmr_region.source_len) ||
765 !fdm_active->cpu_state_data.bytes_dumped) {
766 printk(KERN_ERR "Dump taken by platform is incomplete\n");
770 /* Validate the fadump crash info header */
771 fdh = __va(fw_dump.fadumphdr_addr);
772 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
773 printk(KERN_ERR "Crash info header is not valid.\n");
777 rc = fadump_build_cpu_notes(fdm_active);
782 * We are done validating dump info and elfcore header is now ready
783 * to be exported. set elfcorehdr_addr so that vmcore module will
784 * export the elfcore header through '/proc/vmcore'.
786 elfcorehdr_addr = fdh->elfcorehdr_addr;
791 static inline void fadump_add_crash_memory(unsigned long long base,
792 unsigned long long end)
797 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
798 crash_mem_ranges, base, end - 1, (end - base));
799 crash_memory_ranges[crash_mem_ranges].base = base;
800 crash_memory_ranges[crash_mem_ranges].size = end - base;
804 static void fadump_exclude_reserved_area(unsigned long long start,
805 unsigned long long end)
807 unsigned long long ra_start, ra_end;
809 ra_start = fw_dump.reserve_dump_area_start;
810 ra_end = ra_start + fw_dump.reserve_dump_area_size;
812 if ((ra_start < end) && (ra_end > start)) {
813 if ((start < ra_start) && (end > ra_end)) {
814 fadump_add_crash_memory(start, ra_start);
815 fadump_add_crash_memory(ra_end, end);
816 } else if (start < ra_start) {
817 fadump_add_crash_memory(start, ra_start);
818 } else if (ra_end < end) {
819 fadump_add_crash_memory(ra_end, end);
822 fadump_add_crash_memory(start, end);
825 static int fadump_init_elfcore_header(char *bufp)
829 elf = (struct elfhdr *) bufp;
830 bufp += sizeof(struct elfhdr);
831 memcpy(elf->e_ident, ELFMAG, SELFMAG);
832 elf->e_ident[EI_CLASS] = ELF_CLASS;
833 elf->e_ident[EI_DATA] = ELF_DATA;
834 elf->e_ident[EI_VERSION] = EV_CURRENT;
835 elf->e_ident[EI_OSABI] = ELF_OSABI;
836 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
837 elf->e_type = ET_CORE;
838 elf->e_machine = ELF_ARCH;
839 elf->e_version = EV_CURRENT;
841 elf->e_phoff = sizeof(struct elfhdr);
843 #if defined(_CALL_ELF)
844 elf->e_flags = _CALL_ELF;
848 elf->e_ehsize = sizeof(struct elfhdr);
849 elf->e_phentsize = sizeof(struct elf_phdr);
851 elf->e_shentsize = 0;
859 * Traverse through memblock structure and setup crash memory ranges. These
860 * ranges will be used create PT_LOAD program headers in elfcore header.
862 static void fadump_setup_crash_memory_ranges(void)
864 struct memblock_region *reg;
865 unsigned long long start, end;
867 pr_debug("Setup crash memory ranges.\n");
868 crash_mem_ranges = 0;
870 * add the first memory chunk (RMA_START through boot_memory_size) as
871 * a separate memory chunk. The reason is, at the time crash firmware
872 * will move the content of this memory chunk to different location
873 * specified during fadump registration. We need to create a separate
874 * program header for this chunk with the correct offset.
876 fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
878 for_each_memblock(memory, reg) {
879 start = (unsigned long long)reg->base;
880 end = start + (unsigned long long)reg->size;
883 * skip the first memory chunk that is already added (RMA_START
884 * through boot_memory_size). This logic needs a relook if and
885 * when RMA_START changes to a non-zero value.
887 BUILD_BUG_ON(RMA_START != 0);
888 if (start < fw_dump.boot_memory_size) {
889 if (end > fw_dump.boot_memory_size)
890 start = fw_dump.boot_memory_size;
895 /* add this range excluding the reserved dump area. */
896 fadump_exclude_reserved_area(start, end);
901 * If the given physical address falls within the boot memory region then
902 * return the relocated address that points to the dump region reserved
903 * for saving initial boot memory contents.
905 static inline unsigned long fadump_relocate(unsigned long paddr)
907 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
908 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
913 static int fadump_create_elfcore_headers(char *bufp)
916 struct elf_phdr *phdr;
919 fadump_init_elfcore_header(bufp);
920 elf = (struct elfhdr *)bufp;
921 bufp += sizeof(struct elfhdr);
924 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
925 * will be populated during second kernel boot after crash. Hence
926 * this PT_NOTE will always be the first elf note.
928 * NOTE: Any new ELF note addition should be placed after this note.
930 phdr = (struct elf_phdr *)bufp;
931 bufp += sizeof(struct elf_phdr);
932 phdr->p_type = PT_NOTE;
944 /* setup ELF PT_NOTE for vmcoreinfo */
945 phdr = (struct elf_phdr *)bufp;
946 bufp += sizeof(struct elf_phdr);
947 phdr->p_type = PT_NOTE;
952 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
953 phdr->p_offset = phdr->p_paddr;
954 phdr->p_memsz = vmcoreinfo_max_size;
955 phdr->p_filesz = vmcoreinfo_max_size;
957 /* Increment number of program headers. */
960 /* setup PT_LOAD sections. */
962 for (i = 0; i < crash_mem_ranges; i++) {
963 unsigned long long mbase, msize;
964 mbase = crash_memory_ranges[i].base;
965 msize = crash_memory_ranges[i].size;
970 phdr = (struct elf_phdr *)bufp;
971 bufp += sizeof(struct elf_phdr);
972 phdr->p_type = PT_LOAD;
973 phdr->p_flags = PF_R|PF_W|PF_X;
974 phdr->p_offset = mbase;
976 if (mbase == RMA_START) {
978 * The entire RMA region will be moved by firmware
979 * to the specified destination_address. Hence set
980 * the correct offset.
982 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
985 phdr->p_paddr = mbase;
986 phdr->p_vaddr = (unsigned long)__va(mbase);
987 phdr->p_filesz = msize;
988 phdr->p_memsz = msize;
991 /* Increment number of program headers. */
997 static unsigned long init_fadump_header(unsigned long addr)
999 struct fadump_crash_info_header *fdh;
1004 fw_dump.fadumphdr_addr = addr;
1006 addr += sizeof(struct fadump_crash_info_header);
1008 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1009 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1010 fdh->elfcorehdr_addr = addr;
1011 /* We will set the crashing cpu id in crash_fadump() during crash. */
1012 fdh->crashing_cpu = CPU_UNKNOWN;
1017 static int register_fadump(void)
1023 * If no memory is reserved then we can not register for firmware-
1026 if (!fw_dump.reserve_dump_area_size)
1029 fadump_setup_crash_memory_ranges();
1031 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
1032 /* Initialize fadump crash info header. */
1033 addr = init_fadump_header(addr);
1036 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1037 fadump_create_elfcore_headers(vaddr);
1039 /* register the future kernel dump with firmware. */
1040 return register_fw_dump(&fdm);
1043 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
1046 unsigned int wait_time;
1048 pr_debug("Un-register firmware-assisted dump\n");
1050 /* TODO: Add upper time limit for the delay */
1052 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1053 FADUMP_UNREGISTER, fdm,
1054 sizeof(struct fadump_mem_struct));
1056 wait_time = rtas_busy_delay_time(rc);
1059 } while (wait_time);
1062 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1063 " unexpected error(%d).\n", rc);
1066 fw_dump.dump_registered = 0;
1070 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1073 unsigned int wait_time;
1075 pr_debug("Invalidating firmware-assisted dump registration\n");
1077 /* TODO: Add upper time limit for the delay */
1079 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1080 FADUMP_INVALIDATE, fdm,
1081 sizeof(struct fadump_mem_struct));
1083 wait_time = rtas_busy_delay_time(rc);
1086 } while (wait_time);
1089 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1092 fw_dump.dump_active = 0;
1097 void fadump_cleanup(void)
1099 /* Invalidate the registration only if dump is active. */
1100 if (fw_dump.dump_active) {
1101 init_fadump_mem_struct(&fdm,
1102 be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1103 fadump_invalidate_dump(&fdm);
1108 * Release the memory that was reserved in early boot to preserve the memory
1109 * contents. The released memory will be available for general use.
1111 static void fadump_release_memory(unsigned long begin, unsigned long end)
1114 unsigned long ra_start, ra_end;
1116 ra_start = fw_dump.reserve_dump_area_start;
1117 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1119 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1121 * exclude the dump reserve area. Will reuse it for next
1122 * fadump registration.
1124 if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1127 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1131 static void fadump_invalidate_release_mem(void)
1133 unsigned long reserved_area_start, reserved_area_end;
1134 unsigned long destination_address;
1136 mutex_lock(&fadump_mutex);
1137 if (!fw_dump.dump_active) {
1138 mutex_unlock(&fadump_mutex);
1142 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1144 mutex_unlock(&fadump_mutex);
1147 * Save the current reserved memory bounds we will require them
1148 * later for releasing the memory for general use.
1150 reserved_area_start = fw_dump.reserve_dump_area_start;
1151 reserved_area_end = reserved_area_start +
1152 fw_dump.reserve_dump_area_size;
1154 * Setup reserve_dump_area_start and its size so that we can
1155 * reuse this reserved memory for Re-registration.
1157 fw_dump.reserve_dump_area_start = destination_address;
1158 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1160 fadump_release_memory(reserved_area_start, reserved_area_end);
1161 if (fw_dump.cpu_notes_buf) {
1162 fadump_cpu_notes_buf_free(
1163 (unsigned long)__va(fw_dump.cpu_notes_buf),
1164 fw_dump.cpu_notes_buf_size);
1165 fw_dump.cpu_notes_buf = 0;
1166 fw_dump.cpu_notes_buf_size = 0;
1168 /* Initialize the kernel dump memory structure for FAD registration. */
1169 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1172 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1173 struct kobj_attribute *attr,
1174 const char *buf, size_t count)
1176 if (!fw_dump.dump_active)
1179 if (buf[0] == '1') {
1181 * Take away the '/proc/vmcore'. We are releasing the dump
1182 * memory, hence it will not be valid anymore.
1184 #ifdef CONFIG_PROC_VMCORE
1187 fadump_invalidate_release_mem();
1194 static ssize_t fadump_enabled_show(struct kobject *kobj,
1195 struct kobj_attribute *attr,
1198 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1201 static ssize_t fadump_register_show(struct kobject *kobj,
1202 struct kobj_attribute *attr,
1205 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1208 static ssize_t fadump_register_store(struct kobject *kobj,
1209 struct kobj_attribute *attr,
1210 const char *buf, size_t count)
1214 if (!fw_dump.fadump_enabled || fdm_active)
1217 mutex_lock(&fadump_mutex);
1221 if (fw_dump.dump_registered == 0) {
1224 /* Un-register Firmware-assisted dump */
1225 fadump_unregister_dump(&fdm);
1228 if (fw_dump.dump_registered == 1) {
1232 /* Register Firmware-assisted dump */
1233 ret = register_fadump();
1241 mutex_unlock(&fadump_mutex);
1242 return ret < 0 ? ret : count;
1245 static int fadump_region_show(struct seq_file *m, void *private)
1247 const struct fadump_mem_struct *fdm_ptr;
1249 if (!fw_dump.fadump_enabled)
1252 mutex_lock(&fadump_mutex);
1254 fdm_ptr = fdm_active;
1256 mutex_unlock(&fadump_mutex);
1261 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1263 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1264 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1265 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1266 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1267 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1269 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1271 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1272 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1273 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1274 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1275 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1277 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1279 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1280 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1281 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1282 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1283 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1286 (fw_dump.reserve_dump_area_start ==
1287 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1290 /* Dump is active. Show reserved memory region. */
1292 " : [%#016llx-%#016llx] %#llx bytes, "
1294 (unsigned long long)fw_dump.reserve_dump_area_start,
1295 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1296 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1297 fw_dump.reserve_dump_area_start,
1298 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1299 fw_dump.reserve_dump_area_start);
1302 mutex_unlock(&fadump_mutex);
1306 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1308 fadump_release_memory_store);
1309 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1310 0444, fadump_enabled_show,
1312 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1313 0644, fadump_register_show,
1314 fadump_register_store);
1316 static int fadump_region_open(struct inode *inode, struct file *file)
1318 return single_open(file, fadump_region_show, inode->i_private);
1321 static const struct file_operations fadump_region_fops = {
1322 .open = fadump_region_open,
1324 .llseek = seq_lseek,
1325 .release = single_release,
1328 static void fadump_init_files(void)
1330 struct dentry *debugfs_file;
1333 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1335 printk(KERN_ERR "fadump: unable to create sysfs file"
1336 " fadump_enabled (%d)\n", rc);
1338 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1340 printk(KERN_ERR "fadump: unable to create sysfs file"
1341 " fadump_registered (%d)\n", rc);
1343 debugfs_file = debugfs_create_file("fadump_region", 0444,
1344 powerpc_debugfs_root, NULL,
1345 &fadump_region_fops);
1347 printk(KERN_ERR "fadump: unable to create debugfs file"
1348 " fadump_region\n");
1350 if (fw_dump.dump_active) {
1351 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1353 printk(KERN_ERR "fadump: unable to create sysfs file"
1354 " fadump_release_mem (%d)\n", rc);
1360 * Prepare for firmware-assisted dump.
1362 int __init setup_fadump(void)
1364 if (!fw_dump.fadump_enabled)
1367 if (!fw_dump.fadump_supported) {
1368 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1369 " this hardware\n");
1373 fadump_show_config();
1375 * If dump data is available then see if it is valid and prepare for
1376 * saving it to the disk.
1378 if (fw_dump.dump_active) {
1380 * if dump process fails then invalidate the registration
1381 * and release memory before proceeding for re-registration.
1383 if (process_fadump(fdm_active) < 0)
1384 fadump_invalidate_release_mem();
1386 /* Initialize the kernel dump memory structure for FAD registration. */
1387 else if (fw_dump.reserve_dump_area_size)
1388 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1389 fadump_init_files();
1393 subsys_initcall(setup_fadump);