1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
260e64d46SVivek Goyal #ifndef LINUX_CRASH_DUMP_H
360e64d46SVivek Goyal #define LINUX_CRASH_DUMP_H
460e64d46SVivek Goyal
560e64d46SVivek Goyal #include <linux/kexec.h>
660e64d46SVivek Goyal #include <linux/proc_fs.h>
71f536b9eSFabio Estevam #include <linux/elf.h>
865fddcfcSMike Rapoport #include <linux/pgtable.h>
92724273eSRahul Lakkireddy #include <uapi/linux/vmcore.h>
1060e64d46SVivek Goyal
1133709413SGeert Uytterhoeven /* For IS_ENABLED(CONFIG_CRASH_DUMP) */
12666bfddbSVivek Goyal #define ELFCORE_ADDR_MAX (-1ULL)
1385a0ee34SSimon Horman #define ELFCORE_ADDR_ERR (-2ULL)
1436ac2617SIngo Molnar
152030eae5SVivek Goyal extern unsigned long long elfcorehdr_addr;
16d3bf3795SMichael Holzheu extern unsigned long long elfcorehdr_size;
1736ac2617SIngo Molnar
1833709413SGeert Uytterhoeven #ifdef CONFIG_CRASH_DUMP
195ab03ac5SBjorn Helgaas extern int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size);
205ab03ac5SBjorn Helgaas extern void elfcorehdr_free(unsigned long long addr);
215ab03ac5SBjorn Helgaas extern ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos);
225ab03ac5SBjorn Helgaas extern ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos);
235ab03ac5SBjorn Helgaas extern int remap_oldmem_pfn_range(struct vm_area_struct *vma,
249cb21813SMichael Holzheu unsigned long from, unsigned long pfn,
259cb21813SMichael Holzheu unsigned long size, pgprot_t prot);
26be8a8d06SMichael Holzheu
275d8de293SMatthew Wilcox (Oracle) ssize_t copy_oldmem_page(struct iov_iter *i, unsigned long pfn, size_t csize,
285d8de293SMatthew Wilcox (Oracle) unsigned long offset);
295d8de293SMatthew Wilcox (Oracle) ssize_t copy_oldmem_page_encrypted(struct iov_iter *iter, unsigned long pfn,
305d8de293SMatthew Wilcox (Oracle) size_t csize, unsigned long offset);
31992b649aSLianbo Jiang
3282e0703bSRashika Kheria void vmcore_cleanup(void);
33666bfddbSVivek Goyal
3479e03011SIan Campbell /* Architecture code defines this if there are other possible ELF
3579e03011SIan Campbell * machine types, e.g. on bi-arch capable hardware. */
3679e03011SIan Campbell #ifndef vmcore_elf_check_arch_cross
3779e03011SIan Campbell #define vmcore_elf_check_arch_cross(x) 0
3879e03011SIan Campbell #endif
3979e03011SIan Campbell
409833c394SMika Westerberg /*
419833c394SMika Westerberg * Architecture code can redefine this if there are any special checks
42e55d5312SDaniel Wagner * needed for 32-bit ELF or 64-bit ELF vmcores. In case of 32-bit
43e55d5312SDaniel Wagner * only architecture, vmcore_elf64_check_arch can be set to zero.
449833c394SMika Westerberg */
45e55d5312SDaniel Wagner #ifndef vmcore_elf32_check_arch
46e55d5312SDaniel Wagner #define vmcore_elf32_check_arch(x) elf_check_arch(x)
47e55d5312SDaniel Wagner #endif
48e55d5312SDaniel Wagner
499833c394SMika Westerberg #ifndef vmcore_elf64_check_arch
509833c394SMika Westerberg #define vmcore_elf64_check_arch(x) (elf_check_arch(x) || vmcore_elf_check_arch_cross(x))
519833c394SMika Westerberg #endif
5279e03011SIan Campbell
5357cac4d1SVivek Goyal /*
5457cac4d1SVivek Goyal * is_kdump_kernel() checks whether this kernel is booting after a panic of
5557cac4d1SVivek Goyal * previous kernel or not. This is determined by checking if previous kernel
5657cac4d1SVivek Goyal * has passed the elf core header address on command line.
5757cac4d1SVivek Goyal *
5857cac4d1SVivek Goyal * This is not just a test if CONFIG_CRASH_DUMP is enabled or not. It will
592650cb0cSYaowei Bai * return true if CONFIG_CRASH_DUMP=y and if kernel is booting after a panic
602650cb0cSYaowei Bai * of previous kernel.
6157cac4d1SVivek Goyal */
6257cac4d1SVivek Goyal
is_kdump_kernel(void)632650cb0cSYaowei Bai static inline bool is_kdump_kernel(void)
6495b68decSChandru {
652650cb0cSYaowei Bai return elfcorehdr_addr != ELFCORE_ADDR_MAX;
6695b68decSChandru }
6785a0ee34SSimon Horman
6885a0ee34SSimon Horman /* is_vmcore_usable() checks if the kernel is booting after a panic and
6985a0ee34SSimon Horman * the vmcore region is usable.
7085a0ee34SSimon Horman *
7185a0ee34SSimon Horman * This makes use of the fact that due to alignment -2ULL is not
7285a0ee34SSimon Horman * a valid pointer, much in the vain of IS_ERR(), except
7385a0ee34SSimon Horman * dealing directly with an unsigned long long rather than a pointer.
7485a0ee34SSimon Horman */
7585a0ee34SSimon Horman
is_vmcore_usable(void)7685a0ee34SSimon Horman static inline int is_vmcore_usable(void)
7785a0ee34SSimon Horman {
7885a0ee34SSimon Horman return is_kdump_kernel() && elfcorehdr_addr != ELFCORE_ADDR_ERR ? 1 : 0;
7985a0ee34SSimon Horman }
8085a0ee34SSimon Horman
8185a0ee34SSimon Horman /* vmcore_unusable() marks the vmcore as unusable,
8285a0ee34SSimon Horman * without disturbing the logic of is_kdump_kernel()
8385a0ee34SSimon Horman */
8485a0ee34SSimon Horman
vmcore_unusable(void)8585a0ee34SSimon Horman static inline void vmcore_unusable(void)
8685a0ee34SSimon Horman {
8785a0ee34SSimon Horman if (is_kdump_kernel())
8885a0ee34SSimon Horman elfcorehdr_addr = ELFCORE_ADDR_ERR;
8985a0ee34SSimon Horman }
90997c136fSOlaf Hering
91cc5f2704SDavid Hildenbrand /**
92cc5f2704SDavid Hildenbrand * struct vmcore_cb - driver callbacks for /proc/vmcore handling
93cc5f2704SDavid Hildenbrand * @pfn_is_ram: check whether a PFN really is RAM and should be accessed when
94cc5f2704SDavid Hildenbrand * reading the vmcore. Will return "true" if it is RAM or if the
95cc5f2704SDavid Hildenbrand * callback cannot tell. If any callback returns "false", it's not
96cc5f2704SDavid Hildenbrand * RAM and the page must not be accessed; zeroes should be
97cc5f2704SDavid Hildenbrand * indicated in the vmcore instead. For example, a ballooned page
98cc5f2704SDavid Hildenbrand * contains no data and reading from such a page will cause high
99cc5f2704SDavid Hildenbrand * load in the hypervisor.
100cc5f2704SDavid Hildenbrand * @next: List head to manage registered callbacks internally; initialized by
101cc5f2704SDavid Hildenbrand * register_vmcore_cb().
102cc5f2704SDavid Hildenbrand *
103cc5f2704SDavid Hildenbrand * vmcore callbacks allow drivers managing physical memory ranges to
104cc5f2704SDavid Hildenbrand * coordinate with vmcore handling code, for example, to prevent accessing
105cc5f2704SDavid Hildenbrand * physical memory ranges that should not be accessed when reading the vmcore,
106cc5f2704SDavid Hildenbrand * although included in the vmcore header as memory ranges to dump.
107cc5f2704SDavid Hildenbrand */
108cc5f2704SDavid Hildenbrand struct vmcore_cb {
109cc5f2704SDavid Hildenbrand bool (*pfn_is_ram)(struct vmcore_cb *cb, unsigned long pfn);
110cc5f2704SDavid Hildenbrand struct list_head next;
111cc5f2704SDavid Hildenbrand };
112cc5f2704SDavid Hildenbrand extern void register_vmcore_cb(struct vmcore_cb *cb);
113cc5f2704SDavid Hildenbrand extern void unregister_vmcore_cb(struct vmcore_cb *cb);
114997c136fSOlaf Hering
11595b68decSChandru #else /* !CONFIG_CRASH_DUMP */
is_kdump_kernel(void)1165605f419SChangcheng Deng static inline bool is_kdump_kernel(void) { return false; }
11760e64d46SVivek Goyal #endif /* CONFIG_CRASH_DUMP */
11895b68decSChandru
1192724273eSRahul Lakkireddy /* Device Dump information to be filled by drivers */
1202724273eSRahul Lakkireddy struct vmcoredd_data {
1212724273eSRahul Lakkireddy char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */
1222724273eSRahul Lakkireddy unsigned int size; /* Size of the dump */
1232724273eSRahul Lakkireddy /* Driver's registered callback to be invoked to collect dump */
1242724273eSRahul Lakkireddy int (*vmcoredd_callback)(struct vmcoredd_data *data, void *buf);
1252724273eSRahul Lakkireddy };
1262724273eSRahul Lakkireddy
1272724273eSRahul Lakkireddy #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1282724273eSRahul Lakkireddy int vmcore_add_device_dump(struct vmcoredd_data *data);
1292724273eSRahul Lakkireddy #else
vmcore_add_device_dump(struct vmcoredd_data * data)1302724273eSRahul Lakkireddy static inline int vmcore_add_device_dump(struct vmcoredd_data *data)
1312724273eSRahul Lakkireddy {
1322724273eSRahul Lakkireddy return -EOPNOTSUPP;
1332724273eSRahul Lakkireddy }
1342724273eSRahul Lakkireddy #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
135ae7eb82aSThiago Jung Bauermann
136ae7eb82aSThiago Jung Bauermann #ifdef CONFIG_PROC_VMCORE
137*e0690479SMatthew Wilcox (Oracle) ssize_t read_from_oldmem(struct iov_iter *iter, size_t count,
138*e0690479SMatthew Wilcox (Oracle) u64 *ppos, bool encrypted);
139ae7eb82aSThiago Jung Bauermann #else
read_from_oldmem(struct iov_iter * iter,size_t count,u64 * ppos,bool encrypted)140*e0690479SMatthew Wilcox (Oracle) static inline ssize_t read_from_oldmem(struct iov_iter *iter, size_t count,
141*e0690479SMatthew Wilcox (Oracle) u64 *ppos, bool encrypted)
142ae7eb82aSThiago Jung Bauermann {
143ae7eb82aSThiago Jung Bauermann return -EOPNOTSUPP;
144ae7eb82aSThiago Jung Bauermann }
145ae7eb82aSThiago Jung Bauermann #endif /* CONFIG_PROC_VMCORE */
146ae7eb82aSThiago Jung Bauermann
14760e64d46SVivek Goyal #endif /* LINUX_CRASHDUMP_H */
148