public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "xianglai" <lixianglai@loongson.cn>
To: devel@edk2.groups.io
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>,
	Bibo Mao <maobibo@loongson.cn>, Chao Li <lichao@loongson.cn>,
	Leif Lindholm <quic_llindhol@quicinc.com>,
	Liming Gao <gaoliming@byosoft.com.cn>,
	Michael D Kinney <michael.d.kinney@intel.com>
Subject: [edk2-platforms][PATCH V4 8/8] Platform/Loongson: Enable zero address protection.
Date: Fri, 13 Jan 2023 11:17:38 +0800	[thread overview]
Message-ID: <081f0ac9e3e10898db9f200d435152eb00bd0e75.1673579388.git.lixianglai@loongson.cn> (raw)
In-Reply-To: <cover.1673579388.git.lixianglai@loongson.cn>

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


  parent reply	other threads:[~2023-01-13  3:18 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-13  3:17 [edk2-platforms][PATCH V4 0/8] Loongarch add flash device xianglai
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 1/8] Platform/Loongson: Add bootmode support xianglai
2023-01-16  3:22   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 2/8] Platform/Loongson: Add nvme device driver for loongarch xianglai
2023-01-16  3:22   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 3/8] Platform/Loongson: Add pflash driver xianglai
2023-01-16  3:22   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 4/8] Platform/Loongson: Support pflash for loongarch xianglai
2023-01-16  3:22   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 5/8] Platform/Loongson: Modify the page table entry access priority xianglai
2023-01-16  3:23   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 6/8] Platform/Loongson: Optimize page table entry null determination xianglai
2023-01-16  3:23   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` [edk2-platforms][PATCH V4 7/8] Platform/Loongson: Optimize the huge page and page entry conversion xianglai
2023-01-16  3:23   ` [edk2-devel] " Chao Li
2023-01-13  3:17 ` xianglai [this message]
2023-01-16  3:23   ` [edk2-devel] [edk2-platforms][PATCH V4 8/8] Platform/Loongson: Enable zero address protection Chao Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=081f0ac9e3e10898db9f200d435152eb00bd0e75.1673579388.git.lixianglai@loongson.cn \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox