VirtualBox

source: vbox/trunk/src/VBox/Additions/common/crOpenGL/fakedri_drv.c@ 23835

Last change on this file since 23835 was 22010, checked in by vboxsync, 15 years ago

crOpenGL: fix mesa entries patching for 64bit guests

  • Property svn:eol-style set to native
File size: 16.0 KB
Line 
1/* $Id$ */
2
3/** @file
4 * VBox OpenGL DRI driver functions
5 */
6
7/*
8 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#define _GNU_SOURCE 1
24
25#include "cr_error.h"
26#include "cr_gl.h"
27#include "cr_mem.h"
28#include "stub.h"
29#include "fakedri_drv.h"
30#include "dri_glx.h"
31#include "iprt/mem.h"
32#include "iprt/err.h"
33#include <dlfcn.h>
34#include <elf.h>
35#include <unistd.h>
36#include "xf86.h"
37
38#define VBOX_NO_MESA_PATCH_REPORTS
39
40//#define DEBUG_DRI_CALLS
41
42//@todo this could be different...
43#ifdef RT_ARCH_AMD64
44# define DRI_DEFAULT_DRIVER_DIR "/usr/lib64/dri:/usr/lib/dri"
45# define DRI_XORG_DRV_DIR "/usr/lib/xorg/modules/drivers/"
46#else
47# define DRI_DEFAULT_DRIVER_DIR "/usr/lib/dri"
48# define DRI_XORG_DRV_DIR "/usr/lib/xorg/modules/drivers/"
49#endif
50
51#ifdef DEBUG_DRI_CALLS
52 #define SWDRI_SHOWNAME(pext, func) \
53 crDebug("SWDRI: sc %s->%s", #pext, #func)
54#else
55 #define SWDRI_SHOWNAME(pext, func)
56#endif
57
58#define SWDRI_SAFECALL(pext, func, ...) \
59 SWDRI_SHOWNAME(pext, func); \
60 if (pext && pext->func){ \
61 (*pext->func)(__VA_ARGS__); \
62 } else { \
63 crDebug("swcore_call NULL for "#func); \
64 }
65
66#define SWDRI_SAFERET(pext, func, ...) \
67 SWDRI_SHOWNAME(pext, func); \
68 if (pext && pext->func){ \
69 return (*pext->func)(__VA_ARGS__); \
70 } else { \
71 crDebug("swcore_call NULL for "#func); \
72 return 0; \
73 }
74
75#define SWDRI_SAFERET_CORE(func, ...) SWDRI_SAFERET(gpSwDriCoreExternsion, func, __VA_ARGS__)
76#define SWDRI_SAFECALL_CORE(func, ...) SWDRI_SAFECALL(gpSwDriCoreExternsion, func, __VA_ARGS__)
77#define SWDRI_SAFERET_SWRAST(func, ...) SWDRI_SAFERET(gpSwDriSwrastExtension, func, __VA_ARGS__)
78#define SWDRI_SAFECALL_SWRAST(func, ...) SWDRI_SAFECALL(gpSwDriSwrastExtension, func, __VA_ARGS__)
79
80#ifndef PAGESIZE
81#define PAGESIZE 4096
82#endif
83
84#ifdef RT_ARCH_AMD64
85# define DRI_ELFSYM Elf64_Sym
86#else
87# define DRI_ELFSYM Elf32_Sym
88#endif
89
90static struct _glapi_table vbox_glapi_table;
91fakedri_glxapi_table glxim;
92
93static const __DRIextension **gppSwDriExternsion = NULL;
94static const __DRIcoreExtension *gpSwDriCoreExternsion = NULL;
95static const __DRIswrastExtension *gpSwDriSwrastExtension = NULL;
96
97extern const __DRIextension * __driDriverExtensions[];
98
99#define GLAPI_ENTRY(Func) pGLTable->Func = cr_gl##Func;
100static void
101vboxFillMesaGLAPITable(struct _glapi_table *pGLTable)
102{
103 #include "fakedri_glfuncsList.h"
104
105 pGLTable->SampleMaskSGIS = cr_glSampleMaskEXT;
106 pGLTable->SamplePatternSGIS = cr_glSamplePatternEXT;
107 pGLTable->WindowPos2dMESA = cr_glWindowPos2d;
108 pGLTable->WindowPos2dvMESA = cr_glWindowPos2dv;
109 pGLTable->WindowPos2fMESA = cr_glWindowPos2f;
110 pGLTable->WindowPos2fvMESA = cr_glWindowPos2fv;
111 pGLTable->WindowPos2iMESA = cr_glWindowPos2i;
112 pGLTable->WindowPos2ivMESA = cr_glWindowPos2iv;
113 pGLTable->WindowPos2sMESA = cr_glWindowPos2s;
114 pGLTable->WindowPos2svMESA = cr_glWindowPos2sv;
115 pGLTable->WindowPos3dMESA = cr_glWindowPos3d;
116 pGLTable->WindowPos3dvMESA = cr_glWindowPos3dv;
117 pGLTable->WindowPos3fMESA = cr_glWindowPos3f;
118 pGLTable->WindowPos3fvMESA = cr_glWindowPos3fv;
119 pGLTable->WindowPos3iMESA = cr_glWindowPos3i;
120 pGLTable->WindowPos3ivMESA = cr_glWindowPos3iv;
121 pGLTable->WindowPos3sMESA = cr_glWindowPos3s;
122 pGLTable->WindowPos3svMESA = cr_glWindowPos3sv;
123};
124#undef GLAPI_ENTRY
125
126#define GLXAPI_ENTRY(Func) pGLXTable->Func = VBOXGLXTAG(glX##Func);
127static void
128vboxFillGLXAPITable(fakedri_glxapi_table *pGLXTable)
129{
130 #include "fakedri_glxfuncsList.h"
131}
132#undef GLXAPI_ENTRY
133
134static void
135vboxPatchMesaExport(const char* psFuncName, const void *pStart, const void *pEnd)
136{
137 Dl_info dlip;
138 DRI_ELFSYM* sym=0;
139 int rv;
140 void *alPatch;
141 void *pMesaEntry;
142
143#ifndef VBOX_NO_MESA_PATCH_REPORTS
144 crDebug("vboxPatchMesaExport: %s", psFuncName);
145#endif
146
147 pMesaEntry = dlsym(RTLD_DEFAULT, psFuncName);
148
149 if (!pMesaEntry)
150 {
151 crDebug("%s not defined in current scope, are we being loaded by mesa's libGL.so?", psFuncName);
152 return;
153 }
154
155 rv = dladdr1(pMesaEntry, &dlip, (void**)&sym, RTLD_DL_SYMENT);
156 if (!rv || !sym)
157 {
158 crError("Failed to get size for %p(%s)", pMesaEntry, psFuncName);
159 return;
160 }
161
162#if VBOX_OGL_GLX_USE_CSTUBS
163 {
164 Dl_info dlip1;
165 DRI_ELFSYM* sym1=0;
166 int rv;
167
168 rv = dladdr1(pStart, &dlip1, (void**)&sym1, RTLD_DL_SYMENT);
169 if (!rv || !sym)
170 {
171 crError("Failed to get size for %p", pStart);
172 return;
173 }
174
175 pEnd = pStart + sym1->st_size;
176 crDebug("VBox Entry: %p, start: %p(%s:%s), size: %i", pStart, dlip1.dli_saddr, dlip1.dli_fname, dlip1.dli_sname, sym1->st_size);
177 }
178#endif
179
180#ifndef VBOX_NO_MESA_PATCH_REPORTS
181 crDebug("Mesa Entry: %p, start: %p(%s:%s), size: %i", pMesaEntry, dlip.dli_saddr, dlip.dli_fname, dlip.dli_sname, sym->st_size);
182 crDebug("Vbox code: start: %p, end %p, size: %i", pStart, pEnd, pEnd-pStart);
183#endif
184
185 if (sym->st_size<(pEnd-pStart))
186 {
187 crDebug("Can't patch size too small.(%s)", psFuncName);
188 return;
189 }
190
191 /* Get aligned start adress we're going to patch*/
192 alPatch = (void*) ((uintptr_t)dlip.dli_saddr & ~(uintptr_t)(PAGESIZE-1));
193
194#ifndef VBOX_NO_MESA_PATCH_REPORTS
195 crDebug("MProtecting: %p, %i", alPatch, dlip.dli_saddr-alPatch+pEnd-pStart);
196#endif
197
198 /* Get write access to mesa functions */
199 rv = RTMemProtect(alPatch, dlip.dli_saddr-alPatch+pEnd-pStart,
200 RTMEM_PROT_READ|RTMEM_PROT_WRITE|RTMEM_PROT_EXEC);
201 if (RT_FAILURE(rv))
202 {
203 crError("mprotect failed with %x (%s)", rv, psFuncName);
204 }
205
206#ifndef VBOX_NO_MESA_PATCH_REPORTS
207 crDebug("Writing %i bytes to %p from %p", pEnd-pStart, dlip.dli_saddr, pStart);
208#endif
209
210 crMemcpy(dlip.dli_saddr, pStart, pEnd-pStart);
211
212 /*@todo Restore the protection, probably have to check what was it before us...*/
213 rv = RTMemProtect(alPatch, dlip.dli_saddr-alPatch+pEnd-pStart,
214 RTMEM_PROT_READ|RTMEM_PROT_EXEC);
215 if (RT_FAILURE(rv))
216 {
217 crError("mprotect2 failed with %x (%s)", rv, psFuncName);
218 }
219}
220
221#ifdef VBOX_OGL_GLX_USE_CSTUBS
222static void
223# define GLXAPI_ENTRY(Func) vboxPatchMesaExport("glX"#Func, &vbox_glX##Func, NULL);
224vboxPatchMesaExports()
225#else
226static void
227# define GLXAPI_ENTRY(Func) vboxPatchMesaExport("glX"#Func, &vbox_glX##Func, &vbox_glX##Func##_EndProc);
228vboxPatchMesaExports()
229#endif
230{
231 crDebug("Patching mesa glx entries");
232 #include "fakedri_glxfuncsList.h"
233}
234#undef GLXAPI_ENTRY
235
236bool vbox_load_sw_dri()
237{
238 const char *libPaths, *p, *next;;
239 char realDriverName[200];
240 void *handle;
241 int len, i;
242
243 /*code from Mesa-7.2/src/glx/x11/dri_common.c:driOpenDriver*/
244
245 libPaths = NULL;
246 if (geteuid() == getuid()) {
247 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
248 libPaths = getenv("LIBGL_DRIVERS_PATH");
249 if (!libPaths)
250 libPaths = getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
251 }
252 if (libPaths == NULL)
253 libPaths = DRI_DEFAULT_DRIVER_DIR;
254
255 handle = NULL;
256 for (p = libPaths; *p; p = next)
257 {
258 next = strchr(p, ':');
259 if (next == NULL)
260 {
261 len = strlen(p);
262 next = p + len;
263 }
264 else
265 {
266 len = next - p;
267 next++;
268 }
269
270 snprintf(realDriverName, sizeof realDriverName, "%.*s/%s_dri.so", len, p, "swrast");
271 crDebug("trying %s", realDriverName);
272 handle = dlopen(realDriverName, RTLD_NOW | RTLD_LOCAL);
273 if (handle) break;
274 }
275
276 /*end code*/
277
278 if (handle) gppSwDriExternsion = dlsym(handle, "__driDriverExtensions");
279
280 if (!gppSwDriExternsion)
281 {
282 crDebug("%s doesn't export __driDriverExtensions", realDriverName);
283 return false;
284 }
285 crDebug("loaded %s", realDriverName);
286
287 for (i = 0; gppSwDriExternsion[i]; i++)
288 {
289 if (strcmp(gppSwDriExternsion[i]->name, __DRI_CORE) == 0)
290 gpSwDriCoreExternsion = (__DRIcoreExtension *) gppSwDriExternsion[i];
291 if (strcmp(gppSwDriExternsion[i]->name, __DRI_SWRAST) == 0)
292 gpSwDriSwrastExtension = (__DRIswrastExtension *) gppSwDriExternsion[i];
293 }
294
295 return gpSwDriCoreExternsion && gpSwDriSwrastExtension;
296}
297
298void __attribute__ ((constructor)) vbox_install_into_mesa(void)
299{
300 if (!stubInit())
301 {
302 crDebug("vboxdriInitScreen: stubInit failed");
303 return;
304 }
305
306 {
307 void (*pxf86Msg)(MessageType type, const char *format, ...) _printf_attribute(2,3);
308
309 pxf86Msg = dlsym(RTLD_DEFAULT, "xf86Msg");
310 if (pxf86Msg)
311 {
312 pxf86Msg(X_INFO, "Next line is added to allow vboxvideo_drv.so to appear as whitelisted driver\n");
313 pxf86Msg(X_INFO, "The file referenced, is *NOT* loaded\n");
314 pxf86Msg(X_INFO, "Loading %s/ati_drv.so\n", DRI_XORG_DRV_DIR);
315
316 /* we're failing to proxy software dri driver calls for certain xservers, so just make sure we're unloaded for now */
317 __driDriverExtensions[0] = NULL;
318 return;
319 }
320 }
321
322 /* Load swrast_dri.so to proxy dri related calls there. */
323 if (!vbox_load_sw_dri())
324 {
325 crDebug("vboxdriInitScreen: vbox_load_sw_dri failed...going to fail badly");
326 return;
327 }
328
329 /* Handle gl api.
330 * In the end application call would look like this:
331 * app call glFoo->(mesa asm dispatch stub)->cr_glFoo(vbox asm dispatch stub)->SPU Foo function(packspuFoo or alike)
332 * Note, we don't need to install extension functions via _glapi_add_dispatch, because we'd override glXGetProcAddress.
333 */
334 /* We don't support all mesa's functions. Initialize our table to mesa dispatch first*/
335 crMemcpy(&vbox_glapi_table, _glapi_get_dispatch(), sizeof(struct _glapi_table));
336 /* Now install our assembly dispatch entries into table */
337 vboxFillMesaGLAPITable(&vbox_glapi_table);
338 /* Install our dispatch table into mesa */
339 _glapi_set_dispatch(&vbox_glapi_table);
340
341 /* Handle glx api.
342 * In the end application call would look like this:
343 * app call glxFoo->(mesa asm dispatch stub patched with vbox_glXFoo:jmp glxim[Foo's index])->VBOXGLXTAG(glxFoo)
344 */
345 /* Fill structure used by our assembly stubs */
346 vboxFillGLXAPITable(&glxim);
347 /* Now patch functions exported by libGL.so */
348 vboxPatchMesaExports();
349}
350
351/*
352 * @todo we're missing first glx related call from the client application.
353 * Luckily, this doesn't add much problems, except for some cases.
354 */
355
356/* __DRIcoreExtension */
357
358static __DRIscreen *
359vboxdriCreateNewScreen(int screen, int fd, unsigned int sarea_handle,
360 const __DRIextension **extensions, const __DRIconfig ***driverConfigs,
361 void *loaderPrivate)
362{
363 (void) fd;
364 (void) sarea_handle;
365 SWDRI_SAFERET_SWRAST(createNewScreen, screen, extensions, driverConfigs, loaderPrivate);
366}
367
368static void
369vboxdriDestroyScreen(__DRIscreen *screen)
370{
371 SWDRI_SAFECALL_CORE(destroyScreen, screen);
372}
373
374static const __DRIextension **
375vboxdriGetExtensions(__DRIscreen *screen)
376{
377 SWDRI_SAFERET_CORE(getExtensions, screen);
378}
379
380static int
381vboxdriGetConfigAttrib(const __DRIconfig *config,
382 unsigned int attrib,
383 unsigned int *value)
384{
385 SWDRI_SAFERET_CORE(getConfigAttrib, config, attrib, value);
386}
387
388static int
389vboxdriIndexConfigAttrib(const __DRIconfig *config, int index,
390 unsigned int *attrib, unsigned int *value)
391{
392 SWDRI_SAFERET_CORE(indexConfigAttrib, config, index, attrib, value);
393}
394
395static __DRIdrawable *
396vboxdriCreateNewDrawable(__DRIscreen *screen,
397 const __DRIconfig *config,
398 unsigned int drawable_id,
399 unsigned int head,
400 void *loaderPrivate)
401{
402 (void) drawable_id;
403 (void) head;
404 SWDRI_SAFERET_SWRAST(createNewDrawable, screen, config, loaderPrivate);
405}
406
407static void
408vboxdriDestroyDrawable(__DRIdrawable *drawable)
409{
410 SWDRI_SAFECALL_CORE(destroyDrawable, drawable);
411}
412
413static void
414vboxdriSwapBuffers(__DRIdrawable *drawable)
415{
416 SWDRI_SAFECALL_CORE(swapBuffers, drawable);
417}
418
419static __DRIcontext *
420vboxdriCreateNewContext(__DRIscreen *screen,
421 const __DRIconfig *config,
422 __DRIcontext *shared,
423 void *loaderPrivate)
424{
425 SWDRI_SAFERET_CORE(createNewContext, screen, config, shared, loaderPrivate);
426}
427
428static int
429vboxdriCopyContext(__DRIcontext *dest,
430 __DRIcontext *src,
431 unsigned long mask)
432{
433 SWDRI_SAFERET_CORE(copyContext, dest, src, mask);
434}
435
436static void
437vboxdriDestroyContext(__DRIcontext *context)
438{
439 SWDRI_SAFECALL_CORE(destroyContext, context);
440}
441
442static int
443vboxdriBindContext(__DRIcontext *ctx,
444 __DRIdrawable *pdraw,
445 __DRIdrawable *pread)
446{
447 SWDRI_SAFERET_CORE(bindContext, ctx, pdraw, pread);
448}
449
450static int
451vboxdriUnbindContext(__DRIcontext *ctx)
452{
453 SWDRI_SAFERET_CORE(unbindContext, ctx)
454}
455
456/* __DRIlegacyExtension */
457
458static __DRIscreen *
459vboxdriCreateNewScreen_Legacy(int scrn,
460 const __DRIversion *ddx_version,
461 const __DRIversion *dri_version,
462 const __DRIversion *drm_version,
463 const __DRIframebuffer *frame_buffer,
464 drmAddress pSAREA, int fd,
465 const __DRIextension **extensions,
466 const __DRIconfig ***driver_modes,
467 void *loaderPrivate)
468{
469 (void) ddx_version;
470 (void) dri_version;
471 (void) frame_buffer;
472 (void) pSAREA;
473 (void) fd;
474 SWDRI_SAFERET_SWRAST(createNewScreen, scrn, extensions, driver_modes, loaderPrivate);
475}
476
477static __DRIdrawable *
478vboxdriCreateNewDrawable_Legacy(__DRIscreen *psp, const __DRIconfig *config,
479 drm_drawable_t hwDrawable, int renderType,
480 const int *attrs, void *data)
481{
482 (void) hwDrawable;
483 (void) renderType;
484 (void) attrs;
485 (void) data;
486 SWDRI_SAFERET_SWRAST(createNewDrawable, psp, config, data);
487}
488
489static __DRIcontext *
490vboxdriCreateNewContext_Legacy(__DRIscreen *psp, const __DRIconfig *config,
491 int render_type, __DRIcontext *shared,
492 drm_context_t hwContext, void *data)
493{
494 (void) render_type;
495 (void) hwContext;
496 return vboxdriCreateNewContext(psp, config, shared, data);
497}
498
499
500static const __DRIlegacyExtension vboxdriLegacyExtension = {
501 { __DRI_LEGACY, __DRI_LEGACY_VERSION },
502 vboxdriCreateNewScreen_Legacy,
503 vboxdriCreateNewDrawable_Legacy,
504 vboxdriCreateNewContext_Legacy
505};
506
507static const __DRIcoreExtension vboxdriCoreExtension = {
508 { __DRI_CORE, __DRI_CORE_VERSION },
509 vboxdriCreateNewScreen, /* driCreateNewScreen */
510 vboxdriDestroyScreen,
511 vboxdriGetExtensions,
512 vboxdriGetConfigAttrib,
513 vboxdriIndexConfigAttrib,
514 vboxdriCreateNewDrawable, /* driCreateNewDrawable */
515 vboxdriDestroyDrawable,
516 vboxdriSwapBuffers,
517 vboxdriCreateNewContext,
518 vboxdriCopyContext,
519 vboxdriDestroyContext,
520 vboxdriBindContext,
521 vboxdriUnbindContext
522};
523
524/* This structure is used by dri_util from mesa, don't rename it! */
525DECLEXPORT(const __DRIextension *) __driDriverExtensions[] = {
526 &vboxdriLegacyExtension.base,
527 &vboxdriCoreExtension.base,
528 NULL
529};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette