Skip to contentSkip to conversation

target/ppc: BHRB avoid using host pointer in translated code

PATCH In review Nicholas Piggin 4 participants 6 messages Feb 15, 2024 – Feb 27, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/ppc: BHRB avoid using host pointer in translated code

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

---
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(-)
target/ppc/cpu.h +2 -3
index eaa24f2c95..6b050ea628 100644
@@ -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
 
target/ppc/cpu_init.c +0 -1
index 2494527765..262b1d7852 100644
@@ -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;
     }
 }
target/ppc/machine.c +1 -1
index 731dd8df35..3541cd83cd 100644
@@ -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()
     }
target/ppc/translate.c +23 -22

86 lines not on this page · raw message

Signature · 1 line
2.42.0
#1 lore raw
Quoted 4 lines · Nicholas Piggin

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

Quoted 8 lines

---
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.

There's nothing inherently wrong with it (depending on what the pointer is pointing to!), but you need to use the right type. target_ulong and the _tl suffix are for the type which depends on the size of the target's 'long'. The TCG type which is "size of a host pointer" is TCG_TYPE_PTR, and you want the _ptr suffix functions and to pass it around with TCGv_ptr.

thanks
-- PMM

#4 lore raw
In reply to Nicholas Piggin

target/ppc/machine.c static const VMStateDescription vmstate_bhrb = { · line 727

Quoted 10 lines · Nicholas Piggin
target/ppc/machine.c +1 -1
index 731dd8df35..3541cd83cd 100644
@@ -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),

This requires a version bump.

Quoted 15 lines
target/ppc/translate.c +3 -2
index 81afc892de..05f0f1ac52 100644
@@ -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();
 

target/ppc/translate.c static void gen_rvwinkle(DisasContext *ctx) · line 4181

Quoted 10 lines
target/ppc/translate.c +5 -3
-    /* 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);

Better to add the constant with the store offset.

  tcg_gen_ext_i32_ptr(ptr, offset);
  tcg_gen_add_ptr(ptr, ptr, tcg_env);
  tcg_gen_st_i64(value, ptr, offsetof(bhrb));

r~

#5 lore raw
Quoted 13 lines · Richard Henderson

target/ppc/machine.c static const VMStateDescription vmstate_bhrb = { · line 727

Quoted 10 lines · Nicholas Piggin
target/ppc/machine.c +1 -1
index 731dd8df35..3541cd83cd 100644
@@ -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),

This requires a version bump.

In this case the first patch hasn't gone upstream yet.

Quoted 28 lines
Quoted 15 lines
target/ppc/translate.c +3 -2
index 81afc892de..05f0f1ac52 100644
@@ -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();
 

target/ppc/translate.c static void gen_rvwinkle(DisasContext *ctx) · line 4181

Quoted 10 lines
target/ppc/translate.c +5 -3
-    /* 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);

Better to add the constant with the store offset.

Quoted 3 lines
   tcg_gen_ext_i32_ptr(ptr, offset);
   tcg_gen_add_ptr(ptr, ptr, tcg_env);
   tcg_gen_st_i64(value, ptr, offsetof(bhrb));

Good suggestion thanks. Maybe in light of the fact I was wrong about not using host pointer here, the original approach is better and just needs a smaller fix for 32-bit hosts.

Thanks,
Nick

#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell
Quoted 7 lines · Nicholas Piggin

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

---
Hi Glenn,

Quoted 8 lines
Quoted 5 lines

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.

There's nothing inherently wrong with it (depending on what the pointer is pointing to!), but you need to use the right type.

Ah okay, thanks for the correction.

Quoted 4 lines

target_ulong and the _tl suffix are for the type which depends on the size of the target's 'long'. The TCG type which is "size of a host pointer" is TCG_TYPE_PTR, and you want the _ptr suffix functions and to pass it around with TCGv_ptr.

In that case, original approach may be better with small fixes for 32-bit host.

Thanks,
Nick

#3 lore raw
Quoted 29 lines · Nicholas Piggin
Quoted 18 lines · Peter Maydell
Quoted 7 lines · Nicholas Piggin

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

---
Hi Glenn,

Quoted 8 lines

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.

Quoted 2 lines

There's nothing inherently wrong with it (depending on what the pointer is pointing to!), but you need to use the right type.

Ah okay, thanks for the correction.

Quoted 4 lines

target_ulong and the _tl suffix are for the type which depends on the size of the target's 'long'. The TCG type which is "size of a host pointer" is TCG_TYPE_PTR, and you want the _ptr suffix functions and to pass it around with TCGv_ptr.

Quoted 5 lines

In that case, original approach may be better with small fixes for 32-bit host.

Thanks,
Nick

Peter/Nick, thanks for looking into this. I'll work on submitting a v4 of the original BHRB series adding Peter's suggestions (and probably a rebase) as soon as I have a chance. Unfortunately, I have some higher priority items to work on at the moment, so it could take a week or two before I can get to it.

Thanks,
Glenn