1*29e3ea8cSJordan Niethe#!/bin/bash
2*29e3ea8cSJordan Niethe# SPDX-License-Identifier: GPL-2.0-or-later
3*29e3ea8cSJordan Niethe
4*29e3ea8cSJordan NietheTIMEOUT=30
5*29e3ea8cSJordan Niethe
6*29e3ea8cSJordan NietheDEBUFS_DIR=`cat /proc/mounts | grep debugfs | awk '{print $2}'`
7*29e3ea8cSJordan Nietheif [ ! -e "$DEBUFS_DIR" ]
8*29e3ea8cSJordan Niethethen
9*29e3ea8cSJordan Niethe	echo "debugfs not found, skipping" 1>&2
10*29e3ea8cSJordan Niethe	exit 4
11*29e3ea8cSJordan Niethefi
12*29e3ea8cSJordan Niethe
13*29e3ea8cSJordan Nietheif [ ! -e "$DEBUFS_DIR/tracing/current_tracer" ]
14*29e3ea8cSJordan Niethethen
15*29e3ea8cSJordan Niethe	echo "Tracing files not found, skipping" 1>&2
16*29e3ea8cSJordan Niethe	exit 4
17*29e3ea8cSJordan Niethefi
18*29e3ea8cSJordan Niethe
19*29e3ea8cSJordan Niethe
20*29e3ea8cSJordan Nietheecho "Testing for spurious faults when mapping kernel memory..."
21*29e3ea8cSJordan Niethe
22*29e3ea8cSJordan Nietheif grep -q "FUNCTION TRACING IS CORRUPTED" "$DEBUFS_DIR/tracing/trace"
23*29e3ea8cSJordan Niethethen
24*29e3ea8cSJordan Niethe	echo "FAILED: Ftrace already dead. Probably due to a spurious fault" 1>&2
25*29e3ea8cSJordan Niethe	exit 1
26*29e3ea8cSJordan Niethefi
27*29e3ea8cSJordan Niethe
28*29e3ea8cSJordan Niethedmesg -C
29*29e3ea8cSJordan NietheSTART_TIME=`date +%s`
30*29e3ea8cSJordan NietheEND_TIME=`expr $START_TIME + $TIMEOUT`
31*29e3ea8cSJordan Niethewhile [ `date +%s` -lt $END_TIME ]
32*29e3ea8cSJordan Niethedo
33*29e3ea8cSJordan Niethe	echo function > $DEBUFS_DIR/tracing/current_tracer
34*29e3ea8cSJordan Niethe	echo nop > $DEBUFS_DIR/tracing/current_tracer
35*29e3ea8cSJordan Niethe	if dmesg | grep -q 'ftrace bug'
36*29e3ea8cSJordan Niethe	then
37*29e3ea8cSJordan Niethe		break
38*29e3ea8cSJordan Niethe	fi
39*29e3ea8cSJordan Niethedone
40*29e3ea8cSJordan Niethe
41*29e3ea8cSJordan Nietheecho nop > $DEBUFS_DIR/tracing/current_tracer
42*29e3ea8cSJordan Nietheif dmesg | grep -q 'ftrace bug'
43*29e3ea8cSJordan Niethethen
44*29e3ea8cSJordan Niethe	echo "FAILED: Mapping kernel memory causes spurious faults" 1>&2
45*29e3ea8cSJordan Niethe	exit 1
46*29e3ea8cSJordan Nietheelse
47*29e3ea8cSJordan Niethe	echo "OK: Mapping kernel memory does not cause spurious faults"
48*29e3ea8cSJordan Niethe	exit 0
49*29e3ea8cSJordan Niethefi
50