1 | n/a | #ifdef __x86_64__ |
---|
2 | n/a | |
---|
3 | n/a | /* ----------------------------------------------------------------------- |
---|
4 | n/a | x86-ffi64.c - Copyright (c) 2002 Bo Thorsen <bo@suse.de> |
---|
5 | n/a | |
---|
6 | n/a | x86-64 Foreign Function Interface |
---|
7 | n/a | |
---|
8 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
9 | n/a | a copy of this software and associated documentation files (the |
---|
10 | n/a | ``Software''), to deal in the Software without restriction, including |
---|
11 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
12 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
13 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
14 | n/a | the following conditions: |
---|
15 | n/a | |
---|
16 | n/a | The above copyright notice and this permission notice shall be included |
---|
17 | n/a | in all copies or substantial portions of the Software. |
---|
18 | n/a | |
---|
19 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
20 | n/a | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
21 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
22 | n/a | IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
---|
23 | n/a | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
---|
24 | n/a | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
---|
25 | n/a | OTHER DEALINGS IN THE SOFTWARE. |
---|
26 | n/a | ----------------------------------------------------------------------- */ |
---|
27 | n/a | |
---|
28 | n/a | #include <ffi.h> |
---|
29 | n/a | #include <ffi_common.h> |
---|
30 | n/a | |
---|
31 | n/a | #include <stdlib.h> |
---|
32 | n/a | #include <stdarg.h> |
---|
33 | n/a | |
---|
34 | n/a | #define MAX_GPR_REGS 6 |
---|
35 | n/a | #define MAX_SSE_REGS 8 |
---|
36 | n/a | |
---|
37 | n/a | typedef struct RegisterArgs { |
---|
38 | n/a | /* Registers for argument passing. */ |
---|
39 | n/a | UINT64 gpr[MAX_GPR_REGS]; |
---|
40 | n/a | __int128_t sse[MAX_SSE_REGS]; |
---|
41 | n/a | } RegisterArgs; |
---|
42 | n/a | |
---|
43 | n/a | extern void |
---|
44 | n/a | ffi_call_unix64( |
---|
45 | n/a | void* args, |
---|
46 | n/a | unsigned long bytes, |
---|
47 | n/a | unsigned flags, |
---|
48 | n/a | void* raddr, |
---|
49 | n/a | void (*fnaddr)(void), |
---|
50 | n/a | unsigned ssecount); |
---|
51 | n/a | |
---|
52 | n/a | /* All reference to register classes here is identical to the code in |
---|
53 | n/a | gcc/config/i386/i386.c. Do *not* change one without the other. */ |
---|
54 | n/a | |
---|
55 | n/a | /* Register class used for passing given 64bit part of the argument. |
---|
56 | n/a | These represent classes as documented by the PS ABI, with the exception |
---|
57 | n/a | of SSESF, SSEDF classes, that are basically SSE class, just gcc will |
---|
58 | n/a | use SF or DFmode move instead of DImode to avoid reformating penalties. |
---|
59 | n/a | |
---|
60 | n/a | Similary we play games with INTEGERSI_CLASS to use cheaper SImode moves |
---|
61 | n/a | whenever possible (upper half does contain padding). */ |
---|
62 | n/a | enum x86_64_reg_class |
---|
63 | n/a | { |
---|
64 | n/a | X86_64_NO_CLASS, |
---|
65 | n/a | X86_64_INTEGER_CLASS, |
---|
66 | n/a | X86_64_INTEGERSI_CLASS, |
---|
67 | n/a | X86_64_SSE_CLASS, |
---|
68 | n/a | X86_64_SSESF_CLASS, |
---|
69 | n/a | X86_64_SSEDF_CLASS, |
---|
70 | n/a | X86_64_SSEUP_CLASS, |
---|
71 | n/a | X86_64_X87_CLASS, |
---|
72 | n/a | X86_64_X87UP_CLASS, |
---|
73 | n/a | X86_64_COMPLEX_X87_CLASS, |
---|
74 | n/a | X86_64_MEMORY_CLASS |
---|
75 | n/a | }; |
---|
76 | n/a | |
---|
77 | n/a | #define MAX_CLASSES 4 |
---|
78 | n/a | #define SSE_CLASS_P(X) ((X) >= X86_64_SSE_CLASS && X <= X86_64_SSEUP_CLASS) |
---|
79 | n/a | |
---|
80 | n/a | /* x86-64 register passing implementation. See x86-64 ABI for details. Goal |
---|
81 | n/a | of this code is to classify each 8bytes of incoming argument by the register |
---|
82 | n/a | class and assign registers accordingly. */ |
---|
83 | n/a | |
---|
84 | n/a | /* Return the union class of CLASS1 and CLASS2. |
---|
85 | n/a | See the x86-64 PS ABI for details. */ |
---|
86 | n/a | static enum x86_64_reg_class |
---|
87 | n/a | merge_classes( |
---|
88 | n/a | enum x86_64_reg_class class1, |
---|
89 | n/a | enum x86_64_reg_class class2) |
---|
90 | n/a | { |
---|
91 | n/a | /* Rule #1: If both classes are equal, this is the resulting class. */ |
---|
92 | n/a | if (class1 == class2) |
---|
93 | n/a | return class1; |
---|
94 | n/a | |
---|
95 | n/a | /* Rule #2: If one of the classes is NO_CLASS, the resulting class is |
---|
96 | n/a | the other class. */ |
---|
97 | n/a | if (class1 == X86_64_NO_CLASS) |
---|
98 | n/a | return class2; |
---|
99 | n/a | |
---|
100 | n/a | if (class2 == X86_64_NO_CLASS) |
---|
101 | n/a | return class1; |
---|
102 | n/a | |
---|
103 | n/a | /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */ |
---|
104 | n/a | if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS) |
---|
105 | n/a | return X86_64_MEMORY_CLASS; |
---|
106 | n/a | |
---|
107 | n/a | /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */ |
---|
108 | n/a | if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS) |
---|
109 | n/a | || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS)) |
---|
110 | n/a | return X86_64_INTEGERSI_CLASS; |
---|
111 | n/a | |
---|
112 | n/a | if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS |
---|
113 | n/a | || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS) |
---|
114 | n/a | return X86_64_INTEGER_CLASS; |
---|
115 | n/a | |
---|
116 | n/a | /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class, |
---|
117 | n/a | MEMORY is used. */ |
---|
118 | n/a | if (class1 == X86_64_X87_CLASS |
---|
119 | n/a | || class1 == X86_64_X87UP_CLASS |
---|
120 | n/a | || class1 == X86_64_COMPLEX_X87_CLASS |
---|
121 | n/a | || class2 == X86_64_X87_CLASS |
---|
122 | n/a | || class2 == X86_64_X87UP_CLASS |
---|
123 | n/a | || class2 == X86_64_COMPLEX_X87_CLASS) |
---|
124 | n/a | return X86_64_MEMORY_CLASS; |
---|
125 | n/a | |
---|
126 | n/a | /* Rule #6: Otherwise class SSE is used. */ |
---|
127 | n/a | return X86_64_SSE_CLASS; |
---|
128 | n/a | } |
---|
129 | n/a | |
---|
130 | n/a | /* Classify the argument of type TYPE and mode MODE. |
---|
131 | n/a | CLASSES will be filled by the register class used to pass each word |
---|
132 | n/a | of the operand. The number of words is returned. In case the parameter |
---|
133 | n/a | should be passed in memory, 0 is returned. As a special case for zero |
---|
134 | n/a | sized containers, classes[0] will be NO_CLASS and 1 is returned. |
---|
135 | n/a | |
---|
136 | n/a | See the x86-64 PS ABI for details. */ |
---|
137 | n/a | |
---|
138 | n/a | static int |
---|
139 | n/a | classify_argument( |
---|
140 | n/a | ffi_type* type, |
---|
141 | n/a | enum x86_64_reg_class classes[], |
---|
142 | n/a | size_t byte_offset) |
---|
143 | n/a | { |
---|
144 | n/a | switch (type->type) |
---|
145 | n/a | { |
---|
146 | n/a | case FFI_TYPE_UINT8: |
---|
147 | n/a | case FFI_TYPE_SINT8: |
---|
148 | n/a | case FFI_TYPE_UINT16: |
---|
149 | n/a | case FFI_TYPE_SINT16: |
---|
150 | n/a | case FFI_TYPE_UINT32: |
---|
151 | n/a | case FFI_TYPE_SINT32: |
---|
152 | n/a | case FFI_TYPE_UINT64: |
---|
153 | n/a | case FFI_TYPE_SINT64: |
---|
154 | n/a | case FFI_TYPE_POINTER: |
---|
155 | n/a | #if 0 |
---|
156 | n/a | if (byte_offset + type->size <= 4) |
---|
157 | n/a | classes[0] = X86_64_INTEGERSI_CLASS; |
---|
158 | n/a | else |
---|
159 | n/a | classes[0] = X86_64_INTEGER_CLASS; |
---|
160 | n/a | |
---|
161 | n/a | return 1; |
---|
162 | n/a | #else |
---|
163 | n/a | { |
---|
164 | n/a | int size = byte_offset + type->size; |
---|
165 | n/a | |
---|
166 | n/a | if (size <= 4) |
---|
167 | n/a | { |
---|
168 | n/a | classes[0] = X86_64_INTEGERSI_CLASS; |
---|
169 | n/a | return 1; |
---|
170 | n/a | } |
---|
171 | n/a | else if (size <= 8) |
---|
172 | n/a | { |
---|
173 | n/a | classes[0] = X86_64_INTEGER_CLASS; |
---|
174 | n/a | return 1; |
---|
175 | n/a | } |
---|
176 | n/a | else if (size <= 12) |
---|
177 | n/a | { |
---|
178 | n/a | classes[0] = X86_64_INTEGER_CLASS; |
---|
179 | n/a | classes[1] = X86_64_INTEGERSI_CLASS; |
---|
180 | n/a | return 2; |
---|
181 | n/a | } |
---|
182 | n/a | else if (size <= 16) |
---|
183 | n/a | { |
---|
184 | n/a | classes[0] = classes[1] = X86_64_INTEGERSI_CLASS; |
---|
185 | n/a | return 2; |
---|
186 | n/a | } |
---|
187 | n/a | else |
---|
188 | n/a | FFI_ASSERT (0); |
---|
189 | n/a | } |
---|
190 | n/a | #endif |
---|
191 | n/a | |
---|
192 | n/a | case FFI_TYPE_FLOAT: |
---|
193 | n/a | if (byte_offset == 0) |
---|
194 | n/a | classes[0] = X86_64_SSESF_CLASS; |
---|
195 | n/a | else |
---|
196 | n/a | classes[0] = X86_64_SSE_CLASS; |
---|
197 | n/a | |
---|
198 | n/a | return 1; |
---|
199 | n/a | |
---|
200 | n/a | case FFI_TYPE_DOUBLE: |
---|
201 | n/a | classes[0] = X86_64_SSEDF_CLASS; |
---|
202 | n/a | return 1; |
---|
203 | n/a | |
---|
204 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
205 | n/a | classes[0] = X86_64_X87_CLASS; |
---|
206 | n/a | classes[1] = X86_64_X87UP_CLASS; |
---|
207 | n/a | return 2; |
---|
208 | n/a | |
---|
209 | n/a | case FFI_TYPE_STRUCT: |
---|
210 | n/a | { |
---|
211 | n/a | ffi_type** ptr; |
---|
212 | n/a | int i; |
---|
213 | n/a | enum x86_64_reg_class subclasses[MAX_CLASSES]; |
---|
214 | n/a | const int UNITS_PER_WORD = 8; |
---|
215 | n/a | int words = |
---|
216 | n/a | (type->size + UNITS_PER_WORD - 1) / UNITS_PER_WORD; |
---|
217 | n/a | |
---|
218 | n/a | /* If the struct is larger than 16 bytes, pass it on the stack. */ |
---|
219 | n/a | if (type->size > 16) |
---|
220 | n/a | return 0; |
---|
221 | n/a | |
---|
222 | n/a | for (i = 0; i < words; i++) |
---|
223 | n/a | classes[i] = X86_64_NO_CLASS; |
---|
224 | n/a | |
---|
225 | n/a | /* Merge the fields of structure. */ |
---|
226 | n/a | for (ptr = type->elements; *ptr != NULL; ptr++) |
---|
227 | n/a | { |
---|
228 | n/a | int num, pos; |
---|
229 | n/a | |
---|
230 | n/a | byte_offset = ALIGN(byte_offset, (*ptr)->alignment); |
---|
231 | n/a | |
---|
232 | n/a | num = classify_argument(*ptr, subclasses, byte_offset % 8); |
---|
233 | n/a | |
---|
234 | n/a | if (num == 0) |
---|
235 | n/a | return 0; |
---|
236 | n/a | |
---|
237 | n/a | pos = byte_offset / 8; |
---|
238 | n/a | |
---|
239 | n/a | for (i = 0; i < num; i++) |
---|
240 | n/a | { |
---|
241 | n/a | classes[i + pos] = |
---|
242 | n/a | merge_classes(subclasses[i], classes[i + pos]); |
---|
243 | n/a | } |
---|
244 | n/a | |
---|
245 | n/a | byte_offset += (*ptr)->size; |
---|
246 | n/a | } |
---|
247 | n/a | |
---|
248 | n/a | if (words > 2) |
---|
249 | n/a | { |
---|
250 | n/a | /* When size > 16 bytes, if the first one isn't |
---|
251 | n/a | X86_64_SSE_CLASS or any other ones aren't |
---|
252 | n/a | X86_64_SSEUP_CLASS, everything should be passed in |
---|
253 | n/a | memory. */ |
---|
254 | n/a | if (classes[0] != X86_64_SSE_CLASS) |
---|
255 | n/a | return 0; |
---|
256 | n/a | |
---|
257 | n/a | for (i = 1; i < words; i++) |
---|
258 | n/a | if (classes[i] != X86_64_SSEUP_CLASS) |
---|
259 | n/a | return 0; |
---|
260 | n/a | } |
---|
261 | n/a | |
---|
262 | n/a | |
---|
263 | n/a | /* Final merger cleanup. */ |
---|
264 | n/a | for (i = 0; i < words; i++) |
---|
265 | n/a | { |
---|
266 | n/a | /* If one class is MEMORY, everything should be passed in |
---|
267 | n/a | memory. */ |
---|
268 | n/a | if (classes[i] == X86_64_MEMORY_CLASS) |
---|
269 | n/a | return 0; |
---|
270 | n/a | |
---|
271 | n/a | /* The X86_64_SSEUP_CLASS should be always preceded by |
---|
272 | n/a | X86_64_SSE_CLASS. */ |
---|
273 | n/a | if (classes[i] == X86_64_SSEUP_CLASS |
---|
274 | n/a | && classes[i - 1] != X86_64_SSE_CLASS |
---|
275 | n/a | && classes[i - 1] != X86_64_SSEUP_CLASS) |
---|
276 | n/a | { |
---|
277 | n/a | FFI_ASSERT(i != 0); |
---|
278 | n/a | classes[i] = X86_64_SSE_CLASS; |
---|
279 | n/a | } |
---|
280 | n/a | |
---|
281 | n/a | /* X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS. */ |
---|
282 | n/a | if (classes[i] == X86_64_X87UP_CLASS |
---|
283 | n/a | && classes[i - 1] != X86_64_X87_CLASS) |
---|
284 | n/a | { |
---|
285 | n/a | FFI_ASSERT(i != 0); |
---|
286 | n/a | classes[i] = X86_64_SSE_CLASS; |
---|
287 | n/a | } |
---|
288 | n/a | } |
---|
289 | n/a | |
---|
290 | n/a | return words; |
---|
291 | n/a | } |
---|
292 | n/a | |
---|
293 | n/a | default: |
---|
294 | n/a | FFI_ASSERT(0); |
---|
295 | n/a | } |
---|
296 | n/a | |
---|
297 | n/a | return 0; /* Never reached. */ |
---|
298 | n/a | } |
---|
299 | n/a | |
---|
300 | n/a | /* Examine the argument and return set number of register required in each |
---|
301 | n/a | class. Return zero if parameter should be passed in memory, otherwise |
---|
302 | n/a | the number of registers. */ |
---|
303 | n/a | static int |
---|
304 | n/a | examine_argument( |
---|
305 | n/a | ffi_type* type, |
---|
306 | n/a | enum x86_64_reg_class classes[MAX_CLASSES], |
---|
307 | n/a | _Bool in_return, |
---|
308 | n/a | int* pngpr, |
---|
309 | n/a | int* pnsse) |
---|
310 | n/a | { |
---|
311 | n/a | int n = classify_argument(type, classes, 0); |
---|
312 | n/a | int ngpr = 0; |
---|
313 | n/a | int nsse = 0; |
---|
314 | n/a | int i; |
---|
315 | n/a | |
---|
316 | n/a | if (n == 0) |
---|
317 | n/a | return 0; |
---|
318 | n/a | |
---|
319 | n/a | for (i = 0; i < n; ++i) |
---|
320 | n/a | { |
---|
321 | n/a | switch (classes[i]) |
---|
322 | n/a | { |
---|
323 | n/a | case X86_64_INTEGER_CLASS: |
---|
324 | n/a | case X86_64_INTEGERSI_CLASS: |
---|
325 | n/a | ngpr++; |
---|
326 | n/a | break; |
---|
327 | n/a | |
---|
328 | n/a | case X86_64_SSE_CLASS: |
---|
329 | n/a | case X86_64_SSESF_CLASS: |
---|
330 | n/a | case X86_64_SSEDF_CLASS: |
---|
331 | n/a | nsse++; |
---|
332 | n/a | break; |
---|
333 | n/a | |
---|
334 | n/a | case X86_64_NO_CLASS: |
---|
335 | n/a | case X86_64_SSEUP_CLASS: |
---|
336 | n/a | break; |
---|
337 | n/a | |
---|
338 | n/a | case X86_64_X87_CLASS: |
---|
339 | n/a | case X86_64_X87UP_CLASS: |
---|
340 | n/a | case X86_64_COMPLEX_X87_CLASS: |
---|
341 | n/a | return in_return != 0; |
---|
342 | n/a | |
---|
343 | n/a | default: |
---|
344 | n/a | abort(); |
---|
345 | n/a | } |
---|
346 | n/a | } |
---|
347 | n/a | |
---|
348 | n/a | *pngpr = ngpr; |
---|
349 | n/a | *pnsse = nsse; |
---|
350 | n/a | |
---|
351 | n/a | return n; |
---|
352 | n/a | } |
---|
353 | n/a | |
---|
354 | n/a | /* Perform machine dependent cif processing. */ |
---|
355 | n/a | ffi_status |
---|
356 | n/a | ffi_prep_cif_machdep( |
---|
357 | n/a | ffi_cif* cif) |
---|
358 | n/a | { |
---|
359 | n/a | int gprcount = 0; |
---|
360 | n/a | int ssecount = 0; |
---|
361 | n/a | int flags = cif->rtype->type; |
---|
362 | n/a | int i, avn, n, ngpr, nsse; |
---|
363 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
---|
364 | n/a | size_t bytes; |
---|
365 | n/a | |
---|
366 | n/a | if (flags != FFI_TYPE_VOID) |
---|
367 | n/a | { |
---|
368 | n/a | n = examine_argument (cif->rtype, classes, 1, &ngpr, &nsse); |
---|
369 | n/a | |
---|
370 | n/a | if (n == 0) |
---|
371 | n/a | { |
---|
372 | n/a | /* The return value is passed in memory. A pointer to that |
---|
373 | n/a | memory is the first argument. Allocate a register for it. */ |
---|
374 | n/a | gprcount++; |
---|
375 | n/a | |
---|
376 | n/a | /* We don't have to do anything in asm for the return. */ |
---|
377 | n/a | flags = FFI_TYPE_VOID; |
---|
378 | n/a | } |
---|
379 | n/a | else if (flags == FFI_TYPE_STRUCT) |
---|
380 | n/a | { |
---|
381 | n/a | /* Mark which registers the result appears in. */ |
---|
382 | n/a | _Bool sse0 = SSE_CLASS_P(classes[0]); |
---|
383 | n/a | _Bool sse1 = n == 2 && SSE_CLASS_P(classes[1]); |
---|
384 | n/a | |
---|
385 | n/a | if (sse0 && !sse1) |
---|
386 | n/a | flags |= 1 << 8; |
---|
387 | n/a | else if (!sse0 && sse1) |
---|
388 | n/a | flags |= 1 << 9; |
---|
389 | n/a | else if (sse0 && sse1) |
---|
390 | n/a | flags |= 1 << 10; |
---|
391 | n/a | |
---|
392 | n/a | /* Mark the true size of the structure. */ |
---|
393 | n/a | flags |= cif->rtype->size << 12; |
---|
394 | n/a | } |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | /* Go over all arguments and determine the way they should be passed. |
---|
398 | n/a | If it's in a register and there is space for it, let that be so. If |
---|
399 | n/a | not, add it's size to the stack byte count. */ |
---|
400 | n/a | for (bytes = 0, i = 0, avn = cif->nargs; i < avn; i++) |
---|
401 | n/a | { |
---|
402 | n/a | if (examine_argument(cif->arg_types[i], classes, 0, &ngpr, &nsse) == 0 |
---|
403 | n/a | || gprcount + ngpr > MAX_GPR_REGS |
---|
404 | n/a | || ssecount + nsse > MAX_SSE_REGS) |
---|
405 | n/a | { |
---|
406 | n/a | long align = cif->arg_types[i]->alignment; |
---|
407 | n/a | |
---|
408 | n/a | if (align < 8) |
---|
409 | n/a | align = 8; |
---|
410 | n/a | |
---|
411 | n/a | bytes = ALIGN(bytes, align); |
---|
412 | n/a | bytes += cif->arg_types[i]->size; |
---|
413 | n/a | } |
---|
414 | n/a | else |
---|
415 | n/a | { |
---|
416 | n/a | gprcount += ngpr; |
---|
417 | n/a | ssecount += nsse; |
---|
418 | n/a | } |
---|
419 | n/a | } |
---|
420 | n/a | |
---|
421 | n/a | if (ssecount) |
---|
422 | n/a | flags |= 1 << 11; |
---|
423 | n/a | |
---|
424 | n/a | cif->flags = flags; |
---|
425 | n/a | cif->bytes = bytes; |
---|
426 | n/a | cif->bytes = ALIGN(bytes,8); |
---|
427 | n/a | |
---|
428 | n/a | return FFI_OK; |
---|
429 | n/a | } |
---|
430 | n/a | |
---|
431 | n/a | void |
---|
432 | n/a | ffi_call( |
---|
433 | n/a | ffi_cif* cif, |
---|
434 | n/a | void (*fn)(void), |
---|
435 | n/a | void* rvalue, |
---|
436 | n/a | void** avalue) |
---|
437 | n/a | { |
---|
438 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
---|
439 | n/a | char* stack; |
---|
440 | n/a | char* argp; |
---|
441 | n/a | ffi_type** arg_types; |
---|
442 | n/a | int gprcount, ssecount, ngpr, nsse, i, avn; |
---|
443 | n/a | _Bool ret_in_memory; |
---|
444 | n/a | RegisterArgs* reg_args; |
---|
445 | n/a | |
---|
446 | n/a | /* Can't call 32-bit mode from 64-bit mode. */ |
---|
447 | n/a | FFI_ASSERT(cif->abi == FFI_UNIX64); |
---|
448 | n/a | |
---|
449 | n/a | /* If the return value is a struct and we don't have a return value |
---|
450 | n/a | address then we need to make one. Note the setting of flags to |
---|
451 | n/a | VOID above in ffi_prep_cif_machdep. */ |
---|
452 | n/a | ret_in_memory = (cif->rtype->type == FFI_TYPE_STRUCT |
---|
453 | n/a | && (cif->flags & 0xff) == FFI_TYPE_VOID); |
---|
454 | n/a | |
---|
455 | n/a | if (rvalue == NULL && ret_in_memory) |
---|
456 | n/a | rvalue = alloca (cif->rtype->size); |
---|
457 | n/a | |
---|
458 | n/a | /* Allocate the space for the arguments, plus 4 words of temp space. */ |
---|
459 | n/a | stack = alloca(sizeof(RegisterArgs) + cif->bytes + 4 * 8); |
---|
460 | n/a | reg_args = (RegisterArgs*)stack; |
---|
461 | n/a | argp = stack + sizeof(RegisterArgs); |
---|
462 | n/a | |
---|
463 | n/a | gprcount = ssecount = 0; |
---|
464 | n/a | |
---|
465 | n/a | /* If the return value is passed in memory, add the pointer as the |
---|
466 | n/a | first integer argument. */ |
---|
467 | n/a | if (ret_in_memory) |
---|
468 | n/a | reg_args->gpr[gprcount++] = (long) rvalue; |
---|
469 | n/a | |
---|
470 | n/a | avn = cif->nargs; |
---|
471 | n/a | arg_types = cif->arg_types; |
---|
472 | n/a | |
---|
473 | n/a | for (i = 0; i < avn; ++i) |
---|
474 | n/a | { |
---|
475 | n/a | size_t size = arg_types[i]->size; |
---|
476 | n/a | int n; |
---|
477 | n/a | |
---|
478 | n/a | n = examine_argument (arg_types[i], classes, 0, &ngpr, &nsse); |
---|
479 | n/a | |
---|
480 | n/a | if (n == 0 |
---|
481 | n/a | || gprcount + ngpr > MAX_GPR_REGS |
---|
482 | n/a | || ssecount + nsse > MAX_SSE_REGS) |
---|
483 | n/a | { |
---|
484 | n/a | long align = arg_types[i]->alignment; |
---|
485 | n/a | |
---|
486 | n/a | /* Stack arguments are *always* at least 8 byte aligned. */ |
---|
487 | n/a | if (align < 8) |
---|
488 | n/a | align = 8; |
---|
489 | n/a | |
---|
490 | n/a | /* Pass this argument in memory. */ |
---|
491 | n/a | argp = (void *) ALIGN (argp, align); |
---|
492 | n/a | memcpy (argp, avalue[i], size); |
---|
493 | n/a | argp += size; |
---|
494 | n/a | } |
---|
495 | n/a | else |
---|
496 | n/a | { /* The argument is passed entirely in registers. */ |
---|
497 | n/a | char *a = (char *) avalue[i]; |
---|
498 | n/a | int j; |
---|
499 | n/a | |
---|
500 | n/a | for (j = 0; j < n; j++, a += 8, size -= 8) |
---|
501 | n/a | { |
---|
502 | n/a | switch (classes[j]) |
---|
503 | n/a | { |
---|
504 | n/a | case X86_64_INTEGER_CLASS: |
---|
505 | n/a | case X86_64_INTEGERSI_CLASS: |
---|
506 | n/a | reg_args->gpr[gprcount] = 0; |
---|
507 | n/a | switch (arg_types[i]->type) { |
---|
508 | n/a | case FFI_TYPE_SINT8: |
---|
509 | n/a | { |
---|
510 | n/a | int8_t shortval = *(int8_t*)a; |
---|
511 | n/a | int64_t actval = (int64_t)shortval; |
---|
512 | n/a | reg_args->gpr[gprcount] = actval; |
---|
513 | n/a | /*memcpy (®_args->gpr[gprcount], &actval, 8);*/ |
---|
514 | n/a | break; |
---|
515 | n/a | } |
---|
516 | n/a | |
---|
517 | n/a | case FFI_TYPE_SINT16: |
---|
518 | n/a | { |
---|
519 | n/a | int16_t shortval = *(int16_t*)a; |
---|
520 | n/a | int64_t actval = (int64_t)shortval; |
---|
521 | n/a | memcpy (®_args->gpr[gprcount], &actval, 8); |
---|
522 | n/a | break; |
---|
523 | n/a | } |
---|
524 | n/a | |
---|
525 | n/a | case FFI_TYPE_SINT32: |
---|
526 | n/a | { |
---|
527 | n/a | int32_t shortval = *(int32_t*)a; |
---|
528 | n/a | int64_t actval = (int64_t)shortval; |
---|
529 | n/a | memcpy (®_args->gpr[gprcount], &actval, 8); |
---|
530 | n/a | break; |
---|
531 | n/a | } |
---|
532 | n/a | |
---|
533 | n/a | case FFI_TYPE_UINT8: |
---|
534 | n/a | { |
---|
535 | n/a | u_int8_t shortval = *(u_int8_t*)a; |
---|
536 | n/a | u_int64_t actval = (u_int64_t)shortval; |
---|
537 | n/a | /*memcpy (®_args->gpr[gprcount], &actval, 8);*/ |
---|
538 | n/a | reg_args->gpr[gprcount] = actval; |
---|
539 | n/a | break; |
---|
540 | n/a | } |
---|
541 | n/a | |
---|
542 | n/a | case FFI_TYPE_UINT16: |
---|
543 | n/a | { |
---|
544 | n/a | u_int16_t shortval = *(u_int16_t*)a; |
---|
545 | n/a | u_int64_t actval = (u_int64_t)shortval; |
---|
546 | n/a | memcpy (®_args->gpr[gprcount], &actval, 8); |
---|
547 | n/a | break; |
---|
548 | n/a | } |
---|
549 | n/a | |
---|
550 | n/a | case FFI_TYPE_UINT32: |
---|
551 | n/a | { |
---|
552 | n/a | u_int32_t shortval = *(u_int32_t*)a; |
---|
553 | n/a | u_int64_t actval = (u_int64_t)shortval; |
---|
554 | n/a | memcpy (®_args->gpr[gprcount], &actval, 8); |
---|
555 | n/a | break; |
---|
556 | n/a | } |
---|
557 | n/a | |
---|
558 | n/a | default: |
---|
559 | n/a | //memcpy (®_args->gpr[gprcount], a, size < 8 ? size : 8); |
---|
560 | n/a | reg_args->gpr[gprcount] = *(int64_t*)a; |
---|
561 | n/a | } |
---|
562 | n/a | gprcount++; |
---|
563 | n/a | break; |
---|
564 | n/a | |
---|
565 | n/a | case X86_64_SSE_CLASS: |
---|
566 | n/a | case X86_64_SSEDF_CLASS: |
---|
567 | n/a | reg_args->sse[ssecount++] = *(UINT64 *) a; |
---|
568 | n/a | break; |
---|
569 | n/a | |
---|
570 | n/a | case X86_64_SSESF_CLASS: |
---|
571 | n/a | reg_args->sse[ssecount++] = *(UINT32 *) a; |
---|
572 | n/a | break; |
---|
573 | n/a | |
---|
574 | n/a | default: |
---|
575 | n/a | abort(); |
---|
576 | n/a | } |
---|
577 | n/a | } |
---|
578 | n/a | } |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | ffi_call_unix64 (stack, cif->bytes + sizeof(RegisterArgs), |
---|
582 | n/a | cif->flags, rvalue, fn, ssecount); |
---|
583 | n/a | } |
---|
584 | n/a | |
---|
585 | n/a | extern void ffi_closure_unix64(void); |
---|
586 | n/a | |
---|
587 | n/a | ffi_status |
---|
588 | n/a | ffi_prep_closure( |
---|
589 | n/a | ffi_closure* closure, |
---|
590 | n/a | ffi_cif* cif, |
---|
591 | n/a | void (*fun)(ffi_cif*, void*, void**, void*), |
---|
592 | n/a | void* user_data) |
---|
593 | n/a | { |
---|
594 | n/a | volatile unsigned short* tramp; |
---|
595 | n/a | |
---|
596 | n/a | if (cif->abi != FFI_UNIX64) |
---|
597 | n/a | return FFI_BAD_ABI; |
---|
598 | n/a | |
---|
599 | n/a | tramp = (volatile unsigned short*)&closure->tramp[0]; |
---|
600 | n/a | |
---|
601 | n/a | tramp[0] = 0xbb49; /* mov <code>, %r11 */ |
---|
602 | n/a | *(void* volatile*)&tramp[1] = ffi_closure_unix64; |
---|
603 | n/a | tramp[5] = 0xba49; /* mov <data>, %r10 */ |
---|
604 | n/a | *(void* volatile*)&tramp[6] = closure; |
---|
605 | n/a | |
---|
606 | n/a | /* Set the carry bit if the function uses any sse registers. |
---|
607 | n/a | This is clc or stc, together with the first byte of the jmp. */ |
---|
608 | n/a | tramp[10] = cif->flags & (1 << 11) ? 0x49f9 : 0x49f8; |
---|
609 | n/a | tramp[11] = 0xe3ff; /* jmp *%r11 */ |
---|
610 | n/a | |
---|
611 | n/a | closure->cif = cif; |
---|
612 | n/a | closure->fun = fun; |
---|
613 | n/a | closure->user_data = user_data; |
---|
614 | n/a | |
---|
615 | n/a | return FFI_OK; |
---|
616 | n/a | } |
---|
617 | n/a | |
---|
618 | n/a | #pragma clang diagnostic push |
---|
619 | n/a | #pragma clang diagnostic ignored "-Wmissing-prototypes" |
---|
620 | n/a | int |
---|
621 | n/a | ffi_closure_unix64_inner( |
---|
622 | n/a | ffi_closure* closure, |
---|
623 | n/a | void* rvalue, |
---|
624 | n/a | RegisterArgs* reg_args, |
---|
625 | n/a | char* argp) |
---|
626 | n/a | #pragma clang diagnostic pop |
---|
627 | n/a | { |
---|
628 | n/a | ffi_cif* cif = closure->cif; |
---|
629 | n/a | void** avalue = alloca(cif->nargs * sizeof(void *)); |
---|
630 | n/a | ffi_type** arg_types; |
---|
631 | n/a | long i, avn; |
---|
632 | n/a | int gprcount = 0; |
---|
633 | n/a | int ssecount = 0; |
---|
634 | n/a | int ngpr, nsse; |
---|
635 | n/a | int ret; |
---|
636 | n/a | |
---|
637 | n/a | ret = cif->rtype->type; |
---|
638 | n/a | |
---|
639 | n/a | if (ret != FFI_TYPE_VOID) |
---|
640 | n/a | { |
---|
641 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
---|
642 | n/a | int n = examine_argument (cif->rtype, classes, 1, &ngpr, &nsse); |
---|
643 | n/a | |
---|
644 | n/a | if (n == 0) |
---|
645 | n/a | { |
---|
646 | n/a | /* The return value goes in memory. Arrange for the closure |
---|
647 | n/a | return value to go directly back to the original caller. */ |
---|
648 | n/a | rvalue = (void *) reg_args->gpr[gprcount++]; |
---|
649 | n/a | |
---|
650 | n/a | /* We don't have to do anything in asm for the return. */ |
---|
651 | n/a | ret = FFI_TYPE_VOID; |
---|
652 | n/a | } |
---|
653 | n/a | else if (ret == FFI_TYPE_STRUCT && n == 2) |
---|
654 | n/a | { |
---|
655 | n/a | /* Mark which register the second word of the structure goes in. */ |
---|
656 | n/a | _Bool sse0 = SSE_CLASS_P (classes[0]); |
---|
657 | n/a | _Bool sse1 = SSE_CLASS_P (classes[1]); |
---|
658 | n/a | |
---|
659 | n/a | if (!sse0 && sse1) |
---|
660 | n/a | ret |= 1 << 8; |
---|
661 | n/a | else if (sse0 && !sse1) |
---|
662 | n/a | ret |= 1 << 9; |
---|
663 | n/a | } |
---|
664 | n/a | } |
---|
665 | n/a | |
---|
666 | n/a | avn = cif->nargs; |
---|
667 | n/a | arg_types = cif->arg_types; |
---|
668 | n/a | |
---|
669 | n/a | for (i = 0; i < avn; ++i) |
---|
670 | n/a | { |
---|
671 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
---|
672 | n/a | int n; |
---|
673 | n/a | |
---|
674 | n/a | n = examine_argument (arg_types[i], classes, 0, &ngpr, &nsse); |
---|
675 | n/a | |
---|
676 | n/a | if (n == 0 |
---|
677 | n/a | || gprcount + ngpr > MAX_GPR_REGS |
---|
678 | n/a | || ssecount + nsse > MAX_SSE_REGS) |
---|
679 | n/a | { |
---|
680 | n/a | long align = arg_types[i]->alignment; |
---|
681 | n/a | |
---|
682 | n/a | /* Stack arguments are *always* at least 8 byte aligned. */ |
---|
683 | n/a | if (align < 8) |
---|
684 | n/a | align = 8; |
---|
685 | n/a | |
---|
686 | n/a | /* Pass this argument in memory. */ |
---|
687 | n/a | argp = (void *) ALIGN (argp, align); |
---|
688 | n/a | avalue[i] = argp; |
---|
689 | n/a | argp += arg_types[i]->size; |
---|
690 | n/a | } |
---|
691 | n/a | |
---|
692 | n/a | #if !defined(X86_DARWIN) |
---|
693 | n/a | /* If the argument is in a single register, or two consecutive |
---|
694 | n/a | registers, then we can use that address directly. */ |
---|
695 | n/a | else if (n == 1 || (n == 2 && |
---|
696 | n/a | SSE_CLASS_P (classes[0]) == SSE_CLASS_P (classes[1]))) |
---|
697 | n/a | { |
---|
698 | n/a | // The argument is in a single register. |
---|
699 | n/a | if (SSE_CLASS_P (classes[0])) |
---|
700 | n/a | { |
---|
701 | n/a | avalue[i] = ®_args->sse[ssecount]; |
---|
702 | n/a | ssecount += n; |
---|
703 | n/a | } |
---|
704 | n/a | else |
---|
705 | n/a | { |
---|
706 | n/a | avalue[i] = ®_args->gpr[gprcount]; |
---|
707 | n/a | gprcount += n; |
---|
708 | n/a | } |
---|
709 | n/a | } |
---|
710 | n/a | #endif |
---|
711 | n/a | |
---|
712 | n/a | /* Otherwise, allocate space to make them consecutive. */ |
---|
713 | n/a | else |
---|
714 | n/a | { |
---|
715 | n/a | char *a = alloca (16); |
---|
716 | n/a | int j; |
---|
717 | n/a | |
---|
718 | n/a | avalue[i] = a; |
---|
719 | n/a | |
---|
720 | n/a | for (j = 0; j < n; j++, a += 8) |
---|
721 | n/a | { |
---|
722 | n/a | if (SSE_CLASS_P (classes[j])) |
---|
723 | n/a | memcpy (a, ®_args->sse[ssecount++], 8); |
---|
724 | n/a | else |
---|
725 | n/a | memcpy (a, ®_args->gpr[gprcount++], 8); |
---|
726 | n/a | } |
---|
727 | n/a | } |
---|
728 | n/a | } |
---|
729 | n/a | |
---|
730 | n/a | /* Invoke the closure. */ |
---|
731 | n/a | closure->fun (cif, rvalue, avalue, closure->user_data); |
---|
732 | n/a | |
---|
733 | n/a | /* Tell assembly how to perform return type promotions. */ |
---|
734 | n/a | return ret; |
---|
735 | n/a | } |
---|
736 | n/a | |
---|
737 | n/a | #endif /* __x86_64__ */ |
---|