2 * Mesa 3-D graphics library
4 * Copyright (C) 2017 Red Hat
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Ben Crocker <bcrocker@redhat.com>
28 #ifdef HAVE_FUNC_ATTRIBUTE_VISIBILITY
29 #define HIDDEN __attribute__((visibility("hidden")))
34 // NOTE: These must be powers of two:
35 #define PPC64LE_ENTRY_SIZE 64
36 #define PPC64LE_PAGE_ALIGN 65536
37 #if ((PPC64LE_ENTRY_SIZE & (PPC64LE_ENTRY_SIZE - 1)) != 0)
38 #error PPC64LE_ENTRY_SIZE must be a power of two!
40 #if ((PPC64LE_PAGE_ALIGN & (PPC64LE_PAGE_ALIGN - 1)) != 0)
41 #error PPC64LE_PAGE_ALIGN must be a power of two!
45 ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE
) "\n"
46 "ppc64le_entry_start:");
48 #define STUB_ASM_ENTRY(func) \
50 ".type " func ", @function\n" \
51 ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n" \
53 " addis 2, 12, .TOC.-" func "@ha\n\t" \
54 " addi 2, 2, .TOC.-" func "@l\n\t" \
55 " .localentry " func ", .-" func "\n\t"
57 #define STUB_ASM_CODE(slot) \
58 " addis 11, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t" \
59 " ld 11, " ENTRY_CURRENT_TABLE "@got@tprel@l(11)\n\t" \
60 " add 11, 11," ENTRY_CURRENT_TABLE "@tls\n\t" \
62 " ld 12, " slot "*8(11)\n\t" \
66 #define MAPI_TMP_STUB_ASM_GCC
69 #ifndef MAPI_MODE_BRIDGE
72 #include "u_execmem.h"
75 entry_patch_public(void)
80 ppc64le_entry_start
[] HIDDEN
;
83 entry_get_public(int slot
)
85 return (mapi_func
) (ppc64le_entry_start
+ slot
* PPC64LE_ENTRY_SIZE
);
90 __asm__("ppc64le_dispatch_tls:\n\t"
91 " addis 3, 2, " ENTRY_CURRENT_TABLE
"@got@tprel@ha\n\t"
92 " ld 3, " ENTRY_CURRENT_TABLE
"@got@tprel@l(3)\n\t"
96 extern uint64_t ppc64le_dispatch_tls();
98 static const uint32_t code_templ
[] = {
99 // This should be functionally the same code as would be generated from
100 // the STUB_ASM_CODE macro, but defined as a buffer.
101 // This is used to generate new dispatch stubs. Mesa will copy this
102 // data to the dispatch stub, and then it will patch the slot number and
103 // any addresses that it needs to.
104 // NOTE!!! NOTE!!! NOTE!!!
105 // This representation is correct for both little- and big-endian systems.
106 // However, more work needs to be done for big-endian Linux because it
107 // adheres to an older, AIX-compatible ABI that uses function descriptors.
109 0x7C0802A6, // <ENTRY+00>: mflr 0
110 0xF8010010, // <ENTRY+04>: std 0, 16(1)
111 0xE96C0028, // <ENTRY+08>: ld 11, 9000f-1000b+0(12)
112 0x7D6B6A14, // <ENTRY+12>: add 11, 11, 13
113 0xE96B0000, // <ENTRY+16>: ld 11, 0(11)
114 0xE80C0030, // <ENTRY+20>: ld 0, 9000f-1000b+8(12)
115 0x7D8B002A, // <ENTRY+24>: ldx 12, 11, 0
116 0x7D8903A6, // <ENTRY+28>: mtctr 12
117 0x4E800420, // <ENTRY+32>: bctr
118 0x60000000, // <ENTRY+36>: nop
120 0, 0, // <ENTRY+40>: .quad _glapi_tls_Dispatch
121 0, 0 // <ENTRY+48>: .quad <slot>*8
123 static const uint64_t TEMPLATE_OFFSET_TLS_ADDR
= sizeof(code_templ
) - 2*8;
124 static const uint64_t TEMPLATE_OFFSET_SLOT
= sizeof(code_templ
) - 1*8;
127 entry_patch(mapi_func entry
, int slot
)
129 char *code
= (char *) entry
;
130 *((uint64_t *) (code
+ TEMPLATE_OFFSET_TLS_ADDR
)) = ppc64le_dispatch_tls();
131 *((uint64_t *) (code
+ TEMPLATE_OFFSET_SLOT
)) = slot
* sizeof(mapi_func
);
135 entry_generate(int slot
)
140 code
= u_execmem_alloc(sizeof(code_templ
));
144 memcpy(code
, code_templ
, sizeof(code_templ
));
146 entry
= (mapi_func
) code
;
147 entry_patch(entry
, slot
);
152 #endif /* MAPI_MODE_BRIDGE */