dtrace: deduplicate some RISC-V functions

match_opcode() is defined in FBT, kinst, and dtrace_subr.c. The function
prologue-checking functions are defined in FBT and kinst.

Reviewed by:	markj
Approved by:	markj (mentor)
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D40335
This commit is contained in:
Christos Margiolis 2023-05-30 18:07:18 +03:00
parent 5b1268252c
commit 7a8cf053d1
4 changed files with 90 additions and 69 deletions

View File

@ -2433,6 +2433,14 @@ extern int dtrace_instr_size(uint8_t *instr);
extern int dtrace_dis_get_byte(void *p);
#endif
#if defined(__riscv)
extern int dtrace_match_opcode(uint32_t insn, int match, int mask);
extern int dtrace_instr_sdsp(uint32_t **instr);
extern int dtrace_instr_ret(uint32_t **instr);
extern int dtrace_instr_c_sdsp(uint32_t **instr);
extern int dtrace_instr_c_ret(uint32_t **instr);
#endif
#endif /* _ASM */
#if defined(__i386) || defined(__amd64)

View File

@ -37,6 +37,8 @@
#include <machine/frame.h>
#include <machine/md_var.h>
#include <machine/encoding.h>
#include <machine/riscvreg.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
@ -59,6 +61,12 @@ uint16_t dtrace_fuword16_nocheck(void *);
uint32_t dtrace_fuword32_nocheck(void *);
uint64_t dtrace_fuword64_nocheck(void *);
int dtrace_match_opcode(uint32_t, int, int);
int dtrace_instr_sdsp(uint32_t **);
int dtrace_instr_ret(uint32_t **);
int dtrace_instr_c_sdsp(uint32_t **);
int dtrace_instr_c_ret(uint32_t **);
void
dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
uint32_t *intrpc)
@ -441,3 +449,68 @@ dtrace_fuword64(void *uaddr)
return (dtrace_fuword64_nocheck(uaddr));
}
int
dtrace_match_opcode(uint32_t insn, int match, int mask)
{
if (((insn ^ match) & mask) == 0)
return (1);
return (0);
}
int
dtrace_instr_sdsp(uint32_t **instr)
{
if (dtrace_match_opcode(**instr, (MATCH_SD | RS2_RA | RS1_SP),
(MASK_SD | RS2_MASK | RS1_MASK)))
return (1);
return (0);
}
int
dtrace_instr_c_sdsp(uint32_t **instr)
{
uint16_t *instr1;
int i;
for (i = 0; i < 2; i++) {
instr1 = (uint16_t *)(*instr) + i;
if (dtrace_match_opcode(*instr1, (MATCH_C_SDSP | RS2_C_RA),
(MASK_C_SDSP | RS2_C_MASK))) {
*instr = (uint32_t *)instr1;
return (1);
}
}
return (0);
}
int
dtrace_instr_ret(uint32_t **instr)
{
if (dtrace_match_opcode(**instr, (MATCH_JALR | (X_RA << RS1_SHIFT)),
(MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK)))
return (1);
return (0);
}
int
dtrace_instr_c_ret(uint32_t **instr)
{
uint16_t *instr1;
int i;
for (i = 0; i < 2; i++) {
instr1 = (uint16_t *)(*instr) + i;
if (dtrace_match_opcode(*instr1,
(MATCH_C_JR | (X_RA << RD_SHIFT)), (MASK_C_JR | RD_MASK))) {
*instr = (uint32_t *)instr1;
return (1);
}
}
return (0);
}

View File

