summaryrefslogtreecommitdiff
path: root/include/rv/ltl_monitor.h
blob: 67031a774e3d3c1e07653f5ddffca433f0ec4493 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* SPDX-License-Identifier: GPL-2.0 */
/**
 * This file must be combined with the $(MODEL_NAME).h file generated by
 * tools/verification/rvgen.
 */

#include <linux/args.h>
#include <linux/rv.h>
#include <linux/stringify.h>
#include <linux/seq_buf.h>
#include <rv/instrumentation.h>
#include <trace/events/task.h>
#include <trace/events/sched.h>

#ifndef MONITOR_NAME
#error "Please include $(MODEL_NAME).h generated by rvgen"
#endif

#ifdef CONFIG_RV_REACTORS
#define RV_MONITOR_NAME CONCATENATE(rv_, MONITOR_NAME)
static struct rv_monitor RV_MONITOR_NAME;

static void rv_cond_react(struct task_struct *task)
{
	if (!rv_reacting_on() || !RV_MONITOR_NAME.react)
		return;
	RV_MONITOR_NAME.react("rv: "__stringify(MONITOR_NAME)": %s[%d]: violation detected\n",
			      task->comm, task->pid);
}
#else
static void rv_cond_react(struct task_struct *task)
{
}
#endif

static int ltl_monitor_slot = RV_PER_TASK_MONITOR_INIT;

static void ltl_atoms_fetch(struct task_struct *task, struct ltl_monitor *mon);
static void ltl_atoms_init(struct task_struct *task, struct ltl_monitor *mon, bool task_creation);

static struct ltl_monitor *ltl_get_monitor(struct task_struct *task)
{
	return &task->rv[ltl_monitor_slot].ltl_mon;
}

static void ltl_task_init(struct task_struct *task, bool task_creation)
{
	struct ltl_monitor *mon = ltl_get_monitor(task);

	memset(&mon->states, 0, sizeof(mon->states));

	for (int i = 0; i < LTL_NUM_ATOM; ++i)
		__set_bit(i, mon->unknown_atoms);

	ltl_atoms_init(task, mon, task_creation);
	ltl_atoms_fetch(task, mon);
}

static void handle_task_newtask(void *data, struct task_struct *task, unsigned long flags)
{
	ltl_task_init(task, true);
}

static int ltl_monitor_init(void)
{
	struct task_struct *g, *p;
	int ret, cpu;

	ret = rv_get_task_monitor_slot();
	if (ret < 0)
		return ret;

	ltl_monitor_slot = ret;

	rv_attach_trace_probe(name, task_newtask, handle_task_newtask);

	read_lock(&tasklist_lock);

	for_each_process_thread(g, p)
		ltl_task_init(p, false);

	for_each_present_cpu(cpu)
		ltl_task_init(idle_task(cpu), false);

	read_unlock(&tasklist_lock);

	return 0;
}

static void ltl_monitor_destroy(void)
{
	rv_detach_trace_probe(name, task_newtask, handle_task_newtask);

	rv_put_task_monitor_slot(ltl_monitor_slot);
	ltl_monitor_slot = RV_PER_TASK_MONITOR_INIT;
}

static void ltl_illegal_state(struct task_struct *task, struct ltl_monitor *mon)
{
	CONCATENATE(trace_error_, MONITOR_NAME)(task);
	rv_cond_react(task);
}

static void ltl_attempt_start(struct task_struct *task, struct ltl_monitor *mon)
{
	if (rv_ltl_all_atoms_known(mon))
		ltl_start(task, mon);
}

static inline void ltl_atom_set(struct ltl_monitor *mon, enum ltl_atom atom, bool value)
{
	__clear_bit(atom, mon->unknown_atoms);
	if (value)
		__set_bit(atom, mon->atoms);
	else
		__clear_bit(atom, mon->atoms);
}

static void
ltl_trace_event(struct task_struct *task, struct ltl_monitor *mon, unsigned long *next_state)
{
	const char *format_str = "%s";
	DECLARE_SEQ_BUF(atoms, 64);
	char states[32], next[32];
	int i;

	if (!CONCATENATE(CONCATENATE(trace_event_, MONITOR_NAME), _enabled)())
		return;

	snprintf(states, sizeof(states), "%*pbl", RV_MAX_BA_STATES, mon->states);
	snprintf(next, sizeof(next), "%*pbl", RV_MAX_BA_STATES, next_state);

	for (i = 0; i < LTL_NUM_ATOM; ++i) {
		if (test_bit(i, mon->atoms)) {
			seq_buf_printf(&atoms, format_str, ltl_atom_str(i));
			format_str = ",%s";
		}
	}

	CONCATENATE(trace_event_, MONITOR_NAME)(task, states, atoms.buffer, next);
}

static void ltl_validate(struct task_struct *task, struct ltl_monitor *mon)
{
	DECLARE_BITMAP(next_states, RV_MAX_BA_STATES) = {0};

	if (!rv_ltl_valid_state(mon))
		return;

	for (unsigned int i = 0; i < RV_NUM_BA_STATES; ++i) {
		if (test_bit(i, mon->states))
			ltl_possible_next_states(mon, i, next_states);
	}

	ltl_trace_event(task, mon, next_states);

	memcpy(mon->states, next_states, sizeof(next_states));

	if (!rv_ltl_valid_state(mon))
		ltl_illegal_state(task, mon);
}

static void ltl_atom_update(struct task_struct *task, enum ltl_atom atom, bool value)
{
	struct ltl_monitor *mon = ltl_get_monitor(task);

	ltl_atom_set(mon, atom, value);
	ltl_atoms_fetch(task, mon);

	if (!rv_ltl_valid_state(mon)) {
		ltl_attempt_start(task, mon);
		return;
	}

	ltl_validate(task, mon);
}

static void __maybe_unused ltl_atom_pulse(struct task_struct *task, enum ltl_atom atom, bool value)
{
	struct ltl_monitor *mon = ltl_get_monitor(task);

	ltl_atom_update(task, atom, value);

	ltl_atom_set(mon, atom, !value);
	ltl_validate(task, mon);
}