Add mterp checks after monitor entry/exit opcodes.

Check whether we are allowed to continue running in mterp.

This tries to enforce that we bail out of mterp after suspend point
if needed (in particular, if debugger event switches interpreters).

Test: ./art/test.py -b -r --interpreter
Change-Id: If2d55e5ece22831ef864c181b7d6d5c70ca50e5d
diff --git a/runtime/interpreter/mterp/arm/other.S b/runtime/interpreter/mterp/arm/other.S
index 340038c..fcdde1e 100644
--- a/runtime/interpreter/mterp/arm/other.S
+++ b/runtime/interpreter/mterp/arm/other.S
@@ -159,6 +159,9 @@
     cmp      r0, #0
     bne      MterpException
     FETCH_ADVANCE_INST 1
+    ldr      r0, [rSELF, #THREAD_USE_MTERP_OFFSET]
+    cmp      r0, #0
+    beq      MterpFallback
     GET_INST_OPCODE ip                   @ extract opcode from rINST
     GOTO_OPCODE ip                       @ jump to next instruction
 
@@ -179,6 +182,9 @@
     cmp     r0, #0                      @ failed?
     bne     MterpException
     FETCH_ADVANCE_INST 1                @ before throw: advance rPC, load rINST
+    ldr      r0, [rSELF, #THREAD_USE_MTERP_OFFSET]
+    cmp      r0, #0
+    beq      MterpFallback
     GET_INST_OPCODE ip                  @ extract opcode from rINST
     GOTO_OPCODE ip                      @ jump to next instruction
 
diff --git a/runtime/interpreter/mterp/arm64/other.S b/runtime/interpreter/mterp/arm64/other.S
index 024a5c8..f1d0ef3 100644
--- a/runtime/interpreter/mterp/arm64/other.S
+++ b/runtime/interpreter/mterp/arm64/other.S
@@ -146,6 +146,8 @@
     bl       artLockObjectFromCode
     cbnz     w0, MterpException
     FETCH_ADVANCE_INST 1
+    ldr      w0, [xSELF, #THREAD_USE_MTERP_OFFSET]
+    cbz      w0, MterpFallback
     GET_INST_OPCODE ip                   // extract opcode from rINST
     GOTO_OPCODE ip                       // jump to next instruction
 
@@ -165,6 +167,8 @@
     bl       artUnlockObjectFromCode    // w0<- success for unlock(self, obj)
     cbnz     w0, MterpException
     FETCH_ADVANCE_INST 1                // before throw: advance rPC, load rINST
+    ldr      w0, [xSELF, #THREAD_USE_MTERP_OFFSET]
+    cbz      w0, MterpFallback
     GET_INST_OPCODE ip                  // extract opcode from rINST
     GOTO_OPCODE ip                      // jump to next instruction
 
diff --git a/runtime/interpreter/mterp/x86/invoke.S b/runtime/interpreter/mterp/x86/invoke.S
index cfb9c7c..06cd904 100644
--- a/runtime/interpreter/mterp/x86/invoke.S
+++ b/runtime/interpreter/mterp/x86/invoke.S
@@ -18,8 +18,7 @@
     jz      MterpException
     ADVANCE_PC 3
     movl    rSELF, %eax
-    movb    THREAD_USE_MTERP_OFFSET(%eax), %al
-    testb   %al, %al
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%eax)
     jz      MterpFallback
     RESTORE_IBASE
     FETCH_INST
@@ -45,8 +44,7 @@
     jz      MterpException
     ADVANCE_PC 4
     movl    rSELF, %eax
-    movb    THREAD_USE_MTERP_OFFSET(%eax), %al
-    testb   %al, %al
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%eax)
     jz      MterpFallback
     RESTORE_IBASE
     FETCH_INST
diff --git a/runtime/interpreter/mterp/x86/main.S b/runtime/interpreter/mterp/x86/main.S
index b233f2c..0621fb4 100644
--- a/runtime/interpreter/mterp/x86/main.S
+++ b/runtime/interpreter/mterp/x86/main.S
@@ -91,6 +91,8 @@
 #include "asm_support.h"
 #include "interpreter/cfi_asm_support.h"
 
+#define LITERAL(value) $$(value)
+
 /*
  * Handle mac compiler specific
  */
@@ -561,8 +563,7 @@
     movl    rPC, OFF_FP_DEX_PC_PTR(rFP)
     /* Do we need to switch interpreters? */
     movl    rSELF, %eax
-    movb    THREAD_USE_MTERP_OFFSET(%eax), %al
-    testb   %al, %al
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%eax)
     jz      MterpFallback
     /* resume execution at catch block */
     REFRESH_IBASE
diff --git a/runtime/interpreter/mterp/x86/other.S b/runtime/interpreter/mterp/x86/other.S
index 5de3381..270ccb6 100644
--- a/runtime/interpreter/mterp/x86/other.S
+++ b/runtime/interpreter/mterp/x86/other.S
@@ -132,7 +132,12 @@
     RESTORE_IBASE
     testb   %al, %al
     jnz     MterpException
-    ADVANCE_PC_FETCH_AND_GOTO_NEXT 1
+    ADVANCE_PC 1
+    movl    rSELF, %eax
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%eax)
+    jz      MterpFallback
+    FETCH_INST
+    GOTO_NEXT
 
 %def op_monitor_exit():
 /*
@@ -152,7 +157,12 @@
     RESTORE_IBASE
     testb   %al, %al
     jnz     MterpException
-    ADVANCE_PC_FETCH_AND_GOTO_NEXT 1
+    ADVANCE_PC 1
+    movl    rSELF, %eax
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%eax)
+    jz      MterpFallback
+    FETCH_INST
+    GOTO_NEXT
 
 %def op_move(is_object="0"):
     /* for move, move-object, long-to-int */
diff --git a/runtime/interpreter/mterp/x86_64/invoke.S b/runtime/interpreter/mterp/x86_64/invoke.S
index f727915..15b48c9 100644
--- a/runtime/interpreter/mterp/x86_64/invoke.S
+++ b/runtime/interpreter/mterp/x86_64/invoke.S
@@ -16,8 +16,7 @@
     jz      MterpException
     ADVANCE_PC 3
     movq    rSELF, %rax
-    movb    THREAD_USE_MTERP_OFFSET(%rax), %al
-    testb   %al, %al
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%rax)
     jz      MterpFallback
     FETCH_INST
     GOTO_NEXT
@@ -40,8 +39,7 @@
     jz      MterpException
     ADVANCE_PC 4
     movq    rSELF, %rax
-    movb    THREAD_USE_MTERP_OFFSET(%rax), %al
-    testb   %al, %al
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%rax)
     jz      MterpFallback
     FETCH_INST
     GOTO_NEXT
diff --git a/runtime/interpreter/mterp/x86_64/main.S b/runtime/interpreter/mterp/x86_64/main.S
index 75eb00c..4609067 100644
--- a/runtime/interpreter/mterp/x86_64/main.S
+++ b/runtime/interpreter/mterp/x86_64/main.S
@@ -87,6 +87,8 @@
 #include "asm_support.h"
 #include "interpreter/cfi_asm_support.h"
 
+#define LITERAL(value) $$(value)
+
 /*
  * Handle mac compiler specific
  */
@@ -527,8 +529,7 @@
     movq    rPC, OFF_FP_DEX_PC_PTR(rFP)
     /* Do we need to switch interpreters? */
     movq    rSELF, %rax
-    movb    THREAD_USE_MTERP_OFFSET(%rax), %al
-    testb   %al, %al
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%rax)
     jz      MterpFallback
     /* resume execution at catch block */
     REFRESH_IBASE
diff --git a/runtime/interpreter/mterp/x86_64/other.S b/runtime/interpreter/mterp/x86_64/other.S
index 849155c..412389f 100644
--- a/runtime/interpreter/mterp/x86_64/other.S
+++ b/runtime/interpreter/mterp/x86_64/other.S
@@ -108,7 +108,12 @@
     call    SYMBOL(artLockObjectFromCode)   # (object, self)
     testq   %rax, %rax
     jnz     MterpException
-    ADVANCE_PC_FETCH_AND_GOTO_NEXT 1
+    ADVANCE_PC 1
+    movq    rSELF, %rax
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%rax)
+    jz      MterpFallback
+    FETCH_INST
+    GOTO_NEXT
 
 %def op_monitor_exit():
 /*
@@ -125,7 +130,12 @@
     call    SYMBOL(artUnlockObjectFromCode) # (object, self)
     testq   %rax, %rax
     jnz     MterpException
-    ADVANCE_PC_FETCH_AND_GOTO_NEXT 1
+    ADVANCE_PC 1
+    movq    rSELF, %rax
+    cmpb    LITERAL(0), THREAD_USE_MTERP_OFFSET(%rax)
+    jz      MterpFallback
+    FETCH_INST
+    GOTO_NEXT
 
 %def op_move(is_object="0"):
     /* for move, move-object, long-to-int */