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#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/kallsyms.h>
33
34#include <linux/perf_event.h>
35#include <linux/hw_breakpoint.h>
36
37struct perf_event * __percpu *sample_hbp;
38
39static char ksym_name[KSYM_NAME_LEN] = "pid_max";
40module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);
41MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"
42 " write operations on the kernel symbol");
43
44static void sample_hbp_handler(struct perf_event *bp,
45 struct perf_sample_data *data,
46 struct pt_regs *regs)
47{
48 printk(KERN_INFO "%s value is changed\n", ksym_name);
49 dump_stack();
50 printk(KERN_INFO "Dump stack from sample_hbp_handler\n");
51}
52
53static int __init hw_break_module_init(void)
54{
55 int ret;
56 struct perf_event_attr attr;
57
58 hw_breakpoint_init(&attr);
59 attr.bp_addr = kallsyms_lookup_name(ksym_name);
60 attr.bp_len = HW_BREAKPOINT_LEN_4;
61 attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
62
63 sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
64 if (IS_ERR((void __force *)sample_hbp)) {
65 ret = PTR_ERR((void __force *)sample_hbp);
66 goto fail;
67 }
68
69 printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);
70
71 return 0;
72
73fail:
74 printk(KERN_INFO "Breakpoint registration failed\n");
75
76 return ret;
77}
78
79static void __exit hw_break_module_exit(void)
80{
81 unregister_wide_hw_breakpoint(sample_hbp);
82 printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);
83}
84
85module_init(hw_break_module_init);
86module_exit(hw_break_module_exit);
87
88MODULE_LICENSE("GPL");
89MODULE_AUTHOR("K.Prasad");
90MODULE_DESCRIPTION("ksym breakpoint");
91