From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E149D20D2C3BA for ; Mon, 27 Mar 2017 02:29:02 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 47F868553C; Mon, 27 Mar 2017 09:29:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 47F868553C Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=lersek@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 47F868553C Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-77.phx2.redhat.com [10.3.116.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35E9C17C46; Mon, 27 Mar 2017 09:28:59 +0000 (UTC) To: Brijesh Singh , michael.d.kinney@intel.com, jordan.l.justen@intel.com, edk2-devel@ml01.01.org, liming.gao@intel.com References: <149013076154.27235.10725020825643505862.stgit@brijesh-build-machine> <149013080578.27235.9783953356246180002.stgit@brijesh-build-machine> Cc: brijesh.singh@amd.com, leo.duran@amd.com, Thomas.Lendacky@amd.com From: Laszlo Ersek Message-ID: <5d6d6dfa-4077-f560-bf89-878ca35f368e@redhat.com> Date: Mon, 27 Mar 2017 11:28:59 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <149013080578.27235.9783953356246180002.stgit@brijesh-build-machine> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 27 Mar 2017 09:29:02 +0000 (UTC) Subject: Re: [RFC PATCH v2 07/10] OvmfPkg/BmDmaLib: Add SEV support X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Mar 2017 09:29:03 -0000 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit On 03/21/17 22:13, Brijesh Singh wrote: > When SEV is enabled, the DMA operations must be performed on a shared > (i.e unencrypted) pages. The patch adds SEV specific hooks to use the > bounce buffer when caller map/unmap host address to a DMA address and > similarly clears/set memory encryption attribute when caller allocates or > free the DMA pages. > > Signed-off-by: Brijesh Singh > --- > OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.c | 60 +++++++++++++++++++++++++++ > OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf | 3 + > 2 files changed, 61 insertions(+), 2 deletions(-) It looks like Leo's DxeBmDmaLib isn't final yet (based on the v4 feedback I can see on the list). Can we perhaps modify the BmDmaLib library class (= public interface) such that a client module (an executable or even another library) can reuse the base instance of BmDmaLib, and only provide the hooks? If not, I guess it's not a big deal, MdeModulePkg/Library/DxeBmDmaLib contains about 10-12 KB of source. I figured I'd ask though. Thanks, Laszlo > > diff --git a/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.c b/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > index 0d960a8..39814cc 100644 > --- a/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > +++ b/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > > > #define FORCE_BELOW_4GB_TRUE TRUE > @@ -100,6 +101,15 @@ AllocateBounceBuffer ( > } > > // > + // Clear C-bit on DMA pages > + // > + if (MemEncryptSevIsEnabled ()) { > + Status = MemEncryptSevClearPageEncMask (MapInfo->MappedHostAddress, MapInfo->NumberOfPages); > + if (Status != EFI_SUCCESS) { > + return Status; > + } > + } > + // > // If this is a read operation from the Bus Master's point of view, > // then copy the contents of the real buffer into the mapped buffer > // so the Bus Master can read the contents of the real buffer. > @@ -170,6 +180,23 @@ BmDmaMap ( > > PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress; > if (DmaAbove4GB || (PhysicalAddress + *NumberOfBytes) <= SIZE_4GB) { > + > + // > + // When SEV is enabled the DMA operation must be performed on shared pages. We force to use the > + // bounce buffer path which will take care of allocating shared Dma buffers mapping > + // > + if (MemEncryptSevIsEnabled () && > + (Operation == DmaOperationBusMasterRead || Operation == DmaOperationBusMasterWrite)) { > + return AllocateBounceBuffer ( > + FORCE_BELOW_4GB_FALSE, > + Operation, > + PhysicalAddress, > + NumberOfBytes, > + DeviceAddress, > + Mapping > + ); > + } > + > // > // If we CAN handle DMA above 4GB or the transfer is below 4GB, > // the DeviceAddress is simply the HostAddress > @@ -218,7 +245,8 @@ BmDmaUnmap ( > IN VOID *Mapping > ) > { > - MAP_INFO *MapInfo; > + MAP_INFO *MapInfo; > + EFI_STATUS Status; > > // > // Check for invalid inputs > @@ -251,6 +279,17 @@ BmDmaUnmap ( > } > > // > + // When SEV is enabled then Dma buffer allocate by bounce buffer have C-bit cleared, > + // restore the C-bit before we release the resources > + // > + if (MemEncryptSevIsEnabled ()) { > + Status = MemEncryptSevSetPageEncMask (MapInfo->MappedHostAddress, MapInfo->NumberOfPages); > + if (Status != EFI_SUCCESS) { > + return Status; > + } > + } > + > + // > // Free the mapped buffer and the MAP_INFO structure. > // > gBS->FreePages (MapInfo->MappedHostAddress, MapInfo->NumberOfPages); > @@ -322,8 +361,15 @@ BmDmaAllocateBuffer ( > ); > if (!EFI_ERROR (Status)) { > *HostAddress = (VOID *) (UINTN) PhysicalAddress; > + // > + // Clear C-bit on Dma pages > + // > + if (MemEncryptSevIsEnabled ()) { > + Status = MemEncryptSevClearPageEncMask (PhysicalAddress, Pages); > + } > } > > + > return Status; > } > > @@ -346,6 +392,18 @@ BmDmaFreeBuffer ( > IN UINTN Pages > ) > { > + EFI_STATUS Status; > + > + // > + // Restore the C-bit on DMA pages > + // > + if (MemEncryptSevIsEnabled ()) { > + Status = MemEncryptSevSetPageEncMask ((UINTN) HostAddress, Pages); > + if (Status != EFI_SUCCESS) { > + return Status; > + } > + } > + > return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages); > } > > diff --git a/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf b/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > index 4ddb27d..fb97caa 100644 > --- a/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > +++ b/OvmfPkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > @@ -29,6 +29,7 @@ > [Packages] > MdePkg/MdePkg.dec > MdeModulePkg/MdeModulePkg.dec > + OvmfPkg/OvmfPkg.dec > > [LibraryClasses] > BaseLib > @@ -37,5 +38,5 @@ > DxeServicesTableLib > MemoryAllocationLib > UefiBootServicesTableLib > - > + MemEncryptSevLib > > > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel >