Skip to contentSkip to conversation

libelf and the new domain builder

PATCH New Gerd Hoffmann 1 participant 11 messages Dec 1, 2006

lore
Share & tools
lore
#0 lore raw

cover libelf and the new domain builder

Hi,

This is a patchset with the rewritten domain builder for the xen tools.

The ELF parsing bits of the domain builder rewrite have been factored out recently, so the patchset adds these first and the domain builder on top of that.

The ELF parser code is used by both xen kernel (for the dom0 builder) and the tools (via libxc). It switches endianess and elfsize at runtime. We'll need that for 32-on-64 support comming soon and it likely also is handy for the bigendian experiments some ia64 guys are doing.

The new domain builder builds on top of libelf. It's a redesign and major rewrite of the domU generic domain builder. I've tried to make all architectures use the same code as far as possible to avoid cut&paste of code (and bugs!). It is also prepared to handle 32-on-64, only the hypercalls for the mode setting need to be added.

Current state:
x86_32(p), x86_64
- should work without problems.
ia64
- not looked yet at the dom0 builder.
- tools are compile-tested now and then, they should at least build and might even work although that is completely untested.
powerpc
- some code lines are there. It is incomplete and I didn't even compile it.

The patches (and a few more ...) are also available for download from
http://www.suse.de/~kraxel/patches/unstable-hg12663-20061201-quilt/

Reviews, questions, comments are welcome.
Also patches of course, especially for !x86 ...

cheers,

Gerd

--

#1 lore raw

PATCH 1/10 Generate headers with arch-specific structs.

This patch adds a script to generate headers with arch-specific structs which can be included on any architecture. Can be used to deal with structs of "foreign" architectures, needed for 32-on-64 support for example.

---
 tools/Rules.mk                            |    2 
 xen/Makefile                              |    5 
 xen/include/public/foreign/Makefile       |   30 +++++
 xen/include/public/foreign/mkchecker.py   |   58 +++++++++++
 xen/include/public/foreign/mkheader.py    |  153 ++++++++++++++++++++++++++++++
 xen/include/public/foreign/reference.size |   17 +++
 xen/include/public/foreign/structs.py     |   49 +++++++++
 7 files changed, 314 insertions(+)
xen/include/public/foreign/Makefile new file +30 -0
Index: build-32-unstable-12502/xen/include/public/foreign/Makefile
===================================================================
@@ -0,0 +1,30 @@
+XEN_ROOT := ../../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+architectures := x86_32 x86_64 ia64
+headers := $(patsubst %, %.h, $(architectures))
+scripts := $(wildcard *.py)
+
+.PHONY: all clean check-headers
+all: $(headers) check-headers
+
+clean:
+	rm -f $(headers)
+	rm -f checker checker.c $(XEN_TARGET_ARCH).size
+	rm -f *.pyc *.o *~
+
+check-headers: checker
+ifeq ($(CROSS_COMPILE),)
+	./checker > $(XEN_TARGET_ARCH).size
+	diff -u reference.size $(XEN_TARGET_ARCH).size
+else
+	@echo "cross build: skipping check"
+endif
+
+%.h: ../arch-%.h ../xen.h $(scripts)
+	python mkheader.py $* $@ $< ../xen.h
+
+checker.o: checker.c $(headers)
+
+checker.c: $(scripts)
+	python mkchecker.py $(XEN_TARGET_ARCH) $@ $(architectures)
xen/include/public/foreign/mkheader.py new file +153 -0

153 lines not on this page · raw message

xen/include/public/foreign/structs.py new file +49 -0

49 lines not on this page · raw message

xen/include/public/foreign/reference.size new file +17 -0
Index: build-32-unstable-12502/xen/include/public/foreign/reference.size
===================================================================
@@ -0,0 +1,17 @@
+
+structs              |  x86_32  x86_64    ia64
+
+start_info           |    1104    1152    1152
+trap_info            |       8      16       -
+pt_fpreg             |       -       -      16
+cpu_user_regs        |      68     200     496
+xen_ia64_boot_param  |       -       -      96
+ia64_tr_entry        |       -       -      32
+vcpu_extra_regs      |       -       -     536
+vcpu_guest_context   |    2800    5168    1056
+arch_vcpu_info       |      24      16       0
+vcpu_time_info       |      32      32      32
+vcpu_info            |      64      64      48
+arch_shared_info     |     268     280     272
+shared_info          |    2584    3368    4384
+
xen/Makefile +5 -0
Index: build-32-unstable-12502/xen/Makefile
===================================================================
@@ -31,8 +31,11 @@ _install: $(TARGET).gz
 	$(INSTALL_DATA) $(TARGET)-syms $(DESTDIR)/boot/$(notdir $(TARGET))-syms-$(XEN_FULLVERSION)
 	[ -d $(DESTDIR)/usr/include/xen/io ] || \
 		$(INSTALL_DIR) $(DESTDIR)/usr/include/xen/io
