main: Added entry points for NamedRenderbufferStorage/Multisample
[mesa.git] / src / mapi / glapi / glapi_entrypoint.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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
17 * OR 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
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glapi_entrypoint.c
27 *
28 * Arch-specific code for manipulating GL API entrypoints (dispatch stubs).
29 */
30
31
32 #include "c11/threads.h"
33 #include "glapi/glapi_priv.h"
34 #include "u_execmem.h"
35
36
37 #ifdef USE_X86_ASM
38
39 #if defined( GLX_USE_TLS )
40 extern GLubyte gl_dispatch_functions_start[];
41 extern GLubyte gl_dispatch_functions_end[];
42 #else
43 extern const GLubyte gl_dispatch_functions_start[];
44 #endif
45
46 #endif /* USE_X86_ASM */
47
48
49 #if defined(DISPATCH_FUNCTION_SIZE)
50
51 _glapi_proc
52 get_entrypoint_address(unsigned int functionOffset)
53 {
54 return (_glapi_proc) (gl_dispatch_functions_start
55 + (DISPATCH_FUNCTION_SIZE * functionOffset));
56 }
57
58 #endif
59
60
61 #if defined(USE_X86_ASM)
62
63 /**
64 * Perform platform-specific GL API entry-point fixups.
65 */
66 static void
67 init_glapi_relocs( void )
68 {
69 #if defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
70 extern unsigned long _x86_get_dispatch(void);
71 char run_time_patch[] = {
72 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
73 };
74 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
75 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
76 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
77
78 *offset = _x86_get_dispatch();
79 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
80 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
81 curr_func += DISPATCH_FUNCTION_SIZE;
82 }
83 #endif
84 }
85
86
87 /**
88 * Generate a dispatch function (entrypoint) which jumps through
89 * the given slot number (offset) in the current dispatch table.
90 * We need assembly language in order to accomplish this.
91 */
92 _glapi_proc
93 generate_entrypoint(unsigned int functionOffset)
94 {
95 /* 32 is chosen as something of a magic offset. For x86, the dispatch
96 * at offset 32 is the first one where the offset in the
97 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
98 */
99 const GLubyte * const template_func = gl_dispatch_functions_start
100 + (DISPATCH_FUNCTION_SIZE * 32);
101 GLubyte * const code = (GLubyte *) u_execmem_alloc(DISPATCH_FUNCTION_SIZE);
102
103
104 if ( code != NULL ) {
105 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
106 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
107 }
108
109 return (_glapi_proc) code;
110 }
111
112
113 /**
114 * This function inserts a new dispatch offset into the assembly language
115 * stub that was generated with the preceeding function.
116 */
117 void
118 fill_in_entrypoint_offset(_glapi_proc entrypoint, unsigned int offset)
119 {
120 GLubyte * const code = (GLubyte *) entrypoint;
121
122 #if defined(GLX_USE_TLS)
123 *((unsigned int *)(code + 8)) = 4 * offset;
124 #else
125 *((unsigned int *)(code + 11)) = 4 * offset;
126 *((unsigned int *)(code + 22)) = 4 * offset;
127 #endif
128 }
129
130
131 #elif defined(USE_SPARC_ASM)
132
133 extern void __glapi_sparc_icache_flush(unsigned int *);
134
135 static void
136 init_glapi_relocs( void )
137 {
138 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
139 static const unsigned int template[] = {
140 #ifdef GLX_USE_TLS
141 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
142 0x8730e00a, /* srl %g3, 10, %g3 */
143 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
144 #ifdef __arch64__
145 0xc259c002, /* ldx [%g7 + %g2], %g1 */
146 0xc2584003, /* ldx [%g1 + %g3], %g1 */
147 #else
148 0xc201c002, /* ld [%g7 + %g2], %g1 */
149 0xc2004003, /* ld [%g1 + %g3], %g1 */
150 #endif
151 0x81c04000, /* jmp %g1 */
152 0x01000000, /* nop */
153 #else
154 #ifdef __arch64__
155 0x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */
156 0x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */
157 0x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */
158 0x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */
159 0x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */
160 0x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */
161 0xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */
162 #else
163 0x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */
164 0x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */
165 0xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
166 #endif
167 0x80a06000, /* --> cmp %g1, 0 */
168 0x02800005, /* --> be +4*5 */
169 0x01000000, /* --> nop */
170 #ifdef __arch64__
171 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
172 #else
173 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
174 #endif
175 0x81c04000, /* --> jmp %g1 */
176 0x01000000, /* --> nop */
177 #ifdef __arch64__
178 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
179 #else
180 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
181 #endif
182 0xa0100003, /* --> mov %g3, %l0 */
183 0x40000000, /* --> call _glapi_get_dispatch */
184 0x01000000, /* --> nop */
185 0x82100008, /* --> mov %o0, %g1 */
186 0x86100010, /* --> mov %l0, %g3 */
187 0x10bffff7, /* --> ba -4*9 */
188 0x81e80000, /* --> restore */
189 #endif
190 };
191 #ifdef GLX_USE_TLS
192 extern unsigned int __glapi_sparc_tls_stub;
193 extern unsigned long __glapi_sparc_get_dispatch(void);
194 unsigned int *code = &__glapi_sparc_tls_stub;
195 unsigned long dispatch = __glapi_sparc_get_dispatch();
196 #else
197 extern unsigned int __glapi_sparc_pthread_stub;
198 unsigned int *code = &__glapi_sparc_pthread_stub;
199 unsigned long dispatch = (unsigned long) &_glapi_Dispatch;
200 unsigned long call_dest = (unsigned long ) &_glapi_get_dispatch;
201 int idx;
202 #endif
203
204 #ifdef GLX_USE_TLS
205 code[0] = template[0] | (dispatch >> 10);
206 code[1] = template[1];
207 __glapi_sparc_icache_flush(&code[0]);
208 code[2] = template[2] | (dispatch & 0x3ff);
209 code[3] = template[3];
210 __glapi_sparc_icache_flush(&code[2]);
211 code[4] = template[4];
212 code[5] = template[5];
213 __glapi_sparc_icache_flush(&code[4]);
214 code[6] = template[6];
215 __glapi_sparc_icache_flush(&code[6]);
216 #else
217 #if defined(__arch64__)
218 code[0] = template[0] | (dispatch >> (32 + 10));
219 code[1] = template[1] | ((dispatch & 0xffffffff) >> 10);
220 __glapi_sparc_icache_flush(&code[0]);
221 code[2] = template[2] | ((dispatch >> 32) & 0x3ff);
222 code[3] = template[3];
223 __glapi_sparc_icache_flush(&code[2]);
224 code[4] = template[4];
225 code[5] = template[5];
226 __glapi_sparc_icache_flush(&code[4]);
227 code[6] = template[6] | (dispatch & 0x3ff);
228 idx = 7;
229 #else
230 code[0] = template[0] | (dispatch >> 10);
231 code[1] = template[1];
232 __glapi_sparc_icache_flush(&code[0]);
233 code[2] = template[2] | (dispatch & 0x3ff);
234 idx = 3;
235 #endif
236 code[idx + 0] = template[idx + 0];
237 __glapi_sparc_icache_flush(&code[idx - 1]);
238 code[idx + 1] = template[idx + 1];
239 code[idx + 2] = template[idx + 2];
240 __glapi_sparc_icache_flush(&code[idx + 1]);
241 code[idx + 3] = template[idx + 3];
242 code[idx + 4] = template[idx + 4];
243 __glapi_sparc_icache_flush(&code[idx + 3]);
244 code[idx + 5] = template[idx + 5];
245 code[idx + 6] = template[idx + 6];
246 __glapi_sparc_icache_flush(&code[idx + 5]);
247 code[idx + 7] = template[idx + 7];
248 code[idx + 8] = template[idx + 8] |
249 (((call_dest - ((unsigned long) &code[idx + 8]))
250 >> 2) & 0x3fffffff);
251 __glapi_sparc_icache_flush(&code[idx + 7]);
252 code[idx + 9] = template[idx + 9];
253 code[idx + 10] = template[idx + 10];
254 __glapi_sparc_icache_flush(&code[idx + 9]);
255 code[idx + 11] = template[idx + 11];
256 code[idx + 12] = template[idx + 12];
257 __glapi_sparc_icache_flush(&code[idx + 11]);
258 code[idx + 13] = template[idx + 13];
259 __glapi_sparc_icache_flush(&code[idx + 13]);
260 #endif
261 #endif
262 }
263
264
265 _glapi_proc
266 generate_entrypoint(GLuint functionOffset)
267 {
268 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
269 static const unsigned int template[] = {
270 0x07000000, /* sethi %hi(0), %g3 */
271 0x8210000f, /* mov %o7, %g1 */
272 0x40000000, /* call */
273 0x9e100001, /* mov %g1, %o7 */
274 };
275 #ifdef GLX_USE_TLS
276 extern unsigned int __glapi_sparc_tls_stub;
277 unsigned long call_dest = (unsigned long ) &__glapi_sparc_tls_stub;
278 #else
279 extern unsigned int __glapi_sparc_pthread_stub;
280 unsigned long call_dest = (unsigned long ) &__glapi_sparc_pthread_stub;
281 #endif
282 unsigned int *code = (unsigned int *) u_execmem_alloc(sizeof(template));
283 if (code) {
284 code[0] = template[0] | (functionOffset & 0x3fffff);
285 code[1] = template[1];
286 __glapi_sparc_icache_flush(&code[0]);
287 code[2] = template[2] |
288 (((call_dest - ((unsigned long) &code[2]))
289 >> 2) & 0x3fffffff);
290 code[3] = template[3];
291 __glapi_sparc_icache_flush(&code[2]);
292 }
293 return (_glapi_proc) code;
294 #endif
295 }
296
297
298 void
299 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
300 {
301 unsigned int *code = (unsigned int *) entrypoint;
302
303 code[0] &= ~0x3fffff;
304 code[0] |= (offset * sizeof(void *)) & 0x3fffff;
305 __glapi_sparc_icache_flush(&code[0]);
306 }
307
308
309 #else /* USE_*_ASM */
310
311 static void
312 init_glapi_relocs( void )
313 {
314 }
315
316
317 _glapi_proc
318 generate_entrypoint(GLuint functionOffset)
319 {
320 (void) functionOffset;
321 return NULL;
322 }
323
324
325 void
326 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
327 {
328 /* an unimplemented architecture */
329 (void) entrypoint;
330 (void) offset;
331 }
332
333 #endif /* USE_*_ASM */
334
335
336 void
337 init_glapi_relocs_once( void )
338 {
339 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
340 static once_flag flag = ONCE_FLAG_INIT;
341 call_once(&flag, init_glapi_relocs);
342 #endif
343 }