Skip to contentSkip to conversation

grub-xen: support booting huge pv-domains

PATCH Superseded Juergen Gross 3 participants 54 messages Feb 17, 2016 – Feb 19, 2016 Xen hypervisor interface

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

v3 cover grub-xen: support booting huge pv-domains

The Xen hypervisor supports starting a dom0 with large memory (up to the TB range) by not including the initrd and p2m list in the initial kernel mapping. Especially the p2m list can grow larger than the available virtual space in the initial mapping.

The started kernel is indicating the support of each feature via elf notes.

This series enables grub-xen to do the same as the hypervisor.

Tested with:
- 32 bit domU (kernel not supporting unmapped initrd)
- 32 bit domU (kernel supporting unmapped initrd)
- 1 GB 64 bit domU (kernel supporting unmapped initrd, not p2m)
- 1 GB 64 bit domU (kernel supporting unmapped initrd and p2m)
- 900GB 64 bit domU (kernel supporting unmapped initrd and p2m)

Changes in V3:
- added new patch 1 (free memory in case of error) as requested by Daniel Kiper
- added new patch 2 (avoid global variables) as requested by Daniel Kiper
- added new patch 3 (use constants for elf notes) as requested by Daniel Kiper
- added new patch 4 (sync with new Xen headers) in order to use constants in assembly code
- modified patch 9 (was patch 5) to use constants instead of numbers and added lots of comments to assembly code as requested by Daniel Kiper

Changes in V2:
- rebased patch 5 to current master

Juergen Gross (10):
xen: make xen loader callable multiple times
xen: reduce number of global variables in xen loader
xen: add elfnote.h to avoid using numbers instead of constants
xen: synchronize xen header
xen: factor out p2m list allocation into separate function
xen: factor out allocation of special pages into separate function
xen: factor out allocation of page tables into separate function
xen: add capability to load initrd outside of initial mapping
xen: modify page table construction
xen: add capability to load p2m list outside of kernel mapping

 grub-core/lib/i386/xen/relocator.S   |  87 ++--
 grub-core/lib/x86_64/xen/relocator.S | 134 +++---
 grub-core/lib/xen/relocator.c        |  25 +-
 grub-core/loader/i386/xen.c          | 778 +++++++++++++++++++++++------------
 grub-core/loader/i386/xen_fileXX.c   |  43 +-
 include/grub/i386/memory.h           |   7 +
 include/grub/xen/relocator.h         |   6 +-
 include/grub/xen_file.h              |   3 +
 include/xen/arch-x86/xen-x86_32.h    |  22 +-
 include/xen/arch-x86/xen-x86_64.h    |   8 +-
 include/xen/elfnote.h                | 281 +++++++++++++
 include/xen/xen.h                    | 125 ++++--
 12 files changed, 1070 insertions(+), 449 deletions(-)
 create mode 100644 include/xen/elfnote.h
Signature · 1 line
2.6.2
author maintainer
#1 lore raw

PATCH v3 1/10 xen: make xen loader callable multiple times

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +19 -9
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
 fail:
+  err = grub_errno;
 
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
 static grub_err_t
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;
 
       if (grub_initrd_load (&initrd_ctx, argv,
 			    get_virtual_current_address (ch)))
grub-core/loader/i386/xen_fileXX.c +11 -6
index 1ba5649..5475819 100644
@@ -35,7 +35,8 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
   if (grub_file_read (elf->file, buf, sz) != (grub_ssize_t) sz)
     {
       if (grub_errno)
-	return grub_errno;
+	goto out;
+      grub_free (buf);
       return grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
 			 elf->file->name);
     }
@@ -123,14 +124,14 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	{
 	  xi->virt_base = grub_strtoull (ptr + sizeof ("VIRT_BASE=") - 1, &ptr, 16);
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;
 	  continue;
 	}
       if (grub_strncmp (ptr, "VIRT_ENTRY=", sizeof ("VIRT_ENTRY=") - 1) == 0)
 	{
 	  xi->entry_point = grub_strtoull (ptr + sizeof ("VIRT_ENTRY=") - 1, &ptr, 16);
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;
 	  continue;
 	}
       if (grub_strncmp (ptr, "HYPERCALL_PAGE=", sizeof ("HYPERCALL_PAGE=") - 1) == 0)
@@ -138,7 +139,7 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	  xi->hypercall_page = grub_strtoull (ptr + sizeof ("HYPERCALL_PAGE=") - 1, &ptr, 16);
 	  xi->has_hypercall_page = 1;
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;
 	  continue;
 	}
       if (grub_strncmp (ptr, "ELF_PADDR_OFFSET=", sizeof ("ELF_PADDR_OFFSET=") - 1) == 0)
@@ -146,7 +147,7 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	  xi->paddr_offset = grub_strtoull (ptr + sizeof ("ELF_PADDR_OFFSET=") - 1, &ptr, 16);
 	  has_paddr = 1;
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;
 	  continue;
 	}
     }
@@ -154,7 +155,11 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
     xi->hypercall_page = (xi->hypercall_page << 12) + xi->virt_base;
   if (!has_paddr)
     xi->paddr_offset = xi->virt_base;
-  return GRUB_ERR_NONE;
+
+out:
+  grub_free (buf);
+
+  return grub_errno;
 }
 
 #pragma GCC diagnostic ignored "-Wcast-align"
Signature · 1 line
2.6.2
#2 lore raw

Re: [PATCH v3 01/10] xen: make xen loader callable multiple times

Quoted 4 lines · Juergen Gross

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating

Do you mean grub_xen_reset?

Quoted 50 lines

a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +14 -5
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,

grub-core/loader/i386/xen.c grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)), · line 515

Quoted 4 lines
grub-core/loader/i386/xen.c +1 -0
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
 fail:
+  err = grub_errno;

I do not think this is needed.

Quoted 16 lines
grub-core/loader/i386/xen.c +3 -3
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
 static grub_err_t

grub-core/loader/i386/xen.c grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), · line 565

Quoted 5 lines
grub-core/loader/i386/xen.c +1 -1
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;

It looks that this change should not be part of this patch.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 39

Quoted 13 lines
grub-core/loader/i386/xen.c +0 -0
       if (grub_initrd_load (&initrd_ctx, argv,
 			    get_virtual_current_address (ch)))
grub-core/loader/i386/xen_fileXX.c +2 -1
index 1ba5649..5475819 100644
@@ -35,7 +35,8 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
   if (grub_file_read (elf->file, buf, sz) != (grub_ssize_t) sz)
     {
       if (grub_errno)
-	return grub_errno;
+	goto out;
+      grub_free (buf);

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 127

Quoted 9 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
       return grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
 			 elf->file->name);
     }
@@ -123,14 +124,14 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	{
 	  xi->virt_base = grub_strtoull (ptr + sizeof ("VIRT_BASE=") - 1, &ptr, 16);
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 134

Quoted 8 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
 	  continue;
 	}
       if (grub_strncmp (ptr, "VIRT_ENTRY=", sizeof ("VIRT_ENTRY=") - 1) == 0)
 	{
 	  xi->entry_point = grub_strtoull (ptr + sizeof ("VIRT_ENTRY=") - 1, &ptr, 16);
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 142

Quoted 9 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
 	  continue;
 	}
       if (grub_strncmp (ptr, "HYPERCALL_PAGE=", sizeof ("HYPERCALL_PAGE=") - 1) == 0)
@@ -138,7 +139,7 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	  xi->hypercall_page = grub_strtoull (ptr + sizeof ("HYPERCALL_PAGE=") - 1, &ptr, 16);
 	  xi->has_hypercall_page = 1;
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 150

Quoted 9 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
 	  continue;
 	}
       if (grub_strncmp (ptr, "ELF_PADDR_OFFSET=", sizeof ("ELF_PADDR_OFFSET=") - 1) == 0)
@@ -146,7 +147,7 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	  xi->paddr_offset = grub_strtoull (ptr + sizeof ("ELF_PADDR_OFFSET=") - 1, &ptr, 16);
 	  has_paddr = 1;
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 162

Quoted 13 lines
grub-core/loader/i386/xen_fileXX.c +5 -1
 	  continue;
 	}
     }
@@ -154,7 +155,11 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
     xi->hypercall_page = (xi->hypercall_page << 12) + xi->virt_base;
   if (!has_paddr)
     xi->paddr_offset = xi->virt_base;
-  return GRUB_ERR_NONE;
+
+out:
+  grub_free (buf);
+
+  return grub_errno;

Make sense but this should be separate patch.

Daniel

author maintainer
#3 lore raw

Re: [PATCH v3 01/10] xen: make xen loader callable multiple times

Quoted 7 lines · Daniel Kiper
Quoted 4 lines · Juergen Gross

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating

Do you mean grub_xen_reset?

No. I do want to call grub_relocator_unload() and I'm doing it in grub_xen_reset(). Other modules don't call grub_xen_reset(). :-)

Quoted 53 lines
Quoted 53 lines

a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +14 -5
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
Quoted 4 lines

grub-core/loader/i386/xen.c grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)), · line 515

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 fail:
+  err = grub_errno;

I do not think this is needed.

grub_elf_close() and others might clobber grub_errno.

Quoted 14 lines
Quoted 14 lines
grub-core/loader/i386/xen.c +3 -3
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
Quoted 9 lines

grub-core/loader/i386/xen.c grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), · line 565

Quoted 7 lines
grub-core/loader/i386/xen.c +1 -1
 static grub_err_t
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;

It looks that this change should not be part of this patch.

Why not? It's correcting a memory leak in case of failure. Like the other cases below, too. That's the purpose of this patch, after all.

Juergen

Quoted 82 lines
Quoted 8 lines
grub-core/loader/i386/xen.c +0 -0
       if (grub_initrd_load (&initrd_ctx, argv,
 			    get_virtual_current_address (ch)))
grub-core/loader/i386/xen_fileXX.c +0 -0
index 1ba5649..5475819 100644

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 39

Quoted 6 lines
grub-core/loader/i386/xen_fileXX.c +2 -1
@@ -35,7 +35,8 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
   if (grub_file_read (elf->file, buf, sz) != (grub_ssize_t) sz)
     {
       if (grub_errno)
-	return grub_errno;
+	goto out;
+      grub_free (buf);

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 127

Quoted 9 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
       return grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
 			 elf->file->name);
     }
@@ -123,14 +124,14 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	{
 	  xi->virt_base = grub_strtoull (ptr + sizeof ("VIRT_BASE=") - 1, &ptr, 16);
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 134

Quoted 8 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
 	  continue;
 	}
       if (grub_strncmp (ptr, "VIRT_ENTRY=", sizeof ("VIRT_ENTRY=") - 1) == 0)
 	{
 	  xi->entry_point = grub_strtoull (ptr + sizeof ("VIRT_ENTRY=") - 1, &ptr, 16);
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 142

Quoted 9 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
 	  continue;
 	}
       if (grub_strncmp (ptr, "HYPERCALL_PAGE=", sizeof ("HYPERCALL_PAGE=") - 1) == 0)
@@ -138,7 +139,7 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	  xi->hypercall_page = grub_strtoull (ptr + sizeof ("HYPERCALL_PAGE=") - 1, &ptr, 16);
 	  xi->has_hypercall_page = 1;
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 150

Quoted 9 lines
grub-core/loader/i386/xen_fileXX.c +1 -1
 	  continue;
 	}
       if (grub_strncmp (ptr, "ELF_PADDR_OFFSET=", sizeof ("ELF_PADDR_OFFSET=") - 1) == 0)
@@ -146,7 +147,7 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
 	  xi->paddr_offset = grub_strtoull (ptr + sizeof ("ELF_PADDR_OFFSET=") - 1, &ptr, 16);
 	  has_paddr = 1;
 	  if (grub_errno)
-	    return grub_errno;
+	    goto out;

Ditto.

grub-core/loader/i386/xen_fileXX.c parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi, · line 162

Quoted 13 lines
grub-core/loader/i386/xen_fileXX.c +5 -1
 	  continue;
 	}
     }
@@ -154,7 +155,11 @@ parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
     xi->hypercall_page = (xi->hypercall_page << 12) + xi->virt_base;
   if (!has_paddr)
     xi->paddr_offset = xi->virt_base;
-  return GRUB_ERR_NONE;
+
+out:
+  grub_free (buf);
+
+  return grub_errno;

Make sense but this should be separate patch.

Daniel

#4 lore raw

Re: [PATCH v3 01/10] xen: make xen loader callable multiple times

Quoted 65 lines · Juergen Gross
Quoted 7 lines · Daniel Kiper
Quoted 4 lines · Juergen Gross

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating

Do you mean grub_xen_reset?

No. I do want to call grub_relocator_unload() and I'm doing it in grub_xen_reset(). Other modules don't call grub_xen_reset(). :-)

Quoted 53 lines
Quoted 53 lines

a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +14 -5
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
Quoted 6 lines
Quoted 4 lines

grub-core/loader/i386/xen.c grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)), · line 515

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 fail:
+  err = grub_errno;

I do not think this is needed.

grub_elf_close() and others might clobber grub_errno.

OK, so, please say it in comment. It is not obvious.

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
grub-core/loader/i386/xen.c +3 -3
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
Quoted 9 lines

grub-core/loader/i386/xen.c grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), · line 565

Quoted 7 lines
grub-core/loader/i386/xen.c +1 -1
 static grub_err_t
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;

It looks that this change should not be part of this patch.

Quoted 2 lines

Why not? It's correcting a memory leak in case of failure. Like the other cases below, too. That's the purpose of this patch, after all.

OK but you are referring to grub_relocator_unload() in commit message and below you are trying to fix different memory leak in the same patch. So, I think everything below should be separate patch.

Daniel

#5 lore raw

Re: [PATCH v3 01/10] xen: make xen loader callable multiple times

In reply to Juergen Gross
Quoted 65 lines · Juergen Gross
Quoted 7 lines · Daniel Kiper
Quoted 4 lines · Juergen Gross

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating

Do you mean grub_xen_reset?

No. I do want to call grub_relocator_unload() and I'm doing it in grub_xen_reset(). Other modules don't call grub_xen_reset(). :-)

Quoted 53 lines
Quoted 53 lines

a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +14 -5
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
Quoted 6 lines
Quoted 4 lines

grub-core/loader/i386/xen.c grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)), · line 515

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 fail:
+  err = grub_errno;

I do not think this is needed.

grub_elf_close() and others might clobber grub_errno.

OK, so, please say it in comment. It is not obvious.

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
grub-core/loader/i386/xen.c +3 -3
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
Quoted 9 lines

grub-core/loader/i386/xen.c grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), · line 565

Quoted 7 lines
grub-core/loader/i386/xen.c +1 -1
 static grub_err_t
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;

It looks that this change should not be part of this patch.

Quoted 2 lines

Why not? It's correcting a memory leak in case of failure. Like the other cases below, too. That's the purpose of this patch, after all.

OK but you are referring to grub_relocator_unload() in commit message and below you are trying to fix different memory leak in the same patch. So, I think everything below should be separate patch.

Daniel

author maintainer
#6 lore raw

Re: [PATCH v3 01/10] xen: make xen loader callable multiple times

Quoted 66 lines · Daniel Kiper
Quoted 65 lines · Juergen Gross
Quoted 7 lines · Daniel Kiper
Quoted 4 lines · Juergen Gross

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating

Do you mean grub_xen_reset?

No. I do want to call grub_relocator_unload() and I'm doing it in grub_xen_reset(). Other modules don't call grub_xen_reset(). :-)

Quoted 53 lines
Quoted 53 lines

a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +14 -5
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

grub-core/loader/i386/xen.c grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)), · line 515

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 fail:
+  err = grub_errno;

I do not think this is needed.

grub_elf_close() and others might clobber grub_errno.

OK, so, please say it in comment. It is not obvious.

Okay.

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 16 lines
grub-core/loader/i386/xen.c +3 -3
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
 static grub_err_t

grub-core/loader/i386/xen.c grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), · line 565

Quoted 5 lines
grub-core/loader/i386/xen.c +1 -1
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;

It looks that this change should not be part of this patch.

Quoted 6 lines
Quoted 2 lines

Why not? It's correcting a memory leak in case of failure. Like the other cases below, too. That's the purpose of this patch, after all.

OK but you are referring to grub_relocator_unload() in commit message and below you are trying to fix different memory leak in the same patch. So, I think everything below should be separate patch.

Okay.

Juergen

author maintainer
#7 lore raw

Re: [PATCH v3 01/10] xen: make xen loader callable multiple times

In reply to Daniel Kiper
Quoted 66 lines · Daniel Kiper
Quoted 65 lines · Juergen Gross
Quoted 7 lines · Daniel Kiper
Quoted 4 lines · Juergen Gross

The loader for xen paravirtualized environment isn't callable multiple times as it won't free any memory in case of failure.

Call grub_relocator_unload() as other modules do it before allocating

Do you mean grub_xen_reset?

No. I do want to call grub_relocator_unload() and I'm doing it in grub_xen_reset(). Other modules don't call grub_xen_reset(). :-)

Quoted 53 lines
Quoted 53 lines

a new relocator or when unloading the module.

---
 grub-core/loader/i386/xen.c        | 28 +++++++++++++++++++---------
 grub-core/loader/i386/xen_fileXX.c | 17 +++++++++++------
 2 files changed, 30 insertions(+), 15 deletions(-)
grub-core/loader/i386/xen.c +14 -5
index c4d9689..ff7c553 100644
@@ -316,11 +316,23 @@ grub_xen_boot (void)
 				  xen_inf.virt_base);
 }
 
+static void
+grub_xen_reset (void)
+{
+  grub_memset (&next_start, 0, sizeof (next_start));
+  xen_module_info_page = NULL;
+  n_modules = 0;
+
+  grub_relocator_unload (relocator);
+  relocator = NULL;
+  loaded = 0;
+}
+
 static grub_err_t
 grub_xen_unload (void)
 {
+  grub_xen_reset ();
   grub_dl_unref (my_mod);
-  loaded = 0;
   return GRUB_ERR_NONE;
 }
 
@@ -403,10 +415,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  grub_memset (&next_start, 0, sizeof (next_start));
-
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      (char *) next_start.cmd_line,
@@ -503,16 +512,17 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   goto fail;
 
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

grub-core/loader/i386/xen.c grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)), · line 515

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 fail:
+  err = grub_errno;

I do not think this is needed.

grub_elf_close() and others might clobber grub_errno.

OK, so, please say it in comment. It is not obvious.

Okay.

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 16 lines
grub-core/loader/i386/xen.c +3 -3
   if (elf)
     grub_elf_close (elf);
   else if (file)
     grub_file_close (file);
 
-  if (grub_errno != GRUB_ERR_NONE)
-    loaded = 0;
+  if (err != GRUB_ERR_NONE)
+    grub_xen_reset ();
 
-  return grub_errno;
+  return err;
 }
 
 static grub_err_t

grub-core/loader/i386/xen.c grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), · line 565

Quoted 5 lines
grub-core/loader/i386/xen.c +1 -1
@@ -552,7 +562,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
     {
       err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
       if (err)
-	return err;
+	goto fail;

It looks that this change should not be part of this patch.

Quoted 6 lines
Quoted 2 lines

Why not? It's correcting a memory leak in case of failure. Like the other cases below, too. That's the purpose of this patch, after all.

OK but you are referring to grub_relocator_unload() in commit message and below you are trying to fix different memory leak in the same patch. So, I think everything below should be separate patch.

Okay.

Juergen

author maintainer
#8 lore raw

PATCH v3 2/10 xen: reduce number of global variables in xen loader

In reply to Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +138 -121
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;
+      xen_state.next_start.flags |= SIF_MULTIBOOT_MOD;
 
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					     max_addr, MAX_MODULES
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, MAX_MODULES
 					     *
-					     sizeof (xen_module_info_page
+					     sizeof (xen_state.module_info_page
 						     [0]));
       if (err)
 	return err;
-      xen_module_info_page = get_virtual_current_address (ch);
-      grub_memset (xen_module_info_page, 0, MAX_MODULES
-		   * sizeof (xen_module_info_page[0]));
-      max_addr += MAX_MODULES * sizeof (xen_module_info_page[0]);
+      xen_state.module_info_page = get_virtual_current_address (ch);
+      grub_memset (xen_state.module_info_page, 0, MAX_MODULES
+		   * sizeof (xen_state.module_info_page[0]));
+      xen_state.max_addr +=
+	MAX_MODULES * sizeof (xen_state.module_info_page[0]);
     }
 
-  max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
 
   if (nounzip)
     grub_file_filter_disable_compression ();
@@ -656,20 +669,22 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
 
   cmdline_len = grub_loader_cmdline_size (argc - 1, argv + 1);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, cmdline_len);
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, cmdline_len);
   if (err)
     goto fail;
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
 			      get_virtual_current_address (ch), cmdline_len);
 
-  xen_module_info_page[n_modules].cmdline = max_addr - modules_target_start;
-  max_addr = ALIGN_UP (max_addr + cmdline_len, PAGE_SIZE);
+  xen_state.module_info_page[xen_state.n_modules].cmdline =
+    xen_state.max_addr - xen_state.modules_target_start;
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + cmdline_len, PAGE_SIZE);
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
       if (grub_file_read (file, get_virtual_current_address (ch), size)
@@ -681,15 +696,17 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
 	  goto fail;
 	}
     }
-  next_start.mod_len = max_addr + size - modules_target_start;
-  xen_module_info_page[n_modules].mod_start = max_addr - modules_target_start;
-  xen_module_info_page[n_modules].mod_end =
-    max_addr + size - modules_target_start;
+  xen_state.next_start.mod_len =
+    xen_state.max_addr + size - xen_state.modules_target_start;
+  xen_state.module_info_page[xen_state.n_modules].mod_start =
+    xen_state.max_addr - xen_state.modules_target_start;
+  xen_state.module_info_page[xen_state.n_modules].mod_end =
+    xen_state.max_addr + size - xen_state.modules_target_start;
 
-  n_modules++;
+  xen_state.n_modules++;
   grub_dprintf ("xen", "module, addr=0x%x, size=0x%x\n",
-		(unsigned) max_addr, (unsigned) size);
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+		(unsigned) xen_state.max_addr, (unsigned) size);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
 
 fail:
Signature · 1 line
2.6.2
Reviewed-by on v3 2/10
#9 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 400 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +107 -96
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 10 lines
grub-core/loader/i386/xen.c +5 -5
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 11 lines
grub-core/loader/i386/xen.c +4 -3
+      xen_state.next_start.flags |= SIF_MULTIBOOT_MOD;
 
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					     max_addr, MAX_MODULES
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, MAX_MODULES
 					     *
