Skip to contentSkip to conversation

accel/tcg: Fix guest instruction address in output assembly log

PATCH In review Matt Borgerson 3 participants 4 messages Jul 18, 2023 – Jul 23, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/tcg: Fix guest instruction address in output assembly log

If CF_PCREL is enabled, generated host assembly logging (command line option -d out_asm) may incorrectly report guest instruction virtual addresses as page offsets instead of absolute addresses. This patch corrects the reported guest address.

---
 accel/tcg/translate-all.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
accel/tcg/translate-all.c +20 -2
index a1782db5dd..859db95cf7 100644
@@ -283,6 +283,24 @@ static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
     return tcg_gen_code(tcg_ctx, tb, pc);
 }
 
+static vaddr get_guest_insn_vaddr(TranslationBlock *tb, vaddr pc, size_t insn)
+{
+    g_assert(insn < tb->icount);
+
+    /* FIXME: This replicates the restore_state_to_opc() logic. */
+    vaddr addr = tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS];
+
+    if (tb_cflags(tb) & CF_PCREL) {
+        addr |= (pc & TARGET_PAGE_MASK);
+    } else {
+#if defined(TARGET_I386)
+        addr -= tb->cs_base;
+#endif
+    }
+
+    return addr;
+}
+
 /* Called with mmap_lock held for user mode emulation.  */
 TranslationBlock *tb_gen_code(CPUState *cpu,
                               vaddr pc, uint64_t cs_base,
@@ -458,7 +476,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
             fprintf(logfile, "OUT: [size=%d]\n", gen_code_size);
             fprintf(logfile,
                     "  -- guest addr 0x%016" PRIx64 " + tb prologue\n",
-                    tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS]);
+                    get_guest_insn_vaddr(tb, pc, insn));
             chunk_start = tcg_ctx->gen_insn_end_off[insn];
             disas(logfile, tb->tc.ptr, chunk_start);
 
@@ -471,7 +489,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
                 size_t chunk_end = tcg_ctx->gen_insn_end_off[insn];
                 if (chunk_end > chunk_start) {
                     fprintf(logfile, "  -- guest addr 0x%016" PRIx64 "\n",
-                            tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS]);
+                            get_guest_insn_vaddr(tb, pc, insn));
                     disas(logfile, tb->tc.ptr + chunk_start,
                           chunk_end - chunk_start);
                     chunk_start = chunk_end;
Signature · 1 line
2.34.1
#1 lore raw

Hi Matt,

Quoted 15 lines · Matt Borgerson

If CF_PCREL is enabled, generated host assembly logging (command line option -d out_asm) may incorrectly report guest instruction virtual addresses as page offsets instead of absolute addresses. This patch corrects the reported guest address.

---
  accel/tcg/translate-all.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
accel/tcg/translate-all.c +0 -0
index a1782db5dd..859db95cf7 100644

accel/tcg/translate-all.c static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb, · line 290

Quoted 8 lines
accel/tcg/translate-all.c +5 -0
@@ -283,6 +283,24 @@ static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
      return tcg_gen_code(tcg_ctx, tb, pc);
  }
 
+static vaddr get_guest_insn_vaddr(TranslationBlock *tb, vaddr pc, size_t insn)
+{
+    g_assert(insn < tb->icount);
+
+    /* FIXME: This replicates the restore_state_to_opc() logic. */

Hmmm maybe we could have a public helper defined in accel/tcg/cpu-exec.c so we cat re-use it in accel/tcg/perf.c::perf_report_code().

Quoted 12 lines
accel/tcg/translate-all.c +12 -0
+    vaddr addr = tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS];
+
+    if (tb_cflags(tb) & CF_PCREL) {
+        addr |= (pc & TARGET_PAGE_MASK);
+    } else {
+#if defined(TARGET_I386)
+        addr -= tb->cs_base;
+#endif
+    }
+
+    return addr;
+}
#2 lore raw
In reply to Matt Borgerson
Quoted 22 lines · Matt Borgerson

If CF_PCREL is enabled, generated host assembly logging (command line option -d out_asm) may incorrectly report guest instruction virtual addresses as page offsets instead of absolute addresses. This patch corrects the reported guest address.

---
  accel/tcg/translate-all.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
