134f7f798SMichael Ellerman#!/usr/bin/env bash
234f7f798SMichael Ellerman
334f7f798SMichael Ellermanset -euo pipefail
434f7f798SMichael Ellerman
534f7f798SMichael EllermanTIMEOUT=10
634f7f798SMichael Ellerman
734f7f798SMichael Ellermanfunction do_one
834f7f798SMichael Ellerman{
934f7f798SMichael Ellerman    local mitigation="$1"
1034f7f798SMichael Ellerman    local orig
1134f7f798SMichael Ellerman    local start
1234f7f798SMichael Ellerman    local now
1334f7f798SMichael Ellerman
1434f7f798SMichael Ellerman    orig=$(cat "$mitigation")
1534f7f798SMichael Ellerman
16cb662608SRussell Currey    start=$(date +%s)
1734f7f798SMichael Ellerman    now=$start
1834f7f798SMichael Ellerman
1934f7f798SMichael Ellerman    while [[ $((now-start)) -lt "$TIMEOUT" ]]
2034f7f798SMichael Ellerman    do
2134f7f798SMichael Ellerman        echo 0 > "$mitigation"
2234f7f798SMichael Ellerman        echo 1 > "$mitigation"
2334f7f798SMichael Ellerman
24cb662608SRussell Currey        now=$(date +%s)
2534f7f798SMichael Ellerman    done
2634f7f798SMichael Ellerman
2734f7f798SMichael Ellerman    echo "$orig" > "$mitigation"
2834f7f798SMichael Ellerman}
2934f7f798SMichael Ellerman
3034f7f798SMichael Ellermanrc=0
3134f7f798SMichael Ellermancd /sys/kernel/debug/powerpc || rc=1
3234f7f798SMichael Ellermanif [[ "$rc" -ne 0 ]]; then
3334f7f798SMichael Ellerman    echo "Error: couldn't cd to /sys/kernel/debug/powerpc" >&2
3434f7f798SMichael Ellerman    exit 1
3534f7f798SMichael Ellermanfi
3634f7f798SMichael Ellerman
3734f7f798SMichael Ellermantainted=$(cat /proc/sys/kernel/tainted)
3834f7f798SMichael Ellermanif [[ "$tainted" -ne 0 ]]; then
39*a8a54a65SMichael Ellerman    echo "Warning: kernel already tainted! ($tainted)" >&2
4034f7f798SMichael Ellermanfi
4134f7f798SMichael Ellerman
4234f7f798SMichael Ellermanmitigations="barrier_nospec stf_barrier count_cache_flush rfi_flush entry_flush uaccess_flush"
4334f7f798SMichael Ellerman
4434f7f798SMichael Ellermanfor m in $mitigations
4534f7f798SMichael Ellermando
4618678591SSachin Sant    if [[ -f /sys/kernel/debug/powerpc/$m ]]
4718678591SSachin Sant    then
4834f7f798SMichael Ellerman        do_one "$m" &
4918678591SSachin Sant    fi
5034f7f798SMichael Ellermandone
5134f7f798SMichael Ellerman
5234f7f798SMichael Ellermanecho "Spawned threads enabling/disabling mitigations ..."
5334f7f798SMichael Ellerman
5434f7f798SMichael Ellermanif stress-ng > /dev/null 2>&1; then
5534f7f798SMichael Ellerman    stress="stress-ng"
5634f7f798SMichael Ellermanelif stress > /dev/null 2>&1; then
5734f7f798SMichael Ellerman    stress="stress"
5834f7f798SMichael Ellermanelse
5934f7f798SMichael Ellerman    stress=""
6034f7f798SMichael Ellermanfi
6134f7f798SMichael Ellerman
6234f7f798SMichael Ellermanif [[ -n "$stress" ]]; then
6334f7f798SMichael Ellerman    "$stress" -m "$(nproc)" -t "$TIMEOUT" &
6434f7f798SMichael Ellerman    echo "Spawned VM stressors ..."
6534f7f798SMichael Ellermanfi
6634f7f798SMichael Ellerman
6734f7f798SMichael Ellermanecho "Waiting for timeout ..."
6834f7f798SMichael Ellermanwait
6934f7f798SMichael Ellerman
70*a8a54a65SMichael Ellermanorig_tainted=$tainted
7134f7f798SMichael Ellermantainted=$(cat /proc/sys/kernel/tainted)
72*a8a54a65SMichael Ellermanif [[ "$tainted" != "$orig_tainted" ]]; then
73*a8a54a65SMichael Ellerman    echo "Error: kernel newly tainted, before ($orig_tainted) after ($tainted)" >&2
7434f7f798SMichael Ellerman    exit 1
7534f7f798SMichael Ellermanfi
7634f7f798SMichael Ellerman
7734f7f798SMichael Ellermanecho "OK"
7834f7f798SMichael Ellermanexit 0
79