xref: /aosp_15_r20/external/compiler-rt/lib/builtins/trampoline_setup.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* ===----- trampoline_setup.c - Implement __trampoline_setup -------------===
2*7c3d14c8STreehugger Robot  *
3*7c3d14c8STreehugger Robot  *                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot  *
5*7c3d14c8STreehugger Robot  * This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot  * Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot  *
8*7c3d14c8STreehugger Robot  * ===----------------------------------------------------------------------===
9*7c3d14c8STreehugger Robot  */
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot #include "int_lib.h"
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot extern void __clear_cache(void* start, void* end);
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot /*
16*7c3d14c8STreehugger Robot  * The ppc compiler generates calls to __trampoline_setup() when creating
17*7c3d14c8STreehugger Robot  * trampoline functions on the stack for use with nested functions.
18*7c3d14c8STreehugger Robot  * This function creates a custom 40-byte trampoline function on the stack
19*7c3d14c8STreehugger Robot  * which loads r11 with a pointer to the outer function's locals
20*7c3d14c8STreehugger Robot  * and then jumps to the target nested function.
21*7c3d14c8STreehugger Robot  */
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot #if __ppc__ && !defined(__powerpc64__)
24*7c3d14c8STreehugger Robot COMPILER_RT_ABI void
__trampoline_setup(uint32_t * trampOnStack,int trampSizeAllocated,const void * realFunc,void * localsPtr)25*7c3d14c8STreehugger Robot __trampoline_setup(uint32_t* trampOnStack, int trampSizeAllocated,
26*7c3d14c8STreehugger Robot                    const void* realFunc, void* localsPtr)
27*7c3d14c8STreehugger Robot {
28*7c3d14c8STreehugger Robot     /* should never happen, but if compiler did not allocate */
29*7c3d14c8STreehugger Robot     /* enough space on stack for the trampoline, abort */
30*7c3d14c8STreehugger Robot     if ( trampSizeAllocated < 40 )
31*7c3d14c8STreehugger Robot         compilerrt_abort();
32*7c3d14c8STreehugger Robot 
33*7c3d14c8STreehugger Robot     /* create trampoline */
34*7c3d14c8STreehugger Robot     trampOnStack[0] = 0x7c0802a6;    /* mflr r0 */
35*7c3d14c8STreehugger Robot     trampOnStack[1] = 0x4800000d;    /* bl Lbase */
36*7c3d14c8STreehugger Robot     trampOnStack[2] = (uint32_t)realFunc;
37*7c3d14c8STreehugger Robot     trampOnStack[3] = (uint32_t)localsPtr;
38*7c3d14c8STreehugger Robot     trampOnStack[4] = 0x7d6802a6;    /* Lbase: mflr r11 */
39*7c3d14c8STreehugger Robot     trampOnStack[5] = 0x818b0000;    /* lwz    r12,0(r11) */
40*7c3d14c8STreehugger Robot     trampOnStack[6] = 0x7c0803a6;    /* mtlr r0 */
41*7c3d14c8STreehugger Robot     trampOnStack[7] = 0x7d8903a6;    /* mtctr r12 */
42*7c3d14c8STreehugger Robot     trampOnStack[8] = 0x816b0004;    /* lwz    r11,4(r11) */
43*7c3d14c8STreehugger Robot     trampOnStack[9] = 0x4e800420;    /* bctr */
44*7c3d14c8STreehugger Robot 
45*7c3d14c8STreehugger Robot     /* clear instruction cache */
46*7c3d14c8STreehugger Robot     __clear_cache(trampOnStack, &trampOnStack[10]);
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot #endif /* __ppc__ && !defined(__powerpc64__) */
49