mce_amd_inj.c 11.7 KB
Newer Older
B
Borislav Petkov 已提交
1
/*
2 3 4
 * A simple MCE injection facility for testing different aspects of the RAS
 * code. This driver should be built as module so that it can be loaded
 * on production kernels for testing purposes.
B
Borislav Petkov 已提交
5 6 7 8
 *
 * This file may be distributed under the terms of the GNU General Public
 * License version 2.
 *
9
 * Copyright (c) 2010-15:  Borislav Petkov <bp@alien8.de>
B
Borislav Petkov 已提交
10 11 12 13
 *			Advanced Micro Devices Inc.
 */

#include <linux/kobject.h>
14
#include <linux/debugfs.h>
15
#include <linux/device.h>
16
#include <linux/module.h>
17
#include <linux/cpu.h>
18 19
#include <linux/string.h>
#include <linux/uaccess.h>
20
#include <linux/pci.h>
21

B
Borislav Petkov 已提交
22
#include <asm/mce.h>
23
#include <asm/smp.h>
24
#include <asm/amd_nb.h>
25
#include <asm/irq_vectors.h>
B
Borislav Petkov 已提交
26

27
#include "../kernel/cpu/mcheck/mce-internal.h"
B
Borislav Petkov 已提交
28 29 30 31 32

/*
 * Collect all the MCi_XXX settings
 */
static struct mce i_mce;
33
static struct dentry *dfs_inj;
B
Borislav Petkov 已提交
34

35 36
static u8 n_banks;

37
#define MAX_FLAG_OPT_SIZE	3
38
#define NBCFG			0x44
39 40 41 42

enum injection_type {
	SW_INJ = 0,	/* SW injection, simply decode the error */
	HW_INJ,		/* Trigger a #MC */
43 44
	DFR_INT_INJ,    /* Trigger Deferred error interrupt */
	THR_INT_INJ,    /* Trigger threshold interrupt */
45 46 47 48 49 50
	N_INJ_TYPES,
};

static const char * const flags_options[] = {
	[SW_INJ] = "sw",
	[HW_INJ] = "hw",
51 52
	[DFR_INT_INJ] = "df",
	[THR_INT_INJ] = "th",
53 54 55 56
	NULL
};

/* Set default injection to SW_INJ */
57
static enum injection_type inj_type = SW_INJ;
58

59 60
#define MCE_INJECT_SET(reg)						\
static int inj_##reg##_set(void *data, u64 val)				\
B
Borislav Petkov 已提交
61
{									\
62
	struct mce *m = (struct mce *)data;				\
B
Borislav Petkov 已提交
63
									\
64 65
	m->reg = val;							\
	return 0;							\
B
Borislav Petkov 已提交
66 67
}

68 69 70
MCE_INJECT_SET(status);
MCE_INJECT_SET(misc);
MCE_INJECT_SET(addr);
B
Borislav Petkov 已提交
71

72 73
#define MCE_INJECT_GET(reg)						\
static int inj_##reg##_get(void *data, u64 *val)			\
B
Borislav Petkov 已提交
74
{									\
75 76 77 78
	struct mce *m = (struct mce *)data;				\
									\
	*val = m->reg;							\
	return 0;							\
B
Borislav Petkov 已提交
79 80
}

81 82 83
MCE_INJECT_GET(status);
MCE_INJECT_GET(misc);
MCE_INJECT_GET(addr);
B
Borislav Petkov 已提交
84

85 86 87
DEFINE_SIMPLE_ATTRIBUTE(status_fops, inj_status_get, inj_status_set, "%llx\n");
DEFINE_SIMPLE_ATTRIBUTE(misc_fops, inj_misc_get, inj_misc_set, "%llx\n");
DEFINE_SIMPLE_ATTRIBUTE(addr_fops, inj_addr_get, inj_addr_set, "%llx\n");
B
Borislav Petkov 已提交
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
/*
 * Caller needs to be make sure this cpu doesn't disappear
 * from under us, i.e.: get_cpu/put_cpu.
 */
