From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-SN1-obe.outbound.protection.outlook.com (NAM04-SN1-obe.outbound.protection.outlook.com [40.107.70.58]) by mx.groups.io with SMTP id smtpd.web09.13467.1574280446226956282 for ; Wed, 20 Nov 2019 12:07:26 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=eSYyvhgn; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.70.58, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Cfa2hwEZRBlLVnu03Pdkl/rOFSi63mX3NZMou+wX5WR1W/v0aGQQj1uEshCxDdqQ8LLER502i5649QFIFU6z7li8A0hR20uDGSG90T7aqi3tfa5zMvsWfIJSY2gFYNM7XjC0j0aSSd7SGHX9k5xruJ2U2AwmzkaVd5W0ckvm2oRIZaigzUGtB31VCiiqtwm6gy8bXLqeDCaiShA+Ez98d3MWNfN16hihibZ1KxJuU0uBLz5lXm/733pCjuJYrAXIjQkH4xa2OZVGDCFj4+HkfLzPnIMbrSy93yvs6iE7gs2HuURSmicKpMXCFW4ZyOb7LSNsvgRMqtDxUKdfUTSSVg== 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=XkZ2dBmFRuEZfxM+0L8q4sghPu48Vrb3qtF+u2zKPys=; b=Q8GjR88MQCWbZm4aVThWUXCy0vgAuIZzVXyx72eCD3A8TkBSuK7UXu1M61KjcdVrZQUYOQ6EWVru+heG5qZM3qKNZKJ979SttHoue+9nOacRM05sGmhCDg0u/pSwALb2lKIUFf7gIE4BQhRe4rS4iMAZOIDTOO4diA1w5gsms5lpOYIWYvRZItBR2KhEJ6oljM32kMYanFEvA8ptUEnxBRHo/rZLq/k4PMR1k4oaSYjAI4FGBZ26pW+Fzs8hKJmQXv/6dClX9nm5JgOo6ttU72LwVeWQLRZwrWcwdPHCck7yBeJ1z/po9AVD9aftgBj//B5j6xSfDWGl6ZIfhyu6cQ== 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=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XkZ2dBmFRuEZfxM+0L8q4sghPu48Vrb3qtF+u2zKPys=; b=eSYyvhgnm33tz93pNOtzmT0frDnudU+OhZ7+A08j/KOTsBY+pmOZGXYxozCQAmnq8eSzYUo8qWy+2SNU7LWhvUn2zRa7DVwDkPwBtjdpTnvjXidwv5+GeXuxCMCoJT0MTUNvxZdxANJ/qzlSkk6FS3T8ULh0WZDB48HkLNxHmIc= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Received: from DM6PR12MB3163.namprd12.prod.outlook.com (20.179.71.154) by DM6PR12MB3370.namprd12.prod.outlook.com (20.178.198.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2451.29; Wed, 20 Nov 2019 20:07:24 +0000 Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::dd0c:8e53:4913:8ef4]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::dd0c:8e53:4913:8ef4%5]) with mapi id 15.20.2451.031; Wed, 20 Nov 2019 20:07:24 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io Cc: Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Michael D Kinney , Liming Gao , Eric Dong , Ray Ni , Brijesh Singh Subject: [RFC PATCH v3 07/43] UefiCpuPkg: Implement library support for VMGEXIT Date: Wed, 20 Nov 2019 14:06:29 -0600 Message-Id: <5c8f36407e5ac7c7757ae5108cf861b36287a3ce.1574280425.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM3PR12CA0087.namprd12.prod.outlook.com (2603:10b6:0:57::31) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 43e8c1e8-f8e0-46ac-ea4b-08d76df54213 X-MS-TrafficTypeDiagnostic: DM6PR12MB3370: X-MS-Exchange-PUrlCount: 1 X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-Forefront-PRVS: 02272225C5 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4636009)(376002)(39860400002)(346002)(136003)(366004)(396003)(199004)(189003)(36756003)(66946007)(66556008)(50466002)(2906002)(3846002)(48376002)(6116002)(118296001)(5660300002)(6486002)(2351001)(30864003)(2361001)(6306002)(6512007)(6436002)(14454004)(4326008)(6916009)(316002)(966005)(14444005)(25786009)(478600001)(26005)(186003)(16586007)(8676002)(50226002)(54906003)(66066001)(66476007)(99286004)(47776003)(8936002)(81166006)(81156014)(51416003)(6506007)(386003)(52116002)(76176011)(446003)(7736002)(486006)(2616005)(476003)(86362001)(11346002)(305945005);DIR:OUT;SFP:1101;SCL:1;SRVR:DM6PR12MB3370;H:DM6PR12MB3163.namprd12.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: T3BtCOLX0PpL0HHqJoo+mT4nGq3RSnDqJ+jTNHMC/j3FxarcF63l/JIxljrJGmOLbKDD4+Pw4lBfh70/FXCUbfRblc3lz9viwaKU+oIyUm0bmJNpF+xmhxWpNTmBWcDxn4UPSRtxQN+qQKoAAbLeCPDbiCEX6Bu9M58TlrzBS8+dpJBll6xtLyLMAjHVW6FuZ/zAwuk6KE1DQMw4Z6bg/diYZqMhFL353ZeECXs5XZ9DULfq+O34T3/Q89BDhkEXonCeuIya7ZubXp+wgrSg01Q1BQOTwkyNk6Wfog7LyNejgQMjx9Ogbkb1xzZdiNJBorFKV1jxKuNAU8UEHH8bTHah+l43xsMWQx3BVMznITO+hkQIEYosVl50gR5kkoAcyWh9599G9jV8van4RbGdpEDYQ6iYeYHGHs+VIwUVTlSKdPnaEzQv3oEH2Olvz9SdWavvXxdPy+ihuRCfpWaTQQPeS2do1Y8oPsBHTv0zyac= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 43e8c1e8-f8e0-46ac-ea4b-08d76df54213 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Nov 2019 20:07:24.2469 (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: j1JxOG+KGbGV1u4KDXo6T6uBKlOr45Xx7lX3AHVGmof0OJmwhRgsCBx2dI1aWbI3qYsFAU/Ui9WbT8abnZtDow== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3370 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198 To support issuing a VMGEXIT instruction, create a library that can be used to perform GHCB and VMGEXIT related operations and to issue the actual VMGEXIT instruction when using the GHCB. Additionally, two VMGEXIT / MMIO related functions are created to support flash emulation. Flash emulation currently is done by marking the flash area as read-only and taking a nested page fault to perform the emulation of the instruction. However, emulation cannot be performed because there is no instruction decode assist support when SEV-ES is enabled. Provide routines to initiate an MMIO request to perform actual writes to flash. Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Tom Lendacky --- UefiCpuPkg/UefiCpuPkg.dec | 3 + UefiCpuPkg/UefiCpuPkg.dsc | 5 + UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf | 33 +++++ UefiCpuPkg/Include/Library/VmgExitLib.h | 96 ++++++++++++++ UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c | 132 +++++++++++++++++++ UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni | 15 +++ 6 files changed, 284 insertions(+) create mode 100644 UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf create mode 100644 UefiCpuPkg/Include/Library/VmgExitLib.h create mode 100644 UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c create mode 100644 UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index 12f4413ea5b0..90feb9166dc8 100644 --- a/UefiCpuPkg/UefiCpuPkg.dec +++ b/UefiCpuPkg/UefiCpuPkg.dec @@ -53,6 +53,9 @@ [LibraryClasses.IA32, LibraryClasses.X64] ## MpInitLib|Include/Library/MpInitLib.h + ## @libraryclass Provides function to support VMGEXIT processing. + VmgExitLib|Include/Library/VmgExitLib.h + [Guids] gUefiCpuPkgTokenSpaceGuid = { 0xac05bf33, 0x995a, 0x4ed4, { 0xaa, 0xb8, 0xef, 0x7a, 0xe8, 0xf, 0x5c, 0xb0 }} gMsegSmramGuid = { 0x5802bce4, 0xeeee, 0x4e33, { 0xa1, 0x30, 0xeb, 0xad, 0x27, 0xf0, 0xe4, 0x39 }} diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc index d28cb5cccb52..5ab7e423e8ab 100644 --- a/UefiCpuPkg/UefiCpuPkg.dsc +++ b/UefiCpuPkg/UefiCpuPkg.dsc @@ -63,6 +63,7 @@ [LibraryClasses.common.SEC] HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAllocationLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf [LibraryClasses.common.PEIM] MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAllocationLib.inf @@ -74,6 +75,7 @@ [LibraryClasses.common.PEIM] [LibraryClasses.IA32.PEIM, LibraryClasses.X64.PEIM] PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf [LibraryClasses.common.DXE_DRIVER] MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf @@ -81,12 +83,14 @@ [LibraryClasses.common.DXE_DRIVER] CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf RegisterCpuFeaturesLib|UefiCpuPkg/Library/RegisterCpuFeaturesLib/DxeRegisterCpuFeaturesLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf [LibraryClasses.common.DXE_SMM_DRIVER] SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf [LibraryClasses.common.UEFI_APPLICATION] UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf @@ -136,6 +140,7 @@ [Components.IA32, Components.X64] UefiCpuPkg/Library/SmmCpuPlatformHookLibNull/SmmCpuPlatformHookLibNull.inf UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLibStm.inf + UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationPei.inf UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationSmm.inf UefiCpuPkg/SecCore/SecCore.inf diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf new file mode 100644 index 000000000000..6acfa779e75a --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf @@ -0,0 +1,33 @@ +## @file +# VMGEXIT Support Library. +# +# Copyright (c) 2019, Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VmgExitLib + MODULE_UNI_FILE = VmgExitLib.uni + FILE_GUID = 3cd7368f-ef9b-4a9b-9571-2ed93813677e + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = VmgExitLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + VmgExitLib.c + +[Packages] + MdePkg/MdePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[LibraryClasses] + BaseLib + diff --git a/UefiCpuPkg/Include/Library/VmgExitLib.h b/UefiCpuPkg/Include/Library/VmgExitLib.h new file mode 100644 index 000000000000..b5639fbfa1a5 --- /dev/null +++ b/UefiCpuPkg/Include/Library/VmgExitLib.h @@ -0,0 +1,96 @@ +/** @file + Public header file for the VMGEXIT Support library class. + + This library class defines some routines used when invoking the VMGEXIT + instruction in support of SEV-ES. + + Copyright (c) 2019, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __VMG_EXIT_LIB_H__ +#define __VMG_EXIT_LIB_H__ + +#include + + +/** + Perform VMGEXIT. + + Sets the necessary fields of the GHCB, invokes the VMGEXIT instruction and + then handles the return actions. + + @param[in] GHCB A pointer to the GHCB + @param[in] ExitCode VMGEXIT code to be assigned to the SwExitCode field of + the GHCB. + @param[in] ExitInfo1 VMGEXIT information to be assigned to the SwExitInfo1 + field of the GHCB. + @param[in] ExitInfo2 VMGEXIT information to be assigned to the SwExitInfo2 + field of the GHCB. + + @retval 0 VMGEXIT succeeded. + @retval Others VMGEXIT processing did not succeed. Exception number to + be issued. + +**/ +UINTN +EFIAPI +VmgExit ( + GHCB *Ghcb, + UINT64 ExitCode, + UINT64 ExitInfo1, + UINT64 ExitInfo2 + ); + +/** + Perform pre-VMGEXIT initialization/preparation. + + Performs the necessary steps in preparation for invoking VMGEXIT. + + @param[in] GHCB A pointer to the GHCB + +**/ +VOID +EFIAPI +VmgInit ( + GHCB *Ghcb + ); + +/** + Perform post-VMGEXIT cleanup. + + Performs the necessary steps to cleanup after invoking VMGEXIT. + + @param[in] GHCB A pointer to the GHCB + +**/ +VOID +EFIAPI +VmgDone ( + GHCB *Ghcb + ); + +#define VMGMMIO_READ False +#define VMGMMIO_WRITE True + +/** + Perform MMIO write of a buffer to a non-MMIO marked range. + + Performs an MMIO write without taking a #VC. This is useful + for Flash devices, which are marked read-only. + + @param[in] UINT8 A pointer to the destination buffer + @param[in] UINTN The immediate value to write + @param[in] UINTN Number of bytes to write + +**/ +VOID +EFIAPI +VmgMmioWrite ( + UINT8 *Dest, + UINT8 *Src, + UINTN Bytes + ); + +#endif diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c new file mode 100644 index 000000000000..23965b7ff022 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c @@ -0,0 +1,132 @@ +/** @file + VMGEXIT Support Library. + + Copyright (c) 2019, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include + +STATIC +UINTN +VmgExitErrorCheck ( + GHCB *Ghcb + ) +{ + GHCB_EXIT_INFO ExitInfo; + UINTN Reason, Action; + + if (!Ghcb->SaveArea.SwExitInfo1) { + return 0; + } + + ExitInfo.Uint64 = Ghcb->SaveArea.SwExitInfo1; + Action = ExitInfo.Elements.Lower32Bits; + if (Action == 1) { + Reason = ExitInfo.Elements.Upper32Bits; + + switch (Reason) { + case UD_EXCEPTION: + case GP_EXCEPTION: + return Reason; + } + } + + ASSERT (0); + return GP_EXCEPTION; +} + +UINTN +EFIAPI +VmgExit ( + GHCB *Ghcb, + UINT64 ExitCode, + UINT64 ExitInfo1, + UINT64 ExitInfo2 + ) +{ + Ghcb->SaveArea.SwExitCode = ExitCode; + Ghcb->SaveArea.SwExitInfo1 = ExitInfo1; + Ghcb->SaveArea.SwExitInfo2 = ExitInfo2; + AsmVmgExit (); + + return VmgExitErrorCheck (Ghcb); +} + +VOID +EFIAPI +VmgInit ( + GHCB *Ghcb + ) +{ + SetMem (&Ghcb->SaveArea, sizeof (Ghcb->SaveArea), 0); +} + +VOID +EFIAPI +VmgDone ( + GHCB *Ghcb + ) +{ +} + +UINTN +EFIAPI +VmgMmio ( + UINT8 *MmioAddress, + UINT8 *Buffer, + UINTN Bytes, + BOOLEAN Write + ) +{ + UINT64 MmioOp; + UINT64 ExitInfo1, ExitInfo2; + UINTN Status; + GHCB *Ghcb; + MSR_SEV_ES_GHCB_REGISTER Msr; + + Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB); + Ghcb = Msr.Ghcb; + + if (Write) { + MmioOp = SvmExitMmioWrite; + } else { + MmioOp = SvmExitMmioRead; + } + + ExitInfo1 = (UINT64) (UINTN) MmioAddress; + ExitInfo2 = Bytes; + + if (Write) { + CopyMem (Ghcb->SharedBuffer, Buffer, Bytes); + } + + Ghcb->SaveArea.SwScratch = (UINT64) (UINTN) Ghcb->SharedBuffer; + Status = VmgExit (Ghcb, MmioOp, ExitInfo1, ExitInfo2); + if (Status != 0) { + return Status; + } + + if (!Write) { + CopyMem (Buffer, Ghcb->SharedBuffer, Bytes); + } + + return 0; +} + +VOID +EFIAPI +VmgMmioWrite ( + UINT8 *Dest, + UINT8 *Src, + UINTN Bytes + ) +{ + VmgMmio (Dest, Src, Bytes, TRUE); +} + diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni new file mode 100644 index 000000000000..e8656aae4726 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni @@ -0,0 +1,15 @@ +// /** @file +// VMGEXIT support library instance. +// +// VMGEXIT support library instance. +// +// Copyright (c) 2019, Advanced Micro Devices, Inc. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "VMGEXIT Support Library." + +#string STR_MODULE_DESCRIPTION #language en-US "VMGEXIT Support Library." + -- 2.17.1