From a853eb6363fc6638d767725d10c56711fe33b2f8 Mon Sep 17 00:00:00 2001
From: Nguyen Anh Quynh <aquynh@gmail.com>
Date: Tue, 22 Sep 2015 10:24:26 +0800
Subject: [PATCH] mips, m68k: early check to see if the address of BB is the
 until address

---
 qemu/target-m68k/translate.c | 9 +++++++++
 qemu/target-mips/translate.c | 7 +++++++
 2 files changed, 16 insertions(+)

diff --git a/qemu/target-m68k/translate.c b/qemu/target-m68k/translate.c
index 133957df..fedffecc 100644
--- a/qemu/target-m68k/translate.c
+++ b/qemu/target-m68k/translate.c
@@ -3101,6 +3101,13 @@ gen_intermediate_code_internal(M68kCPU *cpu, TranslationBlock *tb,
     if (max_insns == 0)
         max_insns = CF_COUNT_MASK;
 
+    // Unicorn: early check to see if the address of this block is the until address
+    if (tb->pc == env->uc->addr_end) {
+        gen_tb_start(tcg_ctx);
+        gen_exception(dc, dc->pc, EXCP_TRAP15);
+        goto done_generating;
+    }
+
     // Unicorn: trace this block on request
     // Only hook this block if it is not broken from previous translation due to
     // full translation cache
@@ -3179,6 +3186,8 @@ gen_intermediate_code_internal(M68kCPU *cpu, TranslationBlock *tb,
                 break;
         }
     }
+
+done_generating:
     gen_tb_end(tcg_ctx, tb, num_insns);
     *tcg_ctx->gen_opc_ptr = INDEX_op_end;
 
diff --git a/qemu/target-mips/translate.c b/qemu/target-mips/translate.c
index 718d69aa..0d3c64bb 100644
--- a/qemu/target-mips/translate.c
+++ b/qemu/target-mips/translate.c
@@ -19207,6 +19207,13 @@ gen_intermediate_code_internal(MIPSCPU *cpu, TranslationBlock *tb,
         max_insns = CF_COUNT_MASK;
     LOG_DISAS("\ntb %p idx %d hflags %04x\n", tb, ctx.mem_idx, ctx.hflags);
 
+    // Unicorn: early check to see if the address of this block is the until address
+    if (tb->pc == env->uc->addr_end) {
+        gen_tb_start(tcg_ctx);
+        generate_exception(&ctx, EXCP_SYSCALL);
+        goto done_generating;
+    }
+
     // Unicorn: trace this block on request
     // Only hook this block if it is not broken from previous translation due to
     // full translation cache