[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v2 5/6] multiboot2: Add support for relocatable images



Signed-off-by: Daniel Kiper <daniel.kiper@xxxxxxxxxx>
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)

diff --git a/grub-core/loader/i386/multiboot_mbi.c 
b/grub-core/loader/i386/multiboot_mbi.c
index 956d0e3..abdb98b 100644
--- a/grub-core/loader/i386/multiboot_mbi.c
+++ b/grub-core/loader/i386/multiboot_mbi.c
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                    GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & 
MULTIBOOT_AOUT_KLUDGE))
        grub_errno = err = GRUB_ERR_NONE;
     }
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                 GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c
index ca7154f..1b1f7a9 100644
--- a/grub-core/loader/multiboot.c
+++ b/grub-core/loader/multiboot.c
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer)
+                        void *buffer, int relocatable, grub_uint32_t min_addr,
+                        grub_uint32_t max_addr, grub_size_t align, 
grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align, preference,
+                                     base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align, preference,
+                                     base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF 
magic"));
 }
diff --git a/grub-core/loader/multiboot_elfxx.c 
b/grub-core/loader/multiboot_elfxx.c
index 6a220bd..4fce685 100644
--- a/grub-core/loader/multiboot_elfxx.c
+++ b/grub-core/loader/multiboot_elfxx.c
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, 
void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+                                    void *buffer, int relocatable, 
grub_uint32_t min_addr,
+                                    grub_uint32_t max_addr, grub_size_t align, 
grub_uint32_t preference,
+                                    grub_uint32_t *base_addr, int 
avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, 
const char *filename, voi
          if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
            highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-         grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, 
memsz=0x%lx, vaddr=0x%lx\n",
-                       i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, 
(long) phdr(i)->p_vaddr);
+         grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, 
memsz=0x%lx, vaddr=0x%lx,"
+                       "align=0x%lx, relocatable=%d, 
avoid_efi_boot_services=%d\n", i,
+                       (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, 
(long) phdr(i)->p_vaddr,
+                       (long) align, relocatable, avoid_efi_boot_services);
 
          {
            grub_relocator_chunk_t ch;
-           err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-                                                  &ch, phdr(i)->p_paddr,
-                                                  phdr(i)->p_memsz);
+
+           if (relocatable)
+             err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, 
&ch,
+                                                     min_addr, max_addr - 
phdr(i)->p_memsz,
+                                                     phdr(i)->p_memsz, align ? 
align : 1,
+                                                     preference, 
avoid_efi_boot_services);
+           else
+             err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+                                                    &ch, phdr(i)->p_paddr,
+                                                    phdr(i)->p_memsz);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
                return err;
              }
+           if (base_addr)
+             *base_addr = get_physical_target_address (ch);
            source = get_virtual_current_address (ch);
          }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, 
const char *filename, voi
                                                    + 1, sh->sh_size,
                                                    sh->sh_addralign,
                                                    
GRUB_RELOCATOR_PREFERENCE_NONE,
-                                                   0);
+                                                   avoid_efi_boot_services);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
diff --git a/grub-core/loader/multiboot_mbi2.c 
b/grub-core/loader/multiboot_mbi2.c
index dc9c709..7ac64ec 100644
--- a/grub-core/loader/multiboot_mbi2.c
+++ b/grub-core/loader/multiboot_mbi2.c
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,13 @@ grub_multiboot_load (grub_file_t file, const char 
*filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +177,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
              case MULTIBOOT_TAG_TYPE_EFI_BS:
              case MULTIBOOT_TAG_TYPE_EFI32_IH:
              case MULTIBOOT_TAG_TYPE_EFI64_IH:
+             case MULTIBOOT_TAG_TYPE_BASE_ADDR:
                break;
 
              default:
@@ -215,6 +219,26 @@ grub_multiboot_load (grub_file_t file, const char 
*filename)
        accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
        break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+       relocatable = 1;
+       min_addr = ((struct multiboot_header_tag_relocatable *) tag)->min_addr;
+       max_addr = ((struct multiboot_header_tag_relocatable *) tag)->max_addr;
+       align = ((struct multiboot_header_tag_relocatable *) tag)->align;
+       switch (((struct multiboot_header_tag_relocatable *) tag)->preference)
+         {
+         case MULTIBOOT_LOAD_PREFERENCE_LOW:
+           preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+           break;
+
+         case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+           preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+           break;
+
+         default:
+           preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+         }
+       break;
+
        /* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
        break;
@@ -260,15 +284,22 @@ grub_multiboot_load (grub_file_t file, const char 
*filename)
       else
        code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-                                            &ch, load_addr,
-                                            code_size);
+      if (relocatable)
+       err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+                                               min_addr, max_addr - code_size,
+                                               code_size, align ? align : 1,
+                                               preference, keep_bs);
+      else
+       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+                                              &ch, load_addr,
+                                              code_size);
       if (err)
        {
          grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
          grub_free (buffer);
          return err;
        }
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +321,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+                                    relocatable, min_addr, max_addr,
+                                    align, preference, &base_addr, keep_bs);
       if (err)
        {
          grub_free (buffer);
@@ -303,6 +336,14 @@ grub_multiboot_load (grub_file_t file, const char 
*filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
                                      accepted_consoles,
@@ -409,6 +450,7 @@ grub_multiboot_get_mbi_size (void)
                 + grub_get_multiboot_mmap_count ()
                 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr), MULTIBOOT_TAG_ALIGN)
 #ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
@@ -693,6 +735,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr *) 
ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr); 
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size; 
diff --git a/include/grub/multiboot.h b/include/grub/multiboot.h
index e13c084..ec322b0 100644
--- a/include/grub/multiboot.h
+++ b/include/grub/multiboot.h
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer);
+                        void *buffer, int relocatable, grub_uint32_t min_addr,
+                        grub_uint32_t max_addr, grub_size_t align, 
grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
diff --git a/include/multiboot2.h b/include/multiboot2.h
index 9f97ddc..941a95d 100644
--- a/include/multiboot2.h
+++ b/include/multiboot2.h
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.