18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * usage: insmod data_breakpoint.ko ksym=<ksym_name> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file is a kernel module that places a breakpoint over ksym_name kernel 88c2ecf20Sopenharmony_ci * variable using Hardware Breakpoint register. The corresponding handler which 98c2ecf20Sopenharmony_ci * prints a backtrace is invoked every time a write operation is performed on 108c2ecf20Sopenharmony_ci * that variable. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Copyright (C) IBM Corporation, 2009 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * Author: K.Prasad <prasad@linux.vnet.ibm.com> 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci#include <linux/module.h> /* Needed by all modules */ 178c2ecf20Sopenharmony_ci#include <linux/kernel.h> /* Needed for KERN_INFO */ 188c2ecf20Sopenharmony_ci#include <linux/init.h> /* Needed for the macros */ 198c2ecf20Sopenharmony_ci#include <linux/kallsyms.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <linux/perf_event.h> 228c2ecf20Sopenharmony_ci#include <linux/hw_breakpoint.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistruct perf_event * __percpu *sample_hbp; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic char ksym_name[KSYM_NAME_LEN] = "jiffies"; 278c2ecf20Sopenharmony_cimodule_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO); 288c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any" 298c2ecf20Sopenharmony_ci " write operations on the kernel symbol"); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic void sample_hbp_handler(struct perf_event *bp, 328c2ecf20Sopenharmony_ci struct perf_sample_data *data, 338c2ecf20Sopenharmony_ci struct pt_regs *regs) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci printk(KERN_INFO "%s value is changed\n", ksym_name); 368c2ecf20Sopenharmony_ci dump_stack(); 378c2ecf20Sopenharmony_ci printk(KERN_INFO "Dump stack from sample_hbp_handler\n"); 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int __init hw_break_module_init(void) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci int ret; 438c2ecf20Sopenharmony_ci struct perf_event_attr attr; 448c2ecf20Sopenharmony_ci void *addr = __symbol_get(ksym_name); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci if (!addr) 478c2ecf20Sopenharmony_ci return -ENXIO; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci hw_breakpoint_init(&attr); 508c2ecf20Sopenharmony_ci attr.bp_addr = (unsigned long)addr; 518c2ecf20Sopenharmony_ci attr.bp_len = HW_BREAKPOINT_LEN_4; 528c2ecf20Sopenharmony_ci attr.bp_type = HW_BREAKPOINT_W; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL); 558c2ecf20Sopenharmony_ci if (IS_ERR((void __force *)sample_hbp)) { 568c2ecf20Sopenharmony_ci ret = PTR_ERR((void __force *)sample_hbp); 578c2ecf20Sopenharmony_ci goto fail; 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci return 0; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cifail: 658c2ecf20Sopenharmony_ci printk(KERN_INFO "Breakpoint registration failed\n"); 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci return ret; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic void __exit hw_break_module_exit(void) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci unregister_wide_hw_breakpoint(sample_hbp); 738c2ecf20Sopenharmony_ci#ifdef CONFIG_MODULE_UNLOAD 748c2ecf20Sopenharmony_ci __symbol_put(ksym_name); 758c2ecf20Sopenharmony_ci#endif 768c2ecf20Sopenharmony_ci printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name); 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cimodule_init(hw_break_module_init); 808c2ecf20Sopenharmony_cimodule_exit(hw_break_module_exit); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 838c2ecf20Sopenharmony_ciMODULE_AUTHOR("K.Prasad"); 848c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ksym breakpoint"); 85