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 EAB3474004A for ; Thu, 22 Feb 2024 17:31:35 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=BeVpv1TeoI+oniGZ6vRwyXRvcnWFSAfvmK68Oglgcw0=; c=relaxed/simple; d=groups.io; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results: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=20140610; t=1708623094; v=1; b=fCU2jlK/zfCx9ICZsjPvgcnMCHCHxhcdaioxINxi+XDjlbDsy9e8XQj3t2Tm7GkmY33kjDfD Jx9qDkOn+bx56zjD0pX/DlRlZCJ0O+wm99XsKaj7vqgiF74sK/+lRnuxOTRvgl2wL3A91OGBj4M /5WVRfAoD/3kRKb8aX6gatBA= X-Received: by 127.0.0.2 with SMTP id 5dXGYY7687511xIkkjMdv7lt; Thu, 22 Feb 2024 09:31:34 -0800 X-Received: from NAM10-DM6-obe.outbound.protection.outlook.com (NAM10-DM6-obe.outbound.protection.outlook.com [40.107.93.40]) by mx.groups.io with SMTP id smtpd.web11.18822.1708623093727658847 for ; Thu, 22 Feb 2024 09:31:34 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mFTES7eAcHKnZGJ/ZJ8Xr/NttbH8gHAtWbNwvG/Qi3BjE+fnSFLMytuB37ihuzMcqOn0XNZWkIgnld/1sggqyGD5boUWhvZO9ojViQTVXzkMCQTqfz4/gqRVIlF7fZQQPDF9F/aTl1iDtPpxPAl448ov+h0HeXCd77HgjCYOpjO6h8JT0StUZq7dJ2OI88A1fGRIv88imAG82REf6z+Fl291q8E1GV4Hg5wHHoGHoMarWAHSFj+F5RGAP4lYTZiU7emNxOc6xSIWqKR7XxKWlPX9cItH0HRv6tCnVnp9RLT38IjIn4N8ZGAX4c1fsZSWLSsHGU0c86FwUyTwjJbmTw== 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=sr97pu4Kav3MfPve5Oxm640CSf0O6QeXOTlNub1Hu9w=; b=MhbjnkN+3HQIH+3ajmGt/tYerxOn12nfu8kDK7tflndAhRcMEfLyS7Cb6tfYr+b5GLRcZm7UQ4R/AyaVJYT5SzinK4fw43hb5riF+4RWzo3gLefgPNK75DV3yk/dgGhftmUbON7WpW0StA9/3bCw03KupiEyFsWn7hnEpJ5KwBoodpHlGHmdggifa6xcECNjL89PZ1YJynMbtjPD/pfCdLMMr8Nt8gb53ZzE9YGwp9zsBbQCPS5n4prz389GvxPWPHLEHMLd17YlabH1pIxHVVdKcIAOttFPF/Q/1i2oNChrjjFrtWTVwf5S2nD/aJoF4BpemzA+n+DoXypyFJcMpQ== 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 (0) X-Received: from BN0PR04CA0074.namprd04.prod.outlook.com (2603:10b6:408:ea::19) by LV8PR12MB9110.namprd12.prod.outlook.com (2603:10b6:408:18b::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.27; Thu, 22 Feb 2024 17:31:31 +0000 X-Received: from BN2PEPF000044A5.namprd04.prod.outlook.com (2603:10b6:408:ea:cafe::b1) by BN0PR04CA0074.outlook.office365.com (2603:10b6:408:ea::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.43 via Frontend Transport; Thu, 22 Feb 2024 17:31:31 +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 BN2PEPF000044A5.mail.protection.outlook.com (10.167.243.104) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7292.25 via Frontend Transport; Thu, 22 Feb 2024 17:31:30 +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; Thu, 22 Feb 2024 11:31:30 -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 v2 11/23] MdePkg/BaseLib: Add a new VMGEXIT instruction invocation for SVSM Date: Thu, 22 Feb 2024 11:29:50 -0600 Message-ID: <07e067e33b8c7d50de21886aa503df8e0f225403.1708623001.git.thomas.lendacky@amd.com> In-Reply-To: References: MIME-Version: 1.0 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-TrafficTypeDiagnostic: BN2PEPF000044A5:EE_|LV8PR12MB9110:EE_ X-MS-Office365-Filtering-Correlation-Id: dbf01f04-de87-497b-b6df-08dc33cc1ba4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: jTnCG19xatif+3OzZOEOavM2gr2/7bWDTDiqGJcFBe8mXJSkvMfDzsvp3bFjQ2dzpjeLBoqMwOulSZ8t4e73rlwgQoAX0n2k8+EPm0SHMFvfG6/j6kNcIouGRevWDwyjFLQ0EI9WpkerdOovgIm51GxXYCjDg8ckHbMvb1muBX0PSPLLtyCUKC86gq44DxB2iKMP1QeRNqbjjHcYXe+LmBGP+YIvlA4k6YWlb1riUP+WidZj7L55ujOK0p2X8CtuSMxvMZLavBQDYJDc8CuCUlsAeer04rZdQiSokBzfmejGysuqgTBhmVL85Ip1XsLeIwZSUe/wjeUFJ7tlXSuGxctYEfRIaMz02mVrJEEvg8XWuaIm1X3c8/07jX0JKLvu/zkjURCOs9IWrlkFsy3YfG5Pk+3DJ9/9c+aT+47hPBIIB4duiQ4NLcFpn1+wFwtpPkciPJxHXvDVQ/526Wh94MtElJcq4g6pyKcSAyhNmwHBL66MKB+YurmJ99WOrhkje4He5Oz1c8Fj1iQ7pgXEhmJrfEkOxj3KjSsvnG2zWScVYJBpEUeW7gJQdTkGumMySXZhxAbjKN5c4KVhGpFby8bQroOwNfKNfv33i4TEKa95I0HhPhD5IbfUPUH4gDCu0/mZjp0cyQ1B6kEBEUD1Ww== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2024 17:31:30.9423 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dbf01f04-de87-497b-b6df-08dc33cc1ba4 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: BN2PEPF000044A5.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9110 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: 6z8xhetGnaF7Y6L0q35VzbRRx7686176AA= 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=20140610 header.b="fCU2jlK/"; 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; arc=reject ("signature check failed: fail, {[1] = sig:microsoft.com:reject}") 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. 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.42.0 -=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 (#115846): https://edk2.groups.io/g/devel/message/115846 Mute This Topic: https://groups.io/mt/104512956/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-