aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/ds_selftest.c
blob: e5a263c8a14cda4119c8ee9046a2e4e173d343c8 (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Debug Store support - selftest
 *
 *
 * Copyright (C) 2009 Intel Corporation.
 * Markus Metzger <markus.t.metzger@intel.com>, 2009
 */

#include "ds_selftest.h"

#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/smp.h>

#include <asm/ds.h>


#define BUFFER_SIZE 1021 /* Intentionally chose an odd size. */


static int ds_selftest_bts_consistency(const struct bts_trace *trace)
{
	int error = 0;

	if (!trace) {
		printk(KERN_CONT "failed to access trace...");
		/* Bail out. Other tests are pointless. */
		return -1;
	}

	if (!trace->read) {
		printk(KERN_CONT "bts read not available...");
		error = -1;
	}

	/* Do some sanity checks on the trace configuration. */
	if (!trace->ds.n) {
		printk(KERN_CONT "empty bts buffer...");
		error = -1;
	}
	if (!trace->ds.size) {
		printk(KERN_CONT "bad bts trace setup...");
		error = -1;
	}
	if (trace->ds.end !=
	    (char *)trace->ds.begin + (trace->ds.n * trace->ds.size)) {
		printk(KERN_CONT "bad bts buffer setup...");
		error = -1;
	}
	if ((trace->ds.top < trace->ds.begin) ||
	    (trace->ds.end <= trace->ds.top)) {
		printk(KERN_CONT "bts top out of bounds...");
		error = -1;
	}

	return error;
}

static int ds_selftest_bts_read(struct bts_tracer *tracer,
				const struct bts_trace *trace,
				const void *from, const void *to)
{
	const unsigned char *at;

	/*
	 * Check a few things which do not belong to this test.
	 * They should be covered by other tests.
	 */
	if (!trace)
		return -1;

	if (!trace->read)
		return -1;

	if (to < from)
		return -1;

	if (from < trace->ds.begin)
		return -1;

	if (trace->ds.end < to)
		return -1;

	if (!trace->ds.size)
		return -1;

	/* Now to the test itself. */
	for (at = from; (void *)at < to; at += trace->ds.size) {
		struct bts_struct bts;
		size_t index;
		int error;

		if (((void *)at - trace->ds.begin) % trace->ds.size) {
			printk(KERN_CONT
			       "read from non-integer index...");
			return -1;
		}
		index = ((void *)at - trace->ds.begin) / trace->ds.size;

		memset(&bts, 0, sizeof(bts));
		error = trace->read(tracer, at, &bts);
		if (error < 0) {
			printk(KERN_CONT
			       "error reading bts trace at [%lu] (0x%p)...",
			       index, at);
			return error;
		}

		switch (bts.qualifier) {
		case BTS_BRANCH:
			break;
		default:
			printk(KERN_CONT
			       "unexpected bts entry %llu at [%lu] (0x%p)...",
			       bts.qualifier, index, at);
			return -1;
		}
	}

	return 0;
}

int ds_selftest_bts(void)
{
	const struct bts_trace *trace;
	struct bts_tracer *tracer;
	int error = 0;
	void *top;
	unsigned char buffer[BUFFER_SIZE];

	printk(KERN_INFO "[ds] bts selftest...");

	tracer = ds_request_bts_cpu(smp_processor_id(), buffer, BUFFER_SIZE,
				    NULL, (size_t)-1, BTS_KERNEL);
	if (IS_ERR(tracer)) {
		error = PTR_ERR(tracer);
		tracer = NULL;

		printk(KERN_CONT
		       "initialization failed (err: %d)...", error);
		goto out;
	}

	/* The return should already give us enough trace. */
	ds_suspend_bts(tracer);

	/* Let's see if we can access the trace. */
	trace = ds_read_bts(tracer);

	error = ds_selftest_bts_consistency(trace);
	if (error < 0)
		goto out;

	/* If everything went well, we should have a few trace entries. */
	if (trace->ds.top == trace->ds.begin) {
		/*
		 * It is possible but highly unlikely that we got a
		 * buffer overflow and end up at exactly the same
		 * position we started from.
		 * Let's issue a warning, but continue.
		 */
		printk(KERN_CONT "no trace/overflow...");
	}

	/* Let's try to read the trace we collected. */
	error = ds_selftest_bts_read(tracer, trace,
				     trace->ds.begin, trace->ds.top);
	if (error < 0)
		goto out;

	/*
	 * Let's read the trace again.
	 * Since we suspended tracing, we should get the same result.
	 */
	top = trace->ds.top;

	trace = ds_read_bts(tracer);
	error = ds_selftest_bts_consistency(trace);
	if (error < 0)
		goto out;

	if (top != trace->ds.top) {
		printk(KERN_CONT "suspend not working...");
		error = -1;
		goto out;
	}

	/* Let's collect some more trace - see if resume is working. */
	ds_resume_bts(tracer);
	ds_suspend_bts(tracer);

	trace = ds_read_bts(tracer);

	error = ds_selftest_bts_consistency(trace);
	if (error < 0)
		goto out;

	if (trace->ds.top == top) {
		/*
		 * It is possible but highly unlikely that we got a
		 * buffer overflow and end up at exactly the same
		 * position we started from.
		 * Let's issue a warning and check the full trace.
		 */
		printk(KERN_CONT
		       "no resume progress/overflow...");

		error = ds_selftest_bts_read(tracer, trace,
					     trace->ds.begin, trace->ds.end);
	} else if (trace->ds.top < top) {
		/*
		 * We had a buffer overflow - the entire buffer should
		 * contain trace records.
		 */
		error = ds_selftest_bts_read(tracer, trace,
					     trace->ds.begin, trace->ds.end);
	} else {
		/*
		 * It is quite likely that the buffer did not overflow.
		 * Let's just check the delta trace.
		 */
		error = ds_selftest_bts_read(tracer, trace,
					     top, trace->ds.top);
	}
	if (error < 0)
		goto out;

	error = 0;

	/* The final test: release the tracer while tracing is suspended. */
 out:
	ds_release_bts(tracer);

	printk(KERN_CONT "%s.\n", (error ? "failed" : "passed"));

	return error;
}

int ds_selftest_pebs(void)
{
	return 0;
}