static int toggle_hw_mce_inject(unsigned int cpu, bool enable)
{
	u32 l, h;
	int err;

	err = rdmsr_on_cpu(cpu, MSR_K7_HWCR, &l, &h);
	if (err) {
		pr_err("%s: error reading HWCR\n", __func__);
		return err;
	}

	enable ? (l |= BIT(18)) : (l &= ~BIT(18));

	err = wrmsr_on_cpu(cpu, MSR_K7_HWCR, l, h);
	if (err)
		pr_err("%s: error writing HWCR\n", __func__);

	return err;
}

113
static int __set_inj(const char *buf)
114
{
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	int i;

	for (i = 0; i < N_INJ_TYPES; i++) {
		if (!strncmp(flags_options[i], buf, strlen(flags_options[i]))) {
			inj_type = i;
			return 0;
		}
	}
	return -EINVAL;
}

static ssize_t flags_read(struct file *filp, char __user *ubuf,
			  size_t cnt, loff_t *ppos)
{
	char buf[MAX_FLAG_OPT_SIZE];
	int n;
131

132
	n = sprintf(buf, "%s\n", flags_options[inj_type]);
133

134
	return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
135 136
}

137 138
static ssize_t flags_write(struct file *filp, const char __user *ubuf,
			   size_t cnt, loff_t *ppos)
139
{
140 141
	char buf[MAX_FLAG_OPT_SIZE], *__buf;
	int err;
142

143
	if (cnt > MAX_FLAG_OPT_SIZE)
144
		return -EINVAL;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	if (copy_from_user(&buf, ubuf, cnt))
		return -EFAULT;

	buf[cnt - 1] = 0;

	/* strip whitespace */
	__buf = strstrip(buf);

	err = __set_inj(__buf);
	if (err) {
		pr_err("%s: Invalid flags value: %s\n", __func__, __buf);
		return err;
	}

160
	*ppos += cnt;
161

162
	return cnt;
163 164
}

165 166 167 168 169
static const struct file_operations flags_fops = {
	.read           = flags_read,
	.write          = flags_write,
	.llseek         = generic_file_llseek,
};
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

/*
 * On which CPU to inject?
 */
MCE_INJECT_GET(extcpu);

