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