Skip to contentSkip to conversation

target/riscv/pmp.c: Don't try further once matching PMP entry found

PATCH New Anup Patel 1 participant 2 messages Dec 5, 2018

Subsystems (2)
lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/2 target/riscv/pmp.c: Don't try further once matching PMP entry found

As-per RISC-V privilege spec, we should only consider first matching PMP entry. This is not enforced by current pmp_has_access().

Let's say we have two PMP entries configured by M-mode code:
PMP0: 0x0000000080000000-0x000000008001ffff (A)
PMP1: 0x0000000000000000-0xffffffffffffffff (A,R,W,X)

Now if S-mode code tries to access 0x80000000 then it should generate trap to M-mode because PMP0 blocks the access.

The current implementation of pmp_has_access() checks PMP0 which gives result=0 so it continues and tries PMP1 which gives result=1 hence the access to 0x80000000 from S-mode is allowed.

This patch fixes pmp_has_access() to return immediatiely once a matching entry is found (irrespective to the value of result).

---
 target/riscv/pmp.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/riscv/pmp.c +4 -1
index 3dbb87c69d..936a52b6ba 100644
@@ -193,8 +193,11 @@ bool pmp_has_access(CPURISCVState *env, target_ulong addr, int size, int rw,
                 /* breaks iff other covering entries (invalid config). Punt */
                 *tlb_size = roundpow2(ea - sa);
             }
-            goto match;
         }
+
+        /* only first matching PMP entry applies */
+        if (sa <= addr && (addr + size - 1) < ea)
+            goto match;
     }
 
     /* only allow M mode if no rules are present */
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 hw/riscv/sifive_u: Set 'clock-frequency' DT property for SiFive UART

The 'clock-frequency' DT property is required by U-Boot to compute divider value. This patch sets 'clock-frequency' DT property of SiFive UART DT node (similar to virt machine).

---
 hw/riscv/sifive_u.c | 2 ++
 1 file changed, 2 insertions(+)
hw/riscv/sifive_u.c +2 -0
index b3a4352986..5fa666fefc 100644
@@ -238,6 +238,8 @@ static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,
     qemu_fdt_setprop_cells(fdt, nodename, "reg",
         0x0, memmap[SIFIVE_U_UART0].base,
         0x0, memmap[SIFIVE_U_UART0].size);
+    qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
+        SIFIVE_U_CLOCK_FREQ / 2);
     qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle);
     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 1);
 
Signature · 1 line
2.17.1