ds_selftest.c 9.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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>
13
#include <linux/smp.h>
Markus Metzger's avatar
Markus Metzger committed
14
#include <linux/cpu.h>
15 16 17 18

#include <asm/ds.h>


Markus Metzger's avatar
Markus Metzger committed
19
#define BUFFER_SIZE	521 /* Intentionally chose an odd size. */
20 21


Markus Metzger's avatar
Markus Metzger committed
22 23 24 25 26 27 28
struct ds_selftest_bts_conf {
	struct bts_tracer *tracer;
	int error;
	int (*suspend)(struct bts_tracer *);
	int (*resume)(struct bts_tracer *);
};

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
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;
	}
58 59 60 61 62
	/*
	 * We allow top in [begin; end], since its not clear when the
	 * overflow adjustment happens: after the increment or before the
	 * write.
	 */
63
	if ((trace->ds.top < trace->ds.begin) ||
64
	    (trace->ds.end < trace->ds.top)) {
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
		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;
103
		unsigned long index;
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
		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;
}

Markus Metzger's avatar
Markus Metzger committed
136
static void ds_selftest_bts_cpu(void *arg)
137
{
Markus Metzger's avatar
Markus Metzger committed
138
	struct ds_selftest_bts_conf *conf = arg;
139 140 141
	const struct bts_trace *trace;
	void *top;

Markus Metzger's avatar
Markus Metzger committed
142 143 144
	if (IS_ERR(conf->tracer)) {
		conf->error = PTR_ERR(conf->tracer);
		conf->tracer = NULL;
145 146

		printk(KERN_CONT
Markus Metzger's avatar
Markus Metzger committed
147 148
		       "initialization failed (err: %d)...", conf->error);
		return;
149 150
	}

Markus Metzger's avatar
Markus Metzger committed
151 152 153 154
	/* We should meanwhile have enough trace. */
	conf->error = conf->suspend(conf->tracer);
	if (conf->error < 0)
		return;
155 156

	/* Let's see if we can access the trace. */
Markus Metzger's avatar
Markus Metzger committed
157
	trace = ds_read_bts(conf->tracer);
158

Markus Metzger's avatar
Markus Metzger committed
159 160 161
	conf->error = ds_selftest_bts_consistency(trace);
	if (conf->error < 0)
		return;
162 163 164 165 166 167 168 169 170 171 172 173 174

	/* 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. */
Markus Metzger's avatar
Markus Metzger committed
175 176
	conf->error =
		ds_selftest_bts_read(conf->tracer, trace,
177
				     trace->ds.begin, trace->ds.top);
Markus Metzger's avatar
Markus Metzger committed
178 179
	if (conf->error < 0)
		return;
180 181 182 183 184 185 186

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

Markus Metzger's avatar
Markus Metzger committed
187 188 189 190
	trace = ds_read_bts(conf->tracer);
	conf->error = ds_selftest_bts_consistency(trace);
	if (conf->error < 0)
		return;
191 192 193

	if (top != trace->ds.top) {
		printk(KERN_CONT "suspend not working...");
Markus Metzger's avatar
Markus Metzger committed
194 195
		conf->error = -1;
		return;
196 197 198
	}

	/* Let's collect some more trace - see if resume is working. */
Markus Metzger's avatar
Markus Metzger committed
199 200 201 202 203 204 205
	conf->error = conf->resume(conf->tracer);
	if (conf->error < 0)
		return;

	conf->error = conf->suspend(conf->tracer);
	if (conf->error < 0)
		return;
206

Markus Metzger's avatar
Markus Metzger committed
207
	trace = ds_read_bts(conf->tracer);
208

Markus Metzger's avatar
Markus Metzger committed
209 210 211
	conf->error = ds_selftest_bts_consistency(trace);
	if (conf->error < 0)
		return;
212 213 214 215 216 217 218 219 220 221 222

	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...");

Markus Metzger's avatar
Markus Metzger committed
223 224
		conf->error =
			ds_selftest_bts_read(conf->tracer, trace,
225 226 227 228 229 230
					     trace->ds.begin, trace->ds.end);
	} else if (trace->ds.top < top) {
		/*
		 * We had a buffer overflow - the entire buffer should
		 * contain trace records.
		 */
Markus Metzger's avatar
Markus Metzger committed
231 232
		conf->error =
			ds_selftest_bts_read(conf->tracer, trace,
233 234 235 236 237 238
					     trace->ds.begin, trace->ds.end);
	} else {
		/*
		 * It is quite likely that the buffer did not overflow.
		 * Let's just check the delta trace.
		 */
Markus Metzger's avatar
Markus Metzger committed
239 240 241
		conf->error =
			ds_selftest_bts_read(conf->tracer, trace, top,
					     trace->ds.top);
242
	}
Markus Metzger's avatar
Markus Metzger committed
243 244
	if (conf->error < 0)
		return;
245

Markus Metzger's avatar
Markus Metzger committed
246 247
	conf->error = 0;
}
248

Markus Metzger's avatar
Markus Metzger committed
249 250 251 252 253 254 255 256 257 258 259
static int ds_suspend_bts_wrap(struct bts_tracer *tracer)
{
	ds_suspend_bts(tracer);
	return 0;
}

static int ds_resume_bts_wrap(struct bts_tracer *tracer)
{
	ds_resume_bts(tracer);
	return 0;
}
260

Markus Metzger's avatar
Markus Metzger committed
261 262 263 264
static void ds_release_bts_noirq_wrap(void *tracer)
{
	(void)ds_release_bts_noirq(tracer);
}
265

Markus Metzger's avatar
Markus Metzger committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static int ds_selftest_bts_bad_release_noirq(int cpu,
					     struct bts_tracer *tracer)
{
	int error = -EPERM;

	/* Try to release the tracer on the wrong cpu. */
	get_cpu();
	if (cpu != smp_processor_id()) {
		error = ds_release_bts_noirq(tracer);
		if (error != -EPERM)
			printk(KERN_CONT "release on wrong cpu...");
	}
	put_cpu();

	return error ? 0 : -1;
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
static int ds_selftest_bts_bad_request_cpu(int cpu, void *buffer)
{
	struct bts_tracer *tracer;
	int error;

	/* Try to request cpu tracing while task tracing is active. */
	tracer = ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE, NULL,
				    (size_t)-1, BTS_KERNEL);
	error = PTR_ERR(tracer);
	if (!IS_ERR(tracer)) {
		ds_release_bts(tracer);
		error = 0;
	}

	if (error != -EPERM)
		printk(KERN_CONT "cpu/task tracing overlap...");

	return error ? 0 : -1;
}

static int ds_selftest_bts_bad_request_task(void *buffer)
{
	struct bts_tracer *tracer;
	int error;

	/* Try to request cpu tracing while task tracing is active. */
	tracer = ds_request_bts_task(current, buffer, BUFFER_SIZE, NULL,
				    (size_t)-1, BTS_KERNEL);
	error = PTR_ERR(tracer);
	if (!IS_ERR(tracer)) {
		error = 0;
		ds_release_bts(tracer);
	}

	if (error != -EPERM)
		printk(KERN_CONT "task/cpu tracing overlap...");

	return error ? 0 : -1;
}

Markus Metzger's avatar
Markus Metzger committed
323 324 325 326
int ds_selftest_bts(void)
{
	struct ds_selftest_bts_conf conf;
	unsigned char buffer[BUFFER_SIZE];
327
	unsigned long irq;
Markus Metzger's avatar
Markus Metzger committed
328 329 330 331 332 333 334 335 336 337 338 339 340
	int cpu;

	printk(KERN_INFO "[ds] bts selftest...");
	conf.error = 0;

	get_online_cpus();
	for_each_online_cpu(cpu) {
		conf.suspend = ds_suspend_bts_wrap;
		conf.resume = ds_resume_bts_wrap;
		conf.tracer =
			ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE,
					   NULL, (size_t)-1, BTS_KERNEL);
		ds_selftest_bts_cpu(&conf);
341 342
		if (conf.error >= 0)
			conf.error = ds_selftest_bts_bad_request_task(buffer);
Markus Metzger's avatar
Markus Metzger committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
		ds_release_bts(conf.tracer);
		if (conf.error < 0)
			goto out;

		conf.suspend = ds_suspend_bts_noirq;
		conf.resume = ds_resume_bts_noirq;
		conf.tracer =
			ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE,
					   NULL, (size_t)-1, BTS_KERNEL);
		smp_call_function_single(cpu, ds_selftest_bts_cpu, &conf, 1);
		if (conf.error >= 0) {
			conf.error =
				ds_selftest_bts_bad_release_noirq(cpu,
								  conf.tracer);
			/* We must not release the tracer twice. */
			if (conf.error < 0)
				conf.tracer = NULL;
		}
361 362
		if (conf.error >= 0)
			conf.error = ds_selftest_bts_bad_request_task(buffer);
Markus Metzger's avatar
Markus Metzger committed
363 364 365 366 367 368
		smp_call_function_single(cpu, ds_release_bts_noirq_wrap,
					 conf.tracer, 1);
		if (conf.error < 0)
			goto out;
	}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	conf.suspend = ds_suspend_bts_wrap;
	conf.resume = ds_resume_bts_wrap;
	conf.tracer =
		ds_request_bts_task(current, buffer, BUFFER_SIZE,
				    NULL, (size_t)-1, BTS_KERNEL);
	ds_selftest_bts_cpu(&conf);
	if (conf.error >= 0)
		conf.error = ds_selftest_bts_bad_request_cpu(0, buffer);
	ds_release_bts(conf.tracer);
	if (conf.error < 0)
		goto out;

	conf.suspend = ds_suspend_bts_noirq;
	conf.resume = ds_resume_bts_noirq;
	conf.tracer =
		ds_request_bts_task(current, buffer, BUFFER_SIZE,
				   NULL, (size_t)-1, BTS_KERNEL);
	local_irq_save(irq);
	ds_selftest_bts_cpu(&conf);
	if (conf.error >= 0)
		conf.error = ds_selftest_bts_bad_request_cpu(0, buffer);
	ds_release_bts_noirq(conf.tracer);
	local_irq_restore(irq);
	if (conf.error < 0)
		goto out;

Markus Metzger's avatar
Markus Metzger committed
395 396 397 398 399 400
	conf.error = 0;
 out:
	put_online_cpus();
	printk(KERN_CONT "%s.\n", (conf.error ? "failed" : "passed"));

	return conf.error;
401 402 403 404 405 406
}

int ds_selftest_pebs(void)
{
	return 0;
}