diff mbox series

[RFC] tcg: workaround branch instruction overflow in tcg_out_qemu_ld/st

Message ID 20180427121723.3543-1-lvivier@redhat.com
State New
Headers show
Series [RFC] tcg: workaround branch instruction overflow in tcg_out_qemu_ld/st | expand

Commit Message

Laurent Vivier April 27, 2018, 12:17 p.m. UTC
ppc64 uses a BC instruction to call the tcg_out_qemu_ld/st
slow path. BC instruction uses a relative address encoded
on 14 bits.

The slow path functions are added at the end of the generated
instructions buffer, in the reverse order of the callers.
So more we have slow path functions more the distance between
the caller (BC) and the function increases.

This patch changes the behavior to generate the functions in
the same order of the callers.

Fixes: 15fa08f845 ("tcg: Dynamically allocate TCGOps")
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---

Notes:
    This is an RFC for several reasons:
    - it doens't really fix the overflow problem
      only avoids the case
    - it uses a recursive function to revert the slow path
      functions order (and we can have a stack overflow...),

 tcg/tcg-ldst.inc.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

Comments

Laurent Vivier April 27, 2018, 12:28 p.m. UTC | #1
On 27/04/2018 14:17, Laurent Vivier wrote:
> ppc64 uses a BC instruction to call the tcg_out_qemu_ld/st
> slow path. BC instruction uses a relative address encoded
> on 14 bits.
> 
> The slow path functions are added at the end of the generated
> instructions buffer, in the reverse order of the callers.
> So more we have slow path functions more the distance between
> the caller (BC) and the function increases.
> 
> This patch changes the behavior to generate the functions in
> the same order of the callers.
> 
> Fixes: 15fa08f845 ("tcg: Dynamically allocate TCGOps")
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
> 
> Notes:
>     This is an RFC for several reasons:
>     - it doens't really fix the overflow problem
>       only avoids the case
>     - it uses a recursive function to revert the slow path
>       functions order (and we can have a stack overflow...),

I think the number of slow path functions cannot be greater than
TCG_MAX_INSNS (i.e. 512): is it enough to overflow the stack?

Thanks,
Laurent
diff mbox series

Patch

diff --git a/tcg/tcg-ldst.inc.c b/tcg/tcg-ldst.inc.c
index 0e14cf4357..1f41cda482 100644
--- a/tcg/tcg-ldst.inc.c
+++ b/tcg/tcg-ldst.inc.c
@@ -41,12 +41,16 @@  typedef struct TCGLabelQemuLdst {
 static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l);
 static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l);
 
-static bool tcg_out_ldst_finalize(TCGContext *s)
+static bool tcg_out_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
 {
-    TCGLabelQemuLdst *lb;
+    bool ret;
 
-    /* qemu_ld/st slow paths */
-    for (lb = s->ldst_labels; lb != NULL; lb = lb->next) {
+    if (lb == NULL) {
+        return true;
+    }
+
+    ret = tcg_out_slow_path(s, lb->next);
+    if (ret) {
         if (lb->is_ld) {
             tcg_out_qemu_ld_slow_path(s, lb);
         } else {
@@ -57,11 +61,16 @@  static bool tcg_out_ldst_finalize(TCGContext *s)
            one operation beginning below the high water mark cannot overrun
            the buffer completely.  Thus we can test for overflow after
            generating code without having to check during generation.  */
-        if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
-            return false;
-        }
+        ret = (void *)s->code_ptr <= s->code_gen_highwater;
     }
-    return true;
+
+    return ret;
+}
+
+static bool tcg_out_ldst_finalize(TCGContext *s)
+{
+    /* qemu_ld/st slow paths */
+    return tcg_out_slow_path(s, s->ldst_labels);
 }
 
 /*