Received: by 10.213.65.68 with SMTP id h4csp1960347imn; Thu, 29 Mar 2018 14:31:17 -0700 (PDT) X-Google-Smtp-Source: AIpwx48Qd8xadZSzS6S4jUvMzttV4m38qflmTYrlcSZBpTUAKy4bS0nodXt/goyJ4CfhNXQoTl2d X-Received: by 2002:a17:902:2006:: with SMTP id n6-v6mr10143472pla.150.1522359077289; Thu, 29 Mar 2018 14:31:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522359077; cv=none; d=google.com; s=arc-20160816; b=mrrYwl3Uz9Dt6dBH1OpSI05aYi7GTMPt87Qb438FYmKBIUHNxWxzuWAfnFIAD8Nihk 22uyy9Rms30xGGDvj8TDOyL+aTtl4B8XpnS8V2MU61PHygm+J0R0PxGBxqcnddImJ+vO bWTDDUx7Xe96Ou0RvT8+2brxpPPrD59N54i8C5RVx/F8shMEVA9KXO+5/3n0/LYmYKlp 7kyKnvP9I66tvG+KsQdc+rQMB+zlN+Fcd9rY0GCS36kDrxcf0/OrKl6KdBoWG6iudtXM AzMG12LWe0+ZXtgedlIuTIGC2nQW1taq9C223fykrUFskdwh1OtwI0wdY0f6jcDPZnwS DSnw== 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=/1typvl/jHuMGtDgimCuyvTOPSWJh8lVVBZs7rKHYZc=; b=dO6ctAqR+0OUSp8+HBYVAODaSc21+/9T850455ZoRxPSvQHeKCVnJLbckfe9ID9vRc 5eOSBpX2r+G6HAz1xaSu7SujjsuB5PHP45nnclUCHmcObfrIOitNjottt+J9WR1HNqlr p43i9sO6GH4MW+R+8FUdts0GgXdKlIgdl7S6RFju7JuDJ9raypuPXcpIopXebsZSdD/S nIpjAlMHrnxkYnVS+UAXB/K8okhklG8Yd6/9yz17EEbpDAPkMaPnwuln6S3Ao/7oZFx5 u5IzlApQSXDZqnO9EMhlkfKwpji0eSAsbXmn7u8+ytCbhUJlP/dmOutQjzfzfx1cawoE +65w== 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 y136si5166904pfg.81.2018.03.29.14.31.03; Thu, 29 Mar 2018 14:31:17 -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 S1752787AbeC2V3j (ORCPT + 99 others); Thu, 29 Mar 2018 17:29:39 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36840 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752594AbeC2V3g (ORCPT ); Thu, 29 Mar 2018 17:29:36 -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 3FEF08182D01; Thu, 29 Mar 2018 21:29:36 +0000 (UTC) Received: from flask (unknown [10.43.2.80]) by smtp.corp.redhat.com (Postfix) with SMTP id C71CC94585; Thu, 29 Mar 2018 21:29:33 +0000 (UTC) Received: by flask (sSMTP sendmail emulation); Thu, 29 Mar 2018 23:29:33 +0200 Date: Thu, 29 Mar 2018 23:29:33 +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 v3 2/2] KVM: X86: Add Force Emulation Prefix for "emulate the next instruction" Message-ID: <20180329212932.GA27107@flask> References: <1522198134-2709-1-git-send-email-wanpengli@tencent.com> <1522198134-2709-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: <1522198134-2709-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.8]); Thu, 29 Mar 2018 21:29:36 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 29 Mar 2018 21:29:36 +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-27 17:48-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 > Cc: Paolo Bonzini > Cc: Radim Krčmář > Cc: Andrew Cooper > Cc: Konrad Rzeszutek Wilk > Cc: Liran Alon > Signed-off-by: Wanpeng Li > --- > diff --git 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 { > @@ -4843,8 +4846,21 @@ EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); > int handle_ud(struct kvm_vcpu *vcpu) > { > enum emulation_result er; > + int emulation_type = EMULTYPE_TRAP_UD; > + > + 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) { > + 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) The code afterwards is going to inject an #UD if the emulation failed. I think that preserving the cpu state and forwarding the emulation failure to userspace would be more useful. The change would probably be best as: 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; } Looks great otherwise, thanks. (We want to use this in emulate.c kvm-unit-test that currently fails because of a hack that doesn't work anymore.)