Skip to contentSkip to conversation

target/mips: Fix execution mode in page_table_walk_refill()

PATCH-for-9.1 Philippe Mathieu-Daudé 3 participants 6 messages Aug 11, 2024 – Aug 13, 2024

lore
Share & tools
lore
#0 lore raw

When refactoring page_table_walk_refill() in commit 4e999bf419 we replaced the execution mode and forced it to kernel mode. Restore the previous behavior to also get supervisor / user modes.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2470

Fixes 4e999bf419 ("target/mips: Pass ptw_mmu_idx down from mips_cpu_tlb_fill")
---
 target/mips/tcg/sysemu/tlb_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/tcg/sysemu/tlb_helper.c +3 -2
index 3ba6d369a6..e7ae4f0bef 100644
@@ -940,8 +940,9 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
          * Memory reads during hardware page table walking are performed
          * as if they were kernel-mode load instructions.
          */
-        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL ?
-                           MMU_ERL_IDX : MMU_KERNEL_IDX);
+        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL)
+                          ? MMU_ERL_IDX
+                          : (env->hflags & MIPS_HFLAG_KSU);
 
         if (page_table_walk_refill(env, address, ptw_mmu_idx)) {
             ret = get_physical_address(env, &physical, &prot, address,
Signature · 1 line
2.45.2
#1 lore raw
Quoted 18 lines · Philippe Mathieu-Daudé

When refactoring page_table_walk_refill() in commit 4e999bf419 we replaced the execution mode and forced it to kernel mode. Restore the previous behavior to also get supervisor / user modes.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2470

Fixes 4e999bf419 ("target/mips: Pass ptw_mmu_idx down from mips_cpu_tlb_fill")
---
  target/mips/tcg/sysemu/tlb_helper.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/tcg/sysemu/tlb_helper.c +0 -0
index 3ba6d369a6..e7ae4f0bef 100644

target/mips/tcg/sysemu/tlb_helper.c bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 945

Quoted 8 lines
target/mips/tcg/sysemu/tlb_helper.c +3 -2
@@ -940,8 +940,9 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
           * Memory reads during hardware page table walking are performed
           * as if they were kernel-mode load instructions.
           */
-        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL ?
-                           MMU_ERL_IDX : MMU_KERNEL_IDX);
+        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL)
+                          ? MMU_ERL_IDX
+                          : (env->hflags & MIPS_HFLAG_KSU);

This contradicts the comment above.
If this code change is correct, then the comment isn't.

But the comment certainly makes sense -- page tables are never accessible to user mode.

r~

#2 lore raw
Quoted 23 lines · Richard Henderson
Quoted 22 lines · Philippe Mathieu-Daudé

When refactoring page_table_walk_refill() in commit 4e999bf419 we replaced the execution mode and forced it to kernel mode. Restore the previous behavior to also get supervisor / user modes.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2470

Fixes 4e999bf419 ("target/mips: Pass ptw_mmu_idx down from mips_cpu_tlb_fill")

---
  target/mips/tcg/sysemu/tlb_helper.c | 5 +++--

  1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/tcg/sysemu/tlb_helper.c +0 -0
index 3ba6d369a6..e7ae4f0bef 100644
@@ -940,8 +940,9 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr 
 address, int size,
            * Memory reads during hardware page table walking are performed
            * as if they were kernel-mode load instructions.
            */
Quoted 8 lines

target/mips/tcg/sysemu/tlb_helper.c bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 945

Quoted 5 lines
target/mips/tcg/sysemu/tlb_helper.c +3 -2
-        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL ?
-                           MMU_ERL_IDX : MMU_KERNEL_IDX);
+        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL)
+                          ? MMU_ERL_IDX
+                          : (env->hflags & MIPS_HFLAG_KSU);

This contradicts the comment above.
If this code change is correct, then the comment isn't.

OK.

Quoted 1 line

But the comment certainly makes sense -- page tables are never accessible to user mode.

But we are still dropping the supervisor mode, so OK if I reword as:

"Restore the previous behavior to also get supervisor modes."

?

#3 lore raw
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Richard Henderson
Quoted 13 lines · Philippe Mathieu-Daudé

When refactoring page_table_walk_refill() in commit 4e999bf419 we replaced the execution mode and forced it to kernel mode. Restore the previous behavior to also get supervisor / user modes.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2470

Fixes 4e999bf419 ("target/mips: Pass ptw_mmu_idx down from mips_cpu_tlb_fill")

