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