1 | n/a | /* libffi support for Altera Nios II. |
---|
2 | n/a | |
---|
3 | n/a | Copyright (c) 2013 Mentor Graphics. |
---|
4 | n/a | |
---|
5 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
6 | n/a | a copy of this software and associated documentation files (the |
---|
7 | n/a | ``Software''), to deal in the Software without restriction, including |
---|
8 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
9 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
10 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
11 | n/a | the following conditions: |
---|
12 | n/a | |
---|
13 | n/a | The above copyright notice and this permission notice shall be |
---|
14 | n/a | included in all copies or substantial portions of the Software. |
---|
15 | n/a | |
---|
16 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
---|
17 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
18 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
19 | n/a | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
---|
20 | n/a | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
21 | n/a | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
---|
22 | n/a | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | #include <ffi.h> |
---|
26 | n/a | #include <ffi_common.h> |
---|
27 | n/a | |
---|
28 | n/a | #include <stdlib.h> |
---|
29 | n/a | |
---|
30 | n/a | /* The Nios II Processor Reference Handbook defines the procedure call |
---|
31 | n/a | ABI as follows. |
---|
32 | n/a | |
---|
33 | n/a | Arguments are passed as if a structure containing the types of |
---|
34 | n/a | the arguments were constructed. The first 16 bytes are passed in r4 |
---|
35 | n/a | through r7, the remainder on the stack. The first 16 bytes of a function |
---|
36 | n/a | taking variable arguments are passed in r4-r7 in the same way. |
---|
37 | n/a | |
---|
38 | n/a | Return values of types up to 8 bytes are returned in r2 and r3. For |
---|
39 | n/a | return values greater than 8 bytes, the caller must allocate memory for |
---|
40 | n/a | the result and pass the address as if it were argument 0. |
---|
41 | n/a | |
---|
42 | n/a | While this isn't specified explicitly in the ABI documentation, GCC |
---|
43 | n/a | promotes integral arguments smaller than int size to 32 bits. |
---|
44 | n/a | |
---|
45 | n/a | Also of note, the ABI specifies that all structure objects are |
---|
46 | n/a | aligned to 32 bits even if all their fields have a smaller natural |
---|
47 | n/a | alignment. See FFI_AGGREGATE_ALIGNMENT. */ |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | /* Declare the assembly language hooks. */ |
---|
51 | n/a | |
---|
52 | n/a | extern UINT64 ffi_call_sysv (void (*) (char *, extended_cif *), |
---|
53 | n/a | extended_cif *, |
---|
54 | n/a | unsigned, |
---|
55 | n/a | void (*fn) (void)); |
---|
56 | n/a | extern void ffi_closure_sysv (void); |
---|
57 | n/a | |
---|
58 | n/a | /* Perform machine-dependent cif processing. */ |
---|
59 | n/a | |
---|
60 | n/a | ffi_status ffi_prep_cif_machdep (ffi_cif *cif) |
---|
61 | n/a | { |
---|
62 | n/a | /* We always want at least 16 bytes in the parameter block since it |
---|
63 | n/a | simplifies the low-level call function. Also round the parameter |
---|
64 | n/a | block size up to a multiple of 4 bytes to preserve |
---|
65 | n/a | 32-bit alignment of the stack pointer. */ |
---|
66 | n/a | if (cif->bytes < 16) |
---|
67 | n/a | cif->bytes = 16; |
---|
68 | n/a | else |
---|
69 | n/a | cif->bytes = (cif->bytes + 3) & ~3; |
---|
70 | n/a | |
---|
71 | n/a | return FFI_OK; |
---|
72 | n/a | } |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | /* ffi_prep_args is called by the assembly routine to transfer arguments |
---|
76 | n/a | to the stack using the pointers in the ecif array. |
---|
77 | n/a | Note that the stack buffer is big enough to fit all the arguments, |
---|
78 | n/a | but the first 16 bytes will be copied to registers for the actual |
---|
79 | n/a | call. */ |
---|
80 | n/a | |
---|
81 | n/a | void ffi_prep_args (char *stack, extended_cif *ecif) |
---|
82 | n/a | { |
---|
83 | n/a | char *argp = stack; |
---|
84 | n/a | unsigned int i; |
---|
85 | n/a | |
---|
86 | n/a | /* The implicit return value pointer is passed as if it were a hidden |
---|
87 | n/a | first argument. */ |
---|
88 | n/a | if (ecif->cif->rtype->type == FFI_TYPE_STRUCT |
---|
89 | n/a | && ecif->cif->rtype->size > 8) |
---|
90 | n/a | { |
---|
91 | n/a | (*(void **) argp) = ecif->rvalue; |
---|
92 | n/a | argp += 4; |
---|
93 | n/a | } |
---|
94 | n/a | |
---|
95 | n/a | for (i = 0; i < ecif->cif->nargs; i++) |
---|
96 | n/a | { |
---|
97 | n/a | void *avalue = ecif->avalue[i]; |
---|
98 | n/a | ffi_type *atype = ecif->cif->arg_types[i]; |
---|
99 | n/a | size_t size = atype->size; |
---|
100 | n/a | size_t alignment = atype->alignment; |
---|
101 | n/a | |
---|
102 | n/a | /* Align argp as appropriate for the argument type. */ |
---|
103 | n/a | if ((alignment - 1) & (unsigned) argp) |
---|
104 | n/a | argp = (char *) ALIGN (argp, alignment); |
---|
105 | n/a | |
---|
106 | n/a | /* Copy the argument, promoting integral types smaller than a |
---|
107 | n/a | word to word size. */ |
---|
108 | n/a | if (size < sizeof (int)) |
---|
109 | n/a | { |
---|
110 | n/a | size = sizeof (int); |
---|
111 | n/a | switch (atype->type) |
---|
112 | n/a | { |
---|
113 | n/a | case FFI_TYPE_SINT8: |
---|
114 | n/a | *(signed int *) argp = (signed int) *(SINT8 *) avalue; |
---|
115 | n/a | break; |
---|
116 | n/a | |
---|
117 | n/a | case FFI_TYPE_UINT8: |
---|
118 | n/a | *(unsigned int *) argp = (unsigned int) *(UINT8 *) avalue; |
---|
119 | n/a | break; |
---|
120 | n/a | |
---|
121 | n/a | case FFI_TYPE_SINT16: |
---|
122 | n/a | *(signed int *) argp = (signed int) *(SINT16 *) avalue; |
---|
123 | n/a | break; |
---|
124 | n/a | |
---|
125 | n/a | case FFI_TYPE_UINT16: |
---|
126 | n/a | *(unsigned int *) argp = (unsigned int) *(UINT16 *) avalue; |
---|
127 | n/a | break; |
---|
128 | n/a | |
---|
129 | n/a | case FFI_TYPE_STRUCT: |
---|
130 | n/a | memcpy (argp, avalue, atype->size); |
---|
131 | n/a | break; |
---|
132 | n/a | |
---|
133 | n/a | default: |
---|
134 | n/a | FFI_ASSERT(0); |
---|
135 | n/a | } |
---|
136 | n/a | } |
---|
137 | n/a | else if (size == sizeof (int)) |
---|
138 | n/a | *(unsigned int *) argp = (unsigned int) *(UINT32 *) avalue; |
---|
139 | n/a | else |
---|
140 | n/a | memcpy (argp, avalue, size); |
---|
141 | n/a | argp += size; |
---|
142 | n/a | } |
---|
143 | n/a | } |
---|
144 | n/a | |
---|
145 | n/a | |
---|
146 | n/a | /* Call FN using the prepared CIF. RVALUE points to space allocated by |
---|
147 | n/a | the caller for the return value, and AVALUE is an array of argument |
---|
148 | n/a | pointers. */ |
---|
149 | n/a | |
---|
150 | n/a | void ffi_call (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue) |
---|
151 | n/a | { |
---|
152 | n/a | |
---|
153 | n/a | extended_cif ecif; |
---|
154 | n/a | UINT64 result; |
---|
155 | n/a | |
---|
156 | n/a | /* If bigret is true, this is the case where a return value of larger |
---|
157 | n/a | than 8 bytes is handled by being passed by reference as an implicit |
---|
158 | n/a | argument. */ |
---|
159 | n/a | int bigret = (cif->rtype->type == FFI_TYPE_STRUCT |
---|
160 | n/a | && cif->rtype->size > 8); |
---|
161 | n/a | |
---|
162 | n/a | ecif.cif = cif; |
---|
163 | n/a | ecif.avalue = avalue; |
---|
164 | n/a | |
---|
165 | n/a | /* Allocate space for return value if this is the pass-by-reference case |
---|
166 | n/a | and the caller did not provide a buffer. */ |
---|
167 | n/a | if (rvalue == NULL && bigret) |
---|
168 | n/a | ecif.rvalue = alloca (cif->rtype->size); |
---|
169 | n/a | else |
---|
170 | n/a | ecif.rvalue = rvalue; |
---|
171 | n/a | |
---|
172 | n/a | result = ffi_call_sysv (ffi_prep_args, &ecif, cif->bytes, fn); |
---|
173 | n/a | |
---|
174 | n/a | /* Now result contains the 64 bit contents returned from fn in |
---|
175 | n/a | r2 and r3. Copy the value of the appropriate size to the user-provided |
---|
176 | n/a | rvalue buffer. */ |
---|
177 | n/a | if (rvalue && !bigret) |
---|
178 | n/a | switch (cif->rtype->size) |
---|
179 | n/a | { |
---|
180 | n/a | case 1: |
---|
181 | n/a | *(UINT8 *)rvalue = (UINT8) result; |
---|
182 | n/a | break; |
---|
183 | n/a | case 2: |
---|
184 | n/a | *(UINT16 *)rvalue = (UINT16) result; |
---|
185 | n/a | break; |
---|
186 | n/a | case 4: |
---|
187 | n/a | *(UINT32 *)rvalue = (UINT32) result; |
---|
188 | n/a | break; |
---|
189 | n/a | case 8: |
---|
190 | n/a | *(UINT64 *)rvalue = (UINT64) result; |
---|
191 | n/a | break; |
---|
192 | n/a | default: |
---|
193 | n/a | memcpy (rvalue, (void *)&result, cif->rtype->size); |
---|
194 | n/a | break; |
---|
195 | n/a | } |
---|
196 | n/a | } |
---|
197 | n/a | |
---|
198 | n/a | /* This function is invoked from the closure trampoline to invoke |
---|
199 | n/a | CLOSURE with argument block ARGS. Parse ARGS according to |
---|
200 | n/a | CLOSURE->cfi and invoke CLOSURE->fun. */ |
---|
201 | n/a | |
---|
202 | n/a | static UINT64 |
---|
203 | n/a | ffi_closure_helper (unsigned char *args, |
---|
204 | n/a | ffi_closure *closure) |
---|
205 | n/a | { |
---|
206 | n/a | ffi_cif *cif = closure->cif; |
---|
207 | n/a | unsigned char *argp = args; |
---|
208 | n/a | void **parsed_args = alloca (cif->nargs * sizeof (void *)); |
---|
209 | n/a | UINT64 result; |
---|
210 | n/a | void *retptr; |
---|
211 | n/a | unsigned int i; |
---|
212 | n/a | |
---|
213 | n/a | /* First figure out what to do about the return type. If this is the |
---|
214 | n/a | big-structure-return case, the first arg is the hidden return buffer |
---|
215 | n/a | allocated by the caller. */ |
---|
216 | n/a | if (cif->rtype->type == FFI_TYPE_STRUCT |
---|
217 | n/a | && cif->rtype->size > 8) |
---|
218 | n/a | { |
---|
219 | n/a | retptr = *((void **) argp); |
---|
220 | n/a | argp += 4; |
---|
221 | n/a | } |
---|
222 | n/a | else |
---|
223 | n/a | retptr = (void *) &result; |
---|
224 | n/a | |
---|
225 | n/a | /* Fill in the array of argument pointers. */ |
---|
226 | n/a | for (i = 0; i < cif->nargs; i++) |
---|
227 | n/a | { |
---|
228 | n/a | size_t size = cif->arg_types[i]->size; |
---|
229 | n/a | size_t alignment = cif->arg_types[i]->alignment; |
---|
230 | n/a | |
---|
231 | n/a | /* Align argp as appropriate for the argument type. */ |
---|
232 | n/a | if ((alignment - 1) & (unsigned) argp) |
---|
233 | n/a | argp = (char *) ALIGN (argp, alignment); |
---|
234 | n/a | |
---|
235 | n/a | /* Arguments smaller than an int are promoted to int. */ |
---|
236 | n/a | if (size < sizeof (int)) |
---|
237 | n/a | size = sizeof (int); |
---|
238 | n/a | |
---|
239 | n/a | /* Store the pointer. */ |
---|
240 | n/a | parsed_args[i] = argp; |
---|
241 | n/a | argp += size; |
---|
242 | n/a | } |
---|
243 | n/a | |
---|
244 | n/a | /* Call the user-supplied function. */ |
---|
245 | n/a | (closure->fun) (cif, retptr, parsed_args, closure->user_data); |
---|
246 | n/a | return result; |
---|
247 | n/a | } |
---|
248 | n/a | |
---|
249 | n/a | |
---|
250 | n/a | /* Initialize CLOSURE with a trampoline to call FUN with |
---|
251 | n/a | CIF and USER_DATA. */ |
---|
252 | n/a | ffi_status |
---|
253 | n/a | ffi_prep_closure_loc (ffi_closure* closure, |
---|
254 | n/a | ffi_cif* cif, |
---|
255 | n/a | void (*fun) (ffi_cif*, void*, void**, void*), |
---|
256 | n/a | void *user_data, |
---|
257 | n/a | void *codeloc) |
---|
258 | n/a | { |
---|
259 | n/a | unsigned int *tramp = (unsigned int *) &closure->tramp[0]; |
---|
260 | n/a | int i; |
---|
261 | n/a | |
---|
262 | n/a | if (cif->abi != FFI_SYSV) |
---|
263 | n/a | return FFI_BAD_ABI; |
---|
264 | n/a | |
---|
265 | n/a | /* The trampoline looks like: |
---|
266 | n/a | movhi r8, %hi(ffi_closure_sysv) |
---|
267 | n/a | ori r8, r8, %lo(ffi_closure_sysv) |
---|
268 | n/a | movhi r9, %hi(ffi_closure_helper) |
---|
269 | n/a | ori r0, r9, %lo(ffi_closure_helper) |
---|
270 | n/a | movhi r10, %hi(closure) |
---|
271 | n/a | ori r10, r10, %lo(closure) |
---|
272 | n/a | jmp r8 |
---|
273 | n/a | and then ffi_closure_sysv retrieves the closure pointer out of r10 |
---|
274 | n/a | in addition to the arguments passed in the normal way for the call, |
---|
275 | n/a | and invokes ffi_closure_helper. We encode the pointer to |
---|
276 | n/a | ffi_closure_helper in the trampoline because making a PIC call |
---|
277 | n/a | to it in ffi_closure_sysv would be messy (it would have to indirect |
---|
278 | n/a | through the GOT). */ |
---|
279 | n/a | |
---|
280 | n/a | #define HI(x) ((((unsigned int) (x)) >> 16) & 0xffff) |
---|
281 | n/a | #define LO(x) (((unsigned int) (x)) & 0xffff) |
---|
282 | n/a | tramp[0] = (0 << 27) | (8 << 22) | (HI (ffi_closure_sysv) << 6) | 0x34; |
---|
283 | n/a | tramp[1] = (8 << 27) | (8 << 22) | (LO (ffi_closure_sysv) << 6) | 0x14; |
---|
284 | n/a | tramp[2] = (0 << 27) | (9 << 22) | (HI (ffi_closure_helper) << 6) | 0x34; |
---|
285 | n/a | tramp[3] = (9 << 27) | (9 << 22) | (LO (ffi_closure_helper) << 6) | 0x14; |
---|
286 | n/a | tramp[4] = (0 << 27) | (10 << 22) | (HI (closure) << 6) | 0x34; |
---|
287 | n/a | tramp[5] = (10 << 27) | (10 << 22) | (LO (closure) << 6) | 0x14; |
---|
288 | n/a | tramp[6] = (8 << 27) | (0x0d << 11) | 0x3a; |
---|
289 | n/a | #undef HI |
---|
290 | n/a | #undef LO |
---|
291 | n/a | |
---|
292 | n/a | /* Flush the caches. |
---|
293 | n/a | See Example 9-4 in the Nios II Software Developer's Handbook. */ |
---|
294 | n/a | for (i = 0; i < 7; i++) |
---|
295 | n/a | asm volatile ("flushd 0(%0); flushi %0" :: "r"(tramp + i) : "memory"); |
---|
296 | n/a | asm volatile ("flushp" ::: "memory"); |
---|
297 | n/a | |
---|
298 | n/a | closure->cif = cif; |
---|
299 | n/a | closure->fun = fun; |
---|
300 | n/a | closure->user_data = user_data; |
---|
301 | n/a | |
---|
302 | n/a | return FFI_OK; |
---|
303 | n/a | } |
---|
304 | n/a | |
---|