* [PATCH v2 0/1] relocator: Switch to own page table while moving chunks
@ 2025-08-11 18:02 Vladimir Serbinenko
2025-08-11 18:02 ` [PATCH v2] " Vladimir Serbinenko
0 siblings, 1 reply; 3+ messages in thread
From: Vladimir Serbinenko @ 2025-08-11 18:02 UTC (permalink / raw)
To: grub-devel
Differences from previous version:
* Replaced litterals with defines
* Added comments
* removed empty lines
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2] relocator: Switch to own page table while moving chunks
2025-08-11 18:02 [PATCH v2 0/1] relocator: Switch to own page table while moving chunks Vladimir Serbinenko
@ 2025-08-11 18:02 ` Vladimir Serbinenko
2025-08-12 12:24 ` Daniel Kiper
0 siblings, 1 reply; 3+ messages in thread
From: Vladimir Serbinenko @ 2025-08-11 18:02 UTC (permalink / raw)
To: grub-devel; +Cc: 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
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
grub-core/lib/i386/relocator_common_c.c | 103 +++++++++++++++++++++++-
grub-core/lib/mips/relocator.c | 6 ++
grub-core/lib/powerpc/relocator.c | 6 ++
grub-core/lib/relocator.c | 5 +-
include/grub/relocator_private.h | 2 +
5 files changed, 120 insertions(+), 2 deletions(-)
diff --git a/grub-core/lib/i386/relocator_common_c.c b/grub-core/lib/i386/relocator_common_c.c
index 7be609b73..598a108a9 100644
--- a/grub-core/lib/i386/relocator_common_c.c
+++ b/grub-core/lib/i386/relocator_common_c.c
@@ -26,6 +26,8 @@
#include <grub/relocator.h>
#include <grub/relocator_private.h>
+#include <grub/i386/memory.h>
+
extern grub_uint8_t grub_relocator_forward_start;
extern grub_uint8_t grub_relocator_forward_end;
extern grub_uint8_t grub_relocator_backward_start;
@@ -41,20 +43,119 @@ 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__
+
+#define PAGE_PRESENT 1
+#define PAGE_WRITABLE 2
+#define PAGE_USER 4
+#define PAGE_PS 0x80
+#define PAGE_IDX_SIZE 9
+#define PAGE_PS_SHIFT 21
+#define PAGE_NUM_ENTRIES 0x200
+#define PS_PAGE_SIZE 0x200000
+
+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)))
+{
+ max_ram_size = grub_max (max_ram_size, addr + size);
+ return 0;
+}
+
+static grub_uint64_t
+find_max_size (void)
+{
+ if (!max_ram_size)
+ {
+ /* We need to map the first 4GiB of address space as well as all the
+ available RAM, so start with 4GiB and increase if we see any RAM
+ above this. */
+ 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 () + PS_PAGE_SIZE - 1) >> PAGE_PS_SHIFT;
+ grub_uint64_t npt2pages = (nentries + PAGE_NUM_ENTRIES - 1) >> PAGE_IDX_SIZE;
+ grub_uint64_t npt3pages = (npt2pages + PAGE_NUM_ENTRIES - 1) >> PAGE_IDX_SIZE;
+ grub_uint8_t *p = rels;
+ grub_uint64_t *pt4 = (grub_uint64_t *) (p + GRUB_PAGE_SIZE);
+ grub_uint64_t *pt3 = pt4 + PAGE_NUM_ENTRIES;
+ grub_uint64_t *pt2 = pt3 + (npt3pages << PAGE_IDX_SIZE);
+ grub_uint64_t *endpreamble = pt2 + (npt2pages << PAGE_IDX_SIZE);
+ grub_uint64_t i;
+
+ /* movabs $pt4, %rax. */
+ *p++ = 0x48;
+ *p++ = 0xb8;
+ *(grub_uint64_t *) p = (grub_uint64_t) pt4;
+ p += 8;
+
+ /* mov %rax, %cr3. */
+ *p++ = 0x0f;
+ *p++ = 0x22;
+ *p++ = 0xd8;
+
+ /* jmp $endpreamble. */
+ *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 << GRUB_PAGE_SHIFT)) | PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER;
+
+ for (i = 0; i < npt2pages; i++)
+ pt3[i] = ((grub_uint64_t) pt2 + (i << GRUB_PAGE_SHIFT)) | PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER;
+
+ for (i = 0; i < (npt2pages << PAGE_IDX_SIZE); i++)
+ pt2[i] = (i << PAGE_PS_SHIFT) | PAGE_PS | PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER;
+}
+
+static void
+compute_preamble_size (void)
+{
+ grub_uint64_t nentries = (find_max_size () + PS_PAGE_SIZE - 1) >> PAGE_PS_SHIFT;
+ grub_uint64_t npt2pages = (nentries + PAGE_NUM_ENTRIES - 1) >> PAGE_IDX_SIZE;
+ grub_uint64_t npt3pages = (npt2pages + PAGE_NUM_ENTRIES - 1) >> PAGE_IDX_SIZE;
+ grub_relocator_preamble_size = (npt2pages + npt3pages + 1 + 1) << GRUB_PAGE_SHIFT;
+}
+
+#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
diff --git a/grub-core/lib/mips/relocator.c b/grub-core/lib/mips/relocator.c
index 773f3b769..9f79b40c8 100644
--- a/grub-core/lib/mips/relocator.c
+++ b/grub-core/lib/mips/relocator.c
@@ -45,6 +45,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)
@@ -53,6 +54,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)
{
diff --git a/grub-core/lib/powerpc/relocator.c b/grub-core/lib/powerpc/relocator.c
index 15aeb0246..559e04b5a 100644
--- a/grub-core/lib/powerpc/relocator.c
+++ b/grub-core/lib/powerpc/relocator.c
@@ -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)
{
diff --git a/grub-core/lib/relocator.c b/grub-core/lib/relocator.c
index 3306a1bb7..e3c9696f1 100644
--- a/grub-core/lib/relocator.c
+++ b/grub-core/lib/relocator.c
@@ -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;
@@ -1605,6 +1605,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",
diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h
index d8e972e01..273add76d 100644
--- a/include/grub/relocator_private.h
+++ b/include/grub/relocator_private.h
@@ -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. */
--
2.49.0
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] relocator: Switch to own page table while moving chunks
2025-08-11 18:02 ` [PATCH v2] " Vladimir Serbinenko
@ 2025-08-12 12:24 ` Daniel Kiper
0 siblings, 0 replies; 3+ messages in thread
From: Daniel Kiper @ 2025-08-12 12:24 UTC (permalink / raw)
To: Vladimir Serbinenko; +Cc: grub-devel
On Mon, Aug 11, 2025 at 06:02:54PM +0000, Vladimir Serbinenko wrote:
> 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
>
> Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Daniel
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-08-12 12:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-11 18:02 [PATCH v2 0/1] relocator: Switch to own page table while moving chunks Vladimir Serbinenko
2025-08-11 18:02 ` [PATCH v2] " Vladimir Serbinenko
2025-08-12 12:24 ` Daniel Kiper
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.