Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1330443imw; Tue, 5 Jul 2022 07:40:27 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vyMjFDqiNBM/amDbdTOEZm0sPd3bDaS1Bf42w6YCk0C2WoMpTwSXWLDtLmnZikt6882Qlu X-Received: by 2002:a63:951c:0:b0:40c:f82e:7d36 with SMTP id p28-20020a63951c000000b0040cf82e7d36mr29905201pgd.201.1657032027232; Tue, 05 Jul 2022 07:40:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657032027; cv=none; d=google.com; s=arc-20160816; b=xpjRw9HNURV6+37gU2LBmAEckyWHrIaef/vUE+lKoc9xD8sGpIIo3uyPq9FbvVRd20 BGnA/Ripzysu5zA+2yMJLeoGHiAYBVgnSOAS6SLZ7yhOzJqQ3KrQDHxn88QqkpGLQx51 XbfMupi2CCPM55pRzK+zqVbiaMcQ3OxUvR7N3EWRvYi1Dp3/kyFLT0k3q9IQqcTYzV1Z eaZs6fKRQ5NgP7VFybBTQpd/daeKFmXe00TGUzpukoXowjYvkwQbpNPWtIdqr85RKqab c0dw4k9MF0o3EwUFbVAabo2F9tBxgW8/f6faS0vi+W4OeKtH1ZS95mf9Wwe0rQU8zk5w OYqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=y21vIIappxvjeYgEzXDVagkcbBuiUGzrS1YtS/nblTA=; b=GPKeEbFD31H6GjjvdgphSiBlPcgTDyiC1k+ZG7T7udvsVIcBd1Pa4DgHY4OvGwrpU9 icIK2xtfZe9EV4ASq6vzFbAZkFFJOFP/i6LQHbVb7QRxAsavp11JzeSGoZyGmTH/cqxn +Hb91XMf29UgDCGmYFmLXrltpltMAdPl+bhP5uteTA3s7sXad1X/xF3wJHNfTjvAomMp zyfTDT4qibfArsHJkJBz+76vQ/G19TCz5Nae3TqZn5uv5cV+uahyzjtRQBTshYx++Bbt F3pL2nqW5KlrpurPd7wk2dsQ8gaMpSp034MLiw8U0c6CnL9mPiYWmcIGmhcHvdH/fFBo 4VoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R3o3fGXg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a17090311c400b0016b8167e375si17973092plh.170.2022.07.05.07.40.14; Tue, 05 Jul 2022 07:40:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R3o3fGXg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230043AbiGEN6L (ORCPT + 99 others); Tue, 5 Jul 2022 09:58:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232816AbiGEN5Z (ORCPT ); Tue, 5 Jul 2022 09:57:25 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B712D1A392 for ; Tue, 5 Jul 2022 06:40:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657028420; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y21vIIappxvjeYgEzXDVagkcbBuiUGzrS1YtS/nblTA=; b=R3o3fGXgqZYXbC3azpMnEBLhnEWGREqKvn1ZqAG6lRyL2eqMPQ8Nv0rI2t3Duui+VUkVPU 47+aobvTUue2IcHSGd1ZJcZG3g7sPN3rBeQv8rmiFVLNrmTX59TRbAzcRKpFQZarTqmVYu uxi73f6QIAg6lCikoCfR1AdtJCm2EKE= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-599-ys-V-Wo-OZGkpEbGEPQ_Pw-1; Tue, 05 Jul 2022 09:40:19 -0400 X-MC-Unique: ys-V-Wo-OZGkpEbGEPQ_Pw-1 Received: by mail-wr1-f69.google.com with SMTP id u9-20020adfa189000000b0021b8b3c8f74so1966849wru.12 for ; Tue, 05 Jul 2022 06:40:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=y21vIIappxvjeYgEzXDVagkcbBuiUGzrS1YtS/nblTA=; b=y37grKuoCRDsL4ijs2hv5H3tfJhZ26NSPPk+5N3seLUe4c9czUyMGWteIGsH5c8lnZ 2PAZwgVpv6dnP6jGnbOAEgiUzeqawBKtuHjYwMU9IH7hM/JVfczK0xwbHTWFFxCUHb6i DgzyJIPdq8Ivd5CHIdAPd/SukBMNpqd4HXfAqnqSKuAbh9Rj691shoqNML+z23yXd8rH 9LuUylzAJdJVyyneZ/3MybnW31cfD2OwvUvZtIRkb5/vLkEEUA7yAWpK3IUvTTRQ+ddy HzGmiShwhcwnwh4IErBBaqXqT7jrB9bPfWYVUv1IPVW4pkfCaptLBeSyaxfBpgEuYX6N fYew== X-Gm-Message-State: AJIora8TPLt765FOg3OgggPKHW3BqHRggvuorIh4yh2oFrZYJw43nEd/ vqLAvH4Xv7+SxDBWqQ56x6CAXy5/QvCzHXiRMwm3EmT7BIvwa5yWr+efAPAf+qj0rvL2c6fZkfc Xtz05w+uneivMTGPGuW09xag2 X-Received: by 2002:adf:9d92:0:b0:21d:66c4:e311 with SMTP id p18-20020adf9d92000000b0021d66c4e311mr12870350wre.575.1657028418170; Tue, 05 Jul 2022 06:40:18 -0700 (PDT) X-Received: by 2002:adf:9d92:0:b0:21d:66c4:e311 with SMTP id p18-20020adf9d92000000b0021d66c4e311mr12870330wre.575.1657028417952; Tue, 05 Jul 2022 06:40:17 -0700 (PDT) Received: from [10.35.4.238] (bzq-82-81-161-50.red.bezeqint.net. [82.81.161.50]) by smtp.gmail.com with ESMTPSA id f4-20020adfe904000000b0021d639e3daasm8888774wrm.30.2022.07.05.06.40.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Jul 2022 06:40:17 -0700 (PDT) Message-ID: <646d2f04bbc7530339ca02dcf5eeb3b5e298a1c2.camel@redhat.com> Subject: Re: [PATCH v2 11/11] KVM: x86: emulator/smm: preserve interrupt shadow in SMRAM From: Maxim Levitsky To: Jim Mattson Cc: kvm@vger.kernel.org, Sean Christopherson , x86@kernel.org, Kees Cook , Dave Hansen , linux-kernel@vger.kernel.org, "H. Peter Anvin" , Borislav Petkov , Joerg Roedel , Ingo Molnar , Paolo Bonzini , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li Date: Tue, 05 Jul 2022 16:40:15 +0300 In-Reply-To: <289c2dd941ecbc3c32514fc0603148972524b22d.camel@redhat.com> References: <20220621150902.46126-1-mlevitsk@redhat.com> <20220621150902.46126-12-mlevitsk@redhat.com> <42da1631c8cdd282e5d9cfd0698b6df7deed2daf.camel@redhat.com> <289c2dd941ecbc3c32514fc0603148972524b22d.camel@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-5.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2022-07-05 at 16:38 +0300, Maxim Levitsky wrote: > On Thu, 2022-06-30 at 09:00 -0700, Jim Mattson wrote: > > On Wed, Jun 29, 2022 at 11:00 PM Maxim Levitsky wrote: > > > > > > On Wed, 2022-06-29 at 09:31 -0700, Jim Mattson wrote: > > > > On Tue, Jun 21, 2022 at 8:09 AM Maxim Levitsky wrote: > > > > > When #SMI is asserted, the CPU can be in interrupt shadow > > > > > due to sti or mov ss. > > > > > > > > > > It is not mandatory in  Intel/AMD prm to have the #SMI > > > > > blocked during the shadow, and on top of > > > > > that, since neither SVM nor VMX has true support for SMI > > > > > window, waiting for one instruction would mean single stepping > > > > > the guest. > > > > > > > > > > Instead, allow #SMI in this case, but both reset the interrupt > > > > > window and stash its value in SMRAM to restore it on exit > > > > > from SMM. > > > > > > > > > > This fixes rare failures seen mostly on windows guests on VMX, > > > > > when #SMI falls on the sti instruction which mainfest in > > > > > VM entry failure due to EFLAGS.IF not being set, but STI interrupt > > > > > window still being set in the VMCS. > > > > > > > > I think you're just making stuff up! See Note #5 at > > > > https://sandpile.org/x86/inter.htm. > > > > > > > > Can you reference the vendors' documentation that supports this change? > > > > > > > > > > First of all, just to note that the actual issue here was that > > > we don't clear the shadow bits in the guest interruptability field > > > in the vmcb on SMM entry, that triggered a consistency check because > > > we do clear EFLAGS.IF. > > > Preserving the interrupt shadow is just nice to have. > > > > > > > > > That what Intel's spec says for the 'STI': > > > > > > "The IF flag and the STI and CLI instructions do not prohibit the generation of exceptions and nonmaskable inter- > > > rupts (NMIs). However, NMIs (and system-management interrupts) may be inhibited on the instruction boundary > > > following an execution of STI that begins with IF = 0." > > > > > > Thus it is likely that #SMI are just blocked when in shadow, but it is easier to implement > > > it this way (avoids single stepping the guest) and without any user visable difference, > > > which I noted in the patch description, I noted that there are two ways to solve this, > > > and preserving the int shadow in SMRAM is just more simple way. > > > > It's not true that there is no user-visible difference. In your > > implementation, the SMI handler can see that the interrupt was > > delivered in the interrupt shadow. > > Most of the SMI save state area is reserved, and the handler has no way of knowing > what CPU stored there, it can only access the fields that are reserved in the spec. I mean fields that are not reserved in the spec. Best regards, Maxim Levitsky > > Yes, if the SMI handler really insists it can see that the saved RIP points to an > instruction that follows the STI, but does that really matter? It is allowed by the > spec explicitly anyway. > > Plus our SMI layout (at least for 32 bit) doesn't confirm to the X86 spec anyway, > we as I found out flat out write over the fields that have other meaning in the X86 spec. > > Also I proposed to preserve the int shadow in internal kvm state and migrate > it in upper 4 bits of the 'shadow' field of struct kvm_vcpu_events. > Both Paolo and Sean proposed to store the int shadow in the SMRAM instead, > and you didn't object to this, and now after I refactored and implemented > the whole thing you suddently do. > > BTW, just FYI, I found out that qemu doesn't migrate the 'shadow' field, > this needs to be fixed (not related to the issue, just FYI). > > > > > The right fix for this problem is to block SMI in an interrupt shadow, > > as is likely the case for all modern CPUs. > > Yes, I agree that this is the most correct fix.  > > However AMD just recently posted a VNMI patch series to avoid > single stepping the CPU when NMI is blocked due to the same reason, because > it is fragile. > > Do you really want KVM to single step the guest in this case, to deliver the #SMI? > I can do it, but it is bound to cause lot of trouble. > > Note that I will have to do it on both Intel and AMD, as neither has support for SMI > window, unless I were to use MTF, which is broken on nested virt as you know, > so a nested hypervisor running a guest with SMI will now have to cope with broken MTF. > > Note that I can't use the VIRQ hack we use for interrupt window, because there > is no guarantee that the guest's EFLAGS.IF is on. > > Best regards,    >         Maxim Levitsky > > > > > > > > > As for CPUS that neither block SMI nor preserve the int shadaw, in theory they can, but that would > > > break things, as noted in this mail > > > > > > https://lore.kernel.org/lkml/1284913699-14986-1-git-send-email-avi@redhat.com/ > > > > > > It is possible though that real cpu supports HLT restart flag, which makes this a non issue, > > > still. I can't rule out that a real cpu doesn't preserve the interrupt shadow on SMI, but > > > I don't see why we can't do this to make things more robust. > > > > Because, as I said, I think you're just making stuff up...unless, of > > course, you have documentation to back this up. > > >