[PATCH RFC] RISCV: insert zimop instruction at the start

Deepak Gupta debug@rivosinc.com
Thu Jun 12 21:29:08 GMT 2025


RVA23 mandates that CPU must implement zimop and zcmop extensions. These
extensions are termed as "may be operations" to signify that some instructions
might be leveraged by some future extensions to turn into different operations.
Unless those extensions are used for codegen, there is no way to ensure that
compiled software has zimop/zcmop instruction in them. Without any software
using these instructions, there is no way to ensure that target hardware is
RVA23 compatible.

zicfiss extension converts some of the zimop instructions into shadow stack
instructions. Support for shadow stack is still in the flux. This patch ensures
that if compiler supports shadow stack codegen then sspush/sspopchk
instructions are at the start when userspace starts life. Kernel doesn't enable
shadow stack for userspace by default. Userspace will issue shadow stack enable
prtctls to kernel. Thus always these (sspush/sspopchk) instructions will
default to zimop behavior.

Note that this is not a backward hardware compatible change.

Signed-off-by: Deepak Gupta <debug@rivosinc.com>
---
RVA23 mandates that CPU must implement zimop and zcmop extensions. These
extensions are termed as "may be operations" to signify that some instructions
might be leveraged by some future extensions to turn into different operations.
Unless those extensions are used for codegen, there is no way to ensure that
compiled software has zimop/zcmop instruction in them. Without any software
using these instructions, there is no way to ensure that target hardware is
RVA23 compatible.

zicfiss extension converts some of the zimop instructions into shadow stack
instructions. Support for shadow stack is still in the flux. This patch ensures
that if compiler supports shadow stack codegen then sspush/sspopchk
instructions are at the start when userspace starts life. Kernel doesn't enable
shadow stack for userspace by default. Userspace will issue shadow stack enable
prctls to kernel. Thus always these (sspush/sspopchk) instructions will
default to zimop behavior.

Note that this is not a backward hardware compatible change.
---
 sysdeps/riscv/dl-machine.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
index dcc3e0883b..162b6034ea 100644
--- a/sysdeps/riscv/dl-machine.h
+++ b/sysdeps/riscv/dl-machine.h
@@ -53,6 +53,15 @@
      || (__WORDSIZE == 64 && (type) == R_RISCV_TLS_TPREL64)))	\
    | (ELF_RTYPE_CLASS_COPY * ((type) == R_RISCV_COPY)))
 
+
+#ifdef __riscv_zicfiss
+#define INSERT_ZIMOP \
+     sspush x1       \
+     sspop  x1
+#else
+#define INSERT_ZIMOP
+#endif
+
 /* Return nonzero iff ELF header is compatible with the running host.  */
 static inline int __attribute_used__
 elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
@@ -102,6 +111,7 @@ elf_machine_dynamic (void)
 #define RTLD_START asm (\
 	".text\n\
 	" _RTLD_PROLOGUE (ENTRY_POINT) "\
+        INSERT_ZIMOP \
 	mv a0, sp\n\
 	jal _dl_start\n\
 	" _RTLD_PROLOGUE (_dl_start_user) "\

---
base-commit: 77930e0447e0b37a129db0e13c6c6f5e60a3019e
change-id: 20250612-glibc_zimop-480880581b2b
--
- debug



More information about the Libc-alpha mailing list