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.62]) by mx.groups.io with SMTP id smtpd.web09.11498.1645455578818405399 for ; Mon, 21 Feb 2022 06:59:39 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=R3CGFDUr; 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.62, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f1OCWOuuryl/B7Qa+kk47dbEEB0tC1smlG5HkFHrjxq9Bp62WB4JJpYX0FD7cVELuNvhpiAnFuRElNVbrm752an3Cn1if3/2oUlhgkY+3uNKs9Bpn12EVVVBv/z/ulpOYGzIBki5v+ItJ3qxNoFQw1xt3RoGdl5DHNuhimRQeJZ3ncXG4qHoQ37CITg8SSNrV5IKx6JGXxJB11JZrRyU1WVtZU1ToCNqj6q0bGcdju6uMWcj7j62PkwAELf4YW/lnFmEfshxk9pEV15svN5+4f+f5/+WKfBGy1j6NVYZSeTdLfFANsgR1SgxuDs2GQj9tLsnCwxjVgpQGzs44alpIQ== 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=IFIhqNfVwHRdvJfgatRdkzbvRfOl2PBYEFhqcZIjkt0=; b=kYVfjsj0JXS0UOQPPnHHR2ZI6LijdNfEoxYhgsgip7JbHUr7aX1SLGRLRFICEySU5Nb2DIwsiB6ge00CHSOjUxjsxwKwuASm46O5TTyU+Aq+v2amzThoUyod5YOkKd+HZYndNznuDTeNUzVbGv1WmfGCkPwhHUHyeHjUuwzTZEuncB4VJWVvmIemhB1mBQKnC0fKv6NNrwXce1Ts4CljC2+Sh1Yt+Lw9sQYOIoPTd+s4801jYpCDvLB7GpEeAZqhGYcRT/MvN8gW/tFUsT1BYsf+X33y1ATdtobGTFlodxa4GrR9697ChQW72avbvsr+v19nG8LmIxsT/sj9Gmei7Q== 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=IFIhqNfVwHRdvJfgatRdkzbvRfOl2PBYEFhqcZIjkt0=; b=R3CGFDUrkSmWlCZxWHrnhvPjpTQ99cqsoLFtvUudvw4LvO5YEK0Pyae3ExibgLaH4rTJiK/rJmsupzc7ylVOVTSIreYrYLKQiadWV6OBJXA2n+RW0JJb/0DKy3vBcho/jlHx7T3B75eWpcFweS0pvDjAUvher2SVnHKjaeYkjjs= Received: from DM5PR21CA0044.namprd21.prod.outlook.com (2603:10b6:3:ed::30) by CH0PR12MB5060.namprd12.prod.outlook.com (2603:10b6:610:e3::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4995.16; Mon, 21 Feb 2022 14:59:36 +0000 Received: from DM6NAM11FT059.eop-nam11.prod.protection.outlook.com (2603:10b6:3:ed:cafe::90) by DM5PR21CA0044.outlook.office365.com (2603:10b6:3:ed::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.6 via Frontend Transport; Mon, 21 Feb 2022 14:59:36 +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 DM6NAM11FT059.mail.protection.outlook.com (10.13.172.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4995.15 via Frontend Transport; Mon, 21 Feb 2022 14:59:36 +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; Mon, 21 Feb 2022 08:59:35 -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 v2 1/2] OvmfPkg/ResetVector: cache the SEV status MSR value in workarea Date: Mon, 21 Feb 2022 08:59:13 -0600 Message-ID: <20220221145914.1972322-2-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220221145914.1972322-1-brijesh.singh@amd.com> References: <20220221145914.1972322-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: b91878a2-2f1c-4260-809f-08d9f54ac717 X-MS-TrafficTypeDiagnostic: CH0PR12MB5060:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gE0fVOu3HxSo3Mwmq0PCN0nx7tMLNIHYc+mO5eujWiZfLfzQv7cjOvhfDFVhRfeljV6arsJlTIP5tATIf3QVS6ibagMnyFFVsb/29lqagQxaYD/4/nAn6nAKdvS7b5qofN3C/TV97hIIxAwTCMez+Mu4STjFkCEP/tTocajIKIapkY+MaFQJgmsH37ieBZvWy9o7OlkvlnR53LnsmI0jDCFm/XsyQtq7KyiRlXqBG4HfqEwtbCKlykaJ7Dc48LJ4N8oRXhvGJB0K1hxmgPeT0N8VhfIbvXV/Y/CaX1GFEuFCYOviC5REae7fhrN3xIoMqw5y9+Z4ATeEdbQaSdWZ2DLagJpLUtZu3wImpp6PupfxnXN3pndnLQ3/IbgiqhfD4kULRIizYl3jJKKUZQkMgIMmacXCZ3Y6Xsizi+AQ0t0I/faM6VFCuQJcjSl9BUkKm0+aXcq0wBlO1brIANOTqyb4wtz7e+nGMPXJUUBsywMsxAJEpQ3/oxy89vZ1CpmeczsiP5hsyrihQN6o5PWFDOYbDOPoYQ8fgtnEQnwE+w/cgCvfEJJ/eSIq0VObT4xFMXWXlRGXKNhP/5ISuRj+gtOpwNRzKx+dDWJB7is+aua6Ril5OMWd2xSasWeiJP3sxiuGSFrQSmmWbxIdvivWXm+CUSb0NUjKmzJOjexS3FPDLSbLVM9jyO3RHptJ2yFjhejd2iaIfRq4ruCtQHIoT4OMOQ7/+Memi5oJa+MaeMELzS266cwpyUJrjLD+NHfGLXFbQUdI7RIn1Fvh+JhCK2crbWvyo9m61dBXvwwD/TE= 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:(13230001)(4636009)(46966006)(36840700001)(40470700004)(4326008)(336012)(47076005)(426003)(83380400001)(40460700003)(44832011)(36756003)(5660300002)(2906002)(8936002)(7696005)(1076003)(2616005)(54906003)(966005)(6916009)(16526019)(82310400004)(186003)(70206006)(70586007)(26005)(356005)(81166007)(8676002)(19627235002)(508600001)(316002)(86362001)(36860700001)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Feb 2022 14:59:36.5133 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b91878a2-2f1c-4260-809f-08d9f54ac717 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: DM6NAM11FT059.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5060 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3582 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 Acked-by: 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