From c2cab7406e53db2985bec48af7f8665799f1ead6 Mon Sep 17 00:00:00 2001
From: Liu Wenyuan <liuwenyuan9@huawei.com>
Date: Wed, 23 Aug 2023 17:27:01 +0800
Subject: [PATCH 2/5] build: Makefile for StratoVirt hypervisor

Add support for building StratoVirt hypervisor, including x86_64 and
arm64.

Signed-off-by: Liu Wenyuan <liuwenyuan9@huawei.com>
---
 src/runtime/Makefile              | 51 ++++++++++++++++++++++++++++++-
 src/runtime/arch/amd64-options.mk |  3 ++
 src/runtime/arch/arm64-options.mk |  3 ++
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/runtime/Makefile b/src/runtime/Makefile
index 33fa8f2..0e6e366 100644
--- a/src/runtime/Makefile
+++ b/src/runtime/Makefile
@@ -79,6 +79,7 @@ QEMUBINDIR    := $(PREFIXDEPS)/bin
 CLHBINDIR    := $(PREFIXDEPS)/bin
 FCBINDIR      := $(PREFIXDEPS)/bin
 ACRNBINDIR    := $(PREFIXDEPS)/bin
+STRATOVIRTBINDIR := $(PREFIXDEPS)/bin
 SYSCONFDIR    := /etc
 LOCALSTATEDIR := /var
 
@@ -102,6 +103,7 @@ GENERATED_VARS = \
 		CONFIG_QEMU_SNP_IN \
 		CONFIG_CLH_IN \
 		CONFIG_FC_IN \
+		CONFIG_STRATOVIRT_IN \
 		$(USER_VARS)
 SCRIPTS += $(COLLECT_SCRIPT)
 SCRIPTS_DIR := $(BINDIR)
@@ -144,12 +146,13 @@ HYPERVISOR_ACRN = acrn
 HYPERVISOR_FC = firecracker
 HYPERVISOR_QEMU = qemu
 HYPERVISOR_CLH = cloud-hypervisor
+HYPERVISOR_STRATOVIRT = stratovirt
 
 # Determines which hypervisor is specified in $(CONFIG_FILE).
 DEFAULT_HYPERVISOR ?= $(HYPERVISOR_QEMU)
 
 # List of hypervisors this build system can generate configuration for.
