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