11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
243203993SK.Prasad /*
343203993SK.Prasad * data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address
443203993SK.Prasad *
543203993SK.Prasad * usage: insmod data_breakpoint.ko ksym=<ksym_name>
643203993SK.Prasad *
743203993SK.Prasad * This file is a kernel module that places a breakpoint over ksym_name kernel
843203993SK.Prasad * variable using Hardware Breakpoint register. The corresponding handler which
943203993SK.Prasad * prints a backtrace is invoked every time a write operation is performed on
1043203993SK.Prasad * that variable.
1143203993SK.Prasad *
1243203993SK.Prasad * Copyright (C) IBM Corporation, 2009
13ba6909b7SK.Prasad *
14ba6909b7SK.Prasad * Author: K.Prasad <prasad@linux.vnet.ibm.com>
1543203993SK.Prasad */
1643203993SK.Prasad #include <linux/module.h> /* Needed by all modules */
1743203993SK.Prasad #include <linux/kernel.h> /* Needed for KERN_INFO */
1843203993SK.Prasad #include <linux/init.h> /* Needed for the macros */
19f60d24d2SFrederic Weisbecker #include <linux/kallsyms.h>
2043203993SK.Prasad
21f60d24d2SFrederic Weisbecker #include <linux/perf_event.h>
22f60d24d2SFrederic Weisbecker #include <linux/hw_breakpoint.h>
2343203993SK.Prasad
24*4b49df65SChen Jiahao static struct perf_event * __percpu *sample_hbp;
2543203993SK.Prasad
26d8a84d33SWill Deacon static char ksym_name[KSYM_NAME_LEN] = "jiffies";
2743203993SK.Prasad module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);
2843203993SK.Prasad MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"
2943203993SK.Prasad " write operations on the kernel symbol");
3043203993SK.Prasad
sample_hbp_handler(struct perf_event * bp,struct perf_sample_data * data,struct pt_regs * regs)31a8b0ca17SPeter Zijlstra static void sample_hbp_handler(struct perf_event *bp,
32b326e956SFrederic Weisbecker struct perf_sample_data *data,
33b326e956SFrederic Weisbecker struct pt_regs *regs)
3443203993SK.Prasad {
3543203993SK.Prasad printk(KERN_INFO "%s value is changed\n", ksym_name);
3643203993SK.Prasad dump_stack();
3743203993SK.Prasad printk(KERN_INFO "Dump stack from sample_hbp_handler\n");
3843203993SK.Prasad }
3943203993SK.Prasad
hw_break_module_init(void)4043203993SK.Prasad static int __init hw_break_module_init(void)
4143203993SK.Prasad {
4243203993SK.Prasad int ret;
43b326e956SFrederic Weisbecker struct perf_event_attr attr;
44d8a84d33SWill Deacon void *addr = __symbol_get(ksym_name);
45d8a84d33SWill Deacon
46d8a84d33SWill Deacon if (!addr)
47d8a84d33SWill Deacon return -ENXIO;
4843203993SK.Prasad
49b326e956SFrederic Weisbecker hw_breakpoint_init(&attr);
50d8a84d33SWill Deacon attr.bp_addr = (unsigned long)addr;
51dd1853c3SFrederic Weisbecker attr.bp_len = HW_BREAKPOINT_LEN_4;
524800314eSWill Deacon attr.bp_type = HW_BREAKPOINT_W;
5343203993SK.Prasad
544dc0da86SAvi Kivity sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
5544ee6358STejun Heo if (IS_ERR((void __force *)sample_hbp)) {
5644ee6358STejun Heo ret = PTR_ERR((void __force *)sample_hbp);
57f60d24d2SFrederic Weisbecker goto fail;
58f60d24d2SFrederic Weisbecker }
5943203993SK.Prasad
60f60d24d2SFrederic Weisbecker printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);
6143203993SK.Prasad
6243203993SK.Prasad return 0;
63f60d24d2SFrederic Weisbecker
64f60d24d2SFrederic Weisbecker fail:
65f60d24d2SFrederic Weisbecker printk(KERN_INFO "Breakpoint registration failed\n");
66f60d24d2SFrederic Weisbecker
67f60d24d2SFrederic Weisbecker return ret;
6843203993SK.Prasad }
6943203993SK.Prasad
hw_break_module_exit(void)7043203993SK.Prasad static void __exit hw_break_module_exit(void)
7143203993SK.Prasad {
72f60d24d2SFrederic Weisbecker unregister_wide_hw_breakpoint(sample_hbp);
73b9080468SArnd Bergmann #ifdef CONFIG_MODULE_UNLOAD
74910e230dSRong Tao __symbol_put(ksym_name);
75b9080468SArnd Bergmann #endif
7643203993SK.Prasad printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);
7743203993SK.Prasad }
7843203993SK.Prasad
7943203993SK.Prasad module_init(hw_break_module_init);
8043203993SK.Prasad module_exit(hw_break_module_exit);
8143203993SK.Prasad
8243203993SK.Prasad MODULE_LICENSE("GPL");
8343203993SK.Prasad MODULE_AUTHOR("K.Prasad");
8443203993SK.Prasad MODULE_DESCRIPTION("ksym breakpoint");
85