git: 080e56a6c98c - main - dtrace: expose dtrace_instr_size() to userland and implement it for riscv

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 20 Apr 2023 17:36:11 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=080e56a6c98c7e7a241a4260d4c19d3cad5b22af

commit 080e56a6c98c7e7a241a4260d4c19d3cad5b22af
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2023-04-20 16:21:46 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-04-20 17:35:57 +0000

    dtrace: expose dtrace_instr_size() to userland and implement it for riscv
    
    dtrace_instr_size() is needed by the forthcoming RISC-V port of kinst,
    as well as by libdtrace in D38825 for both amd64 and RISC-V.
    
    Reviewed by:    markj, mhorne
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D39489
---
 cddl/lib/libdtrace/Makefile                        |  6 +++++-
 .../contrib/opensolaris/uts/common/sys/dtrace.h    |  5 ++++-
 sys/cddl/dev/dtrace/riscv/instr_size.c             | 22 ++++++++++++++++++++++
 sys/modules/dtrace/dtrace/Makefile                 |  4 ++++
 4 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/cddl/lib/libdtrace/Makefile b/cddl/lib/libdtrace/Makefile
index 651609409a3a..84258169e44a 100644
--- a/cddl/lib/libdtrace/Makefile
+++ b/cddl/lib/libdtrace/Makefile
@@ -122,10 +122,14 @@ CFLAGS+=	-I${OPENSOLARIS_SYS_DISTDIR}/uts/intel
 .endif
 
 .if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "amd64"
-SRCS+=		dis_tables.c
+SRCS+=		dis_tables.c instr_size.c
 DSRCS+=		regs_x86.d
 .endif
 
+.if ${MACHINE_CPUARCH} == "riscv"
+SRCS+=		instr_size.c
+.endif
+
 YFLAGS+=-d
 
 LIBADD=	ctf elf proc pthread rtld_db
diff --git a/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h b/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h
index d3d8fbca6164..7b8da08e9ae9 100644
--- a/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h
+++ b/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h
@@ -2399,7 +2399,6 @@ extern void dtrace_safe_synchronous_signal(void);
 extern int dtrace_mach_aframes(void);
 
 #if defined(__i386) || defined(__amd64)
-extern int dtrace_instr_size(uint8_t *instr);
 extern int dtrace_instr_size_isa(uint8_t *, model_t, int *);
 extern void dtrace_invop_callsite(void);
 #endif
@@ -2428,6 +2427,10 @@ extern void dtrace_helpers_destroy(proc_t *);
 
 #endif /* _KERNEL */
 
+#if defined(__i386) || defined(__amd64) || defined (__riscv)
+extern int dtrace_instr_size(uint8_t *instr);
+#endif
+
 #endif	/* _ASM */
 
 #if defined(__i386) || defined(__amd64)
diff --git a/sys/cddl/dev/dtrace/riscv/instr_size.c b/sys/cddl/dev/dtrace/riscv/instr_size.c
new file mode 100644
index 000000000000..bfdc962f4aa9
--- /dev/null
+++ b/sys/cddl/dev/dtrace/riscv/instr_size.c
@@ -0,0 +1,22 @@
+/*
+ * SPDX-License-Identifier: CDDL 1.0
+ *
+ * Copyright 2023 Christos Margiolis <christos@FreeBSD.org>
+ */
+
+#include <sys/types.h>
+#include <sys/dtrace.h>
+
+#include <machine/riscvreg.h>
+
+#define RVC_MASK 0x03
+
+int
+dtrace_instr_size(uint8_t *instr)
+{
+	/* Detect compressed instructions. */
+	if ((~(*instr) & RVC_MASK) == 0)
+		return (INSN_SIZE);
+	else
+		return (INSN_C_SIZE);
+}
diff --git a/sys/modules/dtrace/dtrace/Makefile b/sys/modules/dtrace/dtrace/Makefile
index 0bd81bb39db0..1513418281ad 100644
--- a/sys/modules/dtrace/dtrace/Makefile
+++ b/sys/modules/dtrace/dtrace/Makefile
@@ -22,8 +22,12 @@ SRCS+=		dis_tables.c \
 		instr_size.c
 CFLAGS+=       -I${SYSDIR}/cddl/contrib/opensolaris/uts/intel \
                -I${SYSDIR}/cddl/dev/dtrace/x86
+.endif
 
+.if ${MACHINE_CPUARCH} == "riscv"
+SRCS+=		instr_size.c
 .endif
+
 CFLAGS+= ${OPENZFS_CFLAGS}
 
 SRCS+=		bus_if.h device_if.h vnode_if.h