#!/usr/bin/env bash

if [ -z "$KUT_STANDALONE" ]; then
	if [ ! -f config.mak ]; then
		echo "run ./configure && make first. See ./configure -h"
		exit 2
	fi
	source config.mak
	source scripts/arch-run.bash
	source scripts/vmm.bash
fi

vmm_check_supported

function arch_run_qemu()
{
	qemu_cpu="$TARGET_CPU"

	if [ "$QEMU" ] && [ -z "$ACCEL" ] &&
	   [ "$HOST" = "aarch64" ] && [ "$ARCH" = "arm" ] &&
	   [ "$(basename $QEMU)" = "qemu-system-arm" ]; then
		ACCEL="tcg"
	fi

	set_qemu_accelerator || exit $?
	if [ "$ACCEL" = "kvm" ]; then
		QEMU_ARCH=$HOST
	fi

	qemu=$(search_qemu_binary) ||
		exit $?

	if ! $qemu -machine '?' | grep -q 'ARM Virtual Machine'; then
		echo "$qemu doesn't support mach-virt ('-machine virt'). Exiting."
		exit 2
	fi

	M='-machine virt'

	if [ "$ACCEL" = "kvm" ]; then
		if $qemu $M,\? | grep -q gic-version; then
			M+=',gic-version=host'
		fi
	fi

	if [ -z "$qemu_cpu" ]; then
		if ( [ "$ACCEL" = "kvm" ] || [ "$ACCEL" = "hvf" ] ) &&
		   ( [ "$HOST" = "aarch64" ] || [ "$HOST" = "arm" ] ); then
			qemu_cpu="host"
			if [ "$ARCH" = "arm" ] && [ "$HOST" = "aarch64" ]; then
				qemu_cpu+=",aarch64=off"
			fi
		else
			qemu_cpu="$DEFAULT_QEMU_CPU"
		fi
	fi

	if [ "$ARCH" = "arm" ]; then
		M+=",highmem=off"
	fi

	if is_enabled "$EL2"; then
		M+=",virtualization=on"
	fi

	if ! $qemu $M -device '?' | grep -q virtconsole; then
		echo "$qemu doesn't support virtio-console for chr-testdev. Exiting."
		exit 2
	fi

	if ! $qemu $M -chardev '?' | grep -q testdev; then
		echo "$qemu doesn't support chr-testdev. Exiting."
		exit 2
	fi

	if [ "$UEFI_SHELL_RUN" != "y" ] && [ "$EFI_USE_ACPI" != "y" ]; then
		chr_testdev='-device virtio-serial-device'
		chr_testdev+=' -device virtconsole,chardev=ctd -chardev testdev,id=ctd'
	fi

	pci_testdev=
	if $qemu $M -device '?' | grep -q pci-testdev; then
		pci_testdev="-device pci-testdev"
	fi

	A="-accel $ACCEL$ACCEL_PROPS"
	command="$qemu -nodefaults $M $A -cpu $qemu_cpu $chr_testdev $pci_testdev"
	command+=" -display none -serial stdio"
	command="$(migration_cmd) $(timeout_cmd) $command"

	if [ "$UEFI_SHELL_RUN" = "y" ]; then
		ENVIRON_DEFAULT=n run_test_status $command "$@"
	elif [ "$EFI_USE_ACPI" = "y" ]; then
		run_test_status $command -kernel "$@"
	else
		run_test $command -kernel "$@"
	fi
}

function arch_run_kvmtool()
{
	local command

	if [ "$HOST" != "arm" ] && [ "$HOST" != "aarch64" ]; then
		echo "kvmtool requires KVM but the host ('$HOST') is not arm" >&2
		exit 2
	fi

	kvmtool=$(search_kvmtool_binary) ||
		exit $?

	if [ "$ACCEL" ] && [ "$ACCEL" != "kvm" ]; then
		echo "kvmtool does not support $ACCEL" >&2
		exit 2
	fi

	if ! kvm_available; then
		echo "kvmtool requires KVM but not available on the host" >&2
		exit 2
	fi

	command="$(timeout_cmd) $kvmtool run"

	if is_enabled "$EL2"; then
		command+=" --nested"
	fi

	if [ "$HOST" = "aarch64" ] && [ "$ARCH" = "arm" ]; then
		run_test_status $command --kernel "$@" --aarch32
	else
		run_test_status $command --kernel "$@"
	fi
}

case $(vmm_get_target) in
qemu)
	arch_run_qemu "$@"
	;;
kvmtool)
	arch_run_kvmtool "$@"
	;;
esac
