Skip to contentSkip to conversation

Zbb string optimizations and call support in alternatives

PATCH Superseded Heiko Stuebner 8 participants 51 messages Nov 10, 2022 – Nov 25, 2022 1 message not in the archive

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Zbb string optimizations and call support in alternatives

From: Heiko Stuebner <[email protected]>

The Zbb extension can be used to make string functions run a lot faster.

To allow There are essentially two problems to solve:
- making it possible for str* functions to replace what they do in a performant way

This is done by inlining the core functions and then using alternatives to call the actual variant.

This of course will need a more intelligent selection mechanism down the road when more variants may exist using different available extensions.

- actually allowing calls in alternatives
Function calls use auipc + jalr to reach those 32bit relative addresses but when they're compiled the offset will be wrong as alternatives live in a different section. So when the patch gets applied the address will point to the wrong location.

So similar to arm64 the target addresses need to be updated.

This is probably also helpful for other things needing more complex code in alternatives.

In my half-scientific test-case of running the functions in question on a 95 character string in a loop of 10000 iterations, the Zbb variants shave off around 2/3 of the original runtime.

changes since rfc:
- make Zbb code actually work
- drop some unneeded patches
- a lot of cleanups

Heiko Stuebner (7):
efi/riscv: libstub: mark when compiling libstub
RISC-V: add auipc elements to parse_asm header
RISC-V: add U-type imm parsing to parse_asm header
RISC-V: add rd reg parsing to parse_asm header
RISC-V: fix auipc-jalr addresses in patched alternatives
RISC-V: add infrastructure to allow different str* implementations
RISC-V: add zbb support to string functions

 arch/riscv/Kconfig                    |  23 ++++++
 arch/riscv/include/asm/errata_list.h  |   3 +-
 arch/riscv/include/asm/hwcap.h        |   1 +
 arch/riscv/include/asm/parse_asm.h    |  21 +++++
 arch/riscv/include/asm/string.h       |  83 ++++++++++++++++++++
 arch/riscv/kernel/cpu.c               |   1 +
 arch/riscv/kernel/cpufeature.c        |  97 ++++++++++++++++++++++-
 arch/riscv/kernel/image-vars.h        |   6 +-
 arch/riscv/lib/Makefile               |   6 ++
 arch/riscv/lib/strcmp.S               |  39 ++++++++++
 arch/riscv/lib/strcmp_zbb.S           |  91 ++++++++++++++++++++++
 arch/riscv/lib/strlen.S               |  29 +++++++
 arch/riscv/lib/strlen_zbb.S           |  98 ++++++++++++++++++++++++
 arch/riscv/lib/strncmp.S              |  41 ++++++++++
 arch/riscv/lib/strncmp_zbb.S          | 106 ++++++++++++++++++++++++++
 drivers/firmware/efi/libstub/Makefile |   2 +-
 16 files changed, 640 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/lib/strcmp.S
 create mode 100644 arch/riscv/lib/strcmp_zbb.S
 create mode 100644 arch/riscv/lib/strlen.S
 create mode 100644 arch/riscv/lib/strlen_zbb.S
 create mode 100644 arch/riscv/lib/strncmp.S
 create mode 100644 arch/riscv/lib/strncmp_zbb.S
Signature · 7 lines
2.35.1


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

PATCH 1/7 efi/riscv: libstub: mark when compiling libstub

From: Heiko Stuebner <[email protected]>

We want to runtime-optimize some core functions (str*, mem*) but not have this leak into libstub. Instead libstub for the short while it's running should just use the generic implementation.

To be able to determine whether functions are getting compiled as part of libstub or not, add a compile-flag we can check via #ifdef.

---
 drivers/firmware/efi/libstub/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index b1601aad7e1a..39c8e3da1937 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 cflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpic
+				   -fpic -DRISCV_EFISTUB
 cflags-$(CONFIG_LOONGARCH)	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fpie
 
Signature · 7 lines
2.35.1


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

Re: [PATCH 1/7] efi/riscv: libstub: mark when compiling libstub

Quoted 5 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

We want to runtime-optimize some core functions (str*, mem*) but not have this leak into libstub. Instead libstub for the short while it's running should just use the generic

Totally pedantic reword, mostly b/c I am an eejit and confused myself the first time reading this:

"Instead, libstub, for the short while it's running, should just use the generic implementation."

Quoted 5 lines

implementation.

To be able to determine whether functions are getting compiled as part of libstub or not, add a compile-flag we can check via #ifdef.

Exempting the stub makes sense to me given when it runs. What's the actual downside of not exempting it though?

Quoted 26 lines
---
 drivers/firmware/efi/libstub/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index b1601aad7e1a..39c8e3da1937 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 cflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpic
+				   -fpic -DRISCV_EFISTUB
 cflags-$(CONFIG_LOONGARCH)	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fpie
 
Signature · 7 lines
2.35.1


_______________________________________________
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

Re: [PATCH 1/7] efi/riscv: libstub: mark when compiling libstub

Am Sonntag, 13. November 2022, 18:16:10 CET schrieb Conor Dooley:

Quoted 13 lines · Heiko Stuebner
Quoted 5 lines

From: Heiko Stuebner <[email protected]>

We want to runtime-optimize some core functions (str*, mem*) but not have this leak into libstub. Instead libstub for the short while it's running should just use the generic

Totally pedantic reword, mostly b/c I am an eejit and confused myself the first time reading this:

"Instead, libstub, for the short while it's running, should just use the generic implementation."

Quoted 8 lines
Quoted 5 lines

implementation.

To be able to determine whether functions are getting compiled as part of libstub or not, add a compile-flag we can check via #ifdef.

Exempting the stub makes sense to me given when it runs. What's the actual downside of not exempting it though?

You run into build-errors.

I.e. the alternatives (zbb-variants in this case) get compiled so there is the reference to the _strlen_zbb function in the efi-stub, which of course is not fullfillable, hence the "magic" in patch6 :-) .

Heiko

Quoted 5 lines
Quoted 5 lines
---
 drivers/firmware/efi/libstub/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/firmware/efi/libstub/Makefile cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ · line 31

Quoted 13 lines
drivers/firmware/efi/libstub/Makefile +1 -1
index b1601aad7e1a..39c8e3da1937 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 cflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpic
+				   -fpic -DRISCV_EFISTUB
 cflags-$(CONFIG_LOONGARCH)	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fpie
 

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

Reviewed-by
#4 lore raw

Re: [PATCH 1/7] efi/riscv: libstub: mark when compiling libstub

Quoted 16 lines · Heiko Stübner

Am Sonntag, 13. November 2022, 18:16:10 CET schrieb Conor Dooley:

Quoted 15 lines · Heiko Stuebner
Quoted 5 lines

From: Heiko Stuebner <[email protected]>

We want to runtime-optimize some core functions (str*, mem*) but not have this leak into libstub. Instead libstub for the short while it's running should just use the generic

Totally pedantic reword, mostly b/c I am an eejit and confused myself the first time reading this:

"Instead, libstub, for the short while it's running, should just use the generic implementation."

Quoted 2 lines

implementation.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

To be able to determine whether functions are getting compiled as part of libstub or not, add a compile-flag we can check via #ifdef.

Exempting the stub makes sense to me given when it runs. What's the actual downside of not exempting it though?

You run into build-errors.

Ah right, I figured it had to be something more than avoiding some overhead. Might be worth noting that the purpose is to avoid build errors.

Quoted 33 lines

I.e. the alternatives (zbb-variants in this case) get compiled so there is the reference to the _strlen_zbb function in the efi-stub, which of course is not fullfillable, hence the "magic" in patch6 :-) .

Heiko

Quoted 5 lines
Quoted 5 lines
---
 drivers/firmware/efi/libstub/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/firmware/efi/libstub/Makefile cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ · line 31

Quoted 13 lines
drivers/firmware/efi/libstub/Makefile +1 -1
index b1601aad7e1a..39c8e3da1937 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 cflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpic
+				   -fpic -DRISCV_EFISTUB
 cflags-$(CONFIG_LOONGARCH)	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fpie
 
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

#5 lore raw

PATCH 2/7 RISC-V: add auipc elements to parse_asm header

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

We will want to use the opcode parsing outside kdb as well and need at least the auipc element there.

---
 arch/riscv/include/asm/parse_asm.h | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/include/asm/parse_asm.h +3 -0
index f36368de839f..c287c1426aa7 100644
@@ -100,6 +100,7 @@
 #define RVC_C2_RD_OPOFF		7
 
 /* parts of opcode for RVG*/
+#define OPCODE_AUIPC		0x17
 #define OPCODE_BRANCH		0x63
 #define OPCODE_JALR		0x67
 #define OPCODE_JAL		0x6f
@@ -129,6 +130,7 @@
 
 #define FUNCT12_SRET		0x10200000
 
+#define MATCH_AUIPC		(OPCODE_AUIPC)
 #define MATCH_JALR		(FUNCT3_JALR | OPCODE_JALR)
 #define MATCH_JAL		(OPCODE_JAL)
 #define MATCH_BEQ		(FUNCT3_BEQ | OPCODE_BRANCH)
@@ -145,6 +147,7 @@
 #define MATCH_C_JR		(FUNCT4_C_JR | OPCODE_C_2)
 #define MATCH_C_JALR		(FUNCT4_C_JALR | OPCODE_C_2)
 
+#define MASK_AUIPC		0x7f
 #define MASK_JALR		0x707f
 #define MASK_JAL		0x7f
 #define MASK_C_JALR		0xf07f
Signature · 7 lines
2.35.1


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

PATCH 3/7 RISC-V: add U-type imm parsing to parse_asm header

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Similar to other existing types, allow extracting the immediate for a U-type instruction.

U-type immediates are special in that regard, that the value in the instruction in bits [31:12] already represents the same bits of the immediate, so no shifting is required.

---
 arch/riscv/include/asm/parse_asm.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/riscv/include/asm/parse_asm.h +13 -0
index c287c1426aa7..939ede0ee527 100644
@@ -25,6 +25,15 @@
 #define J_IMM_11_MASK		GENMASK(0, 0)
 #define J_IMM_19_12_MASK	GENMASK(7, 0)
 
+/*
+ * U-type IMMs contain the upper 20bits [31:20] of an immediate with
+ * the rest filled in by zeros, so no shifting required. Similarly,
+ * bit31 contains the signed state, so no sign extension necessary.
+ */
+#define U_IMM_SIGN_OPOFF	31
+#define U_IMM_31_12_OPOFF	0
+#define U_IMM_31_12_MASK	GENMASK(31, 12)
+
 /* The bit field of immediate value in B-type instruction */
 #define B_IMM_SIGN_OPOFF	31
 #define B_IMM_10_5_OPOFF	25
@@ -183,6 +192,10 @@ static inline bool is_ ## INSN_NAME ## _insn(long insn) \
 #define RV_X(X, s, mask)  (((X) >> (s)) & (mask))
 #define RVC_X(X, s, mask) RV_X(X, s, mask)
 
+#define EXTRACT_UTYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, U_IMM_31_12_OPOFF, U_IMM_31_12_MASK)); })
+
 #define EXTRACT_JTYPE_IMM(x) \
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, J_IMM_10_1_OPOFF, J_IMM_10_1_MASK) << J_IMM_10_1_OFF) | \
Signature · 7 lines
2.35.1


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

PATCH 4/7 RISC-V: add rd reg parsing to parse_asm header

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Add a macro to allow parsing of the rd register from an instruction.

---
 arch/riscv/include/asm/parse_asm.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/parse_asm.h +5 -0
index 939ede0ee527..305b15f7d41c 100644
@@ -51,6 +51,7 @@
 #define RVG_RS1_OPOFF		15
 #define RVG_RS2_OPOFF		20
 #define RVG_RD_OPOFF		7
+#define RVG_RD_MASK		GENMASK(4, 0)
 
 /* The bit field of immediate value in RVC J instruction */
 #define RVC_J_IMM_SIGN_OPOFF	12
@@ -192,6 +193,10 @@ static inline bool is_ ## INSN_NAME ## _insn(long insn) \
 #define RV_X(X, s, mask)  (((X) >> (s)) & (mask))
 #define RVC_X(X, s, mask) RV_X(X, s, mask)
 
+#define EXTRACT_RD_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
+
 #define EXTRACT_UTYPE_IMM(x) \
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, U_IMM_31_12_OPOFF, U_IMM_31_12_MASK)); })
Signature · 7 lines
2.35.1


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

PATCH 5/7 RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---
 arch/riscv/kernel/cpufeature.c | 79 +++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 2 deletions(-)
arch/riscv/kernel/cpufeature.c +77 -2
index 694267d1fe81..026512ca9c4c 100644
@@ -298,6 +298,74 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
 	return cpu_req_feature;
 }
 
+#include <asm/parse_asm.h>
+
+DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
+DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
+
+static inline bool is_auipc_jalr_pair(long insn1, long insn2)
+{
+	return is_auipc_insn(insn1) && is_jalr_insn(insn2);
+}
+
+#define JALR_SIGN_MASK		BIT(I_IMM_SIGN_OPOFF - I_IMM_11_0_OPOFF)
+#define JALR_OFFSET_MASK	I_IMM_11_0_MASK
+#define AUIPC_OFFSET_MASK	U_IMM_31_12_MASK
+#define AUIPC_PAD		(0x00001000)
+#define JALR_SHIFT		I_IMM_11_0_OPOFF
+
+#define to_jalr_imm(offset)						\
+	((offset & I_IMM_11_0_MASK) << I_IMM_11_0_OPOFF)
+
+#define to_auipc_imm(offset)						\
+	((offset & JALR_SIGN_MASK) ?					\
+	((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) :	\
+	(offset & AUIPC_OFFSET_MASK))
+
+static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
+					     unsigned int len, int patch_offset)
+{
+	int num_instr = len / sizeof(u32);
+	unsigned int call[2];
+	int i;
+	int imm1;
+	u32 rd1;
+
+	for (i = 0; i < num_instr; i++) {
+		/* is there a further instruction? */
+		if (i + 1 >= num_instr)
+			continue;
+
+		if (!is_auipc_jalr_pair(*(alt_ptr + i), *(alt_ptr + i + 1)))
+			continue;
+
+		/* call will use ra register */
+		rd1 = EXTRACT_RD_REG(*(alt_ptr + i));
+		if (rd1 != 1)
+			continue;
+
+		/* get and adjust new target address */
+		imm1 = EXTRACT_UTYPE_IMM(*(alt_ptr + i));
+		imm1 += EXTRACT_ITYPE_IMM(*(alt_ptr + i + 1));
+		imm1 -= patch_offset;
+
+		/* pick the original auipc + jalr */
+		call[0] = *(alt_ptr + i);
+		call[1] = *(alt_ptr + i + 1);
+
+		/* drop the old IMMs */
+		call[0] &= ~(U_IMM_31_12_MASK);
+		call[1] &= ~(I_IMM_11_0_MASK << I_IMM_11_0_OPOFF);
+
+		/* add the adapted IMMs */
+		call[0] |= to_auipc_imm(imm1);
+		call[1] |= to_jalr_imm(imm1);
+
+		/* patch the call place again */
+		patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);
+	}
+}
+
 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
 						  struct alt_entry *end,
 						  unsigned int stage)
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
 		}
 
 		tmp = (1U << alt->errata_id);
-		if (cpu_req_feature & tmp)
-			patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+		if (cpu_req_feature & tmp) {
+			/* do the basic patching */
+			patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+					  alt->alt_len);
+
+			riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+							 alt->alt_len,
+							 alt->old_ptr - alt->alt_ptr);
+		}
 	}
 }
 #endif
Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 5/7
#12 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Quoted 8 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

What a lovely function you've got here, seems to make sense though..

Quoted 109 lines
---
 arch/riscv/kernel/cpufeature.c | 79 +++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 2 deletions(-)
arch/riscv/kernel/cpufeature.c +77 -2
index 694267d1fe81..026512ca9c4c 100644
@@ -298,6 +298,74 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
 	return cpu_req_feature;
 }
 
+#include <asm/parse_asm.h>
+
+DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
+DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
+
+static inline bool is_auipc_jalr_pair(long insn1, long insn2)
+{
+	return is_auipc_insn(insn1) && is_jalr_insn(insn2);
+}
+
+#define JALR_SIGN_MASK		BIT(I_IMM_SIGN_OPOFF - I_IMM_11_0_OPOFF)
+#define JALR_OFFSET_MASK	I_IMM_11_0_MASK
+#define AUIPC_OFFSET_MASK	U_IMM_31_12_MASK
+#define AUIPC_PAD		(0x00001000)
+#define JALR_SHIFT		I_IMM_11_0_OPOFF
+
+#define to_jalr_imm(offset)						\
+	((offset & I_IMM_11_0_MASK) << I_IMM_11_0_OPOFF)
+
+#define to_auipc_imm(offset)						\
+	((offset & JALR_SIGN_MASK) ?					\
+	((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) :	\
+	(offset & AUIPC_OFFSET_MASK))
+
+static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
+					     unsigned int len, int patch_offset)
+{
+	int num_instr = len / sizeof(u32);
+	unsigned int call[2];
+	int i;
+	int imm1;
+	u32 rd1;
+
+	for (i = 0; i < num_instr; i++) {
+		/* is there a further instruction? */
+		if (i + 1 >= num_instr)
+			continue;
+
+		if (!is_auipc_jalr_pair(*(alt_ptr + i), *(alt_ptr + i + 1)))
+			continue;
+
+		/* call will use ra register */
+		rd1 = EXTRACT_RD_REG(*(alt_ptr + i));
+		if (rd1 != 1)
+			continue;
+
+		/* get and adjust new target address */
+		imm1 = EXTRACT_UTYPE_IMM(*(alt_ptr + i));
+		imm1 += EXTRACT_ITYPE_IMM(*(alt_ptr + i + 1));
+		imm1 -= patch_offset;
+
+		/* pick the original auipc + jalr */
+		call[0] = *(alt_ptr + i);
+		call[1] = *(alt_ptr + i + 1);
+
+		/* drop the old IMMs */
+		call[0] &= ~(U_IMM_31_12_MASK);
+		call[1] &= ~(I_IMM_11_0_MASK << I_IMM_11_0_OPOFF);
+
+		/* add the adapted IMMs */
+		call[0] |= to_auipc_imm(imm1);
+		call[1] |= to_jalr_imm(imm1);
+
+		/* patch the call place again */
+		patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);
+	}
+}
+
 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
 						  struct alt_entry *end,
 						  unsigned int stage)
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
 		}
 
 		tmp = (1U << alt->errata_id);
-		if (cpu_req_feature & tmp)
-			patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+		if (cpu_req_feature & tmp) {
+			/* do the basic patching */
+			patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+					  alt->alt_len);
+
+			riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+							 alt->alt_len,
+							 alt->old_ptr - alt->alt_ptr);
+		}
 	}
 }
 #endif
Signature · 7 lines
2.35.1


_______________________________________________
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

#13 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stuebner
Quoted 48 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---
 arch/riscv/kernel/cpufeature.c | 79 +++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 2 deletions(-)
arch/riscv/kernel/cpufeature.c +27 -0
index 694267d1fe81..026512ca9c4c 100644
@@ -298,6 +298,74 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
        return cpu_req_feature;
 }
 
+#include <asm/parse_asm.h>
+
+DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
+DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
+
+static inline bool is_auipc_jalr_pair(long insn1, long insn2)
+{
+       return is_auipc_insn(insn1) && is_jalr_insn(insn2);
+}
+
+#define JALR_SIGN_MASK         BIT(I_IMM_SIGN_OPOFF - I_IMM_11_0_OPOFF)
+#define JALR_OFFSET_MASK       I_IMM_11_0_MASK
+#define AUIPC_OFFSET_MASK      U_IMM_31_12_MASK
+#define AUIPC_PAD              (0x00001000)
+#define JALR_SHIFT             I_IMM_11_0_OPOFF
+
+#define to_jalr_imm(offset)                                            \
+       ((offset & I_IMM_11_0_MASK) << I_IMM_11_0_OPOFF)
+
+#define to_auipc_imm(offset)                                           \
+       ((offset & JALR_SIGN_MASK) ?                                    \
+       ((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) :    \
+       (offset & AUIPC_OFFSET_MASK))
+
+static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
+                                            unsigned int len, int patch_offset)
+{

arch/riscv/kernel/cpufeature.c static u32 __init_or_module cpufeature_probe(unsigned int stage) · line 337

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+       int num_instr = len / sizeof(u32);
+       unsigned int call[2];
+       int i;
+       int imm1;
+       u32 rd1;
+
+       for (i = 0; i < num_instr; i++) {
+               /* is there a further instruction? */
+               if (i + 1 >= num_instr)
+                       continue;

Isn't this the same as for (i = 0; i < num_instr - 1; i++) ?

Quoted 38 lines
arch/riscv/kernel/cpufeature.c +31 -0
+
+               if (!is_auipc_jalr_pair(*(alt_ptr + i), *(alt_ptr + i + 1)))
+                       continue;
+
+               /* call will use ra register */
+               rd1 = EXTRACT_RD_REG(*(alt_ptr + i));
+               if (rd1 != 1)
+                       continue;
+
+               /* get and adjust new target address */
+               imm1 = EXTRACT_UTYPE_IMM(*(alt_ptr + i));
+               imm1 += EXTRACT_ITYPE_IMM(*(alt_ptr + i + 1));
+               imm1 -= patch_offset;
+
+               /* pick the original auipc + jalr */
+               call[0] = *(alt_ptr + i);
+               call[1] = *(alt_ptr + i + 1);
+
+               /* drop the old IMMs */
+               call[0] &= ~(U_IMM_31_12_MASK);
+               call[1] &= ~(I_IMM_11_0_MASK << I_IMM_11_0_OPOFF);
+
+               /* add the adapted IMMs */
+               call[0] |= to_auipc_imm(imm1);
+               call[1] |= to_jalr_imm(imm1);
+
+               /* patch the call place again */
+               patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);
+       }
+}
+
 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                                                  struct alt_entry *end,
                                                  unsigned int stage)
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);