-					     sizeof (xen_module_info_page
+					     sizeof (xen_state.module_info_page
 						     [0]));
       if (err)

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 658

Quoted 10 lines
grub-core/loader/i386/xen.c +5 -4
 	return err;
-      xen_module_info_page = get_virtual_current_address (ch);
-      grub_memset (xen_module_info_page, 0, MAX_MODULES
-		   * sizeof (xen_module_info_page[0]));
-      max_addr += MAX_MODULES * sizeof (xen_module_info_page[0]);
+      xen_state.module_info_page = get_virtual_current_address (ch);
+      grub_memset (xen_state.module_info_page, 0, MAX_MODULES
+		   * sizeof (xen_state.module_info_page[0]));
+      xen_state.max_addr +=
+	MAX_MODULES * sizeof (xen_state.module_info_page[0]);

Ditto.

Daniel

Reviewed-by on v3 2/10
#10 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 400 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +107 -96
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 10 lines
grub-core/loader/i386/xen.c +5 -5
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 11 lines
grub-core/loader/i386/xen.c +4 -3
+      xen_state.next_start.flags |= SIF_MULTIBOOT_MOD;
 
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					     max_addr, MAX_MODULES
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, MAX_MODULES
 					     *
-					     sizeof (xen_module_info_page
+					     sizeof (xen_state.module_info_page
 						     [0]));
       if (err)

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 658

Quoted 10 lines
grub-core/loader/i386/xen.c +5 -4
 	return err;
-      xen_module_info_page = get_virtual_current_address (ch);
-      grub_memset (xen_module_info_page, 0, MAX_MODULES
-		   * sizeof (xen_module_info_page[0]));
-      max_addr += MAX_MODULES * sizeof (xen_module_info_page[0]);
+      xen_state.module_info_page = get_virtual_current_address (ch);
+      grub_memset (xen_state.module_info_page, 0, MAX_MODULES
+		   * sizeof (xen_state.module_info_page[0]));
+      xen_state.max_addr +=
+	MAX_MODULES * sizeof (xen_state.module_info_page[0]);

Ditto.

Daniel

author maintainer
#11 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

Quoted 408 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 397 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +106 -95
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
Quoted 15 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 13 lines
grub-core/loader/i386/xen.c +6 -6
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

Quoted 16 lines
Quoted 16 lines
grub-core/loader/i386/xen.c +4 -6
+      xen_state.next_start.flags |= SIF_MULTIBOOT_MOD;
 
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					     max_addr, MAX_MODULES
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, MAX_MODULES
 					     *
-					     sizeof (xen_module_info_page
+					     sizeof (xen_state.module_info_page
 						     [0]));
       if (err)
 	return err;
-      xen_module_info_page = get_virtual_current_address (ch);
-      grub_memset (xen_module_info_page, 0, MAX_MODULES
-		   * sizeof (xen_module_info_page[0]));
Quoted 8 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 658

Quoted 6 lines
grub-core/loader/i386/xen.c +5 -1
-      max_addr += MAX_MODULES * sizeof (xen_module_info_page[0]);
+      xen_state.module_info_page = get_virtual_current_address (ch);
+      grub_memset (xen_state.module_info_page, 0, MAX_MODULES
+		   * sizeof (xen_state.module_info_page[0]));
+      xen_state.max_addr +=
+	MAX_MODULES * sizeof (xen_state.module_info_page[0]);

Ditto.

Yep. tab again.

Juergen

#12 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

Quoted 425 lines · Juergen Gross
Quoted 408 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 397 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +106 -95
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
Quoted 15 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 13 lines
grub-core/loader/i386/xen.c +6 -6
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 2 lines

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

But your line continuation is indented LESS than the line it is continuing, which is clearly NOT the style.

Signature · 1 line
len Sorensen
#13 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

In reply to Juergen Gross
Quoted 425 lines · Juergen Gross
Quoted 408 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 397 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +106 -95
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
Quoted 15 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 13 lines
grub-core/loader/i386/xen.c +6 -6
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 2 lines

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

But your line continuation is indented LESS than the line it is continuing, which is clearly NOT the style.

Signature · 1 line
len Sorensen
author maintainer
#14 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

Quoted 426 lines · Lennart Sorensen
Quoted 425 lines · Juergen Gross
Quoted 423 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 400 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +107 -96
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 10 lines
grub-core/loader/i386/xen.c +5 -5
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 5 lines
Quoted 2 lines

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

But your line continuation is indented LESS than the line it is continuing, which is clearly NOT the style.

No. Tabs are 8 spaces and have been so in this file and in others as well.

Juergen

BTW: any reason you omitted me from the to: and cc: list?

author maintainer
#15 lore raw

Re: [Xen-devel] [PATCH v3 02/10] xen: reduce number of global variables in xen loader

In reply to Lennart Sorensen
Quoted 426 lines · Lennart Sorensen
Quoted 425 lines · Juergen Gross
Quoted 423 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 400 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +107 -96
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 10 lines
grub-core/loader/i386/xen.c +5 -5
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 5 lines
Quoted 2 lines

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

But your line continuation is indented LESS than the line it is continuing, which is clearly NOT the style.

No. Tabs are 8 spaces and have been so in this file and in others as well.

Juergen

BTW: any reason you omitted me from the to: and cc: list?

#16 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

Quoted 411 lines · Juergen Gross
Quoted 410 lines · Lennart Sorensen
Quoted 409 lines · Juergen Gross
Quoted 408 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 397 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +106 -95
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
grub-core/loader/i386/xen.c +1 -3
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 8 lines
grub-core/loader/i386/xen.c +5 -3
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Now that we have 8 characters worth of quoting and patch indentation, it looks perfect again. :)

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Lost one space.

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

But your line continuation is indented LESS than the line it is continuing, which is clearly NOT the style.

No. Tabs are 8 spaces and have been so in this file and in others as well.

Oh right. The horrible GNU coding style of 2 space indentation but tab replacing 8 spaces, which then gets messed up and looks weird when you start quoting it in email. It would be hard to come up with a worse indentation style than the GNU one.

Quoted 1 line

BTW: any reason you omitted me from the to: and cc: list?

I just hit reply-all, so no idea why it would have been dropped.

Signature · 1 line
Len Sorensen
#17 lore raw

Re: [Xen-devel] [PATCH v3 02/10] xen: reduce number of global variables in xen loader

In reply to Juergen Gross
Quoted 411 lines · Juergen Gross
Quoted 410 lines · Lennart Sorensen
Quoted 409 lines · Juergen Gross
Quoted 408 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 397 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)
grub-core/loader/i386/xen.c +106 -95
index ff7c553..d5fe168 100644
@@ -42,16 +42,20 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static struct grub_relocator *relocator = NULL;
-static grub_uint64_t max_addr;
+struct xen_loader_state {
+  struct grub_relocator *relocator;
+  struct start_info next_start;
+  struct grub_xen_file_info xen_inf;
+  grub_uint64_t max_addr;
+  struct xen_multiboot_mod_list *module_info_page;
+  grub_uint64_t modules_target_start;
+  grub_size_t n_modules;
+  int loaded;
+};
+
+static struct xen_loader_state xen_state;
+
 static grub_dl_t my_mod;
-static int loaded = 0;
-static struct start_info next_start;
-static void *kern_chunk_src;
-static struct grub_xen_file_info xen_inf;
-static struct xen_multiboot_mod_list *xen_module_info_page;
-static grub_uint64_t modules_target_start;
-static grub_size_t n_modules;
 
 #define PAGE_SIZE 4096
 #define MAX_MODULES (PAGE_SIZE / sizeof (struct xen_multiboot_mod_list))
@@ -225,50 +229,55 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = max_addr;
-  next_start.mfn_list = max_addr + xen_inf.virt_base;
-  next_start.first_p2m_pfn = max_addr >> PAGE_SHIFT;	/* Is this right? */
+  state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
   pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, pgtsize);
-  next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, pgtsize);
+  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
   if (err)
     return err;
   new_mfn_list = get_virtual_current_address (ch);
   grub_memcpy (new_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  max_addr = ALIGN_UP (max_addr + pgtsize, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, sizeof (next_start));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = max_addr + xen_inf.virt_base;
+  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
-  max_addr = ALIGN_UP (max_addr + sizeof (next_start), PAGE_SIZE);
+  xen_state.max_addr =
+    ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
 
-  next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
-  grub_memcpy (next_start.magic, grub_xen_start_page_addr->magic,
-	       sizeof (next_start.magic));
-  next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
-  next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
-  next_start.console.domU = grub_xen_start_page_addr->console.domU;
-  next_start.shared_info = grub_xen_start_page_addr->shared_info;
+  xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
+  grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
+	       sizeof (xen_state.next_start.magic));
+  xen_state.next_start.store_mfn = grub_xen_start_page_addr->store_mfn;
+  xen_state.next_start.store_evtchn = grub_xen_start_page_addr->store_evtchn;
+  xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
+  xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, max_addr >> PAGE_SHIFT);
+  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
-  max_addr += 2 * PAGE_SIZE;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
-  next_start.pt_base = max_addr + xen_inf.virt_base;
-  state.paging_start = max_addr >> PAGE_SHIFT;
+  xen_state.next_start.pt_base =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
-  nr_info_pages = max_addr >> PAGE_SHIFT;
+  nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
 
   while (1)
     {
       nr_pages = ALIGN_UP (nr_pages, (ALIGN_SIZE >> PAGE_SHIFT));
-      nr_pt_pages = get_pgtable_size (nr_pages, xen_inf.virt_base);
+      nr_pt_pages = get_pgtable_size (nr_pages, xen_state.xen_inf.virt_base);
       nr_need_pages =
 	nr_info_pages + nr_pt_pages +
 	((ADDITIONAL_SIZE + STACK_SIZE) >> PAGE_SHIFT);
@@ -278,27 +287,28 @@ grub_xen_boot (void)
     }
 
   grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.virt_base,
 		(unsigned long long) page2offset (nr_pages));
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					 max_addr, page2offset (nr_pt_pages));
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr,
+					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
   generate_page_table (get_virtual_current_address (ch),
-		       max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_inf.virt_base, new_mfn_list);
+		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
+		       xen_state.xen_inf.virt_base, new_mfn_list);
 
-  max_addr += page2offset (nr_pt_pages);
-  state.stack = max_addr + STACK_SIZE + xen_inf.virt_base;
-  state.entry_point = xen_inf.entry_point;
+  xen_state.max_addr += page2offset (nr_pt_pages);
+  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  state.entry_point = xen_state.xen_inf.entry_point;
 
-  next_start.nr_p2m_frames += nr_pt_pages;
-  next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   state.paging_size = nr_pt_pages;
 
-  *nst = next_start;
+  *nst = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
@@ -308,24 +318,20 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (relocator, state, nr_pages,
-				  xen_inf.virt_base <
+  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
 				  page2offset (nr_pages - 1) +
-				  xen_inf.virt_base);
+				  xen_state.xen_inf.virt_base);
 }
 
 static void
 grub_xen_reset (void)
 {
-  grub_memset (&next_start, 0, sizeof (next_start));
-  xen_module_info_page = NULL;
-  n_modules = 0;
+  grub_relocator_unload (xen_state.relocator);
 
-  grub_relocator_unload (relocator);
-  relocator = NULL;
-  loaded = 0;
+  grub_memset (&xen_state, 0, sizeof (xen_state));
 }
 
 static grub_err_t
@@ -409,17 +415,22 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file;
   grub_elf_t elf;
   grub_err_t err;
+  void *kern_chunk_src;
+  grub_relocator_chunk_t ch;
+  grub_addr_t kern_start;
+  grub_addr_t kern_end;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
+  /* Call grub_loader_unset early to avoid it being called by grub_loader_set */
   grub_loader_unset ();
 
   grub_xen_reset ();
 
   grub_create_loader_cmdline (argc - 1, argv + 1,
-			      (char *) next_start.cmd_line,
-			      sizeof (next_start.cmd_line) - 1);
+			      (char *) xen_state.next_start.cmd_line,
+			      sizeof (xen_state.next_start.cmd_line) - 1);
 
   file = grub_file_open (argv[0]);
   if (!file)
@@ -429,85 +440,82 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
   if (!elf)
     goto fail;
 
-  err = grub_xen_get_info (elf, &xen_inf);
+  err = grub_xen_get_info (elf, &xen_state.xen_inf);
   if (err)
     goto fail;
 #ifdef __x86_64__
-  if (xen_inf.arch != GRUB_XEN_FILE_X86_64)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_X86_64)
 #else
-  if (xen_inf.arch != GRUB_XEN_FILE_I386_PAE
-      && xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
+  if (xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE
+      && xen_state.xen_inf.arch != GRUB_XEN_FILE_I386_PAE_BIMODE)
 #endif
     {
       grub_error (GRUB_ERR_BAD_OS, "incompatible architecture: %d",
-		  xen_inf.arch);
+		  xen_state.xen_inf.arch);
       goto fail;
     }
 
-  if (xen_inf.virt_base & (PAGE_SIZE - 1))
+  if (xen_state.xen_inf.virt_base & (PAGE_SIZE - 1))
     {
       grub_error (GRUB_ERR_BAD_OS, "unaligned virt_base");
       goto fail;
     }
   grub_dprintf ("xen", "virt_base = %llx, entry = %llx\n",
-		(unsigned long long) xen_inf.virt_base,
-		(unsigned long long) xen_inf.entry_point);
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) xen_state.xen_inf.entry_point);
 
-  relocator = grub_relocator_new ();
-  if (!relocator)
+  xen_state.relocator = grub_relocator_new ();
+  if (!xen_state.relocator)
     goto fail;
 
-  grub_relocator_chunk_t ch;
-  grub_addr_t kern_start = xen_inf.kern_start - xen_inf.paddr_offset;
-  grub_addr_t kern_end = xen_inf.kern_end - xen_inf.paddr_offset;
+  kern_start = xen_state.xen_inf.kern_start - xen_state.xen_inf.paddr_offset;
+  kern_end = xen_state.xen_inf.kern_end - xen_state.xen_inf.paddr_offset;
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       grub_dprintf ("xen", "hypercall page at 0x%llx\n",
-		    (unsigned long long) xen_inf.hypercall_page);
-      if (xen_inf.hypercall_page - xen_inf.virt_base < kern_start)
-	kern_start = xen_inf.hypercall_page - xen_inf.virt_base;
-
-      if (xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE > kern_end)
-	kern_end = xen_inf.hypercall_page - xen_inf.virt_base + PAGE_SIZE;
+		    (unsigned long long) xen_state.xen_inf.hypercall_page);
+      kern_start = grub_min (kern_start, xen_state.xen_inf.hypercall_page -
+					 xen_state.xen_inf.virt_base);
+      kern_end = grub_max (kern_end, xen_state.xen_inf.hypercall_page -
+				     xen_state.xen_inf.virt_base + PAGE_SIZE);
     }
 
-  max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
 
-  err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
 					 kern_end - kern_start);
   if (err)
     goto fail;
   kern_chunk_src = get_virtual_current_address (ch);
 
   grub_dprintf ("xen", "paddr_offset = 0x%llx\n",
-		(unsigned long long) xen_inf.paddr_offset);
+		(unsigned long long) xen_state.xen_inf.paddr_offset);
   grub_dprintf ("xen", "kern_start = 0x%llx, kern_end = 0x%llx\n",
-		(unsigned long long) xen_inf.kern_start,
-		(unsigned long long) xen_inf.kern_end);
+		(unsigned long long) xen_state.xen_inf.kern_start,
+		(unsigned long long) xen_state.xen_inf.kern_end);
 
   err = grub_elfXX_load (elf, argv[0],
 			 (grub_uint8_t *) kern_chunk_src - kern_start
-			 - xen_inf.paddr_offset, 0, 0, 0);
+			 - xen_state.xen_inf.paddr_offset, 0, 0, 0);
 
-  if (xen_inf.has_hypercall_page)
+  if (xen_state.xen_inf.has_hypercall_page)
     {
       unsigned i;
       for (i = 0; i < PAGE_SIZE / HYPERCALL_INTERFACE_SIZE; i++)
 	set_hypercall_interface ((grub_uint8_t *) kern_chunk_src +
 				 i * HYPERCALL_INTERFACE_SIZE +
-				 xen_inf.hypercall_page - xen_inf.virt_base -
-				 kern_start, i);
+				 xen_state.xen_inf.hypercall_page -
+				 xen_state.xen_inf.virt_base - kern_start, i);
     }
 
   if (err)
     goto fail;
 
   grub_dl_ref (my_mod);
-  loaded = 1;
+  xen_state.loaded = 1;
 
   grub_loader_set (grub_xen_boot, grub_xen_unload, 0);
-  loaded = 1;
 
   goto fail;
 
@@ -540,14 +548,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT,
 		  N_("you need to load the kernel first"));
       goto fail;
     }
 
-  if (next_start.mod_start || next_start.mod_len)
+  if (xen_state.next_start.mod_start || xen_state.next_start.mod_len)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
       goto fail;
@@ -560,7 +568,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   if (size)
     {
-      err = grub_relocator_alloc_chunk_addr (relocator, &ch, max_addr, size);
+      err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					     xen_state.max_addr, size);
       if (err)
 	goto fail;
 
@@ -569,13 +578,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  next_start.mod_start = max_addr + xen_inf.virt_base;
-  next_start.mod_len = size;
+  xen_state.next_start.mod_start =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.next_start.mod_len = size;
 
-  max_addr = ALIGN_UP (max_addr + size, PAGE_SIZE);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) next_start.mod_start, (unsigned) size);
+		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -607,45 +617,48 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 
-  if (!loaded)
+  if (!xen_state.loaded)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("you need to load the kernel first"));
     }
 
-  if ((next_start.mod_start || next_start.mod_len) && !xen_module_info_page)
+  if ((xen_state.next_start.mod_start || xen_state.next_start.mod_len) &&
+      !xen_state.module_info_page)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("initrd already loaded"));
     }
 
   /* Leave one space for terminator.  */
-  if (n_modules >= MAX_MODULES - 1)
+  if (xen_state.n_modules >= MAX_MODULES - 1)
     {
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many modules");
     }
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
grub-core/loader/i386/xen.c +1 -3
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 8 lines
grub-core/loader/i386/xen.c +5 -3
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Now that we have 8 characters worth of quoting and patch indentation, it looks perfect again. :)

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Lost one space.

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

But your line continuation is indented LESS than the line it is continuing, which is clearly NOT the style.

No. Tabs are 8 spaces and have been so in this file and in others as well.

Oh right. The horrible GNU coding style of 2 space indentation but tab replacing 8 spaces, which then gets messed up and looks weird when you start quoting it in email. It would be hard to come up with a worse indentation style than the GNU one.

Quoted 1 line

BTW: any reason you omitted me from the to: and cc: list?

I just hit reply-all, so no idea why it would have been dropped.

Signature · 1 line
Len Sorensen
#18 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

In reply to Juergen Gross
Quoted 15 lines · Juergen Gross
Quoted 14 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 3 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)

[...]

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
grub-core/loader/i386/xen.c +1 -5
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
Quoted 8 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 6 lines
grub-core/loader/i386/xen.c +5 -1
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 2 lines

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

Sorry, I have missed that.

Daniel

#19 lore raw

Re: [PATCH v3 02/10] xen: reduce number of global variables in xen loader

In reply to Juergen Gross
Quoted 15 lines · Juergen Gross
Quoted 14 lines · Daniel Kiper
Quoted 5 lines · Juergen Gross

The loader for xen paravirtualized environment is using lots of global variables. Reduce the number by making them either local or by putting them into a single state structure.

Just two nitpicks but in general...

Quoted 3 lines
---
 grub-core/loader/i386/xen.c | 259 +++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)

[...]

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
grub-core/loader/i386/xen.c +1 -5
-  if (!xen_module_info_page)
+  if (!xen_state.module_info_page)
     {
-      n_modules = 0;
-      max_addr = ALIGN_UP (max_addr, PAGE_SIZE);
-      modules_target_start = max_addr;
-      next_start.mod_start = max_addr + xen_inf.virt_base;
Quoted 8 lines

grub-core/loader/i386/xen.c grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), · line 644

Quoted 6 lines
grub-core/loader/i386/xen.c +5 -1
-      next_start.flags |= SIF_MULTIBOOT_MOD;
+      xen_state.n_modules = 0;
+      xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
+      xen_state.modules_target_start = xen_state.max_addr;
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;

Lost one space.

Quoted 2 lines

Really? Common indentation style seams to be to use tabs where possible. And this is a tab.

Sorry, I have missed that.

Daniel

author maintainer
#20 lore raw

PATCH v3 3/10 xen: add elfnote.h to avoid using numbers instead of constants

In reply to Juergen Gross

Various features and parameters of a pv-kernel are specified via elf notes in the kernel image. Those notes are part of the interface between the Xen hypervisor and the kernel.

Instead of using num,bers in the code when interpreting the elf notes make use of the header supplied by Xen for that purpose.

---
 grub-core/loader/i386/xen_fileXX.c |  19 +--
 include/xen/elfnote.h              | 281 +++++++++++++++++++++++++++++++++++++
 2 files changed, 291 insertions(+), 9 deletions(-)
 create mode 100644 include/xen/elfnote.h
grub-core/loader/i386/xen_fileXX.c +10 -9
index 5475819..1f7f71d 100644
@@ -18,6 +18,7 @@
 
 #include <grub/xen_file.h>
 #include <grub/misc.h>
+#include <xen/elfnote.h>
 
 static grub_err_t
 parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
