Skip to contentSkip to conversation

ppc64: Restrict memory allocations for kernel and initrd

PATCH Superseded Stefan Berger 3 participants 16 messages Nov 27, 2023 – Nov 30, 2023

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v7 cover ppc64: Restrict memory allocations for kernel and initrd

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs and i386.

Regards,
Stefan

v7:
- 1/10: Move header file to include/grub/ieee1275/alloc.h for ieee1275/i386
- 6/10: Make new #defines available for ieee1275/i386 but set KVM on Power flag only if __powerpc__ is defined

v6:
- 7/10: Report OOM error if initrd could not be loaded

v5:
- Replaced grub with GRUB in commit messages
- Fixed copyright holders in alloc.h

v4:
- Fixed typos in commit messages found by Hari
- Exporting grub_regions_claim() as suggested by Hari

v3:
- Addressed issues raised by Daniel and added his R-B tags
- Added one comment fix patch and one for debugging output
- Removed len return parameter from request struct since unused

Stefan Berger (10):
kern/ieee1275/init: ppc64: Introduce a request for regions_claim kern/ieee1275/init: ppc64: Decide by request whether to initialize
region
kern/ieee1275/init: ppc64: Return allocated address using context kern/ieee1275/init: ppc64: Add support for alignment requirements kern/ieee1275/init: ppc64: Rename regions_claim to grub_regions_claim kern/ieee1275/cmain: ppc64: Introduce flags to identify KVM and Power
VM
loader/powerpc/ieee1275: Use new allocation function for kernel and
initrd
kern/ieee1275/ieee1275: debug: Display successful memory claims kern/ieee1275/init: ppc64: Fix a comment kern/ieee1275/init: ppc64: Display upper_mem_limit for debugging

 grub-core/Makefile.am                     |  2 +
 grub-core/kern/ieee1275/cmain.c           |  8 ++-
 grub-core/kern/ieee1275/ieee1275.c        |  2 +
 grub-core/kern/ieee1275/init.c            | 73 ++++++++++++++++++-----
 grub-core/loader/powerpc/ieee1275/linux.c | 55 ++++++++++++++---
 include/grub/ieee1275/alloc.h             | 39 ++++++++++++
 include/grub/ieee1275/ieee1275.h          |  4 ++
 7 files changed, 159 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/ieee1275/alloc.h
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v7 1/10 kern/ieee1275/init: ppc64: Introduce a request for regions_claim

The regions_claim() function limits the allocation of memory regions by excluding certain memory areas from being used by GRUB. This for example includes a gap between 640MB and 768MB as well as an upper limit beyond which no memory may be used when an fadump is present. However, the ieee1275 loader for kernel and initrd currently does not use regions_claim() for memory allocation on PowerVM and KVM on Power and therefore may allocate memory in those areas that it should not use.

To make the regions_claim() function more flexible and ulimately usable for the ieee1275 loader, introduce a request structure to pass various parameters to the regions_claim() function that describe the properties of requested memory chunks. In a first step, move the total and flags variables into this structure.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/Makefile.am          |  2 ++
 grub-core/kern/ieee1275/init.c | 43 ++++++++++++++++++++++++++--------
 include/grub/ieee1275/alloc.h  | 31 ++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 10 deletions(-)
 create mode 100644 include/grub/ieee1275/alloc.h
grub-core/Makefile.am +2 -0
index f0cb2f2cc..fc9fc6a90 100644
@@ -153,6 +153,7 @@ endif
 if COND_i386_ieee1275
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
@@ -240,6 +241,7 @@ endif
 
 if COND_powerpc_ieee1275
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
grub-core/kern/ieee1275/init.c +33 -10

85 lines not on this page · raw message

include/grub/ieee1275/alloc.h new file +31 -0
new file mode 100644
index 000000000..12fade5e4
@@ -0,0 +1,31 @@
+/* alloc.h - Memory allocation for PowerVM, KVM on Power, and i386 */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023  Free Software Foundation, Inc.
+ *  Copyright (C) 2023  IBM Corporation
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_IEEE1275_ALLOC_HEADER
+#define GRUB_IEEE1275_ALLOC_HEADER	1
+
+#include <grub/memory.h>
+
+struct regions_claim_request {
+  unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
+  grub_uint32_t total;    /* number of requested bytes */
+};
+
+#endif /* GRUB_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH v7 2/10 kern/ieee1275/init: ppc64: Decide by request whether to initialize region

