From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.43]) by mx.groups.io with SMTP id smtpd.web11.6945.1639020566617812919 for ; Wed, 08 Dec 2021 19:29:26 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=H8EfluvQ; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.92.43, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PyhFknOm8LHz+zSdJ0Sb8WQS90R+LEhCip1NkwlJyHSI6ehTKm2fD4/1NWNRWRVVYhEllb283BFjOlbBWURZhO0dgwK0mjz7qwbu9xBdJ4HNOzIAewAzGrWPgFD3xR9XK/OVdRV1CajdUeBTvmJOFNhtoKccjeRSYAz29xiDa/6JeBxDIppWcFmj/JILPOLEAOw6CTcsBODrWBRN4s81xg0dcyesjNcH+AsGerZGgTOZge9/Od+PktK/UGu+OQrDM52pY9Ld3PFzFNitW854wfrRy/27q1XQAtkH/CeF/w7kuC6VDIsVzZTNkcK54HZFim8dgBJJ4VqJzWarMO3evg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=U5IHDWwx5eEvEn+S9y8BqdCO1Vpl2Bp91uBmvKjsET0=; b=KvmDGP/rWGqojJ9hF+7MqkChP9jMaTHUlZ5+Q+4cQX46TrsMsfVTZWDBTXKth1uW4TAm3ekmSnIuVfZI4qisULzkO+XjvJdspGOMaYNq+42l8bRQP9Exsge3hH4ULHfP9kh00TwYTJeNH0D9POInG+EAJ1zXjap92oc/SjvUEocrDGgSMO1jWE06BDS+yaOY9V41ObNncgAlSPPs7qAdWRbZrcyw4ySmz8qwNqpTdykk4X2p2VenJkM7AEY9QbmeYrSzINZ52efACCy4FbYqV+kIBVRo0UgeRhU8UT7RKQRt9pY4OuARrCJyuEa/yb3RWP7ZUPQ0j8qUWP/VG61xOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=U5IHDWwx5eEvEn+S9y8BqdCO1Vpl2Bp91uBmvKjsET0=; b=H8EfluvQ54CJqITSLrXqQBKUnojvbr0d+6FmPxaT48onI+R1XmaE3fN8Dr7HdDS0+B5xrqEXFCdMPEm881QfMhB23I3L3tHfMEJwmfd9BZTMx8OG0erG7Zui7STzfJoUxdvEtXEzbLJiO0q1WPz8SJ0caN57C6dB1dWURPIQ+ag= Received: from CO2PR04CA0201.namprd04.prod.outlook.com (2603:10b6:104:5::31) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.19; Thu, 9 Dec 2021 03:29:23 +0000 Received: from CO1NAM11FT031.eop-nam11.prod.protection.outlook.com (2603:10b6:104:5:cafe::5a) by CO2PR04CA0201.outlook.office365.com (2603:10b6:104:5::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12 via Frontend Transport; Thu, 9 Dec 2021 03:29:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1NAM11FT031.mail.protection.outlook.com (10.13.174.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4755.13 via Frontend Transport; Thu, 9 Dec 2021 03:29:23 +0000 Received: from sbrijesh-desktop.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Wed, 8 Dec 2021 21:29:20 -0600 From: "Brijesh Singh" To: CC: James Bottomley , Min Xu , "Jiewen Yao" , Tom Lendacky , "Jordan Justen" , Ard Biesheuvel , Erdem Aktas , "Michael Roth" , Gerd Hoffmann , "Michael D Kinney" , Liming Gao , Zhiguang Liu , Ray Ni , Rahul Kumar , Eric Dong , Brijesh Singh , Michael Roth , Jiewen Yao Subject: [PATCH v14 28/32] OvmfPkg/MemEncryptSevLib: change the page state in the RMP table Date: Wed, 8 Dec 2021 21:27:56 -0600 Message-ID: <20211209032800.3802995-29-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211209032800.3802995-1-brijesh.singh@amd.com> References: <20211209032800.3802995-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d75a11c9-ba06-4ef3-0e63-08d9bac4183e X-MS-TrafficTypeDiagnostic: SA0PR12MB4574:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8JZttarudDzvB8usqvWWgxjx2oUKpvlCfy86FSCLZLJU3frFMf1BJYZSVV7NDLvsa1qmMtpinQNOaJM4tAzLkI/fNJK7bdXI/PN89JkOs8uklkrM05bXUkJBGepx+rZ9vQUrq5eSnc/8mHYy+JMIPfGbThFfm97kexIzcOqIZLYuZxPDXcxR62W4scpxvs+xH8sjba9rxeVAphgJJKo/ml9/FzpSTLCUZmUKyeYwgugqJQNTq+KYH1LNYHSeYBw+V4Vbk/WD/86zjCa702CKCwSFLqydEt1xGhXR+Sw4YBnEme6t0tMPfdeL88jH6lCGfLoTTkZHiKuiqUHF7wXji+C6M3uZpIjdBHELzHsI+jsyg2uy3VPrJdCiBPyDyPW+DXDzX7eOaI3ZKWWMqfaVdgCsdZfXyARoZB4/wdfAosLbiur0v0DHS7d59CjKzlo+lSilT9XSqXiAaNU+ak6OMrewBb3H7i1PytBt/8ft9JNSIqUd2oIyWpJ7mxP+vkBr144rPBQuBTwNvc9++aOM+AZFcToqdFSfA8k2SY99Qg2dn68oq/BZQKLxUkwc2zW65T4We1yTxkCwVQB4i1kdauCPAcnDKmRKKhbuGwuJEe2njL3Pfv9jtqa2PrmqCTAuUlISsM1/3gjwPWKeENrOO8p3Uehmx/ifzAhGVutw7UboN9kYipVM+WuaDo5ERvUcK0N791idgjQ13r658ylM5VlhkdMKJDQ1DPD8CCbKNmKMYk8Q1z8tEq5ZSByR1qqm/2A0RfiUJL2ye9QST8SOfSlbLIKJPCFKE/ddINvor9k5RiNnajG7brgdUfp4sLWxMewbExGLWub33hBGC9alM2Vk9zprGHnanI6C1M71QLRyiJKweEMJRR4griuEXOW6 X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(40470700001)(19627235002)(8936002)(5660300002)(508600001)(47076005)(336012)(316002)(186003)(26005)(36756003)(966005)(70206006)(8676002)(2906002)(426003)(54906003)(44832011)(2616005)(83380400001)(7696005)(82310400004)(356005)(40460700001)(86362001)(36860700001)(6916009)(6666004)(1076003)(4326008)(7416002)(16526019)(81166007)(70586007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2021 03:29:23.1149 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d75a11c9-ba06-4ef3-0e63-08d9bac4183e X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT031.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 The MemEncryptSev{Set,Clear}PageEncMask() functions are used to set or clear the memory encryption attribute in the page table. When SEV-SNP is active, we also need to change the page state in the RMP table so that it is in sync with the memory encryption attribute change. Cc: Michael Roth Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Erdem Aktas Cc: Gerd Hoffmann Acked-by: Jiewen Yao Acked-by: Gerd Hoffmann Signed-off-by: Brijesh Singh --- .../X64/PeiDxeVirtualMemory.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c= b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index f1485722f7cf..814f814035fa 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -17,6 +17,7 @@ #include =20 #include "VirtualMemory.h" +#include "SnpPageStateChange.h" =20 STATIC BOOLEAN mAddressEncMaskChecked =3D FALSE; STATIC UINT64 mAddressEncMask; @@ -693,10 +694,12 @@ SetMemoryEncDec ( PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry; PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry; PAGE_TABLE_ENTRY *PageDirectory2MEntry; + PHYSICAL_ADDRESS OrigPhysicalAddress; PAGE_TABLE_4K_ENTRY *PageTableEntry; UINT64 PgTableMask; UINT64 AddressEncMask; BOOLEAN IsWpEnabled; + UINTN OrigLength; RETURN_STATUS Status; =20 // @@ -749,6 +752,22 @@ SetMemoryEncDec ( =20 Status =3D EFI_SUCCESS; =20 + // + // To maintain the security gurantees we must set the page to shared in = the RMP + // table before clearing the memory encryption mask from the current pag= e table. + // + // The InternalSetPageState() is used for setting the page state in the = RMP table. + // + if ((Mode =3D=3D ClearCBit) && MemEncryptSevSnpIsEnabled ()) { + InternalSetPageState (PhysicalAddress, EFI_SIZE_TO_PAGES (Length), Sev= SnpPageShared, FALSE); + } + + // + // Save the specified length and physical address (we need it later). + // + OrigLength =3D Length; + OrigPhysicalAddress =3D PhysicalAddress; + while (Length !=3D 0) { // // If Cr3BaseAddress is not specified then read the current CR3 @@ -922,6 +941,21 @@ SetMemoryEncDec ( // CpuFlushTlb (); =20 + // + // SEV-SNP requires that all the private pages (i.e pages mapped encrypt= ed) must be + // added in the RMP table before the access. + // + // The InternalSetPageState() is used for setting the page state in the = RMP table. + // + if ((Mode =3D=3D SetCBit) && MemEncryptSevSnpIsEnabled ()) { + InternalSetPageState ( + OrigPhysicalAddress, + EFI_SIZE_TO_PAGES (OrigLength), + SevSnpPagePrivate, + FALSE + ); + } + Done: // // Restore page table write protection, if any. --=20 2.25.1