| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | 
|---|
| 2 | #ifndef LINUX_CRASH_DUMP_H | 
|---|
| 3 | #define LINUX_CRASH_DUMP_H | 
|---|
| 4 |  | 
|---|
| 5 | #include <linux/kexec.h> | 
|---|
| 6 | #include <linux/proc_fs.h> | 
|---|
| 7 | #include <linux/elf.h> | 
|---|
| 8 | #include <linux/pgtable.h> | 
|---|
| 9 | #include <uapi/linux/vmcore.h> | 
|---|
| 10 |  | 
|---|
| 11 | /* For IS_ENABLED(CONFIG_CRASH_DUMP) */ | 
|---|
| 12 | #define ELFCORE_ADDR_MAX	(-1ULL) | 
|---|
| 13 | #define ELFCORE_ADDR_ERR	(-2ULL) | 
|---|
| 14 |  | 
|---|
| 15 | extern unsigned long long elfcorehdr_addr; | 
|---|
| 16 | extern unsigned long long elfcorehdr_size; | 
|---|
| 17 |  | 
|---|
| 18 | extern unsigned long long dm_crypt_keys_addr; | 
|---|
| 19 |  | 
|---|
| 20 | #ifdef CONFIG_CRASH_DUMP | 
|---|
| 21 | extern int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size); | 
|---|
| 22 | extern void elfcorehdr_free(unsigned long long addr); | 
|---|
| 23 | extern ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos); | 
|---|
| 24 | extern ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos); | 
|---|
| 25 | void elfcorehdr_fill_device_ram_ptload_elf64(Elf64_Phdr *phdr, | 
|---|
| 26 | unsigned long long paddr, unsigned long long size); | 
|---|
| 27 | extern int remap_oldmem_pfn_range(struct vm_area_struct *vma, | 
|---|
| 28 | unsigned long from, unsigned long pfn, | 
|---|
| 29 | unsigned long size, pgprot_t prot); | 
|---|
| 30 |  | 
|---|
| 31 | ssize_t copy_oldmem_page(struct iov_iter *i, unsigned long pfn, size_t csize, | 
|---|
| 32 | unsigned long offset); | 
|---|
| 33 | ssize_t copy_oldmem_page_encrypted(struct iov_iter *iter, unsigned long pfn, | 
|---|
| 34 | size_t csize, unsigned long offset); | 
|---|
| 35 |  | 
|---|
| 36 | void vmcore_cleanup(void); | 
|---|
| 37 |  | 
|---|
| 38 | /* Architecture code defines this if there are other possible ELF | 
|---|
| 39 | * machine types, e.g. on bi-arch capable hardware. */ | 
|---|
| 40 | #ifndef vmcore_elf_check_arch_cross | 
|---|
| 41 | #define vmcore_elf_check_arch_cross(x) 0 | 
|---|
| 42 | #endif | 
|---|
| 43 |  | 
|---|
| 44 | /* | 
|---|
| 45 | * Architecture code can redefine this if there are any special checks | 
|---|
| 46 | * needed for 32-bit ELF or 64-bit ELF vmcores.  In case of 32-bit | 
|---|
| 47 | * only architecture, vmcore_elf64_check_arch can be set to zero. | 
|---|
| 48 | */ | 
|---|
| 49 | #ifndef vmcore_elf32_check_arch | 
|---|
| 50 | #define vmcore_elf32_check_arch(x) elf_check_arch(x) | 
|---|
| 51 | #endif | 
|---|
| 52 |  | 
|---|
| 53 | #ifndef vmcore_elf64_check_arch | 
|---|
| 54 | #define vmcore_elf64_check_arch(x) (elf_check_arch(x) || vmcore_elf_check_arch_cross(x)) | 
|---|
| 55 | #endif | 
|---|
| 56 |  | 
|---|
| 57 | #ifndef is_kdump_kernel | 
|---|
| 58 | /* | 
|---|
| 59 | * is_kdump_kernel() checks whether this kernel is booting after a panic of | 
|---|
| 60 | * previous kernel or not. This is determined by checking if previous kernel | 
|---|
| 61 | * has passed the elf core header address on command line. | 
|---|
| 62 | * | 
|---|
| 63 | * This is not just a test if CONFIG_CRASH_DUMP is enabled or not. It will | 
|---|
| 64 | * return true if CONFIG_CRASH_DUMP=y and if kernel is booting after a panic | 
|---|
| 65 | * of previous kernel. | 
|---|
| 66 | */ | 
|---|
| 67 |  | 
|---|
| 68 | static inline bool is_kdump_kernel(void) | 
|---|
| 69 | { | 
|---|
| 70 | return elfcorehdr_addr != ELFCORE_ADDR_MAX; | 
|---|
| 71 | } | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 | /* is_vmcore_usable() checks if the kernel is booting after a panic and | 
|---|
| 75 | * the vmcore region is usable. | 
|---|
| 76 | * | 
|---|
| 77 | * This makes use of the fact that due to alignment -2ULL is not | 
|---|
| 78 | * a valid pointer, much in the vain of IS_ERR(), except | 
|---|
| 79 | * dealing directly with an unsigned long long rather than a pointer. | 
|---|
| 80 | */ | 
|---|
| 81 |  | 
|---|
| 82 | static inline int is_vmcore_usable(void) | 
|---|
| 83 | { | 
|---|
| 84 | return elfcorehdr_addr != ELFCORE_ADDR_ERR && | 
|---|
| 85 | elfcorehdr_addr != ELFCORE_ADDR_MAX ? 1 : 0; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | /* vmcore_unusable() marks the vmcore as unusable, | 
|---|
| 89 | * without disturbing the logic of is_kdump_kernel() | 
|---|
| 90 | */ | 
|---|
| 91 |  | 
|---|
| 92 | static inline void vmcore_unusable(void) | 
|---|
| 93 | { | 
|---|
| 94 | elfcorehdr_addr = ELFCORE_ADDR_ERR; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | /** | 
|---|
| 98 | * struct vmcore_cb - driver callbacks for /proc/vmcore handling | 
|---|
| 99 | * @pfn_is_ram: check whether a PFN really is RAM and should be accessed when | 
|---|
| 100 | *              reading the vmcore. Will return "true" if it is RAM or if the | 
|---|
| 101 | *              callback cannot tell. If any callback returns "false", it's not | 
|---|
| 102 | *              RAM and the page must not be accessed; zeroes should be | 
|---|
| 103 | *              indicated in the vmcore instead. For example, a ballooned page | 
|---|
| 104 | *              contains no data and reading from such a page will cause high | 
|---|
| 105 | *              load in the hypervisor. | 
|---|
| 106 | * @get_device_ram: query RAM ranges that can only be detected by device | 
|---|
| 107 | *   drivers, such as the virtio-mem driver, so they can be included in | 
|---|
| 108 | *   the crash dump on architectures that allocate the elfcore hdr in the dump | 
|---|
| 109 | *   ("2nd") kernel. Indicated RAM ranges may contain holes to reduce the | 
|---|
| 110 | *   total number of ranges; such holes can be detected using the pfn_is_ram | 
|---|
| 111 | *   callback just like for other RAM. | 
|---|
| 112 | * @next: List head to manage registered callbacks internally; initialized by | 
|---|
| 113 | *        register_vmcore_cb(). | 
|---|
| 114 | * | 
|---|
| 115 | * vmcore callbacks allow drivers managing physical memory ranges to | 
|---|
| 116 | * coordinate with vmcore handling code, for example, to prevent accessing | 
|---|
| 117 | * physical memory ranges that should not be accessed when reading the vmcore, | 
|---|
| 118 | * although included in the vmcore header as memory ranges to dump. | 
|---|
| 119 | */ | 
|---|
| 120 | struct vmcore_cb { | 
|---|
| 121 | bool (*pfn_is_ram)(struct vmcore_cb *cb, unsigned long pfn); | 
|---|
| 122 | int (*get_device_ram)(struct vmcore_cb *cb, struct list_head *list); | 
|---|
| 123 | struct list_head next; | 
|---|
| 124 | }; | 
|---|
| 125 | extern void register_vmcore_cb(struct vmcore_cb *cb); | 
|---|
| 126 | extern void unregister_vmcore_cb(struct vmcore_cb *cb); | 
|---|
| 127 |  | 
|---|
| 128 | struct vmcore_range { | 
|---|
| 129 | struct list_head list; | 
|---|
| 130 | unsigned long long paddr; | 
|---|
| 131 | unsigned long long size; | 
|---|
| 132 | loff_t offset; | 
|---|
| 133 | }; | 
|---|
| 134 |  | 
|---|
| 135 | /* Allocate a vmcore range and add it to the list. */ | 
|---|
| 136 | static inline int vmcore_alloc_add_range(struct list_head *list, | 
|---|
| 137 | unsigned long long paddr, unsigned long long size) | 
|---|
| 138 | { | 
|---|
| 139 | struct vmcore_range *m = kzalloc(sizeof(*m), GFP_KERNEL); | 
|---|
| 140 |  | 
|---|
| 141 | if (!m) | 
|---|
| 142 | return -ENOMEM; | 
|---|
| 143 | m->paddr = paddr; | 
|---|
| 144 | m->size = size; | 
|---|
| 145 | list_add_tail(new: &m->list, head: list); | 
|---|
| 146 | return 0; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /* Free a list of vmcore ranges. */ | 
|---|
| 150 | static inline void vmcore_free_ranges(struct list_head *list) | 
|---|
| 151 | { | 
|---|
| 152 | struct vmcore_range *m, *tmp; | 
|---|
| 153 |  | 
|---|
| 154 | list_for_each_entry_safe(m, tmp, list, list) { | 
|---|
| 155 | list_del(entry: &m->list); | 
|---|
| 156 | kfree(objp: m); | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | #else /* !CONFIG_CRASH_DUMP */ | 
|---|
| 161 | static inline bool is_kdump_kernel(void) { return false; } | 
|---|
| 162 | #endif /* CONFIG_CRASH_DUMP */ | 
|---|
| 163 |  | 
|---|
| 164 | /* Device Dump information to be filled by drivers */ | 
|---|
| 165 | struct vmcoredd_data { | 
|---|
| 166 | char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */ | 
|---|
| 167 | unsigned int size;                       /* Size of the dump */ | 
|---|
| 168 | /* Driver's registered callback to be invoked to collect dump */ | 
|---|
| 169 | int (*vmcoredd_callback)(struct vmcoredd_data *data, void *buf); | 
|---|
| 170 | }; | 
|---|
| 171 |  | 
|---|
| 172 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP | 
|---|
| 173 | int vmcore_add_device_dump(struct vmcoredd_data *data); | 
|---|
| 174 | #else | 
|---|
| 175 | static inline int vmcore_add_device_dump(struct vmcoredd_data *data) | 
|---|
| 176 | { | 
|---|
| 177 | return -EOPNOTSUPP; | 
|---|
| 178 | } | 
|---|
| 179 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ | 
|---|
| 180 |  | 
|---|
| 181 | #ifdef CONFIG_PROC_VMCORE | 
|---|
| 182 | ssize_t read_from_oldmem(struct iov_iter *iter, size_t count, | 
|---|
| 183 | u64 *ppos, bool encrypted); | 
|---|
| 184 | #else | 
|---|
| 185 | static inline ssize_t read_from_oldmem(struct iov_iter *iter, size_t count, | 
|---|
| 186 | u64 *ppos, bool encrypted) | 
|---|
| 187 | { | 
|---|
| 188 | return -EOPNOTSUPP; | 
|---|
| 189 | } | 
|---|
| 190 | #endif /* CONFIG_PROC_VMCORE */ | 
|---|
| 191 |  | 
|---|
| 192 | #endif /* LINUX_CRASHDUMP_H */ | 
|---|
| 193 |  | 
|---|