aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/libffi/src/aarch64/sysv.S
blob: ffb16f84cb1e3f9a5b22484fdbf767521bbdb91c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Copyright (c) 2009, 2010, 2011, 2012 ARM Ltd.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#define LIBFFI_ASM
#include <fficonfig.h>
#include <ffi.h>

#define cfi_adjust_cfa_offset(off)	.cfi_adjust_cfa_offset off
#define cfi_rel_offset(reg, off)	.cfi_rel_offset reg, off
#define cfi_restore(reg)		.cfi_restore reg
#define cfi_def_cfa_register(reg)	.cfi_def_cfa_register reg

        .text
        .globl ffi_call_SYSV
        .type ffi_call_SYSV, #function

/* ffi_call_SYSV()

   Create a stack frame, setup an argument context, call the callee
   and extract the result.

   The maximum required argument stack size is provided,
   ffi_call_SYSV() allocates that stack space then calls the
   prepare_fn to populate register context and stack.  The
   argument passing registers are loaded from the register
   context and the callee called, on return the register passing
   register are saved back to the context.  Our caller will
   extract the return value from the final state of the saved
   register context.

   Prototype:

   extern unsigned
   ffi_call_SYSV (void (*)(struct call_context *context, unsigned char *,
			   extended_cif *),
                  struct call_context *context,
                  extended_cif *,
                  unsigned required_stack_size,
                  void (*fn)(void));

   Therefore on entry we have:

   x0 prepare_fn
   x1 &context
   x2 &ecif
   x3 bytes
   x4 fn

   This function uses the following stack frame layout:

   ==
                saved x30(lr)
   x29(fp)->    saved x29(fp)
                saved x24
                saved x23
                saved x22
   sp'    ->    saved x21
                ...
   sp     ->    (constructed callee stack arguments)
   ==

   Voila! */

#define ffi_call_SYSV_FS (8 * 4)

        .cfi_startproc
