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 extern GLubyte gl_dispatch_functions_start[];
42 extern GLubyte gl_dispatch_functions_end[];
43
44 #endif /* USE_X86_ASM */
45
46
47 #if defined(DISPATCH_FUNCTION_SIZE)
48
49 _glapi_proc
get_entrypoint_address(unsigned int functionOffset)50 get_entrypoint_address(unsigned int functionOffset)
51 {
52 return (_glapi_proc) (gl_dispatch_functions_start
53 + (DISPATCH_FUNCTION_SIZE * functionOffset));
54 }
55
56 #endif
57
58
59 #if defined(USE_X86_ASM)
60
61 /**
62 * Perform platform-specific GL API entry-point fixups.
63 */
64 static void
init_glapi_relocs(void)65 init_glapi_relocs( void )
66 {
67 #if !defined(GLX_X86_READONLY_TEXT)
68 extern unsigned long _x86_get_dispatch(void);
69 char run_time_patch[] = {
70 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
71 };
72 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
73 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
74 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
75
76 *offset = _x86_get_dispatch();
77 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
78 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
79 curr_func += DISPATCH_FUNCTION_SIZE;
80 }
81 #endif
82 }
83
84
85 /**
86 * Generate a dispatch function (entrypoint) which jumps through
87 * the given slot number (offset) in the current dispatch table.
88 * We need assembly language in order to accomplish this.
89 */
90 _glapi_proc
generate_entrypoint(unsigned int functionOffset)91 generate_entrypoint(unsigned int functionOffset)
92 {
93 /* 32 is chosen as something of a magic offset. For x86, the dispatch
94 * at offset 32 is the first one where the offset in the
95 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
96 */
97 const GLubyte * const template_func = gl_dispatch_functions_start
98 + (DISPATCH_FUNCTION_SIZE * 32);
99 GLubyte * const code = (GLubyte *) u_execmem_alloc(DISPATCH_FUNCTION_SIZE);
100
101
102 if ( code != NULL ) {
103 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
104 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
105 }
106
107 return (_glapi_proc) code;
108 }
109
110
111 /**
112 * This function inserts a new dispatch offset into the assembly language
113 * stub that was generated with the preceeding function.
114 */
115 void
fill_in_entrypoint_offset(_glapi_proc entrypoint,unsigned int offset)116 fill_in_entrypoint_offset(_glapi_proc entrypoint, unsigned int offset)
117 {
118 GLubyte * const code = (GLubyte *) entrypoint;
119
120 *((unsigned int *)(code + 8)) = 4 * offset;
121 }
122
123
124 #elif defined(USE_SPARC_ASM)
125
126 extern void __glapi_sparc_icache_flush(unsigned int *);
127
128 static void
init_glapi_relocs(void)129 init_glapi_relocs( void )
130 {
131 static const unsigned int template[] = {
132 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
133 0x8730e00a, /* srl %g3, 10, %g3 */
134 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
135 #ifdef __arch64__
136 0xc259c002, /* ldx [%g7 + %g2], %g1 */
137 0xc2584003, /* ldx [%g1 + %g3], %g1 */
138 #else
139 0xc201c002, /* ld [%g7 + %g2], %g1 */
140 0xc2004003, /* ld [%g1 + %g3], %g1 */
141 #endif
142 0x81c04000, /* jmp %g1 */
143 0x01000000, /* nop */
144 };
145 extern unsigned int __glapi_sparc_tls_stub;
146 extern unsigned long __glapi_sparc_get_dispatch(void);
147 unsigned int *code = &__glapi_sparc_tls_stub;
148 unsigned long dispatch = __glapi_sparc_get_dispatch();
149
150 code[0] = template[0] | (dispatch >> 10);
151 code[1] = template[1];
152 __glapi_sparc_icache_flush(&code[0]);
153 code[2] = template[2] | (dispatch & 0x3ff);
154 code[3] = template[3];
155 __glapi_sparc_icache_flush(&code[2]);
156 code[4] = template[4];
157 code[5] = template[5];
158 __glapi_sparc_icache_flush(&code[4]);
159 code[6] = template[6];
160 __glapi_sparc_icache_flush(&code[6]);
161 }
162
163
164 _glapi_proc
generate_entrypoint(GLuint functionOffset)165 generate_entrypoint(GLuint functionOffset)
166 {
167 static const unsigned int template[] = {
168 0x07000000, /* sethi %hi(0), %g3 */
169 0x8210000f, /* mov %o7, %g1 */
170 0x40000000, /* call */
171 0x9e100001, /* mov %g1, %o7 */
172 };
173 extern unsigned int __glapi_sparc_tls_stub;
174 unsigned long call_dest = (unsigned long ) &__glapi_sparc_tls_stub;
175 unsigned int *code = (unsigned int *) u_execmem_alloc(sizeof(template));
176 if (code) {
177 code[0] = template[0] | (functionOffset & 0x3fffff);
178 code[1] = template[1];
179 __glapi_sparc_icache_flush(&code[0]);
180 code[2] = template[2] |
181 (((call_dest - ((unsigned long) &code[2]))
182 >> 2) & 0x3fffffff);
183 code[3] = template[3];
184 __glapi_sparc_icache_flush(&code[2]);
185 }
186 return (_glapi_proc) code;
187 }
188
189
190 void
fill_in_entrypoint_offset(_glapi_proc entrypoint,GLuint offset)191 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
192 {
193 unsigned int *code = (unsigned int *) entrypoint;
194
195 code[0] &= ~0x3fffff;
196 code[0] |= (offset * sizeof(void *)) & 0x3fffff;
197 __glapi_sparc_icache_flush(&code[0]);
198 }
199
200
201 #else /* USE_*_ASM */
202
203 static void
init_glapi_relocs(void)204 init_glapi_relocs( void )
205 {
206 }
207
208
209 _glapi_proc
generate_entrypoint(GLuint functionOffset)210 generate_entrypoint(GLuint functionOffset)
211 {
212 (void) functionOffset;
213 return NULL;
214 }
215
216
217 void
fill_in_entrypoint_offset(_glapi_proc entrypoint,GLuint offset)218 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
219 {
220 /* an unimplemented architecture */
221 (void) entrypoint;
222 (void) offset;
223 }
224
225 #endif /* USE_*_ASM */
226
227
228 void
init_glapi_relocs_once(void)229 init_glapi_relocs_once( void )
230 {
231 static once_flag flag = ONCE_FLAG_INIT;
232 call_once(&flag, init_glapi_relocs);
233 }
234