Skip to contentSkip to conversation

Add TCG sanity checks (goto_tb related)

PATCH Applied Max Filippov 3 participants 6 messages Sep 21, 2012 – Sep 22, 2012

lore
Share & tools
lore
#0 lore raw

cover Add TCG sanity checks (goto_tb related)

Does this look sane or should it better be merged with e.g. tcg_dump_ops?

Max Filippov (2):
tcg/README: document tcg_gen_goto_tb restrictions
tcg: add TB sanity checking

 tcg/README |    3 +-
 tcg/tcg.c  |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletions(-)
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH 1/2 tcg/README: document tcg_gen_goto_tb restrictions

See
http://lists.nongnu.org/archive/html/qemu-devel/2012-09/msg03196.html
for the whole story.

---
 tcg/README |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
tcg/README +2 -1
index cfdfd96..86b43f1 100644
@@ -386,7 +386,8 @@ Exit the current TB and return the value t0 (word type).
 
 Exit the current TB and jump to the TB index 'index' (constant) if the
 current TB was linked to this TB. Otherwise execute the next
-instructions.
+instructions. Only indices 0 and 1 are valid and tcg_gen_goto_tb may be issued
+at most once with each slot index per TB.
 
 * qemu_ld8u t0, t1, flags
 qemu_ld8s t0, t1, flags
Signature · 1 line
1.7.7.6
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] tcg/README: document tcg_gen_goto_tb restrictions

Quoted 26 lines · Max Filippov

See
http://lists.nongnu.org/archive/html/qemu-devel/2012-09/msg03196.html
for the whole story.

---
 tcg/README |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
tcg/README +2 -1
index cfdfd96..86b43f1 100644
@@ -386,7 +386,8 @@ Exit the current TB and return the value t0 (word type).
 
 Exit the current TB and jump to the TB index 'index' (constant) if the
 current TB was linked to this TB. Otherwise execute the next
-instructions.
+instructions. Only indices 0 and 1 are valid and tcg_gen_goto_tb may be issued
+at most once with each slot index per TB.
 
 * qemu_ld8u t0, t1, flags
 qemu_ld8s t0, t1, flags
Signature · 1 line
1.7.7.6

Thanks, applied.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#3 lore raw

PATCH 2/2 tcg: add TB sanity checking

In reply to Max Filippov

Do a sanity checking pass on the intermediate code.
Check that goto_tb indices are either 0 or 1 and used at most once per TB.

---
 tcg/tcg.c |   69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
tcg/tcg.c +69 -0
index b8a1bec..cdd1975 100644
@@ -1454,6 +1454,71 @@ static void check_regs(TCGContext *s)
 }
 #endif
 
+#ifdef CONFIG_DEBUG_TCG
+static void tcg_sanity_check(TCGContext *s)
+{
+    const uint16_t *opc_ptr;
+    const TCGArg *args;
+    TCGArg arg;
+    TCGOpcode c;
+    int nb_oargs, nb_iargs, nb_cargs, error_count = 0;
+    const TCGOpDef *def;
+    unsigned goto_tb_slots[2] = {0};
+
+    opc_ptr = gen_opc_buf;
+    args = gen_opparam_buf;
+    while (opc_ptr < gen_opc_ptr) {
+        c = *opc_ptr++;
+        def = &tcg_op_defs[c];
+        if (c == INDEX_op_call) {
+            TCGArg arg;
+
+            /* variable number of arguments */
+            arg = *args++;
+            nb_oargs = arg >> 16;
+            nb_iargs = arg & 0xffff;
+            nb_cargs = def->nb_cargs;
+        } else {
+            if (c == INDEX_op_nopn) {
+                /* variable number of arguments */
+                nb_cargs = *args;
+                nb_oargs = 0;
+                nb_iargs = 0;
+            } else {
+                nb_oargs = def->nb_oargs;
+                nb_iargs = def->nb_iargs;
+                nb_cargs = def->nb_cargs;
+            }
+        }
+
+        switch (c) {
+        case INDEX_op_goto_tb:
+            arg = args[0];
+            if (arg != 0 && arg != 1) {
+                qemu_log("TB ERROR: wrong goto_tb slot index: %"TCG_PRIlx"\n",
+                        arg);
+                ++error_count;
+            } else {
+                ++goto_tb_slots[arg];
+                if (goto_tb_slots[arg] > 1) {
+                    qemu_log("TB ERROR: multiple goto_tb(%"TCG_PRIlx")\n", arg);
+                    ++error_count;
+                }
+            }
+            break;
+
+        default:
+            break;
+        }
+
+        args += nb_iargs + nb_oargs + nb_cargs;
+    }
+    if (error_count) {
+        qemu_log("\n");
+    }
+}
+#endif
+
 static void temp_allocate_frame(TCGContext *s, int temp)
 {
     TCGTemp *ts;
@@ -2082,6 +2147,10 @@ static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
     }
 #endif
 
