public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [patch V5 28/29] MdePkg/IoLib: Filter/trace port IO/MMIO access
@ 2021-04-01  3:15 Dandan Bi
  2021-04-01  3:15 ` [patch V5 29/29] MdePkg/Baseib: Filter/trace MSR access for IA32/X64 Dandan Bi
  0 siblings, 1 reply; 2+ messages in thread
From: Dandan Bi @ 2021-04-01  3:15 UTC (permalink / raw)
  To: devel; +Cc: Sami Mujawar, Michael D Kinney, Liming Gao, Zhiguang Liu

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3246

Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
---
V5: Add missing closing bracket for if (Flag) check in MdePkg/Library/BaseIoLibIntrinsic/IoLibNoIo.c
 .../BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf |  3 +-
 .../BaseIoLibIntrinsicArmVirt.inf             |  3 +-
 .../BaseIoLibIntrinsicInternal.h              |  3 +-
 .../BaseIoLibIntrinsicSev.inf                 |  3 +-
 MdePkg/Library/BaseIoLibIntrinsic/IoLib.c     | 95 ++++++++++++++-----
 .../Library/BaseIoLibIntrinsic/IoLibArmVirt.c | 82 ++++++++++++++--
 MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c  | 58 +++++++++--
 MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c  | 81 ++++++++++++----
 MdePkg/Library/BaseIoLibIntrinsic/IoLibNoIo.c | 74 +++++++++++++--
 9 files changed, 329 insertions(+), 73 deletions(-)

diff --git a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
index 690b95d440..97eeada065 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
+++ b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
@@ -5,11 +5,11 @@
 #  for IA-32 and x64.  On IPF, I/O port requests are translated into MMIO requests.
 #  MMIO requests are forwarded directly to memory.  For EBC, I/O port requests
 #  ASSERT(). For ARM, AARCH64 and RISCV64, this I/O library only provides non I/O
 #  read and write.
 #
-#  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
 #  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
 #  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 #  Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -64,6 +64,7 @@ [Packages]
   MdePkg/MdePkg.dec
 
 [LibraryClasses]
   DebugLib
   BaseLib
+  RegisterFilterLib
 
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicArmVirt.inf b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicArmVirt.inf
index ad68f841fb..cea6857926 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicArmVirt.inf
+++ b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicArmVirt.inf
@@ -1,9 +1,9 @@
 ## @file
 #  Instance of I/O Library using KVM/ARM safe assembler routines
 #
-#  Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
 #  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
 #  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 #  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -42,5 +42,6 @@ [Packages]
   MdePkg/MdePkg.dec
 
 [LibraryClasses]
   DebugLib
   BaseLib
+  RegisterFilterLib
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicInternal.h b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicInternal.h
index 1aae45fa8a..79b2eb3e7b 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicInternal.h
+++ b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicInternal.h
@@ -1,11 +1,11 @@
 /** @file
   Common header file shared by all source files.
 
   This file includes package header files, dependent library classes.
 
-  Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
    SPDX-License-Identifier: BSD-2-Clause-Patent
 **/
 
 #ifndef __BASEIOLIB_INTRINSIC_INTERNAL_H_
 #define __BASEIOLIB_INTRINSIC_INTERNAL_H_
@@ -15,7 +15,8 @@
 #include <Base.h>
 
 #include <Library/IoLib.h>
 #include <Library/BaseLib.h>
 #include <Library/DebugLib.h>
+#include <Library/RegisterFilterLib.h>
 
 #endif
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf
index 86a07e60f8..34f9d1d106 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf
+++ b/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf
@@ -2,11 +2,11 @@
 #  Instance of I/O Library using compiler intrinsics.
 #
 #  I/O Library that uses compiler intrinsics to perform IN and OUT instructions
 #  for IA-32 and x64.
 #
-#  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
 #  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
 #  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -47,6 +47,7 @@ [Packages]
   MdePkg/MdePkg.dec
 
 [LibraryClasses]
   DebugLib
   BaseLib
+  RegisterFilterLib
 
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c
index a6bbc92546..d0d7044f09 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c
+++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLib.c
@@ -1,9 +1,9 @@
 /** @file
   Common I/O Library routines.
 
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include "BaseIoLibIntrinsicInternal.h"
@@ -80,14 +80,19 @@ EFIAPI
 MmioRead8 (
   IN      UINTN                     Address
   )
 {
   UINT8                             Value;
+  BOOLEAN                           Flag;
 
-  MemoryFence ();
-  Value = *(volatile UINT8*)Address;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoRead (FilterWidth8, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    Value = *(volatile UINT8*)Address;
+    MemoryFence ();
+  }
+  FilterAfterMmIoRead (FilterWidth8, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -110,13 +115,19 @@ EFIAPI
 MmioWrite8 (
   IN      UINTN                     Address,
   IN      UINT8                     Value
   )
 {
-  MemoryFence ();
-  *(volatile UINT8*)Address = Value;
-  MemoryFence ();
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMmIoWrite (FilterWidth8, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    *(volatile UINT8*)Address = Value;
+    MemoryFence ();
+  }
+  FilterAfterMmIoWrite (FilterWidth8, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -139,16 +150,20 @@ EFIAPI
 MmioRead16 (
   IN      UINTN                     Address
   )
 {
   UINT16                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Address & 1) == 0);
-
-  MemoryFence ();
-  Value = *(volatile UINT16*)Address;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoRead (FilterWidth16, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    Value = *(volatile UINT16*)Address;
+    MemoryFence ();
+  }
+  FilterAfterMmIoRead (FilterWidth16, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -172,15 +187,21 @@ EFIAPI
 MmioWrite16 (
   IN      UINTN                     Address,
   IN      UINT16                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Address & 1) == 0);
 
-  MemoryFence ();
-  *(volatile UINT16*)Address = Value;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoWrite (FilterWidth16, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    *(volatile UINT16*)Address = Value;
+    MemoryFence ();
+  }
+  FilterAfterMmIoWrite (FilterWidth16, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -203,16 +224,21 @@ EFIAPI
 MmioRead32 (
   IN      UINTN                     Address
   )
 {
   UINT32                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Address & 3) == 0);
 
-  MemoryFence ();
-  Value = *(volatile UINT32*)Address;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoRead (FilterWidth32, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    Value = *(volatile UINT32*)Address;
+    MemoryFence ();
+  }
+  FilterAfterMmIoRead (FilterWidth32, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -236,15 +262,21 @@ EFIAPI
 MmioWrite32 (
   IN      UINTN                     Address,
   IN      UINT32                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Address & 3) == 0);
 
-  MemoryFence ();
-  *(volatile UINT32*)Address = Value;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoWrite (FilterWidth32, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    *(volatile UINT32*)Address = Value;
+    MemoryFence ();
+  }
+  FilterAfterMmIoWrite (FilterWidth32, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -267,16 +299,21 @@ EFIAPI
 MmioRead64 (
   IN      UINTN                     Address
   )
 {
   UINT64                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Address & 7) == 0);
 
-  MemoryFence ();
-  Value = *(volatile UINT64*)Address;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoRead (FilterWidth64, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    Value = *(volatile UINT64*)Address;
+    MemoryFence ();
+  }
+  FilterAfterMmIoRead (FilterWidth64, Address, &Value);
 
   return Value;
 }
 
 /**
@@ -298,14 +335,20 @@ EFIAPI
 MmioWrite64 (
   IN      UINTN                     Address,
   IN      UINT64                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Address & 7) == 0);
 
-  MemoryFence ();
-  *(volatile UINT64*)Address = Value;
-  MemoryFence ();
+  Flag = FilterBeforeMmIoWrite (FilterWidth64, Address, &Value);
+  if (Flag) {
+    MemoryFence ();
+    *(volatile UINT64*)Address = Value;
+    MemoryFence ();
+  }
+  FilterAfterMmIoWrite (FilterWidth64, Address, &Value);
 
   return Value;
 }
 
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLibArmVirt.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLibArmVirt.c
index 9715705ee0..6140840769 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/IoLibArmVirt.c
+++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLibArmVirt.c
@@ -1,9 +1,9 @@
 /** @file
   I/O Library for ARM.
 
-  Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
   Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
   Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -544,11 +544,20 @@ UINT8
 EFIAPI
 MmioRead8 (
   IN      UINTN                     Address
   )
 {
-  return MmioRead8Internal (Address);
+  UINT8      Value;
+  BOOLEAN    Flag;
+
+  Flag = FilterBeforeMmIoRead (FilterWidth8, Address, &Value);
+  if (Flag) {
+    Value = MmioRead8Internal (Address);
+  }
+  FilterAfterMmIoRead (FilterWidth8, Address, &Value);
+
+  return Value;
 }
 
 /**
   Writes an 8-bit MMIO register.
 
@@ -567,11 +576,18 @@ EFIAPI
 MmioWrite8 (
   IN      UINTN                     Address,
   IN      UINT8                     Value
   )
 {
-  MmioWrite8Internal (Address, Value);
+  BOOLEAN               Flag;
+
+  Flag = FilterBeforeMmIoWrite (FilterWidth8, Address, &Value);
+  if (Flag) {
+    MmioWrite8Internal (Address, Value);
+  }
+  FilterAfterMmIoWrite (FilterWidth8, Address, &Value);
+
   return Value;
 }
 
 /**
   Reads a 16-bit MMIO register.
@@ -591,13 +607,22 @@ UINT16
 EFIAPI
 MmioRead16 (
   IN      UINTN                     Address
   )
 {
+  BOOLEAN    Flag;
+  UINT16     Value;
+
   ASSERT ((Address & 1) == 0);
 
-  return MmioRead16Internal (Address);
+  Flag = FilterBeforeMmIoRead (FilterWidth16, Address, &Value);
+  if (Flag) {
+    Value = MmioRead16Internal (Address);
+  }
+  FilterAfterMmIoRead (FilterWidth16, Address, &Value);
+
+  return Value;
 }
 
 /**
   Writes a 16-bit MMIO register.
 
@@ -616,13 +641,20 @@ EFIAPI
 MmioWrite16 (
   IN      UINTN                     Address,
   IN      UINT16                    Value
   )
 {
+  BOOLEAN    Flag;
+
   ASSERT ((Address & 1) == 0);
 
-  MmioWrite16Internal (Address, Value);
+  Flag = FilterBeforeMmIoWrite (FilterWidth16, Address, &Value);
+  if (Flag) {
+    MmioWrite16Internal (Address, Value);
+  }
+  FilterAfterMmIoWrite (FilterWidth16, Address, &Value);
+
   return Value;
 }
 
 /**
   Reads a 32-bit MMIO register.
@@ -642,13 +674,22 @@ UINT32
 EFIAPI
 MmioRead32 (
   IN      UINTN                     Address
   )
 {
+  BOOLEAN   Flag;
+  UINT32    Value;
+
   ASSERT ((Address & 3) == 0);
 
-  return MmioRead32Internal (Address);
+  Flag = FilterBeforeMmIoRead (FilterWidth32, Address, &Value);
+  if (Flag) {
+    Value = MmioRead32Internal (Address);
+  }
+  FilterAfterMmIoRead (FilterWidth32, Address, &Value);
+
+  return Value;
 }
 
 /**
   Writes a 32-bit MMIO register.
 
@@ -667,13 +708,20 @@ EFIAPI
 MmioWrite32 (
   IN      UINTN                     Address,
   IN      UINT32                    Value
   )
 {
+  BOOLEAN   Flag;
+
   ASSERT ((Address & 3) == 0);
 
-  MmioWrite32Internal (Address, Value);
+  Flag = FilterBeforeMmIoWrite (FilterWidth32, Address, &Value);
+  if (Flag) {
+    MmioWrite32Internal (Address, Value);
+  }
+  FilterAfterMmIoWrite (FilterWidth32, Address, &Value);
+
   return Value;
 }
 
 /**
   Reads a 64-bit MMIO register.
@@ -693,13 +741,22 @@ UINT64
 EFIAPI
 MmioRead64 (
   IN      UINTN                     Address
   )
 {
+  BOOLEAN   Flag;
+  UINT64    Value;
+
   ASSERT ((Address & 7) == 0);
 
-  return MmioRead64Internal (Address);
+  Flag = FilterBeforeMmIoRead (FilterWidth64, Address, &Value);
+  if (Flag) {
+    Value = MmioRead64Internal (Address);
+  }
+  FilterAfterMmIoRead (FilterWidth64, Address, &Value);
+
+  return Value;
 }
 
 /**
   Writes a 64-bit MMIO register.
 
@@ -718,10 +775,17 @@ EFIAPI
 MmioWrite64 (
   IN      UINTN                     Address,
   IN      UINT64                    Value
   )
 {
+  BOOLEAN   Flag;
+
   ASSERT ((Address & 7) == 0);
 
-  MmioWrite64Internal (Address, Value);
+  Flag = FilterBeforeMmIoWrite (FilterWidth64, Address, &Value);
+  if (Flag) {
+    MmioWrite64Internal (Address, Value);
+  }
+  FilterAfterMmIoWrite (FilterWidth64, Address, &Value);
+
   return Value;
 }
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c
index b3a1a20256..ecf9ed6191 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c
+++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c
@@ -8,11 +8,11 @@
   with EFIAPI assembler.
 
   We don't advocate putting compiler specifics in libraries or drivers but there
   is no other way to make this work.
 
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 
@@ -37,12 +37,18 @@ EFIAPI
 IoRead8 (
   IN      UINTN                     Port
   )
 {
   UINT8   Data;
+  BOOLEAN Flag;
+
+  Flag = FilterBeforeIoRead (FilterWidth8, Port, &Data);
+  if (Flag) {
+    __asm__ __volatile__ ("inb %w1,%b0" : "=a" (Data) : "d" ((UINT16)Port));
+  }
+  FilterAfterIoRead (FilterWidth8, Port, &Data);
 
-  __asm__ __volatile__ ("inb %w1,%b0" : "=a" (Data) : "d" ((UINT16)Port));
   return Data;
 }
 
 /**
   Writes an 8-bit I/O port.
@@ -64,11 +70,18 @@ EFIAPI
 IoWrite8 (
   IN      UINTN                     Port,
   IN      UINT8                     Value
   )
 {
-  __asm__ __volatile__ ("outb %b0,%w1" : : "a" (Value), "d" ((UINT16)Port));
+  BOOLEAN Flag;
+
+  Flag = FilterBeforeIoWrite (FilterWidth8, Port, &Value);
+  if (Flag) {
+    __asm__ __volatile__ ("outb %b0,%w1" : : "a" (Value), "d" ((UINT16)Port));
+  }
+  FilterAfterIoWrite (FilterWidth8, Port, &Value);
+
   return Value;;
 }
 
 /**
   Reads a 16-bit I/O port.
@@ -90,13 +103,20 @@ EFIAPI
 IoRead16 (
   IN      UINTN                     Port
   )
 {
   UINT16   Data;
+  BOOLEAN  Flag;
 
   ASSERT ((Port & 1) == 0);
-  __asm__ __volatile__ ("inw %w1,%w0" : "=a" (Data) : "d" ((UINT16)Port));
+
+  Flag = FilterBeforeIoRead (FilterWidth16, Port, &Data);
+  if (Flag) {
+     __asm__ __volatile__ ("inw %w1,%w0" : "=a" (Data) : "d" ((UINT16)Port));
+  }
+  FilterAfterIoRead (FilterWidth16, Port, &Data);
+
   return Data;
 }
 
 /**
   Writes a 16-bit I/O port.
@@ -119,12 +139,21 @@ EFIAPI
 IoWrite16 (
   IN      UINTN                     Port,
   IN      UINT16                    Value
   )
 {
+
+  BOOLEAN Flag;
+
   ASSERT ((Port & 1) == 0);
-  __asm__ __volatile__ ("outw %w0,%w1" : : "a" (Value), "d" ((UINT16)Port));
+
+  Flag = FilterBeforeIoWrite (FilterWidth16, Port, &Value);
+  if (Flag) {
+    __asm__ __volatile__ ("outw %w0,%w1" : : "a" (Value), "d" ((UINT16)Port));
+  }
+  FilterAfterIoWrite (FilterWidth16, Port, &Value);
+
   return Value;;
 }
 
 /**
   Reads a 32-bit I/O port.
@@ -146,13 +175,20 @@ EFIAPI
 IoRead32 (
   IN      UINTN                     Port
   )
 {
   UINT32   Data;
+  BOOLEAN  Flag;
 
   ASSERT ((Port & 3) == 0);
-  __asm__ __volatile__ ("inl %w1,%0" : "=a" (Data) : "d" ((UINT16)Port));
+
+  Flag = FilterBeforeIoRead (FilterWidth32, Port, &Data);
+  if (Flag) {
+    __asm__ __volatile__ ("inl %w1,%0" : "=a" (Data) : "d" ((UINT16)Port));
+  }
+  FilterAfterIoRead (FilterWidth32, Port, &Data);
+
   return Data;
 }
 
 /**
   Writes a 32-bit I/O port.
@@ -175,10 +211,18 @@ EFIAPI
 IoWrite32 (
   IN      UINTN                     Port,
   IN      UINT32                    Value
   )
 {
+  BOOLEAN  Flag;
+
   ASSERT ((Port & 3) == 0);
-  __asm__ __volatile__ ("outl %0,%w1" : : "a" (Value), "d" ((UINT16)Port));
+
+  Flag = FilterBeforeIoWrite (FilterWidth32, Port, &Value);
+  if (Flag) {
+    __asm__ __volatile__ ("outl %0,%w1" : : "a" (Value), "d" ((UINT16)Port));
+  }
+  FilterAfterIoWrite (FilterWidth32, Port, &Value);
+
   return Value;
 }
 
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c
index 769dddfce2..d2bc5f527c 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c
+++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c
@@ -6,11 +6,11 @@
   overhead.
 
   We don't advocate putting compiler specifics in libraries or drivers but there
   is no other way to make this work.
 
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 
@@ -64,14 +64,20 @@ EFIAPI
 IoRead8 (
   IN      UINTN                     Port
   )
 {
   UINT8                             Value;
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeIoRead (FilterWidth8, Port, &Value);
+  if (Flag) {
+    _ReadWriteBarrier ();
+    Value = (UINT8)_inp ((UINT16)Port);
+    _ReadWriteBarrier ();
+  }
+  FilterAfterIoRead (FilterWidth8, Port, &Value);
 
-  _ReadWriteBarrier ();
-  Value = (UINT8)_inp ((UINT16)Port);
-  _ReadWriteBarrier ();
   return Value;
 }
 
 /**
   Writes an 8-bit I/O port.
@@ -93,13 +99,20 @@ EFIAPI
 IoWrite8 (
   IN      UINTN                     Port,
   IN      UINT8                     Value
   )
 {
-  _ReadWriteBarrier ();
-  (UINT8)_outp ((UINT16)Port, Value);
-  _ReadWriteBarrier ();
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeIoWrite(FilterWidth8, Port, &Value);
+  if (Flag) {
+    _ReadWriteBarrier ();
+    (UINT8)_outp ((UINT16)Port, Value);
+    _ReadWriteBarrier ();
+  }
+  FilterAfterIoWrite (FilterWidth8, Port, &Value);
+
   return Value;
 }
 
 /**
   Reads a 16-bit I/O port.
@@ -121,15 +134,22 @@ EFIAPI
 IoRead16 (
   IN      UINTN                     Port
   )
 {
   UINT16                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Port & 1) == 0);
-  _ReadWriteBarrier ();
-  Value = _inpw ((UINT16)Port);
-  _ReadWriteBarrier ();
+
+  Flag = FilterBeforeIoRead (FilterWidth16, Port, &Value);
+  if (Flag) {
+    _ReadWriteBarrier ();
+    Value = _inpw ((UINT16)Port);
+    _ReadWriteBarrier ();
+  }
+  FilterBeforeIoRead (FilterWidth16, Port, &Value);
+
   return Value;
 }
 
 /**
   Writes a 16-bit I/O port.
@@ -152,14 +172,22 @@ EFIAPI
 IoWrite16 (
   IN      UINTN                     Port,
   IN      UINT16                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Port & 1) == 0);
-  _ReadWriteBarrier ();
-  _outpw ((UINT16)Port, Value);
-  _ReadWriteBarrier ();
+
+  Flag = FilterBeforeIoWrite(FilterWidth16, Port, &Value);
+  if (Flag) {
+    _ReadWriteBarrier ();
+    _outpw ((UINT16)Port, Value);
+    _ReadWriteBarrier ();
+  }
+  FilterAfterIoWrite (FilterWidth16, Port, &Value);
+
   return Value;
 }
 
 /**
   Reads a 32-bit I/O port.
@@ -181,15 +209,22 @@ EFIAPI
 IoRead32 (
   IN      UINTN                     Port
   )
 {
   UINT32                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Port & 3) == 0);
-  _ReadWriteBarrier ();
-  Value = _inpd ((UINT16)Port);
-  _ReadWriteBarrier ();
+
+  Flag = FilterBeforeIoRead(FilterWidth32, Port, &Value);
+  if (Flag) {
+    _ReadWriteBarrier ();
+    Value = _inpd ((UINT16)Port);
+    _ReadWriteBarrier ();
+  }
+  FilterAfterIoRead (FilterWidth32, Port, &Value);
+
   return Value;
 }
 
 /**
   Writes a 32-bit I/O port.
@@ -212,11 +247,19 @@ EFIAPI
 IoWrite32 (
   IN      UINTN                     Port,
   IN      UINT32                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Port & 3) == 0);
-  _ReadWriteBarrier ();
-  _outpd ((UINT16)Port, Value);
-  _ReadWriteBarrier ();
+
+  Flag = FilterBeforeIoWrite(FilterWidth32, Port, &Value);
+  if (Flag) {
+    _ReadWriteBarrier ();
+    _outpd ((UINT16)Port, Value);
+    _ReadWriteBarrier ();
+  }
+  FilterAfterIoWrite (FilterWidth32, Port, &Value);
+
   return Value;
 }
diff --git a/MdePkg/Library/BaseIoLibIntrinsic/IoLibNoIo.c b/MdePkg/Library/BaseIoLibIntrinsic/IoLibNoIo.c
index a107136a74..291cd86eaa 100644
--- a/MdePkg/Library/BaseIoLibIntrinsic/IoLibNoIo.c
+++ b/MdePkg/Library/BaseIoLibIntrinsic/IoLibNoIo.c
@@ -1,10 +1,10 @@
 /** @file
   I/O library for non I/O read and write access (memory map I/O read and
   write only) architecture, such as ARM and RISC-V processor.
 
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
   Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
   Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -406,12 +406,18 @@ EFIAPI
 MmioRead8 (
   IN      UINTN                     Address
   )
 {
   UINT8                             Value;
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMmIoRead (FilterWidth8, Address, &Value);
+  if (Flag) {
+    Value = *(volatile UINT8*)Address;
+  }
+  FilterAfterMmIoRead (FilterWidth8, Address, &Value);
 
-  Value = *(volatile UINT8*)Address;
   return Value;
 }
 
 /**
   Writes an 8-bit MMIO register.
@@ -431,11 +437,18 @@ EFIAPI
 MmioWrite8 (
   IN      UINTN                     Address,
   IN      UINT8                     Value
   )
 {
-  *(volatile UINT8*)Address = Value;
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMmIoWrite (FilterWidth8, Address, &Value);
+  if (Flag) {
+    *(volatile UINT8*)Address = Value;
+  }
+  FilterAfterMmIoWrite (FilterWidth8, Address, &Value);
+
   return Value;
 }
 
 /**
   Reads a 16-bit MMIO register.
@@ -456,13 +469,20 @@ EFIAPI
 MmioRead16 (
   IN      UINTN                     Address
   )
 {
   UINT16                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Address & 1) == 0);
-  Value = *(volatile UINT16*)Address;
+
+  Flag = FilterBeforeMmIoRead (FilterWidth16, Address, &Value);
+  if (Flag) {
+    Value = *(volatile UINT16*)Address;
+  }
+  FilterAfterMmIoRead (FilterWidth16, Address, &Value);
+
   return Value;
 }
 
 /**
   Writes a 16-bit MMIO register.
@@ -482,12 +502,20 @@ EFIAPI
 MmioWrite16 (
   IN      UINTN                     Address,
   IN      UINT16                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Address & 1) == 0);
-  *(volatile UINT16*)Address = Value;
+
+  Flag = FilterBeforeMmIoWrite (FilterWidth16, Address, &Value);
+  if (Flag) {
+    *(volatile UINT16*)Address = Value;
+  }
+  FilterAfterMmIoWrite (FilterWidth16, Address, &Value);
+
   return Value;
 }
 
 /**
   Reads a 32-bit MMIO register.
@@ -508,13 +536,20 @@ EFIAPI
 MmioRead32 (
   IN      UINTN                     Address
   )
 {
   UINT32                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Address & 3) == 0);
-  Value = *(volatile UINT32*)Address;
+
+  Flag = FilterBeforeMmIoRead (FilterWidth32, Address, &Value);
+  if (Flag) {
+    Value = *(volatile UINT32*)Address;
+  }
+  FilterAfterMmIoRead (FilterWidth32, Address, &Value);
+
   return Value;
 }
 
 /**
   Writes a 32-bit MMIO register.
@@ -534,12 +569,20 @@ EFIAPI
 MmioWrite32 (
   IN      UINTN                     Address,
   IN      UINT32                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Address & 3) == 0);
+
+  Flag = FilterBeforeMmIoWrite (FilterWidth32, Address, &Value);
+  if (Flag) {
   *(volatile UINT32*)Address = Value;
+  }
+  FilterAfterMmIoWrite (FilterWidth32, Address, &Value);
+
   return Value;
 }
 
 /**
   Reads a 64-bit MMIO register.
@@ -560,13 +603,20 @@ EFIAPI
 MmioRead64 (
   IN      UINTN                     Address
   )
 {
   UINT64                            Value;
+  BOOLEAN                           Flag;
 
   ASSERT ((Address & 7) == 0);
-  Value = *(volatile UINT64*)Address;
+
+  Flag = FilterBeforeMmIoRead (FilterWidth64, Address, &Value);
+  if (Flag) {
+    Value = *(volatile UINT64*)Address;
+  }
+  FilterAfterMmIoRead (FilterWidth64, Address, &Value);
+
   return Value;
 }
 
 /**
   Writes a 64-bit MMIO register.
@@ -586,10 +636,18 @@ EFIAPI
 MmioWrite64 (
   IN      UINTN                     Address,
   IN      UINT64                    Value
   )
 {
+  BOOLEAN                           Flag;
+
   ASSERT ((Address & 7) == 0);
-  *(volatile UINT64*)Address = Value;
+
+  Flag = FilterBeforeMmIoWrite (FilterWidth64, Address, &Value);
+  if (Flag) {
+    *(volatile UINT64*)Address = Value;
+  }
+  FilterAfterMmIoWrite (FilterWidth64, Address, &Value);
+
   return Value;
 }
 
-- 
2.18.0.windows.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [patch V5 29/29] MdePkg/Baseib: Filter/trace MSR access for IA32/X64
  2021-04-01  3:15 [patch V5 28/29] MdePkg/IoLib: Filter/trace port IO/MMIO access Dandan Bi
@ 2021-04-01  3:15 ` Dandan Bi
  0 siblings, 0 replies; 2+ messages in thread
