All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: SU Hang <darcy.sh@antgroup.com>
Cc: kvm@vger.kernel.org, pbonzini@redhat.com, thuth@redhat.com,
	drjones@redhat.com
Subject: Re: [kvm-unit-tests PATCH 2/2] x86: replace `int 0x20` with `syscall`
Date: Mon, 25 Apr 2022 15:58:47 +0000	[thread overview]
Message-ID: <YmbFN6yKwnLDRdr8@google.com> (raw)
In-Reply-To: <20220424070951.106990-2-darcy.sh@antgroup.com>

On Sun, Apr 24, 2022, SU Hang wrote:

Why?  As gross as it is, I actually think INTn is a better option because it
doesn't require writing multiple MSRs, and can work for both 64-bit and 32-bit KUT.
The latter is currently a moot point since this code is 64-bit only, but the UMIP
test _does_ support 32-bit, and it's do_ring3() should really be rolled into this
framework.

Furthermore, we really should have a test to verify that KVM correctly emulates
SYSCALL at CPL3 with EFER.SCE=0, and forcing EFER.SCE=1 just to get to CPL3 would
make it impossible to utilize this framework for such a test.

> Signed-off-by: SU Hang <darcy.sh@antgroup.com>
> ---
>  lib/x86/usermode.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/lib/x86/usermode.c b/lib/x86/usermode.c
> index 477cb9f..e4cb899 100644
> --- a/lib/x86/usermode.c
> +++ b/lib/x86/usermode.c
> @@ -12,7 +12,6 @@
>  #include <stdint.h>
>  
>  #define USERMODE_STACK_SIZE	0x2000
> -#define RET_TO_KERNEL_IRQ	0x20
>  
>  static jmp_buf jmpbuf;
>  
> @@ -40,9 +39,11 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
>  	static unsigned char user_stack[USERMODE_STACK_SIZE];
>  
>  	*raised_vector = 0;
> -	set_idt_entry(RET_TO_KERNEL_IRQ, &ret_to_kernel, 3);
>  	handle_exception(fault_vector,
>  			restore_exec_to_jmpbuf_exception_handler);
> +	wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_SCE);

Tangentially related, KUT should really explicitly initialize EFER.SCE during boot.
cstart64.S leaves it alone, which means KUT has no defined value for EFER.SCE.
One thought would be to set EFER.SCE=1 during boot (in a separate patch) and remove
the code from the syscall test that forces EFER.SCE.  AFAICT, no existing test
verifies that KVM injects #UD on SYSCALL without EFER.SCE set, though it would be
nice to add one.

> +	wrmsr(MSR_STAR, ((u64)(USER_CS32 << 16) | KERNEL_CS) << 32);

It doesn't matter at this time because this framework doesn't ses SYSRET, but this
should be USER_CS or USER_CS64.

> +	wrmsr(MSR_LSTAR, (u64)&ret_to_kernel);
>  
>  	if (setjmp(jmpbuf) != 0) {
>  		*raised_vector = 1;
> @@ -73,7 +74,7 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
>  			"mov %[arg4], %%rcx\n\t"
>  			"call *%[func]\n\t"
>  			/* Return to kernel via system call */
> -			"int %[kernel_entry_vector]\n\t"
> +			"syscall\n\t"
>  			/* Kernel Mode */
>  			"ret_to_kernel:\n\t"
>  			"mov %[rsp0], %%rsp\n\t"
> @@ -89,8 +90,7 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
>  			[user_ds]"i"(USER_DS),
>  			[user_cs]"i"(USER_CS),
>  			[user_stack_top]"r"(user_stack +
> -					sizeof(user_stack)),
> -			[kernel_entry_vector]"i"(RET_TO_KERNEL_IRQ)
> +					sizeof(user_stack))
>  			:
>  			"rsi", "rdi", "rbx", "rcx", "rdx", "r8", "r9", "r10", "r11");
>  
> -- 
> 2.32.0.3.g01195cf9f
> 

  reply	other threads:[~2022-04-25 15:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-24  7:09 [kvm-unit-tests PATCH 1/2] x86: replace `push` `pop` with callee-clobbered list SU Hang
2022-04-24  7:09 ` [kvm-unit-tests PATCH 2/2] x86: replace `int 0x20` with `syscall` SU Hang
2022-04-25 15:58   ` Sean Christopherson [this message]
2022-04-27  1:07     ` Sean Christopherson
2022-04-27  9:27     ` SU Hang
2022-04-28 15:58       ` Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YmbFN6yKwnLDRdr8@google.com \
    --to=seanjc@google.com \
    --cc=darcy.sh@antgroup.com \
    --cc=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.