VirtualBox

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

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

crOpenGL: use PIC code for VBoxOGL on 32bit Linuxes

  • Property svn:eol-style set to native
File size: 17.2 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 char patch[5];
143 void *shift;
144
145#ifndef VBOX_NO_MESA_PATCH_REPORTS
146 crDebug("\nvboxPatchMesaExport: %s", psFuncName);
147#endif
148
149 pMesaEntry = dlsym(RTLD_DEFAULT, psFuncName);
150
151 if (!pMesaEntry)
152 {
153 crDebug("%s not defined in current scope, are we being loaded by mesa's libGL.so?", psFuncName);
154 return;
155 }
156
157 rv = dladdr1(pMesaEntry, &dlip, (void**)&sym, RTLD_DL_SYMENT);
158 if (!rv || !sym)
159 {
160 crError("Failed to get size for %p(%s)", pMesaEntry, psFuncName);
161 return;
162 }
163
164#if VBOX_OGL_GLX_USE_CSTUBS
165 {
166 Dl_info dlip1;
167 DRI_ELFSYM* sym1=0;
168 int rv;
169
170 rv = dladdr1(pStart, &dlip1, (void**)&sym1, RTLD_DL_SYMENT);
171 if (!rv || !sym1)
172 {
173 crError("Failed to get size for %p", pStart);
174 return;
175 }
176
177 pEnd = pStart + sym1->st_size;
178 crDebug("VBox Entry: %p, start: %p(%s:%s), size: %i", pStart, dlip1.dli_saddr, dlip1.dli_fname, dlip1.dli_sname, sym1->st_size);
179 }
180#endif
181
182#ifndef VBOX_NO_MESA_PATCH_REPORTS
183 crDebug("Mesa Entry: %p, start: %p(%s:%s), size: %i", pMesaEntry, dlip.dli_saddr, dlip.dli_fname, dlip.dli_sname, sym->st_size);
184 crDebug("Vbox code: start: %p, end %p, size: %i", pStart, pEnd, pEnd-pStart);
185#endif
186
187#ifndef VBOX_OGL_GLX_USE_CSTUBS
188 if (sym->st_size<(pEnd-pStart))
189#endif
190 {
191 /* Try to insert 5 bytes jmp/jmpq to our stub code */
192
193 if (5>(pEnd-pStart))
194 {
195 crDebug("Can't patch size too small.(%s)", psFuncName);
196 return;
197 }
198
199 shift = (void*)((intptr_t)pStart-((intptr_t)dlip.dli_saddr+5));
200# ifndef VBOX_NO_MESA_PATCH_REPORTS
201 crDebug("Inserting jmp[q] with shift %p instead", shift);
202# endif
203
204 {
205#ifdef RT_ARCH_AMD64
206 int64_t offset = (intptr_t)shift;
207#else
208 int64_t offset = (int64_t)((intptr_t)pStart)-((int64_t)((intptr_t)dlip.dli_saddr)+5);
209#endif
210 if ( (RT_HIDWORD(offset) != 0) && (RT_HIDWORD(offset) != 0xFFFFFFFF)
211 && (((int32_t)offset > INT32_MAX) || ((int32_t)offset < INT32_MIN)))
212 {
213 crDebug("Can't patch offset is too big.(%s)", psFuncName);
214 return;
215 }
216 }
217
218 patch[0] = 0xE9;
219 patch[1] = ((char*)&shift)[0];
220 patch[2] = ((char*)&shift)[1];
221 patch[3] = ((char*)&shift)[2];
222 patch[4] = ((char*)&shift)[3];
223
224# ifndef VBOX_NO_MESA_PATCH_REPORTS
225 crDebug("Patch: E9 %x", *((int*)&patch[1]));
226# endif
227 pStart = &patch[0];
228 pEnd = &patch[5];
229 }
230
231 /* Get aligned start adress we're going to patch*/
232 alPatch = (void*) ((uintptr_t)dlip.dli_saddr & ~(uintptr_t)(PAGESIZE-1));
233
234#ifndef VBOX_NO_MESA_PATCH_REPORTS
235 crDebug("MProtecting: %p, %i", alPatch, dlip.dli_saddr-alPatch+pEnd-pStart);
236#endif
237
238 /* Get write access to mesa functions */
239 rv = RTMemProtect(alPatch, dlip.dli_saddr-alPatch+pEnd-pStart,
240 RTMEM_PROT_READ|RTMEM_PROT_WRITE|RTMEM_PROT_EXEC);
241 if (RT_FAILURE(rv))
242 {
243 crError("mprotect failed with %x (%s)", rv, psFuncName);
244 }
245
246#ifndef VBOX_NO_MESA_PATCH_REPORTS
247 crDebug("Writing %i bytes to %p from %p", pEnd-pStart, dlip.dli_saddr, pStart);
248#endif
249
250 crMemcpy(dlip.dli_saddr, pStart, pEnd-pStart);
251
252 /*@todo Restore the protection, probably have to check what was it before us...*/
253 rv = RTMemProtect(alPatch, dlip.dli_saddr-alPatch+pEnd-pStart,
254 RTMEM_PROT_READ|RTMEM_PROT_EXEC);
255 if (RT_FAILURE(rv))
256 {
257 crError("mprotect2 failed with %x (%s)", rv, psFuncName);
258 }
259}
260
261#ifdef VBOX_OGL_GLX_USE_CSTUBS
262static void
263# define GLXAPI_ENTRY(Func) vboxPatchMesaExport("glX"#Func, &vbox_glX##Func, NULL);
264vboxPatchMesaExports()
265#else
266static void
267# define GLXAPI_ENTRY(Func) vboxPatchMesaExport("glX"#Func, &vbox_glX##Func, &vbox_glX##Func##_EndProc);
268vboxPatchMesaExports()
269#endif
270{
271 crDebug("Patching mesa glx entries");
272 #include "fakedri_glxfuncsList.h"
273}
274#undef GLXAPI_ENTRY
275
276bool vbox_load_sw_dri()
277{
278 const char *libPaths, *p, *next;;
279 char realDriverName[200];
280 void *handle;
281 int len, i;
282
283 /*code from Mesa-7.2/src/glx/x11/dri_common.c:driOpenDriver*/
284
285 libPaths = NULL;
286 if (geteuid() == getuid()) {
287 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
288 libPaths = getenv("LIBGL_DRIVERS_PATH");
289 if (!libPaths)
290 libPaths = getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
291 }
292 if (libPaths == NULL)
293 libPaths = DRI_DEFAULT_DRIVER_DIR;
294
295 handle = NULL;
296 for (p = libPaths; *p; p = next)
297 {
298 next = strchr(p, ':');
299 if (next == NULL)
300 {
301 len = strlen(p);
302 next = p + len;
303 }
304 else
305 {
306 len = next - p;
307 next++;
308 }
309
310 snprintf(realDriverName, sizeof realDriverName, "%.*s/%s_dri.so", len, p, "swrast");
311 crDebug("trying %s", realDriverName);
312 handle = dlopen(realDriverName, RTLD_NOW | RTLD_LOCAL);
313 if (handle) break;
314 }
315
316 /*end code*/
317
318 if (handle) gppSwDriExternsion = dlsym(handle, "__driDriverExtensions");
319
320 if (!gppSwDriExternsion)
321 {
322 crDebug("%s doesn't export __driDriverExtensions", realDriverName);
323 return false;
324 }
325 crDebug("loaded %s", realDriverName);
326
327 for (i = 0; gppSwDriExternsion[i]; i++)
328 {
329 if (strcmp(gppSwDriExternsion[i]->name, __DRI_CORE) == 0)
330 gpSwDriCoreExternsion = (__DRIcoreExtension *) gppSwDriExternsion[i];
331 if (strcmp(gppSwDriExternsion[i]->name, __DRI_SWRAST) == 0)
332 gpSwDriSwrastExtension = (__DRIswrastExtension *) gppSwDriExternsion[i];
333 }
334
335 return gpSwDriCoreExternsion && gpSwDriSwrastExtension;
336}
337
338void __attribute__ ((constructor)) vbox_install_into_mesa(void)
339{
340 if (!stubInit())
341 {
342 crDebug("vboxdriInitScreen: stubInit failed");
343 return;
344 }
345
346 {
347 void (*pxf86Msg)(MessageType type, const char *format, ...) _printf_attribute(2,3);
348
349 pxf86Msg = dlsym(RTLD_DEFAULT, "xf86Msg");
350 if (pxf86Msg)
351 {
352 pxf86Msg(X_INFO, "Next line is added to allow vboxvideo_drv.so to appear as whitelisted driver\n");
353 pxf86Msg(X_INFO, "The file referenced, is *NOT* loaded\n");
354 pxf86Msg(X_INFO, "Loading %s/ati_drv.so\n", DRI_XORG_DRV_DIR);
355
356 /* we're failing to proxy software dri driver calls for certain xservers, so just make sure we're unloaded for now */
357 __driDriverExtensions[0] = NULL;
358 return;
359 }
360 }
361
362 /* Load swrast_dri.so to proxy dri related calls there. */
363 if (!vbox_load_sw_dri())
364 {
365 crDebug("vboxdriInitScreen: vbox_load_sw_dri failed...going to fail badly");
366 return;
367 }
368
369 /* Handle gl api.
370 * In the end application call would look like this:
371 * app call glFoo->(mesa asm dispatch stub)->cr_glFoo(vbox asm dispatch stub)->SPU Foo function(packspuFoo or alike)
372 * Note, we don't need to install extension functions via _glapi_add_dispatch, because we'd override glXGetProcAddress.
373 */
374 /* We don't support all mesa's functions. Initialize our table to mesa dispatch first*/
375 crMemcpy(&vbox_glapi_table, _glapi_get_dispatch(), sizeof(struct _glapi_table));
376 /* Now install our assembly dispatch entries into table */
377 vboxFillMesaGLAPITable(&vbox_glapi_table);
378 /* Install our dispatch table into mesa */
379 _glapi_set_dispatch(&vbox_glapi_table);
380
381 /* Handle glx api.
382 * In the end application call would look like this:
383 * app call glxFoo->(mesa asm dispatch stub patched with vbox_glXFoo:jmp glxim[Foo's index])->VBOXGLXTAG(glxFoo)
384 */
385 /* Fill structure used by our assembly stubs */
386 vboxFillGLXAPITable(&glxim);
387 /* Now patch functions exported by libGL.so */
388 vboxPatchMesaExports();
389}
390
391/*
392 * @todo we're missing first glx related call from the client application.
393 * Luckily, this doesn't add much problems, except for some cases.
394 */
395
396/* __DRIcoreExtension */
397
398static __DRIscreen *
399vboxdriCreateNewScreen(int screen, int fd, unsigned int sarea_handle,
400 const __DRIextension **extensions, const __DRIconfig ***driverConfigs,
401 void *loaderPrivate)
402{
403 (void) fd;
404 (void) sarea_handle;
405 SWDRI_SAFERET_SWRAST(createNewScreen, screen, extensions, driverConfigs, loaderPrivate);
406}
407
408static void
409vboxdriDestroyScreen(__DRIscreen *screen)
410{
411 SWDRI_SAFECALL_CORE(destroyScreen, screen);
412}
413
414static const __DRIextension **
415vboxdriGetExtensions(__DRIscreen *screen)
416{
417 SWDRI_SAFERET_CORE(getExtensions, screen);
418}
419
420static int
421vboxdriGetConfigAttrib(const __DRIconfig *config,
422 unsigned int attrib,
423 unsigned int *value)
424{
425 SWDRI_SAFERET_CORE(getConfigAttrib, config, attrib, value);
426}
427
428static int
429vboxdriIndexConfigAttrib(const __DRIconfig *config, int index,
430 unsigned int *attrib, unsigned int *value)
431{
432 SWDRI_SAFERET_CORE(indexConfigAttrib, config, index, attrib, value);
433}
434
435static __DRIdrawable *
436vboxdriCreateNewDrawable(__DRIscreen *screen,
437 const __DRIconfig *config,
438 unsigned int drawable_id,
439 unsigned int head,
440 void *loaderPrivate)
441{
442 (void) drawable_id;
443 (void) head;
444 SWDRI_SAFERET_SWRAST(createNewDrawable, screen, config, loaderPrivate);
445}
446
447static void
448vboxdriDestroyDrawable(__DRIdrawable *drawable)
449{
450 SWDRI_SAFECALL_CORE(destroyDrawable, drawable);
451}
452
453static void
454vboxdriSwapBuffers(__DRIdrawable *drawable)
455{
456 SWDRI_SAFECALL_CORE(swapBuffers, drawable);
457}
458
459static __DRIcontext *
460vboxdriCreateNewContext(__DRIscreen *screen,
461 const __DRIconfig *config,
462 __DRIcontext *shared,
463 void *loaderPrivate)
464{
465 SWDRI_SAFERET_CORE(createNewContext, screen, config, shared, loaderPrivate);
466}
467
468static int
469vboxdriCopyContext(__DRIcontext *dest,
470 __DRIcontext *src,
471 unsigned long mask)
472{
473 SWDRI_SAFERET_CORE(copyContext, dest, src, mask);
474}
475
476static void
477vboxdriDestroyContext(__DRIcontext *context)
478{
479 SWDRI_SAFECALL_CORE(destroyContext, context);
480}
481
482static int
483vboxdriBindContext(__DRIcontext *ctx,
484 __DRIdrawable *pdraw,
485 __DRIdrawable *pread)
486{
487 SWDRI_SAFERET_CORE(bindContext, ctx, pdraw, pread);
488}
489
490static int
491vboxdriUnbindContext(__DRIcontext *ctx)
492{
493 SWDRI_SAFERET_CORE(unbindContext, ctx)
494}
495
496/* __DRIlegacyExtension */
497
498static __DRIscreen *
499vboxdriCreateNewScreen_Legacy(int scrn,
500 const __DRIversion *ddx_version,
501 const __DRIversion *dri_version,
502 const __DRIversion *drm_version,
503 const __DRIframebuffer *frame_buffer,
504 drmAddress pSAREA, int fd,
505 const __DRIextension **extensions,
506 const __DRIconfig ***driver_modes,
507 void *loaderPrivate)
508{
509 (void) ddx_version;
510 (void) dri_version;
511 (void) frame_buffer;
512 (void) pSAREA;
513 (void) fd;
514 SWDRI_SAFERET_SWRAST(createNewScreen, scrn, extensions, driver_modes, loaderPrivate);
515}
516
517static __DRIdrawable *
518vboxdriCreateNewDrawable_Legacy(__DRIscreen *psp, const __DRIconfig *config,
519 drm_drawable_t hwDrawable, int renderType,
520 const int *attrs, void *data)
521{
522 (void) hwDrawable;
523 (void) renderType;
524 (void) attrs;
525 (void) data;
526 SWDRI_SAFERET_SWRAST(createNewDrawable, psp, config, data);
527}
528
529static __DRIcontext *
530vboxdriCreateNewContext_Legacy(__DRIscreen *psp, const __DRIconfig *config,
531 int render_type, __DRIcontext *shared,
532 drm_context_t hwContext, void *data)
533{
534 (void) render_type;
535 (void) hwContext;
536 return vboxdriCreateNewContext(psp, config, shared, data);
537}
538
539
540static const __DRIlegacyExtension vboxdriLegacyExtension = {
541 { __DRI_LEGACY, __DRI_LEGACY_VERSION },
542 vboxdriCreateNewScreen_Legacy,
543 vboxdriCreateNewDrawable_Legacy,
544 vboxdriCreateNewContext_Legacy
545};
546
547static const __DRIcoreExtension vboxdriCoreExtension = {
548 { __DRI_CORE, __DRI_CORE_VERSION },
549 vboxdriCreateNewScreen, /* driCreateNewScreen */
550 vboxdriDestroyScreen,
551 vboxdriGetExtensions,
552 vboxdriGetConfigAttrib,
553 vboxdriIndexConfigAttrib,
554 vboxdriCreateNewDrawable, /* driCreateNewDrawable */
555 vboxdriDestroyDrawable,
556 vboxdriSwapBuffers,
557 vboxdriCreateNewContext,
558 vboxdriCopyContext,
559 vboxdriDestroyContext,
560 vboxdriBindContext,
561 vboxdriUnbindContext
562};
563
564/* This structure is used by dri_util from mesa, don't rename it! */
565DECLEXPORT(const __DRIextension *) __driDriverExtensions[] = {
566 &vboxdriLegacyExtension.base,
567 &vboxdriCoreExtension.base,
568 NULL
569};
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