x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / powerpc / kernel / fadump.c
blob2230fd0ca3e4fc089931f25a61c20cb66aa99a6a
1 /*
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
7 * Manish Ahuja
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>
27 #undef DEBUG
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>
39 #include <asm/page.h>
40 #include <asm/prom.h>
41 #include <asm/rtas.h>
42 #include <asm/fadump.h>
43 #include <asm/debug.h>
44 #include <asm/setup.h>
46 static struct fw_dump fw_dump;
47 static struct fadump_mem_struct fdm;
48 static const struct fadump_mem_struct *fdm_active;
50 static DEFINE_MUTEX(fadump_mutex);
51 struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
52 int crash_mem_ranges;
54 /* Scan the Firmware Assisted dump configuration details. */
55 int __init early_init_dt_scan_fw_dump(unsigned long node,
56 const char *uname, int depth, void *data)
58 __be32 *sections;
59 int i, num_sections;
60 unsigned long size;
61 const int *token;
63 if (depth != 1 || strcmp(uname, "rtas") != 0)
64 return 0;
67 * Check if Firmware Assisted dump is supported. if yes, check
68 * if dump has been initiated on last reboot.
70 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
71 if (!token)
72 return 0;
74 fw_dump.fadump_supported = 1;
75 fw_dump.ibm_configure_kernel_dump = *token;
78 * The 'ibm,kernel-dump' rtas node is present only if there is
79 * dump data waiting for us.
81 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
82 if (fdm_active)
83 fw_dump.dump_active = 1;
85 /* Get the sizes required to store dump data for the firmware provided
86 * dump sections.
87 * For each dump section type supported, a 32bit cell which defines
88 * the ID of a supported section followed by two 32 bit cells which
89 * gives teh size of the section in bytes.
91 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
92 &size);
94 if (!sections)
95 return 0;
97 num_sections = size / (3 * sizeof(u32));
99 for (i = 0; i < num_sections; i++, sections += 3) {
100 u32 type = (u32)of_read_number(sections, 1);
102 switch (type) {
103 case FADUMP_CPU_STATE_DATA:
104 fw_dump.cpu_state_data_size =
105 of_read_ulong(&sections[1], 2);
106 break;
107 case FADUMP_HPTE_REGION:
108 fw_dump.hpte_region_size =
109 of_read_ulong(&sections[1], 2);
110 break;
113 return 1;
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)
128 return;
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,
141 unsigned long addr)
143 if (!fdm)
144 return 0;
146 memset(fdm, 0, sizeof(struct fadump_mem_struct));
147 addr = addr & PAGE_MASK;
149 fdm->header.dump_format_version = 0x00000001;
150 fdm->header.dump_num_sections = 3;
151 fdm->header.dump_status_flag = 0;
152 fdm->header.offset_first_dump_section =
153 (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 = FADUMP_REQUEST_FLAG;
170 fdm->cpu_state_data.source_data_type = FADUMP_CPU_STATE_DATA;
171 fdm->cpu_state_data.source_address = 0;
172 fdm->cpu_state_data.source_len = fw_dump.cpu_state_data_size;
173 fdm->cpu_state_data.destination_address = addr;
174 addr += fw_dump.cpu_state_data_size;
176 /* hpte region section */
177 fdm->hpte_region.request_flag = FADUMP_REQUEST_FLAG;
178 fdm->hpte_region.source_data_type = FADUMP_HPTE_REGION;
179 fdm->hpte_region.source_address = 0;
180 fdm->hpte_region.source_len = fw_dump.hpte_region_size;
181 fdm->hpte_region.destination_address = addr;
182 addr += fw_dump.hpte_region_size;
184 /* RMA region section */
185 fdm->rmr_region.request_flag = FADUMP_REQUEST_FLAG;
186 fdm->rmr_region.source_data_type = FADUMP_REAL_MODE_REGION;
187 fdm->rmr_region.source_address = RMA_START;
188 fdm->rmr_region.source_len = fw_dump.boot_memory_size;
189 fdm->rmr_region.destination_address = addr;
190 addr += fw_dump.boot_memory_size;
192 return addr;
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)
212 unsigned long size;
215 * Check if the size is specified through fadump_reserve_mem= cmdline
216 * option. If yes, then use that.
218 if (fw_dump.reserve_bootvar)
219 return fw_dump.reserve_bootvar;
221 /* divide by 20 to get 5% of value */
222 size = memblock_end_of_DRAM() / 20;
224 /* round it down in multiples of 256 */
225 size = size & ~0x0FFFFFFFUL;
227 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
228 if (memory_limit && size > memory_limit)
229 size = memory_limit;
231 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
235 * Calculate the total memory size required to be reserved for
236 * firmware-assisted dump registration.
238 static unsigned long get_fadump_area_size(void)
240 unsigned long size = 0;
242 size += fw_dump.cpu_state_data_size;
243 size += fw_dump.hpte_region_size;
244 size += fw_dump.boot_memory_size;
245 size += sizeof(struct fadump_crash_info_header);
246 size += sizeof(struct elfhdr); /* ELF core header.*/
247 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
248 /* Program headers for crash memory regions. */
249 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
251 size = PAGE_ALIGN(size);
252 return size;
255 int __init fadump_reserve_mem(void)
257 unsigned long base, size, memory_boundary;
259 if (!fw_dump.fadump_enabled)
260 return 0;
262 if (!fw_dump.fadump_supported) {
263 printk(KERN_INFO "Firmware-assisted dump is not supported on"
264 " this hardware\n");
265 fw_dump.fadump_enabled = 0;
266 return 0;
269 * Initialize boot memory size
270 * If dump is active then we have already calculated the size during
271 * first kernel.
273 if (fdm_active)
274 fw_dump.boot_memory_size = fdm_active->rmr_region.source_len;
275 else
276 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
279 * Calculate the memory boundary.
280 * If memory_limit is less than actual memory boundary then reserve
281 * the memory for fadump beyond the memory_limit and adjust the
282 * memory_limit accordingly, so that the running kernel can run with
283 * specified memory_limit.
285 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
286 size = get_fadump_area_size();
287 if ((memory_limit + size) < memblock_end_of_DRAM())
288 memory_limit += size;
289 else
290 memory_limit = memblock_end_of_DRAM();
291 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
292 " dump, now %#016llx\n", memory_limit);
294 if (memory_limit)
295 memory_boundary = memory_limit;
296 else
297 memory_boundary = memblock_end_of_DRAM();
299 if (fw_dump.dump_active) {
300 printk(KERN_INFO "Firmware-assisted dump is active.\n");
302 * If last boot has crashed then reserve all the memory
303 * above boot_memory_size so that we don't touch it until
304 * dump is written to disk by userspace tool. This memory
305 * will be released for general use once the dump is saved.
307 base = fw_dump.boot_memory_size;
308 size = memory_boundary - base;
309 memblock_reserve(base, size);
310 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
311 "for saving crash dump\n",
312 (unsigned long)(size >> 20),
313 (unsigned long)(base >> 20));
315 fw_dump.fadumphdr_addr =
316 fdm_active->rmr_region.destination_address +
317 fdm_active->rmr_region.source_len;
318 pr_debug("fadumphdr_addr = %p\n",
319 (void *) fw_dump.fadumphdr_addr);
320 } else {
321 /* Reserve the memory at the top of memory. */
322 size = get_fadump_area_size();
323 base = memory_boundary - size;
324 memblock_reserve(base, size);
325 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
326 "for firmware-assisted dump\n",
327 (unsigned long)(size >> 20),
328 (unsigned long)(base >> 20));
330 fw_dump.reserve_dump_area_start = base;
331 fw_dump.reserve_dump_area_size = size;
332 return 1;
335 /* Look for fadump= cmdline option. */
336 static int __init early_fadump_param(char *p)
338 if (!p)
339 return 1;
341 if (strncmp(p, "on", 2) == 0)
342 fw_dump.fadump_enabled = 1;
343 else if (strncmp(p, "off", 3) == 0)
344 fw_dump.fadump_enabled = 0;
346 return 0;
348 early_param("fadump", early_fadump_param);
350 /* Look for fadump_reserve_mem= cmdline option */
351 static int __init early_fadump_reserve_mem(char *p)
353 if (p)
354 fw_dump.reserve_bootvar = memparse(p, &p);
355 return 0;
357 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
359 static void register_fw_dump(struct fadump_mem_struct *fdm)
361 int rc;
362 unsigned int wait_time;
364 pr_debug("Registering for firmware-assisted kernel dump...\n");
366 /* TODO: Add upper time limit for the delay */
367 do {
368 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
369 FADUMP_REGISTER, fdm,
370 sizeof(struct fadump_mem_struct));
372 wait_time = rtas_busy_delay_time(rc);
373 if (wait_time)
374 mdelay(wait_time);
376 } while (wait_time);
378 switch (rc) {
379 case -1:
380 printk(KERN_ERR "Failed to register firmware-assisted kernel"
381 " dump. Hardware Error(%d).\n", rc);
382 break;
383 case -3:
384 printk(KERN_ERR "Failed to register firmware-assisted kernel"
385 " dump. Parameter Error(%d).\n", rc);
386 break;
387 case -9:
388 printk(KERN_ERR "firmware-assisted kernel dump is already "
389 " registered.");
390 fw_dump.dump_registered = 1;
391 break;
392 case 0:
393 printk(KERN_INFO "firmware-assisted kernel dump registration"
394 " is successful\n");
395 fw_dump.dump_registered = 1;
396 break;
400 void crash_fadump(struct pt_regs *regs, const char *str)
402 struct fadump_crash_info_header *fdh = NULL;
404 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
405 return;
407 fdh = __va(fw_dump.fadumphdr_addr);
408 crashing_cpu = smp_processor_id();
409 fdh->crashing_cpu = crashing_cpu;
410 crash_save_vmcoreinfo();
412 if (regs)
413 fdh->regs = *regs;
414 else
415 ppc_save_regs(&fdh->regs);
417 fdh->cpu_online_mask = *cpu_online_mask;
419 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
420 rtas_os_term((char *)str);
423 #define GPR_MASK 0xffffff0000000000
424 static inline int fadump_gpr_index(u64 id)
426 int i = -1;
427 char str[3];
429 if ((id & GPR_MASK) == REG_ID("GPR")) {
430 /* get the digits at the end */
431 id &= ~GPR_MASK;
432 id >>= 24;
433 str[2] = '\0';
434 str[1] = id & 0xff;
435 str[0] = (id >> 8) & 0xff;
436 sscanf(str, "%d", &i);
437 if (i > 31)
438 i = -1;
440 return i;
443 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
444 u64 reg_val)
446 int i;
448 i = fadump_gpr_index(reg_id);
449 if (i >= 0)
450 regs->gpr[i] = (unsigned long)reg_val;
451 else if (reg_id == REG_ID("NIA"))
452 regs->nip = (unsigned long)reg_val;
453 else if (reg_id == REG_ID("MSR"))
454 regs->msr = (unsigned long)reg_val;
455 else if (reg_id == REG_ID("CTR"))
456 regs->ctr = (unsigned long)reg_val;
457 else if (reg_id == REG_ID("LR"))
458 regs->link = (unsigned long)reg_val;
459 else if (reg_id == REG_ID("XER"))
460 regs->xer = (unsigned long)reg_val;
461 else if (reg_id == REG_ID("CR"))
462 regs->ccr = (unsigned long)reg_val;
463 else if (reg_id == REG_ID("DAR"))
464 regs->dar = (unsigned long)reg_val;
465 else if (reg_id == REG_ID("DSISR"))
466 regs->dsisr = (unsigned long)reg_val;
469 static struct fadump_reg_entry*
470 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
472 memset(regs, 0, sizeof(struct pt_regs));
474 while (reg_entry->reg_id != REG_ID("CPUEND")) {
475 fadump_set_regval(regs, reg_entry->reg_id,
476 reg_entry->reg_value);
477 reg_entry++;
479 reg_entry++;
480 return reg_entry;
483 static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
484 void *data, size_t data_len)
486 struct elf_note note;
488 note.n_namesz = strlen(name) + 1;
489 note.n_descsz = data_len;
490 note.n_type = type;
491 memcpy(buf, &note, sizeof(note));
492 buf += (sizeof(note) + 3)/4;
493 memcpy(buf, name, note.n_namesz);
494 buf += (note.n_namesz + 3)/4;
495 memcpy(buf, data, note.n_descsz);
496 buf += (note.n_descsz + 3)/4;
498 return buf;
501 static void fadump_final_note(u32 *buf)
503 struct elf_note note;
505 note.n_namesz = 0;
506 note.n_descsz = 0;
507 note.n_type = 0;
508 memcpy(buf, &note, sizeof(note));
511 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
513 struct elf_prstatus prstatus;
515 memset(&prstatus, 0, sizeof(prstatus));
517 * FIXME: How do i get PID? Do I really need it?
518 * prstatus.pr_pid = ????
520 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
521 buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
522 &prstatus, sizeof(prstatus));
523 return buf;
526 static void fadump_update_elfcore_header(char *bufp)
528 struct elfhdr *elf;
529 struct elf_phdr *phdr;
531 elf = (struct elfhdr *)bufp;
532 bufp += sizeof(struct elfhdr);
534 /* First note is a place holder for cpu notes info. */
535 phdr = (struct elf_phdr *)bufp;
537 if (phdr->p_type == PT_NOTE) {
538 phdr->p_paddr = fw_dump.cpu_notes_buf;
539 phdr->p_offset = phdr->p_paddr;
540 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
541 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
543 return;
546 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
548 void *vaddr;
549 struct page *page;
550 unsigned long order, count, i;
552 order = get_order(size);
553 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
554 if (!vaddr)
555 return NULL;
557 count = 1 << order;
558 page = virt_to_page(vaddr);
559 for (i = 0; i < count; i++)
560 SetPageReserved(page + i);
561 return vaddr;
564 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
566 struct page *page;
567 unsigned long order, count, i;
569 order = get_order(size);
570 count = 1 << order;
571 page = virt_to_page(vaddr);
572 for (i = 0; i < count; i++)
573 ClearPageReserved(page + i);
574 __free_pages(page, order);
578 * Read CPU state dump data and convert it into ELF notes.
579 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
580 * used to access the data to allow for additional fields to be added without
581 * affecting compatibility. Each list of registers for a CPU starts with
582 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
583 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
584 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
585 * of register value. For more details refer to PAPR document.
587 * Only for the crashing cpu we ignore the CPU dump data and get exact
588 * state from fadump crash info structure populated by first kernel at the
589 * time of crash.
591 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
593 struct fadump_reg_save_area_header *reg_header;
594 struct fadump_reg_entry *reg_entry;
595 struct fadump_crash_info_header *fdh = NULL;
596 void *vaddr;
597 unsigned long addr;
598 u32 num_cpus, *note_buf;
599 struct pt_regs regs;
600 int i, rc = 0, cpu = 0;
602 if (!fdm->cpu_state_data.bytes_dumped)
603 return -EINVAL;
605 addr = fdm->cpu_state_data.destination_address;
606 vaddr = __va(addr);
608 reg_header = vaddr;
609 if (reg_header->magic_number != REGSAVE_AREA_MAGIC) {
610 printk(KERN_ERR "Unable to read register save area.\n");
611 return -ENOENT;
613 pr_debug("--------CPU State Data------------\n");
614 pr_debug("Magic Number: %llx\n", reg_header->magic_number);
615 pr_debug("NumCpuOffset: %x\n", reg_header->num_cpu_offset);
617 vaddr += reg_header->num_cpu_offset;
618 num_cpus = *((u32 *)(vaddr));
619 pr_debug("NumCpus : %u\n", num_cpus);
620 vaddr += sizeof(u32);
621 reg_entry = (struct fadump_reg_entry *)vaddr;
623 /* Allocate buffer to hold cpu crash notes. */
624 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
625 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
626 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
627 if (!note_buf) {
628 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
629 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
630 return -ENOMEM;
632 fw_dump.cpu_notes_buf = __pa(note_buf);
634 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
635 (num_cpus * sizeof(note_buf_t)), note_buf);
637 if (fw_dump.fadumphdr_addr)
638 fdh = __va(fw_dump.fadumphdr_addr);
640 for (i = 0; i < num_cpus; i++) {
641 if (reg_entry->reg_id != REG_ID("CPUSTRT")) {
642 printk(KERN_ERR "Unable to read CPU state data\n");
643 rc = -ENOENT;
644 goto error_out;
646 /* Lower 4 bytes of reg_value contains logical cpu id */
647 cpu = reg_entry->reg_value & FADUMP_CPU_ID_MASK;
648 if (!cpumask_test_cpu(cpu, &fdh->cpu_online_mask)) {
649 SKIP_TO_NEXT_CPU(reg_entry);
650 continue;
652 pr_debug("Reading register data for cpu %d...\n", cpu);
653 if (fdh && fdh->crashing_cpu == cpu) {
654 regs = fdh->regs;
655 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
656 SKIP_TO_NEXT_CPU(reg_entry);
657 } else {
658 reg_entry++;
659 reg_entry = fadump_read_registers(reg_entry, &regs);
660 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
663 fadump_final_note(note_buf);
665 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
666 fdh->elfcorehdr_addr);
667 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
668 return 0;
670 error_out:
671 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
672 fw_dump.cpu_notes_buf_size);
673 fw_dump.cpu_notes_buf = 0;
674 fw_dump.cpu_notes_buf_size = 0;
675 return rc;
680 * Validate and process the dump data stored by firmware before exporting
681 * it through '/proc/vmcore'.
683 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
685 struct fadump_crash_info_header *fdh;
686 int rc = 0;
688 if (!fdm_active || !fw_dump.fadumphdr_addr)
689 return -EINVAL;
691 /* Check if the dump data is valid. */
692 if ((fdm_active->header.dump_status_flag == FADUMP_ERROR_FLAG) ||
693 (fdm_active->cpu_state_data.error_flags != 0) ||
694 (fdm_active->rmr_region.error_flags != 0)) {
695 printk(KERN_ERR "Dump taken by platform is not valid\n");
696 return -EINVAL;
698 if ((fdm_active->rmr_region.bytes_dumped !=
699 fdm_active->rmr_region.source_len) ||
700 !fdm_active->cpu_state_data.bytes_dumped) {
701 printk(KERN_ERR "Dump taken by platform is incomplete\n");
702 return -EINVAL;
705 /* Validate the fadump crash info header */
706 fdh = __va(fw_dump.fadumphdr_addr);
707 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
708 printk(KERN_ERR "Crash info header is not valid.\n");
709 return -EINVAL;
712 rc = fadump_build_cpu_notes(fdm_active);
713 if (rc)
714 return rc;
717 * We are done validating dump info and elfcore header is now ready
718 * to be exported. set elfcorehdr_addr so that vmcore module will
719 * export the elfcore header through '/proc/vmcore'.
721 elfcorehdr_addr = fdh->elfcorehdr_addr;
723 return 0;
726 static inline void fadump_add_crash_memory(unsigned long long base,
727 unsigned long long end)
729 if (base == end)
730 return;
732 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
733 crash_mem_ranges, base, end - 1, (end - base));
734 crash_memory_ranges[crash_mem_ranges].base = base;
735 crash_memory_ranges[crash_mem_ranges].size = end - base;
736 crash_mem_ranges++;
739 static void fadump_exclude_reserved_area(unsigned long long start,
740 unsigned long long end)
742 unsigned long long ra_start, ra_end;
744 ra_start = fw_dump.reserve_dump_area_start;
745 ra_end = ra_start + fw_dump.reserve_dump_area_size;
747 if ((ra_start < end) && (ra_end > start)) {
748 if ((start < ra_start) && (end > ra_end)) {
749 fadump_add_crash_memory(start, ra_start);
750 fadump_add_crash_memory(ra_end, end);
751 } else if (start < ra_start) {
752 fadump_add_crash_memory(start, ra_start);
753 } else if (ra_end < end) {
754 fadump_add_crash_memory(ra_end, end);
756 } else
757 fadump_add_crash_memory(start, end);
760 static int fadump_init_elfcore_header(char *bufp)
762 struct elfhdr *elf;
764 elf = (struct elfhdr *) bufp;
765 bufp += sizeof(struct elfhdr);
766 memcpy(elf->e_ident, ELFMAG, SELFMAG);
767 elf->e_ident[EI_CLASS] = ELF_CLASS;
768 elf->e_ident[EI_DATA] = ELF_DATA;
769 elf->e_ident[EI_VERSION] = EV_CURRENT;
770 elf->e_ident[EI_OSABI] = ELF_OSABI;
771 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
772 elf->e_type = ET_CORE;
773 elf->e_machine = ELF_ARCH;
774 elf->e_version = EV_CURRENT;
775 elf->e_entry = 0;
776 elf->e_phoff = sizeof(struct elfhdr);
777 elf->e_shoff = 0;
778 elf->e_flags = ELF_CORE_EFLAGS;
779 elf->e_ehsize = sizeof(struct elfhdr);
780 elf->e_phentsize = sizeof(struct elf_phdr);
781 elf->e_phnum = 0;
782 elf->e_shentsize = 0;
783 elf->e_shnum = 0;
784 elf->e_shstrndx = 0;
786 return 0;
790 * Traverse through memblock structure and setup crash memory ranges. These
791 * ranges will be used create PT_LOAD program headers in elfcore header.
793 static void fadump_setup_crash_memory_ranges(void)
795 struct memblock_region *reg;
796 unsigned long long start, end;
798 pr_debug("Setup crash memory ranges.\n");
799 crash_mem_ranges = 0;
801 * add the first memory chunk (RMA_START through boot_memory_size) as
802 * a separate memory chunk. The reason is, at the time crash firmware
803 * will move the content of this memory chunk to different location
804 * specified during fadump registration. We need to create a separate
805 * program header for this chunk with the correct offset.
807 fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
809 for_each_memblock(memory, reg) {
810 start = (unsigned long long)reg->base;
811 end = start + (unsigned long long)reg->size;
812 if (start == RMA_START && end >= fw_dump.boot_memory_size)
813 start = fw_dump.boot_memory_size;
815 /* add this range excluding the reserved dump area. */
816 fadump_exclude_reserved_area(start, end);
821 * If the given physical address falls within the boot memory region then
822 * return the relocated address that points to the dump region reserved
823 * for saving initial boot memory contents.
825 static inline unsigned long fadump_relocate(unsigned long paddr)
827 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
828 return fdm.rmr_region.destination_address + paddr;
829 else
830 return paddr;
833 static int fadump_create_elfcore_headers(char *bufp)
835 struct elfhdr *elf;
836 struct elf_phdr *phdr;
837 int i;
839 fadump_init_elfcore_header(bufp);
840 elf = (struct elfhdr *)bufp;
841 bufp += sizeof(struct elfhdr);
844 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
845 * will be populated during second kernel boot after crash. Hence
846 * this PT_NOTE will always be the first elf note.
848 * NOTE: Any new ELF note addition should be placed after this note.
850 phdr = (struct elf_phdr *)bufp;
851 bufp += sizeof(struct elf_phdr);
852 phdr->p_type = PT_NOTE;
853 phdr->p_flags = 0;
854 phdr->p_vaddr = 0;
855 phdr->p_align = 0;
857 phdr->p_offset = 0;
858 phdr->p_paddr = 0;
859 phdr->p_filesz = 0;
860 phdr->p_memsz = 0;
862 (elf->e_phnum)++;
864 /* setup ELF PT_NOTE for vmcoreinfo */
865 phdr = (struct elf_phdr *)bufp;
866 bufp += sizeof(struct elf_phdr);
867 phdr->p_type = PT_NOTE;
868 phdr->p_flags = 0;
869 phdr->p_vaddr = 0;
870 phdr->p_align = 0;
872 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
873 phdr->p_offset = phdr->p_paddr;
874 phdr->p_memsz = vmcoreinfo_max_size;
875 phdr->p_filesz = vmcoreinfo_max_size;
877 /* Increment number of program headers. */
878 (elf->e_phnum)++;
880 /* setup PT_LOAD sections. */
882 for (i = 0; i < crash_mem_ranges; i++) {
883 unsigned long long mbase, msize;
884 mbase = crash_memory_ranges[i].base;
885 msize = crash_memory_ranges[i].size;
887 if (!msize)
888 continue;
890 phdr = (struct elf_phdr *)bufp;
891 bufp += sizeof(struct elf_phdr);
892 phdr->p_type = PT_LOAD;
893 phdr->p_flags = PF_R|PF_W|PF_X;
894 phdr->p_offset = mbase;
896 if (mbase == RMA_START) {
898 * The entire RMA region will be moved by firmware
899 * to the specified destination_address. Hence set
900 * the correct offset.
902 phdr->p_offset = fdm.rmr_region.destination_address;
905 phdr->p_paddr = mbase;
906 phdr->p_vaddr = (unsigned long)__va(mbase);
907 phdr->p_filesz = msize;
908 phdr->p_memsz = msize;
909 phdr->p_align = 0;
911 /* Increment number of program headers. */
912 (elf->e_phnum)++;
914 return 0;
917 static unsigned long init_fadump_header(unsigned long addr)
919 struct fadump_crash_info_header *fdh;
921 if (!addr)
922 return 0;
924 fw_dump.fadumphdr_addr = addr;
925 fdh = __va(addr);
926 addr += sizeof(struct fadump_crash_info_header);
928 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
929 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
930 fdh->elfcorehdr_addr = addr;
931 /* We will set the crashing cpu id in crash_fadump() during crash. */
932 fdh->crashing_cpu = CPU_UNKNOWN;
934 return addr;
937 static void register_fadump(void)
939 unsigned long addr;
940 void *vaddr;
943 * If no memory is reserved then we can not register for firmware-
944 * assisted dump.
946 if (!fw_dump.reserve_dump_area_size)
947 return;
949 fadump_setup_crash_memory_ranges();
951 addr = fdm.rmr_region.destination_address + fdm.rmr_region.source_len;
952 /* Initialize fadump crash info header. */
953 addr = init_fadump_header(addr);
954 vaddr = __va(addr);
956 pr_debug("Creating ELF core headers at %#016lx\n", addr);
957 fadump_create_elfcore_headers(vaddr);
959 /* register the future kernel dump with firmware. */
960 register_fw_dump(&fdm);
963 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
965 int rc = 0;
966 unsigned int wait_time;
968 pr_debug("Un-register firmware-assisted dump\n");
970 /* TODO: Add upper time limit for the delay */
971 do {
972 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
973 FADUMP_UNREGISTER, fdm,
974 sizeof(struct fadump_mem_struct));
976 wait_time = rtas_busy_delay_time(rc);
977 if (wait_time)
978 mdelay(wait_time);
979 } while (wait_time);
981 if (rc) {
982 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
983 " unexpected error(%d).\n", rc);
984 return rc;
986 fw_dump.dump_registered = 0;
987 return 0;
990 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
992 int rc = 0;
993 unsigned int wait_time;
995 pr_debug("Invalidating firmware-assisted dump registration\n");
997 /* TODO: Add upper time limit for the delay */
998 do {
999 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1000 FADUMP_INVALIDATE, fdm,
1001 sizeof(struct fadump_mem_struct));
1003 wait_time = rtas_busy_delay_time(rc);
1004 if (wait_time)
1005 mdelay(wait_time);
1006 } while (wait_time);
1008 if (rc) {
1009 printk(KERN_ERR "Failed to invalidate firmware-assisted dump "
1010 "rgistration. unexpected error(%d).\n", rc);
1011 return rc;
1013 fw_dump.dump_active = 0;
1014 fdm_active = NULL;
1015 return 0;
1018 void fadump_cleanup(void)
1020 /* Invalidate the registration only if dump is active. */
1021 if (fw_dump.dump_active) {
1022 init_fadump_mem_struct(&fdm,
1023 fdm_active->cpu_state_data.destination_address);
1024 fadump_invalidate_dump(&fdm);
1029 * Release the memory that was reserved in early boot to preserve the memory
1030 * contents. The released memory will be available for general use.
1032 static void fadump_release_memory(unsigned long begin, unsigned long end)
1034 unsigned long addr;
1035 unsigned long ra_start, ra_end;
1037 ra_start = fw_dump.reserve_dump_area_start;
1038 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1040 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1042 * exclude the dump reserve area. Will reuse it for next
1043 * fadump registration.
1045 if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1046 continue;
1048 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1052 static void fadump_invalidate_release_mem(void)
1054 unsigned long reserved_area_start, reserved_area_end;
1055 unsigned long destination_address;
1057 mutex_lock(&fadump_mutex);
1058 if (!fw_dump.dump_active) {
1059 mutex_unlock(&fadump_mutex);
1060 return;
1063 destination_address = fdm_active->cpu_state_data.destination_address;
1064 fadump_cleanup();
1065 mutex_unlock(&fadump_mutex);
1068 * Save the current reserved memory bounds we will require them
1069 * later for releasing the memory for general use.
1071 reserved_area_start = fw_dump.reserve_dump_area_start;
1072 reserved_area_end = reserved_area_start +
1073 fw_dump.reserve_dump_area_size;
1075 * Setup reserve_dump_area_start and its size so that we can
1076 * reuse this reserved memory for Re-registration.
1078 fw_dump.reserve_dump_area_start = destination_address;
1079 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1081 fadump_release_memory(reserved_area_start, reserved_area_end);
1082 if (fw_dump.cpu_notes_buf) {
1083 fadump_cpu_notes_buf_free(
1084 (unsigned long)__va(fw_dump.cpu_notes_buf),
1085 fw_dump.cpu_notes_buf_size);
1086 fw_dump.cpu_notes_buf = 0;
1087 fw_dump.cpu_notes_buf_size = 0;
1089 /* Initialize the kernel dump memory structure for FAD registration. */
1090 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1093 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1094 struct kobj_attribute *attr,
1095 const char *buf, size_t count)
1097 if (!fw_dump.dump_active)
1098 return -EPERM;
1100 if (buf[0] == '1') {
1102 * Take away the '/proc/vmcore'. We are releasing the dump
1103 * memory, hence it will not be valid anymore.
1105 vmcore_cleanup();
1106 fadump_invalidate_release_mem();
1108 } else
1109 return -EINVAL;
1110 return count;
1113 static ssize_t fadump_enabled_show(struct kobject *kobj,
1114 struct kobj_attribute *attr,
1115 char *buf)
1117 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1120 static ssize_t fadump_register_show(struct kobject *kobj,
1121 struct kobj_attribute *attr,
1122 char *buf)
1124 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1127 static ssize_t fadump_register_store(struct kobject *kobj,
1128 struct kobj_attribute *attr,
1129 const char *buf, size_t count)
1131 int ret = 0;
1133 if (!fw_dump.fadump_enabled || fdm_active)
1134 return -EPERM;
1136 mutex_lock(&fadump_mutex);
1138 switch (buf[0]) {
1139 case '0':
1140 if (fw_dump.dump_registered == 0) {
1141 ret = -EINVAL;
1142 goto unlock_out;
1144 /* Un-register Firmware-assisted dump */
1145 fadump_unregister_dump(&fdm);
1146 break;
1147 case '1':
1148 if (fw_dump.dump_registered == 1) {
1149 ret = -EINVAL;
1150 goto unlock_out;
1152 /* Register Firmware-assisted dump */
1153 register_fadump();
1154 break;
1155 default:
1156 ret = -EINVAL;
1157 break;
1160 unlock_out:
1161 mutex_unlock(&fadump_mutex);
1162 return ret < 0 ? ret : count;
1165 static int fadump_region_show(struct seq_file *m, void *private)
1167 const struct fadump_mem_struct *fdm_ptr;
1169 if (!fw_dump.fadump_enabled)
1170 return 0;
1172 mutex_lock(&fadump_mutex);
1173 if (fdm_active)
1174 fdm_ptr = fdm_active;
1175 else {
1176 mutex_unlock(&fadump_mutex);
1177 fdm_ptr = &fdm;
1180 seq_printf(m,
1181 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1182 "Dumped: %#llx\n",
1183 fdm_ptr->cpu_state_data.destination_address,
1184 fdm_ptr->cpu_state_data.destination_address +
1185 fdm_ptr->cpu_state_data.source_len - 1,
1186 fdm_ptr->cpu_state_data.source_len,
1187 fdm_ptr->cpu_state_data.bytes_dumped);
1188 seq_printf(m,
1189 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1190 "Dumped: %#llx\n",
1191 fdm_ptr->hpte_region.destination_address,
1192 fdm_ptr->hpte_region.destination_address +
1193 fdm_ptr->hpte_region.source_len - 1,
1194 fdm_ptr->hpte_region.source_len,
1195 fdm_ptr->hpte_region.bytes_dumped);
1196 seq_printf(m,
1197 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1198 "Dumped: %#llx\n",
1199 fdm_ptr->rmr_region.destination_address,
1200 fdm_ptr->rmr_region.destination_address +
1201 fdm_ptr->rmr_region.source_len - 1,
1202 fdm_ptr->rmr_region.source_len,
1203 fdm_ptr->rmr_region.bytes_dumped);
1205 if (!fdm_active ||
1206 (fw_dump.reserve_dump_area_start ==
1207 fdm_ptr->cpu_state_data.destination_address))
1208 goto out;
1210 /* Dump is active. Show reserved memory region. */
1211 seq_printf(m,
1212 " : [%#016llx-%#016llx] %#llx bytes, "
1213 "Dumped: %#llx\n",
1214 (unsigned long long)fw_dump.reserve_dump_area_start,
1215 fdm_ptr->cpu_state_data.destination_address - 1,
1216 fdm_ptr->cpu_state_data.destination_address -
1217 fw_dump.reserve_dump_area_start,
1218 fdm_ptr->cpu_state_data.destination_address -
1219 fw_dump.reserve_dump_area_start);
1220 out:
1221 if (fdm_active)
1222 mutex_unlock(&fadump_mutex);
1223 return 0;
1226 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1227 0200, NULL,
1228 fadump_release_memory_store);
1229 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1230 0444, fadump_enabled_show,
1231 NULL);
1232 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1233 0644, fadump_register_show,
1234 fadump_register_store);
1236 static int fadump_region_open(struct inode *inode, struct file *file)
1238 return single_open(file, fadump_region_show, inode->i_private);
1241 static const struct file_operations fadump_region_fops = {
1242 .open = fadump_region_open,
1243 .read = seq_read,
1244 .llseek = seq_lseek,
1245 .release = single_release,
1248 static void fadump_init_files(void)
1250 struct dentry *debugfs_file;
1251 int rc = 0;
1253 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1254 if (rc)
1255 printk(KERN_ERR "fadump: unable to create sysfs file"
1256 " fadump_enabled (%d)\n", rc);
1258 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1259 if (rc)
1260 printk(KERN_ERR "fadump: unable to create sysfs file"
1261 " fadump_registered (%d)\n", rc);
1263 debugfs_file = debugfs_create_file("fadump_region", 0444,
1264 powerpc_debugfs_root, NULL,
1265 &fadump_region_fops);
1266 if (!debugfs_file)
1267 printk(KERN_ERR "fadump: unable to create debugfs file"
1268 " fadump_region\n");
1270 if (fw_dump.dump_active) {
1271 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1272 if (rc)
1273 printk(KERN_ERR "fadump: unable to create sysfs file"
1274 " fadump_release_mem (%d)\n", rc);
1276 return;
1280 * Prepare for firmware-assisted dump.
1282 int __init setup_fadump(void)
1284 if (!fw_dump.fadump_enabled)
1285 return 0;
1287 if (!fw_dump.fadump_supported) {
1288 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1289 " this hardware\n");
1290 return 0;
1293 fadump_show_config();
1295 * If dump data is available then see if it is valid and prepare for
1296 * saving it to the disk.
1298 if (fw_dump.dump_active) {
1300 * if dump process fails then invalidate the registration
1301 * and release memory before proceeding for re-registration.
1303 if (process_fadump(fdm_active) < 0)
1304 fadump_invalidate_release_mem();
1306 /* Initialize the kernel dump memory structure for FAD registration. */
1307 else if (fw_dump.reserve_dump_area_size)
1308 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1309 fadump_init_files();
1311 return 1;
1313 subsys_initcall(setup_fadump);