Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp640294imi; Thu, 21 Jul 2022 08:08:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sKxG4b9Fb4AWNKP6crH7GBWQ+hXHdmVRGCq9HfDb3FWpEbvDw108yUFOPvXL+133xIIWLu X-Received: by 2002:a17:906:6a0f:b0:72b:64ce:289d with SMTP id qw15-20020a1709066a0f00b0072b64ce289dmr39336869ejc.663.1658416079775; Thu, 21 Jul 2022 08:07:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658416079; cv=none; d=google.com; s=arc-20160816; b=Rl49fZCi+x/OtCx70vfI4HW8t3/BXI3OV9KjigKITo1b8ZH4QAKwMZOT8DKCvYYDwm zzrrFBCHuiUDz6ewgu+NoZcilMRaGspqUlJ1620AlUDS0+cb3d/Z74gKD9GN1CgA7L49 heIick6oAbsQ4JIi2GTSyBn0sDcahNJOQOUcBx5bSMMGz5evhxWXTKnY2ESh+XrpL5L5 JKcCkmlLFyRGQjf2aoAewLKNSm9qZCpQe38cRHlPtM3whucqHUPv+Dn9gI3wYdBKlOZF l1PG4gFaywwT+fT6kg/uToMOSSTs04lFbBYKhzednHMRrD62994NAMnD71MPq3IK/VNt oWXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=JdLRsvRnZXSge95hUUniiBkKTb/15qbqNRgmQFuXFpQ=; b=URJTDo/DKz7yacFggHVWvMItqWK4erXyT3biQVVmSZk5GgFvgN7+PKZ+U1dSM6p4ex s5UV/aEUF5us4JqLVa26BULyeC16ikD1NFKmdrG0QS1BgcsIJkYV7WALhMNadetBB8v9 Rp0Eon/Ucl1Sd0AQXr+crp+nVijp06Yd+swXHQGbDLopQBZoVW1YN8xNczjadNcE/6pv mszzn07tUFh6hiRdf8pmG//ovWCq8xAu3PkQTPKUJ+dH1d7XS/7vFmHeQSVDdngJPgUT 5mpQYDVXecPyIogSw+pHBiVxsVhWKEzUCDs66tkjCyzkTaHRH68QLWYc2qQUiQVciGUi qWXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="U9/GVNr7"; 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 e12-20020a50d4cc000000b0043bc31ac7d0si2446916edj.211.2022.07.21.08.07.35; Thu, 21 Jul 2022 08:07:59 -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="U9/GVNr7"; 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 S231365AbiGUOLU (ORCPT + 99 others); Thu, 21 Jul 2022 10:11:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231327AbiGUOLR (ORCPT ); Thu, 21 Jul 2022 10:11:17 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2005052FD5 for ; Thu, 21 Jul 2022 07:11:14 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id bf13so1765309pgb.11 for ; Thu, 21 Jul 2022 07:11:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=JdLRsvRnZXSge95hUUniiBkKTb/15qbqNRgmQFuXFpQ=; b=U9/GVNr7Qn7GXD9Ir8eRl7CIKSjh+S8N0xIJQVcE0Ek6IRZjcuGRrVPhycCG0BujXm /zs70hdIHMmYtwWARr77YGYwuAFIwyUrW68zLuF1r2iAv4SNEmdZMWsbNH+Q0Itg2dsO AWFV7qZl+ejmr1kHeMaKQn2V2iRlaUgOGl2fkqSLhQNcSD2NCjv+6GfvQ/Q4UgpJNQxq aK5s0IshYbWISsTjE5hvtlIdSq6QXbpyL8U8xHszq9rhHMjztAp07NSAo0i6hF6Txmsa dj0pK98iQCMIK6+A2QJD5/7Erhb2E4dU6YtKq0GPFRfbKl4GR+V5yx0mAn+/1nfvisKw 6YcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=JdLRsvRnZXSge95hUUniiBkKTb/15qbqNRgmQFuXFpQ=; b=fARW2GCcvAoI9qQkyEHldQZkRLCWYvNwGf0GYYuK1v7lBsZBZMoYAWdSm+dbRhXxxg bpfBIDy39jaegF+wZd8HCHpfocYeRhBF2BQjbH7tUqnkMoqcDxEFCekax3ouoc3ENX28 zmbDjNilBQ8z75Nn4CjU331TAGXpWwgg4LOWExMuUleBUQl5G9DOUmI7ryjQG90kadUE ijc52SIlfzIN8sx1y6BBHPEaspFjIUKKSj5m55204q8yRtc4uNYjmYKM/+4tOULXBekp BZF9M/8/E6WnpA72PJKXLNOkvaF3rpemz3TGbW1lufxU3yUykmNV6bWshL6oGTVUywcI GSyA== X-Gm-Message-State: AJIora/PvttMqwxuk6hVSM7jkiinFYAdoiudVw96DxyH9GJxi6lIoLIQ 16f+Nu+4krNLO/t/JnPTW45vow== X-Received: by 2002:a63:9041:0:b0:415:c0e8:c588 with SMTP id a62-20020a639041000000b00415c0e8c588mr37681722pge.282.1658412673210; Thu, 21 Jul 2022 07:11:13 -0700 (PDT) Received: from google.com (123.65.230.35.bc.googleusercontent.com. [35.230.65.123]) by smtp.gmail.com with ESMTPSA id o12-20020a170902778c00b0016d1f474653sm1495911pll.52.2022.07.21.07.11.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 07:11:12 -0700 (PDT) Date: Thu, 21 Jul 2022 14:11:08 +0000 From: Sean Christopherson To: Maxim Levitsky Cc: kvm@vger.kernel.org, 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 , Jim Mattson Subject: Re: [PATCH v2 05/11] KVM: x86: emulator: update the emulation mode after CR0 write Message-ID: References: <20220621150902.46126-1-mlevitsk@redhat.com> <20220621150902.46126-6-mlevitsk@redhat.com> <532c71cbca049004bd6860508fdc056ae118ab1f.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <532c71cbca049004bd6860508fdc056ae118ab1f.camel@redhat.com> 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, 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 Thu, Jul 21, 2022, Maxim Levitsky wrote: > On Wed, 2022-07-20 at 23:50 +0000, Sean Christopherson wrote: > > On Tue, Jun 21, 2022, Maxim Levitsky wrote: > > > CR0.PE toggles real/protected mode, thus its update > > > should update the emulation mode. > > > > > > This is likely a benign bug because there is no writeback > > > of state, other than the RIP increment, and when toggling > > > CR0.PE, the CPU has to execute code from a very low memory address. > > > > > > Signed-off-by: Maxim Levitsky > > > --- > > > arch/x86/kvm/emulate.c | 13 ++++++++++++- > > > 1 file changed, 12 insertions(+), 1 deletion(-) > > > > > > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > > > index 6f4632babc4cd8..002687d17f9364 100644 > > > --- a/arch/x86/kvm/emulate.c > > > +++ b/arch/x86/kvm/emulate.c > > > @@ -3659,11 +3659,22 @@ static int em_movbe(struct x86_emulate_ctxt *ctxt) > > > > > > static int em_cr_write(struct x86_emulate_ctxt *ctxt) > > > { > > > - if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) > > > + int cr_num = ctxt->modrm_reg; > > > + int r; > > > + > > > + if (ctxt->ops->set_cr(ctxt, cr_num, ctxt->src.val)) > > > return emulate_gp(ctxt, 0); > > > > > > /* Disable writeback. */ > > > ctxt->dst.type = OP_NONE; > > > + > > > + if (cr_num == 0) { > > > + /* CR0 write might have updated CR0.PE */ > > > > Or toggled CR0.PG. > > I thought about it but paging actually does not affect the CPU mode. Toggling CR0.PG when EFER.LME=1 (and CR4.PAE=1) switches the CPU in and out of long mode. That's why I mentioned the EFER.LMA thing below. It's also notable in that the only reason we don't have to handle CR4 here is because clearing CR4.PAE while long is active causes a #GP. > E.g if you are in protected mode, instructions execute the same regardless > if you have paging or not. > > (There are probably some exceptions but you understand what I mean). > > Best regards, > Maxim Levitsky > > > It's probably also worth noting that ->set_cr() handles side > > effects to other registers, e.g. the lack of an EFER.LMA update makes this look > > suspicious at first glance.