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 []) by mx.groups.io with SMTP id smtpd.web12.10214.1619783548337545009 for ; Fri, 30 Apr 2021 04:52:31 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=nfsGpsCM; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: , mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jTDSzk4ds4X79kiEiXYvKSx2v1JrzjCwBOJ6I6fjwcdP0I/mDPvYToUTC59wlbdKOkx1r7xvLfPjUxK4EXhFtPNXx9yVW9UCHFISJjBui1bLSJ+iUSxPKtPqrgw5ED5KlNjERvmdA/D1AaUbkgpSBmw7LBOv0TDWR/xb0vpPM4RSCvgSwp1VLd6oYUngIR6bKBSwBVvTyKGE32yMCKjsqEqEz1DW425iiuB1OgH+kE7/QlfDRppm4rXY+h451+4wYlM8+zeFpUnUWkKUQPyT+06rB09XCnpH1GrNmhxBBHjt7RZm7+zmYnCLtwSU3dkA8oZS5KYSj0cI0N4JQpl3xw== 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=guEyDV7kLmBzRc+fmqqaIgJ0wzO2wWG8fiqn9KwwGak=; b=M3SOw09bHiAjcqsytcn8uDn/iSw0xFbB+n/KNuD0iqK1KvK1aUqzuS2ngwCBcZU0RXD8g87/C+Ex7mTMR4W4PIM/Dg1U/QM0g90l3l9YjQBuqL0WYMlYKzMeq7eOTpgTMPG6utzyp1ickysbs8crjGqZ3qiq1KV3lzXIgbtjIHf/u7gndjaOsHLbe5TiW6XQMAy+Z5vI0ue+7nQo2WNPNTx6FcNsnHiMRRcClzE1OCbpcRVP+pbJT0ecndgScw1brstVoTKURGpCgrBki6D+kkXvqF8dLwL233BCRo4lxIotvI7zfZRECZtp+3dzx5z5fpO+FEcrvlQuEk8zPqzAqA== 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=guEyDV7kLmBzRc+fmqqaIgJ0wzO2wWG8fiqn9KwwGak=; b=nfsGpsCMenOvMLrs2MCJP5fnXS1GH2+NhySLJTHR99nKuqTlvZKcjc8/DxCwPo8bZ71KHXO/giYYZQTu/Gv84Kv79FY5KQqVPeJtvbMnOIOPDVCmU+6P0AGLZwBuht4FNlE48lLJiFHZmZP0ggcwsuJRktD5iuHAXupEw/OJct8= 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 SN6PR12MB2783.namprd12.prod.outlook.com (2603:10b6:805:78::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.25; Fri, 30 Apr 2021 11:52:26 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4065.027; Fri, 30 Apr 2021 11:52:26 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io Cc: Brijesh Singh , James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Laszlo Ersek , Erdem Aktas Subject: [PATCH RFC v2 17/28] OvmfPkg/ResetVector: Invalidate the GHCB page Date: Fri, 30 Apr 2021 06:51:37 -0500 Message-Id: <20210430115148.22267-18-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210430115148.22267-1-brijesh.singh@amd.com> References: <20210430115148.22267-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR12CA0006.namprd12.prod.outlook.com (2603:10b6:806:6f::11) 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 SA0PR12CA0006.namprd12.prod.outlook.com (2603:10b6:806:6f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.27 via Frontend Transport; Fri, 30 Apr 2021 11:52:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2e00c665-276f-43df-d349-08d90bce6cd1 X-MS-TrafficTypeDiagnostic: SN6PR12MB2783: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BB/wGF321vq+R5emGW/qfbpgOpNnNcIlhmMrmyFUUJha+ByMSonc4T9RDHhzRr63TZVfCfjQAt6o3f6wX8cWVwoVlnAjxeRomv9ZgfjeXcAbamkRxBw+al3i44BRH5NlzGbz8JZrPWEOgjsyK0ESZTwvmgHVCmaqw0tyWWOI5f4ZggjdGVNkxB9iLSMz0zRKxHIyiQekZQFOF06nN8N9vC6/rR5uLZ0qjIO/5MdI7qH371UYWH/Lk1ePnvIz3/IrSw/v4AZzzacU5TNQV1v1M5h4XReMXfAqc84PXfzy/9Kdr6wNsggMUdVqthmrG7zzrC6CkBqRBknSQN17705R4L5K3TfaWZRZR4sT4hbbK3yfTHDzfpyyT1sbxCib7QDECb5ZttiG+2HkDKjgunBtrN0BOzmNIxGp0JUtAhxvgLpcvOgtTSbh6JzlKeTNTAnxtZUmWnBpKtvsw5/GjkhX9HKtC5VM9qC5VxFTONjDAp2V/eQVwsM24zPAN4oYtuPuqxjhHsVhf+AGGe3NYpKpKmHp8Q+DnDRymhlOTabfHbPmNAoWqkacEoI9MqiVsxFzCiqz718tx7JvJYu1JAJOq/9SL5gZiQhrtRM56edspXZWjAqlEwhWwqxHDprTDcrfFeek/NchxmLwSlMKFywmhJrBl98quHXq/Mhbn9Tn+gljZmeJkuQhKVSL4lwzOoI0u8eq1SStJzwTf0TzZO9rpHJx62W0M8+H0LQzfBgfhro= 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)(396003)(136003)(366004)(376002)(346002)(36756003)(44832011)(16526019)(52116002)(2616005)(956004)(66556008)(26005)(38100700002)(54906003)(966005)(1076003)(66476007)(186003)(7696005)(66946007)(5660300002)(6486002)(38350700002)(6916009)(86362001)(19627235002)(316002)(83380400001)(2906002)(6666004)(8936002)(478600001)(8676002)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?tp6QBdVZeKxOez/+Nv/5358nlYN1/VT2ObMGOPgvKccMwN0bVOzVP8Dftlkd?= =?us-ascii?Q?DzmV9hC6Ldy7ZGMbKMvYiOsU75SwqrdYJbqcwdXMSkLlbFc863lx4Q2uKXUK?= =?us-ascii?Q?teFsV6wtlLfen69qs6uVki6Sqx0UQBq2m7jwMLM/4ecdkuRxePyu8mGjitvR?= =?us-ascii?Q?xIqbDzmLDDmBv72oxl6aHMfniZEd1noYTZtlzdZdDA8cAo/7lUW6R883x43H?= =?us-ascii?Q?AGrWqWVB4dMXb2NUejjZn+eabnPT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?6ux1gB7iGUme2jvft8TX+Hu3ymywXz9cRmqnrHj4Ir0Kk9d15g5e4Hk2HQcA?= =?us-ascii?Q?5ge8G6B6lrzuDfW9sv0NnwNaFFdGqE9q1DrS6w7mwJjt+K/Muryo1DqBvS7E?= =?us-ascii?Q?6NpxnZPPNXGlpS0qMiwbA/Nx4tR6exP1yHT3SdIJjXSRIfyv6TUZmv32tdw5?= =?us-ascii?Q?Psnmy7JA+jT46Bv3z3SVQjY4?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2e00c665-276f-43df-d349-08d90bce6cd1 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2021 11:52:26.8183 (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: pYE6m9YjnkVPWaCjsODgKvpVOraEpx+VaWIdLSLw5OMteTsU3rbkBzMturB9AHdZwpkMkMIn34LWg6hJSaVqxw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2783 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3275 When SEV-SNP is active, the GHCB page is mapped un-encrypted in the initial page table built by the reset vector code. Just clearing the encryption attribute from the page table is not enough. The page also needs to be added as shared in the RMP table. The GHCB page was part of the pre-validated memory range specified through the SnpBootBlock GUID. To maintain the security guarantees, we must invalidate the GHCB page before clearing the encryption attribute from the page table, and add the page shared in the RMP table. Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Laszlo Ersek Cc: Erdem Aktas Signed-off-by: Brijesh Singh --- OvmfPkg/ResetVector/Ia32/PageTables64.asm | 82 ++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/OvmfPkg/ResetVector/Ia32/PageTables64.asm b/OvmfPkg/ResetVector/Ia32/PageTables64.asm index 6bf4a3524a..9949fcdfba 100644 --- a/OvmfPkg/ResetVector/Ia32/PageTables64.asm +++ b/OvmfPkg/ResetVector/Ia32/PageTables64.asm @@ -70,9 +70,78 @@ BITS 32 %define GHCB_HYPERVISOR_FEATURES_REQUEST 128 %define GHCB_HYPERVISOR_FEATURES_RESPONSE 129 +; 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 request to terminate protocol values %define GHCB_GENERAL_TERMINATE_REQUEST 255 +; If its an SEV-SNP guest then use the page state change VMGEXIT to invalidate +; the GHCB page. +; +; Modified: EAX, EBX, ECX, EDX +; +InvalidateGHCBPage: + ; Check if it is SEV-SNP guest. + cmp byte[SEV_ES_WORK_AREA_SNP], 1 + jne InvalidateGHCBPageDone + + ; Check whether hypervisor features has SEV-SNP (BIT0) set to indicate that + ; hypervisor supports the page state change. + mov eax, dword[SEV_ES_WORK_AREA_HYPERVISOR_FEATURES] + bt eax, 0 + jnc TerminateSevGuestLaunch + + ; Use PVALIDATE instruction to invalidate the page + mov eax, GHCB_BASE + mov ecx, 0 + mov edx, 0 + DB 0xF2, 0x0F, 0x01, 0xFF + cmp eax, 0 + jnz TerminateSevGuestLaunch + + ; Ask hypervisor to change the page state to shared using the + ; Page State Change VMGEXIT. + ; + ; Setup GHCB MSR + ; GHCB_MSR[55:52] = Page Operation + ; GHCB_MSR[51:12] = Guest Physical Frame Number + ; GHCB_MSR[11:0] = Page State Change Request + ; + mov eax, (GHCB_BASE >> 12) + shl eax, 12 + or eax, GHCB_PAGE_STATE_CHANGE_REQUEST + mov edx, (GHCB_PAGE_STATE_SHARED << 20) + mov ecx, 0xc0010130 + wrmsr + + ; + ; Issue VMGEXIT - NASM doesn't support the vmmcall instruction in 32-bit + ; mode, so work around this by temporarily switching to 64-bit mode. + ; +BITS 64 + rep vmmcall +BITS 32 + + ; + ; Response GHCB MSR + ; GHCB_MSR[51:12] = Guest Physical Frame Number + ; GHCB_MSR[11:0] = Page State Change Response + ; + mov ecx, 0xc0010130 + rdmsr + and eax, 0xfff + cmp eax, GHCB_PAGE_STATE_CHANGE_RESPONSE + jnz TerminateSevGuestLaunch + cmp edx, 0 + jnz TerminateSevGuestLaunch + +InvalidateGHCBPageDone: + OneTimeCallRet InvalidateGHCBPage + ; Check if Secure Encrypted Virtualization (SEV) features are enabled. ; ; Register usage is tight in this routine, so multiple calls for the @@ -450,6 +519,19 @@ clearGhcbMemoryLoop: ; OneTimeCall CheckHypervisorFeatures + ; + ; The page table built above cleared the memory encryption mask from the + ; 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. This + ; is because, in the 32-bit mode all the access are considered private. + ; The invalidation before the zero'ing will cause a #VC. + ; + OneTimeCall InvalidateGHCBPage + SetCr3: ; ; Set CR3 now that the paging structures are available -- 2.17.1