| 1 | n/a | /* ----------------------------------------------------------------------- |
|---|
| 2 | n/a | ffi64.c - Copyright (c) 2013 The Written Word, Inc. |
|---|
| 3 | n/a | Copyright (c) 2011 Anthony Green |
|---|
| 4 | n/a | Copyright (c) 2008, 2010 Red Hat, Inc. |
|---|
| 5 | n/a | Copyright (c) 2002, 2007 Bo Thorsen <bo@suse.de> |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | x86-64 Foreign Function Interface |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 10 | n/a | a copy of this software and associated documentation files (the |
|---|
| 11 | n/a | ``Software''), to deal in the Software without restriction, including |
|---|
| 12 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 13 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 14 | n/a | permit persons to whom the Software is furnished to do so, subject to |
|---|
| 15 | n/a | the following conditions: |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | The above copyright notice and this permission notice shall be included |
|---|
| 18 | n/a | in all copies or substantial portions of the Software. |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
|---|
| 21 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 22 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 23 | n/a | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 24 | n/a | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 25 | n/a | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 26 | n/a | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 27 | n/a | DEALINGS IN THE SOFTWARE. |
|---|
| 28 | n/a | ----------------------------------------------------------------------- */ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | #include <ffi.h> |
|---|
| 31 | n/a | #include <ffi_common.h> |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | #include <stdlib.h> |
|---|
| 34 | n/a | #include <stdarg.h> |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #ifdef __x86_64__ |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | #define MAX_GPR_REGS 6 |
|---|
| 39 | n/a | #define MAX_SSE_REGS 8 |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | #if defined(__INTEL_COMPILER) |
|---|
| 42 | n/a | #include "xmmintrin.h" |
|---|
| 43 | n/a | #define UINT128 __m128 |
|---|
| 44 | n/a | #else |
|---|
| 45 | n/a | #if defined(__SUNPRO_C) |
|---|
| 46 | n/a | #include <sunmedia_types.h> |
|---|
| 47 | n/a | #define UINT128 __m128i |
|---|
| 48 | n/a | #else |
|---|
| 49 | n/a | #define UINT128 __int128_t |
|---|
| 50 | n/a | #endif |
|---|
| 51 | n/a | #endif |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | union big_int_union |
|---|
| 54 | n/a | { |
|---|
| 55 | n/a | UINT32 i32; |
|---|
| 56 | n/a | UINT64 i64; |
|---|
| 57 | n/a | UINT128 i128; |
|---|
| 58 | n/a | }; |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | struct register_args |
|---|
| 61 | n/a | { |
|---|
| 62 | n/a | /* Registers for argument passing. */ |
|---|
| 63 | n/a | UINT64 gpr[MAX_GPR_REGS]; |
|---|
| 64 | n/a | union big_int_union sse[MAX_SSE_REGS]; |
|---|
| 65 | n/a | }; |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | extern void ffi_call_unix64 (void *args, unsigned long bytes, unsigned flags, |
|---|
| 68 | n/a | void *raddr, void (*fnaddr)(void), unsigned ssecount); |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | /* All reference to register classes here is identical to the code in |
|---|
| 71 | n/a | gcc/config/i386/i386.c. Do *not* change one without the other. */ |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | /* Register class used for passing given 64bit part of the argument. |
|---|
| 74 | n/a | These represent classes as documented by the PS ABI, with the |
|---|
| 75 | n/a | exception of SSESF, SSEDF classes, that are basically SSE class, |
|---|
| 76 | n/a | just gcc will use SF or DFmode move instead of DImode to avoid |
|---|
| 77 | n/a | reformatting penalties. |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | Similary we play games with INTEGERSI_CLASS to use cheaper SImode moves |
|---|
| 80 | n/a | whenever possible (upper half does contain padding). */ |
|---|
| 81 | n/a | enum x86_64_reg_class |
|---|
| 82 | n/a | { |
|---|
| 83 | n/a | X86_64_NO_CLASS, |
|---|
| 84 | n/a | X86_64_INTEGER_CLASS, |
|---|
| 85 | n/a | X86_64_INTEGERSI_CLASS, |
|---|
| 86 | n/a | X86_64_SSE_CLASS, |
|---|
| 87 | n/a | X86_64_SSESF_CLASS, |
|---|
| 88 | n/a | X86_64_SSEDF_CLASS, |
|---|
| 89 | n/a | X86_64_SSEUP_CLASS, |
|---|
| 90 | n/a | X86_64_X87_CLASS, |
|---|
| 91 | n/a | X86_64_X87UP_CLASS, |
|---|
| 92 | n/a | X86_64_COMPLEX_X87_CLASS, |
|---|
| 93 | n/a | X86_64_MEMORY_CLASS |
|---|
| 94 | n/a | }; |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | #define MAX_CLASSES 4 |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | #define SSE_CLASS_P(X) ((X) >= X86_64_SSE_CLASS && X <= X86_64_SSEUP_CLASS) |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | /* x86-64 register passing implementation. See x86-64 ABI for details. Goal |
|---|
| 101 | n/a | of this code is to classify each 8bytes of incoming argument by the register |
|---|
| 102 | n/a | class and assign registers accordingly. */ |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | /* Return the union class of CLASS1 and CLASS2. |
|---|
| 105 | n/a | See the x86-64 PS ABI for details. */ |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | static enum x86_64_reg_class |
|---|
| 108 | n/a | merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2) |
|---|
| 109 | n/a | { |
|---|
| 110 | n/a | /* Rule #1: If both classes are equal, this is the resulting class. */ |
|---|
| 111 | n/a | if (class1 == class2) |
|---|
| 112 | n/a | return class1; |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | /* Rule #2: If one of the classes is NO_CLASS, the resulting class is |
|---|
| 115 | n/a | the other class. */ |
|---|
| 116 | n/a | if (class1 == X86_64_NO_CLASS) |
|---|
| 117 | n/a | return class2; |
|---|
| 118 | n/a | if (class2 == X86_64_NO_CLASS) |
|---|
| 119 | n/a | return class1; |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */ |
|---|
| 122 | n/a | if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS) |
|---|
| 123 | n/a | return X86_64_MEMORY_CLASS; |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */ |
|---|
| 126 | n/a | if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS) |
|---|
| 127 | n/a | || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS)) |
|---|
| 128 | n/a | return X86_64_INTEGERSI_CLASS; |
|---|
| 129 | n/a | if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS |
|---|
| 130 | n/a | || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS) |
|---|
| 131 | n/a | return X86_64_INTEGER_CLASS; |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class, |
|---|
| 134 | n/a | MEMORY is used. */ |
|---|
| 135 | n/a | if (class1 == X86_64_X87_CLASS |
|---|
| 136 | n/a | || class1 == X86_64_X87UP_CLASS |
|---|
| 137 | n/a | || class1 == X86_64_COMPLEX_X87_CLASS |
|---|
| 138 | n/a | || class2 == X86_64_X87_CLASS |
|---|
| 139 | n/a | || class2 == X86_64_X87UP_CLASS |
|---|
| 140 | n/a | || class2 == X86_64_COMPLEX_X87_CLASS) |
|---|
| 141 | n/a | return X86_64_MEMORY_CLASS; |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | /* Rule #6: Otherwise class SSE is used. */ |
|---|
| 144 | n/a | return X86_64_SSE_CLASS; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | /* Classify the argument of type TYPE and mode MODE. |
|---|
| 148 | n/a | CLASSES will be filled by the register class used to pass each word |
|---|
| 149 | n/a | of the operand. The number of words is returned. In case the parameter |
|---|
| 150 | n/a | should be passed in memory, 0 is returned. As a special case for zero |
|---|
| 151 | n/a | sized containers, classes[0] will be NO_CLASS and 1 is returned. |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | See the x86-64 PS ABI for details. |
|---|
| 154 | n/a | */ |
|---|
| 155 | n/a | static size_t |
|---|
| 156 | n/a | classify_argument (ffi_type *type, enum x86_64_reg_class classes[], |
|---|
| 157 | n/a | size_t byte_offset) |
|---|
| 158 | n/a | { |
|---|
| 159 | n/a | switch (type->type) |
|---|
| 160 | n/a | { |
|---|
| 161 | n/a | case FFI_TYPE_UINT8: |
|---|
| 162 | n/a | case FFI_TYPE_SINT8: |
|---|
| 163 | n/a | case FFI_TYPE_UINT16: |
|---|
| 164 | n/a | case FFI_TYPE_SINT16: |
|---|
| 165 | n/a | case FFI_TYPE_UINT32: |
|---|
| 166 | n/a | case FFI_TYPE_SINT32: |
|---|
| 167 | n/a | case FFI_TYPE_UINT64: |
|---|
| 168 | n/a | case FFI_TYPE_SINT64: |
|---|
| 169 | n/a | case FFI_TYPE_POINTER: |
|---|
| 170 | n/a | { |
|---|
| 171 | n/a | size_t size = byte_offset + type->size; |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | if (size <= 4) |
|---|
| 174 | n/a | { |
|---|
| 175 | n/a | classes[0] = X86_64_INTEGERSI_CLASS; |
|---|
| 176 | n/a | return 1; |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | else if (size <= 8) |
|---|
| 179 | n/a | { |
|---|
| 180 | n/a | classes[0] = X86_64_INTEGER_CLASS; |
|---|
| 181 | n/a | return 1; |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | else if (size <= 12) |
|---|
| 184 | n/a | { |
|---|
| 185 | n/a | classes[0] = X86_64_INTEGER_CLASS; |
|---|
| 186 | n/a | classes[1] = X86_64_INTEGERSI_CLASS; |
|---|
| 187 | n/a | return 2; |
|---|
| 188 | n/a | } |
|---|
| 189 | n/a | else if (size <= 16) |
|---|
| 190 | n/a | { |
|---|
| 191 | n/a | classes[0] = classes[1] = X86_64_INTEGERSI_CLASS; |
|---|
| 192 | n/a | return 2; |
|---|
| 193 | n/a | } |
|---|
| 194 | n/a | else |
|---|
| 195 | n/a | FFI_ASSERT (0); |
|---|
| 196 | n/a | } |
|---|
| 197 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 198 | n/a | if (!(byte_offset % 8)) |
|---|
| 199 | n/a | classes[0] = X86_64_SSESF_CLASS; |
|---|
| 200 | n/a | else |
|---|
| 201 | n/a | classes[0] = X86_64_SSE_CLASS; |
|---|
| 202 | n/a | return 1; |
|---|
| 203 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 204 | n/a | classes[0] = X86_64_SSEDF_CLASS; |
|---|
| 205 | n/a | return 1; |
|---|
| 206 | n/a | #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 207 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 208 | n/a | classes[0] = X86_64_X87_CLASS; |
|---|
| 209 | n/a | classes[1] = X86_64_X87UP_CLASS; |
|---|
| 210 | n/a | return 2; |
|---|
| 211 | n/a | #endif |
|---|
| 212 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 213 | n/a | { |
|---|
| 214 | n/a | const size_t UNITS_PER_WORD = 8; |
|---|
| 215 | n/a | size_t words = (type->size + UNITS_PER_WORD - 1) / UNITS_PER_WORD; |
|---|
| 216 | n/a | ffi_type **ptr; |
|---|
| 217 | n/a | int i; |
|---|
| 218 | n/a | enum x86_64_reg_class subclasses[MAX_CLASSES]; |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | /* If the struct is larger than 32 bytes, pass it on the stack. */ |
|---|
| 221 | n/a | if (type->size > 32) |
|---|
| 222 | n/a | return 0; |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | for (i = 0; i < words; i++) |
|---|
| 225 | n/a | classes[i] = X86_64_NO_CLASS; |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | /* Zero sized arrays or structures are NO_CLASS. We return 0 to |
|---|
| 228 | n/a | signalize memory class, so handle it as special case. */ |
|---|
| 229 | n/a | if (!words) |
|---|
| 230 | n/a | { |
|---|
| 231 | n/a | classes[0] = X86_64_NO_CLASS; |
|---|
| 232 | n/a | return 1; |
|---|
| 233 | n/a | } |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | /* Merge the fields of structure. */ |
|---|
| 236 | n/a | for (ptr = type->elements; *ptr != NULL; ptr++) |
|---|
| 237 | n/a | { |
|---|
| 238 | n/a | size_t num; |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | byte_offset = ALIGN (byte_offset, (*ptr)->alignment); |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | num = classify_argument (*ptr, subclasses, byte_offset % 8); |
|---|
| 243 | n/a | if (num == 0) |
|---|
| 244 | n/a | return 0; |
|---|
| 245 | n/a | for (i = 0; i < num; i++) |
|---|
| 246 | n/a | { |
|---|
| 247 | n/a | size_t pos = byte_offset / 8; |
|---|
| 248 | n/a | classes[i + pos] = |
|---|
| 249 | n/a | merge_classes (subclasses[i], classes[i + pos]); |
|---|
| 250 | n/a | } |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | byte_offset += (*ptr)->size; |
|---|
| 253 | n/a | } |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | if (words > 2) |
|---|
| 256 | n/a | { |
|---|
| 257 | n/a | /* When size > 16 bytes, if the first one isn't |
|---|
| 258 | n/a | X86_64_SSE_CLASS or any other ones aren't |
|---|
| 259 | n/a | X86_64_SSEUP_CLASS, everything should be passed in |
|---|
| 260 | n/a | memory. */ |
|---|
| 261 | n/a | if (classes[0] != X86_64_SSE_CLASS) |
|---|
| 262 | n/a | return 0; |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | for (i = 1; i < words; i++) |
|---|
| 265 | n/a | if (classes[i] != X86_64_SSEUP_CLASS) |
|---|
| 266 | n/a | return 0; |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | /* Final merger cleanup. */ |
|---|
| 270 | n/a | for (i = 0; i < words; i++) |
|---|
| 271 | n/a | { |
|---|
| 272 | n/a | /* If one class is MEMORY, everything should be passed in |
|---|
| 273 | n/a | memory. */ |
|---|
| 274 | n/a | if (classes[i] == X86_64_MEMORY_CLASS) |
|---|
| 275 | n/a | return 0; |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | /* The X86_64_SSEUP_CLASS should be always preceded by |
|---|
| 278 | n/a | X86_64_SSE_CLASS or X86_64_SSEUP_CLASS. */ |
|---|
| 279 | n/a | if (classes[i] == X86_64_SSEUP_CLASS |
|---|
| 280 | n/a | && classes[i - 1] != X86_64_SSE_CLASS |
|---|
| 281 | n/a | && classes[i - 1] != X86_64_SSEUP_CLASS) |
|---|
| 282 | n/a | { |
|---|
| 283 | n/a | /* The first one should never be X86_64_SSEUP_CLASS. */ |
|---|
| 284 | n/a | FFI_ASSERT (i != 0); |
|---|
| 285 | n/a | classes[i] = X86_64_SSE_CLASS; |
|---|
| 286 | n/a | } |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | /* If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS, |
|---|
| 289 | n/a | everything should be passed in memory. */ |
|---|
| 290 | n/a | if (classes[i] == X86_64_X87UP_CLASS |
|---|
| 291 | n/a | && (classes[i - 1] != X86_64_X87_CLASS)) |
|---|
| 292 | n/a | { |
|---|
| 293 | n/a | /* The first one should never be X86_64_X87UP_CLASS. */ |
|---|
| 294 | n/a | FFI_ASSERT (i != 0); |
|---|
| 295 | n/a | return 0; |
|---|
| 296 | n/a | } |
|---|
| 297 | n/a | } |
|---|
| 298 | n/a | return words; |
|---|
| 299 | n/a | } |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | default: |
|---|
| 302 | n/a | FFI_ASSERT(0); |
|---|
| 303 | n/a | } |
|---|
| 304 | n/a | return 0; /* Never reached. */ |
|---|
| 305 | n/a | } |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | /* Examine the argument and return set number of register required in each |
|---|
| 308 | n/a | class. Return zero iff parameter should be passed in memory, otherwise |
|---|
| 309 | n/a | the number of registers. */ |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | static size_t |
|---|
| 312 | n/a | examine_argument (ffi_type *type, enum x86_64_reg_class classes[MAX_CLASSES], |
|---|
| 313 | n/a | _Bool in_return, int *pngpr, int *pnsse) |
|---|
| 314 | n/a | { |
|---|
| 315 | n/a | size_t n; |
|---|
| 316 | n/a | int i, ngpr, nsse; |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | n = classify_argument (type, classes, 0); |
|---|
| 319 | n/a | if (n == 0) |
|---|
| 320 | n/a | return 0; |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | ngpr = nsse = 0; |
|---|
| 323 | n/a | for (i = 0; i < n; ++i) |
|---|
| 324 | n/a | switch (classes[i]) |
|---|
| 325 | n/a | { |
|---|
| 326 | n/a | case X86_64_INTEGER_CLASS: |
|---|
| 327 | n/a | case X86_64_INTEGERSI_CLASS: |
|---|
| 328 | n/a | ngpr++; |
|---|
| 329 | n/a | break; |
|---|
| 330 | n/a | case X86_64_SSE_CLASS: |
|---|
| 331 | n/a | case X86_64_SSESF_CLASS: |
|---|
| 332 | n/a | case X86_64_SSEDF_CLASS: |
|---|
| 333 | n/a | nsse++; |
|---|
| 334 | n/a | break; |
|---|
| 335 | n/a | case X86_64_NO_CLASS: |
|---|
| 336 | n/a | case X86_64_SSEUP_CLASS: |
|---|
| 337 | n/a | break; |
|---|
| 338 | n/a | case X86_64_X87_CLASS: |
|---|
| 339 | n/a | case X86_64_X87UP_CLASS: |
|---|
| 340 | n/a | case X86_64_COMPLEX_X87_CLASS: |
|---|
| 341 | n/a | return in_return != 0; |
|---|
| 342 | n/a | default: |
|---|
| 343 | n/a | abort (); |
|---|
| 344 | n/a | } |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | *pngpr = ngpr; |
|---|
| 347 | n/a | *pnsse = nsse; |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | return n; |
|---|
| 350 | n/a | } |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | /* Perform machine dependent cif processing. */ |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | ffi_status |
|---|
| 355 | n/a | ffi_prep_cif_machdep (ffi_cif *cif) |
|---|
| 356 | n/a | { |
|---|
| 357 | n/a | int gprcount, ssecount, i, avn, ngpr, nsse, flags; |
|---|
| 358 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
|---|
| 359 | n/a | size_t bytes, n; |
|---|
| 360 | n/a | |
|---|
| 361 | n/a | gprcount = ssecount = 0; |
|---|
| 362 | n/a | |
|---|
| 363 | n/a | flags = cif->rtype->type; |
|---|
| 364 | n/a | if (flags != FFI_TYPE_VOID) |
|---|
| 365 | n/a | { |
|---|
| 366 | n/a | n = examine_argument (cif->rtype, classes, 1, &ngpr, &nsse); |
|---|
| 367 | n/a | if (n == 0) |
|---|
| 368 | n/a | { |
|---|
| 369 | n/a | /* The return value is passed in memory. A pointer to that |
|---|
| 370 | n/a | memory is the first argument. Allocate a register for it. */ |
|---|
| 371 | n/a | gprcount++; |
|---|
| 372 | n/a | /* We don't have to do anything in asm for the return. */ |
|---|
| 373 | n/a | flags = FFI_TYPE_VOID; |
|---|
| 374 | n/a | } |
|---|
| 375 | n/a | else if (flags == FFI_TYPE_STRUCT) |
|---|
| 376 | n/a | { |
|---|
| 377 | n/a | /* Mark which registers the result appears in. */ |
|---|
| 378 | n/a | _Bool sse0 = SSE_CLASS_P (classes[0]); |
|---|
| 379 | n/a | _Bool sse1 = n == 2 && SSE_CLASS_P (classes[1]); |
|---|
| 380 | n/a | if (sse0 && !sse1) |
|---|
| 381 | n/a | flags |= 1 << 8; |
|---|
| 382 | n/a | else if (!sse0 && sse1) |
|---|
| 383 | n/a | flags |= 1 << 9; |
|---|
| 384 | n/a | else if (sse0 && sse1) |
|---|
| 385 | n/a | flags |= 1 << 10; |
|---|
| 386 | n/a | /* Mark the true size of the structure. */ |
|---|
| 387 | n/a | flags |= cif->rtype->size << 12; |
|---|
| 388 | n/a | } |
|---|
| 389 | n/a | } |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | /* Go over all arguments and determine the way they should be passed. |
|---|
| 392 | n/a | If it's in a register and there is space for it, let that be so. If |
|---|
| 393 | n/a | not, add it's size to the stack byte count. */ |
|---|
| 394 | n/a | for (bytes = 0, i = 0, avn = cif->nargs; i < avn; i++) |
|---|
| 395 | n/a | { |
|---|
| 396 | n/a | if (examine_argument (cif->arg_types[i], classes, 0, &ngpr, &nsse) == 0 |
|---|
| 397 | n/a | || gprcount + ngpr > MAX_GPR_REGS |
|---|
| 398 | n/a | || ssecount + nsse > MAX_SSE_REGS) |
|---|
| 399 | n/a | { |
|---|
| 400 | n/a | long align = cif->arg_types[i]->alignment; |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | if (align < 8) |
|---|
| 403 | n/a | align = 8; |
|---|
| 404 | n/a | |
|---|
| 405 | n/a | bytes = ALIGN (bytes, align); |
|---|
| 406 | n/a | bytes += cif->arg_types[i]->size; |
|---|
| 407 | n/a | } |
|---|
| 408 | n/a | else |
|---|
| 409 | n/a | { |
|---|
| 410 | n/a | gprcount += ngpr; |
|---|
| 411 | n/a | ssecount += nsse; |
|---|
| 412 | n/a | } |
|---|
| 413 | n/a | } |
|---|
| 414 | n/a | if (ssecount) |
|---|
| 415 | n/a | flags |= 1 << 11; |
|---|
| 416 | n/a | cif->flags = flags; |
|---|
| 417 | n/a | cif->bytes = (unsigned)ALIGN (bytes, 8); |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | return FFI_OK; |
|---|
| 420 | n/a | } |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | void |
|---|
| 423 | n/a | ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) |
|---|
| 424 | n/a | { |
|---|
| 425 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
|---|
| 426 | n/a | char *stack, *argp; |
|---|
| 427 | n/a | ffi_type **arg_types; |
|---|
| 428 | n/a | int gprcount, ssecount, ngpr, nsse, i, avn; |
|---|
| 429 | n/a | _Bool ret_in_memory; |
|---|
| 430 | n/a | struct register_args *reg_args; |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | /* Can't call 32-bit mode from 64-bit mode. */ |
|---|
| 433 | n/a | FFI_ASSERT (cif->abi == FFI_UNIX64); |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | /* If the return value is a struct and we don't have a return value |
|---|
| 436 | n/a | address then we need to make one. Note the setting of flags to |
|---|
| 437 | n/a | VOID above in ffi_prep_cif_machdep. */ |
|---|
| 438 | n/a | ret_in_memory = (cif->rtype->type == FFI_TYPE_STRUCT |
|---|
| 439 | n/a | && (cif->flags & 0xff) == FFI_TYPE_VOID); |
|---|
| 440 | n/a | if (rvalue == NULL && ret_in_memory) |
|---|
| 441 | n/a | rvalue = alloca (cif->rtype->size); |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | /* Allocate the space for the arguments, plus 4 words of temp space. */ |
|---|
| 444 | n/a | stack = alloca (sizeof (struct register_args) + cif->bytes + 4*8); |
|---|
| 445 | n/a | reg_args = (struct register_args *) stack; |
|---|
| 446 | n/a | argp = stack + sizeof (struct register_args); |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | gprcount = ssecount = 0; |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | /* If the return value is passed in memory, add the pointer as the |
|---|
| 451 | n/a | first integer argument. */ |
|---|
| 452 | n/a | if (ret_in_memory) |
|---|
| 453 | n/a | reg_args->gpr[gprcount++] = (unsigned long) rvalue; |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | avn = cif->nargs; |
|---|
| 456 | n/a | arg_types = cif->arg_types; |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | for (i = 0; i < avn; ++i) |
|---|
| 459 | n/a | { |
|---|
| 460 | n/a | size_t n, size = arg_types[i]->size; |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | n = examine_argument (arg_types[i], classes, 0, &ngpr, &nsse); |
|---|
| 463 | n/a | if (n == 0 |
|---|
| 464 | n/a | || gprcount + ngpr > MAX_GPR_REGS |
|---|
| 465 | n/a | || ssecount + nsse > MAX_SSE_REGS) |
|---|
| 466 | n/a | { |
|---|
| 467 | n/a | long align = arg_types[i]->alignment; |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | /* Stack arguments are *always* at least 8 byte aligned. */ |
|---|
| 470 | n/a | if (align < 8) |
|---|
| 471 | n/a | align = 8; |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | /* Pass this argument in memory. */ |
|---|
| 474 | n/a | argp = (void *) ALIGN (argp, align); |
|---|
| 475 | n/a | memcpy (argp, avalue[i], size); |
|---|
| 476 | n/a | argp += size; |
|---|
| 477 | n/a | } |
|---|
| 478 | n/a | else |
|---|
| 479 | n/a | { |
|---|
| 480 | n/a | /* The argument is passed entirely in registers. */ |
|---|
| 481 | n/a | char *a = (char *) avalue[i]; |
|---|
| 482 | n/a | int j; |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | for (j = 0; j < n; j++, a += 8, size -= 8) |
|---|
| 485 | n/a | { |
|---|
| 486 | n/a | switch (classes[j]) |
|---|
| 487 | n/a | { |
|---|
| 488 | n/a | case X86_64_INTEGER_CLASS: |
|---|
| 489 | n/a | case X86_64_INTEGERSI_CLASS: |
|---|
| 490 | n/a | /* Sign-extend integer arguments passed in general |
|---|
| 491 | n/a | purpose registers, to cope with the fact that |
|---|
| 492 | n/a | LLVM incorrectly assumes that this will be done |
|---|
| 493 | n/a | (the x86-64 PS ABI does not specify this). */ |
|---|
| 494 | n/a | switch (arg_types[i]->type) |
|---|
| 495 | n/a | { |
|---|
| 496 | n/a | case FFI_TYPE_SINT8: |
|---|
| 497 | n/a | *(SINT64 *)®_args->gpr[gprcount] = (SINT64) *((SINT8 *) a); |
|---|
| 498 | n/a | break; |
|---|
| 499 | n/a | case FFI_TYPE_SINT16: |
|---|
| 500 | n/a | *(SINT64 *)®_args->gpr[gprcount] = (SINT64) *((SINT16 *) a); |
|---|
| 501 | n/a | break; |
|---|
| 502 | n/a | case FFI_TYPE_SINT32: |
|---|
| 503 | n/a | *(SINT64 *)®_args->gpr[gprcount] = (SINT64) *((SINT32 *) a); |
|---|
| 504 | n/a | break; |
|---|
| 505 | n/a | default: |
|---|
| 506 | n/a | reg_args->gpr[gprcount] = 0; |
|---|
| 507 | n/a | memcpy (®_args->gpr[gprcount], a, size < 8 ? size : 8); |
|---|
| 508 | n/a | } |
|---|
| 509 | n/a | gprcount++; |
|---|
| 510 | n/a | break; |
|---|
| 511 | n/a | case X86_64_SSE_CLASS: |
|---|
| 512 | n/a | case X86_64_SSEDF_CLASS: |
|---|
| 513 | n/a | reg_args->sse[ssecount++].i64 = *(UINT64 *) a; |
|---|
| 514 | n/a | break; |
|---|
| 515 | n/a | case X86_64_SSESF_CLASS: |
|---|
| 516 | n/a | reg_args->sse[ssecount++].i32 = *(UINT32 *) a; |
|---|
| 517 | n/a | break; |
|---|
| 518 | n/a | default: |
|---|
| 519 | n/a | abort(); |
|---|
| 520 | n/a | } |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | } |
|---|
| 523 | n/a | } |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | ffi_call_unix64 (stack, cif->bytes + sizeof (struct register_args), |
|---|
| 526 | n/a | cif->flags, rvalue, fn, ssecount); |
|---|
| 527 | n/a | } |
|---|
| 528 | n/a | |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | extern void ffi_closure_unix64(void); |
|---|
| 531 | n/a | |
|---|
| 532 | n/a | ffi_status |
|---|
| 533 | n/a | ffi_prep_closure_loc (ffi_closure* closure, |
|---|
| 534 | n/a | ffi_cif* cif, |
|---|
| 535 | n/a | void (*fun)(ffi_cif*, void*, void**, void*), |
|---|
| 536 | n/a | void *user_data, |
|---|
| 537 | n/a | void *codeloc) |
|---|
| 538 | n/a | { |
|---|
| 539 | n/a | volatile unsigned short *tramp; |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | /* Sanity check on the cif ABI. */ |
|---|
| 542 | n/a | { |
|---|
| 543 | n/a | int abi = cif->abi; |
|---|
| 544 | n/a | if (UNLIKELY (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI))) |
|---|
| 545 | n/a | return FFI_BAD_ABI; |
|---|
| 546 | n/a | } |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | tramp = (volatile unsigned short *) &closure->tramp[0]; |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | tramp[0] = 0xbb49; /* mov <code>, %r11 */ |
|---|
| 551 | n/a | *((unsigned long long * volatile) &tramp[1]) |
|---|
| 552 | n/a | = (unsigned long) ffi_closure_unix64; |
|---|
| 553 | n/a | tramp[5] = 0xba49; /* mov <data>, %r10 */ |
|---|
| 554 | n/a | *((unsigned long long * volatile) &tramp[6]) |
|---|
| 555 | n/a | = (unsigned long) codeloc; |
|---|
| 556 | n/a | |
|---|
| 557 | n/a | /* Set the carry bit iff the function uses any sse registers. |
|---|
| 558 | n/a | This is clc or stc, together with the first byte of the jmp. */ |
|---|
| 559 | n/a | tramp[10] = cif->flags & (1 << 11) ? 0x49f9 : 0x49f8; |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | tramp[11] = 0xe3ff; /* jmp *%r11 */ |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | closure->cif = cif; |
|---|
| 564 | n/a | closure->fun = fun; |
|---|
| 565 | n/a | closure->user_data = user_data; |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | return FFI_OK; |
|---|
| 568 | n/a | } |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | int |
|---|
| 571 | n/a | ffi_closure_unix64_inner(ffi_closure *closure, void *rvalue, |
|---|
| 572 | n/a | struct register_args *reg_args, char *argp) |
|---|
| 573 | n/a | { |
|---|
| 574 | n/a | ffi_cif *cif; |
|---|
| 575 | n/a | void **avalue; |
|---|
| 576 | n/a | ffi_type **arg_types; |
|---|
| 577 | n/a | long i, avn; |
|---|
| 578 | n/a | int gprcount, ssecount, ngpr, nsse; |
|---|
| 579 | n/a | int ret; |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | cif = closure->cif; |
|---|
| 582 | n/a | avalue = alloca(cif->nargs * sizeof(void *)); |
|---|
| 583 | n/a | gprcount = ssecount = 0; |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | ret = cif->rtype->type; |
|---|
| 586 | n/a | if (ret != FFI_TYPE_VOID) |
|---|
| 587 | n/a | { |
|---|
| 588 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
|---|
| 589 | n/a | size_t n = examine_argument (cif->rtype, classes, 1, &ngpr, &nsse); |
|---|
| 590 | n/a | if (n == 0) |
|---|
| 591 | n/a | { |
|---|
| 592 | n/a | /* The return value goes in memory. Arrange for the closure |
|---|
| 593 | n/a | return value to go directly back to the original caller. */ |
|---|
| 594 | n/a | rvalue = (void *) (unsigned long) reg_args->gpr[gprcount++]; |
|---|
| 595 | n/a | /* We don't have to do anything in asm for the return. */ |
|---|
| 596 | n/a | ret = FFI_TYPE_VOID; |
|---|
| 597 | n/a | } |
|---|
| 598 | n/a | else if (ret == FFI_TYPE_STRUCT && n == 2) |
|---|
| 599 | n/a | { |
|---|
| 600 | n/a | /* Mark which register the second word of the structure goes in. */ |
|---|
| 601 | n/a | _Bool sse0 = SSE_CLASS_P (classes[0]); |
|---|
| 602 | n/a | _Bool sse1 = SSE_CLASS_P (classes[1]); |
|---|
| 603 | n/a | if (!sse0 && sse1) |
|---|
| 604 | n/a | ret |= 1 << 8; |
|---|
| 605 | n/a | else if (sse0 && !sse1) |
|---|
| 606 | n/a | ret |= 1 << 9; |
|---|
| 607 | n/a | } |
|---|
| 608 | n/a | } |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | avn = cif->nargs; |
|---|
| 611 | n/a | arg_types = cif->arg_types; |
|---|
| 612 | n/a | |
|---|
| 613 | n/a | for (i = 0; i < avn; ++i) |
|---|
| 614 | n/a | { |
|---|
| 615 | n/a | enum x86_64_reg_class classes[MAX_CLASSES]; |
|---|
| 616 | n/a | size_t n; |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | n = examine_argument (arg_types[i], classes, 0, &ngpr, &nsse); |
|---|
| 619 | n/a | if (n == 0 |
|---|
| 620 | n/a | || gprcount + ngpr > MAX_GPR_REGS |
|---|
| 621 | n/a | || ssecount + nsse > MAX_SSE_REGS) |
|---|
| 622 | n/a | { |
|---|
| 623 | n/a | long align = arg_types[i]->alignment; |
|---|
| 624 | n/a | |
|---|
| 625 | n/a | /* Stack arguments are *always* at least 8 byte aligned. */ |
|---|
| 626 | n/a | if (align < 8) |
|---|
| 627 | n/a | align = 8; |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | /* Pass this argument in memory. */ |
|---|
| 630 | n/a | argp = (void *) ALIGN (argp, align); |
|---|
| 631 | n/a | avalue[i] = argp; |
|---|
| 632 | n/a | argp += arg_types[i]->size; |
|---|
| 633 | n/a | } |
|---|
| 634 | n/a | /* If the argument is in a single register, or two consecutive |
|---|
| 635 | n/a | integer registers, then we can use that address directly. */ |
|---|
| 636 | n/a | else if (n == 1 |
|---|
| 637 | n/a | || (n == 2 && !(SSE_CLASS_P (classes[0]) |
|---|
| 638 | n/a | || SSE_CLASS_P (classes[1])))) |
|---|
| 639 | n/a | { |
|---|
| 640 | n/a | /* The argument is in a single register. */ |
|---|
| 641 | n/a | if (SSE_CLASS_P (classes[0])) |
|---|
| 642 | n/a | { |
|---|
| 643 | n/a | avalue[i] = ®_args->sse[ssecount]; |
|---|
| 644 | n/a | ssecount += n; |
|---|
| 645 | n/a | } |
|---|
| 646 | n/a | else |
|---|
| 647 | n/a | { |
|---|
| 648 | n/a | avalue[i] = ®_args->gpr[gprcount]; |
|---|
| 649 | n/a | gprcount += n; |
|---|
| 650 | n/a | } |
|---|
| 651 | n/a | } |
|---|
| 652 | n/a | /* Otherwise, allocate space to make them consecutive. */ |
|---|
| 653 | n/a | else |
|---|
| 654 | n/a | { |
|---|
| 655 | n/a | char *a = alloca (16); |
|---|
| 656 | n/a | int j; |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | avalue[i] = a; |
|---|
| 659 | n/a | for (j = 0; j < n; j++, a += 8) |
|---|
| 660 | n/a | { |
|---|
| 661 | n/a | if (SSE_CLASS_P (classes[j])) |
|---|
| 662 | n/a | memcpy (a, ®_args->sse[ssecount++], 8); |
|---|
| 663 | n/a | else |
|---|
| 664 | n/a | memcpy (a, ®_args->gpr[gprcount++], 8); |
|---|
| 665 | n/a | } |
|---|
| 666 | n/a | } |
|---|
| 667 | n/a | } |
|---|
| 668 | n/a | |
|---|
| 669 | n/a | /* Invoke the closure. */ |
|---|
| 670 | n/a | closure->fun (cif, rvalue, avalue, closure->user_data); |
|---|
| 671 | n/a | |
|---|
| 672 | n/a | /* Tell assembly how to perform return type promotions. */ |
|---|
| 673 | n/a | return ret; |
|---|
| 674 | n/a | } |
|---|
| 675 | n/a | |
|---|
| 676 | n/a | #endif /* __x86_64__ */ |
|---|