Received: by 10.213.65.68 with SMTP id h4csp106548imn; Tue, 3 Apr 2018 16:33:33 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+ORTH8UznUttBTzby5a7dRmNwvW2oGXXITX4yjwTkgjrEwlnIeZVCpE2sHAFaShy785+dK X-Received: by 2002:a17:902:464:: with SMTP id 91-v6mr16022044ple.126.1522798413896; Tue, 03 Apr 2018 16:33:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522798413; cv=none; d=google.com; s=arc-20160816; b=LiaMrgu+v5x+ZpL1PAigRy+1DBT4Qx/mAmqxQlxHzYfmku8Ew1dC9iF7GI8or/xcpv Y2a8w9F6qzmXWCmhzMwa9IpxKuZrryN5Tb9nRlzVIhtmDWXR0L3qK4JI0pW/s4i0tmfC Y+utORuKFDMqlEZJlOjfL1+Dj+8X6VGhGqFKHBAjDx16ntpJ7IfPqrVsfiQ3PpUu96wa wEHi6AafJQaHBvuJa1aoR7xJuVKvq5ThT9/LXnPsRZs+xnzYA72xp1O/BJcoPgqJh1gd o11ywj8kryDg5kAhD9G+y6Gq0wX+y3ro65HC2pBGFSUxQPv9UP+vUZX8hTcMTvY7e69O QqrQ== 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:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=dBbeXKjtoVYxfRHwyXYa9eHbfG/YGL0A+e47dwLGDPM=; b=AYQ0gh5azKFd4Z7b3lUxdGBHKlv4fMU8hlTj2qk/GG+5465NQVboAX8mNwBh4KdB3G +bsK4v3MlPQiXmwWh2VZo9wHpmwrn0ROWyPIb5PTcGvLSdWGY4nEVF5CDTTBGzJN5Hrd RnmKCAPPRSviGvr8YwWLCyUqrgdP4NjiHwdAR2YHEJ1AP2X3au8ZBAzoxvIz7q9UDQHs JGjuqPurrnFr3CMvu+oo/8q2jaHIHIeJhHiDpl3ZVn56f0MiW6PuyeIlcIz1Xm8Y9gEy HSlkbuR9B/3lDTDnuGMxd+vu5Uhwk5oG0y2cKiBO0/lSRife2welM1bdDnwmydJmGhAf mP6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ijKZBR6j; 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 3-v6si1832671pls.387.2018.04.03.16.33.19; Tue, 03 Apr 2018 16:33:33 -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=ijKZBR6j; 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 S1755885AbeDCXas (ORCPT + 99 others); Tue, 3 Apr 2018 19:30:48 -0400 Received: from mail-ot0-f195.google.com ([74.125.82.195]:41963 "EHLO mail-ot0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755823AbeDCXap (ORCPT ); Tue, 3 Apr 2018 19:30:45 -0400 Received: by mail-ot0-f195.google.com with SMTP id i28-v6so21351693otf.8; Tue, 03 Apr 2018 16:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=dBbeXKjtoVYxfRHwyXYa9eHbfG/YGL0A+e47dwLGDPM=; b=ijKZBR6jJc8VFkeAIF33wfReMhr0nIil66kbAam91ttTNCmBpWmUxuGnB28ggcBoqz AJJIHoIFnL7SauHQ1Gp+44JiOHm5GXXRUiilL5QcCoZn4Ml7ciyjDa4VaceEMpY1C4eh X1+rbtKX7aeuPf3i+/aMuW9ZVVDIVOGfxuwkZe58u9IpS0zJycqg+0R5l16XfJpE3SPO bdDcG5RUZ6LKfd2ululVjrucF0syGTUvnijtdc9TWDN/9ocRytM69U9ev9uoeytU59dG PwY/9tujpGVkIsxNWdrEnjDKzNDWJZ5XHSTvW9hifAKdtjO4P/ONjKvULaSftRkkdsa3 fp6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=dBbeXKjtoVYxfRHwyXYa9eHbfG/YGL0A+e47dwLGDPM=; b=i4FiRb78vzponkS0GZqf+R0Wypft7U9Jg7EzX+gPk7pvQuQBPiW/xVBJ3rcVtyrXJs OngoS2EVeaVzL+CR+5ltL4ZqVzRqPyCesz/oqQBuTi3A4ds809goFoWKurJXwAQj/VHC NqrwB7rOmCMiq8GkOCsj/iO4MTMLQ8JpVZApRPRt1XpvOr9w3sSuBzbTGWwSrzu2jj5b RthBscx4uU+1zK8XSUTRzRIAWp1XLCW5/NcIUgbS1tAJ2w0/lJc2PS22U+BUhYQcU8Vy shrfhXYcJmNA8m3bMWg+uM+Uwc2HFMc8FXQrsUmy35Y+EE5bpefIHcshHojM/EmRrr6c frMQ== X-Gm-Message-State: ALQs6tA0OpdU7WrTNcoI333HVc6z59q/neUGzo0hq3uyGJ4fqA02Auon RvvJciphpkp/dKB0QdtLLldlFCem8fowaegKKX8= X-Received: by 2002:a9d:5503:: with SMTP id l3-v6mr4929375oth.389.1522798244744; Tue, 03 Apr 2018 16:30:44 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.210.78 with HTTP; Tue, 3 Apr 2018 16:30:44 -0700 (PDT) In-Reply-To: <20180403192407.GA7351@flask> References: <1522400804-4578-1-git-send-email-wanpengli@tencent.com> <1522400804-4578-3-git-send-email-wanpengli@tencent.com> <20180403192407.GA7351@flask> From: Wanpeng Li Date: Wed, 4 Apr 2018 07:30:44 +0800 Message-ID: Subject: Re: [PATCH v4 2/2] KVM: X86: Add Force Emulation Prefix for "emulate the next instruction" To: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= Cc: LKML , kvm , Paolo Bonzini , Andrew Cooper , Konrad Rzeszutek Wilk , Liran Alon Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2018-04-04 3:24 GMT+08:00 Radim Kr=C4=8Dm=C3=A1=C5=99 : > 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 emul= ator >> (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-hand= ler will >> match "force emulation prefix" to run instruction after prefix by the x8= 6 emulator. >> To not expose the x86 emulator by default, we add a module parameter tha= t 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" \ >> :"=3Db" (*ebx), "=3Da" (*eax), "=3Dc" (*ecx), "=3Dd" (*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) =3D ebx; >> *(unsigned int *)(string + 4) =3D ecx; >> *(unsigned int *)(string + 8) =3D edx; >> >> string[12] =3D 0; >> if (strncmp(string, "KVMKVMKVM\0\0\0", 12) =3D=3D 0) >> printf("kvm guest\n"); >> else >> printf("bare hardware\n"); >> } >> >> Suggested-by: Andrew Cooper >> Reviewed-By: Liran Alon >> Cc: Paolo Bonzini >> Cc: Radim Kr=C4=8Dm=C3=A1=C5=99 >> 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 =3D false; >> module_param(enable_vmware_backdoor, bool, S_IRUGO); >> EXPORT_SYMBOL_GPL(enable_vmware_backdoor); >> >> +static bool __read_mostly force_emulation_prefix =3D 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). Ok, will fallback to normal emulate_ud when this fail. > > With that, or even without as we're talking about a feature that has no > place in any production system, > > Reviewed-by: Radim Kr=C4=8Dm=C3=A1=C5=99 Thanks Radim. :) Regards, Wanpeng Li