+	[ -d $(DESTDIR)/usr/include/xen/foreign ] || \
+		$(INSTALL_DIR) $(DESTDIR)/usr/include/xen/foreign
 	$(INSTALL_DATA) include/public/*.h $(DESTDIR)/usr/include/xen
 	$(INSTALL_DATA) include/public/io/*.h $(DESTDIR)/usr/include/xen/io
+	$(INSTALL_DATA) include/public/foreign/*.h $(DESTDIR)/usr/include/xen/foreign
 	$(INSTALL_DATA) include/public/COPYING $(DESTDIR)/usr/include/xen
 
 .PHONY: _debug
@@ -42,6 +45,7 @@ _debug:
 .PHONY: _clean
 _clean: delete-unfresh-files
 	$(MAKE) -C tools clean
+	$(MAKE) -C include/public/foreign clean
 	$(MAKE) -f $(BASEDIR)/Rules.mk -C common clean
 	$(MAKE) -f $(BASEDIR)/Rules.mk -C drivers clean
 	$(MAKE) -f $(BASEDIR)/Rules.mk -C acm clean
@@ -60,6 +64,7 @@ $(TARGET).gz: $(TARGET)
 
 $(TARGET): delete-unfresh-files
 	$(MAKE) -C tools
+	$(MAKE) -C include/public/foreign
 	$(MAKE) -f $(BASEDIR)/Rules.mk include/xen/compile.h
 	$(MAKE) -f $(BASEDIR)/Rules.mk include/xen/acm_policy.h
 	[ -e include/asm ] || ln -sf asm-$(TARGET_ARCH) include/asm
xen/include/public/foreign/mkchecker.py new file +58 -0

58 lines not on this page · raw message

tools/Rules.mk +2 -0
Index: build-32-unstable-12502/tools/Rules.mk
===================================================================
@@ -46,5 +46,7 @@ mk-symlinks-xen:
 	( cd xen/hvm && ln -sf ../../$(XEN_ROOT)/xen/include/public/hvm/*.h . )
 	mkdir -p xen/io
 	( cd xen/io && ln -sf ../../$(XEN_ROOT)/xen/include/public/io/*.h . )
+	mkdir -p xen/foreign
+	( cd xen/foreign && ln -sf ../../$(XEN_ROOT)/xen/include/public/foreign/*.h . )
 
 mk-symlinks: mk-symlinks-xen mk-symlinks-$(XEN_OS)

--

#2 lore raw

PATCH 2/10 add libelf: an ELF binary parser library.

In reply to Gerd Hoffmann

This patch adds a library with a small collection of helper functions to parse and load elf binaries. The library handles endianess and elfsize at runtime.

The patch also shuffles around the include files a bit. Now there is *one* include file holding all the elf structures (xen/include/public/elfstructs.h) which is included by everyone who needs them.

It's dead code with this patch only, putting the code into use happens in followup patches.

---
 tools/libxc/xc_elf.h                |  525 -----------------------------------
 xen/arch/x86/boot/mkelf32.c         |    2 
 xen/common/Makefile                 |    2 
 xen/common/libelf/Makefile          |    4 
 xen/common/libelf/README            |    1 
 xen/common/libelf/libelf-dominfo.c  |  422 ++++++++++++++++++++++++++++
 xen/common/libelf/libelf-loader.c   |  145 +++++++++
 xen/common/libelf/libelf-private.h  |   32 ++
 xen/common/libelf/libelf-relocate.c |  345 +++++++++++++++++++++++
 xen/common/libelf/libelf-tools.c    |  225 +++++++++++++++
 xen/include/public/elfstructs.h     |  527 ++++++++++++++++++++++++++++++++++++
 xen/include/public/libelf.h         |  227 +++++++++++++++
 xen/include/xen/elf.h               |  490 ---------------------------------
 13 files changed, 1933 insertions(+), 1014 deletions(-)
xen/common/Makefile +2 -0
Index: build-32-unstable-12663/xen/common/Makefile
===================================================================
@@ -32,5 +32,7 @@ obj-$(perfc)       += perfc.o
 obj-$(crash_debug) += gdbstub.o
 obj-$(xenoprof)    += xenoprof.o
 
+subdir-y += libelf
+
 # Object file contains changeset and compiler information.
 version.o: $(BASEDIR)/include/xen/compile.h
xen/common/libelf/Makefile new file +4 -0
Index: build-32-unstable-12663/xen/common/libelf/Makefile
===================================================================
@@ -0,0 +1,4 @@
+obj-y += libelf-tools.o
+obj-y += libelf-loader.o
+obj-y += libelf-dominfo.o
+#obj-y += libelf-relocate.o
xen/common/libelf/README new file +1 -0
Index: build-32-unstable-12663/xen/common/libelf/README
===================================================================
@@ -0,0 +1 @@
+Take care, this code is used by both xen and tools ...
xen/common/libelf/libelf-dominfo.c new file +422 -0

422 lines not on this page · raw message

xen/common/libelf/libelf-loader.c new file +145 -0

145 lines not on this page · raw message

xen/common/libelf/libelf-private.h new file +32 -0
Index: build-32-unstable-12663/xen/common/libelf/libelf-private.h
===================================================================
@@ -0,0 +1,32 @@
+#ifdef __XEN__
+
+#include <xen/string.h>
+#include <xen/lib.h>
+#include <public/elfnote.h>
+#include <public/libelf.h>
+
+#define elf_msg(elf, fmt, args ... ) \
+	if (elf->verbose) printk(fmt, ## args )
+#define elf_err(elf, fmt, args ... ) \
+	printk(fmt, ## args )
+
+#define strtoull(str, end, base) simple_strtoull(str, end, base)
+#define bswap_16(x) (x) /* FIXME */
+#define bswap_32(x) (x)
+#define bswap_64(x) (x)
+
+#else /* !__XEN__ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <byteswap.h>
+#include <xen/elfnote.h>
+#include <xen/libelf.h>
+
+#define elf_msg(elf, fmt, args ... ) \
+	if (elf->log && elf->verbose) fprintf(elf->log, fmt , ## args )
+#define elf_err(elf, fmt, args ... ) \
+	if (elf->log) fprintf(elf->log, fmt , ## args )
+
+#endif
xen/common/libelf/libelf-relocate.c new file +345 -0

345 lines not on this page · raw message

xen/common/libelf/libelf-tools.c new file +225 -0

225 lines not on this page · raw message

xen/include/public/elfstructs.h new file +527 -0

527 lines not on this page · raw message

xen/include/public/libelf.h new file +227 -0

227 lines not on this page · raw message

tools/libxc/xc_elf.h +1 -524

525 lines not on this page · raw message

xen/include/xen/elf.h +1 -489

496 lines not on this page · raw message

xen/arch/x86/boot/mkelf32.c +1 -1
Index: build-32-unstable-12663/xen/arch/x86/boot/mkelf32.c
===================================================================
@@ -25,7 +25,7 @@
 #define s16 int16_t
 #define s32 int32_t
 #define s64 int64_t
-#include "../../../include/xen/elf.h"
+#include "../../../include/public/elfstructs.h"
 
 #define DYNAMICALLY_FILLED   0
 #define RAW_OFFSET         128

--

#3 lore raw

PATCH 3/10 libelf: use for x86 dom0 builder.

In reply to Gerd Hoffmann

This patch switches the x86 dom0 builder over to libelf.

---
 xen/arch/x86/domain_build.c |  223 ++++++++++++++++----------------------------
 xen/common/Makefile         |    2 
 2 files changed, 85 insertions(+), 140 deletions(-)
xen/common/Makefile +1 -1
Index: build-32-unstable-12621/xen/common/Makefile
===================================================================
@@ -2,7 +2,7 @@ obj-y += acm_ops.o
 obj-y += bitmap.o
 obj-y += domctl.o
 obj-y += domain.o
-obj-y += elf.o
+#obj-y += elf.o
 obj-y += event_channel.o
 obj-y += grant_table.o
 obj-y += kernel.o
xen/arch/x86/domain_build.c +84 -139

420 lines not on this page · raw message

--

#4 lore raw

PATCH 4/10 libelf: add to libxc

In reply to Gerd Hoffmann

This patch makes libelf available to the tools, by symlinking the source files and compiling them into libxc.

---
 tools/libxc/Makefile |   15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/libxc/Makefile +15 -0
Index: build-32-unstable-12621/tools/libxc/Makefile
===================================================================
@@ -29,6 +29,21 @@ GUEST_SRCS-$(CONFIG_IA64) += xc_linux_bu
 GUEST_SRCS-$(CONFIG_MIGRATE) += xc_linux_restore.c xc_linux_save.c
 GUEST_SRCS-$(CONFIG_HVM) += xc_hvm_build.c
 
+# symlink libelf from xen/common/libelf/
+LIBELF_SRCS := libelf-tools.c libelf-loader.c
+LIBELF_SRCS += libelf-dominfo.c libelf-relocate.c
+
+libelf-tools.o: libelf-tools.c libelf-private.h
+libelf-loader.o: libelf-loader.c libelf-private.h
+libelf-dominfo.o: libelf-dominfo.c libelf-private.h
+libelf-relocate.o: libelf-relocate.c libelf-private.h
+
+$(LIBELF_SRCS) libelf-private.h:
+	ln -s ../../xen/common/libelf/$@ $@
+
+# add libelf bits to libxc
+GUEST_SRCS-y += $(LIBELF_SRCS)
+
 -include $(XEN_TARGET_ARCH)/Makefile
 
 CFLAGS   += -Werror -Wmissing-prototypes

--

#5 lore raw

PATCH 5/10 libelf: use for hvm builder.

In reply to Gerd Hoffmann

This patch switches over the hvm domain builder to libelf (for loading hvmloader).

---
 tools/libxc/xc_hvm_build.c |  215 +++++++++++++--------------------------------
 1 file changed, 65 insertions(+), 150 deletions(-)
tools/libxc/xc_hvm_build.c +65 -150
Index: build-32-unstable-12621/tools/libxc/xc_hvm_build.c
===================================================================
@@ -2,29 +2,22 @@
  * xc_hvm_build.c
  */
 
-#define ELFSIZE 32
 #include <stddef.h>
 #include <inttypes.h>
-#include "xg_private.h"
-#include "xc_private.h"
-#include "xc_elf.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <zlib.h>
+
+#include "xg_private.h"
+#include "xc_private.h"
+
 #include <xen/hvm/hvm_info_table.h>
 #include <xen/hvm/params.h>
 #include <xen/hvm/e820.h>
 
-#define SCRATCH_PFN 0xFFFFF
+#include <xen/libelf.h>
 
-#define HVM_LOADER_ENTR_ADDR  0x00100000
-static int
-parseelfimage(
-    char *elfbase, unsigned long elfsize, struct domain_setup_info *dsi);
-static int
-loadelfimage(
-    char *elfbase, int xch, uint32_t dom, unsigned long *parray,
-    struct domain_setup_info *dsi);
+#define SCRATCH_PFN 0xFFFFF
 
 int xc_set_hvm_param(
     int handle, domid_t dom, int param, unsigned long value)
@@ -144,6 +137,48 @@ static void build_e820map(void *e820_pag
     *(((unsigned char *)e820_page) + E820_MAP_NR_OFFSET) = nr_map;
 }
 
+static int
+loadelfimage(struct elf_binary *elf, int xch, uint32_t dom, unsigned long *parray)
+{
+    privcmd_mmap_entry_t *entries = NULL;
+    int pages = (elf->pend - elf->pstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
+    int i, rc = -1;
+
+    /* map hvmloader address space */
+    entries = malloc(pages * sizeof(privcmd_mmap_entry_t));
+    if (NULL == entries)
+        goto err;
+    elf->dest = mmap(NULL, pages << PAGE_SHIFT, PROT_READ | PROT_WRITE,
+                     MAP_SHARED, xch, 0);
+    if (MAP_FAILED == elf->dest)
+        goto err;
+
+    for (i = 0; i < pages; i++)
+    {
+        entries[i].va = (uintptr_t)elf->dest + (i << PAGE_SHIFT);
+        entries[i].mfn = parray[(elf->pstart >> PAGE_SHIFT) + i];
+        entries[i].npages = 1;
+    }
+    rc = xc_map_foreign_ranges(xch, dom, entries, pages);
+    if (rc < 0)
+        goto err;
+
+    /* load hvmloader */
+    elf_load_binary(elf);
+    rc = 0;
+
+ err:
+    /* cleanup */
+    if (elf->dest) {
+        munmap(elf->dest, pages << PAGE_SHIFT);
+        elf->dest = NULL;
+    }
+    if (entries)
+        free(entries);
+
+    return rc;
+}
+
 static int setup_guest(int xc_handle,
                        uint32_t dom, int memsize,
                        char *image, unsigned long image_size,
@@ -155,35 +190,35 @@ static int setup_guest(int xc_handle,
     struct xen_add_to_physmap xatp;
     struct shared_info *shared_info;
     void *e820_page;
-    struct domain_setup_info dsi;
-    uint64_t v_end;
+    struct elf_binary elf;
+    uint64_t v_start, v_end;
     int rc;
 
-    memset(&dsi, 0, sizeof(struct domain_setup_info));
-
-    if ( (parseelfimage(image, image_size, &dsi)) != 0 )
+    if (0 != elf_init(&elf, image, image_size))
         goto error_out;
+    elf_parse_binary(&elf);
+    v_start = 0;
+    v_end = (unsigned long long)memsize << 20;
 
-    if ( (dsi.v_kernstart & (PAGE_SIZE - 1)) != 0 )
+    if ( (elf.pstart & (PAGE_SIZE - 1)) != 0 )
     {
         PERROR("Guest OS must load to a page boundary.\n");
         goto error_out;
     }
 
-    v_end = (unsigned long long)memsize << 20;
-
     IPRINTF("VIRTUAL MEMORY ARRANGEMENT:\n"
-           "  Loaded HVM loader:    %016"PRIx64"->%016"PRIx64"\n"
-           "  TOTAL:                %016"PRIx64"->%016"PRIx64"\n",
-           dsi.v_kernstart, dsi.v_kernend,
-           dsi.v_start, v_end);
-    IPRINTF("  ENTRY ADDRESS:        %016"PRIx64"\n", dsi.v_kernentry);
+            "  Loaded HVM loader:    %016"PRIx64"->%016"PRIx64"\n"
+            "  TOTAL:                %016"PRIx64"->%016"PRIx64"\n"
+            "  ENTRY ADDRESS:        %016"PRIx64"\n",
+            elf.pstart, elf.pend,
+            v_start, v_end,
+            elf_uval(&elf, elf.ehdr, e_entry));
 
-    if ( (v_end - dsi.v_start) > ((unsigned long long)nr_pages << PAGE_SHIFT) )
+    if ( (v_end - v_start) > ((unsigned long long)nr_pages << PAGE_SHIFT) )
     {
         PERROR("Initial guest OS requires too much space: "
                "(%lluMB is greater than %lluMB limit)\n",
-               (unsigned long long)(v_end - dsi.v_start) >> 20,
+               (unsigned long long)(v_end - v_start) >> 20,
                ((unsigned long long)nr_pages << PAGE_SHIFT) >> 20);
         goto error_out;
     }
@@ -212,7 +247,7 @@ static int setup_guest(int xc_handle,
         goto error_out;
     }
 
-    loadelfimage(image, xc_handle, dom, page_array, &dsi);
+    loadelfimage(&elf, xc_handle, dom, page_array);
 
     if ( (e820_page = xc_map_foreign_range(
               xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
@@ -256,7 +291,7 @@ static int setup_guest(int xc_handle,
 
     free(page_array);
 
-    ctxt->user_regs.eip = dsi.v_kernentry;
+    ctxt->user_regs.eip = elf_uval(&elf, elf.ehdr, e_entry);
 
     return 0;
 
@@ -316,126 +351,6 @@ static inline int is_loadable_phdr(Elf32
             ((phdr->p_flags & (PF_W|PF_X)) != 0));
 }
 
-static int parseelfimage(char *elfbase,
-                         unsigned long elfsize,
-                         struct domain_setup_info *dsi)
-{
-    Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
-    Elf32_Phdr *phdr;
-    Elf32_Shdr *shdr;
-    unsigned long kernstart = ~0UL, kernend=0UL;
-    char *shstrtab;
-    int h;
-
-    if ( !IS_ELF(*ehdr) )
-    {
-        ERROR("Kernel image does not have an ELF header.");
-        return -EINVAL;
-    }
-
-    if ( (ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize)) > elfsize )
-    {
-        ERROR("ELF program headers extend beyond end of image.");
-        return -EINVAL;
-    }
-
-    if ( (ehdr->e_shoff + (ehdr->e_shnum * ehdr->e_shentsize)) > elfsize )
-    {
-        ERROR("ELF section headers extend beyond end of image.");
-        return -EINVAL;
-    }
-
-    /* Find the section-header strings table. */
-    if ( ehdr->e_shstrndx == SHN_UNDEF )
-    {
-        ERROR("ELF image has no section-header strings table (shstrtab).");
-        return -EINVAL;
-    }
-    shdr = (Elf32_Shdr *)(elfbase + ehdr->e_shoff +
-                          (ehdr->e_shstrndx*ehdr->e_shentsize));
-    shstrtab = elfbase + shdr->sh_offset;
-
-    for ( h = 0; h < ehdr->e_phnum; h++ )
-    {
-        phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
-        if ( !is_loadable_phdr(phdr) )
-            continue;
-        if ( phdr->p_paddr < kernstart )
-            kernstart = phdr->p_paddr;
-        if ( (phdr->p_paddr + phdr->p_memsz) > kernend )
-            kernend = phdr->p_paddr + phdr->p_memsz;
-    }
-
-    if ( (kernstart > kernend) ||
-         (ehdr->e_entry < kernstart) ||
-         (ehdr->e_entry > kernend) )
-    {
-        ERROR("Malformed ELF image.");
-        return -EINVAL;
-    }
-
-    dsi->v_start = 0x00000000;
-
-    dsi->v_kernstart = kernstart;
-    dsi->v_kernend   = kernend;
-    dsi->v_kernentry = HVM_LOADER_ENTR_ADDR;
-
-    dsi->v_end       = dsi->v_kernend;
-
-    return 0;
-}
-
-static int
-loadelfimage(
-    char *elfbase, int xch, uint32_t dom, unsigned long *parray,
-    struct domain_setup_info *dsi)
-{
-    Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
-    Elf32_Phdr *phdr;
-    int h;
-
-    char         *va;
-    unsigned long pa, done, chunksz;
-
-    for ( h = 0; h < ehdr->e_phnum; h++ )
-    {
-        phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
-        if ( !is_loadable_phdr(phdr) )
-            continue;
-
-        for ( done = 0; done < phdr->p_filesz; done += chunksz )
-        {
-            pa = (phdr->p_paddr + done) - dsi->v_start;
-            if ((va = xc_map_foreign_range(
-                xch, dom, PAGE_SIZE, PROT_WRITE,
-                parray[pa >> PAGE_SHIFT])) == 0)
-                return -1;
-            chunksz = phdr->p_filesz - done;
-            if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
-                chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
-            memcpy(va + (pa & (PAGE_SIZE-1)),
-                   elfbase + phdr->p_offset + done, chunksz);
-            munmap(va, PAGE_SIZE);
-        }
-
-        for ( ; done < phdr->p_memsz; done += chunksz )
-        {
-            pa = (phdr->p_paddr + done) - dsi->v_start;
-            if ((va = xc_map_foreign_range(
-                xch, dom, PAGE_SIZE, PROT_WRITE,
-                parray[pa >> PAGE_SHIFT])) == 0)
-                return -1;
-            chunksz = phdr->p_memsz - done;
-            if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
-                chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
-            memset(va + (pa & (PAGE_SIZE-1)), 0, chunksz);
-            munmap(va, PAGE_SIZE);
-        }
-    }
-
-    return 0;
-}
-
 /* xc_hvm_build
  *
  * Create a domain for a virtualized Linux, using files/filenames

--

#6 lore raw

PATCH 6/10 libelf: use for readnotes utility.

In reply to Gerd Hoffmann

This patch makes the readnotes utility use libelf.

---
 tools/xcutils/readnotes.c |  277 +++++++---------------------------------------
 1 file changed, 45 insertions(+), 232 deletions(-)
tools/xcutils/readnotes.c +45 -232
Index: build-32-unstable-12621/tools/xcutils/readnotes.c
===================================================================
@@ -1,4 +1,3 @@
-#include <elf.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
@@ -11,219 +10,35 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include <xen/elfnote.h>
-
-#define ELFNOTE_NAME(_n_) ((void*)(_n_) + sizeof(*(_n_)))
-#define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + (((_n_)->n_namesz+3)&~3))
-#define ELFNOTE_NEXT(_n_) (ELFNOTE_DESC(_n_) + (((_n_)->n_descsz+3)&~3))
-
-#ifndef ELFSIZE
-#include <limits.h>
-#if UINT_MAX == ULONG_MAX
-#define ELFSIZE 32
-#else
-#define ELFSIZE 64
-#endif
-#endif
-
-#if (ELFSIZE == 32)
-typedef Elf32_Nhdr Elf_Nhdr;
-typedef Elf32_Half Elf_Half;
-typedef Elf32_Word Elf_Word;
-#elif (ELFSIZE == 64)
-typedef Elf64_Nhdr Elf_Nhdr;
-typedef Elf64_Half Elf_Half;
-typedef Elf64_Word Elf_Word;
-#else
-#error "Unknown ELFSIZE"
-#endif
-
-static void print_string_note(const char *prefix, Elf_Nhdr *note)
-{
-	printf("%s: %s\n", prefix, (const char *)ELFNOTE_DESC(note));
-}
-
-static void print_numeric_note(const char *prefix,Elf_Nhdr *note)
-{
-	switch (note->n_descsz)
-	{
-	case 4:
-		printf("%s: %#010" PRIx32 " (4 bytes)\n",
-		       prefix, *(uint32_t *)ELFNOTE_DESC(note));
-		break;
-	case 8:
-		printf("%s: %#018" PRIx64 " (8 bytes)\n",
-		       prefix, *(uint64_t *)ELFNOTE_DESC(note));
-		break;
-	default:
-		printf("%s: unknown data size %#lx\n", prefix,
-		       (unsigned long)note->n_descsz);
-		break;
-	}
-}
-
-static inline int is_elf(void *image)
-{
-	/*
-	 * Since we are only accessing the e_ident field we can
-	 * acccess the bytes directly without needing to figure out
-	 * which version of Elf*_Ehdr structure to use.
-	 */
-	const unsigned char *hdr = image;
-	return ( hdr[EI_MAG0] == ELFMAG0 &&
-		 hdr[EI_MAG1] == ELFMAG1 &&
-		 hdr[EI_MAG2] == ELFMAG2 &&
-		 hdr[EI_MAG3] == ELFMAG3 );
-}
-
-static inline unsigned char ehdr_class(void *image)
-{
-	/*
-	 * Since we are only accessing the e_ident field we can
-	 * acccess the bytes directly without needing to figure out
-	 * which version of Elf*_Ehdr structure to use.
-	 */
-	const unsigned char *hdr = image;
-	switch (hdr[EI_CLASS])
-	{
-	case ELFCLASS32:
-	case ELFCLASS64:
-		return hdr[EI_CLASS];
-	default:
-		fprintf(stderr, "Unknown ELF class %d\n", hdr[EI_CLASS]);
-		exit(1);
-	}
-}
+#include <xg_private.h>
 
-static inline Elf_Half ehdr_shnum(void *image)
-{
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-		return ((Elf32_Ehdr *)image)->e_shnum;
-	case ELFCLASS64:
-		return ((Elf64_Ehdr *)image)->e_shnum;
-	default:
-		exit(1);
-	}
-}
+#include <xen/libelf.h>
 
-static inline Elf_Word shdr_type(void *image, int shnum)
+static void print_string_note(const char *prefix, struct elf_binary *elf,
+			      const elf_note *note)
 {
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return shdr->sh_type;
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return shdr->sh_type;
-	}
-	default:
-		exit(1);
-	}
+	printf("%s: %s\n", prefix, (char*)elf_note_desc(elf, note));
 }
 
-static inline const char *shdr_name(void *image, int shnum)
-{
-	const char *shstrtab;
-
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr;
-		/* Find the section-header strings table. */
-		if ( ehdr->e_shstrndx == SHN_UNDEF )
-			return NULL;
-		shdr = (Elf32_Shdr *)(image + ehdr->e_shoff +
-				      (ehdr->e_shstrndx*ehdr->e_shentsize));
-		shstrtab = image + shdr->sh_offset;
-
-		shdr= (Elf32_Shdr*)(image + ehdr->e_shoff +
-				    (shnum*ehdr->e_shentsize));
-		return &shstrtab[shdr->sh_name];
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr;
-		/* Find the section-header strings table. */
-		if ( ehdr->e_shstrndx == SHN_UNDEF )
-			return NULL;
-		shdr = (Elf64_Shdr *)(image + ehdr->e_shoff +
-				      (ehdr->e_shstrndx*ehdr->e_shentsize));
-		shstrtab = image + shdr->sh_offset;
-
-		shdr= (Elf64_Shdr*)(image + ehdr->e_shoff +
-				    (shnum*ehdr->e_shentsize));
-		return &shstrtab[shdr->sh_name];
-	}
-	default:
-		exit(1);
-	}
-}
-static inline void *shdr_start(void *image, int shnum)
+static void print_numeric_note(const char *prefix, struct elf_binary *elf,
+			       const elf_note *note)
 {
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset;
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset;
-	}
-	default:
-		exit(1);
-	}
-}
+	uint64_t value = elf_note_numeric(elf, note);
+	int descsz = elf_uval(elf, note, descsz);
 
