Skip to contentSkip to conversation

riscv: asm: use .insn for making custom instructions

PATCH Applied Ben Dooks 4 participants 5 messages Oct 24, 2025 – Oct 31, 2025 RISC-V

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

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

The assembler has .insn for building custom instructions now, so change the .4byte to .insn. This 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
- new patch description
v3:
- fixed typo missed in compile test of NOP4
- fixed incorrect MIPS changes
- re-wored slightly to shorten sentences and remove typos

---
 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..d29da6ccd3dd 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_I("0x00000013")
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
arch/riscv/include/asm/vendor_extensions/mips.h +3 -3
index ea8ca747d691..ffeb12dc17a3 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("0x00301013\n\t")
+#define MIPS_IHB	ASM_INSN_I("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
#1 lore raw
This patch was applied to riscv/linux.git (fixes) by Paul Walmsley <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Paul Walmsley <[email protected]>:

Quoted 10 lines · On Fri, 24 Oct 2025 18:16:40 +0100 you wrote:

The assembler has .insn for building custom instructions now, so change the .4byte to .insn. This 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.

[...]

Here is the summary with links:
- [v3] riscv: asm: use .insn for making custom instructions

    https://git.kernel.org/riscv/c/44aa25c000b4

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw
In reply to Ben Dooks

Hey Ben,

Quoted 31 lines · Ben Dooks

The assembler has .insn for building custom instructions now, so change the .4byte to .insn. This 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
- new patch description
v3:
- fixed typo missed in compile test of NOP4
- fixed incorrect MIPS changes
- re-wored slightly to shorten sentences and remove typos

---
 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 +0 -0
index 8bd2a11382a3..2d97436999ca 100644

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

Quoted 4 lines
arch/riscv/include/asm/asm.h +1 -0
@@ -12,6 +12,12 @@
 #define __ASM_STR(x)	#x
 #endif
 
+#ifdef CONFIG_AS_HAS_INSN

The current AS_HAS_INSN check in Kconfig is not sufficient for this usage. With LLVM prior to [1] (i.e., older than 19) or GNU binutils prior to [2] (i.e., older than 2.38), the build breaks:

In file included from <built-in>:4:
In file included from lib/vdso/gettimeofday.c:6:
In file included from include/vdso/datapage.h:21:
In file included from include/vdso/processor.h:10:
arch/riscv/include/asm/vdso/processor.h:23:2: error: expected instruction format

     23 |         ALT_RISCV_PAUSE();
        |         ^
  arch/riscv/include/asm/errata_list.h:47:3: note: expanded from macro 'ALT_RISCV_PAUSE'
     47 |                 RISCV_PAUSE, /* Original RISC‑V pause insn */ \
        |                 ^
  arch/riscv/include/asm/insn-def.h:259:21: note: expanded from macro 'RISCV_PAUSE'
    259 | #define RISCV_PAUSE     ASM_INSN_I("0x100000f")
        |                         ^
  arch/riscv/include/asm/asm.h:16:26: note: expanded from macro 'ASM_INSN_I'
     16 | #define ASM_INSN_I(__x) ".insn " __x
        |                          ^
  <inline asm>:5:7: note: instantiated into assembly here
      5 | .insn 0x100000f
        |       ^

arch/riscv/include/asm/vdso/processor.h: Assembler messages:
arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' make[4]: *** [scripts/Makefile.build:287: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1

Maybe this wants a new symbol like CONFIG_AS_HAS_INSN_EXT that checks for this support?

[1]: https://github.com/llvm/llvm-project/commit/2a086dce691e3cc34a2fc27f4fb255bb2cbbfac9
[2]: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=a262b82fdbf4cda3b0648b1adc32245ca3f78b7a

Cheers,
Nathan

Quoted 51 lines
arch/riscv/include/asm/asm.h +5 -0
+#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..d29da6ccd3dd 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_I("0x00000013")
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
arch/riscv/include/asm/vendor_extensions/mips.h +3 -3
index ea8ca747d691..ffeb12dc17a3 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("0x00301013\n\t")
+#define MIPS_IHB	ASM_INSN_I("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

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

#3 lore raw
Quoted 70 lines · Nathan Chancellor

Hey Ben,

Quoted 31 lines · Ben Dooks

The assembler has .insn for building custom instructions now, so change the .4byte to .insn. This 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
- new patch description
v3:
- fixed typo missed in compile test of NOP4
- fixed incorrect MIPS changes
- re-wored slightly to shorten sentences and remove typos

---
 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 +0 -0
index 8bd2a11382a3..2d97436999ca 100644

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

Quoted 4 lines
arch/riscv/include/asm/asm.h +1 -0
@@ -12,6 +12,12 @@
 #define __ASM_STR(x)	#x
 #endif
 
+#ifdef CONFIG_AS_HAS_INSN

The current AS_HAS_INSN check in Kconfig is not sufficient for this usage. With LLVM prior to [1] (i.e., older than 19) or GNU binutils prior to [2] (i.e., older than 2.38), the build breaks:

In file included from <built-in>:4:
In file included from lib/vdso/gettimeofday.c:6:
In file included from include/vdso/datapage.h:21:
In file included from include/vdso/processor.h:10:
arch/riscv/include/asm/vdso/processor.h:23:2: error: expected instruction format

     23 |         ALT_RISCV_PAUSE();
        |         ^
  arch/riscv/include/asm/errata_list.h:47:3: note: expanded from macro 'ALT_RISCV_PAUSE'
     47 |                 RISCV_PAUSE, /* Original RISC‑V pause insn */ \
        |                 ^
  arch/riscv/include/asm/insn-def.h:259:21: note: expanded from macro 'RISCV_PAUSE'
    259 | #define RISCV_PAUSE     ASM_INSN_I("0x100000f")
        |                         ^
  arch/riscv/include/asm/asm.h:16:26: note: expanded from macro 'ASM_INSN_I'
     16 | #define ASM_INSN_I(__x) ".insn " __x
        |                          ^
  <inline asm>:5:7: note: instantiated into assembly here
      5 | .insn 0x100000f
        |       ^

arch/riscv/include/asm/vdso/processor.h: Assembler messages:
arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' arch/riscv/include/asm/vdso/processor.h:27: Error: unrecognized opcode 0x100000f' make[4]: *** [scripts/Makefile.build:287: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1

Quoted 5 lines

Maybe this wants a new symbol like CONFIG_AS_HAS_INSN_EXT that checks for this support?

[1]: https://github.com/llvm/llvm-project/commit/2a086dce691e3cc34a2fc27f4fb255bb2cbbfac9
[2]: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=a262b82fdbf4cda3b0648b1adc32245ca3f78b7a

Or we can change the test in 'config AS_HAS_INSN' to use the hex encoded input. We'd completely stop using .insn for LLVM older than 19 and GNU binutils older than 2.38, but that's probably better than adding yet another config.

Thanks,
drew

Quoted 51 lines

Cheers,
Nathan

Quoted 47 lines
arch/riscv/include/asm/asm.h +5 -0
+#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..d29da6ccd3dd 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_I("0x00000013")
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
arch/riscv/include/asm/vendor_extensions/mips.h +3 -3
index ea8ca747d691..ffeb12dc17a3 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("0x00301013\n\t")
+#define MIPS_IHB	ASM_INSN_I("0x00101013\n\t")
 
 #endif // _ASM_RISCV_VENDOR_EXTENSIONS_MIPS_H
Signature · 1 line
2.37.2.352.g3c44437643
Quoted 4 lines
Quoted 4 lines

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

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

#4 lore raw
Quoted 4 lines · Andrew Jones

Or we can change the test in 'config AS_HAS_INSN' to use the hex encoded input. We'd completely stop using .insn for LLVM older than 19 and GNU binutils older than 2.38, but that's probably better than adding yet another config.

Yeah, that would obviously work too. I was not sure how tolerable it was to lose the use of .insn altogether for those versions hence the split suggestion but if it is not that big of a deal, might as well check for the extended .insn upfront.

Cheers,
Nathan

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