Received: by 10.213.65.68 with SMTP id h4csp201420imn; Mon, 26 Mar 2018 19:14:34 -0700 (PDT) X-Google-Smtp-Source: AG47ELvH9dyLh1BsxE0XcXIRCEywFkIhoz9XAEdq1crg3NrRLYCq5498gCQPC+cUJIYuy01kug44 X-Received: by 2002:a17:902:788e:: with SMTP id q14-v6mr44186896pll.396.1522116874430; Mon, 26 Mar 2018 19:14:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522116874; cv=none; d=google.com; s=arc-20160816; b=IdUwPXZcj1swC4X6p/n9lRa2VtFC6olFqKowY43FQa7eYeXaMwU4InrwLLFD3dzWF4 rRGgjZ4M24mpVZDHgTDiytGq2OV4FoqVRT9rdnzAf6V/fY57ZKoS3POObYdb3byrx1de XPgTAhjGRIV77fetIlf4L+8E5dJqI43vC3+36kZ0WAeto/+CGeZRsfTbQqXrC06AFDLp GExkn5te1Xn/otZnpICIgFFjgE7Qov+nhypF+ECY5KsvG1Co5hZhhyGCWNdQGmY3+QG9 Q+JDatafLJ4pcwkFIK5HuDGtZJNTwtZYjX3Q28WTQe70xxKm9OG4clpPbRHq0h1MCYZA fUPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=v0kMU6ULZX5gTgwOolu3gP/8w2siX5/LWpiM4mK06rk=; b=mwDRPb+kNQNISirBRG8QcMyycwShhQzqIq641ScpGYN5IVtOBp+HD/PVkP/kFWPUax ylcEtDhhAh0pS5SZNUeaZccZFdlZMInvWjEYYQHAa1lmUi1f0qM41ACHgVmJnTWjtXRE SrPWLS6UH3tAA2vJz13cMJfuxbQN8Mumh6TIpdq4yEpmVmGVuFUznqGZ0i3dLJBucg1V pPfJyI27UKUQK3R5XG4aDIkW3PC/2DlKGzjHY/kDd2ECWw7fp1jh9idIy3FT/uXew8X1 oFNQX5zLlmhPcVcQmiS5w7pz2vmWsnnvqDxIOkDmBqchtEZpkBLcrqxvBS6O1raSG7Hu VmzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ryzXH603; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a12-v6si165782plt.606.2018.03.26.19.14.20; Mon, 26 Mar 2018 19:14:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ryzXH603; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752514AbeC0CNZ (ORCPT + 99 others); Mon, 26 Mar 2018 22:13:25 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:33679 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752307AbeC0CNL (ORCPT ); Mon, 26 Mar 2018 22:13:11 -0400 Received: by mail-pg0-f66.google.com with SMTP id i194so3804050pgd.0; Mon, 26 Mar 2018 19:13:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=v0kMU6ULZX5gTgwOolu3gP/8w2siX5/LWpiM4mK06rk=; b=ryzXH603yVlbe4L9sm9T2vW5PGy7cx73BMP/NVfhCpwqR1N5Iazp6OrS4NIVWasfZt oKskLU4OyfVJ5h8UPYhHf2dkJIi2cE0osrTlx1Q4ZJW5vIpgEp5qrk7ftb8TbMPFrbge 9+R6CSxJEQ3juO7995fL4+0hjkz6QxG6b+6lSY11f0B7IF4OmjljcEhD23C+zoaz1Adv Rr0GUd78TSSCC7w6w7MwsF8fQuzJ/VwlmaXPrfY+ZMVTWkV6VUL7/Qf30Vz/XE7my1ok tskea8F+J0gmFJ0poYJ7/XAO0LPfbFQZCpxjglVDgFjX8y9oyK/gY7oqqlAaOxiDpQhZ XaKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=v0kMU6ULZX5gTgwOolu3gP/8w2siX5/LWpiM4mK06rk=; b=o/KxMb8N1kPelULuvepKuyqxcBrQBkXVnw+ySJs2XHP2NPBU2o9hU0JQurHYsq4EvS tPjBwM3MXl0ODmmm4CAf7Umj92oNz9+fXYeP3fOA2MEqPi6ol9SlqRqTcTE32lSlTIx5 nUtuundhGRLbC7KsFxZBrxXJQs91wNHI5v9A5Ekp6W0+ggg02VPOtRNG3fE7IV0SdcOz l6MfysHw0Vv/WEZELBQu8VZtF/JHPtEqA9ikW9bx1jOcTyaR1LkGYKzH/Nw4B4XzwskZ wJ+mzZi02uf9b85HGi3/e3/PKHTwLnxQIRxsIC12G1AkWWRiVruIwGN8daJNV/K9vwCm iqxw== X-Gm-Message-State: AElRT7GpL2kLmhGJUynDLgGvab7aZqPoH1wnkBaKpEhna4QivgQ442N3 dz5ZR8OJh1vQGRqh1x+sAuqHzA== X-Received: by 10.98.66.203 with SMTP id h72mr31638790pfd.156.1522116790435; Mon, 26 Mar 2018 19:13:10 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id k3sm148607pgq.35.2018.03.26.19.13.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Mar 2018 19:13:09 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Andrew Cooper Subject: [PATCH 2/2] KVM: VMX: Add Force Emulation Prefix for "emulate the next instruction" Date: Mon, 26 Mar 2018 19:12:15 -0700 Message-Id: <1522116735-4861-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1522116735-4861-1-git-send-email-wanpengli@tencent.com> References: <1522116735-4861-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li This patch 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 #include #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"); } Suggested-by: Andrew Cooper Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Andrew Cooper Signed-off-by: Wanpeng Li --- arch/x86/kvm/vmx.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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_shadow_vmcs, bool, S_IRUGO); static bool __read_mostly nested = 0; module_param(nested, bool, S_IRUGO); +static bool __read_mostly fep = 0; +module_param(fep, bool, S_IRUGO); + static u64 __read_mostly host_xss; static bool __read_mostly enable_pml = 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 = EMULTYPE_TRAP_UD; + + if (fep) { + char sig[5]; /* ud2; .ascii "kvm" */ + struct x86_exception e; + + kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, + kvm_get_linear_rip(vcpu), sig, sizeof(sig), &e); + if (memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0) { + emulation_type = 0; + kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); + } + } - er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); + er = emulate_instruction(vcpu, emulation_type); if (er == EMULATE_USER_EXIT) return 0; if (er != EMULATE_DONE) -- 2.7.4