xref: /linux/arch/arm/probes/kprobes/core.c (revision fa84cf094ef9667e2b91c104b0a788fd1896f482)
1 /*
2  * arch/arm/kernel/kprobes.c
3  *
4  * Kprobes on ARM
5  *
6  * Abhishek Sagar <sagar.abhishek@gmail.com>
7  * Copyright (C) 2006, 2007 Motorola Inc.
8  *
9  * Nicolas Pitre <nico@marvell.com>
10  * Copyright (C) 2007 Marvell Ltd.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21 
22 #include <linux/kernel.h>
23 #include <linux/kprobes.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/stop_machine.h>
27 #include <linux/sched/debug.h>
28 #include <linux/stringify.h>
29 #include <asm/traps.h>
30 #include <asm/opcodes.h>
31 #include <asm/cacheflush.h>
32 #include <linux/percpu.h>
33 #include <linux/bug.h>
34 #include <asm/patch.h>
35 #include <asm/sections.h>
36 
37 #include "../decode-arm.h"
38 #include "../decode-thumb.h"
39 #include "core.h"
40 
41 #define MIN_STACK_SIZE(addr) 				\
42 	min((unsigned long)MAX_STACK_SIZE,		\
43 	    (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
44 
45 #define flush_insns(addr, size)				\
46 	flush_icache_range((unsigned long)(addr),	\
47 			   (unsigned long)(addr) +	\
48 			   (size))
49 
50 /* Used as a marker in ARM_pc to note when we're in a jprobe. */
51 #define JPROBE_MAGIC_ADDR		0xffffffff
52 
53 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
54 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
55 
56 
57 int __kprobes arch_prepare_kprobe(struct kprobe *p)
58 {
59 	kprobe_opcode_t insn;
60 	kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
61 	unsigned long addr = (unsigned long)p->addr;
62 	bool thumb;
63 	kprobe_decode_insn_t *decode_insn;
64 	const union decode_action *actions;
65 	int is;
66 	const struct decode_checker **checkers;
67 
68 #ifdef CONFIG_THUMB2_KERNEL
69 	thumb = true;
70 	addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
71 	insn = __mem_to_opcode_thumb16(((u16 *)addr)[0]);
72 	if (is_wide_instruction(insn)) {
73 		u16 inst2 = __mem_to_opcode_thumb16(((u16 *)addr)[1]);
74 		insn = __opcode_thumb32_compose(insn, inst2);
75 		decode_insn = thumb32_probes_decode_insn;
76 		actions = kprobes_t32_actions;
77 		checkers = kprobes_t32_checkers;
78 	} else {
79 		decode_insn = thumb16_probes_decode_insn;
80 		actions = kprobes_t16_actions;
81 		checkers = kprobes_t16_checkers;
82 	}
83 #else /* !CONFIG_THUMB2_KERNEL */
84 	thumb = false;
85 	if (addr & 0x3)
86 		return -EINVAL;
87 	insn = __mem_to_opcode_arm(*p->addr);
88 	decode_insn = arm_probes_decode_insn;
89 	actions = kprobes_arm_actions;
90 	checkers = kprobes_arm_checkers;
91 #endif
92 
93 	p->opcode = insn;
94 	p->ainsn.insn = tmp_insn;
95 
96 	switch ((*decode_insn)(insn, &p->ainsn, true, actions, checkers)) {
97 	case INSN_REJECTED:	/* not supported */
98 		return -EINVAL;
99 
100 	case INSN_GOOD:		/* instruction uses slot */
101 		p->ainsn.insn = get_insn_slot();
102 		if (!p->ainsn.insn)
103 			return -ENOMEM;
104 		for (is = 0; is < MAX_INSN_SIZE; ++is)
105 			p->ainsn.insn[is] = tmp_insn[is];
106 		flush_insns(p->ainsn.insn,
107 				sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
108 		p->ainsn.insn_fn = (probes_insn_fn_t *)
109 					((uintptr_t)p->ainsn.insn | thumb);
110 		break;
111 
112 	case INSN_GOOD_NO_SLOT:	/* instruction doesn't need insn slot */
113 		p->ainsn.insn = NULL;
114 		break;
115 	}
116 
117 	/*
118 	 * Never instrument insn like 'str r0, [sp, +/-r1]'. Also, insn likes
119 	 * 'str r0, [sp, #-68]' should also be prohibited.
120 	 * See __und_svc.
121 	 */
122 	if ((p->ainsn.stack_space < 0) ||
123 			(p->ainsn.stack_space > MAX_STACK_SIZE))
124 		return -EINVAL;
125 
126 	return 0;
127 }
128 
129 void __kprobes arch_arm_kprobe(struct kprobe *p)
130 {
131 	unsigned int brkp;
132 	void *addr;
133 
134 	if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
135 		/* Remove any Thumb flag */
136 		addr = (void *)((uintptr_t)p->addr & ~1);
137 
138 		if (is_wide_instruction(p->opcode))
139 			brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
140 		else
141 			brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
142 	} else {
143 		kprobe_opcode_t insn = p->opcode;
144 
145 		addr = p->addr;
146 		brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
147 
148 		if (insn >= 0xe0000000)
149 			brkp |= 0xe0000000;  /* Unconditional instruction */
150 		else
151 			brkp |= insn & 0xf0000000;  /* Copy condition from insn */
152 	}
153 
154 	patch_text(addr, brkp);
155 }
156 
157 /*
158  * The actual disarming is done here on each CPU and synchronized using
159  * stop_machine. This synchronization is necessary on SMP to avoid removing
160  * a probe between the moment the 'Undefined Instruction' exception is raised
161  * and the moment the exception handler reads the faulting instruction from
162  * memory. It is also needed to atomically set the two half-words of a 32-bit
163  * Thumb breakpoint.
164  */
165 struct patch {
166 	void *addr;
167 	unsigned int insn;
168 };
169 
170 static int __kprobes_remove_breakpoint(void *data)
171 {
172 	struct patch *p = data;
173 	__patch_text(p->addr, p->insn);
174 	return 0;
175 }
176 
177 void __kprobes kprobes_remove_breakpoint(void *addr, unsigned int insn)
178 {
179 	struct patch p = {
180 		.addr = addr,
181 		.insn = insn,
182 	};
183 	stop_machine_cpuslocked(__kprobes_remove_breakpoint, &p,
184 				cpu_online_mask);
185 }
186 
187 void __kprobes arch_disarm_kprobe(struct kprobe *p)
188 {
189 	kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
190 			p->opcode);
191 }
192 
193 void __kprobes arch_remove_kprobe(struct kprobe *p)
194 {
195 	if (p->ainsn.insn) {
196 		free_insn_slot(p->ainsn.insn, 0);
197 		p->ainsn.insn = NULL;
198 	}
199 }
200 
201 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
202 {
203 	kcb->prev_kprobe.kp = kprobe_running();
204 	kcb->prev_kprobe.status = kcb->kprobe_status;
205 }
206 
207 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
208 {
209 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
210 	kcb->kprobe_status = kcb->prev_kprobe.status;
211 }
212 
213 static void __kprobes set_current_kprobe(struct kprobe *p)
214 {
215 	__this_cpu_write(current_kprobe, p);
216 }
217 
218 static void __kprobes
219 singlestep_skip(struct kprobe *p, struct pt_regs *regs)
220 {
221 #ifdef CONFIG_THUMB2_KERNEL
222 	regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
223 	if (is_wide_instruction(p->opcode))
224 		regs->ARM_pc += 4;
225 	else
226 		regs->ARM_pc += 2;
227 #else
228 	regs->ARM_pc += 4;
229 #endif
230 }
231 
232 static inline void __kprobes
233 singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
234 {
235 	p->ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
236 }
237 
238 /*
239  * Called with IRQs disabled. IRQs must remain disabled from that point
240  * all the way until processing this kprobe is complete.  The current
241  * kprobes implementation cannot process more than one nested level of
242  * kprobe, and that level is reserved for user kprobe handlers, so we can't
243  * risk encountering a new kprobe in an interrupt handler.
244  */
245 void __kprobes kprobe_handler(struct pt_regs *regs)
246 {
247 	struct kprobe *p, *cur;
248 	struct kprobe_ctlblk *kcb;
249 
250 	kcb = get_kprobe_ctlblk();
251 	cur = kprobe_running();
252 
253 #ifdef CONFIG_THUMB2_KERNEL
254 	/*
255 	 * First look for a probe which was registered using an address with
256 	 * bit 0 set, this is the usual situation for pointers to Thumb code.
257 	 * If not found, fallback to looking for one with bit 0 clear.
258 	 */
259 	p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
260 	if (!p)
261 		p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
262 
263 #else /* ! CONFIG_THUMB2_KERNEL */
264 	p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
265 #endif
266 
267 	if (p) {
268 		if (!p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
269 			/*
270 			 * Probe hit but conditional execution check failed,
271 			 * so just skip the instruction and continue as if
272 			 * nothing had happened.
273 			 * In this case, we can skip recursing check too.
274 			 */
275 			singlestep_skip(p, regs);
276 		} else if (cur) {
277 			/* Kprobe is pending, so we're recursing. */
278 			switch (kcb->kprobe_status) {
279 			case KPROBE_HIT_ACTIVE:
280 			case KPROBE_HIT_SSDONE:
281 			case KPROBE_HIT_SS:
282 				/* A pre- or post-handler probe got us here. */
283 				kprobes_inc_nmissed_count(p);
284 				save_previous_kprobe(kcb);
285 				set_current_kprobe(p);
286 				kcb->kprobe_status = KPROBE_REENTER;
287 				singlestep(p, regs, kcb);
288 				restore_previous_kprobe(kcb);
289 				break;
290 			case KPROBE_REENTER:
291 				/* A nested probe was hit in FIQ, it is a BUG */
292 				pr_warn("Unrecoverable kprobe detected at %p.\n",
293 					p->addr);
294 				/* fall through */
295 			default:
296 				/* impossible cases */
297 				BUG();
298 			}
299 		} else {
300 			/* Probe hit and conditional execution check ok. */
301 			set_current_kprobe(p);
302 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
303 
304 			/*
305 			 * If we have no pre-handler or it returned 0, we
306 			 * continue with normal processing.  If we have a
307 			 * pre-handler and it returned non-zero, it prepped
308 			 * for calling the break_handler below on re-entry,
309 			 * so get out doing nothing more here.
310 			 */
311 			if (!p->pre_handler || !p->pre_handler(p, regs)) {
312 				kcb->kprobe_status = KPROBE_HIT_SS;
313 				singlestep(p, regs, kcb);
314 				if (p->post_handler) {
315 					kcb->kprobe_status = KPROBE_HIT_SSDONE;
316 					p->post_handler(p, regs, 0);
317 				}
318 				reset_current_kprobe();
319 			}
320 		}
321 	} else if (cur) {
322 		/* We probably hit a jprobe.  Call its break handler. */
323 		if (cur->break_handler && cur->break_handler(cur, regs)) {
324 			kcb->kprobe_status = KPROBE_HIT_SS;
325 			singlestep(cur, regs, kcb);
326 			if (cur->post_handler) {
327 				kcb->kprobe_status = KPROBE_HIT_SSDONE;
328 				cur->post_handler(cur, regs, 0);
329 			}
330 		}
331 		reset_current_kprobe();
332 	} else {
333 		/*
334 		 * The probe was removed and a race is in progress.
335 		 * There is nothing we can do about it.  Let's restart
336 		 * the instruction.  By the time we can restart, the
337 		 * real instruction will be there.
338 		 */
339 	}
340 }
341 
342 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
343 {
344 	unsigned long flags;
345 	local_irq_save(flags);
346 	kprobe_handler(regs);
347 	local_irq_restore(flags);
348 	return 0;
349 }
350 
351 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
352 {
353 	struct kprobe *cur = kprobe_running();
354 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
355 
356 	switch (kcb->kprobe_status) {
357 	case KPROBE_HIT_SS:
358 	case KPROBE_REENTER:
359 		/*
360 		 * We are here because the instruction being single
361 		 * stepped caused a page fault. We reset the current
362 		 * kprobe and the PC to point back to the probe address
363 		 * and allow the page fault handler to continue as a
364 		 * normal page fault.
365 		 */
366 		regs->ARM_pc = (long)cur->addr;
367 		if (kcb->kprobe_status == KPROBE_REENTER) {
368 			restore_previous_kprobe(kcb);
369 		} else {
370 			reset_current_kprobe();
371 		}
372 		break;
373 
374 	case KPROBE_HIT_ACTIVE:
375 	case KPROBE_HIT_SSDONE:
376 		/*
377 		 * We increment the nmissed count for accounting,
378 		 * we can also use npre/npostfault count for accounting
379 		 * these specific fault cases.
380 		 */
381 		kprobes_inc_nmissed_count(cur);
382 
383 		/*
384 		 * We come here because instructions in the pre/post
385 		 * handler caused the page_fault, this could happen
386 		 * if handler tries to access user space by
387 		 * copy_from_user(), get_user() etc. Let the
388 		 * user-specified handler try to fix it.
389 		 */
390 		if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
391 			return 1;
392 		break;
393 
394 	default:
395 		break;
396 	}
397 
398 	return 0;
399 }
400 
401 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
402 				       unsigned long val, void *data)
403 {
404 	/*
405 	 * notify_die() is currently never called on ARM,
406 	 * so this callback is currently empty.
407 	 */
408 	return NOTIFY_DONE;
409 }
410 
411 /*
412  * When a retprobed function returns, trampoline_handler() is called,
413  * calling the kretprobe's handler. We construct a struct pt_regs to
414  * give a view of registers r0-r11 to the user return-handler.  This is
415  * not a complete pt_regs structure, but that should be plenty sufficient
416  * for kretprobe handlers which should normally be interested in r0 only
417  * anyway.
418  */
419 void __naked __kprobes kretprobe_trampoline(void)
420 {
421 	__asm__ __volatile__ (
422 		"stmdb	sp!, {r0 - r11}		\n\t"
423 		"mov	r0, sp			\n\t"
424 		"bl	trampoline_handler	\n\t"
425 		"mov	lr, r0			\n\t"
426 		"ldmia	sp!, {r0 - r11}		\n\t"
427 #ifdef CONFIG_THUMB2_KERNEL
428 		"bx	lr			\n\t"
429 #else
430 		"mov	pc, lr			\n\t"
431 #endif
432 		: : : "memory");
433 }
434 
435 /* Called from kretprobe_trampoline */
436 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
437 {
438 	struct kretprobe_instance *ri = NULL;
439 	struct hlist_head *head, empty_rp;
440 	struct hlist_node *tmp;
441 	unsigned long flags, orig_ret_address = 0;
442 	unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
443 	kprobe_opcode_t *correct_ret_addr = NULL;
444 
445 	INIT_HLIST_HEAD(&empty_rp);
446 	kretprobe_hash_lock(current, &head, &flags);
447 
448 	/*
449 	 * It is possible to have multiple instances associated with a given
450 	 * task either because multiple functions in the call path have
451 	 * a return probe installed on them, and/or more than one return
452 	 * probe was registered for a target function.
453 	 *
454 	 * We can handle this because:
455 	 *     - instances are always inserted at the head of the list
456 	 *     - when multiple return probes are registered for the same
457 	 *       function, the first instance's ret_addr will point to the
458 	 *       real return address, and all the rest will point to
459 	 *       kretprobe_trampoline
460 	 */
461 	hlist_for_each_entry_safe(ri, tmp, head, hlist) {
462 		if (ri->task != current)
463 			/* another task is sharing our hash bucket */
464 			continue;
465 
466 		orig_ret_address = (unsigned long)ri->ret_addr;
467 
468 		if (orig_ret_address != trampoline_address)
469 			/*
470 			 * This is the real return address. Any other
471 			 * instances associated with this task are for
472 			 * other calls deeper on the call stack
473 			 */
474 			break;
475 	}
476 
477 	kretprobe_assert(ri, orig_ret_address, trampoline_address);
478 
479 	correct_ret_addr = ri->ret_addr;
480 	hlist_for_each_entry_safe(ri, tmp, head, hlist) {
481 		if (ri->task != current)
482 			/* another task is sharing our hash bucket */
483 			continue;
484 
485 		orig_ret_address = (unsigned long)ri->ret_addr;
486 		if (ri->rp && ri->rp->handler) {
487 			__this_cpu_write(current_kprobe, &ri->rp->kp);
488 			get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
489 			ri->ret_addr = correct_ret_addr;
490 			ri->rp->handler(ri, regs);
491 			__this_cpu_write(current_kprobe, NULL);
492 		}
493 
494 		recycle_rp_inst(ri, &empty_rp);
495 
496 		if (orig_ret_address != trampoline_address)
497 			/*
498 			 * This is the real return address. Any other
499 			 * instances associated with this task are for
500 			 * other calls deeper on the call stack
501 			 */
502 			break;
503 	}
504 
505 	kretprobe_hash_unlock(current, &flags);
506 
507 	hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
508 		hlist_del(&ri->hlist);
509 		kfree(ri);
510 	}
511 
512 	return (void *)orig_ret_address;
513 }
514 
515 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
516 				      struct pt_regs *regs)
517 {
518 	ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
519 
520 	/* Replace the return addr with trampoline addr. */
521 	regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
522 }
523 
524 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
525 {
526 	struct jprobe *jp = container_of(p, struct jprobe, kp);
527 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
528 	long sp_addr = regs->ARM_sp;
529 	long cpsr;
530 
531 	kcb->jprobe_saved_regs = *regs;
532 	memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
533 	regs->ARM_pc = (long)jp->entry;
534 
535 	cpsr = regs->ARM_cpsr | PSR_I_BIT;
536 #ifdef CONFIG_THUMB2_KERNEL
537 	/* Set correct Thumb state in cpsr */
538 	if (regs->ARM_pc & 1)
539 		cpsr |= PSR_T_BIT;
540 	else
541 		cpsr &= ~PSR_T_BIT;
542 #endif
543 	regs->ARM_cpsr = cpsr;
544 
545 	preempt_disable();
546 	return 1;
547 }
548 
549 void __kprobes jprobe_return(void)
550 {
551 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
552 
553 	__asm__ __volatile__ (
554 		/*
555 		 * Setup an empty pt_regs. Fill SP and PC fields as
556 		 * they're needed by longjmp_break_handler.
557 		 *
558 		 * We allocate some slack between the original SP and start of
559 		 * our fabricated regs. To be precise we want to have worst case
560 		 * covered which is STMFD with all 16 regs so we allocate 2 *
561 		 * sizeof(struct_pt_regs)).
562 		 *
563 		 * This is to prevent any simulated instruction from writing
564 		 * over the regs when they are accessing the stack.
565 		 */
566 #ifdef CONFIG_THUMB2_KERNEL
567 		"sub    r0, %0, %1		\n\t"
568 		"mov    sp, r0			\n\t"
569 #else
570 		"sub    sp, %0, %1		\n\t"
571 #endif
572 		"ldr    r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
573 		"str    %0, [sp, %2]		\n\t"
574 		"str    r0, [sp, %3]		\n\t"
575 		"mov    r0, sp			\n\t"
576 		"bl     kprobe_handler		\n\t"
577 
578 		/*
579 		 * Return to the context saved by setjmp_pre_handler
580 		 * and restored by longjmp_break_handler.
581 		 */
582 #ifdef CONFIG_THUMB2_KERNEL
583 		"ldr	lr, [sp, %2]		\n\t" /* lr = saved sp */
584 		"ldrd	r0, r1, [sp, %5]	\n\t" /* r0,r1 = saved lr,pc */
585 		"ldr	r2, [sp, %4]		\n\t" /* r2 = saved psr */
586 		"stmdb	lr!, {r0, r1, r2}	\n\t" /* push saved lr and */
587 						      /* rfe context */
588 		"ldmia	sp, {r0 - r12}		\n\t"
589 		"mov	sp, lr			\n\t"
590 		"ldr	lr, [sp], #4		\n\t"
591 		"rfeia	sp!			\n\t"
592 #else
593 		"ldr	r0, [sp, %4]		\n\t"
594 		"msr	cpsr_cxsf, r0		\n\t"
595 		"ldmia	sp, {r0 - pc}		\n\t"
596 #endif
597 		:
598 		: "r" (kcb->jprobe_saved_regs.ARM_sp),
599 		  "I" (sizeof(struct pt_regs) * 2),
600 		  "J" (offsetof(struct pt_regs, ARM_sp)),
601 		  "J" (offsetof(struct pt_regs, ARM_pc)),
602 		  "J" (offsetof(struct pt_regs, ARM_cpsr)),
603 		  "J" (offsetof(struct pt_regs, ARM_lr))
604 		: "memory", "cc");
605 }
606 
607 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
608 {
609 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
610 	long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
611 	long orig_sp = regs->ARM_sp;
612 	struct jprobe *jp = container_of(p, struct jprobe, kp);
613 
614 	if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
615 		if (orig_sp != stack_addr) {
616 			struct pt_regs *saved_regs =
617 				(struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
618 			printk("current sp %lx does not match saved sp %lx\n",
619 			       orig_sp, stack_addr);
620 			printk("Saved registers for jprobe %p\n", jp);
621 			show_regs(saved_regs);
622 			printk("Current registers\n");
623 			show_regs(regs);
624 			BUG();
625 		}
626 		*regs = kcb->jprobe_saved_regs;
627 		memcpy((void *)stack_addr, kcb->jprobes_stack,
628 		       MIN_STACK_SIZE(stack_addr));
629 		preempt_enable_no_resched();
630 		return 1;
631 	}
632 	return 0;
633 }
634 
635 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
636 {
637 	return 0;
638 }
639 
640 #ifdef CONFIG_THUMB2_KERNEL
641 
642 static struct undef_hook kprobes_thumb16_break_hook = {
643 	.instr_mask	= 0xffff,
644 	.instr_val	= KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
645 	.cpsr_mask	= MODE_MASK,
646 	.cpsr_val	= SVC_MODE,
647 	.fn		= kprobe_trap_handler,
648 };
649 
650 static struct undef_hook kprobes_thumb32_break_hook = {
651 	.instr_mask	= 0xffffffff,
652 	.instr_val	= KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
653 	.cpsr_mask	= MODE_MASK,
654 	.cpsr_val	= SVC_MODE,
655 	.fn		= kprobe_trap_handler,
656 };
657 
658 #else  /* !CONFIG_THUMB2_KERNEL */
659 
660 static struct undef_hook kprobes_arm_break_hook = {
661 	.instr_mask	= 0x0fffffff,
662 	.instr_val	= KPROBE_ARM_BREAKPOINT_INSTRUCTION,
663 	.cpsr_mask	= MODE_MASK,
664 	.cpsr_val	= SVC_MODE,
665 	.fn		= kprobe_trap_handler,
666 };
667 
668 #endif /* !CONFIG_THUMB2_KERNEL */
669 
670 int __init arch_init_kprobes()
671 {
672 	arm_probes_decode_init();
673 #ifdef CONFIG_THUMB2_KERNEL
674 	register_undef_hook(&kprobes_thumb16_break_hook);
675 	register_undef_hook(&kprobes_thumb32_break_hook);
676 #else
677 	register_undef_hook(&kprobes_arm_break_hook);
678 #endif
679 	return 0;
680 }
681 
682 bool arch_within_kprobe_blacklist(unsigned long addr)
683 {
684 	void *a = (void *)addr;
685 
686 	return __in_irqentry_text(addr) ||
687 	       in_entry_text(addr) ||
688 	       in_idmap_text(addr) ||
689 	       memory_contains(__kprobes_text_start, __kprobes_text_end, a, 1);
690 }
691