blob: 755a0d7d887fad22890e6cabf2a25de97fbb4d42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* arch/sparc64/kernel/kprobes.c
2 *
3 * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/config.h>
7#include <linux/kernel.h>
8#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/kdebug.h>
10#include <asm/signal.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070011#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13/* We do not have hardware single-stepping on sparc64.
14 * So we implement software single-stepping with breakpoint
15 * traps. The top-level scheme is similar to that used
16 * in the x86 kprobes implementation.
17 *
18 * In the kprobe->ainsn.insn[] array we store the original
19 * instruction at index zero and a break instruction at
20 * index one.
21 *
22 * When we hit a kprobe we:
23 * - Run the pre-handler
24 * - Remember "regs->tnpc" and interrupt level stored in
25 * "regs->tstate" so we can restore them later
26 * - Disable PIL interrupts
27 * - Set regs->tpc to point to kprobe->ainsn.insn[0]
28 * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
29 * - Mark that we are actively in a kprobe
30 *
31 * At this point we wait for the second breakpoint at
32 * kprobe->ainsn.insn[1] to hit. When it does we:
33 * - Run the post-handler
34 * - Set regs->tpc to "remembered" regs->tnpc stored above,
35 * restore the PIL interrupt level in "regs->tstate" as well
36 * - Make any adjustments necessary to regs->tnpc in order
37 * to handle relative branches correctly. See below.
38 * - Mark that we are no longer actively in a kprobe.
39 */
40
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070041int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 return 0;
44}
45
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070046void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 p->ainsn.insn[0] = *p->addr;
49 p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070050 p->opcode = *p->addr;
51}
52
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070053void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070054{
55 *p->addr = BREAKPOINT_INSTRUCTION;
56 flushi(p->addr);
57}
58
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070059void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070060{
61 *p->addr = p->opcode;
62 flushi(p->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070065void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static struct kprobe *current_kprobe;
70static unsigned long current_kprobe_orig_tnpc;
71static unsigned long current_kprobe_orig_tstate_pil;
72static unsigned int kprobe_status;
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070073static struct kprobe *kprobe_prev;
74static unsigned long kprobe_orig_tnpc_prev;
75static unsigned long kprobe_orig_tstate_pil_prev;
76static unsigned int kprobe_status_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070078static inline void save_previous_kprobe(void)
79{
80 kprobe_status_prev = kprobe_status;
81 kprobe_orig_tnpc_prev = current_kprobe_orig_tnpc;
82 kprobe_orig_tstate_pil_prev = current_kprobe_orig_tstate_pil;
83 kprobe_prev = current_kprobe;
84}
85
86static inline void restore_previous_kprobe(void)
87{
88 kprobe_status = kprobe_status_prev;
89 current_kprobe_orig_tnpc = kprobe_orig_tnpc_prev;
90 current_kprobe_orig_tstate_pil = kprobe_orig_tstate_pil_prev;
91 current_kprobe = kprobe_prev;
92}
93
94static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 current_kprobe_orig_tnpc = regs->tnpc;
97 current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070098 current_kprobe = p;
99}
100
101static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 regs->tstate |= TSTATE_PIL;
104
105 /*single step inline, if it a breakpoint instruction*/
106 if (p->opcode == BREAKPOINT_INSTRUCTION) {
107 regs->tpc = (unsigned long) p->addr;
108 regs->tnpc = current_kprobe_orig_tnpc;
109 } else {
110 regs->tpc = (unsigned long) &p->ainsn.insn[0];
111 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
112 }
113}
114
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700115static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct kprobe *p;
118 void *addr = (void *) regs->tpc;
119 int ret = 0;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (kprobe_running()) {
122 /* We *are* holding lock here, so this is safe.
123 * Disarm the probe we just hit, and ignore it.
124 */
125 p = get_kprobe(addr);
126 if (p) {
127 if (kprobe_status == KPROBE_HIT_SS) {
128 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
129 current_kprobe_orig_tstate_pil);
130 unlock_kprobes();
131 goto no_kprobe;
132 }
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700133 /* We have reentered the kprobe_handler(), since
134 * another probe was hit while within the handler.
135 * We here save the original kprobes variables and
136 * just single step on the instruction of the new probe
137 * without calling any user handlers.
138 */
139 save_previous_kprobe();
140 set_current_kprobe(p, regs);
141 p->nmissed++;
142 kprobe_status = KPROBE_REENTER;
143 prepare_singlestep(p, regs);
144 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 } else {
146 p = current_kprobe;
147 if (p->break_handler && p->break_handler(p, regs))
148 goto ss_probe;
149 }
150 /* If it's not ours, can't be delete race, (we hold lock). */
151 goto no_kprobe;
152 }
153
154 lock_kprobes();
155 p = get_kprobe(addr);
156 if (!p) {
157 unlock_kprobes();
158 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
159 /*
160 * The breakpoint instruction was removed right
161 * after we hit it. Another cpu has removed
162 * either a probepoint or a debugger breakpoint
163 * at this address. In either case, no further
164 * handling of this interrupt is appropriate.
165 */
166 ret = 1;
167 }
168 /* Not one of ours: let kernel handle it */
169 goto no_kprobe;
170 }
171
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800172 /*
173 * This preempt_disable() matches the preempt_enable_no_resched()
174 * in post_kprobes_handler()
175 */
176 preempt_disable();
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700177 set_current_kprobe(p, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (p->pre_handler && p->pre_handler(p, regs))
180 return 1;
181
182ss_probe:
183 prepare_singlestep(p, regs);
184 kprobe_status = KPROBE_HIT_SS;
185 return 1;
186
187no_kprobe:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return ret;
189}
190
191/* If INSN is a relative control transfer instruction,
192 * return the corrected branch destination value.
193 *
194 * The original INSN location was REAL_PC, it actually
195 * executed at PC and produced destination address NPC.
196 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700197static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
198 unsigned long pc,
199 unsigned long npc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 /* Branch not taken, no mods necessary. */
202 if (npc == pc + 0x4UL)
203 return real_pc + 0x4UL;
204
205 /* The three cases are call, branch w/prediction,
206 * and traditional branch.
207 */
208 if ((insn & 0xc0000000) == 0x40000000 ||
209 (insn & 0xc1c00000) == 0x00400000 ||
210 (insn & 0xc1c00000) == 0x00800000) {
211 /* The instruction did all the work for us
212 * already, just apply the offset to the correct
213 * instruction location.
214 */
215 return (real_pc + (npc - pc));
216 }
217
218 return real_pc + 0x4UL;
219}
220
221/* If INSN is an instruction which writes it's PC location
222 * into a destination register, fix that up.
223 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700224static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
225 unsigned long real_pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned long *slot = NULL;
228
229 /* Simplest cast is call, which always uses %o7 */
230 if ((insn & 0xc0000000) == 0x40000000) {
231 slot = &regs->u_regs[UREG_I7];
232 }
233
234 /* Jmpl encodes the register inside of the opcode */
235 if ((insn & 0xc1f80000) == 0x81c00000) {
236 unsigned long rd = ((insn >> 25) & 0x1f);
237
238 if (rd <= 15) {
239 slot = &regs->u_regs[rd];
240 } else {
241 /* Hard case, it goes onto the stack. */
242 flushw_all();
243
244 rd -= 16;
245 slot = (unsigned long *)
246 (regs->u_regs[UREG_FP] + STACK_BIAS);
247 slot += rd;
248 }
249 }
250 if (slot != NULL)
251 *slot = real_pc;
252}
253
254/*
255 * Called after single-stepping. p->addr is the address of the
256 * instruction whose first byte has been replaced by the breakpoint
257 * instruction. To avoid the SMP problems that can occur when we
258 * temporarily put back the original opcode to single-step, we
259 * single-stepped a copy of the instruction. The address of this
260 * copy is p->ainsn.insn.
261 *
262 * This function prepares to return from the post-single-step
263 * breakpoint trap.
264 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700265static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 u32 insn = p->ainsn.insn[0];
268
269 regs->tpc = current_kprobe_orig_tnpc;
270 regs->tnpc = relbranch_fixup(insn,
271 (unsigned long) p->addr,
272 (unsigned long) &p->ainsn.insn[0],
273 regs->tnpc);
274 retpc_fixup(regs, insn, (unsigned long) p->addr);
275
276 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
277 current_kprobe_orig_tstate_pil);
278}
279
280static inline int post_kprobe_handler(struct pt_regs *regs)
281{
282 if (!kprobe_running())
283 return 0;
284
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700285 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
286 kprobe_status = KPROBE_HIT_SSDONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 current_kprobe->post_handler(current_kprobe, regs, 0);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 resume_execution(current_kprobe, regs);
291
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700292 /*Restore back the original saved kprobes variables and continue. */
293 if (kprobe_status == KPROBE_REENTER) {
294 restore_previous_kprobe();
295 goto out;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unlock_kprobes();
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700298out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 preempt_enable_no_resched();
300
301 return 1;
302}
303
304/* Interrupts disabled, kprobe_lock held. */
305static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
306{
307 if (current_kprobe->fault_handler
308 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
309 return 1;
310
311 if (kprobe_status & KPROBE_HIT_SS) {
312 resume_execution(current_kprobe, regs);
313
314 unlock_kprobes();
315 preempt_enable_no_resched();
316 }
317 return 0;
318}
319
320/*
321 * Wrapper routine to for handling exceptions.
322 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700323int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
324 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800327 int ret = NOTIFY_DONE;
328
329 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 switch (val) {
331 case DIE_DEBUG:
332 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800333 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 case DIE_DEBUG_2:
336 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800337 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
339 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 case DIE_PAGE_FAULT:
341 if (kprobe_running() &&
342 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800343 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 break;
345 default:
346 break;
347 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800348 preempt_enable();
349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700352asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
353 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
356
357 if (user_mode(regs)) {
358 local_irq_enable();
359 bad_trap(regs, trap_level);
360 return;
361 }
362
363 /* trap_level == 0x170 --> ta 0x70
364 * trap_level == 0x171 --> ta 0x71
365 */
366 if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
367 (trap_level == 0x170) ? "debug" : "debug_2",
368 regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
369 bad_trap(regs, trap_level);
370}
371
372/* Jprobes support. */
373static struct pt_regs jprobe_saved_regs;
374static struct pt_regs *jprobe_saved_regs_location;
375static struct sparc_stackf jprobe_saved_stack;
376
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700377int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct jprobe *jp = container_of(p, struct jprobe, kp);
380
381 jprobe_saved_regs_location = regs;
382 memcpy(&jprobe_saved_regs, regs, sizeof(*regs));
383
384 /* Save a whole stack frame, this gets arguments
385 * pushed onto the stack after using up all the
386 * arg registers.
387 */
388 memcpy(&jprobe_saved_stack,
389 (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
390 sizeof(jprobe_saved_stack));
391
392 regs->tpc = (unsigned long) jp->entry;
393 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
394 regs->tstate |= TSTATE_PIL;
395
396 return 1;
397}
398
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700399void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 __asm__ __volatile__(
402 ".globl jprobe_return_trap_instruction\n"
403"jprobe_return_trap_instruction:\n\t"
404 "ta 0x70");
405}
406
407extern void jprobe_return_trap_instruction(void);
408
409extern void __show_regs(struct pt_regs * regs);
410
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700411int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 u32 *addr = (u32 *) regs->tpc;
414
415 if (addr == (u32 *) jprobe_return_trap_instruction) {
416 if (jprobe_saved_regs_location != regs) {
417 printk("JPROBE: Current regs (%p) does not match "
418 "saved regs (%p).\n",
419 regs, jprobe_saved_regs_location);
420 printk("JPROBE: Saved registers\n");
421 __show_regs(jprobe_saved_regs_location);
422 printk("JPROBE: Current registers\n");
423 __show_regs(regs);
424 BUG();
425 }
426 /* Restore old register state. Do pt_regs
427 * first so that UREG_FP is the original one for
428 * the stack frame restore.
429 */
430 memcpy(regs, &jprobe_saved_regs, sizeof(*regs));
431
432 memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
433 &jprobe_saved_stack,
434 sizeof(jprobe_saved_stack));
435
436 return 1;
437 }
438 return 0;
439}
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700440
Rusty Lynch67729262005-07-05 18:54:50 -0700441/* architecture specific initialization */
442int arch_init_kprobes(void)
443{
444 return 0;
445}