qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: qemu-ppc@nongnu.org
Cc: Nicholas Piggin <npiggin@gmail.com>,
	qemu-devel@nongnu.org, Glenn Miles <milesg@linux.vnet.ibm.com>
Subject: [PATCH] target/ppc: BHRB avoid using host pointer in translated code
Date: Fri, 16 Feb 2024 03:15:12 +1000	[thread overview]
Message-ID: <20240215171512.800892-1-npiggin@gmail.com> (raw)

Calculate the BHRB base from arithmetic on the tcg_env target ptr.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
Hi Glenn,

I think I have to squash this into the BHRB series. 32-bit host
compile shows up a size mismatch warning... I think it's not quite
right to be using host pointer directly in target code. The change
of offset and mask to 32-bit is needed due to to seemingly missing
tl->ptr conversion helpers, but 32-bit is okay for those anyway.

Thanks,
Nick

 target/ppc/cpu.h       |  5 ++---
 target/ppc/cpu_init.c  |  1 -
 target/ppc/machine.c   |  2 +-
 target/ppc/translate.c | 45 +++++++++++++++++++++---------------------
 4 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/target/ppc/cpu.h b/target/ppc/cpu.h
index eaa24f2c95..6b050ea628 100644
--- a/target/ppc/cpu.h
+++ b/target/ppc/cpu.h
@@ -1325,10 +1325,9 @@ struct CPUArchState {
 #ifdef TARGET_PPC64
     /* Branch History Rolling Buffer (BHRB) resources */
     target_ulong bhrb_num_entries;
-    target_ulong bhrb_base;
     target_ulong bhrb_filter;
-    target_ulong bhrb_offset;
-    target_ulong bhrb_offset_mask;
+    uint32_t bhrb_offset_mask;
+    uint32_t bhrb_offset;
     uint64_t bhrb[BHRB_MAX_NUM_ENTRIES];
 #endif
 
diff --git a/target/ppc/cpu_init.c b/target/ppc/cpu_init.c
index 2494527765..262b1d7852 100644
--- a/target/ppc/cpu_init.c
+++ b/target/ppc/cpu_init.c
@@ -6117,7 +6117,6 @@ static void bhrb_init_state(CPUPPCState *env, target_long num_entries_log2)
             num_entries_log2 = BHRB_MAX_NUM_ENTRIES_LOG2;
         }
         env->bhrb_num_entries = 1 << num_entries_log2;
-        env->bhrb_base = (target_long)&env->bhrb[0];
         env->bhrb_offset_mask = (env->bhrb_num_entries * sizeof(uint64_t)) - 1;
     }
 }
diff --git a/target/ppc/machine.c b/target/ppc/machine.c
index 731dd8df35..3541cd83cd 100644
--- a/target/ppc/machine.c
+++ b/target/ppc/machine.c
@@ -724,7 +724,7 @@ static const VMStateDescription vmstate_bhrb = {
     .minimum_version_id = 1,
     .needed = bhrb_needed,
     .fields = (VMStateField[]) {
-        VMSTATE_UINTTL(env.bhrb_offset, PowerPCCPU),
+        VMSTATE_UINT32(env.bhrb_offset, PowerPCCPU),
         VMSTATE_UINT64_ARRAY(env.bhrb, PowerPCCPU, BHRB_MAX_NUM_ENTRIES),
         VMSTATE_END_OF_LIST()
     }
diff --git a/target/ppc/translate.c b/target/ppc/translate.c
index 81afc892de..05f0f1ac52 100644
--- a/target/ppc/translate.c
+++ b/target/ppc/translate.c
@@ -4167,21 +4167,24 @@ static void gen_rvwinkle(DisasContext *ctx)
 #endif /* defined(CONFIG_USER_ONLY) */
 }
 
