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.65]) by mx.groups.io with SMTP id smtpd.web09.158.1630513074592019784 for ; Wed, 01 Sep 2021 09:17:55 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=lmjYURXy; 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.65, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CZXZzElhxYG6Kq5hRL4XOIMEiQa2wO8GT7EX+VO0iu4lGE/C4h1cwcJN4KorPP+8z50k8+cUKLOED0dIf5SzNnD54wy7YH8g67aR8xP0Ud175FSFLJ9VJ2fEVV/5kIm4ElMWuNUnZRKWVuD3gDVcT2UA2QUCHBX5LvRBPoq2v/XuaJqNy6QvpT9mWZlO/cAY6bncw3VpLGwaLHYbQ2aj+l271AJTtb+vKa9Z1krhUxpcaia74Zlk6Cr1hFY77LhH33Uapl90iphkwJRV8l6ebiqQ2N03xWiIeEs8JVvj0sE5dERu5nCmzx/lbW1WNFRgD+BYWGku/q/wzOH6vk3zrw== 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; bh=Q/7y5WuP4O9pc9aRZ/ATYW4r7uyRxTGbq8b0K+QMSwY=; b=PkZmROi0sy5wX74XTwxKco5Bo/nSCdDe9FHxDQ3G2QH3xqJAjUDfqtu5tFPBrf4vxkTW+2o9BmydibLxnseMyIIrfiabbhlg5hXdGPg/tQNORm2biW2r/3iu8JwgCd5aPZVi1l6sxPvIzcJrzbMIWkXvUVVvBRW4PCCMLcmO9w9G7BQuMVSW0F4uZUNaKA9APKNC2nqAVB6st+RMpUypsj5T3XuDIisqJsTBoVnd3f3VDMpJgNZk9h0SL7PIutsldSHuz6vyVDVS7SRIQeI49HLy3NYSO2hX012N5OfmlXeLz09agz/nA7wnDZegWKWfFd9Va/8z8qRlf8SiN6CTaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=Q/7y5WuP4O9pc9aRZ/ATYW4r7uyRxTGbq8b0K+QMSwY=; b=lmjYURXyTxDbMtBvgXR1aBasgVxHKTIdOcpg9rexGigQSnDASLgvi47RJlyGvlyGeYrEZXVVl4pLVZM4GSpboYi7yUGRWpGa/vh02QF2UnTnMhaDjNlJ6tjwlVDBYN/KontMZH6k5PkG3/hV5eWEBYhG7sDArm4d7YGbHr1oA54= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4512.namprd12.prod.outlook.com (2603:10b6:806:71::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.17; Wed, 1 Sep 2021 16:17:53 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3%6]) with mapi id 15.20.4457.025; Wed, 1 Sep 2021 16:17:53 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io CC: James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Erdem Aktas , Michael Roth , Gerd Hoffmann , Brijesh Singh , Michael Roth Subject: [PATCH v6 25/29] OvmfPkg/MemEncryptSevLib: change the page state in the RMP table Date: Wed, 1 Sep 2021 11:16:42 -0500 Message-ID: <20210901161646.24763-26-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210901161646.24763-1-brijesh.singh@amd.com> References: <20210901161646.24763-1-brijesh.singh@amd.com> X-ClientProxiedBy: SN4PR0501CA0062.namprd05.prod.outlook.com (2603:10b6:803:41::39) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SN4PR0501CA0062.namprd05.prod.outlook.com (2603:10b6:803:41::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.12 via Frontend Transport; Wed, 1 Sep 2021 16:17:53 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 46c404a7-298b-45f4-5bbc-08d96d640d34 X-MS-TrafficTypeDiagnostic: SA0PR12MB4512: X-MS-Exchange-Transport-Forked: True 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: xjJX07jXz4Ww7xJCZPsQUdGVqL9LGJB9rsFB0qqnlYYUjDXD8x1dO9O7a3kNkoPNQVdmV62WSio1mZDp0tT5HQoMDgrnL+dp92jBKIVE5oeam2vIP43tqgBecM3Ny2uDRPIZFuDe6DIKU0Lx7Cj+oiqAQ+wYuDNUuUoqybtwAftxwYMKyyP3vSf69dHIhtx/pSHP+WbIL/J2t6XYJ/Y32zw9MlbOktJhxIid5q208LxLa0AzBYlXw//Y/yTJBXUWNhcyh8ouL9sLp2lXyfwU4zBmZpU8nYoShcrNi3K+1+OxtaqFLHu0otrLPM7DBm8m8R/iO+vRyN1YQWERSAQjrnItDEoUJoJD6Dz+LaiGz7OMWri4fBUruP4o5VIG1dl7CsZSFZALtIqS6khuDpsZW88gQOGfGgT12lBqp2ZOW6XiNFma40d5xkmG4YOCngxZ1HGvOdzxFr4uiOAdXNukPVHiylVZ0V4qf5aVx+3Pmdog1p0Y3B0wJFo6mabOMnJoTQamG9ggGvpCDLd7N93rDQIIy5uTE/Bj/CzqlD9XnUpCd4sH8VHGAiBNMN3II5hM3Qac5UtedIRtkg0dwCf8edB1CSq138A6QnlCBo5UyPXTaCiAzQMOwb9oIuzziMMTpHC4y2Nc6GnF6rGPCuxa1jgMcp13b0wyS/TLfi5YAe/fEyM2zmghDQ5knePnh5CTaeQ3eJCP/1CeVUD1hmlFyR54+ZkPscixjgpwggQtdFF3b5tXA2T+xn0Ap5DI+y4VVNPyRAz69PLBBWl+vbZBmQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(346002)(366004)(39860400002)(136003)(66476007)(66946007)(66556008)(4326008)(478600001)(316002)(26005)(2616005)(956004)(186003)(83380400001)(19627235002)(44832011)(5660300002)(966005)(86362001)(54906003)(7696005)(8936002)(36756003)(2906002)(8676002)(38100700002)(38350700002)(6486002)(6666004)(52116002)(1076003)(6916009);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?WG3IdL/pfh2RZnYuSz0xIXvr/bsX0XtSRoGMMcB+joZLuyK+iXVm+TvQ+fN9?= =?us-ascii?Q?ePP26B+Z/zmwqBbjllb8dWnP99D7xEzCUtPwe5HVvkNPl1IY2F1OSlWjbNRr?= =?us-ascii?Q?WKjwFOZLyaYnAMq6hA/dlcVEacsIONi94ddlRu7DjrLHa7+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DHbKVRin2Y10Dbn/e1qAzQAnoodwAVDOwdxb/u9pqcc?= =?us-ascii?Q?eqLTiYJ/Gb5abOessQG+NnZASvyDwr+EOzZ63U06mkYLuqK2djOFjyRSVQAa?= =?us-ascii?Q?TzYKT5SK5N0c2JPoAURyrlgbdoYR1DVWshLMnbM1G6BqqDz+0E3+eORObH1/?= =?us-ascii?Q?c9pRf5SbinrX66ITD09RFk8VupD9AQa0g9Ely40ji9rVI7QKP3r+EpSNJsmj?= =?us-ascii?Q?DKjf6ooJZFqEOZm0pwe5HfqVc9/xgNZQAOyfQUzcVJ0WuOikF0zIL1lCH+RA?= =?us-ascii?Q?vCV8AxyG1YVT00FF+lxqegPZ?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 46c404a7-298b-45f4-5bbc-08d96d640d34 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Sep 2021 16:17:53.6977 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kKWUpQWMNqb8ehg9uL4MsmUfMTzlvbEr14VgZ4mOIYIozefnn2osgDtAAw6fpRNBpckeDrNs37Gmn69MuT9fWA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4512 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 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.17.1