1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Run a kvm-based test of the specified tree on the specified configs.
5# Fully automated run and error checking, no graphics console.
6#
7# Execute this in the source tree.  Do not run it as a background task
8# because qemu does not seem to like that much.
9#
10# Usage: kvm-test-1-run.sh config resdir seconds qemu-args boot_args_in
11#
12# qemu-args defaults to "-enable-kvm -nographic", along with arguments
13#			specifying the number of CPUs and other options
14#			generated from the underlying CPU architecture.
15# boot_args_in defaults to value returned by the per_version_boot_params
16#			shell function.
17#
18# Anything you specify for either qemu-args or boot_args_in is appended to
19# the default values.  The "-smp" value is deduced from the contents of
20# the config fragment.
21#
22# More sophisticated argument parsing is clearly needed.
23#
24# Copyright (C) IBM Corporation, 2011
25#
26# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
27
28T="`mktemp -d ${TMPDIR-/tmp}/kvm-test-1-run.sh.XXXXXX`"
29trap 'rm -rf $T' 0
30
31. functions.sh
32. $CONFIGFRAG/ver_functions.sh
33
34config_template=${1}
35config_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
36title=`echo $config_template | sed -e 's/^.*\///'`
37resdir=${2}
38if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
39then
40	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
41	exit 1
42fi
43echo ' ---' `date`: Starting build, PID $$
44echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
45touch $resdir/ConfigFragment.input
46
47# Combine additional Kconfig options into an existing set such that
48# newer options win.  The first argument is the Kconfig source ID, the
49# second the to-be-updated file within $T, and the third and final the
50# list of additional Kconfig options.  Note that a $2.tmp file is
51# created when doing the update.
52config_override_param () {
53	if test -n "$3"
54	then
55		echo $3 | sed -e 's/^ *//' -e 's/ *$//' | tr -s " " "\012" > $T/Kconfig_args
56		echo " --- $1" >> $resdir/ConfigFragment.input
57		cat $T/Kconfig_args >> $resdir/ConfigFragment.input
58		config_override.sh $T/$2 $T/Kconfig_args > $T/$2.tmp
59		mv $T/$2.tmp $T/$2
60		# Note that "#CHECK#" is not permitted on commandline.
61	fi
62}
63
64echo > $T/KcList
65config_override_param "$config_dir/CFcommon" KcList "`cat $config_dir/CFcommon 2> /dev/null`"
66config_override_param "$config_template" KcList "`cat $config_template 2> /dev/null`"
67config_override_param "--gdb options" KcList "$TORTURE_KCONFIG_GDB_ARG"
68config_override_param "--kasan options" KcList "$TORTURE_KCONFIG_KASAN_ARG"
69config_override_param "--kcsan options" KcList "$TORTURE_KCONFIG_KCSAN_ARG"
70config_override_param "--kconfig argument" KcList "$TORTURE_KCONFIG_ARG"
71cp $T/KcList $resdir/ConfigFragment
72
73base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
74if test "$base_resdir" != "$resdir" && test -f $base_resdir/bzImage && test -f $base_resdir/vmlinux
75then
76	# Rerunning previous test, so use that test's kernel.
77	QEMU="`identify_qemu $base_resdir/vmlinux`"
78	BOOT_IMAGE="`identify_boot_image $QEMU`"
79	KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
80	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
81	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
82	# Arch-independent indicator
83	touch $resdir/builtkernel
84elif test "$base_resdir" != "$resdir"
85then
86	# Rerunning previous test for which build failed
87	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
88	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
89	echo Initial build failed, not running KVM, see $resdir.
90	if test -f $resdir/build.wait
91	then
92		mv $resdir/build.wait $resdir/build.ready
93	fi
94	exit 1
95elif kvm-build.sh $T/KcList $resdir
96then
97	# Had to build a kernel for this test.
98	QEMU="`identify_qemu vmlinux`"
99	BOOT_IMAGE="`identify_boot_image $QEMU`"
100	cp vmlinux $resdir
101	cp .config $resdir
102	cp Module.symvers $resdir > /dev/null || :
103	cp System.map $resdir > /dev/null || :
104	if test -n "$BOOT_IMAGE"
105	then
106		cp $BOOT_IMAGE $resdir
107		KERNEL=$resdir/${BOOT_IMAGE##*/}
108		# Arch-independent indicator
109		touch $resdir/builtkernel
110	else
111		echo No identifiable boot image, not running KVM, see $resdir.
112		echo Do the torture scripts know about your architecture?
113	fi
114	parse-build.sh $resdir/Make.out $title
115else
116	# Build failed.
117	cp .config $resdir || :
118	echo Build failed, not running KVM, see $resdir.
119	if test -f $resdir/build.wait
120	then
121		mv $resdir/build.wait $resdir/build.ready
122	fi
123	exit 1
124fi
125if test -f $resdir/build.wait
126then
127	mv $resdir/build.wait $resdir/build.ready
128fi
129while test -f $resdir/build.ready
130do
131	sleep 1
132done
133seconds=$3
134qemu_args=$4
135boot_args_in=$5
136
137if test -z "$TORTURE_BUILDONLY"
138then
139	echo ' ---' `date`: Starting kernel
140fi
141
142# Generate -smp qemu argument.
143qemu_args="-enable-kvm -nographic $qemu_args"
144cpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
145cpu_count=`configfrag_boot_cpus "$boot_args_in" "$config_template" "$cpu_count"`
146if test "$cpu_count" -gt "$TORTURE_ALLOTED_CPUS"
147then
148	echo CPU count limited from $cpu_count to $TORTURE_ALLOTED_CPUS | tee -a $resdir/Warnings
149	cpu_count=$TORTURE_ALLOTED_CPUS
150fi
151qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
152qemu_args="`specify_qemu_net "$qemu_args"`"
153
154# Generate architecture-specific and interaction-specific qemu arguments
155qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
156
157# Generate qemu -append arguments
158qemu_append="`identify_qemu_append "$QEMU"`"
159
160# Pull in Kconfig-fragment boot parameters
161boot_args="`configfrag_boot_params "$boot_args_in" "$config_template"`"
162# Generate kernel-version-specific boot parameters
163boot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
164if test -n "$TORTURE_BOOT_GDB_ARG"
165then
166	boot_args="$boot_args $TORTURE_BOOT_GDB_ARG"
167fi
168
169# Give bare-metal advice
170modprobe_args="`echo $boot_args | tr -s ' ' '\012' | grep "^$TORTURE_MOD\." | sed -e "s/$TORTURE_MOD\.//g"`"
171kboot_args="`echo $boot_args | tr -s ' ' '\012' | grep -v "^$TORTURE_MOD\."`"
172testid_txt="`dirname $resdir`/testid.txt"
173touch $resdir/bare-metal
174echo To run this scenario on bare metal: >> $resdir/bare-metal
175echo >> $resdir/bare-metal
176echo " 1." Set your bare-metal build tree to the state shown in this file: >> $resdir/bare-metal
177echo "   " $testid_txt >> $resdir/bare-metal
178echo " 2." Update your bare-metal build tree"'"s .config based on this file: >> $resdir/bare-metal
179echo "   " $resdir/ConfigFragment >> $resdir/bare-metal
180echo " 3." Make the bare-metal kernel"'"s build system aware of your .config updates: >> $resdir/bare-metal
181echo "   " $ 'yes "" | make oldconfig' >> $resdir/bare-metal
182echo " 4." Build your bare-metal kernel. >> $resdir/bare-metal
183echo " 5." Boot your bare-metal kernel with the following parameters: >> $resdir/bare-metal
184echo "   " $kboot_args >> $resdir/bare-metal
185echo " 6." Start the test with the following command: >> $resdir/bare-metal
186echo "   " $ modprobe $TORTURE_MOD $modprobe_args >> $resdir/bare-metal
187echo " 7." After some time, end the test with the following command: >> $resdir/bare-metal
188echo "   " $ rmmod $TORTURE_MOD >> $resdir/bare-metal
189echo " 8." Copy your bare-metal kernel"'"s .config file, overwriting this file: >> $resdir/bare-metal
190echo "   " $resdir/.config >> $resdir/bare-metal
191echo " 9." Copy the console output from just before the modprobe to just after >> $resdir/bare-metal
192echo "   " the rmmod into this file: >> $resdir/bare-metal
193echo "   " $resdir/console.log >> $resdir/bare-metal
194echo "10." Check for runtime errors using the following command: >> $resdir/bare-metal
195echo "   " $ tools/testing/selftests/rcutorture/bin/kvm-recheck.sh `dirname $resdir` >> $resdir/bare-metal
196echo >> $resdir/bare-metal
197echo Some of the above steps may be skipped if you build your bare-metal >> $resdir/bare-metal
198echo kernel here: `head -n 1 $testid_txt | sed -e 's/^Build directory: //'`  >> $resdir/bare-metal
199
200echo $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" $TORTURE_QEMU_GDB_ARG > $resdir/qemu-cmd
201echo "# TORTURE_SHUTDOWN_GRACE=$TORTURE_SHUTDOWN_GRACE" >> $resdir/qemu-cmd
202echo "# seconds=$seconds" >> $resdir/qemu-cmd
203echo "# TORTURE_KCONFIG_GDB_ARG=\"$TORTURE_KCONFIG_GDB_ARG\"" >> $resdir/qemu-cmd
204echo "# TORTURE_JITTER_START=\"$TORTURE_JITTER_START\"" >> $resdir/qemu-cmd
205echo "# TORTURE_JITTER_STOP=\"$TORTURE_JITTER_STOP\"" >> $resdir/qemu-cmd
206echo "# TORTURE_TRUST_MAKE=\"$TORTURE_TRUST_MAKE\"; export TORTURE_TRUST_MAKE" >> $resdir/qemu-cmd
207echo "# TORTURE_CPU_COUNT=$cpu_count" >> $resdir/qemu-cmd
208
209if test -n "$TORTURE_BUILDONLY"
210then
211	echo Build-only run specified, boot/test omitted.
212	touch $resdir/buildonly
213	exit 0
214fi
215
216kvm-test-1-run-qemu.sh $resdir
217parse-console.sh $resdir/console.log $title
218