Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
riscv: Make sure an exception is raised if a pte is malformed
As per the specification, in 64-bit, if any of the pte reserved bits
60-54 is set an exception should be triggered (see 4.4.1, "Addressing and
Memory Protection"). In addition, we must check the napot/pbmt bits are
not set if those extensions are not active.

Reported-by: Andrea Parri <andrea@rivosinc.com>
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20230420150220.60919-1-alexghiti@rivosinc.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
  • Loading branch information
Alexandre Ghiti authored and alistair23 committed May 5, 2023
1 parent 7bf14a2 commit 190e9f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions target/riscv/cpu_bits.h
Expand Up @@ -644,6 +644,7 @@ typedef enum {
#define PTE_SOFT 0x300 /* Reserved for Software */
#define PTE_PBMT 0x6000000000000000ULL /* Page-based memory types */
#define PTE_N 0x8000000000000000ULL /* NAPOT translation */
#define PTE_RESERVED 0x1FC0000000000000ULL /* Reserved bits */
#define PTE_ATTR (PTE_N | PTE_PBMT) /* All attributes bits */

/* Page table PPN shift amount */
Expand Down
15 changes: 11 additions & 4 deletions target/riscv/cpu_helper.c
Expand Up @@ -927,13 +927,20 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,

if (riscv_cpu_sxl(env) == MXL_RV32) {
ppn = pte >> PTE_PPN_SHIFT;
} else if (pbmte || riscv_cpu_cfg(env)->ext_svnapot) {
ppn = (pte & (target_ulong)PTE_PPN_MASK) >> PTE_PPN_SHIFT;
} else {
ppn = pte >> PTE_PPN_SHIFT;
if ((pte & ~(target_ulong)PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
if (pte & PTE_RESERVED) {
return TRANSLATE_FAIL;
}

if (!pbmte && (pte & PTE_PBMT)) {
return TRANSLATE_FAIL;
}

if (!riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
return TRANSLATE_FAIL;
}

ppn = (pte & (target_ulong)PTE_PPN_MASK) >> PTE_PPN_SHIFT;
}

if (!(pte & PTE_V)) {
Expand Down

0 comments on commit 190e9f8

Please sign in to comment.