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