]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c
UefiCpuPkg/Include/Library: Add Register CPU Features Library
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / SmmProfile.c
index 8ecc5415a341fe90b9a49b2dab10805283a5763d..1b84e2c5f3d0db1037886a83d383db90cdc15203 100644 (file)
@@ -2,6 +2,8 @@
 Enable SMM profile.\r
 \r
 Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
+\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -29,11 +31,6 @@ UINTN                     mSmmProfileSize;
 //\r
 UINTN                     mMsrDsAreaSize   = SMM_PROFILE_DTS_SIZE;\r
 \r
-//\r
-// The flag indicates if execute-disable is supported by processor.\r
-//\r
-BOOLEAN                   mXdSupported     = FALSE;\r
-\r
 //\r
 // The flag indicates if execute-disable is enabled on processor.\r
 //\r
@@ -42,7 +39,7 @@ BOOLEAN                   mXdEnabled       = FALSE;
 //\r
 // The flag indicates if BTS is supported by processor.\r
 //\r
-BOOLEAN                   mBtsSupported     = FALSE;\r
+BOOLEAN                   mBtsSupported     = TRUE;\r
 \r
 //\r
 // The flag indicates if SMM profile starts to record data.\r
@@ -144,7 +141,7 @@ GetCpuIndex (
 \r
   ApicId = GetApicId ();\r
 \r
-  for (Index = 0; Index < PcdGet32 (PcdCpuMaxLogicalProcessorNumber); Index++) {\r
+  for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
     if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ApicId) {\r
       return Index;\r
     }\r
@@ -518,7 +515,7 @@ InitPaging (
         //\r
         continue;\r
       }\r
-      Pde = (UINT64 *)(UINTN)(Pml4[Level1] & PHYSICAL_ADDRESS_MASK);\r
+      Pde = (UINT64 *)(UINTN)(Pml4[Level1] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
     } else {\r
       Pde = (UINT64*)(UINTN)mSmmProfileCr3;\r
     }\r
@@ -529,7 +526,13 @@ InitPaging (
         //\r
         continue;\r
       }\r
-      Pte = (UINT64 *)(UINTN)(*Pde & PHYSICAL_ADDRESS_MASK);\r
+      if ((*Pde & IA32_PG_PS) != 0) {\r
+        //\r
+        // This is 1G entry, skip it\r
+        //\r
+        continue;\r
+      }\r
+      Pte = (UINT64 *)(UINTN)(*Pde & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
       if (Pte == 0) {\r
         continue;\r
       }\r
@@ -556,9 +559,9 @@ InitPaging (
 \r
           // Split it\r
           for (Level4 = 0; Level4 < SIZE_4KB / sizeof(*Pt); Level4++) {\r
-            Pt[Level4] = Address + ((Level4 << 12) | PAGE_ATTRIBUTE_BITS);\r
+            Pt[Level4] = Address + ((Level4 << 12) | mAddressEncMask | PAGE_ATTRIBUTE_BITS);\r
           } // end for PT\r
-          *Pte = (UINTN)Pt | PAGE_ATTRIBUTE_BITS;\r
+          *Pte = (UINT64)(UINTN)Pt | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
         } // end if IsAddressSplit\r
       } // end for PTE\r
     } // end for PDE\r
@@ -576,7 +579,7 @@ InitPaging (
         //\r
         continue;\r
       }\r
-      Pde = (UINT64 *)(UINTN)(Pml4[Level1] & PHYSICAL_ADDRESS_MASK);\r
+      Pde = (UINT64 *)(UINTN)(Pml4[Level1] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
     } else {\r
       Pde = (UINT64*)(UINTN)mSmmProfileCr3;\r
     }\r
@@ -587,7 +590,16 @@ InitPaging (
         //\r
         continue;\r
       }\r
