Skip to contentSkip to conversation

lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SH

PATCH New Nylon Chen 4 participants 5 messages Feb 26, 2024 – Mar 6, 2024 RISC-V

lore
Share & tools
lore

Nylon Chen

author
#0 lore raw

PATCH 1/1 lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SH

From: "Nylon Chen" <[email protected]>

Added exception handling for compressed instructions C.LHU, C.LH, and C.SH from the zcb extension to the sbi_misaligned_ldst library.

---
 include/sbi/riscv_encoding.h  |  7 +++++++
 lib/sbi/sbi_misaligned_ldst.c | 10 ++++++++++
 2 files changed, 17 insertions(+)
include/sbi/riscv_encoding.h +7 -0
index e74cc0d..3d77e26 100644
@@ -837,6 +837,13 @@
 #define INSN_MATCH_C_FSWSP		0xe002
 #define INSN_MASK_C_FSWSP		0xe003
 
+#define INSN_MATCH_C_LHU		0x8400
+#define INSN_MASK_C_LHU		0xfc43
+#define INSN_MATCH_C_LH		0x8440
+#define INSN_MASK_C_LH			0xfc43
+#define INSN_MATCH_C_SH		0x8c00
+#define INSN_MASK_C_SH			0xfc43
+
 #define INSN_MASK_WFI			0xffffff00
 #define INSN_MATCH_WFI			0x10500000
 
lib/sbi/sbi_misaligned_ldst.c +10 -0
index aa512de..9ca225a 100644
@@ -123,6 +123,13 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
 		len = 4;
 #endif
 #endif
+	} else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+		len = 2;
+		insn = RVC_RS2S(insn) << SH_RD;
+	} else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+		len = 2;
+		shift = 8 * (sizeof(ulong) - len);
+		insn = RVC_RS2S(insn) << SH_RD;
 	} else {
 		uptrap.epc = regs->mepc;
 		uptrap.cause = CAUSE_MISALIGNED_LOAD;
@@ -237,6 +244,9 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
 		val.data_ulong = GET_F32_RS2C(insn, regs);
 #endif
 #endif
+	} else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+		len		= 2;
+		val.data_ulong = GET_RS2S(insn, regs);
 	} else {
 		uptrap.epc = regs->mepc;
 		uptrap.cause = CAUSE_MISALIGNED_STORE;
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SH

Quoted 49 lines · On Mon, Feb 26, 2024 at 11:27?AM Nylon Chen <[email protected]> wrote:

From: "Nylon Chen" <[email protected]>

Added exception handling for compressed instructions C.LHU, C.LH, and C.SH from the zcb extension to the sbi_misaligned_ldst library.

---
 include/sbi/riscv_encoding.h  |  7 +++++++
 lib/sbi/sbi_misaligned_ldst.c | 10 ++++++++++
 2 files changed, 17 insertions(+)
include/sbi/riscv_encoding.h +7 -0
index e74cc0d..3d77e26 100644
@@ -837,6 +837,13 @@
 #define INSN_MATCH_C_FSWSP             0xe002
 #define INSN_MASK_C_FSWSP              0xe003
 
+#define INSN_MATCH_C_LHU               0x8400
+#define INSN_MASK_C_LHU                0xfc43
+#define INSN_MATCH_C_LH                0x8440
+#define INSN_MASK_C_LH                 0xfc43
+#define INSN_MATCH_C_SH                0x8c00
+#define INSN_MASK_C_SH                 0xfc43
+
 #define INSN_MASK_WFI                  0xffffff00
 #define INSN_MATCH_WFI                 0x10500000
 
lib/sbi/sbi_misaligned_ldst.c +7 -0
index aa512de..9ca225a 100644
@@ -123,6 +123,13 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
                len = 4;
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+               len = 2;
+               insn = RVC_RS2S(insn) << SH_RD;
+       } else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+               len = 2;
+               shift = 8 * (sizeof(ulong) - len);
+               insn = RVC_RS2S(insn) << SH_RD;
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_LOAD;

lib/sbi/sbi_misaligned_ldst.c int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst, · line 249

Quoted 6 lines
lib/sbi/sbi_misaligned_ldst.c +3 -0
@@ -237,6 +244,9 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
                val.data_ulong = GET_F32_RS2C(insn, regs);
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+               len             = 2;
+               val.data_ulong = GET_RS2S(insn, regs);

Don't need to handle "c.sb" instruction ??

Quoted 11 lines
lib/sbi/sbi_misaligned_ldst.c +0 -1
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_STORE;
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

#2 lore raw

PATCH 1/1 lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SH

Anup Patel <[email protected]> ? 2024?3?4? ?? ??1:12???

Quoted 51 lines · On Mon, Feb 26, 2024 at 11:27?AM Nylon Chen <[email protected]> wrote:
Quoted 49 lines

From: "Nylon Chen" <[email protected]>

