Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3417959ybl; Mon, 19 Aug 2019 18:35:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqxEn/pRBeaRpCOgkeVD74gV+PWAbDM2K5l6gDtuK51rMY4OTQuD7GQDVY/ZaeA6ttFBTA38 X-Received: by 2002:a62:52d0:: with SMTP id g199mr27727575pfb.32.1566264954877; Mon, 19 Aug 2019 18:35:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566264954; cv=none; d=google.com; s=arc-20160816; b=BqwZ8uWzpRRH4ZnewViwmj3Zv70g+iEDbyi8UzNexbePMvccqbDGpfSgUWTn1HOGVE 09g9HJYSZaqDhqr6TaR3W3+/b4Xsmz5eK0v3ef6wZ7FjGpSUjboBcUw+aLIrS4y+tfgS 8x2pz1Erf3wySWD5iaxKeeZyI9aVAuzGp5mQabHljdbk1VZAU+RJKP3/mob/OtfwKVm5 QYOFkpEL1ww1jFga319j7rqM/tmwxiclLFVshj8ujc1VHFiFjPn12+Dg+97eJdwqvohK rbi0aOe+goaBwF3Ye2tpwp/JQNCtpY33Bjspstdvj2wiv1bA4DEZOcU1nV4wFumcFqlT FLQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=cJmuTIxhG3GG+PhQj8RAHqnRG0cdBDwfyABQZMFJ7lY=; b=ENnXNKQ5IEw7Wx/4yXBTiv9rBnuWpjLTLVhVfXhp/Oz19INyf3TAEHcOs245fG45JM Q8eZzisfEJN47g8xZrBPgr27RtS8NkrL1YOJ3+WLV8SquNKVm68sncPjNFE6W57oY7qz 9DZOTrKgnR/u+FEXWGCDhBScweSOEgPsqcCfLt7LxAy3l8qZnmt1zkAZ4E88k8VOvt0h yHz9xJ+ZmCDX51Wg7ngpnAtulk+alXjO28xED4N+CxhWGkM72kTMiijo8h+4W02FnRMS hmqu1yl8HI99WNbvxG9PNY7A719EtUX3x7F/qLB9+1niwSTThc6FBuDh1cS1jaXT7e7y 7rVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DCSYnGps; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u29si11220772pgm.325.2019.08.19.18.35.38; Mon, 19 Aug 2019 18:35:54 -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=@kernel.org header.s=default header.b=DCSYnGps; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728950AbfHTBeX (ORCPT + 99 others); Mon, 19 Aug 2019 21:34:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:52270 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728786AbfHTBeW (ORCPT ); Mon, 19 Aug 2019 21:34:22 -0400 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0474B23427 for ; Tue, 20 Aug 2019 01:34:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566264862; bh=t2kfzl1Im6UkgjLX7Fx6EjFHl9UHAnZYRidAq5Pt5wY=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=DCSYnGps/sQTNHtTU7NG9vQIqAUmnw1PuIaB5SzjSHQYTUhxU8XQDXYIBHrNGhfdx 16C4JRzenLAKrHE3ezskVW7UV330X/xa3+esMEuOUKflR8A/w7sVf1/aoDvffuWGgV jSAxXYlVvPxNGMKnVlaaC3TrJTTH0qkGgGOIDkE0= Received: by mail-wr1-f54.google.com with SMTP id k2so10588317wrq.2 for ; Mon, 19 Aug 2019 18:34:21 -0700 (PDT) X-Gm-Message-State: APjAAAWP6pxnQvGXF+k6xLCB1iF0AT7qLvKD4YqfF/om8i50Xsy8qiru s8xEKJhVrLHmkTO/J58/VNdIMj1v57fU6jlT9hmfnQ== X-Received: by 2002:adf:82cd:: with SMTP id 71mr26128200wrc.265.1566264860344; Mon, 19 Aug 2019 18:34:20 -0700 (PDT) MIME-Version: 1.0 References: <20190727055214.9282-1-sean.j.christopherson@intel.com> <20190727055214.9282-9-sean.j.christopherson@intel.com> <20190730024940.GL21120@linux.intel.com> <25BBDA64-1253-4429-95AF-5D578684F6CC@amacapital.net> <20190819220150.GE1916@linux.intel.com> In-Reply-To: <20190819220150.GE1916@linux.intel.com> From: Andy Lutomirski Date: Mon, 19 Aug 2019 18:34:07 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH 08/21] KVM: x86: Add kvm_x86_ops hook to short circuit emulation To: Sean Christopherson Cc: Andy Lutomirski , Paolo Bonzini , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , X86 ML , Jarkko Sakkinen , Joerg Roedel , "H. Peter Anvin" , kvm list , LKML , linux-sgx@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 19, 2019 at 3:01 PM Sean Christopherson wrote: > > On Thu, Aug 15, 2019 at 05:47:12PM -0700, Andy Lutomirski wrote: > > > > > > >> On Jul 29, 2019, at 7:49 PM, Sean Christopherson wrote: > > >> > > >> On Sat, Jul 27, 2019 at 10:38:03AM -0700, Andy Lutomirski wrote: > > >> On Fri, Jul 26, 2019 at 10:52 PM Sean Christopherson > > >> wrote: > > >>> > > >>> Similar to the existing AMD #NPF case where emulation of the current > > >>> instruction is not possible due to lack of information, virtualization > > >>> of Intel SGX will introduce a scenario where emulation is not possible > > >>> due to the VMExit occurring in an SGX enclave. And again similar to > > >>> the AMD case, emulation can be initiated by kvm_mmu_page_fault(), i.e. > > >>> outside of the control of the vendor-specific code. > > >>> > > >>> While the cause and architecturally visible behavior of the two cases > > >>> is different, e.g. Intel SGX will inject a #UD whereas AMD #NPF is a > > >>> clean resume or complete shutdown, the impact on the common emulation > > >>> code is identical: KVM must stop emulation immediately and resume the > > >>> guest. > > >>> > > >>> Replace the exisiting need_emulation_on_page_fault() with a more generic > > >>> is_emulatable() kvm_x86_ops callback, which is called unconditionally > > >>> by x86_emulate_instruction(). > > >> > > >> Having recently noticed that emulate_ud() is broken when the guest's > > >> TF is set, I suppose I should ask: does your new code function > > >> sensibly when TF is set? > > > > > > Barring a VMX fault injection interaction I'm not thinking of, yes. The > > > SGX reaction to the #UD VM-Exit is to inject a #UD and resume the guest, > > > pending breakpoints shouldn't be affected in any way (unless some other > > > part of KVM mucks with them, e.g. when guest single-stepping is enabled). > > > > What I mean is: does the code actually do what you think it does if TF is > > set? Right now, as I understand it, the KVM emulation code has a bug in > > which some emulated faults also inject #DB despite the fact that the > > instruction faulted, and the #DB seems to take precedence over the original > > fault. This confuses the guest. > > Yes. The proposed change is to inject the #UD instead of calling into the > emulator, and by inspection I've verified that all code that injects a #DB > is either contained within the emulator or is mutually exclusive with an > intercepted #UD. It's a qualified yes because I don't have an actual > testcase to verify my literacy. I'll look into adding a test, either to > the selftest/x86/sgx or to kvm-unit-tests. I wrote one, and it fails: # ./tools/testing/selftests/x86/syscall_arg_fault_32 [RUN] SYSENTER with invalid state [OK] Seems okay [RUN] SYSCALL with invalid state [SKIP] Illegal instruction [RUN] SYSENTER with TF and invalid state [OK] Seems okay [RUN] SYSCALL with TF and invalid state [WARN] Got stuck single-stepping -- you probably have a KVM bug emulate_ud() is buggy.