On Fri, May 17, 2024 at 10:46:28AM +0300, 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 <phco...@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;

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

> +  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...

> +      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;

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 ")")...

> +}
> +
> +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...

> +}
> +
> +#else
> +void
> +grub_cpu_relocator_preamble (void *rels __attribute__((unused)))
> +{
> +}
> +#endif
> +
> +

Please drop redundant empty line...

>  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..1c27b6c99 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;
> +

Ditto.

Daniel

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

Reply via email to