Added exception handling for compressed instructions C.LHU, C.LH, and C.SH from the zcb extension to the sbi_misaligned_ldst library.

---
 include/sbi/riscv_encoding.h  |  7 +++++++
 lib/sbi/sbi_misaligned_ldst.c | 10 ++++++++++
 2 files changed, 17 insertions(+)
include/sbi/riscv_encoding.h +7 -0
index e74cc0d..3d77e26 100644
@@ -837,6 +837,13 @@
 #define INSN_MATCH_C_FSWSP             0xe002
 #define INSN_MASK_C_FSWSP              0xe003
 
+#define INSN_MATCH_C_LHU               0x8400
+#define INSN_MASK_C_LHU                0xfc43
+#define INSN_MATCH_C_LH                0x8440
+#define INSN_MASK_C_LH                 0xfc43
+#define INSN_MATCH_C_SH                0x8c00
+#define INSN_MASK_C_SH                 0xfc43
+
 #define INSN_MASK_WFI                  0xffffff00
 #define INSN_MATCH_WFI                 0x10500000
 
lib/sbi/sbi_misaligned_ldst.c +7 -0
index aa512de..9ca225a 100644
@@ -123,6 +123,13 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
                len = 4;
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+               len = 2;
+               insn = RVC_RS2S(insn) << SH_RD;
+       } else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+               len = 2;
+               shift = 8 * (sizeof(ulong) - len);
+               insn = RVC_RS2S(insn) << SH_RD;
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_LOAD;
@@ -237,6 +244,9 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
Quoted 8 lines

lib/sbi/sbi_misaligned_ldst.c int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst, · line 249

Quoted 6 lines
lib/sbi/sbi_misaligned_ldst.c +3 -0
                val.data_ulong = GET_F32_RS2C(insn, regs);
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+               len             = 2;
+               val.data_ulong = GET_RS2S(insn, regs);

Don't need to handle "c.sb" instruction ??

Hi Anup, thanks for your feedback.

in my opinion, because it's a byte so I don't think we'll encounter any misaligned issues.

I also wrote the following test program to specifically test c.sb, and indeed, it doesn't require any exception handler.

/* Check whether there is an address misalignment issue in the store byte instruction of zcb externsion. */
void checkStoreB(unsigned char* memory, size_t offset, unsigned char

value_byte) {
    asm volatile (
        "c.sb %[value_byte], 0(%[addr])"
        :
        : [value_byte] "r" (value_byte), [addr] "r" (memory + offset)
        : "memory"
    );

    printf("Store (c.sb) operation at address: 0x%lx\n", (unsigned

long)(memory + offset));

    if (memcmp(memory + offset, &value_byte, sizeof(unsigned char)) == 0) {
        printf("Store (c.sb) operation is correct: 0x%X\n", value_byte);
    } else {
        printf("Store (c.sb) operation may have address misalignment
issues.\n");
    }
}
int main() {
    size_t size = 10;
    unsigned char* memory = (unsigned char*)malloc(size);

    if (memory == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        return 1;
    }

    size_t offset = 1;

    checkStoreB(memory, offset, 0x42);

...
}

Quoted 15 lines
Quoted 8 lines
lib/sbi/sbi_misaligned_ldst.c +0 -1
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_STORE;
--

2.34.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

Signature · 6 lines
==========================================
sifive system software  team ???

Cell phone:  0989057854
E-mail: nylon7717 at gmail.com
==========================================
#3 lore raw

PATCH 1/1 lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SH

Quoted 61 lines · On Mon, Mar 4, 2024 at 11:50?AM Nylon Chen <[email protected]> wrote:

Anup Patel <[email protected]> ? 2024?3?4? ?? ??1:12???

Quoted 51 lines · On Mon, Feb 26, 2024 at 11:27?AM Nylon Chen <[email protected]> wrote:
Quoted 49 lines

From: "Nylon Chen" <[email protected]>

Added exception handling for compressed instructions C.LHU, C.LH, and C.SH from the zcb extension to the sbi_misaligned_ldst library.

---
 include/sbi/riscv_encoding.h  |  7 +++++++
 lib/sbi/sbi_misaligned_ldst.c | 10 ++++++++++
 2 files changed, 17 insertions(+)
include/sbi/riscv_encoding.h +7 -0
index e74cc0d..3d77e26 100644
@@ -837,6 +837,13 @@
 #define INSN_MATCH_C_FSWSP             0xe002
 #define INSN_MASK_C_FSWSP              0xe003
 
+#define INSN_MATCH_C_LHU               0x8400
+#define INSN_MASK_C_LHU                0xfc43
+#define INSN_MATCH_C_LH                0x8440
+#define INSN_MASK_C_LH                 0xfc43
+#define INSN_MATCH_C_SH                0x8c00
+#define INSN_MASK_C_SH                 0xfc43
+
 #define INSN_MASK_WFI                  0xffffff00
 #define INSN_MATCH_WFI                 0x10500000
 
lib/sbi/sbi_misaligned_ldst.c +7 -0
index aa512de..9ca225a 100644
@@ -123,6 +123,13 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
                len = 4;
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+               len = 2;
+               insn = RVC_RS2S(insn) << SH_RD;
+       } else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+               len = 2;
+               shift = 8 * (sizeof(ulong) - len);
+               insn = RVC_RS2S(insn) << SH_RD;
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_LOAD;
@@ -237,6 +244,9 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
Quoted 8 lines

