From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (NAM10-DM6-obe.outbound.protection.outlook.com [40.107.93.58]) by mx.groups.io with SMTP id smtpd.web08.4215.1642633434926991925 for ; Wed, 19 Jan 2022 15:03:55 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=X8Y+Dyx7; 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.93.58, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PdpWrTq5o2yP2u4rwmNFwy6iYOUAH5x/xYaNwGymHKPId8SvCDIIKjAT/WYZSdmCxvVzsQLOKViZunNy67njunkuqH6O5SjZa9fiSX6RNif0L6cFodwHYstrBOjajocFhO6lInTVp94VqI/MjFpsvj4VToxn/a2ggEvy0+BtO1XztPib4K+efAqID+41vmjOsH/nbSaooTYYP7HyKqQTCOvSYHsmWwtjEsRoyK+iFZve8p4sL8vjPtOqvKtawUzSlF3D2mTReIoXxiGlKjH0wBJVdslZwuprH5cNc7B+vYE1XC18DU6Gkco3URoPc1edwNDJdnxeDrgK7G40cujJfw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iwVY5U0KS+QSxQQxbrqQruOg2/MGiLb/vOPwHbLDTCA=; b=DqY+FOIdGRWncWVtsxzq6iPe6tJAd3xh5BsP9vz5xJNpAp/2oaJDVmyB/7bQwPWRgFnm7j2RJ0VRKQ8MSMyo2WK5Z37Folz23BiElWjXBVr+wuIO9WCKQ0xnqN44qS5bQhp7hWYlwwmBWKApr1+fU8cZWWbs1MKG7/IjvHYebvUioygbuFDC4qUrQQqaywUTSEoz3zukuTiwgNnlNFrfGyjc00jDHjGnsP2SJUZNmKNgBXzOZ0ZKlr1gUYTlEmTaNu/6+wzuB/78jPwxfU2XoIG8Z1RIZl2HFabyBLmff8a77F7RTXCLLnK97q0N+PV7a3mj6876FcZFfhvbdNvTpQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=iwVY5U0KS+QSxQQxbrqQruOg2/MGiLb/vOPwHbLDTCA=; b=X8Y+Dyx7wOqh4/rFlq/RLMuQYLgqFaVxO25F+8hP/f3VkW7JdYzOd+vQCLUPhoDV4j22no8VfaCvpT1Uo3ulbwW3Wgejv43G84i1oQs3F4HLpxXyyzvbQ4BUV3i0T8q5ZLa49iD1gRyEBlo2kIDvYchr37RtYzCRzx1sIDK4M4Q= Received: from DS7PR05CA0059.namprd05.prod.outlook.com (2603:10b6:8:2f::32) by MN2PR12MB2927.namprd12.prod.outlook.com (2603:10b6:208:103::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.8; Wed, 19 Jan 2022 23:03:53 +0000 Received: from DM6NAM11FT013.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2f:cafe::34) by DS7PR05CA0059.outlook.office365.com (2603:10b6:8:2f::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.4 via Frontend Transport; Wed, 19 Jan 2022 23:03:52 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT013.mail.protection.outlook.com (10.13.173.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4909.7 via Frontend Transport; Wed, 19 Jan 2022 23:03:52 +0000 Received: from sbrijesh-desktop.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.18; Wed, 19 Jan 2022 17:03:51 -0600 From: "Brijesh Singh" To: CC: James Bottomley , Min Xu , "Jiewen Yao" , Tom Lendacky , "Jordan Justen" , Ard Biesheuvel , Erdem Aktas , "Michael Roth" , Gerd Hoffmann , Brijesh Singh Subject: [PATCH 1/2] OvmfPkg/ResetVector: cache the SEV status MSR value in workarea Date: Wed, 19 Jan 2022 17:03:31 -0600 Message-ID: <20220119230332.44888-2-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220119230332.44888-1-brijesh.singh@amd.com> References: <20220119230332.44888-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 44b21e8e-d616-4714-bfc8-08d9db9ff662 X-MS-TrafficTypeDiagnostic: MN2PR12MB2927:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4125; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nZfaJ6bIpLPb/CRZlvYvvBtY8rnka1dCl7f4kuu7hsLHD1DVzEVoGsy5/QtdQM+yr9+dUTxX5GLxVP/zAKGRYYxajMi9RvnA/sMLR+KITU3XxLzipTT/p2ZGbd59YQHeEeMcE8HPtBZUnYyLrMcQbSCamfra/TEeJdd9xszSheYfVO3l0mZUk4ZWlCvNpUSdeLu/hBFmO+qb1/eZVam2yWjPnnluL+d1vJuYQo9hgLSZfEejf//FeYBxm3DN4GvtNaUTA8iBbnxKM+Xk5k1o+0kYqYtCnyU/eq2tpzfAcLeWeRzKnS0kThh2SNjYdf3YOkyRapt4yR0gNvrdOyJ5i4aIh5eKNqnbBv+Gbqf0zvtaW3NO+syp884v0COKvQDhj0u49CijrfhKX/HfS7ENgwNBcSatpoF9zQzUFIz9Ylrw0m/jJ7ZNKn9HJf0bceEKcH6BgO1oLdqCxfdpd5Yewtq3atJnVQI8fYFf7JI+Od+NXwO1NSBuJbcO++0a6HVdgKlknWvAyQLLs4xykbl7QcHOyvm8GO8YoExKn8ADojN5EOqGjxwE4+2Wo809Kx9tNwamjrFKn2lJWuFeVSRRatRut8IVxMv17T3mOiFE5mmu4HPZmITseHZI7PIUUdNMDuXLGR9p4oUszmroGs3ur48sTem6LxYxfPLrWa9hbEb71WCh/ncu9voblv2Djm5hxW/hOdPomwfyiyH9rqTEOubPa9AtKNe6N+8ogzEp/BG1yfwOxy63lX0CXMk7QuhE3KDXfpp7NvYdW3zMgOj26Dzqre8vGbaXrgNGNvtksKg= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(40470700002)(36840700001)(36756003)(4326008)(44832011)(81166007)(7696005)(2906002)(426003)(47076005)(86362001)(1076003)(36860700001)(40460700001)(316002)(6666004)(54906003)(19627235002)(8936002)(5660300002)(6916009)(70586007)(70206006)(82310400004)(356005)(8676002)(26005)(83380400001)(16526019)(336012)(2616005)(186003)(508600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2022 23:03:52.8648 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 44b21e8e-d616-4714-bfc8-08d9db9ff662 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT013.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB2927 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain In order to probe the SEV feature the BaseMemEncryptLib and Reset vector reads the SEV_STATUS MSR. Cache the value on the first read in the workarea. In the next patches the value saved in the workarea will be used by the BaseMemEncryptLib. This not only eliminates the extra MSR reads it also helps cleaning up the code in BaseMemEncryptLib. Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Erdem Aktas Cc: Gerd Hoffmann Signed-off-by: Brijesh Singh --- OvmfPkg/Include/WorkArea.h | 12 +++++-- OvmfPkg/Sec/AmdSev.c | 2 +- OvmfPkg/ResetVector/Ia32/AmdSev.asm | 38 +++++++++++++-------- OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm | 3 +- OvmfPkg/ResetVector/ResetVector.nasmb | 3 ++ 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/OvmfPkg/Include/WorkArea.h b/OvmfPkg/Include/WorkArea.h index ce60d97aa886..d982e026def7 100644 --- a/OvmfPkg/Include/WorkArea.h +++ b/OvmfPkg/Include/WorkArea.h @@ -46,12 +46,20 @@ typedef struct _CONFIDENTIAL_COMPUTING_WORK_AREA_HEADER= { // any changes must stay in sync with its usage. // typedef struct _SEC_SEV_ES_WORK_AREA { - UINT8 SevEsEnabled; - UINT8 Reserved1[7]; + // + // Hold the SevStatus MSR value read by OvmfPkg/ResetVector/Ia32/AmdSev.= c + // + UINT64 SevStatusMsrValue; =20 UINT64 RandomData; =20 UINT64 EncryptionMask; + + // + // Indicator that the VC handler is called. It is used during the SevFea= ture + // detection in OvmfPkg/ResetVector/Ia32/AmdSev.c + // + UINT8 ReceivedVc; } SEC_SEV_ES_WORK_AREA; =20 // diff --git a/OvmfPkg/Sec/AmdSev.c b/OvmfPkg/Sec/AmdSev.c index 499d0c27d8fa..d8fd35650d7d 100644 --- a/OvmfPkg/Sec/AmdSev.c +++ b/OvmfPkg/Sec/AmdSev.c @@ -278,7 +278,7 @@ SevEsIsEnabled ( =20 SevEsWorkArea =3D (SEC_SEV_ES_WORK_AREA *)FixedPcdGet32 (PcdSevEsWorkAre= aBase); =20 - return (SevEsWorkArea->SevEsEnabled !=3D 0); + return ((SevEsWorkArea->SevStatusMsrValue & BIT1) !=3D 0); } =20 /** diff --git a/OvmfPkg/ResetVector/Ia32/AmdSev.asm b/OvmfPkg/ResetVector/Ia32= /AmdSev.asm index 1f827da3b929..864d68385342 100644 --- a/OvmfPkg/ResetVector/Ia32/AmdSev.asm +++ b/OvmfPkg/ResetVector/Ia32/AmdSev.asm @@ -157,8 +157,9 @@ SevClearPageEncMaskForGhcbPage: jnz SevClearPageEncMaskForGhcbPageExit =20 ; Check if SEV-ES is enabled - cmp byte[SEV_ES_WORK_AREA], 1 - jnz SevClearPageEncMaskForGhcbPageExit + mov ecx, 1 + bt [SEV_ES_WORK_AREA_STATUS_MSR], ecx + jnc SevClearPageEncMaskForGhcbPageExit =20 ; ; The initial GHCB will live at GHCB_BASE and needs to be un-encrypted= . @@ -219,12 +220,16 @@ GetSevCBitMaskAbove31Exit: ; If SEV is disabled then EAX will be zero. ; CheckSevFeatures: - ; Set the first byte of the workarea to zero to communicate to the SEC - ; phase that SEV-ES is not enabled. If SEV-ES is enabled, the CPUID - ; instruction will trigger a #VC exception where the first byte of the - ; workarea will be set to one or, if CPUID is not being intercepted, - ; the MSR check below will set the first byte of the workarea to one. - mov byte[SEV_ES_WORK_AREA], 0 + ; + ; Clear the workarea, if SEV is enabled then later part of routine + ; will populate the workarea fields. + ; + mov ecx, SEV_ES_WORK_AREA_SIZE + mov eax, SEV_ES_WORK_AREA +ClearSevEsWorkArea: + mov byte [eax], 0 + inc eax + loop ClearSevEsWorkArea =20 ; ; Set up exception handlers to check for SEV-ES @@ -265,6 +270,10 @@ CheckSevFeatures: ; Set the work area header to indicate that the SEV is enabled mov byte[WORK_AREA_GUEST_TYPE], 1 =20 + ; Save the SevStatus MSR value in the workarea + mov [SEV_ES_WORK_AREA_STATUS_MSR], eax + mov [SEV_ES_WORK_AREA_STATUS_MSR + 4], edx + ; Check for SEV-ES memory encryption feature: ; CPUID Fn8000_001F[EAX] - Bit 3 ; CPUID raises a #VC exception if running as an SEV-ES guest @@ -280,10 +289,6 @@ CheckSevFeatures: bt eax, 1 jnc GetSevEncBit =20 - ; Set the first byte of the workarea to one to communicate to the SEC - ; phase that SEV-ES is enabled. - mov byte[SEV_ES_WORK_AREA], 1 - GetSevEncBit: ; Get pte bit position to enable memory encryption ; CPUID Fn8000_001F[EBX] - Bits 5:0 @@ -313,7 +318,10 @@ NoSev: ; ; Perform an SEV-ES sanity check by seeing if a #VC exception occurred= . ; - cmp byte[SEV_ES_WORK_AREA], 0 + ; If SEV-ES is enabled, the CPUID instruction will trigger a #VC excep= tion + ; where the RECEIVED_VC offset in the workarea will be set to one. + ; + cmp byte[SEV_ES_WORK_AREA_RECEIVED_VC], 0 jz NoSevPass =20 ; @@ -407,9 +415,9 @@ SevEsIdtVmmComm: ; If we're here, then we are an SEV-ES guest and this ; was triggered by a CPUID instruction ; - ; Set the first byte of the workarea to one to communicate that + ; Set the recievedVc field in the workarea to communicate that ; a #VC was taken. - mov byte[SEV_ES_WORK_AREA], 1 + mov byte[SEV_ES_WORK_AREA_RECEIVED_VC], 1 =20 pop ecx ; Error code cmp ecx, 0x72 ; Be sure it was CPUID diff --git a/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm b/OvmfPkg/ResetVec= tor/Ia32/Flat32ToFlat64.asm index eb3546668ef8..c5c683ebed3e 100644 --- a/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm +++ b/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm @@ -42,7 +42,8 @@ Transition32FlatTo64Flat: ; xor ebx, ebx =20 - cmp byte[SEV_ES_WORK_AREA], 0 + mov ecx, 1 + bt [SEV_ES_WORK_AREA_STATUS_MSR], ecx jz EnablePaging =20 ; diff --git a/OvmfPkg/ResetVector/ResetVector.nasmb b/OvmfPkg/ResetVector/Re= setVector.nasmb index cc364748b592..9421f4818907 100644 --- a/OvmfPkg/ResetVector/ResetVector.nasmb +++ b/OvmfPkg/ResetVector/ResetVector.nasmb @@ -100,8 +100,11 @@ %define GHCB_BASE (FixedPcdGet32 (PcdOvmfSecGhcbBase)) %define GHCB_SIZE (FixedPcdGet32 (PcdOvmfSecGhcbSize)) %define SEV_ES_WORK_AREA (FixedPcdGet32 (PcdSevEsWorkAreaBase)) + %define SEV_ES_WORK_AREA_SIZE 25 + %define SEV_ES_WORK_AREA_STATUS_MSR (FixedPcdGet32 (PcdSevEsWorkAreaBase= )) %define SEV_ES_WORK_AREA_RDRAND (FixedPcdGet32 (PcdSevEsWorkAreaBase) + = 8) %define SEV_ES_WORK_AREA_ENC_MASK (FixedPcdGet32 (PcdSevEsWorkAreaBase) = + 16) + %define SEV_ES_WORK_AREA_RECEIVED_VC (FixedPcdGet32 (PcdSevEsWorkAreaBas= e) + 24) %define SEV_ES_VC_TOP_OF_STACK (FixedPcdGet32 (PcdOvmfSecPeiTempRamBase)= + FixedPcdGet32 (PcdOvmfSecPeiTempRamSize)) %define SEV_SNP_SECRETS_BASE (FixedPcdGet32 (PcdOvmfSnpSecretsBase)) %define SEV_SNP_SECRETS_SIZE (FixedPcdGet32 (PcdOvmfSnpSecretsSize)) --=20 2.25.1