linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* RISC-V big-endian support
@ 2025-08-22 16:52 Ben Dooks
  2025-08-22 16:52 ` [PATCH 01/18] riscv: add initial kconfig and build flags for big-endian Ben Dooks
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal

This is a new series for `experimental` support for big-endian RISC-V
operation. This has now been tested on both QEMU and a Codethink built
CVA6 FPGA as well as being joined by MIPS and their I8500.

Since MIPS will be shipping systems with big-endian support and the
work we have done with QEMU, we feel that there there is now a good
case for this support to be upstreamed.

The support is currently gated by CONFIG_EXPRIMENTAL until we get more
testing through, and removes CONFIG_VECTOR and the 32bit compatibiltiy
as this has not been tested and needs work.

The KVM support does include an interface to allow access to the HSTATUS
to allow running KVM with different endian-ness to the host (which has
been through a very quick test). We'll push kvmtool pathches once this
has been reviewed.

We have a big-endian buildroot with ucblic on our project page, and will
be working to send updates to buildroot, uclibc, u-boot and qemu with
more work being done by MIPS which will include glibc, clang/llvm, etc.

Project page: https://gitlab.com/CodethinkLabs/riscv_bigendian
MIPS I8500: https://mips.com/products/hardware/i8500/
Kernel branch: https://gitlab.com/CodethinkLabs/linux-kernel/-/commits/bjdooks/dev_v617_rc2



_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 01/18] riscv: add initial kconfig and build flags for big-endian
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 02/18] riscv: update byteorder.h " Ben Dooks
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Add initial kconfig and makefile updates to get a base big-endian
build for arch/riscv. Will require header updates for IO code and
features whcih are known to not work.

Make this depend on CONFIG_EXPERIMENTAL for now as there are bits
that have not been fully fixed to work big-endian.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/Kconfig  | 22 ++++++++++++++++++++++
 arch/riscv/Makefile | 15 ++++++++++++---
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index a4b233a0659e..f1c0a655cd47 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -436,6 +436,28 @@ choice
 		bool "medium any code model"
 endchoice
 
+choice
+	prompt "Data endian"
+	default CPU_LITTLE_ENDIAN
+	help
+	  Configure the endiannes of data access performed by the CPU.
+	  This will require system to be booted from M mode in big endian
+	  and the userland to be compiled for the same endian-ness.
+
+config CPU_BIG_ENDIAN
+	bool "Build big-endian kernel"
+	depends on EXPERT
+	help
+	  Say Y if you want to run big-endian kernel and userspace
+	  Set for expert as this experimental
+
+config CPU_LITTLE_ENDIAN
+       bool "Build little-endian kernel (default)"
+       help
+	  Say  Y if you want to run little-endian kernel and userspace
+	  This is the default for most distributions
+endchoice
+
 config MODULE_SECTIONS
 	bool
 	select HAVE_MOD_ARCH_SPECIFIC
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index df57654a615e..ec3096fd9075 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -21,6 +21,11 @@ else
 endif
 endif
 
+ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
+KBUILD_CPPFLAGS	+= -mbig-endian -D__RISCVEB__
+CHECKFLAGS	+= -D__RISCVEB__
+endif
+
 ifeq ($(CONFIG_CMODEL_MEDLOW),y)
 KBUILD_CFLAGS_MODULE += -mcmodel=medany
 endif
@@ -28,13 +33,17 @@ endif
 export BITS
 ifeq ($(CONFIG_ARCH_RV64I),y)
 	BITS := 64
-	UTS_MACHINE := riscv64
+	ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
+		UTS_MACHINE := riscv64_be
+		KBUILD_LDFLAGS += -melf64briscv
+	else	
+		UTS_MACHINE := riscv64
+		KBUILD_LDFLAGS += -melf64lriscv
+	endif
 
 	KBUILD_CFLAGS += -mabi=lp64
 	KBUILD_AFLAGS += -mabi=lp64
 
-	KBUILD_LDFLAGS += -melf64lriscv
-
 	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \
 			    -Cno-redzone
 else
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 02/18] riscv: update byteorder.h for big-endian
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
  2025-08-22 16:52 ` [PATCH 01/18] riscv: add initial kconfig and build flags for big-endian Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 03/18] riscv: disable vector if big-endian, gcc unsupported option Ben Dooks
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Add checks for big-endian, using BYTE_ORDER as
modern compilers should define this and anything
that isn't is probably too old to build a system.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
v2:
- rewritten to add better __BYTE_ORDER check
---
 arch/riscv/include/uapi/asm/byteorder.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/riscv/include/uapi/asm/byteorder.h b/arch/riscv/include/uapi/asm/byteorder.h
index f671e16bf6af..88be4af063fb 100644
--- a/arch/riscv/include/uapi/asm/byteorder.h
+++ b/arch/riscv/include/uapi/asm/byteorder.h
@@ -7,6 +7,10 @@
 #ifndef _UAPI_ASM_RISCV_BYTEORDER_H
 #define _UAPI_ASM_RISCV_BYTEORDER_H
 
+#if defined(__RISCVEB__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+#include <linux/byteorder/big_endian.h>
+#else
 #include <linux/byteorder/little_endian.h>
+#endif
 
 #endif /* _UAPI_ASM_RISCV_BYTEORDER_H */
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 03/18] riscv: disable vector if big-endian, gcc unsupported option
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
  2025-08-22 16:52 ` [PATCH 01/18] riscv: add initial kconfig and build flags for big-endian Ben Dooks
  2025-08-22 16:52 ` [PATCH 02/18] riscv: update byteorder.h " Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 04/18] riscv: word-at-atime: move to generic if we're big endian Ben Dooks
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

You can't build with vector enabled at the moment so remove
this until gcc is changed.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index f1c0a655cd47..ca5cd4cf6e15 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -656,6 +656,7 @@ config RISCV_ISA_V
 	bool "Vector extension support"
 	depends on TOOLCHAIN_HAS_V
 	depends on FPU
