On Fri, Jul 28, 2023 at 11:09:05AM +0200, Ard Biesheuvel wrote: > The 32-bit trampoline no longer uses the stack for anything except > performing a far return back to long mode. Currently, this stack is > placed in the same page that carries the trampoline code, which means > this page must be mapped writable and executable, and the stack is > therefore executable as well. > > Replace the far return with a far jump, so that the return address can > be pre-calculated and patched into the code before it is called. This > removes the need for a stack entirely, and in a later patch, this will > be taken advantage of by removing writable permissions from (and adding > executable permissions to) this code page explicitly when booting via > the EFI stub. > > Not touching the stack pointer also makes it more straight-forward to > call the trampoline code as an ordinary 64-bit function from C code. > > Note that we need to preserve the value of RSP across the switch into ^^ Passive voice pls. > compatibility mode: the stack pointer may get truncated to 32 bits. > > Acked-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx> > Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx> > --- > arch/x86/boot/compressed/head_64.S | 64 ++++++++++---------- > arch/x86/boot/compressed/pgtable.h | 4 +- > arch/x86/boot/compressed/pgtable_64.c | 12 +++- > 3 files changed, 44 insertions(+), 36 deletions(-) > > diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S > index 491d985be75fd5b0..1b0c61d1b389fd37 100644 > --- a/arch/x86/boot/compressed/head_64.S > +++ b/arch/x86/boot/compressed/head_64.S > @@ -449,9 +449,6 @@ SYM_CODE_START(startup_64) > leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax > call *%rax > > - /* Restore the stack, the 32-bit trampoline uses its own stack */ > - leaq rva(boot_stack_end)(%rbx), %rsp > - > /* > * cleanup_trampoline() would restore trampoline memory. > * > @@ -537,32 +534,37 @@ SYM_FUNC_END(.Lrelocated) > * EDI contains the base address of the trampoline memory. > * Non-zero ESI means trampoline needs to enable 5-level paging. > */ > + .section ".rodata", "a", @progbits > SYM_CODE_START(trampoline_32bit_src) > - /* Grab return address */ > - movq (%rsp), %rax > - > - /* Set up 32-bit addressable stack */ > - leaq TRAMPOLINE_32BIT_STACK_END(%rdi), %rsp > - > - /* Preserve return address and other live 64-bit registers */ > - pushq %rax > + /* Preserve live 64-bit registers */ > pushq %r15 > pushq %rbp > pushq %rbx > > + /* Preserve top half of RSP in a legacy mode GPR to avoid truncation */ > + movq %rsp, %rbx > + shrq $32, %rbx > + > /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ > pushq $__KERNEL32_CS > leaq 0f(%rip), %rax > pushq %rax > lretq > > + /* > + * The 32-bit code below will do a far jump back to long mode and end > + * up here after reconfiguring the number of paging levels. > + */ > +.Lret: shlq $32, %rbx // Reconstruct stack pointer No side comments pls. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette