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).
32 #include "c11/threads.h"
33 #include "glapi/glapi_priv.h"
34 #include "u_execmem.h"
39 #if defined( GLX_USE_TLS )
40 extern GLubyte gl_dispatch_functions_start
[];
41 extern GLubyte gl_dispatch_functions_end
[];
43 extern const GLubyte gl_dispatch_functions_start
[];
46 #endif /* USE_X86_ASM */
49 #if defined(DISPATCH_FUNCTION_SIZE)
52 get_entrypoint_address(unsigned int functionOffset
)
54 return (_glapi_proc
) (gl_dispatch_functions_start
55 + (DISPATCH_FUNCTION_SIZE
* functionOffset
));
61 #if defined(USE_X86_ASM)
64 * Perform platform-specific GL API entry-point fixups.
67 init_glapi_relocs( void )
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 */
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
;
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
;
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.
93 generate_entrypoint(unsigned int functionOffset
)
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.
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
);
104 if ( code
!= NULL
) {
105 (void) memcpy(code
, template_func
, DISPATCH_FUNCTION_SIZE
);
106 fill_in_entrypoint_offset( (_glapi_proc
) code
, functionOffset
);
109 return (_glapi_proc
) code
;
114 * This function inserts a new dispatch offset into the assembly language
115 * stub that was generated with the preceeding function.
118 fill_in_entrypoint_offset(_glapi_proc entrypoint
, unsigned int offset
)
120 GLubyte
* const code
= (GLubyte
*) entrypoint
;
122 #if defined(GLX_USE_TLS)
123 *((unsigned int *)(code
+ 8)) = 4 * offset
;
125 *((unsigned int *)(code
+ 11)) = 4 * offset
;
126 *((unsigned int *)(code
+ 22)) = 4 * offset
;
131 #elif defined(USE_SPARC_ASM)
133 extern void __glapi_sparc_icache_flush(unsigned int *);
136 init_glapi_relocs( void )
138 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
139 static const unsigned int template[] = {
141 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
142 0x8730e00a, /* srl %g3, 10, %g3 */
143 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
145 0xc259c002, /* ldx [%g7 + %g2], %g1 */
146 0xc2584003, /* ldx [%g1 + %g3], %g1 */
148 0xc201c002, /* ld [%g7 + %g2], %g1 */
149 0xc2004003, /* ld [%g1 + %g3], %g1 */
151 0x81c04000, /* jmp %g1 */
152 0x01000000, /* nop */
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 */
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 */
167 0x80a06000, /* --> cmp %g1, 0 */
168 0x02800005, /* --> be +4*5 */
169 0x01000000, /* --> nop */
171 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
173 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
175 0x81c04000, /* --> jmp %g1 */
176 0x01000000, /* --> nop */
178 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
180 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
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 */
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();
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
;
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]);
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);
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);
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]))
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]);
266 generate_entrypoint(GLuint functionOffset
)
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 */
276 extern unsigned int __glapi_sparc_tls_stub
;
277 unsigned long call_dest
= (unsigned long ) &__glapi_sparc_tls_stub
;
279 extern unsigned int __glapi_sparc_pthread_stub
;
280 unsigned long call_dest
= (unsigned long ) &__glapi_sparc_pthread_stub
;
282 unsigned int *code
= (unsigned int *) u_execmem_alloc(sizeof(template));
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]))
290 code
[3] = template[3];
291 __glapi_sparc_icache_flush(&code
[2]);
293 return (_glapi_proc
) code
;
299 fill_in_entrypoint_offset(_glapi_proc entrypoint
, GLuint offset
)
301 unsigned int *code
= (unsigned int *) entrypoint
;
303 code
[0] &= ~0x3fffff;
304 code
[0] |= (offset
* sizeof(void *)) & 0x3fffff;
305 __glapi_sparc_icache_flush(&code
[0]);
309 #else /* USE_*_ASM */
312 init_glapi_relocs( void )
318 generate_entrypoint(GLuint functionOffset
)
320 (void) functionOffset
;
326 fill_in_entrypoint_offset(_glapi_proc entrypoint
, GLuint offset
)
328 /* an unimplemented architecture */
333 #endif /* USE_*_ASM */
337 init_glapi_relocs_once( void )
339 #if defined(HAVE_PTHREAD) || defined(GLX_USE_TLS)
340 static once_flag flag
= ONCE_FLAG_INIT
;
341 call_once(&flag
, init_glapi_relocs
);