+	depends on CPU_LITTLE_ENDIAN
 	select DYNAMIC_SIGFRAME
 	default y
 	help
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 04/18] riscv: word-at-atime: move to generic if we're big endian
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (2 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 03/18] riscv: disable vector if big-endian, gcc unsupported option Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 05/18] riscv: asm: use .insn for making custom instructioons Ben Dooks
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Use the generic word at a time code if we're running in big endian
for now otherwise this doesn't work. Add better support later.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/include/asm/word-at-a-time.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/word-at-a-time.h b/arch/riscv/include/asm/word-at-a-time.h
index 3802cda71ab7..78468f91ddcc 100644
--- a/arch/riscv/include/asm/word-at-a-time.h
+++ b/arch/riscv/include/asm/word-at-a-time.h
@@ -8,8 +8,13 @@
 #ifndef _ASM_RISCV_WORD_AT_A_TIME_H
 #define _ASM_RISCV_WORD_AT_A_TIME_H
 
-
 #include <asm/asm-extable.h>
+
+#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+/* use the generic one for big-endian */
+#include <asm-generic/word-at-a-time.h>
+#else
+
 #include <linux/bitops.h>
 #include <linux/wordpart.h>
 
@@ -47,6 +52,10 @@ static inline unsigned long find_zero(unsigned long mask)
 /* The mask we created is directly usable as a bytemask */
 #define zero_bytemask(mask) (mask)
 
+#endif /* little endian case*/
+
+
+
 #ifdef CONFIG_DCACHE_WORD_ACCESS
 
 /*
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 05/18] riscv: asm: use .insn for making custom instructioons
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (3 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 04/18] riscv: word-at-atime: move to generic if we're big endian Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 06/18] kconfig: remove CONFIG_COMAPT for big-endian Ben Dooks
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, 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.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/include/asm/insn-def.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/riscv/include/asm/insn-def.h b/arch/riscv/include/asm/insn-def.h
index d5adbaec1d01..2a3019bc1a3b 100644
--- a/arch/riscv/include/asm/insn-def.h
+++ b/arch/riscv/include/asm/insn-def.h
@@ -256,10 +256,17 @@
 	INSN_S(OPCODE_OP_IMM, FUNC3(6), __RS2(3),		\
 	       SIMM12((offset) & 0xfe0), RS1(base))
 
+#ifndef CONFIG_AS_HAS_INSN
 #define RISCV_PAUSE	".4byte 0x100000f"
 #define ZAWRS_WRS_NTO	".4byte 0x00d00073"
 #define ZAWRS_WRS_STO	".4byte 0x01d00073"
 #define RISCV_NOP4	".4byte 0x00000013"
+#else
+#define RISCV_PAUSE	".insn 0x100000f"
+#define ZAWRS_WRS_NTO	".insn 0x00d00073"
+#define ZAWRS_WRS_STO	".insn 0x01d00073"
+#define RISCV_NOP4	".insn 0x00000013"
+#endif
 
 #define RISCV_INSN_NOP4	_AC(0x00000013, U)
 
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 06/18] kconfig: remove CONFIG_COMAPT for big-endian
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (4 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 05/18] riscv: asm: use .insn for making custom instructioons Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 07/18] riscv: fixup use of natural endian on instructions Ben Dooks
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

This code does not currently build with big-endian so disable it for now.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index ca5cd4cf6e15..74e01d4de86b 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -1119,7 +1119,7 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
 config COMPAT
 	bool "Kernel support for 32-bit U-mode"
 	default 64BIT
-	depends on 64BIT && MMU
+	depends on 64BIT && MMU && !CPU_BIG_ENDIAN
 	help
 	  This option enables support for a 32-bit U-mode running under a 64-bit
 	  kernel at S-mode. riscv32-specific components such as system calls,
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 07/18] riscv: fixup use of natural endian on instructions
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (5 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 06/18] kconfig: remove CONFIG_COMAPT for big-endian Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 08/18] riscv: bpf: big endian fixes, updated BPF_ALU ops Ben Dooks
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

The priveldged ISA spec says that all instructions should
be treated as little endian, so if we load them from memory
we should do le{16,32}_to_cpu on these and the reverse when
storing.

This fixes jump_label, bug and related functions for big endian
builds.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/kernel/alternative.c      | 10 +++++++++-
 arch/riscv/kernel/cfi.c              |  3 ++-
 arch/riscv/kernel/jump_label.c       |  5 ++++-
 arch/riscv/kernel/traps.c            |  2 ++
 arch/riscv/kernel/traps_misaligned.c |  3 +++
 5 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c
index 7eb3cb1215c6..059bbd800112 100644
--- a/arch/riscv/kernel/alternative.c
+++ b/arch/riscv/kernel/alternative.c
@@ -62,11 +62,16 @@ static void riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info
 	}
 }
 
+static u32 get_u16(u16 *ptr)
+{
+	return le16_to_cpu(*ptr);
+}
+
 static u32 riscv_instruction_at(void *p)
 {
 	u16 *parcel = p;
 
-	return (u32)parcel[0] | (u32)parcel[1] << 16;
+	return (u32)get_u16(parcel+0) | (u32)get_u16(parcel+1) << 16;
 }
 
 static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,
@@ -83,6 +88,8 @@ static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,
 	riscv_insn_insert_utype_itype_imm(&call[0], &call[1], imm);
 
 	/* patch the call place again */
+	call[0] = cpu_to_le32(call[0]);
+	call[1] = cpu_to_le32(call[1]);
 	patch_text_nosync(ptr, call, sizeof(u32) * 2);
 }
 
@@ -98,6 +105,7 @@ static void riscv_alternative_fix_jal(void *ptr, u32 jal_insn, int patch_offset)
 	riscv_insn_insert_jtype_imm(&jal_insn, imm);
 
 	/* patch the call place again */
