From mboxrd@z Thu Jan 1 00:00:00 1970 From: Konrad Rzeszutek Wilk Subject: Re: [PATCH 2/2] KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction" Date: Tue, 27 Mar 2018 00:40:20 -0400 Message-ID: <20180327044020.GB16974@char.us.oracle.com> References: <1522116735-4861-1-git-send-email-wanpengli@tencent.com> <1522116735-4861-3-git-send-email-wanpengli@tencent.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= , Andrew Cooper To: Wanpeng Li Return-path: Content-Disposition: inline In-Reply-To: <1522116735-4861-3-git-send-email-wanpengli@tencent.com> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org On Mon, Mar 26, 2018 at 07:12:15PM -0700, Wanpeng Li wrote: > From: Wanpeng Li >=20 > This patch introduces a Force Emulation Prefix (ud2a; .ascii "kvm") for= =20 > "emulate the next instruction", the codes will be executed by emulator=20 > instead of processor, for testing purposes. Can you expand a bit ? Why do you want this in KVM in the first place? Should this be controlled by a boolean parameter?=20 >=20 > A testcase here: >=20 > #include > #include > =20 > #define HYPERVISOR_INFO 0x40000000 > =20 > #define CPUID(idx, eax, ebx, ecx, edx)\ > asm volatile (\ > "ud2a; .ascii \"kvm\"; 1: cpuid" \ > :"=3Db" (*ebx), "=3Da" (*eax),"=3Dc" (*ecx), "=3Dd" (*edx)\ > :"0"(idx) ); =20 > =20 > void main() =20 > { =20 > unsigned int eax,ebx,ecx,edx; =20 > char string[13]; =20 > =20 > CPUID(HYPERVISOR_INFO, &eax, &ebx, &ecx, &edx); =20 > *(unsigned int *)(string+0) =3D ebx; =20 > *(unsigned int *)(string+4) =3D ecx; =20 > *(unsigned int *)(string+8) =3D edx; =20 > =20 > string[12] =3D 0; =20 > if (strncmp(string, "KVMKVMKVM\0\0\0",12) =3D=3D 0) > printf("kvm guest\n"); =20 > else =20 > printf("bare hardware\n"); =20 > } >=20 > Suggested-by: Andrew Cooper > Cc: Paolo Bonzini > Cc: Radim Kr=C4=8Dm=C3=A1=C5=99 > Cc: Andrew Cooper > Signed-off-by: Wanpeng Li > --- > arch/x86/kvm/vmx.c | 18 +++++++++++++++++- > 1 file changed, 17 insertions(+), 1 deletion(-) >=20 > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c > index 0f99833..90abed8 100644 > --- a/arch/x86/kvm/vmx.c > +++ b/arch/x86/kvm/vmx.c > @@ -108,6 +108,9 @@ module_param_named(enable_shadow_vmcs, enable_shado= w_vmcs, bool, S_IRUGO); > static bool __read_mostly nested =3D 0; > module_param(nested, bool, S_IRUGO); > =20 > +static bool __read_mostly fep =3D 0; > +module_param(fep, bool, S_IRUGO); > + > static u64 __read_mostly host_xss; > =20 > static bool __read_mostly enable_pml =3D 1; > @@ -6218,8 +6221,21 @@ static int handle_machine_check(struct kvm_vcpu = *vcpu) > static int handle_ud(struct kvm_vcpu *vcpu) > { > enum emulation_result er; > + int emulation_type =3D EMULTYPE_TRAP_UD; > + > + if (fep) { > + char sig[5]; /* ud2; .ascii "kvm" */ > + struct x86_exception e; Don't you want to do =3D { }; to memset it? > + > + kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, > + kvm_get_linear_rip(vcpu), sig, sizeof(sig), &e); > + if (memcmp(sig, "\xf\xbkvm", sizeof(sig)) =3D=3D 0) { > + emulation_type =3D 0; > + kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); > + } > + } > =20 > - er =3D emulate_instruction(vcpu, EMULTYPE_TRAP_UD); > + er =3D emulate_instruction(vcpu, emulation_type); > if (er =3D=3D EMULATE_USER_EXIT) > return 0; > if (er !=3D EMULATE_DONE) > --=20 > 2.7.4 >=20