@ -63,8 +63,6 @@ typedef struct dtrace_invop_hdlr {
dtrace_invop_hdlr_t *dtrace_invop_hdlr;
static int match_opcode(uint32_t insn, int match, int mask);
int
dtrace_invop(uintptr_t addr, struct trapframe *frame)
{
@ -243,16 +241,6 @@ dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
(uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
}
static int
match_opcode(uint32_t insn, int match, int mask)
{
if (((insn ^ match) & mask) == 0)
return (1);
return (0);
}
static int
dtrace_invop_start(struct trapframe *frame)
{
@ -265,7 +253,7 @@ dtrace_invop_start(struct trapframe *frame)
if (invop == 0)
return (-1);
if (match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
if (dtrace_match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
(MASK_SD | RS2_MASK | RS1_MASK))) {
/* Non-compressed store of ra to sp */
imm = (invop >> 7) & 0x1f;
@ -276,14 +264,14 @@ dtrace_invop_start(struct trapframe *frame)
return (0);
}
if (match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
if (dtrace_match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
(MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
/* Non-compressed ret */
frame->tf_sepc = frame->tf_ra;
return (0);
}
if (match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
if (dtrace_match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
(MASK_C_SDSP | RS2_C_MASK))) {
/* 'C'-compressed store of ra to sp */
uimm = ((invop >> 10) & 0x7) << 3;
@ -294,14 +282,14 @@ dtrace_invop_start(struct trapframe *frame)
return (0);
}
if (match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
if (dtrace_match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
(MASK_C_JR | RD_MASK))) {
/* 'C'-compressed ret */
frame->tf_sepc = frame->tf_ra;
return (0);
}
if (match_opcode(invop, MATCH_C_NOP, MASK_C_NOP))
if (dtrace_match_opcode(invop, MATCH_C_NOP, MASK_C_NOP))
return (0);
#ifdef INVARIANTS

View File

@ -91,52 +91,6 @@ fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
};
}
static int
match_opcode(uint32_t insn, int match, int mask)
{
if (((insn ^ match) & mask) == 0)
return (1);
return (0);
}
static int
check_c_ret(uint32_t **instr)
{
uint16_t *instr1;
int i;
for (i = 0; i < 2; i++) {
instr1 = (uint16_t *)(*instr) + i;
if (match_opcode(*instr1, (MATCH_C_JR | (X_RA << RD_SHIFT)),
(MASK_C_JR | RD_MASK))) {
*instr = (uint32_t *)instr1;
return (1);
}
}
return (0);
}
static int
check_c_sdsp(uint32_t **instr)
{
uint16_t *instr1;
int i;
for (i = 0; i < 2; i++) {
instr1 = (uint16_t *)(*instr) + i;
if (match_opcode(*instr1, (MATCH_C_SDSP | RS2_C_RA),
(MASK_C_SDSP | RS2_C_MASK))) {
*instr = (uint32_t *)instr1;
return (1);
}
}
return (0);
}
int
fbt_provide_module_function(linker_file_t lf, int symindx,
linker_symval_t *symval, void *opaque)
@ -174,15 +128,14 @@ fbt_provide_module_function(linker_file_t lf, int symindx,
/* Look for sd operation */
for (; instr < limit; instr++) {
/* Look for a non-compressed store of ra to sp */
if (match_opcode(*instr, (MATCH_SD | RS2_RA | RS1_SP),
(MASK_SD | RS2_MASK | RS1_MASK))) {
if (dtrace_instr_sdsp(&instr)) {
rval = DTRACE_INVOP_SD;
patchval = FBT_PATCHVAL;
break;
}
/* Look for a 'C'-compressed store of ra to sp. */
if (check_c_sdsp(&instr)) {
if (dtrace_instr_c_sdsp(&instr)) {
rval = DTRACE_INVOP_C_SDSP;
patchval = FBT_C_PATCHVAL;
break;
@ -213,15 +166,14 @@ fbt_provide_module_function(linker_file_t lf, int symindx,
again:
for (; instr < limit; instr++) {
/* Look for non-compressed return */
if (match_opcode(*instr, (MATCH_JALR | (X_RA << RS1_SHIFT)),
(MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
if (dtrace_instr_ret(&instr)) {
rval = DTRACE_INVOP_RET;
patchval = FBT_PATCHVAL;
break;
}
/* Look for 'C'-compressed return */
if (check_c_ret(&instr)) {
if (dtrace_instr_c_ret(&instr)) {
rval = DTRACE_INVOP_C_RET;
patchval = FBT_C_PATCHVAL;
break;