+#ifdef CONFIG_DEBUG_TCG
+    tcg_sanity_check(s);
+#endif
+
     tcg_reg_alloc_start(s);
 
     s->code_buf = gen_code_buf;
Signature · 1 line
1.7.7.6
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] tcg: add TB sanity checking

tcg/tcg.c static void check_regs(TCGContext *s) · line 1458

Quoted 4 lines · Max Filippov
tcg/tcg.c +2 -0
 
+#ifdef CONFIG_DEBUG_TCG
+static void tcg_sanity_check(TCGContext *s)

#ifndef CONFIG_DEBUG_TCG
#define tcg_sanity_check(s) /*empty*/
#else
static void tcg_sanity_check(TCGContext *s)

Quoted 1 line

+{

[]

tcg/tcg.c static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf, · line 2149

Quoted 7 lines
tcg/tcg.c +5 -0
+}
+#endif
@@ -2082,6 +2147,10 @@ static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
+#ifdef CONFIG_DEBUG_TCG
+    tcg_sanity_check(s);
+#endif

And here we can drop the #ifdef. FWIW.

/mjt

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] tcg: add TB sanity checking

In reply to Max Filippov
Quoted 86 lines · Max Filippov

Do a sanity checking pass on the intermediate code.
Check that goto_tb indices are either 0 or 1 and used at most once per TB.

---
 tcg/tcg.c |   69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
tcg/tcg.c +65 -0
index b8a1bec..cdd1975 100644
@@ -1454,6 +1454,71 @@ static void check_regs(TCGContext *s)
 }
 #endif
 
+#ifdef CONFIG_DEBUG_TCG
+static void tcg_sanity_check(TCGContext *s)
+{
+    const uint16_t *opc_ptr;
+    const TCGArg *args;
+    TCGArg arg;
+    TCGOpcode c;
+    int nb_oargs, nb_iargs, nb_cargs, error_count = 0;
+    const TCGOpDef *def;
+    unsigned goto_tb_slots[2] = {0};
+
+    opc_ptr = gen_opc_buf;
+    args = gen_opparam_buf;
+    while (opc_ptr < gen_opc_ptr) {
+        c = *opc_ptr++;
+        def = &tcg_op_defs[c];
+        if (c == INDEX_op_call) {
+            TCGArg arg;
+
+            /* variable number of arguments */
+            arg = *args++;
+            nb_oargs = arg >> 16;
+            nb_iargs = arg & 0xffff;
+            nb_cargs = def->nb_cargs;
+        } else {
+            if (c == INDEX_op_nopn) {
+                /* variable number of arguments */
+                nb_cargs = *args;
+                nb_oargs = 0;
+                nb_iargs = 0;
+            } else {
+                nb_oargs = def->nb_oargs;
+                nb_iargs = def->nb_iargs;
+                nb_cargs = def->nb_cargs;
+            }
+        }
+
+        switch (c) {
+        case INDEX_op_goto_tb:
+            arg = args[0];
+            if (arg != 0 && arg != 1) {
+                qemu_log("TB ERROR: wrong goto_tb slot index: %"TCG_PRIlx"\n",
+                        arg);
+                ++error_count;
+            } else {
+                ++goto_tb_slots[arg];
+                if (goto_tb_slots[arg] > 1) {
+                    qemu_log("TB ERROR: multiple goto_tb(%"TCG_PRIlx")\n", arg);
+                    ++error_count;
+                }
+            }
+            break;
+
+        default:
+            break;
+        }
+
+        args += nb_iargs + nb_oargs + nb_cargs;
+    }
+    if (error_count) {
+        qemu_log("\n");
+    }
+}
+#endif
+
 static void temp_allocate_frame(TCGContext *s, int temp)
 {
     TCGTemp *ts;

tcg/tcg.c static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf, · line 2156

Quoted 10 lines
tcg/tcg.c +4 -0
@@ -2082,6 +2147,10 @@ static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
     }
 #endif
 
+#ifdef CONFIG_DEBUG_TCG
+    tcg_sanity_check(s);
+#endif
+
     tcg_reg_alloc_start(s);
 
     s->code_buf = gen_code_buf;

I think this is better address in the patch from Richard Henderson.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net