Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fadump.c
Go to the documentation of this file.
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
8  *
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.
13  *
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.
18  *
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.
22  *
23  * Copyright 2011 IBM Corporation
24  * Author: Mahesh Salgaonkar <[email protected]>
25  */
26 
27 #undef DEBUG
28 #define pr_fmt(fmt) "fadump: " fmt
29 
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 
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>
45 
46 static struct fw_dump fw_dump;
47 static struct fadump_mem_struct fdm;
48 static const struct fadump_mem_struct *fdm_active;
49 
50 static DEFINE_MUTEX(fadump_mutex);
51 struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
53 
54 /* Scan the Firmware Assisted dump configuration details. */
56  const char *uname, int depth, void *data)
57 {
58  __be32 *sections;
59  int i, num_sections;
60  unsigned long size;
61  const int *token;
62 
63  if (depth != 1 || strcmp(uname, "rtas") != 0)
64  return 0;
65 
66  /*
67  * Check if Firmware Assisted dump is supported. if yes, check
68  * if dump has been initiated on last reboot.
69  */
70  token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
71  if (!token)
72  return 0;
73 
74  fw_dump.fadump_supported = 1;
75  fw_dump.ibm_configure_kernel_dump = *token;
76 
77  /*
78  * The 'ibm,kernel-dump' rtas node is present only if there is
79  * dump data waiting for us.
80  */
81  fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
82  if (fdm_active)
83  fw_dump.dump_active = 1;
84 
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.
90  */
91  sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
92  &size);
93 
94  if (!sections)
95  return 0;
96 
97  num_sections = size / (3 * sizeof(u32));
98 
99  for (i = 0; i < num_sections; i++, sections += 3) {
100  u32 type = (u32)of_read_number(sections, 1);
101 
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;
111  }
112  }
113  return 1;
114 }
115 
117 {
118  return fw_dump.dump_active;
119 }
120 
121 /* Print firmware assisted dump configurations for debugging purpose. */
122 static void fadump_show_config(void)
123 {
124  pr_debug("Support for firmware-assisted dump (fadump): %s\n",
125  (fw_dump.fadump_supported ? "present" : "no support"));
126 
127  if (!fw_dump.fadump_supported)
128  return;
129 
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);
138 }
139 
140 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
141  unsigned long addr)
142 {
143  if (!fdm)
144  return 0;
145 
146  memset(fdm, 0, sizeof(struct fadump_mem_struct));
147  addr = addr & PAGE_MASK;
148 
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);
154 
155  /*
156  * Fields for disk dump option.
157  * We are not using disk dump option, hence set these fields to 0.
158  */
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;
163 
164  /* set 0 to disable an automatic dump-reboot. */
165  fdm->header.max_time_auto = 0;
166 
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;
175 
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;
183 
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;
191 
192  return addr;
193 }
194 
210 static inline unsigned long fadump_calculate_reserve_size(void)
211 {
212  unsigned long size;
213 
214  /*
215  * Check if the size is specified through fadump_reserve_mem= cmdline
216  * option. If yes, then use that.
217  */
218  if (fw_dump.reserve_bootvar)
219  return fw_dump.reserve_bootvar;
220 
221  /* divide by 20 to get 5% of value */
222  size = memblock_end_of_DRAM() / 20;
223 
224  /* round it down in multiples of 256 */
225  size = size & ~0x0FFFFFFFUL;
226 
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;
230 
231  return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
232 }
233 
234 /*
235  * Calculate the total memory size required to be reserved for
236  * firmware-assisted dump registration.
237  */
238 static unsigned long get_fadump_area_size(void)
239 {
240  unsigned long size = 0;
241 
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);
250 
251  size = PAGE_ALIGN(size);
252  return size;
253 }
254 
256 {
257  unsigned long base, size, memory_boundary;
258 
259  if (!fw_dump.fadump_enabled)
260  return 0;
261 
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;
267  }
268  /*
269  * Initialize boot memory size
270  * If dump is active then we have already calculated the size during
271  * first kernel.
272  */
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();
277 
278  /*
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.
284  */
286  size = get_fadump_area_size();
287  if ((memory_limit + size) < memblock_end_of_DRAM())
288  memory_limit += size;
289  else
291  printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
292  " dump, now %#016llx\n", memory_limit);
293  }
294  if (memory_limit)
295  memory_boundary = memory_limit;
296  else
297  memory_boundary = memblock_end_of_DRAM();
298 
299  if (fw_dump.dump_active) {
300  printk(KERN_INFO "Firmware-assisted dump is active.\n");
301  /*
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.
306  */
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));
314 
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));
329  }
330  fw_dump.reserve_dump_area_start = base;
331  fw_dump.reserve_dump_area_size = size;
332  return 1;
333 }
334 
335 /* Look for fadump= cmdline option. */
336 static int __init early_fadump_param(char *p)
337 {
338  if (!p)
339  return 1;
340 
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;
345 
346  return 0;
347 }
348 early_param("fadump", early_fadump_param);
349 
350 /* Look for fadump_reserve_mem= cmdline option */
351 static int __init early_fadump_reserve_mem(char *p)
352 {
353  if (p)
354  fw_dump.reserve_bootvar = memparse(p, &p);
355  return 0;
356 }
357 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
358 
359 static void register_fw_dump(struct fadump_mem_struct *fdm)
360 {
361  int rc;
362  unsigned int wait_time;
363 
364  pr_debug("Registering for firmware-assisted kernel dump...\n");
365 
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));
371 
372  wait_time = rtas_busy_delay_time(rc);
373  if (wait_time)
374  mdelay(wait_time);
375 
376  } while (wait_time);
377 
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;
397  }
398 }
399 
400 void crash_fadump(struct pt_regs *regs, const char *str)
401 {
402  struct fadump_crash_info_header *fdh = NULL;
403 
404  if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
405  return;
406 
407  fdh = __va(fw_dump.fadumphdr_addr);
409  fdh->crashing_cpu = crashing_cpu;
411 
412  if (regs)
413  fdh->regs = *regs;
414  else
415  ppc_save_regs(&fdh->regs);
416 
417  fdh->cpu_online_mask = *cpu_online_mask;
418 
419  /* Call ibm,os-term rtas call to trigger firmware assisted dump */
420  rtas_os_term((char *)str);
421 }
422 
423 #define GPR_MASK 0xffffff0000000000
424 static inline int fadump_gpr_index(u64 id)
425 {
426  int i = -1;
427  char str[3];
428 
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;
439  }
440  return i;
441 }
442 
443 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
444  u64 reg_val)
445 {
446  int i;
447 
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;
467 }
468 
469 static struct fadump_reg_entry*
470 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
471 {
472  memset(regs, 0, sizeof(struct pt_regs));
473 
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++;
478  }
479  reg_entry++;
480  return reg_entry;
481 }
482 
483 static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
484  void *data, size_t data_len)
485 {
486  struct elf_note note;
487 
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;
497 
498  return buf;
499 }
500 
501 static void fadump_final_note(u32 *buf)
502 {
503  struct elf_note note;
504 
505  note.n_namesz = 0;
506  note.n_descsz = 0;
507  note.n_type = 0;
508  memcpy(buf, &note, sizeof(note));
509 }
510 
511 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
512 {
513  struct elf_prstatus prstatus;
514 
515  memset(&prstatus, 0, sizeof(prstatus));
516  /*
517  * FIXME: How do i get PID? Do I really need it?
518  * prstatus.pr_pid = ????
519  */
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;
524 }
525 
526 static void fadump_update_elfcore_header(char *bufp)
527 {
528  struct elfhdr *elf;
529  struct elf_phdr *phdr;
530 
531  elf = (struct elfhdr *)bufp;
532  bufp += sizeof(struct elfhdr);
533 
534  /* First note is a place holder for cpu notes info. */
535  phdr = (struct elf_phdr *)bufp;
536 
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;
542  }
543  return;
544 }
545 
546 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
547 {
548  void *vaddr;
549  struct page *page;
550  unsigned long order, count, i;
551 
552  order = get_order(size);
553  vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
554  if (!vaddr)
555  return NULL;
556 
557  count = 1 << order;
558  page = virt_to_page(vaddr);
559  for (i = 0; i < count; i++)
560  SetPageReserved(page + i);
561  return vaddr;
562 }
563 
564 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
565 {
566  struct page *page;
567  unsigned long order, count, i;
568 
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);
575 }
576 
577 /*
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.
586  *
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.
590  */
591 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
592 {
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;
601 
602  if (!fdm->cpu_state_data.bytes_dumped)
603  return -EINVAL;
604 
605  addr = fdm->cpu_state_data.destination_address;
606  vaddr = __va(addr);
607 
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;
612  }
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);
616 
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;
622 
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;
631  }
632  fw_dump.cpu_notes_buf = __pa(note_buf);
633 
634  pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
635  (num_cpus * sizeof(note_buf_t)), note_buf);
636 
637  if (fw_dump.fadumphdr_addr)
638  fdh = __va(fw_dump.fadumphdr_addr);
639 
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;
645  }
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;
651  }
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);
661  }
662  }
663  fadump_final_note(note_buf);
664 
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;
669 
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;
676 
677 }
678 
679 /*
680  * Validate and process the dump data stored by firmware before exporting
681  * it through '/proc/vmcore'.
682  */
683 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
684 {
685  struct fadump_crash_info_header *fdh;
686  int rc = 0;
687 
688  if (!fdm_active || !fw_dump.fadumphdr_addr)
689  return -EINVAL;
690 
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;
697  }
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;
703  }
704 
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;
710  }
711 
712  rc = fadump_build_cpu_notes(fdm_active);
713  if (rc)
714  return rc;
715 
716  /*
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'.
720  */
721  elfcorehdr_addr = fdh->elfcorehdr_addr;
722 
723  return 0;
724 }
725 
726 static inline void fadump_add_crash_memory(unsigned long long base,
727  unsigned long long end)
728 {
729  if (base == end)
730  return;
731 
732  pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
733  crash_mem_ranges, base, end - 1, (end - base));
735  crash_memory_ranges[crash_mem_ranges].size = end - base;
737 }
738 
739 static void fadump_exclude_reserved_area(unsigned long long start,
740  unsigned long long end)
741 {
742  unsigned long long ra_start, ra_end;
743 
744  ra_start = fw_dump.reserve_dump_area_start;
745  ra_end = ra_start + fw_dump.reserve_dump_area_size;
746 
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);
755  }
756  } else
757  fadump_add_crash_memory(start, end);
758 }
759 
760 static int fadump_init_elfcore_header(char *bufp)
761 {
762  struct elfhdr *elf;
763 
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;
785 
786  return 0;
787 }
788 
789 /*
790  * Traverse through memblock structure and setup crash memory ranges. These
791  * ranges will be used create PT_LOAD program headers in elfcore header.
792  */
793 static void fadump_setup_crash_memory_ranges(void)
794 {
795  struct memblock_region *reg;
796  unsigned long long start, end;
797 
798  pr_debug("Setup crash memory ranges.\n");
799  crash_mem_ranges = 0;
800  /*
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.
806  */
807  fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
808 
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;
814 
815  /* add this range excluding the reserved dump area. */
816  fadump_exclude_reserved_area(start, end);
817  }
818 }
819 
820 /*
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.
824  */
825 static inline unsigned long fadump_relocate(unsigned long paddr)
826 {
827  if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
828  return fdm.rmr_region.destination_address + paddr;
829  else
830  return paddr;
831 }
832 
833 static int fadump_create_elfcore_headers(char *bufp)
834 {
835  struct elfhdr *elf;
836  struct elf_phdr *phdr;
837  int i;
838 
839  fadump_init_elfcore_header(bufp);
840  elf = (struct elfhdr *)bufp;
841  bufp += sizeof(struct elfhdr);
842 
843  /*
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.
847  *
848  * NOTE: Any new ELF note addition should be placed after this note.
849  */
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;
856 
857  phdr->p_offset = 0;
858  phdr->p_paddr = 0;
859  phdr->p_filesz = 0;
860  phdr->p_memsz = 0;
861 
862  (elf->e_phnum)++;
863 
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;
871 
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;
876 
877  /* Increment number of program headers. */
878  (elf->e_phnum)++;
879 
880  /* setup PT_LOAD sections. */
881 
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;
886 
887  if (!msize)
888  continue;
889 
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;
895 
896  if (mbase == RMA_START) {
897  /*
898  * The entire RMA region will be moved by firmware
899  * to the specified destination_address. Hence set
900  * the correct offset.
901  */
902  phdr->p_offset = fdm.rmr_region.destination_address;
903  }
904 
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;
910 
911  /* Increment number of program headers. */
912  (elf->e_phnum)++;
913  }
914  return 0;
915 }
916 
917 static unsigned long init_fadump_header(unsigned long addr)
918 {
919  struct fadump_crash_info_header *fdh;
920 
921  if (!addr)
922  return 0;
923 
924  fw_dump.fadumphdr_addr = addr;
925  fdh = __va(addr);
926  addr += sizeof(struct fadump_crash_info_header);
927 
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;
933 
934  return addr;
935 }
936 
937 static void register_fadump(void)
938 {
939  unsigned long addr;
940  void *vaddr;
941 
942  /*
943  * If no memory is reserved then we can not register for firmware-
944  * assisted dump.
945  */
946  if (!fw_dump.reserve_dump_area_size)
947  return;
948 
949  fadump_setup_crash_memory_ranges();
950 
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);
955 
956  pr_debug("Creating ELF core headers at %#016lx\n", addr);
957  fadump_create_elfcore_headers(vaddr);
958 
959  /* register the future kernel dump with firmware. */
960  register_fw_dump(&fdm);
961 }
962 
963 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
964 {
965  int rc = 0;
966  unsigned int wait_time;
967 
968  pr_debug("Un-register firmware-assisted dump\n");
969 
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));
975 
976  wait_time = rtas_busy_delay_time(rc);
977  if (wait_time)
978  mdelay(wait_time);
979  } while (wait_time);
980 
981  if (rc) {
982  printk(KERN_ERR "Failed to un-register firmware-assisted dump."
983  " unexpected error(%d).\n", rc);
984  return rc;
985  }
986  fw_dump.dump_registered = 0;
987  return 0;
988 }
989 
990 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
991 {
992  int rc = 0;
993  unsigned int wait_time;
994 
995  pr_debug("Invalidating firmware-assisted dump registration\n");
996 
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));
1002 
1003  wait_time = rtas_busy_delay_time(rc);
1004  if (wait_time)
1005  mdelay(wait_time);
1006  } while (wait_time);
1007 
1008  if (rc) {
1009  printk(KERN_ERR "Failed to invalidate firmware-assisted dump "
1010  "rgistration. unexpected error(%d).\n", rc);
1011  return rc;
1012  }
1013  fw_dump.dump_active = 0;
1014  fdm_active = NULL;
1015  return 0;
1016 }
1017 
1018 void fadump_cleanup(void)
1019 {
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);
1025  }
1026 }
1027 
1028 /*
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.
1031  */
1032 static void fadump_release_memory(unsigned long begin, unsigned long end)
1033 {
1034  unsigned long addr;
1035  unsigned long ra_start, ra_end;
1036 
1037  ra_start = fw_dump.reserve_dump_area_start;
1038  ra_end = ra_start + fw_dump.reserve_dump_area_size;
1039 
1040  for (addr = begin; addr < end; addr += PAGE_SIZE) {
1041  /*
1042  * exclude the dump reserve area. Will reuse it for next
1043  * fadump registration.
1044  */
1045  if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1046  continue;
1047 
1048  ClearPageReserved(pfn_to_page(addr >> PAGE_SHIFT));
1049  init_page_count(pfn_to_page(addr >> PAGE_SHIFT));
1050  free_page((unsigned long)__va(addr));
1051  totalram_pages++;
1052  }
1053 }
1054 
1055 static void fadump_invalidate_release_mem(void)
1056 {
1057  unsigned long reserved_area_start, reserved_area_end;
1058  unsigned long destination_address;
1059 
1060  mutex_lock(&fadump_mutex);
1061  if (!fw_dump.dump_active) {
1062  mutex_unlock(&fadump_mutex);
1063  return;
1064  }
1065 
1066  destination_address = fdm_active->cpu_state_data.destination_address;
1067  fadump_cleanup();
1068  mutex_unlock(&fadump_mutex);
1069 
1070  /*
1071  * Save the current reserved memory bounds we will require them
1072  * later for releasing the memory for general use.
1073  */
1074  reserved_area_start = fw_dump.reserve_dump_area_start;
1075  reserved_area_end = reserved_area_start +
1076  fw_dump.reserve_dump_area_size;
1077  /*
1078  * Setup reserve_dump_area_start and its size so that we can
1079  * reuse this reserved memory for Re-registration.
1080  */
1081  fw_dump.reserve_dump_area_start = destination_address;
1082  fw_dump.reserve_dump_area_size = get_fadump_area_size();
1083 
1084  fadump_release_memory(reserved_area_start, reserved_area_end);
1085  if (fw_dump.cpu_notes_buf) {
1086  fadump_cpu_notes_buf_free(
1087  (unsigned long)__va(fw_dump.cpu_notes_buf),
1088  fw_dump.cpu_notes_buf_size);
1089  fw_dump.cpu_notes_buf = 0;
1090  fw_dump.cpu_notes_buf_size = 0;
1091  }
1092  /* Initialize the kernel dump memory structure for FAD registration. */
1093  init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1094 }
1095 
1096 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1097  struct kobj_attribute *attr,
1098  const char *buf, size_t count)
1099 {
1100  if (!fw_dump.dump_active)
1101  return -EPERM;
1102 
1103  if (buf[0] == '1') {
1104  /*
1105  * Take away the '/proc/vmcore'. We are releasing the dump
1106  * memory, hence it will not be valid anymore.
1107  */
1108  vmcore_cleanup();
1109  fadump_invalidate_release_mem();
1110 
1111  } else
1112  return -EINVAL;
1113  return count;
1114 }
1115 
1116 static ssize_t fadump_enabled_show(struct kobject *kobj,
1117  struct kobj_attribute *attr,
1118  char *buf)
1119 {
1120  return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1121 }
1122 
1123 static ssize_t fadump_register_show(struct kobject *kobj,
1124  struct kobj_attribute *attr,
1125  char *buf)
1126 {
1127  return sprintf(buf, "%d\n", fw_dump.dump_registered);
1128 }
1129 
1130 static ssize_t fadump_register_store(struct kobject *kobj,
1131  struct kobj_attribute *attr,
1132  const char *buf, size_t count)
1133 {
1134  int ret = 0;
1135 
1136  if (!fw_dump.fadump_enabled || fdm_active)
1137  return -EPERM;
1138 
1139  mutex_lock(&fadump_mutex);
1140 
1141  switch (buf[0]) {
1142  case '0':
1143  if (fw_dump.dump_registered == 0) {
1144  ret = -EINVAL;
1145  goto unlock_out;
1146  }
1147  /* Un-register Firmware-assisted dump */
1148  fadump_unregister_dump(&fdm);
1149  break;
1150  case '1':
1151  if (fw_dump.dump_registered == 1) {
1152  ret = -EINVAL;
1153  goto unlock_out;
1154  }
1155  /* Register Firmware-assisted dump */
1156  register_fadump();
1157  break;
1158  default:
1159  ret = -EINVAL;
1160  break;
1161  }
1162 
1163 unlock_out:
1164  mutex_unlock(&fadump_mutex);
1165  return ret < 0 ? ret : count;
1166 }
1167 
1168 static int fadump_region_show(struct seq_file *m, void *private)
1169 {
1170  const struct fadump_mem_struct *fdm_ptr;
1171 
1172  if (!fw_dump.fadump_enabled)
1173  return 0;
1174 
1175  mutex_lock(&fadump_mutex);
1176  if (fdm_active)
1177  fdm_ptr = fdm_active;
1178  else {
1179  mutex_unlock(&fadump_mutex);
1180  fdm_ptr = &fdm;
1181  }
1182 
1183  seq_printf(m,
1184  "CPU : [%#016llx-%#016llx] %#llx bytes, "
1185  "Dumped: %#llx\n",
1186  fdm_ptr->cpu_state_data.destination_address,
1187  fdm_ptr->cpu_state_data.destination_address +
1188  fdm_ptr->cpu_state_data.source_len - 1,
1189  fdm_ptr->cpu_state_data.source_len,
1190  fdm_ptr->cpu_state_data.bytes_dumped);
1191  seq_printf(m,
1192  "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1193  "Dumped: %#llx\n",
1194  fdm_ptr->hpte_region.destination_address,
1195  fdm_ptr->hpte_region.destination_address +
1196  fdm_ptr->hpte_region.source_len - 1,
1197  fdm_ptr->hpte_region.source_len,
1198  fdm_ptr->hpte_region.bytes_dumped);
1199  seq_printf(m,
1200  "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1201  "Dumped: %#llx\n",
1202  fdm_ptr->rmr_region.destination_address,
1203  fdm_ptr->rmr_region.destination_address +
1204  fdm_ptr->rmr_region.source_len - 1,
1205  fdm_ptr->rmr_region.source_len,
1206  fdm_ptr->rmr_region.bytes_dumped);
1207 
1208  if (!fdm_active ||
1209  (fw_dump.reserve_dump_area_start ==
1210  fdm_ptr->cpu_state_data.destination_address))
1211  goto out;
1212 
1213  /* Dump is active. Show reserved memory region. */
1214  seq_printf(m,
1215  " : [%#016llx-%#016llx] %#llx bytes, "
1216  "Dumped: %#llx\n",
1217  (unsigned long long)fw_dump.reserve_dump_area_start,
1218  fdm_ptr->cpu_state_data.destination_address - 1,
1219  fdm_ptr->cpu_state_data.destination_address -
1220  fw_dump.reserve_dump_area_start,
1221  fdm_ptr->cpu_state_data.destination_address -
1222  fw_dump.reserve_dump_area_start);
1223 out:
1224  if (fdm_active)
1225  mutex_unlock(&fadump_mutex);
1226  return 0;
1227 }
1228 
1229 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1230  0200, NULL,
1231  fadump_release_memory_store);
1232 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1233  0444, fadump_enabled_show,
1234  NULL);
1235 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1236  0644, fadump_register_show,
1237  fadump_register_store);
1238 
1239 static int fadump_region_open(struct inode *inode, struct file *file)
1240 {
1241  return single_open(file, fadump_region_show, inode->i_private);
1242 }
1243 
1244 static const struct file_operations fadump_region_fops = {
1245  .open = fadump_region_open,
1246  .read = seq_read,
1247  .llseek = seq_lseek,
1248  .release = single_release,
1249 };
1250 
1251 static void fadump_init_files(void)
1252 {
1253  struct dentry *debugfs_file;
1254  int rc = 0;
1255 
1256  rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1257  if (rc)
1258  printk(KERN_ERR "fadump: unable to create sysfs file"
1259  " fadump_enabled (%d)\n", rc);
1260 
1261  rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1262  if (rc)
1263  printk(KERN_ERR "fadump: unable to create sysfs file"
1264  " fadump_registered (%d)\n", rc);
1265 
1266  debugfs_file = debugfs_create_file("fadump_region", 0444,
1267  powerpc_debugfs_root, NULL,
1268  &fadump_region_fops);
1269  if (!debugfs_file)
1270  printk(KERN_ERR "fadump: unable to create debugfs file"
1271  " fadump_region\n");
1272 
1273  if (fw_dump.dump_active) {
1274  rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1275  if (rc)
1276  printk(KERN_ERR "fadump: unable to create sysfs file"
1277  " fadump_release_mem (%d)\n", rc);
1278  }
1279  return;
1280 }
1281 
1282 /*
1283  * Prepare for firmware-assisted dump.
1284  */
1286 {
1287  if (!fw_dump.fadump_enabled)
1288  return 0;
1289 
1290  if (!fw_dump.fadump_supported) {
1291  printk(KERN_ERR "Firmware-assisted dump is not supported on"
1292  " this hardware\n");
1293  return 0;
1294  }
1295 
1296  fadump_show_config();
1297  /*
1298  * If dump data is available then see if it is valid and prepare for
1299  * saving it to the disk.
1300  */
1301  if (fw_dump.dump_active) {
1302  /*
1303  * if dump process fails then invalidate the registration
1304  * and release memory before proceeding for re-registration.
1305  */
1306  if (process_fadump(fdm_active) < 0)
1307  fadump_invalidate_release_mem();
1308  }
1309  /* Initialize the kernel dump memory structure for FAD registration. */
1310  else if (fw_dump.reserve_dump_area_size)
1311  init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1312  fadump_init_files();
1313 
1314  return 1;
1315 }