# Jailhouse, a Linux-based partitioning hypervisor
#
# Copyright (c) Siemens AG, 2013
+# Copyright (c) Valentine Sinitsyn, 2014
#
# Authors:
# Jan Kiszka <jan.kiszka@siemens.com>
+# Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
#
# This work is licensed under the terms of the GNU GPL, version 2. See
# the COPYING file in the top-level directory.
#
+# asm-defines.h generation code derived from linux/Kbuild:
+#
+# Copyright (c) Linux kernel developers, 2014
+#
LINUXINCLUDE := -I$(src)/arch/$(SRCARCH)/include -I$(src)/include
KBUILD_CFLAGS := -g -Os -Wall -Wstrict-prototypes -Wtype-limits \
-fno-stack-protector -fno-builtin-ffsl
ifeq ($(SRCARCH),x86)
-KBUILD_CFLAGS += -mcmodel=kernel
+KBUILD_CFLAGS += -mcmodel=kernel -mno-sse -mno-mmx -mno-sse2 -mno-3dnow
+KBUILD_CFLAGS += $(call cc-option,-mno-avx,)
KBUILD_CPPFLAGS += -m64
+BUILD_VARIANTS := amd intel
+endif
+
+ifeq ($(SRCARCH),arm)
+KBUILD_CFLAGS += -marm
endif
-ifneq ($(wildcard $(src)/include/jailhouse/config.h),)
-KBUILD_CFLAGS += -include $(src)/include/jailhouse/config.h
+ifneq ($(wildcard $(obj)/include/jailhouse/config.h),)
+KBUILD_CFLAGS += -include $(obj)/include/jailhouse/config.h
endif
-$(obj)/arch/$(SRCARCH)/built-in.o: FORCE
- $(Q)$(MAKE) $(build)=$(obj)/arch/$(SRCARCH)
+CORE_OBJECTS = setup.o printk.o paging.o control.o lib.o mmio.o
-always := jailhouse.bin
+define filechk_config_mk
+( \
+ echo "\$$(foreach config,\$$(filter CONFIG_%, \
+ \$$(.VARIABLES)), \$$(eval undefine \$$(config)))"; \
+ if [ -f $(src)/include/jailhouse/config.h ]; then \
+ sed -e "/^#define \([^[:space:]]*\)[[:space:]]*1/!d" \
+ -e "s/^#define \([^[:space:]]*\)[[:space:]]*1/\1=y/" \
+ $(src)/include/jailhouse/config.h; \
+ fi \
+)
+endef
-hypervisor-y := setup.o printk.o paging.o control.o lib.o \
- arch/$(SRCARCH)/built-in.o hypervisor.lds
-targets += $(hypervisor-y)
+$(obj)/include/generated/config.mk: Makefile FORCE
+ $(call filechk,config_mk)
define filechk_version
$(src)/../scripts/gen_version_h $(src)/..
endef
-clean-files := $(obj)/include/jailhouse/version.h
+clean-dirs += include/generated/
+
+define sed-y
+ "/^=>/{s:=>#\(.*\):/* \1 */:; \
+ s:^=>\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
+ s:^=>\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
+ s:=>::; p;}"
+endef
+
+quiet_cmd_defines = GEN $@
+define cmd_defines
+ (set -e; \
+ echo "#ifndef _GENERATED_ASM_DEFINES_H"; \
+ echo "#define _GENERATED_ASM_DEFINES_H"; \
+ echo "/*"; \
+ echo " * This file is autogenerated. If you need to change it,"; \
+ echo " * edit arch/$(SRCARCH)/asm-defines.c instead."; \
+ echo " *"; \
+ echo " * ALL MANUAL CHANGES TO THIS FILE WILL BE LOST!"; \
+ echo " */"; \
+ echo ""; \
+ sed -ne $(sed-y) $<; \
+ echo ""; \
+ echo "#endif" ) > $@
+endef
+
+defines-file := include/generated/asm-defines.h
+
+targets := $(defines-file) arch/$(SRCARCH)/asm-defines.s
+
+$(obj)/arch/$(SRCARCH)/asm-defines.s: $(src)/arch/$(SRCARCH)/asm-defines.c
+ $(call if_changed_dep,cc_s_c)
-$(obj)/include/jailhouse/version.h: $(src)/Makefile FORCE
+$(obj)/$(defines-file): $(obj)/arch/$(SRCARCH)/asm-defines.s
+ $(Q)mkdir -p $(dir $@)
+ $(call cmd,defines)
+
+$(obj)/include/generated/version.h: $(src)/Makefile FORCE
$(call filechk,version)
-$(obj)/setup.o: $(obj)/include/jailhouse/version.h
+$(foreach co,$(CORE_OBJECTS),\
+ $(eval $(obj)/$(co): $(obj)/$(defines-file)))
+
+$(obj)/setup.o: $(obj)/include/generated/version.h
+
+arch-builtin: $(obj)/$(defines-file) $(obj)/include/generated/config.mk FORCE
+ $(Q)$(MAKE) $(build)=$(obj)/arch/$(SRCARCH) \
+ CONFIG_MK=$(obj)/include/generated/config.mk
+
+always :=
+
+define BUILD_JAILHOUSE_template
+always += jailhouse$(1).bin
+
+hypervisor$(1)-y := $$(CORE_OBJECTS) arch/$$(SRCARCH)/built-in$(1).o \
+ hypervisor.lds
+targets += $$(hypervisor$(1)-y)
-HYPERVISOR_OBJS = $(addprefix $(obj)/,$(hypervisor-y))
+# Need fake recipe here so make will consider this a volatile target
+$$(obj)/arch/$$(SRCARCH)/built-in$(1).o: arch-builtin
+ @
-LDFLAGS_hypervisor.o := -T
+HYPERVISOR$(1)_OBJS = $$(addprefix $$(obj)/,$$(hypervisor$(1)-y))
-targets += hypervisor.o
-$(obj)/hypervisor.o: $(src)/hypervisor.lds $(HYPERVISOR_OBJS)
- $(call if_changed,ld)
+LDFLAGS_hypervisor$(1).o := -T
-OBJCOPYFLAGS_jailhouse.bin := -O binary -R .eh_frame
+targets += hypervisor$(1).o
+$$(obj)/hypervisor$(1).o: $$(src)/hypervisor.lds $$(HYPERVISOR$(1)_OBJS)
+ $$(call if_changed,ld)
+
+OBJCOPYFLAGS_jailhouse$(1).bin := -O binary -R .eh_frame
+
+targets += jailhouse$(1).bin
+$$(obj)/jailhouse$(1).bin: $$(obj)/hypervisor$(1).o
+ $$(call if_changed,objcopy)
+endef
+
+ifneq ($(BUILD_VARIANTS),)
+$(foreach variant,$(BUILD_VARIANTS),\
+ $(eval $(call BUILD_JAILHOUSE_template,-$(variant))))
+else
+$(eval $(call BUILD_JAILHOUSE_template,))
+endif
-targets += jailhouse.bin
-$(obj)/jailhouse.bin: $(obj)/hypervisor.o
- $(call if_changed,objcopy)
+.PHONY: arch-builtin