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