xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/trampoline_setup.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===----- trampoline_setup.c - Implement __trampoline_setup -------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "int_lib.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric extern void __clear_cache(void *start, void *end);
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric // The ppc compiler generates calls to __trampoline_setup() when creating
14*0b57cec5SDimitry Andric // trampoline functions on the stack for use with nested functions.
15*0b57cec5SDimitry Andric // This function creates a custom 40-byte trampoline function on the stack
16*0b57cec5SDimitry Andric // which loads r11 with a pointer to the outer function's locals
17*0b57cec5SDimitry Andric // and then jumps to the target nested function.
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric #if __ppc__ && !defined(__powerpc64__)
20*0b57cec5SDimitry Andric COMPILER_RT_ABI void __trampoline_setup(uint32_t *trampOnStack,
21*0b57cec5SDimitry Andric                                         int trampSizeAllocated,
22*0b57cec5SDimitry Andric                                         const void *realFunc, void *localsPtr) {
23*0b57cec5SDimitry Andric   // should never happen, but if compiler did not allocate
24*0b57cec5SDimitry Andric   // enough space on stack for the trampoline, abort
25*0b57cec5SDimitry Andric   if (trampSizeAllocated < 40)
26*0b57cec5SDimitry Andric     compilerrt_abort();
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric   // create trampoline
29*0b57cec5SDimitry Andric   trampOnStack[0] = 0x7c0802a6; // mflr r0
30*0b57cec5SDimitry Andric   trampOnStack[1] = 0x4800000d; // bl Lbase
31*0b57cec5SDimitry Andric   trampOnStack[2] = (uint32_t)realFunc;
32*0b57cec5SDimitry Andric   trampOnStack[3] = (uint32_t)localsPtr;
33*0b57cec5SDimitry Andric   trampOnStack[4] = 0x7d6802a6; // Lbase: mflr r11
34*0b57cec5SDimitry Andric   trampOnStack[5] = 0x818b0000; // lwz    r12,0(r11)
35*0b57cec5SDimitry Andric   trampOnStack[6] = 0x7c0803a6; // mtlr r0
36*0b57cec5SDimitry Andric   trampOnStack[7] = 0x7d8903a6; // mtctr r12
37*0b57cec5SDimitry Andric   trampOnStack[8] = 0x816b0004; // lwz    r11,4(r11)
38*0b57cec5SDimitry Andric   trampOnStack[9] = 0x4e800420; // bctr
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric   // clear instruction cache
41*0b57cec5SDimitry Andric   __clear_cache(trampOnStack, &trampOnStack[10]);
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric #endif // __ppc__ && !defined(__powerpc64__)
44