Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp2908998imw; Wed, 6 Jul 2022 13:54:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sT9XUsDwH3IfkL2QDyRbVAzuq42zc8KeniEtiQkbCigRofEbTJxObKOUFDiDjZ0lcfmCBb X-Received: by 2002:aa7:d60a:0:b0:43a:5795:b729 with SMTP id c10-20020aa7d60a000000b0043a5795b729mr24419942edr.230.1657140874976; Wed, 06 Jul 2022 13:54:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657140874; cv=none; d=google.com; s=arc-20160816; b=iO60s12vQvCXjM35SqG4XET+bOLKT5WYkiij3k8aSfFrkvT+afk0iCnQuV251OEkBK NrwknGpf+FkY1BBndrdGfmggMb0jMDAhB6kiZsUp3YLiqe+FNmX27aN6SRxQWsqTkHKN Z4O1uYzDb+/ucUYyTpunr9mlQhSlGueDLhizZEmA+Y78eGV/C7EW4Kxp6PA5Z99inUPM VF045Cw6VL2ZLFNIfq4iBdTSelxR/cM/Inv0c0qlQNzsQBmE3wrZj0lwKMDmU46N6N5B I34AY6W/1kIgwPwdo2PKxEgJqOVtbtAq+u0h2HMmllJ6V6WVi1JMipdnSHlQ/CJM1rpo RJWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=V4iWsM3zDJ62xZk9JpVza3Ty+C3Hx5QJjdlOMIUkfEI=; b=U2bCVzA6fW9/fI7KeTvzwAOrvteRv/hd1U1pQAKZYdnh8UZvL1+9+Bqpn/h4arUzeg 0WWv/hkXJsB0sA+UWKC48K1dlY6Vq6YvfZ7IIjZkSnS6HFzXrqIyAm5YpPl0E9esFqX/ ojwHosgMpI28Wi5glE5XHDmjsp2CsLult2jrbR/U17YKwuw0T4SLQeVc+nVHHv0bQ1EV LDTV/DdiZnFig43KfVVABI2Ud1AlPvexBWxHiQnuugeQ4ABMoG5VbSB2AoRl8HlrGHCw 6wQ7rOWWMnN12IoAOCaz5bTfjGz9J8KsQUpct347R4owMAhu6nv/QRx6ip9HCS5gqZZx xjmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EDdwTSV2; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s7-20020a056402520700b0043a8aa80c05si4177022edd.399.2022.07.06.13.54.08; Wed, 06 Jul 2022 13:54:34 -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=@google.com header.s=20210112 header.b=EDdwTSV2; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233570AbiGFUiv (ORCPT + 99 others); Wed, 6 Jul 2022 16:38:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231622AbiGFUiu (ORCPT ); Wed, 6 Jul 2022 16:38:50 -0400 Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84C211C11A for ; Wed, 6 Jul 2022 13:38:47 -0700 (PDT) Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-10bffc214ffso12126035fac.1 for ; Wed, 06 Jul 2022 13:38:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=V4iWsM3zDJ62xZk9JpVza3Ty+C3Hx5QJjdlOMIUkfEI=; b=EDdwTSV2Gu4vOdeLlT31P2HN/4NychO6ng/goB8uYDSxRqx5+20DP6v25Jr2mVjy14 ffhPDZJ7bWPeYiCsTfSOEF5WOm1Wxu2lXrMfGSYjEXgZc59MvMsUXgTcw4J0d6fuNzsp Mch07cuS0FZGE2DmmohXSTThXt/yjeW2guxvrmxiYla0Z9dtCY/5FGQ07LtKY5FACfUZ PhL0i7l/whYaj1re0ZgZkOsFkMpBA8KUnbu5ceOH9v/mS7aYoZwi+g1O6esuCjwfo0KH APsWNPcJNtLUQ53HjSWKhkIfgbVTevM4BT3D44v7miQ7aeRGtZS8e3TvTaqaLhAqc9x/ NWuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=V4iWsM3zDJ62xZk9JpVza3Ty+C3Hx5QJjdlOMIUkfEI=; b=paRAGeDJywgRNlM9e9WHY9N/pEG1rD4GFjIZP5Uib3qTd8uqHa6DzniTtad8NEzRQ9 nwjublAuu4fwqvTHtqcQjRo/Kz36DUOlY/xlPhLUZ3VH8P2Mm8YLQ6Ktb/Rb+tuucnrJ 3zMeQAD0SDYgIFx2IuSc/qo8V2Jl32SwlG6bdQP/Xe/yQ2d6T0G/O1Zm1JQ45e2kn02t LCaiBgTFR6ezqnYXhUFbvc08A20jZ3BF+RltRLijSgCr8T0S/hQa64eB0x6+Ni5l1Kww jem97K8qm/UoPxmKZUIRx+Y8I1mb8et291M098dwAxtuwt1TdE/EbypURs0zgFd+c6se CXow== X-Gm-Message-State: AJIora+GOD6lM+LuuUKPhoqEXyuno3rmizPCU/9pBKR+hCrqKkdLHP9G 2sFgxqJyd2S9SjPiXAN87H/kj0ScEBY7IjJvZR5m7g== X-Received: by 2002:a05:6870:56aa:b0:10b:f4fb:8203 with SMTP id p42-20020a05687056aa00b0010bf4fb8203mr312197oao.181.1657139926714; Wed, 06 Jul 2022 13:38:46 -0700 (PDT) MIME-Version: 1.0 References: <20220621150902.46126-1-mlevitsk@redhat.com> <20220621150902.46126-12-mlevitsk@redhat.com> <42da1631c8cdd282e5d9cfd0698b6df7deed2daf.camel@redhat.com> <289c2dd941ecbc3c32514fc0603148972524b22d.camel@redhat.com> <5ff3c2b4712f6446d2c1361315b972ddad48836f.camel@redhat.com> In-Reply-To: <5ff3c2b4712f6446d2c1361315b972ddad48836f.camel@redhat.com> From: Jim Mattson Date: Wed, 6 Jul 2022 13:38:35 -0700 Message-ID: Subject: Re: [PATCH v2 11/11] KVM: x86: emulator/smm: preserve interrupt shadow in SMRAM To: Maxim Levitsky 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Wed, Jul 6, 2022 at 1:00 PM Maxim Levitsky wrote: > > On Wed, 2022-07-06 at 11:13 -0700, Jim Mattson wrote: > > On Tue, Jul 5, 2022 at 6:38 AM Maxim Levitsky wrote: ... > > > 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. > > > > Shouldn't we fix that? > I am afraid we can't because that will break (in theory) the backward compatibility > (e.g if someone migrates a VM while in SMM). Every time someone says, "We can't fix this, because it breaks backward compatibility," I think, "Another potential use of KVM_CAP_DISABLE_QUIRKS2?" ... > But then after looking at SDM I also found out that Intel and AMD have completely > different SMM layout for 64 bit. We follow the AMD's layout, but we don't > implement many fields, including some that are barely/not documented. > (e.g what is svm_guest_virtual_int?) > > In theory we could use Intel's layout when we run with Intel's vendor ID, > and AMD's vise versa, but we probably won't bother + once again there > is an issue of backward compatibility. This seems pretty egregious, since the SDM specifically states, "Some of the registers in the SMRAM state save area (marked YES in column 3) may be read and changed by the SMI handler, with the changed values restored to the processor registers by the RSM instruction." How can that possibly work with AMD's layout? (See my comment above regarding backwards compatibility.) I wish KVM would stop offering virtual CPU features that are completely broken. > > The vNMI feature isn't available in any shipping processor yet, is it? > Yes, but one of its purposes is to avoid single stepping the guest, > which is especially painful on AMD, because there is no MTF, so > you have to 'borrow' the TF flag in the EFLAGS, and that can leak into > the guest state (e.g pushed onto the stack). So, what's the solution for all of today's SVM-capable processors? KVM will probably be supporting AMD CPUs without vNMI for the next decade or two. > (Actually looking at clause of default treatment of SMIs in Intel's PRM, > they do mention that they preserve the int shadow somewhere at least > on some Intel's CPUs). Yes, this is a required part of VMX-critical state for processors that support SMI recognition while there is blocking by STI or by MOV SS. However, I don't believe that KVM actually saves VMX-critical state on delivery of a virtual SMI. > > BTW, according to my observations, it is really hard to hit this problem, > because it looks like when the CPU is in interrupt shadow, it doesn't process > _real_ interrupts as well (despite the fact that in VM, real interrupts > should never be blocked(*), but yet, that is what I observed on both AMD and Intel. > > (*) You can allow the guest to control the real EFLAGS.IF on both VMX and SVM, > (in which case int shadow should indeed work as on bare metal) > but KVM of course doesn't do it. It doesn't surprise me that hardware treats a virtual interrupt shadow as a physical interrupt shadow. IIRC, each vendor has a way of breaking an endless chain of interrupt shadows, so a malicious guest can't defer interrupts indefinitely. > I observed that when KVM sends #SMI from other vCPU, it sends a vCPU kick, > and the kick never arrives inside the interrupt shadow. > I have seen it on both VMX and SVM. > > What still triggers this problem, is that the instruction which is in the interrupt > shadow can still get a VM exit, (e.g EPT/NPT violation) and then it can notice > the pending SMI. > > I think it has to be EPT/NPT violation btw, because, IMHO most if not all other VM exits I > think are instruction intercepts, which will cause KVM to emulate the instruction > and clear the interrupt shadow, and only after that it will enter SMM. > > Even MMIO/IOPORT access is emulated by the KVM. > > Its not the case with EPT/NPT violation, because the KVM will in this case re-execute > the instruction after it 'fixes' the fault. Probably #PF as well, then, if TDP is disabled.