arch/riscv/kernel/cpufeature.c void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +8 -2
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Quoted 6 lines
arch/riscv/kernel/cpufeature.c +1 -1
+               }
        }
 }
 #endif
--

2.35.1

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

#14 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Quoted 1 line · Emil Renner Berthing

On Thu, 10 Nov 2022 at 17:50, Heiko Stuebner <[email protected]> wrote:

...

Quoted 13 lines
Quoted 13 lines
arch/riscv/kernel/cpufeature.c +7 -2
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
Quoted 8 lines

arch/riscv/kernel/cpufeature.c void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 1 line
arch/riscv/kernel/cpufeature.c +1 -0
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Alternative instruction sequences (old and new) have compression disabled.

Thanks,
drew

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

#15 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Am Montag, 14. November 2022, 12:35:53 CET schrieb Andrew Jones:

Quoted 25 lines · Emil Renner Berthing
Quoted 1 line · Heiko Stuebner

...

Quoted 13 lines
Quoted 13 lines
arch/riscv/kernel/cpufeature.c +7 -2
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
Quoted 8 lines

arch/riscv/kernel/cpufeature.c void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 1 line
arch/riscv/kernel/cpufeature.c +1 -0
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Quoted 1 line

Alternative instruction sequences (old and new) have compression disabled.

That was my first thought as well, but I think Emil was talking more about the placement of the alternative block inside the running kernel.

i.e. I guess the starting point of an alternative sequence could also be unaligned.

Though I don't _yet_ see how an improvement could look like.

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

#16 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Quoted 26 lines · Heiko Stübner

Am Montag, 14. November 2022, 12:35:53 CET schrieb Andrew Jones:

Quoted 25 lines · Emil Renner Berthing
Quoted 1 line · Heiko Stuebner

...

Quoted 21 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);

Review void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 10 lines
(quoted diff) +8 -2
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Quoted 6 lines
Quoted 1 line

Alternative instruction sequences (old and new) have compression disabled.

That was my first thought as well, but I think Emil was talking more about the placement of the alternative block inside the running kernel.

i.e. I guess the starting point of an alternative sequence could also be unaligned.

Oh, I see.

Quoted 2 lines

Though I don't _yet_ see how an improvement could look like.

I think we can patch the alternative macros to add alignment. Something like

arch/riscv/include/asm/alternative-macros.h +6 -0
index ec2f3f1b836f..3c330a9066f7 100644
@@ -20,6 +20,7 @@
        ALT_ENTRY 886b, 888f, \vendor_id, \errata_id, 889f - 888f
        .popsection
        .subsection 1
+       .balign 4
 888 :
        .option push
        .option norvc
@@ -34,6 +35,7 @@
 .endm
 
 .macro __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
+       .balign 4
 886 :
        .option push
        .option norvc
@@ -49,6 +51,7 @@
 
 .macro __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1, \
                                  new_c_2, vendor_id_2, errata_id_2, enable_2
+       .balign 4
 886 :
        .option push
        .option norvc
@@ -87,6 +90,7 @@
        ALT_ENTRY("886b", "888f", __stringify(vendor_id), __stringify(errata_id), "889f - 888f") \
        ".popsection\n"                                                 \
        ".subsection 1\n"                                               \
+       ".balign 4\n"                                                   \
        "888 :\n"                                                       \
        ".option push\n"                                                \
        ".option norvc\n"                                               \
@@ -100,6 +104,7 @@
        ".endif\n"
 
 #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, enable)  \
+       ".balign 4\n"                                                   \
        "886 :\n"                                                       \
        ".option push\n"                                                \
        ".option norvc\n"                                               \
@@ -116,6 +121,7 @@
                                        enable_1,                       \
                                   new_c_2, vendor_id_2, errata_id_2,   \
                                        enable_2)                       \
+       ".balign 4\n"                                                   \
        "886 :\n"                                                       \
        ".option push\n"                                                \
        ".option norvc\n"                                               \

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

#17 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Quoted 88 lines · Andrew Jones
Quoted 26 lines · Heiko Stübner

Am Montag, 14. November 2022, 12:35:53 CET schrieb Andrew Jones:

Quoted 25 lines · Emil Renner Berthing
Quoted 1 line · Heiko Stuebner

...

Quoted 21 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);

Review void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 10 lines
(quoted diff) +8 -2
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Quoted 6 lines
Quoted 1 line

Alternative instruction sequences (old and new) have compression disabled.

That was my first thought as well, but I think Emil was talking more about the placement of the alternative block inside the running kernel.

i.e. I guess the starting point of an alternative sequence could also be unaligned.

Oh, I see.

Quoted 2 lines

Though I don't _yet_ see how an improvement could look like.

I think we can patch the alternative macros to add alignment. Something like

arch/riscv/include/asm/alternative-macros.h +5 -0
index ec2f3f1b836f..3c330a9066f7 100644
@@ -20,6 +20,7 @@
        ALT_ENTRY 886b, 888f, \vendor_id, \errata_id, 889f - 888f
        .popsection
        .subsection 1
+       .balign 4
 888 :
        .option push
        .option norvc
@@ -34,6 +35,7 @@
 .endm
 
 .macro __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
+       .balign 4
 886 :
        .option push
        .option norvc
@@ -49,6 +51,7 @@
 
 .macro __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1, \
                                  new_c_2, vendor_id_2, errata_id_2, enable_2
+       .balign 4
 886 :
        .option push
        .option norvc
@@ -87,6 +90,7 @@
        ALT_ENTRY("886b", "888f", __stringify(vendor_id), __stringify(errata_id), "889f - 888f") \
        ".popsection\n"                                                 \
        ".subsection 1\n"                                               \
+       ".balign 4\n"                                                   \
        "888 :\n"                                                       \
        ".option push\n"                                                \
        ".option norvc\n"                                               \
@@ -100,6 +104,7 @@
        ".endif\n"
 
 #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, enable)  \
+       ".balign 4\n"                                                   \
        "886 :\n"                                                       \
        ".option push\n"                                                \
        ".option norvc\n"                                               \

arch/riscv/include/asm/alternative-macros.h line 127

Quoted 7 lines
arch/riscv/include/asm/alternative-macros.h +1 -0
@@ -116,6 +121,7 @@
                                        enable_1,                       \
                                   new_c_2, vendor_id_2, errata_id_2,   \
                                        enable_2)                       \
+       ".balign 4\n"                                                   \
        "886 :\n"                                                       \
        ".option push\n"                                                \
        ".option norvc\n"                                               \

Why not just use accessors? Eg.

unsigned int riscv_instruction_at(void *p)
{
u16 *parcel = p;
return (unsigned int)parcel[0] | (unsigned int)parcel[1] << 16;
}

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

#18 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stübner
Quoted 27 lines · Heiko Stübner

Am Montag, 14. November 2022, 12:35:53 CET schrieb Andrew Jones:

Quoted 25 lines · Emil Renner Berthing
Quoted 1 line · Heiko Stuebner

...

Quoted 21 lines
Quoted 4 lines
arch/riscv/kernel/cpufeature.c +0 -0
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);

arch/riscv/kernel/cpufeature.c void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +8 -2
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Quoted 6 lines
Quoted 1 line

Alternative instruction sequences (old and new) have compression disabled.

That was my first thought as well, but I think Emil was talking more about the placement of the alternative block inside the running kernel.

i.e. I guess the starting point of an alternative sequence could also be unaligned.

Indeed. Instruction alignment is only guaranteed to be 16bits, even for larger instructions.

Quoted 1 line

Though I don't _yet_ see how an improvement could look like.

The general strategy would be multiple smaller accesses that are then stitched together.
This will require (for a 32bit opcode) at least 2 loads, 1 shift and 1 or — for a critical path of 3 instructions.

Given that we are running on RV64, you can handle up to 48bit by aligning down, performing a 64bit load and (if needed) shifting.

Finally, profiles looks like it will give us support for misaligned
loads (see https://github.com/riscv/riscv-profiles/blob/main/profiles.adoc#rva22-profiles
and where the Zicclsm extension is mandated).

Philipp.

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

#19 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Andrew Jones
Quoted 26 lines · Andrew Jones
Quoted 1 line · Emil Renner Berthing

On Thu, 10 Nov 2022 at 17:50, Heiko Stuebner <[email protected]> wrote:

...

Quoted 13 lines
Quoted 13 lines
arch/riscv/kernel/cpufeature.c +7 -2
@@ -316,8 +384,15 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
                }
 
                tmp = (1U << alt->errata_id);
-               if (cpu_req_feature & tmp)
-                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+               if (cpu_req_feature & tmp) {
+                       /* do the basic patching */
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr,
+                                         alt->alt_len);
+
+                       riscv_alternative_fix_auipc_jalr(alt->old_ptr,
+                                                        alt->alt_len,
Quoted 8 lines

arch/riscv/kernel/cpufeature.c void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, · line 394

Quoted 1 line
arch/riscv/kernel/cpufeature.c +1 -0
+                                                        alt->old_ptr - alt->alt_ptr);

Here you're casting a void pointer to an instruction to an unsigned int pointer, but since we enable compressed instructions this may result in an unaligned pointer. Using this pointer will work, but may be slow. Eg. fault to m-mode to be patched up. We already do that in other places in the arch/riscv, but I'd prefer not to add new instances of this.

Quoted 1 line

Alternative instruction sequences (old and new) have compression disabled.

I see, but if the instructions before the alternative sequence ends on an unaligned address will there be inserted 16bit NOPs to make sure the alternative sequence will be aligned?

Quoted 2 lines

Thanks,
drew

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

#20 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stuebner

Hi Heiko,

Thank you for the patch.

Quoted 38 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---
 arch/riscv/kernel/cpufeature.c | 79 +++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 2 deletions(-)
arch/riscv/kernel/cpufeature.c +17 -0
index 694267d1fe81..026512ca9c4c 100644
@@ -298,6 +298,74 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
        return cpu_req_feature;
 }
 
+#include <asm/parse_asm.h>
+
+DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
+DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
+
+static inline bool is_auipc_jalr_pair(long insn1, long insn2)
+{
+       return is_auipc_insn(insn1) && is_jalr_insn(insn2);
+}
+
+#define JALR_SIGN_MASK         BIT(I_IMM_SIGN_OPOFF - I_IMM_11_0_OPOFF)
+#define JALR_OFFSET_MASK       I_IMM_11_0_MASK
+#define AUIPC_OFFSET_MASK      U_IMM_31_12_MASK
+#define AUIPC_PAD              (0x00001000)
+#define JALR_SHIFT             I_IMM_11_0_OPOFF
+
+#define to_jalr_imm(offset)                                            \

arch/riscv/kernel/cpufeature.c static u32 __init_or_module cpufeature_probe(unsigned int stage) · line 327

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+       ((offset & I_IMM_11_0_MASK) << I_IMM_11_0_OPOFF)
+
+#define to_auipc_imm(offset)                                           \
+       ((offset & JALR_SIGN_MASK) ?                                    \
+       ((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) :    \
+       (offset & AUIPC_OFFSET_MASK))
+
+static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
+                                            unsigned int len, int patch_offset)
+{

I am yet to test this with my ASM code yet, but maybe can we move this to [0] so that other erratas can make use of it too?

[0] arch/riscv/kernel/patch.c

Cheers,
Prabhakar

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

#21 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Am Dienstag, 15. November 2022, 15:28:27 CET schrieb Lad, Prabhakar:

Quoted 57 lines

Hi Heiko,

Thank you for the patch.

Quoted 38 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---
 arch/riscv/kernel/cpufeature.c | 79 +++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 2 deletions(-)
arch/riscv/kernel/cpufeature.c +17 -0
index 694267d1fe81..026512ca9c4c 100644
@@ -298,6 +298,74 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
        return cpu_req_feature;
 }
 
+#include <asm/parse_asm.h>
+
+DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
+DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
+
+static inline bool is_auipc_jalr_pair(long insn1, long insn2)
+{
+       return is_auipc_insn(insn1) && is_jalr_insn(insn2);
+}
+
+#define JALR_SIGN_MASK         BIT(I_IMM_SIGN_OPOFF - I_IMM_11_0_OPOFF)
+#define JALR_OFFSET_MASK       I_IMM_11_0_MASK
+#define AUIPC_OFFSET_MASK      U_IMM_31_12_MASK
+#define AUIPC_PAD              (0x00001000)
+#define JALR_SHIFT             I_IMM_11_0_OPOFF
+
+#define to_jalr_imm(offset)                                            \

arch/riscv/kernel/cpufeature.c static u32 __init_or_module cpufeature_probe(unsigned int stage) · line 327

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+       ((offset & I_IMM_11_0_MASK) << I_IMM_11_0_OPOFF)
+
+#define to_auipc_imm(offset)                                           \
+       ((offset & JALR_SIGN_MASK) ?                                    \
+       ((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) :    \
+       (offset & AUIPC_OFFSET_MASK))
+
+static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
+                                            unsigned int len, int patch_offset)
+{

I am yet to test this with my ASM code yet, but maybe can we move this to [0] so that other erratas can make use of it too?

Quoted 1 line

[0] arch/riscv/kernel/patch.c

yeah, that sounds like a very good plan.

I also want to make the to_foo_imm macros shared.
I.e. right now they're just duplicated from the ftrace patching code.

Heiko

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

#22 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stuebner

Hi Heiko,

Quoted 79 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---
 arch/riscv/kernel/cpufeature.c | 79 +++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 2 deletions(-)
arch/riscv/kernel/cpufeature.c +58 -0
index 694267d1fe81..026512ca9c4c 100644
@@ -298,6 +298,74 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
        return cpu_req_feature;
 }
 
+#include <asm/parse_asm.h>
+
+DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
+DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
+
+static inline bool is_auipc_jalr_pair(long insn1, long insn2)
+{
+       return is_auipc_insn(insn1) && is_jalr_insn(insn2);
+}
+
+#define JALR_SIGN_MASK         BIT(I_IMM_SIGN_OPOFF - I_IMM_11_0_OPOFF)
+#define JALR_OFFSET_MASK       I_IMM_11_0_MASK
+#define AUIPC_OFFSET_MASK      U_IMM_31_12_MASK
+#define AUIPC_PAD              (0x00001000)
+#define JALR_SHIFT             I_IMM_11_0_OPOFF
+
+#define to_jalr_imm(offset)                                            \
+       ((offset & I_IMM_11_0_MASK) << I_IMM_11_0_OPOFF)
+
+#define to_auipc_imm(offset)                                           \
+       ((offset & JALR_SIGN_MASK) ?                                    \
+       ((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) :    \
+       (offset & AUIPC_OFFSET_MASK))
+
+static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
+                                            unsigned int len, int patch_offset)
+{
+       int num_instr = len / sizeof(u32);
+       unsigned int call[2];
+       int i;
+       int imm1;
+       u32 rd1;
+
+       for (i = 0; i < num_instr; i++) {
+               /* is there a further instruction? */
+               if (i + 1 >= num_instr)
+                       continue;
+
+               if (!is_auipc_jalr_pair(*(alt_ptr + i), *(alt_ptr + i + 1)))
+                       continue;
+
+               /* call will use ra register */
+               rd1 = EXTRACT_RD_REG(*(alt_ptr + i));
+               if (rd1 != 1)
+                       continue;
+
+               /* get and adjust new target address */
+               imm1 = EXTRACT_UTYPE_IMM(*(alt_ptr + i));
+               imm1 += EXTRACT_ITYPE_IMM(*(alt_ptr + i + 1));
+               imm1 -= patch_offset;
+
+               /* pick the original auipc + jalr */
+               call[0] = *(alt_ptr + i);
+               call[1] = *(alt_ptr + i + 1);
+
+               /* drop the old IMMs */
+               call[0] &= ~(U_IMM_31_12_MASK);
+               call[1] &= ~(I_IMM_11_0_MASK << I_IMM_11_0_OPOFF);

arch/riscv/kernel/cpufeature.c static u32 __init_or_module cpufeature_probe(unsigned int stage) · line 368

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+
+               /* add the adapted IMMs */
+               call[0] |= to_auipc_imm(imm1);
+               call[1] |= to_jalr_imm(imm1);
+
+               /* patch the call place again */
+               patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);
+       }
+}
+

I have the below assembly code which I have tested without the alternatives for the RZ/Five CMO,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(".option push\n\t\n\t"                    \
         ".option norvc\n\t"                    \
         ".option norelax\n\t"                    \
         "addi sp,sp,-16\n\t"                    \
         "sd    s0,0(sp)\n\t"                    \
         "sd    ra,8(sp)\n\t"                    \
         "addi    s0,sp,16\n\t"                    \
         "mv a4,%6\n\t"                        \
         "mv a3,%5\n\t"                        \
         "mv a2,%4\n\t"                        \
         "mv a1,%3\n\t"                        \
         "mv a0,%0\n\t"                        \
         "call rzfive_cmo\n\t"                    \
         "ld    ra,8(sp)\n\t"                    \
         "ld    s0,0(sp)\n\t"                    \
         "addi    sp,sp,16\n\t"                    \
         ".option pop\n\t"                        \
         : : "r"(_cachesize),                    \
         "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
         "r"((unsigned long)(_start) + (_size)),            \
         "r"((unsigned long) (_start)),                \
         "r"((unsigned long) (_size)),                \
         "r"((unsigned long) (_dir)),                \
         "r"((unsigned long) (_ops))                \
         : "a0", "a1", "a2", "a3", "a4", "memory")

Now when integrate this with ALTERNATIVE_2() as below,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(ALTERNATIVE_2(                        \
    __nops(14),                            \
    "mv a0, %1\n\t"                            \
    "j 2f\n\t"                            \
    "3:\n\t"                            \
    "cbo." __stringify(_op) " (a0)\n\t"                \
    "add a0, a0, %0\n\t"                        \
    "2:\n\t"                            \
    "bltu a0, %2, 3b\n\t"                        \
    __nops(8), 0, CPUFEATURE_ZICBOM, CONFIG_RISCV_ISA_ZICBOM,    \
    ".option push\n\t\n\t"                        \
    ".option norvc\n\t"                        \
    ".option norelax\n\t"                        \
    "addi sp,sp,-16\n\t"                        \
    "sd    s0,0(sp)\n\t"                        \
    "sd    ra,8(sp)\n\t"                        \
    "addi    s0,sp,16\n\t"                        \
    "mv a4,%6\n\t"                            \
    "mv a3,%5\n\t"                            \
    "mv a2,%4\n\t"                            \
    "mv a1,%3\n\t"                            \
    "mv a0,%0\n\t"                            \
    "call rzfive_cmo\n\t"                \
    "ld    ra,8(sp)\n\t"                        \
    "ld    s0,0(sp)\n\t"                        \
    "addi    sp,sp,16\n\t"                        \
    ".option pop\n\t"                        \
    , ANDESTECH_VENDOR_ID,                        \
            ERRATA_ANDESTECH_NO_IOCP, CONFIG_ERRATA_RZFIVE_CMO)    \
    : : "r"(_cachesize),                        \
    "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
    "r"((unsigned long)(_start) + (_size)),            \
    "r"((unsigned long) (_start)),                \
    "r"((unsigned long) (_size)),                \
    "r"((unsigned long) (_dir)),                \
    "r"((unsigned long) (_ops))                \
    : "a0", "a1", "a2", "a3", "a4", "memory")

I am seeing kernel panic with this change. Looking at the riscv_alternative_fix_auipc_jalr() implementation it assumes the rest of the alternative options are calls too. Is my understanding correct here?

Do you think this is the correct approach in my case?

Note, I wanted to test with ALTERNATIVE_2() first to make sure everything is okay and then later test my ALTERNATIVE_3() implementation.

Cheers,
Prabhakar

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

#23 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Hi,

Am Montag, 21. November 2022, 10:50:09 CET schrieb Lad, Prabhakar:

Quoted 11 lines · Heiko Stuebner
Quoted 10 lines

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---

[...]

Quoted 71 lines

I have the below assembly code which I have tested without the alternatives for the RZ/Five CMO,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(".option push\n\t\n\t"                    \
         ".option norvc\n\t"                    \
         ".option norelax\n\t"                    \
         "addi sp,sp,-16\n\t"                    \
         "sd    s0,0(sp)\n\t"                    \
         "sd    ra,8(sp)\n\t"                    \
         "addi    s0,sp,16\n\t"                    \
         "mv a4,%6\n\t"                        \
         "mv a3,%5\n\t"                        \
         "mv a2,%4\n\t"                        \
         "mv a1,%3\n\t"                        \
         "mv a0,%0\n\t"                        \
         "call rzfive_cmo\n\t"                    \
         "ld    ra,8(sp)\n\t"                    \
         "ld    s0,0(sp)\n\t"                    \
         "addi    sp,sp,16\n\t"                    \
         ".option pop\n\t"                        \
         : : "r"(_cachesize),                    \
         "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
         "r"((unsigned long)(_start) + (_size)),            \
         "r"((unsigned long) (_start)),                \
         "r"((unsigned long) (_size)),                \
         "r"((unsigned long) (_dir)),                \
         "r"((unsigned long) (_ops))                \
         : "a0", "a1", "a2", "a3", "a4", "memory")

Now when integrate this with ALTERNATIVE_2() as below,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(ALTERNATIVE_2(                        \
    __nops(14),                            \
    "mv a0, %1\n\t"                            \
    "j 2f\n\t"                            \
    "3:\n\t"                            \
    "cbo." __stringify(_op) " (a0)\n\t"                \
    "add a0, a0, %0\n\t"                        \
    "2:\n\t"                            \
    "bltu a0, %2, 3b\n\t"                        \
    __nops(8), 0, CPUFEATURE_ZICBOM, CONFIG_RISCV_ISA_ZICBOM,    \
    ".option push\n\t\n\t"                        \
    ".option norvc\n\t"                        \
    ".option norelax\n\t"                        \
    "addi sp,sp,-16\n\t"                        \
    "sd    s0,0(sp)\n\t"                        \
    "sd    ra,8(sp)\n\t"                        \
    "addi    s0,sp,16\n\t"                        \
    "mv a4,%6\n\t"                            \
    "mv a3,%5\n\t"                            \
    "mv a2,%4\n\t"                            \
    "mv a1,%3\n\t"                            \
    "mv a0,%0\n\t"                            \
    "call rzfive_cmo\n\t"                \
    "ld    ra,8(sp)\n\t"                        \
    "ld    s0,0(sp)\n\t"                        \
    "addi    sp,sp,16\n\t"                        \
    ".option pop\n\t"                        \
    , ANDESTECH_VENDOR_ID,                        \
            ERRATA_ANDESTECH_NO_IOCP, CONFIG_ERRATA_RZFIVE_CMO)    \
    : : "r"(_cachesize),                        \
    "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
    "r"((unsigned long)(_start) + (_size)),            \
    "r"((unsigned long) (_start)),                \
    "r"((unsigned long) (_size)),                \
    "r"((unsigned long) (_dir)),                \
    "r"((unsigned long) (_ops))                \
    : "a0", "a1", "a2", "a3", "a4", "memory")
Quoted 4 lines

I am seeing kernel panic with this change. Looking at the riscv_alternative_fix_auipc_jalr() implementation it assumes the rest of the alternative options are calls too. Is my understanding correct here?

The loop walks through the instructions after the location got patched and checks if an instruction is an auipc and the next one is a jalr and only then adjusts the address accordingly.

So it _should_ leave all other (non auipc+jalr) instructions alone. (hopefully)

Quoted 1 line

Do you think this is the correct approach in my case?

It does look correct on first glance.

As I had that passing thought, are you actually calling
riscv_alternative_fix_auipc_jalr()
from your errata/.../foo.c after doing the patching?

I.e. with the current patchset, that function is only called from the cpufeature part, but for example not from the other patching locations. [and a future revision should probably change that :-) ]

After making sure that function actually runs, the next thing you could try is to have both the "original" code and the patch be identical, i.e. replace the cbo* part with your code as well and then just output the instructions via printk to check what the addresses do in both.

After riscv_alternative_fix_auipc_jalr() ran then both code variants should be identical when using the same code in both areas.

Quoted 3 lines

Note, I wanted to test with ALTERNATIVE_2() first to make sure everything is okay and then later test my ALTERNATIVE_3() implementation.

sounds like a very sensible idea to use the existing macros first for verification :-)

Heiko

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

#24 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Hi Heiko,

Quoted 89 lines · Heiko Stübner

Hi,

Am Montag, 21. November 2022, 10:50:09 CET schrieb Lad, Prabhakar:

Quoted 11 lines · Heiko Stuebner
Quoted 10 lines

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---

[...]

Quoted 71 lines

I have the below assembly code which I have tested without the alternatives for the RZ/Five CMO,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(".option push\n\t\n\t"                    \
         ".option norvc\n\t"                    \
         ".option norelax\n\t"                    \
         "addi sp,sp,-16\n\t"                    \
         "sd    s0,0(sp)\n\t"                    \
         "sd    ra,8(sp)\n\t"                    \
         "addi    s0,sp,16\n\t"                    \
         "mv a4,%6\n\t"                        \
         "mv a3,%5\n\t"                        \
         "mv a2,%4\n\t"                        \
         "mv a1,%3\n\t"                        \
         "mv a0,%0\n\t"                        \
         "call rzfive_cmo\n\t"                    \
         "ld    ra,8(sp)\n\t"                    \
         "ld    s0,0(sp)\n\t"                    \
         "addi    sp,sp,16\n\t"                    \
         ".option pop\n\t"                        \
         : : "r"(_cachesize),                    \
         "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
         "r"((unsigned long)(_start) + (_size)),            \
         "r"((unsigned long) (_start)),                \
         "r"((unsigned long) (_size)),                \
         "r"((unsigned long) (_dir)),                \
         "r"((unsigned long) (_ops))                \
         : "a0", "a1", "a2", "a3", "a4", "memory")

Now when integrate this with ALTERNATIVE_2() as below,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(ALTERNATIVE_2(                        \
    __nops(14),                            \
    "mv a0, %1\n\t"                            \
    "j 2f\n\t"                            \
    "3:\n\t"                            \
    "cbo." __stringify(_op) " (a0)\n\t"                \
    "add a0, a0, %0\n\t"                        \
    "2:\n\t"                            \
    "bltu a0, %2, 3b\n\t"                        \
    __nops(8), 0, CPUFEATURE_ZICBOM, CONFIG_RISCV_ISA_ZICBOM,    \
    ".option push\n\t\n\t"                        \
    ".option norvc\n\t"                        \
    ".option norelax\n\t"                        \
    "addi sp,sp,-16\n\t"                        \
    "sd    s0,0(sp)\n\t"                        \
    "sd    ra,8(sp)\n\t"                        \
    "addi    s0,sp,16\n\t"                        \
    "mv a4,%6\n\t"                            \
    "mv a3,%5\n\t"                            \
    "mv a2,%4\n\t"                            \
    "mv a1,%3\n\t"                            \
    "mv a0,%0\n\t"                            \
    "call rzfive_cmo\n\t"                \
    "ld    ra,8(sp)\n\t"                        \
    "ld    s0,0(sp)\n\t"                        \
    "addi    sp,sp,16\n\t"                        \
    ".option pop\n\t"                        \
    , ANDESTECH_VENDOR_ID,                        \
            ERRATA_ANDESTECH_NO_IOCP, CONFIG_ERRATA_RZFIVE_CMO)    \
    : : "r"(_cachesize),                        \
    "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
    "r"((unsigned long)(_start) + (_size)),            \
    "r"((unsigned long) (_start)),                \
    "r"((unsigned long) (_size)),                \
    "r"((unsigned long) (_dir)),                \
    "r"((unsigned long) (_ops))                \
    : "a0", "a1", "a2", "a3", "a4", "memory")
Quoted 9 lines
Quoted 4 lines

I am seeing kernel panic with this change. Looking at the riscv_alternative_fix_auipc_jalr() implementation it assumes the rest of the alternative options are calls too. Is my understanding correct here?

The loop walks through the instructions after the location got patched and checks if an instruction is an auipc and the next one is a jalr and only then adjusts the address accordingly.

Ok so my understanding was wrong here.

Quoted 3 lines

So it _should_ leave all other (non auipc+jalr) instructions alone. (hopefully)

Agreed.

Quoted 5 lines
Quoted 2 lines

Do you think this is the correct approach in my case?

It does look correct on first glance.

\o/

Quoted 9 lines

As I had that passing thought, are you actually calling
riscv_alternative_fix_auipc_jalr()
from your errata/.../foo.c after doing the patching?

I.e. with the current patchset, that function is only called from the cpufeature part, but for example not from the other patching locations. [and a future revision should probably change that :-) ]

I have made a local copy of riscv_alternative_fix_auipc_jalr() and then calling it after patch_text_nosync() referring to your patch for str functions.

Quoted 8 lines

After making sure that function actually runs, the next thing you could try is to have both the "original" code and the patch be identical, i.e. replace the cbo* part with your code as well and then just output the instructions via printk to check what the addresses do in both.

After riscv_alternative_fix_auipc_jalr() ran then both code variants should be identical when using the same code in both areas.

So I have added debug prints to match the instructions as below after and before patching:

static void riscv_alternative_print_inst(unsigned int *alt_ptr,
unsigned int len)

{
    int num_instr = len / sizeof(u32);
    int i;

    for (i = 0; i < num_instr; i++)
        pr_err("%s instruction: 0x%x\n", __func__, *(alt_ptr + i));

}

void __init_or_module andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
unsigned long archid, unsigned long impid, unsigned int stage)
{
....

    if (cpu_req_errata & tmp) {
        pr_err("stage: %x -> %px--> %x %x %x\n", stage, alt, tmp,
cpu_req_errata, alt->errata_id);
        pr_err("old:%ps alt:%ps len:%lx\n", alt->old_ptr,
alt->alt_ptr, alt->alt_len);
        pr_err("Print old start\n");
        riscv_alternative_print_inst(alt->old_ptr, alt->alt_len);
        pr_err("Print old end\n");
        patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);

        riscv_alternative_fix_auipc_jalr(alt->old_ptr, alt->alt_len,
                        alt->old_ptr - alt->alt_ptr);
        pr_err("Print patch start\n");
        riscv_alternative_print_inst(alt->alt_ptr, alt->alt_len);
        pr_err("Print patch end\n");
    }

.....
}

Below is the log:

[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print new old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0x78693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x80593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xcba080e7
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a2492c--> 1 1 0
[    0.000000] old:arch_sync_dma_for_device
alt:riscv_noncoherent_supported len:38
[    0.000000] Print  old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x970013
    ====================> This instruction doesn't look correct it
should be 0x13?
[    0.000000] Print  old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0x78713
[    0.000000] riscv_alternative_print_inst instruction: 0x78693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x80593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc82080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 or does it require the
offset too?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a24950--> 1 1 0
[    0.000000] old:arch_sync_dma_for_cpu alt:riscv_noncoherent_supported len:38
[    0.000000] Print old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x97
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0xeee080e7
      ====================> This instruction doesn't look correct it
should be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0x80693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x78593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc4a080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 or does it require the
offset too?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a24974--> 1 1 0
[    0.000000] old:arch_dma_prep_coherent alt:riscv_noncoherent_supported len:38
[    0.000000] Print old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x970013
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x80e70000
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0xe720
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0xe8693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x78593
[    0.000000] riscv_alternative_print_inst instruction: 0x30513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc12080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 + offset?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end

Here is the output from objdump of the file (dma-noncoherent.o):

000000000000032e <.L888^B1>:

 32e:    ff010113              addi    sp,sp,-16
 332:    00813023              sd    s0,0(sp)
 336:    00113423              sd    ra,8(sp)
 33a:    01010413              addi    s0,sp,16
 33e:    000f0713              mv    a4,t5
 342:    00078693              mv    a3,a5
 346:    00088613              mv    a2,a7
 34a:    00080593              mv    a1,a6
 34e:    000e0513              mv    a0,t3
 352:    00000097              auipc    ra,0x0
 356:    000080e7              jalr    ra # 352 <.L888^B1+0x24>
 35a:    00813083              ld    ra,8(sp)
 35e:    00013403              ld    s0,0(sp)
 362:    01010113              addi    sp,sp,16

0000000000000366 <.L888^B2>:

 366:    ff010113              addi    sp,sp,-16
 36a:    00813023              sd    s0,0(sp)
 36e:    00113423              sd    ra,8(sp)
 372:    01010413              addi    s0,sp,16
 376:    00078713              mv    a4,a5
 37a:    00078693              mv    a3,a5
 37e:    00088613              mv    a2,a7
 382:    00080593              mv    a1,a6
 386:    000e0513              mv    a0,t3
 38a:    00000097              auipc    ra,0x0
 38e:    000080e7              jalr    ra # 38a <.L888^B2+0x24>
 392:    00813083              ld    ra,8(sp)
 396:    00013403              ld    s0,0(sp)
 39a:    01010113              addi    sp,sp,16

000000000000039e <.L888^B3>:

 39e:    ff010113              addi    sp,sp,-16
 3a2:    00813023              sd    s0,0(sp)
 3a6:    00113423              sd    ra,8(sp)
 3aa:    01010413              addi    s0,sp,16
 3ae:    000f0713              mv    a4,t5
 3b2:    00080693              mv    a3,a6
 3b6:    00088613              mv    a2,a7
 3ba:    00078593              mv    a1,a5
 3be:    000e0513              mv    a0,t3
 3c2:    00000097              auipc    ra,0x0
 3c6:    000080e7              jalr    ra # 3c2 <.L888^B3+0x24>
 3ca:    00813083              ld    ra,8(sp)
 3ce:    00013403              ld    s0,0(sp)
 3d2:    01010113              addi    sp,sp,16

00000000000003d6 <.L888^B4>:

 3d6:    ff010113              addi    sp,sp,-16
 3da:    00813023              sd    s0,0(sp)
 3de:    00113423              sd    ra,8(sp)
 3e2:    01010413              addi    s0,sp,16
 3e6:    000f0713              mv    a4,t5
 3ea:    000e8693              mv    a3,t4
 3ee:    00088613              mv    a2,a7
 3f2:    00078593              mv    a1,a5
 3f6:    00030513              mv    a0,t1
 3fa:    00000097              auipc    ra,0x0
 3fe:    000080e7              jalr    ra # 3fa <.L888^B4+0x24>
 402:    00813083              ld    ra,8(sp)
 406:    00013403              ld    s0,0(sp)
 40a:    01010113              addi    sp,sp,16

Disassembly of section __ksymtab_strings:

Any pointers what could be happening?

Quoted 8 lines
Quoted 4 lines

Note, I wanted to test with ALTERNATIVE_2() first to make sure everything is okay and then later test my ALTERNATIVE_3() implementation.

sounds like a very sensible idea to use the existing macros first for verification :-)

:)

Cheers,
Prabhakar

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

#25 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Hi Heiko,

Quoted 402 lines · Prabhakar

Hi Heiko,

Quoted 89 lines · Heiko Stübner

Hi,

Am Montag, 21. November 2022, 10:50:09 CET schrieb Lad, Prabhakar:

Quoted 11 lines · Heiko Stuebner
Quoted 10 lines

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---

[...]

Quoted 71 lines

I have the below assembly code which I have tested without the alternatives for the RZ/Five CMO,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(".option push\n\t\n\t"                    \
         ".option norvc\n\t"                    \
         ".option norelax\n\t"                    \
         "addi sp,sp,-16\n\t"                    \
         "sd    s0,0(sp)\n\t"                    \
         "sd    ra,8(sp)\n\t"                    \
         "addi    s0,sp,16\n\t"                    \
         "mv a4,%6\n\t"                        \
         "mv a3,%5\n\t"                        \
         "mv a2,%4\n\t"                        \
         "mv a1,%3\n\t"                        \
         "mv a0,%0\n\t"                        \
         "call rzfive_cmo\n\t"                    \
         "ld    ra,8(sp)\n\t"                    \
         "ld    s0,0(sp)\n\t"                    \
         "addi    sp,sp,16\n\t"                    \
         ".option pop\n\t"                        \
         : : "r"(_cachesize),                    \
         "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
         "r"((unsigned long)(_start) + (_size)),            \
         "r"((unsigned long) (_start)),                \
         "r"((unsigned long) (_size)),                \
         "r"((unsigned long) (_dir)),                \
         "r"((unsigned long) (_ops))                \
         : "a0", "a1", "a2", "a3", "a4", "memory")

Now when integrate this with ALTERNATIVE_2() as below,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(ALTERNATIVE_2(                        \
    __nops(14),                            \
    "mv a0, %1\n\t"                            \
    "j 2f\n\t"                            \
    "3:\n\t"                            \
    "cbo." __stringify(_op) " (a0)\n\t"                \
    "add a0, a0, %0\n\t"                        \
    "2:\n\t"                            \
    "bltu a0, %2, 3b\n\t"                        \
    __nops(8), 0, CPUFEATURE_ZICBOM, CONFIG_RISCV_ISA_ZICBOM,    \
    ".option push\n\t\n\t"                        \
    ".option norvc\n\t"                        \
    ".option norelax\n\t"                        \
    "addi sp,sp,-16\n\t"                        \
    "sd    s0,0(sp)\n\t"                        \
    "sd    ra,8(sp)\n\t"                        \
    "addi    s0,sp,16\n\t"                        \
    "mv a4,%6\n\t"                            \
    "mv a3,%5\n\t"                            \
    "mv a2,%4\n\t"                            \
    "mv a1,%3\n\t"                            \
    "mv a0,%0\n\t"                            \
    "call rzfive_cmo\n\t"                \
    "ld    ra,8(sp)\n\t"                        \
    "ld    s0,0(sp)\n\t"                        \
    "addi    sp,sp,16\n\t"                        \
    ".option pop\n\t"                        \
    , ANDESTECH_VENDOR_ID,                        \
            ERRATA_ANDESTECH_NO_IOCP, CONFIG_ERRATA_RZFIVE_CMO)    \
    : : "r"(_cachesize),                        \
    "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
    "r"((unsigned long)(_start) + (_size)),            \
    "r"((unsigned long) (_start)),                \
    "r"((unsigned long) (_size)),                \
    "r"((unsigned long) (_dir)),                \
    "r"((unsigned long) (_ops))                \
    : "a0", "a1", "a2", "a3", "a4", "memory")
Quoted 9 lines
Quoted 4 lines

I am seeing kernel panic with this change. Looking at the riscv_alternative_fix_auipc_jalr() implementation it assumes the rest of the alternative options are calls too. Is my understanding correct here?

The loop walks through the instructions after the location got patched and checks if an instruction is an auipc and the next one is a jalr and only then adjusts the address accordingly.

Ok so my understanding was wrong here.

Quoted 3 lines

So it _should_ leave all other (non auipc+jalr) instructions alone. (hopefully)

Agreed.

Quoted 5 lines
Quoted 2 lines

Do you think this is the correct approach in my case?

It does look correct on first glance.

\o/

Quoted 9 lines

As I had that passing thought, are you actually calling
riscv_alternative_fix_auipc_jalr()
from your errata/.../foo.c after doing the patching?

I.e. with the current patchset, that function is only called from the cpufeature part, but for example not from the other patching locations. [and a future revision should probably change that :-) ]

I have made a local copy of riscv_alternative_fix_auipc_jalr() and then calling it after patch_text_nosync() referring to your patch for str functions.

Quoted 8 lines

After making sure that function actually runs, the next thing you could try is to have both the "original" code and the patch be identical, i.e. replace the cbo* part with your code as well and then just output the instructions via printk to check what the addresses do in both.

After riscv_alternative_fix_auipc_jalr() ran then both code variants should be identical when using the same code in both areas.

So I have added debug prints to match the instructions as below after and before patching:

static void riscv_alternative_print_inst(unsigned int *alt_ptr,
unsigned int len)

{
    int num_instr = len / sizeof(u32);
    int i;

    for (i = 0; i < num_instr; i++)
        pr_err("%s instruction: 0x%x\n", __func__, *(alt_ptr + i));

}

void __init_or_module andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
unsigned long archid, unsigned long impid, unsigned int stage)
{
....

    if (cpu_req_errata & tmp) {
        pr_err("stage: %x -> %px--> %x %x %x\n", stage, alt, tmp,
cpu_req_errata, alt->errata_id);
        pr_err("old:%ps alt:%ps len:%lx\n", alt->old_ptr,
alt->alt_ptr, alt->alt_len);
        pr_err("Print old start\n");
        riscv_alternative_print_inst(alt->old_ptr, alt->alt_len);
        pr_err("Print old end\n");
        patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);

        riscv_alternative_fix_auipc_jalr(alt->old_ptr, alt->alt_len,
                        alt->old_ptr - alt->alt_ptr);
        pr_err("Print patch start\n");
        riscv_alternative_print_inst(alt->alt_ptr, alt->alt_len);
        pr_err("Print patch end\n");
    }

.....
}

Below is the log:

