aboutsummaryrefslogtreecommitdiffstats
path: root/mce.lex
blob: 7627022482da77da4ce4d300872501c8c3c2e226 (plain)
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
/* Copyright (c) 2008 by Intel Corp.
   Scanner for the machine check grammar.

   mce-inject is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; version
   2.

   mce-inject is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should find a copy of v2 of the GNU General Public License somewhere
   on your Linux system; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

   Authors:
        Andi Kleen
	Ying Huang
*/
%{ 
#define _GNU_SOURCE 1
#include <stdlib.h>
#include <string.h>

#include "mce.h"
#include "parser.h"
#include "mce.tab.h"
#include "util.h"
#include "inject.h"

int yylineno;

static int lookup_symbol(const char *);
%}

%%

#.*\n			/* comment */;
\n			++yylineno;
0x[0-9a-fA-F]+ 		| 
0[0-7]+			|
[0-9]+			yylval = strtoull(yytext, NULL, 0); return NUMBER;
[:{}<>]			return yytext[0];
[_a-zA-Z][_a-zA-Z0-9]*	return lookup_symbol(yytext);
[ \t]+			/* white space */;
.			yyerror("Unrecognized character '%s'", yytext);

%%

/* Keyword handling */

static struct key { 
	const char *name;
	int tok;
	u64 val;
} keys[] = { 
#define KEY(x) { #x, x }
#define KEYVAL(x,v) { #x, x, v }
	KEY(MCE),
	KEY(STATUS), 
	KEY(RIP), 
	KEY(TSC), 
	KEY(ADDR), 
	KEY(MISC), 
	KEY(CPU), 
	KEY(BANK), 
	KEY(MCGSTATUS), 
	KEY(NOBROADCAST), 
	KEY(HOLD),
	KEY(IN_IRQ),
	KEYVAL(CORRECTED, MCI_STATUS_VAL), 	// checkme
	KEYVAL(UNCORRECTED, MCI_STATUS_VAL|MCI_STATUS_UC|MCI_STATUS_EN),
	KEYVAL(FATAL, MCI_STATUS_VAL|MCI_STATUS_UC|MCI_STATUS_EN
	       |MCI_STATUS_PCC),
	KEY(MACHINE), 
	KEY(CHECK), 
	KEY(EXCEPTION), 
	KEYVAL(RIPV, MCG_STATUS_RIPV), 
	KEYVAL(EIPV, MCG_STATUS_EIPV), 
	KEYVAL(MCIP, MCG_STATUS_MCIP), 
	KEYVAL(VAL, MCI_STATUS_VAL), 
	KEYVAL(OVER, MCI_STATUS_OVER), 
	KEYVAL(UC, MCI_STATUS_UC), 
	KEYVAL(EN, MCI_STATUS_EN), 
	KEYVAL(PCC, MCI_STATUS_PCC), 
};

static int cmp_key(const void *av, const void *bv)
{
	const struct key *a = av;
	const struct key *b = bv;
	return strcasecmp(a->name, b->name);
}

static int lookup_symbol(const char *name) 
{
	struct key *k;
	struct key key;
	key.name = name;
	k = bsearch(&key, keys, ARRAY_SIZE(keys), sizeof(struct key), cmp_key);
	if (k != NULL) { 
		yylval = k->val;
		return k->tok;
	}
	return SYMBOL;
}

static void init_lex(void)
{
	qsort(keys, ARRAY_SIZE(keys), sizeof(struct key), cmp_key);
}

int do_dump;
static char **argv;
char *filename = "<stdin>";

int yywrap(void)
{
	if (*argv == NULL)
		return 1;
	filename = *argv;
	yyin = fopen(filename, "r");
	if (!yyin)
		err(filename);
	argv++;
	return 0;
}

int main(int ac, char **av)
{
	init_lex();
	argv = ++av;	
 	if (*av && !strcmp(*av, "--dump")) {
 		do_dump = 1;
 		av++;
 	}
 	init_cpu_info();
 	init_inject();
	if (*argv)
		yywrap();
	return yyparse();
}