Skip to contentSkip to conversation

exec: flush the whole TLB if a watchpoint crosses a page boundary

PATCH Applied Alex Bennée 3 participants 5 messages Jun 3, 2020 – Jun 5, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 exec: flush the whole TLB if a watchpoint crosses a page boundary

There is no particular reason why you can't have a watchpoint in TCG that covers a large chunk of the address space. We could be clever about it but these cases are pretty rare and we can assume the user will expect a little performance degradation.

NB: In my testing gdb will silently squash a watchpoint like:

watch (char[0x7fffffffff]) *0x0

to a 4 byte watchpoint. Practically it will limit the maximum size based on max-value-size. However given enough of a tweak the sky is the limit.

---
v2
- use cleaner in_page = -(addr | TARGET_PAGE_MASK) logic per rth

---
 exec.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
exec.c +7 -1
index 5162f0d12f9..65a4376df37 100644
@@ -1036,6 +1036,7 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
                           int flags, CPUWatchpoint **watchpoint)
 {
     CPUWatchpoint *wp;
+    vaddr in_page;
 
     /* forbid ranges which are empty or run off the end of the address space */
     if (len == 0 || (addr + len - 1) < addr) {
@@ -1056,7 +1057,12 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
         QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
     }
 
-    tlb_flush_page(cpu, addr);
+    in_page = -(addr | TARGET_PAGE_MASK);
+    if (len <= in_page) {
+        tlb_flush_page(cpu, addr);
+    } else {
+        tlb_flush(cpu);
+    }
 
     if (watchpoint)
         *watchpoint = wp;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 13 lines · Alex Bennée

There is no particular reason why you can't have a watchpoint in TCG that covers a large chunk of the address space. We could be clever about it but these cases are pretty rare and we can assume the user will expect a little performance degradation.

NB: In my testing gdb will silently squash a watchpoint like:

watch (char[0x7fffffffff]) *0x0

to a 4 byte watchpoint. Practically it will limit the maximum size based on max-value-size. However given enough of a tweak the sky is the limit.

Quoted 6 lines

---
v2
- use cleaner in_page = -(addr | TARGET_PAGE_MASK) logic per rth

Can we have a macro for this?
Maybe QEMU_IN_PAGE_OFFSET(addr, TARGET_PAGE_MASK)? or QEMU_OFFSET_IN_PAGE()...

Quoted 31 lines
---
 exec.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
exec.c +7 -1
index 5162f0d12f9..65a4376df37 100644
@@ -1036,6 +1036,7 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
                           int flags, CPUWatchpoint **watchpoint)
 {
     CPUWatchpoint *wp;
+    vaddr in_page;
 
     /* forbid ranges which are empty or run off the end of the address space */
     if (len == 0 || (addr + len - 1) < addr) {
@@ -1056,7 +1057,12 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
         QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
     }
 
-    tlb_flush_page(cpu, addr);
+    in_page = -(addr | TARGET_PAGE_MASK);
+    if (len <= in_page) {
+        tlb_flush_page(cpu, addr);
+    } else {
+        tlb_flush(cpu);
+    }
 
     if (watchpoint)
         *watchpoint = wp;
#2 lore raw
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Alex Bennée

There is no particular reason why you can't have a watchpoint in TCG that covers a large chunk of the address space. We could be clever about it but these cases are pretty rare and we can assume the user will expect a little performance degradation.

NB: In my testing gdb will silently squash a watchpoint like:

watch (char[0x7fffffffff]) *0x0

to a 4 byte watchpoint. Practically it will limit the maximum size based on max-value-size. However given enough of a tweak the sky is the limit.

Quoted 7 lines
Quoted 3 lines

---
v2
- use cleaner in_page = -(addr | TARGET_PAGE_MASK) logic per rth

Can we have a macro for this?
Maybe QEMU_IN_PAGE_OFFSET(addr, TARGET_PAGE_MASK)? or QEMU_OFFSET_IN_PAGE()...

As this is queued, I suppose the implicit answer is "no."

Quoted 33 lines
Quoted 32 lines
---
 exec.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
exec.c +7 -1
index 5162f0d12f9..65a4376df37 100644
@@ -1036,6 +1036,7 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
                           int flags, CPUWatchpoint **watchpoint)
 {
     CPUWatchpoint *wp;
+    vaddr in_page;
 
     /* forbid ranges which are empty or run off the end of the address space */
     if (len == 0 || (addr + len - 1) < addr) {
@@ -1056,7 +1057,12 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
         QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
     }
 
-    tlb_flush_page(cpu, addr);
+    in_page = -(addr | TARGET_PAGE_MASK);
+    if (len <= in_page) {
+        tlb_flush_page(cpu, addr);
+    } else {
+        tlb_flush(cpu);
+    }
 
     if (watchpoint)
         *watchpoint = wp;
#3 lore raw
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Alex Bennée

There is no particular reason why you can't have a watchpoint in TCG that covers a large chunk of the address space. We could be clever about it but these cases are pretty rare and we can assume the user will expect a little performance degradation.

NB: In my testing gdb will silently squash a watchpoint like:

watch (char[0x7fffffffff]) *0x0

to a 4 byte watchpoint. Practically it will limit the maximum size based on max-value-size. However given enough of a tweak the sky is the limit.

---
v2
- use cleaner in_page = -(addr | TARGET_PAGE_MASK) logic per rth

Quoted 5 lines
Quoted 3 lines

Can we have a macro for this?
Maybe QEMU_IN_PAGE_OFFSET(addr, TARGET_PAGE_MASK)? or QEMU_OFFSET_IN_PAGE()...

As this is queued, I suppose the implicit answer is "no."

Richard took it into tcg/next as is. I think having a macro may well be nice clean-up but I struggled to pick a good include location so left it for a future clean-up series ;-)

Signature · 1 line
Alex Bennée
Reviewed-by on v2 1/1 Applied
#4 lore raw
In reply to Alex Bennée
Quoted 16 lines · Alex Bennée

There is no particular reason why you can't have a watchpoint in TCG that covers a large chunk of the address space. We could be clever about it but these cases are pretty rare and we can assume the user will expect a little performance degradation.

NB: In my testing gdb will silently squash a watchpoint like:

watch (char[0x7fffffffff]) *0x0

to a 4 byte watchpoint. Practically it will limit the maximum size based on max-value-size. However given enough of a tweak the sky is the limit.

Quoted 6 lines

---
v2
- use cleaner in_page = -(addr | TARGET_PAGE_MASK) logic per rth

---
 exec.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Queued to tcg-next.

r~