Add boot tests

Check that the kernel boots and KVM booted in the expect mode.
This should help us catch issues with QEMU/kernel flags that select the
various KVM modes.

Test: make test, inspect logs
Change-Id: I476f23aa9fde68fb6868c57d059cd85df68727e8
diff --git a/Makefile b/Makefile
index ddb1982..b4ae441 100644
--- a/Makefile
+++ b/Makefile
@@ -395,6 +395,44 @@
 include $(KUT_MAKEFILE)
 
 #
+# Basic boot tests in the various arm64 KVM modes
+#
+
+BOOT_TEST_SCRIPT_DIR := $(SCRIPTS_DIR)/aarch64/
+BOOT_TEST_LOG_DIR := $(DIST_DIR)/logs/boot/
+BOOT_TESTS := test-boot-vhe test-boot-nvhe test-boot-pkvm
+
+define gen_boot_test
+	$(KUT_RUN_TEST) -d $(1) -M $(2) \
+		-k $(KERNEL_IMAGE) \
+		-o $(BOOT_TEST_LOG_DIR)/$(1).log \
+		$(BOOT_TEST_SCRIPT_DIR)/test_boot.sh -- \
+		-a "boot_test_mode=$(2)"
+endef
+
+test: test-boot
+test-list: test-list-boot
+
+.PHONY: test-boot
+test-boot: $(BOOT_TESTS)
+
+.PHONY: test-list-boot
+test-list-boot:
+	@ for x in $(BOOT_TESTS); do echo $x; done
+
+.PHONY: test-boot-vhe
+test-boot-vhe: $(KERNEL_IMAGE)
+	@ $(call gen_boot_test,$@,vhe)
+
+.PHONY: test-boot-nvhe
+test-boot-nvhe: $(KERNEL_IMAGE)
+	@ $(call gen_boot_test,$@,nvhe)
+
+.PHONY: test-boot-pkvm
+test-boot-pkvm: $(KERNEL_IMAGE)
+	@ $(call gen_boot_test,$@,pkvm)
+
+#
 # CrosVM Hello World test
 #
 
diff --git a/aarch64/test_boot.sh b/aarch64/test_boot.sh
new file mode 100644
index 0000000..39ec360
--- /dev/null
+++ b/aarch64/test_boot.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+
+# Copyright 2021 The Android KVM Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Script run inside the host. Reads the value of 'boot_test_mode' from
+# the command line and checks that KVM was booted in that given mode by
+# dumping logs using `dmesg`.
+
+set -eufo pipefail
+
+PASS() { echo -e "\e[32mPASS\e[0m"; }
+FAIL() { echo -e "\e[31mFAIL\e[0m"; }
+
+MODE="$(sed -E 's/.*boot_test_mode=([a-z]+).*/\1/' /proc/cmdline)"
+case "${MODE}" in
+        vhe)  EXPECT='kvm [1]: VHE mode initialized successfully';;
+        nvhe) EXPECT='kvm [1]: Hyp mode initialized successfully';;
+        pkvm) EXPECT='kvm [1]: Protected nVHE mode initialized successfully';;
+        *)    echo "Could not parse expected KVM mode: ${MODE}" 1>&2
+              FAIL
+              exit 1
+              ;;
+esac
+
+if dmesg | grep -F "${EXPECT}"; then
+        PASS
+else
+        FAIL
+fi