blob: 75f51bc2d801e7afac9979dcfab003505ae83fa0 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Kernel probes (kprobes) for SuperH
4 *
5 * Copyright (C) 2007 Chris Smith <chris.smith@st.com>
6 * Copyright (C) 2006 Lineo Solutions, Inc.
7 */
8#include <linux/kprobes.h>
9#include <linux/extable.h>
10#include <linux/ptrace.h>
11#include <linux/preempt.h>
12#include <linux/kdebug.h>
13#include <linux/slab.h>
14#include <asm/cacheflush.h>
15#include <linux/uaccess.h>
16
17DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
18DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
19
20static DEFINE_PER_CPU(struct kprobe, saved_current_opcode);
21static DEFINE_PER_CPU(struct kprobe, saved_next_opcode);
22static DEFINE_PER_CPU(struct kprobe, saved_next_opcode2);
23
24#define OPCODE_JMP(x) (((x) & 0xF0FF) == 0x402b)
25#define OPCODE_JSR(x) (((x) & 0xF0FF) == 0x400b)
26#define OPCODE_BRA(x) (((x) & 0xF000) == 0xa000)
27#define OPCODE_BRAF(x) (((x) & 0xF0FF) == 0x0023)
28#define OPCODE_BSR(x) (((x) & 0xF000) == 0xb000)
29#define OPCODE_BSRF(x) (((x) & 0xF0FF) == 0x0003)
30
31#define OPCODE_BF_S(x) (((x) & 0xFF00) == 0x8f00)
32#define OPCODE_BT_S(x) (((x) & 0xFF00) == 0x8d00)
33
34#define OPCODE_BF(x) (((x) & 0xFF00) == 0x8b00)
35#define OPCODE_BT(x) (((x) & 0xFF00) == 0x8900)
36
37#define OPCODE_RTS(x) (((x) & 0x000F) == 0x000b)
38#define OPCODE_RTE(x) (((x) & 0xFFFF) == 0x002b)
39
40int __kprobes arch_prepare_kprobe(struct kprobe *p)
41{
42 kprobe_opcode_t opcode = *(kprobe_opcode_t *) (p->addr);
43
44 if (OPCODE_RTE(opcode))
45 return -EFAULT; /* Bad breakpoint */
46
47 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
48 p->opcode = opcode;
49
50 return 0;
51}
52
53void __kprobes arch_arm_kprobe(struct kprobe *p)
54{
55 *p->addr = BREAKPOINT_INSTRUCTION;
56 flush_icache_range((unsigned long)p->addr,
57 (unsigned long)p->addr + sizeof(kprobe_opcode_t));
58}
59
60void __kprobes arch_disarm_kprobe(struct kprobe *p)
61{
62 *p->addr = p->opcode;
63 flush_icache_range((unsigned long)p->addr,
64 (unsigned long)p->addr + sizeof(kprobe_opcode_t));
65}
66
67int __kprobes arch_trampoline_kprobe(struct kprobe *p)
68{
69 if (*p->addr == BREAKPOINT_INSTRUCTION)
70 return 1;
71
72 return 0;
73}
74
75/**
76 * If an illegal slot instruction exception occurs for an address
77 * containing a kprobe, remove the probe.
78 *
79 * Returns 0 if the exception was handled successfully, 1 otherwise.
80 */
81int __kprobes kprobe_handle_illslot(unsigned long pc)
82{
83 struct kprobe *p = get_kprobe((kprobe_opcode_t *) pc + 1);
84
85 if (p != NULL) {
86 printk("Warning: removing kprobe from delay slot: 0x%.8x\n",
87 (unsigned int)pc + 2);
88 unregister_kprobe(p);
89 return 0;
90 }
91
92 return 1;
93}
94
95void __kprobes arch_remove_kprobe(struct kprobe *p)
96{
97 struct kprobe *saved = this_cpu_ptr(&saved_next_opcode);
98
99 if (saved->addr) {
100 arch_disarm_kprobe(p);
101 arch_disarm_kprobe(saved);
102
103 saved->addr = NULL;
104 saved->opcode = 0;
105
106 saved = this_cpu_ptr(&saved_next_opcode2);
107 if (saved->addr) {
108 arch_disarm_kprobe(saved);
109
110 saved->addr = NULL;
111 saved->opcode = 0;
112 }
113 }
114}
115
116static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
117{
118 kcb->prev_kprobe.kp = kprobe_running();
119 kcb->prev_kprobe.status = kcb->kprobe_status;
120}
121
122static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
123{
124 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
125 kcb->kprobe_status = kcb->prev_kprobe.status;
126}
127
128static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
129 struct kprobe_ctlblk *kcb)
130{
131 __this_cpu_write(current_kprobe, p);
132}
133
134/*
135 * Singlestep is implemented by disabling the current kprobe and setting one
136 * on the next instruction, following branches. Two probes are set if the
137 * branch is conditional.
138 */
139static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
140{
141 __this_cpu_write(saved_current_opcode.addr, (kprobe_opcode_t *)regs->pc);
142
143 if (p != NULL) {
144 struct kprobe *op1, *op2;
145
146 arch_disarm_kprobe(p);
147
148 op1 = this_cpu_ptr(&saved_next_opcode);
149 op2 = this_cpu_ptr(&saved_next_opcode2);
150
151 if (OPCODE_JSR(p->opcode) || OPCODE_JMP(p->opcode)) {
152 unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
153 op1->addr = (kprobe_opcode_t *) regs->regs[reg_nr];
154 } else if (OPCODE_BRA(p->opcode) || OPCODE_BSR(p->opcode)) {
155 unsigned long disp = (p->opcode & 0x0FFF);
156 op1->addr =
157 (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
158
159 } else if (OPCODE_BRAF(p->opcode) || OPCODE_BSRF(p->opcode)) {
160 unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
161 op1->addr =
162 (kprobe_opcode_t *) (regs->pc + 4 +
163 regs->regs[reg_nr]);
164
165 } else if (OPCODE_RTS(p->opcode)) {
166 op1->addr = (kprobe_opcode_t *) regs->pr;
167
168 } else if (OPCODE_BF(p->opcode) || OPCODE_BT(p->opcode)) {
169 unsigned long disp = (p->opcode & 0x00FF);
170 /* case 1 */
171 op1->addr = p->addr + 1;
172 /* case 2 */
173 op2->addr =
174 (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
175 op2->opcode = *(op2->addr);
176 arch_arm_kprobe(op2);
177
178 } else if (OPCODE_BF_S(p->opcode) || OPCODE_BT_S(p->opcode)) {
179 unsigned long disp = (p->opcode & 0x00FF);
180 /* case 1 */
181 op1->addr = p->addr + 2;
182 /* case 2 */
183 op2->addr =
184 (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
185 op2->opcode = *(op2->addr);
186 arch_arm_kprobe(op2);
187
188 } else {
189 op1->addr = p->addr + 1;
190 }
191
192 op1->opcode = *(op1->addr);
193 arch_arm_kprobe(op1);
194 }
195}
196
197/* Called with kretprobe_lock held */
198void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
199 struct pt_regs *regs)
200{
201 ri->ret_addr = (kprobe_opcode_t *) regs->pr;
202
203 /* Replace the return addr with trampoline addr */
204 regs->pr = (unsigned long)kretprobe_trampoline;
205}
206
207static int __kprobes kprobe_handler(struct pt_regs *regs)
208{
209 struct kprobe *p;
210 int ret = 0;
211 kprobe_opcode_t *addr = NULL;
212 struct kprobe_ctlblk *kcb;
213
214 /*
215 * We don't want to be preempted for the entire
216 * duration of kprobe processing
217 */
218 preempt_disable();
219 kcb = get_kprobe_ctlblk();
220
221 addr = (kprobe_opcode_t *) (regs->pc);
222
223 /* Check we're not actually recursing */
224 if (kprobe_running()) {
225 p = get_kprobe(addr);
226 if (p) {
227 if (kcb->kprobe_status == KPROBE_HIT_SS &&
228 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
229 goto no_kprobe;
230 }
231 /* We have reentered the kprobe_handler(), since
232 * another probe was hit while within the handler.
233 * We here save the original kprobes variables and
234 * just single step on the instruction of the new probe
235 * without calling any user handlers.
236 */
237 save_previous_kprobe(kcb);
238 set_current_kprobe(p, regs, kcb);
239 kprobes_inc_nmissed_count(p);
240 prepare_singlestep(p, regs);
241 kcb->kprobe_status = KPROBE_REENTER;
242 return 1;
243 }
244 goto no_kprobe;
245 }
246
247 p = get_kprobe(addr);
248 if (!p) {
249 /* Not one of ours: let kernel handle it */
250 if (*(kprobe_opcode_t *)addr != BREAKPOINT_INSTRUCTION) {
251 /*
252 * The breakpoint instruction was removed right
253 * after we hit it. Another cpu has removed
254 * either a probepoint or a debugger breakpoint
255 * at this address. In either case, no further
256 * handling of this interrupt is appropriate.
257 */
258 ret = 1;
259 }
260
261 goto no_kprobe;
262 }
263
264 set_current_kprobe(p, regs, kcb);
265 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
266
267 if (p->pre_handler && p->pre_handler(p, regs)) {
268 /* handler has already set things up, so skip ss setup */
269 reset_current_kprobe();
270 preempt_enable_no_resched();
271 return 1;
272 }
273
274 prepare_singlestep(p, regs);
275 kcb->kprobe_status = KPROBE_HIT_SS;
276 return 1;
277
278no_kprobe:
279 preempt_enable_no_resched();
280 return ret;
281}
282
283/*
284 * For function-return probes, init_kprobes() establishes a probepoint
285 * here. When a retprobed function returns, this probe is hit and
286 * trampoline_probe_handler() runs, calling the kretprobe's handler.
287 */
288static void __used kretprobe_trampoline_holder(void)
289{
290 asm volatile (".globl kretprobe_trampoline\n"
291 "kretprobe_trampoline:\n\t"
292 "nop\n");
293}
294
295/*
296 * Called when we hit the probe point at kretprobe_trampoline
297 */
298int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
299{
300 struct kretprobe_instance *ri = NULL;
301 struct hlist_head *head, empty_rp;
302 struct hlist_node *tmp;
303 unsigned long flags, orig_ret_address = 0;
304 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
305
306 INIT_HLIST_HEAD(&empty_rp);
307 kretprobe_hash_lock(current, &head, &flags);
308
309 /*
310 * It is possible to have multiple instances associated with a given
311 * task either because an multiple functions in the call path
312 * have a return probe installed on them, and/or more then one return
313 * return probe was registered for a target function.
314 *
315 * We can handle this because:
316 * - instances are always inserted at the head of the list
317 * - when multiple return probes are registered for the same
318 * function, the first instance's ret_addr will point to the
319 * real return address, and all the rest will point to
320 * kretprobe_trampoline
321 */
322 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
323 if (ri->task != current)
324 /* another task is sharing our hash bucket */
325 continue;
326
327 if (ri->rp && ri->rp->handler) {
328 __this_cpu_write(current_kprobe, &ri->rp->kp);
329 ri->rp->handler(ri, regs);
330 __this_cpu_write(current_kprobe, NULL);
331 }
332
333 orig_ret_address = (unsigned long)ri->ret_addr;
334 recycle_rp_inst(ri, &empty_rp);
335
336 if (orig_ret_address != trampoline_address)
337 /*
338 * This is the real return address. Any other
339 * instances associated with this task are for
340 * other calls deeper on the call stack
341 */
342 break;
343 }
344
345 kretprobe_assert(ri, orig_ret_address, trampoline_address);
346
347 regs->pc = orig_ret_address;
348 kretprobe_hash_unlock(current, &flags);
349
350 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
351 hlist_del(&ri->hlist);
352 kfree(ri);
353 }
354
355 return orig_ret_address;
356}
357
358static int __kprobes post_kprobe_handler(struct pt_regs *regs)
359{
360 struct kprobe *cur = kprobe_running();
361 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
362 kprobe_opcode_t *addr = NULL;
363 struct kprobe *p = NULL;
364
365 if (!cur)
366 return 0;
367
368 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
369 kcb->kprobe_status = KPROBE_HIT_SSDONE;
370 cur->post_handler(cur, regs, 0);
371 }
372
373 p = this_cpu_ptr(&saved_next_opcode);
374 if (p->addr) {
375 arch_disarm_kprobe(p);
376 p->addr = NULL;
377 p->opcode = 0;
378
379 addr = __this_cpu_read(saved_current_opcode.addr);
380 __this_cpu_write(saved_current_opcode.addr, NULL);
381
382 p = get_kprobe(addr);
383 arch_arm_kprobe(p);
384
385 p = this_cpu_ptr(&saved_next_opcode2);
386 if (p->addr) {
387 arch_disarm_kprobe(p);
388 p->addr = NULL;
389 p->opcode = 0;
390 }
391 }
392
393 /* Restore back the original saved kprobes variables and continue. */
394 if (kcb->kprobe_status == KPROBE_REENTER) {
395 restore_previous_kprobe(kcb);
396 goto out;
397 }
398
399 reset_current_kprobe();
400
401out:
402 preempt_enable_no_resched();
403
404 return 1;
405}
406
407int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
408{
409 struct kprobe *cur = kprobe_running();
410 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
411 const struct exception_table_entry *entry;
412
413 switch (kcb->kprobe_status) {
414 case KPROBE_HIT_SS:
415 case KPROBE_REENTER:
416 /*
417 * We are here because the instruction being single
418 * stepped caused a page fault. We reset the current
419 * kprobe, point the pc back to the probe address
420 * and allow the page fault handler to continue as a
421 * normal page fault.
422 */
423 regs->pc = (unsigned long)cur->addr;
424 if (kcb->kprobe_status == KPROBE_REENTER)
425 restore_previous_kprobe(kcb);
426 else
427 reset_current_kprobe();
428 preempt_enable_no_resched();
429 break;
430 case KPROBE_HIT_ACTIVE:
431 case KPROBE_HIT_SSDONE:
432 /*
433 * We increment the nmissed count for accounting,
434 * we can also use npre/npostfault count for accounting
435 * these specific fault cases.
436 */
437 kprobes_inc_nmissed_count(cur);
438
439 /*
440 * We come here because instructions in the pre/post
441 * handler caused the page_fault, this could happen
442 * if handler tries to access user space by
443 * copy_from_user(), get_user() etc. Let the
444 * user-specified handler try to fix it first.
445 */
446 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
447 return 1;
448
449 /*
450 * In case the user-specified fault handler returned
451 * zero, try to fix up.
452 */
453 if ((entry = search_exception_tables(regs->pc)) != NULL) {
454 regs->pc = entry->fixup;
455 return 1;
456 }
457
458 /*
459 * fixup_exception() could not handle it,
460 * Let do_page_fault() fix it.
461 */
462 break;
463 default:
464 break;
465 }
466
467 return 0;
468}
469
470/*
471 * Wrapper routine to for handling exceptions.
472 */
473int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
474 unsigned long val, void *data)
475{
476 struct kprobe *p = NULL;
477 struct die_args *args = (struct die_args *)data;
478 int ret = NOTIFY_DONE;
479 kprobe_opcode_t *addr = NULL;
480 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
481
482 addr = (kprobe_opcode_t *) (args->regs->pc);
483 if (val == DIE_TRAP &&
484 args->trapnr == (BREAKPOINT_INSTRUCTION & 0xff)) {
485 if (!kprobe_running()) {
486 if (kprobe_handler(args->regs)) {
487 ret = NOTIFY_STOP;
488 } else {
489 /* Not a kprobe trap */
490 ret = NOTIFY_DONE;
491 }
492 } else {
493 p = get_kprobe(addr);
494 if ((kcb->kprobe_status == KPROBE_HIT_SS) ||
495 (kcb->kprobe_status == KPROBE_REENTER)) {
496 if (post_kprobe_handler(args->regs))
497 ret = NOTIFY_STOP;
498 } else {
499 if (kprobe_handler(args->regs))
500 ret = NOTIFY_STOP;
501 }
502 }
503 }
504
505 return ret;
506}
507
508static struct kprobe trampoline_p = {
509 .addr = (kprobe_opcode_t *)&kretprobe_trampoline,
510 .pre_handler = trampoline_probe_handler
511};
512
513int __init arch_init_kprobes(void)
514{
515 return register_kprobe(&trampoline_p);
516}