From nobody Sun Feb 8 02:49:09 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB5A515AD90; Wed, 28 Aug 2024 09:35:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837719; cv=none; b=c7mU5e/o+FPG166djgkVausZJF27+9I5VV59yyPJSQv+Fsul2qz/sszVRy5fuiD1J0mJpjfdiE3c67Vf55stxTNyw4/gYYCJmeifiwfeQZ5HfdOG30kD1BTZ4hB1CzRlf0WUkx7i0A3wdAbLPmCVDNYTYRLOr5XTPm6pgfWTk7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837719; c=relaxed/simple; bh=jXkbf1bN1JjFULaCZowJliy7FvNZ7PnLXWSqm/seso8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iUGvMr6+M4rm0UzpZhN6y88j1xDegdnCWGfKtrPFhQMhUSzGfDzzETDPberbSz2ZG5wJYHBQHz9+h2tt/hfYYmrFObdUUHrkW9KjiAaHE+RId9jkyKaotlWibC2Ve1X7lbmo3EX47Gj5roQGvdTCqBleLaOO+GlwwHsWZmForEs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=W9/fVQyt; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="W9/fVQyt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724837718; x=1756373718; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jXkbf1bN1JjFULaCZowJliy7FvNZ7PnLXWSqm/seso8=; b=W9/fVQytqTmNvLJR4dHaKQUzl+HTXi7v+WPvQwwgTCmHAO/Exr5ha9Ak vhkdN25X0fAn4kWr4jpo17KpNhgRFYmSN3TvDTTZhNP/ZRt2FXnCKUt7j eVZma8y/2gX1LXm0oXMLj0yatYqURW8nc6QwtcyycXtssVJGC+XiRJkfq yHcO/GH8Hd3PRN1rrWxtJNyxXvCxf6hsA3TxAZLsD+bkLEaZC/8v5Gs8C lVdGG0T8ZBPYt6uggF9tr5HVdkeQtm+8iKK8vN4GIwvRVHUsC3Rwh8LOq 7w/uG71xt5h+6hP7xzdhAdcX6vQCBmp/TTNZ2plXuMTh2NP0jx76moeQ3 Q==; X-CSE-ConnectionGUID: h8r3xcYSTSmdSA8N70yBrg== X-CSE-MsgGUID: WPEbEYakSvKMY5rKwkptnw== X-IronPort-AV: E=McAfee;i="6700,10204,11177"; a="27123863" X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="27123863" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2024 02:35:15 -0700 X-CSE-ConnectionGUID: NLkYbWrKSumnDYbdXdcHLQ== X-CSE-MsgGUID: zL+fveQ7TaeFooruZG2Qjg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="93879099" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa002.jf.intel.com with ESMTP; 28 Aug 2024 02:35:11 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 7067B118; Wed, 28 Aug 2024 12:35:10 +0300 (EEST) From: "Kirill A. Shutemov" To: "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Kai Huang , Kuppuswamy Sathyanarayanan , stable@vger.kernel.org Subject: [PATCHv6 1/4] x86/tdx: Introduce wrappers to read and write TD metadata Date: Wed, 28 Aug 2024 12:35:02 +0300 Message-ID: <20240828093505.2359947-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> References: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The TDG_VM_WR TDCALL is used to ask the TDX module to change some TD-specific VM configuration. There is currently only one user in the kernel of this TDCALL leaf. More will be added shortly. Refactor to make way for more users of TDG_VM_WR who will need to modify other TD configuration values. Add a wrapper for the TDG_VM_RD TDCALL that requests TD-specific metadata from the TDX module. There are currently no users for TDG_VM_RD. Mark it as __maybe_unused until the first user appears. This is preparation for enumeration and enabling optional TD features. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kai Huang Reviewed-by: Kuppuswamy Sathyanarayanan Cc: stable@vger.kernel.org Reviewed-by: Nikolay Borisov --- arch/x86/coco/tdx/tdx.c | 32 ++++++++++++++++++++++++++----- arch/x86/include/asm/shared/tdx.h | 1 + 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 078e2bac2553..64717a96a936 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -77,6 +77,32 @@ static inline void tdcall(u64 fn, struct tdx_module_args= *args) panic("TDCALL %lld failed (Buggy TDX module!)\n", fn); } =20 +/* Read TD-scoped metadata */ +static inline u64 __maybe_unused tdg_vm_rd(u64 field, u64 *value) +{ + struct tdx_module_args args =3D { + .rdx =3D field, + }; + u64 ret; + + ret =3D __tdcall_ret(TDG_VM_RD, &args); + *value =3D args.r8; + + return ret; +} + +/* Write TD-scoped metadata */ +static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask) +{ + struct tdx_module_args args =3D { + .rdx =3D field, + .r8 =3D value, + .r9 =3D mask, + }; + + return __tdcall(TDG_VM_WR, &args); +} + /** * tdx_mcall_get_report0() - Wrapper to get TDREPORT0 (a.k.a. TDREPORT * subtype 0) using TDG.MR.REPORT TDCALL. @@ -924,10 +950,6 @@ static void tdx_kexec_finish(void) =20 void __init tdx_early_init(void) { - struct tdx_module_args args =3D { - .rdx =3D TDCS_NOTIFY_ENABLES, - .r9 =3D -1ULL, - }; u64 cc_mask; u32 eax, sig[3]; =20 @@ -946,7 +968,7 @@ void __init tdx_early_init(void) cc_set_mask(cc_mask); =20 /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */ - tdcall(TDG_VM_WR, &args); + tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL); =20 /* * All bits above GPA width are reserved and kernel treats shared bit diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/share= d/tdx.h index fdfd41511b02..7e12cfa28bec 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -16,6 +16,7 @@ #define TDG_VP_VEINFO_GET 3 #define TDG_MR_REPORT 4 #define TDG_MEM_PAGE_ACCEPT 6 +#define TDG_VM_RD 7 #define TDG_VM_WR 8 =20 /* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */ --=20 2.45.2 From nobody Sun Feb 8 02:49:09 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B63111662E7; Wed, 28 Aug 2024 09:35:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837718; cv=none; b=kVxK/6nyx2ryJxBqcjH0GKuXewz0fFmDPg6kBAa85aTkYW9B8i2KG1s5QC/Z8GY+gT0Kz5i0K1QPsWsilnQrfm0hPrup0M8bK7j4bKz+wEFxCr0YImvXO+NscX3byqhjdfgzncO/WqkDUnifdyqhzAiYqxbygWiFJO7KhhoxPHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837718; c=relaxed/simple; bh=txhMMZiJ5qMnU9pWlrkfzR83GU0+BDmAM6j8E24Zs5U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=azWatfArdUa4lFXnAIT/KmwR6/LB1mBGP4lvLraOUHJ/ERQkW8gd+DsHzLQAIqXO/nDjYidwCBrmSxRrIMwIx1wqquxpAeiKq2QFEAfo0D69yaNWMNvye2zAdW5yVrGMVhWjgvsREaJrkDjW4pvLQp1PrWR0AiFiw7ot0mfVHKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Zyweyqng; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Zyweyqng" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724837717; x=1756373717; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=txhMMZiJ5qMnU9pWlrkfzR83GU0+BDmAM6j8E24Zs5U=; b=ZyweyqngUtIXd21t01uPa0g4rNKD0LdWvJi/pKz95XGsjoXBBXpDqoyR k+zvP4COFyZb3T/Qfknhk86YL6e+l6nHxQGkBwic4+dhvO1+R4LF0cF+P 79U4I++HjF10MGb107t3u3Xm+79N7WbfzKCYA1+V5hJzQYNVLon8uJTko emjTafH0yz+jRPSxyErd3PAwF6zVqvYSLe501N/LavfXCwF+8FZLJIcZ9 NfwFDsVKAPfbTJK7knkwlS9EfByxeogcfQIuqihFADmnqcKK+GE2UmVTL kKolQzQogABPKscWMPhJWcygatKebQWCuhmvNwxxHpTR978/BMZHRVCO5 A==; X-CSE-ConnectionGUID: uwr7eGUDTE6DdsroZXr9Bw== X-CSE-MsgGUID: Ly8thP7ATvqEL3w6/vXcMQ== X-IronPort-AV: E=McAfee;i="6700,10204,11177"; a="26254883" X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="26254883" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2024 02:35:15 -0700 X-CSE-ConnectionGUID: T/h+enk6SfaHYpNv8dnoiw== X-CSE-MsgGUID: VFvSTLrARWi9vWFF2Kfhmw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="62827318" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa006.fm.intel.com with ESMTP; 28 Aug 2024 02:35:12 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 8665F143; Wed, 28 Aug 2024 12:35:10 +0300 (EEST) From: "Kirill A. Shutemov" To: "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Kuppuswamy Sathyanarayanan , Kai Huang , stable@vger.kernel.org Subject: [PATCHv6 2/4] x86/tdx: Rename tdx_parse_tdinfo() to tdx_setup() Date: Wed, 28 Aug 2024 12:35:03 +0300 Message-ID: <20240828093505.2359947-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> References: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename tdx_parse_tdinfo() to tdx_setup() and move setting NOTIFY_ENABLES there. The function will be extended to adjust TD configuration. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kuppuswamy Sathyanarayanan Reviewed-by: Kai Huang Cc: stable@vger.kernel.org --- arch/x86/coco/tdx/tdx.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 64717a96a936..08ce488b54d0 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -193,7 +193,7 @@ static void __noreturn tdx_panic(const char *msg) __tdx_hypercall(&args); } =20 -static void tdx_parse_tdinfo(u64 *cc_mask) +static void tdx_setup(u64 *cc_mask) { struct tdx_module_args args =3D {}; unsigned int gpa_width; @@ -218,6 +218,9 @@ static void tdx_parse_tdinfo(u64 *cc_mask) gpa_width =3D args.rcx & GENMASK(5, 0); *cc_mask =3D BIT_ULL(gpa_width - 1); =20 + /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */ + tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL); + /* * The kernel can not handle #VE's when accessing normal kernel * memory. Ensure that no #VE will be delivered for accesses to @@ -964,11 +967,11 @@ void __init tdx_early_init(void) setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE); =20 cc_vendor =3D CC_VENDOR_INTEL; - tdx_parse_tdinfo(&cc_mask); - cc_set_mask(cc_mask); =20 - /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */ - tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL); + /* Configure the TD */ + tdx_setup(&cc_mask); + + cc_set_mask(cc_mask); =20 /* * All bits above GPA width are reserved and kernel treats shared bit --=20 2.45.2 From nobody Sun Feb 8 02:49:09 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E48E515E5C8; Wed, 28 Aug 2024 09:35:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837716; cv=none; b=H1i9E/H7p30siE9AYGBvTC2JcFYKDABaJZ0FSPkBnkhVlnytROg+rOsUmEXjBu0Jm1sPaKYtCj+dKJ14+FyiS6rQbbWP/AlFVb6s97Axg71p04pB1wAxcFqLtiolr9EkX9qoYm3xkPyvgHx3Sr7poVL24aSocrgceUahE5sMrBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837716; c=relaxed/simple; bh=jGKw4tU7jAqQLLT4JQD9rbimVPGNiuNBeJ+NAO9B7F8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=if14zXtYRJBf/a1QUCESswa3SM+Nkwb4xd4YgCHiW8YYCLsGaVPoZ18U/hj5uEl4Szasgi7x6+mIBc1J2Yte3xzE4vZJa47K3kG9BmOJyfy03xld1Zid8s4vmH+5bNUjSac/tRoklFIvoEQN0gUPGXMJiTtfviXE74BPZAFzltw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=c22+VrRp; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="c22+VrRp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724837715; x=1756373715; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jGKw4tU7jAqQLLT4JQD9rbimVPGNiuNBeJ+NAO9B7F8=; b=c22+VrRpWiYDD9IWIR/4UtZghx0FF/FNAKEViovAV3KKzKlp4fVBjRvQ nkKNxHG9IASQdgSisSU9LHj/1ZquLFuFIz/AS2VErdvVpNBI3k0GQTZ10 DUvLoEKrDFFA7GFsq9on3JTggA6B7ZffkDZWXA7t3eRSY3aMKKeAjrUv0 qwTH1lx+q9+IVT9L57Qeaxmms2ffyWzqslDU8taSzS/8rO8VXC3pQwAax ToNIRiK3ovTLsp3vzAvLrtPqybDUvD4xfG7upiIX+dCDYloLkQpTtFhL9 45fUUtBMateltyRShoUBm6ltFtYIYo8TTyWgYFOgIstHfVp1U/WzMZgLn w==; X-CSE-ConnectionGUID: 4ufuH3BVQyWnvysyxGxFDg== X-CSE-MsgGUID: LWWvq386RNGOIKur1JWIuw== X-IronPort-AV: E=McAfee;i="6700,10204,11177"; a="26254876" X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="26254876" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2024 02:35:14 -0700 X-CSE-ConnectionGUID: MgM8RcFLTmOh+TOD8VK2LA== X-CSE-MsgGUID: rFjQOtZHS8+nKvhL2yR4tQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="62827315" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa006.fm.intel.com with ESMTP; 28 Aug 2024 02:35:12 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 982972E6; Wed, 28 Aug 2024 12:35:10 +0300 (EEST) From: "Kirill A. Shutemov" To: "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Kai Huang Subject: [PATCHv6 3/4] x86/tdx: Dynamically disable SEPT violations from causing #VEs Date: Wed, 28 Aug 2024 12:35:04 +0300 Message-ID: <20240828093505.2359947-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> References: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Memory access #VEs are hard for Linux to handle in contexts like the entry code or NMIs. But other OSes need them for functionality. There's a static (pre-guest-boot) way for a VMM to choose one or the other. But VMMs don't always know which OS they are booting, so they choose to deliver those #VEs so the "other" OSes will work. That, unfortunately has left us in the lurch and exposed to these hard-to-handle #VEs. The TDX module has introduced a new feature. Even if the static configuration is set to "send nasty #VEs", the kernel can dynamically request that they be disabled. Once they are disabled, access to private memory that is not in the Mapped state in the Secure-EPT (SEPT) will result in an exit to the VMM rather than injecting a #VE. Check if the feature is available and disable SEPT #VE if possible. If the TD is allowed to disable/enable SEPT #VEs, the ATTR_SEPT_VE_DISABLE attribute is no longer reliable. It reflects the initial state of the control for the TD, but it will not be updated if someone (e.g. bootloader) changes it before the kernel starts. Kernel must check TDCS_TD_CTLS bit to determine if SEPT #VEs are enabled or disabled. Signed-off-by: Kirill A. Shutemov Fixes: 373e715e31bf ("x86/tdx: Panic on bad configs that #VE on "private" m= emory access") Cc: stable@vger.kernel.org Acked-by: Kai Huang Reviewed-by: Nikolay Borisov --- arch/x86/coco/tdx/tdx.c | 76 ++++++++++++++++++++++++------- arch/x86/include/asm/shared/tdx.h | 10 +++- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 08ce488b54d0..f969f4f5ebf8 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -78,7 +78,7 @@ static inline void tdcall(u64 fn, struct tdx_module_args = *args) } =20 /* Read TD-scoped metadata */ -static inline u64 __maybe_unused tdg_vm_rd(u64 field, u64 *value) +static inline u64 tdg_vm_rd(u64 field, u64 *value) { struct tdx_module_args args =3D { .rdx =3D field, @@ -193,6 +193,62 @@ static void __noreturn tdx_panic(const char *msg) __tdx_hypercall(&args); } =20 +/* + * The kernel cannot handle #VEs when accessing normal kernel memory. Ensu= re + * that no #VE will be delivered for accesses to TD-private memory. + * + * TDX 1.0 does not allow the guest to disable SEPT #VE on its own. The VMM + * controls if the guest will receive such #VE with TD attribute + * ATTR_SEPT_VE_DISABLE. + * + * Newer TDX modules allow the guest to control if it wants to receive SEPT + * violation #VEs. + * + * Check if the feature is available and disable SEPT #VE if possible. + * + * If the TD is allowed to disable/enable SEPT #VEs, the ATTR_SEPT_VE_DISA= BLE + * attribute is no longer reliable. It reflects the initial state of the + * control for the TD, but it will not be updated if someone (e.g. bootloa= der) + * changes it before the kernel starts. Kernel must check TDCS_TD_CTLS bit= to + * determine if SEPT #VEs are enabled or disabled. + */ +static void disable_sept_ve(u64 td_attr) +{ + const char *msg =3D "TD misconfiguration: SEPT #VE has to be disabled"; + bool debug =3D td_attr & ATTR_DEBUG; + u64 config, controls; + + /* Is this TD allowed to disable SEPT #VE */ + tdg_vm_rd(TDCS_CONFIG_FLAGS, &config); + if (!(config & TDCS_CONFIG_FLEXIBLE_PENDING_VE)) { + /* No SEPT #VE controls for the guest: check the attribute */ + if (td_attr & ATTR_SEPT_VE_DISABLE) + return; + + /* Relax SEPT_VE_DISABLE check for debug TD for backtraces */ + if (debug) + pr_warn("%s\n", msg); + else + tdx_panic(msg); + return; + } + + /* Check if SEPT #VE has been disabled before us */ + tdg_vm_rd(TDCS_TD_CTLS, &controls); + if (controls & TD_CTLS_PENDING_VE_DISABLE) + return; + + /* Keep #VEs enabled for splats in debugging environments */ + if (debug) + return; + + /* Disable SEPT #VEs */ + tdg_vm_wr(TDCS_TD_CTLS, TD_CTLS_PENDING_VE_DISABLE, + TD_CTLS_PENDING_VE_DISABLE); + + return; +} + static void tdx_setup(u64 *cc_mask) { struct tdx_module_args args =3D {}; @@ -218,24 +274,12 @@ static void tdx_setup(u64 *cc_mask) gpa_width =3D args.rcx & GENMASK(5, 0); *cc_mask =3D BIT_ULL(gpa_width - 1); =20 + td_attr =3D args.rdx; + /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */ tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL); =20 - /* - * The kernel can not handle #VE's when accessing normal kernel - * memory. Ensure that no #VE will be delivered for accesses to - * TD-private memory. Only VMM-shared memory (MMIO) will #VE. - */ - td_attr =3D args.rdx; - if (!(td_attr & ATTR_SEPT_VE_DISABLE)) { - const char *msg =3D "TD misconfiguration: SEPT_VE_DISABLE attribute must= be set."; - - /* Relax SEPT_VE_DISABLE check for debug TD. */ - if (td_attr & ATTR_DEBUG) - pr_warn("%s\n", msg); - else - tdx_panic(msg); - } + disable_sept_ve(td_attr); } =20 /* diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/share= d/tdx.h index 7e12cfa28bec..fecb2a6e864b 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -19,9 +19,17 @@ #define TDG_VM_RD 7 #define TDG_VM_WR 8 =20 -/* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */ +/* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */ +#define TDCS_CONFIG_FLAGS 0x1110000300000016 +#define TDCS_TD_CTLS 0x1110000300000017 #define TDCS_NOTIFY_ENABLES 0x9100000000000010 =20 +/* TDCS_CONFIG_FLAGS bits */ +#define TDCS_CONFIG_FLEXIBLE_PENDING_VE BIT_ULL(1) + +/* TDCS_TD_CTLS bits */ +#define TD_CTLS_PENDING_VE_DISABLE BIT_ULL(0) + /* TDX hypercall Leaf IDs */ #define TDVMCALL_MAP_GPA 0x10001 #define TDVMCALL_GET_QUOTE 0x10002 --=20 2.45.2 From nobody Sun Feb 8 02:49:09 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67F391662FA for ; Wed, 28 Aug 2024 09:35:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837718; cv=none; b=LyeCyv6OrNwi7j694t1I6iHekDugUInW0bIyF3lyvpVwctdRb52PAc78KOI11FaqulM6z+Oz+Z/viTkF/RWS5HYhrYW0kkkzvKrqFfpGaN00BKwJkBza3coqm/NLJOC7TPag5AUW6lmHmogaJakXcAXK8jyigkcQ4el9d+SVXP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724837718; c=relaxed/simple; bh=po43unqAaIsTUVdSBbc9kKj7rhAu7QEs5DeNl1JT7Xs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AG/BwN44DDNSRABu3lz8Qa99Lyhd9EMHpoS3wweJ1atW2njuZUxJ9MRSwYOD3q2CH9hME8FEKshZvWb7bt6/kH5r5QMFgxU/N5rZHJ1y5CYW6YZDfsYeXJZZU1aM0MM040fmadnxW+Dv/0yszSoIetak+2YvPETcMlay6ZkIAX0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=M0t6P3In; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="M0t6P3In" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724837718; x=1756373718; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=po43unqAaIsTUVdSBbc9kKj7rhAu7QEs5DeNl1JT7Xs=; b=M0t6P3InTRlbymIqUuuvCOQ6KH1EgMnorJ7HBt89QNxGvnOrdpMmmlxM 8Ilbt68V8Rq00s5ofrOh+7L4UCnCf0WpNVMF1MKDOV9ZbfjFIZMynsIAL OcysWun5Nwj4B/wDtbBg0xdcJmXlHyGbkfU54py9ZDCDrfCHIakvjDHjG P7YFi56G+KnjFk1fLKHxFLZKlzXtHekXK6AFJDh12BxW8MpUXvPfhbj9M PUmU3vEg4++XA68cKAqoAHDo9xasbfX6d+YuFxxS1/s2SZzwi7pm0wLeV iGWAcuYr4H/6MuckA/D298+ZAEb6nCgjvjJbeJaGky2BNMQEAehvXR7en w==; X-CSE-ConnectionGUID: d3ammvVbT9CcfT+9SPDQdg== X-CSE-MsgGUID: x0lOD2ZkQ7CGBcUxYUR3CA== X-IronPort-AV: E=McAfee;i="6700,10204,11177"; a="27123860" X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="27123860" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2024 02:35:14 -0700 X-CSE-ConnectionGUID: mE4SB/2cQ3udmPZuq77twg== X-CSE-MsgGUID: exjcmjNoRJO1eRNaXbGNbQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="93879094" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa002.jf.intel.com with ESMTP; 28 Aug 2024 02:35:11 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id AB30C1F5; Wed, 28 Aug 2024 12:35:10 +0300 (EEST) From: "Kirill A. Shutemov" To: "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Kai Huang Subject: [PATCHv6 4/4] x86/tdx: Enable CPU topology enumeration Date: Wed, 28 Aug 2024 12:35:05 +0300 Message-ID: <20240828093505.2359947-5-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> References: <20240828093505.2359947-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" TDX 1.0 defines baseline behaviour of TDX guest platform. TDX 1.0 generates a #VE when accessing topology-related CPUID leafs (0xB and 0x1F) and the X2APIC_APICID MSR. The kernel returns all zeros on CPUID topology. In practice, this means that the kernel can only boot with a plain topology. Any complications will cause problems. The ENUM_TOPOLOGY feature allows the VMM to provide topology information to the guest. Enabling the feature eliminates topology-related #VEs: the TDX module virtualizes accesses to the CPUID leafs and the MSR. Enable ENUM_TOPOLOGY if it is available. Signed-off-by: Kirill A. Shutemov Acked-by: Kai Huang --- arch/x86/coco/tdx/tdx.c | 27 +++++++++++++++++++++++++++ arch/x86/include/asm/shared/tdx.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index f969f4f5ebf8..ca162a7ce4d1 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -249,6 +249,32 @@ static void disable_sept_ve(u64 td_attr) return; } =20 +/* + * TDX 1.0 generates a #VE when accessing topology-related CPUID leafs (0x= B and + * 0x1F) and the X2APIC_APICID MSR. The kernel returns all zeros on CPUID = #VEs. + * In practice, this means that the kernel can only boot with a plain topo= logy. + * Any complications will cause problems. + * + * The ENUM_TOPOLOGY feature allows the VMM to provide topology informatio= n. + * Enabling the feature eliminates topology-related #VEs: the TDX module + * virtualizes accesses to the CPUID leafs and the MSR. + * + * Enable ENUM_TOPOLOGY if it is available. + */ +static void enable_cpu_topology_enumeration(void) +{ + u64 configured; + + /* Has the VMM provided a valid topology configuration? */ + tdg_vm_rd(TDCS_TOPOLOGY_ENUM_CONFIGURED, &configured); + if (!configured) { + pr_err("VMM did not configure X2APIC_IDs properly\n"); + return; + } + + tdg_vm_wr(TDCS_TD_CTLS, TD_CTLS_ENUM_TOPOLOGY, TD_CTLS_ENUM_TOPOLOGY); +} + static void tdx_setup(u64 *cc_mask) { struct tdx_module_args args =3D {}; @@ -280,6 +306,7 @@ static void tdx_setup(u64 *cc_mask) tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL); =20 disable_sept_ve(td_attr); + enable_cpu_topology_enumeration(); } =20 /* diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/share= d/tdx.h index fecb2a6e864b..89f7fcade8ae 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -23,12 +23,14 @@ #define TDCS_CONFIG_FLAGS 0x1110000300000016 #define TDCS_TD_CTLS 0x1110000300000017 #define TDCS_NOTIFY_ENABLES 0x9100000000000010 +#define TDCS_TOPOLOGY_ENUM_CONFIGURED 0x9100000000000019 =20 /* TDCS_CONFIG_FLAGS bits */ #define TDCS_CONFIG_FLEXIBLE_PENDING_VE BIT_ULL(1) =20 /* TDCS_TD_CTLS bits */ #define TD_CTLS_PENDING_VE_DISABLE BIT_ULL(0) +#define TD_CTLS_ENUM_TOPOLOGY BIT_ULL(1) =20 /* TDX hypercall Leaf IDs */ #define TDVMCALL_MAP_GPA 0x10001 --=20 2.45.2