Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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