-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmc.patch
609 lines (603 loc) · 17.5 KB
/
mmc.patch
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
diff -uNr linux-3.6.8/arch/x86/include/asm/modulememcheck.h linux-3.6.8.0519/arch/x86/include/asm/modulememcheck.h
--- linux-3.6.8/arch/x86/include/asm/modulememcheck.h 1970-01-01 08:00:00.000000000 +0800
+++ linux-3.6.8.0519/arch/x86/include/asm/modulememcheck.h 2014-05-19 10:04:25.001428430 +0800
@@ -0,0 +1,13 @@
+#ifndef ASM_X86_MODULEMEMCHECK_H
+#define ASM_X86_MODULEMEMCHECK_H
+
+#include <linux/types.h>
+
+bool modulememcheck_fault(struct pt_regs *regs, unsigned long address, unsigned long error_code);
+bool modulememcheck_trap(struct pt_regs *regs);
+
+bool modulememcheck_alloc(unsigned long address, unsigned long size);
+void modulememcheck_free(unsigned long address_start);
+bool register_modulememcheck_pf_handler(bool (*handler)(struct pt_regs *regs, unsigned long address, unsigned long error_code, unsigned long buffer_start_address));
+void unregister_modulememcheck_pf_handler(void);
+#endif
diff -uNr linux-3.6.8/arch/x86/kernel/traps.c linux-3.6.8.0519/arch/x86/kernel/traps.c
--- linux-3.6.8/arch/x86/kernel/traps.c 2012-11-27 04:15:45.000000000 +0800
+++ linux-3.6.8.0519/arch/x86/kernel/traps.c 2014-05-19 10:09:08.731185877 +0800
@@ -45,6 +45,7 @@
#endif
#include <asm/kmemcheck.h>
+#include <asm/modulememcheck.h>
#include <asm/stacktrace.h>
#include <asm/processor.h>
#include <asm/debugreg.h>
@@ -404,6 +405,10 @@
if (!dr6 && user_mode(regs))
user_icebp = 1;
+ /* Catch modulememcheck conditions first of all! */
+ if ((dr6 & DR_STEP) && modulememcheck_trap(regs))
+ return;
+
/* Catch kmemcheck conditions first of all! */
if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
return;
diff -uNr linux-3.6.8/arch/x86/mm/fault.c linux-3.6.8.0519/arch/x86/mm/fault.c
--- linux-3.6.8/arch/x86/mm/fault.c 2012-11-27 04:15:45.000000000 +0800
+++ linux-3.6.8.0519/arch/x86/mm/fault.c 2014-05-19 10:10:43.339701166 +0800
@@ -17,6 +17,7 @@
#include <asm/traps.h> /* dotraplinkage, ... */
#include <asm/pgalloc.h> /* pgd_*(), ... */
#include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
+#include <asm/modulememcheck.h> /* modulememcheck_*(), ... */
#include <asm/fixmap.h> /* VSYSCALL_START */
/*
@@ -1044,6 +1045,9 @@
*/
if (unlikely(fault_in_kernel_space(address))) {
if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
+ if (modulememcheck_fault(regs, address, error_code))
+ return;
+
if (vmalloc_fault(address) >= 0)
return;
diff -uNr linux-3.6.8/arch/x86/mm/Makefile linux-3.6.8.0519/arch/x86/mm/Makefile
--- linux-3.6.8/arch/x86/mm/Makefile 2012-11-27 04:15:45.000000000 +0800
+++ linux-3.6.8.0519/arch/x86/mm/Makefile 2014-05-19 10:13:38.616899640 +0800
@@ -17,6 +17,7 @@
obj-$(CONFIG_HIGHMEM) += highmem_32.o
obj-$(CONFIG_KMEMCHECK) += kmemcheck/
+obj-y += modulememcheck/
obj-$(CONFIG_MMIOTRACE) += mmiotrace.o
mmiotrace-y := kmmio.o pf_in.o mmio-mod.o
diff -uNr linux-3.6.8/arch/x86/mm/modulememcheck/Makefile linux-3.6.8.0519/arch/x86/mm/modulememcheck/Makefile
--- linux-3.6.8/arch/x86/mm/modulememcheck/Makefile 1970-01-01 08:00:00.000000000 +0800
+++ linux-3.6.8.0519/arch/x86/mm/modulememcheck/Makefile 2014-05-19 10:12:13.156835521 +0800
@@ -0,0 +1,2 @@
+obj-y := modulememcheck.o
+
diff -uNr linux-3.6.8/arch/x86/mm/modulememcheck/modulememcheck.c linux-3.6.8.0519/arch/x86/mm/modulememcheck/modulememcheck.c
--- linux-3.6.8/arch/x86/mm/modulememcheck/modulememcheck.c 1970-01-01 08:00:00.000000000 +0800
+++ linux-3.6.8.0519/arch/x86/mm/modulememcheck/modulememcheck.c 2014-05-31 16:09:20.868239256 +0800
@@ -0,0 +1,528 @@
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kallsyms.h>
+#include <linux/kernel.h>
+#include <linux/kmemcheck.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/page-flags.h>
+#include <linux/percpu.h>
+#include <linux/ptrace.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/hash.h>
+
+#include <asm/cacheflush.h>
+#include <asm/modulememcheck.h>
+#include <asm/pgtable.h>
+#include <asm/tlbflush.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/preempt.h> // preempt_*
+
+#define MMC_DEBUG_ON
+#ifdef MMC_DEBUG_ON
+#define MMC_DEBUG(X) printk(KERN_ALERT X "\n")
+#else
+#define MMC_DEBUG(X) {}
+#endif
+
+#define HC_2M_PAGE_SHIFT 22
+#define HC_2M_PAGE_SIZE (_AC(1,UL) << HC_2M_PAGE_SHIFT)
+#define HC_2M_PAGE_MASK (~(HC_2M_PAGE_SIZE-1))
+
+struct modulememcheck_alloc_obj {
+ struct list_head list;
+ unsigned long address;
+ unsigned long page_address;
+ unsigned long size; // unit : B
+ unsigned long pages; // amount of pages
+ int type; // is a 2M page ? 1(yse) : 0(no)
+};
+
+static DEFINE_SPINLOCK(mmc_allocations_lock);
+//static DEFINE_PER_CPU(long, modulememcheck_regs_flags);
+
+/* ascending order by address */
+static struct list_head modulememcheck_allocations_head;
+
+#define MODULEMEMCHECK_ENABLED 1
+#define MODULEMEMCHECK_DISABLED 0
+static int modulememcheck_enabled = MODULEMEMCHECK_DISABLED;
+
+/* assigned in the fault handler and used in the trap, store the hide address */
+unsigned long hide_address;
+
+/* function pointer variable */
+static bool (*modulememcheck_pf_handler)(struct pt_regs *regs, unsigned long address, unsigned long error_code, unsigned long buffer_addr) = NULL;
+
+
+static pte_t *modulememcheck_addr_to_pte(unsigned long address)
+{
+ pte_t *pte;
+ unsigned int level;
+ pte = lookup_address(address, &level);
+ return pte;
+}
+
+/* 0 : not interest
+ * 1 : interest pte, not address
+ * other : interest address, the kernel address is > 1
+ */
+static unsigned long modulememcheck_is_interest(unsigned long address)
+{
+ struct modulememcheck_alloc_obj *pos;
+
+ /* check if the pte and address is interested */
+
+ if (list_empty(&modulememcheck_allocations_head))
+ return 0;
+
+ list_for_each_entry(pos, &modulememcheck_allocations_head, list) {
+ if (address >= pos->page_address) {
+ unsigned long psize;
+ psize = pos->type ? HC_2M_PAGE_SIZE : PAGE_SIZE;
+ if (address < (pos->page_address + psize * pos->pages)) {
+ if (address >= pos->address && address < (pos->address + pos->size))
+ return pos->address;
+ else
+ return 1;
+ }
+ } else
+ break;
+ }
+
+ return 0;
+}
+
+int __init modulememcheck_init(void)
+{
+#ifdef CONFIG_SMP
+ /*
+ * Limit SMP to use a single CPU. We rely on the fact that this code
+ * runs before SMP is set up.
+ */
+ if (setup_max_cpus > 1) {
+ printk(KERN_INFO
+ "modulememcheck: Limiting number of CPUs to 1.\n");
+ setup_max_cpus = 1;
+ }
+#endif
+
+ INIT_LIST_HEAD(&modulememcheck_allocations_head);
+
+ printk(KERN_INFO "modulememcheck: Initialized\n");
+ return 0;
+}
+
+early_initcall(modulememcheck_init);
+
+static int modulememcheck_show_addr(unsigned long address)
+{
+ pte_t *pte;
+ //printk("[MMC]->modulememcheck_show_addr : modulememcheck show addr %lx! \n", address);
+ pte = modulememcheck_addr_to_pte(address);
+ if (!pte)
+ return 0;
+ /*
+ if (pte_val(*pte) == 0) { // need load
+ // if (vmalloc_fault(address) < 0)
+ printk(KERN_ALERT "MMC: PTE ===== 00000000000000000000000\n");
+ return 0;
+ }
+*/
+ set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
+ __flush_tlb_one(address);
+ return 1;
+}
+
+static int modulememcheck_hide_addr(unsigned long address)
+{
+ pte_t *pte;
+ //printk("[MMC]->modulememcheck_hide_addr : modulememcheck hide addr %lx! \n", address);
+ pte = modulememcheck_addr_to_pte(address);
+ if (!pte)
+ return 0;
+
+ set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
+ __flush_tlb_one(address);
+ return 1;
+}
+
+static void modulememcheck_show_all(void)
+{
+ struct modulememcheck_alloc_obj *pos;
+
+ if (list_empty(&modulememcheck_allocations_head))
+ return ;
+
+ list_for_each_entry(pos, &modulememcheck_allocations_head, list) {
+ if (0 == pos->type) {
+ int i;
+ unsigned long address = pos->page_address;
+ for (i = 0; i < pos->pages; i++) {
+ modulememcheck_show_addr(address);
+ address += PAGE_SIZE;
+ }
+ }
+ }
+}
+
+static void modulememcheck_hide_all(void)
+{
+ struct modulememcheck_alloc_obj *pos;
+
+ if (list_empty(&modulememcheck_allocations_head))
+ return ;
+
+ list_for_each_entry(pos, &modulememcheck_allocations_head, list) {
+ if (0 == pos->type) {
+ int i;
+ unsigned long address = pos->page_address;
+ for (i = 0; i < pos->pages; i++) {
+ modulememcheck_hide_addr(address);
+ address += PAGE_SIZE;
+ }
+ }
+ }
+}
+
+static long saved_regs_flags = 0;
+static void modulememcheck_set_singlestep(struct pt_regs *regs)
+{
+// long saved_regs_flags = __get_cpu_var(modulememcheck_regs_flags);
+
+// if (!(saved_regs_flags & X86_EFLAGS_TF))
+ saved_regs_flags = regs->flags;
+ if (saved_regs_flags & X86_EFLAGS_IF)
+ regs->flags &= ~X86_EFLAGS_IF;
+ if (!(saved_regs_flags & X86_EFLAGS_TF))
+ regs->flags |= X86_EFLAGS_TF;
+ return ;
+
+/*
+ if (!(regs->flags & X86_EFLAGS_TF))
+ saved_regs_flags = regs->flags;
+
+ regs->flags |= X86_EFLAGS_TF;
+ regs->flags &= ~X86_EFLAGS_IF;
+ return ;
+ */
+}
+
+static void modulememcheck_clear_singlestep(struct pt_regs *regs)
+{
+// long saved_regs_flags = __get_cpu_var(modulememcheck_regs_flags);
+
+ if (!(saved_regs_flags & X86_EFLAGS_TF))
+ regs->flags &= ~X86_EFLAGS_TF;
+ if (saved_regs_flags & X86_EFLAGS_IF)
+ regs->flags |= X86_EFLAGS_IF;
+ return ;
+
+/*
+ if (!(saved_regs_flags & X86_EFLAGS_TF))
+ regs->flags &= ~X86_EFLAGS_TF;
+ if (saved_regs_flags & X86_EFLAGS_IF)
+ regs->flags |= X86_EFLAGS_IF;
+ return ;
+ */
+}
+
+static int mmc_what_flag = 0;
+
+bool modulememcheck_fault(struct pt_regs *regs, unsigned long address,
+ unsigned long error_code)
+{
+ unsigned long ret;
+// unsigned long irq_flags;
+
+ if (modulememcheck_enabled == MODULEMEMCHECK_DISABLED)
+ return false;
+
+ BUG_ON(!modulememcheck_pf_handler);
+
+ BUG_ON(!regs);
+
+
+ if (regs->flags & X86_VM_MASK)
+ return false;
+ if (regs->cs != __KERNEL_CS)
+ return false;
+
+ //printk("[MODULE MEM CHECK] : modulememcheck falut ! \n");
+
+// MMC_DEBUG("modulememcheck_fault : lock");
+// spin_lock_irqsave(&mmc_allocations_lock, irq_flags);
+ modulememcheck_show_all();
+ ret = modulememcheck_is_interest(address);
+// MMC_DEBUG("modulememcheck_fault : unlock");
+// spin_unlock_irqrestore(&mmc_allocations_lock, irq_flags);
+
+ if (ret == 0) {
+ modulememcheck_hide_all();
+ return false;
+ }
+/*
+ // make the pte present
+ if (!modulememcheck_show_addr(address)) { // pte is 0
+ // MMC_DEBUG("modulememcheck_fault : show addr false");
+ printk("modulememcheck_fault : show addr false\n");
+ return false;
+ }
+*/
+ hide_address = address;
+ mmc_what_flag = 1;
+
+// printk(KERN_ALERT"[MMC]fault \n");
+ if (ret > 1) {
+ if ((*modulememcheck_pf_handler)(regs, address, error_code, ret))
+// printk(KERN_ALERT"[MMC]fault : %lx\n", address);
+ ;//printk("[MMC]->modulememcheck_fault\n");
+ }// else
+
+ // ret == 1
+ // for single step
+ modulememcheck_set_singlestep(regs);
+
+ return true;
+}
+
+bool modulememcheck_trap(struct pt_regs *regs)
+{
+ if (modulememcheck_enabled == MODULEMEMCHECK_DISABLED)
+ return false;
+ if (mmc_what_flag == 0)
+ return false;
+// printk(KERN_ALERT"[MMC]trap : \n");
+ modulememcheck_hide_all();
+// modulememcheck_hide_addr(hide_address);
+ mmc_what_flag = 0;
+ modulememcheck_clear_singlestep(regs);
+ return true;
+}
+
+/*
+ * add to list in Ascending order
+ */
+static bool modulememcheck_allocations_add(struct modulememcheck_alloc_obj *obj)
+{
+ struct modulememcheck_alloc_obj *pos;
+ struct modulememcheck_alloc_obj *prev;
+
+ if (list_empty(&modulememcheck_allocations_head)) {
+ list_add(&obj->list, &modulememcheck_allocations_head);
+ return true;
+ }
+
+ list_for_each_entry(pos, &modulememcheck_allocations_head, list) {
+ if (obj->page_address < pos->page_address)
+ break;
+ }
+ prev = list_entry(pos->list.prev, struct modulememcheck_alloc_obj, list);
+
+ // the "address + size < pos->page_address"
+ // can deduce "page_address + pagesize * pages < pos->page_address"
+
+ // add to the end
+ if (&pos->list == &modulememcheck_allocations_head) {
+ if (obj->page_address < (prev->address + prev->size))
+ return false;
+ else {
+ __list_add(&obj->list, &prev->list, &pos->list);
+ return true;
+ }
+ }
+ // add to the first
+ if (&prev->list == &modulememcheck_allocations_head) {
+ if ((obj->address + obj->size) > pos->page_address)
+ return false;
+ else {
+ __list_add(&obj->list, &prev->list, &pos->list);
+ return true;
+ }
+ }
+ // add into the middle
+ if (obj->page_address < (prev->address + prev->size) ||
+ (obj->address + obj->size) > pos->page_address)
+ return false;
+ __list_add(&obj->list, &prev->list, &pos->list);
+ return true;
+}
+
+/*
+ * del from the allocations list
+ */
+static void modulememcheck_allocations_del(struct modulememcheck_alloc_obj *del_obj)
+{
+ list_del(&del_obj->list);
+}
+
+/*
+ * recovery the hide pages, delete objects, free the resource
+ */
+static void modulememcheck_allocations_remove(struct modulememcheck_alloc_obj *obj)
+{
+ if (!obj)
+ return ;
+ if (obj->type == 1) { // 2M page
+ if (!modulememcheck_show_addr(obj->address)) {
+ printk(KERN_ALERT"[MMC] mmc_allocation_remove[large page] : pte is 0\n");
+ }
+ } else {
+ int i;
+ unsigned long address = obj->address;
+ /* recovry all the pages related to the [address, address + size] */
+ for (i = 0; i < obj->pages; i++) {
+ if (!modulememcheck_show_addr(address)) {
+ printk(KERN_ALERT"[MMC] mmc_allocation_remove : pte is 0\n");
+ }
+ address += PAGE_SIZE;
+ }
+ }
+ modulememcheck_allocations_del(obj);
+ //kfree(obj);
+ vfree(obj);
+}
+
+static void modulememcheck_allocations_clear(void)
+{
+ struct modulememcheck_alloc_obj *pos, *node;
+
+ if (list_empty(&modulememcheck_allocations_head)) {
+ return ;
+ }
+
+ list_for_each_entry_safe(pos, node, &modulememcheck_allocations_head, list) {
+ modulememcheck_allocations_remove(pos);
+ }
+}
+
+static struct modulememcheck_alloc_obj *modulememcheck_allocations_lookup(unsigned long address)
+{
+ struct modulememcheck_alloc_obj *pos;
+
+ if (list_empty(&modulememcheck_allocations_head)) {
+ return NULL;
+ }
+
+ list_for_each_entry(pos, &modulememcheck_allocations_head, list) {
+ if (address == pos->address) {
+ return pos;
+ }
+ if (address < pos->address) {
+ break;
+ }
+ }
+ return NULL;
+}
+
+bool modulememcheck_alloc(unsigned long address, unsigned long size)
+{
+ struct modulememcheck_alloc_obj *obj;
+ int i;
+ pte_t *pte;
+ int level;
+// unsigned long irq_flags;
+// obj = (struct modulememcheck_alloc_obj *)kmalloc(sizeof(struct modulememcheck_alloc_obj), GFP_KERNEL);
+ obj = (struct modulememcheck_alloc_obj *)vmalloc(sizeof(struct modulememcheck_alloc_obj));
+ if (!obj) {
+ printk("[MMC]->modulememcheck_alloc :not enough memory\n");
+ return false;
+ }
+ obj->address = address;
+ obj->size = size;
+
+ pte = lookup_address(address, &level);
+ // check whether the page is exist ?
+ if (!(pte_val(*pte) | _PAGE_PRESENT)) {
+ *(char *)address = '0';
+ }
+
+
+// MMC_DEBUG("modulememcheck_alloc : lock");
+// spin_lock_irqsave(&mmc_allocations_lock, irq_flags);
+ // is a 2M page ?
+ if (2 == level) { // 2: PG_LEVEL_2M
+ obj->type = 1;
+ obj->page_address = address & HC_2M_PAGE_MASK;
+ obj->pages = 1;
+ if (!modulememcheck_allocations_add(obj)) {
+ goto mmc_alloc_failed;
+ }
+ modulememcheck_hide_addr(address);
+ } else { // 1: PG_LEVEL_4K
+ obj->type = 0;
+ obj->page_address = address & PAGE_MASK;
+
+ /* hide all the pages related to the [address, address + size] */
+ size = size + address - obj->page_address;
+ obj->pages = (size / PAGE_SIZE) + ((size % PAGE_SIZE) ? 1 : 0);
+
+// printk(KERN_ALERT "[MMC]ALLOC: address 0x%.8lx, paddr 0x%.8lx, pages %d\n",
+// obj->address, obj->page_address, obj->pages);
+
+ if (!modulememcheck_allocations_add(obj)) {
+ goto mmc_alloc_failed;
+ }
+ for (i = 0; i < obj->pages; i++) {
+ modulememcheck_hide_addr(address);
+ address += PAGE_SIZE;
+ }
+ }
+// MMC_DEBUG("modulememcheck_alloc : unlock");
+ //spin_unlock_irqrestore(&mmc_allocations_lock, irq_flags);
+ return true;
+mmc_alloc_failed:
+// MMC_DEBUG("modulememcheck_alloc : unlock");
+ //spin_unlock_irqrestore(&mmc_allocations_lock, irq_flags);
+ vfree(obj);
+ //kfree(obj);
+ return false;
+}
+EXPORT_SYMBOL(modulememcheck_alloc);
+
+void modulememcheck_free(unsigned long address_start)
+{
+ struct modulememcheck_alloc_obj *obj;
+ unsigned long irq_flags;
+// MMC_DEBUG("modulememcheck_free : lock");
+ //spin_lock_irqsave(&mmc_allocations_lock, irq_flags);
+ obj = modulememcheck_allocations_lookup(address_start);
+ modulememcheck_allocations_remove(obj);
+// MMC_DEBUG("modulememcheck_free : unlock");
+// spin_unlock_irqrestore(&mmc_allocations_lock, irq_flags);
+}
+EXPORT_SYMBOL(modulememcheck_free);
+
+/* if have registered before or handler is NULL, then failed */
+bool register_modulememcheck_pf_handler(bool (*handler)(struct pt_regs *regs, unsigned long address, unsigned long error_code, unsigned long buffer_addr))
+{
+ if ((handler == NULL) ||
+ (modulememcheck_enabled == MODULEMEMCHECK_ENABLED))
+ return false;
+ modulememcheck_pf_handler = handler;
+ modulememcheck_enabled = MODULEMEMCHECK_ENABLED;
+ printk("[MMC] : registered!\n");
+ return true;
+}
+EXPORT_SYMBOL(register_modulememcheck_pf_handler);
+
+void unregister_modulememcheck_pf_handler(void)
+{
+ unsigned long irq_flags;
+ if (modulememcheck_enabled == MODULEMEMCHECK_ENABLED) {
+ modulememcheck_enabled = MODULEMEMCHECK_DISABLED;
+ modulememcheck_pf_handler = NULL;
+
+ // clear the allocations objs
+// MMC_DEBUG("unregister_modulememcheck_pf_handler : lock");
+// spin_lock_irqsave(&mmc_allocations_lock, irq_flags);
+ modulememcheck_allocations_clear();
+// MMC_DEBUG("unregister_modulememcheck_pf_handler : lock");
+// spin_unlock_irqrestore(&mmc_allocations_lock, irq_flags);
+
+ printk("[MMC] : unregistered!\n");
+ }
+}
+EXPORT_SYMBOL(unregister_modulememcheck_pf_handler);