Skip to contentSkip to conversation

relocator: Switch to own page table while moving chunks on x86-64

PATCH In review Vladimir Serbinenko 2 participants 2 messages May 17, 2024 – Aug 11, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 relocator: Switch to own page table while moving chunks on x86-64

We need to avoid clobering existing table between starting of chunk movers and the moment we install target page table. Generate temporary table for this rather than hoping that we don't clober existing one.

Fixes 64-bit GhostBSD on 64-bit EFI

---
 grub-core/lib/i386/relocator_common_c.c | 86 ++++++++++++++++++++++++-
 grub-core/lib/mips/relocator.c          |  7 ++
 grub-core/lib/powerpc/relocator.c       |  6 ++
 grub-core/lib/relocator.c               |  5 +-
 include/grub/relocator_private.h        |  2 +
 5 files changed, 104 insertions(+), 2 deletions(-)
grub-core/lib/i386/relocator_common_c.c +85 -1
index 7be609b73..aa24b8abf 100644
@@ -41,20 +41,104 @@ extern grub_size_t grub_relocator_forward_chunk_size;
 
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
-grub_size_t grub_relocator_align = 1;
 grub_size_t grub_relocator_forward_size;
 grub_size_t grub_relocator_backward_size;
+grub_size_t grub_relocator_preamble_size = 0;
 #ifdef __x86_64__
+grub_size_t grub_relocator_align = 4096;
 grub_size_t grub_relocator_jumper_size = 12;
 #else
+grub_size_t grub_relocator_align = 1;
 grub_size_t grub_relocator_jumper_size = 7;
 #endif
 
+#ifdef __x86_64__
+static grub_uint64_t max_ram_size;
+
+  /* Helper for grub_get_multiboot_mmap_count.  */
+static int
+max_hook (grub_uint64_t addr,
+	  grub_uint64_t size,
+	  grub_memory_type_t type __attribute__ ((unused)),
+	  void *data __attribute__ ((unused)))
+{
+  if (addr + size > max_ram_size)
+    max_ram_size = addr + size;
+  return 0;
+}
+
+static grub_uint64_t
+find_max_size (void)
+{
+  if (!max_ram_size)
+    {
+      max_ram_size = 1ULL << 32;
+
+      grub_mmap_iterate (max_hook, NULL);
+    }
+
+  return max_ram_size;
+}
+
+void
+grub_cpu_relocator_preamble (void *rels)
+{
+  grub_uint64_t nentries = (find_max_size () + 0x1fffff) >> 21;
+  grub_uint64_t npt2pages = (nentries + 0x1ff) >> 9;
+  grub_uint64_t npt3pages = (npt2pages + 0x1ff) >> 9;
+  grub_uint8_t *p = rels;
+  grub_uint64_t *pt4 = (grub_uint64_t *) (p + 0x1000);
+  grub_uint64_t *pt3 = pt4 + 0x200;
+  grub_uint64_t *pt2 = pt3 + (npt3pages << 9);
+  grub_uint64_t *endpreamble = pt2 + (npt2pages << 9);
+  grub_uint64_t i;
+
+  *p++ = 0x48;
+  *p++ = 0xb8;
+  *(grub_uint64_t *)p = (grub_uint64_t)pt4;
+  p += 8;
+  *p++ = 0x0f;
+  *p++ = 0x22;
+  *p++ = 0xd8;
+
+  *p++ = 0xe9;
+  *(grub_uint32_t *)p = (grub_uint8_t *)endpreamble - p - 4;
+
+  for (i = 0; i < npt3pages; i++)
+    pt4[i] = ((grub_uint64_t)pt3 + (i << 12)) | 7;
+
+  for (i = 0; i < npt2pages; i++)
+    pt3[i] = ((grub_uint64_t)pt2 + (i << 12)) | 7;
+
+  for (i = 0; i < (npt2pages << 9); i++)
+    pt2[i] = (i << 21) | 0x87;
+}
+
+static void
+compute_preamble_size (void)
+{
+  grub_uint64_t nentries = (find_max_size () + 0x1fffff) >> 21;
+  grub_uint64_t npt2pages = (nentries + 0x1ff) >> 9;
+  grub_uint64_t npt3pages = (npt2pages + 0x1ff) >> 9;
+  grub_relocator_preamble_size = (npt2pages + npt3pages + 1 + 1) << 12;
+}
+
+#else
+void
+grub_cpu_relocator_preamble (void *rels __attribute__((unused)))
+{
+}
+#endif
+
+
 void
 grub_cpu_relocator_init (void)
 {
   grub_relocator_forward_size = RELOCATOR_SIZEOF (_forward);
   grub_relocator_backward_size = RELOCATOR_SIZEOF (_backward);
+#ifdef __x86_64__
+  compute_preamble_size ();
+#endif
 }
 
 void
grub-core/lib/mips/relocator.c +7 -0
index 773f3b769..1c27b6c99 100644
@@ -45,6 +45,8 @@ grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
 grub_size_t grub_relocator_forward_size;
 grub_size_t grub_relocator_backward_size;
 grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
+grub_size_t grub_relocator_preamble_size = 0;
+
 
 void
 grub_cpu_relocator_init (void)
@@ -53,6 +55,11 @@ grub_cpu_relocator_init (void)
   grub_relocator_backward_size = RELOCATOR_SIZEOF(backward);
 }
 
+void
+grub_cpu_relocator_preamble (void *rels __attribute__((unused)))
+{
+}
+
 static void
 write_reg (int regn, grub_uint32_t val, void **target)
 {
grub-core/lib/powerpc/relocator.c +6 -0
index 15aeb0246..559e04b5a 100644
@@ -43,6 +43,7 @@ grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
 grub_size_t grub_relocator_forward_size;
 grub_size_t grub_relocator_backward_size;
 grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
+grub_size_t grub_relocator_preamble_size = 0;
 
 void
 grub_cpu_relocator_init (void)
@@ -51,6 +52,11 @@ grub_cpu_relocator_init (void)
   grub_relocator_backward_size = RELOCATOR_SIZEOF(backward);
 }
 
+void
+grub_cpu_relocator_preamble (void *rels __attribute__((unused)))
+{
+}
+
 static void
 write_reg (int regn, grub_uint32_t val, void **target)
 {
grub-core/lib/relocator.c +4 -1
index e0478ae5b..19a702172 100644
@@ -110,7 +110,7 @@ grub_relocator_new (void)
     return NULL;
 
   ret->postchunks = ~(grub_phys_addr_t) 0;
-  ret->relocators_size = grub_relocator_jumper_size;
+  ret->relocators_size = grub_relocator_jumper_size + grub_relocator_preamble_size;
   grub_dprintf ("relocator", "relocators_size=%lu\n",
 		(unsigned long) ret->relocators_size);
   return ret;
@@ -1601,6 +1601,9 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr,
     grub_free (to);
   }
 
