xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/trampoline_setup.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 
19*bdd1243dSDimitry 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 }
43*bdd1243dSDimitry Andric #endif // __powerpc__ && !defined(__powerpc64__)
44