[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print new old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0x78693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x80593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xcba080e7
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a2492c--> 1 1 0
[    0.000000] old:arch_sync_dma_for_device
alt:riscv_noncoherent_supported len:38
[    0.000000] Print  old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x970013
    ====================> This instruction doesn't look correct it
should be 0x13?
[    0.000000] Print  old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0x78713
[    0.000000] riscv_alternative_print_inst instruction: 0x78693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x80593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc82080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 or does it require the
offset too?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a24950--> 1 1 0
[    0.000000] old:arch_sync_dma_for_cpu alt:riscv_noncoherent_supported len:38
[    0.000000] Print old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x97
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0xeee080e7
      ====================> This instruction doesn't look correct it
should be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0x80693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x78593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc4a080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 or does it require the
offset too?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a24974--> 1 1 0
[    0.000000] old:arch_dma_prep_coherent alt:riscv_noncoherent_supported len:38
[    0.000000] Print old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x970013
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x80e70000
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0xe720
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0xe8693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x78593
[    0.000000] riscv_alternative_print_inst instruction: 0x30513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc12080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 + offset?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end

Here is the output from objdump of the file (dma-noncoherent.o):

000000000000032e <.L888^B1>:

 32e:    ff010113              addi    sp,sp,-16
 332:    00813023              sd    s0,0(sp)
 336:    00113423              sd    ra,8(sp)
 33a:    01010413              addi    s0,sp,16
 33e:    000f0713              mv    a4,t5
 342:    00078693              mv    a3,a5
 346:    00088613              mv    a2,a7
 34a:    00080593              mv    a1,a6
 34e:    000e0513              mv    a0,t3
 352:    00000097              auipc    ra,0x0
 356:    000080e7              jalr    ra # 352 <.L888^B1+0x24>
 35a:    00813083              ld    ra,8(sp)
 35e:    00013403              ld    s0,0(sp)
 362:    01010113              addi    sp,sp,16

0000000000000366 <.L888^B2>:

 366:    ff010113              addi    sp,sp,-16
 36a:    00813023              sd    s0,0(sp)
 36e:    00113423              sd    ra,8(sp)
 372:    01010413              addi    s0,sp,16
 376:    00078713              mv    a4,a5
 37a:    00078693              mv    a3,a5
 37e:    00088613              mv    a2,a7
 382:    00080593              mv    a1,a6
 386:    000e0513              mv    a0,t3
 38a:    00000097              auipc    ra,0x0
 38e:    000080e7              jalr    ra # 38a <.L888^B2+0x24>
 392:    00813083              ld    ra,8(sp)
 396:    00013403              ld    s0,0(sp)
 39a:    01010113              addi    sp,sp,16

000000000000039e <.L888^B3>:

 39e:    ff010113              addi    sp,sp,-16
 3a2:    00813023              sd    s0,0(sp)
 3a6:    00113423              sd    ra,8(sp)
 3aa:    01010413              addi    s0,sp,16
 3ae:    000f0713              mv    a4,t5
 3b2:    00080693              mv    a3,a6
 3b6:    00088613              mv    a2,a7
 3ba:    00078593              mv    a1,a5
 3be:    000e0513              mv    a0,t3
 3c2:    00000097              auipc    ra,0x0
 3c6:    000080e7              jalr    ra # 3c2 <.L888^B3+0x24>
 3ca:    00813083              ld    ra,8(sp)
 3ce:    00013403              ld    s0,0(sp)
 3d2:    01010113              addi    sp,sp,16

00000000000003d6 <.L888^B4>:

 3d6:    ff010113              addi    sp,sp,-16
 3da:    00813023              sd    s0,0(sp)
 3de:    00113423              sd    ra,8(sp)
 3e2:    01010413              addi    s0,sp,16
 3e6:    000f0713              mv    a4,t5
 3ea:    000e8693              mv    a3,t4
 3ee:    00088613              mv    a2,a7
 3f2:    00078593              mv    a1,a5
 3f6:    00030513              mv    a0,t1
 3fa:    00000097              auipc    ra,0x0
 3fe:    000080e7              jalr    ra # 3fa <.L888^B4+0x24>
 402:    00813083              ld    ra,8(sp)
 406:    00013403              ld    s0,0(sp)
 40a:    01010113              addi    sp,sp,16
Quoted 4 lines

Disassembly of section __ksymtab_strings:

Any pointers what could be happening?

Some more information,

- If I drop the riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I can see the instructions as 0x13 (nop) which is correct.

- if I call riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I dont see 0x13 (nop) consistently for old instructions.

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then just use patch_text_nosync() I can see the correct actual instruction being printed apart from jalr (is some sort of offset added to it as I see last 4 bits match?) and then is replaced correctly by the same alt instructions apart from the jalr (log [0]).

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then use patch_text_nosync() and riscv_alternative_fix_auipc_jalr() I can see the actual old instructions differs a bit and again the jalr instruction differs too in the patched code (log [1]).

[0] https://paste.debian.net/1261412/
[1] https://paste.debian.net/1261413/

Attached is the objump of dma-noncoherent.o for reference.

Note, if I replace the old/orignal instruction to my asm code for rz/five cmo and replace the errata id's to deadbeef the code works OK.

Cheers,
Prabhakar

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

#26 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 418 lines

Hi Heiko,

Quoted 402 lines · Prabhakar

Hi Heiko,

Quoted 89 lines · Heiko Stübner

Hi,

Am Montag, 21. November 2022, 10:50:09 CET schrieb Lad, Prabhakar:

Quoted 11 lines · Heiko Stuebner
Quoted 10 lines

From: Heiko Stuebner <[email protected]>

Alternatives live in a different section, so addresses used by call functions will point to wrong locations after the patch got applied.

Similar to arm64, adjust the location to consider that offset.

---

[...]

Quoted 71 lines

I have the below assembly code which I have tested without the alternatives for the RZ/Five CMO,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(".option push\n\t\n\t"                    \
         ".option norvc\n\t"                    \
         ".option norelax\n\t"                    \
         "addi sp,sp,-16\n\t"                    \
         "sd    s0,0(sp)\n\t"                    \
         "sd    ra,8(sp)\n\t"                    \
         "addi    s0,sp,16\n\t"                    \
         "mv a4,%6\n\t"                        \
         "mv a3,%5\n\t"                        \
         "mv a2,%4\n\t"                        \
         "mv a1,%3\n\t"                        \
         "mv a0,%0\n\t"                        \
         "call rzfive_cmo\n\t"                    \
         "ld    ra,8(sp)\n\t"                    \
         "ld    s0,0(sp)\n\t"                    \
         "addi    sp,sp,16\n\t"                    \
         ".option pop\n\t"                        \
         : : "r"(_cachesize),                    \
         "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
         "r"((unsigned long)(_start) + (_size)),            \
         "r"((unsigned long) (_start)),                \
         "r"((unsigned long) (_size)),                \
         "r"((unsigned long) (_dir)),                \
         "r"((unsigned long) (_ops))                \
         : "a0", "a1", "a2", "a3", "a4", "memory")

Now when integrate this with ALTERNATIVE_2() as below,

#define ALT_CMO_OP(_op, _start, _size, _cachesize, _dir, _ops) \

asm volatile(ALTERNATIVE_2(                        \
    __nops(14),                            \
    "mv a0, %1\n\t"                            \
    "j 2f\n\t"                            \
    "3:\n\t"                            \
    "cbo." __stringify(_op) " (a0)\n\t"                \
    "add a0, a0, %0\n\t"                        \
    "2:\n\t"                            \
    "bltu a0, %2, 3b\n\t"                        \
    __nops(8), 0, CPUFEATURE_ZICBOM, CONFIG_RISCV_ISA_ZICBOM,    \
    ".option push\n\t\n\t"                        \
    ".option norvc\n\t"                        \
    ".option norelax\n\t"                        \
    "addi sp,sp,-16\n\t"                        \
    "sd    s0,0(sp)\n\t"                        \
    "sd    ra,8(sp)\n\t"                        \
    "addi    s0,sp,16\n\t"                        \
    "mv a4,%6\n\t"                            \
    "mv a3,%5\n\t"                            \
    "mv a2,%4\n\t"                            \
    "mv a1,%3\n\t"                            \
    "mv a0,%0\n\t"                            \
    "call rzfive_cmo\n\t"                \
    "ld    ra,8(sp)\n\t"                        \
    "ld    s0,0(sp)\n\t"                        \
    "addi    sp,sp,16\n\t"                        \
    ".option pop\n\t"                        \
    , ANDESTECH_VENDOR_ID,                        \
            ERRATA_ANDESTECH_NO_IOCP, CONFIG_ERRATA_RZFIVE_CMO)    \
    : : "r"(_cachesize),                        \
    "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),    \
    "r"((unsigned long)(_start) + (_size)),            \
    "r"((unsigned long) (_start)),                \
    "r"((unsigned long) (_size)),                \
    "r"((unsigned long) (_dir)),                \
    "r"((unsigned long) (_ops))                \
    : "a0", "a1", "a2", "a3", "a4", "memory")
Quoted 9 lines
Quoted 4 lines

I am seeing kernel panic with this change. Looking at the riscv_alternative_fix_auipc_jalr() implementation it assumes the rest of the alternative options are calls too. Is my understanding correct here?

The loop walks through the instructions after the location got patched and checks if an instruction is an auipc and the next one is a jalr and only then adjusts the address accordingly.

Ok so my understanding was wrong here.

Quoted 3 lines

So it _should_ leave all other (non auipc+jalr) instructions alone. (hopefully)

Agreed.

Quoted 5 lines
Quoted 2 lines

Do you think this is the correct approach in my case?

It does look correct on first glance.

\o/

Quoted 9 lines

As I had that passing thought, are you actually calling
riscv_alternative_fix_auipc_jalr()
from your errata/.../foo.c after doing the patching?

I.e. with the current patchset, that function is only called from the cpufeature part, but for example not from the other patching locations. [and a future revision should probably change that :-) ]

I have made a local copy of riscv_alternative_fix_auipc_jalr() and then calling it after patch_text_nosync() referring to your patch for str functions.

Quoted 8 lines

After making sure that function actually runs, the next thing you could try is to have both the "original" code and the patch be identical, i.e. replace the cbo* part with your code as well and then just output the instructions via printk to check what the addresses do in both.

After riscv_alternative_fix_auipc_jalr() ran then both code variants should be identical when using the same code in both areas.

So I have added debug prints to match the instructions as below after and before patching:

static void riscv_alternative_print_inst(unsigned int *alt_ptr,
unsigned int len)

{
    int num_instr = len / sizeof(u32);
    int i;

    for (i = 0; i < num_instr; i++)
        pr_err("%s instruction: 0x%x\n", __func__, *(alt_ptr + i));

}

void __init_or_module andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
unsigned long archid, unsigned long impid, unsigned int stage)
{
....

    if (cpu_req_errata & tmp) {
        pr_err("stage: %x -> %px--> %x %x %x\n", stage, alt, tmp,
cpu_req_errata, alt->errata_id);
        pr_err("old:%ps alt:%ps len:%lx\n", alt->old_ptr,
alt->alt_ptr, alt->alt_len);
        pr_err("Print old start\n");
        riscv_alternative_print_inst(alt->old_ptr, alt->alt_len);
        pr_err("Print old end\n");
        patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);

        riscv_alternative_fix_auipc_jalr(alt->old_ptr, alt->alt_len,
                        alt->old_ptr - alt->alt_ptr);
        pr_err("Print patch start\n");
        riscv_alternative_print_inst(alt->alt_ptr, alt->alt_len);
        pr_err("Print patch end\n");
    }

.....
}

Below is the log:

[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print new old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0x78693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x80593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xcba080e7
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a2492c--> 1 1 0
[    0.000000] old:arch_sync_dma_for_device
alt:riscv_noncoherent_supported len:38
[    0.000000] Print  old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x970013
    ====================> This instruction doesn't look correct it
should be 0x13?
[    0.000000] Print  old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0x78713
[    0.000000] riscv_alternative_print_inst instruction: 0x78693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x80593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc82080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 or does it require the
offset too?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a24950--> 1 1 0
[    0.000000] old:arch_sync_dma_for_cpu alt:riscv_noncoherent_supported len:38
[    0.000000] Print old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x97
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0xeee080e7
      ====================> This instruction doesn't look correct it
should be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0x80693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x78593
[    0.000000] riscv_alternative_print_inst instruction: 0xe0513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc4a080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 or does it require the
offset too?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end
[    0.000000] stage: 0 -> ffffffff80a24974--> 1 1 0
[    0.000000] old:arch_dma_prep_coherent alt:riscv_noncoherent_supported len:38
[    0.000000] Print old start
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x970013
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x80e70000
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0xe720
====================> This instruction doesn't look correct it should
be 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] riscv_alternative_print_inst instruction: 0x13
[    0.000000] Print old end
[    0.000000] riscv_alternative_fix_auipc_jalr num instruction: 14
[    0.000000] Print patch start
[    0.000000] riscv_alternative_print_inst instruction: 0xff010113
[    0.000000] riscv_alternative_print_inst instruction: 0x813023
[    0.000000] riscv_alternative_print_inst instruction: 0x113423
[    0.000000] riscv_alternative_print_inst instruction: 0x1010413
[    0.000000] riscv_alternative_print_inst instruction: 0xf0713
[    0.000000] riscv_alternative_print_inst instruction: 0xe8693
[    0.000000] riscv_alternative_print_inst instruction: 0x88613
[    0.000000] riscv_alternative_print_inst instruction: 0x78593
[    0.000000] riscv_alternative_print_inst instruction: 0x30513
[    0.000000] riscv_alternative_print_inst instruction: 0x97
[    0.000000] riscv_alternative_print_inst instruction: 0xc12080e7
====================> This instruction doesn't look correct comparing
to objdump output this should be 000080e7 + offset?
[    0.000000] riscv_alternative_print_inst instruction: 0x813083
[    0.000000] riscv_alternative_print_inst instruction: 0x13403
[    0.000000] riscv_alternative_print_inst instruction: 0x1010113
[    0.000000] Print patch end

Here is the output from objdump of the file (dma-noncoherent.o):

000000000000032e <.L888^B1>:

 32e:    ff010113              addi    sp,sp,-16
 332:    00813023              sd    s0,0(sp)
 336:    00113423              sd    ra,8(sp)
 33a:    01010413              addi    s0,sp,16
 33e:    000f0713              mv    a4,t5
 342:    00078693              mv    a3,a5
 346:    00088613              mv    a2,a7
 34a:    00080593              mv    a1,a6
 34e:    000e0513              mv    a0,t3
 352:    00000097              auipc    ra,0x0
 356:    000080e7              jalr    ra # 352 <.L888^B1+0x24>
 35a:    00813083              ld    ra,8(sp)
 35e:    00013403              ld    s0,0(sp)
 362:    01010113              addi    sp,sp,16

0000000000000366 <.L888^B2>:

 366:    ff010113              addi    sp,sp,-16
 36a:    00813023              sd    s0,0(sp)
 36e:    00113423              sd    ra,8(sp)
 372:    01010413              addi    s0,sp,16
 376:    00078713              mv    a4,a5
 37a:    00078693              mv    a3,a5
 37e:    00088613              mv    a2,a7
 382:    00080593              mv    a1,a6
 386:    000e0513              mv    a0,t3
 38a:    00000097              auipc    ra,0x0
 38e:    000080e7              jalr    ra # 38a <.L888^B2+0x24>
 392:    00813083              ld    ra,8(sp)
 396:    00013403              ld    s0,0(sp)
 39a:    01010113              addi    sp,sp,16

000000000000039e <.L888^B3>:

 39e:    ff010113              addi    sp,sp,-16
 3a2:    00813023              sd    s0,0(sp)
 3a6:    00113423              sd    ra,8(sp)
 3aa:    01010413              addi    s0,sp,16
 3ae:    000f0713              mv    a4,t5
 3b2:    00080693              mv    a3,a6
 3b6:    00088613              mv    a2,a7
 3ba:    00078593              mv    a1,a5
 3be:    000e0513              mv    a0,t3
 3c2:    00000097              auipc    ra,0x0
 3c6:    000080e7              jalr    ra # 3c2 <.L888^B3+0x24>
 3ca:    00813083              ld    ra,8(sp)
 3ce:    00013403              ld    s0,0(sp)
 3d2:    01010113              addi    sp,sp,16

00000000000003d6 <.L888^B4>:

 3d6:    ff010113              addi    sp,sp,-16
 3da:    00813023              sd    s0,0(sp)
 3de:    00113423              sd    ra,8(sp)
 3e2:    01010413              addi    s0,sp,16
 3e6:    000f0713              mv    a4,t5
 3ea:    000e8693              mv    a3,t4
 3ee:    00088613              mv    a2,a7
 3f2:    00078593              mv    a1,a5
 3f6:    00030513              mv    a0,t1
 3fa:    00000097              auipc    ra,0x0
 3fe:    000080e7              jalr    ra # 3fa <.L888^B4+0x24>
 402:    00813083              ld    ra,8(sp)
 406:    00013403              ld    s0,0(sp)
 40a:    01010113              addi    sp,sp,16
Quoted 4 lines

Disassembly of section __ksymtab_strings:

Any pointers what could be happening?

Some more information,

- If I drop the riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I can see the instructions as 0x13 (nop) which is correct.

Quoted 4 lines

- if I call riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I dont see 0x13 (nop) consistently for old instructions.

which is to be expected I guess.

alt->old_ptr points to the memory location where the live kernel code lives.

I.e. the code at this location is the thing the kernel actually runs. The code at this location then gets overwritten by the alternative assembly.

Quoted 13 lines

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then just use patch_text_nosync() I can see the correct actual instruction being printed apart from jalr (is some sort of offset added to it as I see last 4 bits match?) and then is replaced correctly by the same alt instructions apart from the jalr (log [0]).

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then use patch_text_nosync() and riscv_alternative_fix_auipc_jalr() I can see the actual old instructions differs a bit and again the jalr instruction differs too in the patched code (log [1]).

Quoted 4 lines

[0] https://paste.debian.net/1261412/
[1] https://paste.debian.net/1261413/

Attached is the objump of dma-noncoherent.o for reference.

I did read that objdumps are not really conclusive when looking at auipc + jalr instructions, hence the printing of the actual instructions.

As either manually or with a helper like

	https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

In your log the two jalr instructions decode to different offsets,

	jalr x1, x1, -180
vs
	jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

Interestingly the

	auipc x1, 0

is 0 for both cases.

I'll try to build a real test-setup mimicing what you're doing tomorrow (european tomorrow).

Heiko

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

#27 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Am Montag, 21. November 2022, 23:17:11 CET schrieb Heiko Stübner:

Quoted 55 lines

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 11 lines

Some more information,

- If I drop the riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I can see the instructions as 0x13 (nop) which is correct.

- if I call riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I dont see 0x13 (nop) consistently for old instructions.

which is to be expected I guess.

alt->old_ptr points to the memory location where the live kernel code lives.

I.e. the code at this location is the thing the kernel actually runs. The code at this location then gets overwritten by the alternative assembly.

Quoted 13 lines

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then just use patch_text_nosync() I can see the correct actual instruction being printed apart from jalr (is some sort of offset added to it as I see last 4 bits match?) and then is replaced correctly by the same alt instructions apart from the jalr (log [0]).

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then use patch_text_nosync() and riscv_alternative_fix_auipc_jalr() I can see the actual old instructions differs a bit and again the jalr instruction differs too in the patched code (log [1]).

Quoted 4 lines

[0] https://paste.debian.net/1261412/
[1] https://paste.debian.net/1261413/

Attached is the objump of dma-noncoherent.o for reference.

I did read that objdumps are not really conclusive when looking at auipc + jalr instructions, hence the printing of the actual instructions.

As either manually or with a helper like

	https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

In your log the two jalr instructions decode to different offsets,

	jalr x1, x1, -180
vs
	jalr x1, x1, -834
Quoted 8 lines

Can you check what the patch_offset value is in your case?

Interestingly the

	auipc x1, 0

is 0 for both cases.

I'll try to build a real test-setup mimicing what you're doing tomorrow (european tomorrow).

also, is it possible for you to put your code on some github or so? Sometimes looking at the actual code makes things a lot easier :-)

Thanks
Heiko

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

#28 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Hi Heiko,

Quoted 63 lines · Heiko Stübner

Am Montag, 21. November 2022, 23:17:11 CET schrieb Heiko Stübner:

Quoted 61 lines

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 11 lines

Some more information,

- If I drop the riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I can see the instructions as 0x13 (nop) which is correct.

- if I call riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I dont see 0x13 (nop) consistently for old instructions.

which is to be expected I guess.

alt->old_ptr points to the memory location where the live kernel code lives.

I.e. the code at this location is the thing the kernel actually runs. The code at this location then gets overwritten by the alternative assembly.

Quoted 13 lines

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then just use patch_text_nosync() I can see the correct actual instruction being printed apart from jalr (is some sort of offset added to it as I see last 4 bits match?) and then is replaced correctly by the same alt instructions apart from the jalr (log [0]).

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then use patch_text_nosync() and riscv_alternative_fix_auipc_jalr() I can see the actual old instructions differs a bit and again the jalr instruction differs too in the patched code (log [1]).

Quoted 4 lines

[0] https://paste.debian.net/1261412/
[1] https://paste.debian.net/1261413/

Attached is the objump of dma-noncoherent.o for reference.

I did read that objdumps are not really conclusive when looking at auipc + jalr instructions, hence the printing of the actual instructions.

As either manually or with a helper like

      https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

In your log the two jalr instructions decode to different offsets,

      jalr x1, x1, -180
vs
      jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

Interestingly the

      auipc x1, 0

is 0 for both cases.

Quoted 7 lines
Quoted 2 lines

I'll try to build a real test-setup mimicing what you're doing tomorrow (european tomorrow).

also, is it possible for you to put your code on some github or so? Sometimes looking at the actual code makes things a lot easier :-)

I have pushed my changes here
https://github.com/prabhakarlad/linux/tree/rzfive-cmo

Cheers,
Prabhakar

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

#29 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stübner

Hi Heiko,

Quoted 7 lines · Heiko Stübner

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 5 lines

Hi Heiko,

Quoted 1 line · Prabhakar

<snip>

Quoted 12 lines
Quoted 11 lines

Some more information,

- If I drop the riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I can see the instructions as 0x13 (nop) which is correct.

- if I call riscv_alternative_fix_auipc_jalr() call after patch_text_nosync() and then print the alt->old_ptr instructions before patching I dont see 0x13 (nop) consistently for old instructions.

Quoted 5 lines

which is to be expected I guess.

alt->old_ptr points to the memory location where the live kernel code lives.

Agreed.

Quoted 4 lines

I.e. the code at this location is the thing the kernel actually runs. The code at this location then gets overwritten by the alternative assembly.

But shouldn't the actual code be nops before patching?

Quoted 19 lines
Quoted 14 lines

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then just use patch_text_nosync() I can see the correct actual instruction being printed apart from jalr (is some sort of offset added to it as I see last 4 bits match?) and then is replaced correctly by the same alt instructions apart from the jalr (log [0]).

- If I replace the nop's in the old instructions with my assembly code of rz/five cmo and then use patch_text_nosync() and riscv_alternative_fix_auipc_jalr() I can see the actual old instructions differs a bit and again the jalr instruction differs too in the patched code (log [1]).

Quoted 4 lines

[0] https://paste.debian.net/1261412/
[1] https://paste.debian.net/1261413/

Attached is the objump of dma-noncoherent.o for reference.

Quoted 9 lines

I did read that objdumps are not really conclusive when looking at auipc + jalr instructions, hence the printing of the actual instructions.

As either manually or with a helper like

        https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

OK, I will give it a try.

Quoted 7 lines

In your log the two jalr instructions decode to different offsets,

        jalr x1, x1, -180
vs
        jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

I'll check that and let you know.

Quoted 7 lines

Interestingly the

        auipc x1, 0

is 0 for both cases.

I'll try to build a real test-setup mimicing what you're doing tomorrow (european tomorrow).

Thank you!

Cheers,
Prabhakar

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

#30 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

In reply to Heiko Stübner

Hi Heiko,

Quoted 4 lines · Heiko Stübner

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 2 lines

Hi Heiko,

<snip>

Quoted 4 lines

As either manually or with a helper like

        https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7
Quoted 9 lines

you can then decode the actual instruction and compare.

In your log the two jalr instructions decode to different offsets,

        jalr x1, x1, -180
vs
        jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

patch_offset for the above case is -654.

Cheers,
Prabhakar

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

#31 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Am Dienstag, 22. November 2022, 11:59:57 CET schrieb Lad, Prabhakar:

Quoted 14 lines

Hi Heiko,

Quoted 4 lines · Heiko Stübner

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 2 lines

Hi Heiko,

<snip>

Quoted 6 lines

As either manually or with a helper like

        https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

Quoted 8 lines
Quoted 7 lines

In your log the two jalr instructions decode to different offsets,

        jalr x1, x1, -180
vs
        jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

patch_offset for the above case is -654.

which is a big indicator that the auipc-jalr-fixup function is not catching the instruction ... i.e. -180 - 654 = -834.

I managed to reproduce that issue with your branch now (after hacking up stuff a bit to run in qemu :-) ).

I'll try to find out where the fixup fails.

Heiko

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

#32 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Am Dienstag, 22. November 2022, 12:19:40 CET schrieb Heiko Stübner:

Quoted 24 lines

Am Dienstag, 22. November 2022, 11:59:57 CET schrieb Lad, Prabhakar:

Quoted 14 lines

Hi Heiko,

Quoted 4 lines · Heiko Stübner

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 2 lines

Hi Heiko,

<snip>

Quoted 6 lines

As either manually or with a helper like

        https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

Quoted 8 lines
Quoted 7 lines

In your log the two jalr instructions decode to different offsets,

        jalr x1, x1, -180
vs
        jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

patch_offset for the above case is -654.

Quoted 7 lines

which is a big indicator that the auipc-jalr-fixup function is not catching the instruction ... i.e. -180 - 654 = -834.

I managed to reproduce that issue with your branch now (after hacking up stuff a bit to run in qemu :-) ).

I'll try to find out where the fixup fails.

imagine me with a slightly red head now ... as there is a slightly embarrassing mistake in the fixup function ;-) .

When going from void* to unsigned int* pointers I have missed adjusting the actual patch-location.

The call needs to be

	patch_text_nosync(alt_ptr + i, call, 8);

instead of the current

	patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);

In my str* cases this didn't matter because "i" was 0 there, but in your longer assembly it actually patched the wrong location.

Heiko

============
For reference, my debug prints to find where the patching fails was:

arch/riscv/errata/renesas/errata.c +5 -1
index 986f1c762d72..a5a47c5e9ff8 100644
@@ -72,6 +72,7 @@ static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
        u32 rd1;
 
        for (i = 0; i < num_instr; i++) {
+printk("%s: looking at inst 0x%x\n", __func__, *(alt_ptr + i));
                /* is there a further instruction? */
                if (i + 1 >= num_instr)
                        continue;
@@ -84,6 +85,7 @@ static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
                if (rd1 != 1)
                        continue;
 
+printk("%s: -> found a auipc + jalr pair\n", __func__);
                /* get and adjust new target address */
                imm1 = EXTRACT_UTYPE_IMM(*(alt_ptr + i));
                imm1 += EXTRACT_ITYPE_IMM(*(alt_ptr + i + 1));
@@ -101,8 +103,10 @@ static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
                call[0] |= to_auipc_imm(imm1);
                call[1] |= to_jalr_imm(imm1);
 
+printk("%s: patching to 0x%x and 0x%x\n", __func__, call[0], call[1]);
                /* patch the call place again */
-               patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);
+               patch_text_nosync(alt_ptr + i, call, 8);
+printk("%s: patched to 0x%x and 0x%x\n", __func__, *(alt_ptr + i), *(alt_ptr + i + 1));
        }
 }
 

and then realizing that the "patching to" and "patched to" where different.

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

#33 lore raw

Re: [PATCH 5/7] RISC-V: fix auipc-jalr addresses in patched alternatives

Hi Heiko,

Quoted 29 lines · Heiko Stübner

Am Dienstag, 22. November 2022, 12:19:40 CET schrieb Heiko Stübner:

Quoted 27 lines

Am Dienstag, 22. November 2022, 11:59:57 CET schrieb Lad, Prabhakar:

Quoted 14 lines

Hi Heiko,

Quoted 4 lines · Heiko Stübner

Am Montag, 21. November 2022, 22:31:36 CET schrieb Lad, Prabhakar:

Quoted 2 lines

Hi Heiko,

<snip>

Quoted 6 lines

As either manually or with a helper like

        https://luplab.gitlab.io/rvcodecjs/#q=0xf4c080e7

you can then decode the actual instruction and compare.

Quoted 8 lines
Quoted 7 lines

In your log the two jalr instructions decode to different offsets,

        jalr x1, x1, -180
vs
        jalr x1, x1, -834

Can you check what the patch_offset value is in your case?

patch_offset for the above case is -654.

which is a big indicator that the auipc-jalr-fixup function is not catching the instruction ... i.e. -180 - 654 = -834.

Quoted 8 lines
Quoted 4 lines

I managed to reproduce that issue with your branch now (after hacking up stuff a bit to run in qemu :-) ).

I'll try to find out where the fixup fails.

imagine me with a slightly red head now ... as there is a slightly embarrassing mistake in the fixup function ;-) .

Cheer up now :-)

Quoted 7 lines

When going from void* to unsigned int* pointers I have missed adjusting the actual patch-location.

The call needs to be

        patch_text_nosync(alt_ptr + i, call, 8);

That did the trick! I have done some limited testing on the board (even replaced orignal instructions back to nop's even with its working too).

Quoted 6 lines

instead of the current

        patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);

In my str* cases this didn't matter because "i" was 0 there, but in your longer assembly it actually patched the wrong location.

Ahaa right the alt macro just had calls.

Quoted 38 lines

Heiko

============
For reference, my debug prints to find where the patching fails was:

arch/riscv/errata/renesas/errata.c +5 -1
index 986f1c762d72..a5a47c5e9ff8 100644
@@ -72,6 +72,7 @@ static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
        u32 rd1;
 
        for (i = 0; i < num_instr; i++) {
+printk("%s: looking at inst 0x%x\n", __func__, *(alt_ptr + i));
                /* is there a further instruction? */
                if (i + 1 >= num_instr)
                        continue;
@@ -84,6 +85,7 @@ static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
                if (rd1 != 1)
                        continue;
 
+printk("%s: -> found a auipc + jalr pair\n", __func__);
                /* get and adjust new target address */
                imm1 = EXTRACT_UTYPE_IMM(*(alt_ptr + i));
                imm1 += EXTRACT_ITYPE_IMM(*(alt_ptr + i + 1));
@@ -101,8 +103,10 @@ static void riscv_alternative_fix_auipc_jalr(unsigned int *alt_ptr,
                call[0] |= to_auipc_imm(imm1);
                call[1] |= to_jalr_imm(imm1);
 
+printk("%s: patching to 0x%x and 0x%x\n", __func__, call[0], call[1]);
                /* patch the call place again */
-               patch_text_nosync(alt_ptr + i * sizeof(u32), call, 8);
+               patch_text_nosync(alt_ptr + i, call, 8);
+printk("%s: patched to 0x%x and 0x%x\n", __func__, *(alt_ptr + i), *(alt_ptr + i + 1));
        }
 }
 
Quoted 2 lines

and then realizing that the "patching to" and "patched to" where different.

Thanks for the hunk.

Quoted 1 line

Now waiting for your v3. Meanwhile, I'll look into the ALT3() macro.

Cheers,
Prabhakar

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

#34 lore raw

PATCH 6/7 RISC-V: add infrastructure to allow different str* implementations

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Depending on supported extensions on specific RISC-V cores, optimized str* functions might make sense.

This adds basic infrastructure to allow patching the function calls via alternatives later on.

The main idea is to have the core str* functions be inline functions which then call the most optimized variant and this call then be replaced via alternatives.

The big advantage is that we don't need additional calls. Though we need to duplicate the generic functions as the main code expects either itself or the architecture to provide the str* functions.

The added *_generic functions are done in assembler (taken from disassembling the main-kernel functions for now) to allow us to control the used registers.

---
 arch/riscv/include/asm/string.h | 66 +++++++++++++++++++++++++++++++++
 arch/riscv/kernel/image-vars.h  |  6 +--
 arch/riscv/lib/Makefile         |  3 ++
 arch/riscv/lib/strcmp.S         | 39 +++++++++++++++++++
 arch/riscv/lib/strlen.S         | 29 +++++++++++++++
 arch/riscv/lib/strncmp.S        | 41 ++++++++++++++++++++
 6 files changed, 181 insertions(+), 3 deletions(-)
 create mode 100644 arch/riscv/lib/strcmp.S
 create mode 100644 arch/riscv/lib/strlen.S
 create mode 100644 arch/riscv/lib/strncmp.S
arch/riscv/include/asm/string.h +66 -0
index 909049366555..b98481d2d154 100644
@@ -18,6 +18,72 @@ extern asmlinkage void *__memcpy(void *, const void *, size_t);
 #define __HAVE_ARCH_MEMMOVE
 extern asmlinkage void *memmove(void *, const void *, size_t);
 extern asmlinkage void *__memmove(void *, const void *, size_t);
+
+#define __HAVE_ARCH_STRCMP
+extern asmlinkage int __strcmp_generic(const char *cs, const char *ct);
+
+static inline int strcmp(const char *cs, const char *ct)
+{
+#ifdef RISCV_EFISTUB
+	return __strcmp_generic(cs, ct);
+#else
+	register const char *a0 asm("a0") = cs;
+	register const char *a1 asm("a1") = ct;
+	register int a0_out asm("a0");
+
+	asm volatile("call __strcmp_generic\n\t"
+		: "=r"(a0_out)
+		: "r"(a0), "r"(a1)
+		: "ra", "t0", "t1", "t2");
+
+	return a0_out;
+#endif
+}
+
+#define __HAVE_ARCH_STRNCMP
+extern asmlinkage int __strncmp_generic(const char *cs,
+					const char *ct, size_t count);
+
+static inline int strncmp(const char *cs, const char *ct, size_t count)
+{
+#ifdef RISCV_EFISTUB
+	return __strncmp_generic(cs, ct, count);
+#else
+	register const char *a0 asm("a0") = cs;
+	register const char *a1 asm("a1") = ct;
+	register size_t a2 asm("a2") = count;
+	register int a0_out asm("a0");
+
+	asm volatile("call __strncmp_generic\n\t"
+		: "=r"(a0_out)
+		: "r"(a0), "r"(a1), "r"(a2)
+		: "ra", "t0", "t1", "t2");
+
+	return a0_out;
+#endif
+}
+
+#define __HAVE_ARCH_STRLEN
+extern asmlinkage __kernel_size_t __strlen_generic(const char *);
+
+static inline __kernel_size_t strlen(const char *s)
+{
+#ifdef RISCV_EFISTUB
+	return __strlen_generic(s);
+#else
+	register const char *a0 asm("a0") = s;
+	register int a0_out asm("a0");
+
+	asm volatile(
+		"call __strlen_generic\n\t"
+		: "=r"(a0_out)
+		: "r"(a0)
+		: "ra", "t0", "t1");
+
+	return a0_out;
+#endif
+}
+
 /* For those files which don't want to check by kasan. */
 #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
 #define memcpy(dst, src, len) __memcpy(dst, src, len)
arch/riscv/kernel/image-vars.h +3 -3
index d6e5f739905e..2f270b9fde63 100644
@@ -25,10 +25,10 @@
  */
 __efistub_memcmp		= memcmp;
 __efistub_memchr		= memchr;
-__efistub_strlen		= strlen;
+__efistub___strlen_generic	= __strlen_generic;
 __efistub_strnlen		= strnlen;
-__efistub_strcmp		= strcmp;
-__efistub_strncmp		= strncmp;
+__efistub___strcmp_generic	= __strcmp_generic;
+__efistub___strncmp_generic	= __strncmp_generic;
 __efistub_strrchr		= strrchr;
 
 __efistub__start		= _start;
arch/riscv/lib/Makefile +3 -0
index 25d5c9664e57..6c74b0bedd60 100644
@@ -3,6 +3,9 @@ lib-y			+= delay.o
 lib-y			+= memcpy.o
 lib-y			+= memset.o
 lib-y			+= memmove.o
+lib-y			+= strcmp.o
+lib-y			+= strlen.o
+lib-y			+= strncmp.o
 lib-$(CONFIG_MMU)	+= uaccess.o
 lib-$(CONFIG_64BIT)	+= tishift.o
 
arch/riscv/lib/strcmp.S new file +39 -0
new file mode 100644
index 000000000000..f23a5c5e39d8
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+/* int __strcmp_generic(const char *cs, const char *ct) */
+ENTRY(__strcmp_generic)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3, t4, t5
+	 */
+	mv	t2, a1
+1:
+	lbu	t1, 0(a0)
+	lbu	t0, 0(a1)
+	addi	a0, a0, 1
+	addi	a1, a1, 1
+	beq	t1, t0, 3f
+	li	a0, 1
+	bgeu	t1, t0, 2f
+	li	a0, -1
+2:
+	mv	a1, t2
+	ret
+3:
+	bnez	t1, 1b
+	li	a0, 0
+	j	2b
+END(__strcmp_generic)
+EXPORT_SYMBOL(__strcmp_generic)
arch/riscv/lib/strlen.S new file +29 -0
new file mode 100644
index 000000000000..e0e7440ac724
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+/* int __strlen_generic(const char *s) */
+ENTRY(__strlen_generic)
+	/*
+	 * Returns
+	 *   a0 - string length
+	 *
+	 * Parameters
+	 *   a0 - String to measure
+	 *
+	 * Clobbers:
+	 *   t0, t1
+	 */
+	mv	t1, a0
+1:
+	lbu	t0, 0(t1)
+	bnez	t0, 2f
+	sub	a0, t1, a0
+	ret
+2:
+	addi	t1, t1, 1
+	j	1b
+END(__strlen_generic)
+EXPORT_SYMBOL(__strlen_generic)
arch/riscv/lib/strncmp.S new file +41 -0

41 lines not on this page · raw message

Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 6/7
#35 lore raw

Re: [PATCH 6/7] RISC-V: add infrastructure to allow different str* implementations

Quoted 149 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Depending on supported extensions on specific RISC-V cores, optimized str* functions might make sense.

This adds basic infrastructure to allow patching the function calls via alternatives later on.

The main idea is to have the core str* functions be inline functions which then call the most optimized variant and this call then be replaced via alternatives.

The big advantage is that we don't need additional calls. Though we need to duplicate the generic functions as the main code expects either itself or the architecture to provide the str* functions.

The added *_generic functions are done in assembler (taken from disassembling the main-kernel functions for now) to allow us to control the used registers.

---
 arch/riscv/include/asm/string.h | 66 +++++++++++++++++++++++++++++++++
 arch/riscv/kernel/image-vars.h  |  6 +--
 arch/riscv/lib/Makefile         |  3 ++
 arch/riscv/lib/strcmp.S         | 39 +++++++++++++++++++
 arch/riscv/lib/strlen.S         | 29 +++++++++++++++
 arch/riscv/lib/strncmp.S        | 41 ++++++++++++++++++++
 6 files changed, 181 insertions(+), 3 deletions(-)
 create mode 100644 arch/riscv/lib/strcmp.S
 create mode 100644 arch/riscv/lib/strlen.S
 create mode 100644 arch/riscv/lib/strncmp.S
arch/riscv/include/asm/string.h +66 -0
index 909049366555..b98481d2d154 100644
@@ -18,6 +18,72 @@ extern asmlinkage void *__memcpy(void *, const void *, size_t);
 #define __HAVE_ARCH_MEMMOVE
 extern asmlinkage void *memmove(void *, const void *, size_t);
 extern asmlinkage void *__memmove(void *, const void *, size_t);
+
+#define __HAVE_ARCH_STRCMP
+extern asmlinkage int __strcmp_generic(const char *cs, const char *ct);
+
+static inline int strcmp(const char *cs, const char *ct)
+{
+#ifdef RISCV_EFISTUB
+	return __strcmp_generic(cs, ct);
+#else
+	register const char *a0 asm("a0") = cs;
+	register const char *a1 asm("a1") = ct;
+	register int a0_out asm("a0");
+
+	asm volatile("call __strcmp_generic\n\t"
+		: "=r"(a0_out)
+		: "r"(a0), "r"(a1)
+		: "ra", "t0", "t1", "t2");
+
+	return a0_out;
+#endif
+}
+
+#define __HAVE_ARCH_STRNCMP
+extern asmlinkage int __strncmp_generic(const char *cs,
+					const char *ct, size_t count);
+
+static inline int strncmp(const char *cs, const char *ct, size_t count)
+{
+#ifdef RISCV_EFISTUB
+	return __strncmp_generic(cs, ct, count);
+#else
+	register const char *a0 asm("a0") = cs;
+	register const char *a1 asm("a1") = ct;
+	register size_t a2 asm("a2") = count;
+	register int a0_out asm("a0");
+
+	asm volatile("call __strncmp_generic\n\t"
+		: "=r"(a0_out)
+		: "r"(a0), "r"(a1), "r"(a2)
+		: "ra", "t0", "t1", "t2");
+
+	return a0_out;
+#endif
+}
+
+#define __HAVE_ARCH_STRLEN
+extern asmlinkage __kernel_size_t __strlen_generic(const char *);
+
+static inline __kernel_size_t strlen(const char *s)
+{
+#ifdef RISCV_EFISTUB
+	return __strlen_generic(s);
+#else
+	register const char *a0 asm("a0") = s;
+	register int a0_out asm("a0");
+
+	asm volatile(
+		"call __strlen_generic\n\t"
+		: "=r"(a0_out)
+		: "r"(a0)
+		: "ra", "t0", "t1");
+
+	return a0_out;
+#endif
+}
+
 /* For those files which don't want to check by kasan. */
 #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
 #define memcpy(dst, src, len) __memcpy(dst, src, len)
arch/riscv/kernel/image-vars.h +3 -3
index d6e5f739905e..2f270b9fde63 100644
@@ -25,10 +25,10 @@
  */
 __efistub_memcmp		= memcmp;
 __efistub_memchr		= memchr;
-__efistub_strlen		= strlen;
+__efistub___strlen_generic	= __strlen_generic;
 __efistub_strnlen		= strnlen;
-__efistub_strcmp		= strcmp;
-__efistub_strncmp		= strncmp;
+__efistub___strcmp_generic	= __strcmp_generic;
+__efistub___strncmp_generic	= __strncmp_generic;
 __efistub_strrchr		= strrchr;
 
 __efistub__start		= _start;
arch/riscv/lib/Makefile +3 -0
index 25d5c9664e57..6c74b0bedd60 100644
@@ -3,6 +3,9 @@ lib-y			+= delay.o
 lib-y			+= memcpy.o
 lib-y			+= memset.o
 lib-y			+= memmove.o
+lib-y			+= strcmp.o
+lib-y			+= strlen.o
+lib-y			+= strncmp.o
 lib-$(CONFIG_MMU)	+= uaccess.o
 lib-$(CONFIG_64BIT)	+= tishift.o
 
arch/riscv/lib/strcmp.S new file +1 -0
new file mode 100644
index 000000000000..f23a5c5e39d8
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */

arch/riscv/lib/strcmp.S line 11

Quoted 10 lines
arch/riscv/lib/strcmp.S new file +10 -0
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+/* int __strcmp_generic(const char *cs, const char *ct) */
+ENTRY(__strcmp_generic)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp

The strncmp_generic one below says "like strncmp" too. Given the below copy paste stuff, is this one copy paste or intentional?

arch/riscv/lib/strcmp.S line 16

Quoted 5 lines
arch/riscv/lib/strcmp.S new file +5 -0
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare

Above line is a copy paste error?

arch/riscv/lib/strcmp.S line 19

Quoted 3 lines
arch/riscv/lib/strcmp.S new file +3 -0
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3, t4, t5

As is this one?

Other than those, seems to make sense?

Quoted 110 lines
arch/riscv/lib/strcmp.S new file +20 -0
+	 */
+	mv	t2, a1
+1:
+	lbu	t1, 0(a0)
+	lbu	t0, 0(a1)
+	addi	a0, a0, 1
+	addi	a1, a1, 1
+	beq	t1, t0, 3f
+	li	a0, 1
+	bgeu	t1, t0, 2f
+	li	a0, -1
+2:
+	mv	a1, t2
+	ret
+3:
+	bnez	t1, 1b
+	li	a0, 0
+	j	2b
+END(__strcmp_generic)
+EXPORT_SYMBOL(__strcmp_generic)
arch/riscv/lib/strlen.S new file +29 -0
new file mode 100644
index 000000000000..e0e7440ac724
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+/* int __strlen_generic(const char *s) */
+ENTRY(__strlen_generic)
+	/*
+	 * Returns
+	 *   a0 - string length
+	 *
+	 * Parameters
+	 *   a0 - String to measure
+	 *
+	 * Clobbers:
+	 *   t0, t1
+	 */
+	mv	t1, a0
+1:
+	lbu	t0, 0(t1)
+	bnez	t0, 2f
+	sub	a0, t1, a0
+	ret
+2:
+	addi	t1, t1, 1
+	j	1b
+END(__strlen_generic)
+EXPORT_SYMBOL(__strlen_generic)
arch/riscv/lib/strncmp.S new file +41 -0
new file mode 100644
index 000000000000..8d271cd0df72
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+/* int __strncmp_generic(const char *cs, const char *ct, size_t count) */
+ENTRY(__strncmp_generic)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare
+	 *
+	 * Clobbers
+	 *   t0, t1, t2
+	 */
+	li	t0, 0
+1:
+	beq	a2, t0, 4f
+	add	t1, a0, t0
+	add	t2, a1, t0
+	lbu	t1, 0(t1)
+	lbu	t2, 0(t2)
+	beq	t1, t2, 3f
+	li	a0, 1
+	bgeu	t1, t2, 2f
+	li	a0, -1
+2:
+	ret
+3:
+	addi	t0, t0, 1
+	bnez	t1, 1b
+4:
+	li	a0, 0
+	j	2b
+END(__strncmp_generic)
+EXPORT_SYMBOL(__strncmp_generic)
Signature · 7 lines
2.35.1