lib/sbi/sbi_misaligned_ldst.c int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst, · line 249

Quoted 6 lines
lib/sbi/sbi_misaligned_ldst.c +3 -0
                val.data_ulong = GET_F32_RS2C(insn, regs);
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+               len             = 2;
+               val.data_ulong = GET_RS2S(insn, regs);

Don't need to handle "c.sb" instruction ??

Quoted 7 lines

Hi Anup, thanks for your feedback.

in my opinion, because it's a byte so I don't think we'll encounter any misaligned issues.

I also wrote the following test program to specifically test c.sb, and indeed, it doesn't require any exception handler.

Ahh, yes. We certainly don't need for the misaligned load/store handling.

There is a separate series from Bo Gan where we are re-purposing the misaligned load/store handling for emulating load/store upon access fault. I got mixed-up between two separate patches.

Regards,
Anup

Quoted 69 lines

/* Check whether there is an address misalignment issue in the store byte instruction of zcb externsion. */
void checkStoreB(unsigned char* memory, size_t offset, unsigned char

value_byte) {
    asm volatile (
        "c.sb %[value_byte], 0(%[addr])"
        :
        : [value_byte] "r" (value_byte), [addr] "r" (memory + offset)
        : "memory"
    );

    printf("Store (c.sb) operation at address: 0x%lx\n", (unsigned

long)(memory + offset));

    if (memcmp(memory + offset, &value_byte, sizeof(unsigned char)) == 0) {
        printf("Store (c.sb) operation is correct: 0x%X\n", value_byte);
    } else {
        printf("Store (c.sb) operation may have address misalignment
issues.\n");
    }
}
int main() {
    size_t size = 10;
    unsigned char* memory = (unsigned char*)malloc(size);

    if (memory == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        return 1;
    }

    size_t offset = 1;

    checkStoreB(memory, offset, 0x42);

...
}

Quoted 8 lines
Quoted 8 lines
lib/sbi/sbi_misaligned_ldst.c +0 -1
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_STORE;
--

2.34.1

Quoted 7 lines
Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

--

==========================================
sifive system software team ???

Cell phone: 0989057854
E-mail: nylon7717 at gmail.com
==========================================

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#4 lore raw

PATCH 1/1 lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SH

In reply to Nylon Chen
Quoted 7 lines · On Mon, Feb 26, 2024 at 11:26?AM Nylon Chen <[email protected]> wrote:

From: "Nylon Chen" <[email protected]>

Added exception handling for compressed instructions C.LHU, C.LH, and C.SH from the zcb extension to the sbi_misaligned_ldst library.

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 59 lines
---
 include/sbi/riscv_encoding.h  |  7 +++++++
 lib/sbi/sbi_misaligned_ldst.c | 10 ++++++++++
 2 files changed, 17 insertions(+)
include/sbi/riscv_encoding.h +7 -0
index e74cc0d..3d77e26 100644
@@ -837,6 +837,13 @@
 #define INSN_MATCH_C_FSWSP             0xe002
 #define INSN_MASK_C_FSWSP              0xe003
 
+#define INSN_MATCH_C_LHU               0x8400
+#define INSN_MASK_C_LHU                0xfc43
+#define INSN_MATCH_C_LH                0x8440
+#define INSN_MASK_C_LH                 0xfc43
+#define INSN_MATCH_C_SH                0x8c00
+#define INSN_MASK_C_SH                 0xfc43
+
 #define INSN_MASK_WFI                  0xffffff00
 #define INSN_MATCH_WFI                 0x10500000
 
lib/sbi/sbi_misaligned_ldst.c +10 -1
index aa512de..9ca225a 100644
@@ -123,6 +123,13 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
                len = 4;
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+               len = 2;
+               insn = RVC_RS2S(insn) << SH_RD;
+       } else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+               len = 2;
+               shift = 8 * (sizeof(ulong) - len);
+               insn = RVC_RS2S(insn) << SH_RD;
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_LOAD;
@@ -237,6 +244,9 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
                val.data_ulong = GET_F32_RS2C(insn, regs);
 #endif
 #endif
+       } else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+               len             = 2;
+               val.data_ulong = GET_RS2S(insn, regs);
        } else {
                uptrap.epc = regs->mepc;
                uptrap.cause = CAUSE_MISALIGNED_STORE;
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi