Re: [edk2-devel] [PATCH 3/6] UefiCpuPkg/MtrrLib: Substract TME-MK KEY_ID_BITS from CPU max PA

2023-03-29 Thread Michael D Kinney
Reviewed-by: Michael D Kinney 

> -Original Message-
> From: Ni, Ray 
> Sent: Tuesday, March 21, 2023 4:57 PM
> To: devel@edk2.groups.io
> Cc: Dong, Eric ; Kumar, Rahul R 
> ; Gerd Hoffmann ; Kinney,
> Michael D ; Anadani, Ahmad 
> 
> Subject: [PATCH 3/6] UefiCpuPkg/MtrrLib: Substract TME-MK KEY_ID_BITS from 
> CPU max PA
> 
> CPUID enumeration of MAX_PA is unaffected by TME-MK activation and
> will continue to report the maximum physical address bits available
> for software to use, irrespective of the number of KeyID bits.
> 
> So, we need to check if TME is enabled and adjust the PA size
> accordingly.
> 
> Signed-off-by: Ray Ni 
> Cc: Eric Dong 
> Cc: Rahul Kumar 
> Cc: Gerd Hoffmann 
> Cc: Michael D Kinney 
> Cc: Ahmad Anadani 
> ---
>  UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 24 ++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c 
> b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> index e5c862c83d..a66357e305 100644
> --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> @@ -756,8 +756,11 @@ MtrrLibInitializeMtrrMask (
>OUT UINT64  *MtrrValidAddressMask
> 
>)
> 
>  {
> 
> -  UINT32  MaxExtendedFunction;
> 
> -  CPUID_VIR_PHY_ADDRESS_SIZE_EAX  VirPhyAddressSize;
> 
> +  UINT32   MaxExtendedFunction;
> 
> +  CPUID_VIR_PHY_ADDRESS_SIZE_EAX   VirPhyAddressSize;
> 
> +  UINT32   MaxFunction;
> 
> +  CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX  ExtendedFeatureFlagsEcx;
> 
> +  MSR_IA32_TME_ACTIVATE_REGISTER   TmeActivate;
> 
> 
> 
>AsmCpuid (CPUID_EXTENDED_FUNCTION, , NULL, NULL, NULL);
> 
> 
> 
> @@ -767,6 +770,23 @@ MtrrLibInitializeMtrrMask (
>  VirPhyAddressSize.Bits.PhysicalAddressBits = 36;
> 
>}
> 
> 
> 
> +  //
> 
> +  // CPUID enumeration of MAX_PA is unaffected by TME-MK activation and will 
> continue
> 
> +  // to report the maximum physical address bits available for software to 
> use,
> 
> +  // irrespective of the number of KeyID bits.
> 
> +  // So, we need to check if TME is enabled and adjust the PA size 
> accordingly.
> 
> +  //
> 
> +  AsmCpuid (CPUID_SIGNATURE, , NULL, NULL, NULL);
> 
> +  if (MaxFunction >= CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS) {
> 
> +AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS, 0, NULL, NULL, 
> , NULL);
> 
> +if (ExtendedFeatureFlagsEcx.Bits.TME_EN == 1) {
> 
> +  TmeActivate.Uint64 = AsmReadMsr64 (MSR_IA32_TME_ACTIVATE);
> 
> +  if (TmeActivate.Bits.TmeEnable == 1) {
> 
> +VirPhyAddressSize.Bits.PhysicalAddressBits -= 
> TmeActivate.Bits.MkTmeKeyidBits;
> 
> +  }
> 
> +}
> 
> +  }
> 
> +
> 
>*MtrrValidBitsMask= LShiftU64 (1, 
> VirPhyAddressSize.Bits.PhysicalAddressBits) - 1;
> 
>*MtrrValidAddressMask = *MtrrValidBitsMask & 0xf000ULL;
> 
>  }
> 
> --
> 2.39.1.windows.1



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




[edk2-devel] [PATCH 3/6] UefiCpuPkg/MtrrLib: Substract TME-MK KEY_ID_BITS from CPU max PA

2023-03-21 Thread Ni, Ray
CPUID enumeration of MAX_PA is unaffected by TME-MK activation and
will continue to report the maximum physical address bits available
for software to use, irrespective of the number of KeyID bits.

So, we need to check if TME is enabled and adjust the PA size
accordingly.

Signed-off-by: Ray Ni 
Cc: Eric Dong 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Michael D Kinney 
Cc: Ahmad Anadani 
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 24 ++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c 
b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index e5c862c83d..a66357e305 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -756,8 +756,11 @@ MtrrLibInitializeMtrrMask (
   OUT UINT64  *MtrrValidAddressMask
   )
 {
-  UINT32  MaxExtendedFunction;
-  CPUID_VIR_PHY_ADDRESS_SIZE_EAX  VirPhyAddressSize;
+  UINT32   MaxExtendedFunction;
+  CPUID_VIR_PHY_ADDRESS_SIZE_EAX   VirPhyAddressSize;
+  UINT32   MaxFunction;
+  CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX  ExtendedFeatureFlagsEcx;
+  MSR_IA32_TME_ACTIVATE_REGISTER   TmeActivate;
 
   AsmCpuid (CPUID_EXTENDED_FUNCTION, , NULL, NULL, NULL);
 
@@ -767,6 +770,23 @@ MtrrLibInitializeMtrrMask (
 VirPhyAddressSize.Bits.PhysicalAddressBits = 36;
   }
 
+  //
+  // CPUID enumeration of MAX_PA is unaffected by TME-MK activation and will 
continue
+  // to report the maximum physical address bits available for software to use,
+  // irrespective of the number of KeyID bits.
+  // So, we need to check if TME is enabled and adjust the PA size accordingly.
+  //
+  AsmCpuid (CPUID_SIGNATURE, , NULL, NULL, NULL);
+  if (MaxFunction >= CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS) {
+AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS, 0, NULL, NULL, 
, NULL);
+if (ExtendedFeatureFlagsEcx.Bits.TME_EN == 1) {
+  TmeActivate.Uint64 = AsmReadMsr64 (MSR_IA32_TME_ACTIVATE);
+  if (TmeActivate.Bits.TmeEnable == 1) {
+VirPhyAddressSize.Bits.PhysicalAddressBits -= 
TmeActivate.Bits.MkTmeKeyidBits;
+  }
+}
+  }
+
   *MtrrValidBitsMask= LShiftU64 (1, 
VirPhyAddressSize.Bits.PhysicalAddressBits) - 1;
   *MtrrValidAddressMask = *MtrrValidBitsMask & 0xf000ULL;
 }
-- 
2.39.1.windows.1



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