From: Dandan Bi @ 2021-04-01  3:15 UTC (permalink / raw)
  To: devel; +Cc: Michael D Kinney, Liming Gao, Zhiguang Liu

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3246

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
---
V5: NA
 MdePkg/Library/BaseLib/BaseLib.inf          |  5 ++-
 MdePkg/Library/BaseLib/Ia32/GccInlinePriv.c | 38 +++++++++++-------
 MdePkg/Library/BaseLib/Ia32/ReadMsr64.c     | 38 ++++++++++++++++--
 MdePkg/Library/BaseLib/Ia32/WriteMsr64.c    | 22 +++++++----
 MdePkg/Library/BaseLib/X64/GccInlinePriv.c  | 43 ++++++++++++++-------
 MdePkg/Library/BaseLib/X64/ReadMsr64.c      | 15 ++++++-
 MdePkg/Library/BaseLib/X64/WriteMsr64.c     | 13 ++++++-
 7 files changed, 130 insertions(+), 44 deletions(-)

diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/BaseLib.inf
index 3b85c56c3c..58e29cc7af 100644
--- a/MdePkg/Library/BaseLib/BaseLib.inf
+++ b/MdePkg/Library/BaseLib/BaseLib.inf
@@ -1,9 +1,9 @@
 ## @file
 #  Base Library implementation.
 #