-HYPERVISORS := $(HYPERVISOR_ACRN) $(HYPERVISOR_FC) $(HYPERVISOR_QEMU) $(HYPERVISOR_CLH)
+HYPERVISORS := $(HYPERVISOR_ACRN) $(HYPERVISOR_FC) $(HYPERVISOR_QEMU) $(HYPERVISOR_CLH) $(HYPERVISOR_STRATOVIRT)
 
 QEMUPATH := $(QEMUBINDIR)/$(QEMUCMD)
 QEMUVALIDHYPERVISORPATHS := [\"$(QEMUPATH)\"]
@@ -175,6 +178,9 @@ ACRNVALIDHYPERVISORPATHS := [\"$(ACRNPATH)\"]
 ACRNCTLPATH := $(ACRNBINDIR)/$(ACRNCTLCMD)
 ACRNVALIDCTLPATHS := [\"$(ACRNCTLPATH)\"]
 
+STRATOVIRTPATH = $(STRATOVIRTBINDIR)/$(STRATOVIRTCMD)
+STRATOVIRTVALIDHYPERVISORPATHS := [\"$(STRATOVIRTPATH)\"]
+
 # Default number of vCPUs
 DEFVCPUS := 1
 # Default maximum number of vCPUs
@@ -217,6 +223,7 @@ DEFVALIDENTROPYSOURCES := [\"/dev/urandom\",\"/dev/random\",\"\"]
 DEFDISABLEBLOCK := false
 DEFSHAREDFS_CLH_VIRTIOFS := virtio-fs
 DEFSHAREDFS_QEMU_VIRTIOFS := virtio-fs
+DEFSHAREDFS_STRATOVIRT_VIRTIOFS := virtio-fs
 DEFSHAREDFS_QEMU_TDX_VIRTIOFS := virtio-9p
 DEFSHAREDFS_QEMU_SEV_VIRTIOFS := virtio-9p
 DEFSHAREDFS_QEMU_SNP_VIRTIOFS := virtio-9p
@@ -379,6 +386,36 @@ ifneq (,$(CLHCMD))
     KERNELPATH_CLH = $(KERNELDIR)/$(KERNEL_NAME_CLH)
 endif
 
+ifneq (,$(STRATOVIRTCMD))
+    KNOWN_HYPERVISORS += $(HYPERVISOR_STRATOVIRT)
+
+    CONFIG_FILE_STRATOVIRT = configuration-stratovirt.toml
+    CONFIG_STRATOVIRT = config/$(CONFIG_FILE_STRATOVIRT)
+    CONFIG_STRATOVIRT_IN = $(CONFIG_STRATOVIRT).in
+
+    CONFIG_PATH_STRATOVIRT = $(abspath $(CONFDIR)/$(CONFIG_FILE_STRATOVIRT))
+    CONFIG_PATHS += $(CONFIG_PATH_STRATOVIRT)
+
+    SYSCONFIG_STRATOVIRT = $(abspath $(SYSCONFDIR)/$(CONFIG_FILE_STRATOVIRT))
+    SYSCONFIG_PATHS += $(SYSCONFIG_STRATOVIRT)
+
+    CONFIGS += $(CONFIG_STRATOVIRT)
+
+    # stratovirt-specific options (all should be suffixed by "_STRATOVIRT")
+    DEFMACHINETYPE_STRATOVIRT := microvm
+    DEFBLOCKSTORAGEDRIVER_STRATOVIRT := virtio-mmio
+    DEFNETWORKMODEL_STRATOVIRT := tcfilter
+    DEFSTATICRESOURCEMGMT_STRATOVIRT = true
+ifeq ($(ARCH),amd64)
+    KERNELTYPE_STRATOVIRT = compressed
+endif
+ifeq ($(ARCH),arm64)
+    KERNELTYPE_STRATOVIRT = uncompressed
+endif
+    KERNEL_NAME_STRATOVIRT = $(call MAKE_KERNEL_NAME,$(KERNELTYPE_STRATOVIRT))
+    KERNELPATH_STRATOVIRT = $(KERNELDIR)/$(KERNEL_NAME_STRATOVIRT)
+endif
+
 ifneq (,$(FCCMD))
     KNOWN_HYPERVISORS += $(HYPERVISOR_FC)
 
@@ -477,6 +514,7 @@ USER_VARS += BINDIR
 USER_VARS += CONFIG_ACRN_IN
 USER_VARS += CONFIG_CLH_IN
 USER_VARS += CONFIG_FC_IN
+USER_VARS += CONFIG_STRATOVIRT_IN
 USER_VARS += CONFIG_PATH
 USER_VARS += CONFIG_QEMU_IN
 USER_VARS += DESTDIR
@@ -495,6 +533,8 @@ USER_VARS += FCPATH
 USER_VARS += FCVALIDHYPERVISORPATHS
 USER_VARS += FCJAILERPATH
 USER_VARS += FCVALIDJAILERPATHS
+USER_VARS += STRATOVIRTPATH
+USER_VARS += STRATOVIRTVALIDHYPERVISORPATHS
 USER_VARS += SYSCONFIG
 USER_VARS += IMAGENAME
 USER_VARS += IMAGEPATH
@@ -516,6 +556,7 @@ USER_VARS += KERNELTDXPATH
 USER_VARS += KERNELSNPPATH
 USER_VARS += KERNELPATH_CLH
 USER_VARS += KERNELPATH_FC
+USER_VARS += KERNELPATH_STRATOVIRT
 USER_VARS += KERNELVIRTIOFSPATH
 USER_VARS += FIRMWAREPATH
 USER_VARS += FIRMWARESEVPATH
@@ -527,6 +568,7 @@ USER_VARS += MACHINEACCELERATORS
 USER_VARS += CPUFEATURES
 USER_VARS += TDXCPUFEATURES
 USER_VARS += DEFMACHINETYPE_CLH
+USER_VARS += DEFMACHINETYPE_STRATOVIRT
 USER_VARS += KERNELPARAMS
 USER_VARS += KERNELTDXPARAMS
 USER_VARS += LIBEXECDIR
@@ -568,6 +610,7 @@ USER_VARS += DEFNETWORKMODEL_ACRN
 USER_VARS += DEFNETWORKMODEL_CLH
 USER_VARS += DEFNETWORKMODEL_FC
 USER_VARS += DEFNETWORKMODEL_QEMU
+USER_VARS += DEFNETWORKMODEL_STRATOVIRT
 USER_VARS += DEFDISABLEGUESTEMPTYDIR
 USER_VARS += DEFDISABLEGUESTSECCOMP
 USER_VARS += DEFDISABLESELINUX
@@ -578,9 +621,11 @@ USER_VARS += DEFDISABLEBLOCK
 USER_VARS += DEFBLOCKSTORAGEDRIVER_ACRN
 USER_VARS += DEFBLOCKSTORAGEDRIVER_FC
 USER_VARS += DEFBLOCKSTORAGEDRIVER_QEMU
+USER_VARS += DEFBLOCKSTORAGEDRIVER_STRATOVIRT
 USER_VARS += DEFBLOCKDEVICEAIO_QEMU
 USER_VARS += DEFSHAREDFS_CLH_VIRTIOFS
 USER_VARS += DEFSHAREDFS_QEMU_VIRTIOFS
+USER_VARS += DEFSHAREDFS_STRATOVIRT_VIRTIOFS
 USER_VARS += DEFSHAREDFS_QEMU_TDX_VIRTIOFS
 USER_VARS += DEFSHAREDFS_QEMU_SEV_VIRTIOFS
 USER_VARS += DEFSHAREDFS_QEMU_SNP_VIRTIOFS
@@ -605,6 +650,7 @@ USER_VARS += DEFSANDBOXCGROUPONLY
 USER_VARS += DEFSTATICRESOURCEMGMT
 USER_VARS += DEFSTATICRESOURCEMGMT_CLH
 USER_VARS += DEFSTATICRESOURCEMGMT_FC
+USER_VARS += DEFSTATICRESOURCEMGMT_STRATOVIRT
 USER_VARS += DEFSTATICRESOURCEMGMT_TEE
 USER_VARS += DEFBINDMOUNTS
 USER_VARS += DEFSERVICEOFFLOAD
@@ -936,6 +982,9 @@ ifneq (,$(findstring $(HYPERVISOR_FC),$(KNOWN_HYPERVISORS)))
 endif
 ifneq (,$(findstring $(HYPERVISOR_ACRN),$(KNOWN_HYPERVISORS)))
 	@printf "\t$(HYPERVISOR_ACRN) hypervisor path (ACRNPATH) : %s\n" $(abspath $(ACRNPATH))
+endif
+ifneq (,$(findstring $(HYPERVISOR_STRATOVIRT),$(KNOWN_HYPERVISORS)))
+	@printf "\t$(HYPERVISOR_STRATOVIRT) hypervisor path (STRATOVIRTPATH) : %s\n" $(abspath $(STRATOVIRTPATH))
 endif
 	@printf "\tassets path (PKGDATADIR) : %s\n" $(abspath $(PKGDATADIR))
 	@printf "\tshim path (PKGLIBEXECDIR) : %s\n" $(abspath $(PKGLIBEXECDIR))
diff --git a/src/runtime/arch/amd64-options.mk b/src/runtime/arch/amd64-options.mk
index e606815..940e87a 100644
--- a/src/runtime/arch/amd64-options.mk
+++ b/src/runtime/arch/amd64-options.mk
@@ -28,3 +28,6 @@ ACRNCTLCMD := acrnctl
 CLHCMD := cloud-hypervisor
 
 DEFSTATICRESOURCEMGMT_CLH := false
+
+# stratovirt binary name
+STRATOVIRTCMD := stratovirt
diff --git a/src/runtime/arch/arm64-options.mk b/src/runtime/arch/arm64-options.mk
index 7f74ae3..895c93f 100644
--- a/src/runtime/arch/arm64-options.mk
+++ b/src/runtime/arch/arm64-options.mk
@@ -21,3 +21,6 @@ FCJAILERCMD := jailer
 CLHCMD := cloud-hypervisor
 
 DEFSTATICRESOURCEMGMT_CLH := true
+
+# stratovirt binary name
+STRATOVIRTCMD := stratovirt
-- 
2.34.1

