Re: [edk2-devel] [PATCH v2] OvmfPkg: Don't make APIC MMIO accesses with encryption bit set

2024-04-26 Thread Roth, Michael via groups.io
On Fri, Apr 26, 2024 at 05:25:15PM +0200, Ard Biesheuvel wrote:
> On Fri, 26 Apr 2024 at 16:16, Roth, Michael via groups.io
>  wrote:
> >
> > On Fri, Apr 26, 2024 at 08:51:20AM -0500, Roth, Michael via groups.io wrote:
> > > For the most part, OVMF will clear the encryption bit for MMIO regions,
> > > but there is currently one known exception during SEC when the APIC
> > > base address is accessed via MMIO with the encryption bit set for
> > > SEV-ES/SEV-SNP guests. In the case of SEV-SNP, this requires special
> > > handling on the hypervisor side which may not be available in the
> > > future[1], so make the necessary changes in the SEC-configured page
> > > table to clear the encryption bit for 4K region containing the APIC
> > > base address.
> > >
> > > Since CpuPageTableLib is used to handle the splitting, some additional
> > > care must be taken to clear the C-bit in all non-leaf PTEs since the
> > > library expects that to be the case. Add handling for that when setting
> > > up the SEC page table.
> >
> > Tom just noticed another spot where a non-leaf C-bit needs to be cleared
> > (the one mapping the GHCB page). It doesn't affect patch functionality
> > but should be included for completeness of this change, so will send a
> > quick v3 with this addressed.
> >
> > -Mike
> >
> 
> While at it, could you please fix your email setup and use 8bit
> transfer encoding instead of quoted printable? Also, please put me on
> cc so I don't have to apply the garbled groups.io copy.

I'm not sure why git send-email seems to be defaulting to
"quoted-printable", but I've set sendemail.transferencoding=8bit and
that seems to have resolved things.

Thanks,

Mike


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118350): https://edk2.groups.io/g/devel/message/118350
Mute This Topic: https://groups.io/mt/105750506/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v2] OvmfPkg: Don't make APIC MMIO accesses with encryption bit set

2024-04-26 Thread Ard Biesheuvel
On Fri, 26 Apr 2024 at 16:16, Roth, Michael via groups.io
 wrote:
>
> On Fri, Apr 26, 2024 at 08:51:20AM -0500, Roth, Michael via groups.io wrote:
> > For the most part, OVMF will clear the encryption bit for MMIO regions,
> > but there is currently one known exception during SEC when the APIC
> > base address is accessed via MMIO with the encryption bit set for
> > SEV-ES/SEV-SNP guests. In the case of SEV-SNP, this requires special
> > handling on the hypervisor side which may not be available in the
> > future[1], so make the necessary changes in the SEC-configured page
> > table to clear the encryption bit for 4K region containing the APIC
> > base address.
> >
> > Since CpuPageTableLib is used to handle the splitting, some additional
> > care must be taken to clear the C-bit in all non-leaf PTEs since the
> > library expects that to be the case. Add handling for that when setting
> > up the SEC page table.
>
> Tom just noticed another spot where a non-leaf C-bit needs to be cleared
> (the one mapping the GHCB page). It doesn't affect patch functionality
> but should be included for completeness of this change, so will send a
> quick v3 with this addressed.
>
> -Mike
>

While at it, could you please fix your email setup and use 8bit
transfer encoding instead of quoted printable? Also, please put me on
cc so I don't have to apply the garbled groups.io copy.


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118343): https://edk2.groups.io/g/devel/message/118343
Mute This Topic: https://groups.io/mt/105750506/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v2] OvmfPkg: Don't make APIC MMIO accesses with encryption bit set

2024-04-26 Thread Roth, Michael via groups.io
On Fri, Apr 26, 2024 at 08:51:20AM -0500, Roth, Michael via groups.io wrote:
> For the most part, OVMF will clear the encryption bit for MMIO regions,
> but there is currently one known exception during SEC when the APIC
> base address is accessed via MMIO with the encryption bit set for
> SEV-ES/SEV-SNP guests. In the case of SEV-SNP, this requires special
> handling on the hypervisor side which may not be available in the
> future[1], so make the necessary changes in the SEC-configured page
> table to clear the encryption bit for 4K region containing the APIC
> base address.
> 
> Since CpuPageTableLib is used to handle the splitting, some additional
> care must be taken to clear the C-bit in all non-leaf PTEs since the
> library expects that to be the case. Add handling for that when setting
> up the SEC page table.

Tom just noticed another spot where a non-leaf C-bit needs to be cleared
(the one mapping the GHCB page). It doesn't affect patch functionality
but should be included for completeness of this change, so will send a
quick v3 with this addressed.

-Mike

