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;
216 * Check if the size is specified through crashkernel= cmdline
217 * option. If yes, then use that but ignore base as fadump
218 * reserves memory at end of RAM.
220 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
222 if (ret == 0 && size > 0) {
223 fw_dump.reserve_bootvar = (unsigned long)size;
224 return fw_dump.reserve_bootvar;
227 /* divide by 20 to get 5% of value */
228 size = memblock_end_of_DRAM() / 20;
230 /* round it down in multiples of 256 */
231 size = size & ~0x0FFFFFFFUL;
233 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
234 if (memory_limit && size > memory_limit)
237 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
241 * Calculate the total memory size required to be reserved for
242 * firmware-assisted dump registration.
244 static unsigned long get_fadump_area_size(void)
246 unsigned long size = 0;
248 size += fw_dump.cpu_state_data_size;
249 size += fw_dump.hpte_region_size;
250 size += fw_dump.boot_memory_size;
251 size += sizeof(struct fadump_crash_info_header);
252 size += sizeof(struct elfhdr); /* ELF core header.*/
253 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
254 /* Program headers for crash memory regions. */
255 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
257 size = PAGE_ALIGN(size);
261 int __init fadump_reserve_mem(void)
263 unsigned long base, size, memory_boundary;
265 if (!fw_dump.fadump_enabled)
268 if (!fw_dump.fadump_supported) {
269 printk(KERN_INFO "Firmware-assisted dump is not supported on"
271 fw_dump.fadump_enabled = 0;
275 * Initialize boot memory size
276 * If dump is active then we have already calculated the size during
280 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
282 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
285 * Calculate the memory boundary.
286 * If memory_limit is less than actual memory boundary then reserve
287 * the memory for fadump beyond the memory_limit and adjust the
288 * memory_limit accordingly, so that the running kernel can run with
289 * specified memory_limit.
291 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
292 size = get_fadump_area_size();
293 if ((memory_limit + size) < memblock_end_of_DRAM())
294 memory_limit += size;
296 memory_limit = memblock_end_of_DRAM();
297 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
298 " dump, now %#016llx\n", memory_limit);
301 memory_boundary = memory_limit;
303 memory_boundary = memblock_end_of_DRAM();
305 if (fw_dump.dump_active) {
306 printk(KERN_INFO "Firmware-assisted dump is active.\n");
308 * If last boot has crashed then reserve all the memory
309 * above boot_memory_size so that we don't touch it until
310 * dump is written to disk by userspace tool. This memory
311 * will be released for general use once the dump is saved.
313 base = fw_dump.boot_memory_size;
314 size = memory_boundary - base;
315 memblock_reserve(base, size);
316 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
317 "for saving crash dump\n",
318 (unsigned long)(size >> 20),
319 (unsigned long)(base >> 20));
321 fw_dump.fadumphdr_addr =
322 be64_to_cpu(fdm_active->rmr_region.destination_address) +
323 be64_to_cpu(fdm_active->rmr_region.source_len);
324 pr_debug("fadumphdr_addr = %p\n",
325 (void *) fw_dump.fadumphdr_addr);
327 size = get_fadump_area_size();
330 * Reserve memory at an offset closer to bottom of the RAM to
331 * minimize the impact of memory hot-remove operation. We can't
332 * use memblock_find_in_range() here since it doesn't allocate
333 * from bottom to top.
335 for (base = fw_dump.boot_memory_size;
336 base <= (memory_boundary - size);
338 if (memblock_is_region_memory(base, size) &&
339 !memblock_is_region_reserved(base, size))
342 if ((base > (memory_boundary - size)) ||
343 memblock_reserve(base, size)) {
344 pr_err("Failed to reserve memory\n");
348 pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
349 "assisted dump (System RAM: %ldMB)\n",
350 (unsigned long)(size >> 20),
351 (unsigned long)(base >> 20),
352 (unsigned long)(memblock_phys_mem_size() >> 20));
355 fw_dump.reserve_dump_area_start = base;
356 fw_dump.reserve_dump_area_size = size;
360 unsigned long __init arch_reserved_kernel_pages(void)
362 return memblock_reserved_size() / PAGE_SIZE;
365 /* Look for fadump= cmdline option. */
366 static int __init early_fadump_param(char *p)
371 if (strncmp(p, "on", 2) == 0)
372 fw_dump.fadump_enabled = 1;
373 else if (strncmp(p, "off", 3) == 0)
374 fw_dump.fadump_enabled = 0;
378 early_param("fadump", early_fadump_param);
380 static void register_fw_dump(struct fadump_mem_struct *fdm)
383 unsigned int wait_time;
385 pr_debug("Registering for firmware-assisted kernel dump...\n");
387 /* TODO: Add upper time limit for the delay */
389 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
390 FADUMP_REGISTER, fdm,
391 sizeof(struct fadump_mem_struct));
393 wait_time = rtas_busy_delay_time(rc);
401 printk(KERN_ERR "Failed to register firmware-assisted kernel"
402 " dump. Hardware Error(%d).\n", rc);
405 printk(KERN_ERR "Failed to register firmware-assisted kernel"
406 " dump. Parameter Error(%d).\n", rc);
409 printk(KERN_ERR "firmware-assisted kernel dump is already "
411 fw_dump.dump_registered = 1;
414 printk(KERN_INFO "firmware-assisted kernel dump registration"
416 fw_dump.dump_registered = 1;
421 void crash_fadump(struct pt_regs *regs, const char *str)
423 struct fadump_crash_info_header *fdh = NULL;
424 int old_cpu, this_cpu;
426 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
430 * old_cpu == -1 means this is the first CPU which has come here,
431 * go ahead and trigger fadump.
433 * old_cpu != -1 means some other CPU has already on it's way
434 * to trigger fadump, just keep looping here.
436 this_cpu = smp_processor_id();
437 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
441 * We can't loop here indefinitely. Wait as long as fadump
442 * is in force. If we race with fadump un-registration this
443 * loop will break and then we go down to normal panic path
444 * and reboot. If fadump is in force the first crashing
445 * cpu will definitely trigger fadump.
447 while (fw_dump.dump_registered)
452 fdh = __va(fw_dump.fadumphdr_addr);
453 fdh->crashing_cpu = crashing_cpu;
454 crash_save_vmcoreinfo();
459 ppc_save_regs(&fdh->regs);
461 fdh->online_mask = *cpu_online_mask;
463 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
464 rtas_os_term((char *)str);
467 #define GPR_MASK 0xffffff0000000000
468 static inline int fadump_gpr_index(u64 id)
473 if ((id & GPR_MASK) == REG_ID("GPR")) {
474 /* get the digits at the end */
479 str[0] = (id >> 8) & 0xff;
480 sscanf(str, "%d", &i);
487 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
492 i = fadump_gpr_index(reg_id);
494 regs->gpr[i] = (unsigned long)reg_val;
495 else if (reg_id == REG_ID("NIA"))
496 regs->nip = (unsigned long)reg_val;
497 else if (reg_id == REG_ID("MSR"))
498 regs->msr = (unsigned long)reg_val;
499 else if (reg_id == REG_ID("CTR"))
500 regs->ctr = (unsigned long)reg_val;
501 else if (reg_id == REG_ID("LR"))
502 regs->link = (unsigned long)reg_val;
503 else if (reg_id == REG_ID("XER"))
504 regs->xer = (unsigned long)reg_val;
505 else if (reg_id == REG_ID("CR"))
506 regs->ccr = (unsigned long)reg_val;
507 else if (reg_id == REG_ID("DAR"))
508 regs->dar = (unsigned long)reg_val;
509 else if (reg_id == REG_ID("DSISR"))
510 regs->dsisr = (unsigned long)reg_val;
513 static struct fadump_reg_entry*
514 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
516 memset(regs, 0, sizeof(struct pt_regs));
518 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
519 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
520 be64_to_cpu(reg_entry->reg_value));
527 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
529 struct elf_prstatus prstatus;
531 memset(&prstatus, 0, sizeof(prstatus));
533 * FIXME: How do i get PID? Do I really need it?
534 * prstatus.pr_pid = ????
536 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
537 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
538 &prstatus, sizeof(prstatus));
542 static void fadump_update_elfcore_header(char *bufp)
545 struct elf_phdr *phdr;
547 elf = (struct elfhdr *)bufp;
548 bufp += sizeof(struct elfhdr);
550 /* First note is a place holder for cpu notes info. */
551 phdr = (struct elf_phdr *)bufp;
553 if (phdr->p_type == PT_NOTE) {
554 phdr->p_paddr = fw_dump.cpu_notes_buf;
555 phdr->p_offset = phdr->p_paddr;
556 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
557 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
562 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
566 unsigned long order, count, i;
568 order = get_order(size);
569 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
574 page = virt_to_page(vaddr);
575 for (i = 0; i < count; i++)
576 SetPageReserved(page + i);
580 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
583 unsigned long order, count, i;
585 order = get_order(size);
587 page = virt_to_page(vaddr);
588 for (i = 0; i < count; i++)
589 ClearPageReserved(page + i);
590 __free_pages(page, order);
594 * Read CPU state dump data and convert it into ELF notes.
595 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
596 * used to access the data to allow for additional fields to be added without
597 * affecting compatibility. Each list of registers for a CPU starts with
598 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
599 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
600 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
601 * of register value. For more details refer to PAPR document.
603 * Only for the crashing cpu we ignore the CPU dump data and get exact
604 * state from fadump crash info structure populated by first kernel at the
607 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
609 struct fadump_reg_save_area_header *reg_header;
610 struct fadump_reg_entry *reg_entry;
611 struct fadump_crash_info_header *fdh = NULL;
614 u32 num_cpus, *note_buf;
616 int i, rc = 0, cpu = 0;
618 if (!fdm->cpu_state_data.bytes_dumped)
621 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
625 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
626 printk(KERN_ERR "Unable to read register save area.\n");
629 pr_debug("--------CPU State Data------------\n");
630 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
631 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
633 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
634 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
635 pr_debug("NumCpus : %u\n", num_cpus);
636 vaddr += sizeof(u32);
637 reg_entry = (struct fadump_reg_entry *)vaddr;
639 /* Allocate buffer to hold cpu crash notes. */
640 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
641 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
642 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
644 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
645 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
648 fw_dump.cpu_notes_buf = __pa(note_buf);
650 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
651 (num_cpus * sizeof(note_buf_t)), note_buf);
653 if (fw_dump.fadumphdr_addr)
654 fdh = __va(fw_dump.fadumphdr_addr);
656 for (i = 0; i < num_cpus; i++) {
657 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
658 printk(KERN_ERR "Unable to read CPU state data\n");
662 /* Lower 4 bytes of reg_value contains logical cpu id */
663 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
664 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
665 SKIP_TO_NEXT_CPU(reg_entry);
668 pr_debug("Reading register data for cpu %d...\n", cpu);
669 if (fdh && fdh->crashing_cpu == cpu) {
671 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
672 SKIP_TO_NEXT_CPU(reg_entry);
675 reg_entry = fadump_read_registers(reg_entry, ®s);
676 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
679 final_note(note_buf);
682 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
683 fdh->elfcorehdr_addr);
684 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
689 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
690 fw_dump.cpu_notes_buf_size);
691 fw_dump.cpu_notes_buf = 0;
692 fw_dump.cpu_notes_buf_size = 0;
698 * Validate and process the dump data stored by firmware before exporting
699 * it through '/proc/vmcore'.
701 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
703 struct fadump_crash_info_header *fdh;
706 if (!fdm_active || !fw_dump.fadumphdr_addr)
709 /* Check if the dump data is valid. */
710 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
711 (fdm_active->cpu_state_data.error_flags != 0) ||
712 (fdm_active->rmr_region.error_flags != 0)) {
713 printk(KERN_ERR "Dump taken by platform is not valid\n");
716 if ((fdm_active->rmr_region.bytes_dumped !=
717 fdm_active->rmr_region.source_len) ||
718 !fdm_active->cpu_state_data.bytes_dumped) {
719 printk(KERN_ERR "Dump taken by platform is incomplete\n");
723 /* Validate the fadump crash info header */
724 fdh = __va(fw_dump.fadumphdr_addr);
725 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
726 printk(KERN_ERR "Crash info header is not valid.\n");
730 rc = fadump_build_cpu_notes(fdm_active);
735 * We are done validating dump info and elfcore header is now ready
736 * to be exported. set elfcorehdr_addr so that vmcore module will
737 * export the elfcore header through '/proc/vmcore'.
739 elfcorehdr_addr = fdh->elfcorehdr_addr;
744 static inline void fadump_add_crash_memory(unsigned long long base,
745 unsigned long long end)
750 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
751 crash_mem_ranges, base, end - 1, (end - base));
752 crash_memory_ranges[crash_mem_ranges].base = base;
753 crash_memory_ranges[crash_mem_ranges].size = end - base;
757 static void fadump_exclude_reserved_area(unsigned long long start,
758 unsigned long long end)
760 unsigned long long ra_start, ra_end;
762 ra_start = fw_dump.reserve_dump_area_start;
763 ra_end = ra_start + fw_dump.reserve_dump_area_size;
765 if ((ra_start < end) && (ra_end > start)) {
766 if ((start < ra_start) && (end > ra_end)) {
767 fadump_add_crash_memory(start, ra_start);
768 fadump_add_crash_memory(ra_end, end);
769 } else if (start < ra_start) {
770 fadump_add_crash_memory(start, ra_start);
771 } else if (ra_end < end) {
772 fadump_add_crash_memory(ra_end, end);
775 fadump_add_crash_memory(start, end);
778 static int fadump_init_elfcore_header(char *bufp)
782 elf = (struct elfhdr *) bufp;
783 bufp += sizeof(struct elfhdr);
784 memcpy(elf->e_ident, ELFMAG, SELFMAG);
785 elf->e_ident[EI_CLASS] = ELF_CLASS;
786 elf->e_ident[EI_DATA] = ELF_DATA;
787 elf->e_ident[EI_VERSION] = EV_CURRENT;
788 elf->e_ident[EI_OSABI] = ELF_OSABI;
789 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
790 elf->e_type = ET_CORE;
791 elf->e_machine = ELF_ARCH;
792 elf->e_version = EV_CURRENT;
794 elf->e_phoff = sizeof(struct elfhdr);
796 #if defined(_CALL_ELF)
797 elf->e_flags = _CALL_ELF;
801 elf->e_ehsize = sizeof(struct elfhdr);
802 elf->e_phentsize = sizeof(struct elf_phdr);
804 elf->e_shentsize = 0;
812 * Traverse through memblock structure and setup crash memory ranges. These
813 * ranges will be used create PT_LOAD program headers in elfcore header.
815 static void fadump_setup_crash_memory_ranges(void)
817 struct memblock_region *reg;
818 unsigned long long start, end;
820 pr_debug("Setup crash memory ranges.\n");
821 crash_mem_ranges = 0;
823 * add the first memory chunk (RMA_START through boot_memory_size) as
824 * a separate memory chunk. The reason is, at the time crash firmware
825 * will move the content of this memory chunk to different location
826 * specified during fadump registration. We need to create a separate
827 * program header for this chunk with the correct offset.
829 fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
831 for_each_memblock(memory, reg) {
832 start = (unsigned long long)reg->base;
833 end = start + (unsigned long long)reg->size;
834 if (start == RMA_START && end >= fw_dump.boot_memory_size)
835 start = fw_dump.boot_memory_size;
837 /* add this range excluding the reserved dump area. */
838 fadump_exclude_reserved_area(start, end);
843 * If the given physical address falls within the boot memory region then
844 * return the relocated address that points to the dump region reserved
845 * for saving initial boot memory contents.
847 static inline unsigned long fadump_relocate(unsigned long paddr)
849 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
850 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
855 static int fadump_create_elfcore_headers(char *bufp)
858 struct elf_phdr *phdr;
861 fadump_init_elfcore_header(bufp);
862 elf = (struct elfhdr *)bufp;
863 bufp += sizeof(struct elfhdr);
866 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
867 * will be populated during second kernel boot after crash. Hence
868 * this PT_NOTE will always be the first elf note.
870 * NOTE: Any new ELF note addition should be placed after this note.
872 phdr = (struct elf_phdr *)bufp;
873 bufp += sizeof(struct elf_phdr);
874 phdr->p_type = PT_NOTE;
886 /* setup ELF PT_NOTE for vmcoreinfo */
887 phdr = (struct elf_phdr *)bufp;
888 bufp += sizeof(struct elf_phdr);
889 phdr->p_type = PT_NOTE;
894 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
895 phdr->p_offset = phdr->p_paddr;
896 phdr->p_memsz = vmcoreinfo_max_size;
897 phdr->p_filesz = vmcoreinfo_max_size;
899 /* Increment number of program headers. */
902 /* setup PT_LOAD sections. */
904 for (i = 0; i < crash_mem_ranges; i++) {
905 unsigned long long mbase, msize;
906 mbase = crash_memory_ranges[i].base;
907 msize = crash_memory_ranges[i].size;
912 phdr = (struct elf_phdr *)bufp;
913 bufp += sizeof(struct elf_phdr);
914 phdr->p_type = PT_LOAD;
915 phdr->p_flags = PF_R|PF_W|PF_X;
916 phdr->p_offset = mbase;
918 if (mbase == RMA_START) {
920 * The entire RMA region will be moved by firmware
921 * to the specified destination_address. Hence set
922 * the correct offset.
924 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
927 phdr->p_paddr = mbase;
928 phdr->p_vaddr = (unsigned long)__va(mbase);
929 phdr->p_filesz = msize;
930 phdr->p_memsz = msize;
933 /* Increment number of program headers. */
939 static unsigned long init_fadump_header(unsigned long addr)
941 struct fadump_crash_info_header *fdh;
946 fw_dump.fadumphdr_addr = addr;
948 addr += sizeof(struct fadump_crash_info_header);
950 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
951 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
952 fdh->elfcorehdr_addr = addr;
953 /* We will set the crashing cpu id in crash_fadump() during crash. */
954 fdh->crashing_cpu = CPU_UNKNOWN;
959 static void register_fadump(void)
965 * If no memory is reserved then we can not register for firmware-
968 if (!fw_dump.reserve_dump_area_size)
971 fadump_setup_crash_memory_ranges();
973 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
974 /* Initialize fadump crash info header. */
975 addr = init_fadump_header(addr);
978 pr_debug("Creating ELF core headers at %#016lx\n", addr);
979 fadump_create_elfcore_headers(vaddr);
981 /* register the future kernel dump with firmware. */
982 register_fw_dump(&fdm);
985 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
988 unsigned int wait_time;
990 pr_debug("Un-register firmware-assisted dump\n");
992 /* TODO: Add upper time limit for the delay */
994 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
995 FADUMP_UNREGISTER, fdm,
996 sizeof(struct fadump_mem_struct));
998 wait_time = rtas_busy_delay_time(rc);
1001 } while (wait_time);
1004 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1005 " unexpected error(%d).\n", rc);
1008 fw_dump.dump_registered = 0;
1012 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1015 unsigned int wait_time;
1017 pr_debug("Invalidating firmware-assisted dump registration\n");
1019 /* TODO: Add upper time limit for the delay */
1021 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1022 FADUMP_INVALIDATE, fdm,
1023 sizeof(struct fadump_mem_struct));
1025 wait_time = rtas_busy_delay_time(rc);
1028 } while (wait_time);
1031 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1034 fw_dump.dump_active = 0;
1039 void fadump_cleanup(void)
1041 /* Invalidate the registration only if dump is active. */
1042 if (fw_dump.dump_active) {
1043 init_fadump_mem_struct(&fdm,
1044 be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1045 fadump_invalidate_dump(&fdm);
1050 * Release the memory that was reserved in early boot to preserve the memory
1051 * contents. The released memory will be available for general use.
1053 static void fadump_release_memory(unsigned long begin, unsigned long end)
1056 unsigned long ra_start, ra_end;
1058 ra_start = fw_dump.reserve_dump_area_start;
1059 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1061 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1063 * exclude the dump reserve area. Will reuse it for next
1064 * fadump registration.
1066 if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1069 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1073 static void fadump_invalidate_release_mem(void)
1075 unsigned long reserved_area_start, reserved_area_end;
1076 unsigned long destination_address;
1078 mutex_lock(&fadump_mutex);
1079 if (!fw_dump.dump_active) {
1080 mutex_unlock(&fadump_mutex);
1084 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1086 mutex_unlock(&fadump_mutex);
1089 * Save the current reserved memory bounds we will require them
1090 * later for releasing the memory for general use.
1092 reserved_area_start = fw_dump.reserve_dump_area_start;
1093 reserved_area_end = reserved_area_start +
1094 fw_dump.reserve_dump_area_size;
1096 * Setup reserve_dump_area_start and its size so that we can
1097 * reuse this reserved memory for Re-registration.
1099 fw_dump.reserve_dump_area_start = destination_address;
1100 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1102 fadump_release_memory(reserved_area_start, reserved_area_end);
1103 if (fw_dump.cpu_notes_buf) {
1104 fadump_cpu_notes_buf_free(
1105 (unsigned long)__va(fw_dump.cpu_notes_buf),
1106 fw_dump.cpu_notes_buf_size);
1107 fw_dump.cpu_notes_buf = 0;
1108 fw_dump.cpu_notes_buf_size = 0;
1110 /* Initialize the kernel dump memory structure for FAD registration. */
1111 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1114 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1115 struct kobj_attribute *attr,
1116 const char *buf, size_t count)
1118 if (!fw_dump.dump_active)
1121 if (buf[0] == '1') {
1123 * Take away the '/proc/vmcore'. We are releasing the dump
1124 * memory, hence it will not be valid anymore.
1126 #ifdef CONFIG_PROC_VMCORE
1129 fadump_invalidate_release_mem();
1136 static ssize_t fadump_enabled_show(struct kobject *kobj,
1137 struct kobj_attribute *attr,
1140 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1143 static ssize_t fadump_register_show(struct kobject *kobj,
1144 struct kobj_attribute *attr,
1147 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1150 static ssize_t fadump_register_store(struct kobject *kobj,
1151 struct kobj_attribute *attr,
1152 const char *buf, size_t count)
1156 if (!fw_dump.fadump_enabled || fdm_active)
1159 mutex_lock(&fadump_mutex);
1163 if (fw_dump.dump_registered == 0) {
1167 /* Un-register Firmware-assisted dump */
1168 fadump_unregister_dump(&fdm);
1171 if (fw_dump.dump_registered == 1) {
1175 /* Register Firmware-assisted dump */
1184 mutex_unlock(&fadump_mutex);
1185 return ret < 0 ? ret : count;
1188 static int fadump_region_show(struct seq_file *m, void *private)
1190 const struct fadump_mem_struct *fdm_ptr;
1192 if (!fw_dump.fadump_enabled)
1195 mutex_lock(&fadump_mutex);
1197 fdm_ptr = fdm_active;
1199 mutex_unlock(&fadump_mutex);
1204 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1206 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1207 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1208 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1209 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1210 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1212 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1214 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1215 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1216 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1217 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1218 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1220 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1222 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1223 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1224 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1225 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1226 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1229 (fw_dump.reserve_dump_area_start ==
1230 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1233 /* Dump is active. Show reserved memory region. */
1235 " : [%#016llx-%#016llx] %#llx bytes, "
1237 (unsigned long long)fw_dump.reserve_dump_area_start,
1238 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1239 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1240 fw_dump.reserve_dump_area_start,
1241 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1242 fw_dump.reserve_dump_area_start);
1245 mutex_unlock(&fadump_mutex);
1249 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1251 fadump_release_memory_store);
1252 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1253 0444, fadump_enabled_show,
1255 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1256 0644, fadump_register_show,
1257 fadump_register_store);
1259 static int fadump_region_open(struct inode *inode, struct file *file)
1261 return single_open(file, fadump_region_show, inode->i_private);
1264 static const struct file_operations fadump_region_fops = {
1265 .open = fadump_region_open,
1267 .llseek = seq_lseek,
1268 .release = single_release,
1271 static void fadump_init_files(void)
1273 struct dentry *debugfs_file;
1276 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1278 printk(KERN_ERR "fadump: unable to create sysfs file"
1279 " fadump_enabled (%d)\n", rc);
1281 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1283 printk(KERN_ERR "fadump: unable to create sysfs file"
1284 " fadump_registered (%d)\n", rc);
1286 debugfs_file = debugfs_create_file("fadump_region", 0444,
1287 powerpc_debugfs_root, NULL,
1288 &fadump_region_fops);
1290 printk(KERN_ERR "fadump: unable to create debugfs file"
1291 " fadump_region\n");
1293 if (fw_dump.dump_active) {
1294 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1296 printk(KERN_ERR "fadump: unable to create sysfs file"
1297 " fadump_release_mem (%d)\n", rc);
1303 * Prepare for firmware-assisted dump.
1305 int __init setup_fadump(void)
1307 if (!fw_dump.fadump_enabled)
1310 if (!fw_dump.fadump_supported) {
1311 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1312 " this hardware\n");
1316 fadump_show_config();
1318 * If dump data is available then see if it is valid and prepare for
1319 * saving it to the disk.
1321 if (fw_dump.dump_active) {
1323 * if dump process fails then invalidate the registration
1324 * and release memory before proceeding for re-registration.
1326 if (process_fadump(fdm_active) < 0)
1327 fadump_invalidate_release_mem();
1329 /* Initialize the kernel dump memory structure for FAD registration. */
1330 else if (fw_dump.reserve_dump_area_size)
1331 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1332 fadump_init_files();
1336 subsys_initcall(setup_fadump);