public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Lendacky, Thomas" <thomas.lendacky@amd.com>
To: Ashish Kalra <Ashish.Kalra@amd.com>, devel@edk2.groups.io
Cc: brijesh.singh@amd.com, jejb@linux.ibm.com, erdemaktas@google.com,
	jiewen.yao@intel.com, min.m.xu@intel.com, lersek@redhat.com,
	jordan.l.justen@intel.com, ard.biesheuvel@arm.com
Subject: Re: [PATCH v4 2/4] OvmfPkg/BaseMemEncryptLib: Support to issue unencrypted hypercall
Date: Tue, 22 Jun 2021 17:50:03 -0500	[thread overview]
Message-ID: <6fa3515f-81b4-ce03-f3c0-2358273b5dba@amd.com> (raw)
In-Reply-To: <3bfdbe553d597f489a03c21fbc0f6c614f92c32f.1624281247.git.ashish.kalra@amd.com>

On 6/21/21 8:57 AM, Ashish Kalra wrote:
> From: Brijesh Singh <brijesh.singh@amd.com>
> 
> By default all the SEV guest memory regions are considered encrypted,
> if a guest changes the encryption attribute of the page (e.g mark a
> page as decrypted) then notify hypervisor. Hypervisor will need to
> track the unencrypted pages. The information will be used during
> guest live migration, guest page migration and guest debugging.
> 
> Invoke hypercall via the new hypercall library.
> 
> This hypercall is used to notify hypervisor when a page is marked as
> 'decrypted' (i.e C-bit removed).
> 
> Cc: Jordan Justen <jordan.l.justen@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
> 
> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
> ---
>  OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf   |  1 +
>  OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf   |  1 +
>  OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 22 ++++++++++++++++++++
>  3 files changed, 24 insertions(+)
> 
> diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
> index f2e162d680..aefcd7c0f7 100644
> --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
> +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
> @@ -49,6 +49,7 @@
>    DebugLib
>    MemoryAllocationLib
>    PcdLib
> +  MemEncryptHypercallLib
>  
>  [FeaturePcd]
>    gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire
> diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf
> index 03a78c32df..7503f56a0b 100644
> --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf
> +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf
> @@ -49,6 +49,7 @@
>    DebugLib
>    MemoryAllocationLib
>    PcdLib
> +  MemEncryptHypercallLib
>  
>  [FeaturePcd]
>    gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire
> diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c
> index c696745f9d..12b3a9fcfb 100644
> --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c
> +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c
> @@ -15,6 +15,7 @@
>  #include <Library/MemEncryptSevLib.h>
>  #include <Register/Amd/Cpuid.h>
>  #include <Register/Cpuid.h>
> +#include <Library/MemEncryptHypercallLib.h>
>  
>  #include "VirtualMemory.h"
>  
> @@ -585,6 +586,9 @@ SetMemoryEncDec (
>    UINT64                         AddressEncMask;
>    BOOLEAN                        IsWpEnabled;
>    RETURN_STATUS                  Status;
> +  UINTN                          Size;
> +  BOOLEAN                        CBitChanged;
> +  PHYSICAL_ADDRESS               OrigPhysicalAddress;
>  
>    //
>    // Set PageMapLevel4Entry to suppress incorrect compiler/analyzer warnings.
> @@ -636,6 +640,10 @@ SetMemoryEncDec (
>  
>    Status = EFI_SUCCESS;
>  
> +  Size = Length;
> +  CBitChanged = FALSE;
> +  OrigPhysicalAddress = PhysicalAddress;
> +
>    while (Length != 0)
>    {
>      //
> @@ -695,6 +703,7 @@ SetMemoryEncDec (
>            ));
>          PhysicalAddress += BIT30;
>          Length -= BIT30;
> +        CBitChanged = TRUE;
>        } else {
>          //
>          // We must split the page
> @@ -749,6 +758,7 @@ SetMemoryEncDec (
>            SetOrClearCBit (&PageDirectory2MEntry->Uint64, Mode);
>            PhysicalAddress += BIT21;
>            Length -= BIT21;
> +          CBitChanged = TRUE;
>          } else {
>            //
>            // We must split up this page into 4K pages
> @@ -791,6 +801,7 @@ SetMemoryEncDec (
>          SetOrClearCBit (&PageTableEntry->Uint64, Mode);
>          PhysicalAddress += EFI_PAGE_SIZE;
>          Length -= EFI_PAGE_SIZE;
> +        CBitChanged = TRUE;
>        }
>      }
>    }
> @@ -808,6 +819,17 @@ SetMemoryEncDec (
>    //
>    CpuFlushTlb();
>  
> +  //
> +  // Notify Hypervisor on C-bit status
> +  //
> +  if (CBitChanged) {
> +    SetMemoryEncDecHypercall3 (
> +      OrigPhysicalAddress,
> +      EFI_SIZE_TO_PAGES(Size),
> +      KVM_MAP_GPA_RANGE_ENC_STAT(!Mode)
> +      );
> +  }

Maybe add a comment here that the "Mode" doesn't care whether the change
was for 4k vs 2mb vs 1gb. It is just tracking the total number of pages
changed (should you just remove those #defines for the page sizes then?)
which can be done by calculating everything at the 4k level.

Thanks,
Tom

> +
>  Done:
>    //
>    // Restore page table write protection, if any.
> 

  reply	other threads:[~2021-06-22 22:50 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-21 13:56 [PATCH v4 0/4] SEV Live Migration support for OVMF Ashish Kalra
2021-06-21 13:56 ` [PATCH v4 1/4] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls Ashish Kalra
2021-06-22 19:47   ` Brijesh Singh
2021-06-22 19:58     ` Brijesh Singh
2021-06-22 22:47   ` Lendacky, Thomas
2021-06-22 23:20     ` Ashish Kalra
2021-06-22 23:38       ` Brijesh Singh
2021-06-23  1:47     ` Ashish Kalra
2021-06-23 15:02       ` Ashish Kalra
2021-06-21 13:57 ` [PATCH v4 2/4] OvmfPkg/BaseMemEncryptLib: Support to issue unencrypted hypercall Ashish Kalra
2021-06-22 22:50   ` Lendacky, Thomas [this message]
2021-06-21 13:57 ` [PATCH v4 3/4] OvmfPkg/PlatformPei: Mark SEC GHCB page as unencrypted via hypercall Ashish Kalra
2021-06-22 20:35   ` Brijesh Singh
2021-06-21 13:57 ` [PATCH v4 4/4] OvmfPkg/PlatformDxe: Add support for SEV live migration Ashish Kalra
2021-06-22 23:06   ` Lendacky, Thomas
2021-06-24 16:29     ` Ashish Kalra
2021-06-22 17:20 ` [PATCH v4 0/4] SEV Live Migration support for OVMF Laszlo Ersek
2021-06-22 17:45   ` Brijesh Singh
2021-06-22 17:46   ` Ashish Kalra
2021-06-23 13:18     ` [edk2-devel] " Dov Murik
2021-06-23 16:42     ` Laszlo Ersek
2021-06-23 16:49       ` Laszlo Ersek
2021-06-23 17:03         ` Ashish Kalra
2021-06-30  9:11         ` Ashish Kalra
2021-06-30 16:25           ` [edk2-devel] " Laszlo Ersek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6fa3515f-81b4-ce03-f3c0-2358273b5dba@amd.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox