mesa: Prefix main includes with dir to avoid conflicts.
[mesa.git] / src / mesa / glapi / glapi_getproc.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_getproc.
27 *
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
30 */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include "main/glheader.h"
36 #include "glapi.h"
37 #include "glapioffsets.h"
38 #include "glapitable.h"
39
40
41 static void
42 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
43
44
45 /**
46 * strdup() is actually not a standard ANSI C or POSIX routine.
47 * Irix will not define it if ANSI mode is in effect.
48 */
49 static char *
50 str_dup(const char *str)
51 {
52 char *copy;
53 copy = (char*) malloc(strlen(str) + 1);
54 if (!copy)
55 return NULL;
56 strcpy(copy, str);
57 return copy;
58 }
59
60
61
62 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
63 # define DISPATCH_FUNCTION_SIZE 16
64 #elif defined(USE_X86_ASM)
65 # if defined(THREADS) && !defined(GLX_USE_TLS)
66 # define DISPATCH_FUNCTION_SIZE 32
67 # else
68 # define DISPATCH_FUNCTION_SIZE 16
69 # endif
70 #endif
71
72 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
73 # define NEED_FUNCTION_POINTER
74 #endif
75
76 /* The code in this file is auto-generated with Python */
77 #include "glprocs.h"
78
79
80 /**
81 * Search the table of static entrypoint functions for the named function
82 * and return the corresponding glprocs_table_t entry.
83 */
84 static const glprocs_table_t *
85 find_entry( const char * n )
86 {
87 GLuint i;
88 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
89 const char *testName = gl_string_table + static_functions[i].Name_offset;
90 if (strcmp(testName, n) == 0) {
91 return &static_functions[i];
92 }
93 }
94 return NULL;
95 }
96
97
98 /**
99 * Return dispatch table offset of the named static (built-in) function.
100 * Return -1 if function not found.
101 */
102 static GLint
103 get_static_proc_offset(const char *funcName)
104 {
105 const glprocs_table_t * const f = find_entry( funcName );
106 if (f) {
107 return f->Offset;
108 }
109 return -1;
110 }
111
112
113 #if !defined(XFree86Server) && !defined(XGLServer)
114 #ifdef USE_X86_ASM
115
116 #if defined( GLX_USE_TLS )
117 extern GLubyte gl_dispatch_functions_start[];
118 extern GLubyte gl_dispatch_functions_end[];
119 #else
120 extern const GLubyte gl_dispatch_functions_start[];
121 #endif
122
123 #endif /* USE_X86_ASM */
124
125
126 /**
127 * Return dispatch function address for the named static (built-in) function.
128 * Return NULL if function not found.
129 */
130 static _glapi_proc
131 get_static_proc_address(const char *funcName)
132 {
133 const glprocs_table_t * const f = find_entry( funcName );
134 if (f) {
135 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
136 return (f->Address == NULL)
137 ? (_glapi_proc) (gl_dispatch_functions_start
138 + (DISPATCH_FUNCTION_SIZE * f->Offset))
139 : f->Address;
140 #elif defined(DISPATCH_FUNCTION_SIZE)
141 return (_glapi_proc) (gl_dispatch_functions_start
142 + (DISPATCH_FUNCTION_SIZE * f->Offset));
143 #else
144 return f->Address;
145 #endif
146 }
147 else {
148 return NULL;
149 }
150 }
151
152 #endif /* !defined(XFree86Server) && !defined(XGLServer) */
153
154
155
156 /**
157 * Return the name of the function at the given offset in the dispatch
158 * table. For debugging only.
159 */
160 static const char *
161 get_static_proc_name( GLuint offset )
162 {
163 GLuint i;
164 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
165 if (static_functions[i].Offset == offset) {
166 return gl_string_table + static_functions[i].Name_offset;
167 }
168 }
169 return NULL;
170 }
171
172
173
174 /**********************************************************************
175 * Extension function management.
176 */
177
178
179 /**
180 * Track information about a function added to the GL API.
181 */
182 struct _glapi_function {
183 /**
184 * Name of the function.
185 */
186 const char * name;
187
188
189 /**
190 * Text string that describes the types of the parameters passed to the
191 * named function. Parameter types are converted to characters using the
192 * following rules:
193 * - 'i' for \c GLint, \c GLuint, and \c GLenum
194 * - 'p' for any pointer type
195 * - 'f' for \c GLfloat and \c GLclampf
196 * - 'd' for \c GLdouble and \c GLclampd
197 */
198 const char * parameter_signature;
199
200
201 /**
202 * Offset in the dispatch table where the pointer to the real function is
203 * located. If the driver has not requested that the named function be
204 * added to the dispatch table, this will have the value ~0.
205 */
206 unsigned dispatch_offset;
207
208
209 /**
210 * Pointer to the dispatch stub for the named function.
211 *
212 * \todo
213 * The semantic of this field should be changed slightly. Currently, it
214 * is always expected to be non-\c NULL. However, it would be better to
215 * only allocate the entry-point stub when the application requests the
216 * function via \c glXGetProcAddress. This would save memory for all the
217 * functions that the driver exports but that the application never wants
218 * to call.
219 */
220 _glapi_proc dispatch_stub;
221 };
222
223
224 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
225 static GLuint NumExtEntryPoints = 0;
226
227 #ifdef USE_SPARC_ASM
228 extern void __glapi_sparc_icache_flush(unsigned int *);
229 #endif
230
231 /**
232 * Generate a dispatch function (entrypoint) which jumps through
233 * the given slot number (offset) in the current dispatch table.
234 * We need assembly language in order to accomplish this.
235 */
236 static _glapi_proc
237 generate_entrypoint(GLuint functionOffset)
238 {
239 #if defined(USE_X86_ASM)
240 /* 32 is chosen as something of a magic offset. For x86, the dispatch
241 * at offset 32 is the first one where the offset in the
242 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
243 */
244 const GLubyte * const template_func = gl_dispatch_functions_start
245 + (DISPATCH_FUNCTION_SIZE * 32);
246 GLubyte * const code = (GLubyte *) malloc(DISPATCH_FUNCTION_SIZE);
247
248
249 if ( code != NULL ) {
250 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
251 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
252 }
253
254 return (_glapi_proc) code;
255 #elif defined(USE_SPARC_ASM)
256
257 #ifdef __arch64__
258 static const unsigned int insn_template[] = {
259 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
260 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
261 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
262 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
263 0x8528b020, /* sllx %g2, 32, %g2 */
264 0xc2584002, /* ldx [%g1 + %g2], %g1 */
265 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
266 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
267 0xc6584002, /* ldx [%g1 + %g2], %g3 */
268 0x81c0c000, /* jmpl %g3, %g0 */
269 0x01000000 /* nop */
270 };
271 #else
272 static const unsigned int insn_template[] = {
273 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
274 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
275 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
276 0x81c0c000, /* jmpl %g3, %g0 */
277 0x01000000 /* nop */
278 };
279 #endif /* __arch64__ */
280 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
281 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
282 if (code) {
283 memcpy(code, insn_template, sizeof(insn_template));
284
285 #ifdef __arch64__
286 code[0] |= (glapi_addr >> (32 + 10));
287 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
288 __glapi_sparc_icache_flush(&code[0]);
289 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
290 code[3] |= (glapi_addr & ((1 << 10) - 1));
291 __glapi_sparc_icache_flush(&code[2]);
292 code[6] |= ((functionOffset * 8) >> 10);
293 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
294 __glapi_sparc_icache_flush(&code[6]);
295 #else
296 code[0] |= (glapi_addr >> 10);
297 code[1] |= (glapi_addr & ((1 << 10) - 1));
298 __glapi_sparc_icache_flush(&code[0]);
299 code[2] |= (functionOffset * 4);
300 __glapi_sparc_icache_flush(&code[2]);
301 #endif /* __arch64__ */
302 }
303 return (_glapi_proc) code;
304 #else
305 (void) functionOffset;
306 return NULL;
307 #endif /* USE_*_ASM */
308 }
309
310
311 /**
312 * This function inserts a new dispatch offset into the assembly language
313 * stub that was generated with the preceeding function.
314 */
315 static void
316 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
317 {
318 #if defined(USE_X86_ASM)
319 GLubyte * const code = (GLubyte *) entrypoint;
320
321 #if DISPATCH_FUNCTION_SIZE == 32
322 *((unsigned int *)(code + 11)) = 4 * offset;
323 *((unsigned int *)(code + 22)) = 4 * offset;
324 #elif DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
325 *((unsigned int *)(code + 8)) = 4 * offset;
326 #elif DISPATCH_FUNCTION_SIZE == 16
327 *((unsigned int *)(code + 7)) = 4 * offset;
328 #else
329 # error Invalid DISPATCH_FUNCTION_SIZE!
330 #endif
331
332 #elif defined(USE_SPARC_ASM)
333
334 /* XXX this hasn't been tested! */
335 unsigned int *code = (unsigned int *) entrypoint;
336 #ifdef __arch64__
337 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
338 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
339 code[6] |= ((offset * 8) >> 10);
340 code[7] |= ((offset * 8) & ((1 << 10) - 1));
341 __glapi_sparc_icache_flush(&code[6]);
342 #else /* __arch64__ */
343 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
344 code[2] |= (offset * 4);
345 __glapi_sparc_icache_flush(&code[2]);
346 #endif /* __arch64__ */
347
348 #else
349
350 /* an unimplemented architecture */
351 (void) entrypoint;
352 (void) offset;
353
354 #endif /* USE_*_ASM */
355 }
356
357
358 /**
359 * Generate new entrypoint
360 *
361 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
362 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
363 * never happens, and the user calls this function, he'll segfault. That's
364 * what you get when you try calling a GL function that doesn't really exist.
365 *
366 * \param funcName Name of the function to create an entry-point for.
367 *
368 * \sa _glapi_add_entrypoint
369 */
370
371 static struct _glapi_function *
372 add_function_name( const char * funcName )
373 {
374 struct _glapi_function * entry = NULL;
375
376 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
377 _glapi_proc entrypoint = generate_entrypoint(~0);
378 if (entrypoint != NULL) {
379 entry = & ExtEntryTable[NumExtEntryPoints];
380
381 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
382 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
383 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
384 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
385 NumExtEntryPoints++;
386 }
387 }
388
389 return entry;
390 }
391
392
393 /**
394 * Fill-in the dispatch stub for the named function.
395 *
396 * This function is intended to be called by a hardware driver. When called,
397 * a dispatch stub may be created created for the function. A pointer to this
398 * dispatch function will be returned by glXGetProcAddress.
399 *
400 * \param function_names Array of pointers to function names that should
401 * share a common dispatch offset.
402 * \param parameter_signature String representing the types of the parameters
403 * passed to the named function. Parameter types
404 * are converted to characters using the following
405 * rules:
406 * - 'i' for \c GLint, \c GLuint, and \c GLenum
407 * - 'p' for any pointer type
408 * - 'f' for \c GLfloat and \c GLclampf
409 * - 'd' for \c GLdouble and \c GLclampd
410 *
411 * \returns
412 * The offset in the dispatch table of the named function. A pointer to the
413 * driver's implementation of the named function should be stored at
414 * \c dispatch_table[\c offset]. Return -1 if error/problem.
415 *
416 * \sa glXGetProcAddress
417 *
418 * \warning
419 * This function can only handle up to 8 names at a time. As far as I know,
420 * the maximum number of names ever associated with an existing GL function is
421 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
422 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
423 * too painful of a limitation.
424 *
425 * \todo
426 * Determine whether or not \c parameter_signature should be allowed to be
427 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
428 * pass in an empty string.
429 *
430 * \todo
431 * Determine if code should be added to reject function names that start with
432 * 'glX'.
433 *
434 * \bug
435 * Add code to compare \c parameter_signature with the parameter signature of
436 * a static function. In order to do that, we need to find a way to \b get
437 * the parameter signature of a static function.
438 */
439
440 PUBLIC int
441 _glapi_add_dispatch( const char * const * function_names,
442 const char * parameter_signature )
443 {
444 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
445 const char * const real_sig = (parameter_signature != NULL)
446 ? parameter_signature : "";
447 struct _glapi_function * entry[8];
448 GLboolean is_static[8];
449 unsigned i;
450 unsigned j;
451 int offset = ~0;
452 int new_offset;
453
454
455 (void) memset( is_static, 0, sizeof( is_static ) );
456 (void) memset( entry, 0, sizeof( entry ) );
457
458 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
459 /* Do some trivial validation on the name of the function.
460 */
461
462 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
463 return -1;
464
465 /* Determine if the named function already exists. If the function does
466 * exist, it must have the same parameter signature as the function
467 * being added.
468 */
469
470 new_offset = get_static_proc_offset(function_names[i]);
471 if (new_offset >= 0) {
472 /* FIXME: Make sure the parameter signatures match! How do we get
473 * FIXME: the parameter signature for static functions?
474 */
475
476 if ( (offset != ~0) && (new_offset != offset) ) {
477 return -1;
478 }
479
480 is_static[i] = GL_TRUE;
481 offset = new_offset;
482 }
483
484
485 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
486 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
487 /* The offset may be ~0 if the function name was added by
488 * glXGetProcAddress but never filled in by the driver.
489 */
490
491 if (ExtEntryTable[j].dispatch_offset != ~0) {
492 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
493 != 0) {
494 return -1;
495 }
496
497 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
498 return -1;
499 }
500
501 offset = ExtEntryTable[j].dispatch_offset;
502 }
503
504 entry[i] = & ExtEntryTable[j];
505 break;
506 }
507 }
508 }
509
510 if (offset == ~0) {
511 offset = next_dynamic_offset;
512 next_dynamic_offset++;
513 }
514
515 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
516 if (! is_static[i] ) {
517 if (entry[i] == NULL) {
518 entry[i] = add_function_name( function_names[i] );
519 if (entry[i] == NULL) {
520 /* FIXME: Possible memory leak here.
521 */
522 return -1;
523 }
524 }
525
526 entry[i]->parameter_signature = str_dup(real_sig);
527 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
528 entry[i]->dispatch_offset = offset;
529 }
530 }
531
532 return offset;
533 }
534
535
536 /**
537 * Return offset of entrypoint for named function within dispatch table.
538 */
539 PUBLIC GLint
540 _glapi_get_proc_offset(const char *funcName)
541 {
542 /* search extension functions first */
543 GLuint i;
544 for (i = 0; i < NumExtEntryPoints; i++) {
545 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
546 return ExtEntryTable[i].dispatch_offset;
547 }
548 }
549 /* search static functions */
550 return get_static_proc_offset(funcName);
551 }
552
553
554
555 /**
556 * Return pointer to the named function. If the function name isn't found
557 * in the name of static functions, try generating a new API entrypoint on
558 * the fly with assembly language.
559 */
560 _glapi_proc
561 _glapi_get_proc_address(const char *funcName)
562 {
563 struct _glapi_function * entry;
564 GLuint i;
565
566 #ifdef MANGLE
567 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
568 return NULL;
569 #else
570 if (funcName[0] != 'g' || funcName[1] != 'l')
571 return NULL;
572 #endif
573
574 /* search extension functions first */
575 for (i = 0; i < NumExtEntryPoints; i++) {
576 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
577 return ExtEntryTable[i].dispatch_stub;
578 }
579 }
580
581 #if !defined( XFree86Server ) && !defined( XGLServer )
582 /* search static functions */
583 {
584 const _glapi_proc func = get_static_proc_address(funcName);
585 if (func)
586 return func;
587 }
588 #endif /* !defined( XFree86Server ) */
589
590 entry = add_function_name(funcName);
591 return (entry == NULL) ? NULL : entry->dispatch_stub;
592 }
593
594
595
596 /**
597 * Return the name of the function at the given dispatch offset.
598 * This is only intended for debugging.
599 */
600 const char *
601 _glapi_get_proc_name(GLuint offset)
602 {
603 GLuint i;
604 const char * n;
605
606 /* search built-in functions */
607 n = get_static_proc_name(offset);
608 if ( n != NULL ) {
609 return n;
610 }
611
612 /* search added extension functions */
613 for (i = 0; i < NumExtEntryPoints; i++) {
614 if (ExtEntryTable[i].dispatch_offset == offset) {
615 return ExtEntryTable[i].name;
616 }
617 }
618 return NULL;
619 }