-struct name_address_offset {
- const char *Name;
- _glapi_proc Address;
- GLuint Offset;
-};
-
-
-static struct name_address_offset ExtEntryTable[MAX_EXTENSION_FUNCS];
-static GLuint NumExtEntryPoints = 0;
-
-#ifdef USE_SPARC_ASM
-extern void __glapi_sparc_icache_flush(unsigned int *);
-#endif
-
-/**
- * Generate a dispatch function (entrypoint) which jumps through
- * the given slot number (offset) in the current dispatch table.
- * We need assembly language in order to accomplish this.
- */
-static _glapi_proc
-generate_entrypoint(GLuint functionOffset)
-{
-#if defined(USE_X86_ASM)
- /*
- * This x86 code contributed by Josh Vanderhoof.
- *
- * 0: a1 10 32 54 76 movl __glapi_Dispatch,%eax
- * 00 01 02 03 04
- * 5: 85 c0 testl %eax,%eax
- * 05 06
- * 7: 74 06 je f <entrypoint+0xf>
- * 07 08
- * 9: ff a0 10 32 54 76 jmp *0x76543210(%eax)
- * 09 0a 0b 0c 0d 0e
- * f: e8 fc ff ff ff call __glapi_get_dispatch
- * 0f 10 11 12 13
- * 14: ff a0 10 32 54 76 jmp *0x76543210(%eax)
- * 14 15 16 17 18 19
- */
- static const unsigned char insn_template[] = {
- 0xa1, 0x00, 0x00, 0x00, 0x00,
- 0x85, 0xc0,
- 0x74, 0x06,
- 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00,
- 0xe8, 0x00, 0x00, 0x00, 0x00,
- 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00
- };
- unsigned char *code = (unsigned char *) malloc(sizeof(insn_template));
- unsigned int next_insn;
- if (code) {
- memcpy(code, insn_template, sizeof(insn_template));
-
-#if defined( THREADS )
- *(unsigned int *)(code + 0x01) = (unsigned int)&_glapi_DispatchTSD;
-#else
- *(unsigned int *)(code + 0x01) = (unsigned int)&_glapi_Dispatch;
-#endif
- *(unsigned int *)(code + 0x0b) = (unsigned int)functionOffset * 4;
- next_insn = (unsigned int)(code + 0x14);
- *(unsigned int *)(code + 0x10) = (unsigned int)_glapi_get_dispatch - next_insn;
- *(unsigned int *)(code + 0x16) = (unsigned int)functionOffset * 4;
- }
- return (_glapi_proc) code;
-#elif defined(USE_SPARC_ASM)
-
-#if (defined(__sparc_v9__) && (!defined(__linux__) || defined(__linux_sparc_64__)))
- static const unsigned int insn_template[] = {
- 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
- 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
- 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
- 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
- 0x8528b020, /* sllx %g2, 32, %g2 */
- 0xc2584002, /* ldx [%g1 + %g2], %g1 */
- 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
- 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
- 0xc6584002, /* ldx [%g1 + %g2], %g3 */
- 0x81c0c000, /* jmpl %g3, %g0 */
- 0x01000000 /* nop */
- };
-#else
- static const unsigned int insn_template[] = {
- 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
- 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
- 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
- 0x81c0c000, /* jmpl %g3, %g0 */
- 0x01000000 /* nop */
- };
-#endif
- unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
- unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
- if (code) {
- memcpy(code, insn_template, sizeof(insn_template));
-
-#if (defined(__sparc_v9__) && (!defined(__linux__) || defined(__linux_sparc_64__)))
- code[0] |= (glapi_addr >> (32 + 10));
- code[1] |= ((glapi_addr & 0xffffffff) >> 10);
- __glapi_sparc_icache_flush(&code[0]);
- code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
- code[3] |= (glapi_addr & ((1 << 10) - 1));
- __glapi_sparc_icache_flush(&code[2]);
- code[6] |= ((functionOffset * 8) >> 10);
- code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
- __glapi_sparc_icache_flush(&code[6]);
-#else
- code[0] |= (glapi_addr >> 10);
- code[1] |= (glapi_addr & ((1 << 10) - 1));
- __glapi_sparc_icache_flush(&code[0]);
- code[2] |= (functionOffset * 4);
- __glapi_sparc_icache_flush(&code[2]);
-#endif
- }
- return (_glapi_proc) code;
-#else
- (void) functionOffset;
- return NULL;
-#endif /* USE_*_ASM */
-}
-
-
-/**
- * This function inserts a new dispatch offset into the assembly language
- * stub that was generated with the preceeding function.
- */
-static void
-fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
-{
-#if defined(USE_X86_ASM)
-
- unsigned char *code = (unsigned char *) entrypoint;
- *(unsigned int *)(code + 0x0b) = offset * 4;
- *(unsigned int *)(code + 0x16) = offset * 4;
-
-#elif defined(USE_SPARC_ASM)
-
- /* XXX this hasn't been tested! */
- unsigned int *code = (unsigned int *) entrypoint;
-#if (defined(__sparc_v9__) && (!defined(__linux__) || defined(__linux_sparc_64__)))
- code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
- code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
- code[6] |= ((offset * 8) >> 10);
- code[7] |= ((offset * 8) & ((1 << 10) - 1));
- __glapi_sparc_icache_flush(&code[6]);
-#else /* __sparc_v9__ && !linux */
- code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
- code[2] |= (offset * 4);
- __glapi_sparc_icache_flush(&code[2]);
-#endif /* __sparc_v9__ && !linux */
-
-#else
-
- /* an unimplemented architecture */
- (void) entrypoint;
- (void) offset;
-
-#endif /* USE_*_ASM */
-}
-
-
-/**
- * Add a new extension function entrypoint.
- * Return: GL_TRUE = success or GL_FALSE = failure
- */
-GLboolean
-_glapi_add_entrypoint(const char *funcName, GLuint offset)
-{
- /* trivial rejection test */
-#ifdef MANGLE
- if (!funcName || funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
- return GL_FALSE;
-#else
- if (!funcName || funcName[0] != 'g' || funcName[1] != 'l')
- return GL_FALSE;
-#endif
-
- /* first check if the named function is already statically present */
- {
- GLint index = get_static_proc_offset(funcName);
- if (index >= 0) {
- return (GLboolean) ((GLuint) index == offset); /* bad offset! */
- }
- }
-
- /* See if this function has already been dynamically added */
- {
- GLuint i;
- for (i = 0; i < NumExtEntryPoints; i++) {
- if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
- /* function already registered */
- if (ExtEntryTable[i].Offset == offset) {
- return GL_TRUE; /* offsets match */
- }
- else if (ExtEntryTable[i].Offset == (GLuint) ~0
- && offset < DISPATCH_TABLE_SIZE) {
- /* need to patch-up the dispatch code */
- if (offset != (GLuint) ~0) {
- fill_in_entrypoint_offset(ExtEntryTable[i].Address, offset);
- ExtEntryTable[i].Offset = offset;
- }
- return GL_TRUE;
- }
- else {
- return GL_FALSE; /* bad offset! */
- }
- }
- }
- }
-
- /* This is a new function, try to add it. */
- if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS ||
- offset >= DISPATCH_TABLE_SIZE) {
- /* No space left */
- return GL_FALSE;
- }
- else {
- _glapi_proc entrypoint = generate_entrypoint(offset);
- if (!entrypoint)
- return GL_FALSE; /* couldn't generate assembly */
-
- /* OK! */
- ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName);
- ExtEntryTable[NumExtEntryPoints].Offset = offset;
- ExtEntryTable[NumExtEntryPoints].Address = entrypoint;
- NumExtEntryPoints++;
-
- return GL_TRUE; /* success */
- }
-
- /* should never get here, silence compiler warnings */
- return GL_FALSE;
-}
-
-
-/**
- * Return offset of entrypoint for named function within dispatch table.
- */
-GLint
-_glapi_get_proc_offset(const char *funcName)
-{
- /* search extension functions first */
- GLuint i;
- for (i = 0; i < NumExtEntryPoints; i++) {
- if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
- return ExtEntryTable[i].Offset;
- }
- }
-
- /* search static functions */
- return get_static_proc_offset(funcName);
-}
-
-
-
-/**
- * Return pointer to the named function. If the function name isn't found
- * in the name of static functions, try generating a new API entrypoint on
- * the fly with assembly language.
- */
-const _glapi_proc
-_glapi_get_proc_address(const char *funcName)
-{
- GLuint i;
-
-#ifdef MANGLE
- if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
- return NULL;
-#else
- if (funcName[0] != 'g' || funcName[1] != 'l')
- return NULL;
-#endif
-
- /* search extension functions first */
- for (i = 0; i < NumExtEntryPoints; i++) {
- if (strcmp(ExtEntryTable[i].Name, funcName) == 0) {
- return ExtEntryTable[i].Address;
- }
- }
-
- /* search static functions */
- {
- const _glapi_proc func = get_static_proc_address(funcName);
- if (func)
- return func;
- }
-
- /* generate new entrypoint - use a temporary dispatch offset of
- * ~0 (i.e. -1). Later, when the driver calls _glapi_add_entrypoint()
- * we'll put in the proper offset. If that never happens, and the
- * user calls this function, he'll segfault. That's what you get
- * when you try calling a GL function that doesn't really exist.
- */
- if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
- _glapi_proc entrypoint = generate_entrypoint(~0);
- if (!entrypoint)
- return GL_FALSE;
-
- ExtEntryTable[NumExtEntryPoints].Name = str_dup(funcName);
- ExtEntryTable[NumExtEntryPoints].Offset = ~0;
- ExtEntryTable[NumExtEntryPoints].Address = entrypoint;
- NumExtEntryPoints++;
-
- return entrypoint;
- }
- else {
- /* no space for new functions! */
- return NULL;
- }
-}
-
-
-
-/**
- * Return the name of the function at the given dispatch offset.
- * This is only intended for debugging.
- */
-const char *
-_glapi_get_proc_name(GLuint offset)
-{
- GLuint i;
- const char * n;
-
- /* search built-in functions */
- n = get_static_proc_name(offset);
- if ( n != NULL ) {
- return n;
- }
-
- /* search added extension functions */
- for (i = 0; i < NumExtEntryPoints; i++) {
- if (ExtEntryTable[i].Offset == offset) {
- return ExtEntryTable[i].Name;
- }
- }
- return NULL;
-}
-
-
-