Skip to contentSkip to conversation

lib: sbi_misaligned_ldst: Set GVA if not emulating

PATCH New Vivian Wang 3 participants 8 messages Sep 2, 2022 – Sep 13, 2022

lore
Share & tools
lore
#0 lore raw

cover lib: sbi_misaligned_ldst: Set GVA if not emulating

Fix of a previous patch 1c4ce74f5128 ("lib: sbi: Set gva when creating sbi_trap_info"), correctly set hstatus.GVA for misaligned loads/stores that cannot be emulated at all.

See: https://lists.infradead.org/pipermail/opensbi/2022-August/003274.html

Vivian Wang (2):
lib: sbi_trap: Add helper to get GVA in sbi_trap_regs
lib: sbi_misaligned_ldst: Set GVA if not emulating

 include/sbi/sbi_trap.h        | 18 ++++++++++++++++++
 lib/sbi/sbi_misaligned_ldst.c |  4 ++--
 lib/sbi/sbi_trap.c            | 14 +-------------
 3 files changed, 21 insertions(+), 15 deletions(-)
Signature · 1 line
2.37.1
#1 lore raw

cover lib: sbi_misaligned_ldst: Set GVA if not emulating

Fix of a previous patch 1c4ce74f5128 ("lib: sbi: Set gva when creating sbi_trap_info"), correctly set hstatus.GVA for misaligned loads/stores that cannot be emulated at all.

See: https://lists.infradead.org/pipermail/opensbi/2022-August/003274.html

Vivian Wang (2):
lib: sbi_trap: Add helper to get GVA in sbi_trap_regs
lib: sbi_misaligned_ldst: Set GVA if not emulating

 include/sbi/sbi_trap.h        | 18 ++++++++++++++++++
 lib/sbi/sbi_misaligned_ldst.c |  4 ++--
 lib/sbi/sbi_trap.c            | 14 +-------------
 3 files changed, 21 insertions(+), 15 deletions(-)
Signature · 1 line
2.37.1
#2 lore raw

PATCH 1/2 lib: sbi_trap: Add helper to get GVA in sbi_trap_regs

In reply to Vivian Wang

The GVA bit is in mstatus on RV64, and in mstatush in RV32. Refactor code handling this in sbi_trap_handler into a helper function to extract GVA from sbi_trap_regs, so that future code accessing GVA can be XLEN-agnostic.

---
 include/sbi/sbi_trap.h | 18 ++++++++++++++++++
 lib/sbi/sbi_trap.c     | 14 +-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
include/sbi/sbi_trap.h +18 -0
index fe3640a..a562b95 100644
@@ -10,6 +10,8 @@
 #ifndef __SBI_TRAP_H__
 #define __SBI_TRAP_H__
 
+#include <sbi/riscv_encoding.h>
+
 /* clang-format off */
 
 /** Index of zero member in sbi_trap_regs */
@@ -206,6 +208,22 @@ struct sbi_trap_info {
 	unsigned long gva;
 };
 
+static inline unsigned long sbi_regs_gva(const struct sbi_trap_regs *regs)
+{
+	/*
+	 * If the hypervisor extension is not implemented, mstatus[h].GVA is a
+	 * WPRI field, which is guaranteed to read as zero. In addition, in this
+	 * case we don't read mstatush and instead pretend it is zero, which
+	 * handles privileged spec version < 1.12.
+	 */
+
+#if __riscv_xlen == 32
+	return (regs->mstatusH & MSTATUSH_GVA) ? 1 : 0;
+#else
+	return (regs->mstatus & MSTATUS_GVA) ? 1 : 0;
+#endif
+}
+
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      struct sbi_trap_info *trap);
 
lib/sbi/sbi_trap.c +1 -13
index 4c1339e..c875c90 100644
@@ -316,19 +316,7 @@ struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs)
 		trap.tval = mtval;
 		trap.tval2 = mtval2;
 		trap.tinst = mtinst;
