From patchwork Fri Mar 30 09:06:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10317437 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0FC3860212 for ; Fri, 30 Mar 2018 09:09:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F3B9B2A532 for ; Fri, 30 Mar 2018 09:09:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DA9642A566; Fri, 30 Mar 2018 09:09:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6B12A2A532 for ; Fri, 30 Mar 2018 09:09:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751251AbeC3JJL (ORCPT ); Fri, 30 Mar 2018 05:09:11 -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-Google-Smtp-Source: AIpwx49etu5qBp6iXnO0HmaS4T4g1Sjmbk/ey5k9onOl0YMGgAY37eBFHZufUQ0LChur2x8gnK0hQg== 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 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Radim Krčmář --- 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;