-      Pte = (UINT64 *)(UINTN)(*Pde & PHYSICAL_ADDRESS_MASK);\r
+      if ((*Pde & IA32_PG_PS) != 0) {\r
+        //\r
+        // This is 1G entry, set NX bit and skip it\r
+        //\r
+        if (mXdSupported) {\r
+          *Pde = *Pde | IA32_PG_NX;\r
+        }\r
+        continue;\r
+      }\r
+      Pte = (UINT64 *)(UINTN)(*Pde & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
       if (Pte == 0) {\r
         continue;\r
       }\r
@@ -614,7 +626,7 @@ InitPaging (
           }\r
         } else {\r
           // 4KB page\r
-          Pt = (UINT64 *)(UINTN)(*Pte & PHYSICAL_ADDRESS_MASK);\r
+          Pt = (UINT64 *)(UINTN)(*Pte & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
           if (Pt == 0) {\r
             continue;\r
           }\r
@@ -815,13 +827,13 @@ InitSmmProfileInternal (
   UINTN                      MsrDsAreaSizePerCpu;\r
   UINTN                      TotalSize;\r
 \r
-  mPFEntryCount = (UINTN *)AllocateZeroPool (sizeof (UINTN) * PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+  mPFEntryCount = (UINTN *)AllocateZeroPool (sizeof (UINTN) * mMaxNumberOfCpus);\r
   ASSERT (mPFEntryCount != NULL);\r
   mLastPFEntryValue = (UINT64  (*)[MAX_PF_ENTRY_COUNT])AllocateZeroPool (\r
-                                                         sizeof (mLastPFEntryValue[0]) * PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+                                                         sizeof (mLastPFEntryValue[0]) * mMaxNumberOfCpus);\r
   ASSERT (mLastPFEntryValue != NULL);\r
   mLastPFEntryPointer = (UINT64 *(*)[MAX_PF_ENTRY_COUNT])AllocateZeroPool (\r
-                                                           sizeof (mLastPFEntryPointer[0]) * PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+                                                           sizeof (mLastPFEntryPointer[0]) * mMaxNumberOfCpus);\r
   ASSERT (mLastPFEntryPointer != NULL);\r
 \r
   //\r
@@ -862,17 +874,17 @@ InitSmmProfileInternal (
   mSmmProfileBase->NumCpus        = gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;\r
 \r
   if (mBtsSupported) {\r
-    mMsrDsArea = (MSR_DS_AREA_STRUCT **)AllocateZeroPool (sizeof (MSR_DS_AREA_STRUCT *) * PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+    mMsrDsArea = (MSR_DS_AREA_STRUCT **)AllocateZeroPool (sizeof (MSR_DS_AREA_STRUCT *) * mMaxNumberOfCpus);\r
     ASSERT (mMsrDsArea != NULL);\r
-    mMsrBTSRecord = (BRANCH_TRACE_RECORD **)AllocateZeroPool (sizeof (BRANCH_TRACE_RECORD *) * PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+    mMsrBTSRecord = (BRANCH_TRACE_RECORD **)AllocateZeroPool (sizeof (BRANCH_TRACE_RECORD *) * mMaxNumberOfCpus);\r
     ASSERT (mMsrBTSRecord != NULL);\r
-    mMsrPEBSRecord = (PEBS_RECORD **)AllocateZeroPool (sizeof (PEBS_RECORD *) * PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+    mMsrPEBSRecord = (PEBS_RECORD **)AllocateZeroPool (sizeof (PEBS_RECORD *) * mMaxNumberOfCpus);\r
     ASSERT (mMsrPEBSRecord != NULL);\r
 \r
     mMsrDsAreaBase  = (MSR_DS_AREA_STRUCT *)((UINTN)Base + mSmmProfileSize);\r
-    MsrDsAreaSizePerCpu = mMsrDsAreaSize / PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
+    MsrDsAreaSizePerCpu = mMsrDsAreaSize / mMaxNumberOfCpus;\r
     mBTSRecordNumber    = (MsrDsAreaSizePerCpu - sizeof(PEBS_RECORD) * PEBS_RECORD_NUMBER - sizeof(MSR_DS_AREA_STRUCT)) / sizeof(BRANCH_TRACE_RECORD);\r
-    for (Index = 0; Index < PcdGet32 (PcdCpuMaxLogicalProcessorNumber); Index++) {\r
+    for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
       mMsrDsArea[Index]     = (MSR_DS_AREA_STRUCT *)((UINTN)mMsrDsAreaBase + MsrDsAreaSizePerCpu * Index);\r
       mMsrBTSRecord[Index]  = (BRANCH_TRACE_RECORD *)((UINTN)mMsrDsArea[Index] + sizeof(MSR_DS_AREA_STRUCT));\r
       mMsrPEBSRecord[Index] = (PEBS_RECORD *)((UINTN)mMsrDsArea[Index] + MsrDsAreaSizePerCpu - sizeof(PEBS_RECORD) * PEBS_RECORD_NUMBER);\r
@@ -925,17 +937,15 @@ InitSmmProfileInternal (
 /**\r
   Check if XD feature is supported by a processor.\r
 \r
-  @param[in,out] Buffer  The pointer to private data buffer.\r
-\r
 **/\r
 VOID\r
-EFIAPI\r
 CheckFeatureSupported (\r
-  IN OUT VOID   *Buffer\r
+  VOID\r
   )\r
 {\r
-  UINT32                 RegEax;\r
-  UINT32                 RegEdx;\r
+  UINT32                         RegEax;\r
+  UINT32                         RegEdx;\r
+  MSR_IA32_MISC_ENABLE_REGISTER  MiscEnableMsr;\r
 \r
   if (mXdSupported) {\r
     AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL);\r
@@ -966,9 +976,10 @@ CheckFeatureSupported (
       //    BTINT bits in the MSR_DEBUGCTLA MSR.\r
       // 2. The IA32_DS_AREA MSR can be programmed to point to the DS save area.\r
       //\r
-      if (AsmMsrBitFieldRead64 (MSR_IA32_MISC_ENABLE, 11, 11) == 1) {\r
+      MiscEnableMsr.Uint64 = AsmReadMsr64 (MSR_IA32_MISC_ENABLE);\r
+      if (MiscEnableMsr.Bits.BTS == 1) {\r
         //\r
-        // BTS facilities is not supported if MSR_IA32_MISC_ENABLE BIT11 is set.\r
+        // BTS facilities is not supported if MSR_IA32_MISC_ENABLE.BTS bit is set.\r
         //\r
         mBtsSupported = FALSE;\r
       }\r
@@ -976,67 +987,6 @@ CheckFeatureSupported (
   }\r
 }\r
 \r
-/**\r
-  Check if XD and BTS features are supported by all processors.\r
-\r
-**/\r
-VOID\r
-CheckProcessorFeature (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                        Status;\r
-  EFI_MP_SERVICES_PROTOCOL          *MpServices;\r
-\r
-  Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpServices);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // First detect if XD and BTS are supported\r
-  //\r
-  mXdSupported  = TRUE;\r
-  mBtsSupported = TRUE;\r
-\r
-  //\r
-  // Check if XD and BTS are supported on all processors.\r
-  //\r
-  CheckFeatureSupported (NULL);\r
-\r
-  //\r
-  //Check on other processors if BSP supports this\r
-  //\r
-  if (mXdSupported || mBtsSupported) {\r
-    MpServices->StartupAllAPs (\r
-                  MpServices,\r
-                  CheckFeatureSupported,\r
-                  TRUE,\r
-                  NULL,\r
-                  0,\r
-                  NULL,\r
-                  NULL\r
-                  );\r
-  }\r
-}\r
-\r
-/**\r
-  Enable XD feature.\r
-\r
-**/\r
-VOID\r
-ActivateXd (\r
-  VOID\r
-  )\r
-{\r
-  UINT64           MsrRegisters;\r
-\r
-  MsrRegisters = AsmReadMsr64 (MSR_EFER);\r
-  if ((MsrRegisters & MSR_EFER_XD) != 0) {\r
-    return ;\r
-  }\r
-  MsrRegisters |= MSR_EFER_XD;\r
-  AsmWriteMsr64 (MSR_EFER, MsrRegisters);\r
-}\r
-\r
 /**\r
   Enable single step.\r
 \r
@@ -1427,5 +1377,8 @@ InitIdtr (
   VOID\r
   )\r
 {\r
-  SmmRegisterExceptionHandler (&mSmmCpuService, EXCEPT_IA32_DEBUG, DebugExceptionHandler);\r
+  EFI_STATUS                        Status;\r
+\r
+  Status = SmmRegisterExceptionHandler (&mSmmCpuService, EXCEPT_IA32_DEBUG, DebugExceptionHandler);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r