Skip to content

Commit 64ef578

Browse files
ardbiesheuvelbp3tk0v
authored andcommitted
x86/decompressor: Call trampoline directly from C code
Instead of returning to the asm calling code to invoke the trampoline, call it straight from the C code that sets it up. That way, the struct return type is no longer needed for returning two values, and the call can be made conditional more cleanly in a subsequent patch. This means that all callee save 64-bit registers need to be preserved and restored, as their contents may not survive the legacy mode switch. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Link: https://lore.kernel.org/r/20230807162720.545787-13-ardb@kernel.org
1 parent bd328aa commit 64ef578

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

arch/x86/boot/compressed/head_64.S

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -430,25 +430,14 @@ SYM_CODE_START(startup_64)
430430
#endif
431431

432432
/*
433-
* paging_prepare() sets up the trampoline and checks if we need to
434-
* enable 5-level paging.
435-
*
436-
* paging_prepare() returns a two-quadword structure which lands
437-
* into RDX:RAX:
438-
* - Address of the trampoline is returned in RAX.
439-
* - Non zero RDX means trampoline needs to enable 5-level
440-
* paging.
433+
* configure_5level_paging() updates the number of paging levels using
434+
* a trampoline in 32-bit addressable memory if the current number does
435+
* not match the desired number.
441436
*
442437
* Pass the boot_params pointer as the first argument.
443438
*/
444439
movq %r15, %rdi
445-
call paging_prepare
446-
447-
/* Pass the trampoline address and boolean flag as args #1 and #2 */
448-
movq %rax, %rdi
449-
movq %rdx, %rsi
450-
leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax
451-
call *%rax
440+
call configure_5level_paging
452441

453442
/*
454443
* cleanup_trampoline() would restore trampoline memory.
@@ -543,11 +532,14 @@ SYM_FUNC_END(.Lrelocated)
543532
.section ".rodata", "a", @progbits
544533
SYM_CODE_START(trampoline_32bit_src)
545534
/*
546-
* Preserve live 64-bit registers on the stack: this is necessary
547-
* because the architecture does not guarantee that GPRs will retain
548-
* their full 64-bit values across a 32-bit mode switch.
535+
* Preserve callee save 64-bit registers on the stack: this is
536+
* necessary because the architecture does not guarantee that GPRs will
537+
* retain their full 64-bit values across a 32-bit mode switch.
549538
*/
550539
pushq %r15
540+
pushq %r14
541+
pushq %r13
542+
pushq %r12
551543
pushq %rbp
552544
pushq %rbx
553545

@@ -574,6 +566,9 @@ SYM_CODE_START(trampoline_32bit_src)
574566
/* Restore the preserved 64-bit registers */
575567
popq %rbx
576568
popq %rbp
569+
popq %r12
570+
popq %r13
571+
popq %r14
577572
popq %r15
578573
retq
579574

arch/x86/boot/compressed/pgtable_64.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ unsigned int __section(".data") pgdir_shift = 39;
1616
unsigned int __section(".data") ptrs_per_p4d = 1;
1717
#endif
1818

19-
struct paging_config {
20-
unsigned long trampoline_start;
21-
unsigned long l5_required;
22-
};
23-
2419
/* Buffer to preserve trampoline memory */
2520
static char trampoline_save[TRAMPOLINE_32BIT_SIZE];
2621

@@ -29,7 +24,7 @@ static char trampoline_save[TRAMPOLINE_32BIT_SIZE];
2924
* purposes.
3025
*
3126
* Avoid putting the pointer into .bss as it will be cleared between
32-
* paging_prepare() and extract_kernel().
27+
* configure_5level_paging() and extract_kernel().
3328
*/
3429
unsigned long *trampoline_32bit __section(".data");
3530

@@ -106,13 +101,13 @@ static unsigned long find_trampoline_placement(void)
106101
return bios_start - TRAMPOLINE_32BIT_SIZE;
107102
}
108103

109-
struct paging_config paging_prepare(void *rmode)
104+
asmlinkage void configure_5level_paging(struct boot_params *bp)
110105
{
111-
struct paging_config paging_config = {};
112-
void *tramp_code;
106+
void (*toggle_la57)(void *trampoline, bool enable_5lvl);
107+
bool l5_required = false;
113108

114109
/* Initialize boot_params. Required for cmdline_find_option_bool(). */
115-
boot_params = rmode;
110+
boot_params = bp;
116111

117112
/*
118113
* Check if LA57 is desired and supported.
@@ -130,17 +125,15 @@ struct paging_config paging_prepare(void *rmode)
130125
!cmdline_find_option_bool("no5lvl") &&
131126
native_cpuid_eax(0) >= 7 &&
132127
(native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31)))) {
133-
paging_config.l5_required = 1;
128+
l5_required = true;
134129

135130
/* Initialize variables for 5-level paging */
136131
__pgtable_l5_enabled = 1;
137132
pgdir_shift = 48;
138133
ptrs_per_p4d = 512;
139134
}
140135

141-
paging_config.trampoline_start = find_trampoline_placement();
142-
143-
trampoline_32bit = (unsigned long *)paging_config.trampoline_start;
136+
trampoline_32bit = (unsigned long *)find_trampoline_placement();
144137

145138
/* Preserve trampoline memory */
146139
memcpy(trampoline_save, trampoline_32bit, TRAMPOLINE_32BIT_SIZE);
@@ -149,7 +142,7 @@ struct paging_config paging_prepare(void *rmode)
149142
memset(trampoline_32bit, 0, TRAMPOLINE_32BIT_SIZE);
150143

151144
/* Copy trampoline code in place */
152-
tramp_code = memcpy(trampoline_32bit +
145+
toggle_la57 = memcpy(trampoline_32bit +
153146
TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long),
154147
&trampoline_32bit_src, TRAMPOLINE_32BIT_CODE_SIZE);
155148

@@ -159,7 +152,8 @@ struct paging_config paging_prepare(void *rmode)
159152
* immediate absolute address, which needs to be adjusted based on the
160153
* placement of the trampoline.
161154
*/
162-
*(u32 *)(tramp_code + trampoline_ljmp_imm_offset) += (unsigned long)tramp_code;
155+
*(u32 *)((u8 *)toggle_la57 + trampoline_ljmp_imm_offset) +=
156+
(unsigned long)toggle_la57;
163157

164158
/*
165159
* The code below prepares page table in trampoline memory.
@@ -175,10 +169,10 @@ struct paging_config paging_prepare(void *rmode)
175169
* We are not going to use the page table in trampoline memory if we
176170
* are already in the desired paging mode.
177171
*/
178-
if (paging_config.l5_required == !!(native_read_cr4() & X86_CR4_LA57))
172+
if (l5_required == !!(native_read_cr4() & X86_CR4_LA57))
179173
goto out;
180174

181-
if (paging_config.l5_required) {
175+
if (l5_required) {
182176
/*
183177
* For 4- to 5-level paging transition, set up current CR3 as
184178
* the first and the only entry in a new top-level page table.
@@ -201,7 +195,7 @@ struct paging_config paging_prepare(void *rmode)
201195
}
202196

203197
out:
204-
return paging_config;
198+
toggle_la57(trampoline_32bit, l5_required);
205199
}
206200

207201
void cleanup_trampoline(void *pgtable)

0 commit comments

Comments
 (0)