Re: [kvm-unit-tests PATCH 2/3] x86: Align L2's stacks

Sean Christopherson
2021-12-09T20:07:00Z

On Thu, Dec 09, 2021, Aaron Lewis wrote:
> Setting the stack to PAGE_SIZE - 1 sets the stack to being 1-byte

LOL, nice. It's also pointless because any access larger than a byte that occurs
without first adjusting the stack will explode.

> aligned, which fails in usermode with alignment checks enabled (ie: with
> flags cr0.am set and eflags.ac set). This was causing an #AC in
> usermode.c when preparing to call the callback in run_in_user().
> Aligning the stack fixes the issue.
>
> For the purposes of fixing the #AC in usermode.c the stack has to be
> aligned to at least an 8-byte boundary. Setting it to a page aligned
> boundary ensures any stack alignment requirements are met as x86_64
> stacks generally want to be 16-byte aligned.
>
> Signed-off-by: Aaron Lewis <aaronlewis@google.com>
> ---
> x86/vmx.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/x86/vmx.c b/x86/vmx.c
> index 6dc9a55..44f4861 100644
> --- a/x86/vmx.c
> +++ b/x86/vmx.c
> @@ -1242,7 +1242,7 @@ static void init_vmcs_guest(void)
> vmcs_write(GUEST_CR4, guest_cr4);
> vmcs_write(GUEST_SYSENTER_CS, KERNEL_CS);
> vmcs_write(GUEST_SYSENTER_ESP,
> - (u64)(guest_syscall_stack + PAGE_SIZE - 1));
> + (u64)(guest_syscall_stack + PAGE_SIZE));
> vmcs_write(GUEST_SYSENTER_EIP, (u64)(&entry_sysenter));
> vmcs_write(GUEST_DR7, 0);
> vmcs_write(GUEST_EFER, rdmsr(MSR_EFER));
> @@ -1292,7 +1292,7 @@ static void init_vmcs_guest(void)
>
> /* 26.3.1.4 */
> vmcs_write(GUEST_RIP, (u64)(&guest_entry));
> - vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE - 1));
> + vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE));

Rather than do the math here, which looks arbitrary at first blance, what about
adjusting on allocation and renaming the variables accordingly? E.g.

diff --git a/x86/vmx.c b/x86/vmx.c
index 6dc9a55..bc8be77 100644
--- a/x86/vmx.c
+++ b/x86/vmx.c
@@ -1388,8 +1388,8 @@ void init_vmx(u64 *vmxon_region)
static void alloc_bsp_vmx_pages(void)
{
bsp_vmxon_region = alloc_page();
- guest_stack = alloc_page();
- guest_syscall_stack = alloc_page();
+ guest_stack_top = alloc_page() + PAGE_SIZE;
+ guest_syscall_stack_top = alloc_page() + PAGE_SIZE;
vmcs_root = alloc_page();
}

> vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED);
>
> /* 26.3.1.5 */
> --
> 2.34.1.173.g76aa8bc2d0-goog
>