Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 00/10] 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 -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 01/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. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- 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(+) Index: build-32-unstable-12502/xen/include/public/foreign/Makefile ==================================================================--- /dev/null +++ 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) Index: build-32-unstable-12502/xen/include/public/foreign/mkheader.py ==================================================================--- /dev/null +++ build-32-unstable-12502/xen/include/public/foreign/mkheader.py @@ -0,0 +1,153 @@ +#!/usr/bin/python + +import sys, re; +from structs import structs, defines; + +# command line arguments +arch = sys.argv[1]; +outfile = sys.argv[2]; +infiles = sys.argv[3:]; + + +########################################################################### + +########################################################################### +# configuration #2: architecture information + +inttypes = {}; +header = {}; +footer = {}; + +# x86_32 +inttypes["x86_32"] = { + "unsigned long" : "uint32_t", + "long" : "uint32_t", + "xen_pfn_t" : "uint32_t", +}; +header["x86_32"] = """ +#pragma pack(push, 4) +"""; +footer["x86_32"] = """ +#pragma pack(pop) +"""; + +# x86_64 +inttypes["x86_64"] = { + "unsigned long" : "__align8__ uint64_t", + "long" : "__align8__ uint64_t", + "xen_pfn_t" : "__align8__ uint64_t", +}; +header["x86_64"] = """ +#ifdef __GNUC__ +# define __DECL_REG(name) union { uint64_t r ## name, e ## name; } +# define __align8__ __attribute__((aligned (8))) +#else +# define __DECL_REG(name) uint64_t r ## name +# define __align8__ FIXME +#endif +"""; + +# ia64 +inttypes["ia64"] = { + "unsigned long" : "__align8__ uint64_t", + "long" : "__align8__ uint64_t", + "xen_pfn_t" : "__align8__ uint64_t", + "long double" : "__align16__ ldouble_t", +}; +header["ia64"] = """ +#define __align8__ __attribute__((aligned (8))) +#define __align16__ __attribute__((aligned (16))) +typedef unsigned char ldouble_t[16]; +"""; + + +########################################################################### +# main + +input = ""; +output = ""; +fileid = re.sub("[-.]", "_", "__FOREIGN_%s__" % outfile.upper()); + +# read input header files +for name in infiles: + f = open(name, "r"); + input += f.read(); + f.close(); + +# add header +output += """ +/* + * public xen defines and struct for %s + * generated by %s -- DO NOT EDIT + */ + +#ifndef %s +#define %s 1 + +""" % (arch, sys.argv[0], fileid, fileid) + +if arch in header: + output += header[arch]; + output += "\n"; + +# add defines to output +for line in re.findall("#define[^\n]+", input): + for define in defines: + regex = "#define\s+%s\\b" % define; + match = re.search(regex, line); + if None == match: + continue; + if define.upper()[0] == define[0]: + replace = define + "_" + arch.upper(); + else: + replace = define + "_" + arch; + regex = "\\b%s\\b" % define; + output += re.sub(regex, replace, line) + "\n"; +output += "\n"; + +# delete defines, comments, empty lines +input = re.sub("#define[^\n]+\n", "", input); +input = re.compile("/\*(.*?)\*/", re.S).sub("", input) +input = re.compile("\n\s*\n", re.S).sub("\n", input); + +# add structs to output +for struct in structs: + regex = "struct\s+%s\s*\{(.*?)\};" % struct; + match = re.search("struct\s+" + struct + "\s*\{(.*?)\};", input, re.S) + if None == match: + output += "#define %s_has_no_%s 1\n" % (arch, struct); + else: + output += "struct %s_%s {%s};\n" % (struct, arch, match.group(1)); + output += "typedef struct %s_%s %s_%s_t;\n" % (struct, arch, struct, arch); + output += "\n"; + +# add footer +if arch in footer: + output += footer[arch]; + output += "\n"; +output += "#endif /* %s */\n" % fileid; + +# replace: defines +for define in defines: + if define.upper()[0] == define[0]: + replace = define + "_" + arch.upper(); + else: + replace = define + "_" + arch; + output = re.sub("\\b%s\\b" % define, replace, output); + +# replace: structs + struct typedefs +for struct in structs: + output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output); + output = re.sub("\\b(%s)_t\\b" % struct, "\\1_%s_t" % arch, output); + +# replace: integer types +integers = inttypes[arch].keys(); +integers.sort(lambda a, b: cmp(len(b),len(a))); +for type in integers: + output = re.sub("\\b%s\\b" % type, inttypes[arch][type], output); + +# print results +f = open(outfile, "w"); +f.write(output); +f.close; + Index: build-32-unstable-12502/xen/include/public/foreign/structs.py ==================================================================--- /dev/null +++ build-32-unstable-12502/xen/include/public/foreign/structs.py @@ -0,0 +1,49 @@ +# configuration: what needs translation + +structs = [ "start_info", + "trap_info", + "pt_fpreg", + "cpu_user_regs", + "xen_ia64_boot_param", + "ia64_tr_entry", + "vcpu_extra_regs", + "vcpu_guest_context", + "arch_vcpu_info", + "vcpu_time_info", + "vcpu_info", + "arch_shared_info", + "shared_info" ]; + +defines = [ "FLAT_RING1_CS", + "FLAT_RING1_DS", + "FLAT_RING1_SS", + + "FLAT_RING3_CS64", + "FLAT_RING3_DS64", + "FLAT_RING3_SS64", + "FLAT_KERNEL_CS64", + "FLAT_KERNEL_DS64", + "FLAT_KERNEL_SS64", + + "FLAT_KERNEL_CS", + "FLAT_KERNEL_DS", + "FLAT_KERNEL_SS", + + # x86_{32,64} + "_VGCF_i387_valid", + "VGCF_i387_valid", + "_VGCF_in_kernel", + "VGCF_in_kernel", + "_VGCF_failsafe_disables_events", + "VGCF_failsafe_disables_events", + "_VGCF_syscall_disables_events", + "VGCF_syscall_disables_events", + + # ia64 + "VGCF_EXTRA_REGS", + + # all archs + "xen_pfn_to_cr3", + "MAX_VIRT_CPUS", + "MAX_GUEST_CMDLINE" ]; + Index: build-32-unstable-12502/xen/include/public/foreign/reference.size ==================================================================--- /dev/null +++ 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 + Index: build-32-unstable-12502/xen/Makefile ==================================================================--- build-32-unstable-12502.orig/xen/Makefile +++ 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 Index: build-32-unstable-12502/xen/include/public/foreign/mkchecker.py ==================================================================--- /dev/null +++ build-32-unstable-12502/xen/include/public/foreign/mkchecker.py @@ -0,0 +1,58 @@ +#!/usr/bin/python + +import sys; +from structs import structs; + +# command line arguments +arch = sys.argv[1]; +outfile = sys.argv[2]; +archs = sys.argv[3:]; + +f = open(outfile, "w"); +f.write('''''' +/* + * sanity checks for generated foreign headers: + * - verify struct sizes + * + * generated by %s -- DO NOT EDIT + */ +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <inttypes.h> +#include "../xen.h" +''''''); + +for a in archs: + f.write(''#include "%s.h"\n'' % a); + +f.write(''int main(int argc, char *argv[])\n{\n''); + +f.write(''\tprintf("\\n");''); +f.write(''printf("%-20s |", "structs");\n''); +for a in archs: + f.write(''\tprintf("%%8s", "%s");\n'' % a); +f.write(''\tprintf("\\n");''); + +f.write(''\tprintf("\\n");''); +for struct in structs: + f.write(''\tprintf("%%-20s |", "%s");\n'' % struct); + for a in archs: + if a == arch: + s = struct; # native + else: + s = struct + "_" + a; + f.write(''#ifdef %s_has_no_%s\n'' % (a, struct)); + f.write(''\tprintf("%8s", "-");\n''); + f.write("#else\n"); + f.write(''\tprintf("%%8zd", sizeof(struct %s));\n'' % s); + f.write("#endif\n"); + + f.write(''\tprintf("\\n");\n\n''); + +f.write(''\tprintf("\\n");\n''); +f.write(''\texit(0);\n''); +f.write(''}\n''); + +f.close(); + Index: build-32-unstable-12502/tools/Rules.mk ==================================================================--- build-32-unstable-12502.orig/tools/Rules.mk +++ 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) -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 02/10] add libelf: an ELF binary parser library.
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. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- 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(-) Index: build-32-unstable-12663/xen/common/Makefile ==================================================================--- build-32-unstable-12663.orig/xen/common/Makefile +++ 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 Index: build-32-unstable-12663/xen/common/libelf/Makefile ==================================================================--- /dev/null +++ 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 Index: build-32-unstable-12663/xen/common/libelf/README ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/common/libelf/README @@ -0,0 +1 @@ +Take care, this code is used by both xen and tools ... Index: build-32-unstable-12663/xen/common/libelf/libelf-dominfo.c ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/common/libelf/libelf-dominfo.c @@ -0,0 +1,422 @@ +/* + * parse xen-specific informations out of elf kernel binaries. + */ + +#include "libelf-private.h" + +/* ------------------------------------------------------------------------ */ +/* xen features */ + +const char *elf_xen_feature_names[] = { + [XENFEAT_writable_page_tables] = "writable_page_tables", + [XENFEAT_writable_descriptor_tables] = "writable_descriptor_tables", + [XENFEAT_auto_translated_physmap] = "auto_translated_physmap", + [XENFEAT_supervisor_mode_kernel] = "supervisor_mode_kernel", + [XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb" +}; +const int elf_xen_features + sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]); + +int elf_xen_parse_features(const char *features, + uint32_t *supported, + uint32_t *required) +{ + char feature[64]; + int pos, len, i; + + if (NULL == features) + return 0; + for (pos = 0; features[pos] != ''\0''; pos += len) + { + memset(feature, 0, sizeof(feature)); + for (len = 0;; len++) + { + if (len >= sizeof(feature)-1) + break; + if (features[pos + len] == ''\0'') + break; + if (features[pos + len] == ''|'') + { + len++; + break; + } + feature[len] = features[pos + len]; + } + + for (i = 0; i < elf_xen_features; i++) + { + if (!elf_xen_feature_names[i]) + continue; + if (NULL != required && feature[0] == ''!'') + { + /* required */ + if (0 == strcmp(feature + 1, elf_xen_feature_names[i])) + { + elf_xen_feature_set(i, supported); + elf_xen_feature_set(i, required); + break; + } + } + else + { + /* supported */ + if (0 == strcmp(feature, elf_xen_feature_names[i])) + { + elf_xen_feature_set(i, supported); + break; + } + } + } + if (i == elf_xen_features) + return -1; + } + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* xen elf notes */ + +int elf_xen_parse_note(struct elf_binary *elf, + struct elf_dom_parms *parms, + const elf_note *note) +{ +/* *INDENT-OFF* */ + static const struct { + char *name; + int str; + } note_desc[] = { + [XEN_ELFNOTE_ENTRY] = { "ENTRY", 0}, + [XEN_ELFNOTE_HYPERCALL_PAGE] = { "HYPERCALL_PAGE", 0}, + [XEN_ELFNOTE_VIRT_BASE] = { "VIRT_BASE", 0}, + [XEN_ELFNOTE_PADDR_OFFSET] = { "PADDR_OFFSET", 0}, +#ifdef XEN_ELFNOTE_HV_START_LOW + [XEN_ELFNOTE_HV_START_LOW] = { "HV_START_LOW", 0}, +#endif + [XEN_ELFNOTE_XEN_VERSION] = { "XEN_VERSION", 1}, + [XEN_ELFNOTE_GUEST_OS] = { "GUEST_OS", 1}, + [XEN_ELFNOTE_GUEST_VERSION] = { "GUEST_VERSION", 1}, + [XEN_ELFNOTE_LOADER] = { "LOADER", 1}, + [XEN_ELFNOTE_PAE_MODE] = { "PAE_MODE", 1}, + [XEN_ELFNOTE_FEATURES] = { "FEATURES", 1}, + [XEN_ELFNOTE_BSD_SYMTAB] = { "BSD_SYMTAB", 1}, + }; +/* *INDENT-ON* */ + + const char *str = NULL; + uint64_t val = 0; + int type = elf_uval(elf, note, type); + + if ((type >= sizeof(note_desc) / sizeof(note_desc[0])) || + (NULL == note_desc[type].name)) + { + elf_err(elf, "%s: unknown xen elf note (0x%x)\n", + __FUNCTION__, type); + return -1; + } + + if (note_desc[type].str) + { + str = elf_note_desc(elf, note); + elf_msg(elf, "%s: %s = \"%s\"\n", __FUNCTION__, + note_desc[type].name, str); + } + else + { + val = elf_note_numeric(elf, note); + elf_msg(elf, "%s: %s = 0x%" PRIx64 "\n", __FUNCTION__, + note_desc[type].name, val); + } + + switch (type) + { + case XEN_ELFNOTE_LOADER: + strncpy(parms->loader, str, sizeof(parms->loader)); + break; + case XEN_ELFNOTE_GUEST_OS: + strncpy(parms->guest_os, str, sizeof(parms->guest_os)); + break; + case XEN_ELFNOTE_GUEST_VERSION: + strncpy(parms->guest_ver, str, sizeof(parms->guest_ver)); + break; + case XEN_ELFNOTE_XEN_VERSION: + strncpy(parms->xen_ver, str, sizeof(parms->xen_ver)); + break; + case XEN_ELFNOTE_PAE_MODE: + if (0 == strcmp(str, "yes")) + parms->pae = 2; + break; + case XEN_ELFNOTE_BSD_SYMTAB: + if (0 == strcmp(str, "yes")) + parms->bsd_symtab = 1; + break; + + case XEN_ELFNOTE_VIRT_BASE: + parms->virt_base = val; + break; + case XEN_ELFNOTE_ENTRY: + parms->virt_entry = val; + break; + case XEN_ELFNOTE_PADDR_OFFSET: + parms->elf_paddr_offset = val; + break; + case XEN_ELFNOTE_HYPERCALL_PAGE: + parms->virt_hypercall = val; + break; +#ifdef XEN_ELFNOTE_HV_START_LOW + case XEN_ELFNOTE_HV_START_LOW: + parms->virt_hv_start_low = val; + break; +#endif + + case XEN_ELFNOTE_FEATURES: + if (0 != elf_xen_parse_features(str, parms->f_supported, + parms->f_required)) + return -1; + break; + + } + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* __xen_guest section */ + +int elf_xen_parse_guest_info(struct elf_binary *elf, + struct elf_dom_parms *parms) +{ + const char *h; + char name[32], value[128]; + int len; + + h = parms->guest_info; + while (*h) + { + memset(name, 0, sizeof(name)); + memset(value, 0, sizeof(value)); + for (len = 0;; len++, h++) { + if (len >= sizeof(name)-1) + break; + if (*h == ''\0'') + break; + if (*h == '','') + { + h++; + break; + } + if (*h == ''='') + { + h++; + for (len = 0;; len++, h++) { + if (len >= sizeof(value)-1) + break; + if (*h == ''\0'') + break; + if (*h == '','') + { + h++; + break; + } + value[len] = *h; + } + break; + } + name[len] = *h; + } + elf_msg(elf, "%s: %s=\"%s\"\n", __FUNCTION__, name, value); + + /* strings */ + if (0 == strcmp(name, "LOADER")) + strncpy(parms->loader, value, sizeof(parms->loader)); + if (0 == strcmp(name, "GUEST_OS")) + strncpy(parms->guest_os, value, sizeof(parms->guest_os)); + if (0 == strcmp(name, "GUEST_VER")) + strncpy(parms->guest_ver, value, sizeof(parms->guest_ver)); + if (0 == strcmp(name, "XEN_VER")) + strncpy(parms->xen_ver, value, sizeof(parms->xen_ver)); + if (0 == strcmp(name, "PAE")) + { + if (0 == strcmp(value, "yes[extended-cr3]")) + parms->pae = 2; + else if (0 == strncmp(value, "yes", 3)) + parms->pae = 1; + } + if (0 == strcmp(name, "BSD_SYMTAB")) + parms->bsd_symtab = 1; + + /* longs */ + if (0 == strcmp(name, "VIRT_BASE")) + parms->virt_base = strtoull(value, NULL, 0); + if (0 == strcmp(name, "VIRT_ENTRY")) + parms->virt_entry = strtoull(value, NULL, 0); + if (0 == strcmp(name, "ELF_PADDR_OFFSET")) + parms->elf_paddr_offset = strtoull(value, NULL, 0); + if (0 == strcmp(name, "HYPERCALL_PAGE")) + parms->virt_hypercall = (strtoull(value, NULL, 0) << 12) + + parms->virt_base; + + /* other */ + if (0 == strcmp(name, "FEATURES")) + if (0 != elf_xen_parse_features(value, parms->f_supported, + parms->f_required)) + return -1; + } + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* sanity checks */ + +static int elf_xen_note_check(struct elf_binary *elf, + struct elf_dom_parms *parms) +{ + if (NULL == parms->elf_note_start && NULL == parms->guest_info) { + int machine = elf_uval(elf, elf->ehdr, e_machine); + if (EM_386 == machine || EM_X86_64 == machine) { + elf_err(elf, "%s: ERROR: Not a Xen-ELF image: " + "No ELF notes or ''__xen_guest'' section found.\n", + __FUNCTION__); + return -1; + } + return 0; + } + + /* Check the contents of the Xen notes or guest string. */ + if ( ( 0 == strlen(parms->loader) || strncmp(parms->loader, "generic", 7) ) && + ( 0 == strlen(parms->guest_os) || strncmp(parms->guest_os, "linux", 5) ) ) + { + elf_err(elf, "%s: ERROR: Will only load images built for the generic " + "loader or Linux images", __FUNCTION__); + return -1; + } + + if ( 0 == strlen(parms->xen_ver) || strncmp(parms->xen_ver, "xen-3.0", 7) ) + { + elf_err(elf, "%s: ERROR: Xen will only load images built for Xen v3.0\n", + __FUNCTION__); + return -1; + } + return 0; +} + +static int elf_xen_addr_calc_check(struct elf_binary *elf, + struct elf_dom_parms *parms) +{ + if (UNSET_ADDR != parms->elf_paddr_offset && + UNSET_ADDR == parms->virt_base ) + { + elf_err(elf, "%s: ERROR: ELF_PADDR_OFFSET set, VIRT_BASE unset\n", + __FUNCTION__); + return -1; + } + + /* Initial guess for virt_base is 0 if it is not explicitly defined. */ + if (UNSET_ADDR == parms->virt_base) + { + parms->virt_base = 0; + elf_msg(elf, "%s: VIRT_BASE unset, using 0x%" PRIx64 "\n", + __FUNCTION__, parms->virt_base); + } + + /* + * If we are using the legacy __xen_guest section then elf_pa_off + * defaults to v_start in order to maintain compatibility with + * older hypervisors which set padd in the ELF header to + * virt_base. + * + * If we are using the modern ELF notes interface then the default + * is 0. + */ + if (UNSET_ADDR == parms->elf_paddr_offset) + { + if (parms->elf_note_start) + parms->elf_paddr_offset = 0; + else + parms->elf_paddr_offset = parms->virt_base; + elf_msg(elf, "%s: ELF_PADDR_OFFSET unset, using 0x%" PRIx64 "\n", + __FUNCTION__, parms->elf_paddr_offset); + } + + parms->virt_offset = parms->virt_base - parms->elf_paddr_offset; + parms->virt_kstart = elf->pstart + parms->virt_offset; + parms->virt_kend = elf->pend + parms->virt_offset; + + if (UNSET_ADDR == parms->virt_entry) + parms->virt_entry = elf_uval(elf, elf->ehdr, e_entry); + + elf_msg(elf, "%s: addresses:\n", __FUNCTION__); + elf_msg(elf, " virt_base = 0x%" PRIx64 "\n", parms->virt_base); + elf_msg(elf, " elf_paddr_offset = 0x%" PRIx64 "\n", parms->elf_paddr_offset); + elf_msg(elf, " virt_offset = 0x%" PRIx64 "\n", parms->virt_offset); + elf_msg(elf, " virt_kstart = 0x%" PRIx64 "\n", parms->virt_kstart); + elf_msg(elf, " virt_kend = 0x%" PRIx64 "\n", parms->virt_kend); + elf_msg(elf, " virt_entry = 0x%" PRIx64 "\n", parms->virt_entry); + + if ( (parms->virt_kstart > parms->virt_kend) || + (parms->virt_entry < parms->virt_kstart) || + (parms->virt_entry > parms->virt_kend) || + (parms->virt_base > parms->virt_kstart) ) + { + elf_err(elf, "%s: ERROR: ELF start or entries are out of bounds.\n", + __FUNCTION__); + return -1; + } + + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* glue it all together ... */ + +int elf_xen_parse(struct elf_binary *elf, + struct elf_dom_parms *parms) +{ + const elf_note *note; + const elf_shdr *shdr; + int xen_elfnotes = 0; + int i, count; + + memset(parms, 0, sizeof(*parms)); + parms->virt_base = UNSET_ADDR; + parms->virt_entry = UNSET_ADDR; + parms->virt_hypercall = UNSET_ADDR; + parms->virt_hv_start_low = UNSET_ADDR; + parms->elf_paddr_offset = UNSET_ADDR; + + /* find and parse elf notes */ + count = elf_shdr_count(elf); + for (i = 0; i < count; i++) + { + shdr = elf_shdr_by_index(elf, i); + if (0 == strcmp(elf_section_name(elf, shdr), "__xen_guest")) + parms->guest_info = elf_section_start(elf, shdr); + if (elf_uval(elf, shdr, sh_type) != SHT_NOTE) + continue; + parms->elf_note_start = elf_section_start(elf, shdr); + parms->elf_note_end = elf_section_end(elf, shdr); + for (note = parms->elf_note_start; + (void *)note < parms->elf_note_end; + note = elf_note_next(elf, note)) + { + if (0 != strcmp(elf_note_name(elf, note), "Xen")) + continue; + if (0 != elf_xen_parse_note(elf, parms, note)) + return -1; + xen_elfnotes++; + } + } + + if (!xen_elfnotes && parms->guest_info) + { + parms->elf_note_start = NULL; + parms->elf_note_end = NULL; + elf_msg(elf, "%s: __xen_guest: \"%s\"\n", __FUNCTION__, + parms->guest_info); + elf_xen_parse_guest_info(elf, parms); + } + + if (0 != elf_xen_note_check(elf, parms)) + return -1; + if (0 != elf_xen_addr_calc_check(elf, parms)) + return -1; + return 0; +} Index: build-32-unstable-12663/xen/common/libelf/libelf-loader.c ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/common/libelf/libelf-loader.c @@ -0,0 +1,145 @@ +/* + * parse and load elf binaries + */ + +#include "libelf-private.h" + +/* ------------------------------------------------------------------------ */ + +int elf_init(struct elf_binary *elf, const char *image, size_t size) +{ + const elf_shdr *shdr; + uint64_t i, count, section, offset; + + if (!elf_is_elfbinary(image)) + return -1; + + memset(elf, 0, sizeof(*elf)); + elf->image = image; + elf->size = size; + elf->ehdr = (elf_ehdr *) image; + elf->class = elf->ehdr->e32.e_ident[EI_CLASS]; + elf->data = elf->ehdr->e32.e_ident[EI_DATA]; + + /* sanity check phdr */ + offset = elf_uval(elf, elf->ehdr, e_phoff) + + elf_uval(elf, elf->ehdr, e_phentsize) * elf_phdr_count(elf); + if (offset > elf->size) + return -1; + + /* sanity check shdr */ + offset = elf_uval(elf, elf->ehdr, e_shoff) + + elf_uval(elf, elf->ehdr, e_shentsize) * elf_shdr_count(elf); + if (offset > elf->size) + return -1; + + /* find section string table */ + section = elf_uval(elf, elf->ehdr, e_shstrndx); + shdr = elf_shdr_by_index(elf, section); + if (NULL != shdr) + elf->sec_strtab = elf_section_start(elf, shdr); + + /* find symbol table, symbol string table */ + count = elf_shdr_count(elf); + for (i = 0; i < count; i++) + { + shdr = elf_shdr_by_index(elf, i); + if (elf_uval(elf, shdr, sh_type) != SHT_SYMTAB) + continue; + elf->sym_tab = shdr; + shdr = elf_shdr_by_index(elf, elf_uval(elf, shdr, sh_link)); + if (NULL == shdr) + { + elf->sym_tab = NULL; + continue; + } + elf->sym_strtab = elf_section_start(elf, shdr); + break; + } + return 0; +} + +#ifndef __XEN__ +void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose) +{ + elf->log = log; + elf->verbose = verbose; +} +#else +void elf_set_verbose(struct elf_binary *elf) +{ + elf->verbose = 1; +} +#endif + +void elf_parse_binary(struct elf_binary *elf) +{ + const elf_phdr *phdr; + uint64_t low = -1; + uint64_t high = 0; + uint64_t i, count, paddr, memsz; + + count = elf_uval(elf, elf->ehdr, e_phnum); + for (i = 0; i < count; i++) + { + phdr = elf_phdr_by_index(elf, i); + if (!elf_phdr_is_loadable(elf, phdr)) + continue; + paddr = elf_uval(elf, phdr, p_paddr); + memsz = elf_uval(elf, phdr, p_memsz); + elf_msg(elf, "%s: phdr: paddr=0x%" PRIx64 + " memsz=0x%" PRIx64 "\n", __FUNCTION__, paddr, memsz); + if (low > paddr) + low = paddr; + if (high < paddr + memsz) + high = paddr + memsz; + } + elf->pstart = low; + elf->pend = high; + elf_msg(elf, "%s: memory: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", + __FUNCTION__, elf->pstart, elf->pend); +} + +void elf_load_binary(struct elf_binary *elf) +{ + const elf_phdr *phdr; + uint64_t i, count, paddr, offset, filesz, memsz; + char *dest; + + count = elf_uval(elf, elf->ehdr, e_phnum); + for (i = 0; i < count; i++) + { + phdr = elf_phdr_by_index(elf, i); + if (!elf_phdr_is_loadable(elf, phdr)) + continue; + paddr = elf_uval(elf, phdr, p_paddr); + offset = elf_uval(elf, phdr, p_offset); + filesz = elf_uval(elf, phdr, p_filesz); + memsz = elf_uval(elf, phdr, p_memsz); + dest = elf_get_ptr(elf, paddr); + memcpy(dest, elf->image + offset, filesz); + memset(dest + filesz, 0, memsz - filesz); + } +} + +void *elf_get_ptr(struct elf_binary *elf, unsigned long addr) +{ + return elf->dest + addr - elf->pstart; +} + +uint64_t elf_lookup_addr(struct elf_binary * elf, const char *symbol) +{ + const elf_sym *sym; + uint64_t value; + + sym = elf_sym_by_name(elf, symbol); + if (NULL == sym) + { + elf_err(elf, "%s: not found: %s\n", __FUNCTION__, symbol); + return -1; + } + value = elf_uval(elf, sym, st_value); + elf_msg(elf, "%s: symbol \"%s\" at 0x%" PRIx64 "\n", __FUNCTION__, + symbol, value); + return value; +} Index: build-32-unstable-12663/xen/common/libelf/libelf-private.h ==================================================================--- /dev/null +++ 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 Index: build-32-unstable-12663/xen/common/libelf/libelf-relocate.c ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/common/libelf/libelf-relocate.c @@ -0,0 +1,345 @@ +/* + * ELF relocation code (not used by xen kernel right now). + */ + +#include "libelf-private.h" + +/* ------------------------------------------------------------------------ */ + +static const char *rel_names_i386[] = { + "R_386_NONE", + "R_386_32", + "R_386_PC32", + "R_386_GOT32", + "R_386_PLT32", + "R_386_COPY", + "R_386_GLOB_DAT", + "R_386_JMP_SLOT", + "R_386_RELATIVE", + "R_386_GOTOFF", + "R_386_GOTPC", + "R_386_32PLT", + "R_386_TLS_TPOFF", + "R_386_TLS_IE", + "R_386_TLS_GOTIE", + "R_386_TLS_LE", + "R_386_TLS_GD", + "R_386_TLS_LDM", + "R_386_16", + "R_386_PC16", + "R_386_8", + "R_386_PC8", + "R_386_TLS_GD_32", + "R_386_TLS_GD_PUSH", + "R_386_TLS_GD_CALL", + "R_386_TLS_GD_POP", + "R_386_TLS_LDM_32", + "R_386_TLS_LDM_PUSH", + "R_386_TLS_LDM_CALL", + "R_386_TLS_LDM_POP", + "R_386_TLS_LDO_32", + "R_386_TLS_IE_32", + "R_386_TLS_LE_32", + "R_386_TLS_DTPMOD32", + "R_386_TLS_DTPOFF32", + "R_386_TLS_TPOFF32", +}; + +static int elf_reloc_i386(struct elf_binary *elf, int type, + uint64_t addr, uint64_t value) +{ + void *ptr = elf_get_ptr(elf, addr); + uint32_t *u32; + + switch (type) + { + case 1 /* R_386_32 */ : + u32 = ptr; + *u32 += elf->reloc_offset; + break; + case 2 /* R_386_PC32 */ : + /* nothing */ + break; + default: + return -1; + } + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static const char *rel_names_x86_64[] = { + "R_X86_64_NONE", + "R_X86_64_64", + "R_X86_64_PC32", + "R_X86_64_GOT32", + "R_X86_64_PLT32", + "R_X86_64_COPY", + "R_X86_64_GLOB_DAT", + "R_X86_64_JUMP_SLOT", + "R_X86_64_RELATIVE", + "R_X86_64_GOTPCREL", + "R_X86_64_32", + "R_X86_64_32S", + "R_X86_64_16", + "R_X86_64_PC16", + "R_X86_64_8", + "R_X86_64_PC8", + "R_X86_64_DTPMOD64", + "R_X86_64_DTPOFF64", + "R_X86_64_TPOFF64", + "R_X86_64_TLSGD", + "R_X86_64_TLSLD", + "R_X86_64_DTPOFF32", + "R_X86_64_GOTTPOFF", + "R_X86_64_TPOFF32", +}; + +static int elf_reloc_x86_64(struct elf_binary *elf, int type, + uint64_t addr, uint64_t value) +{ + void *ptr = elf_get_ptr(elf, addr); + uint64_t *u64; + uint32_t *u32; + int32_t *s32; + + switch (type) + { + case 1 /* R_X86_64_64 */ : + u64 = ptr; + value += elf->reloc_offset; + *u64 = value; + break; + case 2 /* R_X86_64_PC32 */ : + u32 = ptr; + *u32 = value - addr; + if (*u32 != (uint32_t) (value - addr)) + { + elf_err(elf, "R_X86_64_PC32 overflow: 0x%" PRIx32 " != 0x%" PRIx32 "\n", + *u32, (uint32_t) (value - addr)); + return -1; + } + break; + case 10 /* R_X86_64_32 */ : + u32 = ptr; + value += elf->reloc_offset; + *u32 = value; + if (*u32 != value) + { + elf_err(elf, "R_X86_64_32 overflow: 0x%" PRIx32 " != 0x%" PRIx64 "\n", + *u32, value); + return -1; + } + break; + case 11 /* R_X86_64_32S */ : + s32 = ptr; + value += elf->reloc_offset; + *s32 = value; + if (*s32 != (int64_t) value) + { + elf_err(elf, "R_X86_64_32S overflow: 0x%" PRIx32 " != 0x%" PRIx64 "\n", + *s32, (int64_t) value); + return -1; + } + break; + default: + return -1; + } + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static struct relocs { + const char **names; + int count; + int (*func) (struct elf_binary * elf, int type, uint64_t addr, + uint64_t value); +} relocs[] +/* *INDENT-OFF* */ +{ + [EM_386] = { + .names = rel_names_i386, + .count = sizeof(rel_names_i386) / sizeof(rel_names_i386[0]), + .func = elf_reloc_i386, + }, + [EM_X86_64] = { + .names = rel_names_x86_64, + .count = sizeof(rel_names_x86_64) / sizeof(rel_names_x86_64[0]), + .func = elf_reloc_x86_64, + } +}; +/* *INDENT-ON* */ + +/* ------------------------------------------------------------------------ */ + +static const char *rela_name(int machine, int type) +{ + if (machine > sizeof(relocs) / sizeof(relocs[0])) + return "unknown mach"; + if (!relocs[machine].names) + return "unknown mach"; + if (type > relocs[machine].count) + return "unknown rela"; + return relocs[machine].names[type]; +} + +static int elf_reloc_section(struct elf_binary *elf, + const elf_shdr * rels, + const elf_shdr * sect, const elf_shdr * syms) +{ + const void *ptr, *end; + const elf_shdr *shdr; + const elf_rela *rela; + const elf_rel *rel; + const elf_sym *sym; + uint64_t s_type; + uint64_t r_offset; + uint64_t r_info; + uint64_t r_addend; + int r_type, r_sym; + size_t rsize; + uint64_t shndx, sbase, addr, value; + const char *sname; + int machine; + + machine = elf_uval(elf, elf->ehdr, e_machine); + if (machine >= sizeof(relocs) / sizeof(relocs[0]) || + NULL == relocs[machine].func) + { + elf_err(elf, "%s: can''t handle machine %d\n", + __FUNCTION__, machine); + return -1; + } + if (elf_swap(elf)) + { + elf_err(elf, "%s: non-native byte order, relocation not supported\n", + __FUNCTION__); + return -1; + } + + s_type = elf_uval(elf, rels, sh_type); + rsize = (SHT_REL == s_type) ? elf_size(elf, rel) : elf_size(elf, rela); + ptr = elf_section_start(elf, rels); + end = elf_section_end(elf, rels); + + for (; ptr < end; ptr += rsize) + { + switch (s_type) + { + case SHT_REL: + rel = ptr; + r_offset = elf_uval(elf, rel, r_offset); + r_info = elf_uval(elf, rel, r_info); + r_addend = 0; + break; + case SHT_RELA: + rela = ptr; + r_offset = elf_uval(elf, rela, r_offset); + r_info = elf_uval(elf, rela, r_info); + r_addend = elf_uval(elf, rela, r_addend); + break; + default: + /* can''t happen */ + return -1; + } + if (elf_64bit(elf)) + { + r_type = ELF64_R_TYPE(r_info); + r_sym = ELF64_R_SYM(r_info); + } + else + { + r_type = ELF32_R_TYPE(r_info); + r_sym = ELF32_R_SYM(r_info); + } + + sym = elf_sym_by_index(elf, r_sym); + shndx = elf_uval(elf, sym, st_shndx); + switch (shndx) + { + case SHN_UNDEF: + sname = "*UNDEF*"; + sbase = 0; + break; + case SHN_COMMON: + elf_err(elf, "%s: invalid section: %" PRId64 "\n", + __FUNCTION__, shndx); + return -1; + case SHN_ABS: + sname = "*ABS*"; + sbase = 0; + break; + default: + shdr = elf_shdr_by_index(elf, shndx); + if (NULL == shdr) + { + elf_err(elf, "%s: invalid section: %" PRId64 "\n", + __FUNCTION__, shndx); + return -1; + } + sname = elf_section_name(elf, shdr); + sbase = elf_uval(elf, shdr, sh_addr); + } + + addr = r_offset; + value = elf_uval(elf, sym, st_value); + value += r_addend; + + if (elf->log && elf->verbose > 1) + { + uint64_t st_name = elf_uval(elf, sym, st_name); + const char *name = st_name ? elf->sym_strtab + st_name : "*NONE*"; + + elf_msg(elf, + "%s: type %s [%d], off 0x%" PRIx64 ", add 0x%" PRIx64 "," + " sym %s [0x%" PRIx64 "], sec %s [0x%" PRIx64 "]" + " -> addr 0x%" PRIx64 " value 0x%" PRIx64 "\n", + __FUNCTION__, rela_name(machine, r_type), r_type, r_offset, + r_addend, name, elf_uval(elf, sym, st_value), sname, sbase, + addr, value); + } + + if (-1 == relocs[machine].func(elf, r_type, addr, value)) + { + elf_err(elf, "%s: unknown/unsupported reloc type %s [%d]\n", + __FUNCTION__, rela_name(machine, r_type), r_type); + return -1; + } + } + return 0; +} + +int elf_reloc(struct elf_binary *elf) +{ + const elf_shdr *rels, *sect, *syms; + uint64_t i, count, type; + + count = elf_shdr_count(elf); + for (i = 0; i < count; i++) + { + rels = elf_shdr_by_index(elf, i); + type = elf_uval(elf, rels, sh_type); + if (type != SHT_REL && type != SHT_RELA) + continue; + + sect = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_info)); + syms = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_link)); + if (NULL == sect || NULL == syms) + continue; + + if (!(elf_uval(elf, sect, sh_flags) & SHF_ALLOC)) + { + elf_msg(elf, "%s: relocations for %s, skipping\n", + __FUNCTION__, elf_section_name(elf, sect)); + continue; + } + + elf_msg(elf, "%s: relocations for %s @ 0x%" PRIx64 "\n", + __FUNCTION__, elf_section_name(elf, sect), + elf_uval(elf, sect, sh_addr)); + if (0 != elf_reloc_section(elf, rels, sect, syms)) + return -1; + } + return 0; +} Index: build-32-unstable-12663/xen/common/libelf/libelf-tools.c ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/common/libelf/libelf-tools.c @@ -0,0 +1,225 @@ +/* + * various helper functions to access elf structures + */ + +#include "libelf-private.h" + +/* ------------------------------------------------------------------------ */ + +uint64_t elf_access_unsigned(struct elf_binary * elf, const void *ptr, + off_t offset, size_t size) +{ + int need_swap = elf_swap(elf); + const uint8_t *u8; + const uint16_t *u16; + const uint32_t *u32; + const uint64_t *u64; + + switch (size) + { + case 1: + u8 = ptr + offset; + return *u8; + case 2: + u16 = ptr + offset; + return need_swap ? bswap_16(*u16) : *u16; + case 4: + u32 = ptr + offset; + return need_swap ? bswap_32(*u32) : *u32; + case 8: + u64 = ptr + offset; + return need_swap ? bswap_64(*u64) : *u64; + default: + return 0; + } +} + +int64_t elf_access_signed(struct elf_binary *elf, const void *ptr, + off_t offset, size_t size) +{ + int need_swap = elf_swap(elf); + const int8_t *s8; + const int16_t *s16; + const int32_t *s32; + const int64_t *s64; + + switch (size) + { + case 1: + s8 = ptr + offset; + return *s8; + case 2: + s16 = ptr + offset; + return need_swap ? bswap_16(*s16) : *s16; + case 4: + s32 = ptr + offset; + return need_swap ? bswap_32(*s32) : *s32; + case 8: + s64 = ptr + offset; + return need_swap ? bswap_64(*s64) : *s64; + default: + return 0; + } +} + +uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr) +{ + int elf_round = (elf_64bit(elf) ? 8 : 4) - 1; + + return (addr + elf_round) & ~elf_round; +} + +/* ------------------------------------------------------------------------ */ + +int elf_shdr_count(struct elf_binary *elf) +{ + return elf_uval(elf, elf->ehdr, e_shnum); +} + +int elf_phdr_count(struct elf_binary *elf) +{ + return elf_uval(elf, elf->ehdr, e_phnum); +} + +const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name) +{ + uint64_t count = elf_shdr_count(elf); + const elf_shdr *shdr; + const char *sname; + int i; + + for (i = 0; i < count; i++) { + shdr = elf_shdr_by_index(elf, i); + sname = elf_section_name(elf, shdr); + if (sname && 0 == strcmp(sname, name)) + return shdr; + } + return NULL; +} + +const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index) +{ + uint64_t count = elf_shdr_count(elf); + const void *ptr = NULL; + + if (index < count) + ptr = elf->image + + elf_uval(elf, elf->ehdr, e_shoff) + + elf_uval(elf, elf->ehdr, e_shentsize) * index; + return ptr; +} + +const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index) +{ + uint64_t count = elf_uval(elf, elf->ehdr, e_phnum); + const void *ptr = NULL; + + if (index < count) + ptr = elf->image + + elf_uval(elf, elf->ehdr, e_phoff) + + elf_uval(elf, elf->ehdr, e_phentsize) * index; + return ptr; +} + +const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr) +{ + if (NULL == elf->sec_strtab) + return "unknown"; + return elf->sec_strtab + elf_uval(elf, shdr, sh_name); +} + +const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr) +{ + return elf->image + elf_uval(elf, shdr, sh_offset); +} + +const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr) +{ + return elf->image + + elf_uval(elf, shdr, sh_offset) + elf_uval(elf, shdr, sh_size); +} + +const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol) +{ + const void *ptr = elf_section_start(elf, elf->sym_tab); + const void *end = elf_section_end(elf, elf->sym_tab); + const elf_sym *sym; + uint64_t info, name; + + for (; ptr < end; ptr += elf_size(elf, sym)) + { + sym = ptr; + info = elf_uval(elf, sym, st_info); + name = elf_uval(elf, sym, st_name); + if (ELF32_ST_BIND(info) != STB_GLOBAL) + continue; + if (strcmp(elf->sym_strtab + name, symbol) != 0) + continue; + return sym; + } + return NULL; +} + +const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index) +{ + const void *ptr = elf_section_start(elf, elf->sym_tab); + const elf_sym *sym; + + sym = ptr + index * elf_size(elf, sym); + return sym; +} + +const char *elf_note_name(struct elf_binary *elf, const elf_note * note) +{ + return (void *)note + elf_size(elf, note); +} + +const void *elf_note_desc(struct elf_binary *elf, const elf_note * note) +{ + int namesz = (elf_uval(elf, note, namesz) + 3) & ~3; + + return (void *)note + elf_size(elf, note) + namesz; +} + +uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note) +{ + const void *desc = elf_note_desc(elf, note); + int descsz = elf_uval(elf, note, descsz); + + switch (descsz) + { + case 1: + case 2: + case 4: + case 8: + return elf_access_unsigned(elf, desc, 0, descsz); + default: + return 0; + } +} +const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note) +{ + int namesz = (elf_uval(elf, note, namesz) + 3) & ~3; + int descsz = (elf_uval(elf, note, descsz) + 3) & ~3; + + return (void *)note + elf_size(elf, note) + namesz + descsz; +} + +/* ------------------------------------------------------------------------ */ + +int elf_is_elfbinary(const void *image) +{ + const Elf32_Ehdr *ehdr = image; + + if (IS_ELF(*ehdr)) + return 1; + return 0; +} + +int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr) +{ + uint64_t p_type = elf_uval(elf, phdr, p_type); + uint64_t p_flags = elf_uval(elf, phdr, p_flags); + + return ((p_type == PT_LOAD) && (p_flags & (PF_W | PF_X)) != 0); +} Index: build-32-unstable-12663/xen/include/public/elfstructs.h ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/include/public/elfstructs.h @@ -0,0 +1,527 @@ +#ifndef __XEN_PUBLIC_ELFSTRUCTS_H__ +#define __XEN_PUBLIC_ELFSTRUCTS_H__ 1 +/* + * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'''' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +typedef uint8_t Elf_Byte; + +typedef uint32_t Elf32_Addr; /* Unsigned program address */ +typedef uint32_t Elf32_Off; /* Unsigned file offset */ +typedef int32_t Elf32_Sword; /* Signed large integer */ +typedef uint32_t Elf32_Word; /* Unsigned large integer */ +typedef uint16_t Elf32_Half; /* Unsigned medium integer */ + +typedef uint64_t Elf64_Addr; +typedef uint64_t Elf64_Off; +typedef int32_t Elf64_Shalf; + +typedef int32_t Elf64_Sword; +typedef uint32_t Elf64_Word; + +typedef int64_t Elf64_Sxword; +typedef uint64_t Elf64_Xword; + +typedef uint32_t Elf64_Half; +typedef uint16_t Elf64_Quarter; + +/* + * e_ident[] identification indexes + * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html + */ +#define EI_MAG0 0 /* file ID */ +#define EI_MAG1 1 /* file ID */ +#define EI_MAG2 2 /* file ID */ +#define EI_MAG3 3 /* file ID */ +#define EI_CLASS 4 /* file class */ +#define EI_DATA 5 /* data encoding */ +#define EI_VERSION 6 /* ELF header version */ +#define EI_OSABI 7 /* OS/ABI ID */ +#define EI_ABIVERSION 8 /* ABI version */ +#define EI_PAD 9 /* start of pad bytes */ +#define EI_NIDENT 16 /* Size of e_ident[] */ + +/* e_ident[] magic number */ +#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ +#define ELFMAG1 ''E'' /* e_ident[EI_MAG1] */ +#define ELFMAG2 ''L'' /* e_ident[EI_MAG2] */ +#define ELFMAG3 ''F'' /* e_ident[EI_MAG3] */ +#define ELFMAG "\177ELF" /* magic */ +#define SELFMAG 4 /* size of magic */ + +/* e_ident[] file class */ +#define ELFCLASSNONE 0 /* invalid */ +#define ELFCLASS32 1 /* 32-bit objs */ +#define ELFCLASS64 2 /* 64-bit objs */ +#define ELFCLASSNUM 3 /* number of classes */ + +/* e_ident[] data encoding */ +#define ELFDATANONE 0 /* invalid */ +#define ELFDATA2LSB 1 /* Little-Endian */ +#define ELFDATA2MSB 2 /* Big-Endian */ +#define ELFDATANUM 3 /* number of data encode defines */ + +/* e_ident[] Operating System/ABI */ +#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ +#define ELFOSABI_HPUX 1 /* HP-UX operating system */ +#define ELFOSABI_NETBSD 2 /* NetBSD */ +#define ELFOSABI_LINUX 3 /* GNU/Linux */ +#define ELFOSABI_HURD 4 /* GNU/Hurd */ +#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ +#define ELFOSABI_SOLARIS 6 /* Solaris */ +#define ELFOSABI_MONTEREY 7 /* Monterey */ +#define ELFOSABI_IRIX 8 /* IRIX */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD */ +#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD */ +#define ELFOSABI_ARM 97 /* ARM */ +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ + +/* e_ident */ +#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ + (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ + (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ + (ehdr).e_ident[EI_MAG3] == ELFMAG3) + +/* ELF Header */ +typedef struct elfhdr { + unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ + Elf32_Half e_type; /* object file type */ + Elf32_Half e_machine; /* machine */ + Elf32_Word e_version; /* object file version */ + Elf32_Addr e_entry; /* virtual entry point */ + Elf32_Off e_phoff; /* program header table offset */ + Elf32_Off e_shoff; /* section header table offset */ + Elf32_Word e_flags; /* processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size */ + Elf32_Half e_phentsize; /* program header entry size */ + Elf32_Half e_phnum; /* number of program header entries */ + Elf32_Half e_shentsize; /* section header entry size */ + Elf32_Half e_shnum; /* number of section header entries */ + Elf32_Half e_shstrndx; /* section header table''s "section + header string table" entry offset */ +} Elf32_Ehdr; + +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* Id bytes */ + Elf64_Quarter e_type; /* file type */ + Elf64_Quarter e_machine; /* machine type */ + Elf64_Half e_version; /* version number */ + Elf64_Addr e_entry; /* entry point */ + Elf64_Off e_phoff; /* Program hdr offset */ + Elf64_Off e_shoff; /* Section hdr offset */ + Elf64_Half e_flags; /* Processor flags */ + Elf64_Quarter e_ehsize; /* sizeof ehdr */ + Elf64_Quarter e_phentsize; /* Program header entry size */ + Elf64_Quarter e_phnum; /* Number of program headers */ + Elf64_Quarter e_shentsize; /* Section header entry size */ + Elf64_Quarter e_shnum; /* Number of section headers */ + Elf64_Quarter e_shstrndx; /* String table index */ +} Elf64_Ehdr; + +/* e_type */ +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* relocatable file */ +#define ET_EXEC 2 /* executable file */ +#define ET_DYN 3 /* shared object file */ +#define ET_CORE 4 /* core file */ +#define ET_NUM 5 /* number of types */ +#define ET_LOPROC 0xff00 /* reserved range for processor */ +#define ET_HIPROC 0xffff /* specific e_type */ + +/* e_machine */ +#define EM_NONE 0 /* No Machine */ +#define EM_M32 1 /* AT&T WE 32100 */ +#define EM_SPARC 2 /* SPARC */ +#define EM_386 3 /* Intel 80386 */ +#define EM_68K 4 /* Motorola 68000 */ +#define EM_88K 5 /* Motorola 88000 */ +#define EM_486 6 /* Intel 80486 - unused? */ +#define EM_860 7 /* Intel 80860 */ +#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ +/* + * Don''t know if EM_MIPS_RS4_BE, + * EM_SPARC64, EM_PARISC, + * or EM_PPC are ABI compliant + */ +#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ +#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ +#define EM_PARISC 15 /* HPPA */ +#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ +#define EM_PPC 20 /* PowerPC */ +#define EM_PPC64 21 /* PowerPC 64-bit */ +#define EM_ARM 40 /* Advanced RISC Machines ARM */ +#define EM_ALPHA 41 /* DEC ALPHA */ +#define EM_SPARCV9 43 /* SPARC version 9 */ +#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ +#define EM_IA_64 50 /* Intel Merced */ +#define EM_X86_64 62 /* AMD x86-64 architecture */ +#define EM_VAX 75 /* DEC VAX */ + +/* Version */ +#define EV_NONE 0 /* Invalid */ +#define EV_CURRENT 1 /* Current */ +#define EV_NUM 2 /* number of versions */ + +/* Section Header */ +typedef struct { + Elf32_Word sh_name; /* name - index into section header + string table section */ + Elf32_Word sh_type; /* type */ + Elf32_Word sh_flags; /* flags */ + Elf32_Addr sh_addr; /* address */ + Elf32_Off sh_offset; /* file offset */ + Elf32_Word sh_size; /* section size */ + Elf32_Word sh_link; /* section header table index link */ + Elf32_Word sh_info; /* extra information */ + Elf32_Word sh_addralign; /* address alignment */ + Elf32_Word sh_entsize; /* section entry size */ +} Elf32_Shdr; + +typedef struct { + Elf64_Half sh_name; /* section name */ + Elf64_Half sh_type; /* section type */ + Elf64_Xword sh_flags; /* section flags */ + Elf64_Addr sh_addr; /* virtual address */ + Elf64_Off sh_offset; /* file offset */ + Elf64_Xword sh_size; /* section size */ + Elf64_Half sh_link; /* link to another */ + Elf64_Half sh_info; /* misc info */ + Elf64_Xword sh_addralign; /* memory alignment */ + Elf64_Xword sh_entsize; /* table entry size */ +} Elf64_Shdr; + +/* Special Section Indexes */ +#define SHN_UNDEF 0 /* undefined */ +#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ +#define SHN_LOPROC 0xff00 /* reserved range for processor */ +#define SHN_HIPROC 0xff1f /* specific section indexes */ +#define SHN_ABS 0xfff1 /* absolute value */ +#define SHN_COMMON 0xfff2 /* common symbol */ +#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ + +/* sh_type */ +#define SHT_NULL 0 /* inactive */ +#define SHT_PROGBITS 1 /* program defined information */ +#define SHT_SYMTAB 2 /* symbol table section */ +#define SHT_STRTAB 3 /* string table section */ +#define SHT_RELA 4 /* relocation section with addends*/ +#define SHT_HASH 5 /* symbol hash table section */ +#define SHT_DYNAMIC 6 /* dynamic section */ +#define SHT_NOTE 7 /* note section */ +#define SHT_NOBITS 8 /* no space section */ +#define SHT_REL 9 /* relation section without addends */ +#define SHT_SHLIB 10 /* reserved - purpose unknown */ +#define SHT_DYNSYM 11 /* dynamic symbol table section */ +#define SHT_NUM 12 /* number of section types */ +#define SHT_LOPROC 0x70000000 /* reserved range for processor */ +#define SHT_HIPROC 0x7fffffff /* specific section header types */ +#define SHT_LOUSER 0x80000000 /* reserved range for application */ +#define SHT_HIUSER 0xffffffff /* specific indexes */ + +/* Section names */ +#define ELF_BSS ".bss" /* uninitialized data */ +#define ELF_DATA ".data" /* initialized data */ +#define ELF_DEBUG ".debug" /* debug */ +#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ +#define ELF_DYNSTR ".dynstr" /* dynamic string table */ +#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ +#define ELF_FINI ".fini" /* termination code */ +#define ELF_GOT ".got" /* global offset table */ +#define ELF_HASH ".hash" /* symbol hash table */ +#define ELF_INIT ".init" /* initialization code */ +#define ELF_REL_DATA ".rel.data" /* relocation data */ +#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ +#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ +#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ +#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ +#define ELF_REL_TEXT ".rel.text" /* relocation code */ +#define ELF_RODATA ".rodata" /* read-only data */ +#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ +#define ELF_STRTAB ".strtab" /* string table */ +#define ELF_SYMTAB ".symtab" /* symbol table */ +#define ELF_TEXT ".text" /* code */ + + +/* Section Attribute Flags - sh_flags */ +#define SHF_WRITE 0x1 /* Writable */ +#define SHF_ALLOC 0x2 /* occupies memory */ +#define SHF_EXECINSTR 0x4 /* executable */ +#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ + /* specific section attributes */ + +/* Symbol Table Entry */ +typedef struct elf32_sym { + Elf32_Word st_name; /* name - index into string table */ + Elf32_Addr st_value; /* symbol value */ + Elf32_Word st_size; /* symbol size */ + unsigned char st_info; /* type and binding */ + unsigned char st_other; /* 0 - no defined meaning */ + Elf32_Half st_shndx; /* section header index */ +} Elf32_Sym; + +typedef struct { + Elf64_Half st_name; /* Symbol name index in str table */ + Elf_Byte st_info; /* type / binding attrs */ + Elf_Byte st_other; /* unused */ + Elf64_Quarter st_shndx; /* section index of symbol */ + Elf64_Xword st_value; /* value of symbol */ + Elf64_Xword st_size; /* size of symbol */ +} Elf64_Sym; + +/* Symbol table index */ +#define STN_UNDEF 0 /* undefined */ + +/* Extract symbol info - st_info */ +#define ELF32_ST_BIND(x) ((x) >> 4) +#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) +#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) + +#define ELF64_ST_BIND(x) ((x) >> 4) +#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) +#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) + +/* Symbol Binding - ELF32_ST_BIND - st_info */ +#define STB_LOCAL 0 /* Local symbol */ +#define STB_GLOBAL 1 /* Global symbol */ +#define STB_WEAK 2 /* like global - lower precedence */ +#define STB_NUM 3 /* number of symbol bindings */ +#define STB_LOPROC 13 /* reserved range for processor */ +#define STB_HIPROC 15 /* specific symbol bindings */ + +/* Symbol type - ELF32_ST_TYPE - st_info */ +#define STT_NOTYPE 0 /* not specified */ +#define STT_OBJECT 1 /* data object */ +#define STT_FUNC 2 /* function */ +#define STT_SECTION 3 /* section */ +#define STT_FILE 4 /* file */ +#define STT_NUM 5 /* number of symbol types */ +#define STT_LOPROC 13 /* reserved range for processor */ +#define STT_HIPROC 15 /* specific symbol types */ + +/* Relocation entry with implicit addend */ +typedef struct { + Elf32_Addr r_offset; /* offset of relocation */ + Elf32_Word r_info; /* symbol table index and type */ +} Elf32_Rel; + +/* Relocation entry with explicit addend */ +typedef struct { + Elf32_Addr r_offset; /* offset of relocation */ + Elf32_Word r_info; /* symbol table index and type */ + Elf32_Sword r_addend; +} Elf32_Rela; + +/* Extract relocation info - r_info */ +#define ELF32_R_SYM(i) ((i) >> 8) +#define ELF32_R_TYPE(i) ((unsigned char) (i)) +#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) + +typedef struct { + Elf64_Xword r_offset; /* where to do it */ + Elf64_Xword r_info; /* index & type of relocation */ +} Elf64_Rel; + +typedef struct { + Elf64_Xword r_offset; /* where to do it */ + Elf64_Xword r_info; /* index & type of relocation */ + Elf64_Sxword r_addend; /* adjustment value */ +} Elf64_Rela; + +#define ELF64_R_SYM(info) ((info) >> 32) +#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF) +#define ELF64_R_INFO(s,t) (((s) << 32) + (u_int32_t)(t)) + +/* Program Header */ +typedef struct { + Elf32_Word p_type; /* segment type */ + Elf32_Off p_offset; /* segment offset */ + Elf32_Addr p_vaddr; /* virtual address of segment */ + Elf32_Addr p_paddr; /* physical address - ignored? */ + Elf32_Word p_filesz; /* number of bytes in file for seg. */ + Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ + Elf32_Word p_flags; /* flags */ + Elf32_Word p_align; /* memory alignment */ +} Elf32_Phdr; + +typedef struct { + Elf64_Half p_type; /* entry type */ + Elf64_Half p_flags; /* flags */ + Elf64_Off p_offset; /* offset */ + Elf64_Addr p_vaddr; /* virtual address */ + Elf64_Addr p_paddr; /* physical address */ + Elf64_Xword p_filesz; /* file size */ + Elf64_Xword p_memsz; /* memory size */ + Elf64_Xword p_align; /* memory & file alignment */ +} Elf64_Phdr; + +/* Segment types - p_type */ +#define PT_NULL 0 /* unused */ +#define PT_LOAD 1 /* loadable segment */ +#define PT_DYNAMIC 2 /* dynamic linking section */ +#define PT_INTERP 3 /* the RTLD */ +#define PT_NOTE 4 /* auxiliary information */ +#define PT_SHLIB 5 /* reserved - purpose undefined */ +#define PT_PHDR 6 /* program header */ +#define PT_NUM 7 /* Number of segment types */ +#define PT_LOPROC 0x70000000 /* reserved range for processor */ +#define PT_HIPROC 0x7fffffff /* specific segment types */ + +/* Segment flags - p_flags */ +#define PF_X 0x1 /* Executable */ +#define PF_W 0x2 /* Writable */ +#define PF_R 0x4 /* Readable */ +#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ + /* specific segment flags */ + +/* Dynamic structure */ +typedef struct { + Elf32_Sword d_tag; /* controls meaning of d_val */ + union { + Elf32_Word d_val; /* Multiple meanings - see d_tag */ + Elf32_Addr d_ptr; /* program virtual address */ + } d_un; +} Elf32_Dyn; + +typedef struct { + Elf64_Xword d_tag; /* controls meaning of d_val */ + union { + Elf64_Addr d_ptr; + Elf64_Xword d_val; + } d_un; +} Elf64_Dyn; + +/* Dynamic Array Tags - d_tag */ +#define DT_NULL 0 /* marks end of _DYNAMIC array */ +#define DT_NEEDED 1 /* string table offset of needed lib */ +#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ +#define DT_PLTGOT 3 /* address PLT/GOT */ +#define DT_HASH 4 /* address of symbol hash table */ +#define DT_STRTAB 5 /* address of string table */ +#define DT_SYMTAB 6 /* address of symbol table */ +#define DT_RELA 7 /* address of relocation table */ +#define DT_RELASZ 8 /* size of relocation table */ +#define DT_RELAENT 9 /* size of relocation entry */ +#define DT_STRSZ 10 /* size of string table */ +#define DT_SYMENT 11 /* size of symbol table entry */ +#define DT_INIT 12 /* address of initialization func. */ +#define DT_FINI 13 /* address of termination function */ +#define DT_SONAME 14 /* string table offset of shared obj */ +#define DT_RPATH 15 /* string table offset of library + search path */ +#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ +#define DT_REL 17 /* address of rel. tbl. w addends */ +#define DT_RELSZ 18 /* size of DT_REL relocation table */ +#define DT_RELENT 19 /* size of DT_REL relocation entry */ +#define DT_PLTREL 20 /* PLT referenced relocation entry */ +#define DT_DEBUG 21 /* bugger */ +#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ +#define DT_JMPREL 23 /* add. of PLT''s relocation entries */ +#define DT_BIND_NOW 24 /* Bind now regardless of env setting */ +#define DT_NUM 25 /* Number used. */ +#define DT_LOPROC 0x70000000 /* reserved range for processor */ +#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ + +/* Standard ELF hashing function */ +unsigned int elf_hash(const unsigned char *name); + +/* + * Note Definitions + */ +typedef struct { + Elf32_Word namesz; + Elf32_Word descsz; + Elf32_Word type; +} Elf32_Note; + +typedef struct { + Elf64_Half namesz; + Elf64_Half descsz; + Elf64_Half type; +} Elf64_Note; + + +#if defined(ELFSIZE) +#define CONCAT(x,y) __CONCAT(x,y) +#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) +#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) +#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) +#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) +#endif + +#if defined(ELFSIZE) && (ELFSIZE == 32) +#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr +#define Elf_Shdr Elf32_Shdr +#define Elf_Sym Elf32_Sym +#define Elf_Rel Elf32_Rel +#define Elf_RelA Elf32_Rela +#define Elf_Dyn Elf32_Dyn +#define Elf_Word Elf32_Word +#define Elf_Sword Elf32_Sword +#define Elf_Addr Elf32_Addr +#define Elf_Off Elf32_Off +#define Elf_Nhdr Elf32_Nhdr +#define Elf_Note Elf32_Note + +#define ELF_R_SYM ELF32_R_SYM +#define ELF_R_TYPE ELF32_R_TYPE +#define ELF_R_INFO ELF32_R_INFO +#define ELFCLASS ELFCLASS32 + +#define ELF_ST_BIND ELF32_ST_BIND +#define ELF_ST_TYPE ELF32_ST_TYPE +#define ELF_ST_INFO ELF32_ST_INFO + +#define AuxInfo Aux32Info +#elif defined(ELFSIZE) && (ELFSIZE == 64) +#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr +#define Elf_Shdr Elf64_Shdr +#define Elf_Sym Elf64_Sym +#define Elf_Rel Elf64_Rel +#define Elf_RelA Elf64_Rela +#define Elf_Dyn Elf64_Dyn +#define Elf_Word Elf64_Word +#define Elf_Sword Elf64_Sword +#define Elf_Addr Elf64_Addr +#define Elf_Off Elf64_Off +#define Elf_Nhdr Elf64_Nhdr +#define Elf_Note Elf64_Note + +#define ELF_R_SYM ELF64_R_SYM +#define ELF_R_TYPE ELF64_R_TYPE +#define ELF_R_INFO ELF64_R_INFO +#define ELFCLASS ELFCLASS64 + +#define ELF_ST_BIND ELF64_ST_BIND +#define ELF_ST_TYPE ELF64_ST_TYPE +#define ELF_ST_INFO ELF64_ST_INFO + +#define AuxInfo Aux64Info +#endif + +#endif /* __XEN_PUBLIC_ELFSTRUCTS_H__ */ Index: build-32-unstable-12663/xen/include/public/libelf.h ==================================================================--- /dev/null +++ build-32-unstable-12663/xen/include/public/libelf.h @@ -0,0 +1,227 @@ +#ifndef __XC_LIBELF__ +#define __XC_LIBELF__ 1 + +#ifdef __XEN__ + +#include <xen/inttypes.h> +#include <public/features.h> +typedef uint64_t off_t; + +#else /* !__XEN__ */ + +#include <stddef.h> +#include <inttypes.h> + +#endif + +#undef ELFSIZE +#include "elfstructs.h" +#include "features.h" + +/* ------------------------------------------------------------------------ */ + +typedef union { + Elf32_Ehdr e32; + Elf64_Ehdr e64; +} elf_ehdr; + +typedef union { + Elf32_Phdr e32; + Elf64_Phdr e64; +} elf_phdr; + +typedef union { + Elf32_Shdr e32; + Elf64_Shdr e64; +} elf_shdr; + +typedef union { + Elf32_Sym e32; + Elf64_Sym e64; +} elf_sym; + +typedef union { + Elf32_Rel e32; + Elf64_Rel e64; +} elf_rel; + +typedef union { + Elf32_Rela e32; + Elf64_Rela e64; +} elf_rela; + +typedef union { + Elf32_Note e32; + Elf64_Note e64; +} elf_note; + +struct elf_binary { + /* elf binary */ + const char *image; + size_t size; + char class; + char data; + + const elf_ehdr *ehdr; + const char *sec_strtab; + const elf_shdr *sym_tab; + const char *sym_strtab; + + /* loaded to */ + char *dest; + uint64_t pstart; + uint64_t pend; + uint64_t reloc_offset; + +#ifndef __XEN__ + /* misc */ + FILE *log; +#endif + int verbose; +}; + +/* ------------------------------------------------------------------------ */ +/* accessing elf header fields */ + +#if BYTE_ORDER == BIG_ENDIAN +# define NATIVE_ELFDATA ELFDATA2MSB +#elif BYTE_ORDER == LITTLE_ENDIAN +# define NATIVE_ELFDATA ELFDATA2LSB +#else +# error BYTE_ORDER unknown or not defined +#endif + +#define elf_64bit(elf) (ELFCLASS64 == (elf)->class) +#define elf_msb(elf) (ELFDATA2MSB == (elf)->data) +#define elf_lsb(elf) (ELFDATA2LSB == (elf)->data) +#define elf_swap(elf) (NATIVE_ELFDATA != (elf)->data) + +#define elf_uval(elf, str, elem) \ + ((ELFCLASS64 == (elf)->class) \ + ? elf_access_unsigned((elf), (str), \ + offsetof(typeof(*(str)),e64.elem), \ + sizeof((str)->e64.elem)) \ + : elf_access_unsigned((elf), (str), \ + offsetof(typeof(*(str)),e32.elem), \ + sizeof((str)->e32.elem))) + +#define elf_sval(elf, str, elem) \ + ((ELFCLASS64 == (elf)->class) \ + ? elf_access_signed((elf), (str), \ + offsetof(typeof(*(str)),e64.elem), \ + sizeof((str)->e64.elem)) \ + : elf_access_signed((elf), (str), \ + offsetof(typeof(*(str)),e32.elem), \ + sizeof((str)->e32.elem))) + +#define elf_size(elf, str) \ + ((ELFCLASS64 == (elf)->class) \ + ? sizeof((str)->e64) \ + : sizeof((str)->e32)) + +uint64_t elf_access_unsigned(struct elf_binary *elf, const void *ptr, + off_t offset, size_t size); +int64_t elf_access_signed(struct elf_binary *elf, const void *ptr, + off_t offset, size_t size); + +uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr); + +/* ------------------------------------------------------------------------ */ +/* xc_libelf_tools.c */ + +int elf_shdr_count(struct elf_binary *elf); +int elf_phdr_count(struct elf_binary *elf); + +const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name); +const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index); +const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index); + +const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr); +const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr); +const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr); + +const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol); +const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index); + +const char *elf_note_name(struct elf_binary *elf, const elf_note * note); +const void *elf_note_desc(struct elf_binary *elf, const elf_note * note); +uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note); +const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note); + +int elf_is_elfbinary(const void *image); +int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr); + +/* ------------------------------------------------------------------------ */ +/* xc_libelf_loader.c */ + +int elf_init(struct elf_binary *elf, const char *image, size_t size); +#ifdef __XEN__ +void elf_set_verbose(struct elf_binary *elf); +#else +void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose); +#endif + +void elf_parse_binary(struct elf_binary *elf); +void elf_load_binary(struct elf_binary *elf); + +void *elf_get_ptr(struct elf_binary *elf, unsigned long addr); +uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol); + +/* ------------------------------------------------------------------------ */ +/* xc_libelf_relocate.c */ + +int elf_reloc(struct elf_binary *elf); + +/* ------------------------------------------------------------------------ */ +/* xc_libelf_dominfo.c */ + +#define UNSET_ADDR ((uint64_t)-1) + +struct elf_dom_parms { + /* raw */ + const char *guest_info; + const void *elf_note_start; + const void *elf_note_end; + + /* parsed */ + char guest_os[16]; + char guest_ver[16]; + char xen_ver[16]; + char loader[16]; + int pae; + int bsd_symtab; + uint64_t virt_base; + uint64_t virt_entry; + uint64_t virt_hypercall; + uint64_t virt_hv_start_low; + uint64_t elf_paddr_offset; + uint32_t f_supported[XENFEAT_NR_SUBMAPS]; + uint32_t f_required[XENFEAT_NR_SUBMAPS]; + + /* calculated */ + uint64_t virt_offset; + uint64_t virt_kstart; + uint64_t virt_kend; +}; + +static inline void elf_xen_feature_set(int nr, uint32_t * addr) +{ + addr[nr >> 5] |= 1 << (nr & 31); +} +static inline int elf_xen_feature_get(int nr, uint32_t * addr) +{ + return !!(addr[nr >> 5] & (1 << (nr & 31))); +} + +int elf_xen_parse_features(const char *features, + uint32_t *supported, + uint32_t *required); +int elf_xen_parse_note(struct elf_binary *elf, + struct elf_dom_parms *parms, + const elf_note *note); +int elf_xen_parse_guest_info(struct elf_binary *elf, + struct elf_dom_parms *parms); +int elf_xen_parse(struct elf_binary *elf, + struct elf_dom_parms *parms); + +#endif /* __XC_LIBELF__ */ Index: build-32-unstable-12663/tools/libxc/xc_elf.h ==================================================================--- build-32-unstable-12663.orig/tools/libxc/xc_elf.h +++ build-32-unstable-12663/tools/libxc/xc_elf.h @@ -1,524 +1 @@ -/* - * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'''' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -typedef uint8_t Elf_Byte; - -typedef uint32_t Elf32_Addr; /* Unsigned program address */ -typedef uint32_t Elf32_Off; /* Unsigned file offset */ -typedef int32_t Elf32_Sword; /* Signed large integer */ -typedef uint32_t Elf32_Word; /* Unsigned large integer */ -typedef uint16_t Elf32_Half; /* Unsigned medium integer */ - -typedef uint64_t Elf64_Addr; -typedef uint64_t Elf64_Off; -typedef int32_t Elf64_Shalf; - -typedef int32_t Elf64_Sword; -typedef uint32_t Elf64_Word; - -typedef int64_t Elf64_Sxword; -typedef uint64_t Elf64_Xword; - -typedef uint32_t Elf64_Half; -typedef uint16_t Elf64_Quarter; - -/* - * e_ident[] identification indexes - * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html - */ -#define EI_MAG0 0 /* file ID */ -#define EI_MAG1 1 /* file ID */ -#define EI_MAG2 2 /* file ID */ -#define EI_MAG3 3 /* file ID */ -#define EI_CLASS 4 /* file class */ -#define EI_DATA 5 /* data encoding */ -#define EI_VERSION 6 /* ELF header version */ -#define EI_OSABI 7 /* OS/ABI ID */ -#define EI_ABIVERSION 8 /* ABI version */ -#define EI_PAD 9 /* start of pad bytes */ -#define EI_NIDENT 16 /* Size of e_ident[] */ - -/* e_ident[] magic number */ -#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ -#define ELFMAG1 ''E'' /* e_ident[EI_MAG1] */ -#define ELFMAG2 ''L'' /* e_ident[EI_MAG2] */ -#define ELFMAG3 ''F'' /* e_ident[EI_MAG3] */ -#define ELFMAG "\177ELF" /* magic */ -#define SELFMAG 4 /* size of magic */ - -/* e_ident[] file class */ -#define ELFCLASSNONE 0 /* invalid */ -#define ELFCLASS32 1 /* 32-bit objs */ -#define ELFCLASS64 2 /* 64-bit objs */ -#define ELFCLASSNUM 3 /* number of classes */ - -/* e_ident[] data encoding */ -#define ELFDATANONE 0 /* invalid */ -#define ELFDATA2LSB 1 /* Little-Endian */ -#define ELFDATA2MSB 2 /* Big-Endian */ -#define ELFDATANUM 3 /* number of data encode defines */ - -/* e_ident[] Operating System/ABI */ -#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ -#define ELFOSABI_HPUX 1 /* HP-UX operating system */ -#define ELFOSABI_NETBSD 2 /* NetBSD */ -#define ELFOSABI_LINUX 3 /* GNU/Linux */ -#define ELFOSABI_HURD 4 /* GNU/Hurd */ -#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ -#define ELFOSABI_SOLARIS 6 /* Solaris */ -#define ELFOSABI_MONTEREY 7 /* Monterey */ -#define ELFOSABI_IRIX 8 /* IRIX */ -#define ELFOSABI_FREEBSD 9 /* FreeBSD */ -#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ -#define ELFOSABI_MODESTO 11 /* Novell Modesto */ -#define ELFOSABI_OPENBSD 12 /* OpenBSD */ -#define ELFOSABI_ARM 97 /* ARM */ -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ - -/* e_ident */ -#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ - (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ - (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ - (ehdr).e_ident[EI_MAG3] == ELFMAG3) - -/* ELF Header */ -typedef struct elfhdr { - unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ - Elf32_Half e_type; /* object file type */ - Elf32_Half e_machine; /* machine */ - Elf32_Word e_version; /* object file version */ - Elf32_Addr e_entry; /* virtual entry point */ - Elf32_Off e_phoff; /* program header table offset */ - Elf32_Off e_shoff; /* section header table offset */ - Elf32_Word e_flags; /* processor-specific flags */ - Elf32_Half e_ehsize; /* ELF header size */ - Elf32_Half e_phentsize; /* program header entry size */ - Elf32_Half e_phnum; /* number of program header entries */ - Elf32_Half e_shentsize; /* section header entry size */ - Elf32_Half e_shnum; /* number of section header entries */ - Elf32_Half e_shstrndx; /* section header table''s "section - header string table" entry offset */ -} Elf32_Ehdr; - -typedef struct { - unsigned char e_ident[EI_NIDENT]; /* Id bytes */ - Elf64_Quarter e_type; /* file type */ - Elf64_Quarter e_machine; /* machine type */ - Elf64_Half e_version; /* version number */ - Elf64_Addr e_entry; /* entry point */ - Elf64_Off e_phoff; /* Program hdr offset */ - Elf64_Off e_shoff; /* Section hdr offset */ - Elf64_Half e_flags; /* Processor flags */ - Elf64_Quarter e_ehsize; /* sizeof ehdr */ - Elf64_Quarter e_phentsize; /* Program header entry size */ - Elf64_Quarter e_phnum; /* Number of program headers */ - Elf64_Quarter e_shentsize; /* Section header entry size */ - Elf64_Quarter e_shnum; /* Number of section headers */ - Elf64_Quarter e_shstrndx; /* String table index */ -} Elf64_Ehdr; - -/* e_type */ -#define ET_NONE 0 /* No file type */ -#define ET_REL 1 /* relocatable file */ -#define ET_EXEC 2 /* executable file */ -#define ET_DYN 3 /* shared object file */ -#define ET_CORE 4 /* core file */ -#define ET_NUM 5 /* number of types */ -#define ET_LOPROC 0xff00 /* reserved range for processor */ -#define ET_HIPROC 0xffff /* specific e_type */ - -/* e_machine */ -#define EM_NONE 0 /* No Machine */ -#define EM_M32 1 /* AT&T WE 32100 */ -#define EM_SPARC 2 /* SPARC */ -#define EM_386 3 /* Intel 80386 */ -#define EM_68K 4 /* Motorola 68000 */ -#define EM_88K 5 /* Motorola 88000 */ -#define EM_486 6 /* Intel 80486 - unused? */ -#define EM_860 7 /* Intel 80860 */ -#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ -/* - * Don''t know if EM_MIPS_RS4_BE, - * EM_SPARC64, EM_PARISC, - * or EM_PPC are ABI compliant - */ -#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ -#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ -#define EM_PARISC 15 /* HPPA */ -#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ -#define EM_PPC 20 /* PowerPC */ -#define EM_PPC64 21 /* PowerPC 64-bit */ -#define EM_ARM 40 /* Advanced RISC Machines ARM */ -#define EM_ALPHA 41 /* DEC ALPHA */ -#define EM_SPARCV9 43 /* SPARC version 9 */ -#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ -#define EM_IA_64 50 /* Intel Merced */ -#define EM_X86_64 62 /* AMD x86-64 architecture */ -#define EM_VAX 75 /* DEC VAX */ - -/* Version */ -#define EV_NONE 0 /* Invalid */ -#define EV_CURRENT 1 /* Current */ -#define EV_NUM 2 /* number of versions */ - -/* Section Header */ -typedef struct { - Elf32_Word sh_name; /* name - index into section header - string table section */ - Elf32_Word sh_type; /* type */ - Elf32_Word sh_flags; /* flags */ - Elf32_Addr sh_addr; /* address */ - Elf32_Off sh_offset; /* file offset */ - Elf32_Word sh_size; /* section size */ - Elf32_Word sh_link; /* section header table index link */ - Elf32_Word sh_info; /* extra information */ - Elf32_Word sh_addralign; /* address alignment */ - Elf32_Word sh_entsize; /* section entry size */ -} Elf32_Shdr; - -typedef struct { - Elf64_Half sh_name; /* section name */ - Elf64_Half sh_type; /* section type */ - Elf64_Xword sh_flags; /* section flags */ - Elf64_Addr sh_addr; /* virtual address */ - Elf64_Off sh_offset; /* file offset */ - Elf64_Xword sh_size; /* section size */ - Elf64_Half sh_link; /* link to another */ - Elf64_Half sh_info; /* misc info */ - Elf64_Xword sh_addralign; /* memory alignment */ - Elf64_Xword sh_entsize; /* table entry size */ -} Elf64_Shdr; - -/* Special Section Indexes */ -#define SHN_UNDEF 0 /* undefined */ -#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ -#define SHN_LOPROC 0xff00 /* reserved range for processor */ -#define SHN_HIPROC 0xff1f /* specific section indexes */ -#define SHN_ABS 0xfff1 /* absolute value */ -#define SHN_COMMON 0xfff2 /* common symbol */ -#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ - -/* sh_type */ -#define SHT_NULL 0 /* inactive */ -#define SHT_PROGBITS 1 /* program defined information */ -#define SHT_SYMTAB 2 /* symbol table section */ -#define SHT_STRTAB 3 /* string table section */ -#define SHT_RELA 4 /* relocation section with addends*/ -#define SHT_HASH 5 /* symbol hash table section */ -#define SHT_DYNAMIC 6 /* dynamic section */ -#define SHT_NOTE 7 /* note section */ -#define SHT_NOBITS 8 /* no space section */ -#define SHT_REL 9 /* relation section without addends */ -#define SHT_SHLIB 10 /* reserved - purpose unknown */ -#define SHT_DYNSYM 11 /* dynamic symbol table section */ -#define SHT_NUM 12 /* number of section types */ -#define SHT_LOPROC 0x70000000 /* reserved range for processor */ -#define SHT_HIPROC 0x7fffffff /* specific section header types */ -#define SHT_LOUSER 0x80000000 /* reserved range for application */ -#define SHT_HIUSER 0xffffffff /* specific indexes */ - -/* Section names */ -#define ELF_BSS ".bss" /* uninitialized data */ -#define ELF_DATA ".data" /* initialized data */ -#define ELF_DEBUG ".debug" /* debug */ -#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ -#define ELF_DYNSTR ".dynstr" /* dynamic string table */ -#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ -#define ELF_FINI ".fini" /* termination code */ -#define ELF_GOT ".got" /* global offset table */ -#define ELF_HASH ".hash" /* symbol hash table */ -#define ELF_INIT ".init" /* initialization code */ -#define ELF_REL_DATA ".rel.data" /* relocation data */ -#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ -#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ -#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ -#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ -#define ELF_REL_TEXT ".rel.text" /* relocation code */ -#define ELF_RODATA ".rodata" /* read-only data */ -#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ -#define ELF_STRTAB ".strtab" /* string table */ -#define ELF_SYMTAB ".symtab" /* symbol table */ -#define ELF_TEXT ".text" /* code */ - - -/* Section Attribute Flags - sh_flags */ -#define SHF_WRITE 0x1 /* Writable */ -#define SHF_ALLOC 0x2 /* occupies memory */ -#define SHF_EXECINSTR 0x4 /* executable */ -#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ - /* specific section attributes */ - -/* Symbol Table Entry */ -typedef struct elf32_sym { - Elf32_Word st_name; /* name - index into string table */ - Elf32_Addr st_value; /* symbol value */ - Elf32_Word st_size; /* symbol size */ - unsigned char st_info; /* type and binding */ - unsigned char st_other; /* 0 - no defined meaning */ - Elf32_Half st_shndx; /* section header index */ -} Elf32_Sym; - -typedef struct { - Elf64_Half st_name; /* Symbol name index in str table */ - Elf_Byte st_info; /* type / binding attrs */ - Elf_Byte st_other; /* unused */ - Elf64_Quarter st_shndx; /* section index of symbol */ - Elf64_Xword st_value; /* value of symbol */ - Elf64_Xword st_size; /* size of symbol */ -} Elf64_Sym; - -/* Symbol table index */ -#define STN_UNDEF 0 /* undefined */ - -/* Extract symbol info - st_info */ -#define ELF32_ST_BIND(x) ((x) >> 4) -#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) -#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) - -#define ELF64_ST_BIND(x) ((x) >> 4) -#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) -#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) - -/* Symbol Binding - ELF32_ST_BIND - st_info */ -#define STB_LOCAL 0 /* Local symbol */ -#define STB_GLOBAL 1 /* Global symbol */ -#define STB_WEAK 2 /* like global - lower precedence */ -#define STB_NUM 3 /* number of symbol bindings */ -#define STB_LOPROC 13 /* reserved range for processor */ -#define STB_HIPROC 15 /* specific symbol bindings */ - -/* Symbol type - ELF32_ST_TYPE - st_info */ -#define STT_NOTYPE 0 /* not specified */ -#define STT_OBJECT 1 /* data object */ -#define STT_FUNC 2 /* function */ -#define STT_SECTION 3 /* section */ -#define STT_FILE 4 /* file */ -#define STT_NUM 5 /* number of symbol types */ -#define STT_LOPROC 13 /* reserved range for processor */ -#define STT_HIPROC 15 /* specific symbol types */ - -/* Relocation entry with implicit addend */ -typedef struct { - Elf32_Addr r_offset; /* offset of relocation */ - Elf32_Word r_info; /* symbol table index and type */ -} Elf32_Rel; - -/* Relocation entry with explicit addend */ -typedef struct { - Elf32_Addr r_offset; /* offset of relocation */ - Elf32_Word r_info; /* symbol table index and type */ - Elf32_Sword r_addend; -} Elf32_Rela; - -/* Extract relocation info - r_info */ -#define ELF32_R_SYM(i) ((i) >> 8) -#define ELF32_R_TYPE(i) ((unsigned char) (i)) -#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) - -typedef struct { - Elf64_Xword r_offset; /* where to do it */ - Elf64_Xword r_info; /* index & type of relocation */ -} Elf64_Rel; - -typedef struct { - Elf64_Xword r_offset; /* where to do it */ - Elf64_Xword r_info; /* index & type of relocation */ - Elf64_Sxword r_addend; /* adjustment value */ -} Elf64_Rela; - -#define ELF64_R_SYM(info) ((info) >> 32) -#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF) -#define ELF64_R_INFO(s,t) (((s) << 32) + (u_int32_t)(t)) - -/* Program Header */ -typedef struct { - Elf32_Word p_type; /* segment type */ - Elf32_Off p_offset; /* segment offset */ - Elf32_Addr p_vaddr; /* virtual address of segment */ - Elf32_Addr p_paddr; /* physical address - ignored? */ - Elf32_Word p_filesz; /* number of bytes in file for seg. */ - Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ - Elf32_Word p_flags; /* flags */ - Elf32_Word p_align; /* memory alignment */ -} Elf32_Phdr; - -typedef struct { - Elf64_Half p_type; /* entry type */ - Elf64_Half p_flags; /* flags */ - Elf64_Off p_offset; /* offset */ - Elf64_Addr p_vaddr; /* virtual address */ - Elf64_Addr p_paddr; /* physical address */ - Elf64_Xword p_filesz; /* file size */ - Elf64_Xword p_memsz; /* memory size */ - Elf64_Xword p_align; /* memory & file alignment */ -} Elf64_Phdr; - -/* Segment types - p_type */ -#define PT_NULL 0 /* unused */ -#define PT_LOAD 1 /* loadable segment */ -#define PT_DYNAMIC 2 /* dynamic linking section */ -#define PT_INTERP 3 /* the RTLD */ -#define PT_NOTE 4 /* auxiliary information */ -#define PT_SHLIB 5 /* reserved - purpose undefined */ -#define PT_PHDR 6 /* program header */ -#define PT_NUM 7 /* Number of segment types */ -#define PT_LOPROC 0x70000000 /* reserved range for processor */ -#define PT_HIPROC 0x7fffffff /* specific segment types */ - -/* Segment flags - p_flags */ -#define PF_X 0x1 /* Executable */ -#define PF_W 0x2 /* Writable */ -#define PF_R 0x4 /* Readable */ -#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ - /* specific segment flags */ - -/* Dynamic structure */ -typedef struct { - Elf32_Sword d_tag; /* controls meaning of d_val */ - union { - Elf32_Word d_val; /* Multiple meanings - see d_tag */ - Elf32_Addr d_ptr; /* program virtual address */ - } d_un; -} Elf32_Dyn; - -typedef struct { - Elf64_Xword d_tag; /* controls meaning of d_val */ - union { - Elf64_Addr d_ptr; - Elf64_Xword d_val; - } d_un; -} Elf64_Dyn; - -/* Dynamic Array Tags - d_tag */ -#define DT_NULL 0 /* marks end of _DYNAMIC array */ -#define DT_NEEDED 1 /* string table offset of needed lib */ -#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ -#define DT_PLTGOT 3 /* address PLT/GOT */ -#define DT_HASH 4 /* address of symbol hash table */ -#define DT_STRTAB 5 /* address of string table */ -#define DT_SYMTAB 6 /* address of symbol table */ -#define DT_RELA 7 /* address of relocation table */ -#define DT_RELASZ 8 /* size of relocation table */ -#define DT_RELAENT 9 /* size of relocation entry */ -#define DT_STRSZ 10 /* size of string table */ -#define DT_SYMENT 11 /* size of symbol table entry */ -#define DT_INIT 12 /* address of initialization func. */ -#define DT_FINI 13 /* address of termination function */ -#define DT_SONAME 14 /* string table offset of shared obj */ -#define DT_RPATH 15 /* string table offset of library - search path */ -#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ -#define DT_REL 17 /* address of rel. tbl. w addends */ -#define DT_RELSZ 18 /* size of DT_REL relocation table */ -#define DT_RELENT 19 /* size of DT_REL relocation entry */ -#define DT_PLTREL 20 /* PLT referenced relocation entry */ -#define DT_DEBUG 21 /* bugger */ -#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ -#define DT_JMPREL 23 /* add. of PLT''s relocation entries */ -#define DT_BIND_NOW 24 /* Bind now regardless of env setting */ -#define DT_NUM 25 /* Number used. */ -#define DT_LOPROC 0x70000000 /* reserved range for processor */ -#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ - -/* Standard ELF hashing function */ -unsigned int elf_hash(const unsigned char *name); - -/* - * Note Definitions - */ -typedef struct { - Elf32_Word namesz; - Elf32_Word descsz; - Elf32_Word type; -} Elf32_Note; - -typedef struct { - Elf64_Half namesz; - Elf64_Half descsz; - Elf64_Half type; -} Elf64_Note; - - -#if defined(ELFSIZE) -#define CONCAT(x,y) __CONCAT(x,y) -#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) -#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) -#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) -#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) -#endif - -#if defined(ELFSIZE) && (ELFSIZE == 32) -#define Elf_Ehdr Elf32_Ehdr -#define Elf_Phdr Elf32_Phdr -#define Elf_Shdr Elf32_Shdr -#define Elf_Sym Elf32_Sym -#define Elf_Rel Elf32_Rel -#define Elf_RelA Elf32_Rela -#define Elf_Dyn Elf32_Dyn -#define Elf_Word Elf32_Word -#define Elf_Sword Elf32_Sword -#define Elf_Addr Elf32_Addr -#define Elf_Off Elf32_Off -#define Elf_Nhdr Elf32_Nhdr -#define Elf_Note Elf32_Note - -#define ELF_R_SYM ELF32_R_SYM -#define ELF_R_TYPE ELF32_R_TYPE -#define ELF_R_INFO ELF32_R_INFO -#define ELFCLASS ELFCLASS32 - -#define ELF_ST_BIND ELF32_ST_BIND -#define ELF_ST_TYPE ELF32_ST_TYPE -#define ELF_ST_INFO ELF32_ST_INFO - -#define AuxInfo Aux32Info -#elif defined(ELFSIZE) && (ELFSIZE == 64) -#define Elf_Ehdr Elf64_Ehdr -#define Elf_Phdr Elf64_Phdr -#define Elf_Shdr Elf64_Shdr -#define Elf_Sym Elf64_Sym -#define Elf_Rel Elf64_Rel -#define Elf_RelA Elf64_Rela -#define Elf_Dyn Elf64_Dyn -#define Elf_Word Elf64_Word -#define Elf_Sword Elf64_Sword -#define Elf_Addr Elf64_Addr -#define Elf_Off Elf64_Off -#define Elf_Nhdr Elf64_Nhdr -#define Elf_Note Elf64_Note - -#define ELF_R_SYM ELF64_R_SYM -#define ELF_R_TYPE ELF64_R_TYPE -#define ELF_R_INFO ELF64_R_INFO -#define ELFCLASS ELFCLASS64 - -#define ELF_ST_BIND ELF64_ST_BIND -#define ELF_ST_TYPE ELF64_ST_TYPE -#define ELF_ST_INFO ELF64_ST_INFO - -#define AuxInfo Aux64Info -#endif - +#include <xen/elfstructs.h> Index: build-32-unstable-12663/xen/include/xen/elf.h ==================================================================--- build-32-unstable-12663.orig/xen/include/xen/elf.h +++ build-32-unstable-12663/xen/include/xen/elf.h @@ -27,495 +27,7 @@ #ifndef __XEN_ELF_H__ #define __XEN_ELF_H__ -typedef u8 Elf_Byte; - -typedef u32 Elf32_Addr; /* Unsigned program address */ -typedef u32 Elf32_Off; /* Unsigned file offset */ -typedef s32 Elf32_Sword; /* Signed large integer */ -typedef u32 Elf32_Word; /* Unsigned large integer */ -typedef u16 Elf32_Half; /* Unsigned medium integer */ - -typedef u64 Elf64_Addr; -typedef u64 Elf64_Off; -typedef s32 Elf64_Shalf; - -typedef s32 Elf64_Sword; -typedef u32 Elf64_Word; - -typedef s64 Elf64_Sxword; -typedef u64 Elf64_Xword; - -typedef u32 Elf64_Half; -typedef u16 Elf64_Quarter; - -/* - * e_ident[] identification indexes - * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html - */ -#define EI_MAG0 0 /* file ID */ -#define EI_MAG1 1 /* file ID */ -#define EI_MAG2 2 /* file ID */ -#define EI_MAG3 3 /* file ID */ -#define EI_CLASS 4 /* file class */ -#define EI_DATA 5 /* data encoding */ -#define EI_VERSION 6 /* ELF header version */ -#define EI_OSABI 7 /* OS/ABI ID */ -#define EI_ABIVERSION 8 /* ABI version */ -#define EI_PAD 9 /* start of pad bytes */ -#define EI_NIDENT 16 /* Size of e_ident[] */ - -/* e_ident[] magic number */ -#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ -#define ELFMAG1 ''E'' /* e_ident[EI_MAG1] */ -#define ELFMAG2 ''L'' /* e_ident[EI_MAG2] */ -#define ELFMAG3 ''F'' /* e_ident[EI_MAG3] */ -#define ELFMAG "\177ELF" /* magic */ -#define SELFMAG 4 /* size of magic */ - -/* e_ident[] file class */ -#define ELFCLASSNONE 0 /* invalid */ -#define ELFCLASS32 1 /* 32-bit objs */ -#define ELFCLASS64 2 /* 64-bit objs */ -#define ELFCLASSNUM 3 /* number of classes */ - -/* e_ident[] data encoding */ -#define ELFDATANONE 0 /* invalid */ -#define ELFDATA2LSB 1 /* Little-Endian */ -#define ELFDATA2MSB 2 /* Big-Endian */ -#define ELFDATANUM 3 /* number of data encode defines */ - -/* e_ident[] Operating System/ABI */ -#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ -#define ELFOSABI_HPUX 1 /* HP-UX operating system */ -#define ELFOSABI_NETBSD 2 /* NetBSD */ -#define ELFOSABI_LINUX 3 /* GNU/Linux */ -#define ELFOSABI_HURD 4 /* GNU/Hurd */ -#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ -#define ELFOSABI_SOLARIS 6 /* Solaris */ -#define ELFOSABI_MONTEREY 7 /* Monterey */ -#define ELFOSABI_IRIX 8 /* IRIX */ -#define ELFOSABI_FREEBSD 9 /* FreeBSD */ -#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ -#define ELFOSABI_MODESTO 11 /* Novell Modesto */ -#define ELFOSABI_OPENBSD 12 /* OpenBSD */ -#define ELFOSABI_ARM 97 /* ARM */ -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ - -/* e_ident */ -#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ - (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ - (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ - (ehdr).e_ident[EI_MAG3] == ELFMAG3) - -/* ELF Header */ -typedef struct elfhdr { - unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ - Elf32_Half e_type; /* object file type */ - Elf32_Half e_machine; /* machine */ - Elf32_Word e_version; /* object file version */ - Elf32_Addr e_entry; /* virtual entry point */ - Elf32_Off e_phoff; /* program header table offset */ - Elf32_Off e_shoff; /* section header table offset */ - Elf32_Word e_flags; /* processor-specific flags */ - Elf32_Half e_ehsize; /* ELF header size */ - Elf32_Half e_phentsize; /* program header entry size */ - Elf32_Half e_phnum; /* number of program header entries */ - Elf32_Half e_shentsize; /* section header entry size */ - Elf32_Half e_shnum; /* number of section header entries */ - Elf32_Half e_shstrndx; /* section header table''s "section - header string table" entry offset */ -} Elf32_Ehdr; - -typedef struct { - unsigned char e_ident[EI_NIDENT]; /* Id bytes */ - Elf64_Quarter e_type; /* file type */ - Elf64_Quarter e_machine; /* machine type */ - Elf64_Half e_version; /* version number */ - Elf64_Addr e_entry; /* entry point */ - Elf64_Off e_phoff; /* Program hdr offset */ - Elf64_Off e_shoff; /* Section hdr offset */ - Elf64_Half e_flags; /* Processor flags */ - Elf64_Quarter e_ehsize; /* sizeof ehdr */ - Elf64_Quarter e_phentsize; /* Program header entry size */ - Elf64_Quarter e_phnum; /* Number of program headers */ - Elf64_Quarter e_shentsize; /* Section header entry size */ - Elf64_Quarter e_shnum; /* Number of section headers */ - Elf64_Quarter e_shstrndx; /* String table index */ -} Elf64_Ehdr; - -/* e_type */ -#define ET_NONE 0 /* No file type */ -#define ET_REL 1 /* relocatable file */ -#define ET_EXEC 2 /* executable file */ -#define ET_DYN 3 /* shared object file */ -#define ET_CORE 4 /* core file */ -#define ET_NUM 5 /* number of types */ -#define ET_LOPROC 0xff00 /* reserved range for processor */ -#define ET_HIPROC 0xffff /* specific e_type */ - -/* e_machine */ -#define EM_NONE 0 /* No Machine */ -#define EM_M32 1 /* AT&T WE 32100 */ -#define EM_SPARC 2 /* SPARC */ -#define EM_386 3 /* Intel 80386 */ -#define EM_68K 4 /* Motorola 68000 */ -#define EM_88K 5 /* Motorola 88000 */ -#define EM_486 6 /* Intel 80486 - unused? */ -#define EM_860 7 /* Intel 80860 */ -#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ -/* - * Don''t know if EM_MIPS_RS4_BE, - * EM_SPARC64, EM_PARISC, - * or EM_PPC are ABI compliant - */ -#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ -#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ -#define EM_PARISC 15 /* HPPA */ -#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ -#define EM_PPC 20 /* PowerPC */ -#define EM_PPC64 21 /* PowerPC 64-bit */ -#define EM_ARM 40 /* Advanced RISC Machines ARM */ -#define EM_ALPHA 41 /* DEC ALPHA */ -#define EM_SPARCV9 43 /* SPARC version 9 */ -#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ -#define EM_IA_64 50 /* Intel Merced */ -#define EM_X86_64 62 /* AMD x86-64 architecture */ -#define EM_VAX 75 /* DEC VAX */ - -/* Version */ -#define EV_NONE 0 /* Invalid */ -#define EV_CURRENT 1 /* Current */ -#define EV_NUM 2 /* number of versions */ - -/* Section Header */ -typedef struct { - Elf32_Word sh_name; /* name - index into section header - string table section */ - Elf32_Word sh_type; /* type */ - Elf32_Word sh_flags; /* flags */ - Elf32_Addr sh_addr; /* address */ - Elf32_Off sh_offset; /* file offset */ - Elf32_Word sh_size; /* section size */ - Elf32_Word sh_link; /* section header table index link */ - Elf32_Word sh_info; /* extra information */ - Elf32_Word sh_addralign; /* address alignment */ - Elf32_Word sh_entsize; /* section entry size */ -} Elf32_Shdr; - -typedef struct { - Elf64_Half sh_name; /* section name */ - Elf64_Half sh_type; /* section type */ - Elf64_Xword sh_flags; /* section flags */ - Elf64_Addr sh_addr; /* virtual address */ - Elf64_Off sh_offset; /* file offset */ - Elf64_Xword sh_size; /* section size */ - Elf64_Half sh_link; /* link to another */ - Elf64_Half sh_info; /* misc info */ - Elf64_Xword sh_addralign; /* memory alignment */ - Elf64_Xword sh_entsize; /* table entry size */ -} Elf64_Shdr; - -/* Special Section Indexes */ -#define SHN_UNDEF 0 /* undefined */ -#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ -#define SHN_LOPROC 0xff00 /* reserved range for processor */ -#define SHN_HIPROC 0xff1f /* specific section indexes */ -#define SHN_ABS 0xfff1 /* absolute value */ -#define SHN_COMMON 0xfff2 /* common symbol */ -#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ - -/* sh_type */ -#define SHT_NULL 0 /* inactive */ -#define SHT_PROGBITS 1 /* program defined information */ -#define SHT_SYMTAB 2 /* symbol table section */ -#define SHT_STRTAB 3 /* string table section */ -#define SHT_RELA 4 /* relocation section with addends*/ -#define SHT_HASH 5 /* symbol hash table section */ -#define SHT_DYNAMIC 6 /* dynamic section */ -#define SHT_NOTE 7 /* note section */ -#define SHT_NOBITS 8 /* no space section */ -#define SHT_REL 9 /* relation section without addends */ -#define SHT_SHLIB 10 /* reserved - purpose unknown */ -#define SHT_DYNSYM 11 /* dynamic symbol table section */ -#define SHT_NUM 12 /* number of section types */ -#define SHT_LOPROC 0x70000000 /* reserved range for processor */ -#define SHT_HIPROC 0x7fffffff /* specific section header types */ -#define SHT_LOUSER 0x80000000 /* reserved range for application */ -#define SHT_HIUSER 0xffffffff /* specific indexes */ - -/* Section names */ -#define ELF_BSS ".bss" /* uninitialized data */ -#define ELF_DATA ".data" /* initialized data */ -#define ELF_DEBUG ".debug" /* debug */ -#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ -#define ELF_DYNSTR ".dynstr" /* dynamic string table */ -#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ -#define ELF_FINI ".fini" /* termination code */ -#define ELF_GOT ".got" /* global offset table */ -#define ELF_HASH ".hash" /* symbol hash table */ -#define ELF_INIT ".init" /* initialization code */ -#define ELF_REL_DATA ".rel.data" /* relocation data */ -#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ -#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ -#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ -#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ -#define ELF_REL_TEXT ".rel.text" /* relocation code */ -#define ELF_RODATA ".rodata" /* read-only data */ -#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ -#define ELF_STRTAB ".strtab" /* string table */ -#define ELF_SYMTAB ".symtab" /* symbol table */ -#define ELF_TEXT ".text" /* code */ - - -/* Section Attribute Flags - sh_flags */ -#define SHF_WRITE 0x1 /* Writable */ -#define SHF_ALLOC 0x2 /* occupies memory */ -#define SHF_EXECINSTR 0x4 /* executable */ -#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ - /* specific section attributes */ - -/* Symbol Table Entry */ -typedef struct elf32_sym { - Elf32_Word st_name; /* name - index into string table */ - Elf32_Addr st_value; /* symbol value */ - Elf32_Word st_size; /* symbol size */ - unsigned char st_info; /* type and binding */ - unsigned char st_other; /* 0 - no defined meaning */ - Elf32_Half st_shndx; /* section header index */ -} Elf32_Sym; - -typedef struct { - Elf64_Half st_name; /* Symbol name index in str table */ - Elf_Byte st_info; /* type / binding attrs */ - Elf_Byte st_other; /* unused */ - Elf64_Quarter st_shndx; /* section index of symbol */ - Elf64_Xword st_value; /* value of symbol */ - Elf64_Xword st_size; /* size of symbol */ -} Elf64_Sym; - -/* Symbol table index */ -#define STN_UNDEF 0 /* undefined */ - -/* Extract symbol info - st_info */ -#define ELF32_ST_BIND(x) ((x) >> 4) -#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) -#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) - -#define ELF64_ST_BIND(x) ((x) >> 4) -#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) -#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) - -/* Symbol Binding - ELF32_ST_BIND - st_info */ -#define STB_LOCAL 0 /* Local symbol */ -#define STB_GLOBAL 1 /* Global symbol */ -#define STB_WEAK 2 /* like global - lower precedence */ -#define STB_NUM 3 /* number of symbol bindings */ -#define STB_LOPROC 13 /* reserved range for processor */ -#define STB_HIPROC 15 /* specific symbol bindings */ - -/* Symbol type - ELF32_ST_TYPE - st_info */ -#define STT_NOTYPE 0 /* not specified */ -#define STT_OBJECT 1 /* data object */ -#define STT_FUNC 2 /* function */ -#define STT_SECTION 3 /* section */ -#define STT_FILE 4 /* file */ -#define STT_NUM 5 /* number of symbol types */ -#define STT_LOPROC 13 /* reserved range for processor */ -#define STT_HIPROC 15 /* specific symbol types */ - -/* Relocation entry with implicit addend */ -typedef struct { - Elf32_Addr r_offset; /* offset of relocation */ - Elf32_Word r_info; /* symbol table index and type */ -} Elf32_Rel; - -/* Relocation entry with explicit addend */ -typedef struct { - Elf32_Addr r_offset; /* offset of relocation */ - Elf32_Word r_info; /* symbol table index and type */ - Elf32_Sword r_addend; -} Elf32_Rela; - -/* Extract relocation info - r_info */ -#define ELF32_R_SYM(i) ((i) >> 8) -#define ELF32_R_TYPE(i) ((unsigned char) (i)) -#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) - -typedef struct { - Elf64_Xword r_offset; /* where to do it */ - Elf64_Xword r_info; /* index & type of relocation */ -} Elf64_Rel; - -typedef struct { - Elf64_Xword r_offset; /* where to do it */ - Elf64_Xword r_info; /* index & type of relocation */ - Elf64_Sxword r_addend; /* adjustment value */ -} Elf64_Rela; - -#define ELF64_R_SYM(info) ((info) >> 32) -#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF) -#define ELF64_R_INFO(s,t) (((s) << 32) + (u32)(t)) - -/* Program Header */ -typedef struct { - Elf32_Word p_type; /* segment type */ - Elf32_Off p_offset; /* segment offset */ - Elf32_Addr p_vaddr; /* virtual address of segment */ - Elf32_Addr p_paddr; /* physical address - ignored? */ - Elf32_Word p_filesz; /* number of bytes in file for seg. */ - Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ - Elf32_Word p_flags; /* flags */ - Elf32_Word p_align; /* memory alignment */ -} Elf32_Phdr; - -typedef struct { - Elf64_Half p_type; /* entry type */ - Elf64_Half p_flags; /* flags */ - Elf64_Off p_offset; /* offset */ - Elf64_Addr p_vaddr; /* virtual address */ - Elf64_Addr p_paddr; /* physical address */ - Elf64_Xword p_filesz; /* file size */ - Elf64_Xword p_memsz; /* memory size */ - Elf64_Xword p_align; /* memory & file alignment */ -} Elf64_Phdr; - -/* Segment types - p_type */ -#define PT_NULL 0 /* unused */ -#define PT_LOAD 1 /* loadable segment */ -#define PT_DYNAMIC 2 /* dynamic linking section */ -#define PT_INTERP 3 /* the RTLD */ -#define PT_NOTE 4 /* auxiliary information */ -#define PT_SHLIB 5 /* reserved - purpose undefined */ -#define PT_PHDR 6 /* program header */ -#define PT_NUM 7 /* Number of segment types */ -#define PT_LOPROC 0x70000000 /* reserved range for processor */ -#define PT_HIPROC 0x7fffffff /* specific segment types */ - -/* Segment flags - p_flags */ -#define PF_X 0x1 /* Executable */ -#define PF_W 0x2 /* Writable */ -#define PF_R 0x4 /* Readable */ -#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ - /* specific segment flags */ - -/* Dynamic structure */ -typedef struct { - Elf32_Sword d_tag; /* controls meaning of d_val */ - union { - Elf32_Word d_val; /* Multiple meanings - see d_tag */ - Elf32_Addr d_ptr; /* program virtual address */ - } d_un; -} Elf32_Dyn; - -typedef struct { - Elf64_Xword d_tag; /* controls meaning of d_val */ - union { - Elf64_Addr d_ptr; - Elf64_Xword d_val; - } d_un; -} Elf64_Dyn; - -/* Dynamic Array Tags - d_tag */ -#define DT_NULL 0 /* marks end of _DYNAMIC array */ -#define DT_NEEDED 1 /* string table offset of needed lib */ -#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ -#define DT_PLTGOT 3 /* address PLT/GOT */ -#define DT_HASH 4 /* address of symbol hash table */ -#define DT_STRTAB 5 /* address of string table */ -#define DT_SYMTAB 6 /* address of symbol table */ -#define DT_RELA 7 /* address of relocation table */ -#define DT_RELASZ 8 /* size of relocation table */ -#define DT_RELAENT 9 /* size of relocation entry */ -#define DT_STRSZ 10 /* size of string table */ -#define DT_SYMENT 11 /* size of symbol table entry */ -#define DT_INIT 12 /* address of initialization func. */ -#define DT_FINI 13 /* address of termination function */ -#define DT_SONAME 14 /* string table offset of shared obj */ -#define DT_RPATH 15 /* string table offset of library - search path */ -#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ -#define DT_REL 17 /* address of rel. tbl. w addends */ -#define DT_RELSZ 18 /* size of DT_REL relocation table */ -#define DT_RELENT 19 /* size of DT_REL relocation entry */ -#define DT_PLTREL 20 /* PLT referenced relocation entry */ -#define DT_DEBUG 21 /* bugger */ -#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ -#define DT_JMPREL 23 /* add. of PLT''s relocation entries */ -#define DT_BIND_NOW 24 /* Bind now regardless of env setting */ -#define DT_NUM 25 /* Number used. */ -#define DT_LOPROC 0x70000000 /* reserved range for processor */ -#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ - -/* Standard ELF hashing function */ -unsigned int elf_hash(const unsigned char *name); - -/* - * Note Definitions - */ - -typedef struct { - u32 namesz; - u32 descsz; - u32 type; -} Elf_Note; /* same format for both 32-bit and 64-bit ELF */ - -#if defined(ELFSIZE) -#define CONCAT(x,y) __CONCAT(x,y) -#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) -#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) -#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) -#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) -#endif - -#if defined(ELFSIZE) && (ELFSIZE == 32) -#define Elf_Ehdr Elf32_Ehdr -#define Elf_Phdr Elf32_Phdr -#define Elf_Shdr Elf32_Shdr -#define Elf_Sym Elf32_Sym -#define Elf_Rel Elf32_Rel -#define Elf_RelA Elf32_Rela -#define Elf_Dyn Elf32_Dyn -#define Elf_Word Elf32_Word -#define Elf_Sword Elf32_Sword -#define Elf_Addr Elf32_Addr -#define Elf_Off Elf32_Off -#define Elf_Nhdr Elf32_Nhdr - -#define ELF_R_SYM ELF32_R_SYM -#define ELF_R_TYPE ELF32_R_TYPE -#define ELF_R_INFO ELF32_R_INFO -#define ELFCLASS ELFCLASS32 - -#define ELF_ST_BIND ELF32_ST_BIND -#define ELF_ST_TYPE ELF32_ST_TYPE -#define ELF_ST_INFO ELF32_ST_INFO - -#define AuxInfo Aux32Info -#elif defined(ELFSIZE) && (ELFSIZE == 64) -#define Elf_Ehdr Elf64_Ehdr -#define Elf_Phdr Elf64_Phdr -#define Elf_Shdr Elf64_Shdr -#define Elf_Sym Elf64_Sym -#define Elf_Rel Elf64_Rel -#define Elf_RelA Elf64_Rela -#define Elf_Dyn Elf64_Dyn -#define Elf_Word Elf64_Word -#define Elf_Sword Elf64_Sword -#define Elf_Addr Elf64_Addr -#define Elf_Off Elf64_Off -#define Elf_Nhdr Elf64_Nhdr - -#define ELF_R_SYM ELF64_R_SYM -#define ELF_R_TYPE ELF64_R_TYPE -#define ELF_R_INFO ELF64_R_INFO -#define ELFCLASS ELFCLASS64 - -#define ELF_ST_BIND ELF64_ST_BIND -#define ELF_ST_TYPE ELF64_ST_TYPE -#define ELF_ST_INFO ELF64_ST_INFO - -#define AuxInfo Aux64Info -#endif +#include <public/elfstructs.h> struct domain_setup_info; extern int loadelfimage(struct domain_setup_info *); Index: build-32-unstable-12663/xen/arch/x86/boot/mkelf32.c ==================================================================--- build-32-unstable-12663.orig/xen/arch/x86/boot/mkelf32.c +++ 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 -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 03/10] libelf: use for x86 dom0 builder.
This patch switches the x86 dom0 builder over to libelf. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- xen/arch/x86/domain_build.c | 223 ++++++++++++++++---------------------------- xen/common/Makefile | 2 2 files changed, 85 insertions(+), 140 deletions(-) Index: build-32-unstable-12621/xen/common/Makefile ==================================================================--- build-32-unstable-12621.orig/xen/common/Makefile +++ 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 Index: build-32-unstable-12621/xen/arch/x86/domain_build.c ==================================================================--- build-32-unstable-12621.orig/xen/arch/x86/domain_build.c +++ build-32-unstable-12621/xen/arch/x86/domain_build.c @@ -13,7 +13,6 @@ #include <xen/delay.h> #include <xen/event.h> #include <xen/console.h> -#include <xen/elf.h> #include <xen/kernel.h> #include <xen/domain.h> #include <xen/version.h> @@ -28,7 +27,7 @@ #include <asm/shadow.h> #include <public/version.h> -#include <public/elfnote.h> +#include <public/libelf.h> extern unsigned long initial_images_nrpages(void); extern void discard_initial_images(void); @@ -186,69 +185,12 @@ static void process_dom0_ioports_disable } } -static const char *feature_names[XENFEAT_NR_SUBMAPS*32] = { - [XENFEAT_writable_page_tables] = "writable_page_tables", - [XENFEAT_writable_descriptor_tables] = "writable_descriptor_tables", - [XENFEAT_auto_translated_physmap] = "auto_translated_physmap", - [XENFEAT_supervisor_mode_kernel] = "supervisor_mode_kernel", - [XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb" -}; - -static void parse_features( - const char *feats, - uint32_t supported[XENFEAT_NR_SUBMAPS], - uint32_t required[XENFEAT_NR_SUBMAPS]) -{ - const char *end, *p; - int i, req; - - if ( (end = strchr(feats, '','')) == NULL ) - end = feats + strlen(feats); - - while ( feats < end ) - { - p = strchr(feats, ''|''); - if ( (p == NULL) || (p > end) ) - p = end; - - req = (*feats == ''!''); - if ( req ) - feats++; - - for ( i = 0; i < XENFEAT_NR_SUBMAPS*32; i++ ) - { - if ( feature_names[i] == NULL ) - continue; - - if ( strncmp(feature_names[i], feats, p-feats) == 0 ) - { - set_bit(i, supported); - if ( req ) - set_bit(i, required); - break; - } - } - - if ( i == XENFEAT_NR_SUBMAPS*32 ) - { - printk("Unknown kernel feature \"%.*s\".\n", - (int)(p-feats), feats); - if ( req ) - panic("Domain 0 requires an unknown hypervisor feature.\n"); - } - - feats = p; - if ( *feats == ''|'' ) - feats++; - } -} - int construct_dom0(struct domain *d, unsigned long _image_start, unsigned long image_len, unsigned long _initrd_start, unsigned long initrd_len, char *cmdline) { - int i, rc, dom0_pae, xen_pae, order; + int i, rc, compatible, order, machine; struct cpu_user_regs *regs; unsigned long pfn, mfn; unsigned long nr_pages; @@ -259,9 +201,6 @@ int construct_dom0(struct domain *d, struct page_info *page = NULL; start_info_t *si; struct vcpu *v = d->vcpu[0]; - const char *p; - unsigned long hypercall_page; - int hypercall_page_defined; #if defined(__i386__) char *image_start = (char *)_image_start; /* use lowmem mappings */ char *initrd_start = (char *)_initrd_start; /* use lowmem mappings */ @@ -283,7 +222,10 @@ int construct_dom0(struct domain *d, * *_start address are page-aligned, except v_start (and v_end) which are * superpage-aligned. */ - struct domain_setup_info dsi; + struct elf_binary elf; + struct elf_dom_parms parms; + unsigned long vkern_start; + unsigned long vkern_end; unsigned long vinitrd_start; unsigned long vinitrd_end; unsigned long vphysmap_start; @@ -294,6 +236,7 @@ int construct_dom0(struct domain *d, unsigned long vstack_end; unsigned long vpt_start; unsigned long vpt_end; + unsigned long v_start; unsigned long v_end; /* Machine address of next candidate page-table page. */ @@ -308,46 +251,67 @@ int construct_dom0(struct domain *d, BUG_ON(d->vcpu[0] == NULL); BUG_ON(test_bit(_VCPUF_initialised, &v->vcpu_flags)); - memset(&dsi, 0, sizeof(struct domain_setup_info)); - dsi.image_addr = (unsigned long)image_start; - dsi.image_len = image_len; - printk("*** LOADING DOMAIN 0 ***\n"); d->max_pages = ~0U; nr_pages = compute_dom0_nr_pages(); - if ( (rc = parseelfimage(&dsi)) != 0 ) + if (0 != (rc = elf_init(&elf, image_start, image_len))) return rc; +#ifdef VERBOSE + elf_set_verbose(&elf); +#endif + elf_parse_binary(&elf); + if (0 != (elf_xen_parse(&elf, &parms))) + return rc; + printk(" Dom0 kernel: %s%s, %s, paddr 0x%" PRIx64 " -> 0x%" PRIx64 "\n", + elf_64bit(&elf) ? "64-bit" : "32-bit", + parms.pae ? ", PAE" : "", + elf_msb(&elf) ? "msb" : "lsb", + elf.pstart, elf.pend); - dom0_pae = (dsi.pae_kernel != PAEKERN_no); - xen_pae = (CONFIG_PAGING_LEVELS == 3); - if ( dom0_pae != xen_pae ) + /* compatibility check */ + compatible = 0; + machine = elf_uval(&elf, elf.ehdr, e_machine); + switch (CONFIG_PAGING_LEVELS) { + case 2: /* x86_32 */ + printk(" Xen kernel: 32-bit, lsb\n"); + if (!elf_64bit(&elf) && !parms.pae && machine == EM_386) + compatible = 1; + break; + case 3: /* x86_32p */ + printk(" Xen kernel : 32-bit, PAE, lsb\n"); + if (!elf_64bit(&elf) && parms.pae && machine == EM_386) + compatible = 1; + break; + case 4: /* x86_64 */ +#ifndef CONFIG_COMPAT + printk(" Xen kernel : 64-bit, lsb\n"); +#else + printk(" Xen kernel : 64-bit, lsb, compat32\n"); + if (!elf_64bit(&elf) && parms.pae && machine == EM_386) + compatible = 1; +#endif + if (elf_64bit(&elf) && machine == EM_X86_64) + compatible = 1; + break; + } + if ( !compatible ) { - printk("PAE mode mismatch between Xen and DOM0 (xen=%s, dom0=%s)\n", - xen_pae ? "yes" : "no", dom0_pae ? "yes" : "no"); + printk("Mismatch between Xen and DOM0 kernel\n"); return -EINVAL; } - if ( xen_pae && dsi.pae_kernel == PAEKERN_extended_cr3 ) + if ( parms.pae == PAEKERN_extended_cr3 ) set_bit(VMASST_TYPE_pae_extended_cr3, &d->vm_assist); - if ( (p = xen_elfnote_string(&dsi, XEN_ELFNOTE_FEATURES)) != NULL ) - { - parse_features(p, - dom0_features_supported, - dom0_features_required); - printk("Domain 0 kernel supports features = { %08x }.\n", - dom0_features_supported[0]); - printk("Domain 0 kernel requires features = { %08x }.\n", - dom0_features_required[0]); - if ( dom0_features_required[0] ) + if ( parms.f_required[0] /* Huh? -- kraxel */ ) panic("Domain 0 requires an unsupported hypervisor feature.\n"); - } + /* Align load address to 4MB boundary. */ - dsi.v_start &= ~((1UL<<22)-1); + v_start = parms.virt_base & ~((1UL<<22)-1); /* * Why do we need this? The number of page-table frames depends on the @@ -356,7 +320,9 @@ int construct_dom0(struct domain *d, * read-only). We have a pair of simultaneous equations in two unknowns, * which we solve by exhaustive search. */ - vinitrd_start = round_pgup(dsi.v_end); + vkern_start = parms.virt_kstart; + vkern_end = parms.virt_kend; + vinitrd_start = round_pgup(vkern_end); vinitrd_end = vinitrd_start + initrd_len; vphysmap_start = round_pgup(vinitrd_end); vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long)); @@ -374,12 +340,12 @@ int construct_dom0(struct domain *d, if ( (v_end - vstack_end) < (512UL << 10) ) v_end += 1UL << 22; /* Add extra 4MB to get >= 512kB padding. */ #if defined(__i386__) && !defined(CONFIG_X86_PAE) - if ( (((v_end - dsi.v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >> + if ( (((v_end - v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >> L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages ) break; #elif defined(__i386__) && defined(CONFIG_X86_PAE) /* 5 pages: 1x 3rd + 4x 2nd level */ - if ( (((v_end - dsi.v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >> + if ( (((v_end - v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >> L2_PAGETABLE_SHIFT) + 5) <= nr_pt_pages ) break; #elif defined(__x86_64__) @@ -387,15 +353,15 @@ int construct_dom0(struct domain *d, (((((_h) + ((1UL<<(_s))-1)) & ~((1UL<<(_s))-1)) - \ ((_l) & ~((1UL<<(_s))-1))) >> (_s)) if ( (1 + /* # L4 */ - NR(dsi.v_start, v_end, L4_PAGETABLE_SHIFT) + /* # L3 */ - NR(dsi.v_start, v_end, L3_PAGETABLE_SHIFT) + /* # L2 */ - NR(dsi.v_start, v_end, L2_PAGETABLE_SHIFT)) /* # L1 */ + NR(v_start, v_end, L4_PAGETABLE_SHIFT) + /* # L3 */ + NR(v_start, v_end, L3_PAGETABLE_SHIFT) + /* # L2 */ + NR(v_start, v_end, L2_PAGETABLE_SHIFT)) /* # L1 */ <= nr_pt_pages ) break; #endif } - order = get_order_from_bytes(v_end - dsi.v_start); + order = get_order_from_bytes(v_end - v_start); if ( (1UL << order) > nr_pages ) panic("Domain 0 allocation is too small for kernel image.\n"); @@ -422,24 +388,24 @@ int construct_dom0(struct domain *d, " Page tables: %p->%p\n" " Boot stack: %p->%p\n" " TOTAL: %p->%p\n", - _p(dsi.v_kernstart), _p(dsi.v_kernend), + _p(vkern_start), _p(vkern_end), _p(vinitrd_start), _p(vinitrd_end), _p(vphysmap_start), _p(vphysmap_end), _p(vstartinfo_start), _p(vstartinfo_end), _p(vpt_start), _p(vpt_end), _p(vstack_start), _p(vstack_end), - _p(dsi.v_start), _p(v_end)); - printk(" ENTRY ADDRESS: %p\n", _p(dsi.v_kernentry)); + _p(v_start), _p(v_end)); + printk(" ENTRY ADDRESS: %p\n", _p(parms.virt_entry)); - if ( ((v_end - dsi.v_start)>>PAGE_SHIFT) > nr_pages ) + if ( ((v_end - v_start)>>PAGE_SHIFT) > nr_pages ) { printk("Initial guest OS requires too much space\n" "(%luMB is greater than %luMB limit)\n", - (v_end-dsi.v_start)>>20, nr_pages>>(20-PAGE_SHIFT)); + (v_end-v_start)>>20, nr_pages>>(20-PAGE_SHIFT)); return -ENOMEM; } - mpt_alloc = (vpt_start - dsi.v_start) + + mpt_alloc = (vpt_start - v_start) + (unsigned long)pfn_to_paddr(alloc_spfn); #if defined(__i386__) @@ -447,7 +413,7 @@ int construct_dom0(struct domain *d, * Protect the lowest 1GB of memory. We use a temporary mapping there * from which we copy the kernel and ramdisk images. */ - if ( dsi.v_start < (1UL<<30) ) + if ( v_start < (1UL<<30) ) { printk("Initial loading isn''t allowed to lowest 1GB of memory.\n"); return -EINVAL; @@ -477,9 +443,9 @@ int construct_dom0(struct domain *d, l2e_from_page(virt_to_page(d->arch.mm_perdomain_pt) + i, __PAGE_HYPERVISOR); - l2tab += l2_linear_offset(dsi.v_start); + l2tab += l2_linear_offset(v_start); mfn = alloc_spfn; - for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ ) + for ( count = 0; count < ((v_end-v_start)>>PAGE_SHIFT); count++ ) { if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) ) { @@ -489,7 +455,7 @@ int construct_dom0(struct domain *d, l2tab++; clear_page(l1tab); if ( count == 0 ) - l1tab += l1_table_offset(dsi.v_start); + l1tab += l1_table_offset(v_start); } *l1tab = l1e_from_pfn(mfn, L1_PROT); l1tab++; @@ -578,7 +544,7 @@ int construct_dom0(struct domain *d, #elif defined(__x86_64__) /* Overlap with Xen protected area? */ - if ( (dsi.v_start < HYPERVISOR_VIRT_END) && + if ( (v_start < HYPERVISOR_VIRT_END) && (v_end > HYPERVISOR_VIRT_START) ) { printk("DOM0 image overlaps with Xen private area.\n"); @@ -595,9 +561,9 @@ int construct_dom0(struct domain *d, l4e_from_paddr(__pa(d->arch.mm_perdomain_l3), __PAGE_HYPERVISOR); v->arch.guest_table = pagetable_from_paddr(__pa(l4start)); - l4tab += l4_table_offset(dsi.v_start); + l4tab += l4_table_offset(v_start); mfn = alloc_spfn; - for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ ) + for ( count = 0; count < ((v_end-v_start)>>PAGE_SHIFT); count++ ) { if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) ) { @@ -605,14 +571,14 @@ int construct_dom0(struct domain *d, l1start = l1tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE; clear_page(l1tab); if ( count == 0 ) - l1tab += l1_table_offset(dsi.v_start); + l1tab += l1_table_offset(v_start); if ( !((unsigned long)l2tab & (PAGE_SIZE-1)) ) { maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l2_page_table; l2start = l2tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE; clear_page(l2tab); if ( count == 0 ) - l2tab += l2_table_offset(dsi.v_start); + l2tab += l2_table_offset(v_start); if ( !((unsigned long)l3tab & (PAGE_SIZE-1)) ) { maddr_to_page(mpt_alloc)->u.inuse.type_info @@ -620,7 +586,7 @@ int construct_dom0(struct domain *d, l3start = l3tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE; clear_page(l3tab); if ( count == 0 ) - l3tab += l3_table_offset(dsi.v_start); + l3tab += l3_table_offset(v_start); *l4tab = l4e_from_paddr(__pa(l3start), L4_PROT); l4tab++; } @@ -706,20 +672,19 @@ int construct_dom0(struct domain *d, write_ptbase(v); /* Copy the OS image and free temporary buffer. */ - (void)loadelfimage(&dsi); + elf.dest = (void*)vkern_start; + elf_load_binary(&elf); - hypercall_page - xen_elfnote_numeric(&dsi, XEN_ELFNOTE_HYPERCALL_PAGE, &hypercall_page_defined); - if ( hypercall_page_defined ) + if ( UNSET_ADDR != parms.virt_hypercall ) { - if ( (hypercall_page < dsi.v_start) || (hypercall_page >= v_end) ) + if ( (parms.virt_hypercall < v_start) || (parms.virt_hypercall >= v_end) ) { write_ptbase(current); local_irq_enable(); printk("Invalid HYPERCALL_PAGE field in ELF notes.\n"); return -1; } - hypercall_page_initialise(d, (void *)hypercall_page); + hypercall_page_initialise(d, (void*)(unsigned long)parms.virt_hypercall); } /* Copy the initial ramdisk. */ @@ -742,14 +707,14 @@ int construct_dom0(struct domain *d, si->mfn_list = vphysmap_start; sprintf(si->magic, "xen-%i.%i-x86_%d%s", xen_major_version(), xen_minor_version(), - BITS_PER_LONG, xen_pae ? "p" : ""); + BITS_PER_LONG, parms.pae ? "p" : ""); /* Write the phys->machine and machine->phys table entries. */ for ( pfn = 0; pfn < d->tot_pages; pfn++ ) { mfn = pfn + alloc_spfn; #ifndef NDEBUG -#define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT) +#define REVERSE_START ((v_end - v_start) >> PAGE_SHIFT) if ( pfn > REVERSE_START ) mfn = alloc_epfn - (pfn - REVERSE_START); #endif @@ -817,7 +782,7 @@ int construct_dom0(struct domain *d, regs->ds = regs->es = regs->fs = regs->gs = FLAT_KERNEL_DS; regs->ss = FLAT_KERNEL_SS; regs->cs = FLAT_KERNEL_CS; - regs->eip = dsi.v_kernentry; + regs->eip = parms.virt_entry; regs->esp = vstack_end; regs->esi = vstartinfo_start; regs->eflags = X86_EFLAGS_IF; @@ -888,26 +853,6 @@ int construct_dom0(struct domain *d, return 0; } -int elf_sanity_check(Elf_Ehdr *ehdr) -{ - if ( !IS_ELF(*ehdr) || -#if defined(__i386__) - (ehdr->e_ident[EI_CLASS] != ELFCLASS32) || - (ehdr->e_machine != EM_386) || -#elif defined(__x86_64__) - (ehdr->e_ident[EI_CLASS] != ELFCLASS64) || - (ehdr->e_machine != EM_X86_64) || -#endif - (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) || - (ehdr->e_type != ET_EXEC) ) - { - printk("DOM0 image is not a Xen-compatible Elf image.\n"); - return 0; - } - - return 1; -} - /* * Local variables: * mode: C -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
This patch makes libelf available to the tools, by symlinking the source files and compiling them into libxc. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- tools/libxc/Makefile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) Index: build-32-unstable-12621/tools/libxc/Makefile ==================================================================--- build-32-unstable-12621.orig/tools/libxc/Makefile +++ 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 -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 05/10] libelf: use for hvm builder.
This patch switches over the hvm domain builder to libelf (for loading hvmloader). Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- tools/libxc/xc_hvm_build.c | 215 +++++++++++++-------------------------------- 1 file changed, 65 insertions(+), 150 deletions(-) Index: build-32-unstable-12621/tools/libxc/xc_hvm_build.c ==================================================================--- build-32-unstable-12621.orig/tools/libxc/xc_hvm_build.c +++ 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 -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 06/10] libelf: use for readnotes utility.
This patch makes the readnotes utility use libelf. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- tools/xcutils/readnotes.c | 277 +++++++--------------------------------------- 1 file changed, 45 insertions(+), 232 deletions(-) Index: build-32-unstable-12621/tools/xcutils/readnotes.c ==================================================================--- build-32-unstable-12621.orig/tools/xcutils/readnotes.c +++ 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; } -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
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. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- tools/libxc/xg_private.h | 114 +++++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 47 deletions(-) Index: build-32-unstable-11822/tools/libxc/xg_private.h ==================================================================--- build-32-unstable-11822.orig/tools/libxc/xg_private.h +++ 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; -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 08/10] libxc domain builder rewrite, core bits.
Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- 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(+) Index: build-32-unstable-12621/tools/libxc/Makefile ==================================================================--- build-32-unstable-12621.orig/tools/libxc/Makefile +++ 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 Index: build-32-unstable-12621/tools/libxc/xc_dom.h ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom.h @@ -0,0 +1,267 @@ +#include <xen/libelf.h> + +#ifdef XEN_DOMINF_hvm_guest /* new after 3.0.3 */ +# define XC_UNSTABLE 1 +#endif + +#define INVALID_P2M_ENTRY ((xen_pfn_t)-1) + +/* --- typedefs and structs ---------------------------------------- */ + +typedef uint64_t xen_vaddr_t; +typedef uint64_t xen_paddr_t; + +/* FIXME: temporary hack ... */ +#ifndef PRIpfn +#define PRIpfn "lx" +#endif + +struct xc_dom_seg { + xen_vaddr_t vstart; + xen_vaddr_t vend; + xen_pfn_t pfn; +}; + +struct xc_dom_mem { + struct xc_dom_mem *next; + void *mmap_ptr; + size_t mmap_len; + unsigned char memory[0]; +}; + +struct xc_dom_phys { + struct xc_dom_phys *next; + void *ptr; + xen_pfn_t first; + xen_pfn_t count; +}; + +struct xc_dom_image { + /* files */ + void *kernel_blob; + size_t kernel_size; + void *ramdisk_blob; + size_t ramdisk_size; + + /* arguments and parameters */ + char *cmdline; + uint32_t f_requested[XENFEAT_NR_SUBMAPS]; + + /* info from (elf) kernel image */ + struct elf_dom_parms parms; + char *guest_type; + + /* memory layout */ + struct xc_dom_seg kernel_seg; + struct xc_dom_seg ramdisk_seg; + struct xc_dom_seg p2m_seg; + struct xc_dom_seg pgtables_seg; + xen_pfn_t start_info_pfn; + xen_pfn_t console_pfn; + xen_pfn_t xenstore_pfn; + xen_pfn_t shared_info_pfn; + xen_pfn_t bootstack_pfn; + xen_vaddr_t virt_alloc_end; + xen_vaddr_t bsd_symtab_start; + + /* initial page tables */ + unsigned int pgtables; + unsigned int pg_l4; + unsigned int pg_l3; + unsigned int pg_l2; + unsigned int pg_l1; + unsigned int alloc_bootstack; + unsigned int extra_pages; + xen_vaddr_t virt_pgtab_end; + + /* other state info */ + uint32_t f_active[XENFEAT_NR_SUBMAPS]; + xen_pfn_t *p2m_host; + void *p2m_guest; + + /* physical memory */ + xen_pfn_t total_pages; + struct xc_dom_phys *phys_pages; + + /* malloc memory pool */ + struct xc_dom_mem *memblocks; + + /* memory footprint stats */ + size_t alloc_malloc; + size_t alloc_mem_map; + size_t alloc_file_map; + size_t alloc_domU_map; + + /* misc xen domain config stuff */ + unsigned long flags; + unsigned int console_evtchn; + unsigned int xenstore_evtchn; + xen_pfn_t shared_info_mfn; + int guest_xc; + domid_t guest_domid; + + /* kernel loader, arch hooks */ + struct xc_dom_loader *kernel_loader; + void *private_loader; + + /* kernel loader */ + struct xc_dom_arch *arch_hooks; +}; + +/* --- pluggable kernel loader ------------------------------------- */ + +struct xc_dom_loader { + char *name; + int (*probe) (struct xc_dom_image * dom); + int (*parser) (struct xc_dom_image * dom); + int (*loader) (struct xc_dom_image * dom); + + struct xc_dom_loader *next; +}; + +#define __init __attribute__ ((constructor)) +void xc_dom_register_loader(struct xc_dom_loader *loader); + +/* --- arch specific hooks ----------------------------------------- */ + +struct xc_dom_arch { + /* pagetable setup */ + int (*alloc_magic_pages) (struct xc_dom_image * dom); + int (*count_pgtables) (struct xc_dom_image * dom); + int (*setup_pgtables) (struct xc_dom_image * dom); + + /* arch-specific data structs setup */ + int (*start_info) (struct xc_dom_image * dom, int xen_version); + int (*shared_info) (struct xc_dom_image * dom, void *shared_info); + int (*vcpu) (struct xc_dom_image * dom, void *vcpu_ctxt); + + char *guest_type; + int page_shift; + int sizeof_pfn; + + struct xc_dom_arch *next; +}; +void xc_dom_register_arch_hooks(struct xc_dom_arch *hooks); + +#define XC_DOM_PAGE_SHIFT(dom) ((dom)->arch_hooks->page_shift) +#define XC_DOM_PAGE_SIZE(dom) (1 << (dom)->arch_hooks->page_shift) + +/* --- main functions ---------------------------------------------- */ + +struct xc_dom_image *xc_dom_allocate(const char *cmdline, const char *features); +void xc_dom_release_phys(struct xc_dom_image *dom); +void xc_dom_release(struct xc_dom_image *dom); +int xc_dom_mem_init(struct xc_dom_image *dom, unsigned int mem_mb); + +size_t xc_dom_check_gzip(void *blob, size_t ziplen); +int xc_dom_do_gunzip(void *src, size_t srclen, void *dst, size_t dstlen); +int xc_dom_try_gunzip(struct xc_dom_image *dom, void **blob, size_t * size); + +int xc_dom_kernel_file(struct xc_dom_image *dom, const char *filename); +int xc_dom_ramdisk_file(struct xc_dom_image *dom, const char *filename); +int xc_dom_kernel_mem(struct xc_dom_image *dom, const void *mem, + size_t memsize); +int xc_dom_ramdisk_mem(struct xc_dom_image *dom, const void *mem, + size_t memsize); + +int xc_dom_parse_image(struct xc_dom_image *dom); +int xc_dom_build_image(struct xc_dom_image *dom); +int xc_dom_update_guest_p2m(struct xc_dom_image *dom); + +int xc_dom_boot_init(struct xc_dom_image *dom, int xc, domid_t domid); +void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn, + xen_pfn_t count); +int xc_dom_boot_image(struct xc_dom_image *dom); + +/* --- debugging bits ---------------------------------------------- */ + +typedef enum { + XC_ERROR_NONE = 0, + XC_INTERNAL_ERROR = 1, + XC_INVALID_KERNEL = 2, + XC_INVALID_PARAM = 3, + XC_OUT_OF_MEMORY = 4, +} xc_errno; + +extern FILE *xc_dom_logfile; +xc_errno xc_error; +char xc_errstr[1024]; + +void xc_dom_loginit(void); +int xc_dom_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2))); +int xc_dom_panic_func(const char *file, int line, xc_errno err, + const char *fmt, ...) + __attribute__ ((format(printf, 4, 5))); +#define xc_dom_panic(err, fmt, args...) \ + xc_dom_panic_func(__FILE__, __LINE__, err, fmt, ## args) +#define xc_dom_trace(mark) \ + xc_dom_printf("%s:%d: trace %s\n", __FILE__, __LINE__, mark) + +void xc_dom_log_memory_footprint(struct xc_dom_image *dom); + +/* --- simple memory pool ------------------------------------------ */ + +void *xc_dom_malloc(struct xc_dom_image *dom, size_t size); +void *xc_dom_malloc_page_aligned(struct xc_dom_image *dom, size_t size); +void *xc_dom_malloc_filemap(struct xc_dom_image *dom, + const char *filename, size_t * size); +char *xc_dom_strdup(struct xc_dom_image *dom, const char *str); + +/* --- alloc memory pool ------------------------------------------- */ + +int xc_dom_alloc_page(struct xc_dom_image *dom, char *name); +int xc_dom_alloc_segment(struct xc_dom_image *dom, + struct xc_dom_seg *seg, char *name, + xen_vaddr_t start, xen_vaddr_t size); + +/* --- misc bits --------------------------------------------------- */ + +void *xc_dom_pfn_to_ptr(struct xc_dom_image *dom, xen_pfn_t first, + xen_pfn_t count); +void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn); +void xc_dom_unmap_all(struct xc_dom_image *dom); + +static inline void *xc_dom_seg_to_ptr(struct xc_dom_image *dom, + struct xc_dom_seg *seg) +{ + xen_vaddr_t segsize = seg->vend - seg->vstart; + unsigned int page_size = XC_DOM_PAGE_SIZE(dom); + xen_pfn_t pages = (segsize + page_size - 1) / page_size; + + return xc_dom_pfn_to_ptr(dom, seg->pfn, pages); +} + +static inline void *xc_dom_vaddr_to_ptr(struct xc_dom_image *dom, + xen_vaddr_t vaddr) +{ + unsigned int page_size = XC_DOM_PAGE_SIZE(dom); + xen_pfn_t page = (vaddr - dom->parms.virt_base) / page_size; + unsigned int offset = (vaddr - dom->parms.virt_base) % page_size; + void *ptr = xc_dom_pfn_to_ptr(dom, page, 0); + + if (!ptr) + return NULL; + return ptr + offset; +} + +static inline int xc_dom_feature_translated(struct xc_dom_image *dom) +{ + return elf_xen_feature_get(XENFEAT_auto_translated_physmap, dom->f_active); +} + +static inline xen_pfn_t xc_dom_p2m_host(struct xc_dom_image *dom, xen_pfn_t pfn) +{ + return dom->p2m_host[pfn]; +} + +static inline xen_pfn_t xc_dom_p2m_guest(struct xc_dom_image *dom, + xen_pfn_t pfn) +{ + if (xc_dom_feature_translated(dom)) + return pfn; + return xc_dom_p2m_host(dom, pfn); +} + +/* --- arch bits --------------------------------------------------- */ + +int xc_dom_compat_check(struct xc_dom_image *dom, int xc); Index: build-32-unstable-12621/tools/libxc/xc_dom_boot.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_boot.c @@ -0,0 +1,514 @@ +/* + * Xen domain builder -- xen booter. + * + * This is the code which actually boots a fresh + * prepared domain image as xen guest domain. + * + * ==> this is the only domain bilder code piece + * where xen hypercalls are allowed <=+ * + * This code is licenced under the GPL. + * written 2006 by Gerd Hoffmann <kraxel@suse.de>. + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <inttypes.h> +#include <zlib.h> + +#include "xg_private.h" +#include "xc_dom.h" +#include <xen/hvm/params.h> + +/* ------------------------------------------------------------------------ */ + +static int setup_hypercall_page(struct xc_dom_image *dom) +{ + struct xen_domctl domctl; + xen_pfn_t pfn; + int rc; + + if (-1 == dom->parms.virt_hypercall) + return 0; + pfn = (dom->parms.virt_hypercall - dom->parms.virt_base) + >> XC_DOM_PAGE_SHIFT(dom); + + xc_dom_printf("%s: vaddr=0x%" PRIx64 " pfn=0x%" PRIpfn "\n", __FUNCTION__, + dom->parms.virt_hypercall, pfn); + domctl.cmd = XEN_DOMCTL_hypercall_init; + domctl.domain = dom->guest_domid; + domctl.u.hypercall_init.gmfn = xc_dom_p2m_guest(dom, pfn); + rc = xc_domctl(dom->guest_xc, &domctl); + if (0 != rc) + xc_dom_panic(XC_INTERNAL_ERROR, "%s: HYPERCALL_INIT failed (rc=%d)\n", + __FUNCTION__, rc); + return rc; +} + +static int launch_vm(int xc, domid_t domid, void *ctxt) +{ + struct xen_domctl domctl; + int rc; + + xc_dom_printf("%s: called, ctxt=%p\n", __FUNCTION__, ctxt); + memset(&domctl, 0, sizeof(domctl)); + domctl.cmd = XEN_DOMCTL_setvcpucontext; + domctl.domain = domid; + domctl.u.vcpucontext.vcpu = 0; + set_xen_guest_handle(domctl.u.vcpucontext.ctxt, ctxt); + rc = xc_domctl(xc, &domctl); + if (0 != rc) + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: SETVCPUCONTEXT failed (rc=%d)\n", __FUNCTION__, rc); + return rc; +} + +static int clear_page(struct xc_dom_image *dom, xen_pfn_t pfn) +{ + xen_pfn_t dst; + int rc; + + if (0 == pfn) + return 0; + + dst = xc_dom_p2m_host(dom, pfn); + xc_dom_printf("%s: pfn 0x%" PRIpfn ", mfn 0x%" PRIpfn "\n", + __FUNCTION__, pfn, dst); + rc = xc_clear_domain_page(dom->guest_xc, dom->guest_domid, dst); + if (0 != rc) + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: xc_clear_domain_page failed (pfn 0x%" PRIpfn + ", rc=%d)\n", __FUNCTION__, pfn, rc); + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* arch stuff: x86 bits */ + +#if defined(__i386__) || defined(__x86_64__) + +static int x86_shadow(int xc, domid_t domid) +{ + int rc, mode; + + mode = XEN_DOMCTL_SHADOW_ENABLE_REFCOUNT | + XEN_DOMCTL_SHADOW_ENABLE_TRANSLATE; + + rc = xc_shadow_control(xc, domid, + XEN_DOMCTL_SHADOW_OP_ENABLE, + NULL, 0, NULL, mode, NULL); + if (0 != rc) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: SHADOW_OP_ENABLE (mode=0x%x) failed (rc=%d)\n", + __FUNCTION__, mode, rc); + return rc; + } + xc_dom_printf("%s: shadow enabled (mode=0x%x)\n", __FUNCTION__, mode); + return rc; +} + +static int arch_setup_early(struct xc_dom_image *dom) +{ + int rc = 0; + + if (xc_dom_feature_translated(dom)) + rc = x86_shadow(dom->guest_xc, dom->guest_domid); + return rc; +} + +static int arch_setup_late(struct xc_dom_image *dom) +{ + static const struct { + char *guest; + unsigned long pgd_type; + } types[] = { + { "xen-3.0-x86_32", MMUEXT_PIN_L2_TABLE}, + { "xen-3.0-x86_32p", MMUEXT_PIN_L3_TABLE}, + { "xen-3.0-x86_64", MMUEXT_PIN_L4_TABLE}, + }; + unsigned long pgd_type = 0; + shared_info_t *shared_info; + int i, rc; + + for (i = 0; i < sizeof(types) / sizeof(types[0]); i++) + if (0 == strcmp(types[i].guest, dom->guest_type)) + pgd_type = types[i].pgd_type; + + if (!xc_dom_feature_translated(dom)) + { + /* paravirtualized guest */ + xc_dom_unmap_one(dom, dom->pgtables_seg.pfn); + rc = pin_table(dom->guest_xc, pgd_type, + xc_dom_p2m_host(dom, dom->pgtables_seg.pfn), + dom->guest_domid); + if (0 != rc) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: pin_table failed (pfn 0x%" PRIpfn ", rc=%d)\n", + __FUNCTION__, dom->pgtables_seg.pfn, rc); + return rc; + } + } + else + { + /* paravirtualized guest with auto-translation */ + struct xen_add_to_physmap xatp; + int i; + + /* Map shared info frame into guest physmap. */ + xatp.domid = dom->guest_domid; + xatp.space = XENMAPSPACE_shared_info; + xatp.idx = 0; + xatp.gpfn = dom->shared_info_pfn; + rc = xc_memory_op(dom->guest_xc, XENMEM_add_to_physmap, &xatp); + if (rc != 0) + { + xc_dom_panic(XC_INTERNAL_ERROR, "%s: mapping shared_info failed " + "(pfn=0x%" PRIpfn ", rc=%d)\n", + __FUNCTION__, xatp.gpfn, rc); + return rc; + } + + /* Map grant table frames into guest physmap. */ + for (i = 0;; i++) + { + xatp.domid = dom->guest_domid; + xatp.space = XENMAPSPACE_grant_table; + xatp.idx = i; + xatp.gpfn = dom->total_pages + i; + rc = xc_memory_op(dom->guest_xc, XENMEM_add_to_physmap, &xatp); + if (rc != 0) + { + if (i > 0 && errno == EINVAL) + { + xc_dom_printf("%s: %d grant tables mapped\n", __FUNCTION__, + i); + break; + } + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: mapping grant tables failed " "(pfn=0x%" + PRIpfn ", rc=%d)\n", __FUNCTION__, xatp.gpfn, rc); + return rc; + } + } + } + + /* setup shared_info page */ + xc_dom_printf("%s: shared_info: pfn 0x%" PRIpfn ", mfn 0x%" PRIpfn "\n", + __FUNCTION__, dom->shared_info_pfn, dom->shared_info_mfn); + shared_info = xc_map_foreign_range(dom->guest_xc, dom->guest_domid, + PAGE_SIZE_X86, + PROT_READ | PROT_WRITE, + dom->shared_info_mfn); + dom->arch_hooks->shared_info(dom, shared_info); + munmap(shared_info, PAGE_SIZE_X86); + + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* arch stuff: ia64 */ + +#elif defined(__ia64__) + +static int arch_setup_early(struct xc_dom_image *dom) +{ + DECLARE_DOMCTL; + int rc; + + xc_dom_printf("%s: setup firmware\n", __FUNCTION__); + + memset(&domctl, 0, sizeof(domctl)); + domctl.cmd = XEN_DOMCTL_arch_setup; + domctl.domain = dom->guest_domid; + domctl.u.arch_setup.flags = 0; + domctl.u.arch_setup.bp = (dom->start_info_pfn << PAGE_SHIFT) + + sizeof(start_info_t); + domctl.u.arch_setup.maxmem = dom->low_top << PAGE_SHIFT; + rc = xc_domctl(dom->guest_xc, &domctl); + return rc; +} + +static int arch_setup_late(struct xc_dom_image *dom) +{ + xc_dom_printf("%s: doing nothing\n", __FUNCTION__); + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* arch stuff: powerpc */ + +#elif defined(__powerpc64__) + +static int arch_setup_early(struct xc_dom_image *dom) +{ + xc_dom_printf("%s: doing nothing\n", __FUNCTION__); + return 0; +} + +static int arch_setup_late(struct xc_dom_image *dom) +{ + start_info_t *si + xc_dom_pfn_to_ptr(dom, dom->start_info_pfn, 1); + + xc_dom_printf("%s: TODO: setup devtree\n", __FUNCTION__); + +#if 0 + load_devtree(dom->guest_xc, + dom->guest_domid, + dom->p2m_host, + devtree, // FIXME + devtree_addr, // FIXME + dom->ramdisk_seg.vstart, + dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart, + si, + dom->start_info_pfn << PAGE_SHIFT); +#endif + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* arch stuff: other */ + +#else + +static int arch_setup_early(struct xc_dom_image *dom) +{ + xc_dom_printf("%s: doing nothing\n", __FUNCTION__); + return 0; +} + +static int arch_setup_late(struct xc_dom_image *dom) +{ + xc_dom_printf("%s: doing nothing\n", __FUNCTION__); + return 0; +} + +#endif /* arch stuff */ + +/* ------------------------------------------------------------------------ */ + +int xc_dom_compat_check(struct xc_dom_image *dom, int xc) +{ + xen_capabilities_info_t xen_caps = ""; + char *item, *ptr; + int match, found = 0, rc; + + rc = xc_version(xc, XENVER_capabilities, &xen_caps); + if (0 != rc) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: get XENVER_capabilities failed (rc=%d)\n", + __FUNCTION__, rc); + goto out; + } + + for (item = strtok_r(xen_caps, " ", &ptr); + NULL != item; item = strtok_r(NULL, " ", &ptr)) + { + match = (0 == strcmp(dom->guest_type, item)); + xc_dom_printf("%s: supported guest type: %s%s\n", __FUNCTION__, + item, match ? " <= matches" : ""); + if (match) + found++; + } + if (!found) + xc_dom_panic(XC_INVALID_KERNEL, + "%s: guest type %s not supported by xen kernel, sorry\n", + __FUNCTION__, dom->guest_type); + + out: + return found; +} + +int xc_dom_boot_init(struct xc_dom_image *dom, int xc, domid_t domid) +{ + long rc; + +#ifndef XC_UNSTABLE + xen_paddr_t entry; + xen_pfn_t pfn, pages; + xc_mmu_t *mmu; +#endif + + xc_dom_printf("%s: called\n", __FUNCTION__); + dom->guest_xc = xc; + dom->guest_domid = domid; + +#ifdef XC_UNSTABLE + + /* allocate guest memory */ + rc = xc_domain_memory_populate_physmap(xc, domid, dom->total_pages, 0, 0, + dom->p2m_host); + if (0 != rc) + { + xc_dom_panic(XC_OUT_OF_MEMORY, + "%s: can''t allocate low memory for domain\n", + __FUNCTION__); + return rc; + } + +#else /* TESTING */ + + /* get guest page frame numbers */ + rc = xc_get_pfn_list(xc, domid, dom->p2m_host, dom->total_pages); + if (dom->total_pages != rc) + { + xc_dom_panic(XC_INTERNAL_ERROR, "%s: xc_get_pfn_list failed " + "(0x%" PRIpfn "/0x%" PRIpfn ")\n", + __FUNCTION__, pages, dom->total_pages); + return -1; + } + + /* setup machphys entries */ + mmu = xc_init_mmu_updates(dom->guest_xc, dom->guest_domid); + if (NULL == mmu) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: xc_init_mmu_updates failed\n", __FUNCTION__); + return -1; + } + + for (pfn = 0; pfn < dom->total_pages; pfn++) + { + entry = ((uint64_t) (dom->p2m_host[pfn]) << page_shift) | + MMU_MACHPHYS_UPDATE; + rc = xc_add_mmu_update(dom->guest_xc, mmu, entry, pfn); + if (0 != rc) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: xc_add_mmu_update failed (rc=%ld)\n", + __FUNCTION__, rc); + return rc; + } + } + + rc = xc_finish_mmu_updates(dom->guest_xc, mmu); + if (0 != rc) + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: xc_finish_mmu_updates failed (rc=%ld)\n", + __FUNCTION__, rc); + return rc; + +#endif + + return 0; +} + +void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn, + xen_pfn_t count) +{ + int page_shift = XC_DOM_PAGE_SHIFT(dom); + privcmd_mmap_entry_t *entries; + void *ptr; + int i, rc; + + entries = xc_dom_malloc(dom, count * sizeof(privcmd_mmap_entry_t)); + if (NULL == entries) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: failed to mmap domU pages 0x%" PRIpfn "+0x%" PRIpfn + " [malloc]\n", __FUNCTION__, pfn, count); + return NULL; + } + + ptr = mmap(NULL, count << page_shift, PROT_READ | PROT_WRITE, + MAP_SHARED, dom->guest_xc, 0); + if (MAP_FAILED == ptr) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: failed to mmap domU pages 0x%" PRIpfn "+0x%" PRIpfn + " [mmap]\n", __FUNCTION__, pfn, count); + return NULL; + } + + for (i = 0; i < count; i++) + { + entries[i].va = (uintptr_t) ptr + (i << page_shift); + entries[i].mfn = dom->p2m_host[pfn + i]; + entries[i].npages = 1; + } + + rc = xc_map_foreign_ranges(dom->guest_xc, dom->guest_domid, entries, count); + if (rc < 0) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: failed to mmap domU pages 0x%" PRIpfn "+0x%" PRIpfn + " [xenctl, rc=%d]\n", __FUNCTION__, pfn, count, rc); + return NULL; + } + return ptr; +} + +int xc_dom_boot_image(struct xc_dom_image *dom) +{ + struct xen_domctl domctl; + int rc, xen_version; + void *ctxt; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* collect some info */ + xen_version = xc_version(dom->guest_xc, XENVER_version, NULL); + + domctl.cmd = XEN_DOMCTL_getdomaininfo; + domctl.domain = dom->guest_domid; + rc = xc_domctl(dom->guest_xc, &domctl); + if (0 != rc) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: getdomaininfo failed (rc=%d)\n", __FUNCTION__, rc); + return rc; + } + if (domctl.domain != dom->guest_domid) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: Huh? domid mismatch (%d != %d)\n", __FUNCTION__, + domctl.domain, dom->guest_domid); + return -1; + } + dom->shared_info_mfn = domctl.u.getdomaininfo.shared_info_frame; + + /* sanity checks */ + if (!xc_dom_compat_check(dom, dom->guest_xc)) + return -1; + + /* initial mm setup */ + if (0 != (rc = arch_setup_early(dom))) + return rc; + if (0 != (rc = xc_dom_update_guest_p2m(dom))) + return rc; + if (dom->arch_hooks->setup_pgtables) + if (0 != (rc = dom->arch_hooks->setup_pgtables(dom))) + return rc; + + if (0 != (rc = clear_page(dom, dom->console_pfn))) + return rc; + if (0 != (rc = clear_page(dom, dom->xenstore_pfn))) + return rc; + + /* start info page */ + if (dom->arch_hooks->start_info) + dom->arch_hooks->start_info(dom, xen_version); + + /* hypercall page */ + if (0 != (rc = setup_hypercall_page(dom))) + return rc; + xc_dom_log_memory_footprint(dom); + + /* misc x86 stuff */ + if (0 != (rc = arch_setup_late(dom))) + return rc; + + /* let the vm run */ + ctxt = xc_dom_malloc(dom, PAGE_SIZE * 2 /* FIXME */ ); + memset(ctxt, 0, PAGE_SIZE * 2); + if (0 != (rc = dom->arch_hooks->vcpu(dom, ctxt))) + return rc; + xc_dom_unmap_all(dom); + rc = launch_vm(dom->guest_xc, dom->guest_domid, ctxt); + + return rc; +} Index: build-32-unstable-12621/tools/libxc/xc_dom_core.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_core.c @@ -0,0 +1,775 @@ +/* + * Xen domain builder -- core bits. + * + * The core code goes here: + * - allocate and release domain structs. + * - memory management functions. + * - misc helper functions. + * + * This code is licenced under the GPL. + * written 2006 by Gerd Hoffmann <kraxel@suse.de>. + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <inttypes.h> +#include <zlib.h> + +#include "xg_private.h" +#include "xc_dom.h" + +/* ------------------------------------------------------------------------ */ +/* debugging */ + +FILE *xc_dom_logfile = NULL; + +xc_errno xc_error; +char xc_errstr[1024]; + +void xc_dom_loginit(void) +{ + if (xc_dom_logfile) + return; + xc_dom_logfile = fopen("/var/log/xen/domain-builder-ng.log", "a"); + setvbuf(xc_dom_logfile, NULL, _IONBF, 0); + xc_dom_printf("### ----- xc domain builder logfile opened -----\n"); +} + +int xc_dom_printf(const char *fmt, ...) +{ + va_list args; + char buf[1024]; + int rc; + + if (!xc_dom_logfile) + return 0; + + va_start(args, fmt); + rc = vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + rc = fwrite(buf, rc, 1, xc_dom_logfile); + + return rc; +} + +int xc_dom_panic_func(const char *file, int line, xc_errno err, + const char *fmt, ...) +{ + FILE *fp = stderr; + va_list args; + int rc = 0; + + if (xc_dom_logfile) + fp = xc_dom_logfile; + + xc_error = err; + va_start(args, fmt); + rc += snprintf(xc_errstr + rc, sizeof(xc_errstr) - rc, "%s:%d: panic: ", + file, line); + rc += vsnprintf(xc_errstr + rc, sizeof(xc_errstr) - rc, fmt, args); + va_end(args); + rc = fwrite(xc_errstr, rc, 1, fp); + + return rc; +} + +static void print_mem(const char *name, size_t mem) +{ + if (mem > 32 * 1024 * 1024) + xc_dom_printf("%-24s : %zd MB\n", name, mem / (1024 * 1024)); + else if (mem > 32 * 1024) + xc_dom_printf("%-24s : %zd kB\n", name, mem / 1024); + else + xc_dom_printf("%-24s : %zd bytes\n", name, mem); +} + +void xc_dom_log_memory_footprint(struct xc_dom_image *dom) +{ + xc_dom_printf("domain builder memory footprint\n"); + xc_dom_printf(" allocated\n"); + print_mem(" malloc", dom->alloc_malloc); + print_mem(" anon mmap", dom->alloc_mem_map); + xc_dom_printf(" mapped\n"); + print_mem(" file mmap", dom->alloc_file_map); + print_mem(" domU mmap", dom->alloc_domU_map); +} + +/* ------------------------------------------------------------------------ */ +/* simple memory pool */ + +void *xc_dom_malloc(struct xc_dom_image *dom, size_t size) +{ + struct xc_dom_mem *block; + + block = malloc(sizeof(*block) + size); + if (NULL == block) + return NULL; + memset(block, 0, sizeof(*block) + size); + block->next = dom->memblocks; + dom->memblocks = block; + dom->alloc_malloc += sizeof(*block) + size; + if (size > 100 * 1024) + print_mem(__FUNCTION__, size); + return block->memory; +} + +void *xc_dom_malloc_page_aligned(struct xc_dom_image *dom, size_t size) +{ + struct xc_dom_mem *block; + + block = malloc(sizeof(*block)); + if (NULL == block) + return NULL; + memset(block, 0, sizeof(*block)); + block->mmap_len = size; + block->mmap_ptr = mmap(NULL, block->mmap_len, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + if (MAP_FAILED == block->mmap_ptr) + { + free(block); + return NULL; + } + block->next = dom->memblocks; + dom->memblocks = block; + dom->alloc_malloc += sizeof(*block); + dom->alloc_mem_map += block->mmap_len; + if (size > 100 * 1024) + print_mem(__FUNCTION__, size); + return block->mmap_ptr; +} + +void *xc_dom_malloc_filemap(struct xc_dom_image *dom, + const char *filename, size_t * size) +{ + struct xc_dom_mem *block = NULL; + int fd = -1; + + fd = open(filename, O_RDONLY); + if (-1 == fd) + goto err; + + lseek(fd, 0, SEEK_SET); + *size = lseek(fd, 0, SEEK_END); + + block = malloc(sizeof(*block)); + if (NULL == block) + goto err; + memset(block, 0, sizeof(*block)); + block->mmap_len = *size; + block->mmap_ptr = mmap(NULL, block->mmap_len, PROT_READ, MAP_SHARED, fd, 0); + if (MAP_FAILED == block->mmap_ptr) + goto err; + block->next = dom->memblocks; + dom->memblocks = block; + dom->alloc_malloc += sizeof(*block); + dom->alloc_file_map += block->mmap_len; + close(fd); + if (*size > 100 * 1024) + print_mem(__FUNCTION__, *size); + return block->mmap_ptr; + + err: + if (-1 != fd) + close(fd); + if (block) + free(block); + return NULL; +} + +static void xc_dom_free_all(struct xc_dom_image *dom) +{ + struct xc_dom_mem *block; + + while (NULL != (block = dom->memblocks)) + { + dom->memblocks = block->next; + if (block->mmap_ptr) + munmap(block->mmap_ptr, block->mmap_len); + free(block); + } +} + +char *xc_dom_strdup(struct xc_dom_image *dom, const char *str) +{ + size_t len = strlen(str) + 1; + char *nstr = xc_dom_malloc(dom, len); + + if (NULL == nstr) + return NULL; + memcpy(nstr, str, len); + return nstr; +} + +/* ------------------------------------------------------------------------ */ +/* read files, copy memory blocks, with transparent gunzip */ + +size_t xc_dom_check_gzip(void *blob, size_t ziplen) +{ + unsigned char *gzlen; + size_t unziplen; + + if (0 != strncmp(blob, "\037\213", 2)) + /* not gzipped */ + return 0; + + gzlen = blob + ziplen - 4; + unziplen = gzlen[3] << 24 | gzlen[2] << 16 | gzlen[1] << 8 | gzlen[0]; + if (unziplen < ziplen || unziplen > ziplen * 8) + { + xc_dom_printf + ("%s: size (zip %zd, unzip %zd) looks insane, skip gunzip\n", + __FUNCTION__, ziplen, unziplen); + return 0; + } + + return unziplen + 16; +} + +int xc_dom_do_gunzip(void *src, size_t srclen, void *dst, size_t dstlen) +{ + z_stream zStream; + int rc; + + memset(&zStream, 0, sizeof(zStream)); + zStream.next_in = src; + zStream.avail_in = srclen; + zStream.next_out = dst; + zStream.avail_out = dstlen; + rc = inflateInit2(&zStream, (MAX_WBITS + 32)); /* +32 means "handle gzip" */ + if (rc != Z_OK) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: inflateInit2 failed (rc=%d)\n", __FUNCTION__, rc); + return -1; + } + rc = inflate(&zStream, Z_FINISH); + if (rc != Z_STREAM_END) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: inflate failed (rc=%d)\n", __FUNCTION__, rc); + return -1; + } + + xc_dom_printf("%s: unzip ok, 0x%zx -> 0x%zx\n", + __FUNCTION__, srclen, dstlen); + return 0; +} + +int xc_dom_try_gunzip(struct xc_dom_image *dom, void **blob, size_t * size) +{ + void *unzip; + size_t unziplen; + + unziplen = xc_dom_check_gzip(*blob, *size); + if (0 == unziplen) + return 0; + + unzip = xc_dom_malloc(dom, unziplen); + if (NULL == unzip) + return -1; + + if (-1 == xc_dom_do_gunzip(*blob, *size, unzip, unziplen)) + return -1; + + *blob = unzip; + *size = unziplen; + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* domain memory */ + +void *xc_dom_pfn_to_ptr(struct xc_dom_image *dom, xen_pfn_t pfn, + xen_pfn_t count) +{ + struct xc_dom_phys *phys; + unsigned int page_shift = XC_DOM_PAGE_SHIFT(dom); + char *mode = "unset"; + + if (pfn > dom->total_pages) + { + xc_dom_printf("%s: pfn out of range (0x%" PRIpfn " > 0x%" PRIpfn ")\n", + __FUNCTION__, pfn, dom->total_pages); + return NULL; + } + + /* already allocated? */ + for (phys = dom->phys_pages; NULL != phys; phys = phys->next) + { + if (pfn >= phys->first + phys->count) + continue; + if (count) + { + /* size given: must be completely within the already allocated block */ + if (pfn + count <= phys->first) + continue; + if (pfn < phys->first || pfn + count > phys->first + phys->count) + { + xc_dom_printf("%s: request overlaps allocated block" + " (req 0x%" PRIpfn "+0x%" PRIpfn "," + " blk 0x%" PRIpfn "+0x%" PRIpfn ")\n", + __FUNCTION__, pfn, count, phys->first, + phys->count); + return NULL; + } + } + else + { + /* no size given: block must be allocated already, + just hand out a pointer to it */ + if (pfn < phys->first) + continue; + } + return phys->ptr + ((pfn - phys->first) << page_shift); + } + + /* allocating is allowed with size specified only */ + if (0 == count) + { + xc_dom_printf("%s: no block found, no size given," + " can''t malloc (pfn 0x%" PRIpfn ")\n", __FUNCTION__, pfn); + return NULL; + } + + /* not found, no overlap => allocate */ + phys = xc_dom_malloc(dom, sizeof(*phys)); + if (NULL == phys) + return NULL; + memset(phys, 0, sizeof(*phys)); + phys->first = pfn; + phys->count = count; + + if (dom->guest_domid) + { + mode = "domU mapping"; + phys->ptr = xc_dom_boot_domU_map(dom, phys->first, phys->count); + if (NULL == phys->ptr) + return NULL; + dom->alloc_domU_map += phys->count << page_shift; + } + else + { + mode = "anonymous memory"; + phys->ptr = mmap(NULL, phys->count << page_shift, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + if (MAP_FAILED == phys->ptr) + { + xc_dom_panic(XC_OUT_OF_MEMORY, + "%s: oom: can''t allocate 0x%" PRIpfn " pages\n", + __FUNCTION__, count); + return NULL; + } + dom->alloc_mem_map += phys->count << page_shift; + } + +#if 1 + xc_dom_printf("%s: %s: pfn 0x%" PRIpfn "+0x%" PRIpfn " at %p\n", + __FUNCTION__, mode, phys->first, phys->count, phys->ptr); +#endif + phys->next = dom->phys_pages; + dom->phys_pages = phys; + return phys->ptr; +} + +int xc_dom_alloc_segment(struct xc_dom_image *dom, + struct xc_dom_seg *seg, char *name, + xen_vaddr_t start, xen_vaddr_t size) +{ + unsigned int page_size = XC_DOM_PAGE_SIZE(dom); + xen_pfn_t pages = (size + page_size - 1) / page_size; + void *ptr; + + if (0 == start) + start = dom->virt_alloc_end; + + if (start & (page_size - 1)) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: segment start isn''t page aligned (0x%" PRIx64 ")\n", + __FUNCTION__, start); + return -1; + } + if (start < dom->virt_alloc_end) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: segment start too low (0x%" PRIx64 " < 0x%" PRIx64 + ")\n", __FUNCTION__, start, dom->virt_alloc_end); + return -1; + } + + seg->vstart = start; + seg->vend = start + pages * page_size; + seg->pfn = (seg->vstart - dom->parms.virt_base) / page_size; + dom->virt_alloc_end = seg->vend; + + xc_dom_printf("%-20s: %-12s : 0x%" PRIx64 " -> 0x%" PRIx64 + " (pfn 0x%" PRIpfn " + 0x%" PRIpfn " pages)\n", + __FUNCTION__, name, seg->vstart, seg->vend, seg->pfn, pages); + + /* map pages */ + ptr = xc_dom_seg_to_ptr(dom, seg); + if (NULL == ptr) + return -1; + + return 0; +} + +int xc_dom_alloc_page(struct xc_dom_image *dom, char *name) +{ + unsigned int page_size = XC_DOM_PAGE_SIZE(dom); + xen_vaddr_t start; + xen_pfn_t pfn; + + start = dom->virt_alloc_end; + dom->virt_alloc_end += page_size; + pfn = (start - dom->parms.virt_base) / page_size; + + xc_dom_printf("%-20s: %-12s : 0x%" PRIx64 " (pfn 0x%" PRIpfn ")\n", + __FUNCTION__, name, start, pfn); + return pfn; +} + +void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn) +{ + unsigned int page_shift = XC_DOM_PAGE_SHIFT(dom); + struct xc_dom_phys *phys, *prev = NULL; + + for (phys = dom->phys_pages; NULL != phys; phys = phys->next) + { + if (pfn >= phys->first && pfn < phys->first + phys->count) + break; + prev = phys; + } + if (!phys) + { + xc_dom_printf("%s: Huh? no mapping with pfn 0x%" PRIpfn "\n", + __FUNCTION__, pfn); + return; + } + + munmap(phys->ptr, phys->count << page_shift); + if (prev) + prev->next = phys->next; + else + dom->phys_pages = phys->next; +} + +void xc_dom_unmap_all(struct xc_dom_image *dom) +{ + while (dom->phys_pages) + xc_dom_unmap_one(dom, dom->phys_pages->first); +} + +/* ------------------------------------------------------------------------ */ +/* pluggable kernel loaders */ + +static struct xc_dom_loader *first_loader = NULL; +static struct xc_dom_arch *first_hook = NULL; + +void xc_dom_register_loader(struct xc_dom_loader *loader) +{ + loader->next = first_loader; + first_loader = loader; +} + +static struct xc_dom_loader *xc_dom_find_loader(struct xc_dom_image *dom) +{ + struct xc_dom_loader *loader = first_loader; + + while (NULL != loader) + { + xc_dom_printf("%s: trying %s loader ... ", __FUNCTION__, loader->name); + if (0 == loader->probe(dom)) + { + xc_dom_printf("OK\n"); + return loader; + } + xc_dom_printf("failed\n"); + loader = loader->next; + } + xc_dom_panic(XC_INVALID_KERNEL, "%s: no loader found\n", __FUNCTION__); + return NULL; +} + +void xc_dom_register_arch_hooks(struct xc_dom_arch *hooks) +{ + hooks->next = first_hook; + first_hook = hooks; +} + +static struct xc_dom_arch *xc_dom_find_arch_hooks(char *guest_type) +{ + struct xc_dom_arch *hooks = first_hook; + + while (NULL != hooks) + { + if (0 == strcmp(hooks->guest_type, guest_type)) + return hooks; + hooks = hooks->next; + } + xc_dom_panic(XC_INVALID_KERNEL, + "%s: not found (type %s)\n", __FUNCTION__, guest_type); + return NULL; +} + +/* ------------------------------------------------------------------------ */ +/* public interface */ + +void xc_dom_release(struct xc_dom_image *dom) +{ + xc_dom_printf("%s: called\n", __FUNCTION__); + if (dom->phys_pages) + xc_dom_unmap_all(dom); + xc_dom_free_all(dom); + free(dom); +} + +struct xc_dom_image *xc_dom_allocate(const char *cmdline, const char *features) +{ + struct xc_dom_image *dom; + + xc_dom_printf("%s: cmdline=\"%s\", features=\"%s\"\n", + __FUNCTION__, cmdline, features); + dom = malloc(sizeof(*dom)); + if (!dom) + goto err; + + memset(dom, 0, sizeof(*dom)); + if (cmdline) + dom->cmdline = xc_dom_strdup(dom, cmdline); + if (features) + elf_xen_parse_features(features, dom->f_requested, NULL); + + dom->parms.virt_base = UNSET_ADDR; + dom->parms.virt_entry = UNSET_ADDR; + dom->parms.virt_hypercall = UNSET_ADDR; + dom->parms.virt_hv_start_low = UNSET_ADDR; + dom->parms.elf_paddr_offset = UNSET_ADDR; + + dom->alloc_malloc += sizeof(*dom); + return dom; + + err: + if (dom) + xc_dom_release(dom); + return NULL; +} + +int xc_dom_kernel_file(struct xc_dom_image *dom, const char *filename) +{ + xc_dom_printf("%s: filename=\"%s\"\n", __FUNCTION__, filename); + dom->kernel_blob = xc_dom_malloc_filemap(dom, filename, &dom->kernel_size); + if (NULL == dom->kernel_blob) + return -1; + return xc_dom_try_gunzip(dom, &dom->kernel_blob, &dom->kernel_size); +} + +int xc_dom_ramdisk_file(struct xc_dom_image *dom, const char *filename) +{ + xc_dom_printf("%s: filename=\"%s\"\n", __FUNCTION__, filename); + dom->ramdisk_blob + xc_dom_malloc_filemap(dom, filename, &dom->ramdisk_size); + if (NULL == dom->ramdisk_blob) + return -1; +// return xc_dom_try_gunzip(dom, &dom->ramdisk_blob, &dom->ramdisk_size); + return 0; +} + +int xc_dom_kernel_mem(struct xc_dom_image *dom, const void *mem, size_t memsize) +{ + xc_dom_printf("%s: called\n", __FUNCTION__); + dom->kernel_blob = (void *)mem; + dom->kernel_size = memsize; + return xc_dom_try_gunzip(dom, &dom->kernel_blob, &dom->kernel_size); +} + +int xc_dom_ramdisk_mem(struct xc_dom_image *dom, const void *mem, + size_t memsize) +{ + xc_dom_printf("%s: called\n", __FUNCTION__); + dom->ramdisk_blob = (void *)mem; + dom->ramdisk_size = memsize; +// return xc_dom_try_gunzip(dom, &dom->ramdisk_blob, &dom->ramdisk_size); + return 0; +} + +int xc_dom_parse_image(struct xc_dom_image *dom) +{ + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* parse kernel image */ + dom->kernel_loader = xc_dom_find_loader(dom); + if (NULL == dom->kernel_loader) + goto err; + if (0 != dom->kernel_loader->parser(dom)) + goto err; + if (NULL == dom->guest_type) + { + xc_dom_panic(XC_INTERNAL_ERROR, + "%s: guest_type not set\n", __FUNCTION__); + goto err; + } + + /* check features */ + for (i = 0; i < XENFEAT_NR_SUBMAPS; i++) + { + dom->f_active[i] |= dom->f_requested[i]; /* cmd line */ + dom->f_active[i] |= dom->parms.f_required[i]; /* kernel */ + if ((dom->f_active[i] & dom->parms.f_supported[i]) != dom->f_active[i]) + { + xc_dom_panic(XC_INVALID_PARAM, + "%s: unsupported feature requested\n", __FUNCTION__); + goto err; + } + } + return 0; + + err: + return -1; +} + +int xc_dom_mem_init(struct xc_dom_image *dom, unsigned int mem_mb) +{ + unsigned int page_shift; + xen_pfn_t nr_pages, pfn; + + dom->arch_hooks = xc_dom_find_arch_hooks(dom->guest_type); + if (NULL == dom->arch_hooks) + { + xc_dom_panic(XC_INTERNAL_ERROR, "%s: arch hooks not set\n", + __FUNCTION__); + return -1; + } + + page_shift = XC_DOM_PAGE_SHIFT(dom); + nr_pages = mem_mb << (20 - page_shift); + + xc_dom_printf("%s: mem %d MB, nr_pages 0x%" PRIpfn "\n", __FUNCTION__, + mem_mb, nr_pages); + dom->total_pages = nr_pages; + + xc_dom_printf("%s: 0x%" PRIpfn " pages\n", + __FUNCTION__, dom->total_pages); + + /* setup initial p2m */ + dom->p2m_host = xc_dom_malloc(dom, sizeof(xen_pfn_t) * dom->total_pages); + for (pfn = 0; pfn < dom->total_pages; pfn++) + dom->p2m_host[pfn] = pfn; + return 0; +} + +int xc_dom_update_guest_p2m(struct xc_dom_image *dom) +{ + uint32_t *p2m_32; + uint64_t *p2m_64; + xen_pfn_t i; + + if (!dom->p2m_guest) + return 0; + + switch (dom->arch_hooks->sizeof_pfn) + { + case 4: + xc_dom_printf("%s: dst 32bit, pages 0x%" PRIpfn " \n", + __FUNCTION__, dom->total_pages); + p2m_32 = dom->p2m_guest; + for (i = 0; i < dom->total_pages; i++) + if (INVALID_P2M_ENTRY != dom->p2m_host[i]) + p2m_32[i] = dom->p2m_host[i]; + else + p2m_32[i] = (uint32_t) - 1; + break; + case 8: + xc_dom_printf("%s: dst 64bit, pages 0x%" PRIpfn " \n", + __FUNCTION__, dom->total_pages); + p2m_64 = dom->p2m_guest; + for (i = 0; i < dom->total_pages; i++) + if (INVALID_P2M_ENTRY != dom->p2m_host[i]) + p2m_64[i] = dom->p2m_host[i]; + else + p2m_64[i] = (uint64_t) - 1; + break; + default: + xc_dom_panic(XC_INTERNAL_ERROR, + "sizeof_pfn is invalid (is %d, can be 4 or 8)", + dom->arch_hooks->sizeof_pfn); + return -1; + } + return 0; +} + +int xc_dom_build_image(struct xc_dom_image *dom) +{ + unsigned int page_size; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* check for arch hooks */ + if (NULL == dom->arch_hooks) + { + xc_dom_panic(XC_INTERNAL_ERROR, "%s: arch hooks not set\n", + __FUNCTION__); + goto err; + } + page_size = XC_DOM_PAGE_SIZE(dom); + + /* load kernel */ + if (0 != xc_dom_alloc_segment(dom, &dom->kernel_seg, "kernel", + dom->kernel_seg.vstart, + dom->kernel_seg.vend - + dom->kernel_seg.vstart)) + goto err; + if (0 != dom->kernel_loader->loader(dom)) + goto err; + + /* load ramdisk */ + if (dom->ramdisk_blob) + { + size_t unziplen, ramdisklen; + void *ramdiskmap; + + unziplen = xc_dom_check_gzip(dom->ramdisk_blob, dom->ramdisk_size); + ramdisklen = unziplen ? unziplen : dom->ramdisk_size; + if (0 != xc_dom_alloc_segment(dom, &dom->ramdisk_seg, "ramdisk", 0, + ramdisklen)) + goto err; + ramdiskmap = xc_dom_seg_to_ptr(dom, &dom->ramdisk_seg); + if (unziplen) + { + if (-1 == xc_dom_do_gunzip(dom->ramdisk_blob, dom->ramdisk_size, + ramdiskmap, ramdisklen)) + goto err; + } + else + memcpy(ramdiskmap, dom->ramdisk_blob, dom->ramdisk_size); + } + + /* allocate other pages */ + if (0 != dom->arch_hooks->alloc_magic_pages(dom)) + goto err; + if (dom->arch_hooks->count_pgtables) + { + dom->arch_hooks->count_pgtables(dom); + if (dom->pgtables > 0) + if (0 !+ xc_dom_alloc_segment(dom, &dom->pgtables_seg, "page tables", 0, + dom->pgtables * page_size)) + goto err; + } + if (dom->alloc_bootstack) + dom->bootstack_pfn = xc_dom_alloc_page(dom, "boot stack"); + xc_dom_printf("%-20s: virt_alloc_end : 0x%" PRIx64 "\n", + __FUNCTION__, dom->virt_alloc_end); + xc_dom_printf("%-20s: virt_pgtab_end : 0x%" PRIx64 "\n", + __FUNCTION__, dom->virt_pgtab_end); + return 0; + + err: + return -1; +} Index: build-32-unstable-12621/tools/libxc/xc_dom_ia64.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_ia64.c @@ -0,0 +1,118 @@ +/* + * Xen domain builder -- ia64 bits. + * + * Most architecture-specific code for ia64 goes here. + * - fill architecture-specific structs. + * + * This code is licenced under the GPL. + * written 2006 by Gerd Hoffmann <kraxel@suse.de>. + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <inttypes.h> + +#include <xen/xen.h> +#include <xen/foreign/ia64.h> + +#include "xg_private.h" +#include "xc_dom.h" + +/* ------------------------------------------------------------------------ */ + +static int alloc_magic_pages(struct xc_dom_image *dom) +{ + /* allocate special pages */ + dom->console_pfn = dom->total_pages -1; + dom->xenstore_pfn = dom->total_pages -2; + dom->start_info_pfn = dom->total_pages -3; + return 0; +} + +static int start_info_ia64(struct xc_dom_image *dom, int xen_version) +{ + start_info_ia64_t *start_info + xc_dom_pfn_to_ptr(dom, dom->start_info_pfn, 1); + struct xen_ia64_boot_param_ia64 *bp + (struct xen_ia64_boot_param_ia64 *)(start_info + 1); + + xc_dom_printf("%s\n", __FUNCTION__); + + sprintf(start_info->magic, dom->guest_type); + start_info->flags = dom->flags; + start_info->nr_pages = dom->total_pages; + start_info->store_mfn = dom->xenstore_pfn; + start_info->store_evtchn = dom->xenstore_evtchn; + start_info->console.domU.mfn = dom->console_pfn; + start_info->console.domU.evtchn = dom->console_evtchn; + + if (dom->ramdisk_blob) + { + start_info->mod_start = dom->ramdisk_seg.vstart; + start_info->mod_len = dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart; + bp->initrd_start = start_info->mod_start; + bp->initrd_size = start_info->mod_len; + } + if (dom->cmdline) + { + strncpy((char *)start_info->cmd_line, dom->cmdline, MAX_GUEST_CMDLINE); + start_info->cmd_line[MAX_GUEST_CMDLINE - 1] = ''\0''; + bp->command_line = (dom->start_info_pfn << PAGE_SHIFT_IA64) + + offsetof(start_info_t, cmd_line); + } + return 0; +} + +static int shared_info_ia64(struct xc_dom_image *dom, void *ptr) +{ + shared_info_ia64_t *shared_info = ptr; + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + memset(shared_info, 0, sizeof(*shared_info)); + for (i = 0; i < MAX_VIRT_CPUS; i++) + shared_info->vcpu_info[i].evtchn_upcall_mask = 1; + shared_info->arch.start_info_pfn = dom->start_info_pfn; + return 0; +} + +extern unsigned long xc_ia64_fpsr_default(void); + +static int vcpu_ia64(struct xc_dom_image *dom, void *ptr) +{ + vcpu_guest_context_ia64_t *ctxt = ptr; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* clear everything */ + memset(ctxt, 0, sizeof(*ctxt)); + + ctxt->flags = 0; + ctxt->user_regs.cr_ipsr = 0; /* all necessary bits filled by hypervisor */ + ctxt->user_regs.cr_iip = dom->parms.virt_entry; + ctxt->user_regs.cr_ifs = (uint64_t) 1 << 63; +#ifdef __ia64__ /* FIXME */ + ctxt->user_regs.ar_fpsr = xc_ia64_fpsr_default(); +#endif + ctxt->user_regs.r28 = (dom->start_info_pfn << PAGE_SHIFT_IA64) + + sizeof(start_info_ia64_t); + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static struct xc_dom_arch xc_dom_arch = { + .guest_type = "xen-3.0-ia64", + .page_shift = PAGE_SHIFT_IA64, + .alloc_magic_pages = alloc_magic_pages, + .start_info = start_info_ia64, + .shared_info = shared_info_ia64, + .vcpu = vcpu_ia64, +}; + +static void __init register_arch_hooks(void) +{ + xc_dom_register_arch_hooks(&xc_dom_arch); +} Index: build-32-unstable-12621/tools/libxc/xc_dom_x86.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_x86.c @@ -0,0 +1,542 @@ +/* + * Xen domain builder -- i386 and x86_64 bits. + * + * Most architecture-specific code for x86 goes here. + * - prepare page tables. + * - fill architecture-specific structs. + * + * This code is licenced under the GPL. + * written 2006 by Gerd Hoffmann <kraxel@suse.de>. + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <inttypes.h> + +#include <xen/xen.h> +#include <xen/foreign/x86_32.h> +#include <xen/foreign/x86_64.h> +#include <xen/hvm/hvm_info_table.h> +#include <xen/hvm/e820.h> + +#include "xg_private.h" +#include "xc_dom.h" + +/* ------------------------------------------------------------------------ */ + +#define bits_to_mask(bits) (((xen_vaddr_t)1 << (bits))-1) +#define round_down(addr, mask) ((addr) & ~(mask)) +#define round_up(addr, mask) ((addr) | (mask)) + +static inline unsigned long +nr_page_tables(xen_vaddr_t start, xen_vaddr_t end, unsigned long bits) +{ + xen_vaddr_t mask = bits_to_mask(bits); + int tables; + + if (0 == bits) + return 0; /* unused */ + + if (8 * sizeof(unsigned long) == bits) + { + /* must be pgd, need one */ + start = 0; + end = -1; + tables = 1; + } + else + { + start = round_down(start, mask); + end = round_up(end, mask); + tables = ((end - start) >> bits) + 1; + } + + xc_dom_printf("%s: 0x%016" PRIx64 "/%ld: 0x%016" PRIx64 + " -> 0x%016" PRIx64 ", %d table(s)\n", + __FUNCTION__, mask, bits, start, end, tables); + return tables; +} + +static int count_pgtables(struct xc_dom_image *dom, + int l4_bits, int l3_bits, int l2_bits, int l1_bits) +{ + int pages, extra_pages; + xen_vaddr_t try_virt_end; + + extra_pages = dom->alloc_bootstack ? 1 : 0; + extra_pages += dom->extra_pages; + pages = extra_pages; + for (;;) + { + try_virt_end = round_up(dom->virt_alloc_end + pages * PAGE_SIZE_X86, + bits_to_mask(l1_bits)); + dom->pg_l4 + nr_page_tables(dom->parms.virt_base, try_virt_end, l4_bits); + dom->pg_l3 + nr_page_tables(dom->parms.virt_base, try_virt_end, l3_bits); + dom->pg_l2 + nr_page_tables(dom->parms.virt_base, try_virt_end, l2_bits); + dom->pg_l1 + nr_page_tables(dom->parms.virt_base, try_virt_end, l1_bits); + dom->pgtables = dom->pg_l4 + dom->pg_l3 + dom->pg_l2 + dom->pg_l1; + pages = dom->pgtables + extra_pages; + if (dom->virt_alloc_end + pages * PAGE_SIZE_X86 <= try_virt_end + 1) + break; + } + dom->virt_pgtab_end = try_virt_end + 1; + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* i386 pagetables */ + +#define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED) +#define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER) +#define L3_PROT (_PAGE_PRESENT) + +static int count_pgtables_x86_32(struct xc_dom_image *dom) +{ + return count_pgtables(dom, 0, 0, 32, L2_PAGETABLE_SHIFT_I386); +} + +static int count_pgtables_x86_32_pae(struct xc_dom_image *dom) +{ + return count_pgtables(dom, 0, 32, + L3_PAGETABLE_SHIFT_PAE, L2_PAGETABLE_SHIFT_PAE); +} + +static int setup_pgtables_x86_32(struct xc_dom_image *dom) +{ + xen_pfn_t l2pfn = dom->pgtables_seg.pfn; + xen_pfn_t l1pfn = dom->pgtables_seg.pfn + dom->pg_l2; + l2_pgentry_32_t *l2tab = xc_dom_pfn_to_ptr(dom, l2pfn, 1); + l1_pgentry_32_t *l1tab = NULL; + unsigned long l2off, l1off; + xen_vaddr_t addr; + xen_pfn_t pgpfn; + + for (addr = dom->parms.virt_base; addr < dom->virt_pgtab_end; + addr += PAGE_SIZE_X86) + { + if (NULL == l1tab) + { + /* get L1 tab, make L2 entry */ + l1tab = xc_dom_pfn_to_ptr(dom, l1pfn, 1); + l2off = l2_table_offset_i386(addr); + l2tab[l2off] + (xc_dom_p2m_guest(dom, l1pfn) << PAGE_SHIFT_X86) | L2_PROT; + l1pfn++; + } + + /* make L1 entry */ + l1off = l1_table_offset_i386(addr); + pgpfn = (addr - dom->parms.virt_base) >> PAGE_SHIFT_X86; + l1tab[l1off] + (xc_dom_p2m_guest(dom, pgpfn) << PAGE_SHIFT_X86) | L1_PROT; + if (addr >= dom->pgtables_seg.vstart && addr < dom->pgtables_seg.vend) + l1tab[l1off] &= ~_PAGE_RW; /* page tables are r/o */ + if (L1_PAGETABLE_ENTRIES_I386 - 1 == l1off) + l1tab = NULL; + } + return 0; +} + +static int setup_pgtables_x86_32_pae(struct xc_dom_image *dom) +{ + xen_pfn_t l3pfn = dom->pgtables_seg.pfn; + xen_pfn_t l2pfn = dom->pgtables_seg.pfn + dom->pg_l3; + xen_pfn_t l1pfn = dom->pgtables_seg.pfn + dom->pg_l3 + dom->pg_l2; + l3_pgentry_64_t *l3tab = xc_dom_pfn_to_ptr(dom, l3pfn, 1); + l2_pgentry_64_t *l2tab = NULL; + l1_pgentry_64_t *l1tab = NULL; + unsigned long l3off, l2off, l1off; + xen_vaddr_t addr; + xen_pfn_t pgpfn; + + for (addr = dom->parms.virt_base; addr < dom->virt_pgtab_end; + addr += PAGE_SIZE_X86) + { + if (NULL == l2tab) + { + /* get L2 tab, make L3 entry */ + l2tab = xc_dom_pfn_to_ptr(dom, l2pfn, 1); + l3off = l3_table_offset_pae(addr); + l3tab[l3off] + (xc_dom_p2m_guest(dom, l2pfn) << PAGE_SHIFT_X86) | L3_PROT; + l2pfn++; + } + + if (NULL == l1tab) + { + /* get L1 tab, make L2 entry */ + l1tab = xc_dom_pfn_to_ptr(dom, l1pfn, 1); + l2off = l2_table_offset_pae(addr); + l2tab[l2off] + (xc_dom_p2m_guest(dom, l1pfn) << PAGE_SHIFT_X86) | L2_PROT; + if (L2_PAGETABLE_ENTRIES_PAE - 1 == l2off) + l2tab = NULL; + l1pfn++; + } + + /* make L1 entry */ + l1off = l1_table_offset_pae(addr); + pgpfn = (addr - dom->parms.virt_base) >> PAGE_SHIFT_X86; + l1tab[l1off] + (xc_dom_p2m_guest(dom, pgpfn) << PAGE_SHIFT_X86) | L1_PROT; + if (addr >= dom->pgtables_seg.vstart && addr < dom->pgtables_seg.vend) + l1tab[l1off] &= ~_PAGE_RW; /* page tables are r/o */ + if (L1_PAGETABLE_ENTRIES_PAE - 1 == l1off) + l1tab = NULL; + } + return 0; +} + +#undef L1_PROT +#undef L2_PROT +#undef L3_PROT + +/* ------------------------------------------------------------------------ */ +/* x86_64 pagetables */ + +static int count_pgtables_x86_64(struct xc_dom_image *dom) +{ + return count_pgtables(dom, + L4_PAGETABLE_SHIFT_X86_64 + 9, + L4_PAGETABLE_SHIFT_X86_64, + L3_PAGETABLE_SHIFT_X86_64, L2_PAGETABLE_SHIFT_X86_64); +} + +#define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED) +#define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER) +#define L3_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER) +#define L4_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER) + +static int setup_pgtables_x86_64(struct xc_dom_image *dom) +{ + xen_pfn_t l4pfn = dom->pgtables_seg.pfn; + xen_pfn_t l3pfn = dom->pgtables_seg.pfn + dom->pg_l4; + xen_pfn_t l2pfn = dom->pgtables_seg.pfn + dom->pg_l4 + dom->pg_l3; + xen_pfn_t l1pfn + dom->pgtables_seg.pfn + dom->pg_l4 + dom->pg_l3 + dom->pg_l2; + l4_pgentry_64_t *l4tab = xc_dom_pfn_to_ptr(dom, l4pfn, 1); + l3_pgentry_64_t *l3tab = NULL; + l2_pgentry_64_t *l2tab = NULL; + l1_pgentry_64_t *l1tab = NULL; + uint64_t l4off, l3off, l2off, l1off; + uint64_t addr; + xen_pfn_t pgpfn; + + for (addr = dom->parms.virt_base; addr < dom->virt_pgtab_end; + addr += PAGE_SIZE_X86) + { + if (NULL == l3tab) + { + /* get L3 tab, make L4 entry */ + l3tab = xc_dom_pfn_to_ptr(dom, l3pfn, 1); + l4off = l4_table_offset_x86_64(addr); + l4tab[l4off] + (xc_dom_p2m_guest(dom, l3pfn) << PAGE_SHIFT_X86) | L4_PROT; + l3pfn++; + } + + if (NULL == l2tab) + { + /* get L2 tab, make L3 entry */ + l2tab = xc_dom_pfn_to_ptr(dom, l2pfn, 1); + l3off = l3_table_offset_x86_64(addr); + l3tab[l3off] + (xc_dom_p2m_guest(dom, l2pfn) << PAGE_SHIFT_X86) | L3_PROT; + if (L3_PAGETABLE_ENTRIES_X86_64 - 1 == l3off) + l3tab = NULL; + l2pfn++; + } + + if (NULL == l1tab) + { + /* get L1 tab, make L2 entry */ + l1tab = xc_dom_pfn_to_ptr(dom, l1pfn, 1); + l2off = l2_table_offset_x86_64(addr); + l2tab[l2off] + (xc_dom_p2m_guest(dom, l1pfn) << PAGE_SHIFT_X86) | L2_PROT; + if (L2_PAGETABLE_ENTRIES_X86_64 - 1 == l2off) + l2tab = NULL; + l1pfn++; + } + + /* make L1 entry */ + l1off = l1_table_offset_x86_64(addr); + pgpfn = (addr - dom->parms.virt_base) >> PAGE_SHIFT_X86; + l1tab[l1off] + (xc_dom_p2m_guest(dom, pgpfn) << PAGE_SHIFT_X86) | L1_PROT; + if (addr >= dom->pgtables_seg.vstart && addr < dom->pgtables_seg.vend) + l1tab[l1off] &= ~_PAGE_RW; /* page tables are r/o */ + if (L1_PAGETABLE_ENTRIES_X86_64 - 1 == l1off) + l1tab = NULL; + } + return 0; +} + +#undef L1_PROT +#undef L2_PROT +#undef L3_PROT +#undef L4_PROT + +/* ------------------------------------------------------------------------ */ + +static int alloc_magic_pages(struct xc_dom_image *dom) +{ + size_t p2m_size = dom->total_pages * dom->arch_hooks->sizeof_pfn; + + /* allocate phys2mach table */ + if (0 != xc_dom_alloc_segment(dom, &dom->p2m_seg, "phys2mach", 0, p2m_size)) + return -1; + dom->p2m_guest = xc_dom_seg_to_ptr(dom, &dom->p2m_seg); + + /* allocate special pages */ + dom->start_info_pfn = xc_dom_alloc_page(dom, "start info"); + dom->xenstore_pfn = xc_dom_alloc_page(dom, "xenstore"); + dom->console_pfn = xc_dom_alloc_page(dom, "console"); + if (xc_dom_feature_translated(dom)) + dom->shared_info_pfn = xc_dom_alloc_page(dom, "shared info"); + dom->alloc_bootstack = 1; + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static int start_info_x86_32(struct xc_dom_image *dom, int xen_version) +{ + start_info_x86_32_t *start_info + xc_dom_pfn_to_ptr(dom, dom->start_info_pfn, 1); + xen_pfn_t shinfo + xc_dom_feature_translated(dom) ? dom->shared_info_pfn : dom-> + shared_info_mfn; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + sprintf(start_info->magic, dom->guest_type); + start_info->nr_pages = dom->total_pages; + start_info->shared_info = shinfo << PAGE_SHIFT_X86; + start_info->pt_base = dom->pgtables_seg.vstart; + start_info->nr_pt_frames = dom->pgtables; + start_info->mfn_list = dom->p2m_seg.vstart; + + start_info->flags = dom->flags; + start_info->store_mfn = xc_dom_p2m_guest(dom, dom->xenstore_pfn); + start_info->store_evtchn = dom->xenstore_evtchn; + start_info->console.domU.mfn = xc_dom_p2m_guest(dom, dom->console_pfn); + start_info->console.domU.evtchn = dom->console_evtchn; + + if (dom->ramdisk_blob) + { + start_info->mod_start = dom->ramdisk_seg.vstart; + start_info->mod_len = dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart; + } + if (dom->cmdline) + { + strncpy((char *)start_info->cmd_line, dom->cmdline, MAX_GUEST_CMDLINE); + start_info->cmd_line[MAX_GUEST_CMDLINE - 1] = ''\0''; + } + return 0; +} + +static int start_info_x86_64(struct xc_dom_image *dom, int xen_version) +{ + start_info_x86_64_t *start_info + xc_dom_pfn_to_ptr(dom, dom->start_info_pfn, 1); + xen_pfn_t shinfo + xc_dom_feature_translated(dom) ? dom->shared_info_pfn : dom-> + shared_info_mfn; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + sprintf(start_info->magic, dom->guest_type); + start_info->nr_pages = dom->total_pages; + start_info->shared_info = shinfo << PAGE_SHIFT_X86; + start_info->pt_base = dom->pgtables_seg.vstart; + start_info->nr_pt_frames = dom->pgtables; + start_info->mfn_list = dom->p2m_seg.vstart; + + start_info->flags = dom->flags; + start_info->store_mfn = xc_dom_p2m_guest(dom, dom->xenstore_pfn); + start_info->store_evtchn = dom->xenstore_evtchn; + start_info->console.domU.mfn = xc_dom_p2m_guest(dom, dom->console_pfn); + start_info->console.domU.evtchn = dom->console_evtchn; + + if (dom->ramdisk_blob) + { + start_info->mod_start = dom->ramdisk_seg.vstart; + start_info->mod_len = dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart; + } + if (dom->cmdline) + { + strncpy((char *)start_info->cmd_line, dom->cmdline, MAX_GUEST_CMDLINE); + start_info->cmd_line[MAX_GUEST_CMDLINE - 1] = ''\0''; + } + return 0; +} + +static int shared_info_x86_32(struct xc_dom_image *dom, void *ptr) +{ + shared_info_x86_32_t *shared_info = ptr; + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + memset(shared_info, 0, sizeof(*shared_info)); + for (i = 0; i < MAX_VIRT_CPUS; i++) + shared_info->vcpu_info[i].evtchn_upcall_mask = 1; + return 0; +} + +static int shared_info_x86_64(struct xc_dom_image *dom, void *ptr) +{ + shared_info_x86_64_t *shared_info = ptr; + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + memset(shared_info, 0, sizeof(*shared_info)); + for (i = 0; i < MAX_VIRT_CPUS; i++) + shared_info->vcpu_info[i].evtchn_upcall_mask = 1; + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static int vcpu_x86_32(struct xc_dom_image *dom, void *ptr) +{ + vcpu_guest_context_x86_32_t *ctxt = ptr; + xen_pfn_t cr3_pfn; + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* clear everything */ + memset(ctxt, 0, sizeof(*ctxt)); + + /* Virtual IDT is empty at start-of-day. */ + for (i = 0; i < 256; i++) + { + ctxt->trap_ctxt[i].vector = i; + ctxt->trap_ctxt[i].cs = FLAT_KERNEL_CS_X86_32; + } + + /* No callback handlers. */ + ctxt->event_callback_cs = FLAT_KERNEL_CS_X86_32; + ctxt->failsafe_callback_cs = FLAT_KERNEL_CS_X86_32; + + ctxt->user_regs.ds = FLAT_KERNEL_DS_X86_32; + ctxt->user_regs.es = FLAT_KERNEL_DS_X86_32; + ctxt->user_regs.fs = FLAT_KERNEL_DS_X86_32; + ctxt->user_regs.gs = FLAT_KERNEL_DS_X86_32; + ctxt->user_regs.ss = FLAT_KERNEL_SS_X86_32; + ctxt->user_regs.cs = FLAT_KERNEL_CS_X86_32; + ctxt->user_regs.eip = dom->parms.virt_entry; + ctxt->user_regs.esp + dom->parms.virt_base + (dom->bootstack_pfn + 1) * PAGE_SIZE_X86; + ctxt->user_regs.esi + dom->parms.virt_base + (dom->start_info_pfn) * PAGE_SIZE_X86; + ctxt->user_regs.eflags = 1 << 9; /* Interrupt Enable */ + + ctxt->kernel_ss = FLAT_KERNEL_SS_X86_32; + ctxt->kernel_sp + dom->parms.virt_base + (dom->bootstack_pfn + 1) * PAGE_SIZE_X86; + + ctxt->flags = VGCF_in_kernel_X86_32; + cr3_pfn = xc_dom_p2m_guest(dom, dom->pgtables_seg.pfn); + ctxt->ctrlreg[3] = xen_pfn_to_cr3_x86_32(cr3_pfn); + xc_dom_printf("%s: cr3: pfn 0x%" PRIpfn " mfn 0x%" PRIpfn "\n", + __FUNCTION__, dom->pgtables_seg.pfn, cr3_pfn); + + return 0; +} + +static int vcpu_x86_64(struct xc_dom_image *dom, void *ptr) +{ + vcpu_guest_context_x86_64_t *ctxt = ptr; + xen_pfn_t cr3_pfn; + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* clear everything */ + memset(ctxt, 0, sizeof(*ctxt)); + + /* Virtual IDT is empty at start-of-day. */ + for (i = 0; i < 256; i++) + { + ctxt->trap_ctxt[i].vector = i; + ctxt->trap_ctxt[i].cs = FLAT_KERNEL_CS_X86_64; + } + + ctxt->user_regs.ds = FLAT_KERNEL_DS_X86_64; + ctxt->user_regs.es = FLAT_KERNEL_DS_X86_64; + ctxt->user_regs.fs = FLAT_KERNEL_DS_X86_64; + ctxt->user_regs.gs = FLAT_KERNEL_DS_X86_64; + ctxt->user_regs.ss = FLAT_KERNEL_SS_X86_64; + ctxt->user_regs.cs = FLAT_KERNEL_CS_X86_64; + ctxt->user_regs.rip = dom->parms.virt_entry; + ctxt->user_regs.rsp + dom->parms.virt_base + (dom->bootstack_pfn + 1) * PAGE_SIZE_X86; + ctxt->user_regs.rsi + dom->parms.virt_base + (dom->start_info_pfn) * PAGE_SIZE_X86; + ctxt->user_regs.rflags = 1 << 9; /* Interrupt Enable */ + + ctxt->kernel_ss = FLAT_KERNEL_SS_X86_64; + ctxt->kernel_sp + dom->parms.virt_base + (dom->bootstack_pfn + 1) * PAGE_SIZE_X86; + + ctxt->flags = VGCF_in_kernel_X86_64; + cr3_pfn = xc_dom_p2m_guest(dom, dom->pgtables_seg.pfn); + ctxt->ctrlreg[3] = xen_pfn_to_cr3_x86_64(cr3_pfn); + xc_dom_printf("%s: cr3: pfn 0x%" PRIpfn " mfn 0x%" PRIpfn "\n", + __FUNCTION__, dom->pgtables_seg.pfn, cr3_pfn); + + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static struct xc_dom_arch xc_dom_32 = { + .guest_type = "xen-3.0-x86_32", + .page_shift = PAGE_SHIFT_X86, + .sizeof_pfn = 4, + .alloc_magic_pages = alloc_magic_pages, + .count_pgtables = count_pgtables_x86_32, + .setup_pgtables = setup_pgtables_x86_32, + .start_info = start_info_x86_32, + .shared_info = shared_info_x86_32, + .vcpu = vcpu_x86_32, +}; +static struct xc_dom_arch xc_dom_32_pae = { + .guest_type = "xen-3.0-x86_32p", + .page_shift = PAGE_SHIFT_X86, + .sizeof_pfn = 4, + .alloc_magic_pages = alloc_magic_pages, + .count_pgtables = count_pgtables_x86_32_pae, + .setup_pgtables = setup_pgtables_x86_32_pae, + .start_info = start_info_x86_32, + .shared_info = shared_info_x86_32, + .vcpu = vcpu_x86_32, +}; + +static struct xc_dom_arch xc_dom_64 = { + .guest_type = "xen-3.0-x86_64", + .page_shift = PAGE_SHIFT_X86, + .sizeof_pfn = 8, + .alloc_magic_pages = alloc_magic_pages, + .count_pgtables = count_pgtables_x86_64, + .setup_pgtables = setup_pgtables_x86_64, + .start_info = start_info_x86_64, + .shared_info = shared_info_x86_64, + .vcpu = vcpu_x86_64, +}; + +static void __init register_arch_hooks(void) +{ + xc_dom_register_arch_hooks(&xc_dom_32); + xc_dom_register_arch_hooks(&xc_dom_32_pae); + xc_dom_register_arch_hooks(&xc_dom_64); +} Index: build-32-unstable-12621/tools/libxc/xc_dom_powerpc64.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_powerpc64.c @@ -0,0 +1,100 @@ +/* + * Xen domain builder -- ia64 bits. + * + * Most architecture-specific code for ia64 goes here. + * - fill architecture-specific structs. + * + * This code is licenced under the GPL. + * written 2006 by Gerd Hoffmann <kraxel@suse.de>. + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <inttypes.h> + +#include <xen/xen.h> + +#include "xg_private.h" +#include "xc_dom.h" + +/* ------------------------------------------------------------------------ */ + +static int alloc_magic_pages(struct xc_dom_image *dom) +{ + /* allocate special pages */ + dom->low_top--; /* shared_info */ + dom->xenstore_pfn = --dom->low_top; + dom->console_pfn = --dom->low_top; + dom->start_info_pfn = --dom->low_top; + return 0; +} + +static int start_info(struct xc_dom_image *dom, int xen_version) +{ + start_info_t *si + xc_dom_pfn_to_ptr(dom, dom->start_info_pfn, 1); + + xc_dom_printf("%s\n", __FUNCTION__); + + snprintf(si->magic, sizeof(si->magic), "xen-%d.%d-powerpc64HV", 3, 0); + + si->nr_pages = dom->total_pages; + si->shared_info = (dom->total_pages - 1) << PAGE_SHIFT; + si->store_mfn = dom->xenstore_pfn; + si->store_evtchn = dom->store_evtchn; + si->console.domU.mfn = dom->console_pfn; + si->console.domU.evtchn = dom->console_evtchn; + return 0; +} + +static int shared_info(struct xc_dom_image *dom, void *ptr) +{ + shared_info_t *shared_info = ptr; + int i; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + memset(shared_info, 0, sizeof(*shared_info)); + return 0; +} + +static int vcpu(struct xc_dom_image *dom, void *ptr) +{ + vcpu_guest_context_t *ctxt = ptr; + + xc_dom_printf("%s: called\n", __FUNCTION__); + + /* clear everything */ + memset(ctxt, 0, sizeof(*ctxt)); + + memset(&ctxt->user_regs, 0x55, sizeof(ctxt.user_regs)); + ctxt->user_regs.pc = dsi->v_kernentry; + ctxt->user_regs.msr = 0; + ctxt->user_regs.gprs[1] = 0; /* Linux uses its own stack */ + ctxt->user_regs.gprs[3] = devtree_addr; + ctxt->user_regs.gprs[4] = kern_addr; + ctxt->user_regs.gprs[5] = 0; + + /* There is a buggy kernel that does not zero the "local_paca", so + * we must make sure this register is 0 */ + ctxt->user_regs.gprs[13] = 0; + + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static struct xc_dom_arch xc_dom_arch = { + .guest_type = "xen-3.0-powerpc64", + .page_shift = FIXME, + .alloc_magic_pages = alloc_magic_pages, + .start_info = start_info, + .shared_info = shared_info, + .vcpu = vcpu, +}; + +static void __init register_arch_hooks(void) +{ + xc_dom_register_arch_hooks(&xc_dom_arch); +} Index: build-32-unstable-12621/tools/libxc/xc_dom_elfloader.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_elfloader.c @@ -0,0 +1,271 @@ +/* + * Xen domain builder -- ELF bits. + * + * Parse and load ELF kernel images. + * + * This code is licenced under the GPL. + * written 2006 by Gerd Hoffmann <kraxel@suse.de>. + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <inttypes.h> + +#include "xg_private.h" +#include "xc_dom.h" + +#define XEN_VER "xen-3.0" + +/* ------------------------------------------------------------------------ */ + +static char *xc_dom_guest_type(struct xc_dom_image *dom, + struct elf_binary *elf) +{ + uint64_t machine = elf_uval(elf, elf->ehdr, e_machine); + + switch (machine) { + case EM_386: + return dom->parms.pae ? "xen-3.0-x86_32p" : "xen-3.0-x86_32"; + case EM_X86_64: + return "xen-3.0-x86_64"; + case EM_IA_64: + return elf_msb(elf) ? "xen-3.0-ia64be" : "xen-3.0-ia64"; + case EM_PPC64: + return "xen-3.0-powerpc64"; + default: + return "xen-3.0-unknown"; + } +} + +/* ------------------------------------------------------------------------ */ +/* parse elf binary */ + +static int check_elf_kernel(struct xc_dom_image *dom, int verbose) +{ + if (NULL == dom->kernel_blob) + { + if (verbose) + xc_dom_panic(XC_INTERNAL_ERROR, "%s: no kernel image loaded\n", + __FUNCTION__); + return -EINVAL; + } + + if (!elf_is_elfbinary(dom->kernel_blob)) + { + if (verbose) + xc_dom_panic(XC_INVALID_KERNEL, "%s: kernel is not an ELF image\n", + __FUNCTION__); + return -EINVAL; + } + return 0; +} + +static int xc_dom_probe_elf_kernel(struct xc_dom_image *dom) +{ + return check_elf_kernel(dom, 0); +} + +static int xc_dom_load_elf_symtab(struct xc_dom_image *dom, + struct elf_binary *elf, int load) +{ + struct elf_binary syms; + const elf_shdr *shdr, *shdr2; + xen_vaddr_t symtab, maxaddr; + char *hdr; + size_t size; + int h, count, type, i, tables = 0; + + if (elf_swap(elf)) { + xc_dom_printf("%s: non-native byte order, bsd symtab not supported\n", + __FUNCTION__); + return 0; + } + + if (load) { + if (!dom->bsd_symtab_start) + return 0; + size = dom->kernel_seg.vend - dom->bsd_symtab_start; + hdr = xc_dom_vaddr_to_ptr(dom, dom->bsd_symtab_start); + *(int *)hdr = size - sizeof(int); + } else { + size = sizeof(int) + elf_size(elf, elf->ehdr) + + elf_shdr_count(elf) * elf_size(elf, shdr); + hdr = xc_dom_malloc(dom, size); + if (hdr == NULL) + return 0; + dom->bsd_symtab_start = elf_round_up(&syms, dom->kernel_seg.vend); + } + + memcpy(hdr + sizeof(int), + elf->image, + elf_size(elf, elf->ehdr)); + memcpy(hdr + sizeof(int) + elf_size(elf, elf->ehdr), + elf->image + elf_uval(elf, elf->ehdr, e_shoff), + elf_shdr_count(elf) * elf_size(elf, shdr)); + if (elf_64bit(elf)) { + Elf64_Ehdr *ehdr = (Elf64_Ehdr *)(hdr + sizeof(int)); + ehdr->e_phoff = 0; + ehdr->e_phentsize = 0; + ehdr->e_phnum = 0; + ehdr->e_shoff = elf_size(elf, elf->ehdr); + ehdr->e_shstrndx = SHN_UNDEF; + } else { + Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(hdr + sizeof(int)); + ehdr->e_phoff = 0; + ehdr->e_phentsize = 0; + ehdr->e_phnum = 0; + ehdr->e_shoff = elf_size(elf, elf->ehdr); + ehdr->e_shstrndx = SHN_UNDEF; + } + if (0 != elf_init(&syms, hdr + sizeof(int), size - sizeof(int))) + return -1; + if (xc_dom_logfile) + elf_set_logfile(&syms, xc_dom_logfile, 1); + + symtab = dom->bsd_symtab_start + sizeof(int); + maxaddr = elf_round_up(&syms, symtab + elf_size(&syms, syms.ehdr) + + elf_shdr_count(&syms) * elf_size(&syms, shdr)); + + xc_dom_printf("%s/%s: bsd_symtab_start=%" PRIx64 ", kernel.end=0x%" PRIx64 + " -- symtab=0x%" PRIx64 ", maxaddr=0x%" PRIx64 "\n", + __FUNCTION__, load ? "load" : "parse", + dom->bsd_symtab_start, dom->kernel_seg.vend, symtab, maxaddr); + + count = elf_shdr_count(&syms); + for (h = 0; h < count; h++) + { + shdr = elf_shdr_by_index(&syms, h); + type = elf_uval(&syms, shdr, sh_type); + if (type == SHT_STRTAB) + { + /* Look for a strtab @i linked to symtab @h. */ + for (i = 0; i < count; i++) { + shdr2 = elf_shdr_by_index(&syms, i); + if ((elf_uval(&syms, shdr2, sh_type) == SHT_SYMTAB) && + (elf_uval(&syms, shdr2, sh_link) == h)) + break; + } + /* Skip symtab @h if we found no corresponding strtab @i. */ + if (i == count) + { + if (elf_64bit(&syms)) + *(Elf64_Off*)(&shdr->e64.sh_offset) = 0; + else + *(Elf32_Off*)(&shdr->e32.sh_offset) = 0; + continue; + } + } + + if ((type == SHT_STRTAB) || (type == SHT_SYMTAB)) + { + /* Mangled to be based on ELF header location. */ + if (elf_64bit(&syms)) + *(Elf64_Off*)(&shdr->e64.sh_offset) = maxaddr - symtab; + else + *(Elf32_Off*)(&shdr->e32.sh_offset) = maxaddr - symtab; + size = elf_uval(&syms, shdr, sh_size); + maxaddr = elf_round_up(&syms, maxaddr + size); + tables++; + xc_dom_printf("%s: h=%d %s, size=0x%zx, maxaddr=0x%" PRIx64 "\n", + __FUNCTION__, h, + type == SHT_SYMTAB ? "symtab" : "strtab", + size, maxaddr); + + if (load) { + shdr2 = elf_shdr_by_index(elf, h); + memcpy((void*)elf_section_start(&syms, shdr), + elf_section_start(elf, shdr2), + size); + } + } + + /* Name is NULL. */ + if (elf_64bit(&syms)) + *(Elf64_Half*)(&shdr->e64.sh_name) = 0; + else + *(Elf32_Word*)(&shdr->e32.sh_name) = 0; + } + + if (0 == tables) + { + xc_dom_printf("%s: no symbol table present\n", __FUNCTION__); + dom->bsd_symtab_start = 0; + return 0; + } + if (!load) + dom->kernel_seg.vend = maxaddr; + return 0; +} + +static int xc_dom_parse_elf_kernel(struct xc_dom_image *dom) +{ + struct elf_binary *elf; + int rc; + + rc = check_elf_kernel(dom, 1); + if (0 != rc) + return rc; + + elf = xc_dom_malloc(dom, sizeof(*elf)); + dom->private_loader = elf; + rc = elf_init(elf, dom->kernel_blob, dom->kernel_size); + if (xc_dom_logfile) + elf_set_logfile(elf, xc_dom_logfile, 1); + if (0 != rc) + { + xc_dom_panic(XC_INVALID_KERNEL, "%s: corrupted ELF image\n", + __FUNCTION__); + return rc; + } + + /* Find the section-header strings table. */ + if (NULL == elf->sec_strtab) + { + xc_dom_panic(XC_INVALID_KERNEL, "%s: ELF image has no shstrtab\n", + __FUNCTION__); + return -EINVAL; + } + + /* parse binary and get xen meta info */ + elf_parse_binary(elf); + if (0 != (rc = elf_xen_parse(elf, &dom->parms))) + return rc; + + /* find kernel segment */ + dom->kernel_seg.vstart = dom->parms.virt_kstart; + dom->kernel_seg.vend = dom->parms.virt_kend; + + if (dom->parms.bsd_symtab) + xc_dom_load_elf_symtab(dom, elf, 0); + + dom->guest_type = xc_dom_guest_type(dom, elf); + xc_dom_printf("%s: %s: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", + __FUNCTION__, dom->guest_type, + dom->kernel_seg.vstart, dom->kernel_seg.vend); + return 0; +} + +static int xc_dom_load_elf_kernel(struct xc_dom_image *dom) +{ + struct elf_binary *elf = dom->private_loader; + + elf->dest = xc_dom_seg_to_ptr(dom, &dom->kernel_seg); + elf_load_binary(elf); + if (dom->parms.bsd_symtab) + xc_dom_load_elf_symtab(dom, elf, 1); + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static struct xc_dom_loader elf_loader = { + .name = "ELF-generic", + .probe = xc_dom_probe_elf_kernel, + .parser = xc_dom_parse_elf_kernel, + .loader = xc_dom_load_elf_kernel, +}; + +static void __init register_loader(void) +{ + xc_dom_register_loader(&elf_loader); +} Index: build-32-unstable-12621/tools/libxc/xc_dom_binloader.c ==================================================================--- /dev/null +++ build-32-unstable-12621/tools/libxc/xc_dom_binloader.c @@ -0,0 +1,281 @@ +/****************************************************************************** + * + * Loads simple binary images. It''s like a .COM file in MS-DOS. No headers are + * present. The only requirement is that it must have a xen_bin_image table + * somewhere in the first 8192 bytes, starting on a 32-bit aligned address. + * Those familiar with the multiboot specification should recognize this, it''s + * (almost) the same as the multiboot header. + * The layout of the xen_bin_image table is: + * + * Offset Type Name Note + * 0 uint32_t magic required + * 4 uint32_t flags required + * 8 uint32_t checksum required + * 12 uint32_t header_addr required + * 16 uint32_t load_addr required + * 20 uint32_t load_end_addr required + * 24 uint32_t bss_end_addr required + * 28 uint32_t entry_addr required + * + * - magic + * Magic number identifying the table. For images to be loaded by Xen 3, the + * magic value is 0x336ec578 ("xEn3" with the 0x80 bit of the "E" set). + * - flags + * bit 0: indicates whether the image needs to be loaded on a page boundary + * bit 1: reserved, must be 0 (the multiboot spec uses this bit to indicate + * that memory info should be passed to the image) + * bit 2: reserved, must be 0 (the multiboot spec uses this bit to indicate + * that the bootloader should pass video mode info to the image) + * bit 16: reserved, must be 1 (the multiboot spec uses this bit to indicate + * that the values in the fields header_addr - entry_addr are + * valid) + * All other bits should be set to 0. + * - checksum + * When added to "magic" and "flags", the resulting value should be 0. + * - header_addr + * Contains the virtual address corresponding to the beginning of the + * table - the memory location at which the magic value is supposed to be + * loaded. This field serves to synchronize the mapping between OS image + * offsets and virtual memory addresses. + * - load_addr + * Contains the virtual address of the beginning of the text segment. The + * offset in the OS image file at which to start loading is defined by the + * offset at which the table was found, minus (header addr - load addr). + * load addr must be less than or equal to header addr. + * - load_end_addr + * Contains the virtual address of the end of the data segment. + * (load_end_addr - load_addr) specifies how much data to load. This implies + * that the text and data segments must be consecutive in the OS image. If + * this field is zero, the domain builder assumes that the text and data + * segments occupy the whole OS image file. + * - bss_end_addr + * Contains the virtual address of the end of the bss segment. The domain + * builder initializes this area to zero, and reserves the memory it occupies + * to avoid placing boot modules and other data relevant to the loaded image + * in that area. If this field is zero, the domain builder assumes that no bss + * segment is present. + * - entry_addr + * The virtual address at which to start execution of the loaded image. + * + * Some of the field descriptions were copied from "The Multiboot + * Specification", Copyright 1995, 96 Bryan Ford <baford@cs.utah.edu>, + * Erich Stefan Boleyn <erich@uruk.org> Copyright 1999, 2000, 2001, 2002 + * Free Software Foundation, Inc. + */ + +#include <stdlib.h> +#include <inttypes.h> + +#include "xg_private.h" +#include "xc_dom.h" + +#define round_pgup(_p) (((_p)+(PAGE_SIZE_X86-1))&PAGE_MASK_X86) +#define round_pgdown(_p) ((_p)&PAGE_MASK_X86) + +struct xen_bin_image_table +{ + uint32_t magic; + uint32_t flags; + uint32_t checksum; + uint32_t header_addr; + uint32_t load_addr; + uint32_t load_end_addr; + uint32_t bss_end_addr; + uint32_t entry_addr; +}; + +#define XEN_MULTIBOOT_MAGIC3 0x336ec578 + +#define XEN_MULTIBOOT_FLAG_ALIGN4K 0x00000001 +#define XEN_MULTIBOOT_FLAG_NEEDMEMINFO 0x00000002 +#define XEN_MULTIBOOT_FLAG_NEEDVIDINFO 0x00000004 +#define XEN_MULTIBOOT_FLAG_ADDRSVALID 0x00010000 +#define XEN_MULTIBOOT_FLAG_PAE_SHIFT 14 +#define XEN_MULTIBOOT_FLAG_PAE_MASK (3 << XEN_MULTIBOOT_FLAG_PAE_SHIFT) + +/* Flags we test for */ +#define FLAGS_MASK ((~ 0) & (~ XEN_MULTIBOOT_FLAG_ALIGN4K) & \ + (~ XEN_MULTIBOOT_FLAG_PAE_MASK))) +#define FLAGS_REQUIRED XEN_MULTIBOOT_FLAG_ADDRSVALID + +/* --------------------------------------------------------------------- */ + +static struct xen_bin_image_table *find_table(struct xc_dom_image *dom) +{ + struct xen_bin_image_table *table; + uint32_t *probe_ptr; + uint32_t *probe_end; + + probe_ptr = dom->kernel_blob; + probe_end = dom->kernel_blob + dom->kernel_size - sizeof(*table); + if ((void*)probe_end > dom->kernel_blob + 8192) + probe_end = dom->kernel_blob + 8192; + + for (table = NULL; probe_ptr < probe_end; probe_ptr++) + { + if (XEN_MULTIBOOT_MAGIC3 == *probe_ptr) + { + table = (struct xen_bin_image_table *) probe_ptr; + /* Checksum correct? */ + if (0 == table->magic + table->flags + table->checksum) + { + return table; + } + } + } + return NULL; +} + +static int xc_dom_probe_bin_kernel(struct xc_dom_image *dom) +{ + struct xen_bin_image_table *table; + + table = find_table(dom); + if (!table) + return -EINVAL; + return 0; +} + +static int xc_dom_parse_bin_kernel(struct xc_dom_image *dom) +{ + struct xen_bin_image_table *image_info; + char *image = dom->kernel_blob; + size_t image_size = dom->kernel_size; + uint32_t start_addr; + uint32_t load_end_addr; + uint32_t bss_end_addr; + uint32_t pae_flags; + + image_info = find_table(dom); + if (!image_info) + return -EINVAL; + + xc_dom_printf("%s: multiboot header fields\n", __FUNCTION__); + xc_dom_printf(" flags: 0x%" PRIx32 "\n", image_info->flags); + xc_dom_printf(" header_addr: 0x%" PRIx32 "\n", image_info->header_addr); + xc_dom_printf(" load_addr: 0x%" PRIx32 "\n", image_info->load_addr); + xc_dom_printf(" load_end_addr: 0x%" PRIx32 "\n", image_info->load_end_addr); + xc_dom_printf(" bss_end_addr: 0x%" PRIx32 "\n", image_info->bss_end_addr); + xc_dom_printf(" entry_addr: 0x%" PRIx32 "\n", image_info->entry_addr); + +#if 0 + /* Check the flags */ + if ( FLAGS_REQUIRED != (image_info->flags & FLAGS_MASK) ) + { + xc_dom_panic(XC_INVALID_KERNEL, + "%s: xen_bin_image_table flags required " + "0x%08" PRIx32 " found 0x%08" PRIx32 "\n", + __FUNCTION__, FLAGS_REQUIRED, image_info->flags & FLAGS_MASK); + return -EINVAL; + } +#endif + + /* Sanity check on the addresses */ + if ( image_info->header_addr < image_info->load_addr || + ((char *) image_info - image) < + (image_info->header_addr - image_info->load_addr) ) + { + xc_dom_panic(XC_INVALID_KERNEL, "%s: Invalid header_addr.", + __FUNCTION__); + return -EINVAL; + } + + start_addr = image_info->header_addr - ((char *)image_info - image); + load_end_addr = image_info->load_end_addr ?: start_addr + image_size; + bss_end_addr = image_info->bss_end_addr ?: load_end_addr; + + xc_dom_printf("%s: calculated addresses\n", __FUNCTION__); + xc_dom_printf(" start_addr: 0x%" PRIx32 "\n", start_addr); + xc_dom_printf(" load_end_addr: 0x%" PRIx32 "\n", load_end_addr); + xc_dom_printf(" bss_end_addr: 0x%" PRIx32 "\n", bss_end_addr); + + if ( start_addr + image_size < load_end_addr ) + { + xc_dom_panic(XC_INVALID_KERNEL, "%s: Invalid load_end_addr.\n", + __FUNCTION__); + return -EINVAL; + } + + if ( bss_end_addr < load_end_addr) + { + xc_dom_panic(XC_INVALID_KERNEL, "%s: Invalid bss_end_addr.\n", + __FUNCTION__); + return -EINVAL; + } + + dom->kernel_seg.vstart = image_info->load_addr; + dom->kernel_seg.vend = bss_end_addr; + dom->parms.virt_base = start_addr; + dom->parms.virt_entry = image_info->entry_addr; + + pae_flags = image_info->flags & XEN_MULTIBOOT_FLAG_PAE_MASK; + switch (pae_flags >> XEN_MULTIBOOT_FLAG_PAE_SHIFT) { + case 0: + dom->guest_type = "xen-3.0-x86_32"; + break; + case 1: + case 2: + dom->guest_type = "xen-3.0-x86_32p"; + break; + case 3: + /* FIXME: kernel detects and switches at runtime ... */ + dom->guest_type = "xen-3.0-x86_32"; + break; + } + return 0; +} + +static int xc_dom_load_bin_kernel(struct xc_dom_image *dom) +{ + struct xen_bin_image_table *image_info; + char *image = dom->kernel_blob; + char *dest; + size_t image_size = dom->kernel_size; + uint32_t start_addr; + uint32_t load_end_addr; + uint32_t bss_end_addr; + uint32_t skip, text_size, bss_size; + + image_info = find_table(dom); + if (!image_info) + return -EINVAL; + + start_addr = image_info->header_addr - ((char *)image_info - image); + load_end_addr = image_info->load_end_addr ?: start_addr + image_size; + bss_end_addr = image_info->bss_end_addr ?: load_end_addr; + + /* It''s possible that we need to skip the first part of the image */ + skip = image_info->load_addr - start_addr; + text_size = load_end_addr - image_info->load_addr; + bss_size = bss_end_addr - load_end_addr; + + xc_dom_printf("%s: calculated sizes\n", __FUNCTION__); + xc_dom_printf(" skip: 0x%" PRIx32 "\n", skip); + xc_dom_printf(" text_size: 0x%" PRIx32 "\n", text_size); + xc_dom_printf(" bss_size: 0x%" PRIx32 "\n", bss_size); + + dest = xc_dom_vaddr_to_ptr(dom, dom->kernel_seg.vstart); + memcpy(dest, image + skip, text_size); + memset(dest + text_size, 0, bss_size); + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static struct xc_dom_loader bin_loader = { + .name = "multiboot-binary", + .probe = xc_dom_probe_bin_kernel, + .parser = xc_dom_parse_bin_kernel, + .loader = xc_dom_load_bin_kernel, +}; + +static void __init register_loader(void) +{ + xc_dom_register_loader(&bin_loader); +} + +/* + * Local variables: + * c-basic-offset: 4 + * End: + */ -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 09/10] libxc domain builder rewrite, linux builder
use new domain builder for the linux (aka generic elf) loader. Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- tools/libxc/Makefile | 7 + tools/libxc/xc_dom_compat_linux.c | 136 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 2 deletions(-) Index: build-32-unstable-12621/tools/libxc/xc_dom_compat_linux.c ==================================================================--- /dev/null +++ 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 <kraxel@suse.de>. + * + */ +#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; +} Index: build-32-unstable-12621/tools/libxc/Makefile ==================================================================--- build-32-unstable-12621.orig/tools/libxc/Makefile +++ 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 -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gerd Hoffmann
2006-Dec-01 15:02 UTC
[Xen-devel] [patch 10/10] Support transparant gunzipping in the readnotes utility.
Signed-off-by: Gerd Hoffmann <kraxel@suse.de> --- tools/xcutils/readnotes.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) Index: build-32-unstable-12621/tools/xcutils/readnotes.c ==================================================================--- build-32-unstable-12621.orig/tools/xcutils/readnotes.c +++ 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); -- _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel