From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (NAM12-BN8-obe.outbound.protection.outlook.com [40.107.237.61]) by mx.groups.io with SMTP id smtpd.web10.176.1630513061970038126 for ; Wed, 01 Sep 2021 09:17:42 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=KJwdbD57; 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.237.61, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NdjIewHCh370xvp3xc5Uvg5OR6hzeaY1+Le4izlcSb+LVPBiNuo0/I95SiaAvqjoOThe6D8gNL9pYniJH2x0s9azTsPXLhrgR5wQ7zF5ziscc7HlhjtF95jtYbJRCwuTuRp+BnyUR6B8ofg7CnscR6lv9oJyIEi//+5xOTdsvyHTAFKxmGxMNWmIjIcVeetjZGq3iTmeFVNzJTHEeDx5djGI87Qk93yR3vbClxI1PnGeMJIeLvT/IOvE9jN3KiofYSlJAEWdn5eRQ8Zjw00KE19n6+bt0s4owNUk6czx8PqRpjp8Qmd/hMuBmuljT0hUcZax+4PLftW8vWtzdUmX0g== 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=9UR5HfUUEtckMO2C6o31IQ+Zsog97V8y4w4heB5bNB8=; b=SszaQwGWJfkyX6C+cqWCCVShzVNm9PLL4WnGOZq9YEyf6see6AqM5K4bFDMT5D2GdMMmmEBU9tSsGC31SJ5e9+xRYwjY0piCPVOk/P0aQUnOGMABipzPAf1zIPivgZ2rSOToAe6m+VhP9eKsj4wME02HnqEEr/Ym4j5hoozbzgfoazydh/7410BMJV7d6RH7FvdHMrJ0yyB9qlXI9l9ynLo8CnMOO3RAMGbfH+rIZlZ/XaiHBid//BV2Om6brW5rZpVXxCNi06oJ3FR4Dl9eJISxomvm0PpKV35jvw1pu/Aat4sh8HMSJ4Us3t1HLqLT/s3YuMRbpIXNW7/bEu92Og== 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=9UR5HfUUEtckMO2C6o31IQ+Zsog97V8y4w4heB5bNB8=; b=KJwdbD57cSY8b3e4+Nm8hpK92AMqA8I9oeOI2VKqIhytwsA6rWm8TVTnY3TR72n25rnlbCyurZhI6tNhC97iNehUlvL4ok/XU6vRSnTFjNQNRmOhlrhJEgdx0DlkZJImXdKISgpE6HdkziXAGYgaQJPvH/VApjMiHWIpsglmuZA= 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 SA0PR12MB4415.namprd12.prod.outlook.com (2603:10b6:806:70::17) 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:37 +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:37 +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 04/29] OvmfPkg/ResetVector: invalidate the GHCB page Date: Wed, 1 Sep 2021 11:16:21 -0500 Message-ID: <20210901161646.24763-5-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:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7205944d-12ca-4349-0f8d-08d96d640342 X-MS-TrafficTypeDiagnostic: SA0PR12MB4415: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wyo99FRQG9owwvLjljr5DtosDhxjFs68xIqp+gR/ljf7mgvHkIuCFLIo3+Md6rSQhbZyH0coDRtS1LpqYGk1fZ5JSRD/TgSUeidwl8z15LqyshlVGub7kJVv2ocUw85VjXXiwrTVWSrxH3jlS7CUi1kXlcPBsymR8q7qxAazFbSl2rg85kyfESK+MIySqwyBxI+TP+f0p4ok4zvHlpybGRQ1WuHVrwJwLZTNIwcyT/Kes3cil+LwObNd/6hunVqCQMlJgmDXu1PIAPTDcfzBGFa/s14++OqgIMl4S4opVSm7HgBS1AlDGTL2kk2fnLn16CEzXOIVg5uovkWyETFrrJscrSb1ScI5DVdv1eLFR0mI3UfTfx6ORmaxd894+WHUs18l/B92jQY7T3gnFX9ej2ERZH+tEv7B0UfLpuRNVLMPUuWZa3K5HarGhrSMxEUxDYeNDatH9Wz3mIqd43z3JQnNJnQlVlZ+Ktim+/EWvzoCFoeFuDX83qCAXdXT1rVRNWGBC95RljlfFzUMq9pIjfHReA+wYwnVloyzcCGmL/AJqXmn9nJBzr3Bz+84HLDB9ZuFRqMgBvW2UUS47SCZLbn75rz4JcogijMjXZNQyf/rFmMo39pmZu9tp5Ia9xKd+Pk/eR6vUTVGn+WvXiLTxHWjgiSCNE0kBMxXRbben0259ICZ/q+DzwyyEoR/NOA6iQQ/UEBHXZ4GSX2xPX+V92eMyfRrqVCPqwK6cr8deCgOaz1dzfGKBCtE6NYv/dP9QC3ZgceQYFE4FvXo+gZ+lTludLnSwoYb879DUQAhnas= 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)(39860400002)(376002)(136003)(396003)(346002)(366004)(66476007)(1076003)(83380400001)(6916009)(2906002)(8936002)(8676002)(186003)(26005)(66946007)(86362001)(19627235002)(66556008)(316002)(478600001)(966005)(44832011)(2616005)(54906003)(6486002)(5660300002)(36756003)(956004)(4326008)(6666004)(7696005)(52116002)(38350700002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?vieAVMR9mDNhHhnIB7XaKHqdMC8uG17Ehw0lazhIcZCtZexAdCGX+aG/Z14R?= =?us-ascii?Q?w3lnxqEsrC06My2M6tUoU/IlrLZQzquhWGOyQtFcpELLbhfx3V68HKPdqf/0?= =?us-ascii?Q?f866kBa+FfRhQKHscWYTecJia0RE5bJL9s/1o3NCO8pdv96VYCSsOcCQeSl7?= =?us-ascii?Q?G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KbtLOaJSy81Dj?= =?us-ascii?Q?NT5mGu+c08TqjEw2C89nug0BE2tyBkAQIqqPz1CORrupihkS5kcn1FIXt+Zx?= =?us-ascii?Q?rmBLkAhbWCUrYerMNyqgOxVxcYi/4lOgcShL2UvFnjYbhC3H4VwTeQjp/amm?= =?us-ascii?Q?8LRQ8mY/acbhHJdGzOkitVmucvDUZVLhzDrxjxVKG4GztYTl38zY3/Ct1yIA?= =?us-ascii?Q?GUTcinWV8mhOeEpxBoJ72pCjwuko2fXRPuVQAPhiYQlv10pUbH+QdciYksTC?= =?us-ascii?Q?jLJg7QvFjD0LbftH34FzVJw5?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7205944d-12ca-4349-0f8d-08d96d640342 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:36.9793 (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: DfGzOcLScMr4k0RjG+EzZlFtYfr+McO3NHA4uWrIV5CJ8p9N9lFDygwoDptCAl9fU5gSz2lUy4Z2rMEhuIZlOw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4415 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 The GHCB page is part of a pre-validated memory range specified through the SnpBootBlock GUID. When SEV-SNP is active, the GHCB page is pre-validated by the hyperivosr during the SNP guest creation. On boot, the reset vector maps the GHCB page as un-encrypted in the initial page table. Just clearing the encryption attribute from the page table is not enough. To maintain the security guarantees, the page must be invalidated. The page invalidation consists of two steps: 1. Use the PVALIDATE instruction to clear Validated Bit from the RMP table. 2. Use the Page State Change VMGEXIT to ask hypervisor to change the page state to shared in the RMP table. 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 --- OvmfPkg/ResetVector/Ia32/AmdSev.asm | 137 ++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/OvmfPkg/ResetVector/Ia32/AmdSev.asm b/OvmfPkg/ResetVector/Ia32= /AmdSev.asm index 250ac8d8b180..0ac78c73c370 100644 --- a/OvmfPkg/ResetVector/Ia32/AmdSev.asm +++ b/OvmfPkg/ResetVector/Ia32/AmdSev.asm @@ -7,6 +7,8 @@ ; ;-------------------------------------------------------------------------= ----- =20 +%include "Nasm.inc" + BITS 32 =20 ; @@ -65,6 +67,25 @@ BITS 32 PAGE_READ_WRITE + \ PAGE_PRESENT) =20 +; SNP page state change failure +%define TERM_PAGE_STATE_CHANAGE 3 + +; Hypervisor does not support SEV-SNP feature +%define TERM_HV_UNSUPPORTED_FEATURE 4 + +; GHCB SEV Information MSR protocol +%define GHCB_SEV_INFORMATION_REQUEST 2 +%define GHCB_SEV_INFORMATION_RESPONSE 1 + +; GHCB Page Invalidate request and response protocol values +; +%define GHCB_PAGE_STATE_CHANGE_REQUEST 20 +%define GHCB_PAGE_STATE_CHANGE_RESPONSE 21 +%define GHCB_PAGE_STATE_SHARED 2 + +; GHCB Hypervisor features MSR protocol +%define GHCB_HYPERVISOR_FEATURES_REQUEST 128 +%define GHCB_HYPERVISOR_FEATURES_RESPONSE 129 =20 ; Macro is used to issue the MSR protocol based VMGEXIT. The caller is ; responsible to populate values in the EDX:EAX registers. After the vmmca= ll @@ -183,6 +204,19 @@ clearGhcbMemoryLoop: mov dword[ecx * 4 + GHCB_BASE - 4], eax loop clearGhcbMemoryLoop =20 + ; + ; The page table built above cleared the memory encryption mask from t= he + ; GHCB_BASE (aka made it shared). When SEV-SNP is enabled, to maintain + ; the security guarantees, the page state transition from private to + ; shared must go through the page invalidation steps. Invalidate the + ; memory range before loading the page table below. + ; + ; NOTE: the invalidation must happen after zeroing the GHCB memory. Th= is + ; is because, in the 32-bit mode all the access are considered p= rivate. + ; The invalidation before the zero'ing will cause a #VC. + ; + OneTimeCall InvalidateGHCBPage + SevClearPageEncMaskForGhcbPageExit: OneTimeCallRet SevClearPageEncMaskForGhcbPage =20 @@ -334,6 +368,109 @@ SevExit: =20 OneTimeCallRet CheckSevFeatures =20 +; The version 2 of GHCB specification added the support to query the hyper= visor +; features. If the GHCB version is >=3D2 then read the hypervisor features= and +; verify that SEV-SNP feature is supported. +; +CheckSnpHypervisorFeatures: + ; Get the SEV Information + xor eax, eax + xor edx, edx + + VmgExit GHCB_SEV_INFORMATION_REQUEST, GHCB_SEV_INFORMATION_RESPONSE + + ; + ; SEV Information Response GHCB MSR + ; GHCB_MSR[63:48] =3D Maximum protocol version + ; GHCB_MSR[47:32] =3D Minimum protocol version + ; + shr edx, 16 + cmp edx, 2 + jl SevSnpUnsupportedFeature + + ; Get the hypervisor features + xor eax, eax + xor edx, edx + + VmgExit GHCB_HYPERVISOR_FEATURES_REQUEST, GHCB_HYPERVISOR_FEATURES_RES= PONSE + + ; + ; Hypervisor features reponse + ; GHCB_MSR[63:12] =3D Features bitmap + ; BIT0 =3D SEV-SNP Supported + ; + shr eax, 12 + bt eax, 0 + jnc SevSnpUnsupportedFeature + +CheckSnpHypervisorFeaturesDone: + OneTimeCallRet CheckSnpHypervisorFeatures + +; If its an SEV-SNP guest then use the page state change VMGEXIT to invali= date +; the GHCB page. +; +; Modified: EAX, EBX, ECX, EDX +; +InvalidateGHCBPage: + ; Check if SEV-SNP is enabled + ; MSR_0xC0010131 - Bit 2 (SEV-SNP enabled) + mov ecx, SEV_STATUS_MSR + rdmsr + bt eax, 2 + jnc InvalidateGHCBPageDone + + ; Verify that SEV-SNP feature is supported by the hypervisor. + OneTimeCall CheckSnpHypervisorFeatures + + ; Use PVALIDATE instruction to invalidate the page + mov eax, GHCB_BASE + mov ecx, 0 + mov edx, 0 + PVALIDATE + + ; Save the carry flag to be use later. + setc dl + + ; If PVALIDATE fail then abort the launch. + cmp eax, 0 + jne SevSnpPageStateFailureTerminate + + ; Check the carry flag to determine if RMP entry was updated. + cmp dl, 0 + jne SevSnpPageStateFailureTerminate + + ; Ask hypervisor to change the page state to shared using the + ; Page State Change VMGEXIT. + ; + ; Setup GHCB MSR + ; GHCB_MSR[55:52] =3D Page Operation + ; GHCB_MSR[51:12] =3D Guest Physical Frame Number + ; + mov eax, (GHCB_BASE >> 12) + shl eax, 12 + mov edx, (GHCB_PAGE_STATE_SHARED << 20) + + VmgExit GHCB_PAGE_STATE_CHANGE_REQUEST, GHCB_PAGE_STATE_CHANGE_RESPON= SE + + ; + ; Response GHCB MSR + ; GHCB_MSR[63:12] =3D Error code + ; + cmp edx, 0 + jnz SevSnpPageStateFailureTerminate + +InvalidateGHCBPageDone: + OneTimeCallRet InvalidateGHCBPage + +; Terminate the SEV-SNP guest due to the page state change failure +SevSnpPageStateFailureTerminate: + TerminateVmgExit TERM_PAGE_STATE_CHANAGE + +; Terminate the SEV-SNP guest because hypervisor does not support +; the SEV-SNP feature +SevSnpUnsupportedFeature: + TerminateVmgExit TERM_HV_UNSUPPORTED_FEATURE + ; Start of #VC exception handling routines ; =20 --=20 2.17.1