* [PATCH 0/4] Add SmmIoLib
@ 2017-04-21 15:49 Jiewen Yao
2017-04-21 15:49 ` [PATCH 1/4] MdePkg/SmmIoLib: Add header file Jiewen Yao
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Jiewen Yao @ 2017-04-21 15:49 UTC (permalink / raw)
To: edk2-devel
This patch series add SmmIoLib.
It is the first part of bugzillar 491.
https://bugzilla.tianocore.org/show_bug.cgi?id=491
Move generic function - OpalIsValidMmioSpace from OPAL
driver to library.
This SmmIoLib is similar to SmmMemLib.
The second part of bugzillar 491 is to update consumer
SecurityPkg/Tcg/Opal/OpalPasswordSmm. It will be
handled in future patch series.
Jiewen Yao (4):
MdePkg/SmmIoLib: Add header file.
MdePkg/SmmIoLib: Add sample instance.
MdePkg/dec: Add SmmIoLib.
MdePkg/dsc: add SmmIoLib
MdePkg/Include/Library/SmmIoLib.h | 42 +++
MdePkg/Library/SmmIoLib/SmmIoLib.c | 331 ++++++++++++++++++++
MdePkg/Library/SmmIoLib/SmmIoLib.inf | 53 ++++
MdePkg/Library/SmmIoLib/SmmIoLib.uni | 23 ++
MdePkg/MdePkg.dec | 4 +
MdePkg/MdePkg.dsc | 1 +
6 files changed, 454 insertions(+)
create mode 100644 MdePkg/Include/Library/SmmIoLib.h
create mode 100644 MdePkg/Library/SmmIoLib/SmmIoLib.c
create mode 100644 MdePkg/Library/SmmIoLib/SmmIoLib.inf
create mode 100644 MdePkg/Library/SmmIoLib/SmmIoLib.uni
--
2.7.4.windows.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/4] MdePkg/SmmIoLib: Add header file.
2017-04-21 15:49 [PATCH 0/4] Add SmmIoLib Jiewen Yao
@ 2017-04-21 15:49 ` Jiewen Yao
2017-04-21 15:49 ` [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance Jiewen Yao
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Jiewen Yao @ 2017-04-21 15:49 UTC (permalink / raw)
To: edk2-devel; +Cc: Jeff Fan, Liming Gao
This SmmIoLib is used to check if an IO resource
is valid in SMM.
Cc: Jeff Fan <jeff.fan@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
---
MdePkg/Include/Library/SmmIoLib.h | 42 ++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/MdePkg/Include/Library/SmmIoLib.h b/MdePkg/Include/Library/SmmIoLib.h
new file mode 100644
index 0000000..7820f1e
--- /dev/null
+++ b/MdePkg/Include/Library/SmmIoLib.h
@@ -0,0 +1,42 @@
+/** @file
+ Provides services for SMM IO Operation.
+
+ The SMM IO Library provides function for checking if IO resource is accessible inside of SMM.
+
+ Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+ This program and the accompanying materials
+ are licensed and made available under the terms and conditions of the BSD License
+ which accompanies this distribution. The full text of the license may be found at
+ http://opensource.org/licenses/bsd-license.php
+
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _SMM_IO_LIB_H_
+#define _SMM_IO_LIB_H_
+
+/**
+ This function check if the MMIO resource is valid per processor architecture and
+ valid per platform design.
+
+ @param BaseAddress The MMIO start address to be checked.
+ @param Length The MMIO length to be checked.
+ @param Owner A GUID representing the owner of the resource.
+ This GUID may be used by producer to correlate the device ownership of the resource.
+ NULL means no specific owner.
+
+ @retval TRUE This MMIO resource is valid per processor architecture and valid per platform design.
+ @retval FALSE This MMIO resource is not valid per processor architecture or valid per platform design.
+**/
+BOOLEAN
+EFIAPI
+SmmIsMmioValid (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length,
+ IN EFI_GUID *Owner OPTIONAL
+ );
+
+#endif
+
--
2.7.4.windows.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance.
2017-04-21 15:49 [PATCH 0/4] Add SmmIoLib Jiewen Yao
2017-04-21 15:49 ` [PATCH 1/4] MdePkg/SmmIoLib: Add header file Jiewen Yao
@ 2017-04-21 15:49 ` Jiewen Yao
2017-04-24 2:02 ` Fan, Jeff
2017-04-21 15:49 ` [PATCH 3/4] MdePkg/dec: Add SmmIoLib Jiewen Yao
2017-04-21 15:49 ` [PATCH 4/4] MdePkg/dsc: add SmmIoLib Jiewen Yao
3 siblings, 1 reply; 7+ messages in thread
From: Jiewen Yao @ 2017-04-21 15:49 UTC (permalink / raw)
To: edk2-devel; +Cc: Jeff Fan, Liming Gao
The sample instance check if IO resource is valid
one defined in GCD.
A platform may choose add more check to exclude some
other IO resource.
Cc: Jeff Fan <jeff.fan@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
---
MdePkg/Library/SmmIoLib/SmmIoLib.c | 331 ++++++++++++++++++++
MdePkg/Library/SmmIoLib/SmmIoLib.inf | 53 ++++
MdePkg/Library/SmmIoLib/SmmIoLib.uni | 23 ++
3 files changed, 407 insertions(+)
diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.c b/MdePkg/Library/SmmIoLib/SmmIoLib.c
new file mode 100644
index 0000000..995adac
--- /dev/null
+++ b/MdePkg/Library/SmmIoLib/SmmIoLib.c
@@ -0,0 +1,331 @@
+/** @file
+ Instance of SMM IO check library.
+
+ SMM IO check library library implementation. This library consumes GCD to collect all valid
+ IO space defined by a platform.
+ A platform may have its own SmmIoLib instance to exclude more IO space.
+
+ Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+ This program and the accompanying materials
+ are licensed and made available under the terms and conditions of the BSD License
+ which accompanies this distribution. The full text of the license may be found at
+ http://opensource.org/licenses/bsd-license.php
+
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+#include <PiSmm.h>
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/SmmServicesTableLib.h>
+#include <Library/HobLib.h>
+#include <Library/DxeServicesTableLib.h>
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmEndOfDxe.h>
+
+EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace = NULL;
+UINTN mSmmIoLibGcdMemNumberOfDesc = 0;
+
+EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress = 0;
+
+VOID *mSmmIoLibRegistrationEndOfDxe;
+VOID *mSmmIoLibRegistrationReadyToLock;
+
+BOOLEAN mSmmIoLibReadyToLock = FALSE;
+
+/**
+ Calculate and save the maximum support address.
+
+**/
+VOID
+SmmIoLibInternalCalculateMaximumSupportAddress (
+ VOID
+ )
+{
+ VOID *Hob;
+ UINT32 RegEax;
+ UINT8 MemPhysicalAddressBits;
+
+ //
+ // Get physical address bits supported.
+ //
+ Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
+ if (Hob != NULL) {
+ MemPhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
+ } else {
+ AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
+ if (RegEax >= 0x80000008) {
+ AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
+ MemPhysicalAddressBits = (UINT8) RegEax;
+ } else {
+ MemPhysicalAddressBits = 36;
+ }
+ }
+ //
+ // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
+ //
+ ASSERT (MemPhysicalAddressBits <= 52);
+ if (MemPhysicalAddressBits > 48) {
+ MemPhysicalAddressBits = 48;
+ }
+
+ //
+ // Save the maximum support address in one global variable
+ //
+ mSmmIoLibInternalMaximumSupportMemAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, MemPhysicalAddressBits) - 1);
+ DEBUG ((DEBUG_INFO, "mSmmIoLibInternalMaximumSupportMemAddress = 0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress));
+}
+
+/**
+ This function check if the MMIO resource is valid per processor architecture and
+ valid per platform design.
+
+ @param BaseAddress The MMIO start address to be checked.
+ @param Length The MMIO length to be checked.
+ @param Owner A GUID representing the owner of the resource.
+ This GUID may be used by producer to correlate the device ownership of the resource.
+ NULL means no specific owner.
+
+ @retval TRUE This MMIO resource is valid per processor architecture and valid per platform design.
+ @retval FALSE This MMIO resource is not valid per processor architecture or valid per platform design.
+**/
+BOOLEAN
+EFIAPI
+SmmIsMmioValid (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length,
+ IN EFI_GUID *Owner OPTIONAL
+ )
+{
+ UINTN Index;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
+ BOOLEAN InValidRegion;
+
+ //
+ // Check override.
+ // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
+ //
+ if ((Length > mSmmIoLibInternalMaximumSupportMemAddress) ||
+ (BaseAddress > mSmmIoLibInternalMaximumSupportMemAddress) ||
+ ((Length != 0) && (BaseAddress > (mSmmIoLibInternalMaximumSupportMemAddress - (Length - 1)))) ) {
+ //
+ // Overflow happen
+ //
+ DEBUG ((
+ DEBUG_ERROR,
+ "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
+ BaseAddress,
+ Length,
+ mSmmIoLibInternalMaximumSupportMemAddress
+ ));
+ return FALSE;
+ }
+
+ //
+ // Check override for valid MMIO region
+ //
+ if (mSmmIoLibReadyToLock) {
+ InValidRegion = FALSE;
+ for (Index = 0; Index < mSmmIoLibGcdMemNumberOfDesc; Index ++) {
+ Desc = &mSmmIoLibGcdMemSpace[Index];
+ if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
+ (BaseAddress >= Desc->BaseAddress) &&
+ ((BaseAddress + Length) <= (Desc->BaseAddress + Desc->Length))) {
+ InValidRegion = TRUE;
+ }
+ }
+
+ if (!InValidRegion) {
+ DEBUG ((
+ DEBUG_ERROR,
+ "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
+ BaseAddress,
+ Length
+ ));
+ ASSERT (FALSE);
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+/**
+ Merge continous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
+
+ @param[in, out] GcdMemoryMap A pointer to the buffer in which firmware places
+ the current GCD memory map.
+ @param[in, out] NumberOfDescriptors A pointer to the number of the
+ GcdMemoryMap buffer. On input, this is the number of
+ the current GCD memory map. On output,
+ it is the number of new GCD memory map after merge.
+**/
+STATIC
+VOID
+MergeGcdMmioEntry (
+ IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMap,
+ IN OUT UINTN *NumberOfDescriptors
+ )
+{
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEntry;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEnd;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *NewGcdMemoryMapEntry;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *NextGcdMemoryMapEntry;
+
+ GcdMemoryMapEntry = GcdMemoryMap;
+ NewGcdMemoryMapEntry = GcdMemoryMap;
+ GcdMemoryMapEnd = (EFI_GCD_MEMORY_SPACE_DESCRIPTOR *) ((UINT8 *) GcdMemoryMap + (*NumberOfDescriptors) * sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
+ while ((UINTN)GcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) {
+ CopyMem (NewGcdMemoryMapEntry, GcdMemoryMapEntry, sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
+ NextGcdMemoryMapEntry = GcdMemoryMapEntry + 1;
+
+ do {
+ if (((UINTN)NextGcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) &&
+ (GcdMemoryMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) && (NextGcdMemoryMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
+ ((GcdMemoryMapEntry->BaseAddress + GcdMemoryMapEntry->Length) == NextGcdMemoryMapEntry->BaseAddress)) {
+ GcdMemoryMapEntry->Length += NextGcdMemoryMapEntry->Length;
+ if (NewGcdMemoryMapEntry != GcdMemoryMapEntry) {
+ NewGcdMemoryMapEntry->Length += NextGcdMemoryMapEntry->Length;
+ }
+
+ NextGcdMemoryMapEntry = NextGcdMemoryMapEntry + 1;
+ continue;
+ } else {
+ GcdMemoryMapEntry = NextGcdMemoryMapEntry - 1;
+ break;
+ }
+ } while (TRUE);
+
+ GcdMemoryMapEntry = GcdMemoryMapEntry + 1;
+ NewGcdMemoryMapEntry = NewGcdMemoryMapEntry + 1;
+ }
+
+ *NumberOfDescriptors = ((UINTN)NewGcdMemoryMapEntry - (UINTN)GcdMemoryMap) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR);
+
+ return ;
+}
+
+/**
+ Notification for SMM EndOfDxe protocol.
+
+ @param[in] Protocol Points to the protocol's unique identifier.
+ @param[in] Interface Points to the interface instance.
+ @param[in] Handle The handle on which the interface was installed.
+
+ @retval EFI_SUCCESS Notification runs successfully.
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibInternalEndOfDxeNotify (
+ IN CONST EFI_GUID *Protocol,
+ IN VOID *Interface,
+ IN EFI_HANDLE Handle
+ )
+{
+ UINTN NumberOfDescriptors;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
+ EFI_STATUS Status;
+
+ Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
+ if (!EFI_ERROR (Status)) {
+
+ MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);
+
+ mSmmIoLibGcdMemSpace = AllocateCopyPool (NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);
+ ASSERT_EFI_ERROR (Status);
+ if (EFI_ERROR (Status)) {
+ gBS->FreePool (MemSpaceMap);
+ return Status;
+ }
+
+ mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;
+ gBS->FreePool (MemSpaceMap);
+ }
+
+ return EFI_SUCCESS;
+}
+
+/**
+ Notification for SMM ReadyToLock protocol.
+
+ @param[in] Protocol Points to the protocol's unique identifier.
+ @param[in] Interface Points to the interface instance.
+ @param[in] Handle The handle on which the interface was installed.
+
+ @retval EFI_SUCCESS Notification runs successfully.
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibInternalReadyToLockNotify (
+ IN CONST EFI_GUID *Protocol,
+ IN VOID *Interface,
+ IN EFI_HANDLE Handle
+ )
+{
+ mSmmIoLibReadyToLock = TRUE;
+ return EFI_SUCCESS;
+}
+
+/**
+ The constructor function initializes the Smm IO library
+
+ @param ImageHandle The firmware allocated handle for the EFI image.
+ @param SystemTable A pointer to the EFI System Table.
+
+ @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibConstructor (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+{
+ EFI_STATUS Status;
+
+ //
+ // Calculate and save maximum support address
+ //
+ SmmIoLibInternalCalculateMaximumSupportAddress ();
+
+ //
+ // Register EndOfDxe to get GCD resource map
+ //
+ Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);
+ ASSERT_EFI_ERROR (Status);
+
+ //
+ // Register ready to lock so that we can know when to check valid resource region
+ //
+ Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify, &mSmmIoLibRegistrationReadyToLock);
+ ASSERT_EFI_ERROR (Status);
+
+ return EFI_SUCCESS;
+}
+
+/**
+ The destructor function frees resource used in the Smm IO library
+
+ @param[in] ImageHandle The firmware allocated handle for the EFI image.
+ @param[in] SystemTable A pointer to the EFI System Table.
+
+ @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibDestructor (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+{
+ gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mSmmIoLibRegistrationEndOfDxe);
+ gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mSmmIoLibRegistrationReadyToLock);
+ return EFI_SUCCESS;
+}
diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.inf b/MdePkg/Library/SmmIoLib/SmmIoLib.inf
new file mode 100644
index 0000000..d508408
--- /dev/null
+++ b/MdePkg/Library/SmmIoLib/SmmIoLib.inf
@@ -0,0 +1,53 @@
+## @file
+# Instance of SMM IO check library.
+#
+# SMM IO check library library implementation. This library consumes GCD to collect all valid
+# IO space defined by a platform.
+# A platform may have its own SmmIoLib instance to exclude more IO space.
+#
+# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+# This program and the accompanying materials
+# are licensed and made available under the terms and conditions of the BSD License
+# which accompanies this distribution. The full text of the license may be found at
+# http://opensource.org/licenses/bsd-license.php
+#
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+ INF_VERSION = 0x00010005
+ BASE_NAME = SmmIoLib
+ MODULE_UNI_FILE = SmmIoLib.uni
+ FILE_GUID = F0F5A845-E3ED-4C6E-82D6-4ECE85DAC00F
+ MODULE_TYPE = DXE_SMM_DRIVER
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = SmmIoLib|DXE_SMM_DRIVER SMM_CORE
+ CONSTRUCTOR = SmmIoLibConstructor
+ DESTRUCTOR = SmmIoLibDestructor
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+# VALID_ARCHITECTURES = IA32 X64
+#
+
+[Sources]
+ SmmIoLib.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ SmmServicesTableLib
+ UefiBootServicesTableLib
+ DxeServicesTableLib
+ DebugLib
+ BaseMemoryLib
+ MemoryAllocationLib
+ HobLib
+
+[Protocols]
+ gEfiSmmReadyToLockProtocolGuid ## CONSUMES
+ gEfiSmmEndOfDxeProtocolGuid ## CONSUMES
diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.uni b/MdePkg/Library/SmmIoLib/SmmIoLib.uni
new file mode 100644
index 0000000..5d20b4d
--- /dev/null
+++ b/MdePkg/Library/SmmIoLib/SmmIoLib.uni
@@ -0,0 +1,23 @@
+// /** @file
+// Instance of SMM IO check library.
+//
+// SMM IO check library library implementation. This library consumes GCD to collect all valid
+// IO space defined by a platform.
+// A platform may have its own SmmIoLib instance to exclude more IO space.
+//
+// Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+//
+// This program and the accompanying materials
+// are licensed and made available under the terms and conditions of the BSD License
+// which accompanies this distribution. The full text of the license may be found at
+// http://opensource.org/licenses/bsd-license.php.
+// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT #language en-US "Instance of SMM IO check library."
+
+#string STR_MODULE_DESCRIPTION #language en-US "SMM IO check library library implementation. This library consumes GCD to collect all valid IO space defined by a platform. A platform may have its own SmmIoLib instance to exclude more IO space."
+
--
2.7.4.windows.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/4] MdePkg/dec: Add SmmIoLib.
2017-04-21 15:49 [PATCH 0/4] Add SmmIoLib Jiewen Yao
2017-04-21 15:49 ` [PATCH 1/4] MdePkg/SmmIoLib: Add header file Jiewen Yao
2017-04-21 15:49 ` [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance Jiewen Yao
@ 2017-04-21 15:49 ` Jiewen Yao
2017-04-21 15:49 ` [PATCH 4/4] MdePkg/dsc: add SmmIoLib Jiewen Yao
3 siblings, 0 replies; 7+ messages in thread
From: Jiewen Yao @ 2017-04-21 15:49 UTC (permalink / raw)
To: edk2-devel; +Cc: Jeff Fan, Liming Gao
Cc: Jeff Fan <jeff.fan@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
---
MdePkg/MdePkg.dec | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec
index 3310029..5774417 100644
--- a/MdePkg/MdePkg.dec
+++ b/MdePkg/MdePkg.dec
@@ -246,6 +246,10 @@
#
SmmMemLib|Include/Library/SmmMemLib.h
+ ## @libraryclass Provides services for Smm IO Operation.
+ #
+ SmmIoLib|Include/Library/SmmIoLib.h
+
## @libraryclass Provides services to enable/disable periodic SMI handlers.
#
SmmPeriodicSmiLib|Include/Library/SmmPeriodicSmiLib.h
--
2.7.4.windows.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 4/4] MdePkg/dsc: add SmmIoLib
2017-04-21 15:49 [PATCH 0/4] Add SmmIoLib Jiewen Yao
` (2 preceding siblings ...)
2017-04-21 15:49 ` [PATCH 3/4] MdePkg/dec: Add SmmIoLib Jiewen Yao
@ 2017-04-21 15:49 ` Jiewen Yao
3 siblings, 0 replies; 7+ messages in thread
From: Jiewen Yao @ 2017-04-21 15:49 UTC (permalink / raw)
To: edk2-devel; +Cc: Jeff Fan, Liming Gao
Cc: Jeff Fan <jeff.fan@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
---
MdePkg/MdePkg.dsc | 1 +
1 file changed, 1 insertion(+)
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index 8b69de3..d9c2c32 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -154,6 +154,7 @@
MdePkg/Library/BaseS3SmbusLib/BaseS3SmbusLib.inf
MdePkg/Library/BaseS3StallLib/BaseS3StallLib.inf
MdePkg/Library/SmmMemLib/SmmMemLib.inf
+ MdePkg/Library/SmmIoLib/SmmIoLib.inf
MdePkg/Library/BaseRngLib/BaseRngLib.inf
MdePkg/Library/SmmPciExpressLib/SmmPciExpressLib.inf
MdePkg/Library/SmiHandlerProfileLibNull/SmiHandlerProfileLibNull.inf
--
2.7.4.windows.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance.
2017-04-21 15:49 ` [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance Jiewen Yao
@ 2017-04-24 2:02 ` Fan, Jeff
2017-04-24 3:00 ` Yao, Jiewen
0 siblings, 1 reply; 7+ messages in thread
From: Fan, Jeff @ 2017-04-24 2:02 UTC (permalink / raw)
To: Yao, Jiewen, edk2-devel@lists.01.org; +Cc: Gao, Liming
Jiewen,
Could the following ASSER() removed and let caller check the return value?
Otherwise, there is no return on DEBUG build for this servcie.
+ if (!InValidRegion) {
+ DEBUG ((
+ DEBUG_ERROR,
+ "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
+ BaseAddress,
+ Length
+ ));
+ ASSERT (FALSE);
+ return FALSE;
+ }
Thank!
Jeff
-----Original Message-----
From: Yao, Jiewen
Sent: Friday, April 21, 2017 11:50 PM
To: edk2-devel@lists.01.org
Cc: Fan, Jeff; Gao, Liming
Subject: [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance.
The sample instance check if IO resource is valid one defined in GCD.
A platform may choose add more check to exclude some other IO resource.
Cc: Jeff Fan <jeff.fan@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
---
MdePkg/Library/SmmIoLib/SmmIoLib.c | 331 ++++++++++++++++++++
MdePkg/Library/SmmIoLib/SmmIoLib.inf | 53 ++++ MdePkg/Library/SmmIoLib/SmmIoLib.uni | 23 ++
3 files changed, 407 insertions(+)
diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.c b/MdePkg/Library/SmmIoLib/SmmIoLib.c
new file mode 100644
index 0000000..995adac
--- /dev/null
+++ b/MdePkg/Library/SmmIoLib/SmmIoLib.c
@@ -0,0 +1,331 @@
+/** @file
+ Instance of SMM IO check library.
+
+ SMM IO check library library implementation. This library consumes
+ GCD to collect all valid IO space defined by a platform.
+ A platform may have its own SmmIoLib instance to exclude more IO space.
+
+ Copyright (c) 2017, Intel Corporation. All rights reserved.<BR> This
+ program and the accompanying materials are licensed and made
+ available under the terms and conditions of the BSD License which
+ accompanies this distribution. The full text of the license may be
+ found at http://opensource.org/licenses/bsd-license.php
+
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+#include <PiSmm.h>
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h> #include
+<Library/UefiBootServicesTableLib.h>
+#include <Library/SmmServicesTableLib.h> #include <Library/HobLib.h>
+#include <Library/DxeServicesTableLib.h> #include
+<Protocol/SmmReadyToLock.h> #include <Protocol/SmmEndOfDxe.h>
+
+EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace = NULL;
+UINTN mSmmIoLibGcdMemNumberOfDesc = 0;
+
+EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress = 0;
+
+VOID *mSmmIoLibRegistrationEndOfDxe;
+VOID *mSmmIoLibRegistrationReadyToLock;
+
+BOOLEAN mSmmIoLibReadyToLock = FALSE;
+
+/**
+ Calculate and save the maximum support address.
+
+**/
+VOID
+SmmIoLibInternalCalculateMaximumSupportAddress (
+ VOID
+ )
+{
+ VOID *Hob;
+ UINT32 RegEax;
+ UINT8 MemPhysicalAddressBits;
+
+ //
+ // Get physical address bits supported.
+ //
+ Hob = GetFirstHob (EFI_HOB_TYPE_CPU); if (Hob != NULL) {
+ MemPhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
+ } else {
+ AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
+ if (RegEax >= 0x80000008) {
+ AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
+ MemPhysicalAddressBits = (UINT8) RegEax;
+ } else {
+ MemPhysicalAddressBits = 36;
+ }
+ }
+ //
+ // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
+ //
+ ASSERT (MemPhysicalAddressBits <= 52); if (MemPhysicalAddressBits >
+ 48) {
+ MemPhysicalAddressBits = 48;
+ }
+
+ //
+ // Save the maximum support address in one global variable
+ //
+ mSmmIoLibInternalMaximumSupportMemAddress =
+(EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, MemPhysicalAddressBits) -
+1);
+ DEBUG ((DEBUG_INFO, "mSmmIoLibInternalMaximumSupportMemAddress =
+0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress));
+}
+
+/**
+ This function check if the MMIO resource is valid per processor
+architecture and
+ valid per platform design.
+
+ @param BaseAddress The MMIO start address to be checked.
+ @param Length The MMIO length to be checked.
+ @param Owner A GUID representing the owner of the resource.
+ This GUID may be used by producer to correlate the device ownership of the resource.
+ NULL means no specific owner.
+
+ @retval TRUE This MMIO resource is valid per processor architecture and valid per platform design.
+ @retval FALSE This MMIO resource is not valid per processor architecture or valid per platform design.
+**/
+BOOLEAN
+EFIAPI
+SmmIsMmioValid (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length,
+ IN EFI_GUID *Owner OPTIONAL
+ )
+{
+ UINTN Index;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
+ BOOLEAN InValidRegion;
+
+ //
+ // Check override.
+ // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
+ //
+ if ((Length > mSmmIoLibInternalMaximumSupportMemAddress) ||
+ (BaseAddress > mSmmIoLibInternalMaximumSupportMemAddress) ||
+ ((Length != 0) && (BaseAddress > (mSmmIoLibInternalMaximumSupportMemAddress - (Length - 1)))) ) {
+ //
+ // Overflow happen
+ //
+ DEBUG ((
+ DEBUG_ERROR,
+ "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
+ BaseAddress,
+ Length,
+ mSmmIoLibInternalMaximumSupportMemAddress
+ ));
+ return FALSE;
+ }
+
+ //
+ // Check override for valid MMIO region // if
+ (mSmmIoLibReadyToLock) {
+ InValidRegion = FALSE;
+ for (Index = 0; Index < mSmmIoLibGcdMemNumberOfDesc; Index ++) {
+ Desc = &mSmmIoLibGcdMemSpace[Index];
+ if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
+ (BaseAddress >= Desc->BaseAddress) &&
+ ((BaseAddress + Length) <= (Desc->BaseAddress + Desc->Length))) {
+ InValidRegion = TRUE;
+ }
+ }
+
+ if (!InValidRegion) {
+ DEBUG ((
+ DEBUG_ERROR,
+ "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
+ BaseAddress,
+ Length
+ ));
+ ASSERT (FALSE);
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+/**
+ Merge continous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
+
+ @param[in, out] GcdMemoryMap A pointer to the buffer in which firmware places
+ the current GCD memory map.
+ @param[in, out] NumberOfDescriptors A pointer to the number of the
+ GcdMemoryMap buffer. On input, this is the number of
+ the current GCD memory map. On output,
+ it is the number of new GCD memory map after merge.
+**/
+STATIC
+VOID
+MergeGcdMmioEntry (
+ IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMap,
+ IN OUT UINTN *NumberOfDescriptors
+ )
+{
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEntry;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEnd;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *NewGcdMemoryMapEntry;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *NextGcdMemoryMapEntry;
+
+ GcdMemoryMapEntry = GcdMemoryMap;
+ NewGcdMemoryMapEntry = GcdMemoryMap;
+ GcdMemoryMapEnd = (EFI_GCD_MEMORY_SPACE_DESCRIPTOR *) ((UINT8 *)
+ GcdMemoryMap + (*NumberOfDescriptors) *
+ sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
+ while ((UINTN)GcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) {
+ CopyMem (NewGcdMemoryMapEntry, GcdMemoryMapEntry, sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
+ NextGcdMemoryMapEntry = GcdMemoryMapEntry + 1;
+
+ do {
+ if (((UINTN)NextGcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) &&
+ (GcdMemoryMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) && (NextGcdMemoryMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
+ ((GcdMemoryMapEntry->BaseAddress + GcdMemoryMapEntry->Length) == NextGcdMemoryMapEntry->BaseAddress)) {
+ GcdMemoryMapEntry->Length += NextGcdMemoryMapEntry->Length;
+ if (NewGcdMemoryMapEntry != GcdMemoryMapEntry) {
+ NewGcdMemoryMapEntry->Length += NextGcdMemoryMapEntry->Length;
+ }
+
+ NextGcdMemoryMapEntry = NextGcdMemoryMapEntry + 1;
+ continue;
+ } else {
+ GcdMemoryMapEntry = NextGcdMemoryMapEntry - 1;
+ break;
+ }
+ } while (TRUE);
+
+ GcdMemoryMapEntry = GcdMemoryMapEntry + 1;
+ NewGcdMemoryMapEntry = NewGcdMemoryMapEntry + 1; }
+
+ *NumberOfDescriptors = ((UINTN)NewGcdMemoryMapEntry -
+ (UINTN)GcdMemoryMap) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR);
+
+ return ;
+}
+
+/**
+ Notification for SMM EndOfDxe protocol.
+
+ @param[in] Protocol Points to the protocol's unique identifier.
+ @param[in] Interface Points to the interface instance.
+ @param[in] Handle The handle on which the interface was installed.
+
+ @retval EFI_SUCCESS Notification runs successfully.
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibInternalEndOfDxeNotify (
+ IN CONST EFI_GUID *Protocol,
+ IN VOID *Interface,
+ IN EFI_HANDLE Handle
+ )
+{
+ UINTN NumberOfDescriptors;
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
+ EFI_STATUS Status;
+
+ Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
+ if (!EFI_ERROR (Status)) {
+
+ MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);
+
+ mSmmIoLibGcdMemSpace = AllocateCopyPool (NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);
+ ASSERT_EFI_ERROR (Status);
+ if (EFI_ERROR (Status)) {
+ gBS->FreePool (MemSpaceMap);
+ return Status;
+ }
+
+ mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;
+ gBS->FreePool (MemSpaceMap);
+ }
+
+ return EFI_SUCCESS;
+}
+
+/**
+ Notification for SMM ReadyToLock protocol.
+
+ @param[in] Protocol Points to the protocol's unique identifier.
+ @param[in] Interface Points to the interface instance.
+ @param[in] Handle The handle on which the interface was installed.
+
+ @retval EFI_SUCCESS Notification runs successfully.
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibInternalReadyToLockNotify (
+ IN CONST EFI_GUID *Protocol,
+ IN VOID *Interface,
+ IN EFI_HANDLE Handle
+ )
+{
+ mSmmIoLibReadyToLock = TRUE;
+ return EFI_SUCCESS;
+}
+
+/**
+ The constructor function initializes the Smm IO library
+
+ @param ImageHandle The firmware allocated handle for the EFI image.
+ @param SystemTable A pointer to the EFI System Table.
+
+ @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibConstructor (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+{
+ EFI_STATUS Status;
+
+ //
+ // Calculate and save maximum support address //
+ SmmIoLibInternalCalculateMaximumSupportAddress ();
+
+ //
+ // Register EndOfDxe to get GCD resource map // Status =
+ gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid,
+ SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);
+ ASSERT_EFI_ERROR (Status);
+
+ //
+ // Register ready to lock so that we can know when to check valid
+ resource region // Status = gSmst->SmmRegisterProtocolNotify
+ (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify,
+ &mSmmIoLibRegistrationReadyToLock);
+ ASSERT_EFI_ERROR (Status);
+
+ return EFI_SUCCESS;
+}
+
+/**
+ The destructor function frees resource used in the Smm IO library
+
+ @param[in] ImageHandle The firmware allocated handle for the EFI image.
+ @param[in] SystemTable A pointer to the EFI System Table.
+
+ @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
+**/
+EFI_STATUS
+EFIAPI
+SmmIoLibDestructor (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+{
+ gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL,
+&mSmmIoLibRegistrationEndOfDxe);
+ gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid,
+NULL, &mSmmIoLibRegistrationReadyToLock);
+ return EFI_SUCCESS;
+}
diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.inf b/MdePkg/Library/SmmIoLib/SmmIoLib.inf
new file mode 100644
index 0000000..d508408
--- /dev/null
+++ b/MdePkg/Library/SmmIoLib/SmmIoLib.inf
@@ -0,0 +1,53 @@
+## @file
+# Instance of SMM IO check library.
+#
+# SMM IO check library library implementation. This library consumes
+GCD to collect all valid # IO space defined by a platform.
+# A platform may have its own SmmIoLib instance to exclude more IO space.
+#
+# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR> #
+This program and the accompanying materials # are licensed and made
+available under the terms and conditions of the BSD License # which
+accompanies this distribution. The full text of the license may be
+found at # http://opensource.org/licenses/bsd-license.php
+#
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
+BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+ INF_VERSION = 0x00010005
+ BASE_NAME = SmmIoLib
+ MODULE_UNI_FILE = SmmIoLib.uni
+ FILE_GUID = F0F5A845-E3ED-4C6E-82D6-4ECE85DAC00F
+ MODULE_TYPE = DXE_SMM_DRIVER
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = SmmIoLib|DXE_SMM_DRIVER SMM_CORE
+ CONSTRUCTOR = SmmIoLibConstructor
+ DESTRUCTOR = SmmIoLibDestructor
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+# VALID_ARCHITECTURES = IA32 X64
+#
+
+[Sources]
+ SmmIoLib.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ SmmServicesTableLib
+ UefiBootServicesTableLib
+ DxeServicesTableLib
+ DebugLib
+ BaseMemoryLib
+ MemoryAllocationLib
+ HobLib
+
+[Protocols]
+ gEfiSmmReadyToLockProtocolGuid ## CONSUMES
+ gEfiSmmEndOfDxeProtocolGuid ## CONSUMES
diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.uni b/MdePkg/Library/SmmIoLib/SmmIoLib.uni
new file mode 100644
index 0000000..5d20b4d
--- /dev/null
+++ b/MdePkg/Library/SmmIoLib/SmmIoLib.uni
@@ -0,0 +1,23 @@
+// /** @file
+// Instance of SMM IO check library.
+//
+// SMM IO check library library implementation. This library consumes
+GCD to collect all valid // IO space defined by a platform.
+// A platform may have its own SmmIoLib instance to exclude more IO space.
+//
+// Copyright (c) 2017, Intel Corporation. All rights reserved.<BR> //
+// This program and the accompanying materials // are licensed and made
+available under the terms and conditions of the BSD License // which
+accompanies this distribution. The full text of the license may be
+found at // http://opensource.org/licenses/bsd-license.php.
+// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
+BASIS, // WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT #language en-US "Instance of SMM IO check library."
+
+#string STR_MODULE_DESCRIPTION #language en-US "SMM IO check library library implementation. This library consumes GCD to collect all valid IO space defined by a platform. A platform may have its own SmmIoLib instance to exclude more IO space."
+
--
2.7.4.windows.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance.
2017-04-24 2:02 ` Fan, Jeff
@ 2017-04-24 3:00 ` Yao, Jiewen
0 siblings, 0 replies; 7+ messages in thread
From: Yao, Jiewen @ 2017-04-24 3:00 UTC (permalink / raw)
To: Fan, Jeff, edk2-devel@lists.01.org; +Cc: Gao, Liming
Got it.
> -----Original Message-----
> From: Fan, Jeff
> Sent: Monday, April 24, 2017 10:03 AM
> To: Yao, Jiewen <jiewen.yao@intel.com>; edk2-devel@lists.01.org
> Cc: Gao, Liming <liming.gao@intel.com>
> Subject: RE: [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance.
>
> Jiewen,
>
> Could the following ASSER() removed and let caller check the return value?
> Otherwise, there is no return on DEBUG build for this servcie.
>
> + if (!InValidRegion) {
> + DEBUG ((
> + DEBUG_ERROR,
> + "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) -
> Length (0x%lx)\n",
> + BaseAddress,
> + Length
> + ));
> + ASSERT (FALSE);
> + return FALSE;
> + }
>
> Thank!
> Jeff
> -----Original Message-----
> From: Yao, Jiewen
> Sent: Friday, April 21, 2017 11:50 PM
> To: edk2-devel@lists.01.org
> Cc: Fan, Jeff; Gao, Liming
> Subject: [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance.
>
> The sample instance check if IO resource is valid one defined in GCD.
> A platform may choose add more check to exclude some other IO resource.
>
> Cc: Jeff Fan <jeff.fan@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
> ---
> MdePkg/Library/SmmIoLib/SmmIoLib.c | 331 ++++++++++++++++++++
> MdePkg/Library/SmmIoLib/SmmIoLib.inf | 53 ++++
> MdePkg/Library/SmmIoLib/SmmIoLib.uni | 23 ++
> 3 files changed, 407 insertions(+)
>
> diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.c
> b/MdePkg/Library/SmmIoLib/SmmIoLib.c
> new file mode 100644
> index 0000000..995adac
> --- /dev/null
> +++ b/MdePkg/Library/SmmIoLib/SmmIoLib.c
> @@ -0,0 +1,331 @@
> +/** @file
> + Instance of SMM IO check library.
> +
> + SMM IO check library library implementation. This library consumes
> + GCD to collect all valid IO space defined by a platform.
> + A platform may have its own SmmIoLib instance to exclude more IO space.
> +
> + Copyright (c) 2017, Intel Corporation. All rights reserved.<BR> This
> + program and the accompanying materials are licensed and made
> + available under the terms and conditions of the BSD License which
> + accompanies this distribution. The full text of the license may be
> + found at http://opensource.org/licenses/bsd-license.php
> +
> + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS
> OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiSmm.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/MemoryAllocationLib.h> #include
> +<Library/UefiBootServicesTableLib.h>
> +#include <Library/SmmServicesTableLib.h> #include <Library/HobLib.h>
> +#include <Library/DxeServicesTableLib.h> #include
> +<Protocol/SmmReadyToLock.h> #include <Protocol/SmmEndOfDxe.h>
> +
> +EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace
> = NULL;
> +UINTN mSmmIoLibGcdMemNumberOfDesc =
> 0;
> +
> +EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress =
> 0;
> +
> +VOID *mSmmIoLibRegistrationEndOfDxe;
> +VOID *mSmmIoLibRegistrationReadyToLock;
> +
> +BOOLEAN mSmmIoLibReadyToLock = FALSE;
> +
> +/**
> + Calculate and save the maximum support address.
> +
> +**/
> +VOID
> +SmmIoLibInternalCalculateMaximumSupportAddress (
> + VOID
> + )
> +{
> + VOID *Hob;
> + UINT32 RegEax;
> + UINT8 MemPhysicalAddressBits;
> +
> + //
> + // Get physical address bits supported.
> + //
> + Hob = GetFirstHob (EFI_HOB_TYPE_CPU); if (Hob != NULL) {
> + MemPhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
> + } else {
> + AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> + if (RegEax >= 0x80000008) {
> + AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
> + MemPhysicalAddressBits = (UINT8) RegEax;
> + } else {
> + MemPhysicalAddressBits = 36;
> + }
> + }
> + //
> + // IA-32e paging translates 48-bit linear addresses to 52-bit physical
> addresses.
> + //
> + ASSERT (MemPhysicalAddressBits <= 52); if (MemPhysicalAddressBits >
> + 48) {
> + MemPhysicalAddressBits = 48;
> + }
> +
> + //
> + // Save the maximum support address in one global variable
> + //
> + mSmmIoLibInternalMaximumSupportMemAddress =
> +(EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, MemPhysicalAddressBits) -
> +1);
> + DEBUG ((DEBUG_INFO, "mSmmIoLibInternalMaximumSupportMemAddress
> =
> +0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress));
> +}
> +
> +/**
> + This function check if the MMIO resource is valid per processor
> +architecture and
> + valid per platform design.
> +
> + @param BaseAddress The MMIO start address to be checked.
> + @param Length The MMIO length to be checked.
> + @param Owner A GUID representing the owner of the resource.
> + This GUID may be used by producer to correlate the
> device ownership of the resource.
> + NULL means no specific owner.
> +
> + @retval TRUE This MMIO resource is valid per processor architecture and
> valid per platform design.
> + @retval FALSE This MMIO resource is not valid per processor architecture or
> valid per platform design.
> +**/
> +BOOLEAN
> +EFIAPI
> +SmmIsMmioValid (
> + IN EFI_PHYSICAL_ADDRESS BaseAddress,
> + IN UINT64 Length,
> + IN EFI_GUID *Owner OPTIONAL
> + )
> +{
> + UINTN Index;
> + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
> + BOOLEAN InValidRegion;
> +
> + //
> + // Check override.
> + // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is
> valid.
> + //
> + if ((Length > mSmmIoLibInternalMaximumSupportMemAddress) ||
> + (BaseAddress > mSmmIoLibInternalMaximumSupportMemAddress) ||
> + ((Length != 0) && (BaseAddress >
> (mSmmIoLibInternalMaximumSupportMemAddress - (Length - 1)))) ) {
> + //
> + // Overflow happen
> + //
> + DEBUG ((
> + DEBUG_ERROR,
> + "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx),
> MaximumSupportMemAddress (0x%lx)\n",
> + BaseAddress,
> + Length,
> + mSmmIoLibInternalMaximumSupportMemAddress
> + ));
> + return FALSE;
> + }
> +
> + //
> + // Check override for valid MMIO region // if
> + (mSmmIoLibReadyToLock) {
> + InValidRegion = FALSE;
> + for (Index = 0; Index < mSmmIoLibGcdMemNumberOfDesc; Index ++) {
> + Desc = &mSmmIoLibGcdMemSpace[Index];
> + if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)
> &&
> + (BaseAddress >= Desc->BaseAddress) &&
> + ((BaseAddress + Length) <= (Desc->BaseAddress + Desc->Length)))
> {
> + InValidRegion = TRUE;
> + }
> + }
> +
> + if (!InValidRegion) {
> + DEBUG ((
> + DEBUG_ERROR,
> + "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) -
> Length (0x%lx)\n",
> + BaseAddress,
> + Length
> + ));
> + ASSERT (FALSE);
> + return FALSE;
> + }
> + }
> + return TRUE;
> +}
> +
> +/**
> + Merge continous entries whose type is
> EfiGcdMemoryTypeMemoryMappedIo.
> +
> + @param[in, out] GcdMemoryMap A pointer to the buffer in
> which firmware places
> + the current GCD memory map.
> + @param[in, out] NumberOfDescriptors A pointer to the number of the
> + GcdMemoryMap buffer. On
> input, this is the number of
> + the current GCD memory map.
> On output,
> + it is the number of new GCD
> memory map after merge.
> +**/
> +STATIC
> +VOID
> +MergeGcdMmioEntry (
> + IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMap,
> + IN OUT UINTN *NumberOfDescriptors
> + )
> +{
> + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEntry;
> + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEnd;
> + EFI_GCD_MEMORY_SPACE_DESCRIPTOR
> *NewGcdMemoryMapEntry;
> + EFI_GCD_MEMORY_SPACE_DESCRIPTOR
> *NextGcdMemoryMapEntry;
> +
> + GcdMemoryMapEntry = GcdMemoryMap;
> + NewGcdMemoryMapEntry = GcdMemoryMap;
> + GcdMemoryMapEnd = (EFI_GCD_MEMORY_SPACE_DESCRIPTOR *) ((UINT8
> *)
> + GcdMemoryMap + (*NumberOfDescriptors) *
> + sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
> + while ((UINTN)GcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) {
> + CopyMem (NewGcdMemoryMapEntry, GcdMemoryMapEntry,
> sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
> + NextGcdMemoryMapEntry = GcdMemoryMapEntry + 1;
> +
> + do {
> + if (((UINTN)NextGcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd)
> &&
> + (GcdMemoryMapEntry->GcdMemoryType ==
> EfiGcdMemoryTypeMemoryMappedIo) &&
> (NextGcdMemoryMapEntry->GcdMemoryType ==
> EfiGcdMemoryTypeMemoryMappedIo) &&
> + ((GcdMemoryMapEntry->BaseAddress +
> GcdMemoryMapEntry->Length) == NextGcdMemoryMapEntry->BaseAddress))
> {
> + GcdMemoryMapEntry->Length +=
> NextGcdMemoryMapEntry->Length;
> + if (NewGcdMemoryMapEntry != GcdMemoryMapEntry) {
> + NewGcdMemoryMapEntry->Length +=
> NextGcdMemoryMapEntry->Length;
> + }
> +
> + NextGcdMemoryMapEntry = NextGcdMemoryMapEntry + 1;
> + continue;
> + } else {
> + GcdMemoryMapEntry = NextGcdMemoryMapEntry - 1;
> + break;
> + }
> + } while (TRUE);
> +
> + GcdMemoryMapEntry = GcdMemoryMapEntry + 1;
> + NewGcdMemoryMapEntry = NewGcdMemoryMapEntry + 1; }
> +
> + *NumberOfDescriptors = ((UINTN)NewGcdMemoryMapEntry -
> + (UINTN)GcdMemoryMap) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR);
> +
> + return ;
> +}
> +
> +/**
> + Notification for SMM EndOfDxe protocol.
> +
> + @param[in] Protocol Points to the protocol's unique identifier.
> + @param[in] Interface Points to the interface instance.
> + @param[in] Handle The handle on which the interface was installed.
> +
> + @retval EFI_SUCCESS Notification runs successfully.
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmIoLibInternalEndOfDxeNotify (
> + IN CONST EFI_GUID *Protocol,
> + IN VOID *Interface,
> + IN EFI_HANDLE Handle
> + )
> +{
> + UINTN NumberOfDescriptors;
> + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
> + EFI_STATUS Status;
> +
> + Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors,
> &MemSpaceMap);
> + if (!EFI_ERROR (Status)) {
> +
> + MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);
> +
> + mSmmIoLibGcdMemSpace = AllocateCopyPool (NumberOfDescriptors *
> sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);
> + ASSERT_EFI_ERROR (Status);
> + if (EFI_ERROR (Status)) {
> + gBS->FreePool (MemSpaceMap);
> + return Status;
> + }
> +
> + mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;
> + gBS->FreePool (MemSpaceMap);
> + }
> +
> + return EFI_SUCCESS;
> +}
> +
> +/**
> + Notification for SMM ReadyToLock protocol.
> +
> + @param[in] Protocol Points to the protocol's unique identifier.
> + @param[in] Interface Points to the interface instance.
> + @param[in] Handle The handle on which the interface was installed.
> +
> + @retval EFI_SUCCESS Notification runs successfully.
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmIoLibInternalReadyToLockNotify (
> + IN CONST EFI_GUID *Protocol,
> + IN VOID *Interface,
> + IN EFI_HANDLE Handle
> + )
> +{
> + mSmmIoLibReadyToLock = TRUE;
> + return EFI_SUCCESS;
> +}
> +
> +/**
> + The constructor function initializes the Smm IO library
> +
> + @param ImageHandle The firmware allocated handle for the EFI image.
> + @param SystemTable A pointer to the EFI System Table.
> +
> + @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmIoLibConstructor (
> + IN EFI_HANDLE ImageHandle,
> + IN EFI_SYSTEM_TABLE *SystemTable
> + )
> +{
> + EFI_STATUS Status;
> +
> + //
> + // Calculate and save maximum support address //
> + SmmIoLibInternalCalculateMaximumSupportAddress ();
> +
> + //
> + // Register EndOfDxe to get GCD resource map // Status =
> + gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid,
> + SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);
> + ASSERT_EFI_ERROR (Status);
> +
> + //
> + // Register ready to lock so that we can know when to check valid
> + resource region // Status = gSmst->SmmRegisterProtocolNotify
> + (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify,
> + &mSmmIoLibRegistrationReadyToLock);
> + ASSERT_EFI_ERROR (Status);
> +
> + return EFI_SUCCESS;
> +}
> +
> +/**
> + The destructor function frees resource used in the Smm IO library
> +
> + @param[in] ImageHandle The firmware allocated handle for the EFI
> image.
> + @param[in] SystemTable A pointer to the EFI System Table.
> +
> + @retval EFI_SUCCESS The deconstructor always returns
> EFI_SUCCESS.
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmIoLibDestructor (
> + IN EFI_HANDLE ImageHandle,
> + IN EFI_SYSTEM_TABLE *SystemTable
> + )
> +{
> + gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid,
> NULL,
> +&mSmmIoLibRegistrationEndOfDxe);
> + gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid,
> +NULL, &mSmmIoLibRegistrationReadyToLock);
> + return EFI_SUCCESS;
> +}
> diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.inf
> b/MdePkg/Library/SmmIoLib/SmmIoLib.inf
> new file mode 100644
> index 0000000..d508408
> --- /dev/null
> +++ b/MdePkg/Library/SmmIoLib/SmmIoLib.inf
> @@ -0,0 +1,53 @@
> +## @file
> +# Instance of SMM IO check library.
> +#
> +# SMM IO check library library implementation. This library consumes
> +GCD to collect all valid # IO space defined by a platform.
> +# A platform may have its own SmmIoLib instance to exclude more IO space.
> +#
> +# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR> #
> +This program and the accompanying materials # are licensed and made
> +available under the terms and conditions of the BSD License # which
> +accompanies this distribution. The full text of the license may be
> +found at # http://opensource.org/licenses/bsd-license.php
> +#
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> + INF_VERSION = 0x00010005
> + BASE_NAME = SmmIoLib
> + MODULE_UNI_FILE = SmmIoLib.uni
> + FILE_GUID =
> F0F5A845-E3ED-4C6E-82D6-4ECE85DAC00F
> + MODULE_TYPE = DXE_SMM_DRIVER
> + VERSION_STRING = 1.0
> + LIBRARY_CLASS = SmmIoLib|DXE_SMM_DRIVER
> SMM_CORE
> + CONSTRUCTOR = SmmIoLibConstructor
> + DESTRUCTOR = SmmIoLibDestructor
> +
> +#
> +# The following information is for reference only and not required by the build
> tools.
> +#
> +# VALID_ARCHITECTURES = IA32 X64
> +#
> +
> +[Sources]
> + SmmIoLib.c
> +
> +[Packages]
> + MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> + SmmServicesTableLib
> + UefiBootServicesTableLib
> + DxeServicesTableLib
> + DebugLib
> + BaseMemoryLib
> + MemoryAllocationLib
> + HobLib
> +
> +[Protocols]
> + gEfiSmmReadyToLockProtocolGuid ## CONSUMES
> + gEfiSmmEndOfDxeProtocolGuid ## CONSUMES
> diff --git a/MdePkg/Library/SmmIoLib/SmmIoLib.uni
> b/MdePkg/Library/SmmIoLib/SmmIoLib.uni
> new file mode 100644
> index 0000000..5d20b4d
> --- /dev/null
> +++ b/MdePkg/Library/SmmIoLib/SmmIoLib.uni
> @@ -0,0 +1,23 @@
> +// /** @file
> +// Instance of SMM IO check library.
> +//
> +// SMM IO check library library implementation. This library consumes
> +GCD to collect all valid // IO space defined by a platform.
> +// A platform may have its own SmmIoLib instance to exclude more IO space.
> +//
> +// Copyright (c) 2017, Intel Corporation. All rights reserved.<BR> //
> +// This program and the accompanying materials // are licensed and made
> +available under the terms and conditions of the BSD License // which
> +accompanies this distribution. The full text of the license may be
> +found at // http://opensource.org/licenses/bsd-license.php.
> +// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, // WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> +//
> +// **/
> +
> +
> +#string STR_MODULE_ABSTRACT #language en-US "Instance of
> SMM IO check library."
> +
> +#string STR_MODULE_DESCRIPTION #language en-US "SMM IO
> check library library implementation. This library consumes GCD to collect all valid
> IO space defined by a platform. A platform may have its own SmmIoLib instance
> to exclude more IO space."
> +
> --
> 2.7.4.windows.1
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2017-04-24 3:03 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-21 15:49 [PATCH 0/4] Add SmmIoLib Jiewen Yao
2017-04-21 15:49 ` [PATCH 1/4] MdePkg/SmmIoLib: Add header file Jiewen Yao
2017-04-21 15:49 ` [PATCH 2/4] MdePkg/SmmIoLib: Add sample instance Jiewen Yao
2017-04-24 2:02 ` Fan, Jeff
2017-04-24 3:00 ` Yao, Jiewen
2017-04-21 15:49 ` [PATCH 3/4] MdePkg/dec: Add SmmIoLib Jiewen Yao
2017-04-21 15:49 ` [PATCH 4/4] MdePkg/dsc: add SmmIoLib Jiewen Yao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox