Skip to contentSkip to conversation

ARM - Remove fixed map code buffer restriction

PATCH In review Dr. David Alan Gilbert 3 participants 7 messages Dec 12, 2011 – Dec 14, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM - Remove fixed map code buffer restriction

On ARM, don't map the code buffer at a fixed location, and fix up the call/goto tcg routines to let it do long jumps.

Mapping the code buffer at a fixed address could sometimes result in it being mapped over the top of the heap with pretty random results.

This diff is against v1.0.

---
 exec.c               |    4 +---
 tcg/arm/tcg-target.c |   31 ++++++++++++-------------------
 2 files changed, 13 insertions(+), 22 deletions(-)
exec.c +1 -3
index 6b92198..ef83da1 100644
@@ -497,9 +497,7 @@ static void code_gen_alloc(unsigned long tb_size)
         if (code_gen_buffer_size > (512 * 1024 * 1024))
             code_gen_buffer_size = (512 * 1024 * 1024);
 #elif defined(__arm__)
-        /* Map the buffer below 32M, so we can use direct calls and branches */
-        flags |= MAP_FIXED;
-        start = (void *) 0x01000000UL;
+        /* Keep the buffer no bigger than 16GB to branch between blocks */
         if (code_gen_buffer_size > 16 * 1024 * 1024)
             code_gen_buffer_size = 16 * 1024 * 1024;
 #elif defined(__s390x__)
tcg/arm/tcg-target.c +12 -19
index e05a64f..730d913 100644
@@ -842,6 +842,12 @@ static inline void tcg_out_st8(TCGContext *s, int cond,
         tcg_out_st8_12(s, cond, rd, rn, offset);
 }
 
+/* The _goto case is normally between TBs within the same code buffer,
+   and with the code buffer limited to 16GB we shouldn't need the long
+   case.
+
+   .... except to the prologue that is in its own buffer.
+     */
 static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
 {
     int32_t val;
@@ -855,22 +861,20 @@ static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
     if (val - 8 < 0x01fffffd && val - 8 > -0x01fffffd)
         tcg_out_b(s, cond, val);
     else {
-#if 1
-        tcg_abort();
-#else
         if (cond == COND_AL) {
             tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
-            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
+            tcg_out32(s, addr);
         } else {
             tcg_out_movi32(s, cond, TCG_REG_R8, val - 8);
             tcg_out_dat_reg(s, cond, ARITH_ADD,
                             TCG_REG_PC, TCG_REG_PC,
                             TCG_REG_R8, SHIFT_IMM_LSL(0));
         }
-#endif
     }
 }
 
+/* The call case is mostly used for helpers - so it's not unreasonable
+   for them to be beyond branch range */
 static inline void tcg_out_call(TCGContext *s, uint32_t addr)
 {
     int32_t val;
@@ -887,20 +891,9 @@ static inline void tcg_out_call(TCGContext *s, uint32_t addr)
             tcg_out_bl(s, COND_AL, val);
         }
     } else {
-#if 1
-        tcg_abort();
-#else
-        if (cond == COND_AL) {
-            tcg_out_dat_imm(s, cond, ARITH_ADD, TCG_REG_R14, TCG_REG_PC, 4);
-            tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
-            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
-        } else {
-            tcg_out_movi32(s, cond, TCG_REG_R9, addr);
-            tcg_out_dat_reg(s, cond, ARITH_MOV, TCG_REG_R14, 0,
-                            TCG_REG_PC, SHIFT_IMM_LSL(0));
-            tcg_out_bx(s, cond, TCG_REG_R9);
-        }
-#endif
+        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R14, TCG_REG_PC, 4);
+        tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
+        tcg_out32(s, addr);
     }
 }
 
#1 lore raw

CC'ing Andrzej, who is the tcg/arm maintainer.

Quoted 7 lines · Dr. David Alan Gilbert

On ARM, don't map the code buffer at a fixed location, and fix up the call/goto tcg routines to let it do long jumps.

Mapping the code buffer at a fixed address could sometimes result in it being mapped over the top of the heap with pretty random results.

This diff is against v1.0.

Patches should always be against master, although we're still close enough to 1.0 that this will probably apply anyway.

Comments like "This diff is against v1.0." go below the '---' so they don't appear in the final commit log.

Code generally looks OK to me.

Quoted 39 lines

---
 exec.c               |    4 +--- tcg/arm/tcg-target.c |   31 ++++++++++++-------------------

 2 files changed, 13 insertions(+), 22 deletions(-)
exec.c +1 -3
index 6b92198..ef83da1 100644
@@ -497,9 +497,7 @@ static void code_gen_alloc(unsigned long tb_size)
         if (code_gen_buffer_size > (512 * 1024 * 1024))
             code_gen_buffer_size = (512 * 1024 * 1024);
  #elif defined(__arm__)
-        /* Map the buffer below 32M, so we can use direct calls and branches */
-        flags |= MAP_FIXED;
-        start = (void *) 0x01000000UL;
+        /* Keep the buffer no bigger than 16GB to branch between blocks */
         if (code_gen_buffer_size > 16 * 1024 * 1024)
             code_gen_buffer_size = 16 * 1024 * 1024;
  #elif defined(__s390x__)
tcg/arm/tcg-target.c +6 -0
index e05a64f..730d913 100644
@@ -842,6 +842,12 @@ static inline void tcg_out_st8(TCGContext *s, int cond,
         tcg_out_st8_12(s, cond, rd, rn, offset);
  }
 
+/* The _goto case is normally between TBs within the same code buffer,
+   and with the code buffer limited to 16GB we shouldn't need the long
+   case.
+
+   .... except to the prologue that is in its own buffer.
+     */
  static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
  {
     int32_t val;

tcg/arm/tcg-target.c static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr) · line 866

Quoted 10 lines
tcg/arm/tcg-target.c +1 -4
@@ -855,22 +861,20 @@ static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
     if (val - 8 < 0x01fffffd && val - 8 > -0x01fffffd)
         tcg_out_b(s, cond, val);
     else {
-#if 1
-        tcg_abort();
-#else
         if (cond == COND_AL) {
             tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
-            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
+            tcg_out32(s, addr);

I see these XXXs have been there since the ARM tcg target was introduced. Does anybody know what they were referring to? Andrzej?

Quoted 29 lines
tcg/arm/tcg-target.c +2 -11
         } else {
             tcg_out_movi32(s, cond, TCG_REG_R8, val - 8);
             tcg_out_dat_reg(s, cond, ARITH_ADD,
                             TCG_REG_PC, TCG_REG_PC,
                             TCG_REG_R8, SHIFT_IMM_LSL(0));
         }
-#endif
     }
  }
 
+/* The call case is mostly used for helpers - so it's not unreasonable
+   for them to be beyond branch range */
  static inline void tcg_out_call(TCGContext *s, uint32_t addr)
  {
     int32_t val;
@@ -887,20 +891,9 @@ static inline void tcg_out_call(TCGContext *s, uint32_t addr)
             tcg_out_bl(s, COND_AL, val);
         }
     } else {
-#if 1
-        tcg_abort();
-#else
-        if (cond == COND_AL) {
-            tcg_out_dat_imm(s, cond, ARITH_ADD, TCG_REG_R14, TCG_REG_PC, 4);
-            tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
-            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
-        } else {
-            tcg_out_movi32(s, cond, TCG_REG_R9, addr);
-            tcg_out_dat_reg(s, cond, ARITH_MOV, TCG_REG_R14, 0,

tcg/arm/tcg-target.c static inline void tcg_out_call(TCGContext *s, uint32_t addr) · line 899

Quoted 10 lines
tcg/arm/tcg-target.c +3 -4
-                            TCG_REG_PC, SHIFT_IMM_LSL(0));
-            tcg_out_bx(s, cond, TCG_REG_R9);
-        }
-#endif
+        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R14, TCG_REG_PC, 4);
+        tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
+        tcg_out32(s, addr);
     }
  }
 

-- PMM

#2 lore raw

Hi,

Quoted 10 lines · Peter Maydell
Quoted 7 lines · Dr. David Alan Gilbert

On ARM, don't map the code buffer at a fixed location, and fix up the call/goto tcg routines to let it do long jumps.

Mapping the code buffer at a fixed address could sometimes result in it being mapped over the top of the heap with pretty random results.

This diff is against v1.0.

Quoted 7 lines

Patches should always be against master, although we're still close enough to 1.0 that this will probably apply anyway.

Comments like "This diff is against v1.0." go below the '---' so they don't appear in the final commit log.

Code generally looks OK to me.