-static inline void *shdr_end(void *image, int shnum)
-{
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset + shdr->sh_size;
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset + shdr->sh_size;
-	}
-	default:
-		exit(1);
-	}
+	printf("%s: %#*" PRIx64 " (%d bytes)\n",
+	       prefix, 2+2*descsz, value, descsz);
 }
 
 int main(int argc, char **argv)
 {
 	const char *f;
-	int fd,h;
+	int fd,h,size,count;
 	void *image;
 	struct stat st;
-	Elf_Nhdr *note;
+	struct elf_binary elf;
+	const elf_shdr *shdr;
+	const elf_note *note, *end;
 
 	if (argc != 2)
 	{
@@ -251,76 +66,74 @@ int main(int argc, char **argv)
 		fprintf(stderr, "Unable to map %s: %s\n", f, strerror(errno));
 		return 1;
 	}
+	size = st.st_size;
 
-	if ( !is_elf(image) )
+	if (0 != elf_init(&elf, image, size))
 	{
 		fprintf(stderr, "File %s is not an ELF image\n", f);
 		return 1;
 	}
+	elf_set_logfile(&elf, stderr, 0);
 
-	for ( h=0; h < ehdr_shnum(image); h++)
+	count = elf_shdr_count(&elf);
+	for ( h=0; h < count; h++)
 	{
-		if (shdr_type(image,h) != SHT_NOTE)
+		shdr = elf_shdr_by_index(&elf, h);
+		if (elf_uval(&elf, shdr, sh_type) != SHT_NOTE)
 			continue;
-		for (note = (Elf_Nhdr*)shdr_start(image,h);
-		     note < (Elf_Nhdr*)shdr_end(image,h);
-		     note = (Elf_Nhdr*)(ELFNOTE_NEXT(note)))
+		end = elf_section_end(&elf, shdr);
+		for (note = elf_section_start(&elf, shdr);
+		     note < end;
+		     note = elf_note_next(&elf, note))
 		{
-			switch(note->n_type)
+			if (0 != strcmp(elf_note_name(&elf, note), "Xen"))
+				continue;
+			switch(elf_uval(&elf, note, type))
 			{
 			case XEN_ELFNOTE_INFO:
-				print_string_note("INFO", note);
+				print_string_note("INFO", &elf , note);
 				break;
 			case XEN_ELFNOTE_ENTRY:
-				print_numeric_note("ENTRY", note);
+				print_numeric_note("ENTRY", &elf , note);
 				break;
 			case XEN_ELFNOTE_HYPERCALL_PAGE:
-				print_numeric_note("HYPERCALL_PAGE", note);
+				print_numeric_note("HYPERCALL_PAGE", &elf , note);
 				break;
 			case XEN_ELFNOTE_VIRT_BASE:
-				print_numeric_note("VIRT_BASE", note);
+				print_numeric_note("VIRT_BASE", &elf , note);
 				break;
 			case XEN_ELFNOTE_PADDR_OFFSET:
-				print_numeric_note("PADDR_OFFSET", note);
+				print_numeric_note("PADDR_OFFSET", &elf , note);
 				break;
 			case XEN_ELFNOTE_XEN_VERSION:
-				print_string_note("XEN_VERSION", note);
+				print_string_note("XEN_VERSION", &elf , note);
 				break;
 			case XEN_ELFNOTE_GUEST_OS:
-				print_string_note("GUEST_OS", note);
+				print_string_note("GUEST_OS", &elf , note);
 				break;
 			case XEN_ELFNOTE_GUEST_VERSION:
-				print_string_note("GUEST_VERSION", note);
+				print_string_note("GUEST_VERSION", &elf , note);
 				break;
 			case XEN_ELFNOTE_LOADER:
-				print_string_note("LOADER", note);
+				print_string_note("LOADER", &elf , note);
 				break;
 			case XEN_ELFNOTE_PAE_MODE:
-				print_string_note("PAE_MODE", note);
+				print_string_note("PAE_MODE", &elf , note);
 				break;
 			case XEN_ELFNOTE_FEATURES:
-				print_string_note("FEATURES", note);
+				print_string_note("FEATURES", &elf , note);
 				break;
 			default:
-				printf("unknown note type %#lx\n",
-				       (unsigned long)note->n_type);
+				printf("unknown note type %#x\n",
+				       (int)elf_uval(&elf, note, type));
 				break;
 			}
 		}
 	}
 
-	for ( h=0; h < ehdr_shnum(image); h++)
-	{
-		const char *name = shdr_name(image,h);
-
-		if ( name == NULL )
-			continue;
-		if ( strcmp(name, "__xen_guest") != 0 )
-			continue;
-
-		printf("__xen_guest: %s\n", (const char *)shdr_start(image, h));
-		break;
-	}
+	shdr = elf_shdr_by_name(&elf, "__xen_guest");
+	if (shdr)
+		printf("__xen_guest: %s\n", (char*)elf_section_start(&elf, shdr));
 
 	return 0;
 }

