Skip to contentSkip to conversation

hw/riscv/riscv-iommu: fix U-bit check to apply only to leaf S/VS-stage PTEs

PATCH Applied Andrew Jones 4 participants 4 messages Jul 17 – Aug 12 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/riscv/riscv-iommu: fix U-bit check to apply only to leaf S/VS-stage PTEs

Commit b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access") placed its check ahead of the leaf-vs-non-leaf branch in riscv_iommu_spa_fetch(), so it fires on every PTE walked, including non-leaf/table entries. Per the RISC-V privileged spec's address translation algorithm (Sv39/Sv48/etc., the "leaf PTE has been reached" step, followed separately by the U-bit permission check), the U bit is only defined and checked for the leaf PTE reached at the end of the walk -- non-leaf PTEs don't carry a meaningful U bit at all.

Move the check after the leaf/non-leaf branch, alongside the other leaf-only checks, mirroring how the G_STAGE U-bit check (added in 9158c900ab30) is already correctly placed.

Fixes b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access")
---
 hw/riscv/riscv-iommu.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
hw/riscv/riscv-iommu.c +7 -7
index f6865d1e164e..ed9fb09f8bc7 100644
@@ -476,13 +476,6 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
             break;                /* Invalid PTE */
         } else if (pte & PTE_RESERVED(false)) {
             break;                /* Reserved PTE bits set */
-        } else if (!(pte & PTE_U) && !pv) {
-            /*
-             * All accesses are assumed to be User mode unless
-             * process_id is valid (pv).  In case we have a
-             * non-user mode PTE and !pv we need to fault.
-             */
-            break;
         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
             base = PPN_PHYS(ppn); /* Inner PTE, continue walking */
         } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
@@ -491,6 +484,13 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
             break;                /* Reserved leaf PTE flags: PTE_W + PTE_X */
         } else if (ppn & ((1ULL << (va_skip - TARGET_PAGE_BITS)) - 1)) {
             break;                /* Misaligned PPN */
+        } else if (!(pte & PTE_U) && !pv) {
+            /*
+             * All accesses are assumed to be User mode unless
+             * process_id is valid (pv).  In case we have a
+             * non-user mode leaf PTE and !pv we need to fault.
+             */
+            break;
         } else if ((iotlb->perm & IOMMU_RO) && !(pte & PTE_R)) {
             break;                /* Read access check failed */
         } else if ((iotlb->perm & IOMMU_WO) && !(pte & PTE_W)) {
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrew Jones

Commit b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access") placed its check ahead of the leaf-vs-non-leaf branch in riscv_iommu_spa_fetch(), so it fires on every PTE walked, including non-leaf/table entries. Per the RISC-V privileged spec's address translation algorithm (Sv39/Sv48/etc., the "leaf PTE has been reached" step, followed separately by the U-bit permission check), the U bit is only defined and checked for the leaf PTE reached at the end of the walk -- non-leaf PTEs don't carry a meaningful U bit at all.

Quoted 7 lines

Move the check after the leaf/non-leaf branch, alongside the other leaf-only checks, mirroring how the G_STAGE U-bit check (added in 9158c900ab30) is already correctly placed.

Fixes b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access")

---

Quoted 35 lines
  hw/riscv/riscv-iommu.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
hw/riscv/riscv-iommu.c +7 -7
index f6865d1e164e..ed9fb09f8bc7 100644
@@ -476,13 +476,6 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
              break;                /* Invalid PTE */
          } else if (pte & PTE_RESERVED(false)) {
              break;                /* Reserved PTE bits set */
-        } else if (!(pte & PTE_U) && !pv) {
-            /*
-             * All accesses are assumed to be User mode unless
-             * process_id is valid (pv).  In case we have a
-             * non-user mode PTE and !pv we need to fault.
-             */
-            break;
          } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
              base = PPN_PHYS(ppn); /* Inner PTE, continue walking */
          } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
