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