From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from loongson.cn (loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web11.77609.1673579907379790307 for ; Thu, 12 Jan 2023 19:18:28 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: loongson.cn, ip: 114.242.206.163, mailfrom: lixianglai@loongson.cn) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8BxLuuCzcBjRWABAA--.4311S3; Fri, 13 Jan 2023 11:18:26 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axvr5VzcBjisEYAA--.49868S10; Fri, 13 Jan 2023 11:18:26 +0800 (CST) From: "xianglai" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Bibo Mao , Chao Li , Leif Lindholm , Liming Gao , Michael D Kinney Subject: [edk2-platforms][PATCH V4 8/8] Platform/Loongson: Enable zero address protection. Date: Fri, 13 Jan 2023 11:17:38 +0800 Message-Id: <081f0ac9e3e10898db9f200d435152eb00bd0e75.1673579388.git.lixianglai@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Axvr5VzcBjisEYAA--.49868S10 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxuryfZr1UJr18WFyUJw1kZrb_yoWrAF4Dpr 4DZas2kr1UJrWjya15J3WUWanrCr9xGr95JrsIgr17C3srX3WDW34Yvay8tr43Zry3ta40 q3y0gw4UZFnYvrJanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bnkFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26r4j6F4UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7 CjxVAFwI0_Gr1j6F4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2 zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VCjz48v1sIEY20_WwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxF aVAv8VWrMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxV Cjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY 6xIIjxv20xvE14v26F1j6w1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6x AIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Cr0_Gr1UMIIF0xvEx4A2jsIE c7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0zRVWlkUUUUU= Content-Transfer-Encoding: 8bit Set the 0 page property to unreadable, non-writable, non-executable Cc: Ard Biesheuvel Cc: Bibo Mao Cc: Chao Li Cc: Leif Lindholm Cc: Liming Gao Cc: Michael D Kinney Signed-off-by: xianglai li --- .../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 #include "MmuLibCore.h" #include +#include /** 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