public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction"
@ 2018-03-27  2:12 Wanpeng Li
  2018-03-27  2:12 ` [PATCH 1/2] KVM: VMX: Introduce handle_ud() Wanpeng Li
  2018-03-27  2:12 ` [PATCH 2/2] KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction" Wanpeng Li
  0 siblings, 2 replies; 12+ messages in thread
From: Wanpeng Li @ 2018-03-27  2:12 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Radim Krčmář, Andrew Cooper

This patchset introduces a Force Emulation Prefix (ud2a; .ascii "kvm") 
for "emulate the next instruction", the codes will be executed by emulator 
instead of processor, for testing purposes.

A testcase here:

#include <stdio.h>
#include <string.h>
   
#define HYPERVISOR_INFO 0x40000000
   
#define CPUID(idx, eax, ebx, ecx, edx)\
    asm volatile (\
    "ud2a; .ascii \"kvm\"; 1: cpuid" \
    :"=b" (*ebx), "=a" (*eax),"=c" (*ecx), "=d" (*edx)\
        :"0"(idx) );  
   
void main()  
{  
	unsigned int eax,ebx,ecx,edx;  
	char string[13];  
   
	CPUID(HYPERVISOR_INFO, &eax, &ebx, &ecx, &edx);  
	*(unsigned int *)(string+0) = ebx;  
	*(unsigned int *)(string+4) = ecx;  
	*(unsigned int *)(string+8) = edx;  
   
	string[12] = 0;  
	if (strncmp(string, "KVMKVMKVM\0\0\0",12) == 0)
		printf("kvm guest\n");  
	else  
		printf("bare hardware\n");  
}

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>

Wanpeng Li (2):
  KVM: VMX: Introduce handle_ud()
  KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction"

 arch/x86/kvm/vmx.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)

-- 
2.7.4

^ permalink raw reply	[flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] KVM: VMX: Introduce handle_ud()
@ 2018-03-27  7:43 Liran Alon
  0 siblings, 0 replies; 12+ messages in thread
From: Liran Alon @ 2018-03-27  7:43 UTC (permalink / raw)
  To: kernellwp; +Cc: rkrcmar, pbonzini, linux-kernel, andrew.cooper3, kvm


----- kernellwp@gmail.com wrote:

> From: Wanpeng Li <wanpengli@tencent.com>
> 
> Introduce handle_ud() to handle invalid opcode, this function will be
> 
> used by later patches.
> 
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Cc: Andrew Cooper <andrew.cooper3@citrix.com>
> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> ---
>  arch/x86/kvm/vmx.c | 22 ++++++++++++++--------
>  1 file changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 9bc05f5..0f99833 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -6215,6 +6215,18 @@ static int handle_machine_check(struct kvm_vcpu
> *vcpu)
>  	return 1;
>  }
>  
> +static int handle_ud(struct kvm_vcpu *vcpu)
> +{
> +	enum emulation_result er;
> +
> +	er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
> +	if (er == EMULATE_USER_EXIT)
> +		return 0;
> +	if (er != EMULATE_DONE)
> +		kvm_queue_exception(vcpu, UD_VECTOR);
> +	return 1;
> +}
> +
>  static int handle_exception(struct kvm_vcpu *vcpu)
>  {
>  	struct vcpu_vmx *vmx = to_vmx(vcpu);
> @@ -6233,14 +6245,8 @@ static int handle_exception(struct kvm_vcpu
> *vcpu)
>  	if (is_nmi(intr_info))
>  		return 1;  /* already handled by vmx_vcpu_run() */
>  
> -	if (is_invalid_opcode(intr_info)) {
> -		er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
> -		if (er == EMULATE_USER_EXIT)
> -			return 0;
> -		if (er != EMULATE_DONE)
> -			kvm_queue_exception(vcpu, UD_VECTOR);
> -		return 1;
> -	}
> +	if (is_invalid_opcode(intr_info))
> +		return handle_ud(vcpu);
>  
>  	error_code = 0;
>  	if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
> -- 
> 2.7.4

Reviewed-By: Liran Alon <liran.alon@oracle.com>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2018-03-27  7:43 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-27  2:12 [PATCH 0/2] KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction" Wanpeng Li
2018-03-27  2:12 ` [PATCH 1/2] KVM: VMX: Introduce handle_ud() Wanpeng Li
2018-03-27  4:38   ` Konrad Rzeszutek Wilk
2018-03-27  2:12 ` [PATCH 2/2] KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction" Wanpeng Li
2018-03-27  4:40   ` Konrad Rzeszutek Wilk
2018-03-27  4:55     ` Konrad Rzeszutek Wilk
2018-03-27  5:03       ` Wanpeng Li
2018-03-27  5:18         ` Konrad Rzeszutek Wilk
2018-03-27  7:25           ` Paolo Bonzini
2018-03-27  7:29             ` Wanpeng Li
2018-03-27  5:09     ` Wanpeng Li
  -- strict thread matches above, loose matches on Subject: below --
2018-03-27  7:43 [PATCH 1/2] KVM: VMX: Introduce handle_ud() Liran Alon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox