git: 7a8cf053d15e - main - dtrace: deduplicate some RISC-V functions
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 30 May 2023 15:07:37 UTC
The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=7a8cf053d15ed4be7f548bfc9fcbe1367338e2d2 commit 7a8cf053d15ed4be7f548bfc9fcbe1367338e2d2 Author: Christos Margiolis <christos@FreeBSD.org> AuthorDate: 2023-05-30 15:07:18 +0000 Commit: Christos Margiolis <christos@FreeBSD.org> CommitDate: 2023-05-30 15:07:18 +0000 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 --- .../contrib/opensolaris/uts/common/sys/dtrace.h | 8 +++ sys/cddl/dev/dtrace/riscv/dtrace_isa.c | 73 ++++++++++++++++++++++ sys/cddl/dev/dtrace/riscv/dtrace_subr.c | 22 ++----- sys/cddl/dev/fbt/riscv/fbt_isa.c | 56 ++--------------- 4 files changed, 90 insertions(+), 69 deletions(-) diff --git a/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h b/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h index a9730c356e9e..b38bc71a6845 100644 --- a/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h +++ b/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h @@ -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) diff --git a/sys/cddl/dev/dtrace/riscv/dtrace_isa.c b/sys/cddl/dev/dtrace/riscv/dtrace_isa.c index 666f754cc311..1521528c4205 100644 --- a/sys/cddl/dev/dtrace/riscv/dtrace_isa.c +++ b/sys/cddl/dev/dtrace/riscv/dtrace_isa.c @@ -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); +} diff --git a/sys/cddl/dev/dtrace/riscv/dtrace_subr.c b/sys/cddl/dev/dtrace/riscv/dtrace_subr.c index f42c073f94c9..e9e76180a055 100644 --- a/sys/cddl/dev/dtrace/riscv/dtrace_subr.c +++ b/sys/cddl/dev/dtrace/riscv/dtrace_subr.c @@ -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 diff --git a/sys/cddl/dev/fbt/riscv/fbt_isa.c b/sys/cddl/dev/fbt/riscv/fbt_isa.c index 5c8340d9f001..f17ce21a1b66 100644 --- a/sys/cddl/dev/fbt/riscv/fbt_isa.c +++ b/sys/cddl/dev/fbt/riscv/fbt_isa.c @@ -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;