--

#7 lore raw

PATCH 7/10 libxc header fixups.

In reply to Gerd Hoffmann

Make some arch-specific #defines for page table handling available unconditionally, add a suffix to avoid name clashes.

The versions without suffix are defined depending on the architecture like they used to, so code using them continues to work.

---
 tools/libxc/xg_private.h |  114 +++++++++++++++++++++++++++--------------------
 1 file changed, 67 insertions(+), 47 deletions(-)
tools/libxc/xg_private.h +67 -47
Index: build-32-unstable-11822/tools/libxc/xg_private.h
===================================================================
@@ -46,37 +46,41 @@ unsigned long csum_page (void * page);
 #define _PAGE_PSE       0x080
 #define _PAGE_GLOBAL    0x100
 
-#define L1_PAGETABLE_SHIFT_PAE   12
-#define L2_PAGETABLE_SHIFT_PAE   21
-#define L3_PAGETABLE_SHIFT_PAE   30
-
-#define L2_PAGETABLE_SHIFT_I386  22
-
-#if defined(__i386__)
-#define L1_PAGETABLE_SHIFT       12
-#define L2_PAGETABLE_SHIFT       22
-#elif defined(__x86_64__)
-#define L1_PAGETABLE_SHIFT       12
-#define L2_PAGETABLE_SHIFT       21
-#define L3_PAGETABLE_SHIFT       30
-#define L4_PAGETABLE_SHIFT       39
-#endif
+#define L1_PAGETABLE_SHIFT_I386       12
+#define L2_PAGETABLE_SHIFT_I386       22
+#define L1_PAGETABLE_ENTRIES_I386   1024
+#define L2_PAGETABLE_ENTRIES_I386   1024
 
