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;
51 int crash_memory_ranges_size;
53 int max_crash_mem_ranges;
55 /* Scan the Firmware Assisted dump configuration details. */
56 int __init early_init_dt_scan_fw_dump(unsigned long node,
57 const char *uname, int depth, void *data)
59 const __be32 *sections;
64 if (depth != 1 || strcmp(uname, "rtas") != 0)
68 * Check if Firmware Assisted dump is supported. if yes, check
69 * if dump has been initiated on last reboot.
71 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
75 fw_dump.fadump_supported = 1;
76 fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
79 * The 'ibm,kernel-dump' rtas node is present only if there is
80 * dump data waiting for us.
82 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
84 fw_dump.dump_active = 1;
86 /* Get the sizes required to store dump data for the firmware provided
88 * For each dump section type supported, a 32bit cell which defines
89 * the ID of a supported section followed by two 32 bit cells which
90 * gives teh size of the section in bytes.
92 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
98 num_sections = size / (3 * sizeof(u32));
100 for (i = 0; i < num_sections; i++, sections += 3) {
101 u32 type = (u32)of_read_number(sections, 1);
104 case FADUMP_CPU_STATE_DATA:
105 fw_dump.cpu_state_data_size =
106 of_read_ulong(§ions[1], 2);
108 case FADUMP_HPTE_REGION:
109 fw_dump.hpte_region_size =
110 of_read_ulong(§ions[1], 2);
119 * If fadump is registered, check if the memory provided
120 * falls within boot memory area and reserved memory area.
122 int is_fadump_memory_area(u64 addr, ulong size)
124 u64 d_start = fw_dump.reserve_dump_area_start;
125 u64 d_end = d_start + fw_dump.reserve_dump_area_size;
127 if (!fw_dump.dump_registered)
130 if (((addr + size) > d_start) && (addr <= d_end))
133 return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
136 int should_fadump_crash(void)
138 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
143 int is_fadump_active(void)
145 return fw_dump.dump_active;
149 * Returns 1, if there are no holes in boot memory area,
152 static int is_boot_memory_area_contiguous(void)
154 struct memblock_region *reg;
155 unsigned long tstart, tend;
156 unsigned long start_pfn = PHYS_PFN(RMA_START);
157 unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size);
158 unsigned int ret = 0;
160 for_each_memblock(memory, reg) {
161 tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
162 tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
164 /* Memory hole from start_pfn to tstart */
165 if (tstart > start_pfn)
168 if (tend == end_pfn) {
173 start_pfn = tend + 1;
180 /* Print firmware assisted dump configurations for debugging purpose. */
181 static void fadump_show_config(void)
183 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
184 (fw_dump.fadump_supported ? "present" : "no support"));
186 if (!fw_dump.fadump_supported)
189 pr_debug("Fadump enabled : %s\n",
190 (fw_dump.fadump_enabled ? "yes" : "no"));
191 pr_debug("Dump Active : %s\n",
192 (fw_dump.dump_active ? "yes" : "no"));
193 pr_debug("Dump section sizes:\n");
194 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
195 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
196 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
199 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
205 memset(fdm, 0, sizeof(struct fadump_mem_struct));
206 addr = addr & PAGE_MASK;
208 fdm->header.dump_format_version = cpu_to_be32(0x00000001);
209 fdm->header.dump_num_sections = cpu_to_be16(3);
210 fdm->header.dump_status_flag = 0;
211 fdm->header.offset_first_dump_section =
212 cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
215 * Fields for disk dump option.
216 * We are not using disk dump option, hence set these fields to 0.
218 fdm->header.dd_block_size = 0;
219 fdm->header.dd_block_offset = 0;
220 fdm->header.dd_num_blocks = 0;
221 fdm->header.dd_offset_disk_path = 0;
223 /* set 0 to disable an automatic dump-reboot. */
224 fdm->header.max_time_auto = 0;
226 /* Kernel dump sections */
227 /* cpu state data section. */
228 fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
229 fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
230 fdm->cpu_state_data.source_address = 0;
231 fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
232 fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
233 addr += fw_dump.cpu_state_data_size;
235 /* hpte region section */
236 fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
237 fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
238 fdm->hpte_region.source_address = 0;
239 fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
240 fdm->hpte_region.destination_address = cpu_to_be64(addr);
241 addr += fw_dump.hpte_region_size;
243 /* RMA region section */
244 fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
245 fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
246 fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
247 fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
248 fdm->rmr_region.destination_address = cpu_to_be64(addr);
249 addr += fw_dump.boot_memory_size;
255 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
257 * Function to find the largest memory size we need to reserve during early
258 * boot process. This will be the size of the memory that is required for a
259 * kernel to boot successfully.
261 * This function has been taken from phyp-assisted dump feature implementation.
263 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
265 * TODO: Come up with better approach to find out more accurate memory size
266 * that is required for a kernel to boot successfully.
269 static inline unsigned long fadump_calculate_reserve_size(void)
272 unsigned long long base, size;
274 if (fw_dump.reserve_bootvar)
275 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
278 * Check if the size is specified through crashkernel= cmdline
279 * option. If yes, then use that but ignore base as fadump reserves
280 * memory at a predefined offset.
282 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
284 if (ret == 0 && size > 0) {
285 unsigned long max_size;
287 if (fw_dump.reserve_bootvar)
288 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
290 fw_dump.reserve_bootvar = (unsigned long)size;
293 * Adjust if the boot memory size specified is above
296 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
297 if (fw_dump.reserve_bootvar > max_size) {
298 fw_dump.reserve_bootvar = max_size;
299 pr_info("Adjusted boot memory size to %luMB\n",
300 (fw_dump.reserve_bootvar >> 20));
303 return fw_dump.reserve_bootvar;
304 } else if (fw_dump.reserve_bootvar) {
306 * 'fadump_reserve_mem=' is being used to reserve memory
307 * for firmware-assisted dump.
309 return fw_dump.reserve_bootvar;
312 /* divide by 20 to get 5% of value */
313 size = memblock_phys_mem_size() / 20;
315 /* round it down in multiples of 256 */
316 size = size & ~0x0FFFFFFFUL;
318 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
319 if (memory_limit && size > memory_limit)
322 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
326 * Calculate the total memory size required to be reserved for
327 * firmware-assisted dump registration.
329 static unsigned long get_fadump_area_size(void)
331 unsigned long size = 0;
333 size += fw_dump.cpu_state_data_size;
334 size += fw_dump.hpte_region_size;
335 size += fw_dump.boot_memory_size;
336 size += sizeof(struct fadump_crash_info_header);
337 size += sizeof(struct elfhdr); /* ELF core header.*/
338 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
339 /* Program headers for crash memory regions. */
340 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
342 size = PAGE_ALIGN(size);
346 int __init fadump_reserve_mem(void)
348 unsigned long base, size, memory_boundary;
350 if (!fw_dump.fadump_enabled)
353 if (!fw_dump.fadump_supported) {
354 printk(KERN_INFO "Firmware-assisted dump is not supported on"
356 fw_dump.fadump_enabled = 0;
360 * Initialize boot memory size
361 * If dump is active then we have already calculated the size during
365 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
367 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
370 * Calculate the memory boundary.
371 * If memory_limit is less than actual memory boundary then reserve
372 * the memory for fadump beyond the memory_limit and adjust the
373 * memory_limit accordingly, so that the running kernel can run with
374 * specified memory_limit.
376 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
377 size = get_fadump_area_size();
378 if ((memory_limit + size) < memblock_end_of_DRAM())
379 memory_limit += size;
381 memory_limit = memblock_end_of_DRAM();
382 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
383 " dump, now %#016llx\n", memory_limit);
386 memory_boundary = memory_limit;
388 memory_boundary = memblock_end_of_DRAM();
390 if (fw_dump.dump_active) {
391 printk(KERN_INFO "Firmware-assisted dump is active.\n");
393 * If last boot has crashed then reserve all the memory
394 * above boot_memory_size so that we don't touch it until
395 * dump is written to disk by userspace tool. This memory
396 * will be released for general use once the dump is saved.
398 base = fw_dump.boot_memory_size;
399 size = memory_boundary - base;
400 memblock_reserve(base, size);
401 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
402 "for saving crash dump\n",
403 (unsigned long)(size >> 20),
404 (unsigned long)(base >> 20));
406 fw_dump.fadumphdr_addr =
407 be64_to_cpu(fdm_active->rmr_region.destination_address) +
408 be64_to_cpu(fdm_active->rmr_region.source_len);
409 pr_debug("fadumphdr_addr = %p\n",
410 (void *) fw_dump.fadumphdr_addr);
412 size = get_fadump_area_size();
415 * Reserve memory at an offset closer to bottom of the RAM to
416 * minimize the impact of memory hot-remove operation. We can't
417 * use memblock_find_in_range() here since it doesn't allocate
418 * from bottom to top.
420 for (base = fw_dump.boot_memory_size;
421 base <= (memory_boundary - size);
423 if (memblock_is_region_memory(base, size) &&
424 !memblock_is_region_reserved(base, size))
427 if ((base > (memory_boundary - size)) ||
428 memblock_reserve(base, size)) {
429 pr_err("Failed to reserve memory\n");
433 pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
434 "assisted dump (System RAM: %ldMB)\n",
435 (unsigned long)(size >> 20),
436 (unsigned long)(base >> 20),
437 (unsigned long)(memblock_phys_mem_size() >> 20));
440 fw_dump.reserve_dump_area_start = base;
441 fw_dump.reserve_dump_area_size = size;
445 unsigned long __init arch_reserved_kernel_pages(void)
447 return memblock_reserved_size() / PAGE_SIZE;
450 /* Look for fadump= cmdline option. */
451 static int __init early_fadump_param(char *p)
456 if (strncmp(p, "on", 2) == 0)
457 fw_dump.fadump_enabled = 1;
458 else if (strncmp(p, "off", 3) == 0)
459 fw_dump.fadump_enabled = 0;
463 early_param("fadump", early_fadump_param);
466 * Look for fadump_reserve_mem= cmdline option
467 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
468 * the sooner 'crashkernel=' parameter is accustomed to.
470 static int __init early_fadump_reserve_mem(char *p)
473 fw_dump.reserve_bootvar = memparse(p, &p);
476 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
478 static int register_fw_dump(struct fadump_mem_struct *fdm)
481 unsigned int wait_time;
483 pr_debug("Registering for firmware-assisted kernel dump...\n");
485 /* TODO: Add upper time limit for the delay */
487 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
488 FADUMP_REGISTER, fdm,
489 sizeof(struct fadump_mem_struct));
491 wait_time = rtas_busy_delay_time(rc);
500 pr_err("Failed to register. Unknown Error(%d).\n", rc);
503 printk(KERN_ERR "Failed to register firmware-assisted kernel"
504 " dump. Hardware Error(%d).\n", rc);
507 if (!is_boot_memory_area_contiguous())
508 pr_err("Can't have holes in boot memory area while "
509 "registering fadump\n");
511 printk(KERN_ERR "Failed to register firmware-assisted kernel"
512 " dump. Parameter Error(%d).\n", rc);
516 printk(KERN_ERR "firmware-assisted kernel dump is already "
518 fw_dump.dump_registered = 1;
522 printk(KERN_INFO "firmware-assisted kernel dump registration"
524 fw_dump.dump_registered = 1;
531 void crash_fadump(struct pt_regs *regs, const char *str)
533 struct fadump_crash_info_header *fdh = NULL;
534 int old_cpu, this_cpu;
536 if (!should_fadump_crash())
540 * old_cpu == -1 means this is the first CPU which has come here,
541 * go ahead and trigger fadump.
543 * old_cpu != -1 means some other CPU has already on it's way
544 * to trigger fadump, just keep looping here.
546 this_cpu = smp_processor_id();
547 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
551 * We can't loop here indefinitely. Wait as long as fadump
552 * is in force. If we race with fadump un-registration this
553 * loop will break and then we go down to normal panic path
554 * and reboot. If fadump is in force the first crashing
555 * cpu will definitely trigger fadump.
557 while (fw_dump.dump_registered)
562 fdh = __va(fw_dump.fadumphdr_addr);
563 fdh->crashing_cpu = crashing_cpu;
564 crash_save_vmcoreinfo();
569 ppc_save_regs(&fdh->regs);
571 fdh->online_mask = *cpu_online_mask;
573 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
574 rtas_os_term((char *)str);
577 #define GPR_MASK 0xffffff0000000000
578 static inline int fadump_gpr_index(u64 id)
583 if ((id & GPR_MASK) == REG_ID("GPR")) {
584 /* get the digits at the end */
589 str[0] = (id >> 8) & 0xff;
590 sscanf(str, "%d", &i);
597 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
602 i = fadump_gpr_index(reg_id);
604 regs->gpr[i] = (unsigned long)reg_val;
605 else if (reg_id == REG_ID("NIA"))
606 regs->nip = (unsigned long)reg_val;
607 else if (reg_id == REG_ID("MSR"))
608 regs->msr = (unsigned long)reg_val;
609 else if (reg_id == REG_ID("CTR"))
610 regs->ctr = (unsigned long)reg_val;
611 else if (reg_id == REG_ID("LR"))
612 regs->link = (unsigned long)reg_val;
613 else if (reg_id == REG_ID("XER"))
614 regs->xer = (unsigned long)reg_val;
615 else if (reg_id == REG_ID("CR"))
616 regs->ccr = (unsigned long)reg_val;
617 else if (reg_id == REG_ID("DAR"))
618 regs->dar = (unsigned long)reg_val;
619 else if (reg_id == REG_ID("DSISR"))
620 regs->dsisr = (unsigned long)reg_val;
623 static struct fadump_reg_entry*
624 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
626 memset(regs, 0, sizeof(struct pt_regs));
628 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
629 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
630 be64_to_cpu(reg_entry->reg_value));
637 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
639 struct elf_prstatus prstatus;
641 memset(&prstatus, 0, sizeof(prstatus));
643 * FIXME: How do i get PID? Do I really need it?
644 * prstatus.pr_pid = ????
646 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
647 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
648 &prstatus, sizeof(prstatus));
652 static void fadump_update_elfcore_header(char *bufp)
655 struct elf_phdr *phdr;
657 elf = (struct elfhdr *)bufp;
658 bufp += sizeof(struct elfhdr);
660 /* First note is a place holder for cpu notes info. */
661 phdr = (struct elf_phdr *)bufp;
663 if (phdr->p_type == PT_NOTE) {
664 phdr->p_paddr = fw_dump.cpu_notes_buf;
665 phdr->p_offset = phdr->p_paddr;
666 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
667 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
672 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
676 unsigned long order, count, i;
678 order = get_order(size);
679 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
684 page = virt_to_page(vaddr);
685 for (i = 0; i < count; i++)
686 SetPageReserved(page + i);
690 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
693 unsigned long order, count, i;
695 order = get_order(size);
697 page = virt_to_page(vaddr);
698 for (i = 0; i < count; i++)
699 ClearPageReserved(page + i);
700 __free_pages(page, order);
704 * Read CPU state dump data and convert it into ELF notes.
705 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
706 * used to access the data to allow for additional fields to be added without
707 * affecting compatibility. Each list of registers for a CPU starts with
708 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
709 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
710 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
711 * of register value. For more details refer to PAPR document.
713 * Only for the crashing cpu we ignore the CPU dump data and get exact
714 * state from fadump crash info structure populated by first kernel at the
717 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
719 struct fadump_reg_save_area_header *reg_header;
720 struct fadump_reg_entry *reg_entry;
721 struct fadump_crash_info_header *fdh = NULL;
724 u32 num_cpus, *note_buf;
726 int i, rc = 0, cpu = 0;
728 if (!fdm->cpu_state_data.bytes_dumped)
731 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
735 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
736 printk(KERN_ERR "Unable to read register save area.\n");
739 pr_debug("--------CPU State Data------------\n");
740 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
741 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
743 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
744 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
745 pr_debug("NumCpus : %u\n", num_cpus);
746 vaddr += sizeof(u32);
747 reg_entry = (struct fadump_reg_entry *)vaddr;
749 /* Allocate buffer to hold cpu crash notes. */
750 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
751 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
752 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
754 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
755 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
758 fw_dump.cpu_notes_buf = __pa(note_buf);
760 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
761 (num_cpus * sizeof(note_buf_t)), note_buf);
763 if (fw_dump.fadumphdr_addr)
764 fdh = __va(fw_dump.fadumphdr_addr);
766 for (i = 0; i < num_cpus; i++) {
767 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
768 printk(KERN_ERR "Unable to read CPU state data\n");
772 /* Lower 4 bytes of reg_value contains logical cpu id */
773 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
774 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
775 SKIP_TO_NEXT_CPU(reg_entry);
778 pr_debug("Reading register data for cpu %d...\n", cpu);
779 if (fdh && fdh->crashing_cpu == cpu) {
781 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
782 SKIP_TO_NEXT_CPU(reg_entry);
785 reg_entry = fadump_read_registers(reg_entry, ®s);
786 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
789 final_note(note_buf);
792 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
793 fdh->elfcorehdr_addr);
794 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
799 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
800 fw_dump.cpu_notes_buf_size);
801 fw_dump.cpu_notes_buf = 0;
802 fw_dump.cpu_notes_buf_size = 0;
808 * Validate and process the dump data stored by firmware before exporting
809 * it through '/proc/vmcore'.
811 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
813 struct fadump_crash_info_header *fdh;
816 if (!fdm_active || !fw_dump.fadumphdr_addr)
819 /* Check if the dump data is valid. */
820 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
821 (fdm_active->cpu_state_data.error_flags != 0) ||
822 (fdm_active->rmr_region.error_flags != 0)) {
823 printk(KERN_ERR "Dump taken by platform is not valid\n");
826 if ((fdm_active->rmr_region.bytes_dumped !=
827 fdm_active->rmr_region.source_len) ||
828 !fdm_active->cpu_state_data.bytes_dumped) {
829 printk(KERN_ERR "Dump taken by platform is incomplete\n");
833 /* Validate the fadump crash info header */
834 fdh = __va(fw_dump.fadumphdr_addr);
835 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
836 printk(KERN_ERR "Crash info header is not valid.\n");
840 rc = fadump_build_cpu_notes(fdm_active);
845 * We are done validating dump info and elfcore header is now ready
846 * to be exported. set elfcorehdr_addr so that vmcore module will
847 * export the elfcore header through '/proc/vmcore'.
849 elfcorehdr_addr = fdh->elfcorehdr_addr;
854 static void free_crash_memory_ranges(void)
856 kfree(crash_memory_ranges);
857 crash_memory_ranges = NULL;
858 crash_memory_ranges_size = 0;
859 max_crash_mem_ranges = 0;
863 * Allocate or reallocate crash memory ranges array in incremental units
866 static int allocate_crash_memory_ranges(void)
868 struct fad_crash_memory_ranges *new_array;
871 new_size = crash_memory_ranges_size + PAGE_SIZE;
872 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
875 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
876 if (new_array == NULL) {
877 pr_err("Insufficient memory for setting up crash memory ranges\n");
878 free_crash_memory_ranges();
882 crash_memory_ranges = new_array;
883 crash_memory_ranges_size = new_size;
884 max_crash_mem_ranges = (new_size /
885 sizeof(struct fad_crash_memory_ranges));
889 static inline int fadump_add_crash_memory(unsigned long long base,
890 unsigned long long end)
895 if (crash_mem_ranges == max_crash_mem_ranges) {
898 ret = allocate_crash_memory_ranges();
903 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
904 crash_mem_ranges, base, end - 1, (end - base));
905 crash_memory_ranges[crash_mem_ranges].base = base;
906 crash_memory_ranges[crash_mem_ranges].size = end - base;
911 static int fadump_exclude_reserved_area(unsigned long long start,
912 unsigned long long end)
914 unsigned long long ra_start, ra_end;
917 ra_start = fw_dump.reserve_dump_area_start;
918 ra_end = ra_start + fw_dump.reserve_dump_area_size;
920 if ((ra_start < end) && (ra_end > start)) {
921 if ((start < ra_start) && (end > ra_end)) {
922 ret = fadump_add_crash_memory(start, ra_start);
926 ret = fadump_add_crash_memory(ra_end, end);
927 } else if (start < ra_start) {
928 ret = fadump_add_crash_memory(start, ra_start);
929 } else if (ra_end < end) {
930 ret = fadump_add_crash_memory(ra_end, end);
933 ret = fadump_add_crash_memory(start, end);
938 static int fadump_init_elfcore_header(char *bufp)
942 elf = (struct elfhdr *) bufp;
943 bufp += sizeof(struct elfhdr);
944 memcpy(elf->e_ident, ELFMAG, SELFMAG);
945 elf->e_ident[EI_CLASS] = ELF_CLASS;
946 elf->e_ident[EI_DATA] = ELF_DATA;
947 elf->e_ident[EI_VERSION] = EV_CURRENT;
948 elf->e_ident[EI_OSABI] = ELF_OSABI;
949 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
950 elf->e_type = ET_CORE;
951 elf->e_machine = ELF_ARCH;
952 elf->e_version = EV_CURRENT;
954 elf->e_phoff = sizeof(struct elfhdr);
956 #if defined(_CALL_ELF)
957 elf->e_flags = _CALL_ELF;
961 elf->e_ehsize = sizeof(struct elfhdr);
962 elf->e_phentsize = sizeof(struct elf_phdr);
964 elf->e_shentsize = 0;
972 * Traverse through memblock structure and setup crash memory ranges. These
973 * ranges will be used create PT_LOAD program headers in elfcore header.
975 static int fadump_setup_crash_memory_ranges(void)
977 struct memblock_region *reg;
978 unsigned long long start, end;
981 pr_debug("Setup crash memory ranges.\n");
982 crash_mem_ranges = 0;
984 * add the first memory chunk (RMA_START through boot_memory_size) as
985 * a separate memory chunk. The reason is, at the time crash firmware
986 * will move the content of this memory chunk to different location
987 * specified during fadump registration. We need to create a separate
988 * program header for this chunk with the correct offset.
990 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
994 for_each_memblock(memory, reg) {
995 start = (unsigned long long)reg->base;
996 end = start + (unsigned long long)reg->size;
999 * skip the first memory chunk that is already added (RMA_START
1000 * through boot_memory_size). This logic needs a relook if and
1001 * when RMA_START changes to a non-zero value.
1003 BUILD_BUG_ON(RMA_START != 0);
1004 if (start < fw_dump.boot_memory_size) {
1005 if (end > fw_dump.boot_memory_size)
1006 start = fw_dump.boot_memory_size;
1011 /* add this range excluding the reserved dump area. */
1012 ret = fadump_exclude_reserved_area(start, end);
1021 * If the given physical address falls within the boot memory region then
1022 * return the relocated address that points to the dump region reserved
1023 * for saving initial boot memory contents.
1025 static inline unsigned long fadump_relocate(unsigned long paddr)
1027 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
1028 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
1033 static int fadump_create_elfcore_headers(char *bufp)
1036 struct elf_phdr *phdr;
1039 fadump_init_elfcore_header(bufp);
1040 elf = (struct elfhdr *)bufp;
1041 bufp += sizeof(struct elfhdr);
1044 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1045 * will be populated during second kernel boot after crash. Hence
1046 * this PT_NOTE will always be the first elf note.
1048 * NOTE: Any new ELF note addition should be placed after this note.
1050 phdr = (struct elf_phdr *)bufp;
1051 bufp += sizeof(struct elf_phdr);
1052 phdr->p_type = PT_NOTE;
1064 /* setup ELF PT_NOTE for vmcoreinfo */
1065 phdr = (struct elf_phdr *)bufp;
1066 bufp += sizeof(struct elf_phdr);
1067 phdr->p_type = PT_NOTE;
1072 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
1073 phdr->p_offset = phdr->p_paddr;
1074 phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1076 /* Increment number of program headers. */
1079 /* setup PT_LOAD sections. */
1081 for (i = 0; i < crash_mem_ranges; i++) {
1082 unsigned long long mbase, msize;
1083 mbase = crash_memory_ranges[i].base;
1084 msize = crash_memory_ranges[i].size;
1089 phdr = (struct elf_phdr *)bufp;
1090 bufp += sizeof(struct elf_phdr);
1091 phdr->p_type = PT_LOAD;
1092 phdr->p_flags = PF_R|PF_W|PF_X;
1093 phdr->p_offset = mbase;
1095 if (mbase == RMA_START) {
1097 * The entire RMA region will be moved by firmware
1098 * to the specified destination_address. Hence set
1099 * the correct offset.
1101 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
1104 phdr->p_paddr = mbase;
1105 phdr->p_vaddr = (unsigned long)__va(mbase);
1106 phdr->p_filesz = msize;
1107 phdr->p_memsz = msize;
1110 /* Increment number of program headers. */
1116 static unsigned long init_fadump_header(unsigned long addr)
1118 struct fadump_crash_info_header *fdh;
1123 fw_dump.fadumphdr_addr = addr;
1125 addr += sizeof(struct fadump_crash_info_header);
1127 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1128 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1129 fdh->elfcorehdr_addr = addr;
1130 /* We will set the crashing cpu id in crash_fadump() during crash. */
1131 fdh->crashing_cpu = CPU_UNKNOWN;
1136 static int register_fadump(void)
1143 * If no memory is reserved then we can not register for firmware-
1146 if (!fw_dump.reserve_dump_area_size)
1149 ret = fadump_setup_crash_memory_ranges();
1153 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
1154 /* Initialize fadump crash info header. */
1155 addr = init_fadump_header(addr);
1158 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1159 fadump_create_elfcore_headers(vaddr);
1161 /* register the future kernel dump with firmware. */
1162 return register_fw_dump(&fdm);
1165 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
1168 unsigned int wait_time;
1170 pr_debug("Un-register firmware-assisted dump\n");
1172 /* TODO: Add upper time limit for the delay */
1174 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1175 FADUMP_UNREGISTER, fdm,
1176 sizeof(struct fadump_mem_struct));
1178 wait_time = rtas_busy_delay_time(rc);
1181 } while (wait_time);
1184 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1185 " unexpected error(%d).\n", rc);
1188 fw_dump.dump_registered = 0;
1192 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1195 unsigned int wait_time;
1197 pr_debug("Invalidating firmware-assisted dump registration\n");
1199 /* TODO: Add upper time limit for the delay */
1201 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1202 FADUMP_INVALIDATE, fdm,
1203 sizeof(struct fadump_mem_struct));
1205 wait_time = rtas_busy_delay_time(rc);
1208 } while (wait_time);
1211 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1214 fw_dump.dump_active = 0;
1219 void fadump_cleanup(void)
1221 /* Invalidate the registration only if dump is active. */
1222 if (fw_dump.dump_active) {
1223 init_fadump_mem_struct(&fdm,
1224 be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1225 fadump_invalidate_dump(&fdm);
1226 } else if (fw_dump.dump_registered) {
1227 /* Un-register Firmware-assisted dump if it was registered. */
1228 fadump_unregister_dump(&fdm);
1229 free_crash_memory_ranges();
1233 static void fadump_free_reserved_memory(unsigned long start_pfn,
1234 unsigned long end_pfn)
1237 unsigned long time_limit = jiffies + HZ;
1239 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1240 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1242 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1243 free_reserved_page(pfn_to_page(pfn));
1245 if (time_after(jiffies, time_limit)) {
1247 time_limit = jiffies + HZ;
1253 * Skip memory holes and free memory that was actually reserved.
1255 static void fadump_release_reserved_area(unsigned long start, unsigned long end)
1257 struct memblock_region *reg;
1258 unsigned long tstart, tend;
1259 unsigned long start_pfn = PHYS_PFN(start);
1260 unsigned long end_pfn = PHYS_PFN(end);
1262 for_each_memblock(memory, reg) {
1263 tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
1264 tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
1265 if (tstart < tend) {
1266 fadump_free_reserved_memory(tstart, tend);
1268 if (tend == end_pfn)
1271 start_pfn = tend + 1;
1277 * Release the memory that was reserved in early boot to preserve the memory
1278 * contents. The released memory will be available for general use.
1280 static void fadump_release_memory(unsigned long begin, unsigned long end)
1282 unsigned long ra_start, ra_end;
1284 ra_start = fw_dump.reserve_dump_area_start;
1285 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1288 * exclude the dump reserve area. Will reuse it for next
1289 * fadump registration.
1291 if (begin < ra_end && end > ra_start) {
1292 if (begin < ra_start)
1293 fadump_release_reserved_area(begin, ra_start);
1295 fadump_release_reserved_area(ra_end, end);
1297 fadump_release_reserved_area(begin, end);
1300 static void fadump_invalidate_release_mem(void)
1302 unsigned long reserved_area_start, reserved_area_end;
1303 unsigned long destination_address;
1305 mutex_lock(&fadump_mutex);
1306 if (!fw_dump.dump_active) {
1307 mutex_unlock(&fadump_mutex);
1311 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1313 mutex_unlock(&fadump_mutex);
1316 * Save the current reserved memory bounds we will require them
1317 * later for releasing the memory for general use.
1319 reserved_area_start = fw_dump.reserve_dump_area_start;
1320 reserved_area_end = reserved_area_start +
1321 fw_dump.reserve_dump_area_size;
1323 * Setup reserve_dump_area_start and its size so that we can
1324 * reuse this reserved memory for Re-registration.
1326 fw_dump.reserve_dump_area_start = destination_address;
1327 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1329 fadump_release_memory(reserved_area_start, reserved_area_end);
1330 if (fw_dump.cpu_notes_buf) {
1331 fadump_cpu_notes_buf_free(
1332 (unsigned long)__va(fw_dump.cpu_notes_buf),
1333 fw_dump.cpu_notes_buf_size);
1334 fw_dump.cpu_notes_buf = 0;
1335 fw_dump.cpu_notes_buf_size = 0;
1337 /* Initialize the kernel dump memory structure for FAD registration. */
1338 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1341 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1342 struct kobj_attribute *attr,
1343 const char *buf, size_t count)
1345 if (!fw_dump.dump_active)
1348 if (buf[0] == '1') {
1350 * Take away the '/proc/vmcore'. We are releasing the dump
1351 * memory, hence it will not be valid anymore.
1353 #ifdef CONFIG_PROC_VMCORE
1356 fadump_invalidate_release_mem();
1363 static ssize_t fadump_enabled_show(struct kobject *kobj,
1364 struct kobj_attribute *attr,
1367 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1370 static ssize_t fadump_register_show(struct kobject *kobj,
1371 struct kobj_attribute *attr,
1374 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1377 static ssize_t fadump_register_store(struct kobject *kobj,
1378 struct kobj_attribute *attr,
1379 const char *buf, size_t count)
1383 if (!fw_dump.fadump_enabled || fdm_active)
1386 mutex_lock(&fadump_mutex);
1390 if (fw_dump.dump_registered == 0) {
1393 /* Un-register Firmware-assisted dump */
1394 fadump_unregister_dump(&fdm);
1397 if (fw_dump.dump_registered == 1) {
1401 /* Register Firmware-assisted dump */
1402 ret = register_fadump();
1410 mutex_unlock(&fadump_mutex);
1411 return ret < 0 ? ret : count;
1414 static int fadump_region_show(struct seq_file *m, void *private)
1416 const struct fadump_mem_struct *fdm_ptr;
1418 if (!fw_dump.fadump_enabled)
1421 mutex_lock(&fadump_mutex);
1423 fdm_ptr = fdm_active;
1425 mutex_unlock(&fadump_mutex);
1430 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1432 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1433 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1434 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1435 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1436 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1438 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1440 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1441 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1442 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1443 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1444 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1446 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1448 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1449 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1450 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1451 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1452 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1455 (fw_dump.reserve_dump_area_start ==
1456 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1459 /* Dump is active. Show reserved memory region. */
1461 " : [%#016llx-%#016llx] %#llx bytes, "
1463 (unsigned long long)fw_dump.reserve_dump_area_start,
1464 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1465 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1466 fw_dump.reserve_dump_area_start,
1467 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1468 fw_dump.reserve_dump_area_start);
1471 mutex_unlock(&fadump_mutex);
1475 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1477 fadump_release_memory_store);
1478 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1479 0444, fadump_enabled_show,
1481 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1482 0644, fadump_register_show,
1483 fadump_register_store);
1485 static int fadump_region_open(struct inode *inode, struct file *file)
1487 return single_open(file, fadump_region_show, inode->i_private);
1490 static const struct file_operations fadump_region_fops = {
1491 .open = fadump_region_open,
1493 .llseek = seq_lseek,
1494 .release = single_release,
1497 static void fadump_init_files(void)
1499 struct dentry *debugfs_file;
1502 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1504 printk(KERN_ERR "fadump: unable to create sysfs file"
1505 " fadump_enabled (%d)\n", rc);
1507 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1509 printk(KERN_ERR "fadump: unable to create sysfs file"
1510 " fadump_registered (%d)\n", rc);
1512 debugfs_file = debugfs_create_file("fadump_region", 0444,
1513 powerpc_debugfs_root, NULL,
1514 &fadump_region_fops);
1516 printk(KERN_ERR "fadump: unable to create debugfs file"
1517 " fadump_region\n");
1519 if (fw_dump.dump_active) {
1520 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1522 printk(KERN_ERR "fadump: unable to create sysfs file"
1523 " fadump_release_mem (%d)\n", rc);
1529 * Prepare for firmware-assisted dump.
1531 int __init setup_fadump(void)
1533 if (!fw_dump.fadump_enabled)
1536 if (!fw_dump.fadump_supported) {
1537 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1538 " this hardware\n");
1542 fadump_show_config();
1544 * If dump data is available then see if it is valid and prepare for
1545 * saving it to the disk.
1547 if (fw_dump.dump_active) {
1549 * if dump process fails then invalidate the registration
1550 * and release memory before proceeding for re-registration.
1552 if (process_fadump(fdm_active) < 0)
1553 fadump_invalidate_release_mem();
1555 /* Initialize the kernel dump memory structure for FAD registration. */
1556 else if (fw_dump.reserve_dump_area_size)
1557 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1558 fadump_init_files();
1562 subsys_initcall(setup_fadump);