-
-		/*
-		 * If the hypervisor extension is not implemented,
-		 * mstatus[h].GVA is a WPRI field, which is guaranteed to read
-		 * as zero. In addition, in this case we don't read mstatush and
-		 * instead pretend it is zero, which handles privileged spec
-		 * version < 1.12.
-		 */
-#if __riscv_xlen == 32
-		trap.gva = (regs->mstatusH & MSTATUSH_GVA) ? 1 : 0;
-#else
-		trap.gva = (regs->mstatus & MSTATUS_GVA) ? 1 : 0;
-#endif
+		trap.gva   = sbi_regs_gva(regs);
 
 		rc = sbi_trap_redirect(regs, &trap);
 		break;
Signature · 1 line
2.37.1
#3 lore raw

PATCH 1/2 lib: sbi_trap: Add helper to get GVA in sbi_trap_regs

Quoted 11 lines · Vivian Wang

The GVA bit is in mstatus on RV64, and in mstatush in RV32. Refactor code handling this in sbi_trap_handler into a helper function to extract GVA from sbi_trap_regs, so that future code accessing GVA can be XLEN-agnostic.

---
 include/sbi/sbi_trap.h | 18 ++++++++++++++++++
 lib/sbi/sbi_trap.c     | 14 +-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
Applied
#4 lore raw

PATCH 1/2 lib: sbi_trap: Add helper to get GVA in sbi_trap_regs

In reply to Vivian Wang
Quoted 7 lines · Vivian Wang

The GVA bit is in mstatus on RV64, and in mstatush in RV32. Refactor code handling this in sbi_trap_handler into a helper function to extract GVA from sbi_trap_regs, so that future code accessing GVA can be XLEN-agnostic.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 74 lines
---
 include/sbi/sbi_trap.h | 18 ++++++++++++++++++
 lib/sbi/sbi_trap.c     | 14 +-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
include/sbi/sbi_trap.h +18 -0
index fe3640a..a562b95 100644
@@ -10,6 +10,8 @@
 #ifndef __SBI_TRAP_H__
 #define __SBI_TRAP_H__
 
+#include <sbi/riscv_encoding.h>
+
 /* clang-format off */
 
 /** Index of zero member in sbi_trap_regs */
@@ -206,6 +208,22 @@ struct sbi_trap_info {
        unsigned long gva;
 };
 
+static inline unsigned long sbi_regs_gva(const struct sbi_trap_regs *regs)
+{
+       /*
+        * If the hypervisor extension is not implemented, mstatus[h].GVA is a
+        * WPRI field, which is guaranteed to read as zero. In addition, in this
+        * case we don't read mstatush and instead pretend it is zero, which
+        * handles privileged spec version < 1.12.
+        */
+
+#if __riscv_xlen == 32
+       return (regs->mstatusH & MSTATUSH_GVA) ? 1 : 0;
+#else
+       return (regs->mstatus & MSTATUS_GVA) ? 1 : 0;
+#endif
+}
+
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
                      struct sbi_trap_info *trap);
 
lib/sbi/sbi_trap.c +1 -14
index 4c1339e..c875c90 100644
@@ -316,19 +316,7 @@ struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs)
                trap.tval = mtval;
                trap.tval2 = mtval2;
                trap.tinst = mtinst;
-
-               /*
-                * If the hypervisor extension is not implemented,
-                * mstatus[h].GVA is a WPRI field, which is guaranteed to read
-                * as zero. In addition, in this case we don't read mstatush and
-                * instead pretend it is zero, which handles privileged spec
-                * version < 1.12.
-                */
-#if __riscv_xlen == 32
-               trap.gva = (regs->mstatusH & MSTATUSH_GVA) ? 1 : 0;
-#else
-               trap.gva = (regs->mstatus & MSTATUS_GVA) ? 1 : 0;
-#endif
+               trap.gva   = sbi_regs_gva(regs);
 
                rc = sbi_trap_redirect(regs, &trap);
                break;
--

2.37.1

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

#5 lore raw

PATCH 2/2 lib: sbi_misaligned_ldst: Set GVA if not emulating

In reply to Vivian Wang

If a particular misaligned load or store cannot be emulated at all, for the redirected trap, trap.gva is set to 0, but it should be the same as mstatus[h].GVA of the original trap. Fix this so that if the trap is destined for HS-mode, hstatus.GVA is then set correctly.

Fixes 1c4ce74f5128 ("lib: sbi: Set gva when creating sbi_trap_info")
---
 lib/sbi/sbi_misaligned_ldst.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/sbi/sbi_misaligned_ldst.c +2 -2
index 92a2393..4b91e07 100644
@@ -129,7 +129,7 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
 		uptrap.tval = addr;
 		uptrap.tval2 = tval2;
 		uptrap.tinst = tinst;
-		uptrap.gva   = 0;
+		uptrap.gva   = sbi_regs_gva(regs);
 		return sbi_trap_redirect(regs, &uptrap);
 	}
 
@@ -245,7 +245,7 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
 		uptrap.tval = addr;
 		uptrap.tval2 = tval2;
 		uptrap.tinst = tinst;
-		uptrap.gva   = 0;
+		uptrap.gva   = sbi_regs_gva(regs);
 		return sbi_trap_redirect(regs, &uptrap);
 	}
 
Signature · 1 line
2.37.1
#6 lore raw

PATCH 2/2 lib: sbi_misaligned_ldst: Set GVA if not emulating

Quoted 35 lines · Vivian Wang

If a particular misaligned load or store cannot be emulated at all, for the redirected trap, trap.gva is set to 0, but it should be the same as mstatus[h].GVA of the original trap. Fix this so that if the trap is destined for HS-mode, hstatus.GVA is then set correctly.

Fixes 1c4ce74f5128 ("lib: sbi: Set gva when creating sbi_trap_info")
---
 lib/sbi/sbi_misaligned_ldst.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/sbi/sbi_misaligned_ldst.c +2 -2
index 92a2393..4b91e07 100644
@@ -129,7 +129,7 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
 		uptrap.tval = addr;
 		uptrap.tval2 = tval2;
 		uptrap.tinst = tinst;
-		uptrap.gva   = 0;
+		uptrap.gva   = sbi_regs_gva(regs);
 		return sbi_trap_redirect(regs, &uptrap);
 	}
 
@@ -245,7 +245,7 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
 		uptrap.tval = addr;
 		uptrap.tval2 = tval2;
 		uptrap.tinst = tinst;
-		uptrap.gva   = 0;
+		uptrap.gva   = sbi_regs_gva(regs);
 		return sbi_trap_redirect(regs, &uptrap);
 	}
 
Signature · 1 line
2.37.1
Applied
#7 lore raw

PATCH 2/2 lib: sbi_misaligned_ldst: Set GVA if not emulating

In reply to Vivian Wang
Quoted 8 lines · Vivian Wang

If a particular misaligned load or store cannot be emulated at all, for the redirected trap, trap.gva is set to 0, but it should be the same as mstatus[h].GVA of the original trap. Fix this so that if the trap is destined for HS-mode, hstatus.GVA is then set correctly.

Fixes 1c4ce74f5128 ("lib: sbi: Set gva when creating sbi_trap_info")

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 34 lines
---
 lib/sbi/sbi_misaligned_ldst.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/sbi/sbi_misaligned_ldst.c +2 -3
index 92a2393..4b91e07 100644
@@ -129,7 +129,7 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
                uptrap.tval = addr;
                uptrap.tval2 = tval2;
                uptrap.tinst = tinst;
-               uptrap.gva   = 0;
+               uptrap.gva   = sbi_regs_gva(regs);
                return sbi_trap_redirect(regs, &uptrap);
        }
 
@@ -245,7 +245,7 @@ int sbi_misaligned_store_handler(ulong addr, ulong tval2, ulong tinst,
                uptrap.tval = addr;
                uptrap.tval2 = tval2;
                uptrap.tinst = tinst;
-               uptrap.gva   = 0;
+               uptrap.gva   = sbi_regs_gva(regs);
                return sbi_trap_redirect(regs, &uptrap);
        }
 
--

2.37.1

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