> 
> While here, drop special handling for the APIC base address in the
> SEV-ES/SNP #VC handler.
> 
> [1] https://lore.kernel.org/lkml/20240208002420.34mvemnzrwwsa...@amd.com/#t
> 
> Suggested-by: Tom Lendacky 
> Cc: Erdem Aktas 
> Cc: Jiewen Yao 
> Cc: Min Xu 
> Cc: Tom Lendacky 
> Cc: Jianyong Wu 
> Cc: Anatol Belski 
> Signed-off-by: Michael Roth 
> ---
> changes since v1:
>   - use CpuPageTableLib to handle splitting (Gerd, Tom)
> 
>  OvmfPkg/AmdSev/AmdSevX64.fdf|  5 +-
>  OvmfPkg/Bhyve/BhyveX64.dsc  |  1 +
>  OvmfPkg/CloudHv/CloudHvX64.fdf  |  5 +-
>  OvmfPkg/Library/CcExitLib/CcExitVcHandler.c | 12 +
>  OvmfPkg/Microvm/MicrovmX64.fdf  |  3 ++
>  OvmfPkg/OvmfPkg.dec |  5 ++
>  OvmfPkg/OvmfPkgX64.fdf  |  5 +-
>  OvmfPkg/ResetVector/Ia32/PageTables64.asm   | 20 +++
>  OvmfPkg/Sec/AmdSev.c| 58 +
>  OvmfPkg/Sec/AmdSev.h| 14 +
>  OvmfPkg/Sec/SecMain.c   |  1 +
>  OvmfPkg/Sec/SecMain.inf |  3 ++
>  12 files changed, 108 insertions(+), 24 deletions(-)
> 
> diff --git a/OvmfPkg/AmdSev/AmdSevX64.fdf b/OvmfPkg/AmdSev/AmdSevX64.fdf
> index d49555c6c8..595945181c 100644
> --- a/OvmfPkg/AmdSev/AmdSevX64.fdf
> +++ b/OvmfPkg/AmdSev/AmdSevX64.fdf
> @@ -77,7 +77,10 @@ 
> gUefiOvmfPkgTokenSpaceGuid.PcdSevLaunchSecretBase|gUefiOvmfPkgTokenSpaceGuid.Pcd
>  0x010C00|0x000400
> 
>  
> gUefiOvmfPkgTokenSpaceGuid.PcdQemuHashTableBase|gUefiOvmfPkgTokenSpaceGuid.PcdQemuHashTableSize
> 
>  
> 
> -0x011000|0x00F000
> 
> +0x011000|0x001000
> 
> +gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableSize
> 
> +
> 
> +0x012000|0x00E000
> 
>  
> gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize
> 
>  
> 
>  0x02|0x0E
> 
> diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc
> index 6f305d690d..78050959f8 100644
> --- a/OvmfPkg/Bhyve/BhyveX64.dsc
> +++ b/OvmfPkg/Bhyve/BhyveX64.dsc
> @@ -174,6 +174,7 @@
>PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
> 
>DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
> 
>
> ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf
> 
> +  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
> 
>  
> 
>
> CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf
> 
>
> FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf
> 
> diff --git a/OvmfPkg/CloudHv/CloudHvX64.fdf b/OvmfPkg/CloudHv/CloudHvX64.fdf
> index eae3ada191..3e6688b103 100644
> --- a/OvmfPkg/CloudHv/CloudHvX64.fdf
> +++ b/OvmfPkg/CloudHv/CloudHvX64.fdf
> @@ -76,7 +76,10 @@ 
> gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCpuidBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCp
>  0x00F000|0x001000
> 
>  
> gUefiOvmfPkgTokenSpaceGuid.PcdXenPvhStartOfDayStructPtr|gUefiOvmfPkgTokenSpaceGuid.PcdXenPvhStartOfDayStructPtrSize
> 
>  
> 
> -0x01|0x01
> 
> +0x01|0x001000
> 
> +gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableSize
> 
> +
> 
> +0x011000|0x00F000
> 
>  
> gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize
> 
>  
> 
>  0x02|0x0E
> 
> diff --git a/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c 
> b/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
> index 549375dfed..da8f1e5db9 100644
> --- a/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
> +++ b/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
> @@ -98,7 +98,7 @@ UnsupportedExit (
>Validate that the MMIO memory access is not to encrypted memory.
> 
>  
> 
>Examine the pagetable entry for the 

[edk2-devel] [PATCH v2] OvmfPkg: Don't make APIC MMIO accesses with encryption bit set

2024-04-26 Thread Roth, Michael via groups.io
For the most part, OVMF will clear the encryption bit for MMIO regions,
but there is currently one known exception during SEC when the APIC
base address is accessed via MMIO with the encryption bit set for
SEV-ES/SEV-SNP guests. In the case of SEV-SNP, this requires special
handling on the hypervisor side which may not be available in the
future[1], so make the necessary changes in the SEC-configured page
table to clear the encryption bit for 4K region containing the APIC
base address.

Since CpuPageTableLib is used to handle the splitting, some additional
care must be taken to clear the C-bit in all non-leaf PTEs since the
library expects that to be the case. Add handling for that when setting
up the SEC page table.

While here, drop special handling for the APIC base address in the
SEV-ES/SNP #VC handler.

[1] https://lore.kernel.org/lkml/20240208002420.34mvemnzrwwsa...@amd.com/#t

Suggested-by: Tom Lendacky 
Cc: Erdem Aktas 
Cc: Jiewen Yao 
Cc: Min Xu 
Cc: Tom Lendacky 
Cc: Jianyong Wu 
Cc: Anatol Belski 
Signed-off-by: Michael Roth 
---
changes since v1:
  - use CpuPageTableLib to handle splitting (Gerd, Tom)

 OvmfPkg/AmdSev/AmdSevX64.fdf|  5 +-
 OvmfPkg/Bhyve/BhyveX64.dsc  |  1 +
 OvmfPkg/CloudHv/CloudHvX64.fdf  |  5 +-
 OvmfPkg/Library/CcExitLib/CcExitVcHandler.c | 12 +
 OvmfPkg/Microvm/MicrovmX64.fdf  |  3 ++
 OvmfPkg/OvmfPkg.dec |  5 ++
 OvmfPkg/OvmfPkgX64.fdf  |  5 +-
 OvmfPkg/ResetVector/Ia32/PageTables64.asm   | 20 +++
 OvmfPkg/Sec/AmdSev.c| 58 +
 OvmfPkg/Sec/AmdSev.h| 14 +
 OvmfPkg/Sec/SecMain.c   |  1 +
 OvmfPkg/Sec/SecMain.inf |  3 ++
 12 files changed, 108 insertions(+), 24 deletions(-)

diff --git a/OvmfPkg/AmdSev/AmdSevX64.fdf b/OvmfPkg/AmdSev/AmdSevX64.fdf
index d49555c6c8..595945181c 100644
--- a/OvmfPkg/AmdSev/AmdSevX64.fdf
+++ b/OvmfPkg/AmdSev/AmdSevX64.fdf
@@ -77,7 +77,10 @@ 
gUefiOvmfPkgTokenSpaceGuid.PcdSevLaunchSecretBase|gUefiOvmfPkgTokenSpaceGuid.Pcd
 0x010C00|0x000400

 
gUefiOvmfPkgTokenSpaceGuid.PcdQemuHashTableBase|gUefiOvmfPkgTokenSpaceGuid.PcdQemuHashTableSize

 

-0x011000|0x00F000

+0x011000|0x001000

+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableSize

+

+0x012000|0x00E000

 
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize

 

 0x02|0x0E

diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc
index 6f305d690d..78050959f8 100644
--- a/OvmfPkg/Bhyve/BhyveX64.dsc
+++ b/OvmfPkg/Bhyve/BhyveX64.dsc
@@ -174,6 +174,7 @@
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf

   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf

   
ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf

+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf

 

   
CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf

   
FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf

diff --git a/OvmfPkg/CloudHv/CloudHvX64.fdf b/OvmfPkg/CloudHv/CloudHvX64.fdf
index eae3ada191..3e6688b103 100644
--- a/OvmfPkg/CloudHv/CloudHvX64.fdf
+++ b/OvmfPkg/CloudHv/CloudHvX64.fdf
@@ -76,7 +76,10 @@ 
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCpuidBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCp
 0x00F000|0x001000

 
gUefiOvmfPkgTokenSpaceGuid.PcdXenPvhStartOfDayStructPtr|gUefiOvmfPkgTokenSpaceGuid.PcdXenPvhStartOfDayStructPtrSize

 

-0x01|0x01

+0x01|0x001000

+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecApicPageTableSize

+

+0x011000|0x00F000

 
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize

 

 0x02|0x0E

diff --git a/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c 
b/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
index 549375dfed..da8f1e5db9 100644
--- a/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
+++ b/OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
@@ -98,7 +98,7 @@ UnsupportedExit (
   Validate that the MMIO memory access is not to encrypted memory.

 

   Examine the pagetable entry for the memory specified. MMIO should not be

-  performed against encrypted memory. MMIO to the APIC page is always allowed.

+  performed against encrypted memory.

 

   @param[in] Ghcb   Pointer to the Guest-Hypervisor Communication Block

   @param[in] MemoryAddress  Memory address to validate

@@ -118,16 +118,6 @@ ValidateMmioMemory (
 {

   MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE  State;

   GHCB_EVENT_INJECTION GpEvent;

-  UINTNAddress;

-

-  //

-  // Allow APIC accesses (which will have the encryption bit set during

-  // SEC and PEI phases).

-