Received: by 10.213.65.68 with SMTP id h4csp3887326imn; Tue, 3 Apr 2018 12:25:41 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/IQQRZU5jR3D4yUc1gB/Rmd5zMK8lNF62wGXNqDxRXnuPxnNXXjzW8o4uvLQExr9DoeHZP X-Received: by 2002:a17:902:aa48:: with SMTP id c8-v6mr15010468plr.361.1522783541759; Tue, 03 Apr 2018 12:25:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522783541; cv=none; d=google.com; s=arc-20160816; b=fKrLArB+0vVAHGcTpzbq8qAfkV3vh2RzsIKQTxgwtTmZAuA21YjBE5EJaafZmmaxSw JgBd5CQXfaL8oUovWGxJHSTU34U3kCQO1qc76pjgYbiLfofMD9C+4BTGzTNjypt+UTyc eSwI2YgLBYf9OIgqJ3Y3vVB1rfhnoCcTbfUKBQER/dVx9gYWzsOpEl2uUxUk6vqcaN0N jC63QTLwyk5vmWpQJqOs4T2p46sGU8R8+V56LEZUn02LtZpx0BODlNC7hmspTHOTSyII hCzX5I7JBaI71neiIhtSl99TNaO5CkAicNNeiKaTsaNEzKWVJ/0qSupm0zw3duQnjsiv EsiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=B4Q+gmUq2F7jHn+Fn95gp42bvcyMV5JMcLS5HSUOtTA=; b=jiJDAmXkVR7dRAE3iSF9Gxt1uXEY++0Hu73KQEgAvhK1tPcCoAEmx0Le1EdFfr+k6H hsv6SHkm6AMKQyqWh99pZx0gd+yrQDdlWCpRZCTnZbVntFgixuR1MPmdFuD+qZkAG9YD NP/yPVrTySZNTIrLY6sb829msnXlZh00w7xgfUkTQWbyoRzRP6BkcTdmCN1aw6oXH2zc NPwyxOt8ZKhFzrtwmtKdJnvewsTp2Rx/NIE/C5uTfOSe3RaCuUnw7y3FFH/AGfItWACR nI7ZrcnBUGdu1Du9Hr62ddn8tudVV2nb4JQoHGqVDOi0VxYqYwvjG2u/AMmEzWfNDdgO mVRg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k9-v6si1164535plt.438.2018.04.03.12.25.27; Tue, 03 Apr 2018 12:25:41 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752991AbeDCTYO (ORCPT + 99 others); Tue, 3 Apr 2018 15:24:14 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:38304 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752230AbeDCTYM (ORCPT ); Tue, 3 Apr 2018 15:24:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3BFE8402314E; Tue, 3 Apr 2018 19:24:12 +0000 (UTC) Received: from flask (unknown [10.43.2.80]) by smtp.corp.redhat.com (Postfix) with SMTP id 032867C56; Tue, 3 Apr 2018 19:24:08 +0000 (UTC) Received: by flask (sSMTP sendmail emulation); Tue, 03 Apr 2018 21:24:08 +0200 Date: Tue, 3 Apr 2018 21:24:08 +0200 From: Radim =?utf-8?B?S3LEjW3DocWZ?= To: Wanpeng Li Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Andrew Cooper , Konrad Rzeszutek Wilk , Liran Alon Subject: Re: [PATCH v4 2/2] KVM: X86: Add Force Emulation Prefix for "emulate the next instruction" Message-ID: <20180403192407.GA7351@flask> References: <1522400804-4578-1-git-send-email-wanpengli@tencent.com> <1522400804-4578-3-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1522400804-4578-3-git-send-email-wanpengli@tencent.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 03 Apr 2018 19:24:12 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 03 Apr 2018 19:24:12 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'rkrcmar@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2018-03-30 02:06-0700, Wanpeng Li: > 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); Looking at it again, we should skip the following check if the call failed (the sig is undefined in that case). With that, or even without as we're talking about a feature that has no place in any production system, Reviewed-by: Radim Krčmář > + 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 >