_______________________________________________
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

#36 lore raw

PATCH 7/7 RISC-V: add zbb support to string functions

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Add handling for ZBB extension and add support for using it as a variant for optimized string functions.

---
 arch/riscv/Kconfig                   |  23 ++++++
 arch/riscv/include/asm/errata_list.h |   3 +-
 arch/riscv/include/asm/hwcap.h       |   1 +
 arch/riscv/include/asm/string.h      |  29 ++++++--
 arch/riscv/kernel/cpu.c              |   1 +
 arch/riscv/kernel/cpufeature.c       |  18 +++++
 arch/riscv/lib/Makefile              |   3 +
 arch/riscv/lib/strcmp_zbb.S          |  91 +++++++++++++++++++++++
 arch/riscv/lib/strlen_zbb.S          |  98 +++++++++++++++++++++++++
 arch/riscv/lib/strncmp_zbb.S         | 106 +++++++++++++++++++++++++++
 10 files changed, 366 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/lib/strcmp_zbb.S
 create mode 100644 arch/riscv/lib/strlen_zbb.S
 create mode 100644 arch/riscv/lib/strncmp_zbb.S
arch/riscv/Kconfig +23 -0
index acfc4d298aab..56633931e808 100644
@@ -411,6 +411,29 @@ config RISCV_ISA_SVPBMT
 
 	   If you don't know what to do here, say Y.
 
+config TOOLCHAIN_HAS_ZBB
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zbb)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbb)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900
+
+config RISCV_ISA_ZBB
+	bool "Zbb extension support for "
+	depends on TOOLCHAIN_HAS_ZBB
+	depends on !XIP_KERNEL && MMU
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZBB
+	   extension (basic bit manipulation) and enable its usage.
+
+	   The Zbb extension provides instructions to accelerate a number
+	   of bit-specific operations (count bit population, sign extending,
+	   bitrotation, etc).
+
+	   If you don't know what to do here, say Y.
+
 config TOOLCHAIN_HAS_ZICBOM
 	bool
 	default y
arch/riscv/include/asm/errata_list.h +2 -1
index 4180312d2a70..95e626b7281e 100644
@@ -24,7 +24,8 @@
 
 #define	CPUFEATURE_SVPBMT 0
 #define	CPUFEATURE_ZICBOM 1
-#define	CPUFEATURE_NUMBER 2
+#define	CPUFEATURE_ZBB 2
+#define	CPUFEATURE_NUMBER 3
 
 #ifdef __ASSEMBLY__
 
arch/riscv/include/asm/hwcap.h +1 -0
index b22525290073..ac5555fd9788 100644
@@ -59,6 +59,7 @@ enum riscv_isa_ext_id {
 	RISCV_ISA_EXT_ZIHINTPAUSE,
 	RISCV_ISA_EXT_SSTC,
 	RISCV_ISA_EXT_SVINVAL,
+	RISCV_ISA_EXT_ZBB,
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/include/asm/string.h +23 -6
index b98481d2d154..806c402c874e 100644
@@ -6,6 +6,8 @@
 #ifndef _ASM_RISCV_STRING_H
 #define _ASM_RISCV_STRING_H
 
+#include <asm/alternative-macros.h>
+#include <asm/errata_list.h>
 #include <linux/types.h>
 #include <linux/linkage.h>
 
@@ -21,6 +23,7 @@ extern asmlinkage void *__memmove(void *, const void *, size_t);
 
 #define __HAVE_ARCH_STRCMP
 extern asmlinkage int __strcmp_generic(const char *cs, const char *ct);
+extern asmlinkage int __strcmp_zbb(const char *cs, const char *ct);
 
 static inline int strcmp(const char *cs, const char *ct)
 {
@@ -31,10 +34,14 @@ static inline int strcmp(const char *cs, const char *ct)
 	register const char *a1 asm("a1") = ct;
 	register int a0_out asm("a0");
 
-	asm volatile("call __strcmp_generic\n\t"
+	asm volatile(
+		ALTERNATIVE(
+			"call __strcmp_generic\n\t",
+			"call __strcmp_zbb\n\t",
+			0, CPUFEATURE_ZBB, CONFIG_RISCV_ISA_ZBB)
 		: "=r"(a0_out)
 		: "r"(a0), "r"(a1)
-		: "ra", "t0", "t1", "t2");
+		: "ra", "t0", "t1", "t2", "t3", "t4", "t5");
 
 	return a0_out;
 #endif
@@ -43,6 +50,8 @@ static inline int strcmp(const char *cs, const char *ct)
 #define __HAVE_ARCH_STRNCMP
 extern asmlinkage int __strncmp_generic(const char *cs,
 					const char *ct, size_t count);
+extern asmlinkage int __strncmp_zbb(const char *cs,
+				    const char *ct, size_t count);
 
 static inline int strncmp(const char *cs, const char *ct, size_t count)
 {
@@ -54,10 +63,14 @@ static inline int strncmp(const char *cs, const char *ct, size_t count)
 	register size_t a2 asm("a2") = count;
 	register int a0_out asm("a0");
 
-	asm volatile("call __strncmp_generic\n\t"
+	asm volatile(
+		ALTERNATIVE(
+			"call __strncmp_generic\n\t",
+			"call __strncmp_zbb\n\t",
+			0, CPUFEATURE_ZBB, CONFIG_RISCV_ISA_ZBB)
 		: "=r"(a0_out)
 		: "r"(a0), "r"(a1), "r"(a2)
-		: "ra", "t0", "t1", "t2");
+		: "ra", "t0", "t1", "t2", "t3", "t4", "t5", "t6");
 
 	return a0_out;
 #endif
@@ -65,6 +78,7 @@ static inline int strncmp(const char *cs, const char *ct, size_t count)
 
 #define __HAVE_ARCH_STRLEN
 extern asmlinkage __kernel_size_t __strlen_generic(const char *);
+extern asmlinkage __kernel_size_t __strlen_zbb(const char *);
 
 static inline __kernel_size_t strlen(const char *s)
 {
@@ -75,10 +89,13 @@ static inline __kernel_size_t strlen(const char *s)
 	register int a0_out asm("a0");
 
 	asm volatile(
-		"call __strlen_generic\n\t"
+		ALTERNATIVE(
+			"call __strlen_generic\n\t",
+			"call __strlen_zbb\n\t",
+			0, CPUFEATURE_ZBB, CONFIG_RISCV_ISA_ZBB)
 		: "=r"(a0_out)
 		: "r"(a0)
-		: "ra", "t0", "t1");
+		: "ra", "t0", "t1", "t2", "t3");
 
 	return a0_out;
 #endif
arch/riscv/kernel/cpu.c +1 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
arch/riscv/kernel/cpufeature.c +18 -0
index 026512ca9c4c..f19b9d4e2dca 100644
@@ -201,6 +201,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
 				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 				SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
 				SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);
@@ -278,6 +279,20 @@ static bool __init_or_module cpufeature_probe_zicbom(unsigned int stage)
 	return true;
 }
 
+static bool __init_or_module cpufeature_probe_zbb(unsigned int stage)
+{
+	if (!IS_ENABLED(CONFIG_RISCV_ISA_ZBB))
+		return false;
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return false;
+
+	if (!riscv_isa_extension_available(NULL, ZBB))
+		return false;
+
+	return true;
+}
+
 /*
  * Probe presence of individual extensions.
  *
@@ -295,6 +310,9 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
 	if (cpufeature_probe_zicbom(stage))
 		cpu_req_feature |= BIT(CPUFEATURE_ZICBOM);
 
+	if (cpufeature_probe_zbb(stage))
+		cpu_req_feature |= BIT(CPUFEATURE_ZBB);
+
 	return cpu_req_feature;
 }
 
arch/riscv/lib/Makefile +3 -0
index 6c74b0bedd60..b632483f851c 100644
@@ -4,8 +4,11 @@ lib-y			+= memcpy.o
 lib-y			+= memset.o
 lib-y			+= memmove.o
 lib-y			+= strcmp.o
+lib-$(CONFIG_RISCV_ISA_ZBB) += strcmp_zbb.o
 lib-y			+= strlen.o
+lib-$(CONFIG_RISCV_ISA_ZBB) += strlen_zbb.o
 lib-y			+= strncmp.o
+lib-$(CONFIG_RISCV_ISA_ZBB) += strncmp_zbb.o
 lib-$(CONFIG_MMU)	+= uaccess.o
 lib-$(CONFIG_64BIT)	+= tishift.o
 
arch/riscv/lib/strcmp_zbb.S new file +91 -0

91 lines not on this page · raw message

arch/riscv/lib/strlen_zbb.S new file +98 -0

98 lines not on this page · raw message

arch/riscv/lib/strncmp_zbb.S new file +106 -0

106 lines not on this page · raw message

Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 7/7
#37 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Hey Heiko,
I always seem to forget to open my mails.. I swear I'm not being rude!

In the back of my head while looking at this series, I've been wondering about Palmer's hwcap stuff. Wonder what the craic is there - I assume he's just been too busy with the profile stuff. Ditto Ztso, but iirc that's related. Anyway, that's just an aside as I was reading through the patch & typed it somewhere lest I forget to bring it up elsewhere.

Quoted 27 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Add handling for ZBB extension and add support for using it as a variant for optimized string functions.

---
 arch/riscv/Kconfig                   |  23 ++++++
 arch/riscv/include/asm/errata_list.h |   3 +-
 arch/riscv/include/asm/hwcap.h       |   1 +
 arch/riscv/include/asm/string.h      |  29 ++++++--
 arch/riscv/kernel/cpu.c              |   1 +
 arch/riscv/kernel/cpufeature.c       |  18 +++++
 arch/riscv/lib/Makefile              |   3 +
 arch/riscv/lib/strcmp_zbb.S          |  91 +++++++++++++++++++++++
 arch/riscv/lib/strlen_zbb.S          |  98 +++++++++++++++++++++++++
 arch/riscv/lib/strncmp_zbb.S         | 106 +++++++++++++++++++++++++++
 10 files changed, 366 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/lib/strcmp_zbb.S
 create mode 100644 arch/riscv/lib/strlen_zbb.S
 create mode 100644 arch/riscv/lib/strncmp_zbb.S
arch/riscv/Kconfig +0 -0
index acfc4d298aab..56633931e808 100644

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 419

Quoted 9 lines
arch/riscv/Kconfig +6 -0
@@ -411,6 +411,29 @@ config RISCV_ISA_SVPBMT
 
 	   If you don't know what to do here, say Y.
 
+config TOOLCHAIN_HAS_ZBB
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zbb)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbb)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900

Drew wants to switch away from this method and use insn defs instead - and I must admit it does look cleaner! Check out his Zicboz series and see what you think of it.

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 422

Quoted 2 lines
arch/riscv/Kconfig +2 -0
+config RISCV_ISA_ZBB
+	bool "Zbb extension support for "

Missing some words in this line?

Quoted 36 lines
arch/riscv/Kconfig +14 -0
+	depends on TOOLCHAIN_HAS_ZBB
+	depends on !XIP_KERNEL && MMU
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZBB
+	   extension (basic bit manipulation) and enable its usage.
+
+	   The Zbb extension provides instructions to accelerate a number
+	   of bit-specific operations (count bit population, sign extending,
+	   bitrotation, etc).
+
+	   If you don't know what to do here, say Y.
+
 config TOOLCHAIN_HAS_ZICBOM
 	bool
 	default y
arch/riscv/include/asm/errata_list.h +2 -1
index 4180312d2a70..95e626b7281e 100644
@@ -24,7 +24,8 @@
 
 #define	CPUFEATURE_SVPBMT 0
 #define	CPUFEATURE_ZICBOM 1
-#define	CPUFEATURE_NUMBER 2
+#define	CPUFEATURE_ZBB 2
+#define	CPUFEATURE_NUMBER 3
 
 #ifdef __ASSEMBLY__
 
arch/riscv/include/asm/hwcap.h +0 -0
index b22525290073..ac5555fd9788 100644

arch/riscv/include/asm/hwcap.h enum riscv_isa_ext_id { · line 62

Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1 -0
@@ -59,6 +59,7 @@ enum riscv_isa_ext_id {
 	RISCV_ISA_EXT_ZIHINTPAUSE,
 	RISCV_ISA_EXT_SSTC,
 	RISCV_ISA_EXT_SVINVAL,
+	RISCV_ISA_EXT_ZBB,

With ZIHINTPAUSE before SSTC and SVINIVAL I assume this is not something we are canonically ordering but I never, ever know which ones we are allowed to re-order at will.

Quoted 8 lines
arch/riscv/include/asm/hwcap.h +0 -0
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 7 lines
arch/riscv/kernel/cpu.c +1 -0
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 207

Quoted 12 lines
arch/riscv/kernel/cpufeature.c +1 -0
index 026512ca9c4c..f19b9d4e2dca 100644
@@ -201,6 +201,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
 				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 				SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
 				SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);

This one looks like it is, sstc aside. Same question as above, can I reorder this one? I'll send a patch for it if I can...

arch/riscv/lib/strcmp_zbb.S line 4

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +4 -0
new file mode 100644
index 000000000000..aff9b941d3ee
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>

Is a Co-developed-by: appropriate then?

Quoted 18 lines
arch/riscv/lib/strcmp_zbb.S new file +18 -0
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+#define src1		a0
+#define result		a0
+#define src2		t5
+#define data1		t0
+#define data2		t1
+#define align		t2
+#define data1_orcb	t3
+#define m1		t4
+
+.option push
+.option arch,+zbb
+

arch/riscv/lib/strcmp_zbb.S line 32

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +10 -0
+/* int __strcmp_zbb(const char *cs, const char *ct) */
+ENTRY(__strcmp_zbb)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare

Same copy paste mistake here with a2?

arch/riscv/lib/strcmp_zbb.S line 42

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +10 -0
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3, t4, t5
+	 */
+	mv	src2, a1
+
+	or	align, src1, src2
+	li	m1, -1
+	and	align, align, SZREG-1
+	bnez	align, 3f

Line of whitespace here would be nice.

Quoted 4 lines
arch/riscv/lib/strcmp_zbb.S new file +4 -0
+	/* Main loop for aligned string.  */
+	.p2align 3
+1:
+	REG_L	data1, 0(src1)

arch/riscv/lib/strcmp_zbb.S line 56

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +10 -0
+	REG_L	data2, 0(src2)
+	orc.b	data1_orcb, data1
+	bne	data1_orcb, m1, 2f
+	addi	src1, src1, SZREG
+	addi	src2, src2, SZREG
+	beq	data1, data2, 1b
+
+	/* Words don't match, and no null byte in the first
+	 * word. Get bytes in big-endian order and compare.  */
+#ifndef CONFIG_CPU_BIG_ENDIAN

I know this is a lift from the reference implementation in the spec, but do we actually need this ifndef section?

arch/riscv/lib/strcmp_zbb.S line 68

Quoted 12 lines
arch/riscv/lib/strcmp_zbb.S new file +12 -0
+	rev8	data1, data1
+	rev8	data2, data2
+#endif
+	/* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence.  */
+	sltu	result, data1, data2
+	neg	result, result
+	ori	result, result, 1
+	ret
+
+2:
+	/* Found a null byte.
+	 * If words don't match, fall back to simple loop.  */

Can the multiline comments match the usual multiline comment style that you used as the start of the function?

Quoted 73 lines
arch/riscv/lib/strcmp_zbb.S new file +23 -0
+	bne	data1, data2, 3f
+
+	/* Otherwise, strings are equal.  */
+	li	result, 0
+	ret
+
+	/* Simple loop for misaligned strings.  */
+	.p2align 3
+3:
+	lbu	data1, 0(src1)
+	lbu	data2, 0(src2)
+	addi	src1, src1, 1
+	addi	src2, src2, 1
+	bne	data1, data2, 4f
+	bnez	data1, 3b
+
+4:
+	sub	result, data1, data2
+	ret
+END(__strcmp_zbb)
+EXPORT_SYMBOL(__strcmp_zbb)
+
+.option pop
arch/riscv/lib/strlen_zbb.S new file +44 -0
new file mode 100644
index 000000000000..bc8d3607a32f
@@ -0,0 +1,98 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+#define src		a0
+#define result		a0
+#define addr		t0
+#define data		t1
+#define offset		t2
+#define offset_bits	t2
+#define valid_bytes	t3
+#define m1		t3
+
+#ifdef CONFIG_CPU_BIG_ENDIAN
+# define CZ	clz
+# define SHIFT	sll
+#else
+# define CZ	ctz
+# define SHIFT	srl
+#endif
+
+.option push
+.option arch,+zbb
+
+/* int __strlen_zbb(const char *s) */
+ENTRY(__strlen_zbb)
+	/*
+	 * Returns
+	 *   a0 - string length
+	 *
+	 * Parameters
+	 *   a0 - String to measure
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3
+	 */
+
+	/* Number of irrelevant bytes in the first word.  */

arch/riscv/lib/strlen_zbb.S line 54

Quoted 10 lines
arch/riscv/lib/strlen_zbb.S new file +10 -0
+	andi	offset, src, SZREG-1
+	/* Align pointer.  */
+	andi	addr, src, -SZREG
+
+	li	valid_bytes, SZREG
+	sub	valid_bytes, valid_bytes, offset
+	slli	offset_bits, offset, RISCV_LGPTR
+
+	/* Get the first word.  */
+	REG_L	data, 0(addr)

A line of whitespace prior to the comments would go a long way here with making this a little more readable - especially as a diff in a mailclient.

I am oh-so-far from an expert on this kind of stuff, but these three functions seem to match up with the reference implementations in the spec. With the couple different nit pick bits fixed, feel free to tack on:

Anyways, hopefully I've not missed a bunch of should-be-obvious things while trying to review the series..

Thanks,
Conor.

Quoted 164 lines
arch/riscv/lib/strlen_zbb.S new file +44 -0
+	/* Shift away the partial data we loaded to remove the irrelevant bytes
+	 * preceding the string with the effect of adding NUL bytes at the
+	 * end of the string.  */
+	SHIFT	data, data, offset_bits
+	/* Convert non-NUL into 0xff and NUL into 0x00.  */
+	orc.b	data, data
+	/* Convert non-NUL into 0x00 and NUL into 0xff.  */
+	not	data, data
+	/* Search for the first set bit (corresponding to a NUL byte in the
+	 * original chunk).  */
+	CZ	data, data
+	/* The first chunk is special: commpare against the number
+	 * of valid bytes in this chunk.  */
+	srli	result, data, 3
+	bgtu	valid_bytes, result, 3f
+
+	/* Prepare for the word comparison loop.  */
+	addi	offset, addr, SZREG
+	li	m1, -1
+
+	/* Our critical loop is 4 instructions and processes data in
+	 * 4 byte or 8 byte chunks.  */
+	.p2align 3
+1:
+	REG_L	data, SZREG(addr)
+	addi	addr, addr, SZREG
+	orc.b	data, data
+	beq	data, m1, 1b
+2:
+	not	data, data
+	CZ	data, data
+	/* Get number of processed words.  */
+	sub	offset, addr, offset
+	/* Add number of characters in the first word.  */
+	add	result, result, offset
+	srli	data, data, 3
+	/* Add number of characters in the last word.  */
+	add	result, result, data
+3:
+	ret
+END(__strlen_zbb)
+EXPORT_SYMBOL(__strlen_zbb)
+
+.option pop
arch/riscv/lib/strncmp_zbb.S new file +106 -0
new file mode 100644
index 000000000000..852c8425d238
@@ -0,0 +1,106 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+#define src1		a0
+#define result		a0
+#define src2		t6
+#define len		a2
+#define data1		t0
+#define data2		t1
+#define align		t2
+#define data1_orcb	t3
+#define limit		t4
+#define m1		t5
+
+.option push
+.option arch,+zbb
+
+/* int __strncmp_zbb(const char *cs, const char *ct, size_t count) */
+ENTRY(__strncmp_zbb)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3, t4, t5, t6
+	 */
+	mv	src2, a1
+
+	or	align, src1, src2
+	li	m1, -1
+	and	align, align, SZREG-1
+	add	limit, src1, len
+	bnez	align, 4f
+
+	/* Adjust limit for fast-path.  */
+	addi	limit, limit, -SZREG
+	/* Main loop for aligned string.  */
+	.p2align 3
+1:
+	bgt	src1, limit, 3f
+	REG_L	data1, 0(src1)
+	REG_L	data2, 0(src2)
+	orc.b	data1_orcb, data1
+	bne	data1_orcb, m1, 2f
+	addi	src1, src1, SZREG
+	addi	src2, src2, SZREG
+	beq	data1, data2, 1b
+
+	/* Words don't match, and no null byte in the first
+	 * word. Get bytes in big-endian order and compare.  */
+#ifndef CONFIG_CPU_BIG_ENDIAN
+	rev8	data1, data1
+	rev8	data2, data2
+#endif
+	/* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence.  */
+	sltu	result, data1, data2
+	neg	result, result
+	ori	result, result, 1
+	ret
+
+2:
+	/* Found a null byte.
+	 * If words don't match, fall back to simple loop.  */
+	bne	data1, data2, 3f
+
+	/* Otherwise, strings are equal.  */
+	li	result, 0
+	ret
+
+	/* Simple loop for misaligned strings.  */
+3:
+	/* Restore limit for slow-path.  */
+	addi	limit, limit, SZREG
+	.p2align 3
+4:
+	bge	src1, limit, 6f
+	lbu	data1, 0(src1)
+	lbu	data2, 0(src2)
+	addi	src1, src1, 1
+	addi	src2, src2, 1
+	bne	data1, data2, 5f
+	bnez	data1, 4b
+
+5:
+	sub	result, data1, data2
+	ret
+
+6:
+	li	result, 0
+	ret
+END(__strncmp_zbb)
+EXPORT_SYMBOL(__strncmp_zbb)
+
+.option pop
Signature · 7 lines
2.35.1


_______________________________________________
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

#38 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Hi Conor,

Am Montag, 14. November 2022, 00:29:35 CET schrieb Conor Dooley:

Quoted 1 line

I always seem to forget to open my mails.. I swear I'm not being rude!

no worries :-) .

