1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_KEXEC_H
31965aae3SH. Peter Anvin #define _ASM_X86_KEXEC_H
4bb898558SAl Viro
5bb898558SAl Viro #ifdef CONFIG_X86_32
6bb898558SAl Viro # define PA_CONTROL_PAGE 0
7bb898558SAl Viro # define VA_CONTROL_PAGE 1
8bb898558SAl Viro # define PA_PGD 2
99868ee63SHuang Ying # define PA_SWAP_PAGE 3
109868ee63SHuang Ying # define PAGES_NR 4
11bb898558SAl Viro #else
12bb898558SAl Viro # define PA_CONTROL_PAGE 0
13fee7b0d8SHuang Ying # define VA_CONTROL_PAGE 1
14fee7b0d8SHuang Ying # define PA_TABLE_PAGE 2
15fee7b0d8SHuang Ying # define PA_SWAP_PAGE 3
16fee7b0d8SHuang Ying # define PAGES_NR 4
17bb898558SAl Viro #endif
18bb898558SAl Viro
19*e2612a54SDavid Woodhouse # define KEXEC_CONTROL_PAGE_SIZE 4096
20bb898558SAl Viro # define KEXEC_CONTROL_CODE_MAX_SIZE 2048
21bb898558SAl Viro
22bb898558SAl Viro #ifndef __ASSEMBLY__
23bb898558SAl Viro
24bb898558SAl Viro #include <linux/string.h>
25de0d22e5SNick Desaulniers #include <linux/kernel.h>
26bb898558SAl Viro
27bb898558SAl Viro #include <asm/page.h>
28bb898558SAl Viro #include <asm/ptrace.h>
2927f48d3eSVivek Goyal #include <asm/bootparam.h>
30bb898558SAl Viro
31dd5f7260SVivek Goyal struct kimage;
32dd5f7260SVivek Goyal
33bb898558SAl Viro /*
34bb898558SAl Viro * KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
35bb898558SAl Viro * I.e. Maximum page that is mapped directly into kernel memory,
36bb898558SAl Viro * and kmap is not required.
37bb898558SAl Viro *
38bb898558SAl Viro * So far x86_64 is limited to 40 physical address bits.
39bb898558SAl Viro */
40bb898558SAl Viro #ifdef CONFIG_X86_32
41bb898558SAl Viro /* Maximum physical address we can use pages from */
42bb898558SAl Viro # define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
43bb898558SAl Viro /* Maximum address we can reach in physical address mode */
44bb898558SAl Viro # define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
45bb898558SAl Viro /* Maximum address we can use for the control code buffer */
46bb898558SAl Viro # define KEXEC_CONTROL_MEMORY_LIMIT TASK_SIZE
47bb898558SAl Viro
48bb898558SAl Viro
49bb898558SAl Viro /* The native architecture */
50bb898558SAl Viro # define KEXEC_ARCH KEXEC_ARCH_386
51bb898558SAl Viro
52bb898558SAl Viro /* We can also handle crash dumps from 64 bit kernel. */
53bb898558SAl Viro # define vmcore_elf_check_arch_cross(x) ((x)->e_machine == EM_X86_64)
54bb898558SAl Viro #else
55bb898558SAl Viro /* Maximum physical address we can use pages from */
56577af55dSYinghai Lu # define KEXEC_SOURCE_MEMORY_LIMIT (MAXMEM-1)
57bb898558SAl Viro /* Maximum address we can reach in physical address mode */
58577af55dSYinghai Lu # define KEXEC_DESTINATION_MEMORY_LIMIT (MAXMEM-1)
59bb898558SAl Viro /* Maximum address we can use for the control pages */
60577af55dSYinghai Lu # define KEXEC_CONTROL_MEMORY_LIMIT (MAXMEM-1)
61bb898558SAl Viro
62bb898558SAl Viro /* The native architecture */
63bb898558SAl Viro # define KEXEC_ARCH KEXEC_ARCH_X86_64
64bb898558SAl Viro #endif
65bb898558SAl Viro
66bb898558SAl Viro /*
67bb898558SAl Viro * This function is responsible for capturing register states if coming
68bb898558SAl Viro * via panic otherwise just fix up the ss and sp if coming via kernel
69bb898558SAl Viro * mode exception.
70bb898558SAl Viro */
crash_setup_regs(struct pt_regs * newregs,struct pt_regs * oldregs)71bb898558SAl Viro static inline void crash_setup_regs(struct pt_regs *newregs,
72bb898558SAl Viro struct pt_regs *oldregs)
73bb898558SAl Viro {
74bb898558SAl Viro if (oldregs) {
75bb898558SAl Viro memcpy(newregs, oldregs, sizeof(*newregs));
76bb898558SAl Viro } else {
77bb898558SAl Viro #ifdef CONFIG_X86_32
78bb898558SAl Viro asm volatile("movl %%ebx,%0" : "=m"(newregs->bx));
79bb898558SAl Viro asm volatile("movl %%ecx,%0" : "=m"(newregs->cx));
80bb898558SAl Viro asm volatile("movl %%edx,%0" : "=m"(newregs->dx));
81bb898558SAl Viro asm volatile("movl %%esi,%0" : "=m"(newregs->si));
82bb898558SAl Viro asm volatile("movl %%edi,%0" : "=m"(newregs->di));
83bb898558SAl Viro asm volatile("movl %%ebp,%0" : "=m"(newregs->bp));
84bb898558SAl Viro asm volatile("movl %%eax,%0" : "=m"(newregs->ax));
85bb898558SAl Viro asm volatile("movl %%esp,%0" : "=m"(newregs->sp));
86bb898558SAl Viro asm volatile("movl %%ss, %%eax;" :"=a"(newregs->ss));
87bb898558SAl Viro asm volatile("movl %%cs, %%eax;" :"=a"(newregs->cs));
88bb898558SAl Viro asm volatile("movl %%ds, %%eax;" :"=a"(newregs->ds));
89bb898558SAl Viro asm volatile("movl %%es, %%eax;" :"=a"(newregs->es));
90bb898558SAl Viro asm volatile("pushfl; popl %0" :"=m"(newregs->flags));
91bb898558SAl Viro #else
92bb898558SAl Viro asm volatile("movq %%rbx,%0" : "=m"(newregs->bx));
93bb898558SAl Viro asm volatile("movq %%rcx,%0" : "=m"(newregs->cx));
94bb898558SAl Viro asm volatile("movq %%rdx,%0" : "=m"(newregs->dx));
95bb898558SAl Viro asm volatile("movq %%rsi,%0" : "=m"(newregs->si));
96bb898558SAl Viro asm volatile("movq %%rdi,%0" : "=m"(newregs->di));
97bb898558SAl Viro asm volatile("movq %%rbp,%0" : "=m"(newregs->bp));
98bb898558SAl Viro asm volatile("movq %%rax,%0" : "=m"(newregs->ax));
99bb898558SAl Viro asm volatile("movq %%rsp,%0" : "=m"(newregs->sp));
100bb898558SAl Viro asm volatile("movq %%r8,%0" : "=m"(newregs->r8));
101bb898558SAl Viro asm volatile("movq %%r9,%0" : "=m"(newregs->r9));
102bb898558SAl Viro asm volatile("movq %%r10,%0" : "=m"(newregs->r10));
103bb898558SAl Viro asm volatile("movq %%r11,%0" : "=m"(newregs->r11));
104bb898558SAl Viro asm volatile("movq %%r12,%0" : "=m"(newregs->r12));
105bb898558SAl Viro asm volatile("movq %%r13,%0" : "=m"(newregs->r13));
106bb898558SAl Viro asm volatile("movq %%r14,%0" : "=m"(newregs->r14));
107bb898558SAl Viro asm volatile("movq %%r15,%0" : "=m"(newregs->r15));
108bb898558SAl Viro asm volatile("movl %%ss, %%eax;" :"=a"(newregs->ss));
109bb898558SAl Viro asm volatile("movl %%cs, %%eax;" :"=a"(newregs->cs));
110bb898558SAl Viro asm volatile("pushfq; popq %0" :"=m"(newregs->flags));
111bb898558SAl Viro #endif
112de0d22e5SNick Desaulniers newregs->ip = _THIS_IP_;
113bb898558SAl Viro }
114bb898558SAl Viro }
115bb898558SAl Viro
116bb898558SAl Viro #ifdef CONFIG_X86_32
117bb898558SAl Viro asmlinkage unsigned long
118bb898558SAl Viro relocate_kernel(unsigned long indirection_page,
119bb898558SAl Viro unsigned long control_page,
120bb898558SAl Viro unsigned long start_address,
121bb898558SAl Viro unsigned int has_pae,
122bb898558SAl Viro unsigned int preserve_context);
123bb898558SAl Viro #else
124fee7b0d8SHuang Ying unsigned long
125bb898558SAl Viro relocate_kernel(unsigned long indirection_page,
126bb898558SAl Viro unsigned long page_list,
127fee7b0d8SHuang Ying unsigned long start_address,
1284e237903STom Lendacky unsigned int preserve_context,
12932cb4d02STom Lendacky unsigned int host_mem_enc_active);
130bb898558SAl Viro #endif
131bb898558SAl Viro
13292be3d6bSHuang Ying #define ARCH_HAS_KIMAGE_ARCH
13392be3d6bSHuang Ying
134f5deb796SHuang Ying #ifdef CONFIG_X86_32
13592be3d6bSHuang Ying struct kimage_arch {
13692be3d6bSHuang Ying pgd_t *pgd;
13792be3d6bSHuang Ying #ifdef CONFIG_X86_PAE
13892be3d6bSHuang Ying pmd_t *pmd0;
13992be3d6bSHuang Ying pmd_t *pmd1;
14092be3d6bSHuang Ying #endif
14192be3d6bSHuang Ying pte_t *pte0;
14292be3d6bSHuang Ying pte_t *pte1;
14392be3d6bSHuang Ying };
144f5deb796SHuang Ying #else
145f5deb796SHuang Ying struct kimage_arch {
146*e2612a54SDavid Woodhouse /*
147*e2612a54SDavid Woodhouse * This is a kimage control page, as it must not overlap with either
148*e2612a54SDavid Woodhouse * source or destination address ranges.
149*e2612a54SDavid Woodhouse */
150*e2612a54SDavid Woodhouse pgd_t *pgd;
151*e2612a54SDavid Woodhouse /*
152*e2612a54SDavid Woodhouse * The virtual mapping of the control code page itself is used only
153*e2612a54SDavid Woodhouse * during the transition, while the current kernel's pages are all
154*e2612a54SDavid Woodhouse * in place. Thus the intermediate page table pages used to map it
155*e2612a54SDavid Woodhouse * are not control pages, but instead just normal pages obtained
156*e2612a54SDavid Woodhouse * with get_zeroed_page(). And have to be tracked (below) so that
157*e2612a54SDavid Woodhouse * they can be freed.
158*e2612a54SDavid Woodhouse */
1597f689041SKirill A. Shutemov p4d_t *p4d;
160f5deb796SHuang Ying pud_t *pud;
161f5deb796SHuang Ying pmd_t *pmd;
162f5deb796SHuang Ying pte_t *pte;
163dd5f7260SVivek Goyal };
164dd5f7260SVivek Goyal #endif /* CONFIG_X86_32 */
165dd5f7260SVivek Goyal
166dd5f7260SVivek Goyal #ifdef CONFIG_X86_64
167dd5f7260SVivek Goyal /*
168dd5f7260SVivek Goyal * Number of elements and order of elements in this structure should match
169dd5f7260SVivek Goyal * with the ones in arch/x86/purgatory/entry64.S. If you make a change here
170dd5f7260SVivek Goyal * make an appropriate change in purgatory too.
171dd5f7260SVivek Goyal */
17227f48d3eSVivek Goyal struct kexec_entry64_regs {
17327f48d3eSVivek Goyal uint64_t rax;
17427f48d3eSVivek Goyal uint64_t rcx;
17527f48d3eSVivek Goyal uint64_t rdx;
176dd5f7260SVivek Goyal uint64_t rbx;
17727f48d3eSVivek Goyal uint64_t rsp;
17827f48d3eSVivek Goyal uint64_t rbp;
179dd5f7260SVivek Goyal uint64_t rsi;
180dd5f7260SVivek Goyal uint64_t rdi;
18127f48d3eSVivek Goyal uint64_t r8;
18227f48d3eSVivek Goyal uint64_t r9;
18327f48d3eSVivek Goyal uint64_t r10;
18427f48d3eSVivek Goyal uint64_t r11;
18527f48d3eSVivek Goyal uint64_t r12;
18627f48d3eSVivek Goyal uint64_t r13;
18727f48d3eSVivek Goyal uint64_t r14;
18827f48d3eSVivek Goyal uint64_t r15;
18927f48d3eSVivek Goyal uint64_t rip;
19027f48d3eSVivek Goyal };
191bba4ed01STom Lendacky
192bba4ed01STom Lendacky extern int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages,
193bba4ed01STom Lendacky gfp_t gfp);
194bba4ed01STom Lendacky #define arch_kexec_post_alloc_pages arch_kexec_post_alloc_pages
195bba4ed01STom Lendacky
196bba4ed01STom Lendacky extern void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages);
197bba4ed01STom Lendacky #define arch_kexec_pre_free_pages arch_kexec_pre_free_pages
198bba4ed01STom Lendacky
1990738ecebSNaveen N. Rao void arch_kexec_protect_crashkres(void);
2000738ecebSNaveen N. Rao #define arch_kexec_protect_crashkres arch_kexec_protect_crashkres
2010738ecebSNaveen N. Rao
2020738ecebSNaveen N. Rao void arch_kexec_unprotect_crashkres(void);
2030738ecebSNaveen N. Rao #define arch_kexec_unprotect_crashkres arch_kexec_unprotect_crashkres
2040738ecebSNaveen N. Rao
2053e35142eSNaveen N. Rao #ifdef CONFIG_KEXEC_FILE
2063e35142eSNaveen N. Rao struct purgatory_info;
2073e35142eSNaveen N. Rao int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
2083e35142eSNaveen N. Rao Elf_Shdr *section,
2093e35142eSNaveen N. Rao const Elf_Shdr *relsec,
2103e35142eSNaveen N. Rao const Elf_Shdr *symtab);
2113e35142eSNaveen N. Rao #define arch_kexec_apply_relocations_add arch_kexec_apply_relocations_add
21265d9a9a6SNaveen N. Rao
21365d9a9a6SNaveen N. Rao int arch_kimage_file_post_load_cleanup(struct kimage *image);
21465d9a9a6SNaveen N. Rao #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
2153e35142eSNaveen N. Rao #endif
21692be3d6bSHuang Ying #endif
21792be3d6bSHuang Ying
2180ee59413SHidehiro Kawai extern void kdump_nmi_shootdown_cpus(void);
219f23d1f4aSZhang Yanfei
220ea53ad9cSEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
221ea53ad9cSEric DeVolder void arch_crash_handle_hotplug_event(struct kimage *image);
222ea53ad9cSEric DeVolder #define arch_crash_handle_hotplug_event arch_crash_handle_hotplug_event
223ea53ad9cSEric DeVolder
224ea53ad9cSEric DeVolder #ifdef CONFIG_HOTPLUG_CPU
225a72bbec7SEric DeVolder int arch_crash_hotplug_cpu_support(void);
226a72bbec7SEric DeVolder #define crash_hotplug_cpu_support arch_crash_hotplug_cpu_support
227ea53ad9cSEric DeVolder #endif
228ea53ad9cSEric DeVolder
229ea53ad9cSEric DeVolder #ifdef CONFIG_MEMORY_HOTPLUG
230a72bbec7SEric DeVolder int arch_crash_hotplug_memory_support(void);
231a72bbec7SEric DeVolder #define crash_hotplug_memory_support arch_crash_hotplug_memory_support
232ea53ad9cSEric DeVolder #endif
233a72bbec7SEric DeVolder
234a72bbec7SEric DeVolder unsigned int arch_crash_get_elfcorehdr_size(void);
235a72bbec7SEric DeVolder #define crash_get_elfcorehdr_size arch_crash_get_elfcorehdr_size
236ea53ad9cSEric DeVolder #endif
237ea53ad9cSEric DeVolder
238bb898558SAl Viro #endif /* __ASSEMBLY__ */
239bb898558SAl Viro
2401965aae3SH. Peter Anvin #endif /* _ASM_X86_KEXEC_H */
241