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_X86_64_ASM_SUPPORT_X86_64_S_
18#define ART_RUNTIME_ARCH_X86_64_ASM_SUPPORT_X86_64_S_
19
20#include "asm_support_x86_64.h"
21
22// Regular gas(1) & current clang/llvm assembler support named macro parameters.
23#define MACRO0(macro_name) .macro macro_name
24#define MACRO1(macro_name, macro_arg1) .macro macro_name macro_arg1
25#define MACRO2(macro_name, macro_arg1, macro_arg2) .macro macro_name macro_arg1, macro_arg2
26#define MACRO3(macro_name, macro_arg1, macro_arg2, macro_arg3) .macro macro_name macro_arg1, macro_arg2, macro_arg3
27#define MACRO4(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4) .macro macro_name macro_arg1, macro_arg2, macro_arg3, macro_arg4
28#define END_MACRO .endm
29
30#if defined(__clang__)
31    // Clang/llvm does not support .altmacro. However, the clang/llvm preprocessor doesn't
32    // separate the backslash and parameter by a space. Everything just works.
33    #define RAW_VAR(name) \name
34    #define VAR(name) \name
35    #define CALLVAR(name) SYMBOL(\name)
36    #define PLT_VAR(name) \name@PLT
37    #define REG_VAR(name) %\name
38    #define CALL_MACRO(name) \name
39#else
40    // Regular gas(1) uses \argument_name for macro arguments.
41    // We need to turn on alternate macro syntax so we can use & instead or the preprocessor
42    // will screw us by inserting a space between the \ and the name. Even in this mode there's
43    // no special meaning to $, so literals are still just $x. The use of altmacro means % is a
44    // special character meaning care needs to be taken when passing registers as macro
45    // arguments.
46    .altmacro
47    #define RAW_VAR(name) name&
48    #define VAR(name) name&
49    #define CALLVAR(name) SYMBOL(name&)
50    #define PLT_VAR(name) name&@PLT
51    #define REG_VAR(name) %name
52    #define CALL_MACRO(name) name&
53#endif
54
55#define LITERAL(value) $value
56#if defined(__APPLE__)
57    #define MACRO_LITERAL(value) $(value)
58#else
59    #define MACRO_LITERAL(value) $value
60#endif
61
62#if defined(__APPLE__)
63    #define FUNCTION_TYPE(name)
64    #define SIZE(name)
65#else
66    #define FUNCTION_TYPE(name) .type name, @function
67    #define SIZE(name) .size name, .-name
68#endif
69
70    // CFI support.
71#if !defined(__APPLE__)
72    #define CFI_STARTPROC .cfi_startproc
73    #define CFI_ENDPROC .cfi_endproc
74    #define CFI_ADJUST_CFA_OFFSET(size) .cfi_adjust_cfa_offset size
75    #define CFI_DEF_CFA(reg,size) .cfi_def_cfa reg,size
76    #define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg
77    #define CFI_RESTORE(reg) .cfi_restore reg
78    #define CFI_REL_OFFSET(reg,size) .cfi_rel_offset reg,size
79    #define CFI_REMEMBER_STATE .cfi_remember_state
80    // The spec is not clear whether the CFA is part of the saved state and tools
81    // differ in the behaviour, so explicitly set the CFA to avoid any ambiguity.
82    // The restored CFA state should match the CFA state during CFI_REMEMBER_STATE.
83    // `objdump -Wf libart.so | egrep "_cfa|_state"` is useful to audit the opcodes.
84    #define CFI_RESTORE_STATE_AND_DEF_CFA(reg,off) .cfi_restore_state .cfi_def_cfa reg,off
85    #define CFI_RESTORE_STATE .cfi_restore_state
86#else
87    // Mac OS' doesn't like cfi_* directives.
88    #define CFI_STARTPROC
89    #define CFI_ENDPROC
90    #define CFI_ADJUST_CFA_OFFSET(size)
91    #define CFI_DEF_CFA(reg,size)
92    #define CFI_DEF_CFA_REGISTER(reg)
93    #define CFI_RESTORE(reg)
94    #define CFI_REL_OFFSET(reg,size)
95    #define CFI_REMEMBER_STATE
96    #define CFI_RESTORE_STATE_AND_DEF_CFA(off)
97    #define CFI_RESTORE_STATE
98#endif
99
100    // Symbols.
101#if !defined(__APPLE__)
102    #define SYMBOL(name) name
103    #define PLT_SYMBOL(name) name ## @PLT
104#else
105    #define SYMBOL(name) _ ## name
106    #define PLT_SYMBOL(name) _ ## name
107#endif
108
109// Directive to hide a function symbol.
110#if defined(__APPLE__)
111    #define ASM_HIDDEN .private_extern
112#else
113    #define ASM_HIDDEN .hidden
114#endif
115
116    /* Cache alignment for function entry */
117MACRO0(ALIGN_FUNCTION_ENTRY)
118    // ART-compiled functions have OatQuickMethodHeader but assembly funtions do not.
119    // Prefix the assembly code with 0xFFs, which means there is no method header.
120    .byte 0xFF, 0xFF, 0xFF, 0xFF
121    // Cache alignment for function entry.
122    .balign 16, 0xFF
123END_MACRO
124
125// TODO: we might need to use SYMBOL() here to add the underscore prefix
126// for mac builds.
127MACRO2(DEFINE_FUNCTION_CUSTOM_CFA, c_name, cfa_offset)
128    FUNCTION_TYPE(SYMBOL(\c_name))
129    ASM_HIDDEN CALLVAR(c_name)
130    .globl CALLVAR(c_name)
131    ALIGN_FUNCTION_ENTRY
132CALLVAR(c_name):
133    CFI_STARTPROC
134    // Ensure we get a sane starting CFA.
135    CFI_DEF_CFA(rsp, RAW_VAR(cfa_offset))
136END_MACRO
137
138MACRO1(DEFINE_FUNCTION, c_name)
139    DEFINE_FUNCTION_CUSTOM_CFA RAW_VAR(c_name), __SIZEOF_POINTER__
140END_MACRO
141
142MACRO1(END_FUNCTION, c_name)
143    CFI_ENDPROC
144    SIZE(SYMBOL(\c_name))
145END_MACRO
146
147MACRO1(PUSH, reg)
148    pushq REG_VAR(reg)
149    CFI_ADJUST_CFA_OFFSET(8)
150    CFI_REL_OFFSET(REG_VAR(reg), 0)
151END_MACRO
152
153MACRO1(POP, reg)
154    popq REG_VAR(reg)
155    CFI_ADJUST_CFA_OFFSET(-8)
156    CFI_RESTORE(REG_VAR(reg))
157END_MACRO
158
159// Arguments do not need .cfi_rel_offset as they are caller-saved and
160// therefore cannot hold caller's variables or unwinding data.
161MACRO1(PUSH_ARG, reg)
162    pushq REG_VAR(reg)
163    CFI_ADJUST_CFA_OFFSET(8)
164END_MACRO
165
166MACRO1(POP_ARG, reg)
167    popq REG_VAR(reg)
168    CFI_ADJUST_CFA_OFFSET(-8)
169END_MACRO
170
171MACRO3(SAVE_REG_BASE, base, reg, offset)
172    movq REG_VAR(reg), RAW_VAR(offset)(REG_VAR(base))
173    CFI_REL_OFFSET(REG_VAR(reg), RAW_VAR(offset))
174END_MACRO
175
176MACRO3(RESTORE_REG_BASE, base, reg, offset)
177    movq RAW_VAR(offset)(REG_VAR(base)), REG_VAR(reg)
178    CFI_RESTORE(REG_VAR(reg))
179END_MACRO
180
181MACRO1(UNIMPLEMENTED,name)
182    FUNCTION_TYPE(SYMBOL(\name))
183    ASM_HIDDEN VAR(name)
184    .globl VAR(name)
185    ALIGN_FUNCTION_ENTRY
186VAR(name):
187    CFI_STARTPROC
188    int3
189    int3
190    CFI_ENDPROC
191    SIZE(SYMBOL(\name))
192END_MACRO
193
194MACRO0(UNREACHABLE)
195    int3
196END_MACRO
197
198MACRO0(UNTESTED)
199    int3
200END_MACRO
201
202// Macros to poison (negate) the reference for heap poisoning.
203MACRO1(POISON_HEAP_REF, rRef)
204#ifdef USE_HEAP_POISONING
205    negl REG_VAR(rRef)
206#endif  // USE_HEAP_POISONING
207END_MACRO
208
209// Macros to unpoison (negate) the reference for heap poisoning.
210MACRO1(UNPOISON_HEAP_REF, rRef)
211#ifdef USE_HEAP_POISONING
212    negl REG_VAR(rRef)
213#endif  // USE_HEAP_POISONING
214END_MACRO
215
216    /*
217     * Macro that sets up the callee save frame to conform with
218     * Runtime::CreateCalleeSaveMethod(kSaveRefsOnly)
219     */
220MACRO0(SETUP_SAVE_REFS_ONLY_FRAME)
221#if defined(__APPLE__)
222    int3
223    int3
224#else
225    // R10 := Runtime::Current()
226    movq _ZN3art7Runtime9instance_E@GOTPCREL(%rip), %r10
227    movq (%r10), %r10
228    // Save callee and GPR args, mixed together to agree with core spills bitmap.
229    PUSH r15  // Callee save.
230    PUSH r14  // Callee save.
231    PUSH r13  // Callee save.
232    PUSH r12  // Callee save.
233    PUSH rbp  // Callee save.
234    PUSH rbx  // Callee save.
235    // Create space for FPR args, plus space for ArtMethod*.
236    subq LITERAL(8 + 4 * 8), %rsp
237    CFI_ADJUST_CFA_OFFSET(8 + 4 * 8)
238    // Save FPRs.
239    movq %xmm12, 8(%rsp)
240    movq %xmm13, 16(%rsp)
241    movq %xmm14, 24(%rsp)
242    movq %xmm15, 32(%rsp)
243    // R10 := ArtMethod* for refs only callee save frame method.
244    movq RUNTIME_SAVE_REFS_ONLY_METHOD_OFFSET(%r10), %r10
245    // Store ArtMethod* to bottom of stack.
246    movq %r10, 0(%rsp)
247    // Store rsp as the stop quick frame.
248    movq %rsp, %gs:THREAD_TOP_QUICK_FRAME_OFFSET
249
250    // Ugly compile-time check, but we only have the preprocessor.
251    // Last +8: implicit return address pushed on stack when caller made call.
252#if (FRAME_SIZE_SAVE_REFS_ONLY != 6 * 8 + 4 * 8 + 8 + 8)
253#error "FRAME_SIZE_SAVE_REFS_ONLY(X86_64) size not as expected."
254#endif
255#endif  // __APPLE__
256END_MACRO
257
258MACRO0(RESTORE_SAVE_REFS_ONLY_FRAME)
259    movq 8(%rsp), %xmm12
260    movq 16(%rsp), %xmm13
261    movq 24(%rsp), %xmm14
262    movq 32(%rsp), %xmm15
263    addq LITERAL(8 + 4*8), %rsp
264    CFI_ADJUST_CFA_OFFSET(-8 - 4*8)
265    // TODO: optimize by not restoring callee-saves restored by the ABI
266    POP rbx
267    POP rbp
268    POP r12
269    POP r13
270    POP r14
271    POP r15
272END_MACRO
273
274    /*
275     * Macro that sets up the callee save frame to conform with
276     * Runtime::CreateCalleeSaveMethod(kSaveRefsAndArgs), except for storing the method.
277     */
278MACRO0(SETUP_SAVE_REFS_AND_ARGS_FRAME_REGISTERS_ONLY)
279    // Save callee and GPR args, mixed together to agree with core spills bitmap.
280    PUSH r15      // Callee save.
281    PUSH r14      // Callee save.
282    PUSH r13      // Callee save.
283    PUSH r12      // Callee save.
284    PUSH_ARG r9   // Quick arg 5.
285    PUSH_ARG r8   // Quick arg 4.
286    PUSH_ARG rsi  // Quick arg 1.
287    PUSH rbp      // Callee save.
288    PUSH rbx      // Callee save.
289    PUSH_ARG rdx  // Quick arg 2.
290    PUSH_ARG rcx  // Quick arg 3.
291    // Create space for FPR args and create 2 slots for ArtMethod*.
292    subq MACRO_LITERAL(16 + 12 * 8), %rsp
293    CFI_ADJUST_CFA_OFFSET(16 + 12 * 8)
294    // Save FPRs.
295    movq %xmm0, 16(%rsp)
296    movq %xmm1, 24(%rsp)
297    movq %xmm2, 32(%rsp)
298    movq %xmm3, 40(%rsp)
299    movq %xmm4, 48(%rsp)
300    movq %xmm5, 56(%rsp)
301    movq %xmm6, 64(%rsp)
302    movq %xmm7, 72(%rsp)
303    movq %xmm12, 80(%rsp)
304    movq %xmm13, 88(%rsp)
305    movq %xmm14, 96(%rsp)
306    movq %xmm15, 104(%rsp)
307
308    // Ugly compile-time check, but we only have the preprocessor.
309    // Last +8: implicit return address pushed on stack when caller made call.
310#if (FRAME_SIZE_SAVE_REFS_AND_ARGS != 11 * 8 + 12 * 8 + 16 + 8)
311#error "FRAME_SIZE_SAVE_REFS_AND_ARGS(X86_64) size not as expected."
312#endif
313END_MACRO
314
315MACRO0(RESTORE_SAVE_REFS_AND_ARGS_FRAME)
316    // Restore FPRs.
317    movq 16(%rsp), %xmm0
318    movq 24(%rsp), %xmm1
319    movq 32(%rsp), %xmm2
320    movq 40(%rsp), %xmm3
321    movq 48(%rsp), %xmm4
322    movq 56(%rsp), %xmm5
323    movq 64(%rsp), %xmm6
324    movq 72(%rsp), %xmm7
325    movq 80(%rsp), %xmm12
326    movq 88(%rsp), %xmm13
327    movq 96(%rsp), %xmm14
328    movq 104(%rsp), %xmm15
329    addq MACRO_LITERAL(80 + 4 * 8), %rsp
330    CFI_ADJUST_CFA_OFFSET(-(80 + 4 * 8))
331    // Restore callee and GPR args, mixed together to agree with core spills bitmap.
332    POP_ARG rcx
333    POP_ARG rdx
334    POP rbx
335    POP rbp
336    POP_ARG rsi
337    POP_ARG r8
338    POP_ARG r9
339    POP r12
340    POP r13
341    POP r14
342    POP r15
343END_MACRO
344
345    /*
346     * Macro that sets up the callee save frame to conform with
347     * Runtime::CreateCalleeSaveMethod(kSaveAllCalleeSaves)
348     */
349MACRO0(SETUP_SAVE_ALL_CALLEE_SAVES_FRAME)
350#if defined(__APPLE__)
351    int3
352    int3
353#else
354    // R10 := Runtime::Current()
355    movq _ZN3art7Runtime9instance_E@GOTPCREL(%rip), %r10
356    movq (%r10), %r10
357    // Save callee save registers to agree with core spills bitmap.
358    PUSH r15  // Callee save.
359    PUSH r14  // Callee save.
360    PUSH r13  // Callee save.
361    PUSH r12  // Callee save.
362    PUSH rbp  // Callee save.
363    PUSH rbx  // Callee save.
364    // Create space for FPR args, plus space for ArtMethod*.
365    subq MACRO_LITERAL(4 * 8 + 8), %rsp
366    CFI_ADJUST_CFA_OFFSET(4 * 8 + 8)
367    // Save FPRs.
368    movq %xmm12, 8(%rsp)
369    movq %xmm13, 16(%rsp)
370    movq %xmm14, 24(%rsp)
371    movq %xmm15, 32(%rsp)
372    // R10 := ArtMethod* for save all callee save frame method.
373    movq RUNTIME_SAVE_ALL_CALLEE_SAVES_METHOD_OFFSET(%r10), %r10
374    // Store ArtMethod* to bottom of stack.
375    movq %r10, 0(%rsp)
376    // Store rsp as the top quick frame.
377    movq %rsp, %gs:THREAD_TOP_QUICK_FRAME_OFFSET
378
379    // Ugly compile-time check, but we only have the preprocessor.
380    // Last +8: implicit return address pushed on stack when caller made call.
381#if (FRAME_SIZE_SAVE_ALL_CALLEE_SAVES != 6 * 8 + 4 * 8 + 8 + 8)
382#error "FRAME_SIZE_SAVE_ALL_CALLEE_SAVES(X86_64) size not as expected."
383#endif
384#endif  // __APPLE__
385END_MACRO
386
387MACRO0(SETUP_FP_CALLEE_SAVE_FRAME)
388    // Create space for ART FP callee-saved registers
389    subq MACRO_LITERAL(4 * 8), %rsp
390    CFI_ADJUST_CFA_OFFSET(4 * 8)
391    movq %xmm12, 0(%rsp)
392    movq %xmm13, 8(%rsp)
393    movq %xmm14, 16(%rsp)
394    movq %xmm15, 24(%rsp)
395END_MACRO
396
397MACRO0(RESTORE_FP_CALLEE_SAVE_FRAME)
398    // Restore ART FP callee-saved registers
399    movq 0(%rsp), %xmm12
400    movq 8(%rsp), %xmm13
401    movq 16(%rsp), %xmm14
402    movq 24(%rsp), %xmm15
403    addq MACRO_LITERAL(4 * 8), %rsp
404    CFI_ADJUST_CFA_OFFSET(- 4 * 8)
405END_MACRO
406
407    /*
408     * Macro that calls through to artDeliverPendingExceptionFromCode, where the pending
409     * exception is Thread::Current()->exception_ when the runtime method frame is ready.
410     */
411MACRO0(DELIVER_PENDING_EXCEPTION_FRAME_READY)
412    // (Thread*) setup
413    movq %gs:THREAD_SELF_OFFSET, %rdi
414    call SYMBOL(artDeliverPendingExceptionFromCode)  // artDeliverPendingExceptionFromCode(Thread*)
415    UNREACHABLE
416END_MACRO
417    /*
418     * Macro that calls through to artDeliverPendingExceptionFromCode, where the pending
419     * exception is Thread::Current()->exception_.
420     */
421MACRO0(DELIVER_PENDING_EXCEPTION)
422    SETUP_SAVE_ALL_CALLEE_SAVES_FRAME        // save callee saves for throw
423    DELIVER_PENDING_EXCEPTION_FRAME_READY
424END_MACRO
425
426MACRO0(RETURN_OR_DELIVER_PENDING_EXCEPTION)
427    movq %gs:THREAD_EXCEPTION_OFFSET, %rcx // get exception field
428    testq %rcx, %rcx               // rcx == 0 ?
429    jnz 1f                         // if rcx != 0 goto 1
430    ret                            // return
4311:                                 // deliver exception on current thread
432    DELIVER_PENDING_EXCEPTION
433END_MACRO
434
435#endif  // ART_RUNTIME_ARCH_X86_64_ASM_SUPPORT_X86_64_S_
436