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 /* Look for fadump= cmdline option. */
340 static int __init early_fadump_param(char *p)
345 if (strncmp(p, "on", 2) == 0)
346 fw_dump.fadump_enabled = 1;
347 else if (strncmp(p, "off", 3) == 0)
348 fw_dump.fadump_enabled = 0;
352 early_param("fadump", early_fadump_param);
354 /* Look for fadump_reserve_mem= cmdline option */
355 static int __init early_fadump_reserve_mem(char *p)
358 fw_dump.reserve_bootvar = memparse(p, &p);
361 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
363 static int register_fw_dump(struct fadump_mem_struct *fdm)
366 unsigned int wait_time;
368 pr_debug("Registering for firmware-assisted kernel dump...\n");
370 /* TODO: Add upper time limit for the delay */
372 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
373 FADUMP_REGISTER, fdm,
374 sizeof(struct fadump_mem_struct));
376 wait_time = rtas_busy_delay_time(rc);
385 pr_err("Failed to register. Unknown Error(%d).\n", rc);
388 printk(KERN_ERR "Failed to register firmware-assisted kernel"
389 " dump. Hardware Error(%d).\n", rc);
392 printk(KERN_ERR "Failed to register firmware-assisted kernel"
393 " dump. Parameter Error(%d).\n", rc);
397 printk(KERN_ERR "firmware-assisted kernel dump is already "
399 fw_dump.dump_registered = 1;
403 printk(KERN_INFO "firmware-assisted kernel dump registration"
405 fw_dump.dump_registered = 1;
412 void crash_fadump(struct pt_regs *regs, const char *str)
414 struct fadump_crash_info_header *fdh = NULL;
416 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
419 fdh = __va(fw_dump.fadumphdr_addr);
420 crashing_cpu = smp_processor_id();
421 fdh->crashing_cpu = crashing_cpu;
422 crash_save_vmcoreinfo();
427 ppc_save_regs(&fdh->regs);
429 fdh->cpu_online_mask = *cpu_online_mask;
431 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
432 rtas_os_term((char *)str);
435 #define GPR_MASK 0xffffff0000000000
436 static inline int fadump_gpr_index(u64 id)
441 if ((id & GPR_MASK) == REG_ID("GPR")) {
442 /* get the digits at the end */
447 str[0] = (id >> 8) & 0xff;
448 sscanf(str, "%d", &i);
455 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
460 i = fadump_gpr_index(reg_id);
462 regs->gpr[i] = (unsigned long)reg_val;
463 else if (reg_id == REG_ID("NIA"))
464 regs->nip = (unsigned long)reg_val;
465 else if (reg_id == REG_ID("MSR"))
466 regs->msr = (unsigned long)reg_val;
467 else if (reg_id == REG_ID("CTR"))
468 regs->ctr = (unsigned long)reg_val;
469 else if (reg_id == REG_ID("LR"))
470 regs->link = (unsigned long)reg_val;
471 else if (reg_id == REG_ID("XER"))
472 regs->xer = (unsigned long)reg_val;
473 else if (reg_id == REG_ID("CR"))
474 regs->ccr = (unsigned long)reg_val;
475 else if (reg_id == REG_ID("DAR"))
476 regs->dar = (unsigned long)reg_val;
477 else if (reg_id == REG_ID("DSISR"))
478 regs->dsisr = (unsigned long)reg_val;
481 static struct fadump_reg_entry*
482 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
484 memset(regs, 0, sizeof(struct pt_regs));
486 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
487 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
488 be64_to_cpu(reg_entry->reg_value));
495 static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
496 void *data, size_t data_len)
498 struct elf_note note;
500 note.n_namesz = strlen(name) + 1;
501 note.n_descsz = data_len;
503 memcpy(buf, ¬e, sizeof(note));
504 buf += (sizeof(note) + 3)/4;
505 memcpy(buf, name, note.n_namesz);
506 buf += (note.n_namesz + 3)/4;
507 memcpy(buf, data, note.n_descsz);
508 buf += (note.n_descsz + 3)/4;
513 static void fadump_final_note(u32 *buf)
515 struct elf_note note;
520 memcpy(buf, ¬e, sizeof(note));
523 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
525 struct elf_prstatus prstatus;
527 memset(&prstatus, 0, sizeof(prstatus));
529 * FIXME: How do i get PID? Do I really need it?
530 * prstatus.pr_pid = ????
532 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
533 buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
534 &prstatus, sizeof(prstatus));
538 static void fadump_update_elfcore_header(char *bufp)
541 struct elf_phdr *phdr;
543 elf = (struct elfhdr *)bufp;
544 bufp += sizeof(struct elfhdr);
546 /* First note is a place holder for cpu notes info. */
547 phdr = (struct elf_phdr *)bufp;
549 if (phdr->p_type == PT_NOTE) {
550 phdr->p_paddr = fw_dump.cpu_notes_buf;
551 phdr->p_offset = phdr->p_paddr;
552 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
553 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
558 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
562 unsigned long order, count, i;
564 order = get_order(size);
565 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
570 page = virt_to_page(vaddr);
571 for (i = 0; i < count; i++)
572 SetPageReserved(page + i);
576 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
579 unsigned long order, count, i;
581 order = get_order(size);
583 page = virt_to_page(vaddr);
584 for (i = 0; i < count; i++)
585 ClearPageReserved(page + i);
586 __free_pages(page, order);
590 * Read CPU state dump data and convert it into ELF notes.
591 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
592 * used to access the data to allow for additional fields to be added without
593 * affecting compatibility. Each list of registers for a CPU starts with
594 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
595 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
596 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
597 * of register value. For more details refer to PAPR document.
599 * Only for the crashing cpu we ignore the CPU dump data and get exact
600 * state from fadump crash info structure populated by first kernel at the
603 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
605 struct fadump_reg_save_area_header *reg_header;
606 struct fadump_reg_entry *reg_entry;
607 struct fadump_crash_info_header *fdh = NULL;
610 u32 num_cpus, *note_buf;
612 int i, rc = 0, cpu = 0;
614 if (!fdm->cpu_state_data.bytes_dumped)
617 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
621 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
622 printk(KERN_ERR "Unable to read register save area.\n");
625 pr_debug("--------CPU State Data------------\n");
626 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
627 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
629 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
630 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
631 pr_debug("NumCpus : %u\n", num_cpus);
632 vaddr += sizeof(u32);
633 reg_entry = (struct fadump_reg_entry *)vaddr;
635 /* Allocate buffer to hold cpu crash notes. */
636 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
637 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
638 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
640 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
641 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
644 fw_dump.cpu_notes_buf = __pa(note_buf);
646 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
647 (num_cpus * sizeof(note_buf_t)), note_buf);
649 if (fw_dump.fadumphdr_addr)
650 fdh = __va(fw_dump.fadumphdr_addr);
652 for (i = 0; i < num_cpus; i++) {
653 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
654 printk(KERN_ERR "Unable to read CPU state data\n");
658 /* Lower 4 bytes of reg_value contains logical cpu id */
659 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
660 if (fdh && !cpumask_test_cpu(cpu, &fdh->cpu_online_mask)) {
661 SKIP_TO_NEXT_CPU(reg_entry);
664 pr_debug("Reading register data for cpu %d...\n", cpu);
665 if (fdh && fdh->crashing_cpu == cpu) {
667 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
668 SKIP_TO_NEXT_CPU(reg_entry);
671 reg_entry = fadump_read_registers(reg_entry, ®s);
672 note_buf = fadump_regs_to_elf_notes(note_buf, ®s);
675 fadump_final_note(note_buf);
678 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
679 fdh->elfcorehdr_addr);
680 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
685 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
686 fw_dump.cpu_notes_buf_size);
687 fw_dump.cpu_notes_buf = 0;
688 fw_dump.cpu_notes_buf_size = 0;
694 * Validate and process the dump data stored by firmware before exporting
695 * it through '/proc/vmcore'.
697 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
699 struct fadump_crash_info_header *fdh;
702 if (!fdm_active || !fw_dump.fadumphdr_addr)
705 /* Check if the dump data is valid. */
706 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
707 (fdm_active->cpu_state_data.error_flags != 0) ||
708 (fdm_active->rmr_region.error_flags != 0)) {
709 printk(KERN_ERR "Dump taken by platform is not valid\n");
712 if ((fdm_active->rmr_region.bytes_dumped !=
713 fdm_active->rmr_region.source_len) ||
714 !fdm_active->cpu_state_data.bytes_dumped) {
715 printk(KERN_ERR "Dump taken by platform is incomplete\n");
719 /* Validate the fadump crash info header */
720 fdh = __va(fw_dump.fadumphdr_addr);
721 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
722 printk(KERN_ERR "Crash info header is not valid.\n");
726 rc = fadump_build_cpu_notes(fdm_active);
731 * We are done validating dump info and elfcore header is now ready
732 * to be exported. set elfcorehdr_addr so that vmcore module will
733 * export the elfcore header through '/proc/vmcore'.
735 elfcorehdr_addr = fdh->elfcorehdr_addr;
740 static void free_crash_memory_ranges(void)
742 kfree(crash_memory_ranges);
743 crash_memory_ranges = NULL;
744 crash_memory_ranges_size = 0;
745 max_crash_mem_ranges = 0;
749 * Allocate or reallocate crash memory ranges array in incremental units
752 static int allocate_crash_memory_ranges(void)
754 struct fad_crash_memory_ranges *new_array;
757 new_size = crash_memory_ranges_size + PAGE_SIZE;
758 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
761 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
762 if (new_array == NULL) {
763 pr_err("Insufficient memory for setting up crash memory ranges\n");
764 free_crash_memory_ranges();
768 crash_memory_ranges = new_array;
769 crash_memory_ranges_size = new_size;
770 max_crash_mem_ranges = (new_size /
771 sizeof(struct fad_crash_memory_ranges));
775 static inline int fadump_add_crash_memory(unsigned long long base,
776 unsigned long long end)
781 if (crash_mem_ranges == max_crash_mem_ranges) {
784 ret = allocate_crash_memory_ranges();
789 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
790 crash_mem_ranges, base, end - 1, (end - base));
791 crash_memory_ranges[crash_mem_ranges].base = base;
792 crash_memory_ranges[crash_mem_ranges].size = end - base;
797 static int fadump_exclude_reserved_area(unsigned long long start,
798 unsigned long long end)
800 unsigned long long ra_start, ra_end;
803 ra_start = fw_dump.reserve_dump_area_start;
804 ra_end = ra_start + fw_dump.reserve_dump_area_size;
806 if ((ra_start < end) && (ra_end > start)) {
807 if ((start < ra_start) && (end > ra_end)) {
808 ret = fadump_add_crash_memory(start, ra_start);
812 ret = fadump_add_crash_memory(ra_end, end);
813 } else if (start < ra_start) {
814 ret = fadump_add_crash_memory(start, ra_start);
815 } else if (ra_end < end) {
816 ret = fadump_add_crash_memory(ra_end, end);
819 ret = fadump_add_crash_memory(start, end);
824 static int fadump_init_elfcore_header(char *bufp)
828 elf = (struct elfhdr *) bufp;
829 bufp += sizeof(struct elfhdr);
830 memcpy(elf->e_ident, ELFMAG, SELFMAG);
831 elf->e_ident[EI_CLASS] = ELF_CLASS;
832 elf->e_ident[EI_DATA] = ELF_DATA;
833 elf->e_ident[EI_VERSION] = EV_CURRENT;
834 elf->e_ident[EI_OSABI] = ELF_OSABI;
835 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
836 elf->e_type = ET_CORE;
837 elf->e_machine = ELF_ARCH;
838 elf->e_version = EV_CURRENT;
840 elf->e_phoff = sizeof(struct elfhdr);
842 elf->e_flags = ELF_CORE_EFLAGS;
843 elf->e_ehsize = sizeof(struct elfhdr);
844 elf->e_phentsize = sizeof(struct elf_phdr);
846 elf->e_shentsize = 0;
854 * Traverse through memblock structure and setup crash memory ranges. These
855 * ranges will be used create PT_LOAD program headers in elfcore header.
857 static int fadump_setup_crash_memory_ranges(void)
859 struct memblock_region *reg;
860 unsigned long long start, end;
863 pr_debug("Setup crash memory ranges.\n");
864 crash_mem_ranges = 0;
866 * add the first memory chunk (RMA_START through boot_memory_size) as
867 * a separate memory chunk. The reason is, at the time crash firmware
868 * will move the content of this memory chunk to different location
869 * specified during fadump registration. We need to create a separate
870 * program header for this chunk with the correct offset.
872 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
876 for_each_memblock(memory, reg) {
877 start = (unsigned long long)reg->base;
878 end = start + (unsigned long long)reg->size;
879 if (start == RMA_START && end >= fw_dump.boot_memory_size)
880 start = fw_dump.boot_memory_size;
882 /* add this range excluding the reserved dump area. */
883 ret = fadump_exclude_reserved_area(start, end);
892 * If the given physical address falls within the boot memory region then
893 * return the relocated address that points to the dump region reserved
894 * for saving initial boot memory contents.
896 static inline unsigned long fadump_relocate(unsigned long paddr)
898 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
899 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
904 static int fadump_create_elfcore_headers(char *bufp)
907 struct elf_phdr *phdr;
910 fadump_init_elfcore_header(bufp);
911 elf = (struct elfhdr *)bufp;
912 bufp += sizeof(struct elfhdr);
915 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
916 * will be populated during second kernel boot after crash. Hence
917 * this PT_NOTE will always be the first elf note.
919 * NOTE: Any new ELF note addition should be placed after this note.
921 phdr = (struct elf_phdr *)bufp;
922 bufp += sizeof(struct elf_phdr);
923 phdr->p_type = PT_NOTE;
935 /* setup ELF PT_NOTE for vmcoreinfo */
936 phdr = (struct elf_phdr *)bufp;
937 bufp += sizeof(struct elf_phdr);
938 phdr->p_type = PT_NOTE;
943 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
944 phdr->p_offset = phdr->p_paddr;
945 phdr->p_memsz = vmcoreinfo_max_size;
946 phdr->p_filesz = vmcoreinfo_max_size;
948 /* Increment number of program headers. */
951 /* setup PT_LOAD sections. */
953 for (i = 0; i < crash_mem_ranges; i++) {
954 unsigned long long mbase, msize;
955 mbase = crash_memory_ranges[i].base;
956 msize = crash_memory_ranges[i].size;
961 phdr = (struct elf_phdr *)bufp;
962 bufp += sizeof(struct elf_phdr);
963 phdr->p_type = PT_LOAD;
964 phdr->p_flags = PF_R|PF_W|PF_X;
965 phdr->p_offset = mbase;
967 if (mbase == RMA_START) {
969 * The entire RMA region will be moved by firmware
970 * to the specified destination_address. Hence set
971 * the correct offset.
973 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
976 phdr->p_paddr = mbase;
977 phdr->p_vaddr = (unsigned long)__va(mbase);
978 phdr->p_filesz = msize;
979 phdr->p_memsz = msize;
982 /* Increment number of program headers. */
988 static unsigned long init_fadump_header(unsigned long addr)
990 struct fadump_crash_info_header *fdh;
995 fw_dump.fadumphdr_addr = addr;
997 addr += sizeof(struct fadump_crash_info_header);
999 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1000 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1001 fdh->elfcorehdr_addr = addr;
1002 /* We will set the crashing cpu id in crash_fadump() during crash. */
1003 fdh->crashing_cpu = CPU_UNKNOWN;
1008 static int register_fadump(void)
1015 * If no memory is reserved then we can not register for firmware-
1018 if (!fw_dump.reserve_dump_area_size)
1021 ret = fadump_setup_crash_memory_ranges();
1025 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
1026 /* Initialize fadump crash info header. */
1027 addr = init_fadump_header(addr);
1030 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1031 fadump_create_elfcore_headers(vaddr);
1033 /* register the future kernel dump with firmware. */
1034 return register_fw_dump(&fdm);
1037 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
1040 unsigned int wait_time;
1042 pr_debug("Un-register firmware-assisted dump\n");
1044 /* TODO: Add upper time limit for the delay */
1046 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1047 FADUMP_UNREGISTER, fdm,
1048 sizeof(struct fadump_mem_struct));
1050 wait_time = rtas_busy_delay_time(rc);
1053 } while (wait_time);
1056 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1057 " unexpected error(%d).\n", rc);
1060 fw_dump.dump_registered = 0;
1064 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1067 unsigned int wait_time;
1069 pr_debug("Invalidating firmware-assisted dump registration\n");
1071 /* TODO: Add upper time limit for the delay */
1073 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1074 FADUMP_INVALIDATE, fdm,
1075 sizeof(struct fadump_mem_struct));
1077 wait_time = rtas_busy_delay_time(rc);
1080 } while (wait_time);
1083 printk(KERN_ERR "Failed to invalidate firmware-assisted dump "
1084 "rgistration. unexpected error(%d).\n", rc);
1087 fw_dump.dump_active = 0;
1092 void fadump_cleanup(void)
1094 /* Invalidate the registration only if dump is active. */
1095 if (fw_dump.dump_active) {
1096 init_fadump_mem_struct(&fdm,
1097 be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1098 fadump_invalidate_dump(&fdm);
1099 } else if (fw_dump.dump_registered) {
1100 /* Un-register Firmware-assisted dump if it was registered. */
1101 fadump_unregister_dump(&fdm);
1102 free_crash_memory_ranges();
1107 * Release the memory that was reserved in early boot to preserve the memory
1108 * contents. The released memory will be available for general use.
1110 static void fadump_release_memory(unsigned long begin, unsigned long end)
1113 unsigned long ra_start, ra_end;
1115 ra_start = fw_dump.reserve_dump_area_start;
1116 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1118 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1120 * exclude the dump reserve area. Will reuse it for next
1121 * fadump registration.
1123 if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1126 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1130 static void fadump_invalidate_release_mem(void)
1132 unsigned long reserved_area_start, reserved_area_end;
1133 unsigned long destination_address;
1135 mutex_lock(&fadump_mutex);
1136 if (!fw_dump.dump_active) {
1137 mutex_unlock(&fadump_mutex);
1141 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1143 mutex_unlock(&fadump_mutex);
1146 * Save the current reserved memory bounds we will require them
1147 * later for releasing the memory for general use.
1149 reserved_area_start = fw_dump.reserve_dump_area_start;
1150 reserved_area_end = reserved_area_start +
1151 fw_dump.reserve_dump_area_size;
1153 * Setup reserve_dump_area_start and its size so that we can
1154 * reuse this reserved memory for Re-registration.
1156 fw_dump.reserve_dump_area_start = destination_address;
1157 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1159 fadump_release_memory(reserved_area_start, reserved_area_end);
1160 if (fw_dump.cpu_notes_buf) {
1161 fadump_cpu_notes_buf_free(
1162 (unsigned long)__va(fw_dump.cpu_notes_buf),
1163 fw_dump.cpu_notes_buf_size);
1164 fw_dump.cpu_notes_buf = 0;
1165 fw_dump.cpu_notes_buf_size = 0;
1167 /* Initialize the kernel dump memory structure for FAD registration. */
1168 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1171 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1172 struct kobj_attribute *attr,
1173 const char *buf, size_t count)
1175 if (!fw_dump.dump_active)
1178 if (buf[0] == '1') {
1180 * Take away the '/proc/vmcore'. We are releasing the dump
1181 * memory, hence it will not be valid anymore.
1184 fadump_invalidate_release_mem();
1191 static ssize_t fadump_enabled_show(struct kobject *kobj,
1192 struct kobj_attribute *attr,
1195 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1198 static ssize_t fadump_register_show(struct kobject *kobj,
1199 struct kobj_attribute *attr,
1202 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1205 static ssize_t fadump_register_store(struct kobject *kobj,
1206 struct kobj_attribute *attr,
1207 const char *buf, size_t count)
1211 if (!fw_dump.fadump_enabled || fdm_active)
1214 mutex_lock(&fadump_mutex);
1218 if (fw_dump.dump_registered == 0) {
1221 /* Un-register Firmware-assisted dump */
1222 fadump_unregister_dump(&fdm);
1225 if (fw_dump.dump_registered == 1) {
1229 /* Register Firmware-assisted dump */
1230 ret = register_fadump();
1238 mutex_unlock(&fadump_mutex);
1239 return ret < 0 ? ret : count;
1242 static int fadump_region_show(struct seq_file *m, void *private)
1244 const struct fadump_mem_struct *fdm_ptr;
1246 if (!fw_dump.fadump_enabled)
1249 mutex_lock(&fadump_mutex);
1251 fdm_ptr = fdm_active;
1253 mutex_unlock(&fadump_mutex);
1258 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1260 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1261 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1262 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1263 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1264 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1266 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1268 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1269 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1270 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1271 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1272 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1274 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1276 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1277 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1278 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1279 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1280 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1283 (fw_dump.reserve_dump_area_start ==
1284 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1287 /* Dump is active. Show reserved memory region. */
1289 " : [%#016llx-%#016llx] %#llx bytes, "
1291 (unsigned long long)fw_dump.reserve_dump_area_start,
1292 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1293 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1294 fw_dump.reserve_dump_area_start,
1295 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1296 fw_dump.reserve_dump_area_start);
1299 mutex_unlock(&fadump_mutex);
1303 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1305 fadump_release_memory_store);
1306 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1307 0444, fadump_enabled_show,
1309 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1310 0644, fadump_register_show,
1311 fadump_register_store);
1313 static int fadump_region_open(struct inode *inode, struct file *file)
1315 return single_open(file, fadump_region_show, inode->i_private);
1318 static const struct file_operations fadump_region_fops = {
1319 .open = fadump_region_open,
1321 .llseek = seq_lseek,
1322 .release = single_release,
1325 static void fadump_init_files(void)
1327 struct dentry *debugfs_file;
1330 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1332 printk(KERN_ERR "fadump: unable to create sysfs file"
1333 " fadump_enabled (%d)\n", rc);
1335 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1337 printk(KERN_ERR "fadump: unable to create sysfs file"
1338 " fadump_registered (%d)\n", rc);
1340 debugfs_file = debugfs_create_file("fadump_region", 0444,
1341 powerpc_debugfs_root, NULL,
1342 &fadump_region_fops);
1344 printk(KERN_ERR "fadump: unable to create debugfs file"
1345 " fadump_region\n");
1347 if (fw_dump.dump_active) {
1348 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1350 printk(KERN_ERR "fadump: unable to create sysfs file"
1351 " fadump_release_mem (%d)\n", rc);
1357 * Prepare for firmware-assisted dump.
1359 int __init setup_fadump(void)
1361 if (!fw_dump.fadump_enabled)
1364 if (!fw_dump.fadump_supported) {
1365 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1366 " this hardware\n");
1370 fadump_show_config();
1372 * If dump data is available then see if it is valid and prepare for
1373 * saving it to the disk.
1375 if (fw_dump.dump_active) {
1377 * if dump process fails then invalidate the registration
1378 * and release memory before proceeding for re-registration.
1380 if (process_fadump(fdm_active) < 0)
1381 fadump_invalidate_release_mem();
1383 /* Initialize the kernel dump memory structure for FAD registration. */
1384 else if (fw_dump.reserve_dump_area_size)
1385 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1386 fadump_init_files();
1390 subsys_initcall(setup_fadump);