Received: by 10.213.65.68 with SMTP id h4csp123258imn; Fri, 30 Mar 2018 02:11:30 -0700 (PDT) X-Google-Smtp-Source: AIpwx48b/e6i/YmiSNSdpjjUKu60Xa3zw2rPoUDVql+Yb6GxnFhaSKpDs8BUScWoyze8qrOrAVjC X-Received: by 10.101.99.213 with SMTP id n21mr7953585pgv.202.1522401090431; Fri, 30 Mar 2018 02:11:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522401090; cv=none; d=google.com; s=arc-20160816; b=v+qRVgXAsujPKfboyxa86tISWsQfXw8eDUTFLiSd6wLyCzSS/0+D+n4qK5CZ2eZBvq NaFpUydjPAcdnfdiC3rOCe4uxI4ZqXHnpEOY51hexgLQySjqO/IqzIiG6+1KauR43qPt KqXsGBaAcfsTavLLCn4vNInO5z7m6l2I+N109Zi7BdzaYDpIkDUt5ZvfbCOqrVUsgR+Y l2EyL6imMBm4CybeYA2s8Fw+Vgy50oB2jGkSWJI7H7MUg0AjttiruATDNtFUf3UxR3W0 k3nRZfDCwYUQTAKcRGxMBHWN0ajpPLdT0ZJQMSXMCBeWo/y7GR26I7JXhc4Mxii3eYSK MYsA== 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=DOy5QDFYq+rFTxylPnJJlAijtD++R2Il6CfLaKuEiow=; b=YKWqPWi/+7v6xvtAdLA3B5bwi0c9r/y1aIWDiNOw10gSLDT+EpyJBv0aXayxA6jsjE lMrznsqKBkoUiEd6lr7rLgpO2PsJxbDVXasH1lEpuBzoExB9GzfvIddgxw/uXWtz0uIx Zt08rK03CSROkG0xfwvBsT3sM09QqOKosu57AnUUuRuwhIUtK53YCBwE7f3NRX921Ku2 yy8/vsQdaIg0bGRoW0k7epkqImBJddcd5RiLX6w2ZHY5ky/OcZ/hQdV0Gv5i9dqYD3tN kPUkwFjrY1WE7CxpvGkPMJWhOLEklV5yQlpywST4QfcWubpnivGibtfvOPRNXe3z/8FT 917A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=VfLJGnMo; 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 m190si5347329pga.710.2018.03.30.02.11.16; Fri, 30 Mar 2018 02:11:30 -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=VfLJGnMo; 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 S1752105AbeC3JJM (ORCPT + 99 others); Fri, 30 Mar 2018 05:09:12 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:42742 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750790AbeC3JHm (ORCPT ); Fri, 30 Mar 2018 05:07:42 -0400 Received: by mail-pf0-f194.google.com with SMTP id a16so5094949pfn.9; Fri, 30 Mar 2018 02:07:41 -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=DOy5QDFYq+rFTxylPnJJlAijtD++R2Il6CfLaKuEiow=; b=VfLJGnMox5g7m6OsmX2Y4usimvcMp6/um1DfIsdLhtWESdTFQB/Kpw7EFbJoIdQYqr adS93wvRdObzX6MKtUPDHTVJP+4zfy5AEd2jzzRfSbYeoQBZDVjTu6VOCsPFg79tWP1m Hd0/S0MO4pRuDEKuUEUO8Z3stbVTIxP8lbCuWHzb1eifmFSbeDAMPMz6pJv1JA7K2/rD x5yYeS1xhNEfJ0tTyRFoEVEnfwz/Mx+9V0zgSB3xWfbFfYcrvfAIa7ywICUEhlVvuJM5 3fOGqk1ASQSd93inepQ6aHP5r0JCXLJMgsDHBmHJrMM+rFv5QjeDeBndx+trU2xIoFjJ 8OWA== 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=DOy5QDFYq+rFTxylPnJJlAijtD++R2Il6CfLaKuEiow=; b=g6AVFVooEQ/hReuSXRiU0VA2ikchi6mm5dJHIOCJksTMgXy28MEzy6CpAt+piHsaoR Eg2/Hs1vUITwPf2pRkjk1ZzBcAa2gnVNLMllRwewGBOcrg1Ivuu4WV/6RxcVLZQQGg8j ulu5HRSqQV59n6eE6CDL17Ib2A5hSJPlmoYCpSvHZiGKWzA6zj2ydlqRCOEkoNECxbNC W9PfsNB5N7+d8hkY16Iyj6tarsOcDCw+9r/35Pofb4Ir83VZ6yGCV2OYg4us8cP9hxE7 9NbgACoCmdiod2B2OgepFKQ9P/Qygjl10Huoc8zmJfN28K40qODpbEz5kiiLrcXRzLS6 761A== X-Gm-Message-State: AElRT7EqL6Te4AYEwMmeM8O5rk9iGiXjsSk4v14/Fn83xCDVTZNJJ4rN vAmL118EkpmllqnFgr8y4Vd5nA== X-Received: by 10.99.42.209 with SMTP id q200mr7784128pgq.379.1522400861331; Fri, 30 Mar 2018 02:07:41 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id w9sm10245345pfl.45.2018.03.30.02.07.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 30 Mar 2018 02:07:40 -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 , Konrad Rzeszutek Wilk , Liran Alon Subject: [PATCH v4 2/2] KVM: X86: Add Force Emulation Prefix for "emulate the next instruction" Date: Fri, 30 Mar 2018 02:06:44 -0700 Message-Id: <1522400804-4578-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1522400804-4578-1-git-send-email-wanpengli@tencent.com> References: <1522400804-4578-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 There is no easy way to force KVM to run an instruction through the emulator (by design as that will expose the x86 emulator as a significant attack-surface). However, we do wish to expose the x86 emulator in case we are testing it (e.g. via kvm-unit-tests). Therefore, this patch adds a "force emulation prefix" that is designed to raise #UD which KVM will trap and it's #UD exit-handler will match "force emulation prefix" to run instruction after prefix by the x86 emulator. To not expose the x86 emulator by default, we add a module parameter that should be off by default. A simple testcase here: #include #include #define HYPERVISOR_INFO 0x40000000 #define CPUID(idx, eax, ebx, ecx, edx) \ asm volatile (\ "ud2a; .ascii \"kvm\"; 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 Reviewed-By: Liran Alon Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Andrew Cooper Cc: Konrad Rzeszutek Wilk Cc: Liran Alon Signed-off-by: Wanpeng Li --- arch/x86/kvm/x86.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1eb495e..c619564 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -146,6 +146,9 @@ bool __read_mostly enable_vmware_backdoor = false; module_param(enable_vmware_backdoor, bool, S_IRUGO); EXPORT_SYMBOL_GPL(enable_vmware_backdoor); +static bool __read_mostly force_emulation_prefix = false; +module_param(force_emulation_prefix, bool, S_IRUGO); + #define KVM_NR_SHARED_MSRS 16 struct kvm_shared_msrs_global { @@ -4844,6 +4847,18 @@ int handle_ud(struct kvm_vcpu *vcpu) { enum emulation_result er; + if (force_emulation_prefix) { + 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) { + kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); + return emulate_instruction(vcpu, 0) == EMULATE_DONE; + } + } + er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); if (er == EMULATE_USER_EXIT) return 0; -- 2.7.4