+  grub_cpu_relocator_preamble (rels);
+  rels += grub_relocator_preamble_size;
+  
   for (j = 0; j < nchunks; j++)
     {
       grub_dprintf ("relocator", "sorted chunk %p->%p, 0x%lx\n",
include/grub/relocator_private.h +2 -0
index d8e972e01..273add76d 100644
@@ -27,6 +27,7 @@ extern grub_size_t grub_relocator_align;
 extern grub_size_t grub_relocator_forward_size;
 extern grub_size_t grub_relocator_backward_size;
 extern grub_size_t grub_relocator_jumper_size;
+extern grub_size_t grub_relocator_preamble_size;
 
 void
 grub_cpu_relocator_init (void);
@@ -39,6 +40,7 @@ void grub_cpu_relocator_forward (void *rels, void *src, void *tgt,
 void grub_cpu_relocator_backward (void *rels, void *src, void *tgt,
 				 grub_size_t size);
 void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr);
+void grub_cpu_relocator_preamble (void *rels);
 
 /* Remark: GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG = 1 or 2
    aren't supported.  */
Signature · 7 lines
2.39.2


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

We need to avoid clobering existing table between starting of chunk movers and the moment we install target page table. Generate temporary table for this rather than hoping that we don't clober existing one.

Fixes 64-bit GhostBSD on 64-bit EFI

---
 grub-core/lib/i386/relocator_common_c.c | 86 ++++++++++++++++++++++++-
 grub-core/lib/mips/relocator.c          |  7 ++
 grub-core/lib/powerpc/relocator.c       |  6 ++
 grub-core/lib/relocator.c               |  5 +-
 include/grub/relocator_private.h        |  2 +
 5 files changed, 104 insertions(+), 2 deletions(-)
grub-core/lib/i386/relocator_common_c.c +5 -1
index 7be609b73..aa24b8abf 100644
@@ -41,20 +41,104 @@ extern grub_size_t grub_relocator_forward_chunk_size;
 
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
-grub_size_t grub_relocator_align = 1;
 grub_size_t grub_relocator_forward_size;
 grub_size_t grub_relocator_backward_size;
+grub_size_t grub_relocator_preamble_size = 0;
 #ifdef __x86_64__
+grub_size_t grub_relocator_align = 4096;
 grub_size_t grub_relocator_jumper_size = 12;
 #else
+grub_size_t grub_relocator_align = 1;
 grub_size_t grub_relocator_jumper_size = 7;
 #endif
 
+#ifdef __x86_64__
+static grub_uint64_t max_ram_size;

grub-core/lib/i386/relocator_common_c.c extern grub_size_t grub_relocator_forward_chunk_size; · line 66

Quoted 10 lines
grub-core/lib/i386/relocator_common_c.c +10 -0
+
+  /* Helper for grub_get_multiboot_mmap_count.  */
+static int
+max_hook (grub_uint64_t addr,
+	  grub_uint64_t size,
+	  grub_memory_type_t type __attribute__ ((unused)),
+	  void *data __attribute__ ((unused)))
+{
+  if (addr + size > max_ram_size)
+    max_ram_size = addr + size;

Why not "max_ram_size = grub_max (max_ram_size, addr + size)"?

grub-core/lib/i386/relocator_common_c.c extern grub_size_t grub_relocator_forward_chunk_size; · line 75

Quoted 9 lines
grub-core/lib/i386/relocator_common_c.c +9 -0
+  return 0;
+}
+
+static grub_uint64_t
+find_max_size (void)
+{
+  if (!max_ram_size)
+    {
+      max_ram_size = 1ULL << 32;

I think this value should be explained in the comment...

Quoted 28 lines
grub-core/lib/i386/relocator_common_c.c +28 -0
+      grub_mmap_iterate (max_hook, NULL);
+    }
+
+  return max_ram_size;
+}
+
+void
+grub_cpu_relocator_preamble (void *rels)
+{
+  grub_uint64_t nentries = (find_max_size () + 0x1fffff) >> 21;
+  grub_uint64_t npt2pages = (nentries + 0x1ff) >> 9;
+  grub_uint64_t npt3pages = (npt2pages + 0x1ff) >> 9;
+  grub_uint8_t *p = rels;
+  grub_uint64_t *pt4 = (grub_uint64_t *) (p + 0x1000);
+  grub_uint64_t *pt3 = pt4 + 0x200;
+  grub_uint64_t *pt2 = pt3 + (npt3pages << 9);
+  grub_uint64_t *endpreamble = pt2 + (npt2pages << 9);
+  grub_uint64_t i;
+
+  *p++ = 0x48;
+  *p++ = 0xb8;
+  *(grub_uint64_t *)p = (grub_uint64_t)pt4;
+  p += 8;
+  *p++ = 0x0f;
+  *p++ = 0x22;
+  *p++ = 0xd8;
+
+  *p++ = 0xe9;

grub-core/lib/i386/relocator_common_c.c extern grub_size_t grub_relocator_forward_chunk_size; · line 114

Quoted 10 lines
grub-core/lib/i386/relocator_common_c.c +10 -0
+  *(grub_uint32_t *)p = (grub_uint8_t *)endpreamble - p - 4;
+
+  for (i = 0; i < npt3pages; i++)
+    pt4[i] = ((grub_uint64_t)pt3 + (i << 12)) | 7;
+
+  for (i = 0; i < npt2pages; i++)
+    pt3[i] = ((grub_uint64_t)pt2 + (i << 12)) | 7;
+
+  for (i = 0; i < (npt2pages << 9); i++)
+    pt2[i] = (i << 21) | 0x87;

All these numbers in this function should be explained and/or defined as constants.

And please fix cast formatting here and there (missing spaces after ")")...

grub-core/lib/i386/relocator_common_c.c extern grub_size_t grub_relocator_forward_chunk_size; · line 123

Quoted 9 lines
grub-core/lib/i386/relocator_common_c.c +9 -0
+}
+
+static void
+compute_preamble_size (void)
+{
+  grub_uint64_t nentries = (find_max_size () + 0x1fffff) >> 21;
+  grub_uint64_t npt2pages = (nentries + 0x1ff) >> 9;
+  grub_uint64_t npt3pages = (npt2pages + 0x1ff) >> 9;
+  grub_relocator_preamble_size = (npt2pages + npt3pages + 1 + 1) << 12;

Ditto...

grub-core/lib/i386/relocator_common_c.c extern grub_size_t grub_relocator_forward_chunk_size; · line 133

Quoted 10 lines
grub-core/lib/i386/relocator_common_c.c +10 -0
+}
+
+#else
+void
+grub_cpu_relocator_preamble (void *rels __attribute__((unused)))
+{
+}
+#endif
+
+

Please drop redundant empty line...

Quoted 16 lines
grub-core/lib/i386/relocator_common_c.c +3 -0
 void
 grub_cpu_relocator_init (void)
 {
   grub_relocator_forward_size = RELOCATOR_SIZEOF (_forward);
   grub_relocator_backward_size = RELOCATOR_SIZEOF (_backward);
+#ifdef __x86_64__
+  compute_preamble_size ();
+#endif
 }
 
 void
grub-core/lib/mips/relocator.c +0 -0
index 773f3b769..1c27b6c99 100644

grub-core/lib/mips/relocator.c grub_size_t grub_relocator_align = sizeof (grub_uint32_t); · line 49

Quoted 5 lines
grub-core/lib/mips/relocator.c +2 -0
@@ -45,6 +45,8 @@ grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
 grub_size_t grub_relocator_forward_size;
 grub_size_t grub_relocator_backward_size;
 grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
+grub_size_t grub_relocator_preamble_size = 0;
+

Ditto.

Daniel

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