mapi/glapi: Generate sizeof() helpers instead of fixed sizes.
[mesa.git] / src / mapi / entry_ppc64le_tls.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2017 Red Hat
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Ben Crocker <bcrocker@redhat.com>
26 */
27
28 #ifdef HAVE_FUNC_ATTRIBUTE_VISIBILITY
29 #define HIDDEN __attribute__((visibility("hidden")))
30 #else
31 #define HIDDEN
32 #endif
33
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!
39 #endif
40 #if ((PPC64LE_PAGE_ALIGN & (PPC64LE_PAGE_ALIGN - 1)) != 0)
41 #error PPC64LE_PAGE_ALIGN must be a power of two!
42 #endif
43
44 __asm__(".text\n"
45 ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n"
46 "ppc64le_entry_start:");
47
48 #define STUB_ASM_ENTRY(func) \
49 ".globl " func "\n" \
50 ".type " func ", @function\n" \
51 ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n" \
52 func ":\n\t" \
53 " addis 2, 12, .TOC.-" func "@ha\n\t" \
54 " addi 2, 2, .TOC.-" func "@l\n\t" \
55 " .localentry " func ", .-" func "\n\t"
56
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" \
61 " ld 11, 0(11)\n\t" \
62 " ld 12, " slot "*8(11)\n\t" \
63 " mtctr 12\n\t" \
64 " bctr\n" \
65
66 #define MAPI_TMP_STUB_ASM_GCC
67 #include "mapi_tmp.h"
68
69 #ifndef MAPI_MODE_BRIDGE
70
71 #include <string.h>
72 #include "u_execmem.h"
73
74 void
75 entry_patch_public(void)
76 {
77 }
78
79 extern char
80 ppc64le_entry_start[] HIDDEN;
81
82 mapi_func
83 entry_get_public(int slot)
84 {
85 return (mapi_func) (ppc64le_entry_start + slot * PPC64LE_ENTRY_SIZE);
86 }
87
88 __asm__(".text\n");
89
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"
93 " blr\n"
94 );
95
96 extern uint64_t ppc64le_dispatch_tls();
97
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.
108 // 1000:
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
119 // 9000:
120 0, 0, // <ENTRY+40>: .quad _glapi_tls_Dispatch
121 0, 0 // <ENTRY+48>: .quad <slot>*8
122 };
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;
125
126 void
127 entry_patch(mapi_func entry, int slot)
128 {
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);
132 }
133
134 mapi_func
135 entry_generate(int slot)
136 {
137 char *code;
138 mapi_func entry;
139
140 code = u_execmem_alloc(sizeof(code_templ));
141 if (!code)
142 return NULL;
143
144 memcpy(code, code_templ, sizeof(code_templ));
145
146 entry = (mapi_func) code;
147 entry_patch(entry, slot);
148
149 return entry;
150 }
151
152 #endif /* MAPI_MODE_BRIDGE */