Debian Codegraph @

libffi/src/aarch64/sysv.S

raw
1 /* Copyright (c) 2009, 2010, 2011, 2012 ARM Ltd.
2
3 Permission is hereby granted, free of charge, to any person obtaining
4 a copy of this software and associated documentation files (the
5 ``Software''), to deal in the Software without restriction, including
6 without limitation the rights to use, copy, modify, merge, publish,
7 distribute, sublicense, and/or sell copies of the Software, and to
8 permit persons to whom the Software is furnished to do so, subject to
9 the following conditions:
10
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
21
22 #if defined(__aarch64__) || defined(__arm64__)
23 #define LIBFFI_ASM
24 #include <fficonfig.h>
25 #include <ffi.h>
26 #include <ffi_cfi.h>
27 #include "internal.h"
28
29 #ifdef HAVE_MACHINE_ASM_H
30 #include <machine/asm.h>
31 #else
32 #ifdef __USER_LABEL_PREFIX__
33 #define CONCAT1(a, b) CONCAT2(a, b)
34 #define CONCAT2(a, b) a ## b
35
36 /* Use the right prefix for global labels. */
37 #define CNAME(x) CONCAT1 (__USER_LABEL_PREFIX__, x)
38 #else
39 #define CNAME(x) x
40 #endif
41 #endif
42
43 #ifdef __AARCH64EB__
44 # define BE(X) X
45 #else
46 # define BE(X) 0
47 #endif
48
49 #ifdef __ILP32__
50 #define PTR_REG(n) w##n
51 #else
52 #define PTR_REG(n) x##n
53 #endif
54
55 #ifdef __ILP32__
56 #define PTR_SIZE 4
57 #else
58 #define PTR_SIZE 8
59 #endif
60
61 .text
62 .align 4
63
64 /* ffi_call_SYSV
65 extern void ffi_call_SYSV (void *stack, void *frame,
66 void (*fn)(void), void *rvalue,
67 int flags, void *closure);
68
69 Therefore on entry we have:
70
71 x0 stack
72 x1 frame
73 x2 fn
74 x3 rvalue
75 x4 flags
76 x5 closure
77 */
78
79 cfi_startproc
80 CNAME(ffi_call_SYSV):
81 /* Sign the lr with x1 since that is where it will be stored */
82 SIGN_LR_WITH_REG(x1)
83
84 /* Use a stack frame allocated by our caller. */
85 #if defined(HAVE_PTRAUTH) && defined(__APPLE__)
86 /* darwin's libunwind assumes that the cfa is the sp and that's the data
87 * used to sign the lr. In order to allow unwinding through this
88 * function it is necessary to point the cfa at the signing register.
89 */
90 cfi_def_cfa(x1, 0);
91 #else
92 cfi_def_cfa(x1, 40);
93 #endif
94 stp x29, x30, [x1]
95 mov x9, sp
96 str x9, [x1, #32]
97 mov x29, x1
98 mov sp, x0
99 cfi_def_cfa_register(x29)
100 cfi_rel_offset (x29, 0)
101 cfi_rel_offset (x30, 8)
102
103 mov x9, x2 /* save fn */
104 mov x8, x3 /* install structure return */
105 #ifdef FFI_GO_CLOSURES
106 mov x18, x5 /* install static chain */
107 #endif
108 stp x3, x4, [x29, #16] /* save rvalue and flags */
109
110 /* Load the vector argument passing registers, if necessary. */
111 tbz w4, #AARCH64_FLAG_ARG_V_BIT, 1f
112 ldp q0, q1, [sp, #0]
113 ldp q2, q3, [sp, #32]
114 ldp q4, q5, [sp, #64]
115 ldp q6, q7, [sp, #96]
116 1:
117 /* Load the core argument passing registers, including
118 the structure return pointer. */
119 ldp x0, x1, [sp, #16*N_V_ARG_REG + 0]
120 ldp x2, x3, [sp, #16*N_V_ARG_REG + 16]
121 ldp x4, x5, [sp, #16*N_V_ARG_REG + 32]
122 ldp x6, x7, [sp, #16*N_V_ARG_REG + 48]
123
124 /* Deallocate the context, leaving the stacked arguments. */
125 add sp, sp, #CALL_CONTEXT_SIZE
126
127 BRANCH_AND_LINK_TO_REG x9 /* call fn */
128
129 ldp x3, x4, [x29, #16] /* reload rvalue and flags */
130
131 /* Partially deconstruct the stack frame. */
132 ldr x9, [x29, #32]
133 mov sp, x9
134 cfi_def_cfa_register (sp)
135 mov x2, x29 /* Preserve for auth */
136 ldp x29, x30, [x29]
137
138 /* Save the return value as directed. */
139 adr x5, 0f
140 and w4, w4, #AARCH64_RET_MASK
141 add x5, x5, x4, lsl #3
142 br x5
143
144 /* Note that each table entry is 2 insns, and thus 8 bytes.
145 For integer data, note that we're storing into ffi_arg
146 and therefore we want to extend to 64 bits; these types
147 have two consecutive entries allocated for them. */
148 .align 4
149 0: b 99f /* VOID */
150 nop
151 1: str x0, [x3] /* INT64 */
152 b 99f
153 2: stp x0, x1, [x3] /* INT128 */
154 b 99f
155 3: brk #1000 /* UNUSED */
156 b 99f
157 4: brk #1000 /* UNUSED */
158 b 99f
159 5: brk #1000 /* UNUSED */
160 b 99f
161 6: brk #1000 /* UNUSED */
162 b 99f
163 7: brk #1000 /* UNUSED */
164 b 99f
165 8: st4 { v0.s, v1.s, v2.s, v3.s }[0], [x3] /* S4 */
166 b 99f
167 9: st3 { v0.s, v1.s, v2.s }[0], [x3] /* S3 */
168 b 99f
169 10: stp s0, s1, [x3] /* S2 */
170 b 99f
171 11: str s0, [x3] /* S1 */
172 b 99f
173 12: st4 { v0.d, v1.d, v2.d, v3.d }[0], [x3] /* D4 */
174 b 99f
175 13: st3 { v0.d, v1.d, v2.d }[0], [x3] /* D3 */
176 b 99f
177 14: stp d0, d1, [x3] /* D2 */
178 b 99f
179 15: str d0, [x3] /* D1 */
180 b 99f
181 16: str q3, [x3, #48] /* Q4 */
182 nop
183 17: str q2, [x3, #32] /* Q3 */
184 nop
185 18: stp q0, q1, [x3] /* Q2 */
186 b 99f
187 19: str q0, [x3] /* Q1 */
188 b 99f
189 20: uxtb w0, w0 /* UINT8 */
190 str x0, [x3]
191 21: b 99f /* reserved */
192 nop
193 22: uxth w0, w0 /* UINT16 */
194 str x0, [x3]
195 23: b 99f /* reserved */
196 nop
197 24: mov w0, w0 /* UINT32 */
198 str x0, [x3]
199 25: b 99f /* reserved */
200 nop
201 26: sxtb x0, w0 /* SINT8 */
202 str x0, [x3]
203 27: b 99f /* reserved */
204 nop
205 28: sxth x0, w0 /* SINT16 */
206 str x0, [x3]
207 29: b 99f /* reserved */
208 nop
209 30: sxtw x0, w0 /* SINT32 */
210 str x0, [x3]
211 31: b 99f /* reserved */
212 nop
213
214 /* Return now that result has been populated. */
215 99:
216 AUTH_LR_WITH_REG(x2)
217 ret
218
219 cfi_endproc
220
221 .globl CNAME(ffi_call_SYSV)
222 FFI_HIDDEN(CNAME(ffi_call_SYSV))
223 #ifdef __ELF__
224 .type CNAME(ffi_call_SYSV), #function
225 .size CNAME(ffi_call_SYSV), .-CNAME(ffi_call_SYSV)
226 #endif
227
228 #if FFI_CLOSURES
229
230 /* ffi_closure_SYSV
231
232 Closure invocation glue. This is the low level code invoked directly by
233 the closure trampoline to setup and call a closure.
234
235 On entry x17 points to a struct ffi_closure, x16 has been clobbered
236 all other registers are preserved.
237
238 We allocate a call context and save the argument passing registers,
239 then invoked the generic C ffi_closure_SYSV_inner() function to do all
240 the real work, on return we load the result passing registers back from
241 the call context.
242 */
243
244 #define ffi_closure_SYSV_FS (8*2 + CALL_CONTEXT_SIZE + 64)
245
246 .align 4
247 CNAME(ffi_closure_SYSV_V):
248 cfi_startproc
249 SIGN_LR
250 stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
251 cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
252 cfi_rel_offset (x29, 0)
253 cfi_rel_offset (x30, 8)
254
255 /* Save the argument passing vector registers. */
256 stp q0, q1, [sp, #16 + 0]
257 stp q2, q3, [sp, #16 + 32]
258 stp q4, q5, [sp, #16 + 64]
259 stp q6, q7, [sp, #16 + 96]
260 b 0f
261 cfi_endproc
262
263 .globl CNAME(ffi_closure_SYSV_V)
264 FFI_HIDDEN(CNAME(ffi_closure_SYSV_V))
265 #ifdef __ELF__
266 .type CNAME(ffi_closure_SYSV_V), #function
267 .size CNAME(ffi_closure_SYSV_V), . - CNAME(ffi_closure_SYSV_V)
268 #endif
269
270 .align 4
271 cfi_startproc
272 CNAME(ffi_closure_SYSV):
273 SIGN_LR
274 stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
275 cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
276 cfi_rel_offset (x29, 0)
277 cfi_rel_offset (x30, 8)
278 0:
279 mov x29, sp
280
281 /* Save the argument passing core registers. */
282 stp x0, x1, [sp, #16 + 16*N_V_ARG_REG + 0]
283 stp x2, x3, [sp, #16 + 16*N_V_ARG_REG + 16]
284 stp x4, x5, [sp, #16 + 16*N_V_ARG_REG + 32]
285 stp x6, x7, [sp, #16 + 16*N_V_ARG_REG + 48]
286
287 /* Load ffi_closure_inner arguments. */
288 ldp PTR_REG(0), PTR_REG(1), [x17, #FFI_TRAMPOLINE_CLOSURE_OFFSET] /* load cif, fn */
289 ldr PTR_REG(2), [x17, #FFI_TRAMPOLINE_CLOSURE_OFFSET+PTR_SIZE*2] /* load user_data */
290 #ifdef FFI_GO_CLOSURES
291 .Ldo_closure:
292 #endif
293 add x3, sp, #16 /* load context */
294 add x4, sp, #ffi_closure_SYSV_FS /* load stack */
295 add x5, sp, #16+CALL_CONTEXT_SIZE /* load rvalue */
296 mov x6, x8 /* load struct_rval */
297 bl CNAME(ffi_closure_SYSV_inner)
298
299 /* Load the return value as directed. */
300 adr x1, 0f
301 and w0, w0, #AARCH64_RET_MASK
302 add x1, x1, x0, lsl #3
303 add x3, sp, #16+CALL_CONTEXT_SIZE
304 br x1
305
306 /* Note that each table entry is 2 insns, and thus 8 bytes. */
307 .align 4
308 0: b 99f /* VOID */
309 nop
310 1: ldr x0, [x3] /* INT64 */
311 b 99f
312 2: ldp x0, x1, [x3] /* INT128 */
313 b 99f
314 3: brk #1000 /* UNUSED */
315 nop
316 4: brk #1000 /* UNUSED */
317 nop
318 5: brk #1000 /* UNUSED */
319 nop
320 6: brk #1000 /* UNUSED */
321 nop
322 7: brk #1000 /* UNUSED */
323 nop
324 8: ldr s3, [x3, #12] /* S4 */
325 nop
326 9: ldr s2, [x3, #8] /* S3 */
327 nop
328 10: ldp s0, s1, [x3] /* S2 */
329 b 99f
330 11: ldr s0, [x3] /* S1 */
331 b 99f
332 12: ldr d3, [x3, #24] /* D4 */
333 nop
334 13: ldr d2, [x3, #16] /* D3 */
335 nop
336 14: ldp d0, d1, [x3] /* D2 */
337 b 99f
338 15: ldr d0, [x3] /* D1 */
339 b 99f
340 16: ldr q3, [x3, #48] /* Q4 */
341 nop
342 17: ldr q2, [x3, #32] /* Q3 */
343 nop
344 18: ldp q0, q1, [x3] /* Q2 */
345 b 99f
346 19: ldr q0, [x3] /* Q1 */
347 b 99f
348 20: ldrb w0, [x3, #BE(7)] /* UINT8 */
349 b 99f
350 21: brk #1000 /* reserved */
351 nop
352 22: ldrh w0, [x3, #BE(6)] /* UINT16 */
353 b 99f
354 23: brk #1000 /* reserved */
355 nop
356 24: ldr w0, [x3, #BE(4)] /* UINT32 */
357 b 99f
358 25: brk #1000 /* reserved */
359 nop
360 26: ldrsb x0, [x3, #BE(7)] /* SINT8 */
361 b 99f
362 27: brk #1000 /* reserved */
363 nop
364 28: ldrsh x0, [x3, #BE(6)] /* SINT16 */
365 b 99f
366 29: brk #1000 /* reserved */
367 nop
368 30: ldrsw x0, [x3, #BE(4)] /* SINT32 */
369 nop
370 31: /* reserved */
371 99: ldp x29, x30, [sp], #ffi_closure_SYSV_FS
372 cfi_adjust_cfa_offset (-ffi_closure_SYSV_FS)
373 cfi_restore (x29)
374 cfi_restore (x30)
375 AUTH_LR_AND_RET
376 cfi_endproc
377
378 .globl CNAME(ffi_closure_SYSV)
379 FFI_HIDDEN(CNAME(ffi_closure_SYSV))
380 #ifdef __ELF__
381 .type CNAME(ffi_closure_SYSV), #function
382 .size CNAME(ffi_closure_SYSV), . - CNAME(ffi_closure_SYSV)
383 #endif
384
385 #if defined(FFI_EXEC_STATIC_TRAMP)
386 .align 4
387 CNAME(ffi_closure_SYSV_V_alt):
388 /* See the comments above trampoline_code_table. */
389 ldr x17, [sp, #8] /* Load closure in x17 */
390 add sp, sp, #16 /* Restore the stack */
391 b CNAME(ffi_closure_SYSV_V)
392
393 .globl CNAME(ffi_closure_SYSV_V_alt)
394 FFI_HIDDEN(CNAME(ffi_closure_SYSV_V_alt))
395 #ifdef __ELF__
396 .type CNAME(ffi_closure_SYSV_V_alt), #function
397 .size CNAME(ffi_closure_SYSV_V_alt), . - CNAME(ffi_closure_SYSV_V_alt)
398 #endif
399
400 .align 4
401 CNAME(ffi_closure_SYSV_alt):
402 /* See the comments above trampoline_code_table. */
403 ldr x17, [sp, #8] /* Load closure in x17 */
404 add sp, sp, #16 /* Restore the stack */
405 b CNAME(ffi_closure_SYSV)
406
407 .globl CNAME(ffi_closure_SYSV_alt)
408 FFI_HIDDEN(CNAME(ffi_closure_SYSV_alt))
409 #ifdef __ELF__
410 .type CNAME(ffi_closure_SYSV_alt), #function
411 .size CNAME(ffi_closure_SYSV_alt), . - CNAME(ffi_closure_SYSV_alt)
412 #endif
413
414 /*
415 * Below is the definition of the trampoline code table. Each element in
416 * the code table is a trampoline.
417 */
418 /*
419 * The trampoline uses register x17. It saves the original value of x17 on
420 * the stack.
421 *
422 * The trampoline has two parameters - target code to jump to and data for
423 * the target code. The trampoline extracts the parameters from its parameter
424 * block (see tramp_table_map()). The trampoline saves the data address on
425 * the stack. Finally, it jumps to the target code.
426 *
427 * The target code can choose to:
428 *
429 * - restore the value of x17
430 * - load the data address in a register
431 * - restore the stack pointer to what it was when the trampoline was invoked.
432 */
433 .align AARCH64_TRAMP_MAP_SHIFT
434 CNAME(trampoline_code_table):
435 .rept AARCH64_TRAMP_MAP_SIZE / AARCH64_TRAMP_SIZE
436 sub sp, sp, #16 /* Make space on the stack */
437 str x17, [sp] /* Save x17 on stack */
438 adr x17, #16376 /* Get data address */
439 ldr x17, [x17] /* Copy data into x17 */
440 str x17, [sp, #8] /* Save data on stack */
441 adr x17, #16372 /* Get code address */
442 ldr x17, [x17] /* Load code address into x17 */
443 br x17 /* Jump to code */
444 .endr
445
446 .globl CNAME(trampoline_code_table)
447 FFI_HIDDEN(CNAME(trampoline_code_table))
448 #ifdef __ELF__
449 .type CNAME(trampoline_code_table), #function
450 .size CNAME(trampoline_code_table), . - CNAME(trampoline_code_table)
451 #endif
452 .align AARCH64_TRAMP_MAP_SHIFT
453 #endif /* FFI_EXEC_STATIC_TRAMP */
454
455 #if FFI_EXEC_TRAMPOLINE_TABLE
456
457 #ifdef __MACH__
458 #include <mach/machine/vm_param.h>
459 .align PAGE_MAX_SHIFT
460 CNAME(ffi_closure_trampoline_table_page):
461 .rept PAGE_MAX_SIZE / FFI_TRAMPOLINE_SIZE
462 adr x16, -PAGE_MAX_SIZE
463 ldp x17, x16, [x16]
464 br x16
465 nop /* each entry in the trampoline config page is 2*sizeof(void*) so the trampoline itself cannot be smaller than 16 bytes */
466 .endr
467
468 .globl CNAME(ffi_closure_trampoline_table_page)
469 FFI_HIDDEN(CNAME(ffi_closure_trampoline_table_page))
470 #ifdef __ELF__
471 .type CNAME(ffi_closure_trampoline_table_page), #function
472 .size CNAME(ffi_closure_trampoline_table_page), . - CNAME(ffi_closure_trampoline_table_page)
473 #endif
474 #endif
475
476 #endif /* FFI_EXEC_TRAMPOLINE_TABLE */
477
478 #ifdef FFI_GO_CLOSURES
479 .align 4
480 CNAME(ffi_go_closure_SYSV_V):
481 cfi_startproc
482 stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
483 cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
484 cfi_rel_offset (x29, 0)
485 cfi_rel_offset (x30, 8)
486
487 /* Save the argument passing vector registers. */
488 stp q0, q1, [sp, #16 + 0]
489 stp q2, q3, [sp, #16 + 32]
490 stp q4, q5, [sp, #16 + 64]
491 stp q6, q7, [sp, #16 + 96]
492 b 0f
493 cfi_endproc
494
495 .globl CNAME(ffi_go_closure_SYSV_V)
496 FFI_HIDDEN(CNAME(ffi_go_closure_SYSV_V))
497 #ifdef __ELF__
498 .type CNAME(ffi_go_closure_SYSV_V), #function
499 .size CNAME(ffi_go_closure_SYSV_V), . - CNAME(ffi_go_closure_SYSV_V)
500 #endif
501
502 .align 4
503 cfi_startproc
504 CNAME(ffi_go_closure_SYSV):
505 stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
506 cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
507 cfi_rel_offset (x29, 0)
508 cfi_rel_offset (x30, 8)
509 0:
510 mov x29, sp
511
512 /* Save the argument passing core registers. */
513 stp x0, x1, [sp, #16 + 16*N_V_ARG_REG + 0]
514 stp x2, x3, [sp, #16 + 16*N_V_ARG_REG + 16]
515 stp x4, x5, [sp, #16 + 16*N_V_ARG_REG + 32]
516 stp x6, x7, [sp, #16 + 16*N_V_ARG_REG + 48]
517
518 /* Load ffi_closure_inner arguments. */
519 ldp PTR_REG(0), PTR_REG(1), [x18, #PTR_SIZE]/* load cif, fn */
520 mov x2, x18 /* load user_data */
521 b .Ldo_closure
522 cfi_endproc
523
524 .globl CNAME(ffi_go_closure_SYSV)
525 FFI_HIDDEN(CNAME(ffi_go_closure_SYSV))
526 #ifdef __ELF__
527 .type CNAME(ffi_go_closure_SYSV), #function
528 .size CNAME(ffi_go_closure_SYSV), . - CNAME(ffi_go_closure_SYSV)
529 #endif
530 #endif /* FFI_GO_CLOSURES */
531 #endif /* FFI_CLOSURES */
532 #endif /* __arm64__ */
533
534 #if defined __ELF__ && defined __linux__
535 .section .note.GNU-stack,"",%progbits
536 #endif
537