Skip to contentSkip to conversation

tcg: workaround branch instruction overflow in tcg_out_qemu_ld/st

RFC Superseded Laurent Vivier 1 participant 2 messages Apr 27, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 tcg: workaround branch instruction overflow in tcg_out_qemu_ld/st

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")

---

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(-)
tcg/tcg-ldst.inc.c +17 -8
index 0e14cf4357..1f41cda482 100644
@@ -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);
 }
 
 /*
Signature · 1 line
2.14.3
#1 lore raw
Quoted 14 lines · Laurent Vivier

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")
Quoted 8 lines

---

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