summaryrefslogtreecommitdiff
path: root/sysutils/mcelog/files/patch-mcelog.c
blob: ed2e780c259cdee9655bac8e92d409496c4535fa (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
--- mcelog.c.orig	2016-10-10 22:08:11 UTC
+++ mcelog.c
@@ -20,9 +20,22 @@
 #define _GNU_SOURCE 1
 #include <sys/fcntl.h>
 #include <sys/ioctl.h>
+#ifdef __Linux__
 #include <asm/types.h>
 #include <asm/ioctls.h>
 #include <linux/limits.h>
+#endif
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <machine/cpufunc.h>
+#include <machine/cputypes.h>
+#include <machine/specialreg.h>
+#include <err.h>
+#include <kvm.h>
+#include <limits.h>
+#endif
+#undef CPU_P4
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -60,9 +73,25 @@
 #include "bus.h"
 #include "unknown.h"
 
+struct mca_record {
+	uint64_t	mr_status;
+	uint64_t	mr_addr;
+	uint64_t	mr_misc;
+	uint64_t	mr_tsc;
+	int		mr_apic_id;
+	int		mr_bank;
+	uint64_t	mr_mcg_cap;
+	uint64_t	mr_mcg_status;
+	int		mr_cpu_id;
+	int		mr_cpu_vendor_id;
+	int		mr_cpu;
+};
+
 enum cputype cputype = CPU_GENERIC;	
 
+#ifdef __Linux__
 char *logfn = LOG_DEV_FILENAME; 
+#endif
 
 int ignore_nodev;
 int filter_bogus = 1;
@@ -73,7 +102,9 @@ int ascii_mode;
 int dump_raw_ascii;
 int daemon_mode;
 static char *inputfile;
+#ifdef __Linux__
 char *processor_flags;
+#endif
 static int foreground;
 int filter_memory_errors;
 static struct config_cred runcred = { .uid = -1U, .gid = -1U };
@@ -82,6 +113,10 @@ static char pidfile_default[] = PID_FILE
 static char logfile_default[] = LOG_FILE;
 static char *pidfile = pidfile_default;
 static char *logfile;
+#ifdef __FreeBSD__
+static char *execfile;
+static char *corefile;
+#endif
 static int debug_numerrors;
 int imc_log = -1;
 static int check_only = 0;
@@ -196,6 +231,7 @@ static void parse_cpuid(u32 cpuid, u32 *
 		*model += c.c.ext_model << 4;
 }
 
+#ifdef __Linux__
 static u32 unparse_cpuid(unsigned family, unsigned model)
 {
 	union { 
@@ -213,6 +249,7 @@ static u32 unparse_cpuid(unsigned family
 		c.c.ext_model = model >> 4;
 	return c.v;
 }
+#endif
 
 static char *cputype_name[] = {
 	[CPU_GENERIC] = "generic CPU",
@@ -325,6 +362,7 @@ static char *vendor[] = {
 	[8] = "NSC"
 };
 
+#ifdef __Linux__
 static unsigned cpuvendor_to_num(char *name)
 {
 	unsigned i;
@@ -339,6 +377,7 @@ static unsigned cpuvendor_to_num(char *n
 			return i;
 	return 0;
 }
+#endif
 
 static char *cpuvendor_name(u32 cpuvendor)
 {
@@ -483,6 +522,7 @@ static void dump_mce_raw_ascii(struct mc
 	Wprintf("\n");
 }
 
+#ifdef __Linux__
 int is_cpu_supported(void)
 { 
 	enum { 
@@ -553,14 +593,61 @@ int is_cpu_supported(void)
 
 	return 1;
 } 
+#endif
+
+#ifdef __FreeBSD__
+int is_cpu_supported(void)
+{
+	char vendor[20];
+	u_int regs[4];
+	u_int cpu_id;
+	int family, model;
+	static int checked;
+
+	if (checked)
+		return 1;
+
+	checked = 1;
+
+	do_cpuid(0, regs);
+	((u_int *)vendor)[0] = regs[1];
+	((u_int *)vendor)[1] = regs[3];
+	((u_int *)vendor)[2] = regs[2];
+	vendor[12] = 0;
+
+	do_cpuid(1, regs);
+	cpu_id = regs[0];
+	family = CPUID_TO_FAMILY(cpu_id);
+	model = CPUID_TO_MODEL(cpu_id);
+
+	if (cpu_forced)
+		;
+	else if (!strcmp(vendor,"AuthenticAMD")) {
+			if (family == 15) {
+				cputype = CPU_K8;
+			} else if (family >= 16) {
+				SYSERRprintf("ERROR: AMD Processor family %d: mcelog does not support this processor.  Please use the edac_mce_amd module instead.\n", family);
+				return 0;
+			}
+		} else if (!strcmp(vendor,"GenuineIntel"))
+			cputype = select_intel_cputype(family, model);
+	/* Add checks for other CPUs here */
+		else
+		return 1;
+	return 0;
+}
+#endif
 
+#ifdef __Linux__
 static char *skipspace(char *s)
 {
 	while (isspace(*s))
 		++s;
 	return s;
 }
+#endif
 
+#ifdef __Linux__
 static char *skip_syslog(char *s)
 {
 	char *p;
@@ -571,7 +658,9 @@ static char *skip_syslog(char *s)
 		return p + sizeof("mcelog: ") - 1;
 	return s;
 }
+#endif
 	
+#ifdef __Linux__
 static char *skipgunk(char *s)
 {
 	s = skip_syslog(s);
@@ -596,12 +685,16 @@ static char *skipgunk(char *s)
 
 	return skipspace(s);
 }
+#endif
 
+#ifdef __Linux__
 static inline int urange(unsigned val, unsigned lo, unsigned hi)
 {
 	return val >= lo && val <= hi;
 }
+#endif
 
+#ifdef __Linux__
 static int is_short(char *name)
 {
 	return strlen(name) == 3 && 
@@ -609,7 +702,9 @@ static int is_short(char *name)
 		islower(name[1]) &&
 		islower(name[2]);
 }
+#endif
 
+#ifdef __Linux__
 static unsigned skip_date(char *s)
 {
 	unsigned day, hour, min, year, sec; 
@@ -626,6 +721,7 @@ static unsigned skip_date(char *s)
 		return 0;
 	return next;
 }
+#endif
 
 static void dump_mce_final(struct mce *m, char *symbol, int missing, int recordlen, 
 			   int dseen)
@@ -646,6 +742,7 @@ static void dump_mce_final(struct mce *m
 	flushlog();
 }
 
+#ifdef __Linux__
 static char *skip_patterns[] = {
 	"MCA:*",
 	"MCi_MISC register valid*",
@@ -654,7 +751,9 @@ static char *skip_patterns[] = {
 	"Kernel does not support page offline interface",
 	NULL
 };
+#endif
 
+#ifdef __Linux__
 static int match_patterns(char *s, char **pat)
 {
 	for (; *pat; pat++) 
@@ -662,12 +761,14 @@ static int match_patterns(char *s, char 
 			return 0;
 	return 1;
 }
+#endif
 
 #define FIELD(f) \
 	if (recordlen < endof_field(struct mce, f)) \
 		recordlen = endof_field(struct mce, f)
 
 /* Decode ASCII input for fatal messages */
+#ifdef __Linux__
 static void decodefatal(FILE *inf)
 {
 	struct mce m;
@@ -878,6 +979,227 @@ restart:
 	if (data)
 		dump_mce_final(&m, symbol, missing, recordlen, disclaimer_seen);
 }
+#endif
+
+#ifdef __FreeBSD__
+/*
+ * Table used to map cpuid vendor strings and FreeBSD CPU vendor IDs
+ * to Linux cpuvendor values.
+ */
+static struct {
+	char *name;
+	int vendor_id;
+	u_char cpuvendor;
+} vendor_ids[] = {
+	{ "GenuineIntel", CPU_VENDOR_INTEL, 0 },
+	{ "AuthenticAMD", CPU_VENDOR_AMD, 2 },
+	{ "CentaurHauls", CPU_VENDOR_CENTAUR, 5 },
+#ifdef __i386__
+	{ "CyrixInstead", CPU_VENDOR_CYRIX, 1 },
+	{ "UMC UMC UMC ", CPU_VENDOR_UMC, 3 },
+	{ "GenuineTMx86", CPU_VENDOR_TRANSMETA, 7 },
+	{ "Geode by NSC", CPU_VENDOR_NSC, 8 },
+#endif
+};
+
+static int find_cpu_vendor(const char *vendor)
+{
+	u_int i;
+
+	for (i = 0; i < sizeof(vendor_ids) / sizeof(vendor_ids[0]); i++)
+		if (strcmp(vendor, vendor_ids[i].name) == 0)
+			return (vendor_ids[i].cpuvendor);
+	return (0xff);
+}
+
+static int find_cpu_vendor_id(const char *vendor)
+{
+	u_int i;
+
+	for (i = 0; i < sizeof(vendor_ids) / sizeof(vendor_ids[0]); i++)
+		if (strcmp(vendor, vendor_ids[i].name) == 0)
+			return (vendor_ids[i].vendor_id);
+	return (0);
+}
+
+static int map_cpu_vendor(int vendor_id)
+{
+	u_int i;
+
+	for (i = 0; i < sizeof(vendor_ids) / sizeof(vendor_ids[0]); i++)
+		if (vendor_ids[i].vendor_id == vendor_id)
+			return (vendor_ids[i].cpuvendor);
+	return (0xff);
+}
+
+/* Convert FreeBSD's struct mca_record into a struct mce. */
+static void convert_mca(struct mca_record *mr, struct mce *mce, int live,
+    size_t len)
+{
+	memset(mce, 0, sizeof(*mce));
+	mce->status = mr->mr_status;
+	mce->misc = mr->mr_misc;
+	mce->addr = mr->mr_addr;
+	mce->mcgstatus = mr->mr_mcg_status;
+	mce->tsc = mr->mr_tsc;
+	mce->cpuvendor = map_cpu_vendor(mr->mr_cpu_vendor_id);
+	mce->cpuid = mr->mr_cpu_id;
+	mce->bank = mr->mr_bank;
+	mce->finished = 1;
+	mce->extcpu = mr->mr_cpu;
+	mce->apicid = mr->mr_apic_id;
+	mce->mcgcap = mr->mr_mcg_cap;
+
+	/*
+	 * For older live records (from sysctl), fill in some fields
+	 * using registers from the current CPU.
+	 */
+	if (len < offsetof(struct mca_record, mr_cpu_id) && live) {
+		char vendor[20];
+		u_int regs[4];
+
+		do_cpuid(0, regs);
+		((u_int *)vendor)[0] = regs[1];
+		((u_int *)vendor)[1] = regs[3];
+		((u_int *)vendor)[2] = regs[2];
+		vendor[12] = 0;
+		mce->cpuvendor = find_cpu_vendor(vendor);
+
+		do_cpuid(1, regs);
+		mce->cpuid = regs[0];
+	}
+}
+
+/* Decode ASCII input for fatal messages */
+static void decodefatal(FILE *inf)
+{
+	struct mca_record mr;
+	struct mce m;
+	long long val, val2;
+	char *cp, line[100], *s, symbol[1];
+	const char *fmt;
+	int cpu, data, old, missing;
+	enum rows {
+		BANK = 0x1,
+		MCG = 0x2,
+		VENDOR = 0x4,
+		CPU = 0x8,
+		ADDR = 0x10,
+		MISC = 0x20,
+	};
+
+	symbol[0] = '\0';
+	data = 0;
+	missing = 0;
+	old = 0;
+	memset(&mr, 0, sizeof(mr));
+	while ((s = fgets(line, sizeof(line), inf)) != NULL) {
+		s = strstr(s, "MCA: ");
+		if (s == NULL)
+			continue;
+		s += strlen("MCA: ");
+
+		if (strncmp(s, "bank", 4) == 0 || strncmp(s, "Bank", 4) == 0) {
+			/* Start of a new record, dump the previous one. */
+			if (data != 0) {
+				/* Require some minimum data. */
+				if (data & BANK) {
+					if (mr.mr_status & MC_STATUS_ADDRV &&
+					    !(data & ADDR))
+						missing = 1;
+					if (mr.mr_status & MC_STATUS_MISCV &&
+					    !(data & MISC))
+						missing = 1;
+					convert_mca(&mr, &m, 0, sizeof(mr));
+					mce_cpuid(&m);
+					dump_mce_final(&m, symbol, missing,
+					    sizeof(struct mce), 0);
+				}
+				data = 0;
+				missing = 0;
+				memset(&mr, 0, sizeof(mr));
+			}
+
+			if (s[0] == 'b') {
+				old = 1;
+				fmt = "bank %d, status 0x%llx";
+			} else {
+				old = 0;
+				fmt = "Bank %d, Status 0x%llx";
+			}
+			if (sscanf(s, fmt, &mr.mr_bank, &val) != 2)
+				missing = 1;
+			else {
+				data |= BANK;
+				mr.mr_status = val;
+			}
+		}
+		if (strncmp(s, "Global", 6) == 0) {
+			if (sscanf(s, "Global Cap 0x%llx, Status 0x%llx", &val,
+			    &val2) != 2)
+				missing = 1;
+			else {
+				data |= MCG;
+				mr.mr_mcg_cap = val;
+				mr.mr_mcg_status = val2;
+			}
+		}
+		if (strncmp(s, "Vendor \"", 8) == 0) {
+			s += 8;
+			cp = index(s, '"');
+			if (cp != NULL) {
+				*cp = '\0';
+				mr.mr_cpu_vendor_id = find_cpu_vendor_id(s);
+				s = cp + 1;
+				if (sscanf(s, ", ID 0x%x, APIC ID %d",
+				    &mr.mr_cpu_id, &mr.mr_apic_id) != 2)
+					missing = 1;
+				else
+					data |= VENDOR;
+			} else
+				missing = 1;
+		}
+		if (strncmp(s, "CPU", 3) == 0) {
+			if (sscanf(s, "CPU %d ", &cpu) != 1)
+				missing = 1;
+			else {
+				data |= CPU;
+				if (old)
+					mr.mr_apic_id = cpu;
+				else
+					mr.mr_cpu = cpu;
+			}
+		}
+		if (strncmp(s, "Address", 7) == 0) {
+			if (sscanf(s, "Address 0x%llx", &val) != 1)
+				missing = 1;
+			else {
+				data |= ADDR;
+				mr.mr_addr = val;
+			}
+		}
+		if (strncmp(s, "Misc", 4) == 0) {
+			if (sscanf(s, "Misc 0x%llx", &val) != 1)
+				missing = 1;
+			else {
+				data |= MISC;
+				mr.mr_misc = val;
+			}
+		}
+	}
+
+	/* Dump the last record. */
+	if (data & BANK) {
+		if (mr.mr_status & MC_STATUS_ADDRV && !(data & ADDR))
+			missing = 1;
+		if (mr.mr_status & MC_STATUS_MISCV && !(data & MISC))
+			missing = 1;
+		convert_mca(&mr, &m, 0, sizeof(mr));
+		mce_cpuid(&m);
+		dump_mce_final(&m, symbol, missing, sizeof(struct mce), 0);
+	}
+}
+#endif
 
 static void remove_pidfile(void)
 {
@@ -943,6 +1265,10 @@ void usage(void)
 "  mcelog [options] --ascii < log\n"
 "  mcelog [options] --ascii --file log\n"
 "Decode machine check ASCII output from kernel logs\n"
+#ifdef __FreeBSD_
+"  mcelog [options] -M vmcore -N kernel\n"
+"Decode machine check error records from kernel crashdump.\n"
+#endif
 "\n"
 "Options:\n"  
 "--version           Show the version of mcelog and exit\n"
@@ -1147,6 +1473,14 @@ static int modifier(int opt)
 	case O_IS_CPU_SUPPORTED:
 		check_only = 1;
 		break;
+#ifdef __FreeBSD__
+	case 'M':
+		corefile = strdup(optarg);
+		break;
+	case 'N':
+		execfile = strdup(optarg);
+		break;
+#endif
 	case 0:
 		break;
 	default:
@@ -1195,10 +1529,12 @@ static int combined_modifier(int opt)
 
 static void general_setup(void)
 {
+#ifdef __Linux__
 	trigger_setup();
 	yellow_setup();
 	bus_setup();
 	unknown_setup();
+#endif
 	config_cred("global", "run-credentials", &runcred);
 	if (config_bool("global", "filter-memory-errors") == 1)
 		filter_memory_errors = 1;
@@ -1221,6 +1557,7 @@ static void drop_cred(void)
 	}
 }
 
+#ifdef __Linux__
 static void process(int fd, unsigned recordlen, unsigned loglen, char *buf)
 {	
 	int i; 
@@ -1273,6 +1610,173 @@ static void process(int fd, unsigned rec
 	if (finish)
 		exit(0);
 }
+#endif
+
+#ifdef __FreeBSD__
+#ifdef LOCAL_HACK
+struct mca_record_old {
+	uint64_t	mr_status;
+	uint64_t	mr_addr;
+	uint64_t	mr_misc;
+	uint64_t	mr_tsc;
+	int		mr_apic_id;
+	int		mr_bank;
+};
+#endif
+
+struct mca_record_internal {
+	struct mca_record rec;
+	int		logged;
+	STAILQ_ENTRY(mca_internal) link;
+};
+
+#ifdef LOCAL_HACK
+struct mca_record_internal_old {
+	struct mca_record_old rec;
+	int		logged;
+	STAILQ_ENTRY(mca_internal) link;
+};
+#endif
+
+static struct nlist nl[] = {
+#define	X_MCA_RECORDS		0
+	{ .n_name = "_mca_records" },
+#ifdef LOCAL_HACK
+#define	X_SNAPDATE		1
+	{ .n_name = "_snapdate" },
+#endif
+	{ .n_name = NULL },
+};
+
+static int
+kread(kvm_t *kvm, void *kvm_pointer, void *buf, size_t size, size_t offset)
+{
+	ssize_t ret;
+
+	ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, buf, size);
+	if (ret < 0 || (size_t)ret != size)
+		return (-1);
+	return (0);
+}
+
+static int
+kread_symbol(kvm_t *kvm, int index, void *buf, size_t size)
+{
+	ssize_t ret;
+
+	ret = kvm_read(kvm, nl[index].n_value, buf, size);
+	if (ret < 0 || (size_t)ret != size)
+		return (-1);
+	return (0);
+}
+
+static void process_kvm(const char *execfile, const char *corefile)
+{
+	struct mca_record mr, *mrp;
+	struct mce mce;
+	char errbuf[_POSIX2_LINE_MAX];
+	kvm_t *kvm;
+	size_t record_size, link_offset;
+	int i;
+#ifdef LOCAL_HACK
+	int snapdate;
+#endif
+
+	kvm = kvm_openfiles(execfile, corefile, NULL, O_RDONLY, errbuf);
+	if (kvm == NULL)
+		errx(1, "kvm_openfiles: %s", errbuf);
+	if (kvm_nlist(kvm, nl) != 0)
+		errx(1, "kvm_nlist: %s", kvm_geterr(kvm));
+
+#ifdef LOCAL_HACK
+	if (kread_symbol(kvm, X_SNAPDATE, &snapdate, sizeof(snapdate)) < 0)
+		errx(1, "kvm_read(snapdate) failed");
+#endif
+	/* stqh_first is the first pointer at this address. */
+	if (kread_symbol(kvm, X_MCA_RECORDS, &mrp, sizeof(mrp)) < 0)
+		errx(1, "kvm_read(mca_records) failed");
+#ifdef LOCAL_HACK
+	if (snapdate >= 20100329) {
+#endif
+		record_size = sizeof(struct mca_record);
+		link_offset = __offsetof(struct mca_record_internal,
+		    link.stqe_next);
+#ifdef LOCAL_HACK
+	} else {
+		record_size = sizeof(struct mca_record_old);
+		link_offset = __offsetof(struct mca_record_internal_old,
+		    link.stqe_next);
+	}
+#endif
+
+	for (i = 0; mrp != NULL; i++) {
+		memset(&mr, 0, sizeof(mr));
+		if (kread(kvm, mrp, &mr, record_size, 0) < 0)
+			break;
+		if (kread(kvm, mrp, &mrp, sizeof(mrp), link_offset) < 0)
+			mrp = NULL;
+
+		convert_mca(&mr, &mce, 1, record_size);
+		mce_prepare(&mce);
+		if (!mce_filter(&mce, sizeof(struct mce))) 
+			continue;
+		if (!dump_raw_ascii) {
+			disclaimer();
+			Wprintf("MCE %d\n", i);
+			dump_mce(&mce, sizeof(struct mce));
+		} else
+			dump_mce_raw_ascii(&mce, sizeof(struct mce));
+		flushlog();
+	}
+
+	exit(0);
+}
+
+static void process_live(void)
+{
+	struct mca_record mr;
+	struct mce mce;
+	int mib[4];
+	size_t len;
+	int count, finish, i;
+
+	len = sizeof(count);
+	if (sysctlbyname("hw.mca.count", &count, &len, NULL, 0) < 0)
+		return;
+
+	len = 4;
+	if (sysctlnametomib("hw.mca.records", mib, &len) < 0)
+		return;
+
+	finish = 0;
+	for (i = 0; i < count; i++) {
+		mib[3] = i;
+		len = sizeof(mr);
+		memset(&mr, 0, sizeof(mr));
+		if (sysctl(mib, 4, &mr, &len, NULL, 0) < 0) {
+			warn("sysctl(hw.mca.records.%d)", i);
+			continue;
+		}
+
+		convert_mca(&mr, &mce, 1, len);
+		mce_prepare(&mce);
+		if (numerrors > 0 && --numerrors == 0)
+			finish = 1;
+		if (!mce_filter(&mce, sizeof(struct mce))) 
+			continue;
+		if (!dump_raw_ascii) {
+			disclaimer();
+			Wprintf("MCE %d\n", i);
+			dump_mce(&mce, sizeof(struct mce));
+		} else
+			dump_mce_raw_ascii(&mce, sizeof(struct mce));
+		flushlog();
+	}
+
+	if (finish)
+		exit(0);
+}
+#endif
 
 static void noargs(int ac, char **av)
 {
@@ -1331,12 +1835,14 @@ struct mcefd_data {
 	char *buf;
 };
 
+#ifdef __Linux__
 static void process_mcefd(struct pollfd *pfd, void *data)
 {
 	struct mcefd_data *d = (struct mcefd_data *)data;
 	assert((pfd->revents & POLLIN) != 0);
 	process(pfd->fd, d->recordlen, d->loglen, d->buf);
 }
+#endif
 
 static void handle_sigusr1(int sig)
 {
@@ -1345,13 +1851,18 @@ static void handle_sigusr1(int sig)
 
 int main(int ac, char **av) 
 { 
+#ifdef __Linux__
 	struct mcefd_data d = {};
-	int opt;
 	int fd;
-
+#endif
+	int opt;
 	parse_config(av);
 
+#ifdef __FreeBSD
+	while ((opt = getopt_long(ac, av, "M:N:", options, NULL)) != -1) {
+#else
 	while ((opt = getopt_long(ac, av, "", options, NULL)) != -1) { 
+#endif
 		if (opt == '?') {
 			usage(); 
 		} else if (combined_modifier(opt) > 0) {
@@ -1371,11 +1882,13 @@ int main(int ac, char **av) 
 	} 
 
 	/* before doing anything else let's see if the CPUs are supported */
+#ifdef __Linux__
 	if (!cpu_forced && !is_cpu_supported()) {
 		if (!check_only)
 			fprintf(stderr, "CPU is unsupported\n");
 		exit(1);
 	}
+#endif
 	if (check_only)
 		exit(0);
 
@@ -1394,13 +1907,21 @@ int main(int ac, char **av) 
 	}
 
 	modifier_finish();
+#ifdef __Linux__
 	if (av[optind])
 		logfn = av[optind++];
+#endif
 	if (av[optind])
 		usage();
+#ifdef __FreeBSD__
+	if ((corefile != NULL) ^ (execfile != NULL) ||
+	    (corefile != NULL && daemon_mode))
+		usage();
+#endif
 	checkdmi();
 	general_setup();
 		
+#ifdef __Linux__
 	fd = open(logfn, O_RDONLY); 
 	if (fd < 0) {
 		if (ignore_nodev) 
@@ -1415,27 +1936,44 @@ int main(int ac, char **av) 
 		err("MCE_GET_LOG_LEN");
 
 	d.buf = xalloc(d.recordlen * d.loglen); 
+#endif
 	if (daemon_mode) {
 		prefill_memdb(do_dmi);
 		if (!do_dmi)
 			closedmi();
 		server_setup();
+#ifdef __Linux__
 		page_setup();
+#endif
 		if (imc_log)
 			set_imc_log(cputype);
 		drop_cred();
+#ifdef __Linux__
 		register_pollcb(fd, POLLIN, process_mcefd, &d);
+#endif
 		if (!foreground && daemon(0, need_stdout()) < 0)
 			err("daemon");
 		if (pidfile)
 			write_pidfile();
 		signal(SIGUSR1, handle_sigusr1);
+#ifdef __Linux__
 		event_signal(SIGUSR1);
+#endif
 		eventloop();
 	} else {
+#ifdef __Linux__
 		process(fd, d.recordlen, d.loglen, d.buf);
+#endif
+#ifdef __FreeBSD__
+		if (corefile != NULL)
+			process_kvm(execfile, corefile);
+		else
+			process_live();
+#endif
 	}
+#ifdef __Linux__
 	trigger_wait();
+#endif
 		
 	exit(0); 
 }