From nobody Wed Sep 10 02:29:35 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1683029437; cv=none; d=zohomail.com; s=zohoarc; b=XTJ6HMkE/SpS31XTAEp7jLRTrKZQRXZNkEKlBOBushXJLTwTtFtlbObODVwwCFPeSSzvG/6Zwpbi3hjj/qOAtkU0X5U7YG0BCq8Iant/Nr5BI1O14UUYvb6wGhyJ8g0hbYQ8+621HSmMzFWl890K/6IvQkFveU3XeKq2PQI9i6k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683029437; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=RMQn8Yiv0jCwQnecK3T9cC+8gstSs8Pnq7WDZyHBVNw=; b=TugeVaYvJBrrGBl4xEPazFfGG8K2A8dVmJlpZZiq+8cf+dxzr+E38dhGLj1umjsOYCeoLzaQUzmuWZgBnhMwWpIuKIem/3KTpLpCKI0Bv39SQPW4D97IerVeFhR9LadoEohzMb48a+kqghjs3GIZQ/I1vlgV4fZcWka5uLBF4ls= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1683029437475919.6011856424158; Tue, 2 May 2023 05:10:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.528561.821894 (Exim 4.92) (envelope-from ) id 1ptoph-0008Fd-9o; Tue, 02 May 2023 12:10:13 +0000 Received: by outflank-mailman (output) from mailman id 528561.821894; Tue, 02 May 2023 12:10:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ptoph-0008FU-5s; Tue, 02 May 2023 12:10:13 +0000 Received: by outflank-mailman (input) for mailman id 528561; Tue, 02 May 2023 12:10:12 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ptopg-0008EV-Bf for xen-devel@lists.xenproject.org; Tue, 02 May 2023 12:10:12 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2001:67c:2178:6::1d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 49720d1c-e8e2-11ed-b225-6b7b168915f2; Tue, 02 May 2023 14:10:11 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id DFC711F8D6; Tue, 2 May 2023 12:10:10 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 92321139C3; Tue, 2 May 2023 12:10:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id GmhQIqL9UGTMLwAAMHmgww (envelope-from ); Tue, 02 May 2023 12:10:10 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 49720d1c-e8e2-11ed-b225-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1683029410; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RMQn8Yiv0jCwQnecK3T9cC+8gstSs8Pnq7WDZyHBVNw=; b=UthapKSln5sVxc4P6W4BSa2qf1rvyEffS6HG2itfjchsaqorftKG30uqY4qHygD7foz2Sd qDX60C23glOf63Dt33mUz/OjzAGUAEkfM3DRKtIV4jLuqobQl4wBPaZynsRN7Is2PLb0tE yKXtXL2TX9qODXdqlLZNvDgVEnbQqZM= From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: mikelley@microsoft.com, Juergen Gross , Boris Ostrovsky , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , xen-devel@lists.xenproject.org Subject: [PATCH v6 05/16] x86/xen: set MTRR state when running as Xen PV initial domain Date: Tue, 2 May 2023 14:09:20 +0200 Message-Id: <20230502120931.20719-6-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230502120931.20719-1-jgross@suse.com> References: <20230502120931.20719-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1683029438456100001 Content-Type: text/plain; charset="utf-8" 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 wh= en running on Xen") Signed-off-by: Juergen Gross Reviewed-by: Boris Ostrovsky --- V2: - new patch V3: - move the call of mtrr_overwrite_state() to xen_pv_init_platform() V4: - only call mtrr_overwrite_state() if any MTRR got from Xen (Boris Ostrovsky) --- 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 093b78c8bbec..8732b85d5650 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); =20 +/* 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 =3D { + .cmd =3D XENPF_read_memtype, + .interface_version =3D 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 =3D 36; + eax =3D cpuid_eax(0x80000000); + if ((eax >> 16) =3D=3D 0x8000 && eax >=3D 0x80000008) { + eax =3D cpuid_eax(0x80000008); + width =3D eax & 0xff; + } + + for (reg =3D 0; reg < MTRR_MAX_VAR_RANGES; reg++) { + op.u.read_memtype.reg =3D 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 =3D=3D PFN regarding MTRR settings. + */ + var[reg].base_lo =3D op.u.read_memtype.type; + var[reg].base_lo |=3D op.u.read_memtype.mfn << PAGE_SHIFT; + var[reg].base_hi =3D op.u.read_memtype.mfn >> (32 - PAGE_SHIFT); + mask =3D ~((op.u.read_memtype.nr_mfns << PAGE_SHIFT) - 1); + mask &=3D (1UL << width) - 1; + if (mask) + mask |=3D MTRR_PHYSMASK_V; + var[reg].mask_lo =3D mask; + var[reg].mask_hi =3D 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) =20 /* pvclock is in shared info area */ xen_init_time_ops(); + + if (xen_initial_domain()) + xen_set_mtrr_data(); } =20 static void __init xen_pv_guest_late_init(void) --=20 2.35.3