1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * relocate_kernel.S - put the kernel image in place to boot
4 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
7 #include <linux/linkage.h>
8 #include <asm/page_types.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/processor-flags.h>
14 * Must be relocatable PIC code callable as a C function, in particular
15 * there must be a plain RET and not jump to return thunk.
18 #define PTR(x) (x << 2)
21 * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
22 * ~ control_page + PAGE_SIZE are used as data storage and stack for
25 #define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
27 /* Minimal CPU state */
34 #define CP_VA_CONTROL_PAGE DATA(0x10)
35 #define CP_PA_PGD DATA(0x14)
36 #define CP_PA_SWAP_PAGE DATA(0x18)
37 #define CP_PA_BACKUP_PAGES_MAP DATA(0x1c)
40 SYM_CODE_START_NOALIGN(relocate_kernel)
41 /* Save the CPU context, used for jumping back */
49 movl 20+8(%esp), %ebp /* list of pages */
50 movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
59 /* read the arguments and say goodbye to the stack */
60 movl 20+4(%esp), %ebx /* page_list */
61 movl 20+8(%esp), %ebp /* list of pages */
62 movl 20+12(%esp), %edx /* start address */
63 movl 20+16(%esp), %ecx /* cpu_has_pae */
64 movl 20+20(%esp), %esi /* preserve_context */
66 /* zero out flags, and disable interrupts */
70 /* save some information for jumping back */
71 movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
72 movl %edi, CP_VA_CONTROL_PAGE(%edi)
73 movl PTR(PA_PGD)(%ebp), %eax
74 movl %eax, CP_PA_PGD(%edi)
75 movl PTR(PA_SWAP_PAGE)(%ebp), %eax
76 movl %eax, CP_PA_SWAP_PAGE(%edi)
77 movl %ebx, CP_PA_BACKUP_PAGES_MAP(%edi)
80 * get physical address of control page now
81 * this is impossible after page table switch
83 movl PTR(PA_CONTROL_PAGE)(%ebp), %edi
85 /* switch to new set of page tables */
86 movl PTR(PA_PGD)(%ebp), %eax
89 /* setup a new stack at the end of the physical control page */
90 lea PAGE_SIZE(%edi), %esp
92 /* jump to identity mapped page */
94 addl $(identity_mapped - relocate_kernel), %eax
99 SYM_CODE_END(relocate_kernel)
101 SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
102 /* set return address to 0 if not preserving context */
104 /* store the start address on the stack */
108 * Set cr0 to a known state:
110 * - Alignment check disabled
111 * - Write protect disabled
113 * - Don't do FP software emulation.
114 * - Protected mode enabled
117 andl $~(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %eax
118 orl $(X86_CR0_PE), %eax
121 /* clear cr4 if applicable */
125 * Set cr4 to a known state:
126 * Setting everything to zero seems safe.
134 /* Flush the TLB (needed?) */
138 movl CP_PA_SWAP_PAGE(%edi), %eax
145 * To be certain of avoiding problems with self-modifying code
146 * I need to execute a serializing instruction here.
147 * So I flush the TLB, it's handy, and not processor dependent.
153 * set all of the registers to known values
171 movl CP_PA_SWAP_PAGE(%edi), %esp
172 addl $PAGE_SIZE, %esp
174 ANNOTATE_RETPOLINE_SAFE
177 /* get the re-entry point of the peer system */
182 subl $(1b - relocate_kernel), %ebx
183 movl CP_VA_CONTROL_PAGE(%ebx), %edi
184 lea PAGE_SIZE(%ebx), %esp
185 movl CP_PA_SWAP_PAGE(%ebx), %eax
186 movl CP_PA_BACKUP_PAGES_MAP(%ebx), %edx
191 movl CP_PA_PGD(%ebx), %eax
194 orl $X86_CR0_PG, %eax
196 lea PAGE_SIZE(%edi), %esp
198 addl $(virtual_mapped - relocate_kernel), %eax
203 SYM_CODE_END(identity_mapped)
205 SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
223 SYM_CODE_END(virtual_mapped)
226 SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
236 0: /* top, read another word from the indirection page */
240 testb $0x1, %cl /* is it a destination page */
243 andl $0xfffff000, %edi
246 testb $0x2, %cl /* is it an indirection page */
249 andl $0xfffff000, %ebx
252 testb $0x4, %cl /* is it the done indicator */
256 testb $0x8, %cl /* is it the source indicator */
257 jz 0b /* Ignore it otherwise */
258 movl %ecx, %esi /* For every source page do a copy */
259 andl $0xfffff000, %esi
278 lea PAGE_SIZE(%ebp), %esi
288 SYM_CODE_END(swap_pages)
290 .globl kexec_control_code_size
291 .set kexec_control_code_size, . - relocate_kernel