Skip to contentSkip to conversation

riscv: rv32: Root page table address can be larger than 32-bit

PATCH Superseded Bin Meng 4 participants 12 messages Jul 31, 2019 – Aug 27, 2019 RISC-V

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 riscv: rv32: Root page table address can be larger than 32-bit

For RV32, the root page table's PPN has 22 bits hence its address bits could be larger than the maximum bits that target_ulong is able to represent. Use hwaddr instead.

---

 target/riscv/cpu_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/riscv/cpu_helper.c +2 -2
index e32b612..3150a6a 100644
@@ -176,7 +176,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
 
     *prot = 0;
 
-    target_ulong base;
+    hwaddr base;
     int levels, ptidxbits, ptesize, vm, sum;
     int mxr = get_field(env->mstatus, MSTATUS_MXR);
 
@@ -239,7 +239,7 @@ restart:
                            ((1 << ptidxbits) - 1);
 
         /* check that physical address of PTE is legal */
-        target_ulong pte_addr = base + idx * ptesize;
+        hwaddr pte_addr = base + idx * ptesize;
 
         if (riscv_feature(env, RISCV_FEATURE_PMP) &&
             !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
Signature · 1 line
2.7.4
#1 lore raw
Quoted 2 lines · Bin Meng
target/riscv/cpu_helper.c +1 -1
-    target_ulong base;
+    hwaddr base;

...

target/riscv/cpu_helper.c restart: · line 242

Quoted 2 lines
target/riscv/cpu_helper.c +1 -1
-        target_ulong pte_addr = base + idx * ptesize;
+        hwaddr pte_addr = base + idx * ptesize;

I believe that you either need

base + (hwaddr)idx * ptesize

or change the type of idx to hwaddr above.

Otherwise the multiply overflows before it gets promoted with the add.

r~

author
#2 lore raw

Hi Richard,

Quoted 8 lines · Richard Henderson
Quoted 2 lines · Bin Meng
target/riscv/cpu_helper.c +1 -1
-    target_ulong base;
+    hwaddr base;

...

target/riscv/cpu_helper.c restart: · line 242

Quoted 2 lines
target/riscv/cpu_helper.c +1 -1
-        target_ulong pte_addr = base + idx * ptesize;
+        hwaddr pte_addr = base + idx * ptesize;
Quoted 8 lines

I believe that you either need

base + (hwaddr)idx * ptesize

or change the type of idx to hwaddr above.

Otherwise the multiply overflows before it gets promoted with the add.

I am not sure how (idx * ptesize) could overflow. It represents the offset by a page table which is [0, 4096).

Regards,
Bin

#3 lore raw
Quoted 2 lines · Bin Meng

I am not sure how (idx * ptesize) could overflow. It represents the offset by a page table which is [0, 4096).

You're right, I mis-read what was going on there.

However, lower down, "target_ulong ppn" needs to be promoted to hwaddr, so that

    ppn = pte >> PTE_PPN_SHIFT;
    ...
    base = ppn << PGSHIFT;

does not overflow. (Which is the part of the page table walk that I thought I had gleaned from the patch without actually reading the entire function.)

r~

author
#4 lore raw
Quoted 7 lines · Richard Henderson
Quoted 2 lines · Bin Meng

I am not sure how (idx * ptesize) could overflow. It represents the offset by a page table which is [0, 4096).

You're right, I mis-read what was going on there.

Quoted 8 lines

However, lower down, "target_ulong ppn" needs to be promoted to hwaddr, so that

    ppn = pte >> PTE_PPN_SHIFT;
    ...
    base = ppn << PGSHIFT;

does not overflow. (Which is the part of the page table walk that I thought I had gleaned from the patch without actually reading the entire function.)

Ah, yes. ppn should be promoted. Thanks for the review!

Regards,
Bin

#5 lore raw
Quoted 16 lines · Bin Meng
Quoted 8 lines · Richard Henderson
Quoted 2 lines · Bin Meng

I am not sure how (idx * ptesize) could overflow. It represents the offset by a page table which is [0, 4096).

You're right, I mis-read what was going on there.

However, lower down, "target_ulong ppn" needs to be promoted to hwaddr,

so that

Quoted 5 lines
    ppn = pte >> PTE_PPN_SHIFT;
    ...
    base = ppn << PGSHIFT;
Quoted 6 lines
Quoted 1 line

does not overflow. (Which is the part of the page table walk that I

thought I

Quoted 1 line

had gleaned from the patch without actually reading the entire function.)

Ah, yes. ppn should be promoted. Thanks for the review!

Did I miss a v2?

author
#6 lore raw

Hi Palmer,

Quoted 18 lines · Palmer Dabbelt
Quoted 16 lines · Bin Meng
Quoted 13 lines · Richard Henderson
Quoted 2 lines · Bin Meng

I am not sure how (idx * ptesize) could overflow. It represents the offset by a page table which is [0, 4096).

You're right, I mis-read what was going on there.

However, lower down, "target_ulong ppn" needs to be promoted to hwaddr, so that

    ppn = pte >> PTE_PPN_SHIFT;
    ...
    base = ppn << PGSHIFT;
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

does not overflow. (Which is the part of the page table walk that I thought I had gleaned from the patch without actually reading the entire function.)

Ah, yes. ppn should be promoted. Thanks for the review!

Did I miss a v2?

No, I will send a v2 soon.

Regards,
Bin

author
#7 lore raw

PATCH v2 1/1 riscv: rv32: Root page table address can be larger than 32-bit

In reply to Bin Meng

For RV32, the root page table's PPN has 22 bits hence its address bits could be larger than the maximum bits that target_ulong is able to represent. Use hwaddr instead.

---

Changes in v2:
- promote ppn, env->satp/env->sptbl to hwaddr otherwise the page table base will not be correctly calculated

 target/riscv/cpu_helper.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
target/riscv/cpu_helper.c +5 -5
index e32b612..b2b4f3a 100644
@@ -176,12 +176,12 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
 
     *prot = 0;
 
-    target_ulong base;
+    hwaddr base;
     int levels, ptidxbits, ptesize, vm, sum;
     int mxr = get_field(env->mstatus, MSTATUS_MXR);
 
     if (env->priv_ver >= PRIV_VERSION_1_10_0) {
-        base = get_field(env->satp, SATP_PPN) << PGSHIFT;
+        base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT;
         sum = get_field(env->mstatus, MSTATUS_SUM);
         vm = get_field(env->satp, SATP_MODE);
         switch (vm) {
@@ -201,7 +201,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
           g_assert_not_reached();
         }
     } else {
-        base = env->sptbr << PGSHIFT;
+        base = (hwaddr)(env->sptbr) << PGSHIFT;
         sum = !get_field(env->mstatus, MSTATUS_PUM);
         vm = get_field(env->mstatus, MSTATUS_VM);
         switch (vm) {
@@ -239,7 +239,7 @@ restart:
                            ((1 << ptidxbits) - 1);
 
         /* check that physical address of PTE is legal */
-        target_ulong pte_addr = base + idx * ptesize;
+        hwaddr pte_addr = base + idx * ptesize;
 
         if (riscv_feature(env, RISCV_FEATURE_PMP) &&
             !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
@@ -251,7 +251,7 @@ restart:
 #elif defined(TARGET_RISCV64)
         target_ulong pte = ldq_phys(cs->as, pte_addr);
 #endif
-        target_ulong ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn = pte >> PTE_PPN_SHIFT;
 
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#8 lore raw
Quoted 6 lines · Bin Meng

For RV32, the root page table's PPN has 22 bits hence its address bits could be larger than the maximum bits that target_ulong is able to represent. Use hwaddr instead.

Alistair

Quoted 60 lines

---

Changes in v2:
- promote ppn, env->satp/env->sptbl to hwaddr otherwise the page table base will not be correctly calculated

 target/riscv/cpu_helper.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
target/riscv/cpu_helper.c +5 -6
index e32b612..b2b4f3a 100644
@@ -176,12 +176,12 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
 
     *prot = 0;
 
-    target_ulong base;
+    hwaddr base;
     int levels, ptidxbits, ptesize, vm, sum;
     int mxr = get_field(env->mstatus, MSTATUS_MXR);
 
     if (env->priv_ver >= PRIV_VERSION_1_10_0) {
-        base = get_field(env->satp, SATP_PPN) << PGSHIFT;
+        base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT;
         sum = get_field(env->mstatus, MSTATUS_SUM);
         vm = get_field(env->satp, SATP_MODE);
         switch (vm) {
@@ -201,7 +201,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
           g_assert_not_reached();
         }
     } else {
-        base = env->sptbr << PGSHIFT;
+        base = (hwaddr)(env->sptbr) << PGSHIFT;
         sum = !get_field(env->mstatus, MSTATUS_PUM);
         vm = get_field(env->mstatus, MSTATUS_VM);
         switch (vm) {
@@ -239,7 +239,7 @@ restart:
                            ((1 << ptidxbits) - 1);
 
         /* check that physical address of PTE is legal */
-        target_ulong pte_addr = base + idx * ptesize;
+        hwaddr pte_addr = base + idx * ptesize;
 
         if (riscv_feature(env, RISCV_FEATURE_PMP) &&
             !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
@@ -251,7 +251,7 @@ restart:
 #elif defined(TARGET_RISCV64)
         target_ulong pte = ldq_phys(cs->as, pte_addr);
 #endif
-        target_ulong ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn = pte >> PTE_PPN_SHIFT;
 
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
--

2.7.4

author
#9 lore raw

Hi Palmer,

Quoted 11 lines · Alistair Francis
Quoted 6 lines · Bin Meng

For RV32, the root page table's PPN has 22 bits hence its address bits could be larger than the maximum bits that target_ulong is able to represent. Use hwaddr instead.

Would you take this one too?

Regards,
Bin

author
#10 lore raw
Quoted 11 lines · Bin Meng

Hi Palmer,

Quoted 7 lines · Alistair Francis
Quoted 5 lines · Bin Meng

For RV32, the root page table's PPN has 22 bits hence its address bits could be larger than the maximum bits that target_ulong is able to represent. Use hwaddr instead.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Would you take this one too?

Ping?

What's the status of this patch?

Regards,
Bin

#11 lore raw
Quoted 17 lines · On Sun, 18 Aug 2019 23:00:40 PDT (-0700), [email protected] wrote:
Quoted 16 lines · Bin Meng

Hi Palmer,

Quoted 11 lines · Alistair Francis
Quoted 6 lines · Bin Meng

For RV32, the root page table's PPN has 22 bits hence its address bits could be larger than the maximum bits that target_ulong is able to represent. Use hwaddr instead.

Quoted 6 lines
Quoted 2 lines

Would you take this one too?

Ping?

What's the status of this patch?

Also in the patch queue.