]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_trampoline_x86_64.S
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_trampoline_x86_64.S
1 //===-- xray_trampoline_x86.s -----------------------------------*- ASM -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 //
12 // This implements the X86-specific assembler for the trampolines.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "../builtins/assembly.h"
17 #include "../sanitizer_common/sanitizer_asm.h"
18
19
20
21 .macro SAVE_REGISTERS
22         subq $240, %rsp
23         CFI_DEF_CFA_OFFSET(248)
24         movq %rbp, 232(%rsp)
25         movupd  %xmm0, 216(%rsp)
26         movupd  %xmm1, 200(%rsp)
27         movupd  %xmm2, 184(%rsp)
28         movupd  %xmm3, 168(%rsp)
29         movupd  %xmm4, 152(%rsp)
30         movupd  %xmm5, 136(%rsp)
31         movupd  %xmm6, 120(%rsp)
32         movupd  %xmm7, 104(%rsp)
33         movq    %rdi, 96(%rsp)
34         movq    %rax, 88(%rsp)
35         movq    %rdx, 80(%rsp)
36         movq    %rsi, 72(%rsp)
37         movq    %rcx, 64(%rsp)
38         movq    %r8, 56(%rsp)
39         movq    %r9, 48(%rsp)
40         movq  %r10, 40(%rsp)
41         movq  %r11, 32(%rsp)
42         movq  %r12, 24(%rsp)
43         movq  %r13, 16(%rsp)
44         movq  %r14, 8(%rsp)
45         movq  %r15, 0(%rsp)
46 .endm
47
48 .macro RESTORE_REGISTERS
49         movq  232(%rsp), %rbp
50         movupd  216(%rsp), %xmm0
51         movupd  200(%rsp), %xmm1
52         movupd  184(%rsp), %xmm2
53         movupd  168(%rsp), %xmm3
54         movupd  152(%rsp), %xmm4
55         movupd  136(%rsp), %xmm5
56         movupd  120(%rsp) , %xmm6
57         movupd  104(%rsp) , %xmm7
58         movq    96(%rsp), %rdi
59         movq    88(%rsp), %rax
60         movq    80(%rsp), %rdx
61         movq    72(%rsp), %rsi
62         movq    64(%rsp), %rcx
63         movq    56(%rsp), %r8
64         movq    48(%rsp), %r9
65         movq  40(%rsp), %r10
66         movq  32(%rsp), %r11
67         movq  24(%rsp), %r12
68         movq  16(%rsp), %r13
69         movq  8(%rsp), %r14
70         movq  0(%rsp), %r15
71         addq    $240, %rsp
72         CFI_DEF_CFA_OFFSET(8)
73 .endm
74
75 .macro ALIGNED_CALL_RAX
76         // Call the logging handler, after aligning the stack to a 16-byte boundary.
77         // The approach we're taking here uses additional stack space to stash the
78         // stack pointer twice before aligning the pointer to 16-bytes. If the stack
79         // was 8-byte aligned, it will become 16-byte aligned -- when restoring the
80         // pointer, we can always look -8 bytes from the current position to get
81         // either of the values we've stashed in the first place.
82         pushq %rsp
83         pushq (%rsp)
84         andq $-0x10, %rsp
85   callq *%rax
86         movq 8(%rsp), %rsp
87 .endm
88
89         .text
90 #if !defined(__APPLE__)
91         .section .text
92 #else
93         .section __TEXT,__text
94 #endif
95         .file "xray_trampoline_x86.S"
96
97 //===----------------------------------------------------------------------===//
98
99         .globl ASM_SYMBOL(__xray_FunctionEntry)
100         .align 16, 0x90
101         ASM_TYPE_FUNCTION(__xray_FunctionEntry)
102 # LLVM-MCA-BEGIN __xray_FunctionEntry
103 ASM_SYMBOL(__xray_FunctionEntry):
104         CFI_STARTPROC
105         SAVE_REGISTERS
106
107         // This load has to be atomic, it's concurrent with __xray_patch().
108         // On x86/amd64, a simple (type-aligned) MOV instruction is enough.
109         movq    ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)(%rip), %rax
110         testq   %rax, %rax
111         je      .Ltmp0
112
113         // The patched function prologue puts its xray_instr_map index into %r10d.
114         movl    %r10d, %edi
115         xor     %esi,%esi
116         ALIGNED_CALL_RAX
117
118 .Ltmp0:
119         RESTORE_REGISTERS
120         retq
121 # LLVM-MCA-END
122         ASM_SIZE(__xray_FunctionEntry)
123         CFI_ENDPROC
124
125 //===----------------------------------------------------------------------===//
126
127         .globl ASM_SYMBOL(__xray_FunctionExit)
128         .align 16, 0x90
129         ASM_TYPE_FUNCTION(__xray_FunctionExit)
130 # LLVM-MCA-BEGIN __xray_FunctionExit
131 ASM_SYMBOL(__xray_FunctionExit):
132         CFI_STARTPROC
133         // Save the important registers first. Since we're assuming that this
134         // function is only jumped into, we only preserve the registers for
135         // returning.
136         subq    $56, %rsp
137         CFI_DEF_CFA_OFFSET(64)
138         movq  %rbp, 48(%rsp)
139         movupd  %xmm0, 32(%rsp)
140         movupd  %xmm1, 16(%rsp)
141         movq    %rax, 8(%rsp)
142         movq    %rdx, 0(%rsp)
143         movq    ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)(%rip), %rax
144         testq %rax,%rax
145         je      .Ltmp2
146
147         movl    %r10d, %edi
148         movl    $1, %esi
149   ALIGNED_CALL_RAX
150
151 .Ltmp2:
152         // Restore the important registers.
153         movq  48(%rsp), %rbp
154         movupd  32(%rsp), %xmm0
155         movupd  16(%rsp), %xmm1
156         movq    8(%rsp), %rax
157         movq    0(%rsp), %rdx
158         addq    $56, %rsp
159         CFI_DEF_CFA_OFFSET(8)
160         retq
161 # LLVM-MCA-END
162         ASM_SIZE(__xray_FunctionExit)
163         CFI_ENDPROC
164
165 //===----------------------------------------------------------------------===//
166
167         .globl ASM_SYMBOL(__xray_FunctionTailExit)
168         .align 16, 0x90
169         ASM_TYPE_FUNCTION(__xray_FunctionTailExit)
170 # LLVM-MCA-BEGIN __xray_FunctionTailExit
171 ASM_SYMBOL(__xray_FunctionTailExit):
172         CFI_STARTPROC
173         SAVE_REGISTERS
174
175         movq    ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)(%rip), %rax
176         testq %rax,%rax
177         je      .Ltmp4
178
179         movl    %r10d, %edi
180         movl    $2, %esi
181
182   ALIGNED_CALL_RAX
183
184 .Ltmp4:
185         RESTORE_REGISTERS
186         retq
187 # LLVM-MCA-END
188         ASM_SIZE(__xray_FunctionTailExit)
189         CFI_ENDPROC
190
191 //===----------------------------------------------------------------------===//
192
193         .globl ASM_SYMBOL(__xray_ArgLoggerEntry)
194         .align 16, 0x90
195         ASM_TYPE_FUNCTION(__xray_ArgLoggerEntry)
196 # LLVM-MCA-BEGIN __xray_ArgLoggerEntry
197 ASM_SYMBOL(__xray_ArgLoggerEntry):
198         CFI_STARTPROC
199         SAVE_REGISTERS
200
201         // Again, these function pointer loads must be atomic; MOV is fine.
202         movq    ASM_SYMBOL(_ZN6__xray13XRayArgLoggerE)(%rip), %rax
203         testq   %rax, %rax
204         jne     .Larg1entryLog
205
206         // If [arg1 logging handler] not set, defer to no-arg logging.
207         movq    ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)(%rip), %rax
208         testq   %rax, %rax
209         je      .Larg1entryFail
210
211 .Larg1entryLog:
212
213         // First argument will become the third
214         movq    %rdi, %rdx
215
216         // XRayEntryType::LOG_ARGS_ENTRY into the second
217         mov     $0x3, %esi
218
219         // 32-bit function ID becomes the first
220         movl    %r10d, %edi
221         ALIGNED_CALL_RAX
222
223 .Larg1entryFail:
224         RESTORE_REGISTERS
225         retq
226 # LLVM-MCA-END
227         ASM_SIZE(__xray_ArgLoggerEntry)
228         CFI_ENDPROC
229
230 //===----------------------------------------------------------------------===//
231
232         .global ASM_SYMBOL(__xray_CustomEvent)
233         .align 16, 0x90
234         ASM_TYPE_FUNCTION(__xray_CustomEvent)
235 # LLVM-MCA-BEGIN __xray_CustomEvent
236 ASM_SYMBOL(__xray_CustomEvent):
237         CFI_STARTPROC
238         SAVE_REGISTERS
239
240         // We take two arguments to this trampoline, which should be in rdi     and rsi
241         // already.
242         movq ASM_SYMBOL(_ZN6__xray22XRayPatchedCustomEventE)(%rip), %rax
243         testq %rax,%rax
244         je .LcustomEventCleanup
245
246         ALIGNED_CALL_RAX
247
248 .LcustomEventCleanup:
249         RESTORE_REGISTERS
250         retq
251 # LLVM-MCA-END
252         ASM_SIZE(__xray_CustomEvent)
253         CFI_ENDPROC
254
255 //===----------------------------------------------------------------------===//
256
257         .global ASM_SYMBOL(__xray_TypedEvent)
258         .align 16, 0x90
259         ASM_TYPE_FUNCTION(__xray_TypedEvent)
260 # LLVM-MCA-BEGIN __xray_TypedEvent
261 ASM_SYMBOL(__xray_TypedEvent):
262         CFI_STARTPROC
263         SAVE_REGISTERS
264
265         // We pass three arguments to this trampoline, which should be in rdi, rsi
266         // and rdx without our intervention.
267         movq ASM_SYMBOL(_ZN6__xray21XRayPatchedTypedEventE)(%rip), %rax
268         testq %rax,%rax
269         je .LtypedEventCleanup
270
271         ALIGNED_CALL_RAX
272
273 .LtypedEventCleanup:
274         RESTORE_REGISTERS
275         retq
276 # LLVM-MCA-END
277         ASM_SIZE(__xray_TypedEvent)
278         CFI_ENDPROC
279
280 //===----------------------------------------------------------------------===//
281
282 NO_EXEC_STACK_DIRECTIVE