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
3934f7f798SMichael Ellerman    echo "Error: kernel already tainted!" >&2
4034f7f798SMichael Ellerman    exit 1
4134f7f798SMichael Ellermanfi
4234f7f798SMichael Ellerman
4334f7f798SMichael Ellermanmitigations="barrier_nospec stf_barrier count_cache_flush rfi_flush entry_flush uaccess_flush"
4434f7f798SMichael Ellerman
4534f7f798SMichael Ellermanfor m in $mitigations
4634f7f798SMichael Ellermando
47*18678591SSachin Sant    if [[ -f /sys/kernel/debug/powerpc/$m ]]
48*18678591SSachin Sant    then
4934f7f798SMichael Ellerman        do_one "$m" &
50*18678591SSachin Sant    fi
5134f7f798SMichael Ellermandone
5234f7f798SMichael Ellerman
5334f7f798SMichael Ellermanecho "Spawned threads enabling/disabling mitigations ..."
5434f7f798SMichael Ellerman
5534f7f798SMichael Ellermanif stress-ng > /dev/null 2>&1; then
5634f7f798SMichael Ellerman    stress="stress-ng"
5734f7f798SMichael Ellermanelif stress > /dev/null 2>&1; then
5834f7f798SMichael Ellerman    stress="stress"
5934f7f798SMichael Ellermanelse
6034f7f798SMichael Ellerman    stress=""
6134f7f798SMichael Ellermanfi
6234f7f798SMichael Ellerman
6334f7f798SMichael Ellermanif [[ -n "$stress" ]]; then
6434f7f798SMichael Ellerman    "$stress" -m "$(nproc)" -t "$TIMEOUT" &
6534f7f798SMichael Ellerman    echo "Spawned VM stressors ..."
6634f7f798SMichael Ellermanfi
6734f7f798SMichael Ellerman
6834f7f798SMichael Ellermanecho "Waiting for timeout ..."
6934f7f798SMichael Ellermanwait
7034f7f798SMichael Ellerman
7134f7f798SMichael Ellermantainted=$(cat /proc/sys/kernel/tainted)
7234f7f798SMichael Ellermanif [[ "$tainted" -ne 0 ]]; then
7334f7f798SMichael Ellerman    echo "Error: kernel became tainted!" >&2
7434f7f798SMichael Ellerman    exit 1
7534f7f798SMichael Ellermanfi
7634f7f798SMichael Ellerman
7734f7f798SMichael Ellermanecho "OK"
7834f7f798SMichael Ellermanexit 0
79