michael@0: // Copyright (c) 2012, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // A minimalistic implementation of getcontext() to be used by michael@0: // Google Breakpad on Android. michael@0: michael@0: #include "common/android/ucontext_constants.h" michael@0: michael@0: /* int getcontext (ucontext_t *ucp) */ michael@0: michael@0: #ifdef __arm__ michael@0: michael@0: .text michael@0: .global breakpad_getcontext michael@0: .hidden breakpad_getcontext michael@0: .type breakpad_getcontext, #function michael@0: .align 0 michael@0: .fnstart michael@0: breakpad_getcontext: michael@0: michael@0: /* First, save r4-r11 */ michael@0: add r1, r0, #(MCONTEXT_GREGS_OFFSET + 4*4) michael@0: stm r1, {r4-r11} michael@0: michael@0: /* r12 is a scratch register, don't save it */ michael@0: michael@0: /* Save sp and lr explicitely. */ michael@0: /* - sp can't be stored with stmia in Thumb-2 */ michael@0: /* - STM instructions that store sp and pc are deprecated in ARM */ michael@0: str sp, [r0, #(MCONTEXT_GREGS_OFFSET + 13*4)] michael@0: str lr, [r0, #(MCONTEXT_GREGS_OFFSET + 14*4)] michael@0: michael@0: /* Save the caller's address in 'pc' */ michael@0: str lr, [r0, #(MCONTEXT_GREGS_OFFSET + 15*4)] michael@0: michael@0: /* Save ucontext_t* pointer accross next call */ michael@0: mov r4, r0 michael@0: michael@0: /* Call sigprocmask(SIG_BLOCK, NULL, &(ucontext->uc_sigmask)) */ michael@0: mov r0, #0 /* SIG_BLOCK */ michael@0: mov r1, #0 /* NULL */ michael@0: add r2, r4, #UCONTEXT_SIGMASK_OFFSET michael@0: bl sigprocmask(PLT) michael@0: michael@0: /* Intentionally do not save the FPU state here. This is because on michael@0: * Linux/ARM, one should instead use ptrace(PTRACE_GETFPREGS) or michael@0: * ptrace(PTRACE_GETVFPREGS) to get it. michael@0: * michael@0: * Note that a real implementation of getcontext() would need to save michael@0: * this here to allow setcontext()/swapcontext() to work correctly. michael@0: */ michael@0: michael@0: /* Restore the values of r4 and lr */ michael@0: mov r0, r4 michael@0: ldr lr, [r0, #(MCONTEXT_GREGS_OFFSET + 14*4)] michael@0: ldr r4, [r0, #(MCONTEXT_GREGS_OFFSET + 4*4)] michael@0: michael@0: /* Return 0 */ michael@0: mov r0, #0 michael@0: bx lr michael@0: michael@0: .fnend michael@0: .size breakpad_getcontext, . - breakpad_getcontext michael@0: michael@0: #elif defined(__i386__) michael@0: michael@0: .text michael@0: .global breakpad_getcontext michael@0: .hidden breakpad_getcontext michael@0: .align 4 michael@0: .type breakpad_getcontext, @function michael@0: michael@0: breakpad_getcontext: michael@0: michael@0: movl 4(%esp), %eax /* eax = uc */ michael@0: michael@0: /* Save register values */ michael@0: movl %ecx, MCONTEXT_ECX_OFFSET(%eax) michael@0: movl %edx, MCONTEXT_EDX_OFFSET(%eax) michael@0: movl %ebx, MCONTEXT_EBX_OFFSET(%eax) michael@0: movl %edi, MCONTEXT_EDI_OFFSET(%eax) michael@0: movl %esi, MCONTEXT_ESI_OFFSET(%eax) michael@0: movl %ebp, MCONTEXT_EBP_OFFSET(%eax) michael@0: michael@0: movl (%esp), %edx /* return address */ michael@0: lea 4(%esp), %ecx /* exclude return address from stack */ michael@0: mov %edx, MCONTEXT_EIP_OFFSET(%eax) michael@0: mov %ecx, MCONTEXT_ESP_OFFSET(%eax) michael@0: michael@0: xorl %ecx, %ecx michael@0: movw %fs, %cx michael@0: mov %ecx, MCONTEXT_FS_OFFSET(%eax) michael@0: michael@0: movl $0, MCONTEXT_EAX_OFFSET(%eax) michael@0: michael@0: /* Save floating point state to fpregstate, then update michael@0: * the fpregs pointer to point to it */ michael@0: leal UCONTEXT_FPREGS_MEM_OFFSET(%eax), %ecx michael@0: fnstenv (%ecx) michael@0: fldenv (%ecx) michael@0: mov %ecx, UCONTEXT_FPREGS_OFFSET(%eax) michael@0: michael@0: /* Save signal mask: sigprocmask(SIGBLOCK, NULL, &uc->uc_sigmask) */ michael@0: leal UCONTEXT_SIGMASK_OFFSET(%eax), %edx michael@0: xorl %ecx, %ecx michael@0: push %edx /* &uc->uc_sigmask */ michael@0: push %ecx /* NULL */ michael@0: push %ecx /* SIGBLOCK == 0 on i386 */ michael@0: call sigprocmask@PLT michael@0: addl $12, %esp michael@0: michael@0: movl $0, %eax michael@0: ret michael@0: michael@0: .size breakpad_getcontext, . - breakpad_getcontext michael@0: michael@0: #else michael@0: #error "This file has not been ported for your CPU!" michael@0: #endif