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