Skip to contentSkip to conversation

riscv: asm: use .insn for making custom instructioons

PATCH Superseded Ben Dooks 4 participants 7 messages Oct 17, 2025 – Oct 23, 2025 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

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

Using .word breaks with big endian builds, making something which is not a valid or worse an instruction or pair that does something which is not intended.

It would seem sensible to add an ASM_INSN() wrapper for anyone to use for hand assembly of instructions.

---
 arch/riscv/include/asm/asm.h      | 6 ++++++
 arch/riscv/include/asm/insn-def.h | 8 ++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
arch/riscv/include/asm/asm.h +6 -0
index 8bd2a11382a3..c92f0ff51ffa 100644
@@ -12,6 +12,12 @@
 #define __ASM_STR(x)	#x
 #endif
 
+#ifndef CONFIG_AS_HAS_INSN
+#define ASM_INSN(__x) ".4byte " __x
+#else
+#define ASM_INSN(__x) ".insn " __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..35b5024db0fc 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("0x100000f")
+#define ZAWRS_WRS_NTO	ASM_INSN("0x00d00073")
+#define ZAWRS_WRS_STO	ASM_INSN("0x01d00073")
+#define RISCV_NOP4	ASM_INSN("0x00000013")
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
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 6 lines · Ben Dooks

Using .word breaks with big endian builds, making something which is not a valid or worse an instruction or pair that does something which is not intended.

It would seem sensible to add an ASM_INSN() wrapper for anyone to use for hand assembly of instructions.

Looking at the GNU as documentation for RISC-V:

https://sourceware.org/binutils/docs/as/RISC_002dV_002dDirectives.html

it seems that RISC-V .insn supports several argument formats that .4byte wouldn't. So it seems this is only applicable for opcodes specified as hex bytes? Might be worth adding that to the macro name in some form.

Also, looks like this is missing the opcodes added in commit 1d4ce63e338fc. If you update this one, could you please add those as well?

thanks

- Paul

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

author
#2 lore raw
Quoted 11 lines · Paul Walmsley

Hi Ben,

Quoted 6 lines · Ben Dooks

Using .word breaks with big endian builds, making something which is not a valid or worse an instruction or pair that does something which is not intended.

It would seem sensible to add an ASM_INSN() wrapper for anyone to use for hand assembly of instructions.

Quoted 7 lines

Looking at the GNU as documentation for RISC-V:

https://sourceware.org/binutils/docs/as/RISC_002dV_002dDirectives.html

it seems that RISC-V .insn supports several argument formats that .4byte wouldn't. So it seems this is only applicable for opcodes specified as hex bytes? Might be worth adding that to the macro name in some form.

Changed it to ASM_INSN_I

Quoted 3 lines

Also, looks like this is missing the opcodes added in commit 1d4ce63e338fc. If you update this one, could you please add those as well?

I'll go change those too.

Thank you for the review.

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
Reviewed-by on v1 1/1
#3 lore raw
In reply to Ben Dooks
Quoted 3 lines · Ben Dooks

Using .word breaks with big endian builds, making something which is not a valid or worse an instruction or pair that does something which is not intended.

The motivation is no longer for big endian and, since big endian doesn't require AS_HAS_INSN, is never really could be without adding that dependency. The commit message should be changed to point out that we want to use .insn when possible because it provides validation and allows mapping symbols to identify the words as instructions.

Quoted 3 lines

It would seem sensible to add an ASM_INSN() wrapper for anyone to use for hand assembly of instructions.

This sentence isn't necessary.

Quoted 32 lines
---
 arch/riscv/include/asm/asm.h      | 6 ++++++
 arch/riscv/include/asm/insn-def.h | 8 ++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
arch/riscv/include/asm/asm.h +6 -0
index 8bd2a11382a3..c92f0ff51ffa 100644
@@ -12,6 +12,12 @@
 #define __ASM_STR(x)	#x
 #endif
 
+#ifndef CONFIG_AS_HAS_INSN
+#define ASM_INSN(__x) ".4byte " __x
+#else
+#define ASM_INSN(__x) ".insn " __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 +0 -0
index c9cfcea52cbb..35b5024db0fc 100644
@@ -256,10 +256,10 @@
 	INSN_S(OPCODE_OP_IMM, FUNC3(6), __RS2(3),		\
 	       SIMM12((offset) & 0xfe0), RS1(base))
 

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

Quoted 10 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("0x100000f")
+#define ZAWRS_WRS_NTO	ASM_INSN("0x00d00073")
+#define ZAWRS_WRS_STO	ASM_INSN("0x01d00073")
+#define RISCV_NOP4	ASM_INSN("0x00000013")
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)

Other than the commit message changes, the patch looks good.

Thanks,
drew

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

author
#4 lore raw
Quoted 58 lines · Andrew Jones
Quoted 3 lines · Ben Dooks

Using .word breaks with big endian builds, making something which is not a valid or worse an instruction or pair that does something which is not intended.

The motivation is no longer for big endian and, since big endian doesn't require AS_HAS_INSN, is never really could be without adding that dependency. The commit message should be changed to point out that we want to use .insn when possible because it provides validation and allows mapping symbols to identify the words as instructions.

Quoted 3 lines

It would seem sensible to add an ASM_INSN() wrapper for anyone to use for hand assembly of instructions.

This sentence isn't necessary.

Quoted 41 lines
---
  arch/riscv/include/asm/asm.h      | 6 ++++++
  arch/riscv/include/asm/insn-def.h | 8 ++++----
  2 files changed, 10 insertions(+), 4 deletions(-)
arch/riscv/include/asm/asm.h +6 -0
index 8bd2a11382a3..c92f0ff51ffa 100644
@@ -12,6 +12,12 @@
  #define __ASM_STR(x)	#x
  #endif
 
+#ifndef CONFIG_AS_HAS_INSN
+#define ASM_INSN(__x) ".4byte " __x
+#else
+#define ASM_INSN(__x) ".insn " __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..35b5024db0fc 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("0x100000f")
+#define ZAWRS_WRS_NTO	ASM_INSN("0x00d00073")
+#define ZAWRS_WRS_STO	ASM_INSN("0x01d00073")
+#define RISCV_NOP4	ASM_INSN("0x00000013")
 
Quoted 8 lines

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

Quoted 1 line
arch/riscv/include/asm/insn-def.h +0 -0
  #define RISCV_INSN_NOP4	_AC(0x00000013, U)

Other than the commit message changes, the patch looks good.

Thanks,
drew

Re-worded and add the review.

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
#5 lore raw
In reply to Ben Dooks

arch/riscv/include/asm/asm.h line 19

Quoted 13 lines · Ben Dooks
arch/riscv/include/asm/asm.h +5 -0
index 8bd2a11382a3..c92f0ff51ffa 100644
@@ -12,6 +12,12 @@
 #define __ASM_STR(x)	#x
 #endif
 
+#ifndef CONFIG_AS_HAS_INSN
+#define ASM_INSN(__x) ".4byte " __x
+#else
+#define ASM_INSN(__x) ".insn " __x
+#endif

FWIW writing code such that double negation applies to the else clause
makes conditionals harder to parse by humans (it's !!CONFIG_AS_HAS_INSN effectively here). Would you mind rewriting it as:

#ifdef CONFIG_AS_HAS_INSN
#define ASM_INSN(__x) ".insn " __x
#else
#define ASM_INSN(__x) ".4byte " __x
#endif

or has there been a particular reason you chose the proposed form?

Maciej

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

author
#6 lore raw
Quoted 20 lines · Maciej W. Rozycki

arch/riscv/include/asm/asm.h line 19

Quoted 13 lines · Ben Dooks
arch/riscv/include/asm/asm.h +5 -0
index 8bd2a11382a3..c92f0ff51ffa 100644
@@ -12,6 +12,12 @@
  #define __ASM_STR(x)	#x
  #endif
 
+#ifndef CONFIG_AS_HAS_INSN
+#define ASM_INSN(__x) ".4byte " __x
+#else
+#define ASM_INSN(__x) ".insn " __x
+#endif

FWIW writing code such that double negation applies to the else clause
makes conditionals harder to parse by humans (it's !!CONFIG_AS_HAS_INSN effectively here). Would you mind rewriting it as:

Quoted 7 lines

#ifdef CONFIG_AS_HAS_INSN
#define ASM_INSN(__x) ".insn " __x
#else
#define ASM_INSN(__x) ".4byte " __x
#endif

or has there been a particular reason you chose the proposed form?

Thanks, no idea why it was this way, fixed for v2.

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