xref: /linux/include/linux/hw_breakpoint.h (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HW_BREAKPOINT_H
3 #define _LINUX_HW_BREAKPOINT_H
4 
5 #include <linux/perf_event.h>
6 #include <uapi/linux/hw_breakpoint.h>
7 
8 #ifdef CONFIG_HAVE_HW_BREAKPOINT
9 
10 enum bp_type_idx {
11 	TYPE_INST	= 0,
12 #if defined(CONFIG_HAVE_MIXED_BREAKPOINTS_REGS)
13 	TYPE_DATA	= 0,
14 #else
15 	TYPE_DATA	= 1,
16 #endif
17 	TYPE_MAX
18 };
19 
20 extern int __init init_hw_breakpoint(void);
21 
hw_breakpoint_init(struct perf_event_attr * attr)22 static inline void hw_breakpoint_init(struct perf_event_attr *attr)
23 {
24 	memset(attr, 0, sizeof(*attr));
25 
26 	attr->type = PERF_TYPE_BREAKPOINT;
27 	attr->size = sizeof(*attr);
28 	/*
29 	 * As it's for in-kernel or ptrace use, we want it to be pinned
30 	 * and to call its callback every hits.
31 	 */
32 	attr->pinned = 1;
33 	attr->sample_period = 1;
34 }
35 
ptrace_breakpoint_init(struct perf_event_attr * attr)36 static inline void ptrace_breakpoint_init(struct perf_event_attr *attr)
37 {
38 	hw_breakpoint_init(attr);
39 	attr->exclude_kernel = 1;
40 }
41 
hw_breakpoint_addr(struct perf_event * bp)42 static inline unsigned long hw_breakpoint_addr(struct perf_event *bp)
43 {
44 	return bp->attr.bp_addr;
45 }
46 
hw_breakpoint_type(struct perf_event * bp)47 static inline int hw_breakpoint_type(struct perf_event *bp)
48 {
49 	return bp->attr.bp_type;
50 }
51 
hw_breakpoint_len(struct perf_event * bp)52 static inline unsigned long hw_breakpoint_len(struct perf_event *bp)
53 {
54 	return bp->attr.bp_len;
55 }
56 
57 extern struct perf_event *
58 register_user_hw_breakpoint(struct perf_event_attr *attr,
59 			    perf_overflow_handler_t triggered,
60 			    void *context,
61 			    struct task_struct *tsk);
62 
63 /* FIXME: only change from the attr, and don't unregister */
64 extern int
65 modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr);
66 extern int
67 modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr,
68 				bool check);
69 
70 /*
71  * Kernel breakpoints are not associated with any particular thread.
72  */
73 extern struct perf_event *
74 register_wide_hw_breakpoint_cpu(struct perf_event_attr *attr,
75 				perf_overflow_handler_t	triggered,
76 				void *context,
77 				int cpu);
78 
79 extern struct perf_event * __percpu *
80 register_wide_hw_breakpoint(struct perf_event_attr *attr,
81 			    perf_overflow_handler_t triggered,
82 			    void *context);
83 
84 extern int register_perf_hw_breakpoint(struct perf_event *bp);
85 extern void unregister_hw_breakpoint(struct perf_event *bp);
86 extern void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events);
87 extern bool hw_breakpoint_is_used(void);
88 
89 extern int dbg_reserve_bp_slot(struct perf_event *bp);
90 extern int dbg_release_bp_slot(struct perf_event *bp);
91 extern int reserve_bp_slot(struct perf_event *bp);
92 extern void release_bp_slot(struct perf_event *bp);
93 
94 extern void flush_ptrace_hw_breakpoint(struct task_struct *tsk);
95 
counter_arch_bp(struct perf_event * bp)96 static inline struct arch_hw_breakpoint *counter_arch_bp(struct perf_event *bp)
97 {
98 	return &bp->hw.info;
99 }
100 
101 #else /* !CONFIG_HAVE_HW_BREAKPOINT */
102 
init_hw_breakpoint(void)103 static inline int __init init_hw_breakpoint(void) { return 0; }
104 
105 static inline struct perf_event *
register_user_hw_breakpoint(struct perf_event_attr * attr,perf_overflow_handler_t triggered,void * context,struct task_struct * tsk)106 register_user_hw_breakpoint(struct perf_event_attr *attr,
107 			    perf_overflow_handler_t triggered,
108 			    void *context,
109 			    struct task_struct *tsk)	{ return NULL; }
110 static inline int
modify_user_hw_breakpoint(struct perf_event * bp,struct perf_event_attr * attr)111 modify_user_hw_breakpoint(struct perf_event *bp,
112 			  struct perf_event_attr *attr)	{ return -ENOSYS; }
113 static inline int
modify_user_hw_breakpoint_check(struct perf_event * bp,struct perf_event_attr * attr,bool check)114 modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr,
115 				bool check)	{ return -ENOSYS; }
116 
117 static inline struct perf_event *
register_wide_hw_breakpoint_cpu(struct perf_event_attr * attr,perf_overflow_handler_t triggered,void * context,int cpu)118 register_wide_hw_breakpoint_cpu(struct perf_event_attr *attr,
119 				perf_overflow_handler_t	 triggered,
120 				void *context,
121 				int cpu)		{ return NULL; }
122 static inline struct perf_event * __percpu *
register_wide_hw_breakpoint(struct perf_event_attr * attr,perf_overflow_handler_t triggered,void * context)123 register_wide_hw_breakpoint(struct perf_event_attr *attr,
124 			    perf_overflow_handler_t triggered,
125 			    void *context)		{ return NULL; }
126 static inline int
register_perf_hw_breakpoint(struct perf_event * bp)127 register_perf_hw_breakpoint(struct perf_event *bp)	{ return -ENOSYS; }
unregister_hw_breakpoint(struct perf_event * bp)128 static inline void unregister_hw_breakpoint(struct perf_event *bp)	{ }
129 static inline void
unregister_wide_hw_breakpoint(struct perf_event * __percpu * cpu_events)130 unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)	{ }
hw_breakpoint_is_used(void)131 static inline bool hw_breakpoint_is_used(void)		{ return false; }
132 
133 static inline int
reserve_bp_slot(struct perf_event * bp)134 reserve_bp_slot(struct perf_event *bp)			{return -ENOSYS; }
release_bp_slot(struct perf_event * bp)135 static inline void release_bp_slot(struct perf_event *bp) 		{ }
136 
flush_ptrace_hw_breakpoint(struct task_struct * tsk)137 static inline void flush_ptrace_hw_breakpoint(struct task_struct *tsk)	{ }
138 
counter_arch_bp(struct perf_event * bp)139 static inline struct arch_hw_breakpoint *counter_arch_bp(struct perf_event *bp)
140 {
141 	return NULL;
142 }
143 
144 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
145 #endif /* _LINUX_HW_BREAKPOINT_H */
146