VirtualBox

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

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

crOpenGL: fix offset check

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

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