-#define L1_PAGETABLE_ENTRIES_PAE  512
-#define L2_PAGETABLE_ENTRIES_PAE  512
-#define L3_PAGETABLE_ENTRIES_PAE    4
+#define L1_PAGETABLE_SHIFT_PAE        12
+#define L2_PAGETABLE_SHIFT_PAE        21
+#define L3_PAGETABLE_SHIFT_PAE        30
+#define L1_PAGETABLE_ENTRIES_PAE     512
+#define L2_PAGETABLE_ENTRIES_PAE     512
+#define L3_PAGETABLE_ENTRIES_PAE       4
 
-#define L1_PAGETABLE_ENTRIES_I386 1024
-#define L2_PAGETABLE_ENTRIES_I386 1024
+#define L1_PAGETABLE_SHIFT_X86_64     12
+#define L2_PAGETABLE_SHIFT_X86_64     21
+#define L3_PAGETABLE_SHIFT_X86_64     30
+#define L4_PAGETABLE_SHIFT_X86_64     39
+#define L1_PAGETABLE_ENTRIES_X86_64  512
+#define L2_PAGETABLE_ENTRIES_X86_64  512
+#define L3_PAGETABLE_ENTRIES_X86_64  512
+#define L4_PAGETABLE_ENTRIES_X86_64  512
 
 #if defined(__i386__)
