Skip to content

Commit c22b0bc

Browse files
guoren83palmer-dabbelt
authored andcommittedJan 14, 2021
riscv: Add kprobes supported
This patch enables "kprobe & kretprobe" to work with ftrace interface. It utilized software breakpoint as single-step mechanism. Some instructions which can't be single-step executed must be simulated in kernel execution slot, such as: branch, jal, auipc, la ... Some instructions should be rejected for probing and we use a blacklist to filter, such as: ecall, ebreak, ... We use ebreak & c.ebreak to replace origin instruction and the kprobe handler prepares an executable memory slot for out-of-line execution with a copy of the original instruction being probed. In execution slot we add ebreak behind original instruction to simulate a single-setp mechanism. The patch is based on packi's work [1] and csky's work [2]. - The kprobes_trampoline.S is all from packi's patch - The single-step mechanism is new designed for riscv without hw single-step trap - The simulation codes are from csky - Frankly, all codes refer to other archs' implementation [1] https://lore.kernel.org/linux-riscv/20181113195804.22825-1-me@packi.ch/ [2] https://lore.kernel.org/linux-csky/20200403044150.20562-9-guoren@kernel.org/ Signed-off-by: Guo Ren <guoren@linux.alibaba.com> Co-developed-by: Patrick Stählin <me@packi.ch> Signed-off-by: Patrick Stählin <me@packi.ch> Acked-by: Masami Hiramatsu <mhiramat@kernel.org> Tested-by: Zong Li <zong.li@sifive.com> Reviewed-by: Pekka Enberg <penberg@kernel.org> Cc: Patrick Stählin <me@packi.ch> Cc: Palmer Dabbelt <palmerdabbelt@google.com> Cc: Björn Töpel <bjorn.topel@gmail.com> Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
1 parent afc76b8 commit c22b0bc

13 files changed

+773
-0
lines changed
 

‎arch/riscv/Kconfig

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ config RISCV
7171
select HAVE_GCC_PLUGINS
7272
select HAVE_GENERIC_VDSO if MMU && 64BIT
7373
select HAVE_IRQ_TIME_ACCOUNTING
74+
select HAVE_KPROBES
75+
select HAVE_KRETPROBES
7476
select HAVE_PCI
7577
select HAVE_PERF_EVENTS
7678
select HAVE_PERF_REGS

‎arch/riscv/include/asm/kprobes.h

+40
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,44 @@
1111

1212
#include <asm-generic/kprobes.h>
1313

14+
#ifdef CONFIG_KPROBES
15+
#include <linux/types.h>
16+
#include <linux/ptrace.h>
17+
#include <linux/percpu.h>
18+
19+
#define __ARCH_WANT_KPROBES_INSN_SLOT
20+
#define MAX_INSN_SIZE 2
21+
22+
#define flush_insn_slot(p) do { } while (0)
23+
#define kretprobe_blacklist_size 0
24+
25+
#include <asm/probes.h>
26+
27+
struct prev_kprobe {
28+
struct kprobe *kp;
29+
unsigned int status;
30+
};
31+
32+
/* Single step context for kprobe */
33+
struct kprobe_step_ctx {
34+
unsigned long ss_pending;
35+
unsigned long match_addr;
36+
};
37+
38+
/* per-cpu kprobe control block */
39+
struct kprobe_ctlblk {
40+
unsigned int kprobe_status;
41+
unsigned long saved_status;
42+
struct prev_kprobe prev_kprobe;
43+
struct kprobe_step_ctx ss_ctx;
44+
};
45+
46+
void arch_remove_kprobe(struct kprobe *p);
47+
int kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr);
48+
bool kprobe_breakpoint_handler(struct pt_regs *regs);
49+
bool kprobe_single_step_handler(struct pt_regs *regs);
50+
void kretprobe_trampoline(void);
51+
void __kprobes *trampoline_probe_handler(struct pt_regs *regs);
52+
53+
#endif /* CONFIG_KPROBES */
1454
#endif /* _ASM_RISCV_KPROBES_H */

