1 | n/a | /* ----------------------------------------------------------------------- |
---|
2 | n/a | ffi.c - (c) 2011 Anthony Green |
---|
3 | n/a | (c) 2008 Red Hat, Inc. |
---|
4 | n/a | (c) 2006 Free Software Foundation, Inc. |
---|
5 | n/a | (c) 2003-2004 Randolph Chung <tausq@debian.org> |
---|
6 | n/a | |
---|
7 | n/a | HPPA Foreign Function Interface |
---|
8 | n/a | HP-UX PA ABI support |
---|
9 | n/a | |
---|
10 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
11 | n/a | a copy of this software and associated documentation files (the |
---|
12 | n/a | ``Software''), to deal in the Software without restriction, including |
---|
13 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
14 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
15 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
16 | n/a | the following conditions: |
---|
17 | n/a | |
---|
18 | n/a | The above copyright notice and this permission notice shall be included |
---|
19 | n/a | in all copies or substantial portions of the Software. |
---|
20 | n/a | |
---|
21 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
---|
22 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
23 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
24 | n/a | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
25 | n/a | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
26 | n/a | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
27 | n/a | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
28 | n/a | DEALINGS IN THE SOFTWARE. |
---|
29 | n/a | ----------------------------------------------------------------------- */ |
---|
30 | n/a | |
---|
31 | n/a | #include <ffi.h> |
---|
32 | n/a | #include <ffi_common.h> |
---|
33 | n/a | |
---|
34 | n/a | #include <stdlib.h> |
---|
35 | n/a | #include <stdio.h> |
---|
36 | n/a | |
---|
37 | n/a | #define ROUND_UP(v, a) (((size_t)(v) + (a) - 1) & ~((a) - 1)) |
---|
38 | n/a | |
---|
39 | n/a | #define MIN_STACK_SIZE 64 |
---|
40 | n/a | #define FIRST_ARG_SLOT 9 |
---|
41 | n/a | #define DEBUG_LEVEL 0 |
---|
42 | n/a | |
---|
43 | n/a | #define fldw(addr, fpreg) \ |
---|
44 | n/a | __asm__ volatile ("fldw 0(%0), %%" #fpreg "L" : : "r"(addr) : #fpreg) |
---|
45 | n/a | #define fstw(fpreg, addr) \ |
---|
46 | n/a | __asm__ volatile ("fstw %%" #fpreg "L, 0(%0)" : : "r"(addr)) |
---|
47 | n/a | #define fldd(addr, fpreg) \ |
---|
48 | n/a | __asm__ volatile ("fldd 0(%0), %%" #fpreg : : "r"(addr) : #fpreg) |
---|
49 | n/a | #define fstd(fpreg, addr) \ |
---|
50 | n/a | __asm__ volatile ("fstd %%" #fpreg "L, 0(%0)" : : "r"(addr)) |
---|
51 | n/a | |
---|
52 | n/a | #define debug(lvl, x...) do { if (lvl <= DEBUG_LEVEL) { printf(x); } } while (0) |
---|
53 | n/a | |
---|
54 | n/a | static inline int ffi_struct_type(ffi_type *t) |
---|
55 | n/a | { |
---|
56 | n/a | size_t sz = t->size; |
---|
57 | n/a | |
---|
58 | n/a | /* Small structure results are passed in registers, |
---|
59 | n/a | larger ones are passed by pointer. Note that |
---|
60 | n/a | small structures of size 2, 4 and 8 differ from |
---|
61 | n/a | the corresponding integer types in that they have |
---|
62 | n/a | different alignment requirements. */ |
---|
63 | n/a | |
---|
64 | n/a | if (sz <= 1) |
---|
65 | n/a | return FFI_TYPE_UINT8; |
---|
66 | n/a | else if (sz == 2) |
---|
67 | n/a | return FFI_TYPE_SMALL_STRUCT2; |
---|
68 | n/a | else if (sz == 3) |
---|
69 | n/a | return FFI_TYPE_SMALL_STRUCT3; |
---|
70 | n/a | else if (sz == 4) |
---|
71 | n/a | return FFI_TYPE_SMALL_STRUCT4; |
---|
72 | n/a | else if (sz == 5) |
---|
73 | n/a | return FFI_TYPE_SMALL_STRUCT5; |
---|
74 | n/a | else if (sz == 6) |
---|
75 | n/a | return FFI_TYPE_SMALL_STRUCT6; |
---|
76 | n/a | else if (sz == 7) |
---|
77 | n/a | return FFI_TYPE_SMALL_STRUCT7; |
---|
78 | n/a | else if (sz <= 8) |
---|
79 | n/a | return FFI_TYPE_SMALL_STRUCT8; |
---|
80 | n/a | else |
---|
81 | n/a | return FFI_TYPE_STRUCT; /* else, we pass it by pointer. */ |
---|
82 | n/a | } |
---|
83 | n/a | |
---|
84 | n/a | /* PA has a downward growing stack, which looks like this: |
---|
85 | n/a | |
---|
86 | n/a | Offset |
---|
87 | n/a | [ Variable args ] |
---|
88 | n/a | SP = (4*(n+9)) arg word N |
---|
89 | n/a | ... |
---|
90 | n/a | SP-52 arg word 4 |
---|
91 | n/a | [ Fixed args ] |
---|
92 | n/a | SP-48 arg word 3 |
---|
93 | n/a | SP-44 arg word 2 |
---|
94 | n/a | SP-40 arg word 1 |
---|
95 | n/a | SP-36 arg word 0 |
---|
96 | n/a | [ Frame marker ] |
---|
97 | n/a | ... |
---|
98 | n/a | SP-20 RP |
---|
99 | n/a | SP-4 previous SP |
---|
100 | n/a | |
---|
101 | n/a | The first four argument words on the stack are reserved for use by |
---|
102 | n/a | the callee. Instead, the general and floating registers replace |
---|
103 | n/a | the first four argument slots. Non FP arguments are passed solely |
---|
104 | n/a | in the general registers. FP arguments are passed in both general |
---|
105 | n/a | and floating registers when using libffi. |
---|
106 | n/a | |
---|
107 | n/a | Non-FP 32-bit args are passed in gr26, gr25, gr24 and gr23. |
---|
108 | n/a | Non-FP 64-bit args are passed in register pairs, starting |
---|
109 | n/a | on an odd numbered register (i.e. r25+r26 and r23+r24). |
---|
110 | n/a | FP 32-bit arguments are passed in fr4L, fr5L, fr6L and fr7L. |
---|
111 | n/a | FP 64-bit arguments are passed in fr5 and fr7. |
---|
112 | n/a | |
---|
113 | n/a | The registers are allocated in the same manner as stack slots. |
---|
114 | n/a | This allows the callee to save its arguments on the stack if |
---|
115 | n/a | necessary: |
---|
116 | n/a | |
---|
117 | n/a | arg word 3 -> gr23 or fr7L |
---|
118 | n/a | arg word 2 -> gr24 or fr6L or fr7R |
---|
119 | n/a | arg word 1 -> gr25 or fr5L |
---|
120 | n/a | arg word 0 -> gr26 or fr4L or fr5R |
---|
121 | n/a | |
---|
122 | n/a | Note that fr4R and fr6R are never used for arguments (i.e., |
---|
123 | n/a | doubles are not passed in fr4 or fr6). |
---|
124 | n/a | |
---|
125 | n/a | The rest of the arguments are passed on the stack starting at SP-52, |
---|
126 | n/a | but 64-bit arguments need to be aligned to an 8-byte boundary |
---|
127 | n/a | |
---|
128 | n/a | This means we can have holes either in the register allocation, |
---|
129 | n/a | or in the stack. */ |
---|
130 | n/a | |
---|
131 | n/a | /* ffi_prep_args is called by the assembly routine once stack space |
---|
132 | n/a | has been allocated for the function's arguments |
---|
133 | n/a | |
---|
134 | n/a | The following code will put everything into the stack frame |
---|
135 | n/a | (which was allocated by the asm routine), and on return |
---|
136 | n/a | the asm routine will load the arguments that should be |
---|
137 | n/a | passed by register into the appropriate registers |
---|
138 | n/a | |
---|
139 | n/a | NOTE: We load floating point args in this function... that means we |
---|
140 | n/a | assume gcc will not mess with fp regs in here. */ |
---|
141 | n/a | |
---|
142 | n/a | void ffi_prep_args_pa32(UINT32 *stack, extended_cif *ecif, unsigned bytes) |
---|
143 | n/a | { |
---|
144 | n/a | register unsigned int i; |
---|
145 | n/a | register ffi_type **p_arg; |
---|
146 | n/a | register void **p_argv; |
---|
147 | n/a | unsigned int slot = FIRST_ARG_SLOT; |
---|
148 | n/a | char *dest_cpy; |
---|
149 | n/a | size_t len; |
---|
150 | n/a | |
---|
151 | n/a | debug(1, "%s: stack = %p, ecif = %p, bytes = %u\n", __FUNCTION__, stack, |
---|
152 | n/a | ecif, bytes); |
---|
153 | n/a | |
---|
154 | n/a | p_arg = ecif->cif->arg_types; |
---|
155 | n/a | p_argv = ecif->avalue; |
---|
156 | n/a | |
---|
157 | n/a | for (i = 0; i < ecif->cif->nargs; i++) |
---|
158 | n/a | { |
---|
159 | n/a | int type = (*p_arg)->type; |
---|
160 | n/a | |
---|
161 | n/a | switch (type) |
---|
162 | n/a | { |
---|
163 | n/a | case FFI_TYPE_SINT8: |
---|
164 | n/a | *(SINT32 *)(stack - slot) = *(SINT8 *)(*p_argv); |
---|
165 | n/a | break; |
---|
166 | n/a | |
---|
167 | n/a | case FFI_TYPE_UINT8: |
---|
168 | n/a | *(UINT32 *)(stack - slot) = *(UINT8 *)(*p_argv); |
---|
169 | n/a | break; |
---|
170 | n/a | |
---|
171 | n/a | case FFI_TYPE_SINT16: |
---|
172 | n/a | *(SINT32 *)(stack - slot) = *(SINT16 *)(*p_argv); |
---|
173 | n/a | break; |
---|
174 | n/a | |
---|
175 | n/a | case FFI_TYPE_UINT16: |
---|
176 | n/a | *(UINT32 *)(stack - slot) = *(UINT16 *)(*p_argv); |
---|
177 | n/a | break; |
---|
178 | n/a | |
---|
179 | n/a | case FFI_TYPE_UINT32: |
---|
180 | n/a | case FFI_TYPE_SINT32: |
---|
181 | n/a | case FFI_TYPE_POINTER: |
---|
182 | n/a | debug(3, "Storing UINT32 %u in slot %u\n", *(UINT32 *)(*p_argv), |
---|
183 | n/a | slot); |
---|
184 | n/a | *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv); |
---|
185 | n/a | break; |
---|
186 | n/a | |
---|
187 | n/a | case FFI_TYPE_UINT64: |
---|
188 | n/a | case FFI_TYPE_SINT64: |
---|
189 | n/a | /* Align slot for 64-bit type. */ |
---|
190 | n/a | slot += (slot & 1) ? 1 : 2; |
---|
191 | n/a | *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv); |
---|
192 | n/a | break; |
---|
193 | n/a | |
---|
194 | n/a | case FFI_TYPE_FLOAT: |
---|
195 | n/a | /* First 4 args go in fr4L - fr7L. */ |
---|
196 | n/a | debug(3, "Storing UINT32(float) in slot %u\n", slot); |
---|
197 | n/a | *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv); |
---|
198 | n/a | switch (slot - FIRST_ARG_SLOT) |
---|
199 | n/a | { |
---|
200 | n/a | /* First 4 args go in fr4L - fr7L. */ |
---|
201 | n/a | case 0: fldw(stack - slot, fr4); break; |
---|
202 | n/a | case 1: fldw(stack - slot, fr5); break; |
---|
203 | n/a | case 2: fldw(stack - slot, fr6); break; |
---|
204 | n/a | case 3: fldw(stack - slot, fr7); break; |
---|
205 | n/a | } |
---|
206 | n/a | break; |
---|
207 | n/a | |
---|
208 | n/a | case FFI_TYPE_DOUBLE: |
---|
209 | n/a | /* Align slot for 64-bit type. */ |
---|
210 | n/a | slot += (slot & 1) ? 1 : 2; |
---|
211 | n/a | debug(3, "Storing UINT64(double) at slot %u\n", slot); |
---|
212 | n/a | *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv); |
---|
213 | n/a | switch (slot - FIRST_ARG_SLOT) |
---|
214 | n/a | { |
---|
215 | n/a | /* First 2 args go in fr5, fr7. */ |
---|
216 | n/a | case 1: fldd(stack - slot, fr5); break; |
---|
217 | n/a | case 3: fldd(stack - slot, fr7); break; |
---|
218 | n/a | } |
---|
219 | n/a | break; |
---|
220 | n/a | |
---|
221 | n/a | #ifdef PA_HPUX |
---|
222 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
223 | n/a | /* Long doubles are passed in the same manner as structures |
---|
224 | n/a | larger than 8 bytes. */ |
---|
225 | n/a | *(UINT32 *)(stack - slot) = (UINT32)(*p_argv); |
---|
226 | n/a | break; |
---|
227 | n/a | #endif |
---|
228 | n/a | |
---|
229 | n/a | case FFI_TYPE_STRUCT: |
---|
230 | n/a | |
---|
231 | n/a | /* Structs smaller or equal than 4 bytes are passed in one |
---|
232 | n/a | register. Structs smaller or equal 8 bytes are passed in two |
---|
233 | n/a | registers. Larger structures are passed by pointer. */ |
---|
234 | n/a | |
---|
235 | n/a | len = (*p_arg)->size; |
---|
236 | n/a | if (len <= 4) |
---|
237 | n/a | { |
---|
238 | n/a | dest_cpy = (char *)(stack - slot) + 4 - len; |
---|
239 | n/a | memcpy(dest_cpy, (char *)*p_argv, len); |
---|
240 | n/a | } |
---|
241 | n/a | else if (len <= 8) |
---|
242 | n/a | { |
---|
243 | n/a | slot += (slot & 1) ? 1 : 2; |
---|
244 | n/a | dest_cpy = (char *)(stack - slot) + 8 - len; |
---|
245 | n/a | memcpy(dest_cpy, (char *)*p_argv, len); |
---|
246 | n/a | } |
---|
247 | n/a | else |
---|
248 | n/a | *(UINT32 *)(stack - slot) = (UINT32)(*p_argv); |
---|
249 | n/a | break; |
---|
250 | n/a | |
---|
251 | n/a | default: |
---|
252 | n/a | FFI_ASSERT(0); |
---|
253 | n/a | } |
---|
254 | n/a | |
---|
255 | n/a | slot++; |
---|
256 | n/a | p_arg++; |
---|
257 | n/a | p_argv++; |
---|
258 | n/a | } |
---|
259 | n/a | |
---|
260 | n/a | /* Make sure we didn't mess up and scribble on the stack. */ |
---|
261 | n/a | { |
---|
262 | n/a | unsigned int n; |
---|
263 | n/a | |
---|
264 | n/a | debug(5, "Stack setup:\n"); |
---|
265 | n/a | for (n = 0; n < (bytes + 3) / 4; n++) |
---|
266 | n/a | { |
---|
267 | n/a | if ((n%4) == 0) { debug(5, "\n%08x: ", (unsigned int)(stack - n)); } |
---|
268 | n/a | debug(5, "%08x ", *(stack - n)); |
---|
269 | n/a | } |
---|
270 | n/a | debug(5, "\n"); |
---|
271 | n/a | } |
---|
272 | n/a | |
---|
273 | n/a | FFI_ASSERT(slot * 4 <= bytes); |
---|
274 | n/a | |
---|
275 | n/a | return; |
---|
276 | n/a | } |
---|
277 | n/a | |
---|
278 | n/a | static void ffi_size_stack_pa32(ffi_cif *cif) |
---|
279 | n/a | { |
---|
280 | n/a | ffi_type **ptr; |
---|
281 | n/a | int i; |
---|
282 | n/a | int z = 0; /* # stack slots */ |
---|
283 | n/a | |
---|
284 | n/a | for (ptr = cif->arg_types, i = 0; i < cif->nargs; ptr++, i++) |
---|
285 | n/a | { |
---|
286 | n/a | int type = (*ptr)->type; |
---|
287 | n/a | |
---|
288 | n/a | switch (type) |
---|
289 | n/a | { |
---|
290 | n/a | case FFI_TYPE_DOUBLE: |
---|
291 | n/a | case FFI_TYPE_UINT64: |
---|
292 | n/a | case FFI_TYPE_SINT64: |
---|
293 | n/a | z += 2 + (z & 1); /* must start on even regs, so we may waste one */ |
---|
294 | n/a | break; |
---|
295 | n/a | |
---|
296 | n/a | #ifdef PA_HPUX |
---|
297 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
298 | n/a | #endif |
---|
299 | n/a | case FFI_TYPE_STRUCT: |
---|
300 | n/a | z += 1; /* pass by ptr, callee will copy */ |
---|
301 | n/a | break; |
---|
302 | n/a | |
---|
303 | n/a | default: /* <= 32-bit values */ |
---|
304 | n/a | z++; |
---|
305 | n/a | } |
---|
306 | n/a | } |
---|
307 | n/a | |
---|
308 | n/a | /* We can fit up to 6 args in the default 64-byte stack frame, |
---|
309 | n/a | if we need more, we need more stack. */ |
---|
310 | n/a | if (z <= 6) |
---|
311 | n/a | cif->bytes = MIN_STACK_SIZE; /* min stack size */ |
---|
312 | n/a | else |
---|
313 | n/a | cif->bytes = 64 + ROUND_UP((z - 6) * sizeof(UINT32), MIN_STACK_SIZE); |
---|
314 | n/a | |
---|
315 | n/a | debug(3, "Calculated stack size is %u bytes\n", cif->bytes); |
---|
316 | n/a | } |
---|
317 | n/a | |
---|
318 | n/a | /* Perform machine dependent cif processing. */ |
---|
319 | n/a | ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
---|
320 | n/a | { |
---|
321 | n/a | /* Set the return type flag */ |
---|
322 | n/a | switch (cif->rtype->type) |
---|
323 | n/a | { |
---|
324 | n/a | case FFI_TYPE_VOID: |
---|
325 | n/a | case FFI_TYPE_FLOAT: |
---|
326 | n/a | case FFI_TYPE_DOUBLE: |
---|
327 | n/a | cif->flags = (unsigned) cif->rtype->type; |
---|
328 | n/a | break; |
---|
329 | n/a | |
---|
330 | n/a | #ifdef PA_HPUX |
---|
331 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
332 | n/a | /* Long doubles are treated like a structure. */ |
---|
333 | n/a | cif->flags = FFI_TYPE_STRUCT; |
---|
334 | n/a | break; |
---|
335 | n/a | #endif |
---|
336 | n/a | |
---|
337 | n/a | case FFI_TYPE_STRUCT: |
---|
338 | n/a | /* For the return type we have to check the size of the structures. |
---|
339 | n/a | If the size is smaller or equal 4 bytes, the result is given back |
---|
340 | n/a | in one register. If the size is smaller or equal 8 bytes than we |
---|
341 | n/a | return the result in two registers. But if the size is bigger than |
---|
342 | n/a | 8 bytes, we work with pointers. */ |
---|
343 | n/a | cif->flags = ffi_struct_type(cif->rtype); |
---|
344 | n/a | break; |
---|
345 | n/a | |
---|
346 | n/a | case FFI_TYPE_UINT64: |
---|
347 | n/a | case FFI_TYPE_SINT64: |
---|
348 | n/a | cif->flags = FFI_TYPE_UINT64; |
---|
349 | n/a | break; |
---|
350 | n/a | |
---|
351 | n/a | default: |
---|
352 | n/a | cif->flags = FFI_TYPE_INT; |
---|
353 | n/a | break; |
---|
354 | n/a | } |
---|
355 | n/a | |
---|
356 | n/a | /* Lucky us, because of the unique PA ABI we get to do our |
---|
357 | n/a | own stack sizing. */ |
---|
358 | n/a | switch (cif->abi) |
---|
359 | n/a | { |
---|
360 | n/a | case FFI_PA32: |
---|
361 | n/a | ffi_size_stack_pa32(cif); |
---|
362 | n/a | break; |
---|
363 | n/a | |
---|
364 | n/a | default: |
---|
365 | n/a | FFI_ASSERT(0); |
---|
366 | n/a | break; |
---|
367 | n/a | } |
---|
368 | n/a | |
---|
369 | n/a | return FFI_OK; |
---|
370 | n/a | } |
---|
371 | n/a | |
---|
372 | n/a | extern void ffi_call_pa32(void (*)(UINT32 *, extended_cif *, unsigned), |
---|
373 | n/a | extended_cif *, unsigned, unsigned, unsigned *, |
---|
374 | n/a | void (*fn)(void)); |
---|
375 | n/a | |
---|
376 | n/a | void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) |
---|
377 | n/a | { |
---|
378 | n/a | extended_cif ecif; |
---|
379 | n/a | |
---|
380 | n/a | ecif.cif = cif; |
---|
381 | n/a | ecif.avalue = avalue; |
---|
382 | n/a | |
---|
383 | n/a | /* If the return value is a struct and we don't have a return |
---|
384 | n/a | value address then we need to make one. */ |
---|
385 | n/a | |
---|
386 | n/a | if (rvalue == NULL |
---|
387 | n/a | #ifdef PA_HPUX |
---|
388 | n/a | && (cif->rtype->type == FFI_TYPE_STRUCT |
---|
389 | n/a | || cif->rtype->type == FFI_TYPE_LONGDOUBLE)) |
---|
390 | n/a | #else |
---|
391 | n/a | && cif->rtype->type == FFI_TYPE_STRUCT) |
---|
392 | n/a | #endif |
---|
393 | n/a | { |
---|
394 | n/a | ecif.rvalue = alloca(cif->rtype->size); |
---|
395 | n/a | } |
---|
396 | n/a | else |
---|
397 | n/a | ecif.rvalue = rvalue; |
---|
398 | n/a | |
---|
399 | n/a | |
---|
400 | n/a | switch (cif->abi) |
---|
401 | n/a | { |
---|
402 | n/a | case FFI_PA32: |
---|
403 | n/a | debug(3, "Calling ffi_call_pa32: ecif=%p, bytes=%u, flags=%u, rvalue=%p, fn=%p\n", &ecif, cif->bytes, cif->flags, ecif.rvalue, (void *)fn); |
---|
404 | n/a | ffi_call_pa32(ffi_prep_args_pa32, &ecif, cif->bytes, |
---|
405 | n/a | cif->flags, ecif.rvalue, fn); |
---|
406 | n/a | break; |
---|
407 | n/a | |
---|
408 | n/a | default: |
---|
409 | n/a | FFI_ASSERT(0); |
---|
410 | n/a | break; |
---|
411 | n/a | } |
---|
412 | n/a | } |
---|
413 | n/a | |
---|
414 | n/a | #if FFI_CLOSURES |
---|
415 | n/a | /* This is more-or-less an inverse of ffi_call -- we have arguments on |
---|
416 | n/a | the stack, and we need to fill them into a cif structure and invoke |
---|
417 | n/a | the user function. This really ought to be in asm to make sure |
---|
418 | n/a | the compiler doesn't do things we don't expect. */ |
---|
419 | n/a | ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack) |
---|
420 | n/a | { |
---|
421 | n/a | ffi_cif *cif; |
---|
422 | n/a | void **avalue; |
---|
423 | n/a | void *rvalue; |
---|
424 | n/a | UINT32 ret[2]; /* function can return up to 64-bits in registers */ |
---|
425 | n/a | ffi_type **p_arg; |
---|
426 | n/a | char *tmp; |
---|
427 | n/a | int i, avn; |
---|
428 | n/a | unsigned int slot = FIRST_ARG_SLOT; |
---|
429 | n/a | register UINT32 r28 asm("r28"); |
---|
430 | n/a | |
---|
431 | n/a | cif = closure->cif; |
---|
432 | n/a | |
---|
433 | n/a | /* If returning via structure, callee will write to our pointer. */ |
---|
434 | n/a | if (cif->flags == FFI_TYPE_STRUCT) |
---|
435 | n/a | rvalue = (void *)r28; |
---|
436 | n/a | else |
---|
437 | n/a | rvalue = &ret[0]; |
---|
438 | n/a | |
---|
439 | n/a | avalue = (void **)alloca(cif->nargs * FFI_SIZEOF_ARG); |
---|
440 | n/a | avn = cif->nargs; |
---|
441 | n/a | p_arg = cif->arg_types; |
---|
442 | n/a | |
---|
443 | n/a | for (i = 0; i < avn; i++) |
---|
444 | n/a | { |
---|
445 | n/a | int type = (*p_arg)->type; |
---|
446 | n/a | |
---|
447 | n/a | switch (type) |
---|
448 | n/a | { |
---|
449 | n/a | case FFI_TYPE_SINT8: |
---|
450 | n/a | case FFI_TYPE_UINT8: |
---|
451 | n/a | case FFI_TYPE_SINT16: |
---|
452 | n/a | case FFI_TYPE_UINT16: |
---|
453 | n/a | case FFI_TYPE_SINT32: |
---|
454 | n/a | case FFI_TYPE_UINT32: |
---|
455 | n/a | case FFI_TYPE_POINTER: |
---|
456 | n/a | avalue[i] = (char *)(stack - slot) + sizeof(UINT32) - (*p_arg)->size; |
---|
457 | n/a | break; |
---|
458 | n/a | |
---|
459 | n/a | case FFI_TYPE_SINT64: |
---|
460 | n/a | case FFI_TYPE_UINT64: |
---|
461 | n/a | slot += (slot & 1) ? 1 : 2; |
---|
462 | n/a | avalue[i] = (void *)(stack - slot); |
---|
463 | n/a | break; |
---|
464 | n/a | |
---|
465 | n/a | case FFI_TYPE_FLOAT: |
---|
466 | n/a | #ifdef PA_LINUX |
---|
467 | n/a | /* The closure call is indirect. In Linux, floating point |
---|
468 | n/a | arguments in indirect calls with a prototype are passed |
---|
469 | n/a | in the floating point registers instead of the general |
---|
470 | n/a | registers. So, we need to replace what was previously |
---|
471 | n/a | stored in the current slot with the value in the |
---|
472 | n/a | corresponding floating point register. */ |
---|
473 | n/a | switch (slot - FIRST_ARG_SLOT) |
---|
474 | n/a | { |
---|
475 | n/a | case 0: fstw(fr4, (void *)(stack - slot)); break; |
---|
476 | n/a | case 1: fstw(fr5, (void *)(stack - slot)); break; |
---|
477 | n/a | case 2: fstw(fr6, (void *)(stack - slot)); break; |
---|
478 | n/a | case 3: fstw(fr7, (void *)(stack - slot)); break; |
---|
479 | n/a | } |
---|
480 | n/a | #endif |
---|
481 | n/a | avalue[i] = (void *)(stack - slot); |
---|
482 | n/a | break; |
---|
483 | n/a | |
---|
484 | n/a | case FFI_TYPE_DOUBLE: |
---|
485 | n/a | slot += (slot & 1) ? 1 : 2; |
---|
486 | n/a | #ifdef PA_LINUX |
---|
487 | n/a | /* See previous comment for FFI_TYPE_FLOAT. */ |
---|
488 | n/a | switch (slot - FIRST_ARG_SLOT) |
---|
489 | n/a | { |
---|
490 | n/a | case 1: fstd(fr5, (void *)(stack - slot)); break; |
---|
491 | n/a | case 3: fstd(fr7, (void *)(stack - slot)); break; |
---|
492 | n/a | } |
---|
493 | n/a | #endif |
---|
494 | n/a | avalue[i] = (void *)(stack - slot); |
---|
495 | n/a | break; |
---|
496 | n/a | |
---|
497 | n/a | #ifdef PA_HPUX |
---|
498 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
499 | n/a | /* Long doubles are treated like a big structure. */ |
---|
500 | n/a | avalue[i] = (void *) *(stack - slot); |
---|
501 | n/a | break; |
---|
502 | n/a | #endif |
---|
503 | n/a | |
---|
504 | n/a | case FFI_TYPE_STRUCT: |
---|
505 | n/a | /* Structs smaller or equal than 4 bytes are passed in one |
---|
506 | n/a | register. Structs smaller or equal 8 bytes are passed in two |
---|
507 | n/a | registers. Larger structures are passed by pointer. */ |
---|
508 | n/a | if((*p_arg)->size <= 4) |
---|
509 | n/a | { |
---|
510 | n/a | avalue[i] = (void *)(stack - slot) + sizeof(UINT32) - |
---|
511 | n/a | (*p_arg)->size; |
---|
512 | n/a | } |
---|
513 | n/a | else if ((*p_arg)->size <= 8) |
---|
514 | n/a | { |
---|
515 | n/a | slot += (slot & 1) ? 1 : 2; |
---|
516 | n/a | avalue[i] = (void *)(stack - slot) + sizeof(UINT64) - |
---|
517 | n/a | (*p_arg)->size; |
---|
518 | n/a | } |
---|
519 | n/a | else |
---|
520 | n/a | avalue[i] = (void *) *(stack - slot); |
---|
521 | n/a | break; |
---|
522 | n/a | |
---|
523 | n/a | default: |
---|
524 | n/a | FFI_ASSERT(0); |
---|
525 | n/a | } |
---|
526 | n/a | |
---|
527 | n/a | slot++; |
---|
528 | n/a | p_arg++; |
---|
529 | n/a | } |
---|
530 | n/a | |
---|
531 | n/a | /* Invoke the closure. */ |
---|
532 | n/a | (closure->fun) (cif, rvalue, avalue, closure->user_data); |
---|
533 | n/a | |
---|
534 | n/a | debug(3, "after calling function, ret[0] = %08x, ret[1] = %08x\n", ret[0], |
---|
535 | n/a | ret[1]); |
---|
536 | n/a | |
---|
537 | n/a | /* Store the result using the lower 2 bytes of the flags. */ |
---|
538 | n/a | switch (cif->flags) |
---|
539 | n/a | { |
---|
540 | n/a | case FFI_TYPE_UINT8: |
---|
541 | n/a | *(stack - FIRST_ARG_SLOT) = (UINT8)(ret[0] >> 24); |
---|
542 | n/a | break; |
---|
543 | n/a | case FFI_TYPE_SINT8: |
---|
544 | n/a | *(stack - FIRST_ARG_SLOT) = (SINT8)(ret[0] >> 24); |
---|
545 | n/a | break; |
---|
546 | n/a | case FFI_TYPE_UINT16: |
---|
547 | n/a | *(stack - FIRST_ARG_SLOT) = (UINT16)(ret[0] >> 16); |
---|
548 | n/a | break; |
---|
549 | n/a | case FFI_TYPE_SINT16: |
---|
550 | n/a | *(stack - FIRST_ARG_SLOT) = (SINT16)(ret[0] >> 16); |
---|
551 | n/a | break; |
---|
552 | n/a | case FFI_TYPE_INT: |
---|
553 | n/a | case FFI_TYPE_SINT32: |
---|
554 | n/a | case FFI_TYPE_UINT32: |
---|
555 | n/a | *(stack - FIRST_ARG_SLOT) = ret[0]; |
---|
556 | n/a | break; |
---|
557 | n/a | case FFI_TYPE_SINT64: |
---|
558 | n/a | case FFI_TYPE_UINT64: |
---|
559 | n/a | *(stack - FIRST_ARG_SLOT) = ret[0]; |
---|
560 | n/a | *(stack - FIRST_ARG_SLOT - 1) = ret[1]; |
---|
561 | n/a | break; |
---|
562 | n/a | |
---|
563 | n/a | case FFI_TYPE_DOUBLE: |
---|
564 | n/a | fldd(rvalue, fr4); |
---|
565 | n/a | break; |
---|
566 | n/a | |
---|
567 | n/a | case FFI_TYPE_FLOAT: |
---|
568 | n/a | fldw(rvalue, fr4); |
---|
569 | n/a | break; |
---|
570 | n/a | |
---|
571 | n/a | case FFI_TYPE_STRUCT: |
---|
572 | n/a | /* Don't need a return value, done by caller. */ |
---|
573 | n/a | break; |
---|
574 | n/a | |
---|
575 | n/a | case FFI_TYPE_SMALL_STRUCT2: |
---|
576 | n/a | case FFI_TYPE_SMALL_STRUCT3: |
---|
577 | n/a | case FFI_TYPE_SMALL_STRUCT4: |
---|
578 | n/a | tmp = (void*)(stack - FIRST_ARG_SLOT); |
---|
579 | n/a | tmp += 4 - cif->rtype->size; |
---|
580 | n/a | memcpy((void*)tmp, &ret[0], cif->rtype->size); |
---|
581 | n/a | break; |
---|
582 | n/a | |
---|
583 | n/a | case FFI_TYPE_SMALL_STRUCT5: |
---|
584 | n/a | case FFI_TYPE_SMALL_STRUCT6: |
---|
585 | n/a | case FFI_TYPE_SMALL_STRUCT7: |
---|
586 | n/a | case FFI_TYPE_SMALL_STRUCT8: |
---|
587 | n/a | { |
---|
588 | n/a | unsigned int ret2[2]; |
---|
589 | n/a | int off; |
---|
590 | n/a | |
---|
591 | n/a | /* Right justify ret[0] and ret[1] */ |
---|
592 | n/a | switch (cif->flags) |
---|
593 | n/a | { |
---|
594 | n/a | case FFI_TYPE_SMALL_STRUCT5: off = 3; break; |
---|
595 | n/a | case FFI_TYPE_SMALL_STRUCT6: off = 2; break; |
---|
596 | n/a | case FFI_TYPE_SMALL_STRUCT7: off = 1; break; |
---|
597 | n/a | default: off = 0; break; |
---|
598 | n/a | } |
---|
599 | n/a | |
---|
600 | n/a | memset (ret2, 0, sizeof (ret2)); |
---|
601 | n/a | memcpy ((char *)ret2 + off, ret, 8 - off); |
---|
602 | n/a | |
---|
603 | n/a | *(stack - FIRST_ARG_SLOT) = ret2[0]; |
---|
604 | n/a | *(stack - FIRST_ARG_SLOT - 1) = ret2[1]; |
---|
605 | n/a | } |
---|
606 | n/a | break; |
---|
607 | n/a | |
---|
608 | n/a | case FFI_TYPE_POINTER: |
---|
609 | n/a | case FFI_TYPE_VOID: |
---|
610 | n/a | break; |
---|
611 | n/a | |
---|
612 | n/a | default: |
---|
613 | n/a | debug(0, "assert with cif->flags: %d\n",cif->flags); |
---|
614 | n/a | FFI_ASSERT(0); |
---|
615 | n/a | break; |
---|
616 | n/a | } |
---|
617 | n/a | return FFI_OK; |
---|
618 | n/a | } |
---|
619 | n/a | |
---|
620 | n/a | /* Fill in a closure to refer to the specified fun and user_data. |
---|
621 | n/a | cif specifies the argument and result types for fun. |
---|
622 | n/a | The cif must already be prep'ed. */ |
---|
623 | n/a | |
---|
624 | n/a | extern void ffi_closure_pa32(void); |
---|
625 | n/a | |
---|
626 | n/a | ffi_status |
---|
627 | n/a | ffi_prep_closure_loc (ffi_closure* closure, |
---|
628 | n/a | ffi_cif* cif, |
---|
629 | n/a | void (*fun)(ffi_cif*,void*,void**,void*), |
---|
630 | n/a | void *user_data, |
---|
631 | n/a | void *codeloc) |
---|
632 | n/a | { |
---|
633 | n/a | UINT32 *tramp = (UINT32 *)(closure->tramp); |
---|
634 | n/a | #ifdef PA_HPUX |
---|
635 | n/a | UINT32 *tmp; |
---|
636 | n/a | #endif |
---|
637 | n/a | |
---|
638 | n/a | if (cif->abi != FFI_PA32) |
---|
639 | n/a | return FFI_BAD_ABI; |
---|
640 | n/a | |
---|
641 | n/a | /* Make a small trampoline that will branch to our |
---|
642 | n/a | handler function. Use PC-relative addressing. */ |
---|
643 | n/a | |
---|
644 | n/a | #ifdef PA_LINUX |
---|
645 | n/a | tramp[0] = 0xeaa00000; /* b,l .+8,%r21 ; %r21 <- pc+8 */ |
---|
646 | n/a | tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21 ; mask priv bits */ |
---|
647 | n/a | tramp[2] = 0x4aa10028; /* ldw 20(%r21),%r1 ; load plabel */ |
---|
648 | n/a | tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21 ; get closure addr */ |
---|
649 | n/a | tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22 ; address of handler */ |
---|
650 | n/a | tramp[5] = 0xeac0c000; /* bv%r0(%r22) ; branch to handler */ |
---|
651 | n/a | tramp[6] = 0x0c281093; /* ldw 4(%r1),%r19 ; GP of handler */ |
---|
652 | n/a | tramp[7] = ((UINT32)(ffi_closure_pa32) & ~2); |
---|
653 | n/a | |
---|
654 | n/a | /* Flush d/icache -- have to flush up 2 two lines because of |
---|
655 | n/a | alignment. */ |
---|
656 | n/a | __asm__ volatile( |
---|
657 | n/a | "fdc 0(%0)\n\t" |
---|
658 | n/a | "fdc %1(%0)\n\t" |
---|
659 | n/a | "fic 0(%%sr4, %0)\n\t" |
---|
660 | n/a | "fic %1(%%sr4, %0)\n\t" |
---|
661 | n/a | "sync\n\t" |
---|
662 | n/a | "nop\n\t" |
---|
663 | n/a | "nop\n\t" |
---|
664 | n/a | "nop\n\t" |
---|
665 | n/a | "nop\n\t" |
---|
666 | n/a | "nop\n\t" |
---|
667 | n/a | "nop\n\t" |
---|
668 | n/a | "nop\n" |
---|
669 | n/a | : |
---|
670 | n/a | : "r"((unsigned long)tramp & ~31), |
---|
671 | n/a | "r"(32 /* stride */) |
---|
672 | n/a | : "memory"); |
---|
673 | n/a | #endif |
---|
674 | n/a | |
---|
675 | n/a | #ifdef PA_HPUX |
---|
676 | n/a | tramp[0] = 0xeaa00000; /* b,l .+8,%r21 ; %r21 <- pc+8 */ |
---|
677 | n/a | tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21 ; mask priv bits */ |
---|
678 | n/a | tramp[2] = 0x4aa10038; /* ldw 28(%r21),%r1 ; load plabel */ |
---|
679 | n/a | tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21 ; get closure addr */ |
---|
680 | n/a | tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22 ; address of handler */ |
---|
681 | n/a | tramp[5] = 0x02c010b4; /* ldsid (%r22),%r20 ; load space id */ |
---|
682 | n/a | tramp[6] = 0x00141820; /* mtsp %r20,%sr0 ; into %sr0 */ |
---|
683 | n/a | tramp[7] = 0xe2c00000; /* be 0(%sr0,%r22) ; branch to handler */ |
---|
684 | n/a | tramp[8] = 0x0c281093; /* ldw 4(%r1),%r19 ; GP of handler */ |
---|
685 | n/a | tramp[9] = ((UINT32)(ffi_closure_pa32) & ~2); |
---|
686 | n/a | |
---|
687 | n/a | /* Flush d/icache -- have to flush three lines because of alignment. */ |
---|
688 | n/a | __asm__ volatile( |
---|
689 | n/a | "copy %1,%0\n\t" |
---|
690 | n/a | "fdc,m %2(%0)\n\t" |
---|
691 | n/a | "fdc,m %2(%0)\n\t" |
---|
692 | n/a | "fdc,m %2(%0)\n\t" |
---|
693 | n/a | "ldsid (%1),%0\n\t" |
---|
694 | n/a | "mtsp %0,%%sr0\n\t" |
---|
695 | n/a | "copy %1,%0\n\t" |
---|
696 | n/a | "fic,m %2(%%sr0,%0)\n\t" |
---|
697 | n/a | "fic,m %2(%%sr0,%0)\n\t" |
---|
698 | n/a | "fic,m %2(%%sr0,%0)\n\t" |
---|
699 | n/a | "sync\n\t" |
---|
700 | n/a | "nop\n\t" |
---|
701 | n/a | "nop\n\t" |
---|
702 | n/a | "nop\n\t" |
---|
703 | n/a | "nop\n\t" |
---|
704 | n/a | "nop\n\t" |
---|
705 | n/a | "nop\n\t" |
---|
706 | n/a | "nop\n" |
---|
707 | n/a | : "=&r" ((unsigned long)tmp) |
---|
708 | n/a | : "r" ((unsigned long)tramp & ~31), |
---|
709 | n/a | "r" (32/* stride */) |
---|
710 | n/a | : "memory"); |
---|
711 | n/a | #endif |
---|
712 | n/a | |
---|
713 | n/a | closure->cif = cif; |
---|
714 | n/a | closure->user_data = user_data; |
---|
715 | n/a | closure->fun = fun; |
---|
716 | n/a | |
---|
717 | n/a | return FFI_OK; |
---|
718 | n/a | } |
---|
719 | n/a | #endif |
---|