xref: /OK3568_Linux_fs/kernel/samples/hw_breakpoint/data_breakpoint.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * usage: insmod data_breakpoint.ko ksym=<ksym_name>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This file is a kernel module that places a breakpoint over ksym_name kernel
8*4882a593Smuzhiyun  * variable using Hardware Breakpoint register. The corresponding handler which
9*4882a593Smuzhiyun  * prints a backtrace is invoked every time a write operation is performed on
10*4882a593Smuzhiyun  * that variable.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Copyright (C) IBM Corporation, 2009
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Author: K.Prasad <prasad@linux.vnet.ibm.com>
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun #include <linux/module.h>	/* Needed by all modules */
17*4882a593Smuzhiyun #include <linux/kernel.h>	/* Needed for KERN_INFO */
18*4882a593Smuzhiyun #include <linux/init.h>		/* Needed for the macros */
19*4882a593Smuzhiyun #include <linux/kallsyms.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <linux/perf_event.h>
22*4882a593Smuzhiyun #include <linux/hw_breakpoint.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun struct perf_event * __percpu *sample_hbp;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static char ksym_name[KSYM_NAME_LEN] = "jiffies";
27*4882a593Smuzhiyun module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);
28*4882a593Smuzhiyun MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"
29*4882a593Smuzhiyun 			" write operations on the kernel symbol");
30*4882a593Smuzhiyun 
sample_hbp_handler(struct perf_event * bp,struct perf_sample_data * data,struct pt_regs * regs)31*4882a593Smuzhiyun static void sample_hbp_handler(struct perf_event *bp,
32*4882a593Smuzhiyun 			       struct perf_sample_data *data,
33*4882a593Smuzhiyun 			       struct pt_regs *regs)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	printk(KERN_INFO "%s value is changed\n", ksym_name);
36*4882a593Smuzhiyun 	dump_stack();
37*4882a593Smuzhiyun 	printk(KERN_INFO "Dump stack from sample_hbp_handler\n");
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
hw_break_module_init(void)40*4882a593Smuzhiyun static int __init hw_break_module_init(void)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	int ret;
43*4882a593Smuzhiyun 	struct perf_event_attr attr;
44*4882a593Smuzhiyun 	void *addr = __symbol_get(ksym_name);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (!addr)
47*4882a593Smuzhiyun 		return -ENXIO;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	hw_breakpoint_init(&attr);
50*4882a593Smuzhiyun 	attr.bp_addr = (unsigned long)addr;
51*4882a593Smuzhiyun 	attr.bp_len = HW_BREAKPOINT_LEN_4;
52*4882a593Smuzhiyun 	attr.bp_type = HW_BREAKPOINT_W;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
55*4882a593Smuzhiyun 	if (IS_ERR((void __force *)sample_hbp)) {
56*4882a593Smuzhiyun 		ret = PTR_ERR((void __force *)sample_hbp);
57*4882a593Smuzhiyun 		goto fail;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	return 0;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun fail:
65*4882a593Smuzhiyun 	printk(KERN_INFO "Breakpoint registration failed\n");
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	return ret;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
hw_break_module_exit(void)70*4882a593Smuzhiyun static void __exit hw_break_module_exit(void)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	unregister_wide_hw_breakpoint(sample_hbp);
73*4882a593Smuzhiyun 	symbol_put(ksym_name);
74*4882a593Smuzhiyun 	printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun module_init(hw_break_module_init);
78*4882a593Smuzhiyun module_exit(hw_break_module_exit);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun MODULE_LICENSE("GPL");
81*4882a593Smuzhiyun MODULE_AUTHOR("K.Prasad");
82*4882a593Smuzhiyun MODULE_DESCRIPTION("ksym breakpoint");
83