GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / sh / kernel / hw_breakpoint.c
1 /*
2  * arch/sh/kernel/hw_breakpoint.c
3  *
4  * Unified kernel/user-space hardware breakpoint facility for the on-chip UBC.
5  *
6  * Copyright (C) 2009 - 2010  Paul Mundt
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file "COPYING" in the main directory of this archive
10  * for more details.
11  */
12 #include <linux/init.h>
13 #include <linux/perf_event.h>
14 #include <linux/sched/signal.h>
15 #include <linux/hw_breakpoint.h>
16 #include <linux/percpu.h>
17 #include <linux/kallsyms.h>
18 #include <linux/notifier.h>
19 #include <linux/kprobes.h>
20 #include <linux/kdebug.h>
21 #include <linux/io.h>
22 #include <linux/clk.h>
23 #include <asm/hw_breakpoint.h>
24 #include <asm/mmu_context.h>
25 #include <asm/ptrace.h>
26 #include <asm/traps.h>
27
28 /*
29  * Stores the breakpoints currently in use on each breakpoint address
30  * register for each cpus
31  */
32 static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]);
33
34 /*
35  * A dummy placeholder for early accesses until the CPUs get a chance to
36  * register their UBCs later in the boot process.
37  */
38 static struct sh_ubc ubc_dummy = { .num_events = 0 };
39
40 static struct sh_ubc *sh_ubc __read_mostly = &ubc_dummy;
41
42 /*
43  * Install a perf counter breakpoint.
44  *
45  * We seek a free UBC channel and use it for this breakpoint.
46  *
47  * Atomic: we hold the counter->ctx->lock and we only handle variables
48  * and registers local to this cpu.
49  */
50 int arch_install_hw_breakpoint(struct perf_event *bp)
51 {
52         struct arch_hw_breakpoint *info = counter_arch_bp(bp);
53         int i;
54
55         for (i = 0; i < sh_ubc->num_events; i++) {
56                 struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]);
57
58                 if (!*slot) {
59                         *slot = bp;
60                         break;
61                 }
62         }
63
64         if (WARN_ONCE(i == sh_ubc->num_events, "Can't find any breakpoint slot"))
65                 return -EBUSY;
66
67         clk_enable(sh_ubc->clk);
68         sh_ubc->enable(info, i);
69
70         return 0;
71 }
72
73 /*
74  * Uninstall the breakpoint contained in the given counter.
75  *
76  * First we search the debug address register it uses and then we disable
77  * it.
78  *
79  * Atomic: we hold the counter->ctx->lock and we only handle variables
80  * and registers local to this cpu.
81  */
82 void arch_uninstall_hw_breakpoint(struct perf_event *bp)
83 {
84         struct arch_hw_breakpoint *info = counter_arch_bp(bp);
85         int i;
86
87         for (i = 0; i < sh_ubc->num_events; i++) {
88                 struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]);
89
90                 if (*slot == bp) {
91                         *slot = NULL;
92                         break;
93                 }
94         }
95
96         if (WARN_ONCE(i == sh_ubc->num_events, "Can't find any breakpoint slot"))
97                 return;
98
99         sh_ubc->disable(info, i);
100         clk_disable(sh_ubc->clk);
101 }
102
103 static int get_hbp_len(u16 hbp_len)
104 {
105         unsigned int len_in_bytes = 0;
106
107         switch (hbp_len) {
108         case SH_BREAKPOINT_LEN_1:
109                 len_in_bytes = 1;
110                 break;
111         case SH_BREAKPOINT_LEN_2:
112                 len_in_bytes = 2;
113                 break;
114         case SH_BREAKPOINT_LEN_4:
115                 len_in_bytes = 4;
116                 break;
117         case SH_BREAKPOINT_LEN_8:
118                 len_in_bytes = 8;
119                 break;
120         }
121         return len_in_bytes;
122 }
123
124 /*
125  * Check for virtual address in kernel space.
126  */
127 int arch_check_bp_in_kernelspace(struct perf_event *bp)
128 {
129         unsigned int len;
130         unsigned long va;
131         struct arch_hw_breakpoint *info = counter_arch_bp(bp);
132
133         va = info->address;
134         len = get_hbp_len(info->len);
135
136         return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
137 }
138
139 int arch_bp_generic_fields(int sh_len, int sh_type,
140                            int *gen_len, int *gen_type)
141 {
142         /* Len */
143         switch (sh_len) {
144         case SH_BREAKPOINT_LEN_1:
145                 *gen_len = HW_BREAKPOINT_LEN_1;
146                 break;
147         case SH_BREAKPOINT_LEN_2:
148                 *gen_len = HW_BREAKPOINT_LEN_2;
149                 break;
150         case SH_BREAKPOINT_LEN_4:
151                 *gen_len = HW_BREAKPOINT_LEN_4;
152                 break;
153         case SH_BREAKPOINT_LEN_8:
154                 *gen_len = HW_BREAKPOINT_LEN_8;
155                 break;
156         default:
157                 return -EINVAL;
158         }
159
160         /* Type */
161         switch (sh_type) {
162         case SH_BREAKPOINT_READ:
163                 *gen_type = HW_BREAKPOINT_R;
164                 break;
165         case SH_BREAKPOINT_WRITE:
166                 *gen_type = HW_BREAKPOINT_W;
167                 break;
168         case SH_BREAKPOINT_RW:
169                 *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
170                 break;
171         default:
172                 return -EINVAL;
173         }
174
175         return 0;
176 }
177
178 static int arch_build_bp_info(struct perf_event *bp)
179 {
180         struct arch_hw_breakpoint *info = counter_arch_bp(bp);
181
182         info->address = bp->attr.bp_addr;
183
184         /* Len */
185         switch (bp->attr.bp_len) {
186         case HW_BREAKPOINT_LEN_1:
187                 info->len = SH_BREAKPOINT_LEN_1;
188                 break;
189         case HW_BREAKPOINT_LEN_2:
190                 info->len = SH_BREAKPOINT_LEN_2;
191                 break;
192         case HW_BREAKPOINT_LEN_4:
193                 info->len = SH_BREAKPOINT_LEN_4;
194                 break;
195         case HW_BREAKPOINT_LEN_8:
196                 info->len = SH_BREAKPOINT_LEN_8;
197                 break;
198         default:
199                 return -EINVAL;
200         }
201
202         /* Type */
203         switch (bp->attr.bp_type) {
204         case HW_BREAKPOINT_R:
205                 info->type = SH_BREAKPOINT_READ;
206                 break;
207         case HW_BREAKPOINT_W:
208                 info->type = SH_BREAKPOINT_WRITE;
209                 break;
210         case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
211                 info->type = SH_BREAKPOINT_RW;
212                 break;
213         default:
214                 return -EINVAL;
215         }
216
217         return 0;
218 }
219
220 /*
221  * Validate the arch-specific HW Breakpoint register settings
222  */
223 int arch_validate_hwbkpt_settings(struct perf_event *bp)
224 {
225         struct arch_hw_breakpoint *info = counter_arch_bp(bp);
226         unsigned int align;
227         int ret;
228
229         ret = arch_build_bp_info(bp);
230         if (ret)
231                 return ret;
232
233         ret = -EINVAL;
234
235         switch (info->len) {
236         case SH_BREAKPOINT_LEN_1:
237                 align = 0;
238                 break;
239         case SH_BREAKPOINT_LEN_2:
240                 align = 1;
241                 break;
242         case SH_BREAKPOINT_LEN_4:
243                 align = 3;
244                 break;
245         case SH_BREAKPOINT_LEN_8:
246                 align = 7;
247                 break;
248         default:
249                 return ret;
250         }
251
252         /*
253          * For kernel-addresses, either the address or symbol name can be
254          * specified.
255          */
256         if (info->name)
257                 info->address = (unsigned long)kallsyms_lookup_name(info->name);
258
259         /*
260          * Check that the low-order bits of the address are appropriate
261          * for the alignment implied by len.
262          */
263         if (info->address & align)
264                 return -EINVAL;
265
266         return 0;
267 }
268
269 /*
270  * Release the user breakpoints used by ptrace
271  */
272 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
273 {
274         int i;
275         struct thread_struct *t = &tsk->thread;
276
277         for (i = 0; i < sh_ubc->num_events; i++) {
278                 unregister_hw_breakpoint(t->ptrace_bps[i]);
279                 t->ptrace_bps[i] = NULL;
280         }
281 }
282
283 static int __kprobes hw_breakpoint_handler(struct die_args *args)
284 {
285         int cpu, i, rc = NOTIFY_STOP;
286         struct perf_event *bp;
287         unsigned int cmf, resume_mask;
288
289         /*
290          * Do an early return if none of the channels triggered.
291          */
292         cmf = sh_ubc->triggered_mask();
293         if (unlikely(!cmf))
294                 return NOTIFY_DONE;
295
296         /*
297          * By default, resume all of the active channels.
298          */
299         resume_mask = sh_ubc->active_mask();
300
301         /*
302          * Disable breakpoints during exception handling.
303          */
304         sh_ubc->disable_all();
305
306         cpu = get_cpu();
307         for (i = 0; i < sh_ubc->num_events; i++) {
308                 unsigned long event_mask = (1 << i);
309
310                 if (likely(!(cmf & event_mask)))
311                         continue;
312
313                 /*
314                  * The counter may be concurrently released but that can only
315                  * occur from a call_rcu() path. We can then safely fetch
316                  * the breakpoint, use its callback, touch its counter
317                  * while we are in an rcu_read_lock() path.
318                  */
319                 rcu_read_lock();
320
321                 bp = per_cpu(bp_per_reg[i], cpu);
322                 if (bp)
323                         rc = NOTIFY_DONE;
324
325                 /*
326                  * Reset the condition match flag to denote completion of
327                  * exception handling.
328                  */
329                 sh_ubc->clear_triggered_mask(event_mask);
330
331                 /*
332                  * bp can be NULL due to concurrent perf counter
333                  * removing.
334                  */
335                 if (!bp) {
336                         rcu_read_unlock();
337                         break;
338                 }
339
340                 /*
341                  * Don't restore the channel if the breakpoint is from
342                  * ptrace, as it always operates in one-shot mode.
343                  */
344                 if (bp->overflow_handler == ptrace_triggered)
345                         resume_mask &= ~(1 << i);
346
347                 perf_bp_event(bp, args->regs);
348
349                 /* Deliver the signal to userspace */
350                 if (!arch_check_bp_in_kernelspace(bp)) {
351                         siginfo_t info;
352
353                         info.si_signo = args->signr;
354                         info.si_errno = notifier_to_errno(rc);
355                         info.si_code = TRAP_HWBKPT;
356
357                         force_sig_info(args->signr, &info, current);
358                 }
359
360                 rcu_read_unlock();
361         }
362
363         if (cmf == 0)
364                 rc = NOTIFY_DONE;
365
366         sh_ubc->enable_all(resume_mask);
367
368         put_cpu();
369
370         return rc;
371 }
372
373 BUILD_TRAP_HANDLER(breakpoint)
374 {
375         unsigned long ex = lookup_exception_vector();
376         TRAP_HANDLER_DECL;
377
378         notify_die(DIE_BREAKPOINT, "breakpoint", regs, 0, ex, SIGTRAP);
379 }
380
381 /*
382  * Handle debug exception notifications.
383  */
384 int __kprobes hw_breakpoint_exceptions_notify(struct notifier_block *unused,
385                                     unsigned long val, void *data)
386 {
387         struct die_args *args = data;
388
389         if (val != DIE_BREAKPOINT)
390                 return NOTIFY_DONE;
391
392         /*
393          * If the breakpoint hasn't been triggered by the UBC, it's
394          * probably from a debugger, so don't do anything more here.
395          *
396          * This also permits the UBC interface clock to remain off for
397          * non-UBC breakpoints, as we don't need to check the triggered
398          * or active channel masks.
399          */
400         if (args->trapnr != sh_ubc->trap_nr)
401                 return NOTIFY_DONE;
402
403         return hw_breakpoint_handler(data);
404 }
405
406 void hw_breakpoint_pmu_read(struct perf_event *bp)
407 {
408         /* TODO */
409 }
410
411 int register_sh_ubc(struct sh_ubc *ubc)
412 {
413         /* Bail if it's already assigned */
414         if (sh_ubc != &ubc_dummy)
415                 return -EBUSY;
416         sh_ubc = ubc;
417
418         pr_info("HW Breakpoints: %s UBC support registered\n", ubc->name);
419
420         WARN_ON(ubc->num_events > HBP_NUM);
421
422         return 0;
423 }