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

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