In reply to Stefan Berger

Let the regions_claim() request structure's init_region determine whether to call grub_mm_init_region() on it. This allows for adding memory to GRUB's memory heap if init_region is set to true, or direct usage of the memory otherwise. Set all current callers' init_region to true since they want to add memory regions to GRUB's heap.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c | 5 ++++-
 include/grub/ieee1275/alloc.h  | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/init.c +4 -1
index b65fc9be5..8e69da297 100644
@@ -515,7 +515,8 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
       err = grub_claimmap (addr, len);
       if (err)
 	return err;
-      grub_mm_init_region ((void *) (grub_addr_t) addr, len);
+      if (rcr->init_region)
+          grub_mm_init_region ((void *) (grub_addr_t) addr, len);
       rcr->total -= len;
     }
 
@@ -534,6 +535,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   struct regions_claim_request rcr = {
     .flags = GRUB_MM_ADD_REGION_NONE,
     .total = *(grub_uint32_t *) data,
+    .init_region = true,
   };
   int ret;
 
@@ -551,6 +553,7 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   struct regions_claim_request rcr = {
     .flags = GRUB_MM_ADD_REGION_CONSECUTIVE,
     .total = *(grub_uint32_t *) data,
+    .init_region = true,
   };
   int ret;
 
include/grub/ieee1275/alloc.h +3 -0
index 12fade5e4..523f51fc6 100644
@@ -21,11 +21,14 @@
 #ifndef GRUB_IEEE1275_ALLOC_HEADER
 #define GRUB_IEEE1275_ALLOC_HEADER	1
 
+#include <stdbool.h>
+
 #include <grub/memory.h>
 
 struct regions_claim_request {
   unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
   grub_uint32_t total;    /* number of requested bytes */
+  bool init_region;       /* whether to add memory to the heap using grub_mm_init_region() */
 };
 
 #endif /* GRUB_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v7 3/10 kern/ieee1275/init: ppc64: Return allocated address using context

In reply to Stefan Berger

Return the allocated address of the memory block in the request structure if a memory allocation was actually done. Leave the address untouched otherwise. This enables a caller who wants to use the allocated memory directly (rather than adding the memory to the heap) to see where memory was allocated. None of the current callers need this but the converted ieee1275 loader will make use of it.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c | 2 ++
 include/grub/ieee1275/alloc.h  | 1 +
 2 files changed, 3 insertions(+)
grub-core/kern/ieee1275/init.c +2 -0
index 8e69da297..4b9face59 100644
@@ -518,6 +518,8 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
       if (rcr->init_region)
           grub_mm_init_region ((void *) (grub_addr_t) addr, len);
       rcr->total -= len;
+
+      rcr->addr = addr;
     }
 
   *(grub_uint32_t *) data = rcr->total;
include/grub/ieee1275/alloc.h +1 -0
index 523f51fc6..f3065ff31 100644
@@ -29,6 +29,7 @@ struct regions_claim_request {
   unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
   grub_uint32_t total;    /* number of requested bytes */
   bool init_region;       /* whether to add memory to the heap using grub_mm_init_region() */
+  grub_uint64_t addr;     /* result address */
 };
 
 #endif /* GRUB_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

PATCH v7 4/10 kern/ieee1275/init: ppc64: Add support for alignment requirements

In reply to Stefan Berger

Add support for memory alignment requirements and adjust a candidate address to it before checking whether the block is large enough. This must be done in this order since the alignment adjustment can make a block smaller than what was requested.

None of the current callers has memory alignment requirements but the ieee1275 loader for kernel and initrd will use it to convey them.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c | 14 ++++++++++++++
 include/grub/ieee1275/alloc.h  |  1 +
 2 files changed, 15 insertions(+)
grub-core/kern/ieee1275/init.c +14 -0
index 4b9face59..9a1243639 100644
@@ -502,6 +502,20 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
             }
         }
     }
+
+  /* Honor alignment restrictions on candidate addr */
+  if (rcr->align)
+    {
+      grub_uint64_t align_addr = ALIGN_UP (addr, rcr->align);
+      grub_uint64_t d = align_addr - addr;
+
+      if (d > len)
+        return 0;
+
+      len -= d;
+      addr = align_addr;
+    }
+
   if (rcr->flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < rcr->total)
     return 0;
 
include/grub/ieee1275/alloc.h +1 -0
index f3065ff31..e314c989d 100644
@@ -30,6 +30,7 @@ struct regions_claim_request {
   grub_uint32_t total;    /* number of requested bytes */
   bool init_region;       /* whether to add memory to the heap using grub_mm_init_region() */
   grub_uint64_t addr;     /* result address */
+  grub_size_t align;      /* alignment restrictions */
 };
 
 #endif /* GRUB_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

PATCH v7 5/10 kern/ieee1275/init: ppc64: Rename regions_claim to grub_regions_claim

In reply to Stefan Berger

Rename regions_claim() to grub_regions_claim() to make it available for memory allocation. The ieee1275 loader will use this function on PowerVM and KVM on Power and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c | 10 +++++-----
 include/grub/ieee1275/alloc.h  |  3 +++
 2 files changed, 8 insertions(+), 5 deletions(-)
grub-core/kern/ieee1275/init.c +5 -5
index 9a1243639..a92558e0e 100644
@@ -318,9 +318,9 @@ count_free (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   return 0;
 }
 
-static int
-regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
-	       void *data)
+int
+grub_regions_claim (grub_uint64_t addr, grub_uint64_t len,
+		    grub_memory_type_t type, void *data)
 {
   struct regions_claim_request *rcr = data;
   grub_uint64_t linux_rmo_save;
@@ -555,7 +555,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   };
   int ret;
 
-  ret = regions_claim (addr, len, type, &rcr);
+  ret = grub_regions_claim (addr, len, type, &rcr);
 
   *(grub_uint32_t *) data = rcr.total;
 
@@ -573,7 +573,7 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   };
   int ret;
 
-  ret = regions_claim (addr, len, type, &rcr);
+  ret = grub_regions_claim (addr, len, type, &rcr);
 
   *(grub_uint32_t *) data = rcr.total;
 
include/grub/ieee1275/alloc.h +3 -0
index e314c989d..67a785657 100644
@@ -33,4 +33,7 @@ struct regions_claim_request {
   grub_size_t align;      /* alignment restrictions */
 };
 
+int EXPORT_FUNC(grub_regions_claim) (grub_uint64_t addr, grub_uint64_t len,
+				     grub_memory_type_t type, void *data);
+
 #endif /* GRUB_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

PATCH v7 6/10 kern/ieee1275/cmain: ppc64: Introduce flags to identify KVM and Power VM

In reply to Stefan Berger

Introduce flags to identify PowerVM and KVM on Power and set them where each type of host has been detected.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/cmain.c  | 8 +++++++-
 include/grub/ieee1275/ieee1275.h | 4 ++++
 2 files changed, 11 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/cmain.c +7 -1
index 400d85d28..e74de3248 100644
@@ -126,7 +126,10 @@ grub_ieee1275_find_options (void)
 
 #if defined(__powerpc__)
       if (grub_strncmp (tmp, "IBM,", 4) == 0)
-	grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
+	{
+	  grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
+	  grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_POWER_VM);
+	}
 #endif
     }
 
@@ -193,6 +196,9 @@ grub_ieee1275_find_options (void)
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM);
 
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF);
+#if defined(__powerpc__)
+      grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_POWER_KVM);
+#endif
     }
 }
 
include/grub/ieee1275/ieee1275.h +4 -0
index 9f669fbee..dddb38514 100644
@@ -141,6 +141,10 @@ enum grub_ieee1275_flag
    */
   GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
 #endif
+
+  GRUB_IEEE1275_FLAG_POWER_VM,
+
+  GRUB_IEEE1275_FLAG_POWER_KVM,
 };
 
 extern int EXPORT_FUNC(grub_ieee1275_test_flag) (enum grub_ieee1275_flag flag);
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw

PATCH v7 7/10 loader/powerpc/ieee1275: Use new allocation function for kernel and initrd

In reply to Stefan Berger

On PowerVM and KVM on Power use the new memory allocation function that honors restrictions on which memory GRUB can actually use. In the request structure indicate the request for a single memory block along with address alignment restrictions. Request direct usage of the memory block by setting init_region to false (prevent it from being added to GRUB's heap). Initialize the found addr to '-1', so that '-1' will be returned to the loader in case no memory could be allocated.

Report an out-of-memory error in case the initrd could not be loaded.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/loader/powerpc/ieee1275/linux.c | 55 +++++++++++++++++++----
 1 file changed, 46 insertions(+), 9 deletions(-)
grub-core/loader/powerpc/ieee1275/linux.c +46 -9
index e6d071508..4864e5fb0 100644
@@ -30,6 +30,7 @@
 #include <grub/lib/cmdline.h>
 #include <grub/cache.h>
 #include <grub/linux.h>
+#include <grub/ieee1275/alloc.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -116,6 +117,22 @@ grub_linux_claimmap_iterate (grub_addr_t target, grub_size_t size,
   return ctx.found_addr;
 }
 
