[edk2-devel] [edk2-platforms][PATCH V4 8/8] Platform/Loongson: Enable zero address protection.

xianglai posted 8 patches 3 years ago
[edk2-devel] [edk2-platforms][PATCH V4 8/8] Platform/Loongson: Enable zero address protection.
Posted by xianglai 3 years ago
Set the 0 page property to unreadable, non-writable, non-executable

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 .../LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf     | 2 ++
 .../Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c | 8 +++++++-
 .../LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c       | 5 +++++
 Platform/Loongson/LoongArchQemuPkg/Loongson.dsc           | 2 ++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
index 12848eecfe..d7ab37eea4 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
@@ -28,6 +28,7 @@
   MdePkg/MdePkg.dec
   Platform/Loongson/LoongArchQemuPkg/Loongson.dec
   OvmfPkg/OvmfPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
 
 [PCD]
   gLoongArchQemuPkgTokenSpaceGuid.PcdSwapPageDir
@@ -38,6 +39,7 @@
   gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvSize
   gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvBase
   gLoongArchQemuPkgTokenSpaceGuid.PcdRamSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask
 
 [LibraryClasses]
   MemoryAllocationLib
diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
index dac38c63f2..a45bcbdb6d 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
@@ -735,10 +735,16 @@ EfiAttributeToLoongArchAttribute (
   }
 
   // Write protection attributes
-  if ((EfiAttributes & EFI_MEMORY_RO) != 0) {
+  if (((EfiAttributes & EFI_MEMORY_RO) != 0) ||
+      ((EfiAttributes & EFI_MEMORY_WP) != 0))
+  {
     LoongArchAttributes &= ~PAGE_DIRTY;
   }
 
+  if (EfiAttributes & EFI_MEMORY_RP) {
+    LoongArchAttributes |= PAGE_NO_READ;
+  }
+
   //eXecute protection attribute
   if ((EfiAttributes & EFI_MEMORY_XP) != 0) {
     LoongArchAttributes |= PAGE_NO_EXEC;
diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
index 32a7fc0beb..a21233e796 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
@@ -21,6 +21,7 @@
 #include <Library/QemuFwCfgLib.h>
 #include "MmuLibCore.h"
 #include <Library/CacheMaintenanceLib.h>
+#include <Library/MmuLib.h>
 
 /**
   Return the Virtual Memory Map of your platform
@@ -170,6 +171,10 @@ ConfigureMmu (VOID)
     MemoryTable++;
   }
 
+  if (PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) {
+    LoongArchSetMemoryAttributes (0, EFI_PAGE_SIZE, EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_WP);
+  }
+
   TlbReEntry = AllocatePages (1);
   if (TlbReEntry == NULL) {
     goto FreeTranslationTable;
diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
index 05913db144..13145cf051 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
+++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
@@ -365,6 +365,8 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x40000
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x40000
 
+  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask   | 1
+
 ################################################################################
 #
 # Pcd Dynamic Section - list of all EDK II PCD Entries defined by this Platform
-- 
2.31.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#98433): https://edk2.groups.io/g/devel/message/98433
Mute This Topic: https://groups.io/mt/96239135/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [edk2-platforms][PATCH V4 8/8] Platform/Loongson: Enable zero address protection.
Posted by Chao Li 3 years ago
Reviewed-by: Chao Li <lichao@loongson.cn>


Thanks,
Chao
在 2023/1/13 11:17, xianglai 写道:
> Set the 0 page property to unreadable, non-writable, non-executable
>
> Cc: Ard Biesheuvel<ardb+tianocore@kernel.org>
> Cc: Bibo Mao<maobibo@loongson.cn>
> Cc: Chao Li<lichao@loongson.cn>
> Cc: Leif Lindholm<quic_llindhol@quicinc.com>
> Cc: Liming Gao<gaoliming@byosoft.com.cn>
> Cc: Michael D Kinney<michael.d.kinney@intel.com>
> Signed-off-by: xianglai li<lixianglai@loongson.cn>
> ---
>   .../LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf     | 2 ++
>   .../Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c | 8 +++++++-
>   .../LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c       | 5 +++++
>   Platform/Loongson/LoongArchQemuPkg/Loongson.dsc           | 2 ++
>   4 files changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
> index 12848eecfe..d7ab37eea4 100644
> --- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
> +++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
> @@ -28,6 +28,7 @@
>     MdePkg/MdePkg.dec
>     Platform/Loongson/LoongArchQemuPkg/Loongson.dec
>     OvmfPkg/OvmfPkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
>   
>   [PCD]
>     gLoongArchQemuPkgTokenSpaceGuid.PcdSwapPageDir
> @@ -38,6 +39,7 @@
>     gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvSize
>     gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvBase
>     gLoongArchQemuPkgTokenSpaceGuid.PcdRamSize
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask
>   
>   [LibraryClasses]
>     MemoryAllocationLib
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
> index dac38c63f2..a45bcbdb6d 100644
> --- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
> +++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
> @@ -735,10 +735,16 @@ EfiAttributeToLoongArchAttribute (
>     }
>   
>     // Write protection attributes
> -  if ((EfiAttributes & EFI_MEMORY_RO) != 0) {
> +  if (((EfiAttributes & EFI_MEMORY_RO) != 0) ||
> +      ((EfiAttributes & EFI_MEMORY_WP) != 0))
> +  {
>       LoongArchAttributes &= ~PAGE_DIRTY;
>     }
>   
> +  if (EfiAttributes & EFI_MEMORY_RP) {
> +    LoongArchAttributes |= PAGE_NO_READ;
> +  }
> +
>     //eXecute protection attribute
>     if ((EfiAttributes & EFI_MEMORY_XP) != 0) {
>       LoongArchAttributes |= PAGE_NO_EXEC;
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
> index 32a7fc0beb..a21233e796 100644
> --- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
> +++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
> @@ -21,6 +21,7 @@
>   #include <Library/QemuFwCfgLib.h>
>   #include "MmuLibCore.h"
>   #include <Library/CacheMaintenanceLib.h>
> +#include <Library/MmuLib.h>
>   
>   /**
>     Return the Virtual Memory Map of your platform
> @@ -170,6 +171,10 @@ ConfigureMmu (VOID)
>       MemoryTable++;
>     }
>   
> +  if (PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) {
> +    LoongArchSetMemoryAttributes (0, EFI_PAGE_SIZE, EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_WP);
> +  }
> +
>     TlbReEntry = AllocatePages (1);
>     if (TlbReEntry == NULL) {
>       goto FreeTranslationTable;
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
> index 05913db144..13145cf051 100644
> --- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
> +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
> @@ -365,6 +365,8 @@
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x40000
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x40000
>   
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask   | 1
> +
>   ################################################################################
>   #
>   # Pcd Dynamic Section - list of all EDK II PCD Entries defined by this Platform


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#98543): https://edk2.groups.io/g/devel/message/98543
Mute This Topic: https://groups.io/mt/96239135/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-