1 /* libffi support for Altera Nios II.
3 Copyright (c) 2013 Mentor Graphics.
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 ``Software''), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26 #include <ffi_common.h>
30 /* The Nios II Processor Reference Handbook defines the procedure call
33 Arguments are passed as if a structure containing the types of
34 the arguments were constructed. The first 16 bytes are passed in r4
35 through r7, the remainder on the stack. The first 16 bytes of a function
36 taking variable arguments are passed in r4-r7 in the same way.
38 Return values of types up to 8 bytes are returned in r2 and r3. For
39 return values greater than 8 bytes, the caller must allocate memory for
40 the result and pass the address as if it were argument 0.
42 While this isn't specified explicitly in the ABI documentation, GCC
43 promotes integral arguments smaller than int size to 32 bits.
45 Also of note, the ABI specifies that all structure objects are
46 aligned to 32 bits even if all their fields have a smaller natural
47 alignment. See FFI_AGGREGATE_ALIGNMENT. */
50 /* Declare the assembly language hooks. */
52 extern UINT64
ffi_call_sysv (void (*) (char *, extended_cif
*),
56 extern void ffi_closure_sysv (void);
58 /* Perform machine-dependent cif processing. */
60 ffi_status
ffi_prep_cif_machdep (ffi_cif
*cif
)
62 /* We always want at least 16 bytes in the parameter block since it
63 simplifies the low-level call function. Also round the parameter
64 block size up to a multiple of 4 bytes to preserve
65 32-bit alignment of the stack pointer. */
69 cif
->bytes
= (cif
->bytes
+ 3) & ~3;
75 /* ffi_prep_args is called by the assembly routine to transfer arguments
76 to the stack using the pointers in the ecif array.
77 Note that the stack buffer is big enough to fit all the arguments,
78 but the first 16 bytes will be copied to registers for the actual
81 void ffi_prep_args (char *stack
, extended_cif
*ecif
)
86 /* The implicit return value pointer is passed as if it were a hidden
88 if (ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
89 && ecif
->cif
->rtype
->size
> 8)
91 (*(void **) argp
) = ecif
->rvalue
;
95 for (i
= 0; i
< ecif
->cif
->nargs
; i
++)
97 void *avalue
= ecif
->avalue
[i
];
98 ffi_type
*atype
= ecif
->cif
->arg_types
[i
];
99 size_t size
= atype
->size
;
100 size_t alignment
= atype
->alignment
;
102 /* Align argp as appropriate for the argument type. */
103 if ((alignment
- 1) & (unsigned) argp
)
104 argp
= (char *) ALIGN (argp
, alignment
);
106 /* Copy the argument, promoting integral types smaller than a
107 word to word size. */
108 if (size
< sizeof (int))
114 *(signed int *) argp
= (signed int) *(SINT8
*) avalue
;
118 *(unsigned int *) argp
= (unsigned int) *(UINT8
*) avalue
;
121 case FFI_TYPE_SINT16
:
122 *(signed int *) argp
= (signed int) *(SINT16
*) avalue
;
125 case FFI_TYPE_UINT16
:
126 *(unsigned int *) argp
= (unsigned int) *(UINT16
*) avalue
;
129 case FFI_TYPE_STRUCT
:
130 memcpy (argp
, avalue
, atype
->size
);
137 else if (size
== sizeof (int))
138 *(unsigned int *) argp
= (unsigned int) *(UINT32
*) avalue
;
140 memcpy (argp
, avalue
, size
);
146 /* Call FN using the prepared CIF. RVALUE points to space allocated by
147 the caller for the return value, and AVALUE is an array of argument
150 void ffi_call (ffi_cif
*cif
, void (*fn
) (void), void *rvalue
, void **avalue
)
156 /* If bigret is true, this is the case where a return value of larger
157 than 8 bytes is handled by being passed by reference as an implicit
159 int bigret
= (cif
->rtype
->type
== FFI_TYPE_STRUCT
160 && cif
->rtype
->size
> 8);
163 ecif
.avalue
= avalue
;
165 /* Allocate space for return value if this is the pass-by-reference case
166 and the caller did not provide a buffer. */
167 if (rvalue
== NULL
&& bigret
)
168 ecif
.rvalue
= alloca (cif
->rtype
->size
);
170 ecif
.rvalue
= rvalue
;
172 result
= ffi_call_sysv (ffi_prep_args
, &ecif
, cif
->bytes
, fn
);
174 /* Now result contains the 64 bit contents returned from fn in
175 r2 and r3. Copy the value of the appropriate size to the user-provided
177 if (rvalue
&& !bigret
)
178 switch (cif
->rtype
->size
)
181 *(UINT8
*)rvalue
= (UINT8
) result
;
184 *(UINT16
*)rvalue
= (UINT16
) result
;
187 *(UINT32
*)rvalue
= (UINT32
) result
;
190 *(UINT64
*)rvalue
= (UINT64
) result
;
193 memcpy (rvalue
, (void *)&result
, cif
->rtype
->size
);
198 /* This function is invoked from the closure trampoline to invoke
199 CLOSURE with argument block ARGS. Parse ARGS according to
200 CLOSURE->cfi and invoke CLOSURE->fun. */
203 ffi_closure_helper (unsigned char *args
,
204 ffi_closure
*closure
)
206 ffi_cif
*cif
= closure
->cif
;
207 unsigned char *argp
= args
;
208 void **parsed_args
= alloca (cif
->nargs
* sizeof (void *));
213 /* First figure out what to do about the return type. If this is the
214 big-structure-return case, the first arg is the hidden return buffer
215 allocated by the caller. */
216 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
217 && cif
->rtype
->size
> 8)
219 retptr
= *((void **) argp
);
223 retptr
= (void *) &result
;
225 /* Fill in the array of argument pointers. */
226 for (i
= 0; i
< cif
->nargs
; i
++)
228 size_t size
= cif
->arg_types
[i
]->size
;
229 size_t alignment
= cif
->arg_types
[i
]->alignment
;
231 /* Align argp as appropriate for the argument type. */
232 if ((alignment
- 1) & (unsigned) argp
)
233 argp
= (char *) ALIGN (argp
, alignment
);
235 /* Arguments smaller than an int are promoted to int. */
236 if (size
< sizeof (int))
239 /* Store the pointer. */
240 parsed_args
[i
] = argp
;
244 /* Call the user-supplied function. */
245 (closure
->fun
) (cif
, retptr
, parsed_args
, closure
->user_data
);
250 /* Initialize CLOSURE with a trampoline to call FUN with
251 CIF and USER_DATA. */
253 ffi_prep_closure_loc (ffi_closure
* closure
,
255 void (*fun
) (ffi_cif
*, void*, void**, void*),
259 unsigned int *tramp
= (unsigned int *) &closure
->tramp
[0];
262 if (cif
->abi
!= FFI_SYSV
)
265 /* The trampoline looks like:
266 movhi r8, %hi(ffi_closure_sysv)
267 ori r8, r8, %lo(ffi_closure_sysv)
268 movhi r9, %hi(ffi_closure_helper)
269 ori r0, r9, %lo(ffi_closure_helper)
270 movhi r10, %hi(closure)
271 ori r10, r10, %lo(closure)
273 and then ffi_closure_sysv retrieves the closure pointer out of r10
274 in addition to the arguments passed in the normal way for the call,
275 and invokes ffi_closure_helper. We encode the pointer to
276 ffi_closure_helper in the trampoline because making a PIC call
277 to it in ffi_closure_sysv would be messy (it would have to indirect
280 #define HI(x) ((((unsigned int) (x)) >> 16) & 0xffff)
281 #define LO(x) (((unsigned int) (x)) & 0xffff)
282 tramp
[0] = (0 << 27) | (8 << 22) | (HI (ffi_closure_sysv
) << 6) | 0x34;
283 tramp
[1] = (8 << 27) | (8 << 22) | (LO (ffi_closure_sysv
) << 6) | 0x14;
284 tramp
[2] = (0 << 27) | (9 << 22) | (HI (ffi_closure_helper
) << 6) | 0x34;
285 tramp
[3] = (9 << 27) | (9 << 22) | (LO (ffi_closure_helper
) << 6) | 0x14;
286 tramp
[4] = (0 << 27) | (10 << 22) | (HI (closure
) << 6) | 0x34;
287 tramp
[5] = (10 << 27) | (10 << 22) | (LO (closure
) << 6) | 0x14;
288 tramp
[6] = (8 << 27) | (0x0d << 11) | 0x3a;
293 See Example 9-4 in the Nios II Software Developer's Handbook. */
294 for (i
= 0; i
< 7; i
++)
295 asm volatile ("flushd 0(%0); flushi %0" :: "r"(tramp
+ i
) : "memory");
296 asm volatile ("flushp" ::: "memory");
300 closure
->user_data
= user_data
;