‎arch/riscv/include/asm/probes.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef _ASM_RISCV_PROBES_H
4+
#define _ASM_RISCV_PROBES_H
5+
6+
typedef u32 probe_opcode_t;
7+
typedef bool (probes_handler_t) (u32 opcode, unsigned long addr, struct pt_regs *);
8+
9+
/* architecture specific copy of original instruction */
10+
struct arch_probe_insn {
11+
probe_opcode_t *insn;
12+
probes_handler_t *handler;
13+
/* restore address after simulation */
14+
unsigned long restore;
15+
};
16+
17+
#ifdef CONFIG_KPROBES
18+
typedef u32 kprobe_opcode_t;
19+
struct arch_specific_insn {
20+
struct arch_probe_insn api;
21+
};
22+
#endif
23+
24+
#endif /* _ASM_RISCV_PROBES_H */

‎arch/riscv/kernel/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ obj-y += riscv_ksyms.o
3030
obj-y += stacktrace.o
3131
obj-y += cacheinfo.o
3232
obj-y += patch.o
33+
obj-y += probes/
3334
obj-$(CONFIG_MMU) += vdso.o vdso/
3435

3536
obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o

‎arch/riscv/kernel/probes/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o simulate-insn.o
3+
obj-$(CONFIG_KPROBES) += kprobes_trampoline.o
4+
CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE)
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
#include <linux/kernel.h>
4+
#include <linux/kprobes.h>
5+
#include <linux/module.h>
6+
#include <linux/kallsyms.h>
7+
#include <asm/sections.h>
8+
9+
#include "decode-insn.h"
10+
#include "simulate-insn.h"
11+
12+
/* Return:
13+
* INSN_REJECTED If instruction is one not allowed to kprobe,
14+
* INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
15+
*/
16+
enum probe_insn __kprobes
17+
riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
18+
{
19+
probe_opcode_t insn = le32_to_cpu(*addr);
20+
21+
/*
22+
* Reject instructions list:
23+
*/
24+
RISCV_INSN_REJECTED(system, insn);
25+
RISCV_INSN_REJECTED(fence, insn);
26+
27+
/*
28+
* Simulate instructions list:
29+
* TODO: the REJECTED ones below need to be implemented
30+
*/
31+
#ifdef CONFIG_RISCV_ISA_C
32+
RISCV_INSN_REJECTED(c_j, insn);
33+
RISCV_INSN_REJECTED(c_jr, insn);
34+
RISCV_INSN_REJECTED(c_jal, insn);
35+
RISCV_INSN_REJECTED(c_jalr, insn);
36+
RISCV_INSN_REJECTED(c_beqz, insn);
37+
RISCV_INSN_REJECTED(c_bnez, insn);
38+
RISCV_INSN_REJECTED(c_ebreak, insn);
39+
#endif
40+
41+
RISCV_INSN_REJECTED(auipc, insn);
42+
RISCV_INSN_REJECTED(branch, insn);
43+
44+
RISCV_INSN_SET_SIMULATE(jal, insn);
45+
RISCV_INSN_SET_SIMULATE(jalr, insn);
46+
47+
return INSN_GOOD;
48+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
3+
#ifndef _RISCV_KERNEL_KPROBES_DECODE_INSN_H
4+
#define _RISCV_KERNEL_KPROBES_DECODE_INSN_H
5+
6+
#include <asm/sections.h>
7+
#include <asm/kprobes.h>
8+
9+
enum probe_insn {
10+
INSN_REJECTED,
11+
INSN_GOOD_NO_SLOT,
12+
INSN_GOOD,
13+
};
14+
15+
enum probe_insn __kprobes
16+
riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *asi);
17+
18+
#endif /* _RISCV_KERNEL_KPROBES_DECODE_INSN_H */

‎arch/riscv/kernel/probes/kprobes.c

+398
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,398 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
#include <linux/kprobes.h>
4+
#include <linux/extable.h>
5+
#include <linux/slab.h>
6+
#include <linux/stop_machine.h>
7+
#include <asm/ptrace.h>
8+
#include <linux/uaccess.h>
9+
#include <asm/sections.h>
10+
#include <asm/cacheflush.h>
11+
#include <asm/bug.h>
12+
#include <asm/patch.h>
13+
14+
#include "decode-insn.h"
15+
16+
DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
17+
DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
18+
19+
static void __kprobes
20+
post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
21+
22+
static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
23+
{
24+
unsigned long offset = GET_INSN_LENGTH(p->opcode);
25+
26+
p->ainsn.api.restore = (unsigned long)p->addr + offset;
27+
28+
patch_text(p->ainsn.api.insn, p->opcode);
29+
patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
30+
__BUG_INSN_32);
31+
}
32+
33+
static void __kprobes arch_prepare_simulate(struct kprobe *p)
34+
{
35+
p->ainsn.api.restore = 0;
36+
}
37+
38+
static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
39+
{
40+
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
41+
42+
if (p->ainsn.api.handler)
43+
p->ainsn.api.handler((u32)p->opcode,
44+
(unsigned long)p->addr, regs);
45+
46+
post_kprobe_handler(kcb, regs);
47+
}
48+
49+
int __kprobes arch_prepare_kprobe(struct kprobe *p)
50+
{
51+
unsigned long probe_addr = (unsigned long)p->addr;
52+
53+
if (probe_addr & 0x1) {
54+
pr_warn("Address not aligned.\n");
55+
56+
return -EINVAL;
57+
}
58+
59+
/* copy instruction */
60+
p->opcode = le32_to_cpu(*p->addr);
61+
62+
/* decode instruction */
63+
switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
64+
case INSN_REJECTED: /* insn not supported */
65+
return -EINVAL;
66+
67+
case INSN_GOOD_NO_SLOT: /* insn need simulation */
68+
p->ainsn.api.insn = NULL;
69+
break;
70+
71+
case INSN_GOOD: /* instruction uses slot */
72+
p->ainsn.api.insn = get_insn_slot();
73+
if (!p->ainsn.api.insn)
74+
return -ENOMEM;
75+
break;
76+
}
77+
78+
/* prepare the instruction */
79+
if (p->ainsn.api.insn)
80+
arch_prepare_ss_slot(p);
81+
else
82+
arch_prepare_simulate(p);
83+
84+
return 0;
85+
}
86+
87+
/* install breakpoint in text */
88+
void __kprobes arch_arm_kprobe(struct kprobe *p)
89+
{
90+
if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
91+
patch_text(p->addr, __BUG_INSN_32);
92+
else
93+
patch_text(p->addr, __BUG_INSN_16);
94+
}
95+
96+
/* remove breakpoint from text */
97+
void __kprobes arch_disarm_kprobe(struct kprobe *p)
98+
{
99+
patch_text(p->addr, p->opcode);
100+
}
101+
102+
void __kprobes arch_remove_kprobe(struct kprobe *p)
103+
{
104+
}
105+
106+
static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
107+
{
108+
kcb->prev_kprobe.kp = kprobe_running();
109+
kcb->prev_kprobe.status = kcb->kprobe_status;
110+
}
111+
112+
static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
113+
{
114+
__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
115+
kcb->kprobe_status = kcb->prev_kprobe.status;
116+
}
117+
118+
static void __kprobes set_current_kprobe(struct kprobe *p)
119+
{
120+
__this_cpu_write(current_kprobe, p);
121+
}
122+
123+
/*
124+
* Interrupts need to be disabled before single-step mode is set, and not
125+
* reenabled until after single-step mode ends.
126+
* Without disabling interrupt on local CPU, there is a chance of
127+
* interrupt occurrence in the period of exception return and start of
128+
* out-of-line single-step, that result in wrongly single stepping
129+
* into the interrupt handler.
130+
*/
131+
static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
132+
struct pt_regs *regs)
133+
{
134+
kcb->saved_status = regs->status;
135+
regs->status &= ~SR_SPIE;
136+
}
137+
138+
static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
139+
struct pt_regs *regs)
140+
{
141+
regs->status = kcb->saved_status;
142+
}
143+
144+
static void __kprobes
145+
set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p)
146+
{
147+
unsigned long offset = GET_INSN_LENGTH(p->opcode);
148+
149+
kcb->ss_ctx.ss_pending = true;
150+
kcb->ss_ctx.match_addr = addr + offset;
151+
}
152+
153+
static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
154+
{
155+
kcb->ss_ctx.ss_pending = false;
156+
kcb->ss_ctx.match_addr = 0;
157+
}
158+
159+
static void __kprobes setup_singlestep(struct kprobe *p,
160+
struct pt_regs *regs,
161+
struct kprobe_ctlblk *kcb, int reenter)
162+
{
163+
unsigned long slot;
164+
165+
if (reenter) {
166+
save_previous_kprobe(kcb);
167+
set_current_kprobe(p);
168+
kcb->kprobe_status = KPROBE_REENTER;
169+
} else {
170+
kcb->kprobe_status = KPROBE_HIT_SS;
171+
}
172+
173+
if (p->ainsn.api.insn) {
174+
/* prepare for single stepping */
175+
slot = (unsigned long)p->ainsn.api.insn;
176+
177+
set_ss_context(kcb, slot, p); /* mark pending ss */
178+
179+
/* IRQs and single stepping do not mix well. */
180+
kprobes_save_local_irqflag(kcb, regs);
181+
182+
instruction_pointer_set(regs, slot);
183+
} else {
184+
/* insn simulation */
185+
arch_simulate_insn(p, regs);
186+
}
187+
}
188+
189+
static int __kprobes reenter_kprobe(struct kprobe *p,
190+
struct pt_regs *regs,
191+
struct kprobe_ctlblk *kcb)
192+
{
193+
switch (kcb->kprobe_status) {
194+
case KPROBE_HIT_SSDONE:
195+
case KPROBE_HIT_ACTIVE:
196+
kprobes_inc_nmissed_count(p);
197+
setup_singlestep(p, regs, kcb, 1);
198+
break;
199+
case KPROBE_HIT_SS:
200+
case KPROBE_REENTER:
201+
pr_warn("Unrecoverable kprobe detected.\n");
202+
dump_kprobe(p);
203+
BUG();
204+
break;
205+
default:
206+
WARN_ON(1);
207+
return 0;
208+
}
209+
210+
return 1;
211+
}
212+
213+
static void __kprobes
214+
post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
215+
{
216+
struct kprobe *cur = kprobe_running();
217+
218+
if (!cur)
219+
return;
220+
221+
/* return addr restore if non-branching insn */
222+
if (cur->ainsn.api.restore != 0)
223+
regs->epc = cur->ainsn.api.restore;
224+
225+
/* restore back original saved kprobe variables and continue */
226+
if (kcb->kprobe_status == KPROBE_REENTER) {
227+
restore_previous_kprobe(kcb);
228+
return;
229+
}
230+
231+
/* call post handler */
232+
kcb->kprobe_status = KPROBE_HIT_SSDONE;
233+
if (cur->post_handler) {
234+
/* post_handler can hit breakpoint and single step
235+
* again, so we enable D-flag for recursive exception.
236+
*/
237+
cur->post_handler(cur, regs, 0);
238+
}
239+
240+
reset_current_kprobe();
241+
}
242+
243+
int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
244+
{
245+
struct kprobe *cur = kprobe_running();
246+
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
247+
248+
switch (kcb->kprobe_status) {
249+
case KPROBE_HIT_SS:
250+
case KPROBE_REENTER:
251+
/*
252+
* We are here because the instruction being single
253+
* stepped caused a page fault. We reset the current
254+
* kprobe and the ip points back to the probe address
255+
* and allow the page fault handler to continue as a
256+
* normal page fault.
257+
*/
258+
regs->epc = (unsigned long) cur->addr;
259+
if (!instruction_pointer(regs))
260+
BUG();
261+
262+
if (kcb->kprobe_status == KPROBE_REENTER)
263+
restore_previous_kprobe(kcb);
264+
else
265+
reset_current_kprobe();
266+
267+
break;
268+
case KPROBE_HIT_ACTIVE:
269+
case KPROBE_HIT_SSDONE:
270+
/*
271+
* We increment the nmissed count for accounting,
272+
* we can also use npre/npostfault count for accounting
273+
* these specific fault cases.
274+
*/
275+
kprobes_inc_nmissed_count(cur);
276+
277+
/*
278+
* We come here because instructions in the pre/post
279+
* handler caused the page_fault, this could happen
280+
* if handler tries to access user space by
281+
* copy_from_user(), get_user() etc. Let the
282+
* user-specified handler try to fix it first.
283+
*/
284+
if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
285+
return 1;
286+
287+
/*
288+
* In case the user-specified fault handler returned
289+
* zero, try to fix up.
290+
*/
291+
if (fixup_exception(regs))
292+
return 1;
293+
}
294+
return 0;
295+
}
296+
297+
bool __kprobes
298+
kprobe_breakpoint_handler(struct pt_regs *regs)
299+
{
300+
struct kprobe *p, *cur_kprobe;
301+
struct kprobe_ctlblk *kcb;
302+
unsigned long addr = instruction_pointer(regs);
303+
304+
kcb = get_kprobe_ctlblk();
305+
cur_kprobe = kprobe_running();
306+
307+
p = get_kprobe((kprobe_opcode_t *) addr);
308+
309+
if (p) {
310+
if (cur_kprobe) {
311+
if (reenter_kprobe(p, regs, kcb))
312+
return true;
313+
} else {
314+
/* Probe hit */
315+
set_current_kprobe(p);
316+
kcb->kprobe_status = KPROBE_HIT_ACTIVE;
317+
318+
/*
319+
* If we have no pre-handler or it returned 0, we
320+
* continue with normal processing. If we have a
321+
* pre-handler and it returned non-zero, it will
322+
* modify the execution path and no need to single
323+
* stepping. Let's just reset current kprobe and exit.
324+
*
325+
* pre_handler can hit a breakpoint and can step thru
326+
* before return.
327+
*/
328+
if (!p->pre_handler || !p->pre_handler(p, regs))
329+
setup_singlestep(p, regs, kcb, 0);
330+
else
331+
reset_current_kprobe();
332+
}
333+
return true;
334+
}
335+
336+
/*
337+
* The breakpoint instruction was removed right
338+
* after we hit it. Another cpu has removed
339+
* either a probepoint or a debugger breakpoint
340+
* at this address. In either case, no further
341+
* handling of this interrupt is appropriate.
342+
* Return back to original instruction, and continue.
343+
*/
344+
return false;
345+
}
346+
347+
bool __kprobes
348+
kprobe_single_step_handler(struct pt_regs *regs)
349+
{
350+
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
351+
352+
if ((kcb->ss_ctx.ss_pending)
353+
&& (kcb->ss_ctx.match_addr == instruction_pointer(regs))) {
354+
clear_ss_context(kcb); /* clear pending ss */
355+
356+
kprobes_restore_local_irqflag(kcb, regs);
357+
358+
post_kprobe_handler(kcb, regs);
359+
return true;
360+
}
361+
return false;
362+
}
363+
364+
/*
365+
* Provide a blacklist of symbols identifying ranges which cannot be kprobed.
366+
* This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
367+
*/
368+
int __init arch_populate_kprobe_blacklist(void)
369+
{
370+
int ret;
371+
372+
ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
373+
(unsigned long)__irqentry_text_end);
374+
return ret;
375+
}
376+
377+
void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
378+
{
379+
return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
380+
}
381+
382+
void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
383+
struct pt_regs *regs)
384+
{
385+
ri->ret_addr = (kprobe_opcode_t *)regs->ra;
386+
ri->fp = NULL;
387+
regs->ra = (unsigned long) &kretprobe_trampoline;
388+
}
389+
390+
int __kprobes arch_trampoline_kprobe(struct kprobe *p)
391+
{
392+
return 0;
393+
}
394+
395+
int __init arch_init_kprobes(void)
396+
{
397+
return 0;
398+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
/*
3+
* Author: Patrick Stählin <me@packi.ch>
4+
*/
5+
#include <linux/linkage.h>
6+
7+
#include <asm/asm.h>
8+
#include <asm/asm-offsets.h>
9+
10+
.text
11+
.altmacro
12+
13+
.macro save_all_base_regs
14+
REG_S x1, PT_RA(sp)
15+
REG_S x3, PT_GP(sp)
16+
REG_S x4, PT_TP(sp)
17+
REG_S x5, PT_T0(sp)
18+
REG_S x6, PT_T1(sp)
19+
REG_S x7, PT_T2(sp)
20+
REG_S x8, PT_S0(sp)
21+
REG_S x9, PT_S1(sp)
22+
REG_S x10, PT_A0(sp)
23+
REG_S x11, PT_A1(sp)
24+
REG_S x12, PT_A2(sp)
25+
REG_S x13, PT_A3(sp)
26+
REG_S x14, PT_A4(sp)
27+
REG_S x15, PT_A5(sp)
28+
REG_S x16, PT_A6(sp)
29+
REG_S x17, PT_A7(sp)
30+
REG_S x18, PT_S2(sp)
31+
REG_S x19, PT_S3(sp)
32+
REG_S x20, PT_S4(sp)
33+
REG_S x21, PT_S5(sp)
34+
REG_S x22, PT_S6(sp)
35+
REG_S x23, PT_S7(sp)
36+
REG_S x24, PT_S8(sp)
37+
REG_S x25, PT_S9(sp)
38+
REG_S x26, PT_S10(sp)
39+
REG_S x27, PT_S11(sp)
40+
REG_S x28, PT_T3(sp)
41+
REG_S x29, PT_T4(sp)
42+
REG_S x30, PT_T5(sp)
43+
REG_S x31, PT_T6(sp)
44+
.endm
45+
46+
.macro restore_all_base_regs
47+
REG_L x3, PT_GP(sp)
48+
REG_L x4, PT_TP(sp)
49+
REG_L x5, PT_T0(sp)
50+
REG_L x6, PT_T1(sp)
51+
REG_L x7, PT_T2(sp)
52+
REG_L x8, PT_S0(sp)
53+
REG_L x9, PT_S1(sp)
54+
REG_L x10, PT_A0(sp)
55+
REG_L x11, PT_A1(sp)
56+
REG_L x12, PT_A2(sp)
57+
REG_L x13, PT_A3(sp)
58+
REG_L x14, PT_A4(sp)
59+
REG_L x15, PT_A5(sp)
60+
REG_L x16, PT_A6(sp)
61+
REG_L x17, PT_A7(sp)
62+
REG_L x18, PT_S2(sp)
63+
REG_L x19, PT_S3(sp)
64+
REG_L x20, PT_S4(sp)
65+
REG_L x21, PT_S5(sp)
66+
REG_L x22, PT_S6(sp)
67+
REG_L x23, PT_S7(sp)
68+
REG_L x24, PT_S8(sp)
69+
REG_L x25, PT_S9(sp)
70+
REG_L x26, PT_S10(sp)
71+
REG_L x27, PT_S11(sp)
72+
REG_L x28, PT_T3(sp)
73+
REG_L x29, PT_T4(sp)
74+
REG_L x30, PT_T5(sp)
75+
REG_L x31, PT_T6(sp)
76+
.endm
77+
78+
ENTRY(kretprobe_trampoline)
79+
addi sp, sp, -(PT_SIZE_ON_STACK)
80+
save_all_base_regs
81+
82+
move a0, sp /* pt_regs */
83+
84+
call trampoline_probe_handler
85+
86+
/* use the result as the return-address */
87+
move ra, a0
88+
89+
restore_all_base_regs
90+
addi sp, sp, PT_SIZE_ON_STACK
91+
92+
ret
93+
ENDPROC(kretprobe_trampoline)
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
#include <linux/bitops.h>
4+
#include <linux/kernel.h>
5+
#include <linux/kprobes.h>
6+
7+
#include "decode-insn.h"
8+
#include "simulate-insn.h"
9+
10+
static inline bool rv_insn_reg_get_val(struct pt_regs *regs, u32 index,
11+
unsigned long *ptr)
12+
{
13+
if (index == 0)
14+
*ptr = 0;
15+
else if (index <= 31)
16+
*ptr = *((unsigned long *)regs + index);
17+
else
18+
return false;
19+
20+
return true;
21+
}
22+
23+
static inline bool rv_insn_reg_set_val(struct pt_regs *regs, u32 index,
24+
unsigned long val)
25+
{
26+
if (index == 0)
27+
return false;
28+
else if (index <= 31)
29+
*((unsigned long *)regs + index) = val;
30+
else
31+
return false;
32+
33+
return true;
34+
}
35+
36+
bool __kprobes simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs)
37+
{
38+
/*
39+
* 31 30 21 20 19 12 11 7 6 0
40+
* imm [20] | imm[10:1] | imm[11] | imm[19:12] | rd | opcode
41+
* 1 10 1 8 5 JAL/J
42+
*/
43+
bool ret;
44+
u32 imm;
45+
u32 index = (opcode >> 7) & 0x1f;
46+
47+
ret = rv_insn_reg_set_val(regs, index, addr + 4);
48+
if (!ret)
49+
return ret;
50+
51+
imm = ((opcode >> 21) & 0x3ff) << 1;
52+
imm |= ((opcode >> 20) & 0x1) << 11;
53+
imm |= ((opcode >> 12) & 0xff) << 12;
54+
imm |= ((opcode >> 31) & 0x1) << 20;
55+
56+
instruction_pointer_set(regs, addr + sign_extend32((imm), 20));
57+
58+
return ret;
59+
}
60+
61+
bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs)
62+
{
63+
/*
64+
* 31 20 19 15 14 12 11 7 6 0
65+
* offset[11:0] | rs1 | 010 | rd | opcode
66+
* 12 5 3 5 JALR/JR
67+
*/
68+
bool ret;
69+
unsigned long base_addr;
70+
u32 imm = (opcode >> 20) & 0xfff;
71+
u32 rd_index = (opcode >> 7) & 0x1f;
72+
u32 rs1_index = (opcode >> 15) & 0x1f;
73+
74+
ret = rv_insn_reg_set_val(regs, rd_index, addr + 4);
75+
if (!ret)
76+
return ret;
77+
78+
ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr);
79+
if (!ret)
80+
return ret;
81+
82+
instruction_pointer_set(regs, (base_addr + sign_extend32((imm), 11))&~1);
83+
84+
return ret;
85+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
3+
#ifndef _RISCV_KERNEL_PROBES_SIMULATE_INSN_H
4+
#define _RISCV_KERNEL_PROBES_SIMULATE_INSN_H
5+
6+
#define __RISCV_INSN_FUNCS(name, mask, val) \
7+
static __always_inline bool riscv_insn_is_##name(probe_opcode_t code) \
8+
{ \
9+
BUILD_BUG_ON(~(mask) & (val)); \
10+
return (code & (mask)) == (val); \
11+
} \
12+
bool simulate_##name(u32 opcode, unsigned long addr, \
13+
struct pt_regs *regs)
14+
15+
#define RISCV_INSN_REJECTED(name, code) \
16+
do { \
17+
if (riscv_insn_is_##name(code)) { \
18+
return INSN_REJECTED; \
19+
} \
20+
} while (0)
21+
22+
__RISCV_INSN_FUNCS(system, 0x7f, 0x73);
23+
__RISCV_INSN_FUNCS(fence, 0x7f, 0x0f);
24+
25+
#define RISCV_INSN_SET_SIMULATE(name, code) \
26+
do { \
27+
if (riscv_insn_is_##name(code)) { \
28+
api->handler = simulate_##name; \
29+
return INSN_GOOD_NO_SLOT; \
30+
} \
31+
} while (0)
32+
33+
__RISCV_INSN_FUNCS(c_j, 0xe003, 0xa001);
34+
__RISCV_INSN_FUNCS(c_jr, 0xf007, 0x8002);
35+
__RISCV_INSN_FUNCS(c_jal, 0xe003, 0x2001);
36+
__RISCV_INSN_FUNCS(c_jalr, 0xf007, 0x9002);
37+
__RISCV_INSN_FUNCS(c_beqz, 0xe003, 0xc001);
38+
__RISCV_INSN_FUNCS(c_bnez, 0xe003, 0xe001);
39+
__RISCV_INSN_FUNCS(c_ebreak, 0xffff, 0x9002);
40+
41+
__RISCV_INSN_FUNCS(auipc, 0x7f, 0x17);
42+
__RISCV_INSN_FUNCS(branch, 0x7f, 0x63);
43+
44+
__RISCV_INSN_FUNCS(jal, 0x7f, 0x6f);
45+
__RISCV_INSN_FUNCS(jalr, 0x707f, 0x67);
46+
47+
#endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */

‎arch/riscv/kernel/traps.c

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/signal.h>
1313
#include <linux/kdebug.h>
1414
#include <linux/uaccess.h>
15+
#include <linux/kprobes.h>
1516
#include <linux/mm.h>
1617
#include <linux/module.h>
1718
#include <linux/irq.h>
@@ -145,6 +146,14 @@ static inline unsigned long get_break_insn_length(unsigned long pc)
145146

146147
asmlinkage __visible void do_trap_break(struct pt_regs *regs)
147148
{
149+
#ifdef CONFIG_KPROBES
150+
if (kprobe_single_step_handler(regs))
151+
return;
152+
153+
if (kprobe_breakpoint_handler(regs))
154+
return;
155+
#endif
156+
148157
if (user_mode(regs))
149158
force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
150159
#ifdef CONFIG_KGDB

‎arch/riscv/mm/fault.c

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/perf_event.h>
1414
#include <linux/signal.h>
1515
#include <linux/uaccess.h>
16+
#include <linux/kprobes.h>
1617

1718
#include <asm/ptrace.h>
1819
#include <asm/tlbflush.h>
@@ -213,6 +214,9 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
213214
tsk = current;
214215
mm = tsk->mm;
215216

217+
if (kprobe_page_fault(regs, cause))
218+
return;
219+
216220
/*
217221
* Fault-in kernel-space virtual memory on-demand.
218222
* The 'reference' page table is init_mm.pgd.

0 commit comments

Comments
 (0)
Please sign in to comment.