Debian Codegraph @

src/ia64/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998, 2007, 2008, 2012 Red Hat, Inc.
3 Copyright (c) 2000 Hewlett Packard Company
4 Copyright (c) 2011 Anthony Green
5
6 IA64 Foreign Function Interface
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 ``Software''), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 ----------------------------------------------------------------------- */
28
29 #include <ffi.h>
30 #include <ffi_common.h>
31
32 #include <stdlib.h>
33 #include <stdbool.h>
34 #include <float.h>
35
36 #include "ia64_flags.h"
37
38 /* A 64-bit pointer value. In LP64 mode, this is effectively a plain
39 pointer. In ILP32 mode, it's a pointer that's been extended to
40 64 bits by "addp4". */
41 #ifdef __hpux
42 typedef void *PTR64;
43 #else // some other unix
44 typedef void *PTR64 __attribute__((mode(DI)));
45 #endif
46
47 /* Memory image of fp register contents. This is the implementation
48 specific format used by ldf.fill/stf.spill. All we care about is
49 that it wants a 16 byte aligned slot. */
50 typedef struct
51 {
52 UINT64 x[2] __attribute__((aligned(16)));
53 } fpreg;
54
55
56 /* The stack layout given to ffi_call_unix and ffi_closure_unix_inner. */
57
58 struct ia64_args
59 {
60 fpreg fp_regs[8]; /* Contents of 8 fp arg registers. */
61 UINT64 gp_regs[8]; /* Contents of 8 gp arg registers. */
62 UINT64 other_args[]; /* Arguments passed on stack, variable size. */
63 };
64
65
66 /* Adjust ADDR, a pointer to an 8 byte slot, to point to the low LEN bytes. */
67
68 static inline void *
69 endian_adjust (void *addr, size_t len)
70 {
71 #ifdef __BIG_ENDIAN__
72 return addr + (8 - len);
73 #else
74 return addr;
75 #endif
76 }
77
78 /* Store VALUE to ADDR in the current cpu implementation's fp spill format.
79 This is a macro instead of a function, so that it works for all 3 floating
80 point types without type conversions. Type conversion to long double breaks
81 the denorm support. */
82
83 #ifdef __hpux
84 #define stf_spill(addr, value)
85 #else
86 #define stf_spill(addr, value) \
87 asm ("stf.spill %0 = %1%P0" : "=m" (*addr) : "f"(value));
88 #endif
89
90 /* Load a value from ADDR, which is in the current cpu implementation's
91 fp spill format. As above, this must also be a macro. */
92
93 #ifdef __hpux
94 #define ldf_fill(result, addr)
95 #else
96 #define ldf_fill(result, addr) \
97 asm ("ldf.fill %0 = %1%P1" : "=f"(result) : "m"(*addr));
98 #endif
99
100 /* Return the size of the C type associated with with TYPE. Which will
101 be one of the FFI_IA64_TYPE_HFA_* values. */
102
103 static size_t
104 hfa_type_size (int type)
105 {
106 switch (type)
107 {
108 case FFI_IA64_TYPE_HFA_FLOAT:
109 return sizeof(float);
110 case FFI_IA64_TYPE_HFA_DOUBLE:
111 return sizeof(double);
112 case FFI_IA64_TYPE_HFA_LDOUBLE:
113 return sizeof(__float80);
114 default:
115 abort ();
116 }
117 }
118
119 /* Load from ADDR a value indicated by TYPE. Which will be one of
120 the FFI_IA64_TYPE_HFA_* values. */
121
122 static void
123 hfa_type_load (fpreg *fpaddr, int type, void *addr)
124 {
125 switch (type)
126 {
127 case FFI_IA64_TYPE_HFA_FLOAT:
128 stf_spill (fpaddr, *(float *) addr);
129 return;
130 case FFI_IA64_TYPE_HFA_DOUBLE:
131 stf_spill (fpaddr, *(double *) addr);
132 return;
133 case FFI_IA64_TYPE_HFA_LDOUBLE:
134 stf_spill (fpaddr, *(__float80 *) addr);
135 return;
136 default:
137 abort ();
138 }
139 }
140
141 /* Load VALUE into ADDR as indicated by TYPE. Which will be one of
142 the FFI_IA64_TYPE_HFA_* values. */
143
144 static void
145 hfa_type_store (int type, void *addr, fpreg *fpaddr)
146 {
147 switch (type)
148 {
149 case FFI_IA64_TYPE_HFA_FLOAT:
150 {
151 float result;
152 ldf_fill (result, fpaddr);
153 *(float *) addr = result;
154 break;
155 }
156 case FFI_IA64_TYPE_HFA_DOUBLE:
157 {
158 double result;
159 ldf_fill (result, fpaddr);
160 *(double *) addr = result;
161 break;
162 }
163 case FFI_IA64_TYPE_HFA_LDOUBLE:
164 {
165 __float80 result;
166 ldf_fill (result, fpaddr);
167 *(__float80 *) addr = result;
168 break;
169 }
170 default:
171 abort ();
172 }
173 }
174
175 /* Is TYPE a struct containing floats, doubles, or extended doubles,
176 all of the same fp type? If so, return the element type. Return
177 FFI_TYPE_VOID if not. */
178
179 static int
180 hfa_element_type (ffi_type *type, int nested)
181 {
182 int element = FFI_TYPE_VOID;
183
184 switch (type->type)
185 {
186 case FFI_TYPE_FLOAT:
187 /* We want to return VOID for raw floating-point types, but the
188 synthetic HFA type if we're nested within an aggregate. */
189 if (nested)
190 element = FFI_IA64_TYPE_HFA_FLOAT;
191 break;
192
193 case FFI_TYPE_DOUBLE:
194 /* Similarly. */
195 if (nested)
196 element = FFI_IA64_TYPE_HFA_DOUBLE;
197 break;
198
199 case FFI_TYPE_LONGDOUBLE:
200 /* Similarly, except that that HFA is true for double extended,
201 but not quad precision. Both have sizeof == 16, so tell the
202 difference based on the precision. */
203 if (LDBL_MANT_DIG == 64 && nested)
204 element = FFI_IA64_TYPE_HFA_LDOUBLE;
205 break;
206
207 case FFI_TYPE_STRUCT:
208 {
209 ffi_type **ptr = &type->elements[0];
210
211 for (ptr = &type->elements[0]; *ptr ; ptr++)
212 {
213 int sub_element = hfa_element_type (*ptr, 1);
214 if (sub_element == FFI_TYPE_VOID)
215 return FFI_TYPE_VOID;
216
217 if (element == FFI_TYPE_VOID)
218 element = sub_element;
219 else if (element != sub_element)
220 return FFI_TYPE_VOID;
221 }
222 }
223 break;
224
225 default:
226 return FFI_TYPE_VOID;
227 }
228
229 return element;
230 }
231
232
233 /* Perform machine dependent cif processing. */
234
235 static ffi_status
236 ffi_prep_cif_machdep_core(ffi_cif *cif)
237 {
238 int flags;
239
240 /* Adjust cif->bytes to include space for the bits of the ia64_args frame
241 that precedes the integer register portion. The estimate that the
242 generic bits did for the argument space required is good enough for the
243 integer component. */
244 cif->bytes += offsetof(struct ia64_args, gp_regs[0]);
245 if (cif->bytes < sizeof(struct ia64_args))
246 cif->bytes = sizeof(struct ia64_args);
247
248 /* Set the return type flag. */
249 flags = cif->rtype->type;
250 switch (cif->rtype->type)
251 {
252 case FFI_TYPE_LONGDOUBLE:
253 /* Leave FFI_TYPE_LONGDOUBLE as meaning double extended precision,
254 and encode quad precision as a two-word integer structure. */
255 if (LDBL_MANT_DIG != 64)
256 flags = FFI_IA64_TYPE_SMALL_STRUCT | (16 << 8);
257 break;
258
259 case FFI_TYPE_STRUCT:
260 {
261 size_t size = cif->rtype->size;
262 int hfa_type = hfa_element_type (cif->rtype, 0);
263
264 if (hfa_type != FFI_TYPE_VOID)
265 {
266 size_t nelts = size / hfa_type_size (hfa_type);
267 if (nelts <= 8)
268 flags = hfa_type | (size << 8);
269 }
270 else
271 {
272 if (size <= 32)
273 flags = FFI_IA64_TYPE_SMALL_STRUCT | (size << 8);
274 }
275 }
276 break;
277
278 default:
279 break;
280 }
281 cif->flags = flags;
282
283 return FFI_OK;
284 }
285
286 ffi_status
287 ffi_prep_cif_machdep(ffi_cif *cif)
288 {
289 cif->nfixedargs = cif->nargs;
290 return ffi_prep_cif_machdep_core(cif);
291 }
292
293 ffi_status
294 ffi_prep_cif_machdep_var(ffi_cif *cif,
295 unsigned int nfixedargs,
296 unsigned int ntotalargs MAYBE_UNUSED)
297 {
298 cif->nfixedargs = nfixedargs;
299 return ffi_prep_cif_machdep_core(cif);
300 }
301
302 extern int ffi_call_unix (struct ia64_args *, PTR64, void (*)(void), UINT64);
303
304 void
305 ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
306 {
307 struct ia64_args *stack;
308 long i, avn, gpcount, fpcount;
309 ffi_type **p_arg;
310
311 FFI_ASSERT (cif->abi == FFI_UNIX);
312
313 /* If we have no spot for a return value, make one. */
314 if (rvalue == NULL && cif->rtype->type != FFI_TYPE_VOID)
315 rvalue = alloca (cif->rtype->size);
316
317 /* Allocate the stack frame. */
318 stack = alloca (cif->bytes);
319
320 gpcount = fpcount = 0;
321 avn = cif->nargs;
322 for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
323 {
324 switch ((*p_arg)->type)
325 {
326 case FFI_TYPE_SINT8:
327 stack->gp_regs[gpcount++] = *(SINT8 *)avalue[i];
328 break;
329 case FFI_TYPE_UINT8:
330 stack->gp_regs[gpcount++] = *(UINT8 *)avalue[i];
331 break;
332 case FFI_TYPE_SINT16:
333 stack->gp_regs[gpcount++] = *(SINT16 *)avalue[i];
334 break;
335 case FFI_TYPE_UINT16:
336 stack->gp_regs[gpcount++] = *(UINT16 *)avalue[i];
337 break;
338 case FFI_TYPE_SINT32:
339 stack->gp_regs[gpcount++] = *(SINT32 *)avalue[i];
340 break;
341 case FFI_TYPE_UINT32:
342 stack->gp_regs[gpcount++] = *(UINT32 *)avalue[i];
343 break;
344 case FFI_TYPE_SINT64:
345 case FFI_TYPE_UINT64:
346 stack->gp_regs[gpcount++] = *(UINT64 *)avalue[i];
347 break;
348
349 case FFI_TYPE_POINTER:
350 stack->gp_regs[gpcount++] = (UINT64)(PTR64) *(void **)avalue[i];
351 break;
352
353 case FFI_TYPE_FLOAT:
354 if (gpcount < 8 && fpcount < 8)
355 stf_spill (&stack->fp_regs[fpcount++], *(float *)avalue[i]);
356 {
357 UINT32 tmp;
358 memcpy (&tmp, avalue[i], sizeof (UINT32));
359 stack->gp_regs[gpcount++] = tmp;
360 }
361 break;
362
363 case FFI_TYPE_DOUBLE:
364 if (gpcount < 8 && fpcount < 8)
365 stf_spill (&stack->fp_regs[fpcount++], *(double *)avalue[i]);
366 memcpy (&stack->gp_regs[gpcount++], avalue[i], sizeof (UINT64));
367 break;
368
369 case FFI_TYPE_LONGDOUBLE:
370 if (gpcount & 1)
371 gpcount++;
372 if (LDBL_MANT_DIG == 64 && gpcount < 8 && fpcount < 8)
373 stf_spill (&stack->fp_regs[fpcount++], *(__float80 *)avalue[i]);
374 memcpy (&stack->gp_regs[gpcount], avalue[i], 16);
375 gpcount += 2;
376 break;
377
378 case FFI_TYPE_STRUCT:
379 {
380 size_t size = (*p_arg)->size;
381 size_t align = (*p_arg)->alignment;
382 int hfa_type = hfa_element_type (*p_arg, 0);
383
384 FFI_ASSERT (align <= 16);
385 if (align == 16 && (gpcount & 1))
386 gpcount++;
387
388 if (hfa_type != FFI_TYPE_VOID)
389 {
390 size_t hfa_size = hfa_type_size (hfa_type);
391 size_t offset = 0;
392 size_t gp_offset = gpcount * 8;
393
394 while (fpcount < 8
395 && offset < size
396 && gp_offset < 8 * 8)
397 {
398 hfa_type_load (&stack->fp_regs[fpcount], hfa_type,
399 avalue[i] + offset);
400 offset += hfa_size;
401 gp_offset += hfa_size;
402 fpcount += 1;
403 }
404 }
405
406 memcpy (&stack->gp_regs[gpcount], avalue[i], size);
407 gpcount += (size + 7) / 8;
408 }
409 break;
410
411 default:
412 abort ();
413 }
414 }
415
416 ffi_call_unix (stack, rvalue, fn, cif->flags);
417 }
418
419 /* Closures represent a pair consisting of a function pointer, and
420 some user data. A closure is invoked by reinterpreting the closure
421 as a function pointer, and branching to it. Thus we can make an
422 interpreted function callable as a C function: We turn the
423 interpreter itself, together with a pointer specifying the
424 interpreted procedure, into a closure.
425
426 For IA64, function pointer are already pairs consisting of a code
427 pointer, and a gp pointer. The latter is needed to access global
428 variables. Here we set up such a pair as the first two words of
429 the closure (in the "trampoline" area), but we replace the gp
430 pointer with a pointer to the closure itself. We also add the real
431 gp pointer to the closure. This allows the function entry code to
432 both retrieve the user data, and to restore the correct gp pointer. */
433
434 extern void ffi_closure_unix ();
435
436 ffi_status
437 ffi_prep_closure_loc (ffi_closure* closure,
438 ffi_cif* cif,
439 void (*fun)(ffi_cif*,void*,void**,void*),
440 void *user_data,
441 void *codeloc)
442 {
443 /* The layout of a function descriptor. A C function pointer really
444 points to one of these. */
445 struct ia64_fd
446 {
447 UINT64 code_pointer;
448 UINT64 gp;
449 };
450
451 struct ffi_ia64_trampoline_struct
452 {
453 UINT64 code_pointer; /* Pointer to ffi_closure_unix. */
454 UINT64 fake_gp; /* Pointer to closure, installed as gp. */
455 UINT64 real_gp; /* Real gp value. */
456 };
457
458 struct ffi_ia64_trampoline_struct *tramp;
459 struct ia64_fd *fd;
460
461 if (cif->abi != FFI_UNIX)
462 return FFI_BAD_ABI;
463
464 tramp = (struct ffi_ia64_trampoline_struct *)closure->tramp;
465 fd = (struct ia64_fd *)(void *)ffi_closure_unix;
466
467 tramp->code_pointer = fd->code_pointer;
468 tramp->real_gp = fd->gp;
469 tramp->fake_gp = (UINT64)(PTR64)codeloc;
470 closure->cif = cif;
471 closure->user_data = user_data;
472 closure->fun = fun;
473
474 return FFI_OK;
475 }
476
477
478 UINT64
479 ffi_closure_unix_inner (ffi_closure *closure, struct ia64_args *stack,
480 void *rvalue, void *r8)
481 {
482 ffi_cif *cif;
483 void **avalue;
484 ffi_type **p_arg;
485 long i, avn, gpcount, fpcount, nfixedargs;
486
487 cif = closure->cif;
488 avn = cif->nargs;
489 nfixedargs = cif->nfixedargs;
490 avalue = alloca (avn * sizeof (void *));
491
492 /* If the structure return value is passed in memory get that location
493 from r8 so as to pass the value directly back to the caller. */
494 if (cif->flags == FFI_TYPE_STRUCT)
495 rvalue = r8;
496
497 gpcount = fpcount = 0;
498 for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
499 {
500 int named = i < nfixedargs;
501 switch ((*p_arg)->type)
502 {
503 case FFI_TYPE_SINT8:
504 case FFI_TYPE_UINT8:
505 avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], 1);
506 break;
507 case FFI_TYPE_SINT16:
508 case FFI_TYPE_UINT16:
509 avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], 2);
510 break;
511 case FFI_TYPE_SINT32:
512 case FFI_TYPE_UINT32:
513 avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], 4);
514 break;
515 case FFI_TYPE_SINT64:
516 case FFI_TYPE_UINT64:
517 avalue[i] = &stack->gp_regs[gpcount++];
518 break;
519 case FFI_TYPE_POINTER:
520 avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], sizeof(void*));
521 break;
522
523 case FFI_TYPE_FLOAT:
524 if (named && gpcount < 8 && fpcount < 8)
525 {
526 fpreg *addr = &stack->fp_regs[fpcount++];
527 float result;
528 avalue[i] = addr;
529 ldf_fill (result, addr);
530 *(float *)addr = result;
531 }
532 else
533 avalue[i] = endian_adjust(&stack->gp_regs[gpcount], 4);
534 gpcount++;
535 break;
536
537 case FFI_TYPE_DOUBLE:
538 if (named && gpcount < 8 && fpcount < 8)
539 {
540 fpreg *addr = &stack->fp_regs[fpcount++];
541 double result;
542 avalue[i] = addr;
543 ldf_fill (result, addr);
544 *(double *)addr = result;
545 }
546 else
547 avalue[i] = &stack->gp_regs[gpcount];
548 gpcount++;
549 break;
550
551 case FFI_TYPE_LONGDOUBLE:
552 if (gpcount & 1)
553 gpcount++;
554 if (LDBL_MANT_DIG == 64 && named && gpcount < 8 && fpcount < 8)
555 {
556 fpreg *addr = &stack->fp_regs[fpcount++];
557 __float80 result;
558 avalue[i] = addr;
559 ldf_fill (result, addr);
560 *(__float80 *)addr = result;
561 }
562 else
563 avalue[i] = &stack->gp_regs[gpcount];
564 gpcount += 2;
565 break;
566
567 case FFI_TYPE_STRUCT:
568 {
569 size_t size = (*p_arg)->size;
570 size_t align = (*p_arg)->alignment;
571 int hfa_type = hfa_element_type (*p_arg, 0);
572
573 FFI_ASSERT (align <= 16);
574 if (align == 16 && (gpcount & 1))
575 gpcount++;
576
577 if (hfa_type != FFI_TYPE_VOID)
578 {
579 size_t hfa_size = hfa_type_size (hfa_type);
580 size_t offset = 0;
581 size_t gp_offset = gpcount * 8;
582 void *addr = alloca (size);
583
584 avalue[i] = addr;
585
586 while (fpcount < 8
587 && offset < size
588 && gp_offset < 8 * 8)
589 {
590 hfa_type_store (hfa_type, addr + offset,
591 &stack->fp_regs[fpcount]);
592 offset += hfa_size;
593 gp_offset += hfa_size;
594 fpcount += 1;
595 }
596
597 if (offset < size)
598 memcpy (addr + offset, (char *)stack->gp_regs + gp_offset,
599 size - offset);
600 }
601 else
602 avalue[i] = &stack->gp_regs[gpcount];
603
604 gpcount += (size + 7) / 8;
605 }
606 break;
607
608 default:
609 abort ();
610 }
611 }
612
613 closure->fun (cif, rvalue, avalue, closure->user_data);
614
615 return cif->flags;
616 }