2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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
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.
26 * \file glapi_entrypoint.c
28 * Arch-specific code for manipulating GL API entrypoints (dispatch stubs).
34 #include "c11/threads.h"
35 #include "glapi/glapi_priv.h"
36 #include "u_execmem.h"
41 #if defined( GLX_USE_TLS )
42 extern GLubyte gl_dispatch_functions_start
[];
43 extern GLubyte gl_dispatch_functions_end
[];
45 extern const GLubyte gl_dispatch_functions_start
[];
48 #endif /* USE_X86_ASM */
51 #if defined(DISPATCH_FUNCTION_SIZE)
54 get_entrypoint_address(unsigned int functionOffset
)
56 return (_glapi_proc
) (gl_dispatch_functions_start
57 + (DISPATCH_FUNCTION_SIZE
* functionOffset
));
63 #if defined(USE_X86_ASM)
66 * Perform platform-specific GL API entry-point fixups.
69 init_glapi_relocs( void )
71 #if defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
72 extern unsigned long _x86_get_dispatch(void);
73 char run_time_patch
[] = {
74 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
76 GLuint
*offset
= (GLuint
*) &run_time_patch
[2]; /* 32-bits for x86/32 */
77 const GLubyte
* const get_disp
= (const GLubyte
*) run_time_patch
;
78 GLubyte
* curr_func
= (GLubyte
*) gl_dispatch_functions_start
;
80 *offset
= _x86_get_dispatch();
81 while ( curr_func
!= (GLubyte
*) gl_dispatch_functions_end
) {
82 (void) memcpy( curr_func
, get_disp
, sizeof(run_time_patch
));
83 curr_func
+= DISPATCH_FUNCTION_SIZE
;
90 * Generate a dispatch function (entrypoint) which jumps through
91 * the given slot number (offset) in the current dispatch table.
92 * We need assembly language in order to accomplish this.
95 generate_entrypoint(unsigned int functionOffset
)
97 /* 32 is chosen as something of a magic offset. For x86, the dispatch
98 * at offset 32 is the first one where the offset in the
99 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
101 const GLubyte
* const template_func
= gl_dispatch_functions_start
102 + (DISPATCH_FUNCTION_SIZE
* 32);
103 GLubyte
* const code
= (GLubyte
*) u_execmem_alloc(DISPATCH_FUNCTION_SIZE
);
106 if ( code
!= NULL
) {
107 (void) memcpy(code
, template_func
, DISPATCH_FUNCTION_SIZE
);
108 fill_in_entrypoint_offset( (_glapi_proc
) code
, functionOffset
);
111 return (_glapi_proc
) code
;
116 * This function inserts a new dispatch offset into the assembly language
117 * stub that was generated with the preceeding function.
120 fill_in_entrypoint_offset(_glapi_proc entrypoint
, unsigned int offset
)
122 GLubyte
* const code
= (GLubyte
*) entrypoint
;
124 #if defined(GLX_USE_TLS)
125 *((unsigned int *)(code
+ 8)) = 4 * offset
;
127 *((unsigned int *)(code
+ 11)) = 4 * offset
;
128 *((unsigned int *)(code
+ 22)) = 4 * offset
;
133 #elif defined(USE_SPARC_ASM)
135 extern void __glapi_sparc_icache_flush(unsigned int *);
138 init_glapi_relocs( void )
140 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
141 static const unsigned int template[] = {
143 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
144 0x8730e00a, /* srl %g3, 10, %g3 */
145 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
147 0xc259c002, /* ldx [%g7 + %g2], %g1 */
148 0xc2584003, /* ldx [%g1 + %g3], %g1 */
150 0xc201c002, /* ld [%g7 + %g2], %g1 */
151 0xc2004003, /* ld [%g1 + %g3], %g1 */
153 0x81c04000, /* jmp %g1 */
154 0x01000000, /* nop */
157 0x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */
158 0x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */
159 0x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */
160 0x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */
161 0x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */
162 0x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */
163 0xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */
165 0x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */
166 0x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */
167 0xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
169 0x80a06000, /* --> cmp %g1, 0 */
170 0x02800005, /* --> be +4*5 */
171 0x01000000, /* --> nop */
173 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
175 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
177 0x81c04000, /* --> jmp %g1 */
178 0x01000000, /* --> nop */
180 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
182 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
184 0xa0100003, /* --> mov %g3, %l0 */
185 0x40000000, /* --> call _glapi_get_dispatch */
186 0x01000000, /* --> nop */
187 0x82100008, /* --> mov %o0, %g1 */
188 0x86100010, /* --> mov %l0, %g3 */
189 0x10bffff7, /* --> ba -4*9 */
190 0x81e80000, /* --> restore */
194 extern unsigned int __glapi_sparc_tls_stub
;
195 extern unsigned long __glapi_sparc_get_dispatch(void);
196 unsigned int *code
= &__glapi_sparc_tls_stub
;
197 unsigned long dispatch
= __glapi_sparc_get_dispatch();
199 extern unsigned int __glapi_sparc_pthread_stub
;
200 unsigned int *code
= &__glapi_sparc_pthread_stub
;
201 unsigned long dispatch
= (unsigned long) &_glapi_Dispatch
;
202 unsigned long call_dest
= (unsigned long ) &_glapi_get_dispatch
;
207 code
[0] = template[0] | (dispatch
>> 10);
208 code
[1] = template[1];
209 __glapi_sparc_icache_flush(&code
[0]);
210 code
[2] = template[2] | (dispatch
& 0x3ff);
211 code
[3] = template[3];
212 __glapi_sparc_icache_flush(&code
[2]);
213 code
[4] = template[4];
214 code
[5] = template[5];
215 __glapi_sparc_icache_flush(&code
[4]);
216 code
[6] = template[6];
217 __glapi_sparc_icache_flush(&code
[6]);
219 #if defined(__arch64__)
220 code
[0] = template[0] | (dispatch
>> (32 + 10));
221 code
[1] = template[1] | ((dispatch
& 0xffffffff) >> 10);
222 __glapi_sparc_icache_flush(&code
[0]);
223 code
[2] = template[2] | ((dispatch
>> 32) & 0x3ff);
224 code
[3] = template[3];
225 __glapi_sparc_icache_flush(&code
[2]);
226 code
[4] = template[4];
227 code
[5] = template[5];
228 __glapi_sparc_icache_flush(&code
[4]);
229 code
[6] = template[6] | (dispatch
& 0x3ff);
232 code
[0] = template[0] | (dispatch
>> 10);
233 code
[1] = template[1];
234 __glapi_sparc_icache_flush(&code
[0]);
235 code
[2] = template[2] | (dispatch
& 0x3ff);
238 code
[idx
+ 0] = template[idx
+ 0];
239 __glapi_sparc_icache_flush(&code
[idx
- 1]);
240 code
[idx
+ 1] = template[idx
+ 1];
241 code
[idx
+ 2] = template[idx
+ 2];
242 __glapi_sparc_icache_flush(&code
[idx
+ 1]);
243 code
[idx
+ 3] = template[idx
+ 3];
244 code
[idx
+ 4] = template[idx
+ 4];
245 __glapi_sparc_icache_flush(&code
[idx
+ 3]);
246 code
[idx
+ 5] = template[idx
+ 5];
247 code
[idx
+ 6] = template[idx
+ 6];
248 __glapi_sparc_icache_flush(&code
[idx
+ 5]);
249 code
[idx
+ 7] = template[idx
+ 7];
250 code
[idx
+ 8] = template[idx
+ 8] |
251 (((call_dest
- ((unsigned long) &code
[idx
+ 8]))
253 __glapi_sparc_icache_flush(&code
[idx
+ 7]);
254 code
[idx
+ 9] = template[idx
+ 9];
255 code
[idx
+ 10] = template[idx
+ 10];
256 __glapi_sparc_icache_flush(&code
[idx
+ 9]);
257 code
[idx
+ 11] = template[idx
+ 11];
258 code
[idx
+ 12] = template[idx
+ 12];
259 __glapi_sparc_icache_flush(&code
[idx
+ 11]);
260 code
[idx
+ 13] = template[idx
+ 13];
261 __glapi_sparc_icache_flush(&code
[idx
+ 13]);
268 generate_entrypoint(GLuint functionOffset
)
270 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
271 static const unsigned int template[] = {
272 0x07000000, /* sethi %hi(0), %g3 */
273 0x8210000f, /* mov %o7, %g1 */
274 0x40000000, /* call */
275 0x9e100001, /* mov %g1, %o7 */
278 extern unsigned int __glapi_sparc_tls_stub
;
279 unsigned long call_dest
= (unsigned long ) &__glapi_sparc_tls_stub
;
281 extern unsigned int __glapi_sparc_pthread_stub
;
282 unsigned long call_dest
= (unsigned long ) &__glapi_sparc_pthread_stub
;
284 unsigned int *code
= (unsigned int *) u_execmem_alloc(sizeof(template));
286 code
[0] = template[0] | (functionOffset
& 0x3fffff);
287 code
[1] = template[1];
288 __glapi_sparc_icache_flush(&code
[0]);
289 code
[2] = template[2] |
290 (((call_dest
- ((unsigned long) &code
[2]))
292 code
[3] = template[3];
293 __glapi_sparc_icache_flush(&code
[2]);
295 return (_glapi_proc
) code
;
301 fill_in_entrypoint_offset(_glapi_proc entrypoint
, GLuint offset
)
303 unsigned int *code
= (unsigned int *) entrypoint
;
305 code
[0] &= ~0x3fffff;
306 code
[0] |= (offset
* sizeof(void *)) & 0x3fffff;
307 __glapi_sparc_icache_flush(&code
[0]);
311 #else /* USE_*_ASM */
314 init_glapi_relocs( void )
320 generate_entrypoint(GLuint functionOffset
)
322 (void) functionOffset
;
328 fill_in_entrypoint_offset(_glapi_proc entrypoint
, GLuint offset
)
330 /* an unimplemented architecture */
335 #endif /* USE_*_ASM */
339 init_glapi_relocs_once( void )
341 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
342 static once_flag flag
= ONCE_FLAG_INIT
;
343 call_once(&flag
, init_glapi_relocs
);