From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt0-x22d.google.com (mail-qt0-x22d.google.com [IPv6:2607:f8b0:400d:c0d::22d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 79E6880407 for ; Thu, 16 Mar 2017 19:02:11 -0700 (PDT) Received: by mail-qt0-x22d.google.com with SMTP id r45so52784077qte.3 for ; Thu, 16 Mar 2017 19:02:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=oAE2Tusuq3zQIcxBybUXmw/ziq6OfdXqa5TiSwEkT48=; b=EUTjoQ+xNzJt2Ezm5IeiSIMWjrnleaOwaTMcIUnJTTqKLO8bzn+Tvh1+WyWvAzH3Mt n6p3i4EpKm6d6VBy7GYvF2rzgkDcU2B4UtdTLsVyjK//e5q4kn3vLe9+DuaHjumMwyMq moAVBYSAUWm48JFUPfBZHkBLEPvjYZSr68I354a3/Br9nSMuBKstlmQsSKlMxnzMvO6h 9CiogAGR2Jloyb07rmnyolItZNT//X2tdHxjFy0OUgyCJHoqBbcWHSYIL7NBZuQpx0Fr 80HitXbhwDeBMxVUgTCEyd2f1JcEmVgVwoxuLZ/N7jYF74Q0M43u+Fu+Y8SF7MHE8C3a Fgjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=oAE2Tusuq3zQIcxBybUXmw/ziq6OfdXqa5TiSwEkT48=; b=HuI/NSbEIh/dfL+Ls2AlAGOb08CqnGHZnsoj87oT3FK8QOEktmseijELYDpKvWRmDo tJ5GGAsdCmCamR38r3FbMCixsgXsPBp8q/n9wPDbkZWaFkrWJ67l6B6v4YGDSXAAO4Rk lZZJaeUSCcEAt960CNHyCTjFJHiheSaP3zxCFFFQ60nj8y+3iddWG+mwcS6kGZXC4kEk xM3ucnr3fXbWS+d7uSqg1i444d5rdBbZrwb5rRqanA4FXWE/snai2KqhMzOzga3CapVU BNfwF/syT5Gs5E6qvQjuPTVbqCzKM+zNeAncfP3012JlIQJo/g6Lw91cjOZzX2/opOXK pM7Q== X-Gm-Message-State: AFeK/H2qMGWkpEHzSCrjH/3dVKheoW6Wl/YbNPLMxRfR3epcC7tzYIYQixlLBRUDHg0gAnoDAD7uGopVZGvFFg== X-Received: by 10.200.34.212 with SMTP id g20mr11648758qta.97.1489716130348; Thu, 16 Mar 2017 19:02:10 -0700 (PDT) MIME-Version: 1.0 Received: by 10.12.182.65 with HTTP; Thu, 16 Mar 2017 19:02:09 -0700 (PDT) In-Reply-To: References: <148884284887.29188.7643544710695103939.stgit@brijesh-build-machine> <148884286215.29188.1084675072356724555.stgit@brijesh-build-machine> From: Brijesh Singh Date: Thu, 16 Mar 2017 21:02:09 -0500 Message-ID: To: Laszlo Ersek Cc: "Justen, Jordan L" , edk2-devel@ml01.01.org, Leo Duran , brijesh.singh@amd.com, Tom Lendacky X-Content-Filtered-By: Mailman/MimeDel 2.1.22 Subject: Re: [RFC PATCH v1 2/5] OvmfPkg/MemcryptSevLib: Add SEV helper library 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: Fri, 17 Mar 2017 02:02:11 -0000 Content-Type: text/plain; charset=UTF-8 On Wed, Mar 8, 2017 at 2:40 AM, Laszlo Ersek wrote: > On 03/07/17 23:36, Brijesh Singh wrote: > > > > > > On Tue, Mar 7, 2017 at 4:08 PM, Laszlo Ersek > > wrote: > > > > On 03/07/17 20:14, Brijesh Singh wrote: > > > On Tue, Mar 7, 2017 at 11:06 AM, Laszlo Ersek > > wrote: > > I think SevActive() and SevInitialize() should become part of > > PlatformPei only (if that's possible). > > > > The upcoming PTE massaging functions could become part of the DMA lib > > stuff that you mention (as functions with external linkage), and then > > you could pull the DMA lib into QemuVideoDxe just to make these > > functions available. > > > > Presently the suggested functions don't seem to justify two (or even > > one) new libclass. > > > > > > > > I think I should be able to accomate SevInitialize() and SevActive() > > function inside the PlatformPei. I will drop MemcryptSevLib library in > > next rev. > > > > I will go with your idea for adding PTE massaging function directly > > inside the DMA library and will link that into QemuVideoDxe. > > > > Only part which I have not yet figured out, how to deal with Qemu > > FW_CFG DMA support, I believe some of FW_CFG DMA read and write > > happens fairly early (PEI stage). > > That's right, off the top of my head, minimally PlatformPei uses fw_cfg > heavily during PEI. > > > The PTE massaging code may need to > > allocate memory and not sure how to allocate dynamic memory in early > stages. > > Any pointers ? > > You can use MemoryAllocationLib functions for that (such as > AllocatePool() and AllocatePages()). The OVMF DSC files resolve the lib > class for the PEI phase like this: > > > MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/ > PeiMemoryAllocationLib.inf > > and the PeiMemoryAllocationLib instance maps those functions to the PEI > services. > > A few important things about this: > > - AllocatePool() works up to only ~64KB in size, and the allocation is > backed by a new HOB. Generally speaking, the HOB may be moved to a > different spot in memory before entering the DXE phase, so pointers > returned by such AllocatePool() calls (in PEI) are not safe to > dereference in the DXE phase. > > - FreePool() does nothing, the allocated memory (the HOB, see above) is > only released when the guest OS starts (and it drops all boot services > data type memory). > > - AllocatePages() works as it says on the tin, and the pointer returned > by it is safe to dereference in DXE. > > I took a stab at implementing SEV specific DMA hooks into QemuFwCfgLib. But I found that QemuFWCfgLib is used in both PEI and Dxe phases. It makes things interesting, in SEV guest we can perform DMA operation only when processor is either in 32-bit PAE or long mode (mainly because C-bit is not accessiable in 32 or 16-bit mode). It limits us to using OvmfX64.dsc. Ideally, I would prefer to support both OvmfPkgIa32X64.dsc and OvmfPkgX64.dsc. In my code browsing so far I have found that only QemuFwCfgLib does DMA operations in PEI phase and other packages perform the DMA operation in DXE phase. If we can somehow manage to not require the DMA support in PEI phase then we should be able to support both OvmfPkIa32X64.dsc and OvmfPkgX64.dsc. How about defaulting to I/O operations in PEI stage for SEV guest ? I do understand that QEMU prefers us to use DMA interface for FwCfg write. Additionally, I found that some FwCfg DMA access happens before PublishPeiMemory() hence AllocatePages() was failing to allocate the bounce buffer for SEV DMA. I was thinking that for SEV guest if we get a request to perform smaller reads or writes (maybe < 64 bytes) then silently fallback to IO else perform DMA operations. Thoughts ? -Brijesh - FreePages() however is again a no-op, it practically leaks the memory, > and only the guest OS will be able to release it (see FreePool() above). > One workaround for this could be to stash the address of the PEI-phase > allocation in a GUID HOB or a PCD, and then let some DXE driver in the > DXE phase release the memory with gBS->FreePages(). I'm not sure though > if this complexity is worth it. > > - Note that it is PlatformPei itself that installs the permanent PEI > RAM. Before that happens, PEIMs (including PlatformPei itself) can only > allocate memory from the temporary SEC/PEI heap, which is very very > small, and only AllocatePool() would work at that point (AllocatePages() > wouldn't). However, if you place the AllocatePages() function call after > PublishPeiMemory(), then things should work. > > As far as I can see, you added MemcryptSevInitialize() to the end of > InitializePlatform(); allocating pages at that point should be fine. > > - During S3 resume, a different (pre-reserved) memory area is used as > permanent PEI RAM, which is quite smaller than the one used during > normal boot. It means that, if you need a lot of memory for setting up > SEV during S3 resume, AllocatePages() might run out of memory, and we > might have to resize the pre-reservation mentioned above. > > - If you could reasonably bound the allocation size with a constant, it > might be simplest to use static arrays / variables. Those would be > dropped as soon as the PEI phase was exited. As one quirk however, you > should not rely on such variables being zero-initialized during S3 resume. > > Thanks > Laszlo > -- Confusion is always the most honest response.