public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Jianyong Wu" <jianyong.wu@arm.com>
To: devel@edk2.groups.io, Sami.Mujawar@arm.com
Cc: ardb+tianocore@kernel.org, justin.he@arm.com, jianyong.wu@arm.com
Subject: [PATCH 2/3] CloudHv:arm: build hob for kernel image memory as read-only
Date: Fri, 16 Sep 2022 10:46:19 +0800	[thread overview]
Message-ID: <20220916024620.114084-3-jianyong.wu@arm.com> (raw)
In-Reply-To: <20220916024620.114084-1-jianyong.wu@arm.com>

As we use memory to pass kernel image, the memory region where kernel
image locates should be added into hob as read-only.

Signed-off-by: Jianyong Wu <jianyong.wu@arm.com>
---
 .../CloudHvVirtMemInfoLib.c                   | 66 +++++++++++++++++--
 1 file changed, 62 insertions(+), 4 deletions(-)

diff --git a/ArmVirtPkg/Library/CloudHvVirtMemInfoLib/CloudHvVirtMemInfoLib.c b/ArmVirtPkg/Library/CloudHvVirtMemInfoLib/CloudHvVirtMemInfoLib.c
index 28a0c0b078..d9b7d51a16 100644
--- a/ArmVirtPkg/Library/CloudHvVirtMemInfoLib/CloudHvVirtMemInfoLib.c
+++ b/ArmVirtPkg/Library/CloudHvVirtMemInfoLib/CloudHvVirtMemInfoLib.c
@@ -37,13 +37,14 @@ CloudHvVirtMemInfoPeiLibConstructor (
   )
 {
   VOID                         *DeviceTreeBase;
-  EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttributes;
+  EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttributes, ReadOnlyResourceAttributes;
   INT32                        Node, Prev;
   UINT64                       FirMemNodeBase, FirMemNodeSize;
-  UINT64                       CurBase, MemBase;
+  UINT64                       CurBase, MemBase, CurSizeOff;
   UINT64                       CurSize;
+  UINT64                       KernelStart, KernelSize;
   CONST CHAR8                  *Type;
-  INT32                        Len;
+  INT32                        Len, ChosenNode;
   CONST UINT64                 *RegProp;
   RETURN_STATUS                PcdStatus;
   UINT8                        Index;
@@ -53,6 +54,8 @@ CloudHvVirtMemInfoPeiLibConstructor (
   FirMemNodeBase     = 0;
   FirMemNodeSize     = 0;
   Index              = 0;
+  CurSizeOff         = 0;
+  KernelSize         = 0;
   MemBase            = FixedPcdGet64 (PcdSystemMemoryBase);
   ResourceAttributes = (
                         EFI_RESOURCE_ATTRIBUTE_PRESENT |
@@ -60,6 +63,12 @@ CloudHvVirtMemInfoPeiLibConstructor (
                         EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
                         EFI_RESOURCE_ATTRIBUTE_TESTED
                         );
+  ReadOnlyResourceAttributes = (
+                                EFI_RESOURCE_ATTRIBUTE_PRESENT |
+                                EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+                                EFI_RESOURCE_ATTRIBUTE_TESTED |
+                                EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED
+                                );
   DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);
   if (DeviceTreeBase == NULL) {
     return EFI_NOT_FOUND;
@@ -72,6 +81,21 @@ CloudHvVirtMemInfoPeiLibConstructor (
     return EFI_NOT_FOUND;
   }
 
+  //
+  // Try to get kernel image info from DT
+  //
+  ChosenNode = fdt_path_offset (DeviceTreeBase, "/chosen");
+  if (ChosenNode >= 0) {
+    RegProp = fdt_getprop (DeviceTreeBase, ChosenNode, "linux,kernel-start", &Len);
+    if ((RegProp != NULL) && (Len > 0)) {
+      KernelStart = (UINT64)fdt64_to_cpu (ReadUnaligned64 (RegProp));
+      RegProp     = fdt_getprop (DeviceTreeBase, ChosenNode, "linux,kernel-size", &Len);
+      if ((RegProp != NULL) && (Len > 0)) {
+        KernelSize = (UINT64)fdt64_to_cpu (ReadUnaligned64 (RegProp));
+      }
+    }
+  }
+
   //
   // Look for the lowest memory node
   //
@@ -105,11 +129,26 @@ CloudHvVirtMemInfoPeiLibConstructor (
 
         // We should build Hob seperately for the memory node except the first one
         if (CurBase != MemBase) {
+          // If kernel image resides in current memory node, build hob from CurBase to the beginning of kernel image.
+          if ((KernelSize != 0) && (KernelStart >= CurBase) && (KernelStart + KernelSize <= CurBase + CurSize)) {
+            CurSizeOff =  CurBase + CurSize - KernelStart;
+            // align up with 0x1000
+            CurSizeOff = (CurSizeOff + 0xfff) & ~0xfffUL;
+          }
+
           BuildResourceDescriptorHob (
             EFI_RESOURCE_SYSTEM_MEMORY,
             ResourceAttributes,
             CurBase,
-            CurSize
+            CurSize - CurSizeOff
+            );
+
+          // Add kernel image memory region to hob as read only
+          BuildResourceDescriptorHob (
+            EFI_RESOURCE_SYSTEM_MEMORY,
+            ReadOnlyResourceAttributes,
+            CurBase + CurSize - CurSizeOff,
+            CurSizeOff
             );
         } else {
           FirMemNodeBase = CurBase;
@@ -146,8 +185,27 @@ CloudHvVirtMemInfoPeiLibConstructor (
     return EFI_NOT_FOUND;
   }
 
+  CurSizeOff = 0;
+  // Build hob for the lowest memory node from its base to the beginning of kernel image once the kernel image reside here
+  if ((KernelSize != 0) && (KernelStart >= FirMemNodeBase) && (KernelStart + KernelSize <= FirMemNodeBase + FirMemNodeSize)) {
+    CurSizeOff = FirMemNodeBase + FirMemNodeSize - KernelStart;
+    // Caution the alignment
+    CurSizeOff = (CurSizeOff + 0xfff) & ~0xfffUL;
+
+    // Add kernel image memory region to hob as read only
+    BuildResourceDescriptorHob (
+      EFI_RESOURCE_SYSTEM_MEMORY,
+      ReadOnlyResourceAttributes,
+      FirMemNodeBase + FirMemNodeSize - CurSizeOff,
+      CurSizeOff
+      );
+  }
+
+  FirMemNodeSize -= CurSizeOff;
+
   PcdStatus = PcdSet64S (PcdSystemMemorySize, FirMemNodeSize);
   ASSERT_RETURN_ERROR (PcdStatus);
+
   ASSERT (
     (((UINT64)PcdGet64 (PcdFdBaseAddress) +
       (UINT64)PcdGet32 (PcdFdSize)) <= FirMemNodeBase) ||
-- 
2.17.1


  parent reply	other threads:[~2022-09-16  2:46 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-16  2:46 [PATCH 0/3] CloudHv:arm: Enable direct kernel boot Jianyong Wu
2022-09-16  2:46 ` [PATCH 1/3] CloudHv:arm: add kernel load fs driver Jianyong Wu
2022-11-22 15:44   ` Sami Mujawar
2022-11-22 15:46   ` Sami Mujawar
2022-11-23  6:26     ` Jianyong Wu
2022-09-16  2:46 ` Jianyong Wu [this message]
2022-11-22 15:47   ` [PATCH 2/3] CloudHv:arm: build hob for kernel image memory as read-only Sami Mujawar
2022-11-23  2:56     ` Jianyong Wu
2022-09-16  2:46 ` [PATCH 3/3] CloudHv:arm: add kernel load driver into dsc/fdf Jianyong Wu
2022-11-22 15:48   ` Sami Mujawar
2022-11-23  5:44     ` Jianyong Wu
     [not found]     ` <172A2070EC93BBE5.3230@groups.io>
2023-01-11  8:12       ` [edk2-devel] " Jianyong Wu
2023-01-11  8:35         ` Sami Mujawar
2023-01-11  9:27           ` Jianyong Wu
     [not found]           ` <17393714CBDB554C.25137@groups.io>
2023-04-24  2:36             ` Jianyong Wu
2023-05-23  6:48               ` Jianyong Wu
2022-11-15  3:00 ` [PATCH 0/3] CloudHv:arm: Enable direct kernel boot Jianyong Wu
2022-11-15  8:51   ` Sami Mujawar
2022-11-15  9:01     ` Jianyong Wu

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=20220916024620.114084-3-jianyong.wu@arm.com \
    --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