Path: blob/21.2-virgl/src/mapi/glapi/glapi_entrypoint.c
4560 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 1999-2008 Brian Paul All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*/2324/**25* \file glapi_entrypoint.c26*27* Arch-specific code for manipulating GL API entrypoints (dispatch stubs).28*/293031#include <string.h>3233#include "c11/threads.h"34#include "glapi/glapi_priv.h"35#include "u_execmem.h"363738#ifdef USE_X86_ASM3940#if defined( USE_ELF_TLS )41extern GLubyte gl_dispatch_functions_start[];42extern GLubyte gl_dispatch_functions_end[];43#else44extern const GLubyte gl_dispatch_functions_start[];45#endif4647#endif /* USE_X86_ASM */484950#if defined(DISPATCH_FUNCTION_SIZE)5152_glapi_proc53get_entrypoint_address(unsigned int functionOffset)54{55return (_glapi_proc) (gl_dispatch_functions_start56+ (DISPATCH_FUNCTION_SIZE * functionOffset));57}5859#endif606162#if defined(USE_X86_ASM)6364/**65* Perform platform-specific GL API entry-point fixups.66*/67static void68init_glapi_relocs( void )69{70#if defined(USE_ELF_TLS) && !defined(GLX_X86_READONLY_TEXT)71extern unsigned long _x86_get_dispatch(void);72char run_time_patch[] = {730x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */74};75GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */76const GLubyte * const get_disp = (const GLubyte *) run_time_patch;77GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;7879*offset = _x86_get_dispatch();80while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {81(void) memcpy( curr_func, get_disp, sizeof(run_time_patch));82curr_func += DISPATCH_FUNCTION_SIZE;83}84#endif85}868788/**89* Generate a dispatch function (entrypoint) which jumps through90* the given slot number (offset) in the current dispatch table.91* We need assembly language in order to accomplish this.92*/93_glapi_proc94generate_entrypoint(unsigned int functionOffset)95{96/* 32 is chosen as something of a magic offset. For x86, the dispatch97* at offset 32 is the first one where the offset in the98* "jmp OFFSET*4(%eax)" can't be encoded in a single byte.99*/100const GLubyte * const template_func = gl_dispatch_functions_start101+ (DISPATCH_FUNCTION_SIZE * 32);102GLubyte * const code = (GLubyte *) u_execmem_alloc(DISPATCH_FUNCTION_SIZE);103104105if ( code != NULL ) {106(void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);107fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );108}109110return (_glapi_proc) code;111}112113114/**115* This function inserts a new dispatch offset into the assembly language116* stub that was generated with the preceeding function.117*/118void119fill_in_entrypoint_offset(_glapi_proc entrypoint, unsigned int offset)120{121GLubyte * const code = (GLubyte *) entrypoint;122123#if defined(USE_ELF_TLS)124*((unsigned int *)(code + 8)) = 4 * offset;125#else126*((unsigned int *)(code + 11)) = 4 * offset;127*((unsigned int *)(code + 22)) = 4 * offset;128#endif129}130131132#elif defined(USE_SPARC_ASM)133134extern void __glapi_sparc_icache_flush(unsigned int *);135136static void137init_glapi_relocs( void )138{139#if defined(HAVE_PTHREAD) || defined(USE_ELF_TLS)140static const unsigned int template[] = {141#ifdef USE_ELF_TLS1420x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */1430x8730e00a, /* srl %g3, 10, %g3 */1440x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */145#ifdef __arch64__1460xc259c002, /* ldx [%g7 + %g2], %g1 */1470xc2584003, /* ldx [%g1 + %g3], %g1 */148#else1490xc201c002, /* ld [%g7 + %g2], %g1 */1500xc2004003, /* ld [%g1 + %g3], %g1 */151#endif1520x81c04000, /* jmp %g1 */1530x01000000, /* nop */154#else155#ifdef __arch64__1560x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */1570x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */1580x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */1590x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */1600x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */1610x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */1620xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */163#else1640x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */1650x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */1660xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */167#endif1680x80a06000, /* --> cmp %g1, 0 */1690x02800005, /* --> be +4*5 */1700x01000000, /* --> nop */171#ifdef __arch64__1720xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */173#else1740xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */175#endif1760x81c04000, /* --> jmp %g1 */1770x01000000, /* --> nop */178#ifdef __arch64__1790x9de3bf80, /* 64-bit --> save %sp, -128, %sp */180#else1810x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */182#endif1830xa0100003, /* --> mov %g3, %l0 */1840x40000000, /* --> call _glapi_get_dispatch */1850x01000000, /* --> nop */1860x82100008, /* --> mov %o0, %g1 */1870x86100010, /* --> mov %l0, %g3 */1880x10bffff7, /* --> ba -4*9 */1890x81e80000, /* --> restore */190#endif191};192#ifdef USE_ELF_TLS193extern unsigned int __glapi_sparc_tls_stub;194extern unsigned long __glapi_sparc_get_dispatch(void);195unsigned int *code = &__glapi_sparc_tls_stub;196unsigned long dispatch = __glapi_sparc_get_dispatch();197#else198extern unsigned int __glapi_sparc_pthread_stub;199unsigned int *code = &__glapi_sparc_pthread_stub;200unsigned long dispatch = (unsigned long) &_glapi_Dispatch;201unsigned long call_dest = (unsigned long ) &_glapi_get_dispatch;202int idx;203#endif204205#ifdef USE_ELF_TLS206code[0] = template[0] | (dispatch >> 10);207code[1] = template[1];208__glapi_sparc_icache_flush(&code[0]);209code[2] = template[2] | (dispatch & 0x3ff);210code[3] = template[3];211__glapi_sparc_icache_flush(&code[2]);212code[4] = template[4];213code[5] = template[5];214__glapi_sparc_icache_flush(&code[4]);215code[6] = template[6];216__glapi_sparc_icache_flush(&code[6]);217#else218#if defined(__arch64__)219code[0] = template[0] | (dispatch >> (32 + 10));220code[1] = template[1] | ((dispatch & 0xffffffff) >> 10);221__glapi_sparc_icache_flush(&code[0]);222code[2] = template[2] | ((dispatch >> 32) & 0x3ff);223code[3] = template[3];224__glapi_sparc_icache_flush(&code[2]);225code[4] = template[4];226code[5] = template[5];227__glapi_sparc_icache_flush(&code[4]);228code[6] = template[6] | (dispatch & 0x3ff);229idx = 7;230#else231code[0] = template[0] | (dispatch >> 10);232code[1] = template[1];233__glapi_sparc_icache_flush(&code[0]);234code[2] = template[2] | (dispatch & 0x3ff);235idx = 3;236#endif237code[idx + 0] = template[idx + 0];238__glapi_sparc_icache_flush(&code[idx - 1]);239code[idx + 1] = template[idx + 1];240code[idx + 2] = template[idx + 2];241__glapi_sparc_icache_flush(&code[idx + 1]);242code[idx + 3] = template[idx + 3];243code[idx + 4] = template[idx + 4];244__glapi_sparc_icache_flush(&code[idx + 3]);245code[idx + 5] = template[idx + 5];246code[idx + 6] = template[idx + 6];247__glapi_sparc_icache_flush(&code[idx + 5]);248code[idx + 7] = template[idx + 7];249code[idx + 8] = template[idx + 8] |250(((call_dest - ((unsigned long) &code[idx + 8]))251>> 2) & 0x3fffffff);252__glapi_sparc_icache_flush(&code[idx + 7]);253code[idx + 9] = template[idx + 9];254code[idx + 10] = template[idx + 10];255__glapi_sparc_icache_flush(&code[idx + 9]);256code[idx + 11] = template[idx + 11];257code[idx + 12] = template[idx + 12];258__glapi_sparc_icache_flush(&code[idx + 11]);259code[idx + 13] = template[idx + 13];260__glapi_sparc_icache_flush(&code[idx + 13]);261#endif262#endif263}264265266_glapi_proc267generate_entrypoint(GLuint functionOffset)268{269#if defined(HAVE_PTHREAD) || defined(USE_ELF_TLS)270static const unsigned int template[] = {2710x07000000, /* sethi %hi(0), %g3 */2720x8210000f, /* mov %o7, %g1 */2730x40000000, /* call */2740x9e100001, /* mov %g1, %o7 */275};276#ifdef USE_ELF_TLS277extern unsigned int __glapi_sparc_tls_stub;278unsigned long call_dest = (unsigned long ) &__glapi_sparc_tls_stub;279#else280extern unsigned int __glapi_sparc_pthread_stub;281unsigned long call_dest = (unsigned long ) &__glapi_sparc_pthread_stub;282#endif283unsigned int *code = (unsigned int *) u_execmem_alloc(sizeof(template));284if (code) {285code[0] = template[0] | (functionOffset & 0x3fffff);286code[1] = template[1];287__glapi_sparc_icache_flush(&code[0]);288code[2] = template[2] |289(((call_dest - ((unsigned long) &code[2]))290>> 2) & 0x3fffffff);291code[3] = template[3];292__glapi_sparc_icache_flush(&code[2]);293}294return (_glapi_proc) code;295#endif296}297298299void300fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)301{302unsigned int *code = (unsigned int *) entrypoint;303304code[0] &= ~0x3fffff;305code[0] |= (offset * sizeof(void *)) & 0x3fffff;306__glapi_sparc_icache_flush(&code[0]);307}308309310#else /* USE_*_ASM */311312#if defined(HAVE_PTHREAD) || defined(USE_ELF_TLS)313static void314init_glapi_relocs( void )315{316}317#endif318319320_glapi_proc321generate_entrypoint(GLuint functionOffset)322{323(void) functionOffset;324return NULL;325}326327328void329fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)330{331/* an unimplemented architecture */332(void) entrypoint;333(void) offset;334}335336#endif /* USE_*_ASM */337338339void340init_glapi_relocs_once( void )341{342#if defined(HAVE_PTHREAD) || defined(USE_ELF_TLS)343static once_flag flag = ONCE_FLAG_INIT;344call_once(&flag, init_glapi_relocs);345#endif346}347348349