Blame SOURCES/0304-kern-efi-mm-Extract-function-to-add-memory-regions.patch

b35c50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b35c50
From: Patrick Steinhardt <ps@pks.im>
b35c50
Date: Thu, 21 Apr 2022 15:24:20 +1000
b35c50
Subject: [PATCH] kern/efi/mm: Extract function to add memory regions
b35c50
b35c50
In preparation of support for runtime-allocating additional memory
b35c50
region, this patch extracts the function to retrieve the EFI memory
b35c50
map and add a subset of it to GRUB's own memory regions.
b35c50
b35c50
Signed-off-by: Patrick Steinhardt <ps@pks.im>
b35c50
Signed-off-by: Daniel Axtens <dja@axtens.net>
b35c50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b35c50
Tested-by: Patrick Steinhardt <ps@pks.im>
b35c50
(cherry picked from commit 96a7ea29e3cb61b6c2302e260e8e6a6117e17fa3)
b35c50
[rharwood: backport around our nx]
b35c50
---
b35c50
 grub-core/kern/efi/mm.c | 21 +++++++++++++++------
b35c50
 1 file changed, 15 insertions(+), 6 deletions(-)
b35c50
b35c50
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
b35c50
index 782a1365a1..a1d3b51fe6 100644
b35c50
--- a/grub-core/kern/efi/mm.c
b35c50
+++ b/grub-core/kern/efi/mm.c
b35c50
@@ -667,8 +667,8 @@ grub_nx_init (void)
b35c50
     }
b35c50
 }
b35c50
 
b35c50
-void
b35c50
-grub_efi_mm_init (void)
b35c50
+static grub_err_t
b35c50
+grub_efi_mm_add_regions (grub_size_t required_bytes)
b35c50
 {
b35c50
   grub_efi_memory_descriptor_t *memory_map;
b35c50
   grub_efi_memory_descriptor_t *memory_map_end;
b35c50
@@ -683,7 +683,7 @@ grub_efi_mm_init (void)
b35c50
   /* Prepare a memory region to store two memory maps.  */
b35c50
   memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
b35c50
   if (! memory_map)
b35c50
-    grub_fatal ("cannot allocate memory");
b35c50
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for memory map");
b35c50
 
b35c50
   /* Obtain descriptors for available memory.  */
b35c50
   map_size = MEMORY_MAP_SIZE;
b35c50
@@ -701,14 +701,14 @@ grub_efi_mm_init (void)
b35c50
 
b35c50
       memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (map_size));
b35c50
       if (! memory_map)
b35c50
-	grub_fatal ("cannot allocate memory");
b35c50
+	return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for new memory map");
b35c50
 
b35c50
       mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0,
b35c50
 					   &desc_size, 0);
b35c50
     }
b35c50
 
b35c50
   if (mm_status < 0)
b35c50
-    grub_fatal ("cannot get memory map");
b35c50
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "error fetching memory map from EFI");
b35c50
 
b35c50
   memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
b35c50
 
b35c50
@@ -723,7 +723,7 @@ grub_efi_mm_init (void)
b35c50
 
b35c50
   /* Allocate memory regions for GRUB's memory management.  */
b35c50
   add_memory_regions (filtered_memory_map, desc_size,
b35c50
-		      filtered_memory_map_end, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
b35c50
+		      filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
b35c50
 
b35c50
 #if 0
b35c50
   /* For debug.  */
b35c50
@@ -741,6 +741,15 @@ grub_efi_mm_init (void)
b35c50
   /* Release the memory maps.  */
b35c50
   grub_efi_free_pages ((grub_addr_t) memory_map,
b35c50
 		       2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
b35c50
+
b35c50
+  return GRUB_ERR_NONE;
b35c50
+}
b35c50
+
b35c50
+void
b35c50
+grub_efi_mm_init (void)
b35c50
+{
b35c50
+  if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE)
b35c50
+    grub_fatal ("%s", grub_errmsg);
b35c50
 }
b35c50
 
b35c50
 #if defined (__aarch64__) || defined (__arm__) || defined (__riscv)