From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 373C494195D for ; Fri, 8 Mar 2024 15:31:27 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=OMzYGslqM3c89BheHMnO5VhPJXJox/AEO4vi8ni8tp8=; c=relaxed/simple; d=groups.io; h=Received-SPF:From:To:CC:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20240206; t=1709911885; v=1; b=hVIXNhqmaF0OWDRZqpYlXB1stVQz3D1+SsZqPIYfu9JUfp5mT1Vi6LNdC4le3RvtEqWT19zk R3gb5P4AYbYJJmWwizmLIjQGVvFELGjMME94muU4T5SdW8BaRFcQK/G1tG5ZQdaYA6U7v+8vwdG Po/JquCY7halVl8t79xmP6ug5/fg8T4GLCGwjn1r2CZ8xxxvYsPjZ9wM6rFQeHF3nC2nL0SOQBZ z+t7/u9+/flFcZ7u/seAhKRRqIQ9tzUothccu9rBp9XbBK6JCxDZBRlAIB8yeYj2zgpfuesy18E pI+kCgL8S7XpdEEhq6hkRKs/r+nHLLrSJOs+zkFw/wJMw== X-Received: by 127.0.0.2 with SMTP id Xt6FYY7687511xZpsAfz7ppU; Fri, 08 Mar 2024 07:31:25 -0800 X-Received: from NAM12-DM6-obe.outbound.protection.outlook.com (NAM12-DM6-obe.outbound.protection.outlook.com [40.107.243.77]) by mx.groups.io with SMTP id smtpd.web10.24795.1709911885272145009 for ; Fri, 08 Mar 2024 07:31:25 -0800 X-Received: from CYZPR20CA0018.namprd20.prod.outlook.com (2603:10b6:930:a2::6) by IA1PR12MB6332.namprd12.prod.outlook.com (2603:10b6:208:3e2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.27; Fri, 8 Mar 2024 15:31:22 +0000 X-Received: from CY4PEPF0000E9DA.namprd05.prod.outlook.com (2603:10b6:930:a2:cafe::97) by CYZPR20CA0018.outlook.office365.com (2603:10b6:930:a2::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.31 via Frontend Transport; Fri, 8 Mar 2024 15:31:22 +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; pr=C X-Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9DA.mail.protection.outlook.com (10.167.241.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7362.11 via Frontend Transport; Fri, 8 Mar 2024 15:31:22 +0000 X-Received: from tlendack-t1.amdoffice.net (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.2507.35; Fri, 8 Mar 2024 09:31:21 -0600 From: "Lendacky, Thomas via groups.io" To: CC: Ard Biesheuvel , Erdem Aktas , Gerd Hoffmann , Jiewen Yao , Laszlo Ersek , Liming Gao , Michael D Kinney , Min Xu , Zhiguang Liu , "Rahul Kumar" , Ray Ni , Michael Roth Subject: [edk2-devel] [PATCH v3 11/24] MdePkg/BaseLib: Add a new VMGEXIT instruction invocation for SVSM Date: Fri, 08 Mar 2024 07:31:25 -0800 Message-ID: <667d0ff8da68336a105c3428626726276ecfb424.1709911792.git.thomas.lendacky@amd.com> In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DA:EE_|IA1PR12MB6332:EE_ X-MS-Office365-Filtering-Correlation-Id: f00a3917-68f1-44d8-3274-08dc3f84cf1b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: gfCQCexKa4LXmuzOsQddR5Nr2SMEpHxe8XZyDrje2M9hjjTkLq8KAQ3UbT74LhlJpHtOPXZRo5gBDkCVtv7vI2JHftZKed0vZkasKak4GFRnIfmkRqmfn4hBGwTmK9qr7qW815hSExsffsbCbnkMGhORKZkqRYZ0tOxvj0Ri/HUcc7wNf0hwnD2iKtnjIJcuawmhIBFDwWMAI+m/W9miFAPTXzNP/aOUxDBg9+jOujzCm25AJ5pyl5yhQp9F4lY/5F04rZPGEeGS0Jb3I4jWIJpprLt27ycWSNg7sR+pJD3fD6WJbU5JedfxJNhY/2xXDnj3qqsi7MmCxlnin92jG86NbAQQHK6rK8fVquSOZ9jtkhwyOkNVYWx0m/2GIPfdFqQyW3IsYLJoVLRlQE3GDzViBcRm7Q25KXa57A3zkq3c9p3aB9wVOplrsWIO9daqfAp42tcr+e81e4AiflxgYt2CeKr3s/YKew617YLp/BqJnoku7dDne1TIqhgPBPtHSBnHL3grjsQVJO2lY06YJlXAyufIaDI6n6tDuPOqHjQs2lkBOfbHt5iqAP9hIa5X8pTNinwPfbX8dkSkYRZZcYDqYE9N38pEEX7/LVFwEeqik+gKPQuUzMnlnDaGI0y85cGyYHqPv9oaL8piCjOwhKLXF7iSfJ2g6r4msj8fuPeQv0kW01Wu53Mcrfw5p0NEt7ilrxe9R6hOhqyyRMxhYA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2024 15:31:22.1573 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f00a3917-68f1-44d8-3274-08dc3f84cf1b 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: CY4PEPF0000E9DA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6332 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,thomas.lendacky@amd.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: CLTvglh8pAGLuNo3P601tOHUx7686176AA= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=hVIXNhqm; dmarc=pass (policy=none) header.from=groups.io; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4654 The SVSM specification relies on a specific register calling convention to hold the parameters that are associated with the SVSM request. The SVSM is invoked by requesting the hypervisor to run the VMPL0 VMSA of the guest using the GHCB MSR Protocol or a GHCB NAE event. Create a new version of the VMGEXIT instruction that will adhere to this calling convention and load the SVSM function arguments into the proper register before invoking the VMGEXIT instruction. On return, perform the atomic exchange on the SVSM call pending value as specified in the SVSM specification. Cc: Liming Gao Cc: Michael D Kinney Cc: Zhiguang Liu Acked-by: Gerd Hoffmann Signed-off-by: Tom Lendacky --- MdePkg/Library/BaseLib/BaseLib.inf | 2 + MdePkg/Include/Library/BaseLib.h | 39 ++++++++ MdePkg/Library/BaseLib/Ia32/VmgExitSvsm.nasm | 39 ++++++++ MdePkg/Library/BaseLib/X64/VmgExitSvsm.nasm | 94 ++++++++++++++++++++ 4 files changed, 174 insertions(+) diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/Ba= seLib.inf index 4dbe94be71e1..26e66a8d67cf 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -187,6 +187,7 @@ [Sources.Ia32] Ia32/XGetBv.nasm Ia32/XSetBv.nasm Ia32/VmgExit.nasm + Ia32/VmgExitSvsm.nasm =20 Ia32/DivS64x64Remainder.c Ia32/InternalSwitchStack.c | MSFT @@ -328,6 +329,7 @@ [Sources.X64] X64/XGetBv.nasm X64/XSetBv.nasm X64/VmgExit.nasm + X64/VmgExitSvsm.nasm ChkStkGcc.c | GCC =20 [Sources.EBC] diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/Base= Lib.h index 1fff0fb224f1..95f805599d9d 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -7876,6 +7876,45 @@ AsmVmgExit ( VOID ); =20 +/// +/// The structure used to supply and return data to and from the SVSM. +/// +typedef struct { + VOID *Caa; + UINT64 RaxIn; + UINT64 RcxIn; + UINT64 RdxIn; + UINT64 R8In; + UINT64 R9In; + UINT64 RaxOut; + UINT64 RcxOut; + UINT64 RdxOut; + UINT64 R8Out; + UINT64 R9Out; + UINT8 *CallPending; +} SVSM_CALL_DATA; + +/** + Executes a VMGEXIT instruction (VMMCALL with a REP prefix) with argument= s + and return code + + Executes a VMGEXIT instruction placing the specified arguments in the + corresponding registers before invocation. Upon return an XCHG is done t= o + atomically clear and retrieve the SVSM call pending value. The returned = RAX + register value becomes the function return code. This function is intend= ed + for use with an SVSM. This function is only available on IA-32 and x64. + + @param[in,out] SvsmCallPending Pointer to the location of the SVSM cal= l data + + @return Value of the RAX register on return + +**/ +UINT32 +EFIAPI +AsmVmgExitSvsm ( + IN OUT SVSM_CALL_DATA *SvsmCallData + ); + /** Patch the immediate operand of an IA32 or X64 instruction such that the = byte, word, dword or qword operand is encoded at the end of the instruction's diff --git a/MdePkg/Library/BaseLib/Ia32/VmgExitSvsm.nasm b/MdePkg/Library/= BaseLib/Ia32/VmgExitSvsm.nasm new file mode 100644 index 000000000000..14717bd1af02 --- /dev/null +++ b/MdePkg/Library/BaseLib/Ia32/VmgExitSvsm.nasm @@ -0,0 +1,39 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved. +; SPDX-License-Identifier: BSD-2-Clause-Patent +; +; Module Name: +; +; VmgExitSvsm.Asm +; +; Abstract: +; +; AsmVmgExitSvsm function +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + + DEFAULT REL + SECTION .text + +;-------------------------------------------------------------------------= ----- +; UINT32 +; EFIAPI +; AsmVmgExitSvsm ( +; SVSM_CALL_DATA *SvsmCallData +; ); +;-------------------------------------------------------------------------= ----- +global ASM_PFX(AsmVmgExitSvsm) +ASM_PFX(AsmVmgExitSvsm): +; +; NASM doesn't support the vmmcall instruction in 32-bit mode and NASM ver= sions +; before 2.12 cannot translate the 64-bit "rep vmmcall" instruction into e= lf32 +; format. Given that VMGEXIT does not make sense on IA32, provide a stub +; implementation that is identical to CpuBreakpoint(). In practice, +; AsmVmgExitSvsm() should never be called on IA32. +; + int 3 + ret + diff --git a/MdePkg/Library/BaseLib/X64/VmgExitSvsm.nasm b/MdePkg/Library/B= aseLib/X64/VmgExitSvsm.nasm new file mode 100644 index 000000000000..b8af78890611 --- /dev/null +++ b/MdePkg/Library/BaseLib/X64/VmgExitSvsm.nasm @@ -0,0 +1,94 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved. +; SPDX-License-Identifier: BSD-2-Clause-Patent +; +; Module Name: +; +; VmgExitSvsm.Asm +; +; Abstract: +; +; AsmVmgExitSvsm function +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + + DEFAULT REL + SECTION .text + +;-------------------------------------------------------------------------= ----- +; typedef struct { +; VOID *Caa; +; UINT64 RaxIn; +; UINT64 RcxIn; +; UINT64 RdxIn; +; UINT64 R8In; +; UINT64 R9In; +; UINT64 RaxOut; +; UINT64 RcxOut; +; UINT64 RdxOut; +; UINT64 R8Out; +; UINT64 R9Out; +; UINT8 *CallPending; +; } SVSM_CALL_DATA; +; +; UINT32 +; EFIAPI +; AsmVmgExitSvsm ( +; SVSM_CALL_DATA *SvsmCallData +; ); +;-------------------------------------------------------------------------= ----- +global ASM_PFX(AsmVmgExitSvsm) +ASM_PFX(AsmVmgExitSvsm): + push r10 + push r11 + push r12 + +; +; Calling convention has SvsmCallData in RCX. Move RCX to R12 in order to +; properly populate the SVSM register state. +; + mov r12, rcx + + mov rax, [r12 + 8] + mov rcx, [r12 + 16] + mov rdx, [r12 + 24] + mov r8, [r12 + 32] + mov r9, [r12 + 40] + +; +; Set CA call pending +; + mov r10, [r12] + mov byte [r10], 1 + + rep vmmcall + + mov [r12 + 48], rax + mov [r12 + 56], rcx + mov [r12 + 64], rdx + mov [r12 + 72], r8 + mov [r12 + 80], r9 + +; +; Perform the atomic exchange and return the CA call pending value. +; The call pending value is a one-byte field at offset 0 into the CA, +; which is currently the value in R10. +; + + mov r11, [r12 + 88] ; Get CallPending address + mov cl, byte [r11] + xchg byte [r10], cl + mov byte [r11], cl ; Return the exchanged value + + pop r12 + pop r11 + pop r10 + +; +; RAX has the value to be returned from the SVSM +; + ret + --=20 2.43.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#116536): https://edk2.groups.io/g/devel/message/116536 Mute This Topic: https://groups.io/mt/104810711/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-