1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kdebug.h>
3 #include <linux/kprobes.h>
4 #include <linux/preempt.h>
7 #define KPROBE_BP_INSN larch_insn_gen_break(BRK_KPROBE_BP)
8 #define KPROBE_SSTEPBP_INSN larch_insn_gen_break(BRK_KPROBE_SSTEPBP)
10 DEFINE_PER_CPU(struct kprobe *, current_kprobe);
11 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
13 static void arch_prepare_ss_slot(struct kprobe *p)
15 p->ainsn.insn[0] = *p->addr;
16 p->ainsn.insn[1] = KPROBE_SSTEPBP_INSN;
17 p->ainsn.restore = (unsigned long)p->addr + LOONGARCH_INSN_SIZE;
19 NOKPROBE_SYMBOL(arch_prepare_ss_slot);
21 static void arch_prepare_simulate(struct kprobe *p)
25 NOKPROBE_SYMBOL(arch_prepare_simulate);
27 int arch_prepare_kprobe(struct kprobe *p)
29 union loongarch_instruction insn;
31 if ((unsigned long)p->addr & 0x3)
34 /* copy instruction */
36 insn.word = p->opcode;
38 /* decode instruction */
39 if (insns_not_supported(insn))
42 if (insns_need_simulation(insn)) {
45 p->ainsn.insn = get_insn_slot();
50 /* prepare the instruction */
52 arch_prepare_ss_slot(p);
54 arch_prepare_simulate(p);
58 NOKPROBE_SYMBOL(arch_prepare_kprobe);
60 /* Install breakpoint in text */
61 void arch_arm_kprobe(struct kprobe *p)
63 *p->addr = KPROBE_BP_INSN;
66 NOKPROBE_SYMBOL(arch_arm_kprobe);
68 /* Remove breakpoint from text */
69 void arch_disarm_kprobe(struct kprobe *p)
74 NOKPROBE_SYMBOL(arch_disarm_kprobe);
76 void arch_remove_kprobe(struct kprobe *p)
79 free_insn_slot(p->ainsn.insn, 0);
83 NOKPROBE_SYMBOL(arch_remove_kprobe);
85 static void save_previous_kprobe(struct kprobe_ctlblk *kcb)
87 kcb->prev_kprobe.kp = kprobe_running();
88 kcb->prev_kprobe.status = kcb->kprobe_status;
90 NOKPROBE_SYMBOL(save_previous_kprobe);
92 static void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
94 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
95 kcb->kprobe_status = kcb->prev_kprobe.status;
97 NOKPROBE_SYMBOL(restore_previous_kprobe);
99 static void set_current_kprobe(struct kprobe *p)
101 __this_cpu_write(current_kprobe, p);
103 NOKPROBE_SYMBOL(set_current_kprobe);
106 * Interrupts need to be disabled before single-step mode is set,
107 * and not reenabled until after single-step mode ends.
108 * Without disabling interrupt on local CPU, there is a chance of
109 * interrupt occurrence in the period of exception return and start
110 * of out-of-line single-step, that result in wrongly single stepping
111 * into the interrupt handler.
113 static void save_local_irqflag(struct kprobe_ctlblk *kcb,
114 struct pt_regs *regs)
116 kcb->saved_status = regs->csr_prmd;
117 regs->csr_prmd &= ~CSR_PRMD_PIE;
119 NOKPROBE_SYMBOL(save_local_irqflag);
121 static void restore_local_irqflag(struct kprobe_ctlblk *kcb,
122 struct pt_regs *regs)
124 regs->csr_prmd = kcb->saved_status;
126 NOKPROBE_SYMBOL(restore_local_irqflag);
128 static void post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb,
129 struct pt_regs *regs)
131 /* return addr restore if non-branching insn */
132 if (cur->ainsn.restore != 0)
133 instruction_pointer_set(regs, cur->ainsn.restore);
135 /* restore back original saved kprobe variables and continue */
136 if (kcb->kprobe_status == KPROBE_REENTER) {
137 restore_previous_kprobe(kcb);
138 preempt_enable_no_resched();
143 * update the kcb status even if the cur->post_handler is
144 * not set because reset_curent_kprobe() doesn't update kcb.
146 kcb->kprobe_status = KPROBE_HIT_SSDONE;
147 if (cur->post_handler)
148 cur->post_handler(cur, regs, 0);
150 reset_current_kprobe();
151 preempt_enable_no_resched();
153 NOKPROBE_SYMBOL(post_kprobe_handler);
155 static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
156 struct kprobe_ctlblk *kcb, int reenter)
158 union loongarch_instruction insn;
161 save_previous_kprobe(kcb);
162 set_current_kprobe(p);
163 kcb->kprobe_status = KPROBE_REENTER;
165 kcb->kprobe_status = KPROBE_HIT_SS;
169 /* IRQs and single stepping do not mix well */
170 save_local_irqflag(kcb, regs);
171 /* set ip register to prepare for single stepping */
172 regs->csr_era = (unsigned long)p->ainsn.insn;
174 /* simulate single steping */
175 insn.word = p->opcode;
176 arch_simulate_insn(insn, regs);
177 /* now go for post processing */
178 post_kprobe_handler(p, kcb, regs);
181 NOKPROBE_SYMBOL(setup_singlestep);
183 static bool reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
184 struct kprobe_ctlblk *kcb)
186 switch (kcb->kprobe_status) {
188 case KPROBE_HIT_SSDONE:
189 case KPROBE_HIT_ACTIVE:
190 kprobes_inc_nmissed_count(p);
191 setup_singlestep(p, regs, kcb, 1);
194 pr_warn("Failed to recover from reentered kprobes.\n");
205 NOKPROBE_SYMBOL(reenter_kprobe);
207 bool kprobe_breakpoint_handler(struct pt_regs *regs)
209 struct kprobe_ctlblk *kcb;
210 struct kprobe *p, *cur_kprobe;
211 kprobe_opcode_t *addr = (kprobe_opcode_t *)regs->csr_era;
214 * We don't want to be preempted for the entire
215 * duration of kprobe processing.
218 kcb = get_kprobe_ctlblk();
219 cur_kprobe = kprobe_running();
221 p = get_kprobe(addr);
224 if (reenter_kprobe(p, regs, kcb))
228 set_current_kprobe(p);
229 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
232 * If we have no pre-handler or it returned 0, we
233 * continue with normal processing. If we have a
234 * pre-handler and it returned non-zero, it will
235 * modify the execution path and no need to single
236 * stepping. Let's just reset current kprobe and exit.
238 * pre_handler can hit a breakpoint and can step thru
241 if (!p->pre_handler || !p->pre_handler(p, regs)) {
242 setup_singlestep(p, regs, kcb, 0);
244 reset_current_kprobe();
245 preempt_enable_no_resched();
251 if (*addr != KPROBE_BP_INSN) {
253 * The breakpoint instruction was removed right
254 * after we hit it. Another cpu has removed
255 * either a probepoint or a debugger breakpoint
256 * at this address. In either case, no further
257 * handling of this interrupt is appropriate.
258 * Return back to original instruction, and continue.
260 regs->csr_era = (unsigned long)addr;
261 preempt_enable_no_resched();
265 preempt_enable_no_resched();
268 NOKPROBE_SYMBOL(kprobe_breakpoint_handler);
270 bool kprobe_singlestep_handler(struct pt_regs *regs)
272 struct kprobe *cur = kprobe_running();
273 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
274 unsigned long addr = instruction_pointer(regs);
276 if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
277 ((unsigned long)&cur->ainsn.insn[1] == addr)) {
278 restore_local_irqflag(kcb, regs);
279 post_kprobe_handler(cur, kcb, regs);
283 preempt_enable_no_resched();
286 NOKPROBE_SYMBOL(kprobe_singlestep_handler);
288 bool kprobe_fault_handler(struct pt_regs *regs, int trapnr)
290 struct kprobe *cur = kprobe_running();
291 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
293 switch (kcb->kprobe_status) {
297 * We are here because the instruction being single
298 * stepped caused a page fault. We reset the current
299 * kprobe and the ip points back to the probe address
300 * and allow the page fault handler to continue as a
303 regs->csr_era = (unsigned long)cur->addr;
304 WARN_ON_ONCE(!instruction_pointer(regs));
306 if (kcb->kprobe_status == KPROBE_REENTER) {
307 restore_previous_kprobe(kcb);
309 restore_local_irqflag(kcb, regs);
310 reset_current_kprobe();
312 preempt_enable_no_resched();
317 NOKPROBE_SYMBOL(kprobe_fault_handler);
320 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
321 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
323 int __init arch_populate_kprobe_blacklist(void)
325 return kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
326 (unsigned long)__irqentry_text_end);
329 int __init arch_init_kprobes(void)
334 int arch_trampoline_kprobe(struct kprobe *p)
338 NOKPROBE_SYMBOL(arch_trampoline_kprobe);