summaryrefslogtreecommitdiff
path: root/minix/kernel/watchdog.c
blob: 65bb9ff030511b6f7320e4106cc86913f44eca3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * This is arch independent NMI watchdog implementation part. It is used to
 * detect kernel lockups and help debugging. each architecture must add its own
 * low level code that triggers periodic checks
 */

#include "watchdog.h"
#include "arch/i386/glo.h"

unsigned watchdog_local_timer_ticks = 0U;
struct arch_watchdog *watchdog;
int watchdog_enabled;

static void lockup_check(struct nmi_frame * frame)
{
	/* FIXME this should be CPU local */
	static unsigned no_ticks;
	static unsigned last_tick_count = (unsigned) -1;

	/*
	 * when debugging on serial console, printing takes a lot of time some
	 * times while the kernel is certainly not locked up. We don't want to
	 * report a lockup in such situation
	 */
	if (serial_debug_active)
		return;

	if (last_tick_count != watchdog_local_timer_ticks) {
		if (no_ticks == 1) {
			printf("watchdog : kernel unlocked\n");
			no_ticks = 0;
		}
		/* we are still ticking, everything seems good */
		last_tick_count = watchdog_local_timer_ticks;
		return;
	}

	/*
	 * if watchdog_local_timer_ticks didn't changed since last time, give it
	 * some more time and only if it still dead, trigger the watchdog alarm
	 */
	if (++no_ticks < 10) {
		if (no_ticks == 1)
			printf("WARNING watchdog : possible kernel lockup\n");
		return;
	}

	/* if we get this far, the kernel is locked up */
	arch_watchdog_lockup(frame);
}

void nmi_watchdog_handler(struct nmi_frame * frame)
{
#if SPROFILE
	/*
	 * Do not check for lockups while profiling, it is extremely likely that
	 * a false positive is detected if the frequency is high
	 */
	if (watchdog_enabled && !sprofiling)
		lockup_check(frame);
	if (sprofiling)
		nmi_sprofile_handler(frame);
	
	if ((watchdog_enabled || sprofiling) && watchdog->reinit)
		watchdog->reinit(cpuid);
#else
	if (watchdog_enabled) {
		lockup_check(frame);
		if (watchdog->reinit)
			watchdog->reinit(cpuid);
	}
#endif
}

int nmi_watchdog_start_profiling(const unsigned freq)
{
	int err;
	
	/* if watchdog hasn't been enabled, we must enable it now */
	if (!watchdog_enabled) {
		if (arch_watchdog_init())
			return ENODEV;
	}

	if (!watchdog->profile_init) {
		printf("WARNING NMI watchdog profiling not supported\n");
		nmi_watchdog_stop_profiling();
		return ENODEV;
	}

	err = watchdog->profile_init(freq);
	if (err != OK)
		return err;

	watchdog->resetval = watchdog->profile_resetval;

	return OK;
}

void nmi_watchdog_stop_profiling(void)
{
	/*
	 * if we do not rearm the NMI source, we are done, if we want to keep
	 * the watchdog running, we reset is to its normal value
	 */

	if (watchdog)
		watchdog->resetval = watchdog->watchdog_resetval;

	if (!watchdog_enabled)
		arch_watchdog_stop();
}