I also seem to miss one or another channel of communication way more often than I'd like.

Quoted 5 lines

In the back of my head while looking at this series, I've been wondering about Palmer's hwcap stuff. Wonder what the craic is there - I assume he's just been too busy with the profile stuff. Ditto Ztso, but iirc that's related. Anyway, that's just an aside as I was reading through the patch & typed it somewhere lest I forget to bring it up elsewhere.

That is essentially the next step for me.

The far away goal is doing the stuff we talked about at LPC - i.e. allowing multiple variants for this and then selecting the hopefully fastest one to patch in :-) .

Though the whole thing has way to many moving pieces for my taste, so I'm trying to solve this step by step.

This series "simply" provides an optimization for cores supporting the zbb extension. It works standalone and "solves" my sub-problems of how to patch these core functions and also allowing function calls from alternatives :-) .
[and by that may also help the people working on those more
involved dma-operation variants, that aren't zicbom :-) ]

Next up is the mem* were I essentially have variants for "fast unaligned" access systems - which then will use Palmer's hwcap work.
His series describes the necessary dt-property to declare if a core can support said fast unaligned access.

Heiko

Quoted 465 lines · Heiko Stuebner
Quoted 27 lines

From: Heiko Stuebner <[email protected]>

Add handling for ZBB extension and add support for using it as a variant for optimized string functions.

---
 arch/riscv/Kconfig                   |  23 ++++++
 arch/riscv/include/asm/errata_list.h |   3 +-
 arch/riscv/include/asm/hwcap.h       |   1 +
 arch/riscv/include/asm/string.h      |  29 ++++++--
 arch/riscv/kernel/cpu.c              |   1 +
 arch/riscv/kernel/cpufeature.c       |  18 +++++
 arch/riscv/lib/Makefile              |   3 +
 arch/riscv/lib/strcmp_zbb.S          |  91 +++++++++++++++++++++++
 arch/riscv/lib/strlen_zbb.S          |  98 +++++++++++++++++++++++++
 arch/riscv/lib/strncmp_zbb.S         | 106 +++++++++++++++++++++++++++
 10 files changed, 366 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/lib/strcmp_zbb.S
 create mode 100644 arch/riscv/lib/strlen_zbb.S
 create mode 100644 arch/riscv/lib/strncmp_zbb.S
arch/riscv/Kconfig +0 -0
index acfc4d298aab..56633931e808 100644

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 419

Quoted 9 lines
arch/riscv/Kconfig +6 -0
@@ -411,6 +411,29 @@ config RISCV_ISA_SVPBMT
 
 	   If you don't know what to do here, say Y.
 
+config TOOLCHAIN_HAS_ZBB
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zbb)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbb)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900

Drew wants to switch away from this method and use insn defs instead - and I must admit it does look cleaner! Check out his Zicboz series and see what you think of it.

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 422

Quoted 2 lines
arch/riscv/Kconfig +2 -0
+config RISCV_ISA_ZBB
+	bool "Zbb extension support for "

Missing some words in this line?

Quoted 36 lines
arch/riscv/Kconfig +14 -0
+	depends on TOOLCHAIN_HAS_ZBB
+	depends on !XIP_KERNEL && MMU
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZBB
+	   extension (basic bit manipulation) and enable its usage.
+
+	   The Zbb extension provides instructions to accelerate a number
+	   of bit-specific operations (count bit population, sign extending,
+	   bitrotation, etc).
+
+	   If you don't know what to do here, say Y.
+
 config TOOLCHAIN_HAS_ZICBOM
 	bool
 	default y
arch/riscv/include/asm/errata_list.h +2 -1
index 4180312d2a70..95e626b7281e 100644
@@ -24,7 +24,8 @@
 
 #define	CPUFEATURE_SVPBMT 0
 #define	CPUFEATURE_ZICBOM 1
-#define	CPUFEATURE_NUMBER 2
+#define	CPUFEATURE_ZBB 2
+#define	CPUFEATURE_NUMBER 3
 
 #ifdef __ASSEMBLY__
 
arch/riscv/include/asm/hwcap.h +0 -0
index b22525290073..ac5555fd9788 100644

arch/riscv/include/asm/hwcap.h enum riscv_isa_ext_id { · line 62

Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1 -0
@@ -59,6 +59,7 @@ enum riscv_isa_ext_id {
 	RISCV_ISA_EXT_ZIHINTPAUSE,
 	RISCV_ISA_EXT_SSTC,
 	RISCV_ISA_EXT_SVINVAL,
+	RISCV_ISA_EXT_ZBB,

With ZIHINTPAUSE before SSTC and SVINIVAL I assume this is not something we are canonically ordering but I never, ever know which ones we are allowed to re-order at will.

Quoted 8 lines
arch/riscv/include/asm/hwcap.h +0 -0
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 7 lines
arch/riscv/kernel/cpu.c +1 -0
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 207

Quoted 12 lines
arch/riscv/kernel/cpufeature.c +1 -0
index 026512ca9c4c..f19b9d4e2dca 100644
@@ -201,6 +201,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
 				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 				SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
 				SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);

This one looks like it is, sstc aside. Same question as above, can I reorder this one? I'll send a patch for it if I can...

arch/riscv/lib/strcmp_zbb.S line 4

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +4 -0
new file mode 100644
index 000000000000..aff9b941d3ee
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>

Is a Co-developed-by: appropriate then?

Quoted 18 lines
arch/riscv/lib/strcmp_zbb.S new file +18 -0
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+#define src1		a0
+#define result		a0
+#define src2		t5
+#define data1		t0
+#define data2		t1
+#define align		t2
+#define data1_orcb	t3
+#define m1		t4
+
+.option push
+.option arch,+zbb
+

arch/riscv/lib/strcmp_zbb.S line 32

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +10 -0
+/* int __strcmp_zbb(const char *cs, const char *ct) */
+ENTRY(__strcmp_zbb)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare

Same copy paste mistake here with a2?

arch/riscv/lib/strcmp_zbb.S line 42

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +10 -0
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3, t4, t5
+	 */
+	mv	src2, a1
+
+	or	align, src1, src2
+	li	m1, -1
+	and	align, align, SZREG-1
+	bnez	align, 3f

Line of whitespace here would be nice.

Quoted 4 lines
arch/riscv/lib/strcmp_zbb.S new file +4 -0
+	/* Main loop for aligned string.  */
+	.p2align 3
+1:
+	REG_L	data1, 0(src1)

arch/riscv/lib/strcmp_zbb.S line 56

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +10 -0
+	REG_L	data2, 0(src2)
+	orc.b	data1_orcb, data1
+	bne	data1_orcb, m1, 2f
+	addi	src1, src1, SZREG
+	addi	src2, src2, SZREG
+	beq	data1, data2, 1b
+
+	/* Words don't match, and no null byte in the first
+	 * word. Get bytes in big-endian order and compare.  */
+#ifndef CONFIG_CPU_BIG_ENDIAN

I know this is a lift from the reference implementation in the spec, but do we actually need this ifndef section?

arch/riscv/lib/strcmp_zbb.S line 68

Quoted 12 lines
arch/riscv/lib/strcmp_zbb.S new file +12 -0
+	rev8	data1, data1
+	rev8	data2, data2
+#endif
+	/* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence.  */
+	sltu	result, data1, data2
+	neg	result, result
+	ori	result, result, 1
+	ret
+
+2:
+	/* Found a null byte.
+	 * If words don't match, fall back to simple loop.  */

Can the multiline comments match the usual multiline comment style that you used as the start of the function?

Quoted 73 lines
arch/riscv/lib/strcmp_zbb.S new file +23 -0
+	bne	data1, data2, 3f
+
+	/* Otherwise, strings are equal.  */
+	li	result, 0
+	ret
+
+	/* Simple loop for misaligned strings.  */
+	.p2align 3
+3:
+	lbu	data1, 0(src1)
+	lbu	data2, 0(src2)
+	addi	src1, src1, 1
+	addi	src2, src2, 1
+	bne	data1, data2, 4f
+	bnez	data1, 3b
+
+4:
+	sub	result, data1, data2
+	ret
+END(__strcmp_zbb)
+EXPORT_SYMBOL(__strcmp_zbb)
+
+.option pop
arch/riscv/lib/strlen_zbb.S new file +44 -0
new file mode 100644
index 000000000000..bc8d3607a32f
@@ -0,0 +1,98 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+#define src		a0
+#define result		a0
+#define addr		t0
+#define data		t1
+#define offset		t2
+#define offset_bits	t2
+#define valid_bytes	t3
+#define m1		t3
+
+#ifdef CONFIG_CPU_BIG_ENDIAN
+# define CZ	clz
+# define SHIFT	sll
+#else
+# define CZ	ctz
+# define SHIFT	srl
+#endif
+
+.option push
+.option arch,+zbb
+
+/* int __strlen_zbb(const char *s) */
+ENTRY(__strlen_zbb)
+	/*
+	 * Returns
+	 *   a0 - string length
+	 *
+	 * Parameters
+	 *   a0 - String to measure
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3
+	 */
+
+	/* Number of irrelevant bytes in the first word.  */

arch/riscv/lib/strlen_zbb.S line 54

Quoted 10 lines
arch/riscv/lib/strlen_zbb.S new file +10 -0
+	andi	offset, src, SZREG-1
+	/* Align pointer.  */
+	andi	addr, src, -SZREG
+
+	li	valid_bytes, SZREG
+	sub	valid_bytes, valid_bytes, offset
+	slli	offset_bits, offset, RISCV_LGPTR
+
+	/* Get the first word.  */
+	REG_L	data, 0(addr)

A line of whitespace prior to the comments would go a long way here with making this a little more readable - especially as a diff in a mailclient.

I am oh-so-far from an expert on this kind of stuff, but these three functions seem to match up with the reference implementations in the spec. With the couple different nit pick bits fixed, feel free to tack on:

Anyways, hopefully I've not missed a bunch of should-be-obvious things while trying to review the series..

Thanks,
Conor.

Quoted 148 lines
arch/riscv/lib/strlen_zbb.S new file +44 -0
+	/* Shift away the partial data we loaded to remove the irrelevant bytes
+	 * preceding the string with the effect of adding NUL bytes at the
+	 * end of the string.  */
+	SHIFT	data, data, offset_bits
+	/* Convert non-NUL into 0xff and NUL into 0x00.  */
+	orc.b	data, data
+	/* Convert non-NUL into 0x00 and NUL into 0xff.  */
+	not	data, data
+	/* Search for the first set bit (corresponding to a NUL byte in the
+	 * original chunk).  */
+	CZ	data, data
+	/* The first chunk is special: commpare against the number
+	 * of valid bytes in this chunk.  */
+	srli	result, data, 3
+	bgtu	valid_bytes, result, 3f
+
+	/* Prepare for the word comparison loop.  */
+	addi	offset, addr, SZREG
+	li	m1, -1
+
+	/* Our critical loop is 4 instructions and processes data in
+	 * 4 byte or 8 byte chunks.  */
+	.p2align 3
+1:
+	REG_L	data, SZREG(addr)
+	addi	addr, addr, SZREG
+	orc.b	data, data
+	beq	data, m1, 1b
+2:
+	not	data, data
+	CZ	data, data
+	/* Get number of processed words.  */
+	sub	offset, addr, offset
+	/* Add number of characters in the first word.  */
+	add	result, result, offset
+	srli	data, data, 3
+	/* Add number of characters in the last word.  */
+	add	result, result, data
+3:
+	ret
+END(__strlen_zbb)
+EXPORT_SYMBOL(__strlen_zbb)
+
+.option pop
arch/riscv/lib/strncmp_zbb.S new file +98 -0
new file mode 100644
index 000000000000..852c8425d238
@@ -0,0 +1,106 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm-generic/export.h>
+
+#define src1		a0
+#define result		a0
+#define src2		t6
+#define len		a2
+#define data1		t0
+#define data2		t1
+#define align		t2
+#define data1_orcb	t3
+#define limit		t4
+#define m1		t5
+
+.option push
+.option arch,+zbb
+
+/* int __strncmp_zbb(const char *cs, const char *ct, size_t count) */
+ENTRY(__strncmp_zbb)
+	/*
+	 * Returns
+	 *   a0 - comparison result, like strncmp
+	 *
+	 * Parameters
+	 *   a0 - string1
+	 *   a1 - string2
+	 *   a2 - number of characters to compare
+	 *
+	 * Clobbers
+	 *   t0, t1, t2, t3, t4, t5, t6
+	 */
+	mv	src2, a1
+
+	or	align, src1, src2
+	li	m1, -1
+	and	align, align, SZREG-1
+	add	limit, src1, len
+	bnez	align, 4f
+
+	/* Adjust limit for fast-path.  */
+	addi	limit, limit, -SZREG
+	/* Main loop for aligned string.  */
+	.p2align 3
+1:
+	bgt	src1, limit, 3f
+	REG_L	data1, 0(src1)
+	REG_L	data2, 0(src2)
+	orc.b	data1_orcb, data1
+	bne	data1_orcb, m1, 2f
+	addi	src1, src1, SZREG
+	addi	src2, src2, SZREG
+	beq	data1, data2, 1b
+
+	/* Words don't match, and no null byte in the first
+	 * word. Get bytes in big-endian order and compare.  */
+#ifndef CONFIG_CPU_BIG_ENDIAN
+	rev8	data1, data1
+	rev8	data2, data2
+#endif
+	/* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence.  */
+	sltu	result, data1, data2
+	neg	result, result
+	ori	result, result, 1
+	ret
+
+2:
+	/* Found a null byte.
+	 * If words don't match, fall back to simple loop.  */
+	bne	data1, data2, 3f
+
+	/* Otherwise, strings are equal.  */
+	li	result, 0
+	ret
+
+	/* Simple loop for misaligned strings.  */
+3:
+	/* Restore limit for slow-path.  */
+	addi	limit, limit, SZREG
+	.p2align 3
+4:
+	bge	src1, limit, 6f
+	lbu	data1, 0(src1)
+	lbu	data2, 0(src2)
+	addi	src1, src1, 1
+	addi	src2, src2, 1
+	bne	data1, data2, 5f
+	bnez	data1, 4b
+
+5:
+	sub	result, data1, data2
+	ret
Quoted 9 lines

arch/riscv/lib/strncmp_zbb.S line 106

Quoted 8 lines
arch/riscv/lib/strncmp_zbb.S new file +8 -0
+
+6:
+	li	result, 0
+	ret
+END(__strncmp_zbb)
+EXPORT_SYMBOL(__strncmp_zbb)
+
+.option pop

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

#39 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

In reply to Conor Dooley
Quoted 5 lines
Quoted 5 lines
arch/riscv/include/asm/hwcap.h +0 -0
index b22525290073..ac5555fd9788 100644
@@ -59,6 +59,7 @@ enum riscv_isa_ext_id {
Quoted 8 lines

arch/riscv/include/asm/hwcap.h enum riscv_isa_ext_id { · line 62

Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1 -0
 	RISCV_ISA_EXT_ZIHINTPAUSE,
 	RISCV_ISA_EXT_SSTC,
 	RISCV_ISA_EXT_SVINVAL,
+	RISCV_ISA_EXT_ZBB,

With ZIHINTPAUSE before SSTC and SVINIVAL I assume this is not something we are canonically ordering but I never, ever know which ones we are allowed to re-order at will.

I guess we could extend the comments with suitable hints, which could help in the future.

Quoted 19 lines
Quoted 9 lines
arch/riscv/include/asm/hwcap.h +0 -0
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 7 lines
arch/riscv/kernel/cpu.c +1 -0
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

Quoted 15 lines

arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 207

Quoted 12 lines
arch/riscv/kernel/cpufeature.c +1 -0
index 026512ca9c4c..f19b9d4e2dca 100644
@@ -201,6 +201,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
 				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 				SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
 				SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);

This one looks like it is, sstc aside. Same question as above, can I reorder this one? I'll send a patch for it if I can...

hmm, I don't see the difference between cpu.c above (..., svpbmt, zbb, zicbom, ...) and here (..., svpbmt, zbb, zicbom, ...)

Quoted 4 lines
Quoted 4 lines
arch/riscv/lib/strcmp_zbb.S new file +0 -1
new file mode 100644
index 000000000000..aff9b941d3ee
--- /dev/null
Quoted 8 lines

arch/riscv/lib/strcmp_zbb.S line 4

Quoted 6 lines

+++ b/arch/riscv/lib/strcmp_zbb.S

arch/riscv/lib/strcmp_zbb.S new file +4 -0
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>

Is a Co-developed-by: appropriate then?

I'd think so ... i.e. the assembly is from Christoph, but is originally part of a pending glibc patchset, hence Christoph and me decided on the co-developed thingy :-) .

Quoted 9 lines
Quoted 9 lines
arch/riscv/lib/strcmp_zbb.S new file +9 -0
+	/* Main loop for aligned string.  */
+	.p2align 3
+1:
+	REG_L	data1, 0(src1)
+	REG_L	data2, 0(src2)
+	orc.b	data1_orcb, data1
+	bne	data1_orcb, m1, 2f
+	addi	src1, src1, SZREG
+	addi	src2, src2, SZREG
Quoted 8 lines

arch/riscv/lib/strcmp_zbb.S line 56

Quoted 5 lines
arch/riscv/lib/strcmp_zbb.S new file +5 -0
+	beq	data1, data2, 1b
+
+	/* Words don't match, and no null byte in the first
+	 * word. Get bytes in big-endian order and compare.  */
+#ifndef CONFIG_CPU_BIG_ENDIAN

I know this is a lift from the reference implementation in the spec, but do we actually need this ifndef section?

I don't know, but _if_ big endian support comes in the future, having one place less to break also might be helpful? :-)

Heiko

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

#40 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 14 lines · Heiko Stübner
Quoted 5 lines
Quoted 5 lines
arch/riscv/include/asm/hwcap.h +0 -0
index b22525290073..ac5555fd9788 100644
@@ -59,6 +59,7 @@ enum riscv_isa_ext_id {
Quoted 8 lines

arch/riscv/include/asm/hwcap.h enum riscv_isa_ext_id { · line 62

Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1 -0
 	RISCV_ISA_EXT_ZIHINTPAUSE,
 	RISCV_ISA_EXT_SSTC,
 	RISCV_ISA_EXT_SVINVAL,
+	RISCV_ISA_EXT_ZBB,

With ZIHINTPAUSE before SSTC and SVINIVAL I assume this is not something we are canonically ordering but I never, ever know which ones we are allowed to re-order at will.

Quoted 2 lines

I guess we could extend the comments with suitable hints, which could help in the future.

Aye, for the likes of me that will never, ever remember I like the idea!

Quoted 17 lines
Quoted 17 lines
Quoted 8 lines
arch/riscv/include/asm/hwcap.h +0 -0
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 7 lines
arch/riscv/kernel/cpu.c +1 -0
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
arch/riscv/kernel/cpufeature.c +0 -0
index 026512ca9c4c..f19b9d4e2dca 100644
@@ -201,6 +201,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
Quoted 8 lines

arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 207

Quoted 5 lines
arch/riscv/kernel/cpufeature.c +1 -0
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
 				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 				SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
 				SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);

This one looks like it is, sstc aside. Same question as above, can I reorder this one? I'll send a patch for it if I can...

Quoted 3 lines

hmm, I don't see the difference between cpu.c above (..., svpbmt, zbb, zicbom, ...) and here (..., svpbmt, zbb, zicbom, ...)

sstc appears last here but first in the cpu.c hunk above.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/riscv/lib/strcmp_zbb.S new file +0 -1
new file mode 100644
index 000000000000..aff9b941d3ee
--- /dev/null
Quoted 8 lines

arch/riscv/lib/strcmp_zbb.S line 4

Quoted 6 lines

+++ b/arch/riscv/lib/strcmp_zbb.S

arch/riscv/lib/strcmp_zbb.S new file +4 -0
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>

Is a Co-developed-by: appropriate then?

Quoted 3 lines

I'd think so ... i.e. the assembly is from Christoph, but is originally part of a pending glibc patchset, hence Christoph and me decided on the co-developed thingy :-) .

Check your patch again, I don't see a Co-developed-by: tag. (That's what I was getting at, not the validity of "Author: Christoph...")

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
arch/riscv/lib/strcmp_zbb.S new file +9 -0
+	/* Main loop for aligned string.  */
+	.p2align 3
+1:
+	REG_L	data1, 0(src1)
+	REG_L	data2, 0(src2)
+	orc.b	data1_orcb, data1
+	bne	data1_orcb, m1, 2f
+	addi	src1, src1, SZREG
+	addi	src2, src2, SZREG
Quoted 8 lines

arch/riscv/lib/strcmp_zbb.S line 56

Quoted 5 lines
arch/riscv/lib/strcmp_zbb.S new file +5 -0
+	beq	data1, data2, 1b
+
+	/* Words don't match, and no null byte in the first
+	 * word. Get bytes in big-endian order and compare.  */
+#ifndef CONFIG_CPU_BIG_ENDIAN

I know this is a lift from the reference implementation in the spec, but do we actually need this ifndef section?

Quoted 2 lines

I don't know, but _if_ big endian support comes in the future, having one place less to break also might be helpful? :-)

One less place to have to go and fix it up, but I hope it never comes to pass! And no harm is being close to the one in the spec...

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

#41 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Am Donnerstag, 24. November 2022, 23:32:58 CET schrieb Conor Dooley:

Quoted 15 lines · Heiko Stübner
Quoted 14 lines
Quoted 13 lines

arch/riscv/include/asm/hwcap.h enum riscv_isa_ext_id { · line 62

Quoted 9 lines
arch/riscv/include/asm/hwcap.h +1 -0
index b22525290073..ac5555fd9788 100644
@@ -59,6 +59,7 @@ enum riscv_isa_ext_id {
 	RISCV_ISA_EXT_ZIHINTPAUSE,
 	RISCV_ISA_EXT_SSTC,
 	RISCV_ISA_EXT_SVINVAL,
+	RISCV_ISA_EXT_ZBB,

With ZIHINTPAUSE before SSTC and SVINIVAL I assume this is not something we are canonically ordering but I never, ever know which ones we are allowed to re-order at will.

Quoted 4 lines
Quoted 2 lines

I guess we could extend the comments with suitable hints, which could help in the future.

Aye, for the likes of me that will never, ever remember I like the idea!

I'm 100% with you on this. I remember that this came up either with svpbmt or zicbom in the past, but I still again forgot how the ordering goes.

Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
arch/riscv/include/asm/hwcap.h +0 -0
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
Quoted 8 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 6 lines
arch/riscv/kernel/cpu.c +1 -0
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

Quoted 5 lines

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

This confuses me completely now :-) .

The list is still too short to see where other extensions are placed. I guess I need to find that stuff again about extension ordering.

Quoted 35 lines
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
arch/riscv/kernel/cpufeature.c +0 -0
index 026512ca9c4c..f19b9d4e2dca 100644
@@ -201,6 +201,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
Quoted 8 lines

arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 207

Quoted 5 lines
arch/riscv/kernel/cpufeature.c +1 -0
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
 				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 				SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
 				SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);

This one looks like it is, sstc aside. Same question as above, can I reorder this one? I'll send a patch for it if I can...

Quoted 3 lines

hmm, I don't see the difference between cpu.c above (..., svpbmt, zbb, zicbom, ...) and here (..., svpbmt, zbb, zicbom, ...)

sstc appears last here but first in the cpu.c hunk above.

Quoted 13 lines
Quoted 12 lines

arch/riscv/lib/strcmp_zbb.S line 4

Quoted 10 lines
arch/riscv/lib/strcmp_zbb.S new file +4 -0
new file mode 100644
index 000000000000..aff9b941d3ee
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 VRULL GmbH
+ * Author: Christoph Muellner <[email protected]>

Is a Co-developed-by: appropriate then?

Quoted 6 lines
Quoted 3 lines

I'd think so ... i.e. the assembly is from Christoph, but is originally part of a pending glibc patchset, hence Christoph and me decided on the co-developed thingy :-) .

Check your patch again, I don't see a Co-developed-by: tag. (That's what I was getting at, not the validity of "Author: Christoph...")

Now I remember, I talked with Christoph about that _after_ sending this series. So my "git log" did show the Co-developed-by all the time, which then confused me :-)

Heiko

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

#42 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 2 lines · Heiko Stuebner

Am Donnerstag, 24. November 2022, 23:32:58 CET schrieb Conor Dooley:

Quoted 1 line · Heiko Stübner

...

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 12 lines
arch/riscv/kernel/cpu.c +1 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

Quoted 8 lines
Quoted 5 lines

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

This confuses me completely now :-) .

Can we instead post a patch to the spec that changes the order to alphabetical? The only other option I see is to develop a tool which sorts extensions and every RISC-V developer keeps it in their back pocket. A kernel specific tool to check each list we want to keep sorted would be nice too.

My preference would be to change the spec to alphabetical order, though, because the spec isn't explicit[*] enough to write a tool that can handle all cases. We'll end up needing to have conversations like this one to write the tool and eventually the tool will be what everyone looks to, rather than the spec...

[*] The spec uses words like 'can', 'should', and 'conventional'.

Thanks,
drew

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

#43 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 27 lines · Andrew Jones

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 2 lines · Heiko Stuebner

Am Donnerstag, 24. November 2022, 23:32:58 CET schrieb Conor Dooley:

Quoted 1 line · Heiko Stübner

...

Quoted 21 lines
Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
       __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
Quoted 8 lines
Quoted 8 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data isa_ext_arr[] = { · line 172

Quoted 6 lines
arch/riscv/kernel/cpu.c +1 -0
       __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
       __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+     __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
       __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
       __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
       __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

Quoted 8 lines
Quoted 2 lines

This confuses me completely now :-) .

Can we instead post a patch to the spec that changes the order to alphabetical? The only other option I see is to develop a tool which sorts extensions and every RISC-V developer keeps it in their back pocket. A kernel specific tool to check each list we want to keep sorted would be nice too.

Is there some reason that these things need to be output in canonical order in the first place by the kernel?
Could we say to hell with even trying to figure out what the correct order is (since yeah, it'll be a conversation ~every time this comes up) or is that breaking uAPI since someone's parser in userland may expect to see canonical order only?
That's been my assumption for why it was re-sorted, @Palmer?

Quoted 5 lines

My preference would be to change the spec to alphabetical order, though, because the spec isn't explicit[*] enough to write a tool that can handle all cases. We'll end up needing to have conversations like this one to write the tool and eventually the tool will be what everyone looks to, rather than the spec...

If it could be explicitly clear what constitutes an "additional standard extension" that'd be good enough for me! Say:

src/naming.tex +3 -1
index bfd67d4..9d63a86 100644
@@ -80,7 +80,9 @@ Standard extensions can also be named using a single ``Z'' followed by an
  alphabetical name and an optional version number.  For example,
  ``Zifencei'' names the instruction-fetch fence extension described in
  Chapter~\ref{chap:zifencei}; ``Zifencei2'' and ``Zifencei2p0'' name version
-2.0 of same.
+2.0 of same. The entire set of Additional Standard Extensions are documented
+in the Standard Unprivileged Extensions section of Table~\ref{isanametable}
+below.
  
  The first letter following the ``Z'' conventionally indicates the most closely
  related alphabetical extension category, IMAFDQCVH.  For the ``Zam''

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

A message that is not in the archive (sent off-list or not yet received): CAEg0e7h9skbWPVDsz9CdB8dATN5XM9eT-uPY0A7xRZmX=qTU6A@mail.gmail.com

#45 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 45 lines · Christoph Müllner
Quoted 42 lines · Andrew Jones
Quoted 2 lines · Heiko Stuebner

Am Donnerstag, 24. November 2022, 23:32:58 CET schrieb Conor Dooley:

Quoted 1 line · Heiko Stübner

...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data

Quoted 5 lines
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data

isa_ext_arr[] = {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data · line 171

Quoted 6 lines
arch/riscv/kernel/cpu.c +1 -0
      __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
      __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
      __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+     __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
      __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
      __RISCV_ISA_EXT_DATA(zihintpause,

RISCV_ISA_EXT_ZIHINTPAUSE),

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data · line 172

Quoted 1 line
arch/riscv/kernel/cpu.c +0 -0
      __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

Quoted 8 lines
Quoted 5 lines

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

This confuses me completely now :-) .

Can we instead post a patch to the spec that changes the order to alphabetical? The only other option I see is to develop a tool which sorts extensions and every RISC-V developer keeps it in their back pocket. A kernel specific tool to check each list we want to keep sorted would be nice too.

My preference would be to change the spec to alphabetical order, though, because the spec isn't explicit[*] enough to write a tool that can handle all cases. We'll end up needing to have conversations like this one to write the tool and eventually the tool will be what everyone looks to, rather than the spec...

Quoted 5 lines
Quoted 2 lines

[*] The spec uses words like 'can', 'should', and 'conventional'.

The unpriv spec is clear about the canonical order (table "Standard ISA extension names"):

The caption under table 27.1 does indeed declare the table defines the canonical order and that it *must* be used for the name string, but almost everywhere else in chapter 27 the word "should" is used to suggest how extensions be ordered (only X-extensions say where they must be).

Quoted 2 lines

1) Base ISA
2) Standard Unpriv Extension (non alphabetical)

The 'non alphabetical' part makes this a PITA.

And section 27.6 says the first letter "conventionally indicates...". I suppose we can assume it "must indicate"?

Quoted 1 line

3) Standard Supervisor-Level Extensions

Are the conventions for the first character of S-extensions defined? I've seen 'Ss' for "Privileged arch and Supervisor-level extensions", e.g. Sscofpmf. 'Sv' for virtual memory (I guess) related extensions, e.g. Svinval, and we appear to be using alphabetical order for them.

Quoted 2 lines

4) Standard Machine-Level Extensions
5) Non-Standard (Vendor) Extensions

Anyway, for the relatively easier problem of this kernel list and this patch, we could do something with defines like below in order to try and keep the order right.

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
};

Thanks,
drew

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

#46 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 58 lines · Andrew Jones
Quoted 45 lines · Christoph Müllner
Quoted 42 lines · Andrew Jones
Quoted 2 lines · Heiko Stuebner

Am Donnerstag, 24. November 2022, 23:32:58 CET schrieb Conor Dooley:

Quoted 1 line · Heiko Stübner

...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data

Quoted 5 lines
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data

isa_ext_arr[] = {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data · line 171

Quoted 6 lines
arch/riscv/kernel/cpu.c +1 -0
      __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
      __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
      __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+     __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
      __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
      __RISCV_ISA_EXT_DATA(zihintpause,

RISCV_ISA_EXT_ZIHINTPAUSE),

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data · line 172

Quoted 1 line
arch/riscv/kernel/cpu.c +0 -0
      __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

Quoted 8 lines
Quoted 5 lines

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

This confuses me completely now :-) .

Can we instead post a patch to the spec that changes the order to alphabetical? The only other option I see is to develop a tool which sorts extensions and every RISC-V developer keeps it in their back pocket. A kernel specific tool to check each list we want to keep sorted would be nice too.

My preference would be to change the spec to alphabetical order, though, because the spec isn't explicit[*] enough to write a tool that can handle all cases. We'll end up needing to have conversations like this one to write the tool and eventually the tool will be what everyone looks to, rather than the spec...

Quoted 5 lines
Quoted 2 lines

[*] The spec uses words like 'can', 'should', and 'conventional'.

The unpriv spec is clear about the canonical order (table "Standard ISA extension names"):

The caption under table 27.1 does indeed declare the table defines the canonical order and that it *must* be used for the name string, but almost everywhere else in chapter 27 the word "should" is used to suggest how extensions be ordered (only X-extensions say where they must be).

Quoted 7 lines
Quoted 2 lines

1) Base ISA
2) Standard Unpriv Extension (non alphabetical)

The 'non alphabetical' part makes this a PITA.

And section 27.6 says the first letter "conventionally indicates...". I suppose we can assume it "must indicate"?

I think it is what it is and changing it would just open pointless discussions. Further, I think that consistency is more important than trying to establish a new second ordering scheme (regardless if that is simpler and/or better) unless we have to overcome technical issues that would otherwise not be possible.

Quoted 31 lines
Quoted 2 lines

3) Standard Supervisor-Level Extensions

Are the conventions for the first character of S-extensions defined? I've seen 'Ss' for "Privileged arch and Supervisor-level extensions", e.g. Sscofpmf. 'Sv' for virtual memory (I guess) related extensions, e.g. Svinval, and we appear to be using alphabetical order for them.

Quoted 2 lines

4) Standard Machine-Level Extensions
5) Non-Standard (Vendor) Extensions

Anyway, for the relatively easier problem of this kernel list and this patch, we could do something with defines like below in order to try and keep the order right.

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
Quoted 6 lines
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
};

It might be worth to look how Binutils are grouping them:
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfxx-riscv.c#l1261

They also group extensions together but use the following groups:

const struct riscv_supported_ext *riscv_all_supported_ext[] = {
riscv_supported_std_ext, riscv_supported_std_z_ext, riscv_supported_std_s_ext, riscv_supported_std_zxm_ext, riscv_supported_vendor_x_ext,
NULL
};

BR
Christoph

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

#47 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 30 lines · Christoph Müllner
Quoted 16 lines · Andrew Jones

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
Quoted 6 lines
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
};

It might be worth to look how Binutils are grouping them:
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfxx-riscv.c#l1261

They also group extensions together but use the following groups:

Quoted 9 lines

const struct riscv_supported_ext *riscv_all_supported_ext[] = {
riscv_supported_std_ext, riscv_supported_std_z_ext, riscv_supported_std_s_ext, riscv_supported_std_zxm_ext, riscv_supported_vendor_x_ext,
NULL
};

I think in this case, Drew's differentiation between Zi & Zb is an improvement :)

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

#48 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 42 lines · Conor Dooley
Quoted 30 lines · Christoph Müllner
Quoted 16 lines · Andrew Jones

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
Quoted 6 lines
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
};

It might be worth to look how Binutils are grouping them:
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfxx-riscv.c#l1261

They also group extensions together but use the following groups:

Quoted 9 lines

const struct riscv_supported_ext *riscv_all_supported_ext[] = {
riscv_supported_std_ext, riscv_supported_std_z_ext, riscv_supported_std_s_ext, riscv_supported_std_zxm_ext, riscv_supported_vendor_x_ext,
NULL
};

Quoted 2 lines

I think in this case, Drew's differentiation between Zi & Zb is an improvement :)

That might be the case, but it also brings up the following questions:
Where to put Zf* extensions (between Zi and Zb)?
Where to put Zk* and Zv* (after Zb*)?
Adding a subgroup for each Z{N}* extension might result in many small groups with little benefit.

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

#49 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 43 lines · Christoph Müllner
Quoted 42 lines · Conor Dooley
Quoted 39 lines · Christoph Müllner
Quoted 16 lines · Andrew Jones

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
Quoted 6 lines
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
};

It might be worth to look how Binutils are grouping them:
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfxx-riscv.c#l1261

They also group extensions together but use the following groups:

const struct riscv_supported_ext *riscv_all_supported_ext[] = {
riscv_supported_std_ext, riscv_supported_std_z_ext, riscv_supported_std_s_ext, riscv_supported_std_zxm_ext, riscv_supported_vendor_x_ext,
NULL
};

Quoted 6 lines
Quoted 2 lines

I think in this case, Drew's differentiation between Zi & Zb is an improvement :)

That might be the case, but it also brings up the following questions:
Where to put Zf* extensions (between Zi and Zb)?

I don't think that differs either way?

Quoted 1 line

Where to put Zk* and Zv* (after Zb*)?

Ditto?

Quoted 2 lines

Adding a subgroup for each Z{N}* extension might result in many small groups with little benefit.

If it prevents another one of these ordering episodes, I think it's worth doing! If a "group" only has one element it could alternatively be added directly also.

Also neat, you got your mailer sorted :)

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

#50 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

Quoted 56 lines · Conor Dooley
Quoted 43 lines · Christoph Müllner
Quoted 42 lines · Conor Dooley
Quoted 39 lines · Christoph Müllner
Quoted 16 lines · Andrew Jones

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
Quoted 6 lines
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
};

It might be worth to look how Binutils are grouping them:
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfxx-riscv.c#l1261

They also group extensions together but use the following groups:

const struct riscv_supported_ext *riscv_all_supported_ext[] = {
riscv_supported_std_ext, riscv_supported_std_z_ext, riscv_supported_std_s_ext, riscv_supported_std_zxm_ext, riscv_supported_vendor_x_ext,
NULL
};

Quoted 6 lines
Quoted 2 lines

I think in this case, Drew's differentiation between Zi & Zb is an improvement :)

That might be the case, but it also brings up the following questions:
Where to put Zf* extensions (between Zi and Zb)?

I don't think that differs either way?

Quoted 1 line

Where to put Zk* and Zv* (after Zb*)?

Quoted 8 lines

Ditto?

Quoted 2 lines

Adding a subgroup for each Z{N}* extension might result in many small groups with little benefit.

If it prevents another one of these ordering episodes, I think it's worth doing! If a "group" only has one element it could alternatively be added directly also.

Sounds good then.

Quoted 2 lines

Also neat, you got your mailer sorted :)

One just needs to remember to check the "Plain text mode" box.

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

#51 lore raw

Re: [PATCH 7/7] RISC-V: add zbb support to string functions

In reply to Andrew Jones

Hey Drew, Christoph,

@Christoph, I did not receive your mail unfortunately as it appears to be html. I assume you know why that's a problem and had some mailer issues etc.

also + CC Samuel Ortiz since we discussed this elsewhere...

Quoted 46 lines · Andrew Jones
Quoted 45 lines · Christoph Müllner
Quoted 42 lines · Andrew Jones
Quoted 2 lines · Heiko Stuebner

Am Donnerstag, 24. November 2022, 23:32:58 CET schrieb Conor Dooley:

Quoted 1 line · Heiko Stübner

...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data

Quoted 5 lines
arch/riscv/kernel/cpu.c +0 -0
index bf9dd6764bad..66ff36a57e20 100644
@@ -166,6 +166,7 @@ static struct riscv_isa_ext_data

isa_ext_arr[] = {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data · line 171

Quoted 6 lines
arch/riscv/kernel/cpu.c +1 -0
      __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
      __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
      __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+     __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
      __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
      __RISCV_ISA_EXT_DATA(zihintpause,

RISCV_ISA_EXT_ZIHINTPAUSE),

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

arch/riscv/kernel/cpu.c static struct riscv_isa_ext_data · line 172

Quoted 1 line
arch/riscv/kernel/cpu.c +0 -0
      __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),

This one I do know that Palmer wants canonically ordered.

Quoted 8 lines
Quoted 5 lines

btw, idk if you noticed but I appear to have picked canonical ordering as today's thing to get confused about a lot.

You put zbb after the S extentions - does that meant it is *not* an "Additional Standard Extension" but rather a regular Z one?

This confuses me completely now :-) .

Can we instead post a patch to the spec that changes the order to alphabetical? The only other option I see is to develop a tool which sorts extensions and every RISC-V developer keeps it in their back pocket. A kernel specific tool to check each list we want to keep sorted would be nice too.

My preference would be to change the spec to alphabetical order, though, because the spec isn't explicit[*] enough to write a tool that can handle all cases. We'll end up needing to have conversations like this one to write the tool and eventually the tool will be what everyone looks to, rather than the spec...

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

[*] The spec uses words like 'can', 'should', and 'conventional'.

The unpriv spec is clear about the canonical order (table "Standard ISA extension names"):

So I went reading the isa-manual again for the nth time.. It seems that I missed the sentence:
Standard machine-level instruction-set extensions are prefixed with the three letters ``Zxm''. Woops & apologies @Samuel!

However, that table is only clear (as pointed out by Drew) on a categorical level.

Quoted 5 lines

The caption under table 27.1 does indeed declare the table defines the canonical order and that it *must* be used for the name string, but almost everywhere else in chapter 27 the word "should" is used to suggest how extensions be ordered (only X-extensions say where they must be).

Quoted 7 lines
Quoted 2 lines

1) Base ISA
2) Standard Unpriv Extension (non alphabetical)

The 'non alphabetical' part makes this a PITA.

And section 27.6 says the first letter "conventionally indicates...". I suppose we can assume it "must indicate"?

Convention would imply it *should* but not that it must. I think, for the kernel, we take a stronger view and say that we *will* put them in the listed order in that table.

Quoted 6 lines
Quoted 1 line

3) Standard Supervisor-Level Extensions

Are the conventions for the first character of S-extensions defined? I've seen 'Ss' for "Privileged arch and Supervisor-level extensions", e.g. Sscofpmf. 'Sv' for virtual memory (I guess) related extensions, e.g. Svinval, and we appear to be using alphabetical order for them.

Again, appears to be a "should". I'd vote for doing everyone a favour and making it "must" in this case kernel wise.

Quoted 21 lines
Quoted 3 lines

4) Standard Machine-Level Extensions
5) Non-Standard (Vendor) Extensions

Anyway, for the relatively easier problem of this kernel list and this patch, we could do something with defines like below in order to try and keep the order right.

/*
* Each sub-list is sorted alphabetically.
*/

#define S_EXTENSIONS                                                    \
        __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),         \
        __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),                 \
        __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),           \

__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),

#define Zi_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),             \
        __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
Quoted 8 lines
#define Zb_EXTENSIONS                                                   \
        __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZICBOM),                \
static struct riscv_isa_ext_data isa_ext_arr[] = {
        Zi_EXTENSIONS
        Zb_EXTENSIONS
        S_EXTENSIONS
        __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX)

The above LGTM (apart from the accident in the zbb entry!)

Thanks for all putting up with my confusion here - although the lack of clarity appears to be rather widespread hahaha.

Thanks,
Conor.

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