From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (NAM12-DM6-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web12.10872.1591363730710345513 for ; Fri, 05 Jun 2020 06:28:51 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=xm3qIUI0; spf=none, err=SPF record not found (domain: amd.com, ip: , mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Rr7yCuluATAWgmfy31P4//pMrTmSHw39isFDT5r92pL1j47xYZuPP/MHOD7EPjinUFvgqJBKmZWbSPOBsYENrPKFc66x14ghq9cQqr3PPS4jxnVT28IefpQA3Nr54AuUNAcA5jgzgVhaBXenEsCVGTuNcb0GmIQB9RdiyjP5eYhHrHSTAk6S5sLD9JV6cRucpbc+m9b7/cN7B4Uaklju6yf6F5gb3RMpDWvRQp4WybfWEES7OTFmE/OX1ip3fNM9KRJvvPdSuE8srPTKy09VxmxwS5SzdsKsCbR+YDXKhvJvO22DYT/XfKUiuF38cvRioaHSssP+eQD3cHB8NiqCeg== 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-SenderADCheck; bh=7dol9obmJ67i1qb1vIU9wSBgA5TILh2rR1rYfx3A27A=; b=K6rP3RaZXcLK0CM524j8ZEQqPRzBdBl6rznF46UQpIxli1rqC123IOdj0CYh+PsRr4CZajXc9jaTpEA6zyYGVDwMuTXI5/L6xZWIh0x22DcobpCcwQsyQiSHio7vp7FNkQ/XRUAZKWizfGj64j+XPG1zqX9LyiTdmL/NGn9dBPaSBXACy+GsYGts+cGXqmXtYlUYsWs476j1HJl9H1LgxJbVmthKt9i1I10qQAYXmIh7GLjsJNW8DUjd1I0Qe6zA95odEgkCYPtxIGr+qI98ctrJhLKJMbR69OhajwVYkNm5Y3OiPV0QNe9H4W8uSwAQ7TYbLPkF/o9JQDylHQnYhQ== 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=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7dol9obmJ67i1qb1vIU9wSBgA5TILh2rR1rYfx3A27A=; b=xm3qIUI0mWRgYYDiK+BBrKX/PjmPBY9CTGG8xtT6WHSLb3XZslnxzw7YyZYgUfNShXg8wk98QdfLTj+jIX71pO0w7a/I94LtVzgixIf75zNz0TaMFbZr9e3jLjRgOYAeP+QwsKFDZ2atpLVhbc5F3HD50Srh4tCrwcHgsH7z0cw= 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 DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR12MB1354.namprd12.prod.outlook.com (2603:10b6:3:7a::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3066.20; Fri, 5 Jun 2020 13:28:49 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1%10]) with mapi id 15.20.3066.019; Fri, 5 Jun 2020 13:28:49 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io Cc: Brijesh Singh , Ard Biesheuvel , Eric Dong , Jordan Justen , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni Subject: [PATCH v9 31/46] OvmfPkg: Create GHCB pages for use during Pei and Dxe phase Date: Fri, 5 Jun 2020 08:27:22 -0500 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: DM5PR19CA0060.namprd19.prod.outlook.com (2603:10b6:3:116::22) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from tlendack-t1.amd.com (165.204.77.1) by DM5PR19CA0060.namprd19.prod.outlook.com (2603:10b6:3:116::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3066.18 via Frontend Transport; Fri, 5 Jun 2020 13:28:19 +0000 X-Mailer: git-send-email 2.27.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f1c2ff58-f861-466d-0f4c-08d80954501c X-MS-TrafficTypeDiagnostic: DM5PR12MB1354: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-Forefront-PRVS: 0425A67DEF X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: s2Y/SWPC09bvtzIquyU+ikjeQFSlVEdiyS4dOHVfoJ7gXhemQT4ugGOGC3Yfs5Pt7FXnCeX1awXlnar2dsDhivIQVm67yxwb8xdIABs0fzXvK/yk8NzQzAmVR8MhnZRlYuyyNAYgsOPDTeqlLxsFkeF8wJpAcnOGvps5Db9XxI7zEUj1i8GObYrd1WMCq4EMn78pNo8lcxoZAXIttKPN1I+JRReypgnKGsUxp8hFEEb3CA0hon3u3mL9uWSuGFxGcTt7Ga0d/LrSqfC2UMkTwKSuU3/tGHWPwASfeNmhdq1bOXuZc6R2dXjHO8CT0Ewmv6O9a486CB9O4JLmtYw3IYi1XFDHgUxq3r37jccFXtT0cq8geRNcvOGNDt9gggsa/f5cr1fys6bD7ewxJOvZnS3CzbRA5arfXvMTi3eRAMe703Iw92hrABlMrlr+IN5i7wm/s32L5Yf9VSnHiOBhCA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(346002)(39860400002)(376002)(366004)(396003)(136003)(52116002)(83380400001)(26005)(316002)(19627235002)(7696005)(6666004)(54906003)(6486002)(478600001)(956004)(16526019)(86362001)(4326008)(36756003)(2616005)(966005)(6916009)(2906002)(8936002)(5660300002)(8676002)(66476007)(66946007)(66556008)(186003)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: BPpvZ4Vh2v/WEwfxxlfzkfoJTnuICf0cCeFfTGtA/f1zmy7KEpXE+See5V0WcjoRTpLxwJ9mSw1zzkCPDRKZ7ef+YlMhpYV/n68icts7i5Rlk499v6j9JgqrFTKppZuj+ndWYQXNEce9y7MGzKIzaYvi5RmCdTqKWRgCVR0ZCcWjNfGVRvumGVK521iCPOQs+SZPrKluRUoyC801dcsQs1k88aCLOoydTmI9dFCcvHiOUJm9eoLLw5x8s6u/SK9SYeZ5wm3LUaVPchtrzEw9BsgjFd1x6uzVT+Bz1zTSONscDvHtq30ewH3j4Clv9p2J88zpbHptrNABGKABOIV7NSzaPLYarbo/ZNWQuyr9zt8eitUQVWRWRjgZ57u1tcHrBKyZrLxog2aGn5+hM37c2EVWV8ZLOjj5/jf1tufU2+Mf1SOvRqEMyGRQ9AOXCPNA/xl6VrWowPThHKtc1frsoZth9z3OIeb87wjsec3+tig= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f1c2ff58-f861-466d-0f4c-08d80954501c X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Jun 2020 13:28:20.0913 (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: lIqPSCHGZhxFMX8p50rdAA2ClKqD5OCjmig8e35Vs5CarF11pCH7yq3gfqNmIF5tjvAfO5ZZksR+Srn6Gz4yMQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1354 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 Allocate memory for the GHCB pages and the per-CPU variable pages during SEV initialization for use during Pei and Dxe phases. The GHCB page(s) must be shared pages, so clear the encryption mask from the current page table entries. Upon successful allocation, set the GHCB PCDs (PcdGhcbBase and PcdGhcbSize). The per-CPU variable page needs to be unique per AP. Using the page after the GHCB ensures that it is unique per AP. Only the GHCB page is marked as shared, keeping the per-CPU variable page encyrpted. The same logic is used in DXE using CreateIdentityMappingPageTables() before switching to the DXE pagetables. The GHCB pages (one per vCPU) will be used by the PEI and DXE #VC exception handlers. The #VC exception handler will fill in the necessary fields of the GHCB and exit to the hypervisor using the VMGEXIT instruction. The hypervisor then accesses the GHCB associated with the vCPU in order to perform the requested function. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Reviewed-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/OvmfPkgIa32.dsc | 2 + OvmfPkg/OvmfPkgIa32X64.dsc | 2 + OvmfPkg/OvmfPkgX64.dsc | 2 + OvmfPkg/PlatformPei/PlatformPei.inf | 2 + OvmfPkg/PlatformPei/AmdSev.c | 45 +++++++++++++++++++- 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index b289260c1f2d..f849a7d698cd 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -611,6 +611,8 @@ [PcdsDynamicDefault] gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask|0x0=0D =0D # Set SEV-ES defaults=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase|0=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbSize|0=0D gUefiCpuPkgTokenSpaceGuid.PcdSevEsIsEnabled|0=0D =0D !if $(SMM_REQUIRE) =3D=3D TRUE=0D diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 00faf5036670..d1b4ffedc6af 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -623,6 +623,8 @@ [PcdsDynamicDefault] gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask|0x0=0D =0D # Set SEV-ES defaults=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase|0=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbSize|0=0D gUefiCpuPkgTokenSpaceGuid.PcdSevEsIsEnabled|0=0D =0D !if $(SMM_REQUIRE) =3D=3D TRUE=0D diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 58671828c5fe..ebc903d922cf 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -621,6 +621,8 @@ [PcdsDynamicDefault] gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask|0x0=0D =0D # Set SEV-ES defaults=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase|0=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbSize|0=0D gUefiCpuPkgTokenSpaceGuid.PcdSevEsIsEnabled|0=0D =0D !if $(SMM_REQUIRE) =3D=3D TRUE=0D diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/Plat= formPei.inf index a54d10ba90d5..4742e1bdf42b 100644 --- a/OvmfPkg/PlatformPei/PlatformPei.inf +++ b/OvmfPkg/PlatformPei/PlatformPei.inf @@ -102,6 +102,8 @@ [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack=0D gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable=0D gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbSize=0D gEfiSecurityPkgTokenSpaceGuid.PcdOptionRomImageVerificationPolicy=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuLocalApicBaseAddress=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber=0D diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index 4dc5340caa7a..4fd4534cabea 100644 --- a/OvmfPkg/PlatformPei/AmdSev.c +++ b/OvmfPkg/PlatformPei/AmdSev.c @@ -10,12 +10,15 @@ // The package level header files this module uses=0D //=0D #include =0D +#include =0D #include =0D #include =0D #include =0D +#include =0D #include =0D #include =0D #include =0D +#include =0D #include =0D #include =0D =0D @@ -32,7 +35,10 @@ AmdSevEsInitialize ( VOID=0D )=0D {=0D - RETURN_STATUS PcdStatus;=0D + VOID *GhcbBase;=0D + PHYSICAL_ADDRESS GhcbBasePa;=0D + UINTN GhcbPageCount, PageCount;=0D + RETURN_STATUS PcdStatus, DecryptStatus;=0D =0D if (!MemEncryptSevEsIsEnabled ()) {=0D return;=0D @@ -40,6 +46,43 @@ AmdSevEsInitialize ( =0D PcdStatus =3D PcdSetBoolS (PcdSevEsIsEnabled, TRUE);=0D ASSERT_RETURN_ERROR (PcdStatus);=0D +=0D + //=0D + // Allocate GHCB and per-CPU variable pages.=0D + //=0D + GhcbPageCount =3D mMaxCpuCount * 2;=0D + GhcbBase =3D AllocatePages (GhcbPageCount);=0D + ASSERT (GhcbBase !=3D NULL);=0D +=0D + GhcbBasePa =3D (PHYSICAL_ADDRESS)(UINTN) GhcbBase;=0D +=0D + //=0D + // Each vCPU gets two consecutive pages, the first is the GHCB and the=0D + // second is the per-CPU variable page. Loop through the allocation and= =0D + // only clear the encryption mask for the GHCB pages.=0D + //=0D + for (PageCount =3D 0; PageCount < GhcbPageCount; PageCount +=3D 2) {=0D + DecryptStatus =3D MemEncryptSevClearPageEncMask (=0D + 0,=0D + GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount),=0D + 1,=0D + TRUE=0D + );=0D + ASSERT_RETURN_ERROR (DecryptStatus);=0D + }=0D +=0D + ZeroMem (GhcbBase, EFI_PAGES_TO_SIZE (GhcbPageCount));=0D +=0D + PcdStatus =3D PcdSet64S (PcdGhcbBase, GhcbBasePa);=0D + ASSERT_RETURN_ERROR (PcdStatus);=0D + PcdStatus =3D PcdSet64S (PcdGhcbSize, EFI_PAGES_TO_SIZE (GhcbPageCount))= ;=0D + ASSERT_RETURN_ERROR (PcdStatus);=0D +=0D + DEBUG ((DEBUG_INFO,=0D + "SEV-ES is enabled, %lu GHCB pages allocated starting at 0x%p\n",=0D + (UINT64)GhcbPageCount, GhcbBase));=0D +=0D + AsmWriteMsr64 (MSR_SEV_ES_GHCB, GhcbBasePa);=0D }=0D =0D /**=0D --=20 2.27.0