| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
sysv.S - Copyright (c) 1998, 2008, 2011 Red Hat, Inc. |
| 3 |
Copyright (c) 2011 Plausible Labs Cooperative, Inc. |
| 4 |
|
| 5 |
ARM Foreign Function Interface |
| 6 |
|
| 7 |
Permission is hereby granted, free of charge, to any person obtaining |
| 8 |
a copy of this software and associated documentation files (the |
| 9 |
``Software''), to deal in the Software without restriction, including |
| 10 |
without limitation the rights to use, copy, modify, merge, publish, |
| 11 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 12 |
permit persons to whom the Software is furnished to do so, subject to |
| 13 |
the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included |
| 16 |
in all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
| 19 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 |
DEALINGS IN THE SOFTWARE. |
| 26 |
----------------------------------------------------------------------- */ |
| 27 |
|
| 28 |
#ifdef __arm__ |
| 29 |
#define LIBFFI_ASM |
| 30 |
#include <fficonfig.h> |
| 31 |
#include <ffi.h> |
| 32 |
#include <ffi_cfi.h> |
| 33 |
#include "internal.h" |
| 34 |
|
| 35 |
/* GCC 4.8 provides __ARM_ARCH; construct it otherwise. */ |
| 36 |
#ifndef __ARM_ARCH |
| 37 |
# if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \ |
| 38 |
|| defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \ |
| 39 |
|| defined(__ARM_ARCH_7EM__) |
| 40 |
# define __ARM_ARCH 7 |
| 41 |
# elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \ |
| 42 |
|| defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) \ |
| 43 |
|| defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) \ |
| 44 |
|| defined(__ARM_ARCH_6M__) |
| 45 |
# define __ARM_ARCH 6 |
| 46 |
# elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) \ |
| 47 |
|| defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) \ |
| 48 |
|| defined(__ARM_ARCH_5TEJ__) |
| 49 |
# define __ARM_ARCH 5 |
| 50 |
# else |
| 51 |
# define __ARM_ARCH 4 |
| 52 |
# endif |
| 53 |
#endif |
| 54 |
|
| 55 |
/* Conditionally compile unwinder directives. */ |
| 56 |
#ifdef __ARM_EABI__ |
| 57 |
# define UNWIND(...) __VA_ARGS__ |
| 58 |
#else |
| 59 |
# define UNWIND(...) |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(HAVE_AS_CFI_PSEUDO_OP) && defined(__ARM_EABI__) |
| 63 |
.cfi_sections .debug_frame |
| 64 |
#endif |
| 65 |
|
| 66 |
#define CONCAT(a, b) CONCAT2(a, b) |
| 67 |
#define CONCAT2(a, b) a ## b |
| 68 |
|
| 69 |
#ifdef __USER_LABEL_PREFIX__ |
| 70 |
# define CNAME(X) CONCAT (__USER_LABEL_PREFIX__, X) |
| 71 |
#else |
| 72 |
# define CNAME(X) X |
| 73 |
#endif |
| 74 |
#ifdef __ELF__ |
| 75 |
# define SIZE(X) .size CNAME(X), . - CNAME(X) |
| 76 |
# define TYPE(X, Y) .type CNAME(X), Y |
| 77 |
#else |
| 78 |
# define SIZE(X) |
| 79 |
# define TYPE(X, Y) |
| 80 |
#endif |
| 81 |
|
| 82 |
#define ARM_FUNC_START_LOCAL(name) \ |
| 83 |
.align 3; \ |
| 84 |
TYPE(CNAME(name), %function); \ |
| 85 |
CNAME(name): |
| 86 |
|
| 87 |
#define ARM_FUNC_START(name) \ |
| 88 |
.globl CNAME(name); \ |
| 89 |
FFI_HIDDEN(CNAME(name)); \ |
| 90 |
ARM_FUNC_START_LOCAL(name) |
| 91 |
|
| 92 |
#define ARM_FUNC_END(name) \ |
| 93 |
SIZE(name) |
| 94 |
|
| 95 |
.text |
| 96 |
.syntax unified |
| 97 |
#if defined(_WIN32) |
| 98 |
/* Windows on ARM is thumb-only */ |
| 99 |
.thumb |
| 100 |
#else |
| 101 |
/* Keep the assembly in ARM mode in other cases, for simplicity |
| 102 |
* (to avoid interworking issues). */ |
| 103 |
#undef __thumb__ |
| 104 |
.arm |
| 105 |
#endif |
| 106 |
|
| 107 |
/* Aid in defining a jump table with 8 bytes between entries. */ |
| 108 |
#ifdef __thumb__ |
| 109 |
/* In thumb mode, instructions can be shorter than expected in arm mode, so |
| 110 |
* we need to align the start of each case. */ |
| 111 |
# define E(index) .align 3 |
| 112 |
#elif defined(__clang__) |
| 113 |
/* ??? The clang assembler doesn't handle .if with symbolic expressions. */ |
| 114 |
# define E(index) |
| 115 |
#else |
| 116 |
# define E(index) \ |
| 117 |
.if . - 0b - 8*index; \ |
| 118 |
.error "type table out of sync"; \ |
| 119 |
.endif |
| 120 |
#endif |
| 121 |
|
| 122 |
|
| 123 |
#ifndef __clang__ |
| 124 |
/* We require interworking on LDM, which implies ARMv5T, |
| 125 |
which implies the existance of BLX. */ |
| 126 |
.arch armv5t |
| 127 |
#endif |
| 128 |
|
| 129 |
/* Note that we use STC and LDC to encode VFP instructions, |
| 130 |
so that we do not need ".fpu vfp", nor get that added to |
| 131 |
the object file attributes. These will not be executed |
| 132 |
unless the FFI_VFP abi is used. */ |
| 133 |
|
| 134 |
@ r0: stack |
| 135 |
@ r1: frame |
| 136 |
@ r2: fn |
| 137 |
@ r3: vfp_used |
| 138 |
|
| 139 |
ARM_FUNC_START(ffi_call_VFP) |
| 140 |
UNWIND(.fnstart) |
| 141 |
cfi_startproc |
| 142 |
|
| 143 |
cmp r3, #3 @ load only d0 if possible |
| 144 |
ite le |
| 145 |
ldcle p11, cr0, [r0] @ vldrle d0, [r0] |
| 146 |
ldcgt p11, cr0, [r0], {16} @ vldmgt r0, {d0-d7} |
| 147 |
add r0, r0, #64 @ discard the vfp register args |
| 148 |
/* FALLTHRU */ |
| 149 |
ARM_FUNC_END(ffi_call_VFP) |
| 150 |
|
| 151 |
ARM_FUNC_START(ffi_call_SYSV) |
| 152 |
stm r1, {fp, lr} |
| 153 |
mov fp, r1 |
| 154 |
|
| 155 |
@ This is a bit of a lie wrt the origin of the unwind info, but |
| 156 |
@ now we've got the usual frame pointer and two saved registers. |
| 157 |
UNWIND(.save {fp,lr}) |
| 158 |
UNWIND(.setfp fp, sp) |
| 159 |
cfi_def_cfa(fp, 8) |
| 160 |
cfi_rel_offset(fp, 0) |
| 161 |
cfi_rel_offset(lr, 4) |
| 162 |
|
| 163 |
mov sp, r0 @ install the stack pointer |
| 164 |
mov lr, r2 @ move the fn pointer out of the way |
| 165 |
ldr ip, [fp, #16] @ install the static chain |
| 166 |
ldmia sp!, {r0-r3} @ move first 4 parameters in registers. |
| 167 |
blx lr @ call fn |
| 168 |
|
| 169 |
@ Load r2 with the pointer to storage for the return value |
| 170 |
@ Load r3 with the return type code |
| 171 |
ldr r2, [fp, #8] |
| 172 |
ldr r3, [fp, #12] |
| 173 |
|
| 174 |
@ Deallocate the stack with the arguments. |
| 175 |
mov sp, fp |
| 176 |
cfi_def_cfa_register(sp) |
| 177 |
|
| 178 |
@ Store values stored in registers. |
| 179 |
#ifndef __thumb__ |
| 180 |
.align 3 |
| 181 |
add pc, pc, r3, lsl #3 |
| 182 |
nop |
| 183 |
#else |
| 184 |
adr ip, 0f |
| 185 |
add ip, ip, r3, lsl #3 |
| 186 |
mov pc, ip |
| 187 |
.align 3 |
| 188 |
#endif |
| 189 |
0: |
| 190 |
E(ARM_TYPE_VFP_S) |
| 191 |
stc p10, cr0, [r2] @ vstr s0, [r2] |
| 192 |
pop {fp,pc} |
| 193 |
E(ARM_TYPE_VFP_D) |
| 194 |
stc p11, cr0, [r2] @ vstr d0, [r2] |
| 195 |
pop {fp,pc} |
| 196 |
E(ARM_TYPE_VFP_N) |
| 197 |
stc p11, cr0, [r2], {8} @ vstm r2, {d0-d3} |
| 198 |
pop {fp,pc} |
| 199 |
E(ARM_TYPE_INT64) |
| 200 |
str r1, [r2, #4] |
| 201 |
nop |
| 202 |
E(ARM_TYPE_INT) |
| 203 |
str r0, [r2] |
| 204 |
pop {fp,pc} |
| 205 |
E(ARM_TYPE_VOID) |
| 206 |
pop {fp,pc} |
| 207 |
nop |
| 208 |
E(ARM_TYPE_STRUCT) |
| 209 |
pop {fp,pc} |
| 210 |
|
| 211 |
cfi_endproc |
| 212 |
UNWIND(.fnend) |
| 213 |
ARM_FUNC_END(ffi_call_SYSV) |
| 214 |
|
| 215 |
#if FFI_CLOSURES |
| 216 |
|
| 217 |
/* |
| 218 |
int ffi_closure_inner_* (cif, fun, user_data, frame) |
| 219 |
*/ |
| 220 |
|
| 221 |
ARM_FUNC_START(ffi_go_closure_SYSV) |
| 222 |
cfi_startproc |
| 223 |
stmdb sp!, {r0-r3} @ save argument regs |
| 224 |
cfi_adjust_cfa_offset(16) |
| 225 |
ldr r0, [ip, #4] @ load cif |
| 226 |
ldr r1, [ip, #8] @ load fun |
| 227 |
mov r2, ip @ load user_data |
| 228 |
b 0f |
| 229 |
cfi_endproc |
| 230 |
ARM_FUNC_END(ffi_go_closure_SYSV) |
| 231 |
|
| 232 |
ARM_FUNC_START(ffi_closure_SYSV) |
| 233 |
UNWIND(.fnstart) |
| 234 |
cfi_startproc |
| 235 |
#ifdef _WIN32 |
| 236 |
ldmfd sp!, {r0, ip} @ restore fp (r0 is used for stack alignment) |
| 237 |
#endif |
| 238 |
stmdb sp!, {r0-r3} @ save argument regs |
| 239 |
cfi_adjust_cfa_offset(16) |
| 240 |
|
| 241 |
#if FFI_EXEC_TRAMPOLINE_TABLE |
| 242 |
ldr ip, [ip] @ ip points to the config page, dereference to get the ffi_closure* |
| 243 |
#endif |
| 244 |
ldr r0, [ip, #FFI_TRAMPOLINE_CLOSURE_OFFSET] @ load cif |
| 245 |
ldr r1, [ip, #FFI_TRAMPOLINE_CLOSURE_OFFSET+4] @ load fun |
| 246 |
ldr r2, [ip, #FFI_TRAMPOLINE_CLOSURE_OFFSET+8] @ load user_data |
| 247 |
0: |
| 248 |
add ip, sp, #16 @ compute entry sp |
| 249 |
sub sp, sp, #64+32 @ allocate frame |
| 250 |
cfi_adjust_cfa_offset(64+32) |
| 251 |
stmdb sp!, {ip,lr} |
| 252 |
|
| 253 |
/* Remember that EABI unwind info only applies at call sites. |
| 254 |
We need do nothing except note the save of the stack pointer |
| 255 |
and the link registers. */ |
| 256 |
UNWIND(.save {sp,lr}) |
| 257 |
cfi_adjust_cfa_offset(8) |
| 258 |
cfi_rel_offset(lr, 4) |
| 259 |
|
| 260 |
add r3, sp, #8 @ load frame |
| 261 |
bl CNAME(ffi_closure_inner_SYSV) |
| 262 |
|
| 263 |
@ Load values returned in registers. |
| 264 |
add r2, sp, #8+64 @ load result |
| 265 |
adr r3, CNAME(ffi_closure_ret) |
| 266 |
#ifndef __thumb__ |
| 267 |
add pc, r3, r0, lsl #3 |
| 268 |
#else |
| 269 |
add r3, r3, r0, lsl #3 |
| 270 |
mov pc, r3 |
| 271 |
#endif |
| 272 |
cfi_endproc |
| 273 |
UNWIND(.fnend) |
| 274 |
ARM_FUNC_END(ffi_closure_SYSV) |
| 275 |
|
| 276 |
ARM_FUNC_START(ffi_go_closure_VFP) |
| 277 |
cfi_startproc |
| 278 |
stmdb sp!, {r0-r3} @ save argument regs |
| 279 |
cfi_adjust_cfa_offset(16) |
| 280 |
ldr r0, [ip, #4] @ load cif |
| 281 |
ldr r1, [ip, #8] @ load fun |
| 282 |
mov r2, ip @ load user_data |
| 283 |
b 0f |
| 284 |
cfi_endproc |
| 285 |
ARM_FUNC_END(ffi_go_closure_VFP) |
| 286 |
|
| 287 |
ARM_FUNC_START(ffi_closure_VFP) |
| 288 |
UNWIND(.fnstart) |
| 289 |
cfi_startproc |
| 290 |
#ifdef _WIN32 |
| 291 |
ldmfd sp!, {r0, ip} @ restore fp (r0 is used for stack alignment) |
| 292 |
#endif |
| 293 |
stmdb sp!, {r0-r3} @ save argument regs |
| 294 |
cfi_adjust_cfa_offset(16) |
| 295 |
|
| 296 |
#if FFI_EXEC_TRAMPOLINE_TABLE |
| 297 |
ldr ip, [ip] @ ip points to the config page, dereference to get the ffi_closure* |
| 298 |
#endif |
| 299 |
ldr r0, [ip, #FFI_TRAMPOLINE_CLOSURE_OFFSET] @ load cif |
| 300 |
ldr r1, [ip, #FFI_TRAMPOLINE_CLOSURE_OFFSET+4] @ load fun |
| 301 |
ldr r2, [ip, #FFI_TRAMPOLINE_CLOSURE_OFFSET+8] @ load user_data |
| 302 |
0: |
| 303 |
add ip, sp, #16 |
| 304 |
sub sp, sp, #64+32 @ allocate frame |
| 305 |
cfi_adjust_cfa_offset(64+32) |
| 306 |
stc p11, cr0, [sp], {16} @ vstm sp, {d0-d7} |
| 307 |
stmdb sp!, {ip,lr} |
| 308 |
|
| 309 |
/* See above. */ |
| 310 |
UNWIND(.save {sp,lr}) |
| 311 |
cfi_adjust_cfa_offset(8) |
| 312 |
cfi_rel_offset(lr, 4) |
| 313 |
|
| 314 |
add r3, sp, #8 @ load frame |
| 315 |
bl CNAME(ffi_closure_inner_VFP) |
| 316 |
|
| 317 |
@ Load values returned in registers. |
| 318 |
add r2, sp, #8+64 @ load result |
| 319 |
adr r3, CNAME(ffi_closure_ret) |
| 320 |
#ifndef __thumb__ |
| 321 |
add pc, r3, r0, lsl #3 |
| 322 |
#else |
| 323 |
add r3, r3, r0, lsl #3 |
| 324 |
mov pc, r3 |
| 325 |
#endif |
| 326 |
cfi_endproc |
| 327 |
UNWIND(.fnend) |
| 328 |
ARM_FUNC_END(ffi_closure_VFP) |
| 329 |
|
| 330 |
/* Load values returned in registers for both closure entry points. |
| 331 |
Note that we use LDM with SP in the register set. This is deprecated |
| 332 |
by ARM, but not yet unpredictable. */ |
| 333 |
|
| 334 |
ARM_FUNC_START_LOCAL(ffi_closure_ret) |
| 335 |
cfi_startproc |
| 336 |
cfi_rel_offset(sp, 0) |
| 337 |
cfi_rel_offset(lr, 4) |
| 338 |
0: |
| 339 |
E(ARM_TYPE_VFP_S) |
| 340 |
ldc p10, cr0, [r2] @ vldr s0, [r2] |
| 341 |
b call_epilogue |
| 342 |
E(ARM_TYPE_VFP_D) |
| 343 |
ldc p11, cr0, [r2] @ vldr d0, [r2] |
| 344 |
b call_epilogue |
| 345 |
E(ARM_TYPE_VFP_N) |
| 346 |
ldc p11, cr0, [r2], {8} @ vldm r2, {d0-d3} |
| 347 |
b call_epilogue |
| 348 |
E(ARM_TYPE_INT64) |
| 349 |
ldr r1, [r2, #4] |
| 350 |
nop |
| 351 |
E(ARM_TYPE_INT) |
| 352 |
ldr r0, [r2] |
| 353 |
b call_epilogue |
| 354 |
E(ARM_TYPE_VOID) |
| 355 |
b call_epilogue |
| 356 |
nop |
| 357 |
E(ARM_TYPE_STRUCT) |
| 358 |
b call_epilogue |
| 359 |
call_epilogue: |
| 360 |
#ifndef __thumb__ |
| 361 |
ldm sp, {sp,pc} |
| 362 |
#else |
| 363 |
ldm sp, {ip,lr} |
| 364 |
mov sp, ip |
| 365 |
bx lr |
| 366 |
#endif |
| 367 |
cfi_endproc |
| 368 |
ARM_FUNC_END(ffi_closure_ret) |
| 369 |
|
| 370 |
#if defined(FFI_EXEC_STATIC_TRAMP) |
| 371 |
ARM_FUNC_START(ffi_closure_SYSV_alt) |
| 372 |
/* See the comments above trampoline_code_table. */ |
| 373 |
ldr ip, [sp, #4] /* Load closure in ip */ |
| 374 |
add sp, sp, 8 /* Restore the stack */ |
| 375 |
b CNAME(ffi_closure_SYSV) |
| 376 |
ARM_FUNC_END(ffi_closure_SYSV_alt) |
| 377 |
|
| 378 |
ARM_FUNC_START(ffi_closure_VFP_alt) |
| 379 |
/* See the comments above trampoline_code_table. */ |
| 380 |
ldr ip, [sp, #4] /* Load closure in ip */ |
| 381 |
add sp, sp, 8 /* Restore the stack */ |
| 382 |
b CNAME(ffi_closure_VFP) |
| 383 |
ARM_FUNC_END(ffi_closure_VFP_alt) |
| 384 |
|
| 385 |
/* |
| 386 |
* Below is the definition of the trampoline code table. Each element in |
| 387 |
* the code table is a trampoline. |
| 388 |
*/ |
| 389 |
/* |
| 390 |
* The trampoline uses register ip (r12). It saves the original value of ip |
| 391 |
* on the stack. |
| 392 |
* |
| 393 |
* The trampoline has two parameters - target code to jump to and data for |
| 394 |
* the target code. The trampoline extracts the parameters from its parameter |
| 395 |
* block (see tramp_table_map()). The trampoline saves the data address on |
| 396 |
* the stack. Finally, it jumps to the target code. |
| 397 |
* |
| 398 |
* The target code can choose to: |
| 399 |
* |
| 400 |
* - restore the value of ip |
| 401 |
* - load the data address in a register |
| 402 |
* - restore the stack pointer to what it was when the trampoline was invoked. |
| 403 |
*/ |
| 404 |
.align ARM_TRAMP_MAP_SHIFT |
| 405 |
ARM_FUNC_START(trampoline_code_table) |
| 406 |
.rept ARM_TRAMP_MAP_SIZE / ARM_TRAMP_SIZE |
| 407 |
sub sp, sp, #8 /* Make space on the stack */ |
| 408 |
str ip, [sp] /* Save ip on stack */ |
| 409 |
ldr ip, [pc, #4080] /* Copy data into ip */ |
| 410 |
str ip, [sp, #4] /* Save data on stack */ |
| 411 |
ldr pc, [pc, #4076] /* Copy code into PC */ |
| 412 |
.endr |
| 413 |
ARM_FUNC_END(trampoline_code_table) |
| 414 |
.align ARM_TRAMP_MAP_SHIFT |
| 415 |
#endif /* FFI_EXEC_STATIC_TRAMP */ |
| 416 |
|
| 417 |
#endif /* FFI_CLOSURES */ |
| 418 |
|
| 419 |
#if FFI_EXEC_TRAMPOLINE_TABLE |
| 420 |
|
| 421 |
#ifdef __MACH__ |
| 422 |
#include <mach/machine/vm_param.h> |
| 423 |
|
| 424 |
.align PAGE_MAX_SHIFT |
| 425 |
ARM_FUNC_START(ffi_closure_trampoline_table_page) |
| 426 |
.rept PAGE_MAX_SIZE / FFI_TRAMPOLINE_SIZE |
| 427 |
adr ip, #-PAGE_MAX_SIZE @ the config page is PAGE_MAX_SIZE behind the trampoline page |
| 428 |
sub ip, #8 @ account for pc bias |
| 429 |
ldr pc, [ip, #4] @ jump to ffi_closure_SYSV or ffi_closure_VFP |
| 430 |
.endr |
| 431 |
ARM_FUNC_END(ffi_closure_trampoline_table_page) |
| 432 |
#endif |
| 433 |
|
| 434 |
#elif defined(_WIN32) |
| 435 |
|
| 436 |
ARM_FUNC_START(ffi_arm_trampoline) |
| 437 |
0: adr ip, 0b |
| 438 |
stmdb sp!, {r0, ip} |
| 439 |
ldr pc, 1f |
| 440 |
1: .long 0 |
| 441 |
ARM_FUNC_END(ffi_arm_trampoline) |
| 442 |
|
| 443 |
#else |
| 444 |
|
| 445 |
ARM_FUNC_START(ffi_arm_trampoline) |
| 446 |
0: adr ip, 0b |
| 447 |
ldr pc, 1f |
| 448 |
1: .long 0 |
| 449 |
ARM_FUNC_END(ffi_arm_trampoline) |
| 450 |
|
| 451 |
#endif /* FFI_EXEC_TRAMPOLINE_TABLE */ |
| 452 |
#endif /* __arm__ */ |
| 453 |
|
| 454 |
#if defined __ELF__ && defined __linux__ |
| 455 |
.section .note.GNU-stack,"",%progbits |
| 456 |
#endif |