2 * Copyright (c) 2009 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Support for x86 machine check architecture.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/sched.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 #include <sys/taskqueue.h>
46 #include <machine/mca.h>
47 #include <machine/md_var.h>
48 #include <machine/specialreg.h>
51 struct mca_record rec;
53 STAILQ_ENTRY(mca_internal) link;
56 static MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
58 static int mca_count; /* Number of records stored. */
60 SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RD, NULL, "Machine Check Architecture");
62 static int mca_enabled = 0;
63 TUNABLE_INT("hw.mca.enabled", &mca_enabled);
64 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
65 "Administrative toggle for machine check support");
67 static STAILQ_HEAD(, mca_internal) mca_records;
68 static struct callout mca_timer;
69 static int mca_ticks = 3600; /* Check hourly by default. */
70 static struct task mca_task;
71 static struct mtx mca_lock;
74 sysctl_mca_ticks(SYSCTL_HANDLER_ARGS)
79 error = sysctl_handle_int(oidp, &value, 0, req);
80 if (error || req->newptr == NULL)
89 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
91 int *name = (int *)arg1;
93 struct mca_record record;
94 struct mca_internal *rec;
100 if (name[0] < 0 || name[0] >= mca_count)
103 mtx_lock_spin(&mca_lock);
104 if (name[0] >= mca_count) {
105 mtx_unlock_spin(&mca_lock);
109 STAILQ_FOREACH(rec, &mca_records, link) {
116 mtx_unlock_spin(&mca_lock);
117 return (SYSCTL_OUT(req, &record, sizeof(record)));
121 mca_error_ttype(uint16_t mca_error)
124 switch ((mca_error & 0x000c) >> 2) {
136 mca_error_level(uint16_t mca_error)
139 switch (mca_error & 0x0003) {
153 mca_error_request(uint16_t mca_error)
156 switch ((mca_error & 0x00f0) >> 4) {
179 /* Dump details about a single machine check. */
180 static void __nonnull(1)
181 mca_log(const struct mca_record *rec)
185 printf("MCA: bank %d, status 0x%016llx\n", rec->mr_bank,
186 (long long)rec->mr_status);
187 printf("MCA: CPU %d ", rec->mr_apic_id);
188 if (rec->mr_status & MC_STATUS_UC)
192 if (rec->mr_status & MC_STATUS_PCC)
194 if (rec->mr_status & MC_STATUS_OVER)
196 mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
198 /* Simple error codes. */
203 printf("unclassified error");
206 printf("ucode ROM parity error");
209 printf("external error");
215 printf("internal timer error");
218 if ((mca_error & 0xfc00) == 0x0400) {
219 printf("internal error %x", mca_error & 0x03ff);
223 /* Compound error codes. */
225 /* Memory hierarchy error. */
226 if ((mca_error & 0xeffc) == 0x000c) {
227 printf("%s memory error", mca_error_level(mca_error));
232 if ((mca_error & 0xeff0) == 0x0010) {
233 printf("%sTLB %s error", mca_error_ttype(mca_error),
234 mca_error_level(mca_error));
239 if ((mca_error & 0xef00) == 0x0100) {
240 printf("%sCACHE %s %s error",
241 mca_error_ttype(mca_error),
242 mca_error_level(mca_error),
243 mca_error_request(mca_error));
247 /* Bus and/or Interconnect error. */
248 if ((mca_error & 0xe800) == 0x0800) {
249 printf("BUS%s ", mca_error_level(mca_error));
250 switch ((mca_error & 0x0600) >> 9) {
264 printf(" %s ", mca_error_request(mca_error));
265 switch ((mca_error & 0x000c) >> 2) {
279 if (mca_error & 0x0100)
280 printf(" timed out");
284 printf("unknown error %x", mca_error);
288 if (rec->mr_status & MC_STATUS_ADDRV)
289 printf("MCA: Address 0x%llx\n", (long long)rec->mr_addr);
292 static int __nonnull(2)
293 mca_check_status(int bank, struct mca_record *rec)
298 status = rdmsr(MSR_MC_STATUS(bank));
299 if (!(status & MC_STATUS_VAL))
302 /* Save exception information. */
303 rec->mr_status = status;
306 if (status & MC_STATUS_ADDRV)
307 rec->mr_addr = rdmsr(MSR_MC_ADDR(bank));
309 if (status & MC_STATUS_MISCV)
310 rec->mr_misc = rdmsr(MSR_MC_MISC(bank));
311 rec->mr_tsc = rdtsc();
312 rec->mr_apic_id = PCPU_GET(apic_id);
315 * Clear machine check. Don't do this for uncorrectable
316 * errors so that the BIOS can see them.
318 if (!(rec->mr_status & (MC_STATUS_PCC | MC_STATUS_UC))) {
319 wrmsr(MSR_MC_STATUS(bank), 0);
325 static void __nonnull(1)
326 mca_record_entry(const struct mca_record *record)
328 struct mca_internal *rec;
330 rec = malloc(sizeof(*rec), M_MCA, M_NOWAIT);
332 printf("MCA: Unable to allocate space for an event.\n");
339 mtx_lock_spin(&mca_lock);
340 STAILQ_INSERT_TAIL(&mca_records, rec, link);
342 mtx_unlock_spin(&mca_lock);
346 * This scans all the machine check banks of the current CPU to see if
347 * there are any machine checks. Any non-recoverable errors are
348 * reported immediately via mca_log(). The current thread must be
349 * pinned when this is called. The 'mcip' parameter indicates if we
350 * are being called from the MC exception handler. In that case this
351 * function returns true if the system is restartable. Otherwise, it
352 * returns a count of the number of valid MC records found.
357 struct mca_record rec;
358 uint64_t mcg_cap, ucmask;
359 int count, i, recoverable;
363 ucmask = MC_STATUS_UC | MC_STATUS_PCC;
365 /* When handling a MCE#, treat the OVER flag as non-restartable. */
367 ucmask |= MC_STATUS_OVER;
368 mcg_cap = rdmsr(MSR_MCG_CAP);
369 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
370 if (mca_check_status(i, &rec)) {
372 if (rec.mr_status & ucmask) {
376 mca_record_entry(&rec);
379 return (mcip ? recoverable : count);
383 * Scan the machine check banks on all CPUs by binding to each CPU in
384 * turn. If any of the CPUs contained new machine check records, log
385 * them to the console.
388 mca_scan_cpus(void *context, int pending)
390 struct mca_internal *mca;
397 for (cpu = 0; cpu <= mp_maxid; cpu++) {
402 count += mca_scan(0);
408 mtx_lock_spin(&mca_lock);
409 STAILQ_FOREACH(mca, &mca_records, link) {
412 mtx_unlock_spin(&mca_lock);
414 mtx_lock_spin(&mca_lock);
417 mtx_unlock_spin(&mca_lock);
422 mca_periodic_scan(void *arg)
425 taskqueue_enqueue(taskqueue_thread, &mca_task);
426 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
430 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
435 error = sysctl_handle_int(oidp, &i, 0, req);
439 taskqueue_enqueue(taskqueue_thread, &mca_task);
444 mca_startup(void *dummy)
447 if (!mca_enabled || !(cpu_feature & CPUID_MCA))
450 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan,
453 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
459 mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
460 STAILQ_INIT(&mca_records);
461 TASK_INIT(&mca_task, 0x8000, mca_scan_cpus, NULL);
462 callout_init(&mca_timer, CALLOUT_MPSAFE);
463 SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
464 "count", CTLFLAG_RD, &mca_count, 0, "Record count");
465 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
466 "interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &mca_ticks,
467 0, sysctl_mca_ticks, "I",
468 "Periodic interval in seconds to scan for machine checks");
469 SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
470 "records", CTLFLAG_RD, sysctl_mca_records, "Machine check records");
471 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
472 "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
473 sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
476 /* Must be executed on each CPU. */
483 /* MCE is required. */
484 if (!mca_enabled || !(cpu_feature & CPUID_MCE))
487 if (cpu_feature & CPUID_MCA) {
488 if (PCPU_GET(cpuid) == 0)
492 mcg_cap = rdmsr(MSR_MCG_CAP);
493 if (mcg_cap & MCG_CAP_CTL_P)
494 /* Enable MCA features. */
495 wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
497 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
499 * Enable logging of all errors. For P6
500 * processors, MC0_CTL is always enabled.
502 * XXX: Better CPU test needed here?
504 if (!(i == 0 && (cpu_id & 0xf00) == 0x600))
505 wrmsr(MSR_MC_CTL(i), 0xffffffffffffffffUL);
507 /* Clear all errors. */
508 wrmsr(MSR_MC_STATUS(i), 0);
513 load_cr4(rcr4() | CR4_MCE);
516 /* Called when a machine check exception fires. */
523 if (!(cpu_feature & CPUID_MCA)) {
525 * Just print the values of the old Pentium registers
528 printf("MC Type: 0x%lx Address: 0x%lx\n",
529 rdmsr(MSR_P5_MC_TYPE), rdmsr(MSR_P5_MC_ADDR));
533 /* Scan the banks and check for any non-recoverable errors. */
534 recoverable = mca_scan(1);
535 mcg_status = rdmsr(MSR_MCG_STATUS);
536 if (!(mcg_status & MCG_STATUS_RIPV))
540 wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
541 return (recoverable);