2 * Copyright © 2018 Broadcom
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
27 * Implements wrappers of libc functions to fake having a DRM device that
28 * isn't actually present in the kernel.
31 /* Prevent glibc from defining open64 when we want to alias it. */
32 #undef _FILE_OFFSET_BITS
33 #define _LARGEFILE64_SOURCE
40 #include <sys/ioctl.h>
43 #include <sys/sysmacros.h>
48 #include <c11/threads.h>
49 #include <drm-uapi/drm.h>
52 #include "util/u_debug.h"
55 #define REAL_FUNCTION_POINTER(x) typeof(x) *real_##x
57 static mtx_t shim_lock
= _MTX_INITIALIZER_NP
;
58 struct set
*opendir_set
;
61 /* If /dev/dri doesn't exist, we'll need an arbitrary pointer that wouldn't be
62 * returned by any other opendir() call so we can return just our fake node.
64 DIR *fake_dev_dri
= (void *)&opendir_set
;
66 /* XXX: implement REAL_FUNCTION_POINTER(close); */
67 REAL_FUNCTION_POINTER(closedir
);
68 REAL_FUNCTION_POINTER(dup
);
69 REAL_FUNCTION_POINTER(fcntl
);
70 REAL_FUNCTION_POINTER(fopen
);
71 REAL_FUNCTION_POINTER(ioctl
);
72 REAL_FUNCTION_POINTER(mmap
);
73 REAL_FUNCTION_POINTER(open
);
74 REAL_FUNCTION_POINTER(opendir
);
75 REAL_FUNCTION_POINTER(readdir
);
76 REAL_FUNCTION_POINTER(readdir64
);
77 REAL_FUNCTION_POINTER(readlink
);
78 REAL_FUNCTION_POINTER(__xstat
);
79 REAL_FUNCTION_POINTER(__xstat64
);
80 REAL_FUNCTION_POINTER(__fxstat
);
81 REAL_FUNCTION_POINTER(__fxstat64
);
83 /* Full path of /dev/dri/renderD* */
84 static char *render_node_path
;
86 static char *render_node_dirent_name
;
87 /* /sys/dev/char/major:minor/device/subsystem */
88 static char *subsystem_path
;
89 int render_node_minor
= -1;
91 struct file_override
{
95 static struct file_override file_overrides
[10];
96 static int file_overrides_count
;
98 /* Come up with a filename for a render node that doesn't actually exist on
102 get_dri_render_node_minor(void)
104 for (int i
= 0; i
< 10; i
++) {
106 asprintf(&render_node_dirent_name
, "renderD%d", minor
);
107 asprintf(&render_node_path
, "/dev/dri/%s",
108 render_node_dirent_name
);
110 if (stat(render_node_path
, &st
) == -1) {
112 render_node_minor
= minor
;
117 fprintf(stderr
, "Couldn't find a spare render node slot\n");
120 static void *get_function_pointer(const char *name
)
122 void *func
= dlsym(RTLD_NEXT
, name
);
124 fprintf(stderr
, "Failed to resolve %s\n", name
);
130 #define GET_FUNCTION_POINTER(x) real_##x = get_function_pointer(#x)
133 drm_shim_override_file(const char *contents
, const char *path_format
, ...)
135 assert(file_overrides_count
< ARRAY_SIZE(file_overrides
));
139 va_start(ap
, path_format
);
140 vasprintf(&path
, path_format
, ap
);
143 struct file_override
*override
= &file_overrides
[file_overrides_count
++];
144 override
->path
= path
;
145 override
->contents
= strdup(contents
);
151 _mesa_set_destroy(opendir_set
, NULL
);
152 free(render_node_path
);
153 free(render_node_dirent_name
);
154 free(subsystem_path
);
157 /* Initialization, which will be called from the first general library call
158 * that might need to be wrapped with the shim.
163 static bool inited
= false;
164 drm_shim_debug
= debug_get_bool_option("DRM_SHIM_DEBUG", false);
166 /* We can't lock this, because we recurse during initialization. */
170 /* This comes first (and we're locked), to make sure we don't recurse
171 * during initialization.
175 opendir_set
= _mesa_set_create(NULL
,
177 _mesa_key_string_equal
);
179 GET_FUNCTION_POINTER(closedir
);
180 GET_FUNCTION_POINTER(dup
);
181 GET_FUNCTION_POINTER(fcntl
);
182 GET_FUNCTION_POINTER(fopen
);
183 GET_FUNCTION_POINTER(ioctl
);
184 GET_FUNCTION_POINTER(mmap
);
185 GET_FUNCTION_POINTER(open
);
186 GET_FUNCTION_POINTER(opendir
);
187 GET_FUNCTION_POINTER(readdir
);
188 GET_FUNCTION_POINTER(readdir64
);
189 GET_FUNCTION_POINTER(readlink
);
190 GET_FUNCTION_POINTER(__xstat
);
191 GET_FUNCTION_POINTER(__xstat64
);
192 GET_FUNCTION_POINTER(__fxstat
);
193 GET_FUNCTION_POINTER(__fxstat64
);
195 get_dri_render_node_minor();
197 if (drm_shim_debug
) {
198 fprintf(stderr
, "Initializing DRM shim on %s\n",
202 asprintf(&subsystem_path
,
203 "/sys/dev/char/%d:%d/device/subsystem",
204 DRM_MAJOR
, render_node_minor
);
206 drm_shim_device_init();
208 atexit(destroy_shim
);
211 /* Override libdrm's reading of various sysfs files for device enumeration. */
212 PUBLIC
FILE *fopen(const char *path
, const char *mode
)
216 for (int i
= 0; i
< file_overrides_count
; i
++) {
217 if (strcmp(file_overrides
[i
].path
, path
) == 0) {
220 write(fds
[1], file_overrides
[i
].contents
,
221 strlen(file_overrides
[i
].contents
));
222 return fdopen(fds
[0], "r");
226 return real_fopen(path
, mode
);
228 PUBLIC
FILE *fopen64(const char *path
, const char *mode
)
229 __attribute__((alias("fopen")));
231 /* Intercepts open(render_node_path) to redirect it to the simulator. */
232 PUBLIC
int open(const char *path
, int flags
, ...)
238 mode_t mode
= va_arg(ap
, mode_t
);
241 if (strcmp(path
, render_node_path
) != 0)
242 return real_open(path
, flags
, mode
);
244 int fd
= real_open("/dev/null", O_RDWR
, 0);
246 drm_shim_fd_register(fd
, NULL
);
250 PUBLIC
int open64(const char*, int, ...) __attribute__((alias("open")));
252 /* Fakes stat to return character device stuff for our fake render node. */
253 PUBLIC
int __xstat(int ver
, const char *path
, struct stat
*st
)
257 /* Note: call real stat if we're in the process of probing for a free
260 if (render_node_minor
== -1)
261 return real___xstat(ver
, path
, st
);
263 /* Fool libdrm's probe of whether the /sys dir for this char dev is
266 char *sys_dev_drm_dir
;
267 asprintf(&sys_dev_drm_dir
, "/sys/dev/char/%d:%d/device/drm",
268 DRM_MAJOR
, render_node_minor
);
269 if (strcmp(path
, sys_dev_drm_dir
) == 0) {
270 free(sys_dev_drm_dir
);
273 free(sys_dev_drm_dir
);
275 if (strcmp(path
, render_node_path
) != 0)
276 return real___xstat(ver
, path
, st
);
278 memset(st
, 0, sizeof(*st
));
279 st
->st_rdev
= makedev(DRM_MAJOR
, render_node_minor
);
280 st
->st_mode
= S_IFCHR
;
285 /* Fakes stat to return character device stuff for our fake render node. */
286 PUBLIC
int __xstat64(int ver
, const char *path
, struct stat64
*st
)
290 /* Note: call real stat if we're in the process of probing for a free
293 if (render_node_minor
== -1)
294 return real___xstat64(ver
, path
, st
);
296 /* Fool libdrm's probe of whether the /sys dir for this char dev is
299 char *sys_dev_drm_dir
;
300 asprintf(&sys_dev_drm_dir
, "/sys/dev/char/%d:%d/device/drm",
301 DRM_MAJOR
, render_node_minor
);
302 if (strcmp(path
, sys_dev_drm_dir
) == 0) {
303 free(sys_dev_drm_dir
);
306 free(sys_dev_drm_dir
);
308 if (strcmp(path
, render_node_path
) != 0)
309 return real___xstat64(ver
, path
, st
);
311 memset(st
, 0, sizeof(*st
));
312 st
->st_rdev
= makedev(DRM_MAJOR
, render_node_minor
);
313 st
->st_mode
= S_IFCHR
;
318 /* Fakes fstat to return character device stuff for our fake render node. */
319 PUBLIC
int __fxstat(int ver
, int fd
, struct stat
*st
)
323 struct shim_fd
*shim_fd
= drm_shim_fd_lookup(fd
);
326 return real___fxstat(ver
, fd
, st
);
328 memset(st
, 0, sizeof(*st
));
329 st
->st_rdev
= makedev(DRM_MAJOR
, render_node_minor
);
330 st
->st_mode
= S_IFCHR
;
335 PUBLIC
int __fxstat64(int ver
, int fd
, struct stat64
*st
)
339 struct shim_fd
*shim_fd
= drm_shim_fd_lookup(fd
);
342 return real___fxstat64(ver
, fd
, st
);
344 memset(st
, 0, sizeof(*st
));
345 st
->st_rdev
= makedev(DRM_MAJOR
, render_node_minor
);
346 st
->st_mode
= S_IFCHR
;
351 /* Tracks if the opendir was on /dev/dri. */
353 opendir(const char *name
)
357 DIR *dir
= real_opendir(name
);
358 if (strcmp(name
, "/dev/dri") == 0) {
360 /* If /dev/dri didn't exist, we still want to be able to return our
361 * fake /dev/dri/render* even though we probably can't
362 * mkdir("/dev/dri"). Return a fake DIR pointer for that.
367 mtx_lock(&shim_lock
);
368 _mesa_set_add(opendir_set
, dir
);
369 mtx_unlock(&shim_lock
);
375 /* If we've reached the end of the real directory list and we're
376 * looking at /dev/dri, add our render node to the list.
378 PUBLIC
struct dirent
*
383 struct dirent
*ent
= NULL
;
385 if (dir
!= fake_dev_dri
)
386 ent
= real_readdir(dir
);
387 static struct dirent render_node_dirent
= { 0 };
390 mtx_lock(&shim_lock
);
391 if (_mesa_set_search(opendir_set
, dir
)) {
392 strcpy(render_node_dirent
.d_name
,
393 render_node_dirent_name
);
394 ent
= &render_node_dirent
;
395 _mesa_set_remove_key(opendir_set
, dir
);
397 mtx_unlock(&shim_lock
);
403 /* If we've reached the end of the real directory list and we're
404 * looking at /dev/dri, add our render node to the list.
406 PUBLIC
struct dirent64
*
411 struct dirent64
*ent
= NULL
;
412 if (dir
!= fake_dev_dri
)
413 ent
= real_readdir64(dir
);
414 static struct dirent64 render_node_dirent
= { 0 };
417 mtx_lock(&shim_lock
);
418 if (_mesa_set_search(opendir_set
, dir
)) {
419 strcpy(render_node_dirent
.d_name
,
420 render_node_dirent_name
);
421 ent
= &render_node_dirent
;
422 _mesa_set_remove_key(opendir_set
, dir
);
424 mtx_unlock(&shim_lock
);
430 /* Cleans up tracking of opendir("/dev/dri") */
436 mtx_lock(&shim_lock
);
437 _mesa_set_remove_key(opendir_set
, dir
);
438 mtx_unlock(&shim_lock
);
440 if (dir
!= fake_dev_dri
)
441 return real_closedir(dir
);
446 /* Handles libdrm's readlink to figure out what kind of device we have. */
448 readlink(const char *path
, char *buf
, size_t size
)
452 if (strcmp(path
, subsystem_path
) != 0)
453 return real_readlink(path
, buf
, size
);
454 strncpy(buf
, "/platform", size
);
457 return strlen(buf
) + 1;
460 /* Main entrypoint to DRM drivers: the ioctl syscall. We send all ioctls on
461 * our DRM fd to drm_shim_ioctl().
464 ioctl(int fd
, unsigned long request
, ...)
469 va_start(ap
, request
);
470 void *arg
= va_arg(ap
, void *);
473 struct shim_fd
*shim_fd
= drm_shim_fd_lookup(fd
);
475 return real_ioctl(fd
, request
, arg
);
477 return drm_shim_ioctl(fd
, request
, arg
);
480 /* Gallium uses this to dup the incoming fd on gbm screen creation */
482 fcntl(int fd
, int cmd
, ...)
486 struct shim_fd
*shim_fd
= drm_shim_fd_lookup(fd
);
490 void *arg
= va_arg(ap
, void *);
493 int ret
= real_fcntl(fd
, cmd
, arg
);
495 if (shim_fd
&& (cmd
== F_DUPFD
|| cmd
== F_DUPFD_CLOEXEC
))
496 drm_shim_fd_register(ret
, shim_fd
);
500 PUBLIC
int fcntl64(int, int, ...)
501 __attribute__((alias("fcntl")));
503 /* I wrote this when trying to fix gallium screen creation, leaving it around
504 * since it's probably good to have.
511 int ret
= real_dup(fd
);
513 struct shim_fd
*shim_fd
= drm_shim_fd_lookup(fd
);
514 if (shim_fd
&& ret
>= 0)
515 drm_shim_fd_register(ret
, shim_fd
);
521 mmap(void *addr
, size_t length
, int prot
, int flags
, int fd
, off_t offset
)
525 struct shim_fd
*shim_fd
= drm_shim_fd_lookup(fd
);
527 return drm_shim_mmap(shim_fd
, length
, prot
, flags
, fd
, offset
);
529 return real_mmap(addr
, length
, prot
, flags
, fd
, offset
);
531 PUBLIC
void *mmap64(void*, size_t, int, int, int, off_t
)
532 __attribute__((alias("mmap")));