Detect and select the system Vulkan backend on PowerVR

This commit is contained in:
wallentx committed 2026-10-11 04:01:50 -05:00
1 parent fda618618c
commit 844a1a2473
24 files changed
+823 -84

No files matched your search

+106 -27
View File
@@ -1,9 +1,10 @@
/*
* GEM handle emulation for the KGSL device presented as a DRM render node.
* GEM handle emulation for KGSL and explicitly enabled system-PowerVR sessions.
*
* Compositors and Mesa validate or convert dma-bufs with PRIME ioctls on the render node; KGSL
* has no GEM. Handles only serve as tokens to these callers, so a table of duplicated dma-buf
* descriptors stands in for the kernel's, and every other device is left to libdrm.
* descriptors stands in for the kernel's. PowerVR's Android driver can import DMA-BUFs
* through Vulkan but rejects PRIME-to-GEM conversion; its real handles still go to libdrm.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
@@ -11,6 +12,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
@@ -18,6 +20,10 @@
#include <unistd.h>
#define MAX_HANDLES 4096
#ifndef F_DUPFD_QUERY
#define F_DUPFD_QUERY 1027
#endif
#define PVR_HANDLE_BIT UINT32_C(0x80000000)
/* DRM_IOCTL_GEM_CLOSE from drm.h: _IOW('d', 0x09, struct drm_gem_close { u32 handle, pad; }). */
struct gem_close {
@@ -30,11 +36,14 @@ struct handle_entry {
uint32_t handle;
int dma_fd;
int drm_fd;
int owned_drm_fd; /* PowerVR: pin the open file description, not its reusable fd number. */
int powervr;
};
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static struct handle_entry handle_entries[MAX_HANDLES];
static uint32_t next_handle = 1;
static uint32_t next_pvr_handle = PVR_HANDLE_BIT;
static dev_t kgsl_dev;
static int kgsl_state; /* 0 unknown, 1 known, -1 absent */
@@ -43,12 +52,38 @@ static int is_kgsl(int fd) {
pthread_mutex_lock(&lock);
if (kgsl_state == 0) {
kgsl_state = stat("/dev/kgsl-3d0", &st) == 0 ? 1 : -1;
kgsl_dev = st.st_rdev;
if (kgsl_state > 0) kgsl_dev = st.st_rdev;
}
pthread_mutex_unlock(&lock);
return kgsl_state > 0 && fstat(fd, &st) == 0 && S_ISCHR(st.st_mode) && st.st_rdev == kgsl_dev;
}
/* Keep the opt-in separate from GPU detection: manual ICDs must retain native DRM behavior. */
struct bl_drm_version {
int major, minor, patch;
size_t name_len;
char *name;
size_t date_len;
char *date;
size_t desc_len;
char *desc;
};
#define BL_DRM_VERSION _IOWR('d', 0x00, struct bl_drm_version)
static int is_powervr(int fd) {
const char *enabled = getenv("BL_POWERVR_DRM_HANDLES");
if (!enabled || strcmp(enabled, "1")) return 0;
struct stat node, candidate;
if (stat("/dev/dri/renderD128", &node) || fstat(fd, &candidate) ||
!S_ISCHR(candidate.st_mode) || !S_ISCHR(node.st_mode) ||
candidate.st_rdev != node.st_rdev) return 0;
char name[16] = {0};
struct bl_drm_version version = {.name_len = sizeof(name) - 1, .name = name};
/* Bypass the preload's ioctl dispatcher to avoid recursively entering this hook. */
return syscall(SYS_ioctl, fd, BL_DRM_VERSION, &version) == 0 &&
version.name_len == 3 && !memcmp(name, "pvr", 3);
}
static void *real(const char *name) {
void *fn = dlsym(RTLD_NEXT, name);
if (!fn) {
@@ -71,10 +106,20 @@ static int same_file_description(int a, int b) {
#endif
}
static int same_owned_description(int owned, int fd) {
int saved_errno = errno;
int result = fcntl(owned, F_DUPFD_QUERY, fd);
errno = saved_errno;
if (result >= 0) return result == 1;
return same_file_description(owned, fd);
}
static int find_handle_locked(int fd, uint32_t handle) {
for (int i = 0; i < MAX_HANDLES; i++) {
if (handle_entries[i].handle == handle &&
same_file_description(handle_entries[i].drm_fd, fd)) {
(handle_entries[i].powervr
? same_owned_description(handle_entries[i].owned_drm_fd, fd)
: same_file_description(handle_entries[i].drm_fd, fd))) {
return i;
}
}
@@ -86,11 +131,15 @@ static int find_handle_locked(int fd, uint32_t handle) {
* rapidly recreated swapchains immediately reused those numbers. Keep storage slots reusable but
* make the public token monotonic so a stale handle can fail instead of aliasing a newer buffer.
*/
static uint32_t alloc_handle_locked(void) {
static uint32_t alloc_handle_locked(int powervr) {
for (int tries = 0; tries <= MAX_HANDLES; tries++) {
uint32_t candidate = next_handle++;
if (next_handle == 0) {
next_handle = 1;
uint32_t candidate;
if (powervr) {
candidate = next_pvr_handle++;
if (!(next_pvr_handle & PVR_HANDLE_BIT)) next_pvr_handle = PVR_HANDLE_BIT;
} else {
candidate = next_handle++;
if (next_handle == PVR_HANDLE_BIT) next_handle = 1;
}
if (candidate == 0) {
continue;
@@ -110,25 +159,45 @@ static uint32_t alloc_handle_locked(void) {
}
int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) {
if (!is_kgsl(fd)) {
int kgsl = is_kgsl(fd);
int powervr = !kgsl && is_powervr(fd);
if (!kgsl) {
int (*fn)(int, int, uint32_t *) = (int (*)(int, int, uint32_t *)) real("drmPrimeFDToHandle");
return fn ? fn(fd, prime_fd, handle) : -ENOSYS;
if (!fn) return -ENOSYS;
int result = fn(fd, prime_fd, handle);
if (!result || !powervr || errno != EINVAL) return result;
}
if (!handle) { errno = EINVAL; return -EINVAL; }
int owner = -1;
if (powervr) {
owner = fcntl(fd, F_DUPFD_CLOEXEC, 0);
if (owner < 0) return -errno;
if (!same_owned_description(owner, fd)) {
close(owner);
errno = EOPNOTSUPP;
return -EOPNOTSUPP;
}
}
int dup_fd = fcntl(prime_fd, F_DUPFD_CLOEXEC, 0);
if (dup_fd < 0) {
return -errno;
int error = errno;
if (owner >= 0) close(owner);
errno = error;
return -error;
}
int ret = -ENOMEM;
pthread_mutex_lock(&lock);
for (int i = 0; i < MAX_HANDLES; i++) {
if (handle_entries[i].handle == 0) {
uint32_t token = alloc_handle_locked();
uint32_t token = alloc_handle_locked(powervr);
if (!token) {
break;
}
handle_entries[i].handle = token;
handle_entries[i].dma_fd = dup_fd;
handle_entries[i].drm_fd = fd;
handle_entries[i].owned_drm_fd = owner;
handle_entries[i].powervr = powervr;
*handle = token;
ret = 0;
break;
@@ -137,33 +206,42 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) {
pthread_mutex_unlock(&lock);
if (ret) {
close(dup_fd);
if (owner >= 0) close(owner);
errno = -ret;
}
return ret;
}
int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd) {
if (!is_kgsl(fd)) {
int (*fn)(int, uint32_t, uint32_t, int *) = (int (*)(int, uint32_t, uint32_t, int *)) real("drmPrimeHandleToFD");
return fn ? fn(fd, handle, flags, prime_fd) : -ENOSYS;
}
int kgsl = is_kgsl(fd);
int ret = -EINVAL;
pthread_mutex_lock(&lock);
int index = find_handle_locked(fd, handle);
if (index >= 0) {
*prime_fd = fcntl(handle_entries[index].dma_fd, F_DUPFD_CLOEXEC, 0);
ret = *prime_fd < 0 ? -errno : 0;
if (!prime_fd || (handle_entries[index].powervr && (flags & ~(O_CLOEXEC | O_RDWR)))) {
errno = EINVAL;
} else {
int command = handle_entries[index].powervr && !(flags & O_CLOEXEC) ? F_DUPFD : F_DUPFD_CLOEXEC;
*prime_fd = fcntl(handle_entries[index].dma_fd, command, 0);
ret = *prime_fd < 0 ? -errno : 0;
}
}
pthread_mutex_unlock(&lock);
return ret;
if (index >= 0 || kgsl) return ret;
/* Unknown handles (including real PowerVR GEM handles) retain kernel semantics. */
int (*fn)(int, uint32_t, uint32_t, int *) =
(int (*)(int, uint32_t, uint32_t, int *)) real("drmPrimeHandleToFD");
return fn ? fn(fd, handle, flags, prime_fd) : -ENOSYS;
}
/* Drops only a handle belonging to this KGSL file description. */
/* Drops only an emulated handle belonging to this file description. */
static int release_handle(int fd, uint32_t handle) {
int found = 0;
pthread_mutex_lock(&lock);
int index = find_handle_locked(fd, handle);
if (index >= 0) {
close(handle_entries[index].dma_fd);
if (handle_entries[index].powervr) close(handle_entries[index].owned_drm_fd);
handle_entries[index] = (struct handle_entry){0};
found = 1;
}
@@ -172,11 +250,10 @@ static int release_handle(int fd, uint32_t handle) {
}
int drmCloseBufferHandle(int fd, uint32_t handle) {
if (!is_kgsl(fd)) {
int (*fn)(int, uint32_t) = (int (*)(int, uint32_t)) real("drmCloseBufferHandle");
return fn ? fn(fd, handle) : -ENOSYS;
}
return release_handle(fd, handle) ? 0 : -EINVAL;
if (release_handle(fd, handle)) return 0;
if (is_kgsl(fd)) { errno = EINVAL; return -EINVAL; }
int (*fn)(int, uint32_t) = (int (*)(int, uint32_t)) real("drmCloseBufferHandle");
return fn ? fn(fd, handle) : -ENOSYS;
}
/*
@@ -190,14 +267,16 @@ int drmCloseBufferHandle(int fd, uint32_t handle) {
* judder replaying old frames in No Man's Sky on an Adreno 840 even with no handle released.
*/
__attribute__((visibility("hidden"))) int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) {
if (request != GEM_CLOSE || !arg || !is_kgsl(fd)) {
if (request != GEM_CLOSE || !arg) {
return 0;
}
if (release_handle(fd, ((struct gem_close *) arg)->handle)) {
*rc = 0;
} else {
} else if (is_kgsl(fd)) {
errno = EINVAL;
*rc = -1;
} else {
return 0;
}
return 1;
}
+256
View File
@@ -0,0 +1,256 @@
#define _GNU_SOURCE
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
/* Only the device/driver boundary is simulated. Descriptors and their lifetimes are real.
* F_DUPFD_QUERY is modelled so this test also runs on older CI kernels; the device probe
* separately exercises the actual query on Android. */
#define TEST_FD_LIMIT 65536
static _Atomic unsigned identities[TEST_FD_LIMIT];
static unsigned identity_serial = 1;
static int identity_denied, kgsl_present;
static int kernel_error = EINVAL;
static int native_imports, native_exports, native_closes;
static const char *driver_name = "pvr";
static int fixture_stat(const char *path, struct stat *st);
static long fixture_syscall(long number, ...);
static int fixture_fcntl(int fd, int op, int arg);
static void *fixture_dlsym(void *object, const char *name);
static int fixture_close(int fd);
#define stat(path, st) fixture_stat(path, st)
#define syscall fixture_syscall
#define fcntl fixture_fcntl
#define dlsym fixture_dlsym
#define close fixture_close
#include "../linuxfs/preload/drm.c"
#undef stat
#undef syscall
#undef fcntl
#undef dlsym
#undef close
static int test_open(const char *path) {
int fd = open(path, O_RDWR | O_CLOEXEC);
assert(fd >= 0 && fd < TEST_FD_LIMIT);
identities[fd] = identity_serial++;
return fd;
}
static int fixture_close(int fd) {
if (fd >= 0 && fd < TEST_FD_LIMIT) identities[fd] = 0;
return close(fd);
}
static int fixture_stat(const char *path, struct stat *st) {
if (!strcmp(path, "/dev/kgsl-3d0")) {
if (kgsl_present) return stat("/dev/zero", st);
errno = ENOENT;
return -1;
}
if (!strcmp(path, "/dev/dri/renderD128")) return stat("/dev/null", st);
return stat(path, st);
}
/* The intercepted DRM calls all take an integer argument. Keep that contract
* explicit; native_import's argument-free F_GETFD uses libc after #undef above. */
static int fixture_fcntl(int fd, int op, int arg) {
if (op == F_DUPFD_QUERY) {
if (identity_denied) { errno = EINVAL; return -1; }
if (fd < 0 || arg < 0 || fd >= TEST_FD_LIMIT || arg >= TEST_FD_LIMIT ||
!identities[fd] || !identities[arg]) { errno = EBADF; return -1; }
return identities[fd] == identities[arg];
}
int ret = fcntl(fd, op, arg);
if (ret >= 0 && (op == F_DUPFD || op == F_DUPFD_CLOEXEC)) {
assert(ret < TEST_FD_LIMIT);
identities[ret] = identities[fd];
}
return ret;
}
static long fixture_syscall(long number, ...) {
va_list ap;
va_start(ap, number);
if (number == SYS_ioctl) {
(void)va_arg(ap, int);
unsigned long request = va_arg(ap, unsigned long);
struct bl_drm_version *v = va_arg(ap, void *);
assert(request == BL_DRM_VERSION);
snprintf(v->name, v->name_len, "%s", driver_name);
v->name_len = strlen(driver_name);
va_end(ap);
return 0;
}
va_end(ap);
assert(number == SYS_kcmp);
errno = EPERM;
return -1;
}
static int native_import(int fd, int dma, uint32_t *handle) {
(void)fd;
native_imports++;
if (fcntl(dma, F_GETFD) < 0) return -1;
if (!handle) { errno = EFAULT; return -1; }
if (kernel_error) { errno = kernel_error; return -1; }
*handle = 42;
return 0;
}
static int native_export(int fd, uint32_t handle, uint32_t flags, int *out) {
(void)flags;
native_exports++;
if (handle != 42 || !out) { errno = EINVAL; return -1; }
*out = fixture_fcntl(fd, F_DUPFD_CLOEXEC, 0);
return *out < 0 ? -1 : 0;
}
static int native_close(int fd, uint32_t handle) {
(void)fd;
native_closes++;
if (handle == 42) return 0;
errno = EINVAL;
return -1;
}
static void *fixture_dlsym(void *object, const char *name) {
assert(object == RTLD_NEXT);
if (!strcmp(name, "drmPrimeFDToHandle")) return native_import;
if (!strcmp(name, "drmPrimeHandleToFD")) return native_export;
if (!strcmp(name, "drmCloseBufferHandle")) return native_close;
abort();
}
static int count_fds(void) {
int count = 0;
for (int fd = 0; fd < TEST_FD_LIMIT; ++fd) count += identities[fd] != 0;
return count;
}
static void empty_table(void) {
for (int i = 0; i < MAX_HANDLES; ++i) assert(!handle_entries[i].handle);
}
int main(void) {
int drm = test_open("/dev/null"), dma = test_open("/dev/zero");
uint32_t handle = 0;
unsetenv("BL_POWERVR_DRM_HANDLES");
assert(drmPrimeFDToHandle(drm, dma, &handle) != 0);
setenv("BL_POWERVR_DRM_HANDLES", "yes", 1);
assert(drmPrimeFDToHandle(drm, dma, &handle) != 0);
setenv("BL_POWERVR_DRM_HANDLES", "1", 1);
driver_name = "other";
assert(drmPrimeFDToHandle(drm, dma, &handle) != 0);
driver_name = "pvr";
assert(drmPrimeFDToHandle(dma, dma, &handle) != 0); /* wrong character device */
kernel_error = EACCES;
assert(drmPrimeFDToHandle(drm, dma, &handle) != 0 && errno == EACCES);
kernel_error = EINVAL;
assert(drmPrimeFDToHandle(drm, -1, &handle) != 0 && errno == EBADF);
empty_table();
int baseline = count_fds();
identity_denied = 1;
assert(drmPrimeFDToHandle(drm, dma, &handle) == -EOPNOTSUPP);
assert(count_fds() == baseline);
identity_denied = 0;
assert(drmPrimeFDToHandle(drm, dma, &handle) == 0 && (handle & PVR_HANDLE_BIT));
assert(count_fds() == baseline + 2);
int alias = fixture_fcntl(drm, F_DUPFD_CLOEXEC, 0), out = -1;
int separate = test_open("/dev/null");
assert(drmPrimeHandleToFD(separate, handle, O_CLOEXEC | O_RDWR, &out) != 0);
struct gem_close close_request = {.handle = handle};
int rc = 123;
assert(bl_drm_gem_close(separate, GEM_CLOSE, &close_request, &rc) == 0 && rc == 123);
assert(drmPrimeHandleToFD(alias, handle, O_CLOEXEC | O_RDWR, &out) == 0);
assert(fcntl(out, F_GETFD) & FD_CLOEXEC);
struct stat src, dst;
assert(fstat(dma, &src) == 0 && fstat(out, &dst) == 0);
assert(src.st_dev == dst.st_dev && src.st_ino == dst.st_ino);
fixture_close(out);
assert(drmPrimeHandleToFD(alias, handle, 0, &out) == 0);
assert(!(fcntl(out, F_GETFD) & FD_CLOEXEC));
fixture_close(out);
assert(drmPrimeHandleToFD(alias, handle, 0x40, &out) == -EINVAL);
/* A closed-and-reused descriptor must not inherit the old open file's handles. */
int old_number = drm;
fixture_close(drm);
drm = test_open("/dev/null");
assert(drm == old_number);
assert(drmPrimeHandleToFD(drm, handle, O_CLOEXEC, &out) != 0);
assert(drmCloseBufferHandle(drm, handle) != 0);
assert(drmPrimeHandleToFD(alias, handle, O_CLOEXEC, &out) == 0);
fixture_close(out);
assert(bl_drm_gem_close(alias, GEM_CLOSE, &close_request, &rc) == 1 && rc == 0);
assert(drmPrimeHandleToFD(alias, handle, O_CLOEXEC, &out) != 0);
fixture_close(alias);
fixture_close(separate);
assert(count_fds() == baseline);
empty_table();
/* Real kernel handles and their direct GEM_CLOSE ioctls are never swallowed. */
kernel_error = 0;
assert(drmPrimeFDToHandle(drm, dma, &handle) == 0 && handle == 42);
empty_table();
assert(drmPrimeHandleToFD(drm, handle, O_CLOEXEC, &out) == 0);
fixture_close(out);
close_request.handle = handle;
assert(bl_drm_gem_close(drm, GEM_CLOSE, &close_request, &rc) == 0);
assert(drmCloseBufferHandle(drm, handle) == 0);
assert(native_imports && native_exports && native_closes);
kernel_error = EINVAL;
/* Slot reuse must not reuse public tokens or leak either owned descriptor. */
uint32_t previous = 0;
for (int n = 0; n < 1000; ++n) {
assert(drmPrimeFDToHandle(drm, dma, &handle) == 0 && handle != previous);
assert(drmCloseBufferHandle(drm, handle) == 0);
if (previous) assert(drmPrimeHandleToFD(drm, previous, O_CLOEXEC, &out) != 0);
previous = handle;
}
assert(count_fds() == baseline);
empty_table();
assert(drmPrimeFDToHandle(drm, dma, &handle) == 0);
pid_t child = fork();
assert(child >= 0);
if (child == 0) {
assert(drmPrimeHandleToFD(drm, handle, O_CLOEXEC, &out) == 0);
fixture_close(out);
assert(drmCloseBufferHandle(drm, handle) == 0);
_exit(0);
}
int status;
assert(waitpid(child, &status, 0) == child && WIFEXITED(status) && WEXITSTATUS(status) == 0);
assert(drmPrimeHandleToFD(drm, handle, O_CLOEXEC, &out) == 0);
fixture_close(out);
assert(drmCloseBufferHandle(drm, handle) == 0);
/* Retain the old KGSL path with the PowerVR opt-in unset. */
unsetenv("BL_POWERVR_DRM_HANDLES");
kgsl_present = 1;
kgsl_state = 0;
assert(drmPrimeFDToHandle(dma, drm, &handle) == 0 && !(handle & PVR_HANDLE_BIT));
assert(drmPrimeHandleToFD(dma, handle, O_CLOEXEC | O_RDWR, &out) == 0);
fixture_close(out);
close_request.handle = handle;
assert(bl_drm_gem_close(dma, GEM_CLOSE, &close_request, &rc) == 1 && rc == 0);
assert(drmCloseBufferHandle(dma, handle) == -EINVAL);
empty_table();
assert(count_fds() == baseline);
fixture_close(drm);
fixture_close(dma);
puts("DRM adapter scoping, ownership, reuse, fork and kernel-passthrough tests passed");
}
+26
View File
@@ -0,0 +1,26 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
class DrmHandleTest(unittest.TestCase):
def test_handle_lifetime_and_device_scope(self):
compiler = shutil.which(os.environ.get("CC", "cc"))
if not compiler:
self.fail("A C compiler is required for the DRM adapter regression test")
source = Path(__file__).with_name("drm_handles_test.c")
with tempfile.TemporaryDirectory(prefix="droiddeck-drm-") as tmp:
program = Path(tmp) / "drm-handles"
built = subprocess.run([compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror",
"-UNDEBUG", "-pthread", str(source), "-ldl", "-o", str(program)],
capture_output=True, text=True)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
ran = subprocess.run([str(program)], capture_output=True, text=True, timeout=30)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
if __name__ == "__main__":
unittest.main()