-#  Copyright (c) 2007 - 2020, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
 #  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
 #  Portions copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
 #  Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -409,10 +409,13 @@ [Packages]
 [LibraryClasses]
   PcdLib
   DebugLib
   BaseMemoryLib
 
+[LibraryClasses.X64, LibraryClasses.IA32]
+  RegisterFilterLib
+
 [Pcd]
   gEfiMdePkgTokenSpaceGuid.PcdMaximumLinkedListLength      ## SOMETIMES_CONSUMES
   gEfiMdePkgTokenSpaceGuid.PcdMaximumAsciiStringLength     ## SOMETIMES_CONSUMES
   gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength   ## SOMETIMES_CONSUMES
   gEfiMdePkgTokenSpaceGuid.PcdControlFlowEnforcementPropertyMask   ## SOMETIMES_CONSUMES
diff --git a/MdePkg/Library/BaseLib/Ia32/GccInlinePriv.c b/MdePkg/Library/BaseLib/Ia32/GccInlinePriv.c
index 30aa63243b..40e8c08beb 100644
--- a/MdePkg/Library/BaseLib/Ia32/GccInlinePriv.c
+++ b/MdePkg/Library/BaseLib/Ia32/GccInlinePriv.c
@@ -1,17 +1,18 @@
 /** @file
   GCC inline implementation of BaseLib processor specific functions that use
   privlidged instructions.
 
-  Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 
 #include "BaseLibInternals.h"
+#include <Library/RegisterFilterLib.h>
 
 /**
   Enables CPU interrupts.
 
   Enables CPU interrupts.
@@ -61,16 +62,21 @@ EFIAPI
 AsmReadMsr64 (
   IN      UINT32                    Index
   )
 {
   UINT64 Data;
-
-  __asm__ __volatile__ (
-    "rdmsr"
-    : "=A" (Data)   // %0
-    : "c"  (Index)  // %1
-    );
+  BOOLEAN Flag;
+
+  Flag = FilterBeforeMsrRead (Index, &Data);
+  if (Flag) {
+    __asm__ __volatile__ (
+      "rdmsr"
+      : "=A" (Data)   // %0
+      : "c"  (Index)  // %1
+      );
+  }
+  FilterAfterMsrRead (Index, &Data);
 
   return Data;
 }
 
 /**
@@ -95,16 +101,22 @@ EFIAPI
 AsmWriteMsr64 (
   IN      UINT32                    Index,
   IN      UINT64                    Value
   )
 {
-  __asm__ __volatile__ (
-    "wrmsr"
-    :
-    : "c" (Index),
-      "A" (Value)
-    );
+  BOOLEAN  Flag;
+
+  Flag = FilterBeforeMsrWrite (Index, &Value);
+  if (Flag) {
+    __asm__ __volatile__ (
+      "wrmsr"
+      :
+      : "c" (Index),
+        "A" (Value)
+      );
+  }
+  FilterAfterMsrWrite (Index, &Value);
 
   return Value;
 }
 
 /**
diff --git a/MdePkg/Library/BaseLib/Ia32/ReadMsr64.c b/MdePkg/Library/BaseLib/Ia32/ReadMsr64.c
index 6d2394b1a3..afe3aa5bdc 100644
--- a/MdePkg/Library/BaseLib/Ia32/ReadMsr64.c
+++ b/MdePkg/Library/BaseLib/Ia32/ReadMsr64.c
@@ -1,15 +1,15 @@
 /** @file
   AsmReadMsr64 function
 
-  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 
-
+#include <Library/RegisterFilterLib.h>
 
 /**
   Returns a 64-bit Machine Specific Register(MSR).
 
   Reads and returns the 64-bit MSR specified by Index. No parameter checking is
@@ -22,16 +22,46 @@
 
   @return The value of the MSR identified by Index.
 
 **/
 UINT64
