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
37
38 #ifdef USE_X86_ASM
39
40 extern GLubyte gl_dispatch_functions_start[];
41 extern GLubyte gl_dispatch_functions_end[];
42
43 #endif /* USE_X86_ASM */
44
45
46 #if defined(DISPATCH_FUNCTION_SIZE)
47
48 _glapi_proc
get_entrypoint_address(unsigned int functionOffset)49 get_entrypoint_address(unsigned int functionOffset)
50 {
51 return (_glapi_proc) (gl_dispatch_functions_start
52 + (DISPATCH_FUNCTION_SIZE * functionOffset));
53 }
54
55 #endif
56
57
58 #if defined(USE_X86_ASM)
59
60 /**
61 * Perform platform-specific GL API entry-point fixups.
62 */
63 static void
init_glapi_relocs(void)64 init_glapi_relocs( void )
65 {
66 #if !defined(GLX_X86_READONLY_TEXT)
67 extern unsigned long _x86_get_dispatch(void);
68 char run_time_patch[] = {
69 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
70 };
71 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
72 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
73 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
74
75 *offset = _x86_get_dispatch();
76 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
77 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
78 curr_func += DISPATCH_FUNCTION_SIZE;
79 }
80 #endif
81 }
82
83 #elif defined(USE_SPARC_ASM)
84
85 extern void __glapi_sparc_icache_flush(unsigned int *);
86
87 static void
init_glapi_relocs(void)88 init_glapi_relocs( void )
89 {
90 static const unsigned int template[] = {
91 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
92 0x8730e00a, /* srl %g3, 10, %g3 */
93 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
94 #ifdef __arch64__
95 0xc259c002, /* ldx [%g7 + %g2], %g1 */
96 0xc2584003, /* ldx [%g1 + %g3], %g1 */
97 #else
98 0xc201c002, /* ld [%g7 + %g2], %g1 */
99 0xc2004003, /* ld [%g1 + %g3], %g1 */
100 #endif
101 0x81c04000, /* jmp %g1 */
102 0x01000000, /* nop */
103 };
104 extern unsigned int __glapi_sparc_tls_stub;
105 extern unsigned long __glapi_sparc_get_dispatch(void);
106 unsigned int *code = &__glapi_sparc_tls_stub;
107 unsigned long dispatch = __glapi_sparc_get_dispatch();
108
109 code[0] = template[0] | (dispatch >> 10);
110 code[1] = template[1];
111 __glapi_sparc_icache_flush(&code[0]);
112 code[2] = template[2] | (dispatch & 0x3ff);
113 code[3] = template[3];
114 __glapi_sparc_icache_flush(&code[2]);
115 code[4] = template[4];
116 code[5] = template[5];
117 __glapi_sparc_icache_flush(&code[4]);
118 code[6] = template[6];
119 __glapi_sparc_icache_flush(&code[6]);
120 }
121
122
123 #else /* USE_*_ASM */
124
125 static void
init_glapi_relocs(void)126 init_glapi_relocs( void )
127 {
128 }
129
130 #endif /* USE_*_ASM */
131
132
133 void
init_glapi_relocs_once(void)134 init_glapi_relocs_once( void )
135 {
136 static once_flag flag = ONCE_FLAG_INIT;
137 call_once(&flag, init_glapi_relocs);
138 }
139