grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: "Vladimir 'phcoder' Serbinenko" <phcoder@gmail.com>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: [PATCH] relocator: Switch to own page table while moving chunks on x86-64
Date: Sun, 8 Oct 2023 16:47:13 +0200	[thread overview]
Message-ID: <CAEaD8JPf8Ka1ys+6CrJifxJNtbkOFVnTpenNrtBj3=weRRoYDA@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #1.2: Type: text/html, Size: 23 bytes --]

[-- Attachment #2: 0001-relocator-Switch-to-own-page-table-while-moving-chun.patch --]
[-- Type: text/x-diff, Size: 6995 bytes --]

From a6ad0d064389b80714128e662e4f447a860bb117 Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phcoder@gmail.com>
Date: Tue, 11 Jul 2023 04:58:23 +0200
Subject: [PATCH] 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

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 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(-)

diff --git a/grub-core/lib/i386/relocator_common_c.c b/grub-core/lib/i386/relocator_common_c.c
index 7be609b73..aa24b8abf 100644
--- a/grub-core/lib/i386/relocator_common_c.c
+++ b/grub-core/lib/i386/relocator_common_c.c
@@ -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
diff --git a/grub-core/lib/mips/relocator.c b/grub-core/lib/mips/relocator.c
index 773f3b769..76c3c3bf2 100644
--- a/grub-core/lib/mips/relocator.c
+++ b/grub-core/lib/mips/relocator.c
@@ -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)
+{
+}
+
 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..70dbcb3f1 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)
+{
+}
+
 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 e0478ae5b..19a702172 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;
@@ -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",
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.42.0


[-- Attachment #3: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

                 reply	other threads:[~2023-10-08 14:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEaD8JPf8Ka1ys+6CrJifxJNtbkOFVnTpenNrtBj3=weRRoYDA@mail.gmail.com' \
    --to=phcoder@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).