+	jal_insn = cpu_to_le32(jal_insn);
 	patch_text_nosync(ptr, &jal_insn, sizeof(u32));
 }
 
diff --git a/arch/riscv/kernel/cfi.c b/arch/riscv/kernel/cfi.c
index 6ec9dbd7292e..ee633094d855 100644
--- a/arch/riscv/kernel/cfi.c
+++ b/arch/riscv/kernel/cfi.c
@@ -37,15 +37,16 @@ static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,
 	 */
 	if (get_kernel_nofault(insn, (void *)regs->epc - 4))
 		return false;
+	insn = le32_to_cpu(insn);
 	if (!riscv_insn_is_beq(insn))
 		return false;
-
 	*type = (u32)regs_ptr[RV_EXTRACT_RS1_REG(insn)];
 
 	if (get_kernel_nofault(insn, (void *)regs->epc) ||
 	    get_kernel_nofault(insn, (void *)regs->epc + GET_INSN_LENGTH(insn)))
 		return false;
 
+	insn = le32_to_cpu(insn);
 	if (riscv_insn_is_jalr(insn))
 		rs1_num = RV_EXTRACT_RS1_REG(insn);
 	else if (riscv_insn_is_c_jalr(insn))
diff --git a/arch/riscv/kernel/jump_label.c b/arch/riscv/kernel/jump_label.c
index b4c1a6a3fbd2..ed9bc7f7d319 100644
--- a/arch/riscv/kernel/jump_label.c
+++ b/arch/riscv/kernel/jump_label.c
@@ -19,7 +19,7 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
 				     enum jump_label_type type)
 {
 	void *addr = (void *)jump_entry_code(entry);
-	u32 insn;
+	__le32 insn;
 
 	if (type == JUMP_LABEL_JMP) {
 		long offset = jump_entry_target(entry) - jump_entry_code(entry);
@@ -36,6 +36,9 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
 		insn = RISCV_INSN_NOP4;
 	}
 
+
+	insn = cpu_to_le32(insn);
+
 	if (early_boot_irqs_disabled) {
 		riscv_patch_in_stop_machine = 1;
 		patch_insn_write(addr, &insn, sizeof(insn));
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 80230de167de..b8b3b6073cf7 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -263,6 +263,7 @@ static inline unsigned long get_break_insn_length(unsigned long pc)
 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
 		return 0;
 
+	insn = le32_to_cpu(insn);
 	return GET_INSN_LENGTH(insn);
 }
 
@@ -411,6 +412,7 @@ int is_valid_bugaddr(unsigned long pc)
 		return 0;
 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
 		return 0;
+	insn = cpu_to_le32(insn);
 	if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
 		return (insn == __BUG_INSN_32);
 	else
diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c
index f760e4fcc052..f11c14bdf73a 100644
--- a/arch/riscv/kernel/traps_misaligned.c
+++ b/arch/riscv/kernel/traps_misaligned.c
@@ -299,6 +299,7 @@ static inline int get_insn(struct pt_regs *regs, ulong epc, ulong *r_insn)
 		 * below with the upper 16 bits half.
 		 */
 		insn &= GENMASK(15, 0);
+		insn = le16_to_cpu(insn);
 		if ((insn & __INSN_LENGTH_MASK) != __INSN_LENGTH_32) {
 			*r_insn = insn;
 			return 0;
@@ -306,12 +307,14 @@ static inline int get_insn(struct pt_regs *regs, ulong epc, ulong *r_insn)
 		epc += sizeof(u16);
 		if (__read_insn(regs, tmp, epc, u16))
 			return -EFAULT;
+		tmp = le16_to_cpu(tmp);
 		*r_insn = (tmp << 16) | insn;
 
 		return 0;
 	} else {
 		if (__read_insn(regs, insn, epc, u32))
 			return -EFAULT;
+		insn = le32_to_cpu(insn);
 		if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) {
 			*r_insn = insn;
 			return 0;
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 08/18] riscv: bpf: big endian fixes, updated BPF_ALU ops
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (6 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 07/18] riscv: fixup use of natural endian on instructions Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 09/18] riscv: probes: sort out endian-ness Ben Dooks
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

If running big endian then the instruction stream needs to
be written le16/le323 and the BPF BSWAP instrictions need
to correctly set the endian.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/net/bpf_jit.h        | 14 +++++++++-----
 arch/riscv/net/bpf_jit_comp64.c | 29 +++++++++++++++++------------
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/arch/riscv/net/bpf_jit.h b/arch/riscv/net/bpf_jit.h
index e7b032dfd17f..0191db443485 100644
--- a/arch/riscv/net/bpf_jit.h
+++ b/arch/riscv/net/bpf_jit.h
@@ -28,6 +28,12 @@ static inline bool rvzbb_enabled(void)
 	return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);
 }
 
+static inline bool alu_end_should_swap(u32 code)
+{
+	u32 endian = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? BPF_FROM_BE : BPF_FROM_LE;
+	return (code & BPF_FROM_BE) != endian;
+}
+
 enum {
 	RV_REG_ZERO =	0,	/* The constant value 0 */
 	RV_REG_RA =	1,	/* Return address */
@@ -117,10 +123,8 @@ static inline void bpf_flush_icache(void *start, void *end)
 /* Emit a 4-byte riscv instruction. */
 static inline void emit(const u32 insn, struct rv_jit_context *ctx)
 {
-	if (ctx->insns) {
-		ctx->insns[ctx->ninsns] = insn;
-		ctx->insns[ctx->ninsns + 1] = (insn >> 16);
-	}
+	if (ctx->insns)
+		put_unaligned_le32(insn, ctx->insns+ctx->ninsns);
 
 	ctx->ninsns += 2;
 }
@@ -131,7 +135,7 @@ static inline void emitc(const u16 insn, struct rv_jit_context *ctx)
 	BUILD_BUG_ON(!rvc_enabled());
 
 	if (ctx->insns)
-		ctx->insns[ctx->ninsns] = insn;
+		ctx->insns[ctx->ninsns] = cpu_to_le16(insn);
 
 	ctx->ninsns++;
 }
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 10e01ff06312..946cf2ab8cfa 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -1479,20 +1479,25 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 
 	/* dst = BSWAP##imm(dst) */
 	case BPF_ALU | BPF_END | BPF_FROM_LE:
-		switch (imm) {
-		case 16:
-			emit_zexth(rd, rd, ctx);
-			break;
-		case 32:
-			if (!aux->verifier_zext)
-				emit_zextw(rd, rd, ctx);
-			break;
-		case 64:
-			/* Do nothing */
-			break;
+	case BPF_ALU | BPF_END | BPF_FROM_BE:
+		if (alu_end_should_swap(code)) {
+			emit_bswap(rd, imm, ctx);
+		} else {
+			switch (imm) {
+			case 16:
+				emit_zexth(rd, rd, ctx);
+				break;
+			case 32:
+				if (!aux->verifier_zext)
+					emit_zextw(rd, rd, ctx);
+				break;
+			case 64:
+				/* Do nothing */
+				break;
+			}
 		}
 		break;
-	case BPF_ALU | BPF_END | BPF_FROM_BE:
+
 	case BPF_ALU64 | BPF_END | BPF_FROM_LE:
 		emit_bswap(rd, imm, ctx);
 		break;
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 09/18] riscv: probes: sort out endian-ness
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (7 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 08/18] riscv: bpf: big endian fixes, updated BPF_ALU ops Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 10/18] riscv: ftrace big endian updates Ben Dooks
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Updated {k,u}probe code to deal with big endian mode where
the instruction stream is always in little endian.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/kernel/probes/decode-insn.c |  2 +-
 arch/riscv/kernel/probes/decode-insn.h |  5 +++++
 arch/riscv/kernel/probes/kprobes.c     | 30 ++++++++++++++++----------
 arch/riscv/kernel/probes/uprobes.c     | 10 ++++-----
 4 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/arch/riscv/kernel/probes/decode-insn.c b/arch/riscv/kernel/probes/decode-insn.c
index 65d9590bfb9f..5f30c10f7d8d 100644
--- a/arch/riscv/kernel/probes/decode-insn.c
+++ b/arch/riscv/kernel/probes/decode-insn.c
@@ -16,7 +16,7 @@
 enum probe_insn __kprobes
 riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 {
-	probe_opcode_t insn = *addr;
+	probe_opcode_t insn = le32_to_cpu(*addr);
 
 	/*
 	 * Reject instructions list:
diff --git a/arch/riscv/kernel/probes/decode-insn.h b/arch/riscv/kernel/probes/decode-insn.h
index 42269a7d676d..0515deb204b5 100644
--- a/arch/riscv/kernel/probes/decode-insn.h
+++ b/arch/riscv/kernel/probes/decode-insn.h
@@ -15,4 +15,9 @@ enum probe_insn {
 enum probe_insn __kprobes
 riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *asi);
 
+static inline int read_insn_length(void *ptr)
+{
+	return GET_INSN_LENGTH(le16_to_cpu(*(__le16 *)ptr));
+}
+
 #endif /* _RISCV_KERNEL_KPROBES_DECODE_INSN_H */
diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index c0738d6c6498..9d496b15d18d 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -24,13 +24,13 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
-	size_t len = GET_INSN_LENGTH(p->opcode);
-	u32 insn = __BUG_INSN_32;
+	size_t len = read_insn_length(&p->opcode);
+	u32 insn = cpu_to_le32(__BUG_INSN_32);
 
 	p->ainsn.api.restore = (unsigned long)p->addr + len;
 
 	patch_text_nosync(p->ainsn.api.insn, &p->opcode, len);
-	patch_text_nosync((void *)p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn));
+	patch_text_nosync((void *)p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(__BUG_INSN_32));
 }
 
 static void __kprobes arch_prepare_simulate(struct kprobe *p)
@@ -58,7 +58,7 @@ static bool __kprobes arch_check_kprobe(struct kprobe *p)
 		if (tmp == addr)
 			return true;
 
-		tmp += GET_INSN_LENGTH(*(u16 *)tmp);
+		tmp += read_insn_length((u16 *)tmp);
 	}
 
 	return false;
@@ -75,9 +75,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 		return -EILSEQ;
 
 	/* copy instruction */
-	p->opcode = (kprobe_opcode_t)(*insn++);
-	if (GET_INSN_LENGTH(p->opcode) == 4)
-		p->opcode |= (kprobe_opcode_t)(*insn) << 16;
+	*((u16 *)&p->opcode) = (*insn++);
+	if (read_insn_length(&p->opcode) == 4)
+		p->opcode = (kprobe_opcode_t)(*(u32 *)p->addr);
 
 	/* decode instruction */
 	switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
@@ -107,16 +107,24 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 /* install breakpoint in text */
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
-	size_t len = GET_INSN_LENGTH(p->opcode);
-	u32 insn = len == 4 ? __BUG_INSN_32 : __BUG_INSN_16;
+	size_t len = read_insn_length(&p->opcode);
+	u32 insn;
+
+	if (len == 4)
+		insn = cpu_to_le32(__BUG_INSN_32);
+	else {
+		insn = cpu_to_le16(__BUG_INSN_16);
+		insn |= cpu_to_le16(__BUG_INSN_16) << 16;
+	}
 
+	pr_info("%s: patching %px (%d bytes)\n", __func__, p->addr, (int)len);
 	patch_text(p->addr, &insn, len);
 }
 
 /* remove breakpoint from text */
 void __kprobes arch_disarm_kprobe(struct kprobe *p)
 {
-	size_t len = GET_INSN_LENGTH(p->opcode);
+	size_t len = read_insn_length(&p->opcode);
 
 	patch_text(p->addr, &p->opcode, len);
 }
@@ -336,7 +344,7 @@ kprobe_single_step_handler(struct pt_regs *regs)
 	struct kprobe *cur = kprobe_running();
 
 	if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
-	    ((unsigned long)&cur->ainsn.api.insn[0] + GET_INSN_LENGTH(cur->opcode) == addr)) {
+	    ((unsigned long)&cur->ainsn.api.insn[0] + read_insn_length(&cur->opcode) == addr)) {
 		kprobes_restore_local_irqflag(kcb, regs);
 		post_kprobe_handler(cur, kcb, regs);
 		return true;
diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
index cc15f7ca6cc1..ffdda8771380 100644
--- a/arch/riscv/kernel/probes/uprobes.c
+++ b/arch/riscv/kernel/probes/uprobes.c
@@ -12,9 +12,9 @@
 bool is_swbp_insn(uprobe_opcode_t *insn)
 {
 #ifdef CONFIG_RISCV_ISA_C
-	return (*insn & 0xffff) == UPROBE_SWBP_INSN;
+	return (*(u16 *)insn) == cpu_to_le16(UPROBE_SWBP_INSN);
 #else
-	return *insn == UPROBE_SWBP_INSN;
+	return *insn == cpu_to_le32(UPROBE_SWBP_INSN);
 #endif
 }
 
@@ -35,7 +35,7 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
 
 	opcode = *(probe_opcode_t *)(&auprobe->insn[0]);
 
-	auprobe->insn_size = GET_INSN_LENGTH(opcode);
+	auprobe->insn_size = GET_INSN_LENGTH(le32_to_cpu(opcode));
 
 	switch (riscv_probe_decode_insn(&opcode, &auprobe->api)) {
 	case INSN_REJECTED:
@@ -173,8 +173,8 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
 
 	/* Add ebreak behind opcode to simulate singlestep */
 	if (vaddr) {
-		dst += GET_INSN_LENGTH(*(probe_opcode_t *)src);
-		*(uprobe_opcode_t *)dst = __BUG_INSN_32;
+		dst += read_insn_length(src);
+		*(uprobe_opcode_t *)dst = cpu_to_le32(__BUG_INSN_32);
 	}
 
 	flush_icache_range(start, start + len);
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 10/18] riscv: ftrace big endian updates
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (8 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 09/18] riscv: probes: sort out endian-ness Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 11/18] riscv: traps: make insn fetch common in unknown instruction Ben Dooks
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Make the ftrace code work with big endian by ensuring the
instruction stream is accessed little endian.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/include/asm/ftrace.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h
index 22ebea3c2b26..8d004639406b 100644
--- a/arch/riscv/include/asm/ftrace.h
+++ b/arch/riscv/include/asm/ftrace.h
@@ -99,8 +99,8 @@ struct dyn_arch_ftrace {
 do {									\
 	unsigned int offset =						\
 		(unsigned long) (callee) - (unsigned long) (caller);	\
-	call[0] = to_auipc_t0(offset);					\
-	call[1] = to_jalr_t0(offset);					\
+	call[0] = cpu_to_le32(to_auipc_t0(offset));			\
+	call[1] = cpu_to_le32(to_jalr_t0(offset));			\
 } while (0)
 
 /*
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 11/18] riscv: traps: make insn fetch common in unknown instruction
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (9 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 10/18] riscv: ftrace big endian updates Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 12/18] riscv: threads need UBE flag setting if big-endian Ben Dooks
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Add the trapped instruction (insn) as the second argument to
riscv_v_first_use_handler() from the trap handler so when we
add more handlers we can do the fetch of the instruction just
once.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
- fixed wording of patch from rfc
v2:
  - fixed todo by going to illegal instruction error if get_user fails
  - added pointer print for failed read
  - fixed issues with rebasing onto main branch
v3:
  - removed print from v2
  - rebased onto latest kernel
---
 arch/riscv/include/asm/vector.h |  4 ++--
 arch/riscv/kernel/traps.c       |  7 +++++--
 arch/riscv/kernel/vector.c      | 11 +----------
 3 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h
index b61786d43c20..7b53aa66d5d4 100644
--- a/arch/riscv/include/asm/vector.h
+++ b/arch/riscv/include/asm/vector.h
@@ -43,7 +43,7 @@
 extern unsigned long riscv_v_vsize;
 int riscv_v_setup_vsize(void);
 bool insn_is_vector(u32 insn_buf);
-bool riscv_v_first_use_handler(struct pt_regs *regs);
+bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn);
 void kernel_vector_begin(void);
 void kernel_vector_end(void);
 void get_cpu_vector_context(void);
@@ -410,7 +410,7 @@ static __always_inline bool has_vector(void) { return false; }
 static __always_inline bool insn_is_vector(u32 insn_buf) { return false; }
 static __always_inline bool has_xtheadvector_no_alternatives(void) { return false; }
 static __always_inline bool has_xtheadvector(void) { return false; }
-static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; }
+static inline bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn) { return false; }
 static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
 static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; }
 #define riscv_v_vsize (0)
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index b8b3b6073cf7..040cb2e7c511 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -172,13 +172,16 @@ DO_ERROR_INFO(do_trap_insn_fault,
 
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
 {
-	bool handled;
+	bool handled = false;
 
 	if (user_mode(regs)) {
+		u32 insn = (u32)regs->badaddr;
+
 		irqentry_enter_from_user_mode(regs);
 		local_irq_enable();
 
-		handled = riscv_v_first_use_handler(regs);
+		handled = riscv_v_first_use_handler(regs, insn);
+
 		if (!handled)
 			do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
 				      "Oops - illegal instruction");
diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c
index 184f780c932d..6cd95326bc16 100644
--- a/arch/riscv/kernel/vector.c
+++ b/arch/riscv/kernel/vector.c
@@ -178,11 +178,8 @@ bool riscv_v_vstate_ctrl_user_allowed(void)
 }
 EXPORT_SYMBOL_GPL(riscv_v_vstate_ctrl_user_allowed);
 
-bool riscv_v_first_use_handler(struct pt_regs *regs)
+bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn)
 {
-	u32 __user *epc = (u32 __user *)regs->epc;
-	u32 insn = (u32)regs->badaddr;
-
 	if (!(has_vector() || has_xtheadvector()))
 		return false;
 
@@ -194,12 +191,6 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
 	if (riscv_v_vstate_query(regs))
 		return false;
 
-	/* Get the instruction */
-	if (!insn) {
-		if (__get_user(insn, epc))
-			return false;
-	}
-
 	/* Filter out non-V instructions */
 	if (!insn_is_vector(insn))
 		return false;
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 12/18] riscv: threads need UBE flag setting if big-endian
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (10 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 11/18] riscv: traps: make insn fetch common in unknown instruction Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 13/18] riscv: fixes for big-endian library routines Ben Dooks
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

