fd0330
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
fd0330
From: Daniel Axtens <dja@axtens.net>
fd0330
Date: Thu, 21 Apr 2022 15:24:16 +1000
fd0330
Subject: [PATCH] mm: Debug support for region operations
fd0330
fd0330
This is handy for debugging. Enable with "set debug=regions".
fd0330
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 8afa5ef45b797ba5d8147ceee85ac2c59dcc7f09)
fd0330
---
fd0330
 grub-core/kern/mm.c | 19 ++++++++++++++++---
fd0330
 1 file changed, 16 insertions(+), 3 deletions(-)
fd0330
fd0330
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
fd0330
index 7be33e23bf..38bfb01df9 100644
fd0330
--- a/grub-core/kern/mm.c
fd0330
+++ b/grub-core/kern/mm.c
fd0330
@@ -115,9 +115,8 @@ grub_mm_init_region (void *addr, grub_size_t size)
fd0330
   grub_mm_header_t h;
fd0330
   grub_mm_region_t r, *p, q;
fd0330
 
fd0330
-#if 0
fd0330
-  grub_printf ("Using memory for heap: start=%p, end=%p\n", addr, addr + (unsigned int) size);
fd0330
-#endif
fd0330
+  grub_dprintf ("regions", "Using memory for heap: start=%p, end=%p\n",
fd0330
+                addr, (char *) addr + (unsigned int) size);
fd0330
 
fd0330
   /* Exclude last 4K to avoid overflows. */
fd0330
   /* If addr + 0x1000 overflows then whole region is in excluded zone.  */
fd0330
@@ -142,8 +141,14 @@ grub_mm_init_region (void *addr, grub_size_t size)
fd0330
        * addr                          q
fd0330
        *   |----size-----|-q->pre_size-|<q region>|
fd0330
        */
fd0330
+      grub_dprintf ("regions", "Can we extend into region above?"
fd0330
+		    " %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " ?=? %p\n",
fd0330
+		    (grub_uint8_t *) addr, size, q->pre_size, (grub_uint8_t *) q);
fd0330
       if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
fd0330
         {
fd0330
+	  grub_dprintf ("regions", "Yes: extending a region: (%p -> %p) -> (%p -> %p)\n",
fd0330
+			q, (grub_uint8_t *) q + sizeof (*q) + q->size,
fd0330
+			addr, (grub_uint8_t *) q + sizeof (*q) + q->size);
fd0330
           /*
fd0330
            * Yes, we can merge the memory starting at addr into the
fd0330
            * existing region from below. Align up addr to GRUB_MM_ALIGN
fd0330
@@ -185,9 +190,15 @@ grub_mm_init_region (void *addr, grub_size_t size)
fd0330
        *   q                       addr
fd0330
        *   |<q region>|-q->post_size-|----size-----|
fd0330
        */
fd0330
+      grub_dprintf ("regions", "Can we extend into region below?"
fd0330
+                    " %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " ?=? %p\n",
fd0330
+                    (grub_uint8_t *) q, sizeof(*q), q->size, q->post_size, (grub_uint8_t *) addr);
fd0330
       if ((grub_uint8_t *) q + sizeof (*q) + q->size + q->post_size ==
fd0330
 	  (grub_uint8_t *) addr)
fd0330
 	{
fd0330
+	  grub_dprintf ("regions", "Yes: extending a region: (%p -> %p) -> (%p -> %p)\n",
fd0330
+			q, (grub_uint8_t *) q + sizeof (*q) + q->size,
fd0330
+			q, (grub_uint8_t *) addr + size);
fd0330
 	  /*
fd0330
 	   * Yes! Follow a similar pattern to above, but simpler.
fd0330
 	   * Our header starts at address - post_size, which should align us
fd0330
@@ -213,6 +224,8 @@ grub_mm_init_region (void *addr, grub_size_t size)
fd0330
 	}
fd0330
     }
fd0330
 
fd0330
+  grub_dprintf ("regions", "No: considering a new region at %p of size %" PRIxGRUB_SIZE "\n",
fd0330
+		addr, size);
fd0330
   /* Allocate a region from the head.  */
fd0330
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
fd0330