ia64/xen-unstable

changeset 13636:583441e296a1

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>
---
Makefile | 11 +-
tools/Rules.mk | 2
xen/Makefile | 4
xen/include/public/foreign/Makefile | 37 +++++++
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 | 52 ++++++++++
8 files changed, 331 insertions(+), 3 deletions(-)
author Emmanuel Ackaouy <ack@xensource.com>
date Thu Jan 25 22:16:52 2007 +0000 (2007-01-25)
parents 4aa6044ab967
children 985384fd424c
files Makefile tools/Rules.mk xen/Makefile xen/include/public/foreign/Makefile xen/include/public/foreign/mkchecker.py xen/include/public/foreign/mkheader.py xen/include/public/foreign/reference.size xen/include/public/foreign/structs.py
line diff
     1.1 --- a/Makefile	Thu Jan 25 22:16:52 2007 +0000
     1.2 +++ b/Makefile	Thu Jan 25 22:16:52 2007 +0000
     1.3 @@ -22,7 +22,7 @@ endif
     1.4  install: install-xen install-kernels install-tools install-docs
     1.5  
     1.6  .PHONY: build
     1.7 -build: kernels
     1.8 +build: kernels build-headers
     1.9  	$(MAKE) -C xen build
    1.10  	$(MAKE) -C tools build
    1.11  	$(MAKE) -C docs build
    1.12 @@ -59,11 +59,11 @@ prep-kernels:
    1.13  	for i in $(XKERNELS) ; do $(MAKE) $$i-prep || exit 1; done
    1.14  
    1.15  .PHONY: install-xen
    1.16 -install-xen:
    1.17 +install-xen: build-headers
    1.18  	$(MAKE) -C xen install
    1.19  
    1.20  .PHONY: install-tools
    1.21 -install-tools:
    1.22 +install-tools: build-headers
    1.23  	$(MAKE) -C tools install
    1.24  
    1.25  .PHONY: install-kernels
    1.26 @@ -82,6 +82,11 @@ dev-docs:
    1.27  .PHONY: kbuild
    1.28  kbuild: kernels
    1.29  
    1.30 +# generate header files
    1.31 +.PHONY: build-headers
    1.32 +build-headers:
    1.33 +	$(MAKE) -C xen/include/public/foreign
    1.34 +
    1.35  # Delete the kernel build trees entirely
    1.36  .PHONY: kdelete
    1.37  kdelete:
     2.1 --- a/tools/Rules.mk	Thu Jan 25 22:16:52 2007 +0000
     2.2 +++ b/tools/Rules.mk	Thu Jan 25 22:16:52 2007 +0000
     2.3 @@ -52,5 +52,7 @@ mk-symlinks-xen:
     2.4  	( cd xen/io && ln -sf ../../$(XEN_ROOT)/xen/include/public/io/*.h . )
     2.5  	mkdir -p xen/arch-x86
     2.6  	( cd xen/arch-x86 && ln -sf ../../$(XEN_ROOT)/xen/include/public/arch-x86/*.h . )
     2.7 +	mkdir -p xen/foreign
     2.8 +	( cd xen/foreign && ln -sf ../../$(XEN_ROOT)/xen/include/public/foreign/*.h . )
     2.9  
    2.10  mk-symlinks: mk-symlinks-xen mk-symlinks-$(XEN_OS)
     3.1 --- a/xen/Makefile	Thu Jan 25 22:16:52 2007 +0000
     3.2 +++ b/xen/Makefile	Thu Jan 25 22:16:52 2007 +0000
     3.3 @@ -35,10 +35,13 @@ build install debug clean distclean csco
     3.4  		$(INSTALL_DIR) $(DESTDIR)/usr/include/xen/hvm
     3.5  	[ -d $(DESTDIR)/usr/include/xen/io ] || \
     3.6  		$(INSTALL_DIR) $(DESTDIR)/usr/include/xen/io
     3.7 +	[ -d $(DESTDIR)/usr/include/xen/foreign ] || \
     3.8 +		$(INSTALL_DIR) $(DESTDIR)/usr/include/xen/foreign
     3.9  	$(INSTALL_DATA) include/public/*.h $(DESTDIR)/usr/include/xen
    3.10  	$(INSTALL_DATA) include/public/arch-x86/*.h $(DESTDIR)/usr/include/xen/arch-x86
    3.11  	$(INSTALL_DATA) include/public/hvm/*.h $(DESTDIR)/usr/include/xen/hvm
    3.12  	$(INSTALL_DATA) include/public/io/*.h $(DESTDIR)/usr/include/xen/io
    3.13 +	$(INSTALL_DATA) include/public/foreign/*.h $(DESTDIR)/usr/include/xen/foreign
    3.14  	$(INSTALL_DATA) include/public/COPYING $(DESTDIR)/usr/include/xen
    3.15  
    3.16  .PHONY: _debug
    3.17 @@ -48,6 +51,7 @@ build install debug clean distclean csco
    3.18  .PHONY: _clean
    3.19  _clean: delete-unfresh-files
    3.20  	$(MAKE) -C tools clean
    3.21 +	$(MAKE) -C include/public/foreign clean
    3.22  	$(MAKE) -f $(BASEDIR)/Rules.mk -C include clean
    3.23  	$(MAKE) -f $(BASEDIR)/Rules.mk -C common clean
    3.24  	$(MAKE) -f $(BASEDIR)/Rules.mk -C drivers clean
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/xen/include/public/foreign/Makefile	Thu Jan 25 22:16:52 2007 +0000
     4.3 @@ -0,0 +1,37 @@
     4.4 +XEN_ROOT := ../../../..
     4.5 +include $(XEN_ROOT)/tools/Rules.mk
     4.6 +
     4.7 +architectures := x86_32 x86_64 ia64
     4.8 +headers := $(patsubst %, %.h, $(architectures))
     4.9 +scripts := $(wildcard *.py)
    4.10 +
    4.11 +.PHONY: all clean check-headers
    4.12 +all: $(headers) check-headers
    4.13 +
    4.14 +clean:
    4.15 +	rm -f $(headers)
    4.16 +	rm -f checker checker.c $(XEN_TARGET_ARCH).size
    4.17 +	rm -f *.pyc *.o *~
    4.18 +
    4.19 +check-headers: checker
    4.20 +ifeq ($(CROSS_COMPILE),)
    4.21 +	./checker > $(XEN_TARGET_ARCH).size
    4.22 +	diff -u reference.size $(XEN_TARGET_ARCH).size
    4.23 +else
    4.24 +	@echo "cross build: skipping check"
    4.25 +endif
    4.26 +
    4.27 +x86_32.h: ../arch-x86/xen-x86_32.h ../arch-x86/xen.h ../xen.h $(scripts)
    4.28 +	python mkheader.py $* $@ $(filter %.h,$^)
    4.29 +
    4.30 +x86_64.h: ../arch-x86/xen-x86_64.h ../arch-x86/xen.h ../xen.h $(scripts)
    4.31 +	python mkheader.py $* $@ $(filter %.h,$^)
    4.32 +
    4.33 +ia64.h: ../arch-ia64.h ../xen.h $(scripts)
    4.34 +	python mkheader.py $* $@ $(filter %.h,$^)
    4.35 +
    4.36 +checker: checker.c $(headers)
    4.37 +	$(HOSTCC) $(CFLAGS) -o $@ $<
    4.38 +
    4.39 +checker.c: $(scripts)
    4.40 +	python mkchecker.py $(XEN_TARGET_ARCH) $@ $(architectures)
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/xen/include/public/foreign/mkchecker.py	Thu Jan 25 22:16:52 2007 +0000
     5.3 @@ -0,0 +1,58 @@
     5.4 +#!/usr/bin/python
     5.5 +
     5.6 +import sys;
     5.7 +from structs import structs;
     5.8 +
     5.9 +# command line arguments
    5.10 +arch    = sys.argv[1];
    5.11 +outfile = sys.argv[2];
    5.12 +archs   = sys.argv[3:];
    5.13 +
    5.14 +f = open(outfile, "w");
    5.15 +f.write('''
    5.16 +/*
    5.17 + * sanity checks for generated foreign headers:
    5.18 + *  - verify struct sizes
    5.19 + *
    5.20 + * generated by %s -- DO NOT EDIT
    5.21 + */
    5.22 +#include <stdio.h>
    5.23 +#include <stdlib.h>
    5.24 +#include <stddef.h>
    5.25 +#include <inttypes.h>
    5.26 +#include "../xen.h"
    5.27 +''');
    5.28 +
    5.29 +for a in archs:
    5.30 +    f.write('#include "%s.h"\n' % a);
    5.31 +
    5.32 +f.write('int main(int argc, char *argv[])\n{\n');
    5.33 +
    5.34 +f.write('\tprintf("\\n");');
    5.35 +f.write('printf("%-20s |", "structs");\n');
    5.36 +for a in archs:
    5.37 +    f.write('\tprintf("%%8s", "%s");\n' % a);
    5.38 +f.write('\tprintf("\\n");');
    5.39 +
    5.40 +f.write('\tprintf("\\n");');
    5.41 +for struct in structs:
    5.42 +    f.write('\tprintf("%%-20s |", "%s");\n' % struct);
    5.43 +    for a in archs:
    5.44 +        if a == arch:
    5.45 +            s = struct; # native
    5.46 +        else:
    5.47 +            s = struct + "_" + a;
    5.48 +        f.write('#ifdef %s_has_no_%s\n' % (a, struct));
    5.49 +        f.write('\tprintf("%8s", "-");\n');
    5.50 +        f.write("#else\n");
    5.51 +        f.write('\tprintf("%%8zd", sizeof(struct %s));\n' % s);
    5.52 +        f.write("#endif\n");
    5.53 +
    5.54 +    f.write('\tprintf("\\n");\n\n');
    5.55 +
    5.56 +f.write('\tprintf("\\n");\n');
    5.57 +f.write('\texit(0);\n');
    5.58 +f.write('}\n');
    5.59 +
    5.60 +f.close();
    5.61 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/xen/include/public/foreign/mkheader.py	Thu Jan 25 22:16:52 2007 +0000
     6.3 @@ -0,0 +1,153 @@
     6.4 +#!/usr/bin/python
     6.5 +
     6.6 +import sys, re;
     6.7 +from structs import structs, defines;
     6.8 +
     6.9 +# command line arguments
    6.10 +arch    = sys.argv[1];
    6.11 +outfile = sys.argv[2];
    6.12 +infiles = sys.argv[3:];
    6.13 +
    6.14 +
    6.15 +###########################################################################
    6.16 +# configuration #2: architecture information
    6.17 +
    6.18 +inttypes = {};
    6.19 +header = {};
    6.20 +footer = {};
    6.21 +
    6.22 +# x86_32
    6.23 +inttypes["x86_32"] = {
    6.24 +    "unsigned long" : "uint32_t",
    6.25 +    "long"          : "uint32_t",
    6.26 +    "xen_pfn_t"     : "uint32_t",
    6.27 +};
    6.28 +header["x86_32"] = """
    6.29 +#define __i386___X86_32 1
    6.30 +#pragma pack(push, 4)
    6.31 +""";
    6.32 +footer["x86_32"] = """
    6.33 +#pragma pack(pop)
    6.34 +""";
    6.35 +
    6.36 +# x86_64
    6.37 +inttypes["x86_64"] = {
    6.38 +    "unsigned long" : "__align8__ uint64_t",
    6.39 +    "long"          : "__align8__ uint64_t",
    6.40 +    "xen_pfn_t"     : "__align8__ uint64_t",
    6.41 +};
    6.42 +header["x86_64"] = """
    6.43 +#ifdef __GNUC__
    6.44 +# define __DECL_REG(name) union { uint64_t r ## name, e ## name; }
    6.45 +# define __align8__ __attribute__((aligned (8)))
    6.46 +#else
    6.47 +# define __DECL_REG(name) uint64_t r ## name
    6.48 +# define __align8__ FIXME
    6.49 +#endif
    6.50 +#define __x86_64___X86_64 1
    6.51 +""";
    6.52 +
    6.53 +# ia64
    6.54 +inttypes["ia64"] = {
    6.55 +    "unsigned long" : "__align8__ uint64_t",
    6.56 +    "long"          : "__align8__ uint64_t",
    6.57 +    "xen_pfn_t"     : "__align8__ uint64_t",
    6.58 +    "long double"   : "__align16__ ldouble_t",
    6.59 +};
    6.60 +header["ia64"] = """
    6.61 +#define __align8__ __attribute__((aligned (8)))
    6.62 +#define __align16__ __attribute__((aligned (16)))
    6.63 +typedef unsigned char ldouble_t[16];
    6.64 +""";
    6.65 +
    6.66 +
    6.67 +###########################################################################
    6.68 +# main
    6.69 +
    6.70 +input  = "";
    6.71 +output = "";
    6.72 +fileid = re.sub("[-.]", "_", "__FOREIGN_%s__" % outfile.upper());
    6.73 +
    6.74 +# read input header files
    6.75 +for name in infiles:
    6.76 +    f = open(name, "r");
    6.77 +    input += f.read();
    6.78 +    f.close();
    6.79 +
    6.80 +# add header
    6.81 +output += """
    6.82 +/*
    6.83 + * public xen defines and struct for %s
    6.84 + * generated by %s -- DO NOT EDIT
    6.85 + */
    6.86 +
    6.87 +#ifndef %s
    6.88 +#define %s 1
    6.89 +
    6.90 +""" % (arch, sys.argv[0], fileid, fileid)
    6.91 +
    6.92 +if arch in header:
    6.93 +    output += header[arch];
    6.94 +    output += "\n";
    6.95 +
    6.96 +# add defines to output
    6.97 +for line in re.findall("#define[^\n]+", input):
    6.98 +    for define in defines:
    6.99 +        regex = "#define\s+%s\\b" % define;
   6.100 +        match = re.search(regex, line);
   6.101 +        if None == match:
   6.102 +            continue;
   6.103 +        if define.upper()[0] == define[0]:
   6.104 +            replace = define + "_" + arch.upper();
   6.105 +        else:
   6.106 +            replace = define + "_" + arch;
   6.107 +        regex = "\\b%s\\b" % define;
   6.108 +        output += re.sub(regex, replace, line) + "\n";
   6.109 +output += "\n";
   6.110 +
   6.111 +# delete defines, comments, empty lines
   6.112 +input = re.sub("#define[^\n]+\n", "", input);
   6.113 +input = re.compile("/\*(.*?)\*/", re.S).sub("", input)
   6.114 +input = re.compile("\n\s*\n", re.S).sub("\n", input);
   6.115 +
   6.116 +# add structs to output
   6.117 +for struct in structs:
   6.118 +    regex = "struct\s+%s\s*\{(.*?)\n\};" % struct;
   6.119 +    match = re.search(regex, input, re.S)
   6.120 +    if None == match:
   6.121 +        output += "#define %s_has_no_%s 1\n" % (arch, struct);
   6.122 +    else:
   6.123 +        output += "struct %s_%s {%s\n};\n" % (struct, arch, match.group(1));
   6.124 +        output += "typedef struct %s_%s %s_%s_t;\n" % (struct, arch, struct, arch);
   6.125 +    output += "\n";
   6.126 +
   6.127 +# add footer
   6.128 +if arch in footer:
   6.129 +    output += footer[arch];
   6.130 +    output += "\n";
   6.131 +output += "#endif /* %s */\n" % fileid;
   6.132 +
   6.133 +# replace: defines
   6.134 +for define in defines:
   6.135 +    if define.upper()[0] == define[0]:
   6.136 +        replace = define + "_" + arch.upper();
   6.137 +    else:
   6.138 +        replace = define + "_" + arch;
   6.139 +    output = re.sub("\\b%s\\b" % define, replace, output);
   6.140 +
   6.141 +# replace: structs + struct typedefs
   6.142 +for struct in structs:
   6.143 +    output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output);
   6.144 +    output = re.sub("\\b(%s)_t\\b" % struct, "\\1_%s_t" % arch, output);
   6.145 +
   6.146 +# replace: integer types
   6.147 +integers = inttypes[arch].keys();
   6.148 +integers.sort(lambda a, b: cmp(len(b),len(a)));
   6.149 +for type in integers:
   6.150 +    output = re.sub("\\b%s\\b" % type, inttypes[arch][type], output);
   6.151 +
   6.152 +# print results
   6.153 +f = open(outfile, "w");
   6.154 +f.write(output);
   6.155 +f.close;
   6.156 +
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/xen/include/public/foreign/reference.size	Thu Jan 25 22:16:52 2007 +0000
     7.3 @@ -0,0 +1,17 @@
     7.4 +
     7.5 +structs              |  x86_32  x86_64    ia64
     7.6 +
     7.7 +start_info           |    1104    1152    1152
     7.8 +trap_info            |       8      16       -
     7.9 +pt_fpreg             |       -       -      16
    7.10 +cpu_user_regs        |      68     200     496
    7.11 +xen_ia64_boot_param  |       -       -      96
    7.12 +ia64_tr_entry        |       -       -      32
    7.13 +vcpu_extra_regs      |       -       -     536
    7.14 +vcpu_guest_context   |    2800    5168    1056
    7.15 +arch_vcpu_info       |      24      16       0
    7.16 +vcpu_time_info       |      32      32      32
    7.17 +vcpu_info            |      64      64      48
    7.18 +arch_shared_info     |     268     280     272
    7.19 +shared_info          |    2584    3368    4384
    7.20 +
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/xen/include/public/foreign/structs.py	Thu Jan 25 22:16:52 2007 +0000
     8.3 @@ -0,0 +1,52 @@
     8.4 +# configuration: what needs translation
     8.5 +
     8.6 +structs = [ "start_info",
     8.7 +            "trap_info",
     8.8 +            "pt_fpreg",
     8.9 +            "cpu_user_regs",
    8.10 +            "xen_ia64_boot_param",
    8.11 +            "ia64_tr_entry",
    8.12 +            "vcpu_extra_regs",
    8.13 +            "vcpu_guest_context",
    8.14 +            "arch_vcpu_info",
    8.15 +            "vcpu_time_info",
    8.16 +            "vcpu_info",
    8.17 +            "arch_shared_info",
    8.18 +            "shared_info" ];
    8.19 +
    8.20 +defines = [ "__i386__",
    8.21 +            "__x86_64__",
    8.22 +
    8.23 +            "FLAT_RING1_CS",
    8.24 +            "FLAT_RING1_DS",
    8.25 +            "FLAT_RING1_SS",
    8.26 +
    8.27 +            "FLAT_RING3_CS64",
    8.28 +            "FLAT_RING3_DS64",
    8.29 +            "FLAT_RING3_SS64",
    8.30 +            "FLAT_KERNEL_CS64",
    8.31 +            "FLAT_KERNEL_DS64",
    8.32 +            "FLAT_KERNEL_SS64",
    8.33 +
    8.34 +            "FLAT_KERNEL_CS",
    8.35 +            "FLAT_KERNEL_DS",
    8.36 +            "FLAT_KERNEL_SS",
    8.37 +
    8.38 +            # x86_{32,64}
    8.39 +            "_VGCF_i387_valid",
    8.40 +            "VGCF_i387_valid",
    8.41 +            "_VGCF_in_kernel",
    8.42 +            "VGCF_in_kernel",
    8.43 +            "_VGCF_failsafe_disables_events",
    8.44 +            "VGCF_failsafe_disables_events",
    8.45 +            "_VGCF_syscall_disables_events",
    8.46 +            "VGCF_syscall_disables_events",
    8.47 +
    8.48 +            # ia64
    8.49 +            "VGCF_EXTRA_REGS",
    8.50 +
    8.51 +            # all archs
    8.52 +            "xen_pfn_to_cr3",
    8.53 +            "MAX_VIRT_CPUS",
    8.54 +            "MAX_GUEST_CMDLINE" ];
    8.55 +