Fix generic JNI stubs to not discard the Java native frame.

Change-Id: Ic856b442fdde5ce91673fc5856eb0dfc84c75d28
diff --git a/runtime/arch/arm/quick_entrypoints_arm.S b/runtime/arch/arm/quick_entrypoints_arm.S
index fec1ce5..539b607 100644
--- a/runtime/arch/arm/quick_entrypoints_arm.S
+++ b/runtime/arch/arm/quick_entrypoints_arm.S
@@ -890,7 +890,7 @@
     // r1: pointer to the bottom of the used area of the alloca, can restore stack till there.
 
     // Check for error = 0.
-    cbz r0, .Lentry_error
+    cbz r0, .Lexception_in_native
 
     // Release part of the alloca.
     mov sp, r1
@@ -920,10 +920,6 @@
 
     blx artQuickGenericJniEndTrampoline
 
-    // Tear down the alloca.
-    mov sp, r10
-    .cfi_def_cfa_register sp
-
     // Restore self pointer.
     mov r9, r11
 
@@ -931,6 +927,10 @@
     ldr r2, [r9, #THREAD_EXCEPTION_OFFSET]  @ load Thread::Current()->exception_
     cbnz r2, .Lexception_in_native
 
+    // Tear down the alloca.
+    mov sp, r10
+    .cfi_def_cfa_register sp
+
     // Tear down the callee-save frame. Skip arg registers.
     add     sp, #FRAME_SIZE_REFS_AND_ARGS_CALLEE_SAVE-FRAME_SIZE_REFS_ONLY_CALLEE_SAVE
     .cfi_adjust_cfa_offset -(FRAME_SIZE_REFS_AND_ARGS_CALLEE_SAVE-FRAME_SIZE_REFS_ONLY_CALLEE_SAVE)
@@ -943,14 +943,11 @@
     .cfi_def_cfa_register r10
     .cfi_adjust_cfa_offset FRAME_SIZE_REFS_AND_ARGS_CALLEE_SAVE-FRAME_SIZE_REFS_ONLY_CALLEE_SAVE
 
-.Lentry_error:
-    mov sp, r10
-    .cfi_def_cfa_register sp
-    mov r9, r11
 .Lexception_in_native:
-    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
+    ldr sp, [r9, #THREAD_TOP_QUICK_FRAME_OFFSET]
+    .cfi_def_cfa_register sp
+    # This will create a new save-all frame, required by the runtime.
     DELIVER_PENDING_EXCEPTION
-
 END art_quick_generic_jni_trampoline
 
     .extern artQuickToInterpreterBridge
diff --git a/runtime/arch/arm64/quick_entrypoints_arm64.S b/runtime/arch/arm64/quick_entrypoints_arm64.S
index 770073b5..ec25a33 100644
--- a/runtime/arch/arm64/quick_entrypoints_arm64.S
+++ b/runtime/arch/arm64/quick_entrypoints_arm64.S
@@ -1510,7 +1510,7 @@
     // x1: pointer to the bottom of the used area of the alloca, can restore stack till there.
 
     // Check for error = 0.
-    cbz x0, .Lentry_error
+    cbz x0, .Lexception_in_native
 
     // Release part of the alloca.
     mov sp, x1
@@ -1545,15 +1545,15 @@
 
     bl artQuickGenericJniEndTrampoline
 
+    // Pending exceptions possible.
+    // Use xETR as xSELF might be scratched by native code
+    ldr x2, [xETR, THREAD_EXCEPTION_OFFSET]
+    cbnz x2, .Lexception_in_native
+
     // Tear down the alloca.
     mov sp, x28
     .cfi_def_cfa_register sp
 
-    // Pending exceptions possible.
-    // Use xETR as xSELF might be scratched by native code
-    ldr x1, [xETR, THREAD_EXCEPTION_OFFSET]
-    cbnz x1, .Lexception_in_native
-
     // Tear down the callee-save frame.
     RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
 
@@ -1561,13 +1561,15 @@
     fmov d0, x0
     ret
 
-.Lentry_error:
-    mov sp, x28
-    .cfi_def_cfa_register sp
 .Lexception_in_native:
-    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
+    // Restore xSELF. It might have been scratched by native code.
+    mov xSELF, xETR
+    // Move to x1 then sp to please assembler.
+    ldr x1, [xSELF, # THREAD_TOP_QUICK_FRAME_OFFSET]
+    mov sp, x1
+    .cfi_def_cfa_register sp
+    # This will create a new save-all frame, required by the runtime.
     DELIVER_PENDING_EXCEPTION
-
 END art_quick_generic_jni_trampoline
 
 /*
diff --git a/runtime/arch/mips/quick_entrypoints_mips.S b/runtime/arch/mips/quick_entrypoints_mips.S
index df2feb7..16f0e70 100644
--- a/runtime/arch/mips/quick_entrypoints_mips.S
+++ b/runtime/arch/mips/quick_entrypoints_mips.S
@@ -1142,10 +1142,10 @@
     addiu   $sp, $sp, -24          # reserve arg slots
     jal     artQuickGenericJniEndTrampoline
     s.d     $f0, 16($sp)           # pass result_f
-    addiu   $sp, $sp, 24           # remove arg slots
 
     lw      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
-    bne     $t0, $zero, 2f         # check for pending exceptions
+    bne     $t0, $zero, 1f         # check for pending exceptions
+
     move    $sp, $s8               # tear down the alloca
 
     # tear dpown the callee-save frame
@@ -1156,9 +1156,8 @@
     nop
 
 1:
-    move    $sp, $s8               # tear down the alloca
-2:
-    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
+    lw $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)
+    # This will create a new save-all frame, required by the runtime.
     DELIVER_PENDING_EXCEPTION
 END art_quick_generic_jni_trampoline
 
diff --git a/runtime/arch/mips64/quick_entrypoints_mips64.S b/runtime/arch/mips64/quick_entrypoints_mips64.S
index 60e692b..6f1b826 100644
--- a/runtime/arch/mips64/quick_entrypoints_mips64.S
+++ b/runtime/arch/mips64/quick_entrypoints_mips64.S
@@ -849,7 +849,7 @@
     dmfc1   $a2, $f0
 
     ld      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
-    bne     $t0, $zero, 2f         # check for pending exceptions
+    bne     $t0, $zero, 1f         # check for pending exceptions
     move    $sp, $s8               # tear down the alloca
 
     # tear dpown the callee-save frame
@@ -859,9 +859,8 @@
     dmtc1   $v0, $f0               # place return value to FP return value
 
 1:
-    move    $sp, $s8               # tear down the alloca
-2:
-    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
+    ld      $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)
+    # This will create a new save-all frame, required by the runtime.
     DELIVER_PENDING_EXCEPTION
 END art_quick_generic_jni_trampoline
 
diff --git a/runtime/arch/x86/quick_entrypoints_x86.S b/runtime/arch/x86/quick_entrypoints_x86.S
index c2acdd1..47bc5ea 100644
--- a/runtime/arch/x86/quick_entrypoints_x86.S
+++ b/runtime/arch/x86/quick_entrypoints_x86.S
@@ -1349,7 +1349,7 @@
 
     // Check for error = 0.
     test %eax, %eax
-    jz .Lentry_error
+    jz .Lexception_in_native
 
     // Release part of the alloca.
     movl %edx, %esp
@@ -1371,15 +1371,16 @@
     pushl %fs:THREAD_SELF_OFFSET  // Pass Thread::Current().
     call SYMBOL(artQuickGenericJniEndTrampoline)
 
-    // Tear down the alloca.
-    movl %ebp, %esp
-    CFI_DEF_CFA_REGISTER(esp)
-
     // Pending exceptions possible.
     mov %fs:THREAD_EXCEPTION_OFFSET, %ebx
     testl %ebx, %ebx
     jnz .Lexception_in_native
 
+    // Tear down the alloca.
+    movl %ebp, %esp
+    CFI_DEF_CFA_REGISTER(esp)
+
+
     // Tear down the callee-save frame.
     // Remove space for FPR args and EAX
     addl LITERAL(4 + 4 * 8), %esp
@@ -1397,11 +1398,11 @@
     movd %edx, %xmm1
     punpckldq %xmm1, %xmm0
     ret
-.Lentry_error:
-    movl %ebp, %esp
-    CFI_DEF_CFA_REGISTER(esp)
 .Lexception_in_native:
-    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
+    movl %fs:THREAD_TOP_QUICK_FRAME_OFFSET, %esp
+    // Do a call to push a new save-all frame required by the runtime.
+    call .Lexception_call
+.Lexception_call:
     DELIVER_PENDING_EXCEPTION
 END_FUNCTION art_quick_generic_jni_trampoline
 
diff --git a/runtime/arch/x86_64/quick_entrypoints_x86_64.S b/runtime/arch/x86_64/quick_entrypoints_x86_64.S
index c865541..406126b 100644
--- a/runtime/arch/x86_64/quick_entrypoints_x86_64.S
+++ b/runtime/arch/x86_64/quick_entrypoints_x86_64.S
@@ -1361,7 +1361,7 @@
 
     // Check for error = 0.
     test %rax, %rax
-    jz .Lentry_error
+    jz .Lexception_in_native
 
     // Release part of the alloca.
     movq %rdx, %rsp
@@ -1398,16 +1398,16 @@
     movq %xmm0, %rdx
     call SYMBOL(artQuickGenericJniEndTrampoline)
 
-    // Tear down the alloca.
-    movq %rbp, %rsp
-    CFI_DEF_CFA_REGISTER(rsp)
-
     // Pending exceptions possible.
     // TODO: use cmpq, needs direct encoding because of gas bug
     movq %gs:THREAD_EXCEPTION_OFFSET, %rcx
     test %rcx, %rcx
     jnz .Lexception_in_native
 
+    // Tear down the alloca.
+    movq %rbp, %rsp
+    CFI_DEF_CFA_REGISTER(rsp)
+
     // Tear down the callee-save frame.
     // Load FPRs.
     // movq %xmm0, 16(%rsp)         // doesn't make sense!!!
@@ -1440,40 +1440,12 @@
     // store into fpr, for when it's a fpr return...
     movq %rax, %xmm0
     ret
-.Lentry_error:
-    movq %rbp, %rsp
-    CFI_DEF_CFA_REGISTER(rsp)
 .Lexception_in_native:
-    // TODO: the handle scope contains the this pointer which is used by the debugger for exception
-    //       delivery.
-    movq %xmm0, 16(%rsp)         // doesn't make sense!!!
-    movq 24(%rsp), %xmm1            // neither does this!!!
-    movq 32(%rsp), %xmm2
-    movq 40(%rsp), %xmm3
-    movq 48(%rsp), %xmm4
-    movq 56(%rsp), %xmm5
-    movq 64(%rsp), %xmm6
-    movq 72(%rsp), %xmm7
-    movq 80(%rsp), %xmm12
-    movq 88(%rsp), %xmm13
-    movq 96(%rsp), %xmm14
-    movq 104(%rsp), %xmm15
-    // was 80 + 32 bytes
-    addq LITERAL(80 + 4*8), %rsp
-    CFI_ADJUST_CFA_OFFSET(-80 - 4*8)
-    // Save callee and GPR args, mixed together to agree with core spills bitmap.
-    POP rcx  // Arg.
-    POP rdx  // Arg.
-    POP rbx  // Callee save.
-    POP rbp  // Callee save.
-    POP rsi  // Arg.
-    POP r8   // Arg.
-    POP r9   // Arg.
-    POP r12  // Callee save.
-    POP r13  // Callee save.
-    POP r14  // Callee save.
-    POP r15  // Callee save.
-
+    movq %gs:THREAD_TOP_QUICK_FRAME_OFFSET, %rsp
+    CFI_DEF_CFA_REGISTER(rsp)
+    // Do a call to push a new save-all frame required by the runtime.
+    call .Lexception_call
+.Lexception_call:
     DELIVER_PENDING_EXCEPTION
 END_FUNCTION art_quick_generic_jni_trampoline