1 | /* $Id: fakedri_drv.c 34211 2010-11-19 16:55:04Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * VBox OpenGL DRI driver functions
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2009 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #define _GNU_SOURCE 1
|
---|
20 |
|
---|
21 | #include "cr_error.h"
|
---|
22 | #include "cr_gl.h"
|
---|
23 | #include "cr_mem.h"
|
---|
24 | #include "stub.h"
|
---|
25 | #include "fakedri_drv.h"
|
---|
26 | #include "dri_glx.h"
|
---|
27 | #include "iprt/mem.h"
|
---|
28 | #include "iprt/err.h"
|
---|
29 | #include <dlfcn.h>
|
---|
30 | #include <elf.h>
|
---|
31 | #include <unistd.h>
|
---|
32 | #include "xf86.h"
|
---|
33 |
|
---|
34 | #define VBOX_NO_MESA_PATCH_REPORTS
|
---|
35 |
|
---|
36 | //#define DEBUG_DRI_CALLS
|
---|
37 |
|
---|
38 | //@todo this could be different...
|
---|
39 | #ifdef RT_ARCH_AMD64
|
---|
40 | # define DRI_DEFAULT_DRIVER_DIR "/usr/lib64/dri:/usr/lib/dri"
|
---|
41 | # define DRI_XORG_DRV_DIR "/usr/lib/xorg/modules/drivers/"
|
---|
42 | #else
|
---|
43 | # define DRI_DEFAULT_DRIVER_DIR "/usr/lib/dri"
|
---|
44 | # define DRI_XORG_DRV_DIR "/usr/lib/xorg/modules/drivers/"
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #ifdef DEBUG_DRI_CALLS
|
---|
48 | #define SWDRI_SHOWNAME(pext, func) \
|
---|
49 | crDebug("SWDRI: sc %s->%s", #pext, #func)
|
---|
50 | #else
|
---|
51 | #define SWDRI_SHOWNAME(pext, func)
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #define SWDRI_SAFECALL(pext, func, ...) \
|
---|
55 | SWDRI_SHOWNAME(pext, func); \
|
---|
56 | if (pext && pext->func){ \
|
---|
57 | (*pext->func)(__VA_ARGS__); \
|
---|
58 | } else { \
|
---|
59 | crDebug("swcore_call NULL for "#func); \
|
---|
60 | }
|
---|
61 |
|
---|
62 | #define SWDRI_SAFERET(pext, func, ...) \
|
---|
63 | SWDRI_SHOWNAME(pext, func); \
|
---|
64 | if (pext && pext->func){ \
|
---|
65 | return (*pext->func)(__VA_ARGS__); \
|
---|
66 | } else { \
|
---|
67 | crDebug("swcore_call NULL for "#func); \
|
---|
68 | return 0; \
|
---|
69 | }
|
---|
70 |
|
---|
71 | #define SWDRI_SAFERET_CORE(func, ...) SWDRI_SAFERET(gpSwDriCoreExternsion, func, __VA_ARGS__)
|
---|
72 | #define SWDRI_SAFECALL_CORE(func, ...) SWDRI_SAFECALL(gpSwDriCoreExternsion, func, __VA_ARGS__)
|
---|
73 | #define SWDRI_SAFERET_SWRAST(func, ...) SWDRI_SAFERET(gpSwDriSwrastExtension, func, __VA_ARGS__)
|
---|
74 | #define SWDRI_SAFECALL_SWRAST(func, ...) SWDRI_SAFECALL(gpSwDriSwrastExtension, func, __VA_ARGS__)
|
---|
75 |
|
---|
76 | #ifndef PAGESIZE
|
---|
77 | #define PAGESIZE 4096
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | #ifdef RT_ARCH_AMD64
|
---|
81 | # define DRI_ELFSYM Elf64_Sym
|
---|
82 | #else
|
---|
83 | # define DRI_ELFSYM Elf32_Sym
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | #ifdef RT_ARCH_AMD64
|
---|
87 | typedef struct _FAKEDRI_PatchNode
|
---|
88 | {
|
---|
89 | const char* psFuncName;
|
---|
90 | void *pDstStart, *pDstEnd;
|
---|
91 | const void *pSrcStart, *pSrcEnd;
|
---|
92 |
|
---|
93 | struct _FAKEDRI_PatchNode *pNext;
|
---|
94 | } FAKEDRI_PatchNode;
|
---|
95 | static FAKEDRI_PatchNode *g_pFreeList=NULL, *g_pRepatchList=NULL;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | static struct _glapi_table vbox_glapi_table;
|
---|
99 | fakedri_glxapi_table glxim;
|
---|
100 |
|
---|
101 | static const __DRIextension **gppSwDriExternsion = NULL;
|
---|
102 | static const __DRIcoreExtension *gpSwDriCoreExternsion = NULL;
|
---|
103 | static const __DRIswrastExtension *gpSwDriSwrastExtension = NULL;
|
---|
104 |
|
---|
105 | extern const __DRIextension * __driDriverExtensions[];
|
---|
106 |
|
---|
107 | #define GLAPI_ENTRY(Func) pGLTable->Func = cr_gl##Func;
|
---|
108 | static void
|
---|
109 | vboxFillMesaGLAPITable(struct _glapi_table *pGLTable)
|
---|
110 | {
|
---|
111 | #include "fakedri_glfuncsList.h"
|
---|
112 |
|
---|
113 | pGLTable->SampleMaskSGIS = cr_glSampleMaskEXT;
|
---|
114 | pGLTable->SamplePatternSGIS = cr_glSamplePatternEXT;
|
---|
115 | pGLTable->WindowPos2dMESA = cr_glWindowPos2d;
|
---|
116 | pGLTable->WindowPos2dvMESA = cr_glWindowPos2dv;
|
---|
117 | pGLTable->WindowPos2fMESA = cr_glWindowPos2f;
|
---|
118 | pGLTable->WindowPos2fvMESA = cr_glWindowPos2fv;
|
---|
119 | pGLTable->WindowPos2iMESA = cr_glWindowPos2i;
|
---|
120 | pGLTable->WindowPos2ivMESA = cr_glWindowPos2iv;
|
---|
121 | pGLTable->WindowPos2sMESA = cr_glWindowPos2s;
|
---|
122 | pGLTable->WindowPos2svMESA = cr_glWindowPos2sv;
|
---|
123 | pGLTable->WindowPos3dMESA = cr_glWindowPos3d;
|
---|
124 | pGLTable->WindowPos3dvMESA = cr_glWindowPos3dv;
|
---|
125 | pGLTable->WindowPos3fMESA = cr_glWindowPos3f;
|
---|
126 | pGLTable->WindowPos3fvMESA = cr_glWindowPos3fv;
|
---|
127 | pGLTable->WindowPos3iMESA = cr_glWindowPos3i;
|
---|
128 | pGLTable->WindowPos3ivMESA = cr_glWindowPos3iv;
|
---|
129 | pGLTable->WindowPos3sMESA = cr_glWindowPos3s;
|
---|
130 | pGLTable->WindowPos3svMESA = cr_glWindowPos3sv;
|
---|
131 | };
|
---|
132 | #undef GLAPI_ENTRY
|
---|
133 |
|
---|
134 | #define GLXAPI_ENTRY(Func) pGLXTable->Func = VBOXGLXTAG(glX##Func);
|
---|
135 | static void
|
---|
136 | vboxFillGLXAPITable(fakedri_glxapi_table *pGLXTable)
|
---|
137 | {
|
---|
138 | #include "fakedri_glxfuncsList.h"
|
---|
139 | }
|
---|
140 | #undef GLXAPI_ENTRY
|
---|
141 |
|
---|
142 | static void
|
---|
143 | vboxApplyPatch(const char* psFuncName, void *pDst, const void *pSrc, unsigned long size)
|
---|
144 | {
|
---|
145 | void *alPatch;
|
---|
146 | int rv;
|
---|
147 |
|
---|
148 | /* Get aligned start address we're going to patch*/
|
---|
149 | alPatch = (void*) ((uintptr_t)pDst & ~(uintptr_t)(PAGESIZE-1));
|
---|
150 |
|
---|
151 | #ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
152 | crDebug("MProtecting: %p, %li", alPatch, pDst-alPatch+size);
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | /* Get write access to mesa functions */
|
---|
156 | rv = RTMemProtect(alPatch, pDst-alPatch+size, RTMEM_PROT_READ|RTMEM_PROT_WRITE|RTMEM_PROT_EXEC);
|
---|
157 | if (RT_FAILURE(rv))
|
---|
158 | {
|
---|
159 | crError("mprotect failed with %x (%s)", rv, psFuncName);
|
---|
160 | }
|
---|
161 |
|
---|
162 | #ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
163 | crDebug("Writing %li bytes to %p from %p", size, pDst, pSrc);
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | crMemcpy(pDst, pSrc, size);
|
---|
167 |
|
---|
168 | /*@todo Restore the protection, probably have to check what was it before us...*/
|
---|
169 | rv = RTMemProtect(alPatch, pDst-alPatch+size, RTMEM_PROT_READ|RTMEM_PROT_EXEC);
|
---|
170 | if (RT_FAILURE(rv))
|
---|
171 | {
|
---|
172 | crError("mprotect2 failed with %x (%s)", rv, psFuncName);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | #define FAKEDRI_JMP64_PATCH_SIZE 13
|
---|
177 |
|
---|
178 | static void
|
---|
179 | vboxPatchMesaExport(const char* psFuncName, const void *pStart, const void *pEnd)
|
---|
180 | {
|
---|
181 | Dl_info dlip;
|
---|
182 | DRI_ELFSYM* sym=0;
|
---|
183 | int rv;
|
---|
184 | void *alPatch;
|
---|
185 | void *pMesaEntry;
|
---|
186 | char patch[FAKEDRI_JMP64_PATCH_SIZE];
|
---|
187 | void *shift;
|
---|
188 | int ignore_size=false;
|
---|
189 |
|
---|
190 | #ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
191 | crDebug("\nvboxPatchMesaExport: %s", psFuncName);
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | pMesaEntry = dlsym(RTLD_DEFAULT, psFuncName);
|
---|
195 |
|
---|
196 | if (!pMesaEntry)
|
---|
197 | {
|
---|
198 | crDebug("%s not defined in current scope, are we being loaded by mesa's libGL.so?", psFuncName);
|
---|
199 | return;
|
---|
200 | }
|
---|
201 |
|
---|
202 | rv = dladdr1(pMesaEntry, &dlip, (void**)&sym, RTLD_DL_SYMENT);
|
---|
203 | if (!rv || !sym)
|
---|
204 | {
|
---|
205 | crError("Failed to get size for %p(%s)", pMesaEntry, psFuncName);
|
---|
206 | return;
|
---|
207 | }
|
---|
208 |
|
---|
209 | #if VBOX_OGL_GLX_USE_CSTUBS
|
---|
210 | {
|
---|
211 | Dl_info dlip1;
|
---|
212 | DRI_ELFSYM* sym1=0;
|
---|
213 | int rv;
|
---|
214 |
|
---|
215 | rv = dladdr1(pStart, &dlip1, (void**)&sym1, RTLD_DL_SYMENT);
|
---|
216 | if (!rv || !sym1)
|
---|
217 | {
|
---|
218 | crError("Failed to get size for %p", pStart);
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 | pEnd = pStart + sym1->st_size;
|
---|
223 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
224 | crDebug("VBox Entry: %p, start: %p(%s:%s), size: %li", pStart, dlip1.dli_saddr, dlip1.dli_fname, dlip1.dli_sname, sym1->st_size);
|
---|
225 | # endif
|
---|
226 | }
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | #ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
230 | crDebug("Mesa Entry: %p, start: %p(%s:%s), size: %li", pMesaEntry, dlip.dli_saddr, dlip.dli_fname, dlip.dli_sname, sym->st_size);
|
---|
231 | crDebug("Vbox code: start: %p, end %p, size: %li", pStart, pEnd, pEnd-pStart);
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | #ifndef VBOX_OGL_GLX_USE_CSTUBS
|
---|
235 | if (sym->st_size<(pEnd-pStart))
|
---|
236 | #endif
|
---|
237 | {
|
---|
238 | #ifdef RT_ARCH_AMD64
|
---|
239 | int64_t offset;
|
---|
240 | #endif
|
---|
241 | /* Try to insert 5 bytes jmp/jmpq to our stub code */
|
---|
242 |
|
---|
243 | if (sym->st_size<5)
|
---|
244 | {
|
---|
245 | /*@todo we don't really know the size of targeted static function, but it's long enough in practice. We will also patch same place twice, but it's ok.*/
|
---|
246 | if (!crStrcmp(psFuncName, "glXDestroyContext") || !crStrcmp(psFuncName, "glXFreeContextEXT"))
|
---|
247 | {
|
---|
248 | if (((unsigned char*)dlip.dli_saddr)[0]==0xEB)
|
---|
249 | {
|
---|
250 | /*it's a rel8 jmp, so we're going to patch the place it targets instead of jmp itself*/
|
---|
251 | dlip.dli_saddr = (void*) ((intptr_t)dlip.dli_saddr + ((char*)dlip.dli_saddr)[1] + 2);
|
---|
252 | ignore_size = true;
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | crError("Can't patch size is too small.(%s)", psFuncName);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | else if (!crStrcmp(psFuncName, "glXCreateGLXPixmapMESA"))
|
---|
261 | {
|
---|
262 | /*@todo it's just a return 0, which we're fine with for now*/
|
---|
263 | return;
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | crError("Can't patch size is too small.(%s)", psFuncName);
|
---|
268 | return;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | shift = (void*)((intptr_t)pStart-((intptr_t)dlip.dli_saddr+5));
|
---|
273 | #ifdef RT_ARCH_AMD64
|
---|
274 | offset = (intptr_t)shift;
|
---|
275 | if (offset>INT32_MAX || offset<INT32_MIN)
|
---|
276 | {
|
---|
277 | /*try to insert 64bit abs jmp*/
|
---|
278 | if (sym->st_size>=FAKEDRI_JMP64_PATCH_SIZE || ignore_size)
|
---|
279 | {
|
---|
280 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
281 | crDebug("Inserting movq/jmp instead");
|
---|
282 | # endif
|
---|
283 | /*add 64bit abs jmp*/
|
---|
284 | patch[0] = 0x49; /*movq %r11,imm64*/
|
---|
285 | patch[1] = 0xBB;
|
---|
286 | crMemcpy(&patch[2], &pStart, 8);
|
---|
287 | patch[10] = 0x41; /*jmp *%r11*/
|
---|
288 | patch[11] = 0xFF;
|
---|
289 | patch[12] = 0xE3;
|
---|
290 | pStart = &patch[0];
|
---|
291 | pEnd = &patch[FAKEDRI_JMP64_PATCH_SIZE];
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | FAKEDRI_PatchNode *pNode;
|
---|
296 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
297 | crDebug("Can't patch offset is too big. Pushing for 2nd pass(%s)", psFuncName);
|
---|
298 | # endif
|
---|
299 | /*Add patch node to repatch with chain jmps in 2nd pass*/
|
---|
300 | pNode = (FAKEDRI_PatchNode *)crAlloc(sizeof(FAKEDRI_PatchNode));
|
---|
301 | if (!pNode)
|
---|
302 | {
|
---|
303 | crError("Not enough memory.");
|
---|
304 | return;
|
---|
305 | }
|
---|
306 | pNode->psFuncName = psFuncName;
|
---|
307 | pNode->pDstStart = dlip.dli_saddr;
|
---|
308 | pNode->pDstEnd = dlip.dli_saddr+sym->st_size;
|
---|
309 | pNode->pSrcStart = pStart;
|
---|
310 | pNode->pSrcEnd = pEnd;
|
---|
311 | pNode->pNext = g_pRepatchList;
|
---|
312 | g_pRepatchList = pNode;
|
---|
313 | return;
|
---|
314 | }
|
---|
315 | }
|
---|
316 | else
|
---|
317 | #endif
|
---|
318 | {
|
---|
319 | #ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
320 | crDebug("Inserting jmp[q] with shift %p instead", shift);
|
---|
321 | #endif
|
---|
322 | patch[0] = 0xE9;
|
---|
323 | crMemcpy(&patch[1], &shift, 4);
|
---|
324 | pStart = &patch[0];
|
---|
325 | pEnd = &patch[5];
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | vboxApplyPatch(psFuncName, dlip.dli_saddr, pStart, pEnd-pStart);
|
---|
330 |
|
---|
331 | #ifdef RT_ARCH_AMD64
|
---|
332 | /*Add rest of mesa function body to free list*/
|
---|
333 | if (sym->st_size-(pEnd-pStart)>=FAKEDRI_JMP64_PATCH_SIZE)
|
---|
334 | {
|
---|
335 | FAKEDRI_PatchNode *pNode = (FAKEDRI_PatchNode *)crAlloc(sizeof(FAKEDRI_PatchNode));
|
---|
336 | if (pNode)
|
---|
337 | {
|
---|
338 | pNode->psFuncName = psFuncName;
|
---|
339 | pNode->pDstStart = dlip.dli_saddr+(pEnd-pStart);
|
---|
340 | pNode->pDstEnd = dlip.dli_saddr+sym->st_size;
|
---|
341 | pNode->pSrcStart = dlip.dli_saddr;
|
---|
342 | pNode->pSrcEnd = NULL;
|
---|
343 | pNode->pNext = g_pFreeList;
|
---|
344 | g_pFreeList = pNode;
|
---|
345 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
346 | crDebug("Added free node %s, func start=%p, free start=%p, size=%#lx",
|
---|
347 | psFuncName, pNode->pSrcStart, pNode->pDstStart, pNode->pDstEnd-pNode->pDstStart);
|
---|
348 | # endif
|
---|
349 | }
|
---|
350 | }
|
---|
351 | #endif
|
---|
352 | }
|
---|
353 |
|
---|
354 | #ifdef RT_ARCH_AMD64
|
---|
355 | static void
|
---|
356 | vboxRepatchMesaExports(void)
|
---|
357 | {
|
---|
358 | FAKEDRI_PatchNode *pFreeNode, *pPatchNode;
|
---|
359 | int64_t offset;
|
---|
360 | char patch[FAKEDRI_JMP64_PATCH_SIZE];
|
---|
361 |
|
---|
362 | pPatchNode = g_pRepatchList;
|
---|
363 | while (pPatchNode)
|
---|
364 | {
|
---|
365 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
366 | crDebug("\nvboxRepatchMesaExports %s", pPatchNode->psFuncName);
|
---|
367 | # endif
|
---|
368 | /*find free place in mesa functions, to place 64bit jump to our stub code*/
|
---|
369 | pFreeNode = g_pFreeList;
|
---|
370 | while (pFreeNode)
|
---|
371 | {
|
---|
372 | if (pFreeNode->pDstEnd-pFreeNode->pDstStart>=FAKEDRI_JMP64_PATCH_SIZE)
|
---|
373 | {
|
---|
374 | offset = ((intptr_t)pFreeNode->pDstStart-((intptr_t)pPatchNode->pDstStart+5));
|
---|
375 | if (offset<=INT32_MAX && offset>=INT32_MIN)
|
---|
376 | {
|
---|
377 | break;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | pFreeNode=pFreeNode->pNext;
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (!pFreeNode)
|
---|
384 | {
|
---|
385 | crError("Failed to find free space, to place repatch for %s.", pPatchNode->psFuncName);
|
---|
386 | return;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /*add 32bit rel jmp, from mesa orginal function to free space in other mesa function*/
|
---|
390 | patch[0] = 0xE9;
|
---|
391 | crMemcpy(&patch[1], &offset, 4);
|
---|
392 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
393 | crDebug("Adding jmp from mesa %s to mesa %s+%#lx", pPatchNode->psFuncName, pFreeNode->psFuncName,
|
---|
394 | pFreeNode->pDstStart-pFreeNode->pSrcStart);
|
---|
395 | # endif
|
---|
396 | vboxApplyPatch(pPatchNode->psFuncName, pPatchNode->pDstStart, &patch[0], 5);
|
---|
397 |
|
---|
398 | /*add 64bit abs jmp, from free space to our stub code*/
|
---|
399 | patch[0] = 0x49; /*movq %r11,imm64*/
|
---|
400 | patch[1] = 0xBB;
|
---|
401 | crMemcpy(&patch[2], &pPatchNode->pSrcStart, 8);
|
---|
402 | patch[10] = 0x41; /*jmp *%r11*/
|
---|
403 | patch[11] = 0xFF;
|
---|
404 | patch[12] = 0xE3;
|
---|
405 | # ifndef VBOX_NO_MESA_PATCH_REPORTS
|
---|
406 | crDebug("Adding jmp from mesa %s+%#lx to vbox %s", pFreeNode->psFuncName, pFreeNode->pDstStart-pFreeNode->pSrcStart,
|
---|
407 | pPatchNode->psFuncName);
|
---|
408 | # endif
|
---|
409 | vboxApplyPatch(pFreeNode->psFuncName, pFreeNode->pDstStart, &patch[0], FAKEDRI_JMP64_PATCH_SIZE);
|
---|
410 | /*mark this space as used*/
|
---|
411 | pFreeNode->pDstStart = pFreeNode->pDstStart+FAKEDRI_JMP64_PATCH_SIZE;
|
---|
412 |
|
---|
413 | pPatchNode = pPatchNode->pNext;
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | static void
|
---|
418 | vboxFakeDriFreeList(FAKEDRI_PatchNode *pList)
|
---|
419 | {
|
---|
420 | FAKEDRI_PatchNode *pNode;
|
---|
421 |
|
---|
422 | while (pList)
|
---|
423 | {
|
---|
424 | pNode=pList;
|
---|
425 | pList=pNode->pNext;
|
---|
426 | crFree(pNode);
|
---|
427 | }
|
---|
428 | }
|
---|
429 | #endif
|
---|
430 |
|
---|
431 | #ifdef VBOX_OGL_GLX_USE_CSTUBS
|
---|
432 | static void
|
---|
433 | # define GLXAPI_ENTRY(Func) vboxPatchMesaExport("glX"#Func, &vbox_glX##Func, NULL);
|
---|
434 | vboxPatchMesaExports()
|
---|
435 | #else
|
---|
436 | static void
|
---|
437 | # define GLXAPI_ENTRY(Func) vboxPatchMesaExport("glX"#Func, &vbox_glX##Func, &vbox_glX##Func##_EndProc);
|
---|
438 | vboxPatchMesaExports()
|
---|
439 | #endif
|
---|
440 | {
|
---|
441 | crDebug("Patching mesa glx entries");
|
---|
442 | #include "fakedri_glxfuncsList.h"
|
---|
443 |
|
---|
444 | #ifdef RT_ARCH_AMD64
|
---|
445 | vboxRepatchMesaExports();
|
---|
446 | vboxFakeDriFreeList(g_pRepatchList);
|
---|
447 | g_pRepatchList = NULL;
|
---|
448 | vboxFakeDriFreeList(g_pFreeList);
|
---|
449 | g_pFreeList = NULL;
|
---|
450 | #endif
|
---|
451 | }
|
---|
452 | #undef GLXAPI_ENTRY
|
---|
453 |
|
---|
454 | bool vbox_load_sw_dri()
|
---|
455 | {
|
---|
456 | const char *libPaths, *p, *next;;
|
---|
457 | char realDriverName[200];
|
---|
458 | void *handle;
|
---|
459 | int len, i;
|
---|
460 |
|
---|
461 | /*code from Mesa-7.2/src/glx/x11/dri_common.c:driOpenDriver*/
|
---|
462 |
|
---|
463 | libPaths = NULL;
|
---|
464 | if (geteuid() == getuid()) {
|
---|
465 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
|
---|
466 | libPaths = getenv("LIBGL_DRIVERS_PATH");
|
---|
467 | if (!libPaths)
|
---|
468 | libPaths = getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
|
---|
469 | }
|
---|
470 | if (libPaths == NULL)
|
---|
471 | libPaths = DRI_DEFAULT_DRIVER_DIR;
|
---|
472 |
|
---|
473 | handle = NULL;
|
---|
474 | for (p = libPaths; *p; p = next)
|
---|
475 | {
|
---|
476 | next = strchr(p, ':');
|
---|
477 | if (next == NULL)
|
---|
478 | {
|
---|
479 | len = strlen(p);
|
---|
480 | next = p + len;
|
---|
481 | }
|
---|
482 | else
|
---|
483 | {
|
---|
484 | len = next - p;
|
---|
485 | next++;
|
---|
486 | }
|
---|
487 |
|
---|
488 | snprintf(realDriverName, sizeof realDriverName, "%.*s/%s_dri.so", len, p, "swrast");
|
---|
489 | crDebug("trying %s", realDriverName);
|
---|
490 | handle = dlopen(realDriverName, RTLD_NOW | RTLD_LOCAL);
|
---|
491 | if (handle) break;
|
---|
492 | }
|
---|
493 |
|
---|
494 | /*end code*/
|
---|
495 |
|
---|
496 | if (handle) gppSwDriExternsion = dlsym(handle, "__driDriverExtensions");
|
---|
497 |
|
---|
498 | if (!gppSwDriExternsion)
|
---|
499 | {
|
---|
500 | crDebug("%s doesn't export __driDriverExtensions", realDriverName);
|
---|
501 | return false;
|
---|
502 | }
|
---|
503 | crDebug("loaded %s", realDriverName);
|
---|
504 |
|
---|
505 | for (i = 0; gppSwDriExternsion[i]; i++)
|
---|
506 | {
|
---|
507 | if (strcmp(gppSwDriExternsion[i]->name, __DRI_CORE) == 0)
|
---|
508 | gpSwDriCoreExternsion = (__DRIcoreExtension *) gppSwDriExternsion[i];
|
---|
509 | if (strcmp(gppSwDriExternsion[i]->name, __DRI_SWRAST) == 0)
|
---|
510 | gpSwDriSwrastExtension = (__DRIswrastExtension *) gppSwDriExternsion[i];
|
---|
511 | }
|
---|
512 |
|
---|
513 | return gpSwDriCoreExternsion && gpSwDriSwrastExtension;
|
---|
514 | }
|
---|
515 |
|
---|
516 | void __attribute__ ((constructor)) vbox_install_into_mesa(void)
|
---|
517 | {
|
---|
518 | {
|
---|
519 | #ifdef _X_ATTRIBUTE_PRINTF
|
---|
520 | void (*pxf86Msg)(MessageType type, const char *format, ...) _X_ATTRIBUTE_PRINTF(2,3);
|
---|
521 | #else
|
---|
522 | void (*pxf86Msg)(MessageType type, const char *format, ...) _printf_attribute(2,3);
|
---|
523 | #endif
|
---|
524 |
|
---|
525 | pxf86Msg = dlsym(RTLD_DEFAULT, "xf86Msg");
|
---|
526 | if (pxf86Msg)
|
---|
527 | {
|
---|
528 | pxf86Msg(X_INFO, "Next line is added to allow vboxvideo_drv.so to appear as whitelisted driver\n");
|
---|
529 | pxf86Msg(X_INFO, "The file referenced, is *NOT* loaded\n");
|
---|
530 | pxf86Msg(X_INFO, "Loading %s/ati_drv.so\n", DRI_XORG_DRV_DIR);
|
---|
531 |
|
---|
532 | /* we're failing to proxy software dri driver calls for certain xservers, so just make sure we're unloaded for now */
|
---|
533 | __driDriverExtensions[0] = NULL;
|
---|
534 | return;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | if (!stubInit())
|
---|
539 | {
|
---|
540 | crDebug("vboxdriInitScreen: stubInit failed");
|
---|
541 | return;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* Load swrast_dri.so to proxy dri related calls there. */
|
---|
545 | if (!vbox_load_sw_dri())
|
---|
546 | {
|
---|
547 | crDebug("vboxdriInitScreen: vbox_load_sw_dri failed...going to fail badly");
|
---|
548 | return;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* Handle gl api.
|
---|
552 | * In the end application call would look like this:
|
---|
553 | * app call glFoo->(mesa asm dispatch stub)->cr_glFoo(vbox asm dispatch stub)->SPU Foo function(packspuFoo or alike)
|
---|
554 | * Note, we don't need to install extension functions via _glapi_add_dispatch, because we'd override glXGetProcAddress.
|
---|
555 | */
|
---|
556 | /* We don't support all mesa's functions. Initialize our table to mesa dispatch first*/
|
---|
557 | crMemcpy(&vbox_glapi_table, _glapi_get_dispatch(), sizeof(struct _glapi_table));
|
---|
558 | /* Now install our assembly dispatch entries into table */
|
---|
559 | vboxFillMesaGLAPITable(&vbox_glapi_table);
|
---|
560 | /* Install our dispatch table into mesa */
|
---|
561 | _glapi_set_dispatch(&vbox_glapi_table);
|
---|
562 |
|
---|
563 | /* Handle glx api.
|
---|
564 | * In the end application call would look like this:
|
---|
565 | * app call glxFoo->(mesa asm dispatch stub patched with vbox_glXFoo:jmp glxim[Foo's index])->VBOXGLXTAG(glxFoo)
|
---|
566 | */
|
---|
567 | /* Fill structure used by our assembly stubs */
|
---|
568 | vboxFillGLXAPITable(&glxim);
|
---|
569 | /* Now patch functions exported by libGL.so */
|
---|
570 | vboxPatchMesaExports();
|
---|
571 | }
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * @todo we're missing first glx related call from the client application.
|
---|
575 | * Luckily, this doesn't add much problems, except for some cases.
|
---|
576 | */
|
---|
577 |
|
---|
578 | /* __DRIcoreExtension */
|
---|
579 |
|
---|
580 | static __DRIscreen *
|
---|
581 | vboxdriCreateNewScreen(int screen, int fd, unsigned int sarea_handle,
|
---|
582 | const __DRIextension **extensions, const __DRIconfig ***driverConfigs,
|
---|
583 | void *loaderPrivate)
|
---|
584 | {
|
---|
585 | (void) fd;
|
---|
586 | (void) sarea_handle;
|
---|
587 | SWDRI_SAFERET_SWRAST(createNewScreen, screen, extensions, driverConfigs, loaderPrivate);
|
---|
588 | }
|
---|
589 |
|
---|
590 | static void
|
---|
591 | vboxdriDestroyScreen(__DRIscreen *screen)
|
---|
592 | {
|
---|
593 | SWDRI_SAFECALL_CORE(destroyScreen, screen);
|
---|
594 | }
|
---|
595 |
|
---|
596 | static const __DRIextension **
|
---|
597 | vboxdriGetExtensions(__DRIscreen *screen)
|
---|
598 | {
|
---|
599 | SWDRI_SAFERET_CORE(getExtensions, screen);
|
---|
600 | }
|
---|
601 |
|
---|
602 | static int
|
---|
603 | vboxdriGetConfigAttrib(const __DRIconfig *config,
|
---|
604 | unsigned int attrib,
|
---|
605 | unsigned int *value)
|
---|
606 | {
|
---|
607 | SWDRI_SAFERET_CORE(getConfigAttrib, config, attrib, value);
|
---|
608 | }
|
---|
609 |
|
---|
610 | static int
|
---|
611 | vboxdriIndexConfigAttrib(const __DRIconfig *config, int index,
|
---|
612 | unsigned int *attrib, unsigned int *value)
|
---|
613 | {
|
---|
614 | SWDRI_SAFERET_CORE(indexConfigAttrib, config, index, attrib, value);
|
---|
615 | }
|
---|
616 |
|
---|
617 | static __DRIdrawable *
|
---|
618 | vboxdriCreateNewDrawable(__DRIscreen *screen,
|
---|
619 | const __DRIconfig *config,
|
---|
620 | unsigned int drawable_id,
|
---|
621 | unsigned int head,
|
---|
622 | void *loaderPrivate)
|
---|
623 | {
|
---|
624 | (void) drawable_id;
|
---|
625 | (void) head;
|
---|
626 | SWDRI_SAFERET_SWRAST(createNewDrawable, screen, config, loaderPrivate);
|
---|
627 | }
|
---|
628 |
|
---|
629 | static void
|
---|
630 | vboxdriDestroyDrawable(__DRIdrawable *drawable)
|
---|
631 | {
|
---|
632 | SWDRI_SAFECALL_CORE(destroyDrawable, drawable);
|
---|
633 | }
|
---|
634 |
|
---|
635 | static void
|
---|
636 | vboxdriSwapBuffers(__DRIdrawable *drawable)
|
---|
637 | {
|
---|
638 | SWDRI_SAFECALL_CORE(swapBuffers, drawable);
|
---|
639 | }
|
---|
640 |
|
---|
641 | static __DRIcontext *
|
---|
642 | vboxdriCreateNewContext(__DRIscreen *screen,
|
---|
643 | const __DRIconfig *config,
|
---|
644 | __DRIcontext *shared,
|
---|
645 | void *loaderPrivate)
|
---|
646 | {
|
---|
647 | SWDRI_SAFERET_CORE(createNewContext, screen, config, shared, loaderPrivate);
|
---|
648 | }
|
---|
649 |
|
---|
650 | static int
|
---|
651 | vboxdriCopyContext(__DRIcontext *dest,
|
---|
652 | __DRIcontext *src,
|
---|
653 | unsigned long mask)
|
---|
654 | {
|
---|
655 | SWDRI_SAFERET_CORE(copyContext, dest, src, mask);
|
---|
656 | }
|
---|
657 |
|
---|
658 | static void
|
---|
659 | vboxdriDestroyContext(__DRIcontext *context)
|
---|
660 | {
|
---|
661 | SWDRI_SAFECALL_CORE(destroyContext, context);
|
---|
662 | }
|
---|
663 |
|
---|
664 | static int
|
---|
665 | vboxdriBindContext(__DRIcontext *ctx,
|
---|
666 | __DRIdrawable *pdraw,
|
---|
667 | __DRIdrawable *pread)
|
---|
668 | {
|
---|
669 | SWDRI_SAFERET_CORE(bindContext, ctx, pdraw, pread);
|
---|
670 | }
|
---|
671 |
|
---|
672 | static int
|
---|
673 | vboxdriUnbindContext(__DRIcontext *ctx)
|
---|
674 | {
|
---|
675 | SWDRI_SAFERET_CORE(unbindContext, ctx)
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* __DRIlegacyExtension */
|
---|
679 |
|
---|
680 | static __DRIscreen *
|
---|
681 | vboxdriCreateNewScreen_Legacy(int scrn,
|
---|
682 | const __DRIversion *ddx_version,
|
---|
683 | const __DRIversion *dri_version,
|
---|
684 | const __DRIversion *drm_version,
|
---|
685 | const __DRIframebuffer *frame_buffer,
|
---|
686 | drmAddress pSAREA, int fd,
|
---|
687 | const __DRIextension **extensions,
|
---|
688 | const __DRIconfig ***driver_modes,
|
---|
689 | void *loaderPrivate)
|
---|
690 | {
|
---|
691 | (void) ddx_version;
|
---|
692 | (void) dri_version;
|
---|
693 | (void) frame_buffer;
|
---|
694 | (void) pSAREA;
|
---|
695 | (void) fd;
|
---|
696 | SWDRI_SAFERET_SWRAST(createNewScreen, scrn, extensions, driver_modes, loaderPrivate);
|
---|
697 | }
|
---|
698 |
|
---|
699 | static __DRIdrawable *
|
---|
700 | vboxdriCreateNewDrawable_Legacy(__DRIscreen *psp, const __DRIconfig *config,
|
---|
701 | drm_drawable_t hwDrawable, int renderType,
|
---|
702 | const int *attrs, void *data)
|
---|
703 | {
|
---|
704 | (void) hwDrawable;
|
---|
705 | (void) renderType;
|
---|
706 | (void) attrs;
|
---|
707 | (void) data;
|
---|
708 | SWDRI_SAFERET_SWRAST(createNewDrawable, psp, config, data);
|
---|
709 | }
|
---|
710 |
|
---|
711 | static __DRIcontext *
|
---|
712 | vboxdriCreateNewContext_Legacy(__DRIscreen *psp, const __DRIconfig *config,
|
---|
713 | int render_type, __DRIcontext *shared,
|
---|
714 | drm_context_t hwContext, void *data)
|
---|
715 | {
|
---|
716 | (void) render_type;
|
---|
717 | (void) hwContext;
|
---|
718 | return vboxdriCreateNewContext(psp, config, shared, data);
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | static const __DRIlegacyExtension vboxdriLegacyExtension = {
|
---|
723 | { __DRI_LEGACY, __DRI_LEGACY_VERSION },
|
---|
724 | vboxdriCreateNewScreen_Legacy,
|
---|
725 | vboxdriCreateNewDrawable_Legacy,
|
---|
726 | vboxdriCreateNewContext_Legacy
|
---|
727 | };
|
---|
728 |
|
---|
729 | static const __DRIcoreExtension vboxdriCoreExtension = {
|
---|
730 | { __DRI_CORE, __DRI_CORE_VERSION },
|
---|
731 | vboxdriCreateNewScreen, /* driCreateNewScreen */
|
---|
732 | vboxdriDestroyScreen,
|
---|
733 | vboxdriGetExtensions,
|
---|
734 | vboxdriGetConfigAttrib,
|
---|
735 | vboxdriIndexConfigAttrib,
|
---|
736 | vboxdriCreateNewDrawable, /* driCreateNewDrawable */
|
---|
737 | vboxdriDestroyDrawable,
|
---|
738 | vboxdriSwapBuffers,
|
---|
739 | vboxdriCreateNewContext,
|
---|
740 | vboxdriCopyContext,
|
---|
741 | vboxdriDestroyContext,
|
---|
742 | vboxdriBindContext,
|
---|
743 | vboxdriUnbindContext
|
---|
744 | };
|
---|
745 |
|
---|
746 | /* This structure is used by dri_util from mesa, don't rename it! */
|
---|
747 | DECLEXPORT(const __DRIextension *) __driDriverExtensions[] = {
|
---|
748 | &vboxdriLegacyExtension.base,
|
---|
749 | &vboxdriCoreExtension.base,
|
---|
750 | NULL
|
---|
751 | };
|
---|