Skip to contentSkip to conversation

Xtensa misuse of tb_invalidate_phys_page_range()?

Qemu-devel New Avi Kivity 2 participants 6 messages Mar 19, 2012 – Mar 25, 2012

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

void HELPER(wsr_ibreaka)(uint32_t i, uint32_t v)

{
    if (env->sregs[IBREAKENABLE] & (1 << i) && env->sregs[IBREAKA + i]
!= v) {
        tb_invalidate_phys_page_range(
                env->sregs[IBREAKA + i], env->sregs[IBREAKA + i] + 1, 0);
        tb_invalidate_phys_page_range(v, v + 1, 0);
    }
    env->sregs[IBREAKA + i] = v;
}

tb_invalidate_phys_page_range() expects a virtual address in user mode, and a ram_addr_t in system mode. I'm guessing that v is a virtual address?

This needs to be fixed for system mode if so (and in any case - even if it's a physical address, it needs to be translated to a ram_addr_t).

Similar issue for hw/kvmvapic.c.

Signature · 1 line
error compiling committee.c: too many arguments to function
#1 lore raw
Quoted 11 lines

void HELPER(wsr_ibreaka)(uint32_t i, uint32_t v)
{
   if (env->sregs[IBREAKENABLE] & (1 << i) && env->sregs[IBREAKA + i] != v) {
       tb_invalidate_phys_page_range(
               env->sregs[IBREAKA + i], env->sregs[IBREAKA + i] + 1, 0); tb_invalidate_phys_page_range(v, v + 1, 0);
   }
   env->sregs[IBREAKA + i] = v;
}

Quoted 2 lines

tb_invalidate_phys_page_range() expects a virtual address in user mode, and a ram_addr_t in system mode.  I'm guessing that v is a virtual address?

Yes, it's a virtual address here, as well as in wsr_lbeg/wsr_lend helpers. I made a test for it and it actually fails. I wonder how could it stay unnoticed that long :()

Quoted 2 lines

This needs to be fixed for system mode if so (and in any case - even if it's a physical address, it needs to be translated to a ram_addr_t).

Sure. Will try to fix it, though it's completely unclear to me now how to do it efficiently.

Signature · 2 lines
Thanks.
-- Max
author
#2 lore raw
Quoted 11 lines · Max Filippov
Quoted 11 lines

void HELPER(wsr_ibreaka)(uint32_t i, uint32_t v)

{
   if (env->sregs[IBREAKENABLE] & (1 << i) && env->sregs[IBREAKA + i]
!= v) {
       tb_invalidate_phys_page_range(
               env->sregs[IBREAKA + i], env->sregs[IBREAKA + i] + 1, 0);
       tb_invalidate_phys_page_range(v, v + 1, 0);

}
env->sregs[IBREAKA + i] = v;
}

Quoted 6 lines
Quoted 2 lines

tb_invalidate_phys_page_range() expects a virtual address in user mode, and a ram_addr_t in system mode. I'm guessing that v is a virtual address?

Yes, it's a virtual address here, as well as in wsr_lbeg/wsr_lend helpers. I made a test for it and it actually fails. I wonder how could it stay unnoticed that long :()

There are many silent breakages like that, don't worry.

Quoted 5 lines
Quoted 2 lines

This needs to be fixed for system mode if so (and in any case - even if it's a physical address, it needs to be translated to a ram_addr_t).

Sure. Will try to fix it, though it's completely unclear to me now how to do it efficiently.

Since I'm rewriting this area, don't worry about efficiency. Let's get it correct and after the rewrite we can reexamine efficiency.

I imagine you'll need something like breakpoint_invalidate().

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
Quoted 23 lines
Quoted 11 lines
Quoted 11 lines

void HELPER(wsr_ibreaka)(uint32_t i, uint32_t v)

{
    if (env->sregs[IBREAKENABLE]&  (1<<  i)&&  env->sregs[IBREAKA + i]
!= v) {
        tb_invalidate_phys_page_range(
                env->sregs[IBREAKA + i], env->sregs[IBREAKA + i] + 1, 0);
        tb_invalidate_phys_page_range(v, v + 1, 0);
    }
    env->sregs[IBREAKA + i] = v;
}
Quoted 6 lines
Quoted 2 lines

tb_invalidate_phys_page_range() expects a virtual address in user mode, and a ram_addr_t in system mode. I'm guessing that v is a virtual address?

Yes, it's a virtual address here, as well as in wsr_lbeg/wsr_lend helpers. I made a test for it and it actually fails. I wonder how could it stay unnoticed that long :()

There are many silent breakages like that, don't worry.

Quoted 3 lines
Quoted 2 lines

This needs to be fixed for system mode if so (and in any case - even if it's a physical address, it needs to be translated to a ram_addr_t).

Quoted 7 lines
Quoted 2 lines

Sure. Will try to fix it, though it's completely unclear to me now how to do it efficiently.

Since I'm rewriting this area, don't worry about efficiency. Let's get it correct and after the rewrite we can reexamine efficiency.

I imagine you'll need something like breakpoint_invalidate().

The following RFC patch takes the obvious approach of sharing the breakpoint_invalidate implementation to address this issue. But if we're talking about correctness it's not 100% correct, because guest TLB is independent of both instruction breakpoints and zero overhead loops, and at the moment of TB invalidation relevant TLB mapping may not exist.

Even if we managed to record physical addresses of TBs at the current IBREAKA/LEND, it's not enough because we need to invalidate TBs both at the old and at the new IBREAKA/LEND virtual addresses.

What do you think, do we need yet another address-to-TB map?

Signature · 2 lines
Thanks.
-- Max
#4 lore raw

RFC 1/1 target-xtensa: fix tb invalidation for IBREAK and LOOP

Instruction breakpoint/zero overhead loop handling code is built into TBs pointed to by IBREAKA/LEND SRs. When these or related SRs get changed TBs at virtual addresses corresponding to their old and their new values must be invalidated.

Virtual address range is passed to the tb_invalidate_phys_page_range, which is incorrect in system emulation mode.

To fix it use guest TLB/MMU to translate virtual address to physical address.

However the guest may not have virtual-to-physical mapping at the moment of IBREAKA/LEND change. Looks like only host side virtual address-to-tb mapping similar to one used by the page_find is able to help in this case.

---
 exec-all.h                |    1 +
 exec.c                    |    9 ++++++---
 target-xtensa/op_helper.c |   29 ++++++++++++++++++-----------
 3 files changed, 25 insertions(+), 14 deletions(-)
exec-all.h +1 -0
index 93a5b22..3da16ca 100644
@@ -96,6 +96,7 @@ void QEMU_NORETURN cpu_loop_exit(CPUArchState *env1);
 int page_unprotect(target_ulong address, unsigned long pc, void *puc);
 void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
                                    int is_cpu_write_access);
+void tb_invalidate_phys_addr(target_phys_addr_t addr);
 void tlb_flush_page(CPUArchState *env, target_ulong addr);
 void tlb_flush(CPUArchState *env, int flush_global);
 #if !defined(CONFIG_USER_ONLY)
exec.c +6 -3
index 6731ab8..130ecaa 100644
@@ -1463,13 +1463,11 @@ static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
     tb_invalidate_phys_page_range(pc, pc + 1, 0);
 }
 #else
-static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
+void tb_invalidate_phys_addr(target_phys_addr_t addr)
 {
-    target_phys_addr_t addr;
     ram_addr_t ram_addr;
     MemoryRegionSection *section;
 
-    addr = cpu_get_phys_page_debug(env, pc);
     section = phys_page_find(addr >> TARGET_PAGE_BITS);
     if (!(memory_region_is_ram(section->mr)
           || (section->mr->rom_device && section->mr->readable))) {
@@ -1479,6 +1477,11 @@ static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
         + section_addr(section, addr);
     tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
 }
+
+static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
+{
+    tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc));
+}
 #endif
 #endif /* TARGET_HAS_ICE */
 
target-xtensa/op_helper.c +18 -11
index cdef0db..1b7e51f 100644
@@ -99,6 +99,18 @@ void tlb_fill(CPUXtensaState *env1, target_ulong vaddr, int is_write, int mmu_id
     env = saved_env;
 }
 
+static void tb_invalidate_virtual_addr(CPUXtensaState *env, uint32_t vaddr)
+{
+    uint32_t paddr;
+    uint32_t page_size;
+    unsigned access;
+    int ret = xtensa_get_physical_addr(env, vaddr, 2, 0,
+            &paddr, &page_size, &access);
+    if (ret == 0) {
+        tb_invalidate_phys_addr(paddr);
+    }
+}
+
 void HELPER(exception)(uint32_t excp)
 {
     env->exception_index = excp;
@@ -358,8 +370,7 @@ void HELPER(movsp)(uint32_t pc)
 void HELPER(wsr_lbeg)(uint32_t v)
 {
     if (env->sregs[LBEG] != v) {
-        tb_invalidate_phys_page_range(
-                env->sregs[LEND] - 1, env->sregs[LEND], 0);
+        tb_invalidate_virtual_addr(env, env->sregs[LEND] - 1);
         env->sregs[LBEG] = v;
     }
 }
@@ -367,11 +378,9 @@ void HELPER(wsr_lbeg)(uint32_t v)
 void HELPER(wsr_lend)(uint32_t v)
 {
     if (env->sregs[LEND] != v) {
-        tb_invalidate_phys_page_range(
-                env->sregs[LEND] - 1, env->sregs[LEND], 0);
+        tb_invalidate_virtual_addr(env, env->sregs[LEND] - 1);
         env->sregs[LEND] = v;
-        tb_invalidate_phys_page_range(
-                env->sregs[LEND] - 1, env->sregs[LEND], 0);
+        tb_invalidate_virtual_addr(env, env->sregs[LEND] - 1);
     }
 }
 
@@ -692,8 +701,7 @@ void HELPER(wsr_ibreakenable)(uint32_t v)
 
     for (i = 0; i < env->config->nibreak; ++i) {
         if (change & (1 << i)) {
-            tb_invalidate_phys_page_range(
-                    env->sregs[IBREAKA + i], env->sregs[IBREAKA + i] + 1, 0);
+            tb_invalidate_virtual_addr(env, env->sregs[IBREAKA + i]);
         }
     }
     env->sregs[IBREAKENABLE] = v & ((1 << env->config->nibreak) - 1);
@@ -702,9 +710,8 @@ void HELPER(wsr_ibreakenable)(uint32_t v)
 void HELPER(wsr_ibreaka)(uint32_t i, uint32_t v)
 {
     if (env->sregs[IBREAKENABLE] & (1 << i) && env->sregs[IBREAKA + i] != v) {
-        tb_invalidate_phys_page_range(
-                env->sregs[IBREAKA + i], env->sregs[IBREAKA + i] + 1, 0);
-        tb_invalidate_phys_page_range(v, v + 1, 0);
+        tb_invalidate_virtual_addr(env, env->sregs[IBREAKA + i]);
+        tb_invalidate_virtual_addr(env, v);
     }
     env->sregs[IBREAKA + i] = v;
 }
Signature · 1 line
1.7.7.6
author
#5 lore raw
In reply to Max Filippov
Quoted 9 lines · Max Filippov
Quoted 5 lines

Since I'm rewriting this area, don't worry about efficiency. Let's get it correct and after the rewrite we can reexamine efficiency.

I imagine you'll need something like breakpoint_invalidate().

The following RFC patch takes the obvious approach of sharing the breakpoint_invalidate
implementation to address this issue.

Looks good.

Quoted 7 lines

But if we're talking about correctness it's not
100% correct, because guest TLB is independent of both instruction breakpoints and
zero overhead loops, and at the moment of TB invalidation relevant TLB mapping may not
exist.

Quoted 8 lines

Even if we managed to record physical addresses of TBs at the current IBREAKA/LEND, it's
not enough because we need to invalidate TBs both at the old and at the new IBREAKA/LEND
virtual addresses.

What do you think, do we need yet another address-to-TB map?

No idea what all that means, sorry.

Signature · 1 line
error compiling committee.c: too many arguments to function