1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_ARCH_ARM_ASM_SUPPORT_ARM_S_
18#define ART_RUNTIME_ARCH_ARM_ASM_SUPPORT_ARM_S_
19
20#include "asm_support_arm.h"
21
22// Define special registers.
23
24// Register holding suspend check count down.
25#define rSUSPEND r4
26// Register holding Thread::Current().
27#define rSELF r9
28
29#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
30// Marking Register, holding Thread::Current()->GetIsGcMarking().
31// Only used with the Concurrent Copying (CC) garbage
32// collector, with the Baker read barrier configuration.
33#define rMR r8
34#endif
35
36.syntax unified
37.arch armv7-a
38.thumb
39
40// Macro to generate the value of Runtime::Current into rDest. As it uses labels
41// then the labels need to be unique. We bind these to the function name in the ENTRY macros.
42.macro RUNTIME_CURRENT name, num, rDest
43    .if .Lruntime_current\num\()_used
44         .error
45    .endif
46    .set .Lruntime_current\num\()_used, 1
47    ldr \rDest, .Lruntime_instance_\name\()_\num  @ Load GOT_PREL offset of Runtime::instance_.
48.Lload_got_\name\()_\num\():
49    add \rDest, pc                                @ Fixup GOT_PREL address.
50    ldr \rDest, [\rDest]                          @ Load address of Runtime::instance_.
51    ldr \rDest, [\rDest]                          @ Load Runtime::instance_.
52.endm
53
54// Common ENTRY declaration code for ARM and thumb, an ENTRY should always be paired with an END.
55// Declares the RUNTIME_CURRENT[123] macros that can be used within an ENTRY and will have literals
56// generated at END.
57.macro DEF_ENTRY thumb_or_arm, name, alignment
58    \thumb_or_arm
59// Clang ignores .thumb_func and requires an explicit .thumb. Investigate whether we should still
60// carry around the .thumb_func.
61    .ifc \thumb_or_arm, .thumb_func
62        .thumb
63    .endif
64    .type \name, #function
65    .hidden \name  // Hide this as a global symbol, so we do not incur plt calls.
66    .global \name
67    // ART-compiled functions have OatQuickMethodHeader but assembly funtions do not.
68    // Prefix the assembly code with 0xFFs, which means there is no method header.
69    .byte 0xFF, 0xFF, 0xFF, 0xFF
70    // Cache alignment for function entry.
71    // NB: 0xFF because there is a bug in balign where 0x00 creates nop instructions.
72    .balign \alignment, 0xFF
73\name:
74    .cfi_startproc
75    .fnstart
76    // Track whether RUNTIME_CURRENT was used.
77    .set .Lruntime_current1_used, 0
78    .set .Lruntime_current2_used, 0
79    .set .Lruntime_current3_used, 0
80    // The RUNTIME_CURRENT macros that are bound to the \name argument of DEF_ENTRY to ensure
81    // that label names are unique.
82    .macro RUNTIME_CURRENT1 rDest
83        RUNTIME_CURRENT \name, 1, \rDest
84    .endm
85    .macro RUNTIME_CURRENT2 rDest
86        RUNTIME_CURRENT \name, 2, \rDest
87    .endm
88    .macro RUNTIME_CURRENT3 rDest
89        RUNTIME_CURRENT \name, 3, \rDest
90    .endm
91.endm
92
93// A thumb2 style ENTRY.
94.macro ENTRY name
95    DEF_ENTRY .thumb_func, \name, 16
96.endm
97.macro ENTRY_ALIGNED name, alignment
98    DEF_ENTRY .thumb_func, \name, \alignment
99.endm
100
101// A ARM style ENTRY.
102.macro ARM_ENTRY name
103    DEF_ENTRY .arm, \name, 16
104.endm
105
106// Terminate an ENTRY and generate GOT_PREL references.
107.macro END name
108     // Generate offsets of GOT and Runtime::instance_ used in RUNTIME_CURRENT.
109     .if .Lruntime_current1_used
110         .Lruntime_instance_\name\()_1:
111             .word   _ZN3art7Runtime9instance_E(GOT_PREL)-(.Lload_got_\name\()_1+4)
112     .endif
113     .if .Lruntime_current2_used
114         .Lruntime_instance_\name\()_2:
115             .word   _ZN3art7Runtime9instance_E(GOT_PREL)-(.Lload_got_\name\()_2+4)
116    .endif
117     .if .Lruntime_current3_used
118         .Lruntime_instance_\name\()_3:
119             .word   _ZN3art7Runtime9instance_E(GOT_PREL)-(.Lload_got_\name\()_3+4)
120    .endif
121    // Remove the RUNTIME_CURRENTx macros so they get rebound in the next function entry.
122    .purgem RUNTIME_CURRENT1
123    .purgem RUNTIME_CURRENT2
124    .purgem RUNTIME_CURRENT3
125    .fnend
126    .cfi_endproc
127    .size \name, .-\name
128.endm
129
130// Declare an unimplemented ENTRY that will halt a debugger.
131.macro UNIMPLEMENTED name
132    ENTRY \name
133    bkpt
134    bkpt
135    END \name
136.endm
137
138// Macro to poison (negate) the reference for heap poisoning.
139.macro POISON_HEAP_REF rRef
140#ifdef USE_HEAP_POISONING
141    rsb \rRef, \rRef, #0
142#endif  // USE_HEAP_POISONING
143.endm
144
145// Macro to unpoison (negate) the reference for heap poisoning.
146.macro UNPOISON_HEAP_REF rRef
147#ifdef USE_HEAP_POISONING
148    rsb \rRef, \rRef, #0
149#endif  // USE_HEAP_POISONING
150.endm
151
152// Macro to refresh the Marking Register (R8).
153//
154// This macro must be called at the end of functions implementing
155// entrypoints that possibly (directly or indirectly) perform a
156// suspend check (before they return).
157.macro REFRESH_MARKING_REGISTER
158#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
159    ldr rMR, [rSELF, #THREAD_IS_GC_MARKING_OFFSET]
160#endif
161.endm
162
163    /*
164     * Macro that sets up the callee save frame to conform with
165     * Runtime::CreateCalleeSaveMethod(kSaveRefsAndArgs), except for storing the method.
166     */
167.macro SETUP_SAVE_REFS_AND_ARGS_FRAME_REGISTERS_ONLY
168    // Note: We could avoid saving R8 in the case of Baker read
169    // barriers, as it is overwritten by REFRESH_MARKING_REGISTER
170    // later; but it's not worth handling this special case.
171    push {r1-r3, r5-r8, r10-r11, lr}   @ 10 words of callee saves and args.
172    .cfi_adjust_cfa_offset 40
173    .cfi_rel_offset r1, 0
174    .cfi_rel_offset r2, 4
175    .cfi_rel_offset r3, 8
176    .cfi_rel_offset r5, 12
177    .cfi_rel_offset r6, 16
178    .cfi_rel_offset r7, 20
179    .cfi_rel_offset r8, 24
180    .cfi_rel_offset r10, 28
181    .cfi_rel_offset r11, 32
182    .cfi_rel_offset lr, 36
183    vpush {s0-s15}                     @ 16 words of float args.
184    .cfi_adjust_cfa_offset 64
185    sub sp, #8                         @ 2 words of space, alignment padding and Method*
186    .cfi_adjust_cfa_offset 8
187    // Ugly compile-time check, but we only have the preprocessor.
188#if (FRAME_SIZE_SAVE_REFS_AND_ARGS != 40 + 64 + 8)
189#error "FRAME_SIZE_SAVE_REFS_AND_ARGS(ARM) size not as expected."
190#endif
191.endm
192
193.macro RESTORE_SAVE_REFS_AND_ARGS_FRAME
194    add  sp, #8                      @ rewind sp
195    .cfi_adjust_cfa_offset -8
196    vpop {s0-s15}
197    .cfi_adjust_cfa_offset -64
198    // Note: Likewise, we could avoid restoring R8 in the case of Baker
199    // read barriers, as it is overwritten by REFRESH_MARKING_REGISTER
200    // later; but it's not worth handling this special case.
201    pop {r1-r3, r5-r8, r10-r11, lr}  @ 10 words of callee saves and args.
202    .cfi_restore r1
203    .cfi_restore r2
204    .cfi_restore r3
205    .cfi_restore r5
206    .cfi_restore r6
207    .cfi_restore r7
208    .cfi_restore r8
209    .cfi_restore r10
210    .cfi_restore r11
211    .cfi_restore lr
212    .cfi_adjust_cfa_offset -40
213.endm
214
215    /*
216     * Macro to spill the GPRs.
217     */
218.macro SPILL_ALL_CALLEE_SAVE_GPRS
219    push {r4-r11, lr}                             @ 9 words (36 bytes) of callee saves.
220    .cfi_adjust_cfa_offset 36
221    .cfi_rel_offset r4, 0
222    .cfi_rel_offset r5, 4
223    .cfi_rel_offset r6, 8
224    .cfi_rel_offset r7, 12
225    .cfi_rel_offset r8, 16
226    .cfi_rel_offset r9, 20
227    .cfi_rel_offset r10, 24
228    .cfi_rel_offset r11, 28
229    .cfi_rel_offset lr, 32
230.endm
231
232    /*
233     * Macro that sets up the callee save frame to conform with
234     * Runtime::CreateCalleeSaveMethod(kSaveAllCalleeSaves)
235     */
236.macro SETUP_SAVE_ALL_CALLEE_SAVES_FRAME rTemp
237    SPILL_ALL_CALLEE_SAVE_GPRS                    @ 9 words (36 bytes) of callee saves.
238    vpush {s16-s31}                               @ 16 words (64 bytes) of floats.
239    .cfi_adjust_cfa_offset 64
240    sub sp, #12                                   @ 3 words of space, bottom word will hold Method*
241    .cfi_adjust_cfa_offset 12
242    RUNTIME_CURRENT1 \rTemp                       @ Load Runtime::Current into rTemp.
243    @ Load kSaveAllCalleeSaves Method* into rTemp.
244    ldr \rTemp, [\rTemp, #RUNTIME_SAVE_ALL_CALLEE_SAVES_METHOD_OFFSET]
245    str \rTemp, [sp, #0]                          @ Place Method* at bottom of stack.
246    str sp, [rSELF, #THREAD_TOP_QUICK_FRAME_OFFSET]  @ Place sp in Thread::Current()->top_quick_frame.
247
248     // Ugly compile-time check, but we only have the preprocessor.
249#if (FRAME_SIZE_SAVE_ALL_CALLEE_SAVES != 36 + 64 + 12)
250#error "FRAME_SIZE_SAVE_ALL_CALLEE_SAVES(ARM) size not as expected."
251#endif
252.endm
253
254    /*
255     * Macro that calls through to artDeliverPendingExceptionFromCode, where the pending
256     * exception is Thread::Current()->exception_ when the runtime method frame is ready.
257     */
258.macro DELIVER_PENDING_EXCEPTION_FRAME_READY
259    mov    r0, rSELF                           @ pass Thread::Current
260    bl     artDeliverPendingExceptionFromCode  @ artDeliverPendingExceptionFromCode(Thread*)
261.endm
262
263    /*
264     * Macro that calls through to artDeliverPendingExceptionFromCode, where the pending
265     * exception is Thread::Current()->exception_.
266     */
267.macro DELIVER_PENDING_EXCEPTION
268    SETUP_SAVE_ALL_CALLEE_SAVES_FRAME r0       @ save callee saves for throw
269    DELIVER_PENDING_EXCEPTION_FRAME_READY
270.endm
271
272#endif  // ART_RUNTIME_ARCH_X86_ASM_SUPPORT_X86_S_
273