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