-#define L1_PAGETABLE_ENTRIES   1024
-#define L2_PAGETABLE_ENTRIES   1024
+#define L1_PAGETABLE_SHIFT     L1_PAGETABLE_SHIFT_I386
+#define L2_PAGETABLE_SHIFT     L2_PAGETABLE_SHIFT_I386
+#define L1_PAGETABLE_ENTRIES   L1_PAGETABLE_ENTRIES_I386
+#define L2_PAGETABLE_ENTRIES   L2_PAGETABLE_ENTRIES_I386
 #elif defined(__x86_64__)
-#define L1_PAGETABLE_ENTRIES    512
-#define L2_PAGETABLE_ENTRIES    512
-#define L3_PAGETABLE_ENTRIES    512
-#define L4_PAGETABLE_ENTRIES    512
+#define L1_PAGETABLE_SHIFT     L1_PAGETABLE_SHIFT_X86_64
+#define L2_PAGETABLE_SHIFT     L2_PAGETABLE_SHIFT_X86_64
+#define L3_PAGETABLE_SHIFT     L3_PAGETABLE_SHIFT_X86_64
+#define L4_PAGETABLE_SHIFT     L4_PAGETABLE_SHIFT_X86_64
+#define L1_PAGETABLE_ENTRIES   L1_PAGETABLE_ENTRIES_X86_64
+#define L2_PAGETABLE_ENTRIES   L2_PAGETABLE_ENTRIES_X86_64
+#define L3_PAGETABLE_ENTRIES   L3_PAGETABLE_ENTRIES_X86_64
+#define L4_PAGETABLE_ENTRIES   L4_PAGETABLE_ENTRIES_X86_64
 #endif
 
 typedef uint32_t l1_pgentry_32_t;
@@ -84,13 +88,23 @@ typedef uint32_t l2_pgentry_32_t;
 typedef uint64_t l1_pgentry_64_t;
 typedef uint64_t l2_pgentry_64_t;
 typedef uint64_t l3_pgentry_64_t;
-typedef unsigned long l1_pgentry_t;
-typedef unsigned long l2_pgentry_t;
-#if defined(__x86_64__)
-typedef unsigned long l3_pgentry_t;
-typedef unsigned long l4_pgentry_t;
+typedef uint64_t l4_pgentry_64_t;
+
+#if defined(__i386__)
+typedef l1_pgentry_32_t l1_pgentry_t;
+typedef l2_pgentry_32_t l2_pgentry_t;
+#elif defined(__x86_64__)
+typedef l1_pgentry_64_t l1_pgentry_t;
+typedef l2_pgentry_64_t l2_pgentry_t;
+typedef l3_pgentry_64_t l3_pgentry_t;
+typedef l4_pgentry_64_t l4_pgentry_t;
 #endif
 