-EFIAPI
-AsmReadMsr64 (
+AsmReadMsr64Internal (
   IN UINT32  Index
   )
 {
   _asm {
     mov     ecx, Index
     rdmsr
   }
 }
 
+/**
+  Returns a 64-bit Machine Specific Register(MSR).
+
+  Reads and returns the 64-bit MSR specified by Index. No parameter checking is
+  performed on Index, and some Index values may cause CPU exceptions. The
+  caller must either guarantee that Index is valid, or the caller must set up
+  exception handlers to catch the exceptions. This function is only available
+  on IA-32 and x64.
+
+  @param  Index The 32-bit MSR index to read.
+
+  @return The value of the MSR identified by Index.
+
+**/
+UINT64
+EFIAPI
+AsmReadMsr64 (
+  IN UINT32  Index
+  )
+{
+  UINT64                            Value;
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMsrRead (Index, &Value);
+  if (Flag) {
+    Value = AsmReadMsr64Internal (Index);
+  }
+  FilterAfterMsrRead (Index, &Value);
+
+  return Value;
+}
diff --git a/MdePkg/Library/BaseLib/Ia32/WriteMsr64.c b/MdePkg/Library/BaseLib/Ia32/WriteMsr64.c
index badf1d8e58..ba0cf3f74c 100644
--- a/MdePkg/Library/BaseLib/Ia32/WriteMsr64.c
+++ b/MdePkg/Library/BaseLib/Ia32/WriteMsr64.c
@@ -1,15 +1,15 @@
 /** @file
   AsmWriteMsr64 function
 
-  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 
-
+#include <Library/RegisterFilterLib.h>
 
 /**
   Writes a 64-bit value to a Machine Specific Register(MSR), and returns the
   value.
 
@@ -31,13 +31,21 @@ EFIAPI
 AsmWriteMsr64 (
   IN UINT32  Index,
   IN UINT64  Value
   )
 {
-  _asm {
-    mov     edx, dword ptr [Value + 4]
-    mov     eax, dword ptr [Value + 0]
-    mov     ecx, Index
-    wrmsr
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMsrWrite (Index, &Value);
+  if (Flag) {
+    _asm {
+      mov     edx, dword ptr [Value + 4]
+      mov     eax, dword ptr [Value + 0]
+      mov     ecx, Index
+      wrmsr
+    }
   }
+  FilterAfterMsrWrite (Index, &Value);
+
+  return Value;
 }
 
diff --git a/MdePkg/Library/BaseLib/X64/GccInlinePriv.c b/MdePkg/Library/BaseLib/X64/GccInlinePriv.c
index 98be19b3c7..e4920f2116 100644
--- a/MdePkg/Library/BaseLib/X64/GccInlinePriv.c
+++ b/MdePkg/Library/BaseLib/X64/GccInlinePriv.c
@@ -1,17 +1,18 @@
 /** @file
   GCC inline implementation of BaseLib processor specific functions that use
   privlidged instructions.
 
-  Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 
 #include "BaseLibInternals.h"
+#include <Library/RegisterFilterLib.h>
 
 /**
   Enables CPU interrupts.
 
   Enables CPU interrupts.
@@ -62,17 +63,24 @@ AsmReadMsr64 (
   IN      UINT32                    Index
   )
 {
   UINT32 LowData;
   UINT32 HighData;
-
-  __asm__ __volatile__ (
-    "rdmsr"
-    : "=a" (LowData),   // %0
-      "=d" (HighData)   // %1
-    : "c"  (Index)      // %2
-    );
+  UINT64 Value;
+  BOOLEAN Flag;
+
+  Flag = FilterBeforeMsrRead (Index, &Value);
+  if (Flag) {
+    __asm__ __volatile__ (
+      "rdmsr"
+      : "=a" (LowData),   // %0
+        "=d" (HighData)   // %1
+      : "c"  (Index)      // %2
+      );
+    Value = (((UINT64)HighData) << 32) | LowData;
+  }
+  FilterAfterMsrRead (Index, &Value);
 
   return (((UINT64)HighData) << 32) | LowData;
 }
 
 /**
@@ -99,21 +107,26 @@ AsmWriteMsr64 (
   IN      UINT64                    Value
   )
 {
   UINT32 LowData;
   UINT32 HighData;
+  BOOLEAN Flag;
 
   LowData  = (UINT32)(Value);
   HighData = (UINT32)(Value >> 32);
 
-  __asm__ __volatile__ (
-    "wrmsr"
-    :
-    : "c" (Index),
-      "a" (LowData),
-      "d" (HighData)
-    );
+  Flag = FilterBeforeMsrWrite (Index, &Value);
+  if (Flag) {
+    __asm__ __volatile__ (
+      "wrmsr"
+      :
+      : "c" (Index),
+        "a" (LowData),
+        "d" (HighData)
+      );
+  }
+  FilterAfterMsrWrite (Index, &Value);
 
   return Value;
 }
 
 /**
diff --git a/MdePkg/Library/BaseLib/X64/ReadMsr64.c b/MdePkg/Library/BaseLib/X64/ReadMsr64.c
index 5ee7ca53f3..36a349432c 100644
--- a/MdePkg/Library/BaseLib/X64/ReadMsr64.c
+++ b/MdePkg/Library/BaseLib/X64/ReadMsr64.c
@@ -1,17 +1,19 @@
 /** @file
   CpuBreakpoint function.
 
-  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 /**
   Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics.
 **/
 
+#include <Library/RegisterFilterLib.h>
+
 unsigned __int64 __readmsr (int register);
 
 #pragma intrinsic(__readmsr)
 
 /**
@@ -26,8 +28,17 @@ UINT64
 EFIAPI
 AsmReadMsr64 (
   IN UINT32  Index
   )
 {
-  return __readmsr (Index);
+  UINT64                            Value;
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMsrRead (Index, &Value);
+  if (Flag) {
+    Value = __readmsr (Index);
+  }
+  FilterAfterMsrRead (Index, &Value);
+
+  return Value;
 }
 
diff --git a/MdePkg/Library/BaseLib/X64/WriteMsr64.c b/MdePkg/Library/BaseLib/X64/WriteMsr64.c
index 98c5458d8a..bb030832c4 100644
--- a/MdePkg/Library/BaseLib/X64/WriteMsr64.c
+++ b/MdePkg/Library/BaseLib/X64/WriteMsr64.c
@@ -1,17 +1,19 @@
 /** @file
   CpuBreakpoint function.
 
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 /**
   Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics.
 **/
 
+#include <Library/RegisterFilterLib.h>
+
 void __writemsr (unsigned long Register, unsigned __int64 Value);
 
 #pragma intrinsic(__writemsr)
 
 /**
@@ -28,9 +30,16 @@ EFIAPI
 AsmWriteMsr64 (
   IN UINT32  Index,
   IN UINT64  Value
   )
 {
-  __writemsr (Index, Value);
+  BOOLEAN                           Flag;
+
+  Flag = FilterBeforeMsrWrite (Index, &Value);
+  if (Flag) {
+    __writemsr (Index, Value);
+  }
+  FilterAfterMsrWrite (Index, &Value);
+
   return Value;
 }
 
-- 
2.18.0.windows.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-01  3:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-01  3:15 [patch V5 28/29] MdePkg/IoLib: Filter/trace port IO/MMIO access Dandan Bi
2021-04-01  3:15 ` [patch V5 29/29] MdePkg/Baseib: Filter/trace MSR access for IA32/X64 Dandan Bi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox