10b57cec5SDimitry Andric //===----- trampoline_setup.c - Implement __trampoline_setup -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "int_lib.h" 100b57cec5SDimitry Andric 110b57cec5SDimitry Andric extern void __clear_cache(void *start, void *end); 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric // The ppc compiler generates calls to __trampoline_setup() when creating 140b57cec5SDimitry Andric // trampoline functions on the stack for use with nested functions. 150b57cec5SDimitry Andric // This function creates a custom 40-byte trampoline function on the stack 160b57cec5SDimitry Andric // which loads r11 with a pointer to the outer function's locals 170b57cec5SDimitry Andric // and then jumps to the target nested function. 180b57cec5SDimitry Andric 19bdd1243dSDimitry Andric #if __powerpc__ && !defined(__powerpc64__) 200b57cec5SDimitry Andric COMPILER_RT_ABI void __trampoline_setup(uint32_t *trampOnStack, 210b57cec5SDimitry Andric int trampSizeAllocated, 220b57cec5SDimitry Andric const void *realFunc, void *localsPtr) { 230b57cec5SDimitry Andric // should never happen, but if compiler did not allocate 240b57cec5SDimitry Andric // enough space on stack for the trampoline, abort 250b57cec5SDimitry Andric if (trampSizeAllocated < 40) 260b57cec5SDimitry Andric compilerrt_abort(); 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric // create trampoline 290b57cec5SDimitry Andric trampOnStack[0] = 0x7c0802a6; // mflr r0 300b57cec5SDimitry Andric trampOnStack[1] = 0x4800000d; // bl Lbase 310b57cec5SDimitry Andric trampOnStack[2] = (uint32_t)realFunc; 320b57cec5SDimitry Andric trampOnStack[3] = (uint32_t)localsPtr; 330b57cec5SDimitry Andric trampOnStack[4] = 0x7d6802a6; // Lbase: mflr r11 340b57cec5SDimitry Andric trampOnStack[5] = 0x818b0000; // lwz r12,0(r11) 350b57cec5SDimitry Andric trampOnStack[6] = 0x7c0803a6; // mtlr r0 360b57cec5SDimitry Andric trampOnStack[7] = 0x7d8903a6; // mtctr r12 370b57cec5SDimitry Andric trampOnStack[8] = 0x816b0004; // lwz r11,4(r11) 380b57cec5SDimitry Andric trampOnStack[9] = 0x4e800420; // bctr 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric // clear instruction cache 410b57cec5SDimitry Andric __clear_cache(trampOnStack, &trampOnStack[10]); 420b57cec5SDimitry Andric } 43bdd1243dSDimitry Andric #endif // __powerpc__ && !defined(__powerpc64__) 44*36b606aeSDimitry Andric 45*36b606aeSDimitry Andric // The AArch64 compiler generates calls to __trampoline_setup() when creating 46*36b606aeSDimitry Andric // trampoline functions on the stack for use with nested functions. 47*36b606aeSDimitry Andric // This function creates a custom 36-byte trampoline function on the stack 48*36b606aeSDimitry Andric // which loads x18 with a pointer to the outer function's locals 49*36b606aeSDimitry Andric // and then jumps to the target nested function. 50*36b606aeSDimitry Andric // Note: x18 is a reserved platform register on Windows and macOS. 51*36b606aeSDimitry Andric 52*36b606aeSDimitry Andric #if defined(__aarch64__) && defined(__ELF__) 53*36b606aeSDimitry Andric COMPILER_RT_ABI void __trampoline_setup(uint32_t *trampOnStack, 54*36b606aeSDimitry Andric int trampSizeAllocated, 55*36b606aeSDimitry Andric const void *realFunc, void *localsPtr) { 56*36b606aeSDimitry Andric // This should never happen, but if compiler did not allocate 57*36b606aeSDimitry Andric // enough space on stack for the trampoline, abort. 58*36b606aeSDimitry Andric if (trampSizeAllocated < 36) 59*36b606aeSDimitry Andric compilerrt_abort(); 60*36b606aeSDimitry Andric 61*36b606aeSDimitry Andric // create trampoline 62*36b606aeSDimitry Andric // Load realFunc into x17. mov/movk 16 bits at a time. 63*36b606aeSDimitry Andric trampOnStack[0] = 64*36b606aeSDimitry Andric 0xd2800000u | ((((uint64_t)realFunc >> 0) & 0xffffu) << 5) | 0x11; 65*36b606aeSDimitry Andric trampOnStack[1] = 66*36b606aeSDimitry Andric 0xf2a00000u | ((((uint64_t)realFunc >> 16) & 0xffffu) << 5) | 0x11; 67*36b606aeSDimitry Andric trampOnStack[2] = 68*36b606aeSDimitry Andric 0xf2c00000u | ((((uint64_t)realFunc >> 32) & 0xffffu) << 5) | 0x11; 69*36b606aeSDimitry Andric trampOnStack[3] = 70*36b606aeSDimitry Andric 0xf2e00000u | ((((uint64_t)realFunc >> 48) & 0xffffu) << 5) | 0x11; 71*36b606aeSDimitry Andric // Load localsPtr into x18 72*36b606aeSDimitry Andric trampOnStack[4] = 73*36b606aeSDimitry Andric 0xd2800000u | ((((uint64_t)localsPtr >> 0) & 0xffffu) << 5) | 0x12; 74*36b606aeSDimitry Andric trampOnStack[5] = 75*36b606aeSDimitry Andric 0xf2a00000u | ((((uint64_t)localsPtr >> 16) & 0xffffu) << 5) | 0x12; 76*36b606aeSDimitry Andric trampOnStack[6] = 77*36b606aeSDimitry Andric 0xf2c00000u | ((((uint64_t)localsPtr >> 32) & 0xffffu) << 5) | 0x12; 78*36b606aeSDimitry Andric trampOnStack[7] = 79*36b606aeSDimitry Andric 0xf2e00000u | ((((uint64_t)localsPtr >> 48) & 0xffffu) << 5) | 0x12; 80*36b606aeSDimitry Andric trampOnStack[8] = 0xd61f0220; // br x17 81*36b606aeSDimitry Andric 82*36b606aeSDimitry Andric // Clear instruction cache. 83*36b606aeSDimitry Andric __clear_cache(trampOnStack, &trampOnStack[9]); 84*36b606aeSDimitry Andric } 85*36b606aeSDimitry Andric #endif // defined(__aarch64__) && !defined(__APPLE__) && !defined(_WIN64) 86