Merge commit 'origin/master' into gallium-sampler-view
[mesa.git] / src / mesa / glapi / glapi_entrypoint.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 #ifdef HAVE_DIX_CONFIG_H
33 #include <dix-config.h>
34 #include "glapi/mesa.h"
35 #else
36 #include "main/glheader.h"
37 #include "main/compiler.h"
38 #endif
39
40 #include "glapi/glapi.h"
41 #include "glapi/glapi_priv.h"
42
43
44 #ifdef USE_X86_ASM
45
46 #if defined( GLX_USE_TLS )
47 extern GLubyte gl_dispatch_functions_start[];
48 extern GLubyte gl_dispatch_functions_end[];
49 #else
50 extern const GLubyte gl_dispatch_functions_start[];
51 #endif
52
53 #endif /* USE_X86_ASM */
54
55
56 #if defined(DISPATCH_FUNCTION_SIZE)
57
58 _glapi_proc
59 get_entrypoint_address(GLuint functionOffset)
60 {
61 return (_glapi_proc) (gl_dispatch_functions_start
62 + (DISPATCH_FUNCTION_SIZE * functionOffset));
63 }
64
65 #endif
66
67
68 #if defined(PTHREADS) || defined(GLX_USE_TLS)
69
70 /**
71 * Perform platform-specific GL API entry-point fixups.
72 */
73 static void
74 init_glapi_relocs( void )
75 {
76 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
77 extern unsigned long _x86_get_dispatch(void);
78 char run_time_patch[] = {
79 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
80 };
81 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
82 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
83 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
84
85 *offset = _x86_get_dispatch();
86 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
87 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
88 curr_func += DISPATCH_FUNCTION_SIZE;
89 }
90 #endif
91 #ifdef USE_SPARC_ASM
92 extern void __glapi_sparc_icache_flush(unsigned int *);
93 static const unsigned int template[] = {
94 #ifdef GLX_USE_TLS
95 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
96 0x8730e00a, /* srl %g3, 10, %g3 */
97 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
98 #ifdef __arch64__
99 0xc259c002, /* ldx [%g7 + %g2], %g1 */
100 0xc2584003, /* ldx [%g1 + %g3], %g1 */
101 #else
102 0xc201c002, /* ld [%g7 + %g2], %g1 */
103 0xc2004003, /* ld [%g1 + %g3], %g1 */
104 #endif
105 0x81c04000, /* jmp %g1 */
106 0x01000000, /* nop */
107 #else
108 #ifdef __arch64__
109 0x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */
110 0x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */
111 0x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */
112 0x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */
113 0x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */
114 0x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */
115 0xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */
116 #else
117 0x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */
118 0x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */
119 0xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
120 #endif
121 0x80a06000, /* --> cmp %g1, 0 */
122 0x02800005, /* --> be +4*5 */
123 0x01000000, /* --> nop */
124 #ifdef __arch64__
125 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
126 #else
127 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
128 #endif
129 0x81c04000, /* --> jmp %g1 */
130 0x01000000, /* --> nop */
131 #ifdef __arch64__
132 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
133 #else
134 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
135 #endif
136 0xa0100003, /* --> mov %g3, %l0 */
137 0x40000000, /* --> call _glapi_get_dispatch */
138 0x01000000, /* --> nop */
139 0x82100008, /* --> mov %o0, %g1 */
140 0x86100010, /* --> mov %l0, %g3 */
141 0x10bffff7, /* --> ba -4*9 */
142 0x81e80000, /* --> restore */
143 #endif
144 };
145 #ifdef GLX_USE_TLS
146 extern unsigned int __glapi_sparc_tls_stub;
147 extern unsigned long __glapi_sparc_get_dispatch(void);
148 unsigned int *code = &__glapi_sparc_tls_stub;
149 unsigned long dispatch = __glapi_sparc_get_dispatch();
150 #else
151 extern unsigned int __glapi_sparc_pthread_stub;
152 unsigned int *code = &__glapi_sparc_pthread_stub;
153 unsigned long dispatch = (unsigned long) &_glapi_Dispatch;
154 unsigned long call_dest = (unsigned long ) &_glapi_get_dispatch;
155 int idx;
156 #endif
157
158 #if defined(GLX_USE_TLS)
159 code[0] = template[0] | (dispatch >> 10);
160 code[1] = template[1];
161 __glapi_sparc_icache_flush(&code[0]);
162 code[2] = template[2] | (dispatch & 0x3ff);
163 code[3] = template[3];
164 __glapi_sparc_icache_flush(&code[2]);
165 code[4] = template[4];
166 code[5] = template[5];
167 __glapi_sparc_icache_flush(&code[4]);
168 code[6] = template[6];
169 __glapi_sparc_icache_flush(&code[6]);
170 #else
171 #if defined(__arch64__)
172 code[0] = template[0] | (dispatch >> (32 + 10));
173 code[1] = template[1] | ((dispatch & 0xffffffff) >> 10);
174 __glapi_sparc_icache_flush(&code[0]);
175 code[2] = template[2] | ((dispatch >> 32) & 0x3ff);
176 code[3] = template[3];
177 __glapi_sparc_icache_flush(&code[2]);
178 code[4] = template[4];
179 code[5] = template[5];
180 __glapi_sparc_icache_flush(&code[4]);
181 code[6] = template[6] | (dispatch & 0x3ff);
182 idx = 7;
183 #else
184 code[0] = template[0] | (dispatch >> 10);
185 code[1] = template[1];
186 __glapi_sparc_icache_flush(&code[0]);
187 code[2] = template[2] | (dispatch & 0x3ff);
188 idx = 3;
189 #endif
190 code[idx + 0] = template[idx + 0];
191 __glapi_sparc_icache_flush(&code[idx - 1]);
192 code[idx + 1] = template[idx + 1];
193 code[idx + 2] = template[idx + 2];
194 __glapi_sparc_icache_flush(&code[idx + 1]);
195 code[idx + 3] = template[idx + 3];
196 code[idx + 4] = template[idx + 4];
197 __glapi_sparc_icache_flush(&code[idx + 3]);
198 code[idx + 5] = template[idx + 5];
199 code[idx + 6] = template[idx + 6];
200 __glapi_sparc_icache_flush(&code[idx + 5]);
201 code[idx + 7] = template[idx + 7];
202 code[idx + 8] = template[idx + 8] |
203 (((call_dest - ((unsigned long) &code[idx + 8]))
204 >> 2) & 0x3fffffff);
205 __glapi_sparc_icache_flush(&code[idx + 7]);
206 code[idx + 9] = template[idx + 9];
207 code[idx + 10] = template[idx + 10];
208 __glapi_sparc_icache_flush(&code[idx + 9]);
209 code[idx + 11] = template[idx + 11];
210 code[idx + 12] = template[idx + 12];
211 __glapi_sparc_icache_flush(&code[idx + 11]);
212 code[idx + 13] = template[idx + 13];
213 __glapi_sparc_icache_flush(&code[idx + 13]);
214 #endif
215 #endif
216 }
217
218 void
219 init_glapi_relocs_once( void )
220 {
221 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
222 pthread_once( & once_control, init_glapi_relocs );
223 }
224
225 #else
226
227 void
228 init_glapi_relocs_once( void ) { }
229
230 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
231
232
233 #ifdef USE_SPARC_ASM
234 extern void __glapi_sparc_icache_flush(unsigned int *);
235 #endif
236
237 /**
238 * Generate a dispatch function (entrypoint) which jumps through
239 * the given slot number (offset) in the current dispatch table.
240 * We need assembly language in order to accomplish this.
241 */
242 _glapi_proc
243 generate_entrypoint(GLuint functionOffset)
244 {
245 #if defined(USE_X86_ASM)
246 /* 32 is chosen as something of a magic offset. For x86, the dispatch
247 * at offset 32 is the first one where the offset in the
248 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
249 */
250 const GLubyte * const template_func = gl_dispatch_functions_start
251 + (DISPATCH_FUNCTION_SIZE * 32);
252 GLubyte * const code = (GLubyte *) malloc(DISPATCH_FUNCTION_SIZE);
253
254
255 if ( code != NULL ) {
256 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
257 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
258 }
259
260 return (_glapi_proc) code;
261 #elif defined(USE_SPARC_ASM)
262
263 #if defined(PTHREADS) || defined(GLX_USE_TLS)
264 static const unsigned int template[] = {
265 0x07000000, /* sethi %hi(0), %g3 */
266 0x8210000f, /* mov %o7, %g1 */
267 0x40000000, /* call */
268 0x9e100001, /* mov %g1, %o7 */
269 };
270 #ifdef GLX_USE_TLS
271 extern unsigned int __glapi_sparc_tls_stub;
272 unsigned long call_dest = (unsigned long ) &__glapi_sparc_tls_stub;
273 #else
274 extern unsigned int __glapi_sparc_pthread_stub;
275 unsigned long call_dest = (unsigned long ) &__glapi_sparc_pthread_stub;
276 #endif
277 unsigned int *code = (unsigned int *) malloc(sizeof(template));
278 if (code) {
279 code[0] = template[0] | (functionOffset & 0x3fffff);
280 code[1] = template[1];
281 __glapi_sparc_icache_flush(&code[0]);
282 code[2] = template[2] |
283 (((call_dest - ((unsigned long) &code[2]))
284 >> 2) & 0x3fffffff);
285 code[3] = template[3];
286 __glapi_sparc_icache_flush(&code[2]);
287 }
288 return (_glapi_proc) code;
289 #endif
290
291 #else
292 (void) functionOffset;
293 return NULL;
294 #endif /* USE_*_ASM */
295 }
296
297
298 /**
299 * This function inserts a new dispatch offset into the assembly language
300 * stub that was generated with the preceeding function.
301 */
302 void
303 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
304 {
305 #if defined(USE_X86_ASM)
306 GLubyte * const code = (GLubyte *) entrypoint;
307
308 #if DISPATCH_FUNCTION_SIZE == 32
309 *((unsigned int *)(code + 11)) = 4 * offset;
310 *((unsigned int *)(code + 22)) = 4 * offset;
311 #elif DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
312 *((unsigned int *)(code + 8)) = 4 * offset;
313 #elif DISPATCH_FUNCTION_SIZE == 16
314 *((unsigned int *)(code + 7)) = 4 * offset;
315 #else
316 # error Invalid DISPATCH_FUNCTION_SIZE!
317 #endif
318
319 #elif defined(USE_SPARC_ASM)
320 unsigned int *code = (unsigned int *) entrypoint;
321 code[0] &= ~0x3fffff;
322 code[0] |= (offset * sizeof(void *)) & 0x3fffff;
323 __glapi_sparc_icache_flush(&code[0]);
324 #else
325
326 /* an unimplemented architecture */
327 (void) entrypoint;
328 (void) offset;
329
330 #endif /* USE_*_ASM */
331 }