static int inj_extcpu_set(void *data, u64 val)
{
	struct mce *m = (struct mce *)data;

	if (val >= nr_cpu_ids || !cpu_online(val)) {
		pr_err("%s: Invalid CPU: %llu\n", __func__, val);
		return -EINVAL;
	}
	m->extcpu = val;
	return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(extcpu_fops, inj_extcpu_get, inj_extcpu_set, "%llu\n");

190 191 192 193 194
static void trigger_mce(void *info)
{
	asm volatile("int $18");
}

195 196 197 198 199 200 201 202 203 204
static void trigger_dfr_int(void *info)
{
	asm volatile("int %0" :: "i" (DEFERRED_ERROR_VECTOR));
}

static void trigger_thr_int(void *info)
{
	asm volatile("int %0" :: "i" (THRESHOLD_APIC_VECTOR));
}

205 206 207 208 209
static u32 get_nbc_for_node(int node_id)
{
	struct cpuinfo_x86 *c = &boot_cpu_data;
	u32 cores_per_node;

210
	cores_per_node = (c->x86_max_cores * smp_num_siblings) / amd_get_nodes_per_socket();
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 243

	return cores_per_node * node_id;
}

static void toggle_nb_mca_mst_cpu(u16 nid)
{
	struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
	u32 val;
	int err;

	if (!F3)
		return;

	err = pci_read_config_dword(F3, NBCFG, &val);
	if (err) {
		pr_err("%s: Error reading F%dx%03x.\n",
		       __func__, PCI_FUNC(F3->devfn), NBCFG);
		return;
	}

	if (val & BIT(27))
		return;

	pr_err("%s: Set D18F3x44[NbMcaToMstCpuEn] which BIOS hasn't done.\n",
	       __func__);

	val |= BIT(27);
	err = pci_write_config_dword(F3, NBCFG, val);
	if (err)
		pr_err("%s: Error writing F%dx%03x.\n",
		       __func__, PCI_FUNC(F3->devfn), NBCFG);
}

244 245 246 247 248 249
static void do_inject(void)
{
	u64 mcg_status = 0;
	unsigned int cpu = i_mce.extcpu;
	u8 b = i_mce.bank;

250 251 252
	if (i_mce.misc)
		i_mce.status |= MCI_STATUS_MISCV;

253
	if (inj_type == SW_INJ) {
254
		mce_inject_log(&i_mce);
255 256 257 258 259 260 261 262 263
		return;
	}

	/* prep MCE global settings for the injection */
	mcg_status = MCG_STATUS_MCIP | MCG_STATUS_EIPV;

	if (!(i_mce.status & MCI_STATUS_PCC))
		mcg_status |= MCG_STATUS_RIPV;

264 265 266 267 268 269 270 271 272 273
	/*
	 * Ensure necessary status bits for deferred errors:
	 * - MCx_STATUS[Deferred]: make sure it is a deferred error
	 * - MCx_STATUS[UC] cleared: deferred errors are _not_ UC
	 */
	if (inj_type == DFR_INT_INJ) {
		i_mce.status |= MCI_STATUS_DEFERRED;
		i_mce.status |= (i_mce.status & ~MCI_STATUS_UC);
	}

274 275 276 277 278 279 280 281 282 283
	/*
	 * For multi node CPUs, logging and reporting of bank 4 errors happens
	 * only on the node base core. Refer to D18F3x44[NbMcaToMstCpuEn] for
	 * Fam10h and later BKDGs.
	 */
	if (static_cpu_has(X86_FEATURE_AMD_DCM) && b == 4) {
		toggle_nb_mca_mst_cpu(amd_get_nb_id(cpu));
		cpu = get_nbc_for_node(amd_get_nb_id(cpu));
	}

284 285 286 287
	get_online_cpus();
	if (!cpu_online(cpu))
		goto err;

288 289 290 291 292
	toggle_hw_mce_inject(cpu, true);

	wrmsr_on_cpu(cpu, MSR_IA32_MCG_STATUS,
		     (u32)mcg_status, (u32)(mcg_status >> 32));

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	if (boot_cpu_has(X86_FEATURE_SMCA)) {
		if (inj_type == DFR_INT_INJ) {
			wrmsr_on_cpu(cpu, MSR_AMD64_SMCA_MCx_DESTAT(b),
				     (u32)i_mce.status, (u32)(i_mce.status >> 32));

			wrmsr_on_cpu(cpu, MSR_AMD64_SMCA_MCx_DEADDR(b),
				     (u32)i_mce.addr, (u32)(i_mce.addr >> 32));
		} else {
			wrmsr_on_cpu(cpu, MSR_AMD64_SMCA_MCx_STATUS(b),
				     (u32)i_mce.status, (u32)(i_mce.status >> 32));

			wrmsr_on_cpu(cpu, MSR_AMD64_SMCA_MCx_ADDR(b),
				     (u32)i_mce.addr, (u32)(i_mce.addr >> 32));
		}

		wrmsr_on_cpu(cpu, MSR_AMD64_SMCA_MCx_MISC(b),
			     (u32)i_mce.misc, (u32)(i_mce.misc >> 32));
	} else {
		wrmsr_on_cpu(cpu, MSR_IA32_MCx_STATUS(b),
			     (u32)i_mce.status, (u32)(i_mce.status >> 32));
313

314 315
		wrmsr_on_cpu(cpu, MSR_IA32_MCx_ADDR(b),
			     (u32)i_mce.addr, (u32)(i_mce.addr >> 32));
316

317 318 319
		wrmsr_on_cpu(cpu, MSR_IA32_MCx_MISC(b),
			     (u32)i_mce.misc, (u32)(i_mce.misc >> 32));
	}
320 321 322

	toggle_hw_mce_inject(cpu, false);

323 324 325 326 327 328 329 330 331 332
	switch (inj_type) {
	case DFR_INT_INJ:
		smp_call_function_single(cpu, trigger_dfr_int, NULL, 0);
		break;
	case THR_INT_INJ:
		smp_call_function_single(cpu, trigger_thr_int, NULL, 0);
		break;
	default:
		smp_call_function_single(cpu, trigger_mce, NULL, 0);
	}
333 334 335 336 337 338

err:
	put_online_cpus();

}

B
Borislav Petkov 已提交
339 340 341 342
/*
 * This denotes into which bank we're injecting and triggers
 * the injection, at the same time.
 */
343
static int inj_bank_set(void *data, u64 val)
B
Borislav Petkov 已提交
344
{
345
	struct mce *m = (struct mce *)data;
B
Borislav Petkov 已提交
346

347 348 349
	if (val >= n_banks) {
		pr_err("Non-existent MCE bank: %llu\n", val);
		return -EINVAL;
350
	}
B
Borislav Petkov 已提交
351

352
	m->bank = val;
353
	do_inject();
B
Borislav Petkov 已提交
354

355
	return 0;
B
Borislav Petkov 已提交
356 357
}

358
MCE_INJECT_GET(bank);
B
Borislav Petkov 已提交
359

360 361
DEFINE_SIMPLE_ATTRIBUTE(bank_fops, inj_bank_get, inj_bank_set, "%llu\n");

362
static const char readme_msg[] =
363 364 365 366 367 368 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 395 396
"Description of the files and their usages:\n"
"\n"
"Note1: i refers to the bank number below.\n"
"Note2: See respective BKDGs for the exact bit definitions of the files below\n"
"as they mirror the hardware registers.\n"
"\n"
"status:\t Set MCi_STATUS: the bits in that MSR control the error type and\n"
"\t attributes of the error which caused the MCE.\n"
"\n"
"misc:\t Set MCi_MISC: provide auxiliary info about the error. It is mostly\n"
"\t used for error thresholding purposes and its validity is indicated by\n"
"\t MCi_STATUS[MiscV].\n"
"\n"
"addr:\t Error address value to be written to MCi_ADDR. Log address information\n"
"\t associated with the error.\n"
"\n"
"cpu:\t The CPU to inject the error on.\n"
"\n"
"bank:\t Specify the bank you want to inject the error into: the number of\n"
"\t banks in a processor varies and is family/model-specific, therefore, the\n"
"\t supplied value is sanity-checked. Setting the bank value also triggers the\n"
"\t injection.\n"
"\n"
"flags:\t Injection type to be performed. Writing to this file will trigger a\n"
"\t real machine check, an APIC interrupt or invoke the error decoder routines\n"
"\t for AMD processors.\n"
"\n"
"\t Allowed error injection types:\n"
"\t  - \"sw\": Software error injection. Decode error to a human-readable \n"
"\t    format only. Safe to use.\n"
"\t  - \"hw\": Hardware error injection. Causes the #MC exception handler to \n"
"\t    handle the error. Be warned: might cause system panic if MCi_STATUS[PCC] \n"
"\t    is set. Therefore, consider setting (debugfs_mountpoint)/mce/fake_panic \n"
"\t    before injecting.\n"
397 398 399 400 401
"\t  - \"df\": Trigger APIC interrupt for Deferred error. Causes deferred \n"
"\t    error APIC interrupt handler to handle the error if the feature is \n"
"\t    is present in hardware. \n"
"\t  - \"th\": Trigger APIC interrupt for Threshold errors. Causes threshold \n"
"\t    APIC interrupt handler to handle the error. \n"
402
"\n";
403 404 405 406 407 408 409 410 411 412 413 414 415

static ssize_t
inj_readme_read(struct file *filp, char __user *ubuf,
		       size_t cnt, loff_t *ppos)
{
	return simple_read_from_buffer(ubuf, cnt, ppos,
					readme_msg, strlen(readme_msg));
}

static const struct file_operations readme_fops = {
	.read		= inj_readme_read,
};

416
static struct dfs_node {
417 418 419
	char *name;
	struct dentry *d;
	const struct file_operations *fops;
420
	umode_t perm;
421
} dfs_fls[] = {
422 423 424 425 426 427
	{ .name = "status",	.fops = &status_fops, .perm = S_IRUSR | S_IWUSR },
	{ .name = "misc",	.fops = &misc_fops,   .perm = S_IRUSR | S_IWUSR },
	{ .name = "addr",	.fops = &addr_fops,   .perm = S_IRUSR | S_IWUSR },
	{ .name = "bank",	.fops = &bank_fops,   .perm = S_IRUSR | S_IWUSR },
	{ .name = "flags",	.fops = &flags_fops,  .perm = S_IRUSR | S_IWUSR },
	{ .name = "cpu",	.fops = &extcpu_fops, .perm = S_IRUSR | S_IWUSR },
428
	{ .name = "README",	.fops = &readme_fops, .perm = S_IRUSR | S_IRGRP | S_IROTH },
B
Borislav Petkov 已提交
429 430
};

431
static int __init init_mce_inject(void)
B
Borislav Petkov 已提交
432
{
433
	int i;
434 435 436 437
	u64 cap;

	rdmsrl(MSR_IA32_MCG_CAP, cap);
	n_banks = cap & MCG_BANKCNT_MASK;
B
Borislav Petkov 已提交
438

439 440
	dfs_inj = debugfs_create_dir("mce-inject", NULL);
	if (!dfs_inj)
B
Borislav Petkov 已提交
441 442
		return -EINVAL;

443 444
	for (i = 0; i < ARRAY_SIZE(dfs_fls); i++) {
		dfs_fls[i].d = debugfs_create_file(dfs_fls[i].name,
445
						    dfs_fls[i].perm,
446 447 448
						    dfs_inj,
						    &i_mce,
						    dfs_fls[i].fops);
B
Borislav Petkov 已提交
449

450 451
		if (!dfs_fls[i].d)
			goto err_dfs_add;
B
Borislav Petkov 已提交
452
	}
453

B
Borislav Petkov 已提交
454 455
	return 0;

456
err_dfs_add:
457
	while (--i >= 0)
458
		debugfs_remove(dfs_fls[i].d);
B
Borislav Petkov 已提交
459

460 461
	debugfs_remove(dfs_inj);
	dfs_inj = NULL;
B
Borislav Petkov 已提交
462

463
	return -ENOMEM;
B
Borislav Petkov 已提交
464 465
}

466
static void __exit exit_mce_inject(void)
B
Borislav Petkov 已提交
467 468 469
{
	int i;

470 471
	for (i = 0; i < ARRAY_SIZE(dfs_fls); i++)
		debugfs_remove(dfs_fls[i].d);
B
Borislav Petkov 已提交
472

473
	memset(&dfs_fls, 0, sizeof(dfs_fls));
B
Borislav Petkov 已提交
474

475 476
	debugfs_remove(dfs_inj);
	dfs_inj = NULL;
B
Borislav Petkov 已提交
477
}
478 479
module_init(init_mce_inject);
module_exit(exit_mce_inject);
B
Borislav Petkov 已提交
480 481

MODULE_LICENSE("GPL");
482
MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>");
B
Borislav Petkov 已提交
483
MODULE_AUTHOR("AMD Inc.");
484
MODULE_DESCRIPTION("MCE injection facility for RAS testing");