1#!/usr/bin/env bash
2
3set -euo pipefail
4
5TIMEOUT=10
6
7function do_one
8{
9    local mitigation="$1"
10    local orig
11    local start
12    local now
13
14    orig=$(cat "$mitigation")
15
16    start=$EPOCHSECONDS
17    now=$start
18
19    while [[ $((now-start)) -lt "$TIMEOUT" ]]
20    do
21        echo 0 > "$mitigation"
22        echo 1 > "$mitigation"
23
24        now=$EPOCHSECONDS
25    done
26
27    echo "$orig" > "$mitigation"
28}
29
30rc=0
31cd /sys/kernel/debug/powerpc || rc=1
32if [[ "$rc" -ne 0 ]]; then
33    echo "Error: couldn't cd to /sys/kernel/debug/powerpc" >&2
34    exit 1
35fi
36
37tainted=$(cat /proc/sys/kernel/tainted)
38if [[ "$tainted" -ne 0 ]]; then
39    echo "Error: kernel already tainted!" >&2
40    exit 1
41fi
42
43mitigations="barrier_nospec stf_barrier count_cache_flush rfi_flush entry_flush uaccess_flush"
44
45for m in $mitigations
46do
47    do_one "$m" &
48done
49
50echo "Spawned threads enabling/disabling mitigations ..."
51
52if stress-ng > /dev/null 2>&1; then
53    stress="stress-ng"
54elif stress > /dev/null 2>&1; then
55    stress="stress"
56else
57    stress=""
58fi
59
60if [[ -n "$stress" ]]; then
61    "$stress" -m "$(nproc)" -t "$TIMEOUT" &
62    echo "Spawned VM stressors ..."
63fi
64
65echo "Waiting for timeout ..."
66wait
67
68tainted=$(cat /proc/sys/kernel/tainted)
69if [[ "$tainted" -ne 0 ]]; then
70    echo "Error: kernel became tainted!" >&2
71    exit 1
72fi
73
74echo "OK"
75exit 0
76