+static grub_addr_t
+grub_linux_claimmap_iterate_restricted (grub_size_t size, grub_size_t align)
+{
+  struct regions_claim_request rcr = {
+    .flags = GRUB_MM_ADD_REGION_CONSECUTIVE,
+    .total = size,
+    .init_region = false,
+    .addr = (grub_uint64_t) -1,
+    .align = align,
+  };
+
+  grub_machine_mmap_iterate (grub_regions_claim, &rcr);
+
+  return rcr.addr;
+}
+
 static grub_err_t
 grub_linux_boot (void)
 {
@@ -227,10 +244,18 @@ grub_linux_load64 (grub_elf_t elf, const char *filename)
   offset = entry - base_addr;
   /* Linux's incorrectly contains a virtual address.  */
 
-  /* On some systems, firmware occupies the memory we're trying to use.
-   * Happily, Linux can be loaded anywhere (it relocates itself).  Iterate
-   * until we find an open area.  */
-  seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF64_LOADMASK, linux_size, align);
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_VM) ||
+      grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_KVM))
+    {
+      seg_addr = grub_linux_claimmap_iterate_restricted (linux_size, align);
+    }
+  else
+    {
+      /* On some systems, firmware occupies the memory we're trying to use.
+       * Happily, Linux can be loaded anywhere (it relocates itself).  Iterate
+       * until we find an open area.  */
+      seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF64_LOADMASK, linux_size, align);
+    }
   if (seg_addr == (grub_addr_t) -1)
     return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
 
@@ -339,13 +364,25 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   size = grub_get_initrd_size (&initrd_ctx);
 
-  first_addr = linux_addr + linux_size;
 
-  /* Attempt to claim at a series of addresses until successful in
-     the same way that grub_rescue_cmd_linux does.  */
-  addr = grub_linux_claimmap_iterate (first_addr, size, 0x100000);
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_VM) ||
+      grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_KVM))
+    {
+      addr = grub_linux_claimmap_iterate_restricted (size, 0x100000);
+    }
+  else
+    {
+      /* Attempt to claim at a series of addresses until successful in
+         the same way that grub_rescue_cmd_linux does.  */
+      first_addr = linux_addr + linux_size;
+      addr = grub_linux_claimmap_iterate (first_addr, size, 0x100000);
+    }
+
   if (addr == (grub_addr_t) -1)
-     goto fail;
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+      goto fail;
+    }
 
   grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
 
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#8 lore raw

PATCH v7 8/10 kern/ieee1275/ieee1275: debug: Display successful memory claims

In reply to Stefan Berger

Display successful memory claims with exact address and rounded-down MiB location and rounded-up size in MiB.

Cc Eric Snowberg <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/ieee1275.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/kern/ieee1275/ieee1275.c +2 -0
index db870db47..fef3ccf1a 100644
@@ -590,6 +590,8 @@ grub_ieee1275_claim (grub_addr_t addr, grub_size_t size, unsigned int align,
     *result = args.base;
   if (args.base == IEEE1275_CELL_INVALID)
     return -1;
+  grub_dprintf ("mmap", "CLAIMED: 0x%x (%d MiB)  size: %d MiB\n",
+		args.base, args.base >> 20, ALIGN_UP(size, 1 << 20) >> 20);
   return 0;
 }
 
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#9 lore raw

PATCH v7 9/10 kern/ieee1275/init: ppc64: Fix a comment

In reply to Stefan Berger
---
 grub-core/kern/ieee1275/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/ieee1275/init.c +1 -1