To me as well, I'll apply if there are no objections. I remember Peter asked about MAP_FIXED on other hosts, has there been a resolution?

Quoted 45 lines
Quoted 45 lines

---
 exec.c               |    4 +--- tcg/arm/tcg-target.c |   31 ++++++++++++-------------------

 2 files changed, 13 insertions(+), 22 deletions(-)
exec.c +1 -3
index 6b92198..ef83da1 100644
@@ -497,9 +497,7 @@ static void code_gen_alloc(unsigned long tb_size)
         if (code_gen_buffer_size > (512 * 1024 * 1024))
             code_gen_buffer_size = (512 * 1024 * 1024);
  #elif defined(__arm__)
-        /* Map the buffer below 32M, so we can use direct calls and branches */
-        flags |= MAP_FIXED;
-        start = (void *) 0x01000000UL;
+        /* Keep the buffer no bigger than 16GB to branch between blocks */
         if (code_gen_buffer_size > 16 * 1024 * 1024)
             code_gen_buffer_size = 16 * 1024 * 1024;
  #elif defined(__s390x__)
tcg/arm/tcg-target.c +6 -2
index e05a64f..730d913 100644
@@ -842,6 +842,12 @@ static inline void tcg_out_st8(TCGContext *s, int cond,
         tcg_out_st8_12(s, cond, rd, rn, offset);
  }
 
+/* The _goto case is normally between TBs within the same code buffer,
+   and with the code buffer limited to 16GB we shouldn't need the long
+   case.
+
+   .... except to the prologue that is in its own buffer.
+     */
  static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
  {
     int32_t val;
@@ -855,22 +861,20 @@ static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
     if (val - 8 < 0x01fffffd && val - 8 > -0x01fffffd)
         tcg_out_b(s, cond, val);
     else {
-#if 1
-        tcg_abort();
Quoted 8 lines

tcg/arm/tcg-target.c static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr) · line 866

Quoted 5 lines
tcg/arm/tcg-target.c +1 -2
-#else
         if (cond == COND_AL) {
             tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
-            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
+            tcg_out32(s, addr);

I see these XXXs have been there since the ARM tcg target was introduced. Does anybody know what they were referring to? Andrzej?

David asked me about them, but I couldn't figure it out. (the part in tcg_out_call apparently is just copy&pasted from tcg_ouy_goto)

BTW: I think we can also use the "ld" branch when we see the goto target is in Thumb mode.

Cheers

#3 lore raw
Quoted 2 lines · andrzej zaborowski

BTW: I think we can also use the "ld" branch when we see the goto target is in Thumb mode.

The target of a goto is currently never Thumb (because gotos are always to other TCG generated code and we only generate ARM insns). If we did need to be able to do a goto-Thumb we'd want to support the short-range version too. But I think we might as well leave it until we actually need it.

-- PMM

#4 lore raw
Quoted 6 lines · Peter Maydell
Quoted 2 lines · andrzej zaborowski

BTW: I think we can also use the "ld" branch when we see the goto target is in Thumb mode.

The target of a goto is currently never Thumb (because gotos are always to other TCG generated code and we only generate ARM insns).

I'm aware of that, I just like functions that can do what their name says well. :)

Cheers

#5 lore raw
Quoted 10 lines · andrzej zaborowski
Quoted 6 lines · Peter Maydell
Quoted 2 lines · andrzej zaborowski

BTW: I think we can also use the "ld" branch when we see the goto target is in Thumb mode.

The target of a goto is currently never Thumb (because gotos are always to other TCG generated code and we only generate ARM insns).

I'm aware of that, I just like functions that can do what their name says well. :)

It does have an assert which will catch it if you try, so no one should get caught out
by it, and on ARMv7 the add is apparently an interworking branch, so I think it might
even work.

Dave

#6 lore raw
In reply to Dr. David Alan Gilbert
Quoted 6 lines · Dr. David Alan Gilbert

On ARM, don't map the code buffer at a fixed location, and fix up the call/goto tcg routines to let it do long jumps.

Mapping the code buffer at a fixed address could sometimes result in it being mapped over the top of the heap with pretty random results.

Quoted 7 lines

This diff is against v1.0.

---
 exec.c               |    4 +--- tcg/arm/tcg-target.c |   31 ++++++++++++-------------------

 2 files changed, 13 insertions(+), 22 deletions(-)

Thanks, I applied this patch.

Cheers