xref: /linux/samples/hw_breakpoint/data_breakpoint.c (revision 432039933a16b8227b7b267f46ac1c1b9b3adf14)
1*43203993SK.Prasad /*
2*43203993SK.Prasad  * data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address
3*43203993SK.Prasad  *
4*43203993SK.Prasad  * This program is free software; you can redistribute it and/or modify
5*43203993SK.Prasad  * it under the terms of the GNU General Public License as published by
6*43203993SK.Prasad  * the Free Software Foundation; either version 2 of the License, or
7*43203993SK.Prasad  * (at your option) any later version.
8*43203993SK.Prasad  *
9*43203993SK.Prasad  * This program is distributed in the hope that it will be useful,
10*43203993SK.Prasad  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*43203993SK.Prasad  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*43203993SK.Prasad  * GNU General Public License for more details.
13*43203993SK.Prasad  *
14*43203993SK.Prasad  * You should have received a copy of the GNU General Public License
15*43203993SK.Prasad  * along with this program; if not, write to the Free Software
16*43203993SK.Prasad  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*43203993SK.Prasad  *
18*43203993SK.Prasad  * usage: insmod data_breakpoint.ko ksym=<ksym_name>
19*43203993SK.Prasad  *
20*43203993SK.Prasad  * This file is a kernel module that places a breakpoint over ksym_name kernel
21*43203993SK.Prasad  * variable using Hardware Breakpoint register. The corresponding handler which
22*43203993SK.Prasad  * prints a backtrace is invoked everytime a write operation is performed on
23*43203993SK.Prasad  * that variable.
24*43203993SK.Prasad  *
25*43203993SK.Prasad  * Copyright (C) IBM Corporation, 2009
26*43203993SK.Prasad  */
27*43203993SK.Prasad #include <linux/module.h>	/* Needed by all modules */
28*43203993SK.Prasad #include <linux/kernel.h>	/* Needed for KERN_INFO */
29*43203993SK.Prasad #include <linux/init.h>		/* Needed for the macros */
30*43203993SK.Prasad 
31*43203993SK.Prasad #include <asm/hw_breakpoint.h>
32*43203993SK.Prasad 
33*43203993SK.Prasad struct hw_breakpoint sample_hbp;
34*43203993SK.Prasad 
35*43203993SK.Prasad static char ksym_name[KSYM_NAME_LEN] = "pid_max";
36*43203993SK.Prasad module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);
37*43203993SK.Prasad MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"
38*43203993SK.Prasad 			" write operations on the kernel symbol");
39*43203993SK.Prasad 
40*43203993SK.Prasad void sample_hbp_handler(struct hw_breakpoint *temp, struct pt_regs
41*43203993SK.Prasad 								*temp_regs)
42*43203993SK.Prasad {
43*43203993SK.Prasad 	printk(KERN_INFO "%s value is changed\n", ksym_name);
44*43203993SK.Prasad 	dump_stack();
45*43203993SK.Prasad 	printk(KERN_INFO "Dump stack from sample_hbp_handler\n");
46*43203993SK.Prasad }
47*43203993SK.Prasad 
48*43203993SK.Prasad static int __init hw_break_module_init(void)
49*43203993SK.Prasad {
50*43203993SK.Prasad 	int ret;
51*43203993SK.Prasad 
52*43203993SK.Prasad #ifdef CONFIG_X86
53*43203993SK.Prasad 	sample_hbp.info.name = ksym_name;
54*43203993SK.Prasad 	sample_hbp.info.type = HW_BREAKPOINT_WRITE;
55*43203993SK.Prasad 	sample_hbp.info.len = HW_BREAKPOINT_LEN_4;
56*43203993SK.Prasad #endif /* CONFIG_X86 */
57*43203993SK.Prasad 
58*43203993SK.Prasad 	sample_hbp.triggered = (void *)sample_hbp_handler;
59*43203993SK.Prasad 
60*43203993SK.Prasad 	ret = register_kernel_hw_breakpoint(&sample_hbp);
61*43203993SK.Prasad 
62*43203993SK.Prasad 	if (ret < 0) {
63*43203993SK.Prasad 		printk(KERN_INFO "Breakpoint registration failed\n");
64*43203993SK.Prasad 		return ret;
65*43203993SK.Prasad 	} else
66*43203993SK.Prasad 		printk(KERN_INFO "HW Breakpoint for %s write installed\n",
67*43203993SK.Prasad 								ksym_name);
68*43203993SK.Prasad 
69*43203993SK.Prasad 	return 0;
70*43203993SK.Prasad }
71*43203993SK.Prasad 
72*43203993SK.Prasad static void __exit hw_break_module_exit(void)
73*43203993SK.Prasad {
74*43203993SK.Prasad 	unregister_kernel_hw_breakpoint(&sample_hbp);
75*43203993SK.Prasad 	printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);
76*43203993SK.Prasad }
77*43203993SK.Prasad 
78*43203993SK.Prasad module_init(hw_break_module_init);
79*43203993SK.Prasad module_exit(hw_break_module_exit);
80*43203993SK.Prasad 
81*43203993SK.Prasad MODULE_LICENSE("GPL");
82*43203993SK.Prasad MODULE_AUTHOR("K.Prasad");
83*43203993SK.Prasad MODULE_DESCRIPTION("ksym breakpoint");
84