accel/tcg/translate-all.c +4 -0
index a1782db5dd..859db95cf7 100644
@@ -283,6 +283,24 @@ static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
      return tcg_gen_code(tcg_ctx, tb, pc);
  }
 
+static vaddr get_guest_insn_vaddr(TranslationBlock *tb, vaddr pc, size_t insn)
+{
+    g_assert(insn < tb->icount);
+

accel/tcg/translate-all.c static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb, · line 299

Quoted 10 lines
accel/tcg/translate-all.c +10 -0
+    /* FIXME: This replicates the restore_state_to_opc() logic. */
+    vaddr addr = tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS];
+
+    if (tb_cflags(tb) & CF_PCREL) {
+        addr |= (pc & TARGET_PAGE_MASK);
+    } else {
+#if defined(TARGET_I386)
+        addr -= tb->cs_base;
+#endif
+    }

I disagree with this. The only bug I see is

accel/tcg/translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 478

Quoted 1 line
accel/tcg/translate-all.c +0 -0
                      "  -- guest addr 0x%016" PRIx64 " + tb prologue\n",

"guest addr", which makes you believe this to be a guest virtual address.

I think it is important to log what is actually in the data structures, which is the page offset.

Why are you so keen to have the virtual address? Why is this more reasonable than the physical address, or anything else?

r~

#3 lore raw
Quoted 41 lines · Richard Henderson
Quoted 22 lines · Matt Borgerson

If CF_PCREL is enabled, generated host assembly logging (command line option -d out_asm) may incorrectly report guest instruction virtual addresses as page offsets instead of absolute addresses. This patch corrects the reported guest address.

---
  accel/tcg/translate-all.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
accel/tcg/translate-all.c +4 -0
index a1782db5dd..859db95cf7 100644
@@ -283,6 +283,24 @@ static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
      return tcg_gen_code(tcg_ctx, tb, pc);
  }
 
+static vaddr get_guest_insn_vaddr(TranslationBlock *tb, vaddr pc, size_t insn)
+{
+    g_assert(insn < tb->icount);
+

accel/tcg/translate-all.c static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb, · line 299

Quoted 10 lines
accel/tcg/translate-all.c +10 -0
+    /* FIXME: This replicates the restore_state_to_opc() logic. */
+    vaddr addr = tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS];
+
+    if (tb_cflags(tb) & CF_PCREL) {
+        addr |= (pc & TARGET_PAGE_MASK);
+    } else {
+#if defined(TARGET_I386)
+        addr -= tb->cs_base;
+#endif
+    }

I disagree with this. The only bug I see is

accel/tcg/translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 478

Quoted 1 line
accel/tcg/translate-all.c +0 -0
                      "  -- guest addr 0x%016" PRIx64 " + tb prologue\n",

"guest addr", which makes you believe this to be a guest virtual address.

Quoted 8 lines

I think it is important to log what is actually in the data structures, which is the page offset.

Why are you so keen to have the virtual address? Why is this more reasonable than the physical address, or anything else?

r~

Hi Richard--

Thanks for the review.

Quoted 5 lines

I disagree with this. The only bug I see is

accel/tcg/translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 478

Quoted 1 line
accel/tcg/translate-all.c +0 -0
                      "  -- guest addr 0x%016" PRIx64 " + tb prologue\n",

"guest addr", which makes you believe this to be a guest virtual address.

Yes, and because this was effectively the behavior of these log messages prior to the introduction of PCREL.

Quoted 4 lines

I think it is important to log what is actually in the data structures, which is the page offset.

Why are you so keen to have the virtual address?

Printing virtual addresses with host translation allows me to plainly see (and grep for) how translation-provoking guest instructions map to corresponding translations without needing to cross-reference against additional logging, in_asm or trace:translate_block for example. I agree logging 'what is actually in the data structures' is important, but page offset alone has limited utility when debugging translation issues without additional logging to provide context.

Quoted 1 line

Why is this more reasonable than the physical address, or anything else?

For the same reason virtual addresses are used when logging input block assembly, performance metrics, trace events, etc.

An oversight with this patch is that raw start words are still printed with OP* log items--ideally those log items would be updated accordingly, if that is the decision.

Ultimately I'm also fine with pulling the 'guest addr' message text and only printing the raw start words, but would prefer not to require extra cross-referencing. If this is the decision, feel free to drop the patch.

Thanks,
Matt