WIP FPC-III support
[linux/fpc-iii.git] / arch / xtensa / kernel / hw_breakpoint.c
blob285fb2942b06593a585e154f32fa67d920afc295
1 /*
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
6 * for more details.
8 * Copyright (C) 2016 Cadence Design Systems Inc.
9 */
11 #include <linux/hw_breakpoint.h>
12 #include <linux/log2.h>
13 #include <linux/percpu.h>
14 #include <linux/perf_event.h>
15 #include <asm/core.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)
25 switch (type) {
26 case TYPE_INST:
27 return XCHAL_NUM_IBREAK;
28 case TYPE_DATA:
29 return XCHAL_NUM_DBREAK;
30 default:
31 pr_warn("unknown slot type: %d\n", type);
32 return 0;
36 int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
38 unsigned int len;
39 unsigned long va;
41 va = hw->address;
42 len = hw->len;
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)
54 /* Type */
55 switch (attr->bp_type) {
56 case HW_BREAKPOINT_X:
57 hw->type = XTENSA_BREAKPOINT_EXECUTE;
58 break;
59 case HW_BREAKPOINT_R:
60 hw->type = XTENSA_BREAKPOINT_LOAD;
61 break;
62 case HW_BREAKPOINT_W:
63 hw->type = XTENSA_BREAKPOINT_STORE;
64 break;
65 case HW_BREAKPOINT_RW:
66 hw->type = XTENSA_BREAKPOINT_LOAD | XTENSA_BREAKPOINT_STORE;
67 break;
68 default:
69 return -EINVAL;
72 /* Len */
73 hw->len = attr->bp_len;
74 if (hw->len < 1 || hw->len > 64 || !is_power_of_2(hw->len))
75 return -EINVAL;
77 /* Address */
78 hw->address = attr->bp_addr;
79 if (hw->address & (hw->len - 1))
80 return -EINVAL;
82 return 0;
85 int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
86 unsigned long val, void *data)
88 return NOTIFY_DONE;
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);
101 switch (sr) {
102 #if XCHAL_NUM_IBREAK > 0
103 case SREG_IBREAKA + 0:
104 xtensa_set_sr(v, SREG_IBREAKA + 0);
105 break;
106 #endif
107 #if XCHAL_NUM_IBREAK > 1
108 case SREG_IBREAKA + 1:
109 xtensa_set_sr(v, SREG_IBREAKA + 1);
110 break;
111 #endif
113 #if XCHAL_NUM_DBREAK > 0
114 case SREG_DBREAKA + 0:
115 xtensa_set_sr(v, SREG_DBREAKA + 0);
116 break;
117 case SREG_DBREAKC + 0:
118 xtensa_set_sr(v, SREG_DBREAKC + 0);
119 break;
120 #endif
121 #if XCHAL_NUM_DBREAK > 1
122 case SREG_DBREAKA + 1:
123 xtensa_set_sr(v, SREG_DBREAKA + 1);
124 break;
126 case SREG_DBREAKC + 1:
127 xtensa_set_sr(v, SREG_DBREAKC + 1);
128 break;
129 #endif
133 static int alloc_slot(struct perf_event **slot, size_t n,
134 struct perf_event *bp)
136 size_t i;
138 for (i = 0; i < n; ++i) {
139 if (!slot[i]) {
140 slot[i] = bp;
141 return i;
144 return -EBUSY;
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)
173 int i;
175 if (counter_arch_bp(bp)->type == XTENSA_BREAKPOINT_EXECUTE) {
176 /* Breakpoint */
177 i = alloc_slot(this_cpu_ptr(bp_on_reg), XCHAL_NUM_IBREAK, bp);
178 if (i < 0)
179 return i;
180 set_ibreak_regs(i, bp);
182 } else {
183 /* Watchpoint */
184 i = alloc_slot(this_cpu_ptr(wp_on_reg), XCHAL_NUM_DBREAK, bp);
185 if (i < 0)
186 return i;
187 set_dbreak_regs(i, bp);
189 return 0;
192 static int free_slot(struct perf_event **slot, size_t n,
193 struct perf_event *bp)
195 size_t i;
197 for (i = 0; i < n; ++i) {
198 if (slot[i] == bp) {
199 slot[i] = NULL;
200 return i;
203 return -EBUSY;
206 void arch_uninstall_hw_breakpoint(struct perf_event *bp)
208 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
209 int i;
211 if (info->type == XTENSA_BREAKPOINT_EXECUTE) {
212 unsigned long ibreakenable;
214 /* Breakpoint */
215 i = free_slot(this_cpu_ptr(bp_on_reg), XCHAL_NUM_IBREAK, bp);
216 if (i >= 0) {
217 ibreakenable = xtensa_get_sr(SREG_IBREAKENABLE);
218 xtensa_set_sr(ibreakenable & ~(1 << i),
219 SREG_IBREAKENABLE);
221 } else {
222 /* Watchpoint */
223 i = free_slot(this_cpu_ptr(wp_on_reg), XCHAL_NUM_DBREAK, bp);
224 if (i >= 0)
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)
235 int i;
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)
265 int i;
267 for (i = 0; i < XCHAL_NUM_DBREAK; ++i) {
268 struct perf_event *bp = this_cpu_ptr(wp_on_reg)[i];
270 if (bp)
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)) {
279 int i;
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);
287 return 0;
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);
296 } else {
297 set_thread_flag(TIF_DB_DISABLED);
298 xtensa_wsr(0, SREG_DBREAKC + dbnum);
300 } else {
301 WARN_ONCE(1,
302 "Wrong/unconfigured DBNUM reported in DEBUGCAUSE: %d\n",
303 dbnum);
305 return 0;
307 return -ENOENT;