|
1 // Copyright (c) 2012, Google Inc. |
|
2 // All rights reserved. |
|
3 // |
|
4 // Redistribution and use in source and binary forms, with or without |
|
5 // modification, are permitted provided that the following conditions are |
|
6 // met: |
|
7 // |
|
8 // * Redistributions of source code must retain the above copyright |
|
9 // notice, this list of conditions and the following disclaimer. |
|
10 // * Redistributions in binary form must reproduce the above |
|
11 // copyright notice, this list of conditions and the following disclaimer |
|
12 // in the documentation and/or other materials provided with the |
|
13 // distribution. |
|
14 // * Neither the name of Google Inc. nor the names of its |
|
15 // contributors may be used to endorse or promote products derived from |
|
16 // this software without specific prior written permission. |
|
17 // |
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
|
30 // A minimalistic implementation of getcontext() to be used by |
|
31 // Google Breakpad on Android. |
|
32 |
|
33 #include "common/android/ucontext_constants.h" |
|
34 |
|
35 /* int getcontext (ucontext_t *ucp) */ |
|
36 |
|
37 #ifdef __arm__ |
|
38 |
|
39 .text |
|
40 .global breakpad_getcontext |
|
41 .hidden breakpad_getcontext |
|
42 .type breakpad_getcontext, #function |
|
43 .align 0 |
|
44 .fnstart |
|
45 breakpad_getcontext: |
|
46 |
|
47 /* First, save r4-r11 */ |
|
48 add r1, r0, #(MCONTEXT_GREGS_OFFSET + 4*4) |
|
49 stm r1, {r4-r11} |
|
50 |
|
51 /* r12 is a scratch register, don't save it */ |
|
52 |
|
53 /* Save sp and lr explicitely. */ |
|
54 /* - sp can't be stored with stmia in Thumb-2 */ |
|
55 /* - STM instructions that store sp and pc are deprecated in ARM */ |
|
56 str sp, [r0, #(MCONTEXT_GREGS_OFFSET + 13*4)] |
|
57 str lr, [r0, #(MCONTEXT_GREGS_OFFSET + 14*4)] |
|
58 |
|
59 /* Save the caller's address in 'pc' */ |
|
60 str lr, [r0, #(MCONTEXT_GREGS_OFFSET + 15*4)] |
|
61 |
|
62 /* Save ucontext_t* pointer accross next call */ |
|
63 mov r4, r0 |
|
64 |
|
65 /* Call sigprocmask(SIG_BLOCK, NULL, &(ucontext->uc_sigmask)) */ |
|
66 mov r0, #0 /* SIG_BLOCK */ |
|
67 mov r1, #0 /* NULL */ |
|
68 add r2, r4, #UCONTEXT_SIGMASK_OFFSET |
|
69 bl sigprocmask(PLT) |
|
70 |
|
71 /* Intentionally do not save the FPU state here. This is because on |
|
72 * Linux/ARM, one should instead use ptrace(PTRACE_GETFPREGS) or |
|
73 * ptrace(PTRACE_GETVFPREGS) to get it. |
|
74 * |
|
75 * Note that a real implementation of getcontext() would need to save |
|
76 * this here to allow setcontext()/swapcontext() to work correctly. |
|
77 */ |
|
78 |
|
79 /* Restore the values of r4 and lr */ |
|
80 mov r0, r4 |
|
81 ldr lr, [r0, #(MCONTEXT_GREGS_OFFSET + 14*4)] |
|
82 ldr r4, [r0, #(MCONTEXT_GREGS_OFFSET + 4*4)] |
|
83 |
|
84 /* Return 0 */ |
|
85 mov r0, #0 |
|
86 bx lr |
|
87 |
|
88 .fnend |
|
89 .size breakpad_getcontext, . - breakpad_getcontext |
|
90 |
|
91 #elif defined(__i386__) |
|
92 |
|
93 .text |
|
94 .global breakpad_getcontext |
|
95 .hidden breakpad_getcontext |
|
96 .align 4 |
|
97 .type breakpad_getcontext, @function |
|
98 |
|
99 breakpad_getcontext: |
|
100 |
|
101 movl 4(%esp), %eax /* eax = uc */ |
|
102 |
|
103 /* Save register values */ |
|
104 movl %ecx, MCONTEXT_ECX_OFFSET(%eax) |
|
105 movl %edx, MCONTEXT_EDX_OFFSET(%eax) |
|
106 movl %ebx, MCONTEXT_EBX_OFFSET(%eax) |
|
107 movl %edi, MCONTEXT_EDI_OFFSET(%eax) |
|
108 movl %esi, MCONTEXT_ESI_OFFSET(%eax) |
|
109 movl %ebp, MCONTEXT_EBP_OFFSET(%eax) |
|
110 |
|
111 movl (%esp), %edx /* return address */ |
|
112 lea 4(%esp), %ecx /* exclude return address from stack */ |
|
113 mov %edx, MCONTEXT_EIP_OFFSET(%eax) |
|
114 mov %ecx, MCONTEXT_ESP_OFFSET(%eax) |
|
115 |
|
116 xorl %ecx, %ecx |
|
117 movw %fs, %cx |
|
118 mov %ecx, MCONTEXT_FS_OFFSET(%eax) |
|
119 |
|
120 movl $0, MCONTEXT_EAX_OFFSET(%eax) |
|
121 |
|
122 /* Save floating point state to fpregstate, then update |
|
123 * the fpregs pointer to point to it */ |
|
124 leal UCONTEXT_FPREGS_MEM_OFFSET(%eax), %ecx |
|
125 fnstenv (%ecx) |
|
126 fldenv (%ecx) |
|
127 mov %ecx, UCONTEXT_FPREGS_OFFSET(%eax) |
|
128 |
|
129 /* Save signal mask: sigprocmask(SIGBLOCK, NULL, &uc->uc_sigmask) */ |
|
130 leal UCONTEXT_SIGMASK_OFFSET(%eax), %edx |
|
131 xorl %ecx, %ecx |
|
132 push %edx /* &uc->uc_sigmask */ |
|
133 push %ecx /* NULL */ |
|
134 push %ecx /* SIGBLOCK == 0 on i386 */ |
|
135 call sigprocmask@PLT |
|
136 addl $12, %esp |
|
137 |
|
138 movl $0, %eax |
|
139 ret |
|
140 |
|
141 .size breakpad_getcontext, . - breakpad_getcontext |
|
142 |
|
143 #else |
|
144 #error "This file has not been ported for your CPU!" |
|
145 #endif |