+#define l1_table_offset_i386(_a) \
+  (((_a) >> L1_PAGETABLE_SHIFT_I386) & (L1_PAGETABLE_ENTRIES_I386 - 1))
+#define l2_table_offset_i386(_a) \
+  (((_a) >> L2_PAGETABLE_SHIFT_I386) & (L2_PAGETABLE_ENTRIES_I386 - 1))
+
 #define l1_table_offset_pae(_a) \
   (((_a) >> L1_PAGETABLE_SHIFT_PAE) & (L1_PAGETABLE_ENTRIES_PAE - 1))
 #define l2_table_offset_pae(_a) \
@@ -98,27 +112,33 @@ typedef unsigned long l4_pgentry_t;
 #define l3_table_offset_pae(_a) \
   (((_a) >> L3_PAGETABLE_SHIFT_PAE) & (L3_PAGETABLE_ENTRIES_PAE - 1))
 
-#define l1_table_offset_i386(_a) \
-  (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES_I386 - 1))
-#define l2_table_offset_i386(_a) \
-  (((_a) >> L2_PAGETABLE_SHIFT_I386) & (L2_PAGETABLE_ENTRIES_I386 - 1))
+#define l1_table_offset_x86_64(_a) \
+  (((_a) >> L1_PAGETABLE_SHIFT_X86_64) & (L1_PAGETABLE_ENTRIES_X86_64 - 1))
+#define l2_table_offset_x86_64(_a) \
+  (((_a) >> L2_PAGETABLE_SHIFT_X86_64) & (L2_PAGETABLE_ENTRIES_X86_64 - 1))
+#define l3_table_offset_x86_64(_a) \
+  (((_a) >> L3_PAGETABLE_SHIFT_X86_64) & (L3_PAGETABLE_ENTRIES_X86_64 - 1))
+#define l4_table_offset_x86_64(_a) \
+  (((_a) >> L4_PAGETABLE_SHIFT_X86_64) & (L4_PAGETABLE_ENTRIES_X86_64 - 1))
 
 #if defined(__i386__)
-#define l1_table_offset(_a) \
-          (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES - 1))
-#define l2_table_offset(_a) \
-          ((_a) >> L2_PAGETABLE_SHIFT)
+#define l1_table_offset(_a) l1_table_offset_i386(_a)
+#define l2_table_offset(_a) l2_table_offset_i386(_a)
 #elif defined(__x86_64__)
-#define l1_table_offset(_a) \
-  (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES - 1))
-#define l2_table_offset(_a) \
-  (((_a) >> L2_PAGETABLE_SHIFT) & (L2_PAGETABLE_ENTRIES - 1))
-#define l3_table_offset(_a) \
-  (((_a) >> L3_PAGETABLE_SHIFT) & (L3_PAGETABLE_ENTRIES - 1))
-#define l4_table_offset(_a) \
-  (((_a) >> L4_PAGETABLE_SHIFT) & (L4_PAGETABLE_ENTRIES - 1))
+#define l1_table_offset(_a) l1_table_offset_x86_64(_a)
+#define l2_table_offset(_a) l2_table_offset_x86_64(_a)
+#define l3_table_offset(_a) l3_table_offset_x86_64(_a)
+#define l4_table_offset(_a) l4_table_offset_x86_64(_a)
 #endif
 
+#define PAGE_SHIFT_X86          12
+#define PAGE_SIZE_X86           (1UL << PAGE_SHIFT_X86)
+#define PAGE_MASK_X86           (~(PAGE_SIZE_X86-1))
+
+#define PAGE_SHIFT_IA64         14
+#define PAGE_SIZE_IA64          (1UL << PAGE_SHIFT_IA64)
+#define PAGE_MASK_IA64          (~(PAGE_SIZE_IA64-1))
+
 struct domain_setup_info
 {
     uint64_t v_start;

--

#8 lore raw

PATCH 8/10 libxc domain builder rewrite, core bits.

In reply to Gerd Hoffmann
---
 tools/libxc/Makefile           |   14 
 tools/libxc/xc_dom.h           |  267 ++++++++++++++
 tools/libxc/xc_dom_binloader.c |  281 ++++++++++++++
 tools/libxc/xc_dom_boot.c      |  514 +++++++++++++++++++++++++++
 tools/libxc/xc_dom_core.c      |  775 +++++++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_dom_elfloader.c |  271 ++++++++++++++
 tools/libxc/xc_dom_ia64.c      |  118 ++++++
 tools/libxc/xc_dom_powerpc64.c |  100 +++++
 tools/libxc/xc_dom_x86.c       |  542 ++++++++++++++++++++++++++++
 9 files changed, 2882 insertions(+)
tools/libxc/Makefile +14 -0
Index: build-32-unstable-12621/tools/libxc/Makefile
===================================================================
@@ -44,6 +44,20 @@ $(LIBELF_SRCS) libelf-private.h:
 # add libelf bits to libxc
 GUEST_SRCS-y += $(LIBELF_SRCS)
 
+# new domain builder
+GUEST_SRCS-y += xc_dom_core.c xc_dom_boot.c
+GUEST_SRCS-y += xc_dom_elfloader.c
+GUEST_SRCS-y += xc_dom_binloader.c
+
+ifeq ($(CONFIG_POWERPC),y)
+# big endian boxes
+GUEST_SRCS-y += xc_dom_powerpc64.c
+else
+# little endian boxes
+GUEST_SRCS-y += xc_dom_x86.c
+GUEST_SRCS-y += xc_dom_ia64.c
+endif
+
 -include $(XEN_TARGET_ARCH)/Makefile
 
 CFLAGS   += -Werror -Wmissing-prototypes
tools/libxc/xc_dom.h new file +267 -0

267 lines not on this page · raw message

tools/libxc/xc_dom_boot.c new file +514 -0

514 lines not on this page · raw message

tools/libxc/xc_dom_core.c new file +775 -0

775 lines not on this page · raw message

tools/libxc/xc_dom_ia64.c new file +118 -0

118 lines not on this page · raw message

tools/libxc/xc_dom_x86.c new file +542 -0

542 lines not on this page · raw message

tools/libxc/xc_dom_powerpc64.c new file +100 -0

100 lines not on this page · raw message

tools/libxc/xc_dom_elfloader.c new file +271 -0

271 lines not on this page · raw message

tools/libxc/xc_dom_binloader.c new file +281 -0

281 lines not on this page · raw message

--

#9 lore raw

PATCH 9/10 libxc domain builder rewrite, linux builder

In reply to Gerd Hoffmann

use new domain builder for the linux (aka generic elf) loader.

---
 tools/libxc/Makefile              |    7 +
 tools/libxc/xc_dom_compat_linux.c |  136 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+), 2 deletions(-)
tools/libxc/xc_dom_compat_linux.c new file +136 -0
Index: build-32-unstable-12621/tools/libxc/xc_dom_compat_linux.c
===================================================================
@@ -0,0 +1,136 @@
+/*
+ * Xen domain builder -- compatibility code.
+ *
+ * Replacements for xc_linux_build & friends,
+ * as example code and to make the new builder
+ * usable as drop-in replacement.
+ *
+ * This code is licenced under the GPL.
+ * written 2006 by Gerd Hoffmann <[email protected]>.
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include <zlib.h>
+
+#include "xenctrl.h"
+#include "xg_private.h"
+#include "xc_dom.h"
+
+/* ------------------------------------------------------------------------ */
+
+static int xc_linux_build_internal(struct xc_dom_image *dom,
+				   int xc_handle, uint32_t domid,
+				   unsigned int mem_mb,
+				   unsigned long flags,
+				   unsigned int store_evtchn,
+				   unsigned long *store_mfn,
+				   unsigned int console_evtchn,
+				   unsigned long *console_mfn)
+{
+    int rc;
+
+    if (0 != (rc = xc_dom_parse_image(dom)))
+	goto out;
+    if (0 != (rc = xc_dom_mem_init(dom, mem_mb)))
+	goto out;
+    if (0 != (rc = xc_dom_boot_init(dom, xc_handle, domid)))
+	goto out;
+    if (0 != (rc = xc_dom_build_image(dom)))
+	goto out;
+
+    dom->flags = flags;
+    dom->console_evtchn = console_evtchn;
+    dom->xenstore_evtchn = store_evtchn;
+    rc = xc_dom_boot_image(dom);
+    if (0 != rc)
+	goto out;
+
+    *console_mfn = xc_dom_p2m_host(dom, dom->console_pfn);
+    *store_mfn = xc_dom_p2m_host(dom, dom->xenstore_pfn);
+
+  out:
+    return rc;
+}
+
+int xc_linux_build_mem(int xc_handle, uint32_t domid,
+#ifdef XC_UNSTABLE
+		       unsigned int mem_mb,
+#endif
+		       const char *image_buffer,
+		       unsigned long image_size,
+		       const char *initrd,
+		       unsigned long initrd_len,
+		       const char *cmdline,
+		       const char *features,
+		       unsigned long flags,
+		       unsigned int store_evtchn,
+		       unsigned long *store_mfn,
+		       unsigned int console_evtchn, unsigned long *console_mfn)
+{
+    struct xc_dom_image *dom;
+    int rc;
+
+#ifndef XC_UNSTABLE
+    unsigned long nr_pages = xc_get_tot_pages(xc_handle, domid);
+    unsigned int mem_mb = nr_pages >> (20 - PAGE_SHIFT);
+#endif
+
+    xc_dom_loginit();
+    dom = xc_dom_allocate(cmdline, features);
+    if (0 != (rc = xc_dom_kernel_mem(dom, image_buffer, image_size)))
+	goto out;
+    if (initrd)
+	if (0 != (rc = xc_dom_ramdisk_mem(dom, initrd, initrd_len)))
+	    goto out;
+
+    rc = xc_linux_build_internal(dom, xc_handle, domid,
+				 mem_mb, flags,
+				 store_evtchn, store_mfn,
+				 console_evtchn, console_mfn);
+
+  out:
+    xc_dom_release(dom);
+    return rc;
+}
+
+int xc_linux_build(int xc_handle, uint32_t domid,
+#ifdef XC_UNSTABLE
+		   unsigned int mem_mb,
+#endif
+		   const char *image_name,
+		   const char *initrd_name,
+		   const char *cmdline,
+		   const char *features,
+		   unsigned long flags,
+		   unsigned int store_evtchn,
+		   unsigned long *store_mfn,
+		   unsigned int console_evtchn, unsigned long *console_mfn)
+{
+    struct xc_dom_image *dom;
+    int rc;
+
+#ifndef XC_UNSTABLE
+    unsigned long nr_pages = xc_get_tot_pages(xc_handle, domid);
+    unsigned int mem_mb = nr_pages >> (20 - PAGE_SHIFT);
+#endif
+
+    xc_dom_loginit();
+    dom = xc_dom_allocate(cmdline, features);
+    if (0 != (rc = xc_dom_kernel_file(dom, image_name)))
+	goto out;
+    if (initrd_name && strlen(initrd_name))
+	if (0 != (rc = xc_dom_ramdisk_file(dom, initrd_name)))
+	    goto out;
+
+    rc = xc_linux_build_internal(dom, xc_handle, domid,
+				 mem_mb, flags,
+				 store_evtchn, store_mfn,
+				 console_evtchn, console_mfn);
+
+  out:
+    xc_dom_release(dom);
+    return rc;
+}
tools/libxc/Makefile +5 -2
Index: build-32-unstable-12621/tools/libxc/Makefile
===================================================================
@@ -24,8 +24,8 @@ GUEST_SRCS-y :=
 GUEST_SRCS-y += xc_load_bin.c
 GUEST_SRCS-y += xc_load_elf.c
 GUEST_SRCS-y += xg_private.c
-GUEST_SRCS-$(CONFIG_X86) += xc_linux_build.c
-GUEST_SRCS-$(CONFIG_IA64) += xc_linux_build.c
+#GUEST_SRCS-$(CONFIG_X86) += xc_linux_build.c
+#GUEST_SRCS-$(CONFIG_IA64) += xc_linux_build.c
 GUEST_SRCS-$(CONFIG_MIGRATE) += xc_linux_restore.c xc_linux_save.c
 GUEST_SRCS-$(CONFIG_HVM) += xc_hvm_build.c
 
@@ -58,6 +58,9 @@ GUEST_SRCS-y += xc_dom_x86.c
 GUEST_SRCS-y += xc_dom_ia64.c
 endif
 
+GUEST_SRCS-$(CONFIG_X86)     += xc_dom_compat_linux.c
+GUEST_SRCS-$(CONFIG_IA64)    += xc_dom_compat_linux.c
+
 -include $(XEN_TARGET_ARCH)/Makefile
 
 CFLAGS   += -Werror -Wmissing-prototypes

--

#10 lore raw

PATCH 10/10 Support transparant gunzipping in the readnotes utility.

In reply to Gerd Hoffmann
---
 tools/xcutils/readnotes.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
tools/xcutils/readnotes.c +12 -2
Index: build-32-unstable-12621/tools/xcutils/readnotes.c
===================================================================
@@ -11,6 +11,7 @@
 #include <sys/mman.h>
 
 #include <xg_private.h>
+#include <xc_dom.h> /* gunzip bits */
 
 #include <xen/libelf.h>
 
@@ -33,8 +34,8 @@ static void print_numeric_note(const cha
 int main(int argc, char **argv)
 {
 	const char *f;
-	int fd,h,size,count;
-	void *image;
+	int fd,h,size,usize,count;
+	void *image,*tmp;
 	struct stat st;
 	struct elf_binary elf;
 	const elf_shdr *shdr;
@@ -68,6 +69,15 @@ int main(int argc, char **argv)
 	}
 	size = st.st_size;
 
+	usize = xc_dom_check_gzip(image, st.st_size);
+	if (usize)
+	{
+		tmp = malloc(size);
+		xc_dom_do_gunzip(image, st.st_size, tmp, size);
+		image = tmp;
+		size = usize;
+	}
+
 	if (0 != elf_init(&elf, image, size))
 	{
 		fprintf(stderr, "File %s is not an ELF image\n", f);

--