2 * Xtensa hardware breakpoints/watchpoints handling functions
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2016 Cadence Design Systems Inc.
11 #include <linux/hw_breakpoint.h>
12 #include <linux/log2.h>
13 #include <linux/percpu.h>
14 #include <linux/perf_event.h>
17 /* Breakpoint currently in use for each IBREAKA. */
18 static DEFINE_PER_CPU(struct perf_event
*, bp_on_reg
[XCHAL_NUM_IBREAK
]);
20 /* Watchpoint currently in use for each DBREAKA. */
21 static DEFINE_PER_CPU(struct perf_event
*, wp_on_reg
[XCHAL_NUM_DBREAK
]);
23 int hw_breakpoint_slots(int type
)
27 return XCHAL_NUM_IBREAK
;
29 return XCHAL_NUM_DBREAK
;
31 pr_warn("unknown slot type: %d\n", type
);
36 int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint
*hw
)
44 return (va
>= TASK_SIZE
) && ((va
+ len
- 1) >= TASK_SIZE
);
48 * Construct an arch_hw_breakpoint from a perf_event.
50 int hw_breakpoint_arch_parse(struct perf_event
*bp
,
51 const struct perf_event_attr
*attr
,
52 struct arch_hw_breakpoint
*hw
)
55 switch (attr
->bp_type
) {
57 hw
->type
= XTENSA_BREAKPOINT_EXECUTE
;
60 hw
->type
= XTENSA_BREAKPOINT_LOAD
;
63 hw
->type
= XTENSA_BREAKPOINT_STORE
;
65 case HW_BREAKPOINT_RW
:
66 hw
->type
= XTENSA_BREAKPOINT_LOAD
| XTENSA_BREAKPOINT_STORE
;
73 hw
->len
= attr
->bp_len
;
74 if (hw
->len
< 1 || hw
->len
> 64 || !is_power_of_2(hw
->len
))
78 hw
->address
= attr
->bp_addr
;
79 if (hw
->address
& (hw
->len
- 1))
85 int hw_breakpoint_exceptions_notify(struct notifier_block
*unused
,
86 unsigned long val
, void *data
)
91 static void xtensa_wsr(unsigned long v
, u8 sr
)
93 /* We don't have indexed wsr and creating instruction dynamically
94 * doesn't seem worth it given how small XCHAL_NUM_IBREAK and
95 * XCHAL_NUM_DBREAK are. Thus the switch. In case build breaks here
96 * the switch below needs to be extended.
98 BUILD_BUG_ON(XCHAL_NUM_IBREAK
> 2);
99 BUILD_BUG_ON(XCHAL_NUM_DBREAK
> 2);
102 #if XCHAL_NUM_IBREAK > 0
103 case SREG_IBREAKA
+ 0:
104 xtensa_set_sr(v
, SREG_IBREAKA
+ 0);
107 #if XCHAL_NUM_IBREAK > 1
108 case SREG_IBREAKA
+ 1:
109 xtensa_set_sr(v
, SREG_IBREAKA
+ 1);
113 #if XCHAL_NUM_DBREAK > 0
114 case SREG_DBREAKA
+ 0:
115 xtensa_set_sr(v
, SREG_DBREAKA
+ 0);
117 case SREG_DBREAKC
+ 0:
118 xtensa_set_sr(v
, SREG_DBREAKC
+ 0);
121 #if XCHAL_NUM_DBREAK > 1
122 case SREG_DBREAKA
+ 1:
123 xtensa_set_sr(v
, SREG_DBREAKA
+ 1);
126 case SREG_DBREAKC
+ 1:
127 xtensa_set_sr(v
, SREG_DBREAKC
+ 1);
133 static int alloc_slot(struct perf_event
**slot
, size_t n
,
134 struct perf_event
*bp
)
138 for (i
= 0; i
< n
; ++i
) {
147 static void set_ibreak_regs(int reg
, struct perf_event
*bp
)
149 struct arch_hw_breakpoint
*info
= counter_arch_bp(bp
);
150 unsigned long ibreakenable
;
152 xtensa_wsr(info
->address
, SREG_IBREAKA
+ reg
);
153 ibreakenable
= xtensa_get_sr(SREG_IBREAKENABLE
);
154 xtensa_set_sr(ibreakenable
| (1 << reg
), SREG_IBREAKENABLE
);
157 static void set_dbreak_regs(int reg
, struct perf_event
*bp
)
159 struct arch_hw_breakpoint
*info
= counter_arch_bp(bp
);
160 unsigned long dbreakc
= DBREAKC_MASK_MASK
& -info
->len
;
162 if (info
->type
& XTENSA_BREAKPOINT_LOAD
)
163 dbreakc
|= DBREAKC_LOAD_MASK
;
164 if (info
->type
& XTENSA_BREAKPOINT_STORE
)
165 dbreakc
|= DBREAKC_STOR_MASK
;
167 xtensa_wsr(info
->address
, SREG_DBREAKA
+ reg
);
168 xtensa_wsr(dbreakc
, SREG_DBREAKC
+ reg
);
171 int arch_install_hw_breakpoint(struct perf_event
*bp
)
175 if (counter_arch_bp(bp
)->type
== XTENSA_BREAKPOINT_EXECUTE
) {
177 i
= alloc_slot(this_cpu_ptr(bp_on_reg
), XCHAL_NUM_IBREAK
, bp
);
180 set_ibreak_regs(i
, bp
);
184 i
= alloc_slot(this_cpu_ptr(wp_on_reg
), XCHAL_NUM_DBREAK
, bp
);
187 set_dbreak_regs(i
, bp
);
192 static int free_slot(struct perf_event
**slot
, size_t n
,
193 struct perf_event
*bp
)
197 for (i
= 0; i
< n
; ++i
) {
206 void arch_uninstall_hw_breakpoint(struct perf_event
*bp
)
208 struct arch_hw_breakpoint
*info
= counter_arch_bp(bp
);
211 if (info
->type
== XTENSA_BREAKPOINT_EXECUTE
) {
212 unsigned long ibreakenable
;
215 i
= free_slot(this_cpu_ptr(bp_on_reg
), XCHAL_NUM_IBREAK
, bp
);
217 ibreakenable
= xtensa_get_sr(SREG_IBREAKENABLE
);
218 xtensa_set_sr(ibreakenable
& ~(1 << i
),
223 i
= free_slot(this_cpu_ptr(wp_on_reg
), XCHAL_NUM_DBREAK
, bp
);
225 xtensa_wsr(0, SREG_DBREAKC
+ i
);
229 void hw_breakpoint_pmu_read(struct perf_event
*bp
)
233 void flush_ptrace_hw_breakpoint(struct task_struct
*tsk
)
236 struct thread_struct
*t
= &tsk
->thread
;
238 for (i
= 0; i
< XCHAL_NUM_IBREAK
; ++i
) {
239 if (t
->ptrace_bp
[i
]) {
240 unregister_hw_breakpoint(t
->ptrace_bp
[i
]);
241 t
->ptrace_bp
[i
] = NULL
;
244 for (i
= 0; i
< XCHAL_NUM_DBREAK
; ++i
) {
245 if (t
->ptrace_wp
[i
]) {
246 unregister_hw_breakpoint(t
->ptrace_wp
[i
]);
247 t
->ptrace_wp
[i
] = NULL
;
253 * Set ptrace breakpoint pointers to zero for this task.
254 * This is required in order to prevent child processes from unregistering
255 * breakpoints held by their parent.
257 void clear_ptrace_hw_breakpoint(struct task_struct
*tsk
)
259 memset(tsk
->thread
.ptrace_bp
, 0, sizeof(tsk
->thread
.ptrace_bp
));
260 memset(tsk
->thread
.ptrace_wp
, 0, sizeof(tsk
->thread
.ptrace_wp
));
263 void restore_dbreak(void)
267 for (i
= 0; i
< XCHAL_NUM_DBREAK
; ++i
) {
268 struct perf_event
*bp
= this_cpu_ptr(wp_on_reg
)[i
];
271 set_dbreak_regs(i
, bp
);
273 clear_thread_flag(TIF_DB_DISABLED
);
276 int check_hw_breakpoint(struct pt_regs
*regs
)
278 if (regs
->debugcause
& BIT(DEBUGCAUSE_IBREAK_BIT
)) {
280 struct perf_event
**bp
= this_cpu_ptr(bp_on_reg
);
282 for (i
= 0; i
< XCHAL_NUM_IBREAK
; ++i
) {
283 if (bp
[i
] && !bp
[i
]->attr
.disabled
&&
284 regs
->pc
== bp
[i
]->attr
.bp_addr
)
285 perf_bp_event(bp
[i
], regs
);
288 } else if (regs
->debugcause
& BIT(DEBUGCAUSE_DBREAK_BIT
)) {
289 struct perf_event
**bp
= this_cpu_ptr(wp_on_reg
);
290 int dbnum
= (regs
->debugcause
& DEBUGCAUSE_DBNUM_MASK
) >>
291 DEBUGCAUSE_DBNUM_SHIFT
;
293 if (dbnum
< XCHAL_NUM_DBREAK
&& bp
[dbnum
]) {
294 if (user_mode(regs
)) {
295 perf_bp_event(bp
[dbnum
], regs
);
297 set_thread_flag(TIF_DB_DISABLED
);
298 xtensa_wsr(0, SREG_DBREAKC
+ dbnum
);
302 "Wrong/unconfigured DBNUM reported in DEBUGCAUSE: %d\n",