Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5687982rwd; Mon, 5 Jun 2023 07:16:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4qxlE2BHi5IMOJYPeiNHJKca4SPs3jzRl48nOxXWxnY///BRPmlMolxV3/YYBWVKI71QjL X-Received: by 2002:a05:6a21:108e:b0:10b:71c:20c5 with SMTP id nl14-20020a056a21108e00b0010b071c20c5mr3268561pzb.51.1685974583323; Mon, 05 Jun 2023 07:16:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685974583; cv=none; d=google.com; s=arc-20160816; b=B8b23AAaL7La5YcI6YOhJfQT+kpcHpPloYO2bIs90uHWozWqnoDfHpQRaOm/KkOe5D 8Zjy+57gDM64asY1Rp7YhblJ19akHRtVAn+piMgWzUY/hT85FchLVNEG8whMQiHeK2D2 2gJsUC3plGNg74zVeHTCsAxDHNYK7XnecmGSs04eYdRodlZshh4AC4cPR7kGgqMwzXGi Hexuo1W4NmYg4Tc19AR24VtgYfWz/FGdsw5kotoOBJE1E/irVZzesD3Ojy4FjihBq9E3 kRYosSBx03KHg+UBoGZAupBMtvMb2m/PZnFVSxyhBujee2vs+XZk9ZvqbBnzlgv+NSp6 4ncw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=Jjr2ZcMBypnD5EDokqM52pjUqBzKKRIB3j2hdweyNvM=; b=jiYYYveoEHcmLC8sENIbgNSS2pMOpFtwNYtsHIuU7McxPvXPD1gn+8n14oBrgWjuWK yzRu9lInrv6Zk0RQXH1Jgc0rTvwk/bS1xnVbJOHHC+8dW7y0glQJg+i23JGeOCmql/G1 nG3eQS2oRU3DZFXP6w6gPL2burNprauu67jjZAYB14zlPGzAIG8Z8HaOFwIYlKbVpEKP 7zk7A8g/HqlkAj+he+Xa6wsOGV7EZ/0cuQvH5OcLNSVTld9myAxs3piBVNZ89ySac4JT 1DkDtPCa1HGakHMf97P+23Gj2vJWNBN8QqHcfsKgmwD9KLmGCogMks/Jl4zgLOE8DJqi Kurw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=RkCFuQjx; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v25-20020a63b959000000b00533ffeaca3dsi5408097pgo.40.2023.06.05.07.16.10; Mon, 05 Jun 2023 07:16:23 -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=@linutronix.de header.s=2020 header.b=RkCFuQjx; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230038AbjFEOJ2 (ORCPT + 99 others); Mon, 5 Jun 2023 10:09:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231432AbjFEOIf (ORCPT ); Mon, 5 Jun 2023 10:08:35 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1D7DE51; Mon, 5 Jun 2023 07:08:18 -0700 (PDT) Date: Mon, 05 Jun 2023 14:08:16 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1685974097; h=from:from:sender:sender:reply-to: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=Jjr2ZcMBypnD5EDokqM52pjUqBzKKRIB3j2hdweyNvM=; b=RkCFuQjxCmlaT5PRef2YE3Z/tVbblUnvWJpiUk6vXER8erEmP7cjj3sc18uvyGlF+6Rea0 MzhzjO0FLFxbCeUJq4zH1zWqdnJ7Kz0XlS8qqcf7ZwH/x6l9r/HGid5ZlXRPbzuMPugzes smu4ajG7twrHLQ6EeZEx3AVhLEnx/8VCZLgHwf0JnDVmf741GFbyE7Ed+ByENNNU3gXLgw awC/Mfh0VwFOGiSaoaw1FSxyJsPLDelFCCgHfIbkCmNV1MnAkb8KOPP6GXdvsCbeb/jp52 zIOKa+Ia5qpi/pwu+F3yVEks0XPhZ/MbgtaknLx9Bv+RewHNLJkZ6LopRSJXuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1685974097; h=from:from:sender:sender:reply-to: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=Jjr2ZcMBypnD5EDokqM52pjUqBzKKRIB3j2hdweyNvM=; b=lc6zQE+qPeqIeQjtuUqjTiOMoP46YV/JgKpKMaufhw3IKEpJ8XQfpBJK2PZwM3MG/UfhcG fEBkAGviaKIql6CA== From: "tip-bot2 for Juergen Gross" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/mtrr] x86/xen: Set MTRR state when running as Xen PV initial domain Cc: Juergen Gross , "Borislav Petkov (AMD)" , Boris Ostrovsky , Michael Kelley , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230502120931.20719-6-jgross@suse.com> References: <20230502120931.20719-6-jgross@suse.com> MIME-Version: 1.0 Message-ID: <168597409652.404.16424399109459449233.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 The following commit has been merged into the x86/mtrr branch of tip: Commit-ID: a153f254e5cdf8fa3a1df90a6ffed3063fede154 Gitweb: https://git.kernel.org/tip/a153f254e5cdf8fa3a1df90a6ffed3063fede154 Author: Juergen Gross AuthorDate: Tue, 02 May 2023 14:09:20 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Thu, 01 Jun 2023 15:04:32 +02:00 x86/xen: Set MTRR state when running as Xen PV initial domain When running as Xen PV initial domain (aka dom0), MTRRs are disabled by the hypervisor, but the system should nevertheless use correct cache memory types. This has always kind of worked, as disabled MTRRs resulted in disabled PAT, too, so that the kernel avoided code paths resulting in inconsistencies. This bypassed all of the sanity checks the kernel is doing with enabled MTRRs in order to avoid memory mappings with conflicting memory types. This has been changed recently, leading to PAT being accepted to be enabled, while MTRRs stayed disabled. The result is that mtrr_type_lookup() no longer is accepting all memory type requests, but started to return WB even if UC- was requested. This led to driver failures during initialization of some devices. In reality MTRRs are still in effect, but they are under complete control of the Xen hypervisor. It is possible, however, to retrieve the MTRR settings from the hypervisor. In order to fix those problems, overwrite the MTRR state via mtrr_overwrite_state() with the MTRR data from the hypervisor, if the system is running as a Xen dom0. Fixes: 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen") Signed-off-by: Juergen Gross Signed-off-by: Borislav Petkov (AMD) Reviewed-by: Boris Ostrovsky Tested-by: Michael Kelley Link: https://lore.kernel.org/r/20230502120931.20719-6-jgross@suse.com Signed-off-by: Borislav Petkov (AMD) --- arch/x86/xen/enlighten_pv.c | 52 ++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+) diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index 093b78c..8732b85 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -68,6 +68,7 @@ #include #include #include +#include #include #include #include @@ -119,6 +120,54 @@ static int __init parse_xen_msr_safe(char *str) } early_param("xen_msr_safe", parse_xen_msr_safe); +/* Get MTRR settings from Xen and put them into mtrr_state. */ +static void __init xen_set_mtrr_data(void) +{ +#ifdef CONFIG_MTRR + struct xen_platform_op op = { + .cmd = XENPF_read_memtype, + .interface_version = XENPF_INTERFACE_VERSION, + }; + unsigned int reg; + unsigned long mask; + uint32_t eax, width; + static struct mtrr_var_range var[MTRR_MAX_VAR_RANGES] __initdata; + + /* Get physical address width (only 64-bit cpus supported). */ + width = 36; + eax = cpuid_eax(0x80000000); + if ((eax >> 16) == 0x8000 && eax >= 0x80000008) { + eax = cpuid_eax(0x80000008); + width = eax & 0xff; + } + + for (reg = 0; reg < MTRR_MAX_VAR_RANGES; reg++) { + op.u.read_memtype.reg = reg; + if (HYPERVISOR_platform_op(&op)) + break; + + /* + * Only called in dom0, which has all RAM PFNs mapped at + * RAM MFNs, and all PCI space etc. is identity mapped. + * This means we can treat MFN == PFN regarding MTRR settings. + */ + var[reg].base_lo = op.u.read_memtype.type; + var[reg].base_lo |= op.u.read_memtype.mfn << PAGE_SHIFT; + var[reg].base_hi = op.u.read_memtype.mfn >> (32 - PAGE_SHIFT); + mask = ~((op.u.read_memtype.nr_mfns << PAGE_SHIFT) - 1); + mask &= (1UL << width) - 1; + if (mask) + mask |= MTRR_PHYSMASK_V; + var[reg].mask_lo = mask; + var[reg].mask_hi = mask >> 32; + } + + /* Only overwrite MTRR state if any MTRR could be got from Xen. */ + if (reg) + mtrr_overwrite_state(var, reg, MTRR_TYPE_UNCACHABLE); +#endif +} + static void __init xen_pv_init_platform(void) { /* PV guests can't operate virtio devices without grants. */ @@ -135,6 +184,9 @@ static void __init xen_pv_init_platform(void) /* pvclock is in shared info area */ xen_init_time_ops(); + + if (xen_initial_domain()) + xen_set_mtrr_data(); } static void __init xen_pv_guest_late_init(void)