-static inline TCGv gen_write_bhrb(TCGv base, TCGv offset, TCGv mask, TCGv value)
+static TCGv_i32 gen_write_bhrb(TCGv_i32 offset, TCGv_i32 mask, TCGv_i64 value)
 {
-    TCGv tmp = tcg_temp_new();
+    TCGv_ptr ptr = tcg_temp_new_ptr();
+    TCGv_i32 tmp = tcg_temp_new_i32();
 
-    /* add base and offset to get address of bhrb entry */
-    tcg_gen_add_tl(tmp, base, offset);
+    /* add base and offset to tcg_env to get address of bhrb entry */
+    tcg_gen_addi_i32(tmp, offset, offsetof(CPUPPCState, bhrb));
+    tcg_gen_ext_i32_ptr(ptr, tmp);
+    tcg_gen_add_ptr(ptr, ptr, tcg_env);
 
     /* store value into bhrb at bhrb_offset */
-    tcg_gen_st_i64(value, (TCGv_ptr)tmp, 0);
+    tcg_gen_st_i64(value, ptr, 0);
 
     /* add 8 to current bhrb_offset */
-    tcg_gen_addi_tl(offset, offset, 8);
+    tcg_gen_addi_i32(offset, offset, 8);
 
     /* apply offset mask */
-    tcg_gen_and_tl(offset, offset, mask);
+    tcg_gen_and_i32(offset, offset, mask);
 
     return offset;
 }
@@ -4193,10 +4196,9 @@ static inline void gen_update_branch_history(DisasContext *ctx,
                                              target_long inst_type)
 {
 #if defined(TARGET_PPC64)
-    TCGv base;
     TCGv tmp;
-    TCGv offset;
-    TCGv mask;
+    TCGv_i32 offset;
+    TCGv_i32 mask;
     TCGLabel *no_update;
 
     if (ctx->has_cfar) {
@@ -4216,32 +4218,31 @@ static inline void gen_update_branch_history(DisasContext *ctx,
     tcg_gen_andi_tl(tmp, tmp, inst_type);
     tcg_gen_brcondi_tl(TCG_COND_EQ, tmp, 0, no_update);
 
-    base = tcg_temp_new();
-    offset = tcg_temp_new();
-    mask = tcg_temp_new();
-
-    /* load bhrb base address */
-    tcg_gen_ld_tl(base, tcg_env, offsetof(CPUPPCState, bhrb_base));
+    offset = tcg_temp_new_i32();
+    mask = tcg_temp_new_i32();
 
     /* load current bhrb_offset */
-    tcg_gen_ld_tl(offset, tcg_env, offsetof(CPUPPCState, bhrb_offset));
+    tcg_gen_ld_i32(offset, tcg_env, offsetof(CPUPPCState, bhrb_offset));
 
     /* load a BHRB offset mask */
-    tcg_gen_ld_tl(mask, tcg_env, offsetof(CPUPPCState, bhrb_offset_mask));
+    tcg_gen_ld_i32(mask, tcg_env, offsetof(CPUPPCState, bhrb_offset_mask));
 
-    offset = gen_write_bhrb(base, offset, mask, tcg_constant_i64(nip));
+    offset = gen_write_bhrb(offset, mask, tcg_constant_i64(nip));
 
     /* Also record the target address for XL-Form branches */
     if (inst_type & BHRB_TYPE_XL_FORM) {
+        TCGv_i64 t = tcg_temp_new_i64();
+
+        tcg_gen_extu_tl_i64(t, target);
 
         /* Set the 'T' bit for target entries */
-        tcg_gen_ori_tl(tmp, target, 0x2);
+        tcg_gen_ori_i64(t, target, 0x2);
 
-        offset = gen_write_bhrb(base, offset, mask, tmp);
+        offset = gen_write_bhrb(offset, mask, t);
     }
 
     /* save updated bhrb_offset for next time */
-    tcg_gen_st_tl(offset, tcg_env, offsetof(CPUPPCState, bhrb_offset));
+    tcg_gen_st_i32(offset, tcg_env, offsetof(CPUPPCState, bhrb_offset));
 
     gen_set_label(no_update);
 #endif
-- 
2.42.0



             reply	other threads:[~2024-02-15 17:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-15 17:15 Nicholas Piggin [this message]
2024-02-15 17:50 ` [PATCH] target/ppc: BHRB avoid using host pointer in translated code Peter Maydell
2024-02-20  0:35   ` Nicholas Piggin
2024-02-27 16:29     ` Miles Glenn
2024-02-15 19:42 ` Richard Henderson
2024-02-20  0:28   ` Nicholas Piggin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240215171512.800892-1-npiggin@gmail.com \
    --to=npiggin@gmail.com \
    --cc=milesg@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).