1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_KEXEC_H
3#define _ASM_X86_KEXEC_H
4
5#ifdef CONFIG_X86_32
6# define PA_CONTROL_PAGE 0
7# define VA_CONTROL_PAGE 1
8# define PA_PGD 2
9# define PA_SWAP_PAGE 3
10# define PAGES_NR 4
11#else
12/* Size of each exception handler referenced by the IDT */
13# define KEXEC_DEBUG_EXC_HANDLER_SIZE 6 /* PUSHI, PUSHI, 2-byte JMP */
14#endif
15
16#ifdef CONFIG_X86_64
17
18#include <linux/bits.h>
19
20#define RELOC_KERNEL_PRESERVE_CONTEXT BIT(0)
21#define RELOC_KERNEL_CACHE_INCOHERENT BIT(1)
22
23#endif
24
25# define KEXEC_CONTROL_PAGE_SIZE 4096
26# define KEXEC_CONTROL_CODE_MAX_SIZE 2048
27
28#ifndef __ASSEMBLER__
29
30#include <linux/string.h>
31#include <linux/kernel.h>
32
33#include <asm/asm.h>
34#include <asm/page.h>
35#include <asm/ptrace.h>
36
37struct kimage;
38
39/*
40 * KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
41 * I.e. Maximum page that is mapped directly into kernel memory,
42 * and kmap is not required.
43 *
44 * So far x86_64 is limited to 40 physical address bits.
45 */
46#ifdef CONFIG_X86_32
47/* Maximum physical address we can use pages from */
48# define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
49/* Maximum address we can reach in physical address mode */
50# define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
51/* Maximum address we can use for the control code buffer */
52# define KEXEC_CONTROL_MEMORY_LIMIT TASK_SIZE
53
54
55/* The native architecture */
56# define KEXEC_ARCH KEXEC_ARCH_386
57
58/* We can also handle crash dumps from 64 bit kernel. */
59# define vmcore_elf_check_arch_cross(x) ((x)->e_machine == EM_X86_64)
60#else
61/* Maximum physical address we can use pages from */
62# define KEXEC_SOURCE_MEMORY_LIMIT (MAXMEM-1)
63/* Maximum address we can reach in physical address mode */
64# define KEXEC_DESTINATION_MEMORY_LIMIT (MAXMEM-1)
65/* Maximum address we can use for the control pages */
66# define KEXEC_CONTROL_MEMORY_LIMIT (MAXMEM-1)
67
68/* The native architecture */
69# define KEXEC_ARCH KEXEC_ARCH_X86_64
70
71extern unsigned long kexec_va_control_page;
72extern unsigned long kexec_pa_table_page;
73extern unsigned long kexec_pa_swap_page;
74extern gate_desc kexec_debug_idt[];
75extern unsigned char kexec_debug_exc_vectors[];
76extern uint16_t kexec_debug_8250_port;
77extern unsigned long kexec_debug_8250_mmio32;
78#endif
79
80/*
81 * This function is responsible for capturing register states if coming
82 * via panic otherwise just fix up the ss and sp if coming via kernel
83 * mode exception.
84 */
85static inline void crash_setup_regs(struct pt_regs *newregs,
86 struct pt_regs *oldregs)
87{
88 if (oldregs) {
89 memcpy(to: newregs, from: oldregs, len: sizeof(*newregs));
90 } else {
91 asm volatile("mov %%" _ASM_BX ",%0" : "=m"(newregs->bx));
92 asm volatile("mov %%" _ASM_CX ",%0" : "=m"(newregs->cx));
93 asm volatile("mov %%" _ASM_DX ",%0" : "=m"(newregs->dx));
94 asm volatile("mov %%" _ASM_SI ",%0" : "=m"(newregs->si));
95 asm volatile("mov %%" _ASM_DI ",%0" : "=m"(newregs->di));
96 asm volatile("mov %%" _ASM_BP ",%0" : "=m"(newregs->bp));
97 asm volatile("mov %%" _ASM_AX ",%0" : "=m"(newregs->ax));
98 asm volatile("mov %%" _ASM_SP ",%0" : "=m"(newregs->sp));
99#ifdef CONFIG_X86_64
100 asm volatile("mov %%r8,%0" : "=m"(newregs->r8));
101 asm volatile("mov %%r9,%0" : "=m"(newregs->r9));
102 asm volatile("mov %%r10,%0" : "=m"(newregs->r10));
103 asm volatile("mov %%r11,%0" : "=m"(newregs->r11));
104 asm volatile("mov %%r12,%0" : "=m"(newregs->r12));
105 asm volatile("mov %%r13,%0" : "=m"(newregs->r13));
106 asm volatile("mov %%r14,%0" : "=m"(newregs->r14));
107 asm volatile("mov %%r15,%0" : "=m"(newregs->r15));
108#endif
109 asm volatile("mov %%ss,%k0" : "=a"(newregs->ss));
110 asm volatile("mov %%cs,%k0" : "=a"(newregs->cs));
111#ifdef CONFIG_X86_32
112 asm volatile("mov %%ds,%k0" : "=a"(newregs->ds));
113 asm volatile("mov %%es,%k0" : "=a"(newregs->es));
114#endif
115 asm volatile("pushf\n\t"
116 "pop %0" : "=m"(newregs->flags));
117 newregs->ip = _THIS_IP_;
118 }
119}
120
121#ifdef CONFIG_X86_32
122typedef asmlinkage unsigned long
123relocate_kernel_fn(unsigned long indirection_page,
124 unsigned long control_page,
125 unsigned long start_address,
126 unsigned int has_pae,
127 unsigned int preserve_context);
128#else
129typedef unsigned long
130relocate_kernel_fn(unsigned long indirection_page,
131 unsigned long pa_control_page,
132 unsigned long start_address,
133 unsigned int flags);
134#endif
135extern relocate_kernel_fn relocate_kernel;
136#define ARCH_HAS_KIMAGE_ARCH
137
138#ifdef CONFIG_X86_32
139struct kimage_arch {
140 pgd_t *pgd;
141#ifdef CONFIG_X86_PAE
142 pmd_t *pmd0;
143 pmd_t *pmd1;
144#endif
145 pte_t *pte0;
146 pte_t *pte1;
147};
148#else
149struct kimage_arch {
150 /*
151 * This is a kimage control page, as it must not overlap with either
152 * source or destination address ranges.
153 */
154 pgd_t *pgd;
155 /*
156 * The virtual mapping of the control code page itself is used only
157 * during the transition, while the current kernel's pages are all
158 * in place. Thus the intermediate page table pages used to map it
159 * are not control pages, but instead just normal pages obtained
160 * with get_zeroed_page(). And have to be tracked (below) so that
161 * they can be freed.
162 */
163 p4d_t *p4d;
164 pud_t *pud;
165 pmd_t *pmd;
166 pte_t *pte;
167};
168#endif /* CONFIG_X86_32 */
169
170#ifdef CONFIG_X86_64
171/*
172 * Number of elements and order of elements in this structure should match
173 * with the ones in arch/x86/purgatory/entry64.S. If you make a change here
174 * make an appropriate change in purgatory too.
175 */
176struct kexec_entry64_regs {
177 uint64_t rax;
178 uint64_t rcx;
179 uint64_t rdx;
180 uint64_t rbx;
181 uint64_t rsp;
182 uint64_t rbp;
183 uint64_t rsi;
184 uint64_t rdi;
185 uint64_t r8;
186 uint64_t r9;
187 uint64_t r10;
188 uint64_t r11;
189 uint64_t r12;
190 uint64_t r13;
191 uint64_t r14;
192 uint64_t r15;
193 uint64_t rip;
194};
195
196extern int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages,
197 gfp_t gfp);
198#define arch_kexec_post_alloc_pages arch_kexec_post_alloc_pages
199
200extern void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages);
201#define arch_kexec_pre_free_pages arch_kexec_pre_free_pages
202
203void arch_kexec_protect_crashkres(void);
204#define arch_kexec_protect_crashkres arch_kexec_protect_crashkres
205
206void arch_kexec_unprotect_crashkres(void);
207#define arch_kexec_unprotect_crashkres arch_kexec_unprotect_crashkres
208
209#ifdef CONFIG_KEXEC_FILE
210struct purgatory_info;
211int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
212 Elf_Shdr *section,
213 const Elf_Shdr *relsec,
214 const Elf_Shdr *symtab);
215#define arch_kexec_apply_relocations_add arch_kexec_apply_relocations_add
216
217int arch_kimage_file_post_load_cleanup(struct kimage *image);
218#define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
219#endif
220#endif
221
222extern void kdump_nmi_shootdown_cpus(void);
223
224#ifdef CONFIG_CRASH_HOTPLUG
225void arch_crash_handle_hotplug_event(struct kimage *image, void *arg);
226#define arch_crash_handle_hotplug_event arch_crash_handle_hotplug_event
227
228int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_flags);
229#define arch_crash_hotplug_support arch_crash_hotplug_support
230
231unsigned int arch_crash_get_elfcorehdr_size(void);
232#define crash_get_elfcorehdr_size arch_crash_get_elfcorehdr_size
233#endif
234
235#endif /* __ASSEMBLER__ */
236
237#endif /* _ASM_X86_KEXEC_H */
238