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_ASM_SUPPORT_X86_S_
18#define ART_RUNTIME_ARCH_X86_ASM_SUPPORT_X86_S_
19
20#include "asm_support_x86.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 MACRO5(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4, macro_arg5) .macro macro_name macro_arg1, macro_arg2, macro_arg3, macro_arg4, macro_arg5
29#define END_MACRO .endm
30
31#if defined(__clang__)
32    // Clang/llvm does not support .altmacro. However, the clang/llvm preprocessor doesn't
33    // separate the backslash and parameter by a space. Everything just works.
34    #define RAW_VAR(name) \name
35    #define VAR(name) \name
36    #define CALLVAR(name) SYMBOL(\name)
37    #define PLT_VAR(name) \name@PLT
38    #define REG_VAR(name) %\name
39    #define CALL_MACRO(name) \name
40#else
41    // Regular gas(1) uses \argument_name for macro arguments.
42    // We need to turn on alternate macro syntax so we can use & instead or the preprocessor
43    // will screw us by inserting a space between the \ and the name. Even in this mode there's
44    // no special meaning to $, so literals are still just $x. The use of altmacro means % is a
45    // special character meaning care needs to be taken when passing registers as macro
46    // arguments.
47    .altmacro
48    #define RAW_VAR(name) name&
49    #define VAR(name) name&
50    #define CALLVAR(name) SYMBOL(name&)
51    #define PLT_VAR(name) name&@PLT
52    #define REG_VAR(name) %name
53    #define CALL_MACRO(name) name&
54#endif
55
56#define LITERAL(value) $value
57#if defined(__APPLE__)
58    #define MACRO_LITERAL(value) $(value)
59#else
60    #define MACRO_LITERAL(value) $value
61#endif
62
63#if defined(__APPLE__)
64    #define FUNCTION_TYPE(name)
65    #define SIZE(name)
66#else
67    #define FUNCTION_TYPE(name) .type name, @function
68    #define SIZE(name) .size name, .-name
69#endif
70
71    // CFI support.
72#if !defined(__APPLE__)
73    #define CFI_STARTPROC .cfi_startproc
74    #define CFI_ENDPROC .cfi_endproc
75    #define CFI_ADJUST_CFA_OFFSET(size) .cfi_adjust_cfa_offset size
76    #define CFI_DEF_CFA(reg,size) .cfi_def_cfa reg,size
77    #define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg
78    #define CFI_RESTORE(reg) .cfi_restore reg
79    #define CFI_REL_OFFSET(reg,size) .cfi_rel_offset reg,size
80    #define CFI_REMEMBER_STATE .cfi_remember_state
81    // The spec is not clear whether the CFA is part of the saved state and tools
82    // differ in the behaviour, so explicitly set the CFA to avoid any ambiguity.
83    // The restored CFA state should match the CFA state during CFI_REMEMBER_STATE.
84    // `objdump -Wf libart.so | egrep "_cfa|_state"` is useful to audit the opcodes.
85    #define CFI_RESTORE_STATE_AND_DEF_CFA(reg,off) .cfi_restore_state .cfi_def_cfa reg,off
86    #define CFI_ESCAPE(...) .cfi_escape __VA_ARGS__
87#else
88    // Mac OS' doesn't like cfi_* directives.
89    #define CFI_STARTPROC
90    #define CFI_ENDPROC
91    #define CFI_ADJUST_CFA_OFFSET(size)
92    #define CFI_DEF_CFA(reg,size)
93    #define CFI_DEF_CFA_REGISTER(reg)
94    #define CFI_RESTORE(reg)
95    #define CFI_REL_OFFSET(reg,size)
96    #define CFI_REMEMBER_STATE
97    #define CFI_RESTORE_STATE_AND_DEF_CFA(reg,off)
98    #define CFI_ESCAPE(...)
99#endif
100
101    // Symbols. On a Mac, we need a leading underscore.
102#if !defined(__APPLE__)
103    #define SYMBOL(name) name
104    #define PLT_SYMBOL(name) name ## @PLT
105#else
106    // Mac OS' symbols have an _ prefix.
107    #define SYMBOL(name) _ ## name
108    #define PLT_SYMBOL(name) _ ## name
109#endif
110
111// Directive to hide a function symbol.
112#if defined(__APPLE__)
113    #define ASM_HIDDEN .private_extern
114#else
115    #define ASM_HIDDEN .hidden
116#endif
117
118    /* Cache alignment for function entry */
119MACRO0(ALIGN_FUNCTION_ENTRY)
120    // ART-compiled functions have OatQuickMethodHeader but assembly funtions do not.
121    // Prefix the assembly code with 0xFFs, which means there is no method header.
122    .byte 0xFF, 0xFF, 0xFF, 0xFF
123    // Cache alignment for function entry.
124    .balign 16, 0xFF
125END_MACRO
126
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(esp, 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    pushl REG_VAR(reg)
149    CFI_ADJUST_CFA_OFFSET(4)
150    CFI_REL_OFFSET(REG_VAR(reg), 0)
151END_MACRO
152
153MACRO1(POP, reg)
154    popl REG_VAR(reg)
155    CFI_ADJUST_CFA_OFFSET(-4)
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    pushl REG_VAR(reg)
163    CFI_ADJUST_CFA_OFFSET(4)
164END_MACRO
165
166MACRO1(POP_ARG, reg)
167    popl REG_VAR(reg)
168    CFI_ADJUST_CFA_OFFSET(-4)
169END_MACRO
170
171MACRO1(CFI_RESTORE_REG, reg)
172    CFI_RESTORE(REG_VAR(reg))
173END_MACRO
174
175#define UNREACHABLE int3
176
177MACRO1(UNIMPLEMENTED,name)
178    FUNCTION_TYPE(\name)
179    .globl VAR(name)
180    ALIGN_FUNCTION_ENTRY
181VAR(name):
182    CFI_STARTPROC
183    UNREACHABLE
184    UNREACHABLE
185    CFI_ENDPROC
186    SIZE(\name)
187END_MACRO
188
189MACRO1(SETUP_GOT_NOSAVE, got_reg)
190#ifndef __APPLE__
191    .ifc VAR(got_reg), ebx
192      call __x86.get_pc_thunk.bx
193      addl $_GLOBAL_OFFSET_TABLE_, %ebx
194    .else
195      .error "Unknown GOT register \got_reg"
196    .endif
197#endif
198END_MACRO
199
200// Macros to poison (negate) the reference for heap poisoning.
201MACRO1(POISON_HEAP_REF, rRef)
202#ifdef USE_HEAP_POISONING
203    neg REG_VAR(rRef)
204#endif  // USE_HEAP_POISONING
205END_MACRO
206
207// Macros to unpoison (negate) the reference for heap poisoning.
208MACRO1(UNPOISON_HEAP_REF, rRef)
209#ifdef USE_HEAP_POISONING
210    neg REG_VAR(rRef)
211#endif  // USE_HEAP_POISONING
212END_MACRO
213
214    /*
215     * Macro that sets up the callee save frame to conform with
216     * Runtime::CreateCalleeSaveMethod(kSaveRefsAndArgs), except for pushing the method
217     */
218MACRO0(SETUP_SAVE_REFS_AND_ARGS_FRAME_REGISTERS_ONLY)
219    PUSH edi      // Save callee saves
220    PUSH esi
221    PUSH ebp
222    PUSH_ARG ebx  // Save args.
223    PUSH_ARG edx
224    PUSH_ARG ecx
225    // Create space for FPR args.
226    subl MACRO_LITERAL(4 * 8), %esp
227    CFI_ADJUST_CFA_OFFSET(4 * 8)
228    // Save FPRs.
229    movsd %xmm0, 0(%esp)
230    movsd %xmm1, 8(%esp)
231    movsd %xmm2, 16(%esp)
232    movsd %xmm3, 24(%esp)
233
234    // Ugly compile-time check, but we only have the preprocessor.
235    // First +4: implicit return address pushed on stack when caller made call.
236    // Last +4: we're not pushing the method on the stack here.
237#if (FRAME_SIZE_SAVE_REFS_AND_ARGS != 4 + 6*4 + 4*8 + 4)
238#error "FRAME_SIZE_SAVE_REFS_AND_ARGS(X86) size not as expected."
239#endif
240END_MACRO
241
242MACRO0(RESTORE_SAVE_REFS_AND_ARGS_FRAME)
243    // Restore FPRs. EAX is still on the stack.
244    movsd 4(%esp), %xmm0
245    movsd 12(%esp), %xmm1
246    movsd 20(%esp), %xmm2
247    movsd 28(%esp), %xmm3
248
249    addl MACRO_LITERAL(36), %esp  // Remove FPRs and method pointer.
250    CFI_ADJUST_CFA_OFFSET(-36)
251
252    POP_ARG ecx                   // Restore args
253    POP_ARG edx
254    POP_ARG ebx
255    POP ebp                       // Restore callee saves
256    POP esi
257    POP edi
258END_MACRO
259
260    /*
261     * Macro that calls through to artDeliverPendingExceptionFromCode, where the pending
262     * exception is Thread::Current()->exception_ when the runtime method frame is ready.
263     */
264MACRO0(DELIVER_PENDING_EXCEPTION_FRAME_READY)
265    // Outgoing argument set up
266    subl MACRO_LITERAL(12), %esp               // alignment padding
267    CFI_ADJUST_CFA_OFFSET(12)
268    pushl %fs:THREAD_SELF_OFFSET               // pass Thread::Current()
269    CFI_ADJUST_CFA_OFFSET(4)
270    call SYMBOL(artDeliverPendingExceptionFromCode)  // artDeliverPendingExceptionFromCode(Thread*)
271    UNREACHABLE
272END_MACRO
273
274#endif  // ART_RUNTIME_ARCH_X86_ASM_SUPPORT_X86_S_
275