---
  target/mips/tcg/sysemu/tlb_helper.c | 5 +++--

  1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 16 lines

target/mips/tcg/sysemu/tlb_helper.c bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 945

Quoted 13 lines
target/mips/tcg/sysemu/tlb_helper.c +3 -2
index 3ba6d369a6..e7ae4f0bef 100644
@@ -940,8 +940,9 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
            * Memory reads during hardware page table walking are performed
            * as if they were kernel-mode load instructions.
            */
-        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL ?
-                           MMU_ERL_IDX : MMU_KERNEL_IDX);
+        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL)
+                          ? MMU_ERL_IDX
+                          : (env->hflags & MIPS_HFLAG_KSU);

This contradicts the comment above.
If this code change is correct, then the comment isn't.

Quoted 8 lines

OK.

Quoted 1 line

But the comment certainly makes sense -- page tables are never accessible to user mode.

But we are still dropping the supervisor mode, so OK if I reword as:

"Restore the previous behavior to also get supervisor modes."

The old code

- env->hflags &= ~MIPS_HFLAG_KSU;

drops both supervisor and user bits, so my comment still stands.

r~

#4 lore raw
Quoted 41 lines · Richard Henderson
Quoted 40 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Richard Henderson
Quoted 22 lines · Philippe Mathieu-Daudé

When refactoring page_table_walk_refill() in commit 4e999bf419 we replaced the execution mode and forced it to kernel mode. Restore the previous behavior to also get supervisor / user modes.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2470

Fixes 4e999bf419 ("target/mips: Pass ptw_mmu_idx down from mips_cpu_tlb_fill")

---
  target/mips/tcg/sysemu/tlb_helper.c | 5 +++--

  1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/tcg/sysemu/tlb_helper.c +0 -0
index 3ba6d369a6..e7ae4f0bef 100644
@@ -940,8 +940,9 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr 
 address, int size,
            * Memory reads during hardware page table walking are performed
            * as if they were kernel-mode load instructions.
            */
Quoted 8 lines

target/mips/tcg/sysemu/tlb_helper.c bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 945

Quoted 5 lines
target/mips/tcg/sysemu/tlb_helper.c +3 -2
-        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL ?
-                           MMU_ERL_IDX : MMU_KERNEL_IDX);
+        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL)
+                          ? MMU_ERL_IDX
+                          : (env->hflags & MIPS_HFLAG_KSU);

This contradicts the comment above.
If this code change is correct, then the comment isn't.

OK.

Quoted 1 line

But the comment certainly makes sense -- page tables are never accessible to user mode.

But we are still dropping the supervisor mode, so OK if I reword as:

Quoted 7 lines
Quoted 1 line

"Restore the previous behavior to also get supervisor modes."

The old code

-        env->hflags &= ~MIPS_HFLAG_KSU;

drops both supervisor and user bits, so my comment still stands.

Right, I missed that.

The issue is in get_pte(), we have:

page_table_walk_refill()
-> get_pte()

     -> cpu_ld[lq]_code()
        -> cpu_mmu_index()

Since we don't mask anymore the modes in hflags, cpu_mmu_index() can return UM or SM.

I'll respin the fix.

Phil.

Reviewed-by
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines · 在2024年8月11日八月 下午5:54,Philippe Mathieu-Daudé写道:

When refactoring page_table_walk_refill() in commit 4e999bf419 we replaced the execution mode and forced it to kernel mode. Restore the previous behavior to also get supervisor / user modes.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2470

Fixes 4e999bf419 ("target/mips: Pass ptw_mmu_idx down from mips_cpu_tlb_fill")

Thanks!

Quoted 25 lines
---
 target/mips/tcg/sysemu/tlb_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/tcg/sysemu/tlb_helper.c +0 -0
index 3ba6d369a6..e7ae4f0bef 100644
@@ -940,8 +940,9 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, 
 int size,
          * Memory reads during hardware page table walking are 

performed
* as if they were kernel-mode load instructions. */

target/mips/tcg/sysemu/tlb_helper.c +3 -2
-        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL ?
-                           MMU_ERL_IDX : MMU_KERNEL_IDX);
+        int ptw_mmu_idx = (env->hflags & MIPS_HFLAG_ERL)
+                          ? MMU_ERL_IDX
+                          : (env->hflags & MIPS_HFLAG_KSU);
 
         if (page_table_walk_refill(env, address, ptw_mmu_idx)) {
             ret = get_physical_address(env, &physical, &prot, address,
Signature · 1 line
2.45.2
Signature · 1 line
- Jiaxun