1#!/bin/bash
2#
3# Shell functions for the rest of the scripts.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, you can access it online at
17# http://www.gnu.org/licenses/gpl-2.0.html.
18#
19# Copyright (C) IBM Corporation, 2013
20#
21# Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
22
23# bootparam_hotplug_cpu bootparam-string
24#
25# Returns 1 if the specified boot-parameter string tells rcutorture to
26# test CPU-hotplug operations.
27bootparam_hotplug_cpu () {
28	echo "$1" | grep -q "rcutorture\.onoff_"
29}
30
31# checkarg --argname argtype $# arg mustmatch cannotmatch
32#
33# Checks the specified argument "arg" against the mustmatch and cannotmatch
34# patterns.
35checkarg () {
36	if test $3 -le 1
37	then
38		echo $1 needs argument $2 matching \"$5\"
39		usage
40	fi
41	if echo "$4" | grep -q -e "$5"
42	then
43		:
44	else
45		echo $1 $2 \"$4\" must match \"$5\"
46		usage
47	fi
48	if echo "$4" | grep -q -e "$6"
49	then
50		echo $1 $2 \"$4\" must not match \"$6\"
51		usage
52	fi
53}
54
55# configfrag_boot_params bootparam-string config-fragment-file
56#
57# Adds boot parameters from the .boot file, if any.
58configfrag_boot_params () {
59	if test -r "$2.boot"
60	then
61		echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '`
62	else
63		echo $1
64	fi
65}
66
67# configfrag_hotplug_cpu config-fragment-file
68#
69# Returns 1 if the config fragment specifies hotplug CPU.
70configfrag_hotplug_cpu () {
71	if test ! -r "$1"
72	then
73		echo Unreadable config fragment "$1" 1>&2
74		exit -1
75	fi
76	grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
77}
78
79# identify_qemu builddir
80#
81# Returns our best guess as to which qemu command is appropriate for
82# the kernel at hand.  Override with the RCU_QEMU_CMD environment variable.
83identify_qemu () {
84	local u="`file "$1"`"
85	if test -n "$RCU_QEMU_CMD"
86	then
87		echo $RCU_QEMU_CMD
88	elif echo $u | grep -q x86-64
89	then
90		echo qemu-system-x86_64
91	elif echo $u | grep -q "Intel 80386"
92	then
93		echo qemu-system-i386
94	elif uname -a | grep -q ppc64
95	then
96		echo qemu-system-ppc64
97	else
98		echo Cannot figure out what qemu command to use! 1>&2
99		echo file $1 output: $u
100		# Usually this will be one of /usr/bin/qemu-system-*
101		# Use RCU_QEMU_CMD environment variable or appropriate
102		# argument to top-level script.
103		exit 1
104	fi
105}
106
107# identify_qemu_append qemu-cmd
108#
109# Output arguments for the qemu "-append" string based on CPU type
110# and the RCU_QEMU_INTERACTIVE environment variable.
111identify_qemu_append () {
112	case "$1" in
113	qemu-system-x86_64|qemu-system-i386)
114		echo noapic selinux=0 initcall_debug debug
115		;;
116	esac
117	if test -n "$RCU_QEMU_INTERACTIVE"
118	then
119		echo root=/dev/sda
120	else
121		echo console=ttyS0
122	fi
123}
124
125# identify_qemu_args qemu-cmd serial-file
126#
127# Output arguments for qemu arguments based on the RCU_QEMU_MAC
128# and RCU_QEMU_INTERACTIVE environment variables.
129identify_qemu_args () {
130	case "$1" in
131	qemu-system-x86_64|qemu-system-i386)
132		;;
133	qemu-system-ppc64)
134		echo -enable-kvm -M pseries -cpu POWER7 -nodefaults
135		echo -device spapr-vscsi
136		if test -n "$RCU_QEMU_INTERACTIVE" -a -n "$RCU_QEMU_MAC"
137		then
138			echo -device spapr-vlan,netdev=net0,mac=$RCU_QEMU_MAC
139			echo -netdev bridge,br=br0,id=net0
140		elif test -n "$RCU_QEMU_INTERACTIVE"
141		then
142			echo -net nic -net user
143		fi
144		;;
145	esac
146	if test -n "$RCU_QEMU_INTERACTIVE"
147	then
148		echo -monitor stdio -serial pty -S
149	else
150		echo -serial file:$2
151	fi
152}
153
154# identify_qemu_vcpus
155#
156# Returns the number of virtual CPUs available to the aggregate of the
157# guest OSes.
158identify_qemu_vcpus () {
159	lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
160}
161
162# print_bug
163#
164# Prints "BUG: " in red followed by remaining arguments
165print_bug () {
166	printf '\033[031mBUG: \033[m'
167	echo $*
168}
169
170# print_warning
171#
172# Prints "WARNING: " in yellow followed by remaining arguments
173print_warning () {
174	printf '\033[033mWARNING: \033[m'
175	echo $*
176}
177
178# specify_qemu_cpus qemu-cmd qemu-args #cpus
179#
180# Appends a string containing "-smp XXX" to qemu-args, unless the incoming
181# qemu-args already contains "-smp".
182specify_qemu_cpus () {
183	local nt;
184
185	if echo $2 | grep -q -e -smp
186	then
187		echo $2
188	else
189		case "$1" in
190		qemu-system-x86_64|qemu-system-i386)
191			echo $2 -smp $3
192			;;
193		qemu-system-ppc64)
194			nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
195			echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt
196			;;
197		esac
198	fi
199}
200