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