2 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3 * dump with assistance from firmware. This approach does not use kexec,
4 * instead firmware assists in booting the kdump kernel while preserving
5 * memory contents. The most of the code implementation has been adapted
6 * from phyp assisted dump implementation written by Linas Vepstas and
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright 2011 IBM Corporation
24 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
28 #define pr_fmt(fmt) "fadump: " fmt
30 #include <linux/string.h>
31 #include <linux/memblock.h>
32 #include <linux/delay.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/crash_dump.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/slab.h>
43 #include <asm/fadump.h>
44 #include <asm/debug.h>
45 #include <asm/setup.h>
47 static struct fw_dump fw_dump;
48 static struct fadump_mem_struct fdm;
49 static const struct fadump_mem_struct *fdm_active;
51 static DEFINE_MUTEX(fadump_mutex);
52 struct fad_crash_memory_ranges *crash_memory_ranges;
53 int crash_memory_ranges_size;
55 int max_crash_mem_ranges;
57 /* Scan the Firmware Assisted dump configuration details. */
58 int __init early_init_dt_scan_fw_dump(unsigned long node,
59 const char *uname, int depth, void *data)
61 const __be32 *sections;
66 if (depth != 1 || strcmp(uname, "rtas") != 0)
70 * Check if Firmware Assisted dump is supported. if yes, check
71 * if dump has been initiated on last reboot.
73 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
77 fw_dump.fadump_supported = 1;
78 fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
81 * The 'ibm,kernel-dump' rtas node is present only if there is
82 * dump data waiting for us.
84 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
86 fw_dump.dump_active = 1;
88 /* Get the sizes required to store dump data for the firmware provided
90 * For each dump section type supported, a 32bit cell which defines
91 * the ID of a supported section followed by two 32 bit cells which
92 * gives teh size of the section in bytes.
94 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
100 num_sections = size / (3 * sizeof(u32));
102 for (i = 0; i < num_sections; i++, sections += 3) {
103 u32 type = (u32)of_read_number(sections, 1);
106 case FADUMP_CPU_STATE_DATA:
107 fw_dump.cpu_state_data_size =
108 of_read_ulong(§ions[1], 2);
110 case FADUMP_HPTE_REGION:
111 fw_dump.hpte_region_size =
112 of_read_ulong(§ions[1], 2);
120 int is_fadump_active(void)
122 return fw_dump.dump_active;
125 /* Print firmware assisted dump configurations for debugging purpose. */
126 static void fadump_show_config(void)
128 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
129 (fw_dump.fadump_supported ? "present" : "no support"));
131 if (!fw_dump.fadump_supported)
134 pr_debug("Fadump enabled : %s\n",
135 (fw_dump.fadump_enabled ? "yes" : "no"));
136 pr_debug("Dump Active : %s\n",
137 (fw_dump.dump_active ? "yes" : "no"));
138 pr_debug("Dump section sizes:\n");
139 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
140 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
141 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
144 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
150 memset(fdm, 0, sizeof(struct fadump_mem_struct));
151 addr = addr & PAGE_MASK;
153 fdm->header.dump_format_version = cpu_to_be32(0x00000001);
154 fdm->header.dump_num_sections = cpu_to_be16(3);
155 fdm->header.dump_status_flag = 0;
156 fdm->header.offset_first_dump_section =
157 cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
160 * Fields for disk dump option.
161 * We are not using disk dump option, hence set these fields to 0.
163 fdm->header.dd_block_size = 0;
164 fdm->header.dd_block_offset = 0;
165 fdm->header.dd_num_blocks = 0;
166 fdm->header.dd_offset_disk_path = 0;
168 /* set 0 to disable an automatic dump-reboot. */
169 fdm->header.max_time_auto = 0;
171 /* Kernel dump sections */
172 /* cpu state data section. */
173 fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
174 fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
175 fdm->cpu_state_data.source_address = 0;
176 fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
177 fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
178 addr += fw_dump.cpu_state_data_size;
180 /* hpte region section */
181 fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
182 fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
183 fdm->hpte_region.source_address = 0;
184 fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
185 fdm->hpte_region.destination_address = cpu_to_be64(addr);
186 addr += fw_dump.hpte_region_size;
188 /* RMA region section */
189 fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
190 fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
191 fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
192 fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
193 fdm->rmr_region.destination_address = cpu_to_be64(addr);
194 addr += fw_dump.boot_memory_size;
200 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
202 * Function to find the largest memory size we need to reserve during early
203 * boot process. This will be the size of the memory that is required for a
204 * kernel to boot successfully.
206 * This function has been taken from phyp-assisted dump feature implementation.
208 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
210 * TODO: Come up with better approach to find out more accurate memory size
211 * that is required for a kernel to boot successfully.
214 static inline unsigned long fadump_calculate_reserve_size(void)
219 * Check if the size is specified through fadump_reserve_mem= cmdline
220 * option. If yes, then use that.
222 if (fw_dump.reserve_bootvar)
223 return fw_dump.reserve_bootvar;
225 /* divide by 20 to get 5% of value */
226 size = memblock_end_of_DRAM() / 20;
228 /* round it down in multiples of 256 */
229 size = size & ~0x0FFFFFFFUL;
231 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
232 if (memory_limit && size > memory_limit)
235 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
239 * Calculate the total memory size required to be reserved for
240 * firmware-assisted dump registration.
242 static unsigned long get_fadump_area_size(void)
244 unsigned long size = 0;
246 size += fw_dump.cpu_state_data_size;
247 size += fw_dump.hpte_region_size;
248 size += fw_dump.boot_memory_size;
249 size += sizeof(struct fadump_crash_info_header);
250 size += sizeof(struct elfhdr); /* ELF core header.*/
251 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
252 /* Program headers for crash memory regions. */
253 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
255 size = PAGE_ALIGN(size);
259 int __init fadump_reserve_mem(void)
261 unsigned long base, size, memory_boundary;
263 if (!fw_dump.fadump_enabled)
266 if (!fw_dump.fadump_supported) {
267 printk(KERN_INFO "Firmware-assisted dump is not supported on"
269 fw_dump.fadump_enabled = 0;
273 * Initialize boot memory size
274 * If dump is active then we have already calculated the size during
278 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
280 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
283 * Calculate the memory boundary.
284 * If memory_limit is less than actual memory boundary then reserve
285 * the memory for fadump beyond the memory_limit and adjust the
286 * memory_limit accordingly, so that the running kernel can run with
287 * specified memory_limit.
289 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
290 size = get_fadump_area_size();
291 if ((memory_limit + size) < memblock_end_of_DRAM())
292 memory_limit += size;
294 memory_limit = memblock_end_of_DRAM();
295 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
296 " dump, now %#016llx\n", memory_limit);
299 memory_boundary = memory_limit;
301 memory_boundary = memblock_end_of_DRAM();
303 if (fw_dump.dump_active) {
304 printk(KERN_INFO "Firmware-assisted dump is active.\n");
306 * If last boot has crashed then reserve all the memory
307 * above boot_memory_size so that we don't touch it until
308 * dump is written to disk by userspace tool. This memory
309 * will be released for general use once the dump is saved.
311 base = fw_dump.boot_memory_size;
312 size = memory_boundary - base;
313 memblock_reserve(base, size);
314 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
315 "for saving crash dump\n",
316 (unsigned long)(size >> 20),
317 (unsigned long)(base >> 20));
319 fw_dump.fadumphdr_addr =
320 be64_to_cpu(fdm_active->rmr_region.destination_address) +
321 be64_to_cpu(fdm_active->rmr_region.source_len);
322 pr_debug("fadumphdr_addr = %p\n",
323 (void *) fw_dump.fadumphdr_addr);
325 /* Reserve the memory at the top of memory. */
326 size = get_fadump_area_size();
327 base = memory_boundary - size;
328 memblock_reserve(base, size);
329 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
330 "for firmware-assisted dump\n",
331 (unsigned long)(size >> 20),
332 (unsigned long)(base >> 20));
334 fw_dump.reserve_dump_area_start = base;
335 fw_dump.reserve_dump_area_size = size;
339 unsigned long __init arch_reserved_kernel_pages(void)
341 return memblock_reserved_size() / PAGE_SIZE;
344 /* Look for fadump= cmdline option. */
345 static int __init early_fadump_param(char *p)
350 if (strncmp(p, "on", 2) == 0)
351 fw_dump.fadump_enabled = 1;
352 else if (strncmp(p, "off", 3) == 0)
353 fw_dump.fadump_enabled = 0;
357 early_param("fadump", early_fadump_param);
359 /* Look for fadump_reserve_mem= cmdline option */
360 static int __init early_fadump_reserve_mem(char *p)
363 fw_dump.reserve_bootvar = memparse(p, &p);
366 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
368 static int register_fw_dump(struct fadump_mem_struct *fdm)
371 unsigned int wait_time;
373 pr_debug("Registering for firmware-assisted kernel dump...\n");
375 /* TODO: Add upper time limit for the delay */
377 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
378 FADUMP_REGISTER, fdm,
379 sizeof(struct fadump_mem_struct));
381 wait_time = rtas_busy_delay_time(rc);
390 pr_err("Failed to register. Unknown Error(%d).\n", rc);
393 printk(KERN_ERR "Failed to register firmware-assisted kernel"
394 " dump. Hardware Error(%d).\n", rc);
397 printk(KERN_ERR "Failed to register firmware-assisted kernel"
398 " dump. Parameter Error(%d).\n", rc);
402 printk(KERN_ERR "firmware-assisted kernel dump is already "
404 fw_dump.dump_registered = 1;
408 printk(KERN_INFO "firmware-assisted kernel dump registration"
410 fw_dump.dump_registered = 1;
417 void crash_fadump(struct pt_regs *regs, const char *str)
419 struct fadump_crash_info_header *fdh = NULL;
421 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
424 fdh = __va(fw_dump.fadumphdr_addr);
425 crashing_cpu = smp_processor_id();
426 fdh->crashing_cpu = crashing_cpu;
427 crash_save_vmcoreinfo();
432 ppc_save_regs(&fdh->regs);
434 fdh->online_mask = *cpu_online_mask;
436 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
437 rtas_os_term((char *)str);
440 #define GPR_MASK 0xffffff0000000000
441 static inline int fadump_gpr_index(u64 id)
446 if ((id & GPR_MASK) == REG_ID("GPR")) {
447 /* get the digits at the end */
452 str[0] = (id >> 8) & 0xff;
453 sscanf(str, "%d", &i);
460 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
465 i = fadump_gpr_index(reg_id);
467 regs->gpr[i] = (unsigned long)reg_val;
468 else if (reg_id == REG_ID("NIA"))
469 regs->nip = (unsigned long)reg_val;
470 else if (reg_id == REG_ID("MSR"))
471 regs->msr = (unsigned long)reg_val;
472 else if (reg_id == REG_ID("CTR"))
473 regs->ctr = (unsigned long)reg_val;
474 else if (reg_id == REG_ID("LR"))
475 regs->link = (unsigned long)reg_val;
476 else if (reg_id == REG_ID("XER"))
477 regs->xer = (unsigned long)reg_val;
478 else if (reg_id == REG_ID("CR"))
479 regs->ccr = (unsigned long)reg_val;
480 else if (reg_id == REG_ID("DAR"))
481 regs->dar = (unsigned long)reg_val;
482 else if (reg_id == REG_ID("DSISR"))
483 regs->dsisr = (unsigned long)reg_val;
486 static struct fadump_reg_entry*
487 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
489 memset(regs, 0, sizeof(struct pt_regs));
491 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
492 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
493 be64_to_cpu(reg_entry->reg_value));
500 static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
501 void *data, size_t data_len)
503 struct elf_note note;
505 note.n_namesz = strlen(name) + 1;
506 note.n_descsz = data_len;
508 memcpy(buf, ¬e, sizeof(note));
509 buf += (sizeof(note) + 3)/4;
510 memcpy(buf, name, note.n_namesz);
511 buf += (note.n_namesz + 3)/4;
512 memcpy(buf, data, note.n_descsz);
513 buf += (note.n_descsz + 3)/4;
518 static void fadump_final_note(u32 *buf)
520 struct elf_note note;
525 memcpy(buf, ¬e, sizeof(note));
528 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
530 struct elf_prstatus prstatus;
532 memset(&prstatus, 0, sizeof(prstatus));
534 * FIXME: How do i get PID? Do I really need it?
535 * prstatus.pr_pid = ????
537 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
538 buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
539 &prstatus, sizeof(prstatus));
543 static void fadump_update_elfcore_header(char *bufp)
546 struct elf_phdr *phdr;
548 elf = (struct elfhdr *)bufp;
549 bufp += sizeof(struct elfhdr);
551 /* First note is a place holder for cpu notes info. */
552 phdr = (struct elf_phdr *)bufp;
554 if (phdr->p_type == PT_NOTE) {
555 phdr->p_paddr = fw_dump.cpu_notes_buf;
556 phdr->p_offset = phdr->p_paddr;
557 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
558 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
563 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
567 unsigned long order, count, i;
569 order = get_order(size);
570 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
575 page = virt_to_page(vaddr);
576 for (i = 0; i < count; i++)
577 SetPageReserved(page + i);
581 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
584 unsigned long order, count, i;
586 order = get_order(size);
588 page = virt_to_page(vaddr);
589 for (i = 0; i < count; i++)
590 ClearPageReserved(page + i);
591 __free_pages(page, order);
595 * Read CPU state dump data and convert it into ELF notes.
596 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
597 * used to access the data to allow for additional fields to be added without
598 * affecting compatibility. Each list of registers for a CPU starts with
599 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
600 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
601 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
602 * of register value. For more details refer to PAPR document.
604 * Only for the crashing cpu we ignore the CPU dump data and get exact
605 * state from fadump crash info structure populated by first kernel at the
608 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
610 struct fadump_reg_save_area_header *reg_header;
611 struct fadump_reg_entry *reg_entry;
612 struct fadump_crash_info_header *fdh = NULL;
615 u32 num_cpus, *note_buf;
617 int i, rc = 0, cpu = 0;
619 if (!fdm->cpu_state_data.bytes_dumped)
622 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
626 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
627 printk(KERN_ERR "Unable to read register save area.\n");
630 pr_debug("--------CPU State Data------------\n");
631 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
632 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
634 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
635 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
636 pr_debug("NumCpus : %u\n", num_cpus);
637 vaddr += sizeof(u32);
638 reg_entry = (struct fadump_reg_entry *)vaddr;
640 /* Allocate buffer to hold cpu crash notes. */
641 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
642 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
643 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
645 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
646 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
649 fw_dump.cpu_notes_buf = __pa(note_buf);
651 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
652 (num_cpus * sizeof(note_buf_t)), note_buf);
654 if (fw_dump.fadumphdr_addr)
655 fdh = __va(fw_dump.fadumphdr_addr);
657 for (i = 0; i < num_cpus; i++) {
658 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
659 printk(KERN_ERR "Unable to read CPU state data\n");
663 /* Lower 4 bytes of reg_value contains logical cpu id */
664 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
665 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
666 SKIP_TO_NEXT_CPU(reg_entry);
669 pr_debug("Reading register data for cpu %d...\n", cpu);
670 if (fdh && fdh->crashing_cpu == cpu) {
672 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
673 SKIP_TO_NEXT_CPU(reg_entry);
676 reg_entry = fadump_read_registers(reg_entry, ®s);
677 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
680 fadump_final_note(note_buf);
683 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
684 fdh->elfcorehdr_addr);
685 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
690 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
691 fw_dump.cpu_notes_buf_size);
692 fw_dump.cpu_notes_buf = 0;
693 fw_dump.cpu_notes_buf_size = 0;
699 * Validate and process the dump data stored by firmware before exporting
700 * it through '/proc/vmcore'.
702 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
704 struct fadump_crash_info_header *fdh;
707 if (!fdm_active || !fw_dump.fadumphdr_addr)
710 /* Check if the dump data is valid. */
711 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
712 (fdm_active->cpu_state_data.error_flags != 0) ||
713 (fdm_active->rmr_region.error_flags != 0)) {
714 printk(KERN_ERR "Dump taken by platform is not valid\n");
717 if ((fdm_active->rmr_region.bytes_dumped !=
718 fdm_active->rmr_region.source_len) ||
719 !fdm_active->cpu_state_data.bytes_dumped) {
720 printk(KERN_ERR "Dump taken by platform is incomplete\n");
724 /* Validate the fadump crash info header */
725 fdh = __va(fw_dump.fadumphdr_addr);
726 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
727 printk(KERN_ERR "Crash info header is not valid.\n");
731 rc = fadump_build_cpu_notes(fdm_active);
736 * We are done validating dump info and elfcore header is now ready
737 * to be exported. set elfcorehdr_addr so that vmcore module will
738 * export the elfcore header through '/proc/vmcore'.
740 elfcorehdr_addr = fdh->elfcorehdr_addr;
745 static void free_crash_memory_ranges(void)
747 kfree(crash_memory_ranges);
748 crash_memory_ranges = NULL;
749 crash_memory_ranges_size = 0;
750 max_crash_mem_ranges = 0;
754 * Allocate or reallocate crash memory ranges array in incremental units
757 static int allocate_crash_memory_ranges(void)
759 struct fad_crash_memory_ranges *new_array;
762 new_size = crash_memory_ranges_size + PAGE_SIZE;
763 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
766 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
767 if (new_array == NULL) {
768 pr_err("Insufficient memory for setting up crash memory ranges\n");
769 free_crash_memory_ranges();
773 crash_memory_ranges = new_array;
774 crash_memory_ranges_size = new_size;
775 max_crash_mem_ranges = (new_size /
776 sizeof(struct fad_crash_memory_ranges));
780 static inline int fadump_add_crash_memory(unsigned long long base,
781 unsigned long long end)
786 if (crash_mem_ranges == max_crash_mem_ranges) {
789 ret = allocate_crash_memory_ranges();
794 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
795 crash_mem_ranges, base, end - 1, (end - base));
796 crash_memory_ranges[crash_mem_ranges].base = base;
797 crash_memory_ranges[crash_mem_ranges].size = end - base;
802 static int fadump_exclude_reserved_area(unsigned long long start,
803 unsigned long long end)
805 unsigned long long ra_start, ra_end;
808 ra_start = fw_dump.reserve_dump_area_start;
809 ra_end = ra_start + fw_dump.reserve_dump_area_size;
811 if ((ra_start < end) && (ra_end > start)) {
812 if ((start < ra_start) && (end > ra_end)) {
813 ret = fadump_add_crash_memory(start, ra_start);
817 ret = fadump_add_crash_memory(ra_end, end);
818 } else if (start < ra_start) {
819 ret = fadump_add_crash_memory(start, ra_start);
820 } else if (ra_end < end) {
821 ret = fadump_add_crash_memory(ra_end, end);
824 ret = fadump_add_crash_memory(start, end);
829 static int fadump_init_elfcore_header(char *bufp)
833 elf = (struct elfhdr *) bufp;
834 bufp += sizeof(struct elfhdr);
835 memcpy(elf->e_ident, ELFMAG, SELFMAG);
836 elf->e_ident[EI_CLASS] = ELF_CLASS;
837 elf->e_ident[EI_DATA] = ELF_DATA;
838 elf->e_ident[EI_VERSION] = EV_CURRENT;
839 elf->e_ident[EI_OSABI] = ELF_OSABI;
840 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
841 elf->e_type = ET_CORE;
842 elf->e_machine = ELF_ARCH;
843 elf->e_version = EV_CURRENT;
845 elf->e_phoff = sizeof(struct elfhdr);
847 #if defined(_CALL_ELF)
848 elf->e_flags = _CALL_ELF;
852 elf->e_ehsize = sizeof(struct elfhdr);
853 elf->e_phentsize = sizeof(struct elf_phdr);
855 elf->e_shentsize = 0;
863 * Traverse through memblock structure and setup crash memory ranges. These
864 * ranges will be used create PT_LOAD program headers in elfcore header.
866 static int fadump_setup_crash_memory_ranges(void)
868 struct memblock_region *reg;
869 unsigned long long start, end;
872 pr_debug("Setup crash memory ranges.\n");
873 crash_mem_ranges = 0;
875 * add the first memory chunk (RMA_START through boot_memory_size) as
876 * a separate memory chunk. The reason is, at the time crash firmware
877 * will move the content of this memory chunk to different location
878 * specified during fadump registration. We need to create a separate
879 * program header for this chunk with the correct offset.
881 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
885 for_each_memblock(memory, reg) {
886 start = (unsigned long long)reg->base;
887 end = start + (unsigned long long)reg->size;
888 if (start == RMA_START && end >= fw_dump.boot_memory_size)
889 start = fw_dump.boot_memory_size;
891 /* add this range excluding the reserved dump area. */
892 ret = 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)
1024 * If no memory is reserved then we can not register for firmware-
1027 if (!fw_dump.reserve_dump_area_size)
1030 ret = fadump_setup_crash_memory_ranges();
1034 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
1035 /* Initialize fadump crash info header. */
1036 addr = init_fadump_header(addr);
1039 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1040 fadump_create_elfcore_headers(vaddr);
1042 /* register the future kernel dump with firmware. */
1043 return register_fw_dump(&fdm);
1046 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
1049 unsigned int wait_time;
1051 pr_debug("Un-register firmware-assisted dump\n");
1053 /* TODO: Add upper time limit for the delay */
1055 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1056 FADUMP_UNREGISTER, fdm,
1057 sizeof(struct fadump_mem_struct));
1059 wait_time = rtas_busy_delay_time(rc);
1062 } while (wait_time);
1065 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1066 " unexpected error(%d).\n", rc);
1069 fw_dump.dump_registered = 0;
1073 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1076 unsigned int wait_time;
1078 pr_debug("Invalidating firmware-assisted dump registration\n");
1080 /* TODO: Add upper time limit for the delay */
1082 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1083 FADUMP_INVALIDATE, fdm,
1084 sizeof(struct fadump_mem_struct));
1086 wait_time = rtas_busy_delay_time(rc);
1089 } while (wait_time);
1092 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1095 fw_dump.dump_active = 0;
1100 void fadump_cleanup(void)
1102 /* Invalidate the registration only if dump is active. */
1103 if (fw_dump.dump_active) {
1104 init_fadump_mem_struct(&fdm,
1105 be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1106 fadump_invalidate_dump(&fdm);
1107 } else if (fw_dump.dump_registered) {
1108 /* Un-register Firmware-assisted dump if it was registered. */
1109 fadump_unregister_dump(&fdm);
1110 free_crash_memory_ranges();
1115 * Release the memory that was reserved in early boot to preserve the memory
1116 * contents. The released memory will be available for general use.
1118 static void fadump_release_memory(unsigned long begin, unsigned long end)
1121 unsigned long ra_start, ra_end;
1123 ra_start = fw_dump.reserve_dump_area_start;
1124 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1126 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1128 * exclude the dump reserve area. Will reuse it for next
1129 * fadump registration.
1131 if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1134 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1138 static void fadump_invalidate_release_mem(void)
1140 unsigned long reserved_area_start, reserved_area_end;
1141 unsigned long destination_address;
1143 mutex_lock(&fadump_mutex);
1144 if (!fw_dump.dump_active) {
1145 mutex_unlock(&fadump_mutex);
1149 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1151 mutex_unlock(&fadump_mutex);
1154 * Save the current reserved memory bounds we will require them
1155 * later for releasing the memory for general use.
1157 reserved_area_start = fw_dump.reserve_dump_area_start;
1158 reserved_area_end = reserved_area_start +
1159 fw_dump.reserve_dump_area_size;
1161 * Setup reserve_dump_area_start and its size so that we can
1162 * reuse this reserved memory for Re-registration.
1164 fw_dump.reserve_dump_area_start = destination_address;
1165 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1167 fadump_release_memory(reserved_area_start, reserved_area_end);
1168 if (fw_dump.cpu_notes_buf) {
1169 fadump_cpu_notes_buf_free(
1170 (unsigned long)__va(fw_dump.cpu_notes_buf),
1171 fw_dump.cpu_notes_buf_size);
1172 fw_dump.cpu_notes_buf = 0;
1173 fw_dump.cpu_notes_buf_size = 0;
1175 /* Initialize the kernel dump memory structure for FAD registration. */
1176 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1179 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1180 struct kobj_attribute *attr,
1181 const char *buf, size_t count)
1183 if (!fw_dump.dump_active)
1186 if (buf[0] == '1') {
1188 * Take away the '/proc/vmcore'. We are releasing the dump
1189 * memory, hence it will not be valid anymore.
1191 #ifdef CONFIG_PROC_VMCORE
1194 fadump_invalidate_release_mem();
1201 static ssize_t fadump_enabled_show(struct kobject *kobj,
1202 struct kobj_attribute *attr,
1205 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1208 static ssize_t fadump_register_show(struct kobject *kobj,
1209 struct kobj_attribute *attr,
1212 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1215 static ssize_t fadump_register_store(struct kobject *kobj,
1216 struct kobj_attribute *attr,
1217 const char *buf, size_t count)
1221 if (!fw_dump.fadump_enabled || fdm_active)
1224 mutex_lock(&fadump_mutex);
1228 if (fw_dump.dump_registered == 0) {
1231 /* Un-register Firmware-assisted dump */
1232 fadump_unregister_dump(&fdm);
1235 if (fw_dump.dump_registered == 1) {
1239 /* Register Firmware-assisted dump */
1240 ret = register_fadump();
1248 mutex_unlock(&fadump_mutex);
1249 return ret < 0 ? ret : count;
1252 static int fadump_region_show(struct seq_file *m, void *private)
1254 const struct fadump_mem_struct *fdm_ptr;
1256 if (!fw_dump.fadump_enabled)
1259 mutex_lock(&fadump_mutex);
1261 fdm_ptr = fdm_active;
1263 mutex_unlock(&fadump_mutex);
1268 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1270 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1271 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1272 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1273 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1274 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1276 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1278 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1279 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1280 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1281 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1282 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1284 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1286 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1287 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1288 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1289 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1290 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1293 (fw_dump.reserve_dump_area_start ==
1294 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1297 /* Dump is active. Show reserved memory region. */
1299 " : [%#016llx-%#016llx] %#llx bytes, "
1301 (unsigned long long)fw_dump.reserve_dump_area_start,
1302 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1303 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1304 fw_dump.reserve_dump_area_start,
1305 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1306 fw_dump.reserve_dump_area_start);
1309 mutex_unlock(&fadump_mutex);
1313 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1315 fadump_release_memory_store);
1316 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1317 0444, fadump_enabled_show,
1319 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1320 0644, fadump_register_show,
1321 fadump_register_store);
1323 static int fadump_region_open(struct inode *inode, struct file *file)
1325 return single_open(file, fadump_region_show, inode->i_private);
1328 static const struct file_operations fadump_region_fops = {
1329 .open = fadump_region_open,
1331 .llseek = seq_lseek,
1332 .release = single_release,
1335 static void fadump_init_files(void)
1337 struct dentry *debugfs_file;
1340 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1342 printk(KERN_ERR "fadump: unable to create sysfs file"
1343 " fadump_enabled (%d)\n", rc);
1345 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1347 printk(KERN_ERR "fadump: unable to create sysfs file"
1348 " fadump_registered (%d)\n", rc);
1350 debugfs_file = debugfs_create_file("fadump_region", 0444,
1351 powerpc_debugfs_root, NULL,
1352 &fadump_region_fops);
1354 printk(KERN_ERR "fadump: unable to create debugfs file"
1355 " fadump_region\n");
1357 if (fw_dump.dump_active) {
1358 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1360 printk(KERN_ERR "fadump: unable to create sysfs file"
1361 " fadump_release_mem (%d)\n", rc);
1367 * Prepare for firmware-assisted dump.
1369 int __init setup_fadump(void)
1371 if (!fw_dump.fadump_enabled)
1374 if (!fw_dump.fadump_supported) {
1375 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1376 " this hardware\n");
1380 fadump_show_config();
1382 * If dump data is available then see if it is valid and prepare for
1383 * saving it to the disk.
1385 if (fw_dump.dump_active) {
1387 * if dump process fails then invalidate the registration
1388 * and release memory before proceeding for re-registration.
1390 if (process_fadump(fdm_active) < 0)
1391 fadump_invalidate_release_mem();
1393 /* Initialize the kernel dump memory structure for FAD registration. */
1394 else if (fw_dump.reserve_dump_area_size)
1395 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1396 fadump_init_files();
1400 subsys_initcall(setup_fadump);