18c2ecf20Sopenharmony_ci#!/bin/bash
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0+
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# Run a kvm-based test of the specified tree on the specified configs.
58c2ecf20Sopenharmony_ci# Fully automated run and error checking, no graphics console.
68c2ecf20Sopenharmony_ci#
78c2ecf20Sopenharmony_ci# Execute this in the source tree.  Do not run it as a background task
88c2ecf20Sopenharmony_ci# because qemu does not seem to like that much.
98c2ecf20Sopenharmony_ci#
108c2ecf20Sopenharmony_ci# Usage: kvm-test-1-run.sh config builddir resdir seconds qemu-args boot_args
118c2ecf20Sopenharmony_ci#
128c2ecf20Sopenharmony_ci# qemu-args defaults to "-enable-kvm -nographic", along with arguments
138c2ecf20Sopenharmony_ci#			specifying the number of CPUs and other options
148c2ecf20Sopenharmony_ci#			generated from the underlying CPU architecture.
158c2ecf20Sopenharmony_ci# boot_args defaults to value returned by the per_version_boot_params
168c2ecf20Sopenharmony_ci#			shell function.
178c2ecf20Sopenharmony_ci#
188c2ecf20Sopenharmony_ci# Anything you specify for either qemu-args or boot_args is appended to
198c2ecf20Sopenharmony_ci# the default values.  The "-smp" value is deduced from the contents of
208c2ecf20Sopenharmony_ci# the config fragment.
218c2ecf20Sopenharmony_ci#
228c2ecf20Sopenharmony_ci# More sophisticated argument parsing is clearly needed.
238c2ecf20Sopenharmony_ci#
248c2ecf20Sopenharmony_ci# Copyright (C) IBM Corporation, 2011
258c2ecf20Sopenharmony_ci#
268c2ecf20Sopenharmony_ci# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciT=${TMPDIR-/tmp}/kvm-test-1-run.sh.$$
298c2ecf20Sopenharmony_citrap 'rm -rf $T' 0
308c2ecf20Sopenharmony_cimkdir $T
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci. functions.sh
338c2ecf20Sopenharmony_ci. $CONFIGFRAG/ver_functions.sh
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciconfig_template=${1}
368c2ecf20Sopenharmony_ciconfig_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
378c2ecf20Sopenharmony_cititle=`echo $config_template | sed -e 's/^.*\///'`
388c2ecf20Sopenharmony_cibuilddir=${2}
398c2ecf20Sopenharmony_ciresdir=${3}
408c2ecf20Sopenharmony_ciif test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
418c2ecf20Sopenharmony_cithen
428c2ecf20Sopenharmony_ci	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
438c2ecf20Sopenharmony_ci	exit 1
448c2ecf20Sopenharmony_cifi
458c2ecf20Sopenharmony_ciecho ' ---' `date`: Starting build
468c2ecf20Sopenharmony_ciecho ' ---' Kconfig fragment at: $config_template >> $resdir/log
478c2ecf20Sopenharmony_citouch $resdir/ConfigFragment.input
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci# Combine additional Kconfig options into an existing set such that
508c2ecf20Sopenharmony_ci# newer options win.  The first argument is the Kconfig source ID, the
518c2ecf20Sopenharmony_ci# second the to-be-updated file within $T, and the third and final the
528c2ecf20Sopenharmony_ci# list of additional Kconfig options.  Note that a $2.tmp file is
538c2ecf20Sopenharmony_ci# created when doing the update.
548c2ecf20Sopenharmony_ciconfig_override_param () {
558c2ecf20Sopenharmony_ci	if test -n "$3"
568c2ecf20Sopenharmony_ci	then
578c2ecf20Sopenharmony_ci		echo $3 | sed -e 's/^ *//' -e 's/ *$//' | tr -s " " "\012" > $T/Kconfig_args
588c2ecf20Sopenharmony_ci		echo " --- $1" >> $resdir/ConfigFragment.input
598c2ecf20Sopenharmony_ci		cat $T/Kconfig_args >> $resdir/ConfigFragment.input
608c2ecf20Sopenharmony_ci		config_override.sh $T/$2 $T/Kconfig_args > $T/$2.tmp
618c2ecf20Sopenharmony_ci		mv $T/$2.tmp $T/$2
628c2ecf20Sopenharmony_ci		# Note that "#CHECK#" is not permitted on commandline.
638c2ecf20Sopenharmony_ci	fi
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciecho > $T/KcList
678c2ecf20Sopenharmony_ciconfig_override_param "$config_dir/CFcommon" KcList "`cat $config_dir/CFcommon 2> /dev/null`"
688c2ecf20Sopenharmony_ciconfig_override_param "$config_template" KcList "`cat $config_template 2> /dev/null`"
698c2ecf20Sopenharmony_ciconfig_override_param "--gdb options" KcList "$TORTURE_KCONFIG_GDB_ARG"
708c2ecf20Sopenharmony_ciconfig_override_param "--kasan options" KcList "$TORTURE_KCONFIG_KASAN_ARG"
718c2ecf20Sopenharmony_ciconfig_override_param "--kcsan options" KcList "$TORTURE_KCONFIG_KCSAN_ARG"
728c2ecf20Sopenharmony_ciconfig_override_param "--kconfig argument" KcList "$TORTURE_KCONFIG_ARG"
738c2ecf20Sopenharmony_cicp $T/KcList $resdir/ConfigFragment
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cibase_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
768c2ecf20Sopenharmony_ciif test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
778c2ecf20Sopenharmony_cithen
788c2ecf20Sopenharmony_ci	# Rerunning previous test, so use that test's kernel.
798c2ecf20Sopenharmony_ci	QEMU="`identify_qemu $base_resdir/vmlinux`"
808c2ecf20Sopenharmony_ci	BOOT_IMAGE="`identify_boot_image $QEMU`"
818c2ecf20Sopenharmony_ci	KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
828c2ecf20Sopenharmony_ci	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
838c2ecf20Sopenharmony_ci	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
848c2ecf20Sopenharmony_ci	# Arch-independent indicator
858c2ecf20Sopenharmony_ci	touch $resdir/builtkernel
868c2ecf20Sopenharmony_cielif kvm-build.sh $T/KcList $resdir
878c2ecf20Sopenharmony_cithen
888c2ecf20Sopenharmony_ci	# Had to build a kernel for this test.
898c2ecf20Sopenharmony_ci	QEMU="`identify_qemu vmlinux`"
908c2ecf20Sopenharmony_ci	BOOT_IMAGE="`identify_boot_image $QEMU`"
918c2ecf20Sopenharmony_ci	cp vmlinux $resdir
928c2ecf20Sopenharmony_ci	cp .config $resdir
938c2ecf20Sopenharmony_ci	cp Module.symvers $resdir > /dev/null || :
948c2ecf20Sopenharmony_ci	cp System.map $resdir > /dev/null || :
958c2ecf20Sopenharmony_ci	if test -n "$BOOT_IMAGE"
968c2ecf20Sopenharmony_ci	then
978c2ecf20Sopenharmony_ci		cp $BOOT_IMAGE $resdir
988c2ecf20Sopenharmony_ci		KERNEL=$resdir/${BOOT_IMAGE##*/}
998c2ecf20Sopenharmony_ci		# Arch-independent indicator
1008c2ecf20Sopenharmony_ci		touch $resdir/builtkernel
1018c2ecf20Sopenharmony_ci	else
1028c2ecf20Sopenharmony_ci		echo No identifiable boot image, not running KVM, see $resdir.
1038c2ecf20Sopenharmony_ci		echo Do the torture scripts know about your architecture?
1048c2ecf20Sopenharmony_ci	fi
1058c2ecf20Sopenharmony_ci	parse-build.sh $resdir/Make.out $title
1068c2ecf20Sopenharmony_cielse
1078c2ecf20Sopenharmony_ci	# Build failed.
1088c2ecf20Sopenharmony_ci	cp .config $resdir || :
1098c2ecf20Sopenharmony_ci	echo Build failed, not running KVM, see $resdir.
1108c2ecf20Sopenharmony_ci	if test -f $builddir.wait
1118c2ecf20Sopenharmony_ci	then
1128c2ecf20Sopenharmony_ci		mv $builddir.wait $builddir.ready
1138c2ecf20Sopenharmony_ci	fi
1148c2ecf20Sopenharmony_ci	exit 1
1158c2ecf20Sopenharmony_cifi
1168c2ecf20Sopenharmony_ciif test -f $builddir.wait
1178c2ecf20Sopenharmony_cithen
1188c2ecf20Sopenharmony_ci	mv $builddir.wait $builddir.ready
1198c2ecf20Sopenharmony_cifi
1208c2ecf20Sopenharmony_ciwhile test -f $builddir.ready
1218c2ecf20Sopenharmony_cido
1228c2ecf20Sopenharmony_ci	sleep 1
1238c2ecf20Sopenharmony_cidone
1248c2ecf20Sopenharmony_ciseconds=$4
1258c2ecf20Sopenharmony_ciqemu_args=$5
1268c2ecf20Sopenharmony_ciboot_args=$6
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cikstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
1298c2ecf20Sopenharmony_ciif test -z "$TORTURE_BUILDONLY"
1308c2ecf20Sopenharmony_cithen
1318c2ecf20Sopenharmony_ci	echo ' ---' `date`: Starting kernel
1328c2ecf20Sopenharmony_cifi
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci# Generate -smp qemu argument.
1358c2ecf20Sopenharmony_ciqemu_args="-enable-kvm -nographic $qemu_args"
1368c2ecf20Sopenharmony_cicpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
1378c2ecf20Sopenharmony_cicpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
1388c2ecf20Sopenharmony_ciif test "$cpu_count" -gt "$TORTURE_ALLOTED_CPUS"
1398c2ecf20Sopenharmony_cithen
1408c2ecf20Sopenharmony_ci	echo CPU count limited from $cpu_count to $TORTURE_ALLOTED_CPUS | tee -a $resdir/Warnings
1418c2ecf20Sopenharmony_ci	cpu_count=$TORTURE_ALLOTED_CPUS
1428c2ecf20Sopenharmony_cifi
1438c2ecf20Sopenharmony_ciqemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
1448c2ecf20Sopenharmony_ciqemu_args="`specify_qemu_net "$qemu_args"`"
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci# Generate architecture-specific and interaction-specific qemu arguments
1478c2ecf20Sopenharmony_ciqemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci# Generate qemu -append arguments
1508c2ecf20Sopenharmony_ciqemu_append="`identify_qemu_append "$QEMU"`"
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci# Pull in Kconfig-fragment boot parameters
1538c2ecf20Sopenharmony_ciboot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
1548c2ecf20Sopenharmony_ci# Generate kernel-version-specific boot parameters
1558c2ecf20Sopenharmony_ciboot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
1568c2ecf20Sopenharmony_ciif test -n "$TORTURE_BOOT_GDB_ARG"
1578c2ecf20Sopenharmony_cithen
1588c2ecf20Sopenharmony_ci	boot_args="$boot_args $TORTURE_BOOT_GDB_ARG"
1598c2ecf20Sopenharmony_cifi
1608c2ecf20Sopenharmony_ciecho $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" $TORTURE_QEMU_GDB_ARG > $resdir/qemu-cmd
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciif test -n "$TORTURE_BUILDONLY"
1638c2ecf20Sopenharmony_cithen
1648c2ecf20Sopenharmony_ci	echo Build-only run specified, boot/test omitted.
1658c2ecf20Sopenharmony_ci	touch $resdir/buildonly
1668c2ecf20Sopenharmony_ci	exit 0
1678c2ecf20Sopenharmony_cifi
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci# Decorate qemu-cmd with redirection, backgrounding, and PID capture
1708c2ecf20Sopenharmony_cised -e 's/$/ 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd
1718c2ecf20Sopenharmony_ciecho 'echo $! > $resdir/qemu_pid' >> $T/qemu-cmd
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci# In case qemu refuses to run...
1748c2ecf20Sopenharmony_ciecho "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci# Attempt to run qemu
1778c2ecf20Sopenharmony_ci( . $T/qemu-cmd; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
1788c2ecf20Sopenharmony_cicommandcompleted=0
1798c2ecf20Sopenharmony_ciif test -z "$TORTURE_KCONFIG_GDB_ARG"
1808c2ecf20Sopenharmony_cithen
1818c2ecf20Sopenharmony_ci	sleep 10 # Give qemu's pid a chance to reach the file
1828c2ecf20Sopenharmony_ci	if test -s "$resdir/qemu_pid"
1838c2ecf20Sopenharmony_ci	then
1848c2ecf20Sopenharmony_ci		qemu_pid=`cat "$resdir/qemu_pid"`
1858c2ecf20Sopenharmony_ci		echo Monitoring qemu job at pid $qemu_pid
1868c2ecf20Sopenharmony_ci	else
1878c2ecf20Sopenharmony_ci		qemu_pid=""
1888c2ecf20Sopenharmony_ci		echo Monitoring qemu job at yet-as-unknown pid
1898c2ecf20Sopenharmony_ci	fi
1908c2ecf20Sopenharmony_cifi
1918c2ecf20Sopenharmony_ciif test -n "$TORTURE_KCONFIG_GDB_ARG"
1928c2ecf20Sopenharmony_cithen
1938c2ecf20Sopenharmony_ci	echo Waiting for you to attach a debug session, for example: > /dev/tty
1948c2ecf20Sopenharmony_ci	echo "    gdb $base_resdir/vmlinux" > /dev/tty
1958c2ecf20Sopenharmony_ci	echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty
1968c2ecf20Sopenharmony_ci	echo "    target remote :1234" > /dev/tty
1978c2ecf20Sopenharmony_ci	echo "    continue" > /dev/tty
1988c2ecf20Sopenharmony_ci	kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
1998c2ecf20Sopenharmony_cifi
2008c2ecf20Sopenharmony_ciwhile :
2018c2ecf20Sopenharmony_cido
2028c2ecf20Sopenharmony_ci	if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
2038c2ecf20Sopenharmony_ci	then
2048c2ecf20Sopenharmony_ci		qemu_pid=`cat "$resdir/qemu_pid"`
2058c2ecf20Sopenharmony_ci	fi
2068c2ecf20Sopenharmony_ci	kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
2078c2ecf20Sopenharmony_ci	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
2088c2ecf20Sopenharmony_ci	then
2098c2ecf20Sopenharmony_ci		if test $kruntime -ge $seconds -o -f "$TORTURE_STOPFILE"
2108c2ecf20Sopenharmony_ci		then
2118c2ecf20Sopenharmony_ci			break;
2128c2ecf20Sopenharmony_ci		fi
2138c2ecf20Sopenharmony_ci		sleep 1
2148c2ecf20Sopenharmony_ci	else
2158c2ecf20Sopenharmony_ci		commandcompleted=1
2168c2ecf20Sopenharmony_ci		if test $kruntime -lt $seconds
2178c2ecf20Sopenharmony_ci		then
2188c2ecf20Sopenharmony_ci			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
2198c2ecf20Sopenharmony_ci			grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
2208c2ecf20Sopenharmony_ci			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
2218c2ecf20Sopenharmony_ci			if test -n "$killpid"
2228c2ecf20Sopenharmony_ci			then
2238c2ecf20Sopenharmony_ci				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
2248c2ecf20Sopenharmony_ci				ps -fp $killpid >> $resdir/Warnings 2>&1
2258c2ecf20Sopenharmony_ci			fi
2268c2ecf20Sopenharmony_ci		else
2278c2ecf20Sopenharmony_ci			echo ' ---' `date`: "Kernel done"
2288c2ecf20Sopenharmony_ci		fi
2298c2ecf20Sopenharmony_ci		break
2308c2ecf20Sopenharmony_ci	fi
2318c2ecf20Sopenharmony_cidone
2328c2ecf20Sopenharmony_ciif test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
2338c2ecf20Sopenharmony_cithen
2348c2ecf20Sopenharmony_ci	qemu_pid=`cat "$resdir/qemu_pid"`
2358c2ecf20Sopenharmony_cifi
2368c2ecf20Sopenharmony_ciif test $commandcompleted -eq 0 -a -n "$qemu_pid"
2378c2ecf20Sopenharmony_cithen
2388c2ecf20Sopenharmony_ci	if ! test -f "$TORTURE_STOPFILE"
2398c2ecf20Sopenharmony_ci	then
2408c2ecf20Sopenharmony_ci		echo Grace period for qemu job at pid $qemu_pid
2418c2ecf20Sopenharmony_ci	fi
2428c2ecf20Sopenharmony_ci	oldline="`tail $resdir/console.log`"
2438c2ecf20Sopenharmony_ci	while :
2448c2ecf20Sopenharmony_ci	do
2458c2ecf20Sopenharmony_ci		if test -f "$TORTURE_STOPFILE"
2468c2ecf20Sopenharmony_ci		then
2478c2ecf20Sopenharmony_ci			echo "PID $qemu_pid killed due to run STOP request" >> $resdir/Warnings 2>&1
2488c2ecf20Sopenharmony_ci			kill -KILL $qemu_pid
2498c2ecf20Sopenharmony_ci			break
2508c2ecf20Sopenharmony_ci		fi
2518c2ecf20Sopenharmony_ci		kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
2528c2ecf20Sopenharmony_ci		if kill -0 $qemu_pid > /dev/null 2>&1
2538c2ecf20Sopenharmony_ci		then
2548c2ecf20Sopenharmony_ci			:
2558c2ecf20Sopenharmony_ci		else
2568c2ecf20Sopenharmony_ci			break
2578c2ecf20Sopenharmony_ci		fi
2588c2ecf20Sopenharmony_ci		must_continue=no
2598c2ecf20Sopenharmony_ci		newline="`tail $resdir/console.log`"
2608c2ecf20Sopenharmony_ci		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
2618c2ecf20Sopenharmony_ci		then
2628c2ecf20Sopenharmony_ci			must_continue=yes
2638c2ecf20Sopenharmony_ci		fi
2648c2ecf20Sopenharmony_ci		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
2658c2ecf20Sopenharmony_ci		if test -z "$last_ts"
2668c2ecf20Sopenharmony_ci		then
2678c2ecf20Sopenharmony_ci			last_ts=0
2688c2ecf20Sopenharmony_ci		fi
2698c2ecf20Sopenharmony_ci		if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
2708c2ecf20Sopenharmony_ci		then
2718c2ecf20Sopenharmony_ci			must_continue=yes
2728c2ecf20Sopenharmony_ci		fi
2738c2ecf20Sopenharmony_ci		if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
2748c2ecf20Sopenharmony_ci		then
2758c2ecf20Sopenharmony_ci			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
2768c2ecf20Sopenharmony_ci			kill -KILL $qemu_pid
2778c2ecf20Sopenharmony_ci			break
2788c2ecf20Sopenharmony_ci		fi
2798c2ecf20Sopenharmony_ci		oldline=$newline
2808c2ecf20Sopenharmony_ci		sleep 10
2818c2ecf20Sopenharmony_ci	done
2828c2ecf20Sopenharmony_cielif test -z "$qemu_pid"
2838c2ecf20Sopenharmony_cithen
2848c2ecf20Sopenharmony_ci	echo Unknown PID, cannot kill qemu command
2858c2ecf20Sopenharmony_cifi
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciparse-console.sh $resdir/console.log $title
288