Skip to contentSkip to conversation

riscv: asm: use .insn for making custom instructioons

PATCH Superseded Ben Dooks 2 participants 3 messages Oct 23, 2025 – Oct 24, 2025 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH v2 1/1 riscv: asm: use .insn for making custom instructioons

The assembler has .insn for building custom instructions now, so remove the .4byte which also ensures the output is marked as an instruction and not as data (which may confuse both debuggers and anything else that relies on this sort of marking)

Add an ASM_INSN_I() wrapper in asm.h to allow the selecting of how this is output so older assemblers are still good.

---
v2:
- fixed #ifndef v #ifdef
- added custom MIPS instructions too
-

---
 arch/riscv/include/asm/asm.h                    | 6 ++++++
 arch/riscv/include/asm/insn-def.h               | 8 ++++----
 arch/riscv/include/asm/vendor_extensions/mips.h | 6 +++---
 3 files changed, 13 insertions(+), 7 deletions(-)
arch/riscv/include/asm/asm.h +6 -0
index 8bd2a11382a3..2d97436999ca 100644
@@ -12,6 +12,12 @@
 #define __ASM_STR(x)	#x
 #endif
 
+#ifdef CONFIG_AS_HAS_INSN
+#define ASM_INSN_I(__x) ".insn " __x
+#else
+#define ASM_INSN_I(__x) ".4byte " __x
+#endif
+
 #if __riscv_xlen == 64
 #define __REG_SEL(a, b)	__ASM_STR(a)
 #elif __riscv_xlen == 32
arch/riscv/include/asm/insn-def.h +4 -4
index c9cfcea52cbb..a7860e6b908d 100644
@@ -256,10 +256,10 @@
 	INSN_S(OPCODE_OP_IMM, FUNC3(6), __RS2(3),		\
 	       SIMM12((offset) & 0xfe0), RS1(base))
 
-#define RISCV_PAUSE	".4byte 0x100000f"
-#define ZAWRS_WRS_NTO	".4byte 0x00d00073"
-#define ZAWRS_WRS_STO	".4byte 0x01d00073"
-#define RISCV_NOP4	".4byte 0x00000013"
+#define RISCV_PAUSE	ASM_INSN_I("0x100000f")
+#define ZAWRS_WRS_NTO	ASM_INSN_I("0x00d00073")
+#define ZAWRS_WRS_STO	ASM_INSN_I("0x01d00073")
+#define RISCV_NOP4	ASM_INSN(_U"0x00000013")
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
arch/riscv/include/asm/vendor_extensions/mips.h +3 -3
index ea8ca747d691..bcdce3ef3869 100644
@@ -30,8 +30,8 @@ extern struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_mips;
  * allowing any subsequent instructions to fetch.
  */
 
-#define MIPS_PAUSE	".4byte 0x00501013\n\t"
-#define MIPS_EHB	".4byte 0x00301013\n\t"
-#define MIPS_IHB	".4byte 0x00101013\n\t"
+#define MIPS_PAUSE	ASM_INSN_I("0x00501013\n\t")
+#define MIPS_EHB	ASM_INSN_I(".4byte 0x00301013\n\t")
+#define MIPS_IHB	ASM_INSN_I(".4byte 0x00101013\n\t")
 
 #endif // _ASM_RISCV_VENDOR_EXTENSIONS_MIPS_H
Signature · 7 lines
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#1 lore raw

Hi Ben,

Quoted 22 lines · Ben Dooks

The assembler has .insn for building custom instructions now, so remove the .4byte which also ensures the output is marked as an instruction and not as data (which may confuse both debuggers and anything else that relies on this sort of marking)

Add an ASM_INSN_I() wrapper in asm.h to allow the selecting of how this is output so older assemblers are still good.

---
v2:
- fixed #ifndef v #ifdef
- added custom MIPS instructions too
-

---
 arch/riscv/include/asm/asm.h                    | 6 ++++++
 arch/riscv/include/asm/insn-def.h               | 8 ++++----
 arch/riscv/include/asm/vendor_extensions/mips.h | 6 +++---
 3 files changed, 13 insertions(+), 7 deletions(-)

[ ... ]

Quoted 6 lines
arch/riscv/include/asm/insn-def.h +0 -0
index c9cfcea52cbb..a7860e6b908d 100644
@@ -256,10 +256,10 @@
 	INSN_S(OPCODE_OP_IMM, FUNC3(6), __RS2(3),		\

arch/riscv/include/asm/insn-def.h line 262

Quoted 10 lines
arch/riscv/include/asm/insn-def.h +4 -4
 	       SIMM12((offset) & 0xfe0), RS1(base))
 
-#define RISCV_PAUSE	".4byte 0x100000f"
-#define ZAWRS_WRS_NTO	".4byte 0x00d00073"
-#define ZAWRS_WRS_STO	".4byte 0x01d00073"
-#define RISCV_NOP4	".4byte 0x00000013"
+#define RISCV_PAUSE	ASM_INSN_I("0x100000f")
+#define ZAWRS_WRS_NTO	ASM_INSN_I("0x00d00073")
+#define ZAWRS_WRS_STO	ASM_INSN_I("0x01d00073")
+#define RISCV_NOP4	ASM_INSN(_U"0x00000013")

This looks garbled. Is this intentional?

Quoted 7 lines
arch/riscv/include/asm/insn-def.h +0 -0
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
arch/riscv/include/asm/vendor_extensions/mips.h +0 -0
index ea8ca747d691..bcdce3ef3869 100644

arch/riscv/include/asm/vendor_extensions/mips.h extern struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_mips; · line 35

Quoted 9 lines
arch/riscv/include/asm/vendor_extensions/mips.h +3 -3
@@ -30,8 +30,8 @@ extern struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_mips;
  * allowing any subsequent instructions to fetch.
  */
 
-#define MIPS_PAUSE	".4byte 0x00501013\n\t"
-#define MIPS_EHB	".4byte 0x00301013\n\t"
-#define MIPS_IHB	".4byte 0x00101013\n\t"
+#define MIPS_PAUSE	ASM_INSN_I("0x00501013\n\t")
+#define MIPS_EHB	ASM_INSN_I(".4byte 0x00301013\n\t")
+#define MIPS_IHB	ASM_INSN_I(".4byte 0x00101013\n\t")

Same for the above two lines?

- Paul

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#2 lore raw
Quoted 37 lines · Paul Walmsley

Hi Ben,

Quoted 22 lines · Ben Dooks

The assembler has .insn for building custom instructions now, so remove the .4byte which also ensures the output is marked as an instruction and not as data (which may confuse both debuggers and anything else that relies on this sort of marking)

Add an ASM_INSN_I() wrapper in asm.h to allow the selecting of how this is output so older assemblers are still good.

---
v2:
- fixed #ifndef v #ifdef
- added custom MIPS instructions too
-

---
  arch/riscv/include/asm/asm.h                    | 6 ++++++
  arch/riscv/include/asm/insn-def.h               | 8 ++++----
  arch/riscv/include/asm/vendor_extensions/mips.h | 6 +++---
  3 files changed, 13 insertions(+), 7 deletions(-)

[ ... ]

Quoted 8 lines
arch/riscv/include/asm/insn-def.h +0 -0
index c9cfcea52cbb..a7860e6b908d 100644
@@ -256,10 +256,10 @@
  	INSN_S(OPCODE_OP_IMM, FUNC3(6), __RS2(3),		\
  	       SIMM12((offset) & 0xfe0), RS1(base))
 
Quoted 10 lines

arch/riscv/include/asm/insn-def.h line 262

Quoted 8 lines
arch/riscv/include/asm/insn-def.h +4 -4
-#define RISCV_PAUSE	".4byte 0x100000f"
-#define ZAWRS_WRS_NTO	".4byte 0x00d00073"
-#define ZAWRS_WRS_STO	".4byte 0x01d00073"
-#define RISCV_NOP4	".4byte 0x00000013"
+#define RISCV_PAUSE	ASM_INSN_I("0x100000f")
+#define ZAWRS_WRS_NTO	ASM_INSN_I("0x00d00073")
+#define ZAWRS_WRS_STO	ASM_INSN_I("0x01d00073")
+#define RISCV_NOP4	ASM_INSN(_U"0x00000013")

This looks garbled. Is this intentional?

No, must have done this between writing it and saving.

Worse is it builds fine :/

Will do v3, thanks.

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv