blob: 8e75ed762fd80a8555d831c6b324b78282553b6c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/kernel.h>
7#include <linux/kprobes.h>
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -08008#include <linux/module.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>
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -080012#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14/* We do not have hardware single-stepping on sparc64.
15 * So we implement software single-stepping with breakpoint
16 * traps. The top-level scheme is similar to that used
17 * in the x86 kprobes implementation.
18 *
19 * In the kprobe->ainsn.insn[] array we store the original
20 * instruction at index zero and a break instruction at
21 * index one.
22 *
23 * When we hit a kprobe we:
24 * - Run the pre-handler
25 * - Remember "regs->tnpc" and interrupt level stored in
26 * "regs->tstate" so we can restore them later
27 * - Disable PIL interrupts
28 * - Set regs->tpc to point to kprobe->ainsn.insn[0]
29 * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
30 * - Mark that we are actively in a kprobe
31 *
32 * At this point we wait for the second breakpoint at
33 * kprobe->ainsn.insn[1] to hit. When it does we:
34 * - Run the post-handler
35 * - Set regs->tpc to "remembered" regs->tnpc stored above,
36 * restore the PIL interrupt level in "regs->tstate" as well
37 * - Make any adjustments necessary to regs->tnpc in order
38 * to handle relative branches correctly. See below.
39 * - Mark that we are no longer actively in a kprobe.
40 */
41
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080042DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
43DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
44
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070045int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 p->ainsn.insn[0] = *p->addr;
48 p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070049 p->opcode = *p->addr;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -080050 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070051}
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 Panchamukhi07fab8d2006-04-18 22:22:03 -070065static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070066{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080067 kcb->prev_kprobe.kp = kprobe_running();
68 kcb->prev_kprobe.status = kcb->kprobe_status;
69 kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc;
70 kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil;
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070071}
72
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -070073static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070074{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080075 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
76 kcb->kprobe_status = kcb->prev_kprobe.status;
77 kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc;
78 kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil;
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070079}
80
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -070081static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080082 struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080084 __get_cpu_var(current_kprobe) = p;
85 kcb->kprobe_orig_tnpc = regs->tnpc;
86 kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070087}
88
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -070089static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080090 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070091{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 regs->tstate |= TSTATE_PIL;
93
94 /*single step inline, if it a breakpoint instruction*/
95 if (p->opcode == BREAKPOINT_INSTRUCTION) {
96 regs->tpc = (unsigned long) p->addr;
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080097 regs->tnpc = kcb->kprobe_orig_tnpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 } else {
99 regs->tpc = (unsigned long) &p->ainsn.insn[0];
100 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
101 }
102}
103
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700104static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct kprobe *p;
107 void *addr = (void *) regs->tpc;
108 int ret = 0;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800109 struct kprobe_ctlblk *kcb;
110
111 /*
112 * We don't want to be preempted for the entire
113 * duration of kprobe processing
114 */
115 preempt_disable();
116 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 p = get_kprobe(addr);
120 if (p) {
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800121 if (kcb->kprobe_status == KPROBE_HIT_SS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800123 kcb->kprobe_orig_tstate_pil);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto no_kprobe;
125 }
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700126 /* We have reentered the kprobe_handler(), since
127 * another probe was hit while within the handler.
128 * We here save the original kprobes variables and
129 * just single step on the instruction of the new probe
130 * without calling any user handlers.
131 */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800132 save_previous_kprobe(kcb);
133 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800134 kprobes_inc_nmissed_count(p);
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800135 kcb->kprobe_status = KPROBE_REENTER;
136 prepare_singlestep(p, regs, kcb);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700137 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800139 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
140 /* The breakpoint instruction was removed by
141 * another cpu right after we hit, no further
142 * handling of this interrupt is appropriate
143 */
144 ret = 1;
145 goto no_kprobe;
146 }
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800147 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (p->break_handler && p->break_handler(p, regs))
149 goto ss_probe;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto no_kprobe;
152 }
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 p = get_kprobe(addr);
155 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
157 /*
158 * The breakpoint instruction was removed right
159 * after we hit it. Another cpu has removed
160 * either a probepoint or a debugger breakpoint
161 * at this address. In either case, no further
162 * handling of this interrupt is appropriate.
163 */
164 ret = 1;
165 }
166 /* Not one of ours: let kernel handle it */
167 goto no_kprobe;
168 }
169
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800170 set_current_kprobe(p, regs, kcb);
171 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (p->pre_handler && p->pre_handler(p, regs))
173 return 1;
174
175ss_probe:
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800176 prepare_singlestep(p, regs, kcb);
177 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 1;
179
180no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800181 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return ret;
183}
184
185/* If INSN is a relative control transfer instruction,
186 * return the corrected branch destination value.
187 *
188 * The original INSN location was REAL_PC, it actually
189 * executed at PC and produced destination address NPC.
190 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700191static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
192 unsigned long pc,
193 unsigned long npc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 /* Branch not taken, no mods necessary. */
196 if (npc == pc + 0x4UL)
197 return real_pc + 0x4UL;
198
199 /* The three cases are call, branch w/prediction,
200 * and traditional branch.
201 */
202 if ((insn & 0xc0000000) == 0x40000000 ||
203 (insn & 0xc1c00000) == 0x00400000 ||
204 (insn & 0xc1c00000) == 0x00800000) {
205 /* The instruction did all the work for us
206 * already, just apply the offset to the correct
207 * instruction location.
208 */
209 return (real_pc + (npc - pc));
210 }
211
212 return real_pc + 0x4UL;
213}
214
215/* If INSN is an instruction which writes it's PC location
216 * into a destination register, fix that up.
217 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700218static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
219 unsigned long real_pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 unsigned long *slot = NULL;
222
223 /* Simplest cast is call, which always uses %o7 */
224 if ((insn & 0xc0000000) == 0x40000000) {
225 slot = &regs->u_regs[UREG_I7];
226 }
227
228 /* Jmpl encodes the register inside of the opcode */
229 if ((insn & 0xc1f80000) == 0x81c00000) {
230 unsigned long rd = ((insn >> 25) & 0x1f);
231
232 if (rd <= 15) {
233 slot = &regs->u_regs[rd];
234 } else {
235 /* Hard case, it goes onto the stack. */
236 flushw_all();
237
238 rd -= 16;
239 slot = (unsigned long *)
240 (regs->u_regs[UREG_FP] + STACK_BIAS);
241 slot += rd;
242 }
243 }
244 if (slot != NULL)
245 *slot = real_pc;
246}
247
248/*
249 * Called after single-stepping. p->addr is the address of the
250 * instruction whose first byte has been replaced by the breakpoint
251 * instruction. To avoid the SMP problems that can occur when we
252 * temporarily put back the original opcode to single-step, we
253 * single-stepped a copy of the instruction. The address of this
254 * copy is p->ainsn.insn.
255 *
256 * This function prepares to return from the post-single-step
257 * breakpoint trap.
258 */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800259static void __kprobes resume_execution(struct kprobe *p,
260 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 u32 insn = p->ainsn.insn[0];
263
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800264 regs->tpc = kcb->kprobe_orig_tnpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 regs->tnpc = relbranch_fixup(insn,
266 (unsigned long) p->addr,
267 (unsigned long) &p->ainsn.insn[0],
268 regs->tnpc);
269 retpc_fixup(regs, insn, (unsigned long) p->addr);
270
271 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800272 kcb->kprobe_orig_tstate_pil);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -0700275static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800277 struct kprobe *cur = kprobe_running();
278 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
279
280 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800283 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
284 kcb->kprobe_status = KPROBE_HIT_SSDONE;
285 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800288 resume_execution(cur, regs, kcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700290 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800291 if (kcb->kprobe_status == KPROBE_REENTER) {
292 restore_previous_kprobe(kcb);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700293 goto out;
294 }
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800295 reset_current_kprobe();
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700296out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 preempt_enable_no_resched();
298
299 return 1;
300}
301
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -0700302static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800304 struct kprobe *cur = kprobe_running();
305 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800306 const struct exception_table_entry *entry;
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800307
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800308 switch(kcb->kprobe_status) {
309 case KPROBE_HIT_SS:
310 case KPROBE_REENTER:
311 /*
312 * We are here because the instruction being single
313 * stepped caused a page fault. We reset the current
314 * kprobe and the tpc points back to the probe address
315 * and allow the page fault handler to continue as a
316 * normal page fault.
317 */
318 regs->tpc = (unsigned long)cur->addr;
319 regs->tnpc = kcb->kprobe_orig_tnpc;
320 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
321 kcb->kprobe_orig_tstate_pil);
322 if (kcb->kprobe_status == KPROBE_REENTER)
323 restore_previous_kprobe(kcb);
324 else
325 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 preempt_enable_no_resched();
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800327 break;
328 case KPROBE_HIT_ACTIVE:
329 case KPROBE_HIT_SSDONE:
330 /*
331 * We increment the nmissed count for accounting,
332 * we can also use npre/npostfault count for accouting
333 * these specific fault cases.
334 */
335 kprobes_inc_nmissed_count(cur);
336
337 /*
338 * We come here because instructions in the pre/post
339 * handler caused the page_fault, this could happen
340 * if handler tries to access user space by
341 * copy_from_user(), get_user() etc. Let the
342 * user-specified handler try to fix it first.
343 */
344 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
345 return 1;
346
347 /*
348 * In case the user-specified fault handler returned
349 * zero, try to fix up.
350 */
351
352 entry = search_exception_tables(regs->tpc);
353 if (entry) {
354 regs->tpc = entry->fixup;
355 regs->tnpc = regs->tpc + 4;
356 return 1;
357 }
358
359 /*
360 * fixup_exception() could not handle it,
361 * Let do_page_fault() fix it.
362 */
363 break;
364 default:
365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
371/*
372 * Wrapper routine to for handling exceptions.
373 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700374int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
375 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800378 int ret = NOTIFY_DONE;
379
bibo,mao2326c772006-03-26 01:38:21 -0800380 if (args->regs && user_mode(args->regs))
381 return ret;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 switch (val) {
384 case DIE_DEBUG:
385 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800386 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388 case DIE_DEBUG_2:
389 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800390 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
392 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800394 /* kprobe_running() needs smp_processor_id() */
395 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (kprobe_running() &&
397 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800398 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800399 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401 default:
402 break;
403 }
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700407asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
408 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
411
412 if (user_mode(regs)) {
413 local_irq_enable();
414 bad_trap(regs, trap_level);
415 return;
416 }
417
418 /* trap_level == 0x170 --> ta 0x70
419 * trap_level == 0x171 --> ta 0x71
420 */
421 if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
422 (trap_level == 0x170) ? "debug" : "debug_2",
423 regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
424 bad_trap(regs, trap_level);
425}
426
427/* Jprobes support. */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700428int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct jprobe *jp = container_of(p, struct jprobe, kp);
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800431 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800433 kcb->jprobe_saved_regs_location = regs;
434 memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* Save a whole stack frame, this gets arguments
437 * pushed onto the stack after using up all the
438 * arg registers.
439 */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800440 memcpy(&(kcb->jprobe_saved_stack),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800442 sizeof(kcb->jprobe_saved_stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 regs->tpc = (unsigned long) jp->entry;
445 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
446 regs->tstate |= TSTATE_PIL;
447
448 return 1;
449}
450
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700451void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 __asm__ __volatile__(
454 ".globl jprobe_return_trap_instruction\n"
455"jprobe_return_trap_instruction:\n\t"
456 "ta 0x70");
457}
458
459extern void jprobe_return_trap_instruction(void);
460
461extern void __show_regs(struct pt_regs * regs);
462
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700463int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 u32 *addr = (u32 *) regs->tpc;
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800466 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (addr == (u32 *) jprobe_return_trap_instruction) {
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800469 if (kcb->jprobe_saved_regs_location != regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 printk("JPROBE: Current regs (%p) does not match "
471 "saved regs (%p).\n",
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800472 regs, kcb->jprobe_saved_regs_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk("JPROBE: Saved registers\n");
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800474 __show_regs(kcb->jprobe_saved_regs_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 printk("JPROBE: Current registers\n");
476 __show_regs(regs);
477 BUG();
478 }
479 /* Restore old register state. Do pt_regs
480 * first so that UREG_FP is the original one for
481 * the stack frame restore.
482 */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800483 memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800486 &(kcb->jprobe_saved_stack),
487 sizeof(kcb->jprobe_saved_stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800489 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 1;
491 }
492 return 0;
493}
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700494
Rusty Lynch67729262005-07-05 18:54:50 -0700495/* architecture specific initialization */
496int arch_init_kprobes(void)
497{
498 return 0;
499}