-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmimgr.c
More file actions
408 lines (311 loc) · 8.18 KB
/
Copy pathnmimgr.c
File metadata and controls
408 lines (311 loc) · 8.18 KB
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
/*
* Copyright (C) 2017 Adrien Mahieux <adrien.mahieux@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
/*
* Kernel Revision history:
* 2.6.32: Using notifier_block structs
* 3.2 : Moved NMI descriptions to an enum: LOCAL, UNKNOWN, MAX
* https://lwn.net/Articles/461215/
* https://lkml.org/lkml/2012/3/8/386
* 3.5 : Moved register_nmi_handler to macro+static struct nmiaction fn##_na
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/nmi.h>
#include <linux/kallsyms.h>
#include <asm/nmi.h>
#include <asm/x86_init.h>
/* Compatibility management */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
#include <linux/notifier.h>
#include <linux/kdebug.h> /* For NMI_DIE and NMI_DIE_IPI */
#include <asm/kdebug.h>
/* HANDLED=1, OK=1, DONE=0 */
#define NMI_HANDLED NOTIFY_OK
#define NMI_DONE NOTIFY_DONE
#endif
#define NMIMGR_VERSION "0.4"
#define NMIMGR_NAME "nmimgr"
#define NMIMGR_NBMAX 256
enum {
OP_IGNORE=0,
OP_DROP,
OP_DEBUG,
OP_PANIC
};
static int events_arr[4][NMIMGR_NBMAX];
static char *events_ignore;
static char *events_debug;
static char *events_drop;
static char *events_panic;
static void __nmimgr_trace(struct pt_regs *regs) {
static void (*sym_show_regs)(struct pt_regs*);
dump_stack();
/* show_regs is not exported */
#ifdef MODULE
# ifdef CONFIG_KALLSYMS
# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
/* Try to find show_regs */
sym_show_regs = (void*)kallsyms_lookup_name("show_regs");
if (sym_show_regs)
sym_show_regs(regs);
# endif
# endif /* CONFIG_KALLSYMS */
#else /* MODULE */
show_regs(regs);
#endif
}
/**
* Handler
*/
static int __nmimgr_handle(unsigned int type, unsigned char reason,
struct pt_regs *regs)
{
int i;
/* ignored NMI */
for (i = 1; i < NMIMGR_NBMAX; i++) {
if (reason == events_arr[OP_IGNORE][i])
return NMI_DONE;
}
pr_notice(NMIMGR_NAME": Handling new NMI type:%u event:0x%02x (%d)\n",
type, reason, reason);
/* Debugging NMI */
for (i = 1; i < NMIMGR_NBMAX; i++ ) {
if (reason == events_arr[OP_DEBUG][i]) {
pr_notice(NMIMGR_NAME": Debug NMI");
__nmimgr_trace(regs);
}
}
/* dropped NMI */
for (i = 1; i < NMIMGR_NBMAX; i++) {
if (reason == events_arr[OP_DROP][i]) {
pr_notice(NMIMGR_NAME": Drop NMI event:0x%02x (%d)\n",
reason, reason);
return NMI_HANDLED;
}
}
/* Panic NMI */
for (i = 1; i < NMIMGR_NBMAX; i++) {
if (reason == events_arr[OP_PANIC][i]) {
pr_emerg(NMIMGR_NAME": Panic on Event:0x%02x(%d)\n",
reason, reason);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
nmi_panic(regs, NMIMGR_NAME": Hit explicit panic");
#else
panic(NMIMGR_NAME": Hit explicit panic");
#endif
}
}
/* Still there: unmanaged NMI Code. Send to other handlers */
pr_notice(NMIMGR_NAME": Unmanaged NMI event:0x%02x (%d), let it pass\n",
reason, reason);
return NMI_DONE;
}
/***** Kernel < 3.2 **********************************************************/
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
static int nmimgr_handle(struct notifier_block *nb, unsigned long val,
void *data)
{
struct die_args *args = (struct die_args *)data;
unsigned char reason = args->err;
/* Only process NMI cases */
switch (val) {
case DIE_NMI:
case DIE_NMIWATCHDOG:
case DIE_NMI_IPI:
case DIE_NMIUNKNOWN:
return __nmimgr_handle(1, reason, args->regs);
default:
break;
}
return NOTIFY_OK;
}
static struct notifier_block nmimgr_notifier = {
.notifier_call = nmimgr_handle,
.priority = 0x7FFFFFFF
};
/**
* Handler registration
*/
static int nmimgr_register(void)
{
int ret = register_die_notifier(&nmimgr_notifier);
if (ret) {
pr_warn(NMIMGR_NAME": Unable to register NMI handler\n");
return ret;
}
pr_notice(NMIMGR_NAME": Registered handler\n");
return 0;
}
/**
* Handler unregistration
*/
static void nmimgr_unregister(void)
{
unregister_die_notifier(&nmimgr_notifier);
}
/***** Kernel 3.2+ ***********************************************************/
#else
static int nmimgr_handle(unsigned int type, struct pt_regs *regs)
{
return __nmimgr_handle(type, x86_platform.get_nmi_reason(), regs);
}
/**
* handler registration
*/
static int nmimgr_register(void)
{
int ret = 0;
int i;
/* A loop wont work because of Macro rewriting of
* register_nmi_handler (https://lkml.org/lkml/2012/3/8/386)
*/
/* We register our handler first, as we only manage a specific list */
ret = register_nmi_handler(
NMI_UNKNOWN, nmimgr_handle, NMI_FLAG_FIRST, NMIMGR_NAME);
if (ret) {
pr_warn(NMIMGR_NAME ": Unable to register NMI_UNKNOWN\n");
i = NMI_UNKNOWN-1;
goto err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
ret = register_nmi_handler(
NMI_SERR, nmimgr_handle, NMI_FLAG_FIRST, NMIMGR_NAME);
if (ret) {
pr_warn(NMIMGR_NAME ": Unable to register NMI_SERR\n");
i = NMI_SERR-1;
goto err;
}
ret = register_nmi_handler(
NMI_IO_CHECK, nmimgr_handle, NMI_FLAG_FIRST, NMIMGR_NAME);
if (ret) {
pr_warn(NMIMGR_NAME ": Unable to register NMI_IO_CHECK\n");
i = NMI_IO_CHECK-1;
goto err;
}
#endif
return 0;
err:
for (; i > 0; i--)
unregister_nmi_handler(i, NMIMGR_NAME);
return ret;
}
/**
* Handler unregistration
*/
static void nmimgr_unregister(void)
{
int i;
for (i = NMI_MAX-1; i > NMI_LOCAL; i--)
unregister_nmi_handler(i, NMIMGR_NAME);
}
#endif
/*****************************************************************************/
static int __init __nmimgr_setup(int op, char *str)
{
char *ret = 0;
if (!str)
return 1;
/* lib/cmdline.c: Extract int list from str into events_panic_list[] */
ret = get_options(str, ARRAY_SIZE(events_arr[op]),
events_arr[op]);
if (ret && *ret != 0) {
pr_err(NMIMGR_NAME": Invalid input '%s', ret:%s\n", str, ret);
return 0;
}
return 1;
}
/**
* Parse the input string
*/
static int __init nmimgr_setup_panic(char *str)
{
if (!str)
return 1;
pr_info(NMIMGR_NAME ": events_panic: %s\n", str);
return __nmimgr_setup(OP_PANIC, str);
}
__setup("nmimgr.events_panic=", nmimgr_setup_panic);
/**
*
*/
static int __init nmimgr_setup_debug(char *str)
{
if (!str)
return 1;
pr_info(NMIMGR_NAME ": events_debug: %s\n", str);
return __nmimgr_setup(OP_DEBUG, str);
}
__setup("nmimgr.events_debug=", nmimgr_setup_debug);
/**
*
*/
static int __init nmimgr_setup_ignore(char *str)
{
if (!str)
return 1;
pr_info(NMIMGR_NAME ": events_ignore: %s\n", str);
return __nmimgr_setup(OP_IGNORE, str);
}
__setup("nmimgr.events_ignore=", nmimgr_setup_ignore);
/**
*
*/
static int __init nmimgr_setup_drop(char *str)
{
if (!str)
return 1;
pr_info(NMIMGR_NAME ": events_drop: %s\n", str);
return __nmimgr_setup(OP_DROP, str);
}
__setup("nmimgr.events_drop=", nmimgr_setup_drop);
/**
* Module initialization
*/
int __init init_module(void)
{
int err;
pr_notice(NMIMGR_NAME ": Loaded module v%s\n", NMIMGR_VERSION);
nmimgr_setup_ignore(events_ignore);
nmimgr_setup_debug(events_debug);
nmimgr_setup_panic(events_panic);
nmimgr_setup_drop(events_drop);
err = nmimgr_register();
if (err) {
pr_warn(NMIMGR_NAME": NMI Management not available\n");
return err;
}
return 0;
}
/* module_init(init_module); */
/**
* Module unloading
*/
void __exit clean_module(void)
{
nmimgr_unregister();
pr_notice(NMIMGR_NAME": unloaded module\n");
}
module_exit(clean_module);
MODULE_AUTHOR("Adrien Mahieux <adrien.mahieux@gmail.com");
MODULE_DESCRIPTION("Remap specified NMI codes to generate a Panic\n"
"or drops specific events (self-test or while kdump'ing)\n"
"Also reads kernel parameter events_panic= upon loading");
MODULE_LICENSE("GPL");
MODULE_VERSION(NMIMGR_VERSION);
/* Parameters */
module_param(events_panic, charp, 0444);
MODULE_PARM_DESC(events_panic, "List of NMIs to panic upon receiving");
module_param(events_debug, charp, 0444);
MODULE_PARM_DESC(events_debug, "List of NMIs to show debug upon receiving");
module_param(events_ignore, charp, 0444);
MODULE_PARM_DESC(events_ignore, "List of NMIs to ignore silently");
module_param(events_drop, charp, 0444);
MODULE_PARM_DESC(events_drop, "List of NMIs to hide from other handlers");