Skip to content

Commit d6a41f1

Browse files
ardbiesheuvelbp3tk0v
authored andcommitted
x86/startup_64: Simplify calculation of initial page table address
Determining the address of the initial page table to program into CR3 involves: - taking the physical address - adding the SME encryption mask On the primary entry path, the code is mapped using a 1:1 virtual to physical translation, so the physical address can be taken directly using a RIP-relative LEA instruction. On the secondary entry path, the address can be obtained by taking the offset from the virtual kernel base (__START_kernel_map) and adding the physical kernel base. This is implemented in a slightly confusing way, so clean this up. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Tested-by: Tom Lendacky <thomas.lendacky@amd.com> Link: https://lore.kernel.org/r/20240227151907.387873-14-ardb+git@google.com
1 parent 63bed96 commit d6a41f1

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

arch/x86/kernel/head_64.S

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,11 @@ SYM_CODE_START_NOALIGN(startup_64)
111111
call __startup_64
112112

113113
/* Form the CR3 value being sure to include the CR3 modifier */
114-
addq $(early_top_pgt - __START_KERNEL_map), %rax
114+
leaq early_top_pgt(%rip), %rcx
115+
addq %rcx, %rax
115116

116117
#ifdef CONFIG_AMD_MEM_ENCRYPT
117118
mov %rax, %rdi
118-
mov %rax, %r14
119-
120-
addq phys_base(%rip), %rdi
121119

122120
/*
123121
* For SEV guests: Verify that the C-bit is correct. A malicious
@@ -126,12 +124,6 @@ SYM_CODE_START_NOALIGN(startup_64)
126124
* the next RET instruction.
127125
*/
128126
call sev_verify_cbit
129-
130-
/*
131-
* Restore CR3 value without the phys_base which will be added
132-
* below, before writing %cr3.
133-
*/
134-
mov %r14, %rax
135127
#endif
136128

137129
jmp 1f
@@ -171,18 +163,18 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
171163
/* Clear %R15 which holds the boot_params pointer on the boot CPU */
172164
xorl %r15d, %r15d
173165

166+
/* Derive the runtime physical address of init_top_pgt[] */
167+
movq phys_base(%rip), %rax
168+
addq $(init_top_pgt - __START_KERNEL_map), %rax
169+
174170
/*
175171
* Retrieve the modifier (SME encryption mask if SME is active) to be
176172
* added to the initial pgdir entry that will be programmed into CR3.
177173
*/
178174
#ifdef CONFIG_AMD_MEM_ENCRYPT
179-
movq sme_me_mask, %rax
180-
#else
181-
xorl %eax, %eax
175+
addq sme_me_mask(%rip), %rax
182176
#endif
183177

184-
/* Form the CR3 value being sure to include the CR3 modifier */
185-
addq $(init_top_pgt - __START_KERNEL_map), %rax
186178
1:
187179

188180
/*
@@ -212,9 +204,6 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
212204
btsl $X86_CR4_PSE_BIT, %ecx
213205
movq %rcx, %cr4
214206

215-
/* Setup early boot stage 4-/5-level pagetables. */
216-
addq phys_base(%rip), %rax
217-
218207
/*
219208
* Switch to new page-table
220209
*

0 commit comments

Comments
 (0)