1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2002, 2007 Bo Thorsen <bo@suse.de>
4 x86-64 Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
28 #include <ffi_common.h>
35 #define MAX_GPR_REGS 6
36 #define MAX_SSE_REGS 8
40 /* Registers for argument passing. */
41 UINT64 gpr
[MAX_GPR_REGS
];
42 __int128_t sse
[MAX_SSE_REGS
];
45 extern void ffi_call_unix64 (void *args
, unsigned long bytes
, unsigned flags
,
46 void *raddr
, void (*fnaddr
)(), unsigned ssecount
);
48 /* All reference to register classes here is identical to the code in
49 gcc/config/i386/i386.c. Do *not* change one without the other. */
51 /* Register class used for passing given 64bit part of the argument.
52 These represent classes as documented by the PS ABI, with the exception
53 of SSESF, SSEDF classes, that are basically SSE class, just gcc will
54 use SF or DFmode move instead of DImode to avoid reformating penalties.
56 Similary we play games with INTEGERSI_CLASS to use cheaper SImode moves
57 whenever possible (upper half does contain padding). */
62 X86_64_INTEGERSI_CLASS
,
69 X86_64_COMPLEX_X87_CLASS
,
75 #define SSE_CLASS_P(X) ((X) >= X86_64_SSE_CLASS && X <= X86_64_SSEUP_CLASS)
77 /* x86-64 register passing implementation. See x86-64 ABI for details. Goal
78 of this code is to classify each 8bytes of incoming argument by the register
79 class and assign registers accordingly. */
81 /* Return the union class of CLASS1 and CLASS2.
82 See the x86-64 PS ABI for details. */
84 static enum x86_64_reg_class
85 merge_classes (enum x86_64_reg_class class1
, enum x86_64_reg_class class2
)
87 /* Rule #1: If both classes are equal, this is the resulting class. */
91 /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
93 if (class1
== X86_64_NO_CLASS
)
95 if (class2
== X86_64_NO_CLASS
)
98 /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */
99 if (class1
== X86_64_MEMORY_CLASS
|| class2
== X86_64_MEMORY_CLASS
)
100 return X86_64_MEMORY_CLASS
;
102 /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */
103 if ((class1
== X86_64_INTEGERSI_CLASS
&& class2
== X86_64_SSESF_CLASS
)
104 || (class2
== X86_64_INTEGERSI_CLASS
&& class1
== X86_64_SSESF_CLASS
))
105 return X86_64_INTEGERSI_CLASS
;
106 if (class1
== X86_64_INTEGER_CLASS
|| class1
== X86_64_INTEGERSI_CLASS
107 || class2
== X86_64_INTEGER_CLASS
|| class2
== X86_64_INTEGERSI_CLASS
)
108 return X86_64_INTEGER_CLASS
;
110 /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
112 if (class1
== X86_64_X87_CLASS
113 || class1
== X86_64_X87UP_CLASS
114 || class1
== X86_64_COMPLEX_X87_CLASS
115 || class2
== X86_64_X87_CLASS
116 || class2
== X86_64_X87UP_CLASS
117 || class2
== X86_64_COMPLEX_X87_CLASS
)
118 return X86_64_MEMORY_CLASS
;
120 /* Rule #6: Otherwise class SSE is used. */
121 return X86_64_SSE_CLASS
;
124 /* Classify the argument of type TYPE and mode MODE.
125 CLASSES will be filled by the register class used to pass each word
126 of the operand. The number of words is returned. In case the parameter
127 should be passed in memory, 0 is returned. As a special case for zero
128 sized containers, classes[0] will be NO_CLASS and 1 is returned.
130 See the x86-64 PS ABI for details.
133 classify_argument (ffi_type
*type
, enum x86_64_reg_class classes
[],
140 case FFI_TYPE_UINT16
:
141 case FFI_TYPE_SINT16
:
142 case FFI_TYPE_UINT32
:
143 case FFI_TYPE_SINT32
:
144 case FFI_TYPE_UINT64
:
145 case FFI_TYPE_SINT64
:
146 case FFI_TYPE_POINTER
:
147 if (byte_offset
+ type
->size
<= 4)
148 classes
[0] = X86_64_INTEGERSI_CLASS
;
150 classes
[0] = X86_64_INTEGER_CLASS
;
153 if (byte_offset
== 0)
154 classes
[0] = X86_64_SSESF_CLASS
;
156 classes
[0] = X86_64_SSE_CLASS
;
158 case FFI_TYPE_DOUBLE
:
159 classes
[0] = X86_64_SSEDF_CLASS
;
161 case FFI_TYPE_LONGDOUBLE
:
162 classes
[0] = X86_64_X87_CLASS
;
163 classes
[1] = X86_64_X87UP_CLASS
;
165 case FFI_TYPE_STRUCT
:
167 const int UNITS_PER_WORD
= 8;
168 int words
= (type
->size
+ UNITS_PER_WORD
- 1) / UNITS_PER_WORD
;
171 enum x86_64_reg_class subclasses
[MAX_CLASSES
];
173 /* If the struct is larger than 16 bytes, pass it on the stack. */
177 for (i
= 0; i
< words
; i
++)
178 classes
[i
] = X86_64_NO_CLASS
;
180 /* Merge the fields of structure. */
181 for (ptr
= type
->elements
; *ptr
!= NULL
; ptr
++)
185 byte_offset
= ALIGN (byte_offset
, (*ptr
)->alignment
);
187 num
= classify_argument (*ptr
, subclasses
, byte_offset
% 8);
190 for (i
= 0; i
< num
; i
++)
192 int pos
= byte_offset
/ 8;
194 merge_classes (subclasses
[i
], classes
[i
+ pos
]);
197 byte_offset
+= (*ptr
)->size
;
200 /* Final merger cleanup. */
201 for (i
= 0; i
< words
; i
++)
203 /* If one class is MEMORY, everything should be passed in
205 if (classes
[i
] == X86_64_MEMORY_CLASS
)
208 /* The X86_64_SSEUP_CLASS should be always preceded by
210 if (classes
[i
] == X86_64_SSEUP_CLASS
211 && (i
== 0 || classes
[i
- 1] != X86_64_SSE_CLASS
))
212 classes
[i
] = X86_64_SSE_CLASS
;
214 /* X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS. */
215 if (classes
[i
] == X86_64_X87UP_CLASS
216 && (i
== 0 || classes
[i
- 1] != X86_64_X87_CLASS
))
217 classes
[i
] = X86_64_SSE_CLASS
;
225 return 0; /* Never reached. */
228 /* Examine the argument and return set number of register required in each
229 class. Return zero iff parameter should be passed in memory, otherwise
230 the number of registers. */
233 examine_argument (ffi_type
*type
, enum x86_64_reg_class classes
[MAX_CLASSES
],
234 _Bool in_return
, int *pngpr
, int *pnsse
)
236 int i
, n
, ngpr
, nsse
;
238 n
= classify_argument (type
, classes
, 0);
243 for (i
= 0; i
< n
; ++i
)
246 case X86_64_INTEGER_CLASS
:
247 case X86_64_INTEGERSI_CLASS
:
250 case X86_64_SSE_CLASS
:
251 case X86_64_SSESF_CLASS
:
252 case X86_64_SSEDF_CLASS
:
255 case X86_64_NO_CLASS
:
256 case X86_64_SSEUP_CLASS
:
258 case X86_64_X87_CLASS
:
259 case X86_64_X87UP_CLASS
:
260 case X86_64_COMPLEX_X87_CLASS
:
261 return in_return
!= 0;
272 /* Perform machine dependent cif processing. */
275 ffi_prep_cif_machdep (ffi_cif
*cif
)
277 int gprcount
, ssecount
, i
, avn
, n
, ngpr
, nsse
, flags
;
278 enum x86_64_reg_class classes
[MAX_CLASSES
];
281 gprcount
= ssecount
= 0;
283 flags
= cif
->rtype
->type
;
284 if (flags
!= FFI_TYPE_VOID
)
286 n
= examine_argument (cif
->rtype
, classes
, 1, &ngpr
, &nsse
);
289 /* The return value is passed in memory. A pointer to that
290 memory is the first argument. Allocate a register for it. */
292 /* We don't have to do anything in asm for the return. */
293 flags
= FFI_TYPE_VOID
;
295 else if (flags
== FFI_TYPE_STRUCT
)
297 /* Mark which registers the result appears in. */
298 _Bool sse0
= SSE_CLASS_P (classes
[0]);
299 _Bool sse1
= n
== 2 && SSE_CLASS_P (classes
[1]);
302 else if (!sse0
&& sse1
)
304 else if (sse0
&& sse1
)
306 /* Mark the true size of the structure. */
307 flags
|= cif
->rtype
->size
<< 12;
311 /* Go over all arguments and determine the way they should be passed.
312 If it's in a register and there is space for it, let that be so. If
313 not, add it's size to the stack byte count. */
314 for (bytes
= 0, i
= 0, avn
= cif
->nargs
; i
< avn
; i
++)
316 if (examine_argument (cif
->arg_types
[i
], classes
, 0, &ngpr
, &nsse
) == 0
317 || gprcount
+ ngpr
> MAX_GPR_REGS
318 || ssecount
+ nsse
> MAX_SSE_REGS
)
320 long align
= cif
->arg_types
[i
]->alignment
;
325 bytes
= ALIGN(bytes
, align
);
326 bytes
+= cif
->arg_types
[i
]->size
;
343 ffi_call (ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
345 enum x86_64_reg_class classes
[MAX_CLASSES
];
347 ffi_type
**arg_types
;
348 int gprcount
, ssecount
, ngpr
, nsse
, i
, avn
;
350 struct register_args
*reg_args
;
352 /* Can't call 32-bit mode from 64-bit mode. */
353 FFI_ASSERT (cif
->abi
== FFI_UNIX64
);
355 /* If the return value is a struct and we don't have a return value
356 address then we need to make one. Note the setting of flags to
357 VOID above in ffi_prep_cif_machdep. */
358 ret_in_memory
= (cif
->rtype
->type
== FFI_TYPE_STRUCT
359 && (cif
->flags
& 0xff) == FFI_TYPE_VOID
);
360 if (rvalue
== NULL
&& ret_in_memory
)
361 rvalue
= alloca (cif
->rtype
->size
);
363 /* Allocate the space for the arguments, plus 4 words of temp space. */
364 stack
= alloca (sizeof (struct register_args
) + cif
->bytes
+ 4*8);
365 reg_args
= (struct register_args
*) stack
;
366 argp
= stack
+ sizeof (struct register_args
);
368 gprcount
= ssecount
= 0;
370 /* If the return value is passed in memory, add the pointer as the
371 first integer argument. */
373 reg_args
->gpr
[gprcount
++] = (long) rvalue
;
376 arg_types
= cif
->arg_types
;
378 for (i
= 0; i
< avn
; ++i
)
380 size_t size
= arg_types
[i
]->size
;
383 n
= examine_argument (arg_types
[i
], classes
, 0, &ngpr
, &nsse
);
385 || gprcount
+ ngpr
> MAX_GPR_REGS
386 || ssecount
+ nsse
> MAX_SSE_REGS
)
388 long align
= arg_types
[i
]->alignment
;
390 /* Stack arguments are *always* at least 8 byte aligned. */
394 /* Pass this argument in memory. */
395 argp
= (void *) ALIGN (argp
, align
);
396 memcpy (argp
, avalue
[i
], size
);
401 /* The argument is passed entirely in registers. */
402 char *a
= (char *) avalue
[i
];
405 for (j
= 0; j
< n
; j
++, a
+= 8, size
-= 8)
409 case X86_64_INTEGER_CLASS
:
410 case X86_64_INTEGERSI_CLASS
:
411 reg_args
->gpr
[gprcount
] = 0;
412 memcpy (®_args
->gpr
[gprcount
], a
, size
< 8 ? size
: 8);
415 case X86_64_SSE_CLASS
:
416 case X86_64_SSEDF_CLASS
:
417 reg_args
->sse
[ssecount
++] = *(UINT64
*) a
;
419 case X86_64_SSESF_CLASS
:
420 reg_args
->sse
[ssecount
++] = *(UINT32
*) a
;
429 ffi_call_unix64 (stack
, cif
->bytes
+ sizeof (struct register_args
),
430 cif
->flags
, rvalue
, fn
, ssecount
);
434 extern void ffi_closure_unix64(void);
437 ffi_prep_closure_loc (ffi_closure
* closure
,
439 void (*fun
)(ffi_cif
*, void*, void**, void*),
443 volatile unsigned short *tramp
;
445 tramp
= (volatile unsigned short *) &closure
->tramp
[0];
447 tramp
[0] = 0xbb49; /* mov <code>, %r11 */
448 *(void * volatile *) &tramp
[1] = ffi_closure_unix64
;
449 tramp
[5] = 0xba49; /* mov <data>, %r10 */
450 *(void * volatile *) &tramp
[6] = codeloc
;
452 /* Set the carry bit iff the function uses any sse registers.
453 This is clc or stc, together with the first byte of the jmp. */
454 tramp
[10] = cif
->flags
& (1 << 11) ? 0x49f9 : 0x49f8;
456 tramp
[11] = 0xe3ff; /* jmp *%r11 */
460 closure
->user_data
= user_data
;
466 ffi_closure_unix64_inner(ffi_closure
*closure
, void *rvalue
,
467 struct register_args
*reg_args
, char *argp
)
471 ffi_type
**arg_types
;
473 int gprcount
, ssecount
, ngpr
, nsse
;
477 avalue
= alloca(cif
->nargs
* sizeof(void *));
478 gprcount
= ssecount
= 0;
480 ret
= cif
->rtype
->type
;
481 if (ret
!= FFI_TYPE_VOID
)
483 enum x86_64_reg_class classes
[MAX_CLASSES
];
484 int n
= examine_argument (cif
->rtype
, classes
, 1, &ngpr
, &nsse
);
487 /* The return value goes in memory. Arrange for the closure
488 return value to go directly back to the original caller. */
489 rvalue
= (void *) reg_args
->gpr
[gprcount
++];
490 /* We don't have to do anything in asm for the return. */
493 else if (ret
== FFI_TYPE_STRUCT
&& n
== 2)
495 /* Mark which register the second word of the structure goes in. */
496 _Bool sse0
= SSE_CLASS_P (classes
[0]);
497 _Bool sse1
= SSE_CLASS_P (classes
[1]);
500 else if (sse0
&& !sse1
)
506 arg_types
= cif
->arg_types
;
508 for (i
= 0; i
< avn
; ++i
)
510 enum x86_64_reg_class classes
[MAX_CLASSES
];
513 n
= examine_argument (arg_types
[i
], classes
, 0, &ngpr
, &nsse
);
515 || gprcount
+ ngpr
> MAX_GPR_REGS
516 || ssecount
+ nsse
> MAX_SSE_REGS
)
518 long align
= arg_types
[i
]->alignment
;
520 /* Stack arguments are *always* at least 8 byte aligned. */
524 /* Pass this argument in memory. */
525 argp
= (void *) ALIGN (argp
, align
);
527 argp
+= arg_types
[i
]->size
;
529 /* If the argument is in a single register, or two consecutive
530 registers, then we can use that address directly. */
533 && SSE_CLASS_P (classes
[0]) == SSE_CLASS_P (classes
[1])))
535 /* The argument is in a single register. */
536 if (SSE_CLASS_P (classes
[0]))
538 avalue
[i
] = ®_args
->sse
[ssecount
];
543 avalue
[i
] = ®_args
->gpr
[gprcount
];
547 /* Otherwise, allocate space to make them consecutive. */
550 char *a
= alloca (16);
554 for (j
= 0; j
< n
; j
++, a
+= 8)
556 if (SSE_CLASS_P (classes
[j
]))
557 memcpy (a
, ®_args
->sse
[ssecount
++], 8);
559 memcpy (a
, ®_args
->gpr
[gprcount
++], 8);
564 /* Invoke the closure. */
565 closure
->fun (cif
, rvalue
, avalue
, closure
->user_data
);
567 /* Tell assembly how to perform return type promotions. */
571 #endif /* __x86_64__ */