index a92558e0e..0f420d5b4 100644
@@ -472,7 +472,7 @@ grub_regions_claim (grub_uint64_t addr, grub_uint64_t len,
               /* We must not exceed the upper_mem_limit (assuming it's >= RMO_ADDR_MAX) */
               if (addr + len > upper_mem_limit)
                 {
-                  /* take the bigger chunk from either below linux_rmo_save or above upper_mem_limit */
+                  /* take the bigger chunk from either below linux_rmo_save or above RMO_ADDR_MAX */
                   len = upper_mem_limit - addr;
                   if (orig_addr < linux_rmo_save && linux_rmo_save - orig_addr > len)
                     {
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#10 lore raw

PATCH v7 10/10 kern/ieee1275/init: ppc64: Display upper_mem_limit for debugging

In reply to Stefan Berger

Display upper_mem_limit and its rounded-down value in MiB.

---
 grub-core/kern/ieee1275/init.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/kern/ieee1275/init.c +3 -0
index 0f420d5b4..a3cb2a11a 100644
@@ -454,6 +454,9 @@ grub_regions_claim (grub_uint64_t addr, grub_uint64_t len,
 
           check_kernel_dump (&upper_mem_limit);
 
+          grub_dprintf ("ieee1275", "upper_mem_limit is at %llx (%lld MiB)\n",
+                        upper_mem_limit, upper_mem_limit >> 20);
+
           /*
            * we order these cases to prefer higher addresses and avoid some
            * splitting issues
Signature · 7 lines
2.42.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#11 lore raw
In reply to Stefan Berger

Hello Stefan!

Quoted 7 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

Is this regions_claims memory allocator available on all PowerPC targets or only on newer POWER machines?

Thanks,
Adrian

Signature · 9 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer
`. `'   Physicist
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#12 lore raw
Quoted 11 lines · John Paul Adrian Glaubitz

Hello Stefan!

Quoted 7 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

Quoted 2 lines

Is this regions_claims memory allocator available on all PowerPC targets or only on newer POWER machines?

It's not available for PowerMACs but for PowerVM and KVM on Power systems as well as i386/ieee1275.

Stefan

Quoted 4 lines

Thanks,
Adrian

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw
Quoted 14 lines · Stefan Berger
Quoted 11 lines · John Paul Adrian Glaubitz

Hello Stefan!

Quoted 7 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

Quoted 5 lines
Quoted 2 lines

Is this regions_claims memory allocator available on all PowerPC targets or only on newer POWER machines?

It's not available for PowerMACs but for PowerVM and KVM on Power systems as well as i386/ieee1275.

Actually, let me clarify. The regions_claims memory allocator is 'available' to all of them but only being used by PowerVM and KVM on Power. The 2nd paragraph states:

Quoted 9 lines

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs and i386.

   Stefan

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

Thanks,
Adrian

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#14 lore raw
In reply to Stefan Berger
Quoted 12 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs and i386.

Quoted 7 lines

Regards,
Stefan

v7:
- 1/10: Move header file to include/grub/ieee1275/alloc.h for ieee1275/i386
- 6/10: Make new #defines available for ieee1275/i386 but set KVM on Power flag only if __powerpc__ is defined

This is the diff between v6 and v7:

grub-core/Makefile.am +2 -1
index 4f9f3f963..fc9fc6a90 100644
@@ -153,6 +153,7 @@ endif
 if COND_i386_ieee1275
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
@@ -240,7 +241,7 @@ endif
 
 if COND_powerpc_ieee1275
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/alloc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
grub-core/kern/ieee1275/cmain.c +2 -0
index cc7c32873..e74de3248 100644
@@ -196,7 +196,9 @@ grub_ieee1275_find_options (void)
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM);
 
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF);
+#if defined(__powerpc__)
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_POWER_KVM);
+#endif
     }
 }
 
grub-core/kern/ieee1275/init.c +2 -2
index 72e0292a3..a3cb2a11a 100644
@@ -46,8 +46,8 @@
 #ifdef __sparc__
 #include <grub/machine/kernel.h>
 #endif
-#ifdef __powerpc__
-#include <grub/powerpc/ieee1275/alloc.h>
+#if defined(__powerpc__) || defined(__i386__)
+#include <grub/ieee1275/alloc.h>
 #endif
 
 /* The maximum heap size we're going to claim at boot. Not used by 

sparc. */

grub-core/loader/powerpc/ieee1275/linux.c +1 -1
index de174416d..4864e5fb0 100644
@@ -30,7 +30,7 @@
 #include <grub/lib/cmdline.h>
 #include <grub/cache.h>
 #include <grub/linux.h>
-#include <grub/powerpc/ieee1275/alloc.h>
+#include <grub/ieee1275/alloc.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
include/grub/ieee1275/alloc.h renamed from include/grub/powerpc/ieee1275/alloc.h +3 -3
similarity index 87%
rename from include/grub/powerpc/ieee1275/alloc.h
rename to include/grub/ieee1275/alloc.h
index c7fee55df..67a785657 100644
@@ -1,4 +1,4 @@
-/* alloc.h - Memory allocation for PowerVM/KVM */
+/* alloc.h - Memory allocation for PowerVM, KVM on Power, and i386 */
 /*
  *  GRUB  --  GRand Unified Bootloader
  *  Copyright (C) 2023  Free Software Foundation, Inc.
@@ -18,8 +18,8 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef GRUB_POWERPC_IEEE1275_ALLOC_HEADER
-#define GRUB_POWERPC_IEEE1275_ALLOC_HEADER     1
+#ifndef GRUB_IEEE1275_ALLOC_HEADER
+#define GRUB_IEEE1275_ALLOC_HEADER     1
 
 #include <stdbool.h>
 
@@ -36,4 +36,4 @@ struct regions_claim_request {
 int EXPORT_FUNC(grub_regions_claim) (grub_uint64_t addr, grub_uint64_t 

len,

                                     grub_memory_type_t type, void *data);

-#endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */
+#endif /* GRUB_IEEE1275_ALLOC_HEADER */

include/grub/ieee1275/ieee1275.h +1 -1
index 5b7a36ad8..dddb38514 100644
@@ -140,11 +140,11 @@ enum grub_ieee1275_flag
    * only 256MB in size, try asking for more with CAS.
    */
   GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
+#endif
 
   GRUB_IEEE1275_FLAG_POWER_VM,
 
   GRUB_IEEE1275_FLAG_POWER_KVM,
-#endif
 };
 
 extern int EXPORT_FUNC(grub_ieee1275_test_flag) (enum 

grub_ieee1275_flag flag);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#15 lore raw
In reply to Stefan Berger
Quoted 11 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs and i386.

This time the patch set breaks SPARC build... :-( I am attaching the patch which fixes it and then all builds pass. Please merge the fix into your patch set and resend it.

Daniel

grub-core/kern/ieee1275/ieee1275.c +3 -2
index fef3ccf1a..36ca2dbfc 100644
@@ -590,8 +590,9 @@ grub_ieee1275_claim (grub_addr_t addr, grub_size_t size, unsigned int align,
     *result = args.base;
   if (args.base == IEEE1275_CELL_INVALID)
     return -1;
-  grub_dprintf ("mmap", "CLAIMED: 0x%x (%d MiB)  size: %d MiB\n",
-		args.base, args.base >> 20, ALIGN_UP(size, 1 << 20) >> 20);
+  grub_dprintf ("mmap", "CLAIMED: 0x%" PRIxGRUB_IEEE1275_CELL_T " (%"
+		PRIuGRUB_IEEE1275_CELL_T " MiB)  size: %" PRIuGRUB_SIZE " MiB\n",
+		args.base, args.base >> 20, ALIGN_UP (size, 1 << 20) >> 20);
   return 0;
 }
 
include/grub/powerpc/ieee1275/ieee1275.h +3 -0
index 3c7683fad..4eb207018 100644
@@ -25,4 +25,7 @@
 #define GRUB_IEEE1275_CELL_SIZEOF 4
 typedef grub_uint32_t grub_ieee1275_cell_t;
 
+#define PRIxGRUB_IEEE1275_CELL_T	PRIxGRUB_UINT32_T
+#define PRIuGRUB_IEEE1275_CELL_T	PRIuGRUB_UINT32_T
+
 #endif /* ! GRUB_IEEE1275_MACHINE_HEADER */
include/grub/sparc64/ieee1275/ieee1275.h +3 -0
index 4b18468d8..ccc71aac6 100644
@@ -25,6 +25,9 @@
 #define GRUB_IEEE1275_CELL_SIZEOF 8
 typedef grub_uint64_t grub_ieee1275_cell_t;
 
+#define PRIxGRUB_IEEE1275_CELL_T	PRIxGRUB_UINT64_T
+#define PRIuGRUB_IEEE1275_CELL_T	PRIuGRUB_UINT64_T
+
 /* Encoding of 'mode' argument to grub_ieee1275_map_physical() */
 #define IEEE1275_MAP_WRITE	0x0001 /* Writable */
 #define IEEE1275_MAP_READ	0x0002 /* Readable */

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel