]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/powerpc/kernel/kprobes.c
[PATCH] kprobes-changed-from-using-spinlock-to-mutex fix
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kernel / kprobes.c
1 /*
2 * Kernel Probes (KProbes)
3 * arch/ppc64/kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
27 * for PPC64
28 */
29
30 #include <linux/config.h>
31 #include <linux/kprobes.h>
32 #include <linux/ptrace.h>
33 #include <linux/preempt.h>
34 #include <asm/cacheflush.h>
35 #include <asm/kdebug.h>
36 #include <asm/sstep.h>
37
38 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
39 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
40
41 int __kprobes arch_prepare_kprobe(struct kprobe *p)
42 {
43 int ret = 0;
44 kprobe_opcode_t insn = *p->addr;
45
46 if ((unsigned long)p->addr & 0x03) {
47 printk("Attempt to register kprobe at an unaligned address\n");
48 ret = -EINVAL;
49 } else if (IS_MTMSRD(insn) || IS_RFID(insn)) {
50 printk("Cannot register a kprobe on rfid or mtmsrd\n");
51 ret = -EINVAL;
52 }
53
54 /* insn must be on a special executable page on ppc64 */
55 if (!ret) {
56 p->ainsn.insn = get_insn_slot();
57 if (!p->ainsn.insn)
58 ret = -ENOMEM;
59 }
60
61 if (!ret) {
62 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
63 p->opcode = *p->addr;
64 }
65
66 return ret;
67 }
68
69 void __kprobes arch_arm_kprobe(struct kprobe *p)
70 {
71 *p->addr = BREAKPOINT_INSTRUCTION;
72 flush_icache_range((unsigned long) p->addr,
73 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
74 }
75
76 void __kprobes arch_disarm_kprobe(struct kprobe *p)
77 {
78 *p->addr = p->opcode;
79 flush_icache_range((unsigned long) p->addr,
80 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
81 }
82
83 void __kprobes arch_remove_kprobe(struct kprobe *p)
84 {
85 free_insn_slot(p->ainsn.insn);
86 }
87
88 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
89 {
90 kprobe_opcode_t insn = *p->ainsn.insn;
91
92 regs->msr |= MSR_SE;
93
94 /* single step inline if it is a trap variant */
95 if (is_trap(insn))
96 regs->nip = (unsigned long)p->addr;
97 else
98 regs->nip = (unsigned long)p->ainsn.insn;
99 }
100
101 static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
102 {
103 kcb->prev_kprobe.kp = kprobe_running();
104 kcb->prev_kprobe.status = kcb->kprobe_status;
105 kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
106 }
107
108 static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
109 {
110 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
111 kcb->kprobe_status = kcb->prev_kprobe.status;
112 kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
113 }
114
115 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
116 struct kprobe_ctlblk *kcb)
117 {
118 __get_cpu_var(current_kprobe) = p;
119 kcb->kprobe_saved_msr = regs->msr;
120 }
121
122 /* Called with kretprobe_lock held */
123 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
124 struct pt_regs *regs)
125 {
126 struct kretprobe_instance *ri;
127
128 if ((ri = get_free_rp_inst(rp)) != NULL) {
129 ri->rp = rp;
130 ri->task = current;
131 ri->ret_addr = (kprobe_opcode_t *)regs->link;
132
133 /* Replace the return addr with trampoline addr */
134 regs->link = (unsigned long)kretprobe_trampoline;
135 add_rp_inst(ri);
136 } else {
137 rp->nmissed++;
138 }
139 }
140
141 static inline int kprobe_handler(struct pt_regs *regs)
142 {
143 struct kprobe *p;
144 int ret = 0;
145 unsigned int *addr = (unsigned int *)regs->nip;
146 struct kprobe_ctlblk *kcb;
147
148 /*
149 * We don't want to be preempted for the entire
150 * duration of kprobe processing
151 */
152 preempt_disable();
153 kcb = get_kprobe_ctlblk();
154
155 /* Check we're not actually recursing */
156 if (kprobe_running()) {
157 p = get_kprobe(addr);
158 if (p) {
159 kprobe_opcode_t insn = *p->ainsn.insn;
160 if (kcb->kprobe_status == KPROBE_HIT_SS &&
161 is_trap(insn)) {
162 regs->msr &= ~MSR_SE;
163 regs->msr |= kcb->kprobe_saved_msr;
164 goto no_kprobe;
165 }
166 /* We have reentered the kprobe_handler(), since
167 * another probe was hit while within the handler.
168 * We here save the original kprobes variables and
169 * just single step on the instruction of the new probe
170 * without calling any user handlers.
171 */
172 save_previous_kprobe(kcb);
173 set_current_kprobe(p, regs, kcb);
174 kcb->kprobe_saved_msr = regs->msr;
175 kprobes_inc_nmissed_count(p);
176 prepare_singlestep(p, regs);
177 kcb->kprobe_status = KPROBE_REENTER;
178 return 1;
179 } else {
180 p = __get_cpu_var(current_kprobe);
181 if (p->break_handler && p->break_handler(p, regs)) {
182 goto ss_probe;
183 }
184 }
185 goto no_kprobe;
186 }
187
188 p = get_kprobe(addr);
189 if (!p) {
190 if (*addr != BREAKPOINT_INSTRUCTION) {
191 /*
192 * PowerPC has multiple variants of the "trap"
193 * instruction. If the current instruction is a
194 * trap variant, it could belong to someone else
195 */
196 kprobe_opcode_t cur_insn = *addr;
197 if (is_trap(cur_insn))
198 goto no_kprobe;
199 /*
200 * The breakpoint instruction was removed right
201 * after we hit it. Another cpu has removed
202 * either a probepoint or a debugger breakpoint
203 * at this address. In either case, no further
204 * handling of this interrupt is appropriate.
205 */
206 ret = 1;
207 }
208 /* Not one of ours: let kernel handle it */
209 goto no_kprobe;
210 }
211
212 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
213 set_current_kprobe(p, regs, kcb);
214 if (p->pre_handler && p->pre_handler(p, regs))
215 /* handler has already set things up, so skip ss setup */
216 return 1;
217
218 ss_probe:
219 prepare_singlestep(p, regs);
220 kcb->kprobe_status = KPROBE_HIT_SS;
221 return 1;
222
223 no_kprobe:
224 preempt_enable_no_resched();
225 return ret;
226 }
227
228 /*
229 * Function return probe trampoline:
230 * - init_kprobes() establishes a probepoint here
231 * - When the probed function returns, this probe
232 * causes the handlers to fire
233 */
234 void kretprobe_trampoline_holder(void)
235 {
236 asm volatile(".global kretprobe_trampoline\n"
237 "kretprobe_trampoline:\n"
238 "nop\n");
239 }
240
241 /*
242 * Called when the probe at kretprobe trampoline is hit
243 */
244 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
245 {
246 struct kretprobe_instance *ri = NULL;
247 struct hlist_head *head;
248 struct hlist_node *node, *tmp;
249 unsigned long flags, orig_ret_address = 0;
250 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
251
252 spin_lock_irqsave(&kretprobe_lock, flags);
253 head = kretprobe_inst_table_head(current);
254
255 /*
256 * It is possible to have multiple instances associated with a given
257 * task either because an multiple functions in the call path
258 * have a return probe installed on them, and/or more then one return
259 * return probe was registered for a target function.
260 *
261 * We can handle this because:
262 * - instances are always inserted at the head of the list
263 * - when multiple return probes are registered for the same
264 * function, the first instance's ret_addr will point to the
265 * real return address, and all the rest will point to
266 * kretprobe_trampoline
267 */
268 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
269 if (ri->task != current)
270 /* another task is sharing our hash bucket */
271 continue;
272
273 if (ri->rp && ri->rp->handler)
274 ri->rp->handler(ri, regs);
275
276 orig_ret_address = (unsigned long)ri->ret_addr;
277 recycle_rp_inst(ri);
278
279 if (orig_ret_address != trampoline_address)
280 /*
281 * This is the real return address. Any other
282 * instances associated with this task are for
283 * other calls deeper on the call stack
284 */
285 break;
286 }
287
288 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
289 regs->nip = orig_ret_address;
290
291 reset_current_kprobe();
292 spin_unlock_irqrestore(&kretprobe_lock, flags);
293 preempt_enable_no_resched();
294
295 /*
296 * By returning a non-zero value, we are telling
297 * kprobe_handler() that we don't want the post_handler
298 * to run (and have re-enabled preemption)
299 */
300 return 1;
301 }
302
303 /*
304 * Called after single-stepping. p->addr is the address of the
305 * instruction whose first byte has been replaced by the "breakpoint"
306 * instruction. To avoid the SMP problems that can occur when we
307 * temporarily put back the original opcode to single-step, we
308 * single-stepped a copy of the instruction. The address of this
309 * copy is p->ainsn.insn.
310 */
311 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
312 {
313 int ret;
314 unsigned int insn = *p->ainsn.insn;
315
316 regs->nip = (unsigned long)p->addr;
317 ret = emulate_step(regs, insn);
318 if (ret == 0)
319 regs->nip = (unsigned long)p->addr + 4;
320 }
321
322 static inline int post_kprobe_handler(struct pt_regs *regs)
323 {
324 struct kprobe *cur = kprobe_running();
325 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
326
327 if (!cur)
328 return 0;
329
330 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
331 kcb->kprobe_status = KPROBE_HIT_SSDONE;
332 cur->post_handler(cur, regs, 0);
333 }
334
335 resume_execution(cur, regs);
336 regs->msr |= kcb->kprobe_saved_msr;
337
338 /*Restore back the original saved kprobes variables and continue. */
339 if (kcb->kprobe_status == KPROBE_REENTER) {
340 restore_previous_kprobe(kcb);
341 goto out;
342 }
343 reset_current_kprobe();
344 out:
345 preempt_enable_no_resched();
346
347 /*
348 * if somebody else is singlestepping across a probe point, msr
349 * will have SE set, in which case, continue the remaining processing
350 * of do_debug, as if this is not a probe hit.
351 */
352 if (regs->msr & MSR_SE)
353 return 0;
354
355 return 1;
356 }
357
358 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
359 {
360 struct kprobe *cur = kprobe_running();
361 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
362
363 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
364 return 1;
365
366 if (kcb->kprobe_status & KPROBE_HIT_SS) {
367 resume_execution(cur, regs);
368 regs->msr &= ~MSR_SE;
369 regs->msr |= kcb->kprobe_saved_msr;
370
371 reset_current_kprobe();
372 preempt_enable_no_resched();
373 }
374 return 0;
375 }
376
377 /*
378 * Wrapper routine to for handling exceptions.
379 */
380 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
381 unsigned long val, void *data)
382 {
383 struct die_args *args = (struct die_args *)data;
384 int ret = NOTIFY_DONE;
385
386 switch (val) {
387 case DIE_BPT:
388 if (kprobe_handler(args->regs))
389 ret = NOTIFY_STOP;
390 break;
391 case DIE_SSTEP:
392 if (post_kprobe_handler(args->regs))
393 ret = NOTIFY_STOP;
394 break;
395 case DIE_PAGE_FAULT:
396 /* kprobe_running() needs smp_processor_id() */
397 preempt_disable();
398 if (kprobe_running() &&
399 kprobe_fault_handler(args->regs, args->trapnr))
400 ret = NOTIFY_STOP;
401 preempt_enable();
402 break;
403 default:
404 break;
405 }
406 return ret;
407 }
408
409 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
410 {
411 struct jprobe *jp = container_of(p, struct jprobe, kp);
412 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
413
414 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
415
416 /* setup return addr to the jprobe handler routine */
417 regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
418 regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
419
420 return 1;
421 }
422
423 void __kprobes jprobe_return(void)
424 {
425 asm volatile("trap" ::: "memory");
426 }
427
428 void __kprobes jprobe_return_end(void)
429 {
430 };
431
432 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
433 {
434 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
435
436 /*
437 * FIXME - we should ideally be validating that we got here 'cos
438 * of the "trap" in jprobe_return() above, before restoring the
439 * saved regs...
440 */
441 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
442 preempt_enable_no_resched();
443 return 1;
444 }
445
446 static struct kprobe trampoline_p = {
447 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
448 .pre_handler = trampoline_probe_handler
449 };
450
451 int __init arch_init_kprobes(void)
452 {
453 return register_kprobe(&trampoline_p);
454 }