From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.72]) by mx.groups.io with SMTP id smtpd.web09.15166.1634672420114027810 for ; Tue, 19 Oct 2021 12:40:20 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=Vke7V0h9; 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.220.72, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZMalGtQivm9qcZZmbzYffypEUcwwU2+IYfkX/VmcDXUpO7x2E2r+f28sR7iW5P4u71gv9IdpYyYTBlP/ffmEaOMFVqbJgmyLIrQlpmlb07COwUNv/d2Q7GuX7ZSfgjf+HGgeffyto+F89syVhC+nOAZJIApTREdl7QGriexElu+YqT8DLETT3BPcpJH3KriFSF/MeRZeOsEZ7nqr3sL91pqk3Dd4jotx+LJf9INgZq29+9Wbt3CR8NHSQawxmEUndnawlrepp0BWQozwm3LB+ba1dDVk/KSu0cik5E2tB4gzXXFb58dOIa/g6zgwVYHYU1IOt2/49OJgTtrdLUsm1A== 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=AAcfYlxvUwlQEaM0iBzjpB3n0oarjVEg++IMGJfyqoo=; b=FjmOy1BosaXtAsWijJ3oyh36VTS6YJjG/RhZIhW0Z0YT3A7neUJdosU9fKZ8ou8uDbmS+IS3rKwh2ykOhkjy4QwRmOd+gxim7fQ5c2uXnHPtHcJ9UilVTnHCfeSi7hClGTe8ORLEw7ydzRWE+HvSGAFXfbw0PeQ4coafJXb1cL6aoyHI2TuKNFfcNJzsqEN1A+4seX94+AF6Ca4kPO3d9tKPNgAl7t/8/Pyiea9TiNLMqq2Rg3aYEN6T4IN9yQ7/G4RNTu6Unkf3unzdGu16QDYHECAF3gWqmx1yWgSP8/OVG4VkWOliaWaINiGEyy0ad7PpCZkRshzQ0c4PLNX4BA== 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=AAcfYlxvUwlQEaM0iBzjpB3n0oarjVEg++IMGJfyqoo=; b=Vke7V0h9wAL+6+AOHp94b1EZ+L87rAd2QfMD9AiPUkhVOBGGyqBzRy3eB8RCxyiVbj+6tUi8JCjWIkwMMFEv6NC6tnJMK2DFIiCjuAb8x9bv/VBsDgZ5BHHW21LoJp46HUzHaolD/mG24zkOns/ARIs6omalDZI2eo87qY0OrLI= Received: from MWHPR02CA0005.namprd02.prod.outlook.com (2603:10b6:300:4b::15) by BY5PR12MB5525.namprd12.prod.outlook.com (2603:10b6:a03:1df::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Tue, 19 Oct 2021 19:40:17 +0000 Received: from CO1NAM11FT054.eop-nam11.prod.protection.outlook.com (2603:10b6:300:4b:cafe::ce) by MWHPR02CA0005.outlook.office365.com (2603:10b6:300:4b::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Tue, 19 Oct 2021 19:40:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; 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=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT054.mail.protection.outlook.com (10.13.174.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4608.15 via Frontend Transport; Tue, 19 Oct 2021 19:40:17 +0000 Received: from sbrijesh-desktop.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Tue, 19 Oct 2021 14:40:12 -0500 From: "Brijesh Singh" To: CC: James Bottomley , Min Xu , "Jiewen Yao" , Tom Lendacky , "Jordan Justen" , Ard Biesheuvel , Erdem Aktas , "Michael Roth" , Gerd Hoffmann , Brijesh Singh , Michael Roth , Jiewen Yao Subject: [PATCH v10 28/32] OvmfPkg/MemEncryptSevLib: change the page state in the RMP table Date: Tue, 19 Oct 2021 14:39:30 -0500 Message-ID: <20211019193934.1052465-29-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211019193934.1052465-1-brijesh.singh@amd.com> References: <20211019193934.1052465-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d961e1f9-aac4-4cc2-c6b9-08d993384764 X-MS-TrafficTypeDiagnostic: BY5PR12MB5525: 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: NyqYMswv4hQKa2SDL2pOSrTmlRs4ilHrxKhazbRyziBF3kd1zMK11Q0WK2ne/JtA++oGCt52D6zzvxrgW34AJ9O+Q8cHpFdyT2Dq3GAYqp8JqCgKy2A0PtipJx9bREUERx8AVLA9HJp0AIsYD61CQdfEv8PL9C9suivDAfCxx6h5zu/KbyH2CL5lz1vkzaWY16pK1+rqAFkejEpfvVV0yD59pocej2I/k+Cr3103VG1ht8AmTocNNXUqitbLgKtESOlcgpv7MmI1q+WZ5J60iKTLzCjeWc3xh5L9YNYV0TDyglA//+Xk2T0jYoKuYedCaoKiPcS4EyCgb2QFiSVURPDVyPZJrv0nqWBGgHO7E6OrdChKZHNnuQ0014VEdhoEJMuvAH2/K9XK5GVxjs79qso2ZgFCgqVxky23LYCe5C/GFH5PN+UiOewQUSyr6P9zxCEF+vYFuuIXN6Q1BSri7uUyV2eKze9vsWmVuwMcBhcPlDsXiHKomVE7kZvWCZdgy1G/CjJC+LOF5AVt8bMb3KZEaZiiRtuPiNPcIOcJldzHxckNUAOOBaKp8qtAPXb7i1DXtZ9YgZZ6XX65t7eT+8HcMIp6bicWPIs2e7Vapju04a0pHLIoVyj8+VHhtqPZ6MDqR7/NaPt23SDi2EghLh5mls4LCe+GxeK1COi5iWYiQHCkJ3/vpVlhridK/aKEjSCtowho4dIFKP+q1ZkTLVgYSVwXNhIR92/tZtGtKPpqcHyYAVEcAe9XpgvV3gLXxjwI61T4wIoKyDFXZEMINRO7xEhmBQKRdnWwn6grCrNZSVLbdpMhaq6xtzcuKP0a X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(316002)(5660300002)(54906003)(83380400001)(36756003)(26005)(4326008)(82310400003)(336012)(6666004)(7696005)(36860700001)(6916009)(8936002)(70586007)(508600001)(81166007)(44832011)(426003)(1076003)(47076005)(70206006)(356005)(86362001)(16526019)(186003)(2616005)(2906002)(19627235002)(8676002)(966005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Oct 2021 19:40:17.3345 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d961e1f9-aac4-4cc2-c6b9-08d993384764 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT054.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5525 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 f146f6d61cc5..56db1e4b6ecf 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; @@ -695,10 +696,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 // @@ -751,6 +754,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) { // @@ -923,6 +942,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