crash_dump.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_CRASH_DUMP_H
  3. #define LINUX_CRASH_DUMP_H
  4. #include <linux/kexec.h>
  5. #include <linux/proc_fs.h>
  6. #include <linux/elf.h>
  7. #include <linux/pgtable.h>
  8. #include <uapi/linux/vmcore.h>
  9. /* For IS_ENABLED(CONFIG_CRASH_DUMP) */
  10. #define ELFCORE_ADDR_MAX (-1ULL)
  11. #define ELFCORE_ADDR_ERR (-2ULL)
  12. extern unsigned long long elfcorehdr_addr;
  13. extern unsigned long long elfcorehdr_size;
  14. #ifdef CONFIG_CRASH_DUMP
  15. extern int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size);
  16. extern void elfcorehdr_free(unsigned long long addr);
  17. extern ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos);
  18. extern ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos);
  19. extern int remap_oldmem_pfn_range(struct vm_area_struct *vma,
  20. unsigned long from, unsigned long pfn,
  21. unsigned long size, pgprot_t prot);
  22. ssize_t copy_oldmem_page(struct iov_iter *i, unsigned long pfn, size_t csize,
  23. unsigned long offset);
  24. ssize_t copy_oldmem_page_encrypted(struct iov_iter *iter, unsigned long pfn,
  25. size_t csize, unsigned long offset);
  26. void vmcore_cleanup(void);
  27. /* Architecture code defines this if there are other possible ELF
  28. * machine types, e.g. on bi-arch capable hardware. */
  29. #ifndef vmcore_elf_check_arch_cross
  30. #define vmcore_elf_check_arch_cross(x) 0
  31. #endif
  32. /*
  33. * Architecture code can redefine this if there are any special checks
  34. * needed for 32-bit ELF or 64-bit ELF vmcores. In case of 32-bit
  35. * only architecture, vmcore_elf64_check_arch can be set to zero.
  36. */
  37. #ifndef vmcore_elf32_check_arch
  38. #define vmcore_elf32_check_arch(x) elf_check_arch(x)
  39. #endif
  40. #ifndef vmcore_elf64_check_arch
  41. #define vmcore_elf64_check_arch(x) (elf_check_arch(x) || vmcore_elf_check_arch_cross(x))
  42. #endif
  43. /*
  44. * is_kdump_kernel() checks whether this kernel is booting after a panic of
  45. * previous kernel or not. This is determined by checking if previous kernel
  46. * has passed the elf core header address on command line.
  47. *
  48. * This is not just a test if CONFIG_CRASH_DUMP is enabled or not. It will
  49. * return true if CONFIG_CRASH_DUMP=y and if kernel is booting after a panic
  50. * of previous kernel.
  51. */
  52. static inline bool is_kdump_kernel(void)
  53. {
  54. return elfcorehdr_addr != ELFCORE_ADDR_MAX;
  55. }
  56. /* is_vmcore_usable() checks if the kernel is booting after a panic and
  57. * the vmcore region is usable.
  58. *
  59. * This makes use of the fact that due to alignment -2ULL is not
  60. * a valid pointer, much in the vain of IS_ERR(), except
  61. * dealing directly with an unsigned long long rather than a pointer.
  62. */
  63. static inline int is_vmcore_usable(void)
  64. {
  65. return is_kdump_kernel() && elfcorehdr_addr != ELFCORE_ADDR_ERR ? 1 : 0;
  66. }
  67. /* vmcore_unusable() marks the vmcore as unusable,
  68. * without disturbing the logic of is_kdump_kernel()
  69. */
  70. static inline void vmcore_unusable(void)
  71. {
  72. if (is_kdump_kernel())
  73. elfcorehdr_addr = ELFCORE_ADDR_ERR;
  74. }
  75. /**
  76. * struct vmcore_cb - driver callbacks for /proc/vmcore handling
  77. * @pfn_is_ram: check whether a PFN really is RAM and should be accessed when
  78. * reading the vmcore. Will return "true" if it is RAM or if the
  79. * callback cannot tell. If any callback returns "false", it's not
  80. * RAM and the page must not be accessed; zeroes should be
  81. * indicated in the vmcore instead. For example, a ballooned page
  82. * contains no data and reading from such a page will cause high
  83. * load in the hypervisor.
  84. * @next: List head to manage registered callbacks internally; initialized by
  85. * register_vmcore_cb().
  86. *
  87. * vmcore callbacks allow drivers managing physical memory ranges to
  88. * coordinate with vmcore handling code, for example, to prevent accessing
  89. * physical memory ranges that should not be accessed when reading the vmcore,
  90. * although included in the vmcore header as memory ranges to dump.
  91. */
  92. struct vmcore_cb {
  93. bool (*pfn_is_ram)(struct vmcore_cb *cb, unsigned long pfn);
  94. struct list_head next;
  95. };
  96. extern void register_vmcore_cb(struct vmcore_cb *cb);
  97. extern void unregister_vmcore_cb(struct vmcore_cb *cb);
  98. #else /* !CONFIG_CRASH_DUMP */
  99. static inline bool is_kdump_kernel(void) { return false; }
  100. #endif /* CONFIG_CRASH_DUMP */
  101. /* Device Dump information to be filled by drivers */
  102. struct vmcoredd_data {
  103. char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */
  104. unsigned int size; /* Size of the dump */
  105. /* Driver's registered callback to be invoked to collect dump */
  106. int (*vmcoredd_callback)(struct vmcoredd_data *data, void *buf);
  107. };
  108. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  109. int vmcore_add_device_dump(struct vmcoredd_data *data);
  110. #else
  111. static inline int vmcore_add_device_dump(struct vmcoredd_data *data)
  112. {
  113. return -EOPNOTSUPP;
  114. }
  115. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  116. #ifdef CONFIG_PROC_VMCORE
  117. ssize_t read_from_oldmem(struct iov_iter *iter, size_t count,
  118. u64 *ppos, bool encrypted);
  119. #else
  120. static inline ssize_t read_from_oldmem(struct iov_iter *iter, size_t count,
  121. u64 *ppos, bool encrypted)
  122. {
  123. return -EOPNOTSUPP;
  124. }
  125. #endif /* CONFIG_PROC_VMCORE */
  126. #endif /* LINUX_CRASHDUMP_H */