@@ -201,35 +202,35 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
       xi->has_note = 1;
       switch (nh->n_type)
 	{
-	case 1:
+	case XEN_ELFNOTE_ENTRY:
 	  xi->entry_point = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 2:
+	case XEN_ELFNOTE_HYPERCALL_PAGE:
 	  xi->hypercall_page = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  xi->has_hypercall_page = 1;
 	  break;
-	case 3:
+	case XEN_ELFNOTE_VIRT_BASE:
 	  xi->virt_base = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 4:
+	case XEN_ELFNOTE_PADDR_OFFSET:
 	  xi->paddr_offset = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 5:
+	case XEN_ELFNOTE_XEN_VERSION:
 	  grub_dprintf ("xen", "xenversion = `%s'\n", (char *) desc);
 	  break;
-	case 6:
+	case XEN_ELFNOTE_GUEST_OS:
 	  grub_dprintf ("xen", "name = `%s'\n", (char *) desc);
 	  break;
-	case 7:
+	case XEN_ELFNOTE_GUEST_VERSION:
 	  grub_dprintf ("xen", "version = `%s'\n", (char *) desc);
 	  break;
-	case 8:
+	case XEN_ELFNOTE_LOADER:
 	  if (descsz < 7
 	      || grub_memcmp (desc, "generic", descsz == 7 ? 7 : 8) != 0)
 	    return grub_error (GRUB_ERR_BAD_OS, "invalid loader");
 	  break;
 	  /* PAE */
-	case 9:
+	case XEN_ELFNOTE_PAE_MODE:
 	  grub_dprintf ("xen", "pae = `%s', %d, %d\n", (char *) desc,
 			xi->arch, descsz);
 	  if (xi->arch != GRUB_XEN_FILE_I386
include/xen/elfnote.h new file +281 -0

281 lines not on this page · raw message

Signature · 1 line
2.6.2
author maintainer
#21 lore raw

PATCH v3 3/10 xen: add elfnote.h to avoid using numbers instead of constants

In reply to Juergen Gross

Various features and parameters of a pv-kernel are specified via elf notes in the kernel image. Those notes are part of the interface between the Xen hypervisor and the kernel.

Instead of using num,bers in the code when interpreting the elf notes make use of the header supplied by Xen for that purpose.

---
 grub-core/loader/i386/xen_fileXX.c |  19 +--
 include/xen/elfnote.h              | 281 +++++++++++++++++++++++++++++++++++++
 2 files changed, 291 insertions(+), 9 deletions(-)
 create mode 100644 include/xen/elfnote.h
grub-core/loader/i386/xen_fileXX.c +10 -9
index 5475819..1f7f71d 100644
@@ -18,6 +18,7 @@
 
 #include <grub/xen_file.h>
 #include <grub/misc.h>
+#include <xen/elfnote.h>
 
 static grub_err_t
 parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
@@ -201,35 +202,35 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
       xi->has_note = 1;
       switch (nh->n_type)
 	{
-	case 1:
+	case XEN_ELFNOTE_ENTRY:
 	  xi->entry_point = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 2:
+	case XEN_ELFNOTE_HYPERCALL_PAGE:
 	  xi->hypercall_page = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  xi->has_hypercall_page = 1;
 	  break;
-	case 3:
+	case XEN_ELFNOTE_VIRT_BASE:
 	  xi->virt_base = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 4:
+	case XEN_ELFNOTE_PADDR_OFFSET:
 	  xi->paddr_offset = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 5:
+	case XEN_ELFNOTE_XEN_VERSION:
 	  grub_dprintf ("xen", "xenversion = `%s'\n", (char *) desc);
 	  break;
-	case 6:
+	case XEN_ELFNOTE_GUEST_OS:
 	  grub_dprintf ("xen", "name = `%s'\n", (char *) desc);
 	  break;
-	case 7:
+	case XEN_ELFNOTE_GUEST_VERSION:
 	  grub_dprintf ("xen", "version = `%s'\n", (char *) desc);
 	  break;
-	case 8:
+	case XEN_ELFNOTE_LOADER:
 	  if (descsz < 7
 	      || grub_memcmp (desc, "generic", descsz == 7 ? 7 : 8) != 0)
 	    return grub_error (GRUB_ERR_BAD_OS, "invalid loader");
 	  break;
 	  /* PAE */
-	case 9:
+	case XEN_ELFNOTE_PAE_MODE:
 	  grub_dprintf ("xen", "pae = `%s', %d, %d\n", (char *) desc,
 			xi->arch, descsz);
 	  if (xi->arch != GRUB_XEN_FILE_I386
include/xen/elfnote.h new file +281 -0

281 lines not on this page · raw message

Signature · 1 line
2.6.2
Reviewed-by on v3 3/10
#22 lore raw

Re: [PATCH v3 03/10] xen: add elfnote.h to avoid using numbers instead of constants

Quoted 8 lines · Juergen Gross

Various features and parameters of a pv-kernel are specified via elf notes in the kernel image. Those notes are part of the interface between the Xen hypervisor and the kernel.

Instead of using num,bers in the code when interpreting the elf notes make use of the header supplied by Xen for that purpose.

Yummy! Just one nitpick.

Quoted 340 lines
---
 grub-core/loader/i386/xen_fileXX.c |  19 +--
 include/xen/elfnote.h              | 281 +++++++++++++++++++++++++++++++++++++
 2 files changed, 291 insertions(+), 9 deletions(-)
 create mode 100644 include/xen/elfnote.h
grub-core/loader/i386/xen_fileXX.c +10 -9
index 5475819..1f7f71d 100644
@@ -18,6 +18,7 @@
 
 #include <grub/xen_file.h>
 #include <grub/misc.h>
+#include <xen/elfnote.h>
 
 static grub_err_t
 parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
@@ -201,35 +202,35 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
       xi->has_note = 1;
       switch (nh->n_type)
 	{
-	case 1:
+	case XEN_ELFNOTE_ENTRY:
 	  xi->entry_point = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 2:
+	case XEN_ELFNOTE_HYPERCALL_PAGE:
 	  xi->hypercall_page = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  xi->has_hypercall_page = 1;
 	  break;
-	case 3:
+	case XEN_ELFNOTE_VIRT_BASE:
 	  xi->virt_base = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 4:
+	case XEN_ELFNOTE_PADDR_OFFSET:
 	  xi->paddr_offset = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 5:
+	case XEN_ELFNOTE_XEN_VERSION:
 	  grub_dprintf ("xen", "xenversion = `%s'\n", (char *) desc);
 	  break;
-	case 6:
+	case XEN_ELFNOTE_GUEST_OS:
 	  grub_dprintf ("xen", "name = `%s'\n", (char *) desc);
 	  break;
-	case 7:
+	case XEN_ELFNOTE_GUEST_VERSION:
 	  grub_dprintf ("xen", "version = `%s'\n", (char *) desc);
 	  break;
-	case 8:
+	case XEN_ELFNOTE_LOADER:
 	  if (descsz < 7
 	      || grub_memcmp (desc, "generic", descsz == 7 ? 7 : 8) != 0)
 	    return grub_error (GRUB_ERR_BAD_OS, "invalid loader");
 	  break;
 	  /* PAE */
-	case 9:
+	case XEN_ELFNOTE_PAE_MODE:
 	  grub_dprintf ("xen", "pae = `%s', %d, %d\n", (char *) desc,
 			xi->arch, descsz);
 	  if (xi->arch != GRUB_XEN_FILE_I386
include/xen/elfnote.h new file +271 -0
new file mode 100644
index 0000000..353985f
@@ -0,0 +1,281 @@
+/******************************************************************************
+ * elfnote.h
+ *
+ * Definitions used for the Xen ELF notes.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2006, Ian Campbell, XenSource Ltd.
+ */
+
+#ifndef __XEN_PUBLIC_ELFNOTE_H__
+#define __XEN_PUBLIC_ELFNOTE_H__
+
+/*
+ * `incontents 200 elfnotes ELF notes
+ *
+ * The notes should live in a PT_NOTE segment and have "Xen" in the
+ * name field.
+ *
+ * Numeric types are either 4 or 8 bytes depending on the content of
+ * the desc field.
+ *
+ * LEGACY indicated the fields in the legacy __xen_guest string which
+ * this a note type replaces.
+ *
+ * String values (for non-legacy) are NULL terminated ASCII, also known
+ * as ASCIZ type.
+ */
+
+/*
+ * NAME=VALUE pair (string).
+ */
+#define XEN_ELFNOTE_INFO           0
+
+/*
+ * The virtual address of the entry point (numeric).
+ *
+ * LEGACY: VIRT_ENTRY
+ */
+#define XEN_ELFNOTE_ENTRY          1
+
+/* The virtual address of the hypercall transfer page (numeric).
+ *
+ * LEGACY: HYPERCALL_PAGE. (n.b. legacy value is a physical page
+ * number not a virtual address)
+ */
+#define XEN_ELFNOTE_HYPERCALL_PAGE 2
+
+/* The virtual address where the kernel image should be mapped (numeric).
+ *
+ * Defaults to 0.
+ *
+ * LEGACY: VIRT_BASE
+ */
+#define XEN_ELFNOTE_VIRT_BASE      3
+
+/*
+ * The offset of the ELF paddr field from the actual required
+ * pseudo-physical address (numeric).
+ *
+ * This is used to maintain backwards compatibility with older kernels
+ * which wrote __PAGE_OFFSET into that field. This field defaults to 0
+ * if not present.
+ *
+ * LEGACY: ELF_PADDR_OFFSET. (n.b. legacy default is VIRT_BASE)
+ */
+#define XEN_ELFNOTE_PADDR_OFFSET   4
+
+/*
+ * The version of Xen that we work with (string).
+ *
+ * LEGACY: XEN_VER
+ */
+#define XEN_ELFNOTE_XEN_VERSION    5
+
+/*
+ * The name of the guest operating system (string).
+ *
+ * LEGACY: GUEST_OS
+ */
+#define XEN_ELFNOTE_GUEST_OS       6
+
+/*
+ * The version of the guest operating system (string).
+ *
+ * LEGACY: GUEST_VER
+ */
+#define XEN_ELFNOTE_GUEST_VERSION  7
+
+/*
+ * The loader type (string).
+ *
+ * LEGACY: LOADER
+ */
+#define XEN_ELFNOTE_LOADER         8
+
+/*
+ * The kernel supports PAE (x86/32 only, string = "yes", "no" or
+ * "bimodal").
+ *
+ * For compatibility with Xen 3.0.3 and earlier the "bimodal" setting
+ * may be given as "yes,bimodal" which will cause older Xen to treat
+ * this kernel as PAE.
+ *
+ * LEGACY: PAE (n.b. The legacy interface included a provision to
+ * indicate 'extended-cr3' support allowing L3 page tables to be
+ * placed above 4G. It is assumed that any kernel new enough to use
+ * these ELF notes will include this and therefore "yes" here is
+ * equivalent to "yes[entended-cr3]" in the __xen_guest interface.
+ */
+#define XEN_ELFNOTE_PAE_MODE       9
+
+/*
+ * The features supported/required by this kernel (string).
+ *
+ * The string must consist of a list of feature names (as given in
+ * features.h, without the "XENFEAT_" prefix) separated by '|'
+ * characters. If a feature is required for the kernel to function
+ * then the feature name must be preceded by a '!' character.
+ *
+ * LEGACY: FEATURES
+ */
+#define XEN_ELFNOTE_FEATURES      10
+
+/*
+ * The kernel requires the symbol table to be loaded (string = "yes" or "no")
+ * LEGACY: BSD_SYMTAB (n.b. The legacy treated the presence or absence
+ * of this string as a boolean flag rather than requiring "yes" or
+ * "no".
+ */
+#define XEN_ELFNOTE_BSD_SYMTAB    11
+
+/*
+ * The lowest address the hypervisor hole can begin at (numeric).
+ *
+ * This must not be set higher than HYPERVISOR_VIRT_START. Its presence
+ * also indicates to the hypervisor that the kernel can deal with the
+ * hole starting at a higher address.
+ */
+#define XEN_ELFNOTE_HV_START_LOW  12
+
+/*
+ * List of maddr_t-sized mask/value pairs describing how to recognize
+ * (non-present) L1 page table entries carrying valid MFNs (numeric).
+ */
+#define XEN_ELFNOTE_L1_MFN_VALID  13
+
+/*
+ * Whether or not the guest supports cooperative suspend cancellation.
+ * This is a numeric value.
+ *
+ * Default is 0
+ */
+#define XEN_ELFNOTE_SUSPEND_CANCEL 14
+
+/*
+ * The (non-default) location the initial phys-to-machine map should be
+ * placed at by the hypervisor (Dom0) or the tools (DomU).
+ * The kernel must be prepared for this mapping to be established using
+ * large pages, despite such otherwise not being available to guests.
+ * The kernel must also be able to handle the page table pages used for
+ * this mapping not being accessible through the initial mapping.
+ * (Only x86-64 supports this at present.)
+ */
+#define XEN_ELFNOTE_INIT_P2M      15
+
+/*
+ * Whether or not the guest can deal with being passed an initrd not
+ * mapped through its initial page tables.
+ */
+#define XEN_ELFNOTE_MOD_START_PFN 16
+
+/*
+ * The features supported by this kernel (numeric).
+ *
+ * Other than XEN_ELFNOTE_FEATURES on pre-4.2 Xen, this note allows a
+ * kernel to specify support for features that older hypervisors don't
+ * know about. The set of features 4.2 and newer hypervisors will
+ * consider supported by the kernel is the combination of the sets
+ * specified through this and the string note.
+ *
+ * LEGACY: FEATURES
+ */
+#define XEN_ELFNOTE_SUPPORTED_FEATURES 17
+
+/*
+ * Physical entry point into the kernel.
+ *
+ * 32bit entry point into the kernel. When requested to launch the
+ * guest kernel in a HVM container, Xen will use this entry point to
+ * launch the guest in 32bit protected mode with paging disabled.
+ * Ignored otherwise.
+ */
+#define XEN_ELFNOTE_PHYS32_ENTRY 18
+
+/*
+ * The number of the highest elfnote defined.
+ */
+#define XEN_ELFNOTE_MAX XEN_ELFNOTE_PHYS32_ENTRY
+
+/*
+ * System information exported through crash notes.
+ *
+ * The kexec / kdump code will create one XEN_ELFNOTE_CRASH_INFO
+ * note in case of a system crash. This note will contain various
+ * information about the system, see xen/include/xen/elfcore.h.
+ */
+#define XEN_ELFNOTE_CRASH_INFO 0x1000001
+
+/*
+ * System registers exported through crash notes.
+ *
+ * The kexec / kdump code will create one XEN_ELFNOTE_CRASH_REGS
+ * note per cpu in case of a system crash. This note is architecture
+ * specific and will contain registers not saved in the "CORE" note.
+ * See xen/include/xen/elfcore.h for more information.
+ */
+#define XEN_ELFNOTE_CRASH_REGS 0x1000002
+
+
+/*
+ * xen dump-core none note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_NONE
+ * in its dump file to indicate that the file is xen dump-core
+ * file. This note doesn't have any other information.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_NONE               0x2000000
+
+/*
+ * xen dump-core header note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_HEADER
+ * in its dump file.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_HEADER             0x2000001
+
+/*
+ * xen dump-core xen version note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_XEN_VERSION
+ * in its dump file. It contains the xen version obtained via the
+ * XENVER hypercall.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_XEN_VERSION        0x2000002
+
+/*
+ * xen dump-core format version note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_FORMAT_VERSION
+ * in its dump file. It contains a format version identifier.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_FORMAT_VERSION     0x2000003
+
+#endif /* __XEN_PUBLIC_ELFNOTE_H__ */

include/xen/elfnote.h line 281

Quoted 10 lines
include/xen/elfnote.h new file +10 -0
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

I am not sure that Emacs footer should be copied to GRUB2 source.

Daniel

author maintainer
#23 lore raw

Re: [PATCH v3 03/10] xen: add elfnote.h to avoid using numbers instead of constants

Quoted 358 lines · Daniel Kiper
Quoted 8 lines · Juergen Gross

Various features and parameters of a pv-kernel are specified via elf notes in the kernel image. Those notes are part of the interface between the Xen hypervisor and the kernel.

Instead of using num,bers in the code when interpreting the elf notes make use of the header supplied by Xen for that purpose.

Yummy! Just one nitpick.

Quoted 344 lines
---
 grub-core/loader/i386/xen_fileXX.c |  19 +--
 include/xen/elfnote.h              | 281 +++++++++++++++++++++++++++++++++++++
 2 files changed, 291 insertions(+), 9 deletions(-)
 create mode 100644 include/xen/elfnote.h
grub-core/loader/i386/xen_fileXX.c +10 -9
index 5475819..1f7f71d 100644
@@ -18,6 +18,7 @@
 
 #include <grub/xen_file.h>
 #include <grub/misc.h>
+#include <xen/elfnote.h>
 
 static grub_err_t
 parse_xen_guest (grub_elf_t elf, struct grub_xen_file_info *xi,
@@ -201,35 +202,35 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
       xi->has_note = 1;
       switch (nh->n_type)
 	{
-	case 1:
+	case XEN_ELFNOTE_ENTRY:
 	  xi->entry_point = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 2:
+	case XEN_ELFNOTE_HYPERCALL_PAGE:
 	  xi->hypercall_page = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  xi->has_hypercall_page = 1;
 	  break;
-	case 3:
+	case XEN_ELFNOTE_VIRT_BASE:
 	  xi->virt_base = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 4:
+	case XEN_ELFNOTE_PADDR_OFFSET:
 	  xi->paddr_offset = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
 	  break;
-	case 5:
+	case XEN_ELFNOTE_XEN_VERSION:
 	  grub_dprintf ("xen", "xenversion = `%s'\n", (char *) desc);
 	  break;
-	case 6:
+	case XEN_ELFNOTE_GUEST_OS:
 	  grub_dprintf ("xen", "name = `%s'\n", (char *) desc);
 	  break;
-	case 7:
+	case XEN_ELFNOTE_GUEST_VERSION:
 	  grub_dprintf ("xen", "version = `%s'\n", (char *) desc);
 	  break;
-	case 8:
+	case XEN_ELFNOTE_LOADER:
 	  if (descsz < 7
 	      || grub_memcmp (desc, "generic", descsz == 7 ? 7 : 8) != 0)
 	    return grub_error (GRUB_ERR_BAD_OS, "invalid loader");
 	  break;
 	  /* PAE */
-	case 9:
+	case XEN_ELFNOTE_PAE_MODE:
 	  grub_dprintf ("xen", "pae = `%s', %d, %d\n", (char *) desc,
 			xi->arch, descsz);
 	  if (xi->arch != GRUB_XEN_FILE_I386
include/xen/elfnote.h new file +275 -0
new file mode 100644
index 0000000..353985f
@@ -0,0 +1,281 @@
+/******************************************************************************
+ * elfnote.h
+ *
+ * Definitions used for the Xen ELF notes.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2006, Ian Campbell, XenSource Ltd.
+ */
+
+#ifndef __XEN_PUBLIC_ELFNOTE_H__
+#define __XEN_PUBLIC_ELFNOTE_H__
+
+/*
+ * `incontents 200 elfnotes ELF notes
+ *
+ * The notes should live in a PT_NOTE segment and have "Xen" in the
+ * name field.
+ *
+ * Numeric types are either 4 or 8 bytes depending on the content of
+ * the desc field.
+ *
+ * LEGACY indicated the fields in the legacy __xen_guest string which
+ * this a note type replaces.
+ *
+ * String values (for non-legacy) are NULL terminated ASCII, also known
+ * as ASCIZ type.
+ */
+
+/*
+ * NAME=VALUE pair (string).
+ */
+#define XEN_ELFNOTE_INFO           0
+
+/*
+ * The virtual address of the entry point (numeric).
+ *
+ * LEGACY: VIRT_ENTRY
+ */
+#define XEN_ELFNOTE_ENTRY          1
+
+/* The virtual address of the hypercall transfer page (numeric).
+ *
+ * LEGACY: HYPERCALL_PAGE. (n.b. legacy value is a physical page
+ * number not a virtual address)
+ */
+#define XEN_ELFNOTE_HYPERCALL_PAGE 2
+
+/* The virtual address where the kernel image should be mapped (numeric).
+ *
+ * Defaults to 0.
+ *
+ * LEGACY: VIRT_BASE
+ */
+#define XEN_ELFNOTE_VIRT_BASE      3
+
+/*
+ * The offset of the ELF paddr field from the actual required
+ * pseudo-physical address (numeric).
+ *
+ * This is used to maintain backwards compatibility with older kernels
+ * which wrote __PAGE_OFFSET into that field. This field defaults to 0
+ * if not present.
+ *
+ * LEGACY: ELF_PADDR_OFFSET. (n.b. legacy default is VIRT_BASE)
+ */
+#define XEN_ELFNOTE_PADDR_OFFSET   4
+
+/*
+ * The version of Xen that we work with (string).
+ *
+ * LEGACY: XEN_VER
+ */
+#define XEN_ELFNOTE_XEN_VERSION    5
+
+/*
+ * The name of the guest operating system (string).
+ *
+ * LEGACY: GUEST_OS
+ */
+#define XEN_ELFNOTE_GUEST_OS       6
+
+/*
+ * The version of the guest operating system (string).
+ *
+ * LEGACY: GUEST_VER
+ */
+#define XEN_ELFNOTE_GUEST_VERSION  7
+
+/*
+ * The loader type (string).
+ *
+ * LEGACY: LOADER
+ */
+#define XEN_ELFNOTE_LOADER         8
+
+/*
+ * The kernel supports PAE (x86/32 only, string = "yes", "no" or
+ * "bimodal").
+ *
+ * For compatibility with Xen 3.0.3 and earlier the "bimodal" setting
+ * may be given as "yes,bimodal" which will cause older Xen to treat
+ * this kernel as PAE.
+ *
+ * LEGACY: PAE (n.b. The legacy interface included a provision to
+ * indicate 'extended-cr3' support allowing L3 page tables to be
+ * placed above 4G. It is assumed that any kernel new enough to use
+ * these ELF notes will include this and therefore "yes" here is
+ * equivalent to "yes[entended-cr3]" in the __xen_guest interface.
+ */
+#define XEN_ELFNOTE_PAE_MODE       9
+
+/*
+ * The features supported/required by this kernel (string).
+ *
+ * The string must consist of a list of feature names (as given in
+ * features.h, without the "XENFEAT_" prefix) separated by '|'
+ * characters. If a feature is required for the kernel to function
+ * then the feature name must be preceded by a '!' character.
+ *
+ * LEGACY: FEATURES
+ */
+#define XEN_ELFNOTE_FEATURES      10
+
+/*
+ * The kernel requires the symbol table to be loaded (string = "yes" or "no")
+ * LEGACY: BSD_SYMTAB (n.b. The legacy treated the presence or absence
+ * of this string as a boolean flag rather than requiring "yes" or
+ * "no".
+ */
+#define XEN_ELFNOTE_BSD_SYMTAB    11
+
+/*
+ * The lowest address the hypervisor hole can begin at (numeric).
+ *
+ * This must not be set higher than HYPERVISOR_VIRT_START. Its presence
+ * also indicates to the hypervisor that the kernel can deal with the
+ * hole starting at a higher address.
+ */
+#define XEN_ELFNOTE_HV_START_LOW  12
+
+/*
+ * List of maddr_t-sized mask/value pairs describing how to recognize
+ * (non-present) L1 page table entries carrying valid MFNs (numeric).
+ */
+#define XEN_ELFNOTE_L1_MFN_VALID  13
+
+/*
+ * Whether or not the guest supports cooperative suspend cancellation.
+ * This is a numeric value.
+ *
+ * Default is 0
+ */
+#define XEN_ELFNOTE_SUSPEND_CANCEL 14
+
+/*
+ * The (non-default) location the initial phys-to-machine map should be
+ * placed at by the hypervisor (Dom0) or the tools (DomU).
+ * The kernel must be prepared for this mapping to be established using
+ * large pages, despite such otherwise not being available to guests.
+ * The kernel must also be able to handle the page table pages used for
+ * this mapping not being accessible through the initial mapping.
+ * (Only x86-64 supports this at present.)
+ */
+#define XEN_ELFNOTE_INIT_P2M      15
+
+/*
+ * Whether or not the guest can deal with being passed an initrd not
+ * mapped through its initial page tables.
+ */
+#define XEN_ELFNOTE_MOD_START_PFN 16
+
+/*
+ * The features supported by this kernel (numeric).
+ *
+ * Other than XEN_ELFNOTE_FEATURES on pre-4.2 Xen, this note allows a
+ * kernel to specify support for features that older hypervisors don't
+ * know about. The set of features 4.2 and newer hypervisors will
+ * consider supported by the kernel is the combination of the sets
+ * specified through this and the string note.
+ *
+ * LEGACY: FEATURES
+ */
+#define XEN_ELFNOTE_SUPPORTED_FEATURES 17
+
+/*
+ * Physical entry point into the kernel.
+ *
+ * 32bit entry point into the kernel. When requested to launch the
+ * guest kernel in a HVM container, Xen will use this entry point to
+ * launch the guest in 32bit protected mode with paging disabled.
+ * Ignored otherwise.
+ */
+#define XEN_ELFNOTE_PHYS32_ENTRY 18
+
+/*
+ * The number of the highest elfnote defined.
+ */
+#define XEN_ELFNOTE_MAX XEN_ELFNOTE_PHYS32_ENTRY
+
+/*
+ * System information exported through crash notes.
+ *
+ * The kexec / kdump code will create one XEN_ELFNOTE_CRASH_INFO
+ * note in case of a system crash. This note will contain various
+ * information about the system, see xen/include/xen/elfcore.h.
+ */
+#define XEN_ELFNOTE_CRASH_INFO 0x1000001
+
+/*
+ * System registers exported through crash notes.
+ *
+ * The kexec / kdump code will create one XEN_ELFNOTE_CRASH_REGS
+ * note per cpu in case of a system crash. This note is architecture
+ * specific and will contain registers not saved in the "CORE" note.
+ * See xen/include/xen/elfcore.h for more information.
+ */
+#define XEN_ELFNOTE_CRASH_REGS 0x1000002
+
+
+/*
+ * xen dump-core none note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_NONE
+ * in its dump file to indicate that the file is xen dump-core
+ * file. This note doesn't have any other information.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_NONE               0x2000000
+
+/*
+ * xen dump-core header note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_HEADER
+ * in its dump file.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_HEADER             0x2000001
+
+/*
+ * xen dump-core xen version note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_XEN_VERSION
+ * in its dump file. It contains the xen version obtained via the
+ * XENVER hypercall.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_XEN_VERSION        0x2000002
+
+/*
+ * xen dump-core format version note.
+ * xm dump-core code will create one XEN_ELFNOTE_DUMPCORE_FORMAT_VERSION
+ * in its dump file. It contains a format version identifier.
+ * See tools/libxc/xc_core.h for more information.
+ */
+#define XEN_ELFNOTE_DUMPCORE_FORMAT_VERSION     0x2000003
+
+#endif /* __XEN_PUBLIC_ELFNOTE_H__ */
+
+/*
+ * Local variables:
+ * mode: C
Quoted 8 lines

include/xen/elfnote.h line 281

Quoted 6 lines
include/xen/elfnote.h new file +6 -0
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

I am not sure that Emacs footer should be copied to GRUB2 source.

Other xen include files contain it, too.

Juergen

#24 lore raw

Re: [PATCH v3 03/10] xen: add elfnote.h to avoid using numbers instead of constants

Quoted 20 lines · Juergen Gross
Quoted 19 lines · Daniel Kiper
Quoted 8 lines · Juergen Gross

Various features and parameters of a pv-kernel are specified via elf notes in the kernel image. Those notes are part of the interface between the Xen hypervisor and the kernel.

Instead of using num,bers in the code when interpreting the elf notes make use of the header supplied by Xen for that purpose.

Yummy! Just one nitpick.

Quoted 5 lines
---
 grub-core/loader/i386/xen_fileXX.c |  19 +--
 include/xen/elfnote.h              | 281 +++++++++++++++++++++++++++++++++++++
 2 files changed, 291 insertions(+), 9 deletions(-)
 create mode 100644 include/xen/elfnote.h

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

Quoted 3 lines
Quoted 1 line

I am not sure that Emacs footer should be copied to GRUB2 source.

Other xen include files contain it, too.

If maintainer do not complain I will do not object.

Daniel

#25 lore raw

Re: [PATCH v3 03/10] xen: add elfnote.h to avoid using numbers instead of constants

In reply to Juergen Gross
Quoted 20 lines · Juergen Gross
Quoted 19 lines · Daniel Kiper
Quoted 8 lines · Juergen Gross

Various features and parameters of a pv-kernel are specified via elf notes in the kernel image. Those notes are part of the interface between the Xen hypervisor and the kernel.

Instead of using num,bers in the code when interpreting the elf notes make use of the header supplied by Xen for that purpose.

Yummy! Just one nitpick.

Quoted 5 lines
---
 grub-core/loader/i386/xen_fileXX.c |  19 +--
 include/xen/elfnote.h              | 281 +++++++++++++++++++++++++++++++++++++
 2 files changed, 291 insertions(+), 9 deletions(-)
 create mode 100644 include/xen/elfnote.h

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

Quoted 3 lines
Quoted 1 line

I am not sure that Emacs footer should be copied to GRUB2 source.

Other xen include files contain it, too.

If maintainer do not complain I will do not object.

Daniel

author maintainer
#26 lore raw

PATCH v3 4/10 xen: synchronize xen header

In reply to Juergen Gross

Get actual version of include/xen/xen.h from the Xen repository in order to be able to use constants defined there.

---
 include/xen/arch-x86/xen-x86_32.h |  22 +++----
 include/xen/arch-x86/xen-x86_64.h |   8 +--
 include/xen/xen.h                 | 125 +++++++++++++++++++++++++++-----------
 3 files changed, 105 insertions(+), 50 deletions(-)
include/xen/arch-x86/xen-x86_32.h +10 -12
index 1504191..7eca6cd 100644
@@ -58,34 +58,31 @@
 #define __HYPERVISOR_VIRT_START_PAE    0xF5800000
 #define __MACH2PHYS_VIRT_START_PAE     0xF5800000
 #define __MACH2PHYS_VIRT_END_PAE       0xF6800000
-#define HYPERVISOR_VIRT_START_PAE      \
-    mk_unsigned_long(__HYPERVISOR_VIRT_START_PAE)
-#define MACH2PHYS_VIRT_START_PAE       \
-    mk_unsigned_long(__MACH2PHYS_VIRT_START_PAE)
-#define MACH2PHYS_VIRT_END_PAE         \
-    mk_unsigned_long(__MACH2PHYS_VIRT_END_PAE)
+#define HYPERVISOR_VIRT_START_PAE      xen_mk_ulong(__HYPERVISOR_VIRT_START_PAE)
+#define MACH2PHYS_VIRT_START_PAE       xen_mk_ulong(__MACH2PHYS_VIRT_START_PAE)
+#define MACH2PHYS_VIRT_END_PAE         xen_mk_ulong(__MACH2PHYS_VIRT_END_PAE)
 
 /* Non-PAE bounds are obsolete. */
 #define __HYPERVISOR_VIRT_START_NONPAE 0xFC000000
 #define __MACH2PHYS_VIRT_START_NONPAE  0xFC000000
 #define __MACH2PHYS_VIRT_END_NONPAE    0xFC400000
 #define HYPERVISOR_VIRT_START_NONPAE   \
-    mk_unsigned_long(__HYPERVISOR_VIRT_START_NONPAE)
+    xen_mk_ulong(__HYPERVISOR_VIRT_START_NONPAE)
 #define MACH2PHYS_VIRT_START_NONPAE    \
-    mk_unsigned_long(__MACH2PHYS_VIRT_START_NONPAE)
+    xen_mk_ulong(__MACH2PHYS_VIRT_START_NONPAE)
 #define MACH2PHYS_VIRT_END_NONPAE      \
-    mk_unsigned_long(__MACH2PHYS_VIRT_END_NONPAE)
+    xen_mk_ulong(__MACH2PHYS_VIRT_END_NONPAE)
 
 #define __HYPERVISOR_VIRT_START __HYPERVISOR_VIRT_START_PAE
 #define __MACH2PHYS_VIRT_START  __MACH2PHYS_VIRT_START_PAE
 #define __MACH2PHYS_VIRT_END    __MACH2PHYS_VIRT_END_PAE
 
 #ifndef HYPERVISOR_VIRT_START
-#define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START)
+#define HYPERVISOR_VIRT_START xen_mk_ulong(__HYPERVISOR_VIRT_START)
 #endif
 
-#define MACH2PHYS_VIRT_START  mk_unsigned_long(__MACH2PHYS_VIRT_START)
-#define MACH2PHYS_VIRT_END    mk_unsigned_long(__MACH2PHYS_VIRT_END)
+#define MACH2PHYS_VIRT_START  xen_mk_ulong(__MACH2PHYS_VIRT_START)
+#define MACH2PHYS_VIRT_END    xen_mk_ulong(__MACH2PHYS_VIRT_END)
 #define MACH2PHYS_NR_ENTRIES  ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>2)
 #ifndef machine_to_phys_mapping
 #define machine_to_phys_mapping ((unsigned long *)MACH2PHYS_VIRT_START)
@@ -104,6 +101,7 @@
     do { if ( sizeof(hnd) == 8 ) *(uint64_t *)&(hnd) = 0;   \
          (hnd).p = val;                                     \
     } while ( 0 )
+#define  int64_aligned_t  int64_t __attribute__((aligned(8)))
 #define uint64_aligned_t uint64_t __attribute__((aligned(8)))
 #define __XEN_GUEST_HANDLE_64(name) __guest_handle_64_ ## name
 #define XEN_GUEST_HANDLE_64(name) __XEN_GUEST_HANDLE_64(name)
include/xen/arch-x86/xen-x86_64.h +4 -4
index 1c4e159..5e18613 100644
@@ -76,12 +76,12 @@
 #define __MACH2PHYS_VIRT_END    0xFFFF804000000000
 
 #ifndef HYPERVISOR_VIRT_START
-#define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START)
-#define HYPERVISOR_VIRT_END   mk_unsigned_long(__HYPERVISOR_VIRT_END)
+#define HYPERVISOR_VIRT_START xen_mk_ulong(__HYPERVISOR_VIRT_START)
+#define HYPERVISOR_VIRT_END   xen_mk_ulong(__HYPERVISOR_VIRT_END)
 #endif
 
-#define MACH2PHYS_VIRT_START  mk_unsigned_long(__MACH2PHYS_VIRT_START)
-#define MACH2PHYS_VIRT_END    mk_unsigned_long(__MACH2PHYS_VIRT_END)
+#define MACH2PHYS_VIRT_START  xen_mk_ulong(__MACH2PHYS_VIRT_START)
+#define MACH2PHYS_VIRT_END    xen_mk_ulong(__MACH2PHYS_VIRT_END)
 #define MACH2PHYS_NR_ENTRIES  ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3)
 #ifndef machine_to_phys_mapping
 #define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START)
include/xen/xen.h +91 -34

230 lines not on this page · raw message

Signature · 1 line
2.6.2
author maintainer
#28 lore raw

PATCH v3 5/10 xen: factor out p2m list allocation into separate function

In reply to Juergen Gross

Do the p2m list allocation of the to be loaded kernel in a separate function. This will allow doing the p2m list allocation at different times of the boot preparations depending on the features the kernel is supporting.

While at this remove superfluous setting of first_p2m_pfn and nr_p2m_frames as those are needed only in case of the p2m list not being mapped by the initial kernel mapping.

---
 grub-core/loader/i386/xen.c | 87 ++++++++++++++++++++++++++-------------------
 1 file changed, 50 insertions(+), 37 deletions(-)
grub-core/loader/i386/xen.c +50 -37
index d5fe168..ca293ac 100644
@@ -44,8 +44,10 @@ GRUB_MOD_LICENSE ("GPLv3+");
 
 struct xen_loader_state {
   struct grub_relocator *relocator;
+  struct grub_relocator_xen_state state;
   struct start_info next_start;
   struct grub_xen_file_info xen_inf;
+  grub_xen_mfn_t *virt_mfn_list;
   grub_uint64_t max_addr;
   struct xen_multiboot_mod_list *module_info_page;
   grub_uint64_t modules_target_start;
@@ -170,7 +172,7 @@ generate_page_table (grub_uint64_t *where, grub_uint64_t paging_start,
 }
 
 static grub_err_t
-set_mfns (grub_xen_mfn_t * new_mfn_list, grub_xen_mfn_t pfn)
+set_mfns (grub_xen_mfn_t pfn)
 {
   grub_xen_mfn_t i, t;
   grub_xen_mfn_t cn_pfn = -1, st_pfn = -1;
@@ -179,32 +181,34 @@ set_mfns (grub_xen_mfn_t * new_mfn_list, grub_xen_mfn_t pfn)
 
   for (i = 0; i < grub_xen_start_page_addr->nr_pages; i++)
     {
-      if (new_mfn_list[i] == grub_xen_start_page_addr->console.domU.mfn)
+      if (xen_state.virt_mfn_list[i] ==
+	  grub_xen_start_page_addr->console.domU.mfn)
 	cn_pfn = i;
-      if (new_mfn_list[i] == grub_xen_start_page_addr->store_mfn)
+      if (xen_state.virt_mfn_list[i] == grub_xen_start_page_addr->store_mfn)
 	st_pfn = i;
     }
   if (cn_pfn == (grub_xen_mfn_t)-1)
     return grub_error (GRUB_ERR_BUG, "no console");
   if (st_pfn == (grub_xen_mfn_t)-1)
     return grub_error (GRUB_ERR_BUG, "no store");
-  t = new_mfn_list[pfn];
-  new_mfn_list[pfn] = new_mfn_list[cn_pfn];
-  new_mfn_list[cn_pfn] = t;
-  t = new_mfn_list[pfn + 1];
-  new_mfn_list[pfn + 1] = new_mfn_list[st_pfn];
-  new_mfn_list[st_pfn] = t;
+  t = xen_state.virt_mfn_list[pfn];
+  xen_state.virt_mfn_list[pfn] = xen_state.virt_mfn_list[cn_pfn];
+  xen_state.virt_mfn_list[cn_pfn] = t;
+  t = xen_state.virt_mfn_list[pfn + 1];
+  xen_state.virt_mfn_list[pfn + 1] = xen_state.virt_mfn_list[st_pfn];
+  xen_state.virt_mfn_list[st_pfn] = t;
 
-  m2p_updates[0].ptr = page2offset (new_mfn_list[pfn]) | MMU_MACHPHYS_UPDATE;
+  m2p_updates[0].ptr =
+    page2offset (xen_state.virt_mfn_list[pfn]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[0].val = pfn;
   m2p_updates[1].ptr =
-    page2offset (new_mfn_list[pfn + 1]) | MMU_MACHPHYS_UPDATE;
+    page2offset (xen_state.virt_mfn_list[pfn + 1]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[1].val = pfn + 1;
   m2p_updates[2].ptr =
-    page2offset (new_mfn_list[cn_pfn]) | MMU_MACHPHYS_UPDATE;
+    page2offset (xen_state.virt_mfn_list[cn_pfn]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[2].val = cn_pfn;
   m2p_updates[3].ptr =
-    page2offset (new_mfn_list[st_pfn]) | MMU_MACHPHYS_UPDATE;
+    page2offset (xen_state.virt_mfn_list[st_pfn]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[3].val = st_pfn;
 
   grub_xen_mmu_update (m2p_updates, 4, NULL, DOMID_SELF);
@@ -213,43 +217,52 @@ set_mfns (grub_xen_mfn_t * new_mfn_list, grub_xen_mfn_t pfn)
 }
 
 static grub_err_t
+grub_xen_p2m_alloc (void)
+{
+  grub_relocator_chunk_t ch;
+  grub_size_t p2msize;
+  grub_err_t err;
+
+  xen_state.state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  p2msize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, p2msize);
+  if (err)
+    return err;
+  xen_state.virt_mfn_list = get_virtual_current_address (ch);
+  grub_memcpy (xen_state.virt_mfn_list,
+	       (void *) grub_xen_start_page_addr->mfn_list, p2msize);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + p2msize, PAGE_SIZE);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
 grub_xen_boot (void)
 {
-  struct grub_relocator_xen_state state;
   grub_relocator_chunk_t ch;
   grub_err_t err;
-  grub_size_t pgtsize;
   struct start_info *nst;
   grub_uint64_t nr_info_pages;
   grub_uint64_t nr_pages, nr_pt_pages, nr_need_pages;
   struct gnttab_set_version gnttab_setver;
-  grub_xen_mfn_t *new_mfn_list;
   grub_size_t i;
 
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = xen_state.max_addr;
-  xen_state.next_start.mfn_list =
-    xen_state.max_addr + xen_state.xen_inf.virt_base;
-  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
-  pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
-					 xen_state.max_addr, pgtsize);
-  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_xen_p2m_alloc ();
   if (err)
     return err;
-  new_mfn_list = get_virtual_current_address (ch);
-  grub_memcpy (new_mfn_list,
-	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
   err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
 					 xen_state.max_addr,
 					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
   xen_state.max_addr =
     ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
@@ -262,14 +275,14 @@ grub_xen_boot (void)
   xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
   xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
+  err = set_mfns (xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
   xen_state.max_addr += 2 * PAGE_SIZE;
 
   xen_state.next_start.pt_base =
     xen_state.max_addr + xen_state.xen_inf.virt_base;
-  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
+  xen_state.state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
   nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
@@ -298,15 +311,15 @@ grub_xen_boot (void)
 
   generate_page_table (get_virtual_current_address (ch),
 		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_state.xen_inf.virt_base, new_mfn_list);
+		       xen_state.xen_inf.virt_base, xen_state.virt_mfn_list);
 
   xen_state.max_addr += page2offset (nr_pt_pages);
-  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
-  state.entry_point = xen_state.xen_inf.entry_point;
+  xen_state.state.stack =
+    xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  xen_state.state.entry_point = xen_state.xen_inf.entry_point;
 
-  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
   xen_state.next_start.nr_pt_frames = nr_pt_pages;
-  state.paging_size = nr_pt_pages;
+  xen_state.state.paging_size = nr_pt_pages;
 
   *nst = xen_state.next_start;
 
@@ -318,7 +331,7 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+  return grub_relocator_xen_boot (xen_state.relocator, xen_state.state, nr_pages,
 				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
Signature · 1 line
2.6.2
author maintainer
#29 lore raw

PATCH v3 5/10 xen: factor out p2m list allocation into separate function

In reply to Juergen Gross

Do the p2m list allocation of the to be loaded kernel in a separate function. This will allow doing the p2m list allocation at different times of the boot preparations depending on the features the kernel is supporting.

While at this remove superfluous setting of first_p2m_pfn and nr_p2m_frames as those are needed only in case of the p2m list not being mapped by the initial kernel mapping.

---
 grub-core/loader/i386/xen.c | 87 ++++++++++++++++++++++++++-------------------
 1 file changed, 50 insertions(+), 37 deletions(-)
grub-core/loader/i386/xen.c +50 -37
index d5fe168..ca293ac 100644
@@ -44,8 +44,10 @@ GRUB_MOD_LICENSE ("GPLv3+");
 
 struct xen_loader_state {
   struct grub_relocator *relocator;
+  struct grub_relocator_xen_state state;
   struct start_info next_start;
   struct grub_xen_file_info xen_inf;
+  grub_xen_mfn_t *virt_mfn_list;
   grub_uint64_t max_addr;
   struct xen_multiboot_mod_list *module_info_page;
   grub_uint64_t modules_target_start;
@@ -170,7 +172,7 @@ generate_page_table (grub_uint64_t *where, grub_uint64_t paging_start,
 }
 
 static grub_err_t
-set_mfns (grub_xen_mfn_t * new_mfn_list, grub_xen_mfn_t pfn)
+set_mfns (grub_xen_mfn_t pfn)
 {
   grub_xen_mfn_t i, t;
   grub_xen_mfn_t cn_pfn = -1, st_pfn = -1;
@@ -179,32 +181,34 @@ set_mfns (grub_xen_mfn_t * new_mfn_list, grub_xen_mfn_t pfn)
 
   for (i = 0; i < grub_xen_start_page_addr->nr_pages; i++)
     {
-      if (new_mfn_list[i] == grub_xen_start_page_addr->console.domU.mfn)
+      if (xen_state.virt_mfn_list[i] ==
+	  grub_xen_start_page_addr->console.domU.mfn)
 	cn_pfn = i;
-      if (new_mfn_list[i] == grub_xen_start_page_addr->store_mfn)
+      if (xen_state.virt_mfn_list[i] == grub_xen_start_page_addr->store_mfn)
 	st_pfn = i;
     }
   if (cn_pfn == (grub_xen_mfn_t)-1)
     return grub_error (GRUB_ERR_BUG, "no console");
   if (st_pfn == (grub_xen_mfn_t)-1)
     return grub_error (GRUB_ERR_BUG, "no store");
-  t = new_mfn_list[pfn];
-  new_mfn_list[pfn] = new_mfn_list[cn_pfn];
-  new_mfn_list[cn_pfn] = t;
-  t = new_mfn_list[pfn + 1];
-  new_mfn_list[pfn + 1] = new_mfn_list[st_pfn];
-  new_mfn_list[st_pfn] = t;
+  t = xen_state.virt_mfn_list[pfn];
+  xen_state.virt_mfn_list[pfn] = xen_state.virt_mfn_list[cn_pfn];
+  xen_state.virt_mfn_list[cn_pfn] = t;
+  t = xen_state.virt_mfn_list[pfn + 1];
+  xen_state.virt_mfn_list[pfn + 1] = xen_state.virt_mfn_list[st_pfn];
+  xen_state.virt_mfn_list[st_pfn] = t;
 
-  m2p_updates[0].ptr = page2offset (new_mfn_list[pfn]) | MMU_MACHPHYS_UPDATE;
+  m2p_updates[0].ptr =
+    page2offset (xen_state.virt_mfn_list[pfn]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[0].val = pfn;
   m2p_updates[1].ptr =
-    page2offset (new_mfn_list[pfn + 1]) | MMU_MACHPHYS_UPDATE;
+    page2offset (xen_state.virt_mfn_list[pfn + 1]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[1].val = pfn + 1;
   m2p_updates[2].ptr =
-    page2offset (new_mfn_list[cn_pfn]) | MMU_MACHPHYS_UPDATE;
+    page2offset (xen_state.virt_mfn_list[cn_pfn]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[2].val = cn_pfn;
   m2p_updates[3].ptr =
-    page2offset (new_mfn_list[st_pfn]) | MMU_MACHPHYS_UPDATE;
+    page2offset (xen_state.virt_mfn_list[st_pfn]) | MMU_MACHPHYS_UPDATE;
   m2p_updates[3].val = st_pfn;
 
   grub_xen_mmu_update (m2p_updates, 4, NULL, DOMID_SELF);
@@ -213,43 +217,52 @@ set_mfns (grub_xen_mfn_t * new_mfn_list, grub_xen_mfn_t pfn)
 }
 
 static grub_err_t
+grub_xen_p2m_alloc (void)
+{
+  grub_relocator_chunk_t ch;
+  grub_size_t p2msize;
+  grub_err_t err;
+
+  xen_state.state.mfn_list = xen_state.max_addr;
+  xen_state.next_start.mfn_list =
+    xen_state.max_addr + xen_state.xen_inf.virt_base;
+  p2msize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
+					 xen_state.max_addr, p2msize);
+  if (err)
+    return err;
+  xen_state.virt_mfn_list = get_virtual_current_address (ch);
+  grub_memcpy (xen_state.virt_mfn_list,
+	       (void *) grub_xen_start_page_addr->mfn_list, p2msize);
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + p2msize, PAGE_SIZE);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
 grub_xen_boot (void)
 {
-  struct grub_relocator_xen_state state;
   grub_relocator_chunk_t ch;
   grub_err_t err;
-  grub_size_t pgtsize;
   struct start_info *nst;
   grub_uint64_t nr_info_pages;
   grub_uint64_t nr_pages, nr_pt_pages, nr_need_pages;
   struct gnttab_set_version gnttab_setver;
-  grub_xen_mfn_t *new_mfn_list;
   grub_size_t i;
 
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  state.mfn_list = xen_state.max_addr;
-  xen_state.next_start.mfn_list =
-    xen_state.max_addr + xen_state.xen_inf.virt_base;
-  xen_state.next_start.first_p2m_pfn = xen_state.max_addr >> PAGE_SHIFT;
-  pgtsize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
-  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
-					 xen_state.max_addr, pgtsize);
-  xen_state.next_start.nr_p2m_frames = (pgtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
+  err = grub_xen_p2m_alloc ();
   if (err)
     return err;
-  new_mfn_list = get_virtual_current_address (ch);
-  grub_memcpy (new_mfn_list,
-	       (void *) grub_xen_start_page_addr->mfn_list, pgtsize);
-  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + pgtsize, PAGE_SIZE);
 
   err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
 					 xen_state.max_addr,
 					 sizeof (xen_state.next_start));
   if (err)
     return err;
-  state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
+  xen_state.state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
   nst = get_virtual_current_address (ch);
   xen_state.max_addr =
     ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
@@ -262,14 +275,14 @@ grub_xen_boot (void)
   xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
   xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (new_mfn_list, xen_state.max_addr >> PAGE_SHIFT);
+  err = set_mfns (xen_state.max_addr >> PAGE_SHIFT);
   if (err)
     return err;
   xen_state.max_addr += 2 * PAGE_SIZE;
 
   xen_state.next_start.pt_base =
     xen_state.max_addr + xen_state.xen_inf.virt_base;
-  state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
+  xen_state.state.paging_start = xen_state.max_addr >> PAGE_SHIFT;
 
   nr_info_pages = xen_state.max_addr >> PAGE_SHIFT;
   nr_pages = nr_info_pages;
@@ -298,15 +311,15 @@ grub_xen_boot (void)
 
   generate_page_table (get_virtual_current_address (ch),
 		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_state.xen_inf.virt_base, new_mfn_list);
+		       xen_state.xen_inf.virt_base, xen_state.virt_mfn_list);
 
   xen_state.max_addr += page2offset (nr_pt_pages);
-  state.stack = xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
-  state.entry_point = xen_state.xen_inf.entry_point;
+  xen_state.state.stack =
+    xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
+  xen_state.state.entry_point = xen_state.xen_inf.entry_point;
 
-  xen_state.next_start.nr_p2m_frames += nr_pt_pages;
   xen_state.next_start.nr_pt_frames = nr_pt_pages;
-  state.paging_size = nr_pt_pages;
+  xen_state.state.paging_size = nr_pt_pages;
 
   *nst = xen_state.next_start;
 
@@ -318,7 +331,7 @@ grub_xen_boot (void)
   for (i = 0; i < ARRAY_SIZE (grub_xen_shared_info->evtchn_pending); i++)
     grub_xen_shared_info->evtchn_pending[i] = 0;
 
-  return grub_relocator_xen_boot (xen_state.relocator, state, nr_pages,
+  return grub_relocator_xen_boot (xen_state.relocator, xen_state.state, nr_pages,
 				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
 				  nr_pages - 1,
Signature · 1 line
2.6.2
Reviewed-by on v3 5/10
#30 lore raw

Re: [PATCH v3 05/10] xen: factor out p2m list allocation into separate function

Quoted 10 lines · Juergen Gross

Do the p2m list allocation of the to be loaded kernel in a separate function. This will allow doing the p2m list allocation at different times of the boot preparations depending on the features the kernel is supporting.

While at this remove superfluous setting of first_p2m_pfn and nr_p2m_frames as those are needed only in case of the p2m list not being mapped by the initial kernel mapping.

Daniel

Reviewed-by on v3 5/10
#31 lore raw

Re: [PATCH v3 05/10] xen: factor out p2m list allocation into separate function

In reply to Juergen Gross
Quoted 10 lines · Juergen Gross

Do the p2m list allocation of the to be loaded kernel in a separate function. This will allow doing the p2m list allocation at different times of the boot preparations depending on the features the kernel is supporting.

While at this remove superfluous setting of first_p2m_pfn and nr_p2m_frames as those are needed only in case of the p2m list not being mapped by the initial kernel mapping.

Daniel

author maintainer
#32 lore raw

PATCH v3 6/10 xen: factor out allocation of special pages into separate function

In reply to Juergen Gross

Do the allocation of special pages (start info, console and xenbus ring buffers) in a separate function. This will allow to do the allocation at different times of the boot preparations depending on the features the kernel is supporting.

---
 grub-core/loader/i386/xen.c | 48 +++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 17 deletions(-)
grub-core/loader/i386/xen.c +31 -17
index ca293ac..69d0e65 100644
@@ -48,6 +48,8 @@ struct xen_loader_state {
   struct start_info next_start;
   struct grub_xen_file_info xen_inf;
   grub_xen_mfn_t *virt_mfn_list;
+  struct start_info *virt_start_info;
+  grub_xen_mfn_t console_pfn;
   grub_uint64_t max_addr;
   struct xen_multiboot_mod_list *module_info_page;
   grub_uint64_t modules_target_start;
@@ -240,22 +242,10 @@ grub_xen_p2m_alloc (void)
 }
 
 static grub_err_t
-grub_xen_boot (void)
+grub_xen_special_alloc (void)
 {
   grub_relocator_chunk_t ch;
   grub_err_t err;
-  struct start_info *nst;
-  grub_uint64_t nr_info_pages;
-  grub_uint64_t nr_pages, nr_pt_pages, nr_need_pages;
-  struct gnttab_set_version gnttab_setver;
-  grub_size_t i;
-
-  if (grub_xen_n_allocated_shared_pages)
-    return grub_error (GRUB_ERR_BUG, "active grants");
-
-  err = grub_xen_p2m_alloc ();
-  if (err)
-    return err;
 
   err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
 					 xen_state.max_addr,
@@ -263,9 +253,11 @@ grub_xen_boot (void)
   if (err)
     return err;
   xen_state.state.start_info = xen_state.max_addr + xen_state.xen_inf.virt_base;
-  nst = get_virtual_current_address (ch);
+  xen_state.virt_start_info = get_virtual_current_address (ch);
   xen_state.max_addr =
     ALIGN_UP (xen_state.max_addr + sizeof (xen_state.next_start), PAGE_SIZE);
+  xen_state.console_pfn = xen_state.max_addr >> PAGE_SHIFT;
+  xen_state.max_addr += 2 * PAGE_SIZE;
 
   xen_state.next_start.nr_pages = grub_xen_start_page_addr->nr_pages;
   grub_memcpy (xen_state.next_start.magic, grub_xen_start_page_addr->magic,
@@ -275,10 +267,28 @@ grub_xen_boot (void)
   xen_state.next_start.console.domU = grub_xen_start_page_addr->console.domU;
   xen_state.next_start.shared_info = grub_xen_start_page_addr->shared_info;
 
-  err = set_mfns (xen_state.max_addr >> PAGE_SHIFT);
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_xen_boot (void)
+{
+  grub_relocator_chunk_t ch;
+  grub_err_t err;
+  grub_uint64_t nr_info_pages;
+  grub_uint64_t nr_pages, nr_pt_pages, nr_need_pages;
+  struct gnttab_set_version gnttab_setver;
+  grub_size_t i;
+
+  if (grub_xen_n_allocated_shared_pages)
+    return grub_error (GRUB_ERR_BUG, "active grants");
+
+  err = grub_xen_p2m_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_special_alloc ();
   if (err)
     return err;
-  xen_state.max_addr += 2 * PAGE_SIZE;
 
   xen_state.next_start.pt_base =
     xen_state.max_addr + xen_state.xen_inf.virt_base;
@@ -309,6 +319,10 @@ grub_xen_boot (void)
   if (err)
     return err;
 
+  err = set_mfns (xen_state.console_pfn);
+  if (err)
+    return err;
+
   generate_page_table (get_virtual_current_address (ch),
 		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
 		       xen_state.xen_inf.virt_base, xen_state.virt_mfn_list);
@@ -321,7 +335,7 @@ grub_xen_boot (void)
   xen_state.next_start.nr_pt_frames = nr_pt_pages;
   xen_state.state.paging_size = nr_pt_pages;
 
-  *nst = xen_state.next_start;
+  *xen_state.virt_start_info = xen_state.next_start;
 
   grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
 
Signature · 1 line
2.6.2
Reviewed-by on v3 6/10
#33 lore raw

Re: [PATCH v3 06/10] xen: factor out allocation of special pages into separate function

Quoted 6 lines · Juergen Gross

Do the allocation of special pages (start info, console and xenbus ring buffers) in a separate function. This will allow to do the allocation at different times of the boot preparations depending on the features the kernel is supporting.

Daniel

Reviewed-by on v3 6/10
#34 lore raw

Re: [PATCH v3 06/10] xen: factor out allocation of special pages into separate function

In reply to Juergen Gross
Quoted 6 lines · Juergen Gross

Do the allocation of special pages (start info, console and xenbus ring buffers) in a separate function. This will allow to do the allocation at different times of the boot preparations depending on the features the kernel is supporting.

Daniel

author maintainer
#35 lore raw

PATCH v3 7/10 xen: factor out allocation of page tables into separate function

In reply to Juergen Gross

Do the allocation of page tables in a separate function. This will allow to do the allocation at different times of the boot preparations depending on the features the kernel is supporting.

---
 grub-core/loader/i386/xen.c | 91 ++++++++++++++++++++++++++++-----------------
 1 file changed, 56 insertions(+), 35 deletions(-)
grub-core/loader/i386/xen.c +56 -35
index 69d0e65..3bcf4c8 100644
@@ -51,6 +51,9 @@ struct xen_loader_state {
   struct start_info *virt_start_info;
   grub_xen_mfn_t console_pfn;
   grub_uint64_t max_addr;
+  grub_uint64_t *virt_pgtable;
+  grub_uint64_t pgtbl_start;
+  grub_uint64_t pgtbl_end;
   struct xen_multiboot_mod_list *module_info_page;
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
@@ -110,17 +113,17 @@ get_pgtable_size (grub_uint64_t total_pages, grub_uint64_t virt_base)
 
 static void
 generate_page_table (grub_uint64_t *where, grub_uint64_t paging_start,
-		     grub_uint64_t total_pages, grub_uint64_t virt_base,
-		     grub_xen_mfn_t *mfn_list)
+		     grub_uint64_t paging_end, grub_uint64_t total_pages,
+		     grub_uint64_t virt_base, grub_xen_mfn_t *mfn_list)
 {
   if (!virt_base)
-    total_pages++;
+    paging_end++;
 
   grub_uint64_t lx[NUMBER_OF_LEVELS], lxs[NUMBER_OF_LEVELS];
   grub_uint64_t nlx, nls, sz = 0;
   int l;
 
-  nlx = total_pages;
+  nlx = paging_end;
   nls = virt_base >> PAGE_SHIFT;
   for (l = 0; l < NUMBER_OF_LEVELS; l++)
     {
@@ -164,7 +167,7 @@ generate_page_table (grub_uint64_t *where, grub_uint64_t paging_start,
   if (pr)
     pg += POINTERS_PER_PAGE;
 
-  for (j = 0; j < total_pages; j++)
+  for (j = 0; j < paging_end; j++)
     {
       if (j >= paging_start && j < lp)
 	pg[j + lxs[0]] = page2offset (mfn_list[j]) | 5;
@@ -271,24 +274,12 @@ grub_xen_special_alloc (void)
 }
 
 static grub_err_t
-grub_xen_boot (void)
+grub_xen_pt_alloc (void)
 {
   grub_relocator_chunk_t ch;
   grub_err_t err;
   grub_uint64_t nr_info_pages;
   grub_uint64_t nr_pages, nr_pt_pages, nr_need_pages;
-  struct gnttab_set_version gnttab_setver;
-  grub_size_t i;
-
-  if (grub_xen_n_allocated_shared_pages)
-    return grub_error (GRUB_ERR_BUG, "active grants");
-
-  err = grub_xen_p2m_alloc ();
-  if (err)
-    return err;
-  err = grub_xen_special_alloc ();
-  if (err)
-    return err;
 
   xen_state.next_start.pt_base =
     xen_state.max_addr + xen_state.xen_inf.virt_base;
@@ -309,31 +300,61 @@ grub_xen_boot (void)
       nr_pages = nr_need_pages;
     }
 
-  grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
-		(unsigned long long) xen_state.xen_inf.virt_base,
-		(unsigned long long) page2offset (nr_pages));
-
   err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
 					 xen_state.max_addr,
 					 page2offset (nr_pt_pages));
   if (err)
     return err;
 
-  err = set_mfns (xen_state.console_pfn);
-  if (err)
-    return err;
-
-  generate_page_table (get_virtual_current_address (ch),
-		       xen_state.max_addr >> PAGE_SHIFT, nr_pages,
-		       xen_state.xen_inf.virt_base, xen_state.virt_mfn_list);
-
+  xen_state.virt_pgtable = get_virtual_current_address (ch);
+  xen_state.pgtbl_start = xen_state.max_addr >> PAGE_SHIFT;
   xen_state.max_addr += page2offset (nr_pt_pages);
   xen_state.state.stack =
     xen_state.max_addr + STACK_SIZE + xen_state.xen_inf.virt_base;
-  xen_state.state.entry_point = xen_state.xen_inf.entry_point;
-
-  xen_state.next_start.nr_pt_frames = nr_pt_pages;
   xen_state.state.paging_size = nr_pt_pages;
+  xen_state.next_start.nr_pt_frames = nr_pt_pages;
+  xen_state.max_addr = page2offset (nr_pages);
+  xen_state.pgtbl_end = nr_pages;
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_xen_boot (void)
+{
+  grub_err_t err;
+  grub_uint64_t nr_pages;
+  struct gnttab_set_version gnttab_setver;
+  grub_size_t i;
+
+  if (grub_xen_n_allocated_shared_pages)
+    return grub_error (GRUB_ERR_BUG, "active grants");
+
+  err = grub_xen_p2m_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_special_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_pt_alloc ();
+  if (err)
+    return err;
+
+  err = set_mfns (xen_state.console_pfn);
+  if (err)
+    return err;
+
+  nr_pages = xen_state.max_addr >> PAGE_SHIFT;
+
+  grub_dprintf ("xen", "bootstrap domain %llx+%llx\n",
+		(unsigned long long) xen_state.xen_inf.virt_base,
+		(unsigned long long) page2offset (nr_pages));
+
+  generate_page_table (xen_state.virt_pgtable, xen_state.pgtbl_start,
+		       xen_state.pgtbl_end, nr_pages,
+		       xen_state.xen_inf.virt_base, xen_state.virt_mfn_list);
+
+  xen_state.state.entry_point = xen_state.xen_inf.entry_point;
 
   *xen_state.virt_start_info = xen_state.next_start;
 
@@ -348,8 +369,8 @@ grub_xen_boot (void)
   return grub_relocator_xen_boot (xen_state.relocator, xen_state.state, nr_pages,
 				  xen_state.xen_inf.virt_base <
 				  PAGE_SIZE ? page2offset (nr_pages) : 0,
-				  nr_pages - 1,
-				  page2offset (nr_pages - 1) +
+				  xen_state.pgtbl_end - 1,
+				  page2offset (xen_state.pgtbl_end - 1) +
 				  xen_state.xen_inf.virt_base);
 }
 
Signature · 1 line
2.6.2
Reviewed-by on v3 7/10
#36 lore raw

Re: [PATCH v3 07/10] xen: factor out allocation of page tables into separate function

Quoted 5 lines · Juergen Gross

Do the allocation of page tables in a separate function. This will allow to do the allocation at different times of the boot preparations depending on the features the kernel is supporting.

Daniel

Reviewed-by on v3 7/10
#37 lore raw

Re: [PATCH v3 07/10] xen: factor out allocation of page tables into separate function

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Do the allocation of page tables in a separate function. This will allow to do the allocation at different times of the boot preparations depending on the features the kernel is supporting.

Daniel

author maintainer
#38 lore raw

PATCH v3 8/10 xen: add capability to load initrd outside of initial mapping

In reply to Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +48 -13
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
 static grub_err_t
+grub_xen_alloc_end (void)
+{
+  grub_err_t err;
+
+  if (xen_state.alloc_end_called)
+    return GRUB_ERR_NONE;
+  xen_state.alloc_end_called = 1;
+
+  err = grub_xen_p2m_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_special_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_pt_alloc ();
+  if (err)
+    return err;
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
 grub_xen_boot (void)
 {
   grub_err_t err;
@@ -330,13 +353,7 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  err = grub_xen_p2m_alloc ();
-  if (err)
-    return err;
-  err = grub_xen_special_alloc ();
-  if (err)
-    return err;
-  err = grub_xen_pt_alloc ();
+  err = grub_xen_alloc_end ();
   if (err)
     return err;
 
@@ -609,6 +626,13 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  if (xen_state.xen_inf.unmapped_initrd)
+    {
+      err = grub_xen_alloc_end ();
+      if (err)
+	goto fail;
+    }
+
   if (grub_initrd_init (argc, argv, &initrd_ctx))
     goto fail;
 
@@ -626,14 +650,24 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  xen_state.next_start.mod_start =
-    xen_state.max_addr + xen_state.xen_inf.virt_base;
-  xen_state.next_start.mod_len = size;
-
-  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
+  if (xen_state.xen_inf.unmapped_initrd)
+    {
+      xen_state.next_start.flags |= SIF_MOD_START_PFN;
+      xen_state.next_start.mod_start = xen_state.max_addr >> PAGE_SHIFT;
+      xen_state.next_start.mod_len = size;
+    }
+  else
+    {
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;
+      xen_state.next_start.mod_len = size;
+    }
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
+		(unsigned) (xen_state.max_addr + xen_state.xen_inf.virt_base),
+		(unsigned) size);
+
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -685,6 +719,7 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
 
   if (!xen_state.module_info_page)
     {
+      xen_state.xen_inf.unmapped_initrd = 0;
       xen_state.n_modules = 0;
       xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
       xen_state.modules_target_start = xen_state.max_addr;
grub-core/loader/i386/xen_fileXX.c +3 -0
index 1f7f71d..d68634d 100644
@@ -259,6 +259,9 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
 					  descsz == 2 ? 2 : 3) == 0)
 	    xi->arch = GRUB_XEN_FILE_I386;
 	  break;
+	case XEN_ELFNOTE_MOD_START_PFN:
+	  xi->unmapped_initrd = !!grub_le_to_cpu32(*(grub_uint32_t *) desc);
+	  break;
 	default:
 	  grub_dprintf ("xen", "unknown note type %d\n", nh->n_type);
 	  break;
include/grub/xen_file.h +1 -0
index 4b2ccba..ed749fa 100644
@@ -36,6 +36,7 @@ struct grub_xen_file_info
   int has_note;
   int has_xen_guest;
   int extended_cr3;
+  int unmapped_initrd;
   enum
   {
     GRUB_XEN_FILE_I386 = 1,
Signature · 1 line
2.6.2
author maintainer
#39 lore raw

PATCH v3 8/10 xen: add capability to load initrd outside of initial mapping

In reply to Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +48 -13
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
 static grub_err_t
+grub_xen_alloc_end (void)
+{
+  grub_err_t err;
+
+  if (xen_state.alloc_end_called)
+    return GRUB_ERR_NONE;
+  xen_state.alloc_end_called = 1;
+
+  err = grub_xen_p2m_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_special_alloc ();
+  if (err)
+    return err;
+  err = grub_xen_pt_alloc ();
+  if (err)
+    return err;
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
 grub_xen_boot (void)
 {
   grub_err_t err;
@@ -330,13 +353,7 @@ grub_xen_boot (void)
   if (grub_xen_n_allocated_shared_pages)
     return grub_error (GRUB_ERR_BUG, "active grants");
 
-  err = grub_xen_p2m_alloc ();
-  if (err)
-    return err;
-  err = grub_xen_special_alloc ();
-  if (err)
-    return err;
-  err = grub_xen_pt_alloc ();
+  err = grub_xen_alloc_end ();
   if (err)
     return err;
 
@@ -609,6 +626,13 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  if (xen_state.xen_inf.unmapped_initrd)
+    {
+      err = grub_xen_alloc_end ();
+      if (err)
+	goto fail;
+    }
+
   if (grub_initrd_init (argc, argv, &initrd_ctx))
     goto fail;
 
@@ -626,14 +650,24 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 	goto fail;
     }
 
-  xen_state.next_start.mod_start =
-    xen_state.max_addr + xen_state.xen_inf.virt_base;
-  xen_state.next_start.mod_len = size;
-
-  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
+  if (xen_state.xen_inf.unmapped_initrd)
+    {
+      xen_state.next_start.flags |= SIF_MOD_START_PFN;
+      xen_state.next_start.mod_start = xen_state.max_addr >> PAGE_SHIFT;
+      xen_state.next_start.mod_len = size;
+    }
+  else
+    {
+      xen_state.next_start.mod_start =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;
+      xen_state.next_start.mod_len = size;
+    }
 
   grub_dprintf ("xen", "Initrd, addr=0x%x, size=0x%x\n",
-		(unsigned) xen_state.next_start.mod_start, (unsigned) size);
+		(unsigned) (xen_state.max_addr + xen_state.xen_inf.virt_base),
+		(unsigned) size);
+
+  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + size, PAGE_SIZE);
 
 fail:
   grub_initrd_close (&initrd_ctx);
@@ -685,6 +719,7 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
 
   if (!xen_state.module_info_page)
     {
+      xen_state.xen_inf.unmapped_initrd = 0;
       xen_state.n_modules = 0;
       xen_state.max_addr = ALIGN_UP (xen_state.max_addr, PAGE_SIZE);
       xen_state.modules_target_start = xen_state.max_addr;
grub-core/loader/i386/xen_fileXX.c +3 -0
index 1f7f71d..d68634d 100644
@@ -259,6 +259,9 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
 					  descsz == 2 ? 2 : 3) == 0)
 	    xi->arch = GRUB_XEN_FILE_I386;
 	  break;
+	case XEN_ELFNOTE_MOD_START_PFN:
+	  xi->unmapped_initrd = !!grub_le_to_cpu32(*(grub_uint32_t *) desc);
+	  break;
 	default:
 	  grub_dprintf ("xen", "unknown note type %d\n", nh->n_type);
 	  break;
include/grub/xen_file.h +1 -0
index 4b2ccba..ed749fa 100644
@@ -36,6 +36,7 @@ struct grub_xen_file_info
   int has_note;
   int has_xen_guest;
   int extended_cr3;
+  int unmapped_initrd;
   enum
   {
     GRUB_XEN_FILE_I386 = 1,
Signature · 1 line
2.6.2
Reviewed-by on v3 8/10
#40 lore raw

Re: [PATCH v3 08/10] xen: add capability to load initrd outside of initial mapping

Quoted 9 lines · Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

One nitpick.

Quoted 19 lines
---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +1 -0
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;

grub-core/loader/i386/xen.c grub_xen_pt_alloc (void) · line 324

Quoted 4 lines
grub-core/loader/i386/xen.c +1 -0
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
 static grub_err_t
+grub_xen_alloc_end (void)

Why is it called grub_xen_alloc_end()?
Could we use just grub_xen_alloc()?

Daniel

Reviewed-by on v3 8/10
#41 lore raw

Re: [PATCH v3 08/10] xen: add capability to load initrd outside of initial mapping

In reply to Juergen Gross
Quoted 9 lines · Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

One nitpick.

Quoted 19 lines
---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +1 -0
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;

grub-core/loader/i386/xen.c grub_xen_pt_alloc (void) · line 324

Quoted 4 lines
grub-core/loader/i386/xen.c +1 -0
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
 static grub_err_t
+grub_xen_alloc_end (void)

Why is it called grub_xen_alloc_end()?
Could we use just grub_xen_alloc()?

Daniel

author maintainer
#42 lore raw

Re: [PATCH v3 08/10] xen: add capability to load initrd outside of initial mapping

Quoted 36 lines · Daniel Kiper
Quoted 9 lines · Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

One nitpick.

Quoted 21 lines
---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +1 -0
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
Quoted 5 lines

grub-core/loader/i386/xen.c grub_xen_pt_alloc (void) · line 324

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 static grub_err_t
+grub_xen_alloc_end (void)

Why is it called grub_xen_alloc_end()?
Could we use just grub_xen_alloc()?

We could, of course. I just wanted to make clear that this function will do the allocations needed to be at the end of the allocation process. Naming it grub_xen_alloc() would make it occur to be a very basic allocation function, which just isn't true. What about grub_xen_alloc_rest()?

Juergen

author maintainer
#43 lore raw

Re: [PATCH v3 08/10] xen: add capability to load initrd outside of initial mapping

In reply to Daniel Kiper
Quoted 36 lines · Daniel Kiper
Quoted 9 lines · Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

One nitpick.

Quoted 21 lines
---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +1 -0
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
Quoted 5 lines

grub-core/loader/i386/xen.c grub_xen_pt_alloc (void) · line 324

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 static grub_err_t
+grub_xen_alloc_end (void)

Why is it called grub_xen_alloc_end()?
Could we use just grub_xen_alloc()?

We could, of course. I just wanted to make clear that this function will do the allocations needed to be at the end of the allocation process. Naming it grub_xen_alloc() would make it occur to be a very basic allocation function, which just isn't true. What about grub_xen_alloc_rest()?

Juergen

#44 lore raw

Re: [PATCH v3 08/10] xen: add capability to load initrd outside of initial mapping

Quoted 43 lines · Juergen Gross
Quoted 36 lines · Daniel Kiper
Quoted 9 lines · Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

One nitpick.

Quoted 21 lines
---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +1 -0
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
Quoted 5 lines

grub-core/loader/i386/xen.c grub_xen_pt_alloc (void) · line 324

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 static grub_err_t
+grub_xen_alloc_end (void)

Why is it called grub_xen_alloc_end()?
Could we use just grub_xen_alloc()?

Quoted 5 lines

We could, of course. I just wanted to make clear that this function will do the allocations needed to be at the end of the allocation process. Naming it grub_xen_alloc() would make it occur to be a very basic allocation function, which just isn't true. What about grub_xen_alloc_rest()?

My order of preference is: grub_xen_alloc_final() or grub_xen_alloc_last() or grub_xen_alloc_end() or grub_xen_alloc_rest().

Choose one which is best for you.

Daniel

#45 lore raw

Re: [PATCH v3 08/10] xen: add capability to load initrd outside of initial mapping

In reply to Juergen Gross
Quoted 43 lines · Juergen Gross
Quoted 36 lines · Daniel Kiper
Quoted 9 lines · Juergen Gross

Modern pvops linux kernels support an initrd not covered by the initial mapping. This capability is flagged by an elf-note.

In case the elf-note is set by the kernel don't place the initrd into the initial mapping. This will allow to load larger initrds and/or support domains with larger memory, as the initial mapping is limited to 2GB and it is containing the p2m list.

One nitpick.

Quoted 21 lines
---
 grub-core/loader/i386/xen.c        | 61 ++++++++++++++++++++++++++++++--------
 grub-core/loader/i386/xen_fileXX.c |  3 ++
 include/grub/xen_file.h            |  1 +
 3 files changed, 52 insertions(+), 13 deletions(-)
grub-core/loader/i386/xen.c +1 -0
index 3bcf4c8..7ac74f6 100644
@@ -58,6 +58,7 @@ struct xen_loader_state {
   grub_uint64_t modules_target_start;
   grub_size_t n_modules;
   int loaded;
+  int alloc_end_called;
 };
 
 static struct xen_loader_state xen_state;
@@ -320,6 +321,28 @@ grub_xen_pt_alloc (void)
 }
 
Quoted 5 lines

grub-core/loader/i386/xen.c grub_xen_pt_alloc (void) · line 324

Quoted 2 lines
grub-core/loader/i386/xen.c +1 -0
 static grub_err_t
+grub_xen_alloc_end (void)

Why is it called grub_xen_alloc_end()?
Could we use just grub_xen_alloc()?

Quoted 5 lines

We could, of course. I just wanted to make clear that this function will do the allocations needed to be at the end of the allocation process. Naming it grub_xen_alloc() would make it occur to be a very basic allocation function, which just isn't true. What about grub_xen_alloc_rest()?

My order of preference is: grub_xen_alloc_final() or grub_xen_alloc_last() or grub_xen_alloc_end() or grub_xen_alloc_rest().

Choose one which is best for you.

Daniel

author maintainer
#46 lore raw

PATCH v3 9/10 xen: modify page table construction

In reply to Juergen Gross

Modify the page table construction to allow multiple virtual regions to be mapped. This is done as preparation for removing the p2m list from the initial kernel mapping in order to support huge pv domains.

This allows a cleaner approach for mapping the relocator page by using this capability.

The interface to the assembler level of the relocator has to be changed in order to be able to process multiple page table areas.

---
V3: use constants instead of numbers as requested by Daniel Kiper
add lots of comments to assembly code as requested by Daniel Kiper

---
 grub-core/lib/i386/xen/relocator.S   |  87 ++++++----
 grub-core/lib/x86_64/xen/relocator.S | 134 ++++++---------
 grub-core/lib/xen/relocator.c        |  25 ++-
 grub-core/loader/i386/xen.c          | 325 ++++++++++++++++++++++++-----------
 include/grub/i386/memory.h           |   7 +
 include/grub/xen/relocator.h         |   6 +-
 6 files changed, 354 insertions(+), 230 deletions(-)
grub-core/lib/i386/xen/relocator.S +51 -36
index 694a54c..f1c729e 100644
@@ -16,6 +16,8 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/i386/memory.h>
+#include <grub/i386/types.h>
 #include <grub/symbol.h>
 #include <grub/xen.h>
 
@@ -23,78 +25,86 @@
 
 VARIABLE(grub_relocator_xen_remap_start)
 LOCAL(base):
-	/* mov imm32, %ebx */
+	/* Remap the remapper to it's new address. */
+	/* mov imm32, %ebx - %ebx: new virtual address of remapper */
 	.byte	0xbb
 VARIABLE(grub_relocator_xen_remapper_virt)
 	.long	0
 
-	/* mov imm32, %ecx */
+	/* mov imm32, %ecx - %ecx: low part of page table entry */
 	.byte	0xb9
 VARIABLE(grub_relocator_xen_remapper_map)
 	.long	0
 
-	/* mov imm32, %edx */
+	/* mov imm32, %edx  - %edx: high part of page table entry */
 	.byte	0xba
 VARIABLE(grub_relocator_xen_remapper_map_high)
 	.long	0
 
-	movl	%ebx, %ebp
+	movl	%ebx, %ebp	/* %ebx is clobbered by hypercall */
 
-	movl    $2, %esi
+	movl    $UVMF_INVLPG, %esi	/* esi: flags (inv. single entry) */
 	movl    $__HYPERVISOR_update_va_mapping, %eax
 	int     $0x82
 
 	movl	%ebp, %ebx
 	addl   $(LOCAL(cont) - LOCAL(base)), %ebx
 
-	jmp *%ebx
+	jmp *%ebx		/* Continue with new virtual address */
 
 LOCAL(cont):
-	xorl	%eax, %eax
-	movl	%eax, %ebp
+	/* Modify mappings of new page tables to be read-only. */
+	/* mov imm32, %eax */
+	.byte	0xb8
+VARIABLE(grub_relocator_xen_paging_areas_addr)
+	.long	0
+	movl	%eax, %ebx
 1:
+	movl	0(%ebx), %ebp	/* Get start pfn of the current area */
+	movl	GRUB_TARGET_SIZEOF_LONG(%ebx), %ecx	/* Get # of pg tables */
+	testl	%ecx, %ecx	/* 0 -> last area reached */
+	jz	3f
+	addl	$(2 * GRUB_TARGET_SIZEOF_LONG), %ebx
+	movl	%ebx, %esp	/* Save current area pointer */
 
+2:
+	movl	%ecx, %edi
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_mfn_list)
 	.long	0
-	movl	%eax, %edi
-	movl	%ebp, %eax
-	movl    0(%edi, %eax, 4), %ecx
-
-	/* mov imm32, %ebx */
-	.byte	0xbb
-VARIABLE(grub_relocator_xen_paging_start)
-	.long	0
-	shll	$12, %eax
-	addl	%eax, %ebx
+	movl    0(%eax, %ebp, 4), %ecx	/* mfn */
+	movl	%ebp, %ebx
+	shll	$PAGE_SHIFT, %ebx	/* virtual address (1:1 mapping) */
 	movl    %ecx, %edx
-	shll    $12,  %ecx
-	shrl    $20,  %edx
-	orl     $5, %ecx
-	movl    $2, %esi
+	shll    $PAGE_SHIFT,  %ecx	/* prepare pte low part */
+	shrl    $(32 - PAGE_SHIFT),  %edx	/* pte high part */
+	orl     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %ecx	/* pte low */
+	movl    $UVMF_INVLPG, %esi
 	movl    $__HYPERVISOR_update_va_mapping, %eax
-	int     $0x82
+	int     $0x82		/* parameters: eax, ebx, ecx, edx, esi */
 
-	incl	%ebp
-	/* mov imm32, %ecx */
-	.byte	0xb9
-VARIABLE(grub_relocator_xen_paging_size)
-	.long	0
-	cmpl	%ebp, %ecx
+	incl	%ebp		/* next pfn */
+	movl	%edi, %ecx
 
-	ja	1b
+	loop	2b
 
+	mov	%esp, %ebx	/* restore area poniter */
+	jmp	1b
+
+3:
+	/* Switch page tables: pin new L3 pt, load cr3, unpin old L3. */
 	/* mov imm32, %ebx */
 	.byte	0xbb
 VARIABLE(grub_relocator_xen_mmu_op_addr)
 	.long  0
-	movl   $3, %ecx
-	movl   $0, %edx
-	movl   $0x7FF0, %esi
+	movl   $3, %ecx		/* 3 mmu ops */
+	movl   $0, %edx		/* pdone (not used) */
+	movl   $DOMID_SELF, %esi
 	movl   $__HYPERVISOR_mmuext_op, %eax
 	int     $0x82
 
+	/* Continue in virtual kernel mapping. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_remap_continue)
@@ -102,6 +112,9 @@ VARIABLE(grub_relocator_xen_remap_continue)
 
 	jmp *%eax
 
+VARIABLE(grub_relocator_xen_paging_areas)
+	.long	0, 0, 0, 0, 0, 0, 0, 0
+
 VARIABLE(grub_relocator_xen_mmu_op)
 	.space 256
 
@@ -109,6 +122,7 @@ VARIABLE(grub_relocator_xen_remap_end)
 
 
 VARIABLE(grub_relocator_xen_start)
+	/* Unmap old remapper area. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_remapper_virt2)
@@ -116,14 +130,14 @@ VARIABLE(grub_relocator_xen_remapper_virt2)
 
 	movl    %eax, %edi
 
-	xorl    %ecx, %ecx
+	xorl    %ecx, %ecx	/* Invalid pte */
 	xorl    %edx, %edx
 
-	movl    $2, %esi
+	movl    $UVMF_INVLPG, %esi
 	movl    $__HYPERVISOR_update_va_mapping, %eax
 	int     $0x82
 
-
+	/* Prepare registers for starting kernel. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_stack)
@@ -145,6 +159,7 @@ VARIABLE(grub_relocator_xen_start_info)
 VARIABLE(grub_relocator_xen_entry_point)
 	.long	0
 
+	/* Now start the new kernel. */
 	jmp *%eax
 
 VARIABLE(grub_relocator_xen_end)
grub-core/lib/x86_64/xen/relocator.S +55 -79

196 lines not on this page · raw message

grub-core/lib/xen/relocator.c +17 -8
index 8f427d3..250fbd4 100644
@@ -36,15 +36,18 @@ extern grub_uint8_t grub_relocator_xen_remap_end;
 extern grub_xen_reg_t grub_relocator_xen_stack;
 extern grub_xen_reg_t grub_relocator_xen_start_info;
 extern grub_xen_reg_t grub_relocator_xen_entry_point;
-extern grub_xen_reg_t grub_relocator_xen_paging_start;
-extern grub_xen_reg_t grub_relocator_xen_paging_size;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt2;
 extern grub_xen_reg_t grub_relocator_xen_remapper_map;
 extern grub_xen_reg_t grub_relocator_xen_mfn_list;
+extern struct {
+  grub_xen_reg_t start;
+  grub_xen_reg_t size;
+} grub_relocator_xen_paging_areas[XEN_MAX_MAPPINGS];
 extern grub_xen_reg_t grub_relocator_xen_remap_continue;
 #ifdef __i386__
 extern grub_xen_reg_t grub_relocator_xen_mmu_op_addr;
+extern grub_xen_reg_t grub_relocator_xen_paging_areas_addr;
 extern grub_xen_reg_t grub_relocator_xen_remapper_map_high;
 #endif
 extern mmuext_op_t grub_relocator_xen_mmu_op[3];
@@ -61,6 +64,7 @@ grub_relocator_xen_boot (struct grub_relocator *rel,
 {
   grub_err_t err;
   void *relst;
+  int i;
   grub_relocator_chunk_t ch, ch_tramp;
   grub_xen_mfn_t *mfn_list =
     (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
@@ -77,8 +81,11 @@ grub_relocator_xen_boot (struct grub_relocator *rel,
   grub_relocator_xen_stack = state.stack;
   grub_relocator_xen_start_info = state.start_info;
   grub_relocator_xen_entry_point = state.entry_point;
-  grub_relocator_xen_paging_start = state.paging_start << 12;
-  grub_relocator_xen_paging_size = state.paging_size;
+  for (i = 0; i < XEN_MAX_MAPPINGS; i++)
+    {
+      grub_relocator_xen_paging_areas[i].start = state.paging_start[i];
+      grub_relocator_xen_paging_areas[i].size = state.paging_size[i];
+    }
   grub_relocator_xen_remapper_virt = remapper_virt;
   grub_relocator_xen_remapper_virt2 = remapper_virt;
   grub_relocator_xen_remap_continue = trampoline_virt;
@@ -88,10 +95,12 @@ grub_relocator_xen_boot (struct grub_relocator *rel,
   grub_relocator_xen_remapper_map_high = (mfn_list[remapper_pfn] >> 20);
   grub_relocator_xen_mmu_op_addr = (char *) &grub_relocator_xen_mmu_op
     - (char *) &grub_relocator_xen_remap_start + remapper_virt;
+  grub_relocator_xen_paging_areas_addr =
+    (char *) &grub_relocator_xen_paging_areas
+    - (char *) &grub_relocator_xen_remap_start + remapper_virt;
 #endif
 
-  grub_relocator_xen_mfn_list = state.mfn_list
-    + state.paging_start * sizeof (grub_addr_t);
+  grub_relocator_xen_mfn_list = state.mfn_list;
 
   grub_memset (grub_relocator_xen_mmu_op, 0,
 	       sizeof (grub_relocator_xen_mmu_op));
@@ -100,9 +109,9 @@ grub_relocator_xen_boot (struct grub_relocator *rel,
 #else
   grub_relocator_xen_mmu_op[0].cmd = MMUEXT_PIN_L4_TABLE;
 #endif
-  grub_relocator_xen_mmu_op[0].arg1.mfn = mfn_list[state.paging_start];
+  grub_relocator_xen_mmu_op[0].arg1.mfn = mfn_list[state.paging_start[0]];
   grub_relocator_xen_mmu_op[1].cmd = MMUEXT_NEW_BASEPTR;
-  grub_relocator_xen_mmu_op[1].arg1.mfn = mfn_list[state.paging_start];
+  grub_relocator_xen_mmu_op[1].arg1.mfn = mfn_list[state.paging_start[0]];
   grub_relocator_xen_mmu_op[2].cmd = MMUEXT_UNPIN_TABLE;
   grub_relocator_xen_mmu_op[2].arg1.mfn =
     mfn_list[grub_xen_start_page_addr->pt_base >> 12];
grub-core/loader/i386/xen.c +220 -105

395 lines not on this page · raw message

include/grub/i386/memory.h +7 -0
index c9b1328..8bb6e1c 100644
@@ -20,6 +20,8 @@
 #ifndef GRUB_MEMORY_CPU_HEADER
 #define GRUB_MEMORY_CPU_HEADER	1
 
+#define PAGE_SHIFT		12
+
 /* The flag for protected mode.  */
 #define GRUB_MEMORY_CPU_CR0_PE_ON		0x1
 #define GRUB_MEMORY_CPU_CR4_PAE_ON		0x00000020
@@ -31,6 +33,11 @@
 #define GRUB_MEMORY_MACHINE_UPPER_START			0x100000	/* 1 MiB */
 #define GRUB_MEMORY_MACHINE_LOWER_SIZE			GRUB_MEMORY_MACHINE_UPPER_START
 
+/* Some PTE definitions. */
+#define GRUB_PAGE_PRESENT			0x00000001
+#define GRUB_PAGE_RW				0x00000002
+#define GRUB_PAGE_USER				0x00000004
+
 #ifndef ASM_FILE
 
 #define GRUB_MMAP_MALLOC_LOW 1
include/grub/xen/relocator.h +4 -2
index ae45dce..35a0ad9 100644
@@ -23,11 +23,13 @@
 #include <grub/err.h>
 #include <grub/relocator.h>
 
+#define XEN_MAX_MAPPINGS 3
+
 struct grub_relocator_xen_state
 {
   grub_addr_t start_info;
-  grub_addr_t paging_start;
-  grub_addr_t paging_size;
+  grub_addr_t paging_start[XEN_MAX_MAPPINGS];
+  grub_addr_t paging_size[XEN_MAX_MAPPINGS];
   grub_addr_t mfn_list;
   grub_addr_t stack;
   grub_addr_t entry_point;
Signature · 1 line
2.6.2
#47 lore raw

Re: [PATCH v3 09/10] xen: modify page table construction

Quoted 274 lines · Juergen Gross

Modify the page table construction to allow multiple virtual regions to be mapped. This is done as preparation for removing the p2m list from the initial kernel mapping in order to support huge pv domains.

This allows a cleaner approach for mapping the relocator page by using this capability.

The interface to the assembler level of the relocator has to be changed in order to be able to process multiple page table areas.

---
V3: use constants instead of numbers as requested by Daniel Kiper
add lots of comments to assembly code as requested by Daniel Kiper

---
 grub-core/lib/i386/xen/relocator.S   |  87 ++++++----
 grub-core/lib/x86_64/xen/relocator.S | 134 ++++++---------
 grub-core/lib/xen/relocator.c        |  25 ++-
 grub-core/loader/i386/xen.c          | 325 ++++++++++++++++++++++++-----------
 include/grub/i386/memory.h           |   7 +
 include/grub/xen/relocator.h         |   6 +-
 6 files changed, 354 insertions(+), 230 deletions(-)
grub-core/lib/i386/xen/relocator.S +51 -36
index 694a54c..f1c729e 100644
@@ -16,6 +16,8 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/i386/memory.h>
+#include <grub/i386/types.h>
 #include <grub/symbol.h>
 #include <grub/xen.h>
 
@@ -23,78 +25,86 @@
 
 VARIABLE(grub_relocator_xen_remap_start)
 LOCAL(base):
-	/* mov imm32, %ebx */
+	/* Remap the remapper to it's new address. */
+	/* mov imm32, %ebx - %ebx: new virtual address of remapper */
 	.byte	0xbb
 VARIABLE(grub_relocator_xen_remapper_virt)
 	.long	0
 
-	/* mov imm32, %ecx */
+	/* mov imm32, %ecx - %ecx: low part of page table entry */
 	.byte	0xb9
 VARIABLE(grub_relocator_xen_remapper_map)
 	.long	0
 
-	/* mov imm32, %edx */
+	/* mov imm32, %edx  - %edx: high part of page table entry */
 	.byte	0xba
 VARIABLE(grub_relocator_xen_remapper_map_high)
 	.long	0
 
-	movl	%ebx, %ebp
+	movl	%ebx, %ebp	/* %ebx is clobbered by hypercall */
 
-	movl    $2, %esi
+	movl    $UVMF_INVLPG, %esi	/* esi: flags (inv. single entry) */
 	movl    $__HYPERVISOR_update_va_mapping, %eax
 	int     $0x82
 
 	movl	%ebp, %ebx
 	addl   $(LOCAL(cont) - LOCAL(base)), %ebx
 
-	jmp *%ebx
+	jmp *%ebx		/* Continue with new virtual address */
 
 LOCAL(cont):
-	xorl	%eax, %eax
-	movl	%eax, %ebp
+	/* Modify mappings of new page tables to be read-only. */
+	/* mov imm32, %eax */
+	.byte	0xb8
+VARIABLE(grub_relocator_xen_paging_areas_addr)
+	.long	0
+	movl	%eax, %ebx
 1:
+	movl	0(%ebx), %ebp	/* Get start pfn of the current area */
+	movl	GRUB_TARGET_SIZEOF_LONG(%ebx), %ecx	/* Get # of pg tables */
+	testl	%ecx, %ecx	/* 0 -> last area reached */
+	jz	3f
+	addl	$(2 * GRUB_TARGET_SIZEOF_LONG), %ebx
+	movl	%ebx, %esp	/* Save current area pointer */
 
+2:
+	movl	%ecx, %edi
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_mfn_list)
 	.long	0
-	movl	%eax, %edi
-	movl	%ebp, %eax
-	movl    0(%edi, %eax, 4), %ecx
-
-	/* mov imm32, %ebx */
-	.byte	0xbb
-VARIABLE(grub_relocator_xen_paging_start)
-	.long	0
-	shll	$12, %eax
-	addl	%eax, %ebx
+	movl    0(%eax, %ebp, 4), %ecx	/* mfn */
+	movl	%ebp, %ebx
+	shll	$PAGE_SHIFT, %ebx	/* virtual address (1:1 mapping) */
 	movl    %ecx, %edx
-	shll    $12,  %ecx
-	shrl    $20,  %edx
-	orl     $5, %ecx
-	movl    $2, %esi
+	shll    $PAGE_SHIFT,  %ecx	/* prepare pte low part */
+	shrl    $(32 - PAGE_SHIFT),  %edx	/* pte high part */
+	orl     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %ecx	/* pte low */
+	movl    $UVMF_INVLPG, %esi
 	movl    $__HYPERVISOR_update_va_mapping, %eax
-	int     $0x82
+	int     $0x82		/* parameters: eax, ebx, ecx, edx, esi */
 
-	incl	%ebp
-	/* mov imm32, %ecx */
-	.byte	0xb9
-VARIABLE(grub_relocator_xen_paging_size)
-	.long	0
-	cmpl	%ebp, %ecx
+	incl	%ebp		/* next pfn */
+	movl	%edi, %ecx
 
-	ja	1b
+	loop	2b
 
+	mov	%esp, %ebx	/* restore area poniter */
+	jmp	1b
+
+3:
+	/* Switch page tables: pin new L3 pt, load cr3, unpin old L3. */
 	/* mov imm32, %ebx */
 	.byte	0xbb
 VARIABLE(grub_relocator_xen_mmu_op_addr)
 	.long  0
-	movl   $3, %ecx
-	movl   $0, %edx
-	movl   $0x7FF0, %esi
+	movl   $3, %ecx		/* 3 mmu ops */
+	movl   $0, %edx		/* pdone (not used) */
+	movl   $DOMID_SELF, %esi
 	movl   $__HYPERVISOR_mmuext_op, %eax
 	int     $0x82
 
+	/* Continue in virtual kernel mapping. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_remap_continue)
@@ -102,6 +112,9 @@ VARIABLE(grub_relocator_xen_remap_continue)
 
 	jmp *%eax
 
+VARIABLE(grub_relocator_xen_paging_areas)
+	.long	0, 0, 0, 0, 0, 0, 0, 0
+
 VARIABLE(grub_relocator_xen_mmu_op)
 	.space 256
 
@@ -109,6 +122,7 @@ VARIABLE(grub_relocator_xen_remap_end)
 
 
 VARIABLE(grub_relocator_xen_start)
+	/* Unmap old remapper area. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_remapper_virt2)
@@ -116,14 +130,14 @@ VARIABLE(grub_relocator_xen_remapper_virt2)
 
 	movl    %eax, %edi
 
-	xorl    %ecx, %ecx
+	xorl    %ecx, %ecx	/* Invalid pte */
 	xorl    %edx, %edx
 
-	movl    $2, %esi
+	movl    $UVMF_INVLPG, %esi
 	movl    $__HYPERVISOR_update_va_mapping, %eax
 	int     $0x82
 
-
+	/* Prepare registers for starting kernel. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_stack)
@@ -145,6 +159,7 @@ VARIABLE(grub_relocator_xen_start_info)
 VARIABLE(grub_relocator_xen_entry_point)
 	.long	0
 
+	/* Now start the new kernel. */
 	jmp *%eax
 
 VARIABLE(grub_relocator_xen_end)
grub-core/lib/x86_64/xen/relocator.S +18 -29
index 92e9e72..f5364ed 100644
@@ -16,95 +16,85 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/x86_64/memory.h>
+#include <grub/x86_64/types.h>
 #include <grub/symbol.h>
 #include <grub/xen.h>
 
+/* Macro to load an imm64 value stored by the C-part into %rax: */
+#define MOV_IMM64_RAX(var) .byte 0x48, 0xb8; VARIABLE(var); .quad 0
+
 	.p2align	4	/* force 16-byte alignment */
 
 VARIABLE(grub_relocator_xen_remap_start)
 LOCAL(base):
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remapper_virt)
-	.quad	0
+	/* Remap the remapper to it's new address. */
+	MOV_IMM64_RAX(grub_relocator_xen_remapper_virt)
 
-	movq    %rax, %rdi
-	movq    %rax, %rbx
+	movq    %rax, %rdi	/* %rdi: new virtual address of remapper */
+	movq    %rax, %rbx	/* Remember new virtual address */
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remapper_map)
-	.quad	0
+	MOV_IMM64_RAX(grub_relocator_xen_remapper_map)
 
-	movq    %rax, %rsi
+	movq    %rax, %rsi	/* %rsi: page table entry */
 
-	movq    $2, %rdx
+	movq    $UVMF_INVLPG, %rdx	/* %rdx: flags (inv. single entry) */
 	movq    $__HYPERVISOR_update_va_mapping, %rax
-	syscall
+	syscall			/* Do the remap operation */
 
 	addq   $(LOCAL(cont) - LOCAL(base)), %rbx
 
-	jmp *%rbx
+	jmp *%rbx		/* Continue with new virtual address */
 
 LOCAL(cont):
-
-	/* mov imm64, %rcx */
-	.byte 	0x48
-	.byte	0xb9
-VARIABLE(grub_relocator_xen_paging_size)
-	.quad	0
+	/* Modify mappings of new page tables to be read-only. */
+	MOV_IMM64_RAX(grub_relocator_xen_mfn_list)
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_paging_start)
-	.quad	0
+	movq	%rax, %rbx	/* %rbx is the base of the p2m list */
+	leaq	EXT_C(grub_relocator_xen_paging_areas) (%rip), %r8
 
-	movq	%rax, %r12
-

grub-core/lib/x86_64/xen/relocator.S line 58

Quoted 10 lines
grub-core/lib/x86_64/xen/relocator.S +2 -7
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_mfn_list)
-	.quad	0
-
-	movq	%rax, %rsi
 1:
+	movq	0(%r8), %r12	/* Get start pfn of the current area */
+	movq	GRUB_TARGET_SIZEOF_LONG(%r8), %rcx	/* Get # of pg tables */

Use %r9 here and...

Quoted 6 lines
grub-core/lib/x86_64/xen/relocator.S +3 -2
+	testq	%rcx, %rcx	/* 0 -> last area reached */
+	jz	3f
+2:
 	movq	%r12, %rdi
-	movq    %rsi, %rbx
-	movq    0(%rsi), %rsi

grub-core/lib/x86_64/xen/relocator.S line 68

Quoted 10 lines
grub-core/lib/x86_64/xen/relocator.S +6 -4
-	shlq    $12,  %rsi
-	orq     $5, %rsi
-	movq    $2, %rdx
-	movq    %rcx, %r9
+	shlq	$PAGE_SHIFT, %rdi	/* virtual address (1:1 mapping) */
+	movq    (%rbx, %r12, 8), %rsi	/* mfn */
+	shlq    $PAGE_SHIFT,  %rsi
+	orq     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %rsi	/* Build pte */
+	movq    $UVMF_INVLPG, %rdx
+	movq    %rcx, %r9	/* %rcx clobbered by hypercall */

... you can avoid this...

grub-core/lib/x86_64/xen/relocator.S line 72

Quoted 4 lines
grub-core/lib/x86_64/xen/relocator.S +0 -0
 	movq    $__HYPERVISOR_update_va_mapping, %rax
 	syscall
 
 	movq    %r9, %rcx

and this...

Quoted 106 lines
grub-core/lib/x86_64/xen/relocator.S +26 -37
-	addq    $8, %rbx
-	addq    $4096, %r12
-	movq    %rbx, %rsi
+	incq	%r12		/* next pfn */
 
-	loop 1b
+	loop 2b
 
-	leaq   LOCAL(mmu_op) (%rip), %rdi
-	movq   $3, %rsi
-	movq   $0, %rdx
-	movq   $0x7FF0, %r10
+	addq	$(2 * GRUB_TARGET_SIZEOF_LONG), %r8	/* next pg table area */
+	jmp	1b
+
+3:
+	/* Switch page tables: pin new L4 pt, load cr3, unpin old L4. */
+	leaq   EXT_C(grub_relocator_xen_mmu_op) (%rip), %rdi
+	movq   $3, %rsi		/* 3 mmu ops */
+	movq   $0, %rdx		/* pdone (not used) */
+	movq   $DOMID_SELF, %r10
 	movq   $__HYPERVISOR_mmuext_op, %rax
 	syscall
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remap_continue)
-	.quad	0
+	/* Continue in virtual kernel mapping. */
+	MOV_IMM64_RAX(grub_relocator_xen_remap_continue)
 
 	jmp *%rax
 
-LOCAL(mmu_op):
+VARIABLE(grub_relocator_xen_paging_areas)
+	/* array of start, size pairs, size 0 is end marker */
+	.quad	0, 0, 0, 0, 0, 0, 0, 0
+
 VARIABLE(grub_relocator_xen_mmu_op)
 	.space 256
 
@@ -112,46 +102,32 @@ VARIABLE(grub_relocator_xen_remap_end)
 
 
 VARIABLE(grub_relocator_xen_start)
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remapper_virt2)
-	.quad	0
+	/* Unmap old remapper area. */
+	MOV_IMM64_RAX(grub_relocator_xen_remapper_virt2)
 
 	movq    %rax, %rdi
 
-	xorq    %rax, %rax
+	xorq    %rax, %rax	/* Invalid pte */
 	movq    %rax, %rsi
 
-	movq    $2, %rdx
+	movq    $UVMF_INVLPG, %rdx
 	movq    $__HYPERVISOR_update_va_mapping, %rax
 	syscall
 
-
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_stack)
-	.quad	0
+	/* Prepare registers for starting kernel. */
+	MOV_IMM64_RAX(grub_relocator_xen_stack)
 
 	movq	%rax, %rsp
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_start_info)
-	.quad	0
+	MOV_IMM64_RAX(grub_relocator_xen_start_info)
 
 	movq	%rax, %rsi
 
 	cld
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_entry_point)
-	.quad	0
+	MOV_IMM64_RAX(grub_relocator_xen_entry_point)
 
+	/* Now start the new kernel. */
 	jmp *%rax
 
 VARIABLE(grub_relocator_xen_end)
grub-core/lib/xen/relocator.c +0 -0
index 8f427d3..250fbd4 100644
@@ -36,15 +36,18 @@ extern grub_uint8_t grub_relocator_xen_remap_end;
 extern grub_xen_reg_t grub_relocator_xen_stack;
 extern grub_xen_reg_t grub_relocator_xen_start_info;
 extern grub_xen_reg_t grub_relocator_xen_entry_point;

grub-core/lib/xen/relocator.c extern grub_uint8_t grub_relocator_xen_remap_end; · line 46

Quoted 10 lines
grub-core/lib/xen/relocator.c +4 -2
-extern grub_xen_reg_t grub_relocator_xen_paging_start;
-extern grub_xen_reg_t grub_relocator_xen_paging_size;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt2;
 extern grub_xen_reg_t grub_relocator_xen_remapper_map;
 extern grub_xen_reg_t grub_relocator_xen_mfn_list;
+extern struct {
+  grub_xen_reg_t start;
+  grub_xen_reg_t size;
+} grub_relocator_xen_paging_areas[XEN_MAX_MAPPINGS];

Should not you add GRUB_PACKED here? Could you define type earlier and use it here?

Daniel

#48 lore raw

Re: [PATCH v3 09/10] xen: modify page table construction

In reply to Juergen Gross
Quoted 274 lines · Juergen Gross

Modify the page table construction to allow multiple virtual regions to be mapped. This is done as preparation for removing the p2m list from the initial kernel mapping in order to support huge pv domains.

This allows a cleaner approach for mapping the relocator page by using this capability.

The interface to the assembler level of the relocator has to be changed in order to be able to process multiple page table areas.

---
V3: use constants instead of numbers as requested by Daniel Kiper
add lots of comments to assembly code as requested by Daniel Kiper

---
 grub-core/lib/i386/xen/relocator.S   |  87 ++++++----
 grub-core/lib/x86_64/xen/relocator.S | 134 ++++++---------
 grub-core/lib/xen/relocator.c        |  25 ++-
 grub-core/loader/i386/xen.c          | 325 ++++++++++++++++++++++++-----------
 include/grub/i386/memory.h           |   7 +
 include/grub/xen/relocator.h         |   6 +-
 6 files changed, 354 insertions(+), 230 deletions(-)
grub-core/lib/i386/xen/relocator.S +51 -36
index 694a54c..f1c729e 100644
@@ -16,6 +16,8 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/i386/memory.h>
+#include <grub/i386/types.h>
 #include <grub/symbol.h>
 #include <grub/xen.h>
 
@@ -23,78 +25,86 @@
 
 VARIABLE(grub_relocator_xen_remap_start)
 LOCAL(base):
-	/* mov imm32, %ebx */
+	/* Remap the remapper to it's new address. */
+	/* mov imm32, %ebx - %ebx: new virtual address of remapper */
 	.byte	0xbb
 VARIABLE(grub_relocator_xen_remapper_virt)
 	.long	0
 
-	/* mov imm32, %ecx */
+	/* mov imm32, %ecx - %ecx: low part of page table entry */
 	.byte	0xb9
 VARIABLE(grub_relocator_xen_remapper_map)
 	.long	0
 
-	/* mov imm32, %edx */
+	/* mov imm32, %edx  - %edx: high part of page table entry */
 	.byte	0xba
 VARIABLE(grub_relocator_xen_remapper_map_high)
 	.long	0
 
-	movl	%ebx, %ebp
+	movl	%ebx, %ebp	/* %ebx is clobbered by hypercall */
 
-	movl    $2, %esi
+	movl    $UVMF_INVLPG, %esi	/* esi: flags (inv. single entry) */
 	movl    $__HYPERVISOR_update_va_mapping, %eax
 	int     $0x82
 
 	movl	%ebp, %ebx
 	addl   $(LOCAL(cont) - LOCAL(base)), %ebx
 
-	jmp *%ebx
+	jmp *%ebx		/* Continue with new virtual address */
 
 LOCAL(cont):
-	xorl	%eax, %eax
-	movl	%eax, %ebp
+	/* Modify mappings of new page tables to be read-only. */
+	/* mov imm32, %eax */
+	.byte	0xb8
+VARIABLE(grub_relocator_xen_paging_areas_addr)
+	.long	0
+	movl	%eax, %ebx
 1:
+	movl	0(%ebx), %ebp	/* Get start pfn of the current area */
+	movl	GRUB_TARGET_SIZEOF_LONG(%ebx), %ecx	/* Get # of pg tables */
+	testl	%ecx, %ecx	/* 0 -> last area reached */
+	jz	3f
+	addl	$(2 * GRUB_TARGET_SIZEOF_LONG), %ebx
+	movl	%ebx, %esp	/* Save current area pointer */
 
+2:
+	movl	%ecx, %edi
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_mfn_list)
 	.long	0
-	movl	%eax, %edi
-	movl	%ebp, %eax
-	movl    0(%edi, %eax, 4), %ecx
-
-	/* mov imm32, %ebx */
-	.byte	0xbb
-VARIABLE(grub_relocator_xen_paging_start)
-	.long	0
-	shll	$12, %eax
-	addl	%eax, %ebx
+	movl    0(%eax, %ebp, 4), %ecx	/* mfn */
+	movl	%ebp, %ebx
+	shll	$PAGE_SHIFT, %ebx	/* virtual address (1:1 mapping) */
 	movl    %ecx, %edx
-	shll    $12,  %ecx
-	shrl    $20,  %edx
-	orl     $5, %ecx
-	movl    $2, %esi
+	shll    $PAGE_SHIFT,  %ecx	/* prepare pte low part */
+	shrl    $(32 - PAGE_SHIFT),  %edx	/* pte high part */
+	orl     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %ecx	/* pte low */
+	movl    $UVMF_INVLPG, %esi
 	movl    $__HYPERVISOR_update_va_mapping, %eax
-	int     $0x82
+	int     $0x82		/* parameters: eax, ebx, ecx, edx, esi */
 
-	incl	%ebp
-	/* mov imm32, %ecx */
-	.byte	0xb9
-VARIABLE(grub_relocator_xen_paging_size)
-	.long	0
-	cmpl	%ebp, %ecx
+	incl	%ebp		/* next pfn */
+	movl	%edi, %ecx
 
-	ja	1b
+	loop	2b
 
+	mov	%esp, %ebx	/* restore area poniter */
+	jmp	1b
+
+3:
+	/* Switch page tables: pin new L3 pt, load cr3, unpin old L3. */
 	/* mov imm32, %ebx */
 	.byte	0xbb
 VARIABLE(grub_relocator_xen_mmu_op_addr)
 	.long  0
-	movl   $3, %ecx
-	movl   $0, %edx
-	movl   $0x7FF0, %esi
+	movl   $3, %ecx		/* 3 mmu ops */
+	movl   $0, %edx		/* pdone (not used) */
+	movl   $DOMID_SELF, %esi
 	movl   $__HYPERVISOR_mmuext_op, %eax
 	int     $0x82
 
+	/* Continue in virtual kernel mapping. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_remap_continue)
@@ -102,6 +112,9 @@ VARIABLE(grub_relocator_xen_remap_continue)
 
 	jmp *%eax
 
+VARIABLE(grub_relocator_xen_paging_areas)
+	.long	0, 0, 0, 0, 0, 0, 0, 0
+
 VARIABLE(grub_relocator_xen_mmu_op)
 	.space 256
 
@@ -109,6 +122,7 @@ VARIABLE(grub_relocator_xen_remap_end)
 
 
 VARIABLE(grub_relocator_xen_start)
+	/* Unmap old remapper area. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_remapper_virt2)
@@ -116,14 +130,14 @@ VARIABLE(grub_relocator_xen_remapper_virt2)
 
 	movl    %eax, %edi
 
-	xorl    %ecx, %ecx
+	xorl    %ecx, %ecx	/* Invalid pte */
 	xorl    %edx, %edx
 
-	movl    $2, %esi
+	movl    $UVMF_INVLPG, %esi
 	movl    $__HYPERVISOR_update_va_mapping, %eax
 	int     $0x82
 
-
+	/* Prepare registers for starting kernel. */
 	/* mov imm32, %eax */
 	.byte	0xb8
 VARIABLE(grub_relocator_xen_stack)
@@ -145,6 +159,7 @@ VARIABLE(grub_relocator_xen_start_info)
 VARIABLE(grub_relocator_xen_entry_point)
 	.long	0
 
+	/* Now start the new kernel. */
 	jmp *%eax
 
 VARIABLE(grub_relocator_xen_end)
grub-core/lib/x86_64/xen/relocator.S +18 -29
index 92e9e72..f5364ed 100644
@@ -16,95 +16,85 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/x86_64/memory.h>
+#include <grub/x86_64/types.h>
 #include <grub/symbol.h>
 #include <grub/xen.h>
 
+/* Macro to load an imm64 value stored by the C-part into %rax: */
+#define MOV_IMM64_RAX(var) .byte 0x48, 0xb8; VARIABLE(var); .quad 0
+
 	.p2align	4	/* force 16-byte alignment */
 
 VARIABLE(grub_relocator_xen_remap_start)
 LOCAL(base):
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remapper_virt)
-	.quad	0
+	/* Remap the remapper to it's new address. */
+	MOV_IMM64_RAX(grub_relocator_xen_remapper_virt)
 
-	movq    %rax, %rdi
-	movq    %rax, %rbx
+	movq    %rax, %rdi	/* %rdi: new virtual address of remapper */
+	movq    %rax, %rbx	/* Remember new virtual address */
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remapper_map)
-	.quad	0
+	MOV_IMM64_RAX(grub_relocator_xen_remapper_map)
 
-	movq    %rax, %rsi
+	movq    %rax, %rsi	/* %rsi: page table entry */
 
-	movq    $2, %rdx
+	movq    $UVMF_INVLPG, %rdx	/* %rdx: flags (inv. single entry) */
 	movq    $__HYPERVISOR_update_va_mapping, %rax
-	syscall
+	syscall			/* Do the remap operation */
 
 	addq   $(LOCAL(cont) - LOCAL(base)), %rbx
 
-	jmp *%rbx
+	jmp *%rbx		/* Continue with new virtual address */
 
 LOCAL(cont):
-
-	/* mov imm64, %rcx */
-	.byte 	0x48
-	.byte	0xb9
-VARIABLE(grub_relocator_xen_paging_size)
-	.quad	0
+	/* Modify mappings of new page tables to be read-only. */
+	MOV_IMM64_RAX(grub_relocator_xen_mfn_list)
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_paging_start)
-	.quad	0
+	movq	%rax, %rbx	/* %rbx is the base of the p2m list */
+	leaq	EXT_C(grub_relocator_xen_paging_areas) (%rip), %r8
 
-	movq	%rax, %r12
-

grub-core/lib/x86_64/xen/relocator.S line 58

Quoted 10 lines
grub-core/lib/x86_64/xen/relocator.S +2 -7
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_mfn_list)
-	.quad	0
-
-	movq	%rax, %rsi
 1:
+	movq	0(%r8), %r12	/* Get start pfn of the current area */
+	movq	GRUB_TARGET_SIZEOF_LONG(%r8), %rcx	/* Get # of pg tables */

Use %r9 here and...

Quoted 6 lines
grub-core/lib/x86_64/xen/relocator.S +3 -2
+	testq	%rcx, %rcx	/* 0 -> last area reached */
+	jz	3f
+2:
 	movq	%r12, %rdi
-	movq    %rsi, %rbx
-	movq    0(%rsi), %rsi

grub-core/lib/x86_64/xen/relocator.S line 68

Quoted 10 lines
grub-core/lib/x86_64/xen/relocator.S +6 -4
-	shlq    $12,  %rsi
-	orq     $5, %rsi
-	movq    $2, %rdx
-	movq    %rcx, %r9
+	shlq	$PAGE_SHIFT, %rdi	/* virtual address (1:1 mapping) */
+	movq    (%rbx, %r12, 8), %rsi	/* mfn */
+	shlq    $PAGE_SHIFT,  %rsi
+	orq     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %rsi	/* Build pte */
+	movq    $UVMF_INVLPG, %rdx
+	movq    %rcx, %r9	/* %rcx clobbered by hypercall */

... you can avoid this...

grub-core/lib/x86_64/xen/relocator.S line 72

Quoted 4 lines
grub-core/lib/x86_64/xen/relocator.S +0 -0
 	movq    $__HYPERVISOR_update_va_mapping, %rax
 	syscall
 
 	movq    %r9, %rcx

and this...

Quoted 106 lines
grub-core/lib/x86_64/xen/relocator.S +26 -37
-	addq    $8, %rbx
-	addq    $4096, %r12
-	movq    %rbx, %rsi
+	incq	%r12		/* next pfn */
 
-	loop 1b
+	loop 2b
 
-	leaq   LOCAL(mmu_op) (%rip), %rdi
-	movq   $3, %rsi
-	movq   $0, %rdx
-	movq   $0x7FF0, %r10
+	addq	$(2 * GRUB_TARGET_SIZEOF_LONG), %r8	/* next pg table area */
+	jmp	1b
+
+3:
+	/* Switch page tables: pin new L4 pt, load cr3, unpin old L4. */
+	leaq   EXT_C(grub_relocator_xen_mmu_op) (%rip), %rdi
+	movq   $3, %rsi		/* 3 mmu ops */
+	movq   $0, %rdx		/* pdone (not used) */
+	movq   $DOMID_SELF, %r10
 	movq   $__HYPERVISOR_mmuext_op, %rax
 	syscall
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remap_continue)
-	.quad	0
+	/* Continue in virtual kernel mapping. */
+	MOV_IMM64_RAX(grub_relocator_xen_remap_continue)
 
 	jmp *%rax
 
-LOCAL(mmu_op):
+VARIABLE(grub_relocator_xen_paging_areas)
+	/* array of start, size pairs, size 0 is end marker */
+	.quad	0, 0, 0, 0, 0, 0, 0, 0
+
 VARIABLE(grub_relocator_xen_mmu_op)
 	.space 256
 
@@ -112,46 +102,32 @@ VARIABLE(grub_relocator_xen_remap_end)
 
 
 VARIABLE(grub_relocator_xen_start)
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_remapper_virt2)
-	.quad	0
+	/* Unmap old remapper area. */
+	MOV_IMM64_RAX(grub_relocator_xen_remapper_virt2)
 
 	movq    %rax, %rdi
 
-	xorq    %rax, %rax
+	xorq    %rax, %rax	/* Invalid pte */
 	movq    %rax, %rsi
 
-	movq    $2, %rdx
+	movq    $UVMF_INVLPG, %rdx
 	movq    $__HYPERVISOR_update_va_mapping, %rax
 	syscall
 
-
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_stack)
-	.quad	0
+	/* Prepare registers for starting kernel. */
+	MOV_IMM64_RAX(grub_relocator_xen_stack)
 
 	movq	%rax, %rsp
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_start_info)
-	.quad	0
+	MOV_IMM64_RAX(grub_relocator_xen_start_info)
 
 	movq	%rax, %rsi
 
 	cld
 
-	/* mov imm64, %rax */
-	.byte 	0x48
-	.byte	0xb8
-VARIABLE(grub_relocator_xen_entry_point)
-	.quad	0
+	MOV_IMM64_RAX(grub_relocator_xen_entry_point)
 
+	/* Now start the new kernel. */
 	jmp *%rax
 
 VARIABLE(grub_relocator_xen_end)
grub-core/lib/xen/relocator.c +0 -0
index 8f427d3..250fbd4 100644
@@ -36,15 +36,18 @@ extern grub_uint8_t grub_relocator_xen_remap_end;
 extern grub_xen_reg_t grub_relocator_xen_stack;
 extern grub_xen_reg_t grub_relocator_xen_start_info;
 extern grub_xen_reg_t grub_relocator_xen_entry_point;

grub-core/lib/xen/relocator.c extern grub_uint8_t grub_relocator_xen_remap_end; · line 46

Quoted 10 lines
grub-core/lib/xen/relocator.c +4 -2
-extern grub_xen_reg_t grub_relocator_xen_paging_start;
-extern grub_xen_reg_t grub_relocator_xen_paging_size;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt2;
 extern grub_xen_reg_t grub_relocator_xen_remapper_map;
 extern grub_xen_reg_t grub_relocator_xen_mfn_list;
+extern struct {
+  grub_xen_reg_t start;
+  grub_xen_reg_t size;
+} grub_relocator_xen_paging_areas[XEN_MAX_MAPPINGS];

Should not you add GRUB_PACKED here? Could you define type earlier and use it here?

Daniel

author maintainer
#49 lore raw

Re: [PATCH v3 09/10] xen: modify page table construction

Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Juergen Gross

Modify the page table construction to allow multiple virtual regions to be mapped. This is done as preparation for removing the p2m list from the initial kernel mapping in order to support huge pv domains.

This allows a cleaner approach for mapping the relocator page by using this capability.

The interface to the assembler level of the relocator has to be changed in order to be able to process multiple page table areas.

---
V3: use constants instead of numbers as requested by Daniel Kiper
add lots of comments to assembly code as requested by Daniel Kiper

...

Quoted 32 lines

grub-core/lib/x86_64/xen/relocator.S line 58

Quoted 3 lines

1:

grub-core/lib/x86_64/xen/relocator.S +2 -0
+	movq	0(%r8), %r12	/* Get start pfn of the current area */
+	movq	GRUB_TARGET_SIZEOF_LONG(%r8), %rcx	/* Get # of pg tables */

Use %r9 here and...

Quoted 6 lines
grub-core/lib/x86_64/xen/relocator.S +3 -2
+	testq	%rcx, %rcx	/* 0 -> last area reached */
+	jz	3f
+2:
 	movq	%r12, %rdi
-	movq    %rsi, %rbx
-	movq    0(%rsi), %rsi

grub-core/lib/x86_64/xen/relocator.S line 68

Quoted 10 lines
grub-core/lib/x86_64/xen/relocator.S +6 -4
-	shlq    $12,  %rsi
-	orq     $5, %rsi
-	movq    $2, %rdx
-	movq    %rcx, %r9
+	shlq	$PAGE_SHIFT, %rdi	/* virtual address (1:1 mapping) */
+	movq    (%rbx, %r12, 8), %rsi	/* mfn */
+	shlq    $PAGE_SHIFT,  %rsi
+	orq     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %rsi	/* Build pte */
+	movq    $UVMF_INVLPG, %rdx
+	movq    %rcx, %r9	/* %rcx clobbered by hypercall */

... you can avoid this...

grub-core/lib/x86_64/xen/relocator.S line 72

Quoted 4 lines
grub-core/lib/x86_64/xen/relocator.S +0 -0
 	movq    $__HYPERVISOR_update_va_mapping, %rax
 	syscall
 
 	movq    %r9, %rcx

and this...

Quoted 8 lines

grub-core/lib/x86_64/xen/relocator.S line 75

Quoted 8 lines
grub-core/lib/x86_64/xen/relocator.S +2 -4
-	addq    $8, %rbx
-	addq    $4096, %r12
-	movq    %rbx, %rsi
+	incq	%r12		/* next pfn */
 
-	loop 1b
+	loop 2b

This would require to open code the loop statement here with %r9 as count register.

...

Quoted 13 lines
Quoted 13 lines
grub-core/lib/xen/relocator.c +0 -2
index 8f427d3..250fbd4 100644
@@ -36,15 +36,18 @@ extern grub_uint8_t grub_relocator_xen_remap_end;
 extern grub_xen_reg_t grub_relocator_xen_stack;
 extern grub_xen_reg_t grub_relocator_xen_start_info;
 extern grub_xen_reg_t grub_relocator_xen_entry_point;
-extern grub_xen_reg_t grub_relocator_xen_paging_start;
-extern grub_xen_reg_t grub_relocator_xen_paging_size;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt2;
 extern grub_xen_reg_t grub_relocator_xen_remapper_map;
Quoted 8 lines

grub-core/lib/xen/relocator.c extern grub_uint8_t grub_relocator_xen_remap_end; · line 46

Quoted 5 lines
grub-core/lib/xen/relocator.c +4 -0
 extern grub_xen_reg_t grub_relocator_xen_mfn_list;
+extern struct {
+  grub_xen_reg_t start;
+  grub_xen_reg_t size;
+} grub_relocator_xen_paging_areas[XEN_MAX_MAPPINGS];

Should not you add GRUB_PACKED here? Could you define type earlier and use it here?

Why packed? Aah, I think I should align the variable in the assembly source instead.

Regarding type: sure I could.

Juergen

author maintainer
#50 lore raw

Re: [PATCH v3 09/10] xen: modify page table construction

In reply to Daniel Kiper
Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Juergen Gross

Modify the page table construction to allow multiple virtual regions to be mapped. This is done as preparation for removing the p2m list from the initial kernel mapping in order to support huge pv domains.

This allows a cleaner approach for mapping the relocator page by using this capability.

The interface to the assembler level of the relocator has to be changed in order to be able to process multiple page table areas.

---
V3: use constants instead of numbers as requested by Daniel Kiper
add lots of comments to assembly code as requested by Daniel Kiper

...

Quoted 32 lines

grub-core/lib/x86_64/xen/relocator.S line 58

Quoted 3 lines

1:

grub-core/lib/x86_64/xen/relocator.S +2 -0
+	movq	0(%r8), %r12	/* Get start pfn of the current area */
+	movq	GRUB_TARGET_SIZEOF_LONG(%r8), %rcx	/* Get # of pg tables */

Use %r9 here and...

Quoted 6 lines
grub-core/lib/x86_64/xen/relocator.S +3 -2
+	testq	%rcx, %rcx	/* 0 -> last area reached */
+	jz	3f
+2:
 	movq	%r12, %rdi
-	movq    %rsi, %rbx
-	movq    0(%rsi), %rsi

grub-core/lib/x86_64/xen/relocator.S line 68

Quoted 10 lines
grub-core/lib/x86_64/xen/relocator.S +6 -4
-	shlq    $12,  %rsi
-	orq     $5, %rsi
-	movq    $2, %rdx
-	movq    %rcx, %r9
+	shlq	$PAGE_SHIFT, %rdi	/* virtual address (1:1 mapping) */
+	movq    (%rbx, %r12, 8), %rsi	/* mfn */
+	shlq    $PAGE_SHIFT,  %rsi
+	orq     $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %rsi	/* Build pte */
+	movq    $UVMF_INVLPG, %rdx
+	movq    %rcx, %r9	/* %rcx clobbered by hypercall */

... you can avoid this...

grub-core/lib/x86_64/xen/relocator.S line 72

Quoted 4 lines
grub-core/lib/x86_64/xen/relocator.S +0 -0
 	movq    $__HYPERVISOR_update_va_mapping, %rax
 	syscall
 
 	movq    %r9, %rcx

and this...

Quoted 8 lines

grub-core/lib/x86_64/xen/relocator.S line 75

Quoted 8 lines
grub-core/lib/x86_64/xen/relocator.S +2 -4
-	addq    $8, %rbx
-	addq    $4096, %r12
-	movq    %rbx, %rsi
+	incq	%r12		/* next pfn */
 
-	loop 1b
+	loop 2b

This would require to open code the loop statement here with %r9 as count register.

...

Quoted 13 lines
Quoted 13 lines
grub-core/lib/xen/relocator.c +0 -2
index 8f427d3..250fbd4 100644
@@ -36,15 +36,18 @@ extern grub_uint8_t grub_relocator_xen_remap_end;
 extern grub_xen_reg_t grub_relocator_xen_stack;
 extern grub_xen_reg_t grub_relocator_xen_start_info;
 extern grub_xen_reg_t grub_relocator_xen_entry_point;
-extern grub_xen_reg_t grub_relocator_xen_paging_start;
-extern grub_xen_reg_t grub_relocator_xen_paging_size;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt;
 extern grub_xen_reg_t grub_relocator_xen_remapper_virt2;
 extern grub_xen_reg_t grub_relocator_xen_remapper_map;
Quoted 8 lines

grub-core/lib/xen/relocator.c extern grub_uint8_t grub_relocator_xen_remap_end; · line 46

Quoted 5 lines
grub-core/lib/xen/relocator.c +4 -0
 extern grub_xen_reg_t grub_relocator_xen_mfn_list;
+extern struct {
+  grub_xen_reg_t start;
+  grub_xen_reg_t size;
+} grub_relocator_xen_paging_areas[XEN_MAX_MAPPINGS];

Should not you add GRUB_PACKED here? Could you define type earlier and use it here?

Why packed? Aah, I think I should align the variable in the assembly source instead.

Regarding type: sure I could.

Juergen

author maintainer
#51 lore raw

PATCH v3 10/10 xen: add capability to load p2m list outside of kernel mapping

In reply to Juergen Gross

Modern pvops linux kernels support a p2m list not covered by the kernel mapping. This capability is flagged by an elf-note specifying the virtual address the kernel is expecting the p2m list to be mapped to.

In case the elf-note is set by the kernel don't place the p2m list into the kernel mapping, but map it to the given address. This will allow to support domains with larger memory, as the kernel mapping is limited to 2GB and a domain with huge memory in the TB range will have a p2m list larger than this.

---
 grub-core/loader/i386/xen.c        | 53 +++++++++++++++++++++++++++++++-------
 grub-core/loader/i386/xen_fileXX.c |  4 +++
 include/grub/xen_file.h            |  2 ++
 3 files changed, 50 insertions(+), 9 deletions(-)
grub-core/loader/i386/xen.c +44 -9
index c514637..5b9bcd7 100644
@@ -316,21 +316,47 @@ static grub_err_t
 grub_xen_p2m_alloc (void)
 {
   grub_relocator_chunk_t ch;
-  grub_size_t p2msize;
+  grub_size_t p2msize, p2malloc;
   grub_err_t err;
+  struct grub_xen_mapping *map;
+
+  map = xen_state.mappings + xen_state.n_mappings;
+  p2msize = ALIGN_UP (sizeof (grub_xen_mfn_t) *
+		      grub_xen_start_page_addr->nr_pages, PAGE_SIZE);
+  if (xen_state.xen_inf.has_p2m_base)
+    {
+      err = get_pgtable_size (xen_state.xen_inf.p2m_base,
+			      xen_state.xen_inf.p2m_base + p2msize,
+			      (xen_state.max_addr + p2msize) >> PAGE_SHIFT);
+      if (err)
+	return err;
+
+      map->area.pfn_start = xen_state.max_addr >> PAGE_SHIFT;
+      p2malloc = p2msize + page2offset (map->area.n_pt_pages);
+      xen_state.n_mappings++;
+      xen_state.next_start.mfn_list = xen_state.xen_inf.p2m_base;
+      xen_state.next_start.first_p2m_pfn = map->area.pfn_start;
+      xen_state.next_start.nr_p2m_frames = p2malloc >> PAGE_SHIFT;
+    }
+  else
+    {
+      xen_state.next_start.mfn_list =
+	xen_state.max_addr + xen_state.xen_inf.virt_base;
+      p2malloc = p2msize;
+    }
 
   xen_state.state.mfn_list = xen_state.max_addr;
-  xen_state.next_start.mfn_list =
-    xen_state.max_addr + xen_state.xen_inf.virt_base;
-  p2msize = sizeof (grub_xen_mfn_t) * grub_xen_start_page_addr->nr_pages;
   err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch,
-					 xen_state.max_addr, p2msize);
+					 xen_state.max_addr, p2malloc);
   if (err)
     return err;
   xen_state.virt_mfn_list = get_virtual_current_address (ch);
+  if (xen_state.xen_inf.has_p2m_base)
+    map->where = (grub_uint64_t *) xen_state.virt_mfn_list +
+		 p2msize / sizeof (grub_uint64_t);
   grub_memcpy (xen_state.virt_mfn_list,
 	       (void *) grub_xen_start_page_addr->mfn_list, p2msize);
-  xen_state.max_addr = ALIGN_UP (xen_state.max_addr + p2msize, PAGE_SIZE);
+  xen_state.max_addr += p2malloc;
 
   return GRUB_ERR_NONE;
 }
@@ -445,9 +471,12 @@ grub_xen_alloc_end (void)
     return GRUB_ERR_NONE;
   xen_state.alloc_end_called = 1;
 
-  err = grub_xen_p2m_alloc ();
-  if (err)
-    return err;
+  if (!xen_state.xen_inf.has_p2m_base)
+    {
+      err = grub_xen_p2m_alloc ();
+      if (err)
+	return err;
+    }
   err = grub_xen_special_alloc ();
   if (err)
     return err;
@@ -472,6 +501,12 @@ grub_xen_boot (void)
   err = grub_xen_alloc_end ();
   if (err)
     return err;
+  if (xen_state.xen_inf.has_p2m_base)
+    {
+      err = grub_xen_p2m_alloc ();
+      if (err)
+	return err;
+    }
 
   err = set_mfns (xen_state.console_pfn);
   if (err)
grub-core/loader/i386/xen_fileXX.c +4 -0
index d68634d..c92b807 100644
@@ -259,6 +259,10 @@ parse_note (grub_elf_t elf, struct grub_xen_file_info *xi,
 					  descsz == 2 ? 2 : 3) == 0)
 	    xi->arch = GRUB_XEN_FILE_I386;
 	  break;
+	case XEN_ELFNOTE_INIT_P2M:
+	  xi->p2m_base = grub_le_to_cpu_addr (*(Elf_Addr *) desc);
+	  xi->has_p2m_base = 1;
+	  break;
 	case XEN_ELFNOTE_MOD_START_PFN:
 	  xi->unmapped_initrd = !!grub_le_to_cpu32(*(grub_uint32_t *) desc);
 	  break;
include/grub/xen_file.h +2 -0
index ed749fa..6587999 100644
@@ -32,9 +32,11 @@ struct grub_xen_file_info
   grub_uint64_t entry_point;
   grub_uint64_t hypercall_page;
   grub_uint64_t paddr_offset;
+  grub_uint64_t p2m_base;
   int has_hypercall_page;
   int has_note;
   int has_xen_guest;
+  int has_p2m_base;
   int extended_cr3;
   int unmapped_initrd;
   enum
Signature · 1 line
2.6.2
Reviewed-by on v3 10/10
#52 lore raw

Re: [PATCH v3 10/10] xen: add capability to load p2m list outside of kernel mapping

Quoted 5 lines · Juergen Gross

Modern pvops linux kernels support a p2m list not covered by the kernel mapping. This capability is flagged by an elf-note specifying the virtual address the kernel is expecting the p2m list to be mapped to.

Quoted 7 lines

In case the elf-note is set by the kernel don't place the p2m list into the kernel mapping, but map it to the given address. This will allow to support domains with larger memory, as the kernel mapping is limited to 2GB and a domain with huge memory in the TB range will have a p2m list larger than this.

Daniel

Reviewed-by on v3 10/10
#53 lore raw

Re: [PATCH v3 10/10] xen: add capability to load p2m list outside of kernel mapping

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Modern pvops linux kernels support a p2m list not covered by the kernel mapping. This capability is flagged by an elf-note specifying the virtual address the kernel is expecting the p2m list to be mapped to.

Quoted 7 lines

In case the elf-note is set by the kernel don't place the p2m list into the kernel mapping, but map it to the given address. This will allow to support domains with larger memory, as the kernel mapping is limited to 2GB and a domain with huge memory in the TB range will have a p2m list larger than this.

Daniel