Skip to contentSkip to conversation

target-i386: Clean up DR7 breakpoint handling

PATCH In review Andreas Färber 2 participants 7 messages Jan 15, 2013

lore
Share & tools
lore
#0 lore raw

v6 cover target-i386: Clean up DR7 breakpoint handling

Hello Guang,

Are you okay with this version?

Regards,
Andreas

v5 -> v6:
* Fix bisectability by deferring use of hw_{local,global}_breakpoint_enabled().
* Reword the commit messages for clarity.
* Squash more constant usage into first patch.
* Reorder DATA_RW and DATA_WR to match original switch cases.
* Untangle introduction of breakpoint helper functions from goto/if refactorings.
* Make hw_breakpoint_enabled() return bool.
* Keep IO_RW in place for patch readability.
* Keep reg variable name for patch readability.
* Move {bp,wp}_match inside loop to clarify scope and to avoid double false init.
* Make check_hw_breakpoints() return bool, change force_dr6_update arg to bool.

changes v4->v5:

- fix some not well formated changes.
- split functional and non-functional changes for cherry-picking suggested by Andreas Färber <[email protected]>

changes v3->v4:

- fix wrong logic of hw_{global,local}_breakpoint_enabled usage suggested by Peter Maydell <[email protected]>

changes v2->v3:

- split hw_breakpoint_enabled into hw_{global,local}_breakpoint_enabled

changes v1->v2:

- add _TYPE_ to the name of dr7 bit field
- fix some coding styles
suggested by Peter Maydell <[email protected]>

Cc liguang <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Peter Maydell <[email protected]>

liguang (4):
target-i386: Define DR7 bit field constants
target-i386: Introduce hw_{local,global}_breakpoint_enabled()
target-i386: Avoid goto in hw_breakpoint_insert()
target-i386: Use switch in check_hw_breakpoints()

 target-i386/cpu.h         |   23 ++++++++++--
 target-i386/helper.c      |   87 +++++++++++++++++++++++++++++----------------
 target-i386/machine.c     |    5 +--
 target-i386/misc_helper.c |    6 ++--
 target-i386/seg_helper.c  |    9 ++---

5 Dateien geändert, 88 Zeilen hinzugefügt(+), 42 Zeilen entfernt(-)

Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v6 1/4 target-i386: Define DR7 bit field constants

From: liguang <[email protected]>

Implicit use of dr7 bit field is a little hard to understand, so define constants for them and use them consistently.

---
 target-i386/cpu.h         |    6 ++++++
 target-i386/helper.c      |   18 +++++++++---------
 target-i386/machine.c     |    5 +++--
 target-i386/misc_helper.c |    4 ++--
 target-i386/seg_helper.c  |    6 +++---

5 Dateien geändert, 23 Zeilen hinzugefügt(+), 16 Zeilen entfernt(-)

target-i386/cpu.h +6 -0
index e4a7c50..6682022 100644
@@ -231,6 +231,12 @@
 #define DR7_TYPE_SHIFT  16
 #define DR7_LEN_SHIFT   18
 #define DR7_FIXED_1     0x00000400
+#define DR7_LOCAL_BP_MASK    0x55
+#define DR7_MAX_BP           4
+#define DR7_TYPE_BP_INST     0x0
+#define DR7_TYPE_DATA_WR     0x1
+#define DR7_TYPE_IO_RW       0x2
+#define DR7_TYPE_DATA_RW     0x3
 
 #define PG_PRESENT_BIT	0
 #define PG_RW_BIT	1
