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