From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.86]) by mx.groups.io with SMTP id smtpd.web11.296.1628196153424961839 for ; Thu, 05 Aug 2021 13:42:35 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=Z6LTTjMY; 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.223.86, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MKWvBlbNUJuxCD/eiEdQYPMJ4idlVU6XhsTfgsVNzyzCyW+Jo07gmPNOvAqwgLqWuILO+ieoLNpe+1NVSs98ofnnqfCI+pCOAEBMImZbXY9jzUWXhdaq86GMgKqsFv50aU4oAkJwyZncKUc/XoHyJ+DZQxdiv4hJn5QCFRhQ0Rj6KyYyVeHtFRXa6br/iDTcT3SSAdAUdMgZdrAwcw8b1S+rW7jrBPy8iMkQEUyiLbIT2iDQsBDvb1mQt+n7Bw6z1Si+tYbbgSXKc5Pgn8Iuf4AF66fmebLRC2ZzXy63OdzeYUPNAg3dcMTdnf4GJfDy1IpQQokynf3XP36SrF9+kg== 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=L04/tEw5YovWGwXGVQGEC5ZTCQ/7on91k3q8061Kx54=; b=PfcZKdBlYkXdB9M4pzK1mcrcl8mypPbBXMs4IbkMEo84Uw7Ing73nePdqEbya5avzpa3GxrNRIhAvoB8YQjw3Eckv1hfsyqlCvdeU/oYMYd9KeQxtEcbrFhy3Vp12r17QoPkx0LBpi6BqjDX1BI/fGNl5hdHfMd2RTuy5/0TrhADgNoHsZeS9xPlUrVf59AHpTuBL8SuJjeVs9/Tlr2qEPAUWnYUpmod1MAAtV2LTS2Y/Uu4SthYH3noXhgPdGpOzwyOlKkMValDJYuFualdRxTrCl5FBDVt3qrON0cppWBXiO8tYY57RidKm+TzYv7Jo/5sG41IVyISpFztvzGyvg== 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=L04/tEw5YovWGwXGVQGEC5ZTCQ/7on91k3q8061Kx54=; b=Z6LTTjMYarmo5nkzAG3g9mN0jZrzU6ooGW0uI8H065Bxo08sqX7q/Jrv713bVCYUuf0B5xVjC65hiCEkTvYL63TLOZvjlRoCCAmduarHZePAm/El6tb4NOHPDb/hDfsgWl+WQ3ABAgZ4DNi4+7Bwdxl3vJP/nnZtKh/lN9Ula0I= 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 SN6PR12MB4751.namprd12.prod.outlook.com (2603:10b6:805:df::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.15; Thu, 5 Aug 2021 20:42:33 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::a8a9:2aac:4fd1:88fa]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::a8a9:2aac:4fd1:88fa%3]) with mapi id 15.20.4394.017; Thu, 5 Aug 2021 20:42:33 +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 , Brijesh Singh Subject: [PATCH v2 3/3] OvmfPkg/ResetVector: move the GHCB page setup in AmdSev.asm Date: Thu, 5 Aug 2021 15:42:14 -0500 Message-ID: <20210805204214.27792-4-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210805204214.27792-1-brijesh.singh@amd.com> References: <20210805204214.27792-1-brijesh.singh@amd.com> X-ClientProxiedBy: SN4PR0501CA0001.namprd05.prod.outlook.com (2603:10b6:803:40::14) 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 SN4PR0501CA0001.namprd05.prod.outlook.com (2603:10b6:803:40::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.5 via Frontend Transport; Thu, 5 Aug 2021 20:42:29 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b694402b-2515-45f6-0b0f-08d958518b41 X-MS-TrafficTypeDiagnostic: SN6PR12MB4751: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: l5x/M02g5dIX7KZOnlMQmmnQP0RMnaqByLMcuh7KZuYHBBN6eSlSQhdl9dkM/iiT5y5zPuwW3XgllRv3qsMkxj6luK3lcxa7qAalSzL1lveVD6WnT4zfzZYM3pX12ZT96H+ftRHcrQ2BEzqGwdJejn3+NVJc9wCAHTH9RjKCpSuEmoVLapJhBj8qgjZ/DAWJwNHL+nU3KV0Cfh3/HBsMKj6gRprYH13IFN8lNZnpUg4jvvd/5VBStUlbzEC5B8aD6iQiP2V1f8/SPaHRDVknq82FoKNzrK66vegjbAbhzD4FQDS+xo3PeBXoNkyDAiw17hS1RxZHF/RflVjjRY3D4EIASPey+HdtYXhXLtpMhLeet0eqW3uzFZwlJbLLRTrObhnEmPL4DB3oUbzDiZravm0EE3u4izuzT8qO8APcb0LuEyIZxNFj0TTFoQhqQh7rwl/k4EtH4DYO3/t/DLQV2v3Tr1l2OppYcJQ9Uyw2N0bnndiLW+D1eN6to1jbFW+l/g+SNox+fw5GKyDgXQ6MbGGuzBt1XvDBvzfM3TojWAxiFVrCQmM+GwWRDYeijOLGzeERh/6pMMRGSQor9o1wQvXSFPLW6p5sSUjNVtvTuyDBwUhZX6QyIvEkNv59nOqpJE6is3la9SnUhMoL8tMZg1I8O/kt1UUKn66hgh7ndb3plq4tjySi3pbz/up4K5DoqdxH1HhoLrTtx28uCmCqxgkDlzfoE5aPOxxBt9HHGG6qysAOw0wvaee2R+pSN4lcPA3x5oMx71GLrOVqxOfRQ863SUscb6sq+BSN3bkBi6M= 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)(346002)(396003)(376002)(366004)(136003)(39860400002)(36756003)(186003)(8676002)(956004)(2616005)(5660300002)(6486002)(6666004)(2906002)(316002)(54906003)(83380400001)(26005)(86362001)(66556008)(52116002)(38350700002)(7696005)(6916009)(4326008)(38100700002)(44832011)(66946007)(478600001)(8936002)(1076003)(19627235002)(966005)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?LmvCd51yTGvo1lPmq9KeB17OrSQ11zzsRqODrAgJ/Z8rh8IJt3CqyhvYBc6W?= =?us-ascii?Q?oGLrTav2al1KezbL3rvexElpE0fE7sEry9qBTckgPBxICFUfiNAL0/6MmKwl?= =?us-ascii?Q?nljC/8eMOyLFIHKDQyoRyKJaW8ZX3H1t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fjSEN+f2fjA4B/RZJLdMpInxCzB8/WoxqidF6OLKk1DNEreh/4+x53wFxE?= =?us-ascii?Q?/8SZEaFH4tektFhXTVPGJRAc6AXwyIVDv1C2ef+xSqw2Yf2tnpMyHmbXgy7s?= =?us-ascii?Q?lkbU7zfrUJerl26v1eOhGlzS2dqmxClrfQXsr/6ihlEzgA3DS8oq2XO+kbpa?= =?us-ascii?Q?ocyplSH4lW3qEJEgOcRHKj/hMU3aja0HBzW+kQmSde+4rGDY1fgIXY1sr9fH?= =?us-ascii?Q?Z5p6NxAAYv8la2qOH4mY79M1eign7tw1rsIO2X4MsNy/WtSVNHwYhdGrF7ut?= =?us-ascii?Q?gv9oFZ+0xAZht51UKwKeJ2R4?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: b694402b-2515-45f6-0b0f-08d958518b41 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Aug 2021 20:42:30.2706 (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: uZwJi5XWKIyfwm4pPMVi/DL3KAioG6cbexbDhMTR4V6AzmInhihyg+LoZ1YplIK+RHqc8qaoBaFCBXXKuWjFsg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB4751 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3429 While build the initial page table, the SetCr3ForPageTables64 checks whether SEV-ES is enabled. If so, clear the page encryption mask from the GHCB page. Move the logic to clear the page encryption mask in the AmdSev.asm. 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 | 113 +++++++++++++++++----- OvmfPkg/ResetVector/Ia32/PageTables64.asm | 53 ++-------- 2 files changed, 94 insertions(+), 72 deletions(-) diff --git a/OvmfPkg/ResetVector/Ia32/AmdSev.asm b/OvmfPkg/ResetVector/Ia32= /AmdSev.asm index 87d81b01e263..fd2e6abcd4a0 100644 --- a/OvmfPkg/ResetVector/Ia32/AmdSev.asm +++ b/OvmfPkg/ResetVector/Ia32/AmdSev.asm @@ -44,6 +44,27 @@ BITS 32 ; The unexpected response code %define TERM_UNEXPECTED_RESP_CODE 2 =20 +%define PAGE_PRESENT 0x01 +%define PAGE_READ_WRITE 0x02 +%define PAGE_USER_SUPERVISOR 0x04 +%define PAGE_WRITE_THROUGH 0x08 +%define PAGE_CACHE_DISABLE 0x010 +%define PAGE_ACCESSED 0x020 +%define PAGE_DIRTY 0x040 +%define PAGE_PAT 0x080 +%define PAGE_GLOBAL 0x0100 +%define PAGE_2M_MBO 0x080 +%define PAGE_2M_PAT 0x01000 + +%define PAGE_4K_PDE_ATTR (PAGE_ACCESSED + \ + PAGE_DIRTY + \ + PAGE_READ_WRITE + \ + PAGE_PRESENT) + +%define PAGE_PDP_ATTR (PAGE_ACCESSED + \ + PAGE_READ_WRITE + \ + PAGE_PRESENT) + =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 @@ -117,6 +138,72 @@ BITS 32 SevEsUnexpectedRespTerminate: TerminateVmgExit TERM_UNEXPECTED_RESP_CODE =20 +; If SEV-ES is enabled then initialize the make the GHCB page shared +SevClearPageEncMaskFromGHCBPage: + ; Check if SEV is enabled + cmp byte[WORK_AREA_GUEST_TYPE], 1 + jnz SevClearPageEncMaskFromGHCBPageExit + + ; Check if SEV-ES is enabled + cmp byte[SEV_ES_WORK_AREA], 1 + jnz SevClearPageEncMaskFromGHCBPageExit + + ; + ; The initial GHCB will live at GHCB_BASE and needs to be un-encrypted= . + ; This requires the 2MB page for this range be broken down into 512 4K= B + ; pages. All will be marked encrypted, except for the GHCB. + ; + mov ecx, (GHCB_BASE >> 21) + mov eax, GHCB_PT_ADDR + PAGE_PDP_ATTR + mov [ecx * 8 + PT_ADDR (0x2000)], eax + + ; + ; Page Table Entries (512 * 4KB entries =3D> 2MB) + ; + mov ecx, 512 +pageTableEntries4kLoop: + mov eax, ecx + dec eax + shl eax, 12 + add eax, GHCB_BASE & 0xFFE0_0000 + add eax, PAGE_4K_PDE_ATTR + mov [ecx * 8 + GHCB_PT_ADDR - 8], eax + mov [(ecx * 8 + GHCB_PT_ADDR - 8) + 4], edx + loop pageTableEntries4kLoop + + ; + ; Clear the encryption bit from the GHCB entry + ; + mov ecx, (GHCB_BASE & 0x1F_FFFF) >> 12 + mov [ecx * 8 + GHCB_PT_ADDR + 4], strict dword 0 + + mov ecx, GHCB_SIZE / 4 + xor eax, eax +clearGhcbMemoryLoop: + mov dword[ecx * 4 + GHCB_BASE - 4], eax + loop clearGhcbMemoryLoop + +SevClearPageEncMaskFromGHCBPageExit: + OneTimeCallRet SevClearPageEncMaskFromGHCBPage + +; Check if SEV is enabled, and get the C-bit mask above 31. +; Modified: EDX +; +; The value is returned in the EDX +GetSevCBitMaskAbove31: + ; Check if SEV is enabled + cmp byte[WORK_AREA_GUEST_TYPE], 1 + jnz NoCbitValue + + mov edx, dword[SEV_ES_WORK_AREA_ENC_MASK + 4] + jmp GetSevCBitMaskAbove31Exit + +NoCbitValue: + xor edx, edx + +GetSevCBitMaskAbove31Exit: + OneTimeCallRet GetSevCBitMaskAbove31 + ; Check if Secure Encrypted Virtualization (SEV) features are enabled. ; ; Register usage is tight in this routine, so multiple calls for the @@ -249,32 +336,6 @@ SevExit: =20 OneTimeCallRet CheckSevFeatures =20 -; Check if Secure Encrypted Virtualization - Encrypted State (SEV-ES) feat= ure -; is enabled. -; -; Modified: EAX -; -; If SEV-ES is enabled then EAX will be non-zero. -; If SEV-ES is disabled then EAX will be zero. -; -IsSevEsEnabled: - xor eax, eax - - ; During CheckSevFeatures, the WORK_AREA_GUEST_TYPE is set - ; to 1 if SEV is enabled. - cmp byte[WORK_AREA_GUEST_TYPE], 1 - jne SevEsDisabled - - ; During CheckSevFeatures, the SEV_ES_WORK_AREA was set to 1 if - ; SEV-ES is enabled. - cmp byte[SEV_ES_WORK_AREA], 1 - jne SevEsDisabled - - mov eax, 1 - -SevEsDisabled: - OneTimeCallRet IsSevEsEnabled - ; Start of #VC exception handling routines ; =20 diff --git a/OvmfPkg/ResetVector/Ia32/PageTables64.asm b/OvmfPkg/ResetVecto= r/Ia32/PageTables64.asm index f688909f1c7d..0e8ba4dde534 100644 --- a/OvmfPkg/ResetVector/Ia32/PageTables64.asm +++ b/OvmfPkg/ResetVector/Ia32/PageTables64.asm @@ -46,16 +46,13 @@ SetCr3ForPageTables64: ; work area when detected. mov byte[WORK_AREA_GUEST_TYPE], 0 =20 + ; Check whether the SEV is active and populate the SevEsWorkArea OneTimeCall CheckSevFeatures - xor edx, edx - test eax, eax - jz SevNotActive =20 - ; If SEV is enabled, C-bit is always above 31 - sub eax, 32 - bts edx, eax - -SevNotActive: + ; If SEV is enabled, the C-bit position is always above 31. + ; The mask will be saved in the EDX and applied during the + ; the page table build below. + OneTimeCall GetSevCBitMaskAbove31 =20 ; ; For OVMF, build some initial page tables at @@ -105,44 +102,8 @@ pageTableEntriesLoop: mov [(ecx * 8 + PT_ADDR (0x2000 - 8)) + 4], edx loop pageTableEntriesLoop =20 - OneTimeCall IsSevEsEnabled - test eax, eax - jz SetCr3 - - ; - ; The initial GHCB will live at GHCB_BASE and needs to be un-encrypted= . - ; This requires the 2MB page for this range be broken down into 512 4K= B - ; pages. All will be marked encrypted, except for the GHCB. - ; - mov ecx, (GHCB_BASE >> 21) - mov eax, GHCB_PT_ADDR + PAGE_PDP_ATTR - mov [ecx * 8 + PT_ADDR (0x2000)], eax - - ; - ; Page Table Entries (512 * 4KB entries =3D> 2MB) - ; - mov ecx, 512 -pageTableEntries4kLoop: - mov eax, ecx - dec eax - shl eax, 12 - add eax, GHCB_BASE & 0xFFE0_0000 - add eax, PAGE_4K_PDE_ATTR - mov [ecx * 8 + GHCB_PT_ADDR - 8], eax - mov [(ecx * 8 + GHCB_PT_ADDR - 8) + 4], edx - loop pageTableEntries4kLoop - - ; - ; Clear the encryption bit from the GHCB entry - ; - mov ecx, (GHCB_BASE & 0x1F_FFFF) >> 12 - mov [ecx * 8 + GHCB_PT_ADDR + 4], strict dword 0 - - mov ecx, GHCB_SIZE / 4 - xor eax, eax -clearGhcbMemoryLoop: - mov dword[ecx * 4 + GHCB_BASE - 4], eax - loop clearGhcbMemoryLoop + ; Clear the C-bit from the GHCB page if the SEV-ES is enabled. + OneTimeCall SevClearPageEncMaskFromGHCBPage =20 SetCr3: ; --=20 2.17.1