target-i386/helper.c +9 -9
index fa622e1..1fceb91 100644
@@ -969,18 +969,18 @@ void hw_breakpoint_insert(CPUX86State *env, int index)
     int type, err = 0;
 
     switch (hw_breakpoint_type(env->dr[7], index)) {
-    case 0:
+    case DR7_TYPE_BP_INST:
         if (hw_breakpoint_enabled(env->dr[7], index))
             err = cpu_breakpoint_insert(env, env->dr[index], BP_CPU,
                                         &env->cpu_breakpoint[index]);
         break;
-    case 1:
+    case DR7_TYPE_DATA_WR:
         type = BP_CPU | BP_MEM_WRITE;
         goto insert_wp;
-    case 2:
+    case DR7_TYPE_IO_RW:
          /* No support for I/O watchpoints yet */
         break;
-    case 3:
+    case DR7_TYPE_DATA_RW:
         type = BP_CPU | BP_MEM_ACCESS;
     insert_wp:
         err = cpu_watchpoint_insert(env, env->dr[index],
@@ -997,15 +997,15 @@ void hw_breakpoint_remove(CPUX86State *env, int index)
     if (!env->cpu_breakpoint[index])
         return;
     switch (hw_breakpoint_type(env->dr[7], index)) {
-    case 0:
+    case DR7_TYPE_BP_INST:
         if (hw_breakpoint_enabled(env->dr[7], index))
             cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
         break;
-    case 1:
-    case 3:
+    case DR7_TYPE_DATA_WR:
+    case DR7_TYPE_DATA_RW:
         cpu_watchpoint_remove_by_ref(env, env->cpu_watchpoint[index]);
         break;
-    case 2:
+    case DR7_TYPE_IO_RW:
         /* No support for I/O watchpoints yet */
         break;
     }
@@ -1018,7 +1018,7 @@ int check_hw_breakpoints(CPUX86State *env, int force_dr6_update)
     int hit_enabled = 0;
 
     dr6 = env->dr[6] & ~0xf;
-    for (reg = 0; reg < 4; reg++) {
+    for (reg = 0; reg < DR7_MAX_BP; reg++) {
         type = hw_breakpoint_type(env->dr[7], reg);
         if ((type == 0 && env->dr[reg] == env->eip) ||
             ((type & 1) && env->cpu_watchpoint[reg] &&
target-i386/machine.c +3 -2
index 8354572..8df6a6b 100644
@@ -265,10 +265,11 @@ static int cpu_post_load(void *opaque, int version_id)
 
     cpu_breakpoint_remove_all(env, BP_CPU);
     cpu_watchpoint_remove_all(env, BP_CPU);
-    for (i = 0; i < 4; i++)
+    for (i = 0; i < DR7_MAX_BP; i++) {
         hw_breakpoint_insert(env, i);
-
+    }
     tlb_flush(env, 1);
+
     return 0;
 }
 
target-i386/misc_helper.c +2 -2
index 719cacd..b3f4e4f 100644
@@ -197,11 +197,11 @@ void helper_movl_drN_T0(CPUX86State *env, int reg, target_ulong t0)
         env->dr[reg] = t0;
         hw_breakpoint_insert(env, reg);
     } else if (reg == 7) {
-        for (i = 0; i < 4; i++) {
+        for (i = 0; i < DR7_MAX_BP; i++) {
             hw_breakpoint_remove(env, i);
         }
         env->dr[7] = t0;
-        for (i = 0; i < 4; i++) {
+        for (i = 0; i < DR7_MAX_BP; i++) {
             hw_breakpoint_insert(env, i);
         }
     } else {
target-i386/seg_helper.c +3 -3
index c2a99ee..c40bd96 100644
@@ -465,13 +465,13 @@ static void switch_tss(CPUX86State *env, int tss_selector,
 
 #ifndef CONFIG_USER_ONLY
     /* reset local breakpoints */
-    if (env->dr[7] & 0x55) {
-        for (i = 0; i < 4; i++) {
+    if (env->dr[7] & DR7_LOCAL_BP_MASK) {
+        for (i = 0; i < DR7_MAX_BP; i++) {
             if (hw_breakpoint_enabled(env->dr[7], i) == 0x1) {
                 hw_breakpoint_remove(env, i);
             }
         }
-        env->dr[7] &= ~0x55;
+        env->dr[7] &= ~DR7_LOCAL_BP_MASK;
     }
 #endif
 }
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v6 2/4 target-i386: Introduce hw_{local, global}_breakpoint_enabled()

In reply to Andreas Färber

From: liguang <[email protected]>

hw_breakpoint_enabled() returned a bit field indicating whether a local breakpoint and/or global breakpoint was enabled. Avoid this number magic by using explicit boolean helper functions hw_local_breakpoint_enabled() and hw_global_breakpoint_enabled(), to aid readability.

Reuse them for the hw_breakpoint_enabled() implementation and change its return type to bool.

While at it, fix Coding Style issues (missing braces).

---
 target-i386/cpu.h        |   15 +++++++++++++--
 target-i386/helper.c     |    9 ++++++---
 target-i386/seg_helper.c |    3 ++-

3 Dateien geändert, 21 Zeilen hinzugefügt(+), 6 Zeilen entfernt(-)

target-i386/cpu.h +13 -2
index 6682022..1e850a7 100644
@@ -1014,9 +1014,20 @@ int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
 #define cpu_handle_mmu_fault cpu_x86_handle_mmu_fault
 void cpu_x86_set_a20(CPUX86State *env, int a20_state);
 
-static inline int hw_breakpoint_enabled(unsigned long dr7, int index)
+static inline bool hw_local_breakpoint_enabled(unsigned long dr7, int index)
 {
-    return (dr7 >> (index * 2)) & 3;
+    return (dr7 >> (index * 2)) & 1;
+}
+
+static inline bool hw_global_breakpoint_enabled(unsigned long dr7, int index)
+{
+    return (dr7 >> (index * 2)) & 2;
+
+}
+static inline bool hw_breakpoint_enabled(unsigned long dr7, int index)
+{
+    return hw_global_breakpoint_enabled(dr7, index) ||
+           hw_local_breakpoint_enabled(dr7, index);
 }
 
 static inline int hw_breakpoint_type(unsigned long dr7, int index)
target-i386/helper.c +6 -3
index 1fceb91..ebdd6a5 100644
@@ -970,9 +970,10 @@ void hw_breakpoint_insert(CPUX86State *env, int index)
 
     switch (hw_breakpoint_type(env->dr[7], index)) {
     case DR7_TYPE_BP_INST:
-        if (hw_breakpoint_enabled(env->dr[7], index))
+        if (hw_breakpoint_enabled(env->dr[7], index)) {
             err = cpu_breakpoint_insert(env, env->dr[index], BP_CPU,
                                         &env->cpu_breakpoint[index]);
+        }
         break;
     case DR7_TYPE_DATA_WR:
         type = BP_CPU | BP_MEM_WRITE;
@@ -998,8 +999,9 @@ void hw_breakpoint_remove(CPUX86State *env, int index)
         return;
     switch (hw_breakpoint_type(env->dr[7], index)) {
     case DR7_TYPE_BP_INST:
-        if (hw_breakpoint_enabled(env->dr[7], index))
+        if (hw_breakpoint_enabled(env->dr[7], index)) {
             cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
+        }
         break;
     case DR7_TYPE_DATA_WR:
     case DR7_TYPE_DATA_RW:
@@ -1024,8 +1026,9 @@ int check_hw_breakpoints(CPUX86State *env, int force_dr6_update)
             ((type & 1) && env->cpu_watchpoint[reg] &&
              (env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT))) {
             dr6 |= 1 << reg;
-            if (hw_breakpoint_enabled(env->dr[7], reg))
+            if (hw_breakpoint_enabled(env->dr[7], reg)) {
                 hit_enabled = 1;
+            }
         }
     }
     if (hit_enabled || force_dr6_update)
target-i386/seg_helper.c +2 -1
index c40bd96..3247dee 100644
@@ -467,7 +467,8 @@ static void switch_tss(CPUX86State *env, int tss_selector,
     /* reset local breakpoints */
     if (env->dr[7] & DR7_LOCAL_BP_MASK) {
         for (i = 0; i < DR7_MAX_BP; i++) {
-            if (hw_breakpoint_enabled(env->dr[7], i) == 0x1) {
+            if (hw_local_breakpoint_enabled(env->dr[7], i) &&
+                !hw_global_breakpoint_enabled(env->dr[7], i)) {
                 hw_breakpoint_remove(env, i);
             }
         }
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v6 3/4 target-i386: Avoid goto in hw_breakpoint_insert()

In reply to Andreas Färber

From: liguang <[email protected]>

"Go To Statement Considered Harmful" -- E. Dijkstra

To avoid an unnecessary goto within the switch statement, move watchpoint insertion out of the switch statement. Improves readability.

While at it, fix Coding Style issues (missing braces, indentation).

---
 target-i386/helper.c |   16 ++++++++++------

1 Datei geändert, 10 Zeilen hinzugefügt(+), 6 Zeilen entfernt(-)

target-i386/helper.c +10 -6
index ebdd6a5..a10b562 100644
@@ -966,7 +966,7 @@ hwaddr cpu_get_phys_page_debug(CPUX86State *env, target_ulong addr)
 
 void hw_breakpoint_insert(CPUX86State *env, int index)
 {
-    int type, err = 0;
+    int type = 0, err = 0;
 
     switch (hw_breakpoint_type(env->dr[7], index)) {
     case DR7_TYPE_BP_INST:
@@ -977,20 +977,24 @@ void hw_breakpoint_insert(CPUX86State *env, int index)
         break;
     case DR7_TYPE_DATA_WR:
         type = BP_CPU | BP_MEM_WRITE;
-        goto insert_wp;
+        break;
     case DR7_TYPE_IO_RW:
-         /* No support for I/O watchpoints yet */
+        /* No support for I/O watchpoints yet */
         break;
     case DR7_TYPE_DATA_RW:
         type = BP_CPU | BP_MEM_ACCESS;
-    insert_wp:
+        break;
+    }
+
+    if (type != 0) {
         err = cpu_watchpoint_insert(env, env->dr[index],
                                     hw_breakpoint_len(env->dr[7], index),
                                     type, &env->cpu_watchpoint[index]);
-        break;
     }
-    if (err)
+
+    if (err) {
         env->cpu_breakpoint[index] = NULL;
+    }
 }
 
 void hw_breakpoint_remove(CPUX86State *env, int index)
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH v6 4/4 target-i386: Use switch in check_hw_breakpoints()

In reply to Andreas Färber

From: liguang <[email protected]>

Replace an if statement using magic numbers for breakpoint type with a more explicit switch statement. This is to aid readability.

Change the return type and force_dr6_update argument type to bool.

While at it, fix Coding Style issues (missing braces).

---
 target-i386/cpu.h         |    2 +-
 target-i386/helper.c      |   44 ++++++++++++++++++++++++++++++++------------
 target-i386/misc_helper.c |    2 +-

3 Dateien geändert, 34 Zeilen hinzugefügt(+), 14 Zeilen entfernt(-)

target-i386/cpu.h +1 -1
index 1e850a7..4e091cd 100644
@@ -1043,7 +1043,7 @@ static inline int hw_breakpoint_len(unsigned long dr7, int index)
 
 void hw_breakpoint_insert(CPUX86State *env, int index);
 void hw_breakpoint_remove(CPUX86State *env, int index);
-int check_hw_breakpoints(CPUX86State *env, int force_dr6_update);
+bool check_hw_breakpoints(CPUX86State *env, bool force_dr6_update);
 void breakpoint_handler(CPUX86State *env);
 
 /* will be suppressed */
target-i386/helper.c +32 -12

74 lines not on this page · raw message

target-i386/misc_helper.c +1 -1
index b3f4e4f..b6d5740 100644
@@ -110,7 +110,7 @@ void helper_into(CPUX86State *env, int next_eip_addend)
 void helper_single_step(CPUX86State *env)
 {
 #ifndef CONFIG_USER_ONLY
-    check_hw_breakpoints(env, 1);
+    check_hw_breakpoints(env, true);
     env->dr[6] |= DR6_BS;
 #endif
     raise_exception(env, EXCP01_DB);
Signature · 1 line
1.7.10.4
#5 lore raw
In reply to Andreas Färber

Thanks for your smooth work!

fine for me.

Quoted 59 lines · 在 2013-01-15二的 09:29 +0100,Andreas Färber写道:

Hello Guang,

Are you okay with this version?

Regards,
Andreas

v5 -> v6:
* Fix bisectability by deferring use of hw_{local,global}_breakpoint_enabled().
* Reword the commit messages for clarity.
* Squash more constant usage into first patch.
* Reorder DATA_RW and DATA_WR to match original switch cases.
* Untangle introduction of breakpoint helper functions from goto/if refactorings.
* Make hw_breakpoint_enabled() return bool.
* Keep IO_RW in place for patch readability.
* Keep reg variable name for patch readability.
* Move {bp,wp}_match inside loop to clarify scope and to avoid double false init.
* Make check_hw_breakpoints() return bool, change force_dr6_update arg to bool.

changes v4->v5:

- fix some not well formated changes.
- split functional and non-functional changes for cherry-picking suggested by Andreas Färber <[email protected]>

changes v3->v4:

- fix wrong logic of hw_{global,local}_breakpoint_enabled usage suggested by Peter Maydell <[email protected]>

changes v2->v3:

- split hw_breakpoint_enabled into hw_{global,local}_breakpoint_enabled

changes v1->v2:

- add _TYPE_ to the name of dr7 bit field
- fix some coding styles
suggested by Peter Maydell <[email protected]>

Cc liguang <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Peter Maydell <[email protected]>

liguang (4):
target-i386: Define DR7 bit field constants
target-i386: Introduce hw_{local,global}_breakpoint_enabled()
target-i386: Avoid goto in hw_breakpoint_insert()
target-i386: Use switch in check_hw_breakpoints()

 target-i386/cpu.h         |   23 ++++++++++--
 target-i386/helper.c      |   87 +++++++++++++++++++++++++++++----------------
 target-i386/machine.c     |    5 +--
 target-i386/misc_helper.c |    6 ++--
 target-i386/seg_helper.c  |    9 ++---

5 Dateien geändert, 88 Zeilen hinzugefügt(+), 42 Zeilen entfernt(-)

Signature · 2 lines
regards!
li guang
author Applied
#6 lore raw

Am 15.01.2013 09:49, schrieb li guang:

Quoted 3 lines

Thanks for your smooth work!

fine for me.

Thanks, applied to qom-cpu:
https://github.com/afaerber/qemu-cpu/commits/qom-cpu

Andreas

Quoted 61 lines · 在 2013-01-15二的 09:29 +0100,Andreas Färber写道:
Quoted 59 lines

Hello Guang,

Are you okay with this version?

Regards,
Andreas

v5 -> v6:
* Fix bisectability by deferring use of hw_{local,global}_breakpoint_enabled().
* Reword the commit messages for clarity.
* Squash more constant usage into first patch.
* Reorder DATA_RW and DATA_WR to match original switch cases.
* Untangle introduction of breakpoint helper functions from goto/if refactorings.
* Make hw_breakpoint_enabled() return bool.
* Keep IO_RW in place for patch readability.
* Keep reg variable name for patch readability.
* Move {bp,wp}_match inside loop to clarify scope and to avoid double false init.
* Make check_hw_breakpoints() return bool, change force_dr6_update arg to bool.

changes v4->v5:

- fix some not well formated changes.
- split functional and non-functional changes for cherry-picking suggested by Andreas Färber <[email protected]>

changes v3->v4:

- fix wrong logic of hw_{global,local}_breakpoint_enabled usage suggested by Peter Maydell <[email protected]>

changes v2->v3:

- split hw_breakpoint_enabled into hw_{global,local}_breakpoint_enabled

changes v1->v2:

- add _TYPE_ to the name of dr7 bit field
- fix some coding styles
suggested by Peter Maydell <[email protected]>

Cc liguang <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Peter Maydell <[email protected]>

liguang (4):
target-i386: Define DR7 bit field constants
target-i386: Introduce hw_{local,global}_breakpoint_enabled()
target-i386: Avoid goto in hw_breakpoint_insert()
target-i386: Use switch in check_hw_breakpoints()

 target-i386/cpu.h         |   23 ++++++++++--
 target-i386/helper.c      |   87 +++++++++++++++++++++++++++++----------------
 target-i386/machine.c     |    5 +--
 target-i386/misc_helper.c |    6 ++--
 target-i386/seg_helper.c  |    9 ++---

5 Dateien geändert, 88 Zeilen hinzugefügt(+), 42 Zeilen entfernt(-)

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg