qemu_v8: build Xen with Buildroot

Commit 8b808915f856 ("qemu_v8: xen: apply patch to fix issue with RPC
buffer release") really did only one part of the job. Only the Xen tools
are built with Buildroot; the hypervisor itself (which needs the patch
mentioned in the commit) is actually built from the top-level xen/
directory. As a result the issue is stil present.
This commit changes the QEMUv8 makefiles as well as the Buildroot
configuration for Xen so that both the hypervisor and the tools are
built. As for the Xen configuration (enabling OP-TEE support), it is
introduced via a patch to the defconfig file.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/br-ext/configs/xen_tools.conf b/br-ext/configs/xen.conf
similarity index 80%
rename from br-ext/configs/xen_tools.conf
rename to br-ext/configs/xen.conf
index ae301bd..4111fa2 100644
--- a/br-ext/configs/xen_tools.conf
+++ b/br-ext/configs/xen.conf
@@ -1,6 +1,6 @@
 BR2_PACKAGE_XEN=y
 BR2_PACKAGE_XEN_TOOLS=y
-BR2_PACKAGE_XEN_HYPERVISOR=n
+BR2_PACKAGE_XEN_HYPERVISOR=y
 BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
 BR2_PACKAGE_BASH=y
 BR2_SYSTEM_BIN_SH_BASH=y
diff --git a/br-ext/patches/xen/0001-Set-config-for-OP-TEE.patch b/br-ext/patches/xen/0001-Set-config-for-OP-TEE.patch
new file mode 100644
index 0000000..dddff4c
--- /dev/null
+++ b/br-ext/patches/xen/0001-Set-config-for-OP-TEE.patch
@@ -0,0 +1,25 @@
+From 2c634e035c3a5cbd0b6d752cbfa1fabca3f6e530 Mon Sep 17 00:00:00 2001
+From: Jerome Forissier <jerome.forissier@linaro.org>
+Date: Fri, 6 May 2022 15:25:57 +0200
+Subject: [PATCH] Set config for OP-TEE
+
+Enables OP-TEE in the default arm64 configuration.
+
+Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
+---
+ xen/arch/arm/configs/arm64_defconfig | 5 +++++
+ 1 file changed, 5 insertions(+)
+
+diff --git a/xen/arch/arm/configs/arm64_defconfig b/xen/arch/arm/configs/arm64_defconfig
+index e69de29bb2..fa571067ee 100644
+--- a/xen/arch/arm/configs/arm64_defconfig
++++ b/xen/arch/arm/configs/arm64_defconfig
+@@ -0,0 +1,5 @@
++CONFIG_EXPERT=y
++CONFIG_TEE=y
++CONFIG_OPTEE=y
++CONFIG_SCHED_CREDIT2_DEFAULT=y
++CONFIG_DEBUG_INFO=n
+-- 
+2.34.1
+
diff --git a/common.mk b/common.mk
index e19560a..7635d30 100644
--- a/common.mk
+++ b/common.mk
@@ -274,9 +274,9 @@
 endif
 
 ifeq ($(XEN_BOOT),y)
-DEFCONFIG_XEN_TOOLS=--br-defconfig build/br-ext/configs/xen_tools.conf
-# Contains a patch to address https://github.com/OP-TEE/optee_os/issues/5288
-# https://lists.xenproject.org/archives/html/xen-devel/2022-05/msg00245.html
+DEFCONFIG_XEN=--br-defconfig build/br-ext/configs/xen.conf
+# The version of Xen provided by Buildroot needs a few patches to work with
+# OP-TEE
 BR2_GLOBAL_PATCH_DIR=../build/br-ext/patches
 endif
 
@@ -345,7 +345,7 @@
 		--br-defconfig build/br-ext/configs/optee_generic \
 		--br-defconfig build/br-ext/configs/$(BUILDROOT_TOOLCHAIN) \
 		$(DEFCONFIG_GDBSERVER) \
-		$(DEFCONFIG_XEN_TOOLS) \
+		$(DEFCONFIG_XEN) \
 		$(DEFCONFIG_TSS) \
 		$(DEFCONFIG_TPM_MODULE) \
 		$(DEFCONFIG_FTPM) \
diff --git a/qemu_v8.mk b/qemu_v8.mk
index 41bb09a..c41219d 100644
--- a/qemu_v8.mk
+++ b/qemu_v8.mk
@@ -99,7 +99,7 @@
 endif
 
 XEN_PATH		?= $(ROOT)/xen
-XEN_IMAGE		?= $(XEN_PATH)/xen/xen.efi
+XEN_IMAGE		?= $(ROOT)/out-br/build/xen-4.14.3/xen/xen.efi
 XEN_EXT4		?= $(BINARIES_PATH)/xen.ext4
 XEN_CFG			?= $(ROOT)/build/qemu_v8/xen/xen.cfg
 
@@ -128,8 +128,8 @@
 endif
 
 ifeq ($(XEN_BOOT),y)
-TARGET_DEPS		+= xen xen-create-image buildroot-domu
-TARGET_CLEAN		+= xen-clean buildroot-domu-clean
+TARGET_DEPS		+= xen-create-image buildroot-domu
+TARGET_CLEAN		+= buildroot-domu-clean
 endif
 
 all: $(TARGET_DEPS)
@@ -374,24 +374,13 @@
 ################################################################################
 # XEN
 ################################################################################
-.PHONY: xen
-$(XEN_PATH)/xen/.config:
-	$(MAKE) -C $(XEN_PATH)/xen XEN_TARGET_ARCH=arm64 defconfig
-	cd $(XEN_PATH)/xen && \
-	tools/kconfig/merge_config.sh -m .config $(ROOT)/build/kconfigs/xen.conf
-
-xen: $(XEN_PATH)/xen/.config
-	$(MAKE) -C $(XEN_PATH) dist-xen \
-	XEN_TARGET_ARCH=arm64 \
-	CONFIG_XEN_INSTALL_SUFFIX=.gz	\
-	CROSS_COMPILE="$(CCACHE)$(AARCH64_CROSS_COMPILE)"
 
 XEN_TMP ?= $(BINARIES_PATH)/xen_files
 
 $(XEN_TMP):
 	mkdir -p $@
 
-xen-create-image: xen linux buildroot | $(XEN_TMP)
+xen-create-image: linux buildroot | $(XEN_TMP)
 	cp $(KERNEL_IMAGE) $(XEN_TMP)
 	cp $(XEN_IMAGE) $(XEN_TMP)
 	cp $(XEN_CFG) $(XEN_TMP)
@@ -399,21 +388,6 @@
 	rm -f $(XEN_EXT4)
 	mke2fs -t ext4 -d $(XEN_TMP) $(XEN_EXT4) 100M
 
-xen-clean:
-	$(MAKE) -C $(XEN_PATH) clean
-
-# Make sure Xen and Xen tools have the same major.minor version or things are likely to break
-ifeq ($(XEN_BOOT),y)
-xen-br = $(ROOT)/buildroot/package/xen/xen.mk
-xen-xen = $(ROOT)/xen/xen/Makefile
-xen-version-br = $(shell sed -E -n 's/^XEN_VERSION = ([0-9]+.[0-9]+).*/\1/p' $(xen-br))
-xen-version-major-xen = $(shell sed -E -n 's/export XEN_VERSION *= ([0-9]+).*/\1/p' $(xen-xen))
-xen-version-minor-xen = $(shell sed -E -n 's/export XEN_SUBVERSION *= ([0-9]+).*/\1/p' $(xen-xen))
-xen-version-xen = $(xen-version-major-xen).$(xen-version-minor-xen)
-ifneq ($(xen-version-br),$(xen-version-xen))
-$(error Xen version mismatch: $(xen-version-br) [in $(xen-br)] != $(xen-version-xen) [in $(xen-xen)])
-endif
-endif
 
 ################################################################################
 # Run targets