@@ -491,6 +484,13 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
              break;                /* Reserved leaf PTE flags: PTE_W + PTE_X */
          } else if (ppn & ((1ULL << (va_skip - TARGET_PAGE_BITS)) - 1)) {
              break;                /* Misaligned PPN */
+        } else if (!(pte & PTE_U) && !pv) {
+            /*
+             * All accesses are assumed to be User mode unless
+             * process_id is valid (pv).  In case we have a
+             * non-user mode leaf PTE and !pv we need to fault.
+             */
+            break;
          } else if ((iotlb->perm & IOMMU_RO) && !(pte & PTE_R)) {
              break;                /* Read access check failed */
          } else if ((iotlb->perm & IOMMU_WO) && !(pte & PTE_W)) {
Reviewed-by on v1 1/1
#2 lore raw
In reply to Andrew Jones
Quoted 10 lines · Andrew Jones

Commit b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access") placed its check ahead of the leaf-vs-non-leaf branch in riscv_iommu_spa_fetch(), so it fires on every PTE walked, including non-leaf/table entries. Per the RISC-V privileged spec's address translation algorithm (Sv39/Sv48/etc., the "leaf PTE has been reached" step, followed separately by the U-bit permission check), the U bit is only defined and checked for the leaf PTE reached at the end of the walk -- non-leaf PTEs don't carry a meaningful U bit at all.

Quoted 6 lines

Move the check after the leaf/non-leaf branch, alongside the other leaf-only checks, mirroring how the G_STAGE U-bit check (added in 9158c900ab30) is already correctly placed.

Fixes b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access")

Thanks,
Nutty

Quoted 36 lines
---
  hw/riscv/riscv-iommu.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
hw/riscv/riscv-iommu.c +7 -7
index f6865d1e164e..ed9fb09f8bc7 100644
@@ -476,13 +476,6 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
              break;                /* Invalid PTE */
          } else if (pte & PTE_RESERVED(false)) {
              break;                /* Reserved PTE bits set */
-        } else if (!(pte & PTE_U) && !pv) {
-            /*
-             * All accesses are assumed to be User mode unless
-             * process_id is valid (pv).  In case we have a
-             * non-user mode PTE and !pv we need to fault.
-             */
-            break;
          } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
              base = PPN_PHYS(ppn); /* Inner PTE, continue walking */
          } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
@@ -491,6 +484,13 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
              break;                /* Reserved leaf PTE flags: PTE_W + PTE_X */
          } else if (ppn & ((1ULL << (va_skip - TARGET_PAGE_BITS)) - 1)) {
              break;                /* Misaligned PPN */
+        } else if (!(pte & PTE_U) && !pv) {
+            /*
+             * All accesses are assumed to be User mode unless
+             * process_id is valid (pv).  In case we have a
+             * non-user mode leaf PTE and !pv we need to fault.
+             */
+            break;
          } else if ((iotlb->perm & IOMMU_RO) && !(pte & PTE_R)) {
              break;                /* Read access check failed */
          } else if ((iotlb->perm & IOMMU_WO) && !(pte & PTE_W)) {
Applied
#3 lore raw
In reply to Andrew Jones
Quoted 10 lines · Andrew Jones

Commit b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and no priv access") placed its check ahead of the leaf-vs-non-leaf branch in riscv_iommu_spa_fetch(), so it fires on every PTE walked, including non-leaf/table entries. Per the RISC-V privileged spec's address translation algorithm (Sv39/Sv48/etc., the "leaf PTE has been reached" step, followed separately by the U-bit permission check), the U bit is only defined and checked for the leaf PTE reached at the end of the walk -- non-leaf PTEs don't carry a meaningful U bit at all.

Quoted 7 lines

Move the check after the leaf/non-leaf branch, alongside the other leaf-only checks, mirroring how the G_STAGE U-bit check (added in 9158c900ab30) is already correctly placed.

Fixes b795ea0ba471 ("hw/riscv/riscv-iommu.c: fault when !PTE_U and

no priv access")

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 40 lines

---
 hw/riscv/riscv-iommu.c | 14 +++++++-------

 1 file changed, 7 insertions(+), 7 deletions(-)
hw/riscv/riscv-iommu.c +0 -0
index f6865d1e164e..ed9fb09f8bc7 100644
@@ -476,13 +476,6 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState
 *s, RISCVIOMMUContext *ctx,

             break;                /* Invalid PTE */
         } else if (pte & PTE_RESERVED(false)) {
             break;                /* Reserved PTE bits set */ -        } else if (!(pte & PTE_U) && !pv) {
-            /*
-             * All accesses are assumed to be User mode unless -             * process_id is valid (pv).  In case we have a -             * non-user mode PTE and !pv we need to fault. -             */
-            break;
         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) { base = PPN_PHYS(ppn); /* Inner PTE, continue walking */ } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {

hw/riscv/riscv-iommu.c +0 -0
@@ -491,6 +484,13 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState
 *s, RISCVIOMMUContext *ctx,

             break;                /* Reserved leaf PTE flags: PTE_W
+ PTE_X */
         } else if (ppn & ((1ULL << (va_skip - TARGET_PAGE_BITS)) - 1)) {
             break;                /* Misaligned PPN */
+        } else if (!(pte & PTE_U) && !pv) {
+            /*
+             * All accesses are assumed to be User mode unless +             * process_id is valid (pv).  In case we have a +             * non-user mode leaf PTE and !pv we need to fault. +             */
+            break;
         } else if ((iotlb->perm & IOMMU_RO) && !(pte & PTE_R)) { break;                /* Read access check failed */ } else if ((iotlb->perm & IOMMU_WO) && !(pte & PTE_W)) {