If operating in big-endian, start all threads with UBE to keep
them the same endian as the system.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/include/asm/csr.h | 1 +
 arch/riscv/kernel/process.c  | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
index 6fed42e37705..78f795936c7e 100644
--- a/arch/riscv/include/asm/csr.h
+++ b/arch/riscv/include/asm/csr.h
@@ -13,6 +13,7 @@
 #define SR_SIE		_AC(0x00000002, UL) /* Supervisor Interrupt Enable */
 #define SR_MIE		_AC(0x00000008, UL) /* Machine Interrupt Enable */
 #define SR_SPIE		_AC(0x00000020, UL) /* Previous Supervisor IE */
+#define SR_UBE		_AC(0x00000040, UL) /* User mode is big-endian */
 #define SR_MPIE		_AC(0x00000080, UL) /* Previous Machine IE */
 #define SR_SPP		_AC(0x00000100, UL) /* Previously Supervisor */
 #define SR_MPP		_AC(0x00001800, UL) /* Previously Machine */
diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c
index a0a40889d79a..c4e335b5e5f3 100644
--- a/arch/riscv/kernel/process.c
+++ b/arch/riscv/kernel/process.c
@@ -144,6 +144,8 @@ void start_thread(struct pt_regs *regs, unsigned long pc,
 	unsigned long sp)
 {
 	regs->status = SR_PIE;
+	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
+		regs->status |= SR_UBE;
 	if (has_fpu()) {
 		regs->status |= SR_FS_INITIAL;
 		/*
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 13/18] riscv: fixes for big-endian library routines
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (11 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 12/18] riscv: threads need UBE flag setting if big-endian Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 14/18] riscv: update to add ASM_INSN for .2byte instructions Ben Dooks
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Update the unaligned handling of memory when in big-endian
and the byte positions inside a loaded word change. Do this
by wrapping the shift macros and swapping the left and right
shfits.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/lib/memmove.S | 28 ++++++++++++++++++++--------
 arch/riscv/lib/uaccess.S | 16 ++++++++++++++--
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/arch/riscv/lib/memmove.S b/arch/riscv/lib/memmove.S
index cb3e2e7ef0ba..6971006a8eb6 100644
--- a/arch/riscv/lib/memmove.S
+++ b/arch/riscv/lib/memmove.S
@@ -6,6 +6,18 @@
 #include <linux/linkage.h>
 #include <asm/asm.h>
 
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define M_SLL sll
+#define M_SRL srl
+#else
+	/* in big-endian, swap the direction of shifts to deal
+	 * with the fact the order of bytes in a word has changed
+	 * from ABCD to DCBA.
+	 */
+#define M_SLL srl
+#define M_SRL sll
+#endif
+
 SYM_FUNC_START(__memmove)
 	/*
 	 * Returns
@@ -133,8 +145,8 @@ SYM_FUNC_START(__memmove)
 	1:
 	REG_L t1, (1 * SZREG)(a1)
 	addi  t3, t3, (2 * SZREG)
-	srl   t0, t0, a6
-	sll   t2, t1, a7
+	M_SRL t0, t0, a6
+	M_SLL t2, t1, a7
 	or    t2, t0, t2
 	REG_S t2, ((0 * SZREG) - (2 * SZREG))(t3)
 
@@ -142,8 +154,8 @@ SYM_FUNC_START(__memmove)
 
 	REG_L t0, (2 * SZREG)(a1)
 	addi  a1, a1, (2 * SZREG)
-	srl   t1, t1, a6
-	sll   t2, t0, a7
+	M_SRL t1, t1, a6
+	M_SLL t2, t0, a7
 	or    t2, t1, t2
 	REG_S t2, ((1 * SZREG) - (2 * SZREG))(t3)
 
@@ -195,8 +207,8 @@ SYM_FUNC_START(__memmove)
 	1:
 	REG_L t0, (-1 * SZREG)(a4)
 	addi  t4, t4, (-2 * SZREG)
-	sll   t1, t1, a7
-	srl   t2, t0, a6
+	M_SLL t1, t1, a7
+	M_SRL t2, t0, a6
 	or    t2, t1, t2
 	REG_S t2, ( 1 * SZREG)(t4)
 
@@ -204,8 +216,8 @@ SYM_FUNC_START(__memmove)
 
 	REG_L t1, (-2 * SZREG)(a4)
 	addi  a4, a4, (-2 * SZREG)
-	sll   t0, t0, a7
-	srl   t2, t1, a6
+	M_SLL t0, t0, a7
+	M_SRL t2, t1, a6
 	or    t2, t0, t2
 	REG_S t2, ( 0 * SZREG)(t4)
 
diff --git a/arch/riscv/lib/uaccess.S b/arch/riscv/lib/uaccess.S
index 4efea1b3326c..71ee4811db68 100644
--- a/arch/riscv/lib/uaccess.S
+++ b/arch/riscv/lib/uaccess.S
@@ -6,6 +6,18 @@
 #include <asm/hwcap.h>
 #include <asm/alternative-macros.h>
 
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define M_SLL sll
+#define M_SRL srl
+#else
+	/* in big-endian, swap the direction of shifts to deal
+	 * with the fact the order of bytes in a word has changed
+	 * from ABCD to DCBA.
+	 */
+#define M_SLL srl
+#define M_SRL sll
+#endif
+
 	.macro fixup op reg addr lbl
 100:
 	\op \reg, \addr
@@ -178,10 +190,10 @@ SYM_FUNC_START(fallback_scalar_usercopy_sum_enabled)
 	 */
 
 	/* At least one iteration will be executed */
-	srl	a4, a5, t3
+	M_SRL	a4, a5, t3
 	fixup REG_L   a5, SZREG(a1), 10f
 	addi	a1, a1, SZREG
-	sll	a2, a5, t4
+	M_SLL	a2, a5, t4
 	or	a2, a2, a4
 	fixup REG_S   a2, 0(a0), 10f
 	addi	a0, a0, SZREG
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 14/18] riscv: update to add ASM_INSN for .2byte instructions
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (12 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 13/18] riscv: fixes for big-endian library routines Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 15/18] KVM: riscv: set HSTATUS big endian same as build Ben Dooks
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

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

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/include/asm/asm.h      |  6 ++++++
 arch/riscv/include/asm/insn-def.h | 15 ++++-----------
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h
index a8a2af6dfe9d..9f6245b62925 100644
--- a/arch/riscv/include/asm/asm.h
+++ b/arch/riscv/include/asm/asm.h
@@ -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
diff --git a/arch/riscv/include/asm/insn-def.h b/arch/riscv/include/asm/insn-def.h
index 2a3019bc1a3b..0677d3bee40c 100644
--- a/arch/riscv/include/asm/insn-def.h
+++ b/arch/riscv/include/asm/insn-def.h
@@ -256,17 +256,10 @@
 	INSN_S(OPCODE_OP_IMM, FUNC3(6), __RS2(3),		\
 	       SIMM12((offset) & 0xfe0), RS1(base))
 
-#ifndef CONFIG_AS_HAS_INSN
-#define RISCV_PAUSE	".4byte 0x100000f"
-#define ZAWRS_WRS_NTO	".4byte 0x00d00073"
-#define ZAWRS_WRS_STO	".4byte 0x01d00073"
-#define RISCV_NOP4	".4byte 0x00000013"
-#else
-#define RISCV_PAUSE	".insn 0x100000f"
-#define ZAWRS_WRS_NTO	".insn 0x00d00073"
-#define ZAWRS_WRS_STO	".insn 0x01d00073"
-#define RISCV_NOP4	".insn 0x00000013"
-#endif
+#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)
 
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 15/18] KVM: riscv: set HSTATUS big endian same as build
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (13 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 14/18] riscv: update to add ASM_INSN for .2byte instructions Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 16/18] KVM: riscv: add hstatus to allow endian control Ben Dooks
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

Start the guest in the same endian as the host.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/kvm/vcpu.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index f001e56403f9..b47f12792f9d 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -75,6 +75,10 @@ static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu,
 	cntx->hstatus |= HSTATUS_VTW;
 	cntx->hstatus |= HSTATUS_SPVP;
 	cntx->hstatus |= HSTATUS_SPV;
+
+	/* If configured big-endian, start guest in big endian */
+	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
+		cntx->hstatus |= HSTATUS_VSBE;
 }
 
 static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu, bool kvm_sbi_reset)
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 16/18] KVM: riscv: add hstatus to allow endian control
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (14 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 15/18] KVM: riscv: set HSTATUS big endian same as build Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 17/18] KVM: riscv: instructions are little endian Ben Dooks
  2025-08-22 16:52 ` [PATCH 18/18] KVM: riscv: add warning if undecoded instruction Ben Dooks
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

---
 arch/riscv/include/uapi/asm/kvm.h | 1 +
 arch/riscv/kvm/vcpu_onereg.c      | 9 +++++++++
 2 files changed, 10 insertions(+)

diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h
index ef27d4289da1..4cec75665f55 100644
--- a/arch/riscv/include/uapi/asm/kvm.h
+++ b/arch/riscv/include/uapi/asm/kvm.h
@@ -81,6 +81,7 @@ struct kvm_riscv_csr {
 	unsigned long satp;
 	unsigned long scounteren;
 	unsigned long senvcfg;
+	unsigned long hstatus;
 };
 
 /* AIA CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
diff --git a/arch/riscv/kvm/vcpu_onereg.c b/arch/riscv/kvm/vcpu_onereg.c
index cce6a38ea54f..25c1e43f80bc 100644
--- a/arch/riscv/kvm/vcpu_onereg.c
+++ b/arch/riscv/kvm/vcpu_onereg.c
@@ -484,6 +484,7 @@ static int kvm_riscv_vcpu_general_get_csr(struct kvm_vcpu *vcpu,
 					  unsigned long reg_num,
 					  unsigned long *out_val)
 {
+	struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
 	struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
 
 	if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long))
@@ -493,6 +494,8 @@ static int kvm_riscv_vcpu_general_get_csr(struct kvm_vcpu *vcpu,
 		kvm_riscv_vcpu_flush_interrupts(vcpu);
 		*out_val = (csr->hvip >> VSIP_TO_HVIP_SHIFT) & VSIP_VALID_MASK;
 		*out_val |= csr->hvip & ~IRQ_LOCAL_MASK;
+	} else if (reg_num == KVM_REG_RISCV_CSR_REG(hstatus)) {
+		*out_val = cntx->hstatus;
 	} else
 		*out_val = ((unsigned long *)csr)[reg_num];
 
@@ -503,6 +506,7 @@ static int kvm_riscv_vcpu_general_set_csr(struct kvm_vcpu *vcpu,
 					  unsigned long reg_num,
 					  unsigned long reg_val)
 {
+	struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
 	struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
 
 	if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long))
@@ -511,6 +515,11 @@ static int kvm_riscv_vcpu_general_set_csr(struct kvm_vcpu *vcpu,
 	if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) {
 		reg_val &= VSIP_VALID_MASK;
 		reg_val <<= VSIP_TO_HVIP_SHIFT;
+	} else if (reg_num == KVM_REG_RISCV_CSR_REG(hstatus)) {
+		/* for now only allow the S level endianness */
+		cntx->hstatus &= ~HSTATUS_VSBE;
+		cntx->hstatus |= reg_val & HSTATUS_VSBE;
+		return 0;
 	}
 
 	((unsigned long *)csr)[reg_num] = reg_val;
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 17/18] KVM: riscv: instructions are little endian
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (15 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 16/18] KVM: riscv: add hstatus to allow endian control Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  2025-08-22 16:52 ` [PATCH 18/18] KVM: riscv: add warning if undecoded instruction Ben Dooks
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

The instructions are always little endian, so ensure the
correct instruction endian.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/kvm/vcpu_insn.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 97dec18e6989..b1edc7b10d7a 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -453,6 +453,8 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,
 				kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
 				return 1;
 			}
+
+			insn = le32_to_cpu(insn);
 		}
 		if (INSN_IS_16BIT(insn))
 			return truly_illegal_insn(vcpu, run, insn);
@@ -509,6 +511,8 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
 			kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
 			return 1;
 		}
+
+		insn = le32_to_cpu(insn);
 		insn_len = INSN_LEN(insn);
 	}
 
@@ -635,6 +639,8 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
 			kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
 			return 1;
 		}
+
+		insn = le32_to_cpu(insn);
 		insn_len = INSN_LEN(insn);
 	}
 
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 18/18] KVM: riscv: add warning if undecoded instruction
  2025-08-22 16:52 RISC-V big-endian support Ben Dooks
                   ` (16 preceding siblings ...)
  2025-08-22 16:52 ` [PATCH 17/18] KVM: riscv: instructions are little endian Ben Dooks
@ 2025-08-22 16:52 ` Ben Dooks
  17 siblings, 0 replies; 19+ messages in thread
From: Ben Dooks @ 2025-08-22 16:52 UTC (permalink / raw)
  To: linux-riscv, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: -linux-kernel, Alexandre Ghiti, Javier Jardon, Ben Dooks,
	Lawrence Hunter, Roan Richmod, Sam Grove, Chao-ying Fu,
	Mukunda Aprameya, Umesh Kalappa, Djordje Todorovic, Kurt Martin,
	allikarjuna Gouda, Vaibhav Chauthmal, wapnil Agrawal, Ben Dooks

It seems a good idea if we get an undecoded instruction here to
at-least print an error.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 arch/riscv/kvm/vcpu_insn.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index b1edc7b10d7a..ce6ef358c9c2 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -557,6 +557,8 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
 		len = 4;
 		shift = 8 * (sizeof(ulong) - len);
 	} else {
+		pr_err("%s: insn 0x%08lx not decoded at %lx\n",
+		       __func__, insn, vcpu->arch.guest_context.sepc);
 		return -EOPNOTSUPP;
 	}
 
@@ -674,6 +676,8 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
 		len = 4;
 		data32 = GET_RS2C(insn, &vcpu->arch.guest_context);
 	} else {
+		pr_err("%s: insn 0x%08lx not decoded at %lx\n",
+		       __func__, insn, vcpu->arch.guest_context.sepc);
 		return -EOPNOTSUPP;
 	}
 
-- 
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2025-08-23  9:36 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-22 16:52 RISC-V big-endian support Ben Dooks
2025-08-22 16:52 ` [PATCH 01/18] riscv: add initial kconfig and build flags for big-endian Ben Dooks
2025-08-22 16:52 ` [PATCH 02/18] riscv: update byteorder.h " Ben Dooks
2025-08-22 16:52 ` [PATCH 03/18] riscv: disable vector if big-endian, gcc unsupported option Ben Dooks
2025-08-22 16:52 ` [PATCH 04/18] riscv: word-at-atime: move to generic if we're big endian Ben Dooks
2025-08-22 16:52 ` [PATCH 05/18] riscv: asm: use .insn for making custom instructioons Ben Dooks
2025-08-22 16:52 ` [PATCH 06/18] kconfig: remove CONFIG_COMAPT for big-endian Ben Dooks
2025-08-22 16:52 ` [PATCH 07/18] riscv: fixup use of natural endian on instructions Ben Dooks
2025-08-22 16:52 ` [PATCH 08/18] riscv: bpf: big endian fixes, updated BPF_ALU ops Ben Dooks
2025-08-22 16:52 ` [PATCH 09/18] riscv: probes: sort out endian-ness Ben Dooks
2025-08-22 16:52 ` [PATCH 10/18] riscv: ftrace big endian updates Ben Dooks
2025-08-22 16:52 ` [PATCH 11/18] riscv: traps: make insn fetch common in unknown instruction Ben Dooks
2025-08-22 16:52 ` [PATCH 12/18] riscv: threads need UBE flag setting if big-endian Ben Dooks
2025-08-22 16:52 ` [PATCH 13/18] riscv: fixes for big-endian library routines Ben Dooks
2025-08-22 16:52 ` [PATCH 14/18] riscv: update to add ASM_INSN for .2byte instructions Ben Dooks
2025-08-22 16:52 ` [PATCH 15/18] KVM: riscv: set HSTATUS big endian same as build Ben Dooks
2025-08-22 16:52 ` [PATCH 16/18] KVM: riscv: add hstatus to allow endian control Ben Dooks
2025-08-22 16:52 ` [PATCH 17/18] KVM: riscv: instructions are little endian Ben Dooks
2025-08-22 16:52 ` [PATCH 18/18] KVM: riscv: add warning if undecoded instruction Ben Dooks

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).