1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kprobes.h>
15#include <linux/preempt.h>
16#include <linux/uaccess.h>
17#include <linux/kdebug.h>
18#include <linux/slab.h>
19
20#include <asm/ptrace.h>
21#include <asm/branch.h>
22#include <asm/break.h>
23
24#include "probes-common.h"
25
26static const union mips_instruction breakpoint_insn = {
27 .b_format = {
28 .opcode = spec_op,
29 .code = BRK_KPROBE_BP,
30 .func = break_op
31 }
32};
33
34static const union mips_instruction breakpoint2_insn = {
35 .b_format = {
36 .opcode = spec_op,
37 .code = BRK_KPROBE_SSTEPBP,
38 .func = break_op
39 }
40};
41
42DEFINE_PER_CPU(struct kprobe *, current_kprobe);
43DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
44
45static int __kprobes insn_has_delayslot(union mips_instruction insn)
46{
47 return __insn_has_delay_slot(insn);
48}
49
50
51
52
53
54
55
56
57static int __kprobes insn_has_ll_or_sc(union mips_instruction insn)
58{
59 int ret = 0;
60
61 switch (insn.i_format.opcode) {
62 case ll_op:
63 case lld_op:
64 case sc_op:
65 case scd_op:
66 ret = 1;
67 break;
68 default:
69 break;
70 }
71 return ret;
72}
73
74int __kprobes arch_prepare_kprobe(struct kprobe *p)
75{
76 union mips_instruction insn;
77 union mips_instruction prev_insn;
78 int ret = 0;
79
80 insn = p->addr[0];
81
82 if (insn_has_ll_or_sc(insn)) {
83 pr_notice("Kprobes for ll and sc instructions are not"
84 "supported\n");
85 ret = -EINVAL;
86 goto out;
87 }
88
89 if (copy_from_kernel_nofault(&prev_insn, p->addr - 1,
90 sizeof(mips_instruction)) == 0 &&
91 insn_has_delayslot(prev_insn)) {
92 pr_notice("Kprobes for branch delayslot are not supported\n");
93 ret = -EINVAL;
94 goto out;
95 }
96
97 if (__insn_is_compact_branch(insn)) {
98 pr_notice("Kprobes for compact branches are not supported\n");
99 ret = -EINVAL;
100 goto out;
101 }
102
103
104 p->ainsn.insn = get_insn_slot();
105 if (!p->ainsn.insn) {
106 ret = -ENOMEM;
107 goto out;
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 if (insn_has_delayslot(insn))
124 memcpy(&p->ainsn.insn[0], p->addr + 1, sizeof(kprobe_opcode_t));
125 else
126 memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
127
128 p->ainsn.insn[1] = breakpoint2_insn;
129 p->opcode = *p->addr;
130
131out:
132 return ret;
133}
134
135void __kprobes arch_arm_kprobe(struct kprobe *p)
136{
137 *p->addr = breakpoint_insn;
138 flush_insn_slot(p);
139}
140
141void __kprobes arch_disarm_kprobe(struct kprobe *p)
142{
143 *p->addr = p->opcode;
144 flush_insn_slot(p);
145}
146
147void __kprobes arch_remove_kprobe(struct kprobe *p)
148{
149 if (p->ainsn.insn) {
150 free_insn_slot(p->ainsn.insn, 0);
151 p->ainsn.insn = NULL;
152 }
153}
154
155static void save_previous_kprobe(struct kprobe_ctlblk *kcb)
156{
157 kcb->prev_kprobe.kp = kprobe_running();
158 kcb->prev_kprobe.status = kcb->kprobe_status;
159 kcb->prev_kprobe.old_SR = kcb->kprobe_old_SR;
160 kcb->prev_kprobe.saved_SR = kcb->kprobe_saved_SR;
161 kcb->prev_kprobe.saved_epc = kcb->kprobe_saved_epc;
162}
163
164static void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
165{
166 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
167 kcb->kprobe_status = kcb->prev_kprobe.status;
168 kcb->kprobe_old_SR = kcb->prev_kprobe.old_SR;
169 kcb->kprobe_saved_SR = kcb->prev_kprobe.saved_SR;
170 kcb->kprobe_saved_epc = kcb->prev_kprobe.saved_epc;
171}
172
173static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
174 struct kprobe_ctlblk *kcb)
175{
176 __this_cpu_write(current_kprobe, p);
177 kcb->kprobe_saved_SR = kcb->kprobe_old_SR = (regs->cp0_status & ST0_IE);
178 kcb->kprobe_saved_epc = regs->cp0_epc;
179}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194static int evaluate_branch_instruction(struct kprobe *p, struct pt_regs *regs,
195 struct kprobe_ctlblk *kcb)
196{
197 union mips_instruction insn = p->opcode;
198 long epc;
199 int ret = 0;
200
201 epc = regs->cp0_epc;
202 if (epc & 3)
203 goto unaligned;
204
205 if (p->ainsn.insn->word == 0)
206 kcb->flags |= SKIP_DELAYSLOT;
207 else
208 kcb->flags &= ~SKIP_DELAYSLOT;
209
210 ret = __compute_return_epc_for_insn(regs, insn);
211 if (ret < 0)
212 return ret;
213
214 if (ret == BRANCH_LIKELY_TAKEN)
215 kcb->flags |= SKIP_DELAYSLOT;
216
217 kcb->target_epc = regs->cp0_epc;
218
219 return 0;
220
221unaligned:
222 pr_notice("%s: unaligned epc - sending SIGBUS.\n", current->comm);
223 force_sig(SIGBUS);
224 return -EFAULT;
225
226}
227
228static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
229 struct kprobe_ctlblk *kcb)
230{
231 int ret = 0;
232
233 regs->cp0_status &= ~ST0_IE;
234
235
236 if (p->opcode.word == breakpoint_insn.word ||
237 p->opcode.word == breakpoint2_insn.word)
238 regs->cp0_epc = (unsigned long)p->addr;
239 else if (insn_has_delayslot(p->opcode)) {
240 ret = evaluate_branch_instruction(p, regs, kcb);
241 if (ret < 0) {
242 pr_notice("Kprobes: Error in evaluating branch\n");
243 return;
244 }
245 }
246 regs->cp0_epc = (unsigned long)&p->ainsn.insn[0];
247}
248
249
250
251
252
253
254
255
256
257
258
259
260
261static void __kprobes resume_execution(struct kprobe *p,
262 struct pt_regs *regs,
263 struct kprobe_ctlblk *kcb)
264{
265 if (insn_has_delayslot(p->opcode))
266 regs->cp0_epc = kcb->target_epc;
267 else {
268 unsigned long orig_epc = kcb->kprobe_saved_epc;
269 regs->cp0_epc = orig_epc + 4;
270 }
271}
272
273static int __kprobes kprobe_handler(struct pt_regs *regs)
274{
275 struct kprobe *p;
276 int ret = 0;
277 kprobe_opcode_t *addr;
278 struct kprobe_ctlblk *kcb;
279
280 addr = (kprobe_opcode_t *) regs->cp0_epc;
281
282
283
284
285
286 preempt_disable();
287 kcb = get_kprobe_ctlblk();
288
289
290 if (kprobe_running()) {
291 p = get_kprobe(addr);
292 if (p) {
293 if (kcb->kprobe_status == KPROBE_HIT_SS &&
294 p->ainsn.insn->word == breakpoint_insn.word) {
295 regs->cp0_status &= ~ST0_IE;
296 regs->cp0_status |= kcb->kprobe_saved_SR;
297 goto no_kprobe;
298 }
299
300
301
302
303
304
305
306 save_previous_kprobe(kcb);
307 set_current_kprobe(p, regs, kcb);
308 kprobes_inc_nmissed_count(p);
309 prepare_singlestep(p, regs, kcb);
310 kcb->kprobe_status = KPROBE_REENTER;
311 if (kcb->flags & SKIP_DELAYSLOT) {
312 resume_execution(p, regs, kcb);
313 restore_previous_kprobe(kcb);
314 preempt_enable_no_resched();
315 }
316 return 1;
317 } else if (addr->word != breakpoint_insn.word) {
318
319
320
321
322
323 ret = 1;
324 }
325 goto no_kprobe;
326 }
327
328 p = get_kprobe(addr);
329 if (!p) {
330 if (addr->word != breakpoint_insn.word) {
331
332
333
334
335
336
337
338 ret = 1;
339 }
340
341 goto no_kprobe;
342 }
343
344 set_current_kprobe(p, regs, kcb);
345 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
346
347 if (p->pre_handler && p->pre_handler(p, regs)) {
348
349 reset_current_kprobe();
350 preempt_enable_no_resched();
351 return 1;
352 }
353
354 prepare_singlestep(p, regs, kcb);
355 if (kcb->flags & SKIP_DELAYSLOT) {
356 kcb->kprobe_status = KPROBE_HIT_SSDONE;
357 if (p->post_handler)
358 p->post_handler(p, regs, 0);
359 resume_execution(p, regs, kcb);
360 preempt_enable_no_resched();
361 } else
362 kcb->kprobe_status = KPROBE_HIT_SS;
363
364 return 1;
365
366no_kprobe:
367 preempt_enable_no_resched();
368 return ret;
369
370}
371
372static inline int post_kprobe_handler(struct pt_regs *regs)
373{
374 struct kprobe *cur = kprobe_running();
375 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
376
377 if (!cur)
378 return 0;
379
380 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
381 kcb->kprobe_status = KPROBE_HIT_SSDONE;
382 cur->post_handler(cur, regs, 0);
383 }
384
385 resume_execution(cur, regs, kcb);
386
387 regs->cp0_status |= kcb->kprobe_saved_SR;
388
389
390 if (kcb->kprobe_status == KPROBE_REENTER) {
391 restore_previous_kprobe(kcb);
392 goto out;
393 }
394 reset_current_kprobe();
395out:
396 preempt_enable_no_resched();
397
398 return 1;
399}
400
401int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
402{
403 struct kprobe *cur = kprobe_running();
404 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
405
406 if (kcb->kprobe_status & KPROBE_HIT_SS) {
407 resume_execution(cur, regs, kcb);
408 regs->cp0_status |= kcb->kprobe_old_SR;
409
410 reset_current_kprobe();
411 preempt_enable_no_resched();
412 }
413 return 0;
414}
415
416
417
418
419int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
420 unsigned long val, void *data)
421{
422
423 struct die_args *args = (struct die_args *)data;
424 int ret = NOTIFY_DONE;
425
426 switch (val) {
427 case DIE_BREAK:
428 if (kprobe_handler(args->regs))
429 ret = NOTIFY_STOP;
430 break;
431 case DIE_SSTEPBP:
432 if (post_kprobe_handler(args->regs))
433 ret = NOTIFY_STOP;
434 break;
435
436 case DIE_PAGE_FAULT:
437
438 preempt_disable();
439
440 if (kprobe_running()
441 && kprobe_fault_handler(args->regs, args->trapnr))
442 ret = NOTIFY_STOP;
443 preempt_enable();
444 break;
445 default:
446 break;
447 }
448 return ret;
449}
450
451
452
453
454
455
456
457static void __used kretprobe_trampoline_holder(void)
458{
459 asm volatile(
460 ".set push\n\t"
461
462 ".set noreorder\n\t"
463 "nop\n\t"
464 ".global kretprobe_trampoline\n"
465 "kretprobe_trampoline:\n\t"
466 "nop\n\t"
467 ".set pop"
468 : : : "memory");
469}
470
471void kretprobe_trampoline(void);
472
473void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
474 struct pt_regs *regs)
475{
476 ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
477 ri->fp = NULL;
478
479
480 regs->regs[31] = (unsigned long)kretprobe_trampoline;
481}
482
483
484
485
486static int __kprobes trampoline_probe_handler(struct kprobe *p,
487 struct pt_regs *regs)
488{
489 instruction_pointer(regs) = __kretprobe_trampoline_handler(regs,
490 kretprobe_trampoline, NULL);
491
492
493
494
495
496 return 1;
497}
498
499int __kprobes arch_trampoline_kprobe(struct kprobe *p)
500{
501 if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
502 return 1;
503
504 return 0;
505}
506
507static struct kprobe trampoline_p = {
508 .addr = (kprobe_opcode_t *)kretprobe_trampoline,
509 .pre_handler = trampoline_probe_handler
510};
511
512int __init arch_init_kprobes(void)
513{
514 return register_kprobe(&trampoline_p);
515}
516