ffi_call_SYSV:
        stp     x29, x30, [sp, #-16]!
	cfi_adjust_cfa_offset (16)
        cfi_rel_offset (x29, 0)
        cfi_rel_offset (x30, 8)

        mov     x29, sp
	cfi_def_cfa_register (x29)
        sub     sp, sp, #ffi_call_SYSV_FS

        stp     x21, x22, [sp, 0]
        cfi_rel_offset (x21, 0 - ffi_call_SYSV_FS)
        cfi_rel_offset (x22, 8 - ffi_call_SYSV_FS)

        stp     x23, x24, [sp, 16]
        cfi_rel_offset (x23, 16 - ffi_call_SYSV_FS)
        cfi_rel_offset (x24, 24 - ffi_call_SYSV_FS)

        mov     x21, x1
        mov     x22, x2
        mov     x24, x4

        /* Allocate the stack space for the actual arguments, many
           arguments will be passed in registers, but we assume
           worst case and allocate sufficient stack for ALL of
           the arguments.  */
        sub     sp, sp, x3

        /* unsigned (*prepare_fn) (struct call_context *context,
				   unsigned char *stack, extended_cif *ecif);
	 */
        mov     x23, x0
        mov     x0, x1
        mov     x1, sp
        /* x2 already in place */
        blr     x23

        /* Preserve the flags returned.  */
        mov     x23, x0

        /* Figure out if we should touch the vector registers.  */
        tbz     x23, #AARCH64_FFI_WITH_V_BIT, 1f

        /* Load the vector argument passing registers.  */
        ldp     q0, q1, [x21, #8*32 +  0]
        ldp     q2, q3, [x21, #8*32 + 32]
        ldp     q4, q5, [x21, #8*32 + 64]
        ldp     q6, q7, [x21, #8*32 + 96]
1:
        /* Load the core argument passing registers.  */
        ldp     x0, x1, [x21,  #0]
        ldp     x2, x3, [x21, #16]
        ldp     x4, x5, [x21, #32]
        ldp     x6, x7, [x21, #48]

        /* Don't forget x8 which may be holding the address of a return buffer.
	 */
        ldr     x8,     [x21, #8*8]

        blr     x24

        /* Save the core argument passing registers.  */
        stp     x0, x1, [x21,  #0]
        stp     x2, x3, [x21, #16]
        stp     x4, x5, [x21, #32]
        stp     x6, x7, [x21, #48]

        /* Note nothing useful ever comes back in x8!  */

        /* Figure out if we should touch the vector registers.  */
        tbz     x23, #AARCH64_FFI_WITH_V_BIT, 1f

        /* Save the vector argument passing registers.  */
        stp     q0, q1, [x21, #8*32 + 0]
        stp     q2, q3, [x21, #8*32 + 32]
        stp     q4, q5, [x21, #8*32 + 64]
        stp     q6, q7, [x21, #8*32 + 96]
1:
        /* All done, unwind our stack frame.  */
        ldp     x21, x22, [x29,  # - ffi_call_SYSV_FS]
        cfi_restore (x21)
        cfi_restore (x22)

        ldp     x23, x24, [x29,  # - ffi_call_SYSV_FS + 16]
        cfi_restore (x23)
        cfi_restore (x24)

        mov     sp, x29
	cfi_def_cfa_register (sp)

        ldp     x29, x30, [sp], #16
	cfi_adjust_cfa_offset (-16)
        cfi_restore (x29)
        cfi_restore (x30)

        ret

        .cfi_endproc
        .size ffi_call_SYSV, .-ffi_call_SYSV

#define ffi_closure_SYSV_FS (8 * 2 + AARCH64_CALL_CONTEXT_SIZE)

/* ffi_closure_SYSV

   Closure invocation glue. This is the low level code invoked directly by
   the closure trampoline to setup and call a closure.

   On entry x17 points to a struct trampoline_data, x16 has been clobbered
   all other registers are preserved.

   We allocate a call context and save the argument passing registers,
   then invoked the generic C ffi_closure_SYSV_inner() function to do all
   the real work, on return we load the result passing registers back from
   the call context.

   On entry

   extern void
   ffi_closure_SYSV (struct trampoline_data *);

   struct trampoline_data
   {
        UINT64 *ffi_closure;
        UINT64 flags;
   };

   This function uses the following stack frame layout:

   ==
                saved x30(lr)
   x29(fp)->    saved x29(fp)
                saved x22
                saved x21
                ...
   sp     ->    call_context
   ==

   Voila!  */

        .text
        .globl ffi_closure_SYSV
        .cfi_startproc
ffi_closure_SYSV:
        stp     x29, x30, [sp, #-16]!
	cfi_adjust_cfa_offset (16)
        cfi_rel_offset (x29, 0)
        cfi_rel_offset (x30, 8)

        mov     x29, sp
        cfi_def_cfa_register (x29)

        sub     sp, sp, #ffi_closure_SYSV_FS

        stp     x21, x22, [x29, #-16]
        cfi_rel_offset (x21, -16)
        cfi_rel_offset (x22, -8)

        /* Load x21 with &call_context.  */
        mov     x21, sp
        /* Preserve our struct trampoline_data *  */
        mov     x22, x17

        /* Save the rest of the argument passing registers.  */
        stp     x0, x1, [x21, #0]
        stp     x2, x3, [x21, #16]
        stp     x4, x5, [x21, #32]
        stp     x6, x7, [x21, #48]
        /* Don't forget we may have been given a result scratch pad address.
	 */
        str     x8,     [x21, #64]

        /* Figure out if we should touch the vector registers.  */
        ldr     x0, [x22, #8]
        tbz     x0, #AARCH64_FFI_WITH_V_BIT, 1f

        /* Save the argument passing vector registers.  */
        stp     q0, q1, [x21, #8*32 + 0]
        stp     q2, q3, [x21, #8*32 + 32]
        stp     q4, q5, [x21, #8*32 + 64]
        stp     q6, q7, [x21, #8*32 + 96]
1:
        /* Load &ffi_closure..  */
        ldr     x0, [x22, #0]
        mov     x1, x21
        /* Compute the location of the stack at the point that the
           trampoline was called.  */
        add     x2, x29, #16

        bl      ffi_closure_SYSV_inner

        /* Figure out if we should touch the vector registers.  */
        ldr     x0, [x22, #8]
        tbz     x0, #AARCH64_FFI_WITH_V_BIT, 1f

        /* Load the result passing vector registers.  */
        ldp     q0, q1, [x21, #8*32 + 0]
        ldp     q2, q3, [x21, #8*32 + 32]
        ldp     q4, q5, [x21, #8*32 + 64]
        ldp     q6, q7, [x21, #8*32 + 96]
1:
        /* Load the result passing core registers.  */
        ldp     x0, x1, [x21,  #0]
        ldp     x2, x3, [x21, #16]
        ldp     x4, x5, [x21, #32]
        ldp     x6, x7, [x21, #48]
        /* Note nothing usefull is returned in x8.  */

        /* We are done, unwind our frame.  */
        ldp     x21, x22, [x29,  #-16]
        cfi_restore (x21)
        cfi_restore (x22)

        mov     sp, x29
        cfi_def_cfa_register (sp)

        ldp     x29, x30, [sp], #16
	cfi_adjust_cfa_offset (-16)
        cfi_restore (x29)
        cfi_restore (x30)

        ret
        .cfi_endproc
        .size ffi_closure_SYSV, .-ffi_closure_SYSV