From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (NAM04-DM6-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web09.26.1616599956611292901 for ; Wed, 24 Mar 2021 08:32:38 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=UU4Bjr6u; 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=VhKRCS8tarrozDgPwIW5eFbPPZaTbLXadHmrJmxORSwIDfkBzeq792VBOjhaTQ/hA00uh7r5r1yJgzbaq0td/4tT4wc7JL6A2dW3u2wwmZ1gyYHrFiejdhA/RrffNA3pgKVdVWcJy1BkkvO7js619WxzsIx6fjVedl9tPCtYBwmq6AjwSXcuvt70LOi6IZbMsfNF8rWGh2sMLHAUcztoLJK6dy48+m1cGPM0y3ngY48l5z2sxmmscjk/u7apW+80C42ZlAXN3EDwsvtWsenKIw64deTJohNrhx0gujU3rJ0t3azUdeztonNQIM3aGxWlNzR8kpIOLtDc8SLgpZiG7w== 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=XlKaFkgLxZJDo3Ld8/F1Jp0UShJWsng4/krEnbVyjPk=; b=aenut+79nfxUyURE6hs3UaKHGbylasbe/neKlwVp8fJI6xMFGbX3ma3Tp0xDwEoOmItz86BTQ73YipxkZJ+VjFIzLuFvSToYgGEqCFDvTrOBpL3wmRVQfvC9UXjP6v/9j/x1daUSVwbKTi13Uhs0zgjliXU589ygvAoXRp3Hdyhb9vWPzV9gj/i1simmKNv1bNrTX8iarp2b67qJ9P22toyf1Un/MigT07ktKivKve8lPZcGdRtiphxKX912/EUtK3Cw2mssLQf6A2uDrNGUGPV6zmgdDlTXSukhXNuduWLTbkBcVF0ZxsE67ByZ64zUIHGCE3OqWhm1WNN5bZtxjw== 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=XlKaFkgLxZJDo3Ld8/F1Jp0UShJWsng4/krEnbVyjPk=; b=UU4Bjr6uYa3YxTrD3GSYyzCpiJxR7kiZis7h9sldcN3ty2HagsI2O/bRkFMHB9rHf06Y/YP25W5sVpQcsdAU4kyzj5yi/LMz9W3oZ9OZYe073Bakq1jw3JhJr0deMnlUbi0kzu52hyx+mBz05pjeHFA3B5MY+NCSUZrr4KlGa1k= 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 SA0PR12MB4430.namprd12.prod.outlook.com (2603:10b6:806:70::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Wed, 24 Mar 2021 15:32:35 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 15:32:35 +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 Subject: [RFC PATCH 11/19] OvmfPkg/ResetVector: Invalidate the GHCB page Date: Wed, 24 Mar 2021 10:32:07 -0500 Message-Id: <20210324153215.17971-12-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324153215.17971-1-brijesh.singh@amd.com> References: <20210324153215.17971-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA9PR11CA0006.namprd11.prod.outlook.com (2603:10b6:806:6e::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 SA9PR11CA0006.namprd11.prod.outlook.com (2603:10b6:806:6e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Wed, 24 Mar 2021 15:32:34 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: c1879f18-67c4-498d-6315-08d8eeda0c5c X-MS-TrafficTypeDiagnostic: SA0PR12MB4430: 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: gllrGrcur9wjy6OuEM1AzIzoQea5J8lBxdfUf8qNh97JX/GplxNQGGOzDQrCZTs8Pp0KHsTBdxsMtgqtbLkdXCV031XxwauOdoCYYotIwFu2zbN83UroXIk2e6XMRwENy+55JJLfONrl4+2/jfGW/q1SeRBaJ6qgZVKQkTBGZqfBgyjgwphOqVwAXHJXepQ6Mz2rmdPRqcWdyKVVx9wzpA5XcBlNEnrvnappZh+387WoGI0FX6JzQ3SJ4nQv2f1aC6vwulXggBfpse2P2NU6oYUWODtCPTOmXooKSbppDCz28cAqld9vdsGzlmR93e723/abR0uXXoKoV3rdd96T1csnMVGfsYxb5SpAp15hLsIcCHU75EnNHWCxAHLC1gtqdDf8FU6zOw3hgE2UkiLxsaUFu7RtoLzZgFsGeXZZu/nkU76gHRFYlYGWS0A/bBPEL69SwElTrR3yx1w3WSNdvf7Y0WiOKYqZ3WOeg6STMPXR1KsRZI7Cy50zvBv8wQ944CnrMi7y1S5FtkM5ezCYVxVevPF1q8nw3kUcmTsVXCYoKyLpjFHRlTqF2e2Cv7yTOhkJZc7m4YGTb7FJyU7l+dbAPxYYXmeMuJ30gHLNneEsDc7i9hi8KmVvwrlaRvF0oLbq2Qieia19rigBybBQcOjXoeSaaM4FypLNX109TljMzu6OHKxzFXJ27KH0Rodocofkw/4GKWX7jtdJBT7yvGtKQc4jFc3YMwQLPsgtO70K6SjbRtzzRda+USIO5eRK 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)(366004)(39860400002)(136003)(376002)(346002)(396003)(2616005)(83380400001)(66476007)(4326008)(44832011)(1076003)(6486002)(2906002)(86362001)(66946007)(478600001)(316002)(66556008)(966005)(16526019)(6666004)(26005)(54906003)(19627235002)(6916009)(186003)(5660300002)(8676002)(7696005)(956004)(8936002)(38100700001)(36756003)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?jSTo9+iIkxpYTUsQNj0yFOAQpOrN/TJZ27/Z9TGU42fuMhA9uz38y2yjvgfd?= =?us-ascii?Q?/AeoYfwUGqr3+0ekPbVnJ+i5tDO8cR1KHcBQVJtguVo4G4USGVTnTu5ycnTP?= =?us-ascii?Q?Y6lPPZIkF9Gbby8Iyj8e2tYn2/5KNqQeUIfCAVNXIh8k6TrhcEctomRrP31q?= =?us-ascii?Q?w0JtoxHpoIgzt61HoiHQ9X3VecU0rdusBiYK47P4m4Ct9c6XRt6kTgNYFrSE?= =?us-ascii?Q?2FVex8DuKre/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-ascii?Q?L9nuo311GyQLSLaTEBzSNO2TPH8u1lnjX5OjXv0h9vTahqQSiMxXaeb2K/p4?= =?us-ascii?Q?PmXFLNTpRiPhGwxZYT0X/wewXq35pdsk1waPZnnIUhZCZYEYbV54y8Lx5cM+?= =?us-ascii?Q?GBDh1uUBaPRIIZ2nY7jmx0Iq2WAdAS8FDjbbcnhYlRRdufFaXvQyRi0bI5Em?= =?us-ascii?Q?tINKVki0GVjcCre1Y43iYTqh?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c1879f18-67c4-498d-6315-08d8eeda0c5c X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 15:32:35.2666 (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: KTkPqbNfuo9Alb9XM75PNzXea7eTNMUYa5gGXfYJXCYaEUul2ZMzoN+m5yQDdEvWMorcKGfd5HGWDyAYHapSmQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4430 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 Signed-off-by: Brijesh Singh --- OvmfPkg/ResetVector/Ia32/PageTables64.asm | 106 ++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/OvmfPkg/ResetVector/Ia32/PageTables64.asm b/OvmfPkg/ResetVector/Ia32/PageTables64.asm index 5fae8986d9..63b864bcf2 100644 --- a/OvmfPkg/ResetVector/Ia32/PageTables64.asm +++ b/OvmfPkg/ResetVector/Ia32/PageTables64.asm @@ -62,6 +62,99 @@ BITS 32 %define GHCB_CPUID_REGISTER_SHIFT 30 %define CPUID_INSN_LEN 2 +; 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 SEV-SNP is enabled, invalidate the GHCB page +InvalidateGHCBPage: + ; Read the SEV_STATUS MSR to check whether SEV-SNP is enabled. + ; MSR_0xC0010131 - Bit 2 (SEV-SNP enabled) + mov ecx, 0xc0010131 + rdmsr + bt eax, 2 + jnc InvalidateGHCBPageDone + + ; 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 ValidationFailure + + ; 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 ValidationFailure + cmp edx, 0 + jnz ValidationFailure + + jmp InvalidateGHCBPageDone + +ValidationFailure: + ; + ; Setup GHCB MSR + ; GHCB_MSR[23:16] = 0 + ; GHCB_MSR[15:12] = 0 + ; GHCB_MSR[11:0] = Terminate Request + ; + mov edx, 0 + mov eax, GHCB_GENERAL_TERMINATE_REQUEST + 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 + +SnpPageStateFailureHlt: + cli + hlt + jmp SnpPageStateFailureHlt + +InvalidateGHCBPageDone: + OneTimeCallRet InvalidateGHCBPage + ; Check if Secure Encrypted Virtualization (SEV) features are enabled. ; @@ -316,6 +409,19 @@ clearGhcbMemoryLoop: mov dword[ecx * 4 + GHCB_BASE - 4], eax loop clearGhcbMemoryLoop + ; + ; 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