/arch/powerpc/kernel/crash_dump.c

http://github.com/mirrors/linux · C · 146 lines · 89 code · 23 blank · 34 comment · 11 complexity · 5d49523737849dddf0672d56473d0d5e MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Routines for doing kexec-based kdump.
  4. *
  5. * Copyright (C) 2005, IBM Corp.
  6. *
  7. * Created by: Michael Ellerman
  8. */
  9. #undef DEBUG
  10. #include <linux/crash_dump.h>
  11. #include <linux/io.h>
  12. #include <linux/memblock.h>
  13. #include <asm/code-patching.h>
  14. #include <asm/kdump.h>
  15. #include <asm/prom.h>
  16. #include <asm/firmware.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/rtas.h>
  19. #ifdef DEBUG
  20. #include <asm/udbg.h>
  21. #define DBG(fmt...) udbg_printf(fmt)
  22. #else
  23. #define DBG(fmt...)
  24. #endif
  25. #ifndef CONFIG_NONSTATIC_KERNEL
  26. void __init reserve_kdump_trampoline(void)
  27. {
  28. memblock_reserve(0, KDUMP_RESERVE_LIMIT);
  29. }
  30. static void __init create_trampoline(unsigned long addr)
  31. {
  32. unsigned int *p = (unsigned int *)addr;
  33. /* The maximum range of a single instruction branch, is the current
  34. * instruction's address + (32 MB - 4) bytes. For the trampoline we
  35. * need to branch to current address + 32 MB. So we insert a nop at
  36. * the trampoline address, then the next instruction (+ 4 bytes)
  37. * does a branch to (32 MB - 4). The net effect is that when we
  38. * branch to "addr" we jump to ("addr" + 32 MB). Although it requires
  39. * two instructions it doesn't require any registers.
  40. */
  41. patch_instruction(p, PPC_INST_NOP);
  42. patch_branch(++p, addr + PHYSICAL_START, 0);
  43. }
  44. void __init setup_kdump_trampoline(void)
  45. {
  46. unsigned long i;
  47. DBG(" -> setup_kdump_trampoline()\n");
  48. for (i = KDUMP_TRAMPOLINE_START; i < KDUMP_TRAMPOLINE_END; i += 8) {
  49. create_trampoline(i);
  50. }
  51. #ifdef CONFIG_PPC_PSERIES
  52. create_trampoline(__pa(system_reset_fwnmi) - PHYSICAL_START);
  53. create_trampoline(__pa(machine_check_fwnmi) - PHYSICAL_START);
  54. #endif /* CONFIG_PPC_PSERIES */
  55. DBG(" <- setup_kdump_trampoline()\n");
  56. }
  57. #endif /* CONFIG_NONSTATIC_KERNEL */
  58. static size_t copy_oldmem_vaddr(void *vaddr, char *buf, size_t csize,
  59. unsigned long offset, int userbuf)
  60. {
  61. if (userbuf) {
  62. if (copy_to_user((char __user *)buf, (vaddr + offset), csize))
  63. return -EFAULT;
  64. } else
  65. memcpy(buf, (vaddr + offset), csize);
  66. return csize;
  67. }
  68. /**
  69. * copy_oldmem_page - copy one page from "oldmem"
  70. * @pfn: page frame number to be copied
  71. * @buf: target memory address for the copy; this can be in kernel address
  72. * space or user address space (see @userbuf)
  73. * @csize: number of bytes to copy
  74. * @offset: offset in bytes into the page (based on pfn) to begin the copy
  75. * @userbuf: if set, @buf is in user address space, use copy_to_user(),
  76. * otherwise @buf is in kernel address space, use memcpy().
  77. *
  78. * Copy a page from "oldmem". For this page, there is no pte mapped
  79. * in the current kernel. We stitch up a pte, similar to kmap_atomic.
  80. */
  81. ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
  82. size_t csize, unsigned long offset, int userbuf)
  83. {
  84. void *vaddr;
  85. phys_addr_t paddr;
  86. if (!csize)
  87. return 0;
  88. csize = min_t(size_t, csize, PAGE_SIZE);
  89. paddr = pfn << PAGE_SHIFT;
  90. if (memblock_is_region_memory(paddr, csize)) {
  91. vaddr = __va(paddr);
  92. csize = copy_oldmem_vaddr(vaddr, buf, csize, offset, userbuf);
  93. } else {
  94. vaddr = ioremap_cache(paddr, PAGE_SIZE);
  95. csize = copy_oldmem_vaddr(vaddr, buf, csize, offset, userbuf);
  96. iounmap(vaddr);
  97. }
  98. return csize;
  99. }
  100. #ifdef CONFIG_PPC_RTAS
  101. /*
  102. * The crashkernel region will almost always overlap the RTAS region, so
  103. * we have to be careful when shrinking the crashkernel region.
  104. */
  105. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  106. {
  107. unsigned long addr;
  108. const __be32 *basep, *sizep;
  109. unsigned int rtas_start = 0, rtas_end = 0;
  110. basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
  111. sizep = of_get_property(rtas.dev, "rtas-size", NULL);
  112. if (basep && sizep) {
  113. rtas_start = be32_to_cpup(basep);
  114. rtas_end = rtas_start + be32_to_cpup(sizep);
  115. }
  116. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  117. /* Does this page overlap with the RTAS region? */
  118. if (addr <= rtas_end && ((addr + PAGE_SIZE) > rtas_start))
  119. continue;
  120. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  121. }
  122. }
  123. #endif