mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
Merge org main c5d7cdc0 (0.6.0+) into the non-Adreno test branch
Conflicts: build.yml takes main's component jobs; the Mali bridge client, Venus GL probe and Xclipse layer build in their own step of the components job (with submodules). vk_present.c keeps main's import list and AHB flag, with the #561 AHB present path still limited to the phone's own driver. TU_DEBUG stays Adreno-only and off under Venus.
This commit is contained in:
commit
379dd900f1
371 files changed
+21595
-1701
No files matched your search
@@ -409,6 +409,8 @@ int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attrib
|
||||
int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
|
||||
void *arg, void *caller, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg,
|
||||
int *rc) __attribute__((visibility("hidden")));
|
||||
|
||||
int ioctl(int fd, unsigned long request, ...) {
|
||||
typedef int (*ioctl_fn)(int, unsigned long, void *);
|
||||
@@ -424,6 +426,7 @@ int ioctl(int fd, unsigned long request, ...) {
|
||||
if (bl_drm_gem_close(fd, request, arg, &rc)) return rc;
|
||||
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
|
||||
if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc;
|
||||
if (bl_writewatch_ioctl(real, fd, request, arg, &rc)) return rc;
|
||||
if (!bl_kgsl_poll(real, fd, request, arg, &rc)) rc = quest_kgsl_ioctl(real, fd, request, arg);
|
||||
bl_kgsl_chip_id_fixup(request, arg, rc);
|
||||
if (arg == NULL || !is_link_query(request)) return rc;
|
||||
|
||||
+279
-74
@@ -17,6 +17,7 @@
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct ns_sem_args {
|
||||
@@ -68,12 +69,13 @@ struct ns_wait_args {
|
||||
#define NS_PROCS 256u
|
||||
#define NS_FDS 65536
|
||||
#define NS_DEVS 8
|
||||
#define NS_VERSION 1u
|
||||
#define NS_VERSION 2u
|
||||
#define NS_HEAD_MAGIC 0x31434e5953544e44ull
|
||||
#define NS_DEV_MAGIC 0x4e54445600000000ull
|
||||
#define NS_OBJ_MAGIC 0x27ull
|
||||
#define NS_WAITERS 0x80000000u
|
||||
#define NS_SWEEP_NS 2000000000ull
|
||||
#define NS_SPIN_NS 4000ull
|
||||
#define NS_DEAD 0xffffffffu
|
||||
|
||||
#ifndef CLOSE_RANGE_CLOEXEC
|
||||
@@ -144,6 +146,7 @@ struct ns_q {
|
||||
uint32_t waiting;
|
||||
uint32_t inuse;
|
||||
uint32_t next_free;
|
||||
uint32_t sleeping;
|
||||
struct ns_ent ent[NS_ENTS];
|
||||
};
|
||||
|
||||
@@ -168,6 +171,7 @@ struct ns_tls {
|
||||
struct ns_dev *d;
|
||||
uint32_t q;
|
||||
int waiting;
|
||||
int sleep;
|
||||
};
|
||||
|
||||
static struct ns_dev ns_devs[NS_DEVS];
|
||||
@@ -255,19 +259,148 @@ static void ns_unlock(uint32_t *w) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ns_block(sigset_t *old) {
|
||||
sigset_t s;
|
||||
sigfillset(&s);
|
||||
sigdelset(&s, SIGSEGV);
|
||||
sigdelset(&s, SIGBUS);
|
||||
sigdelset(&s, SIGILL);
|
||||
sigdelset(&s, SIGFPE);
|
||||
sigdelset(&s, SIGTRAP);
|
||||
pthread_sigmask(SIG_BLOCK, &s, old);
|
||||
#define NS_DEFER 4
|
||||
#define NS_LEVELS 8
|
||||
|
||||
struct ns_cs {
|
||||
int depth;
|
||||
int count;
|
||||
int level;
|
||||
unsigned held_levels;
|
||||
int sigs[NS_DEFER];
|
||||
siginfo_t info[NS_DEFER];
|
||||
sigset_t held[NS_LEVELS];
|
||||
};
|
||||
|
||||
static __thread struct ns_cs ns_cs;
|
||||
static uintptr_t ns_handlers[_NSIG];
|
||||
static uint8_t ns_siginfo[_NSIG];
|
||||
|
||||
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
|
||||
|
||||
static int ns_deferrable(int sig) {
|
||||
return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL
|
||||
&& sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS;
|
||||
}
|
||||
|
||||
static void ns_restore(const sigset_t *old) {
|
||||
pthread_sigmask(SIG_SETMASK, old, NULL);
|
||||
static int ns_defer(int sig, const siginfo_t *si) {
|
||||
struct ns_cs *cs = &ns_cs;
|
||||
if (sig < SIGRTMIN)
|
||||
for (int i = 0; i < NS_DEFER; i++)
|
||||
if (__atomic_load_n(&cs->sigs[i], __ATOMIC_RELAXED) == sig) return 1;
|
||||
int i = __atomic_fetch_add(&cs->count, 1, __ATOMIC_RELAXED);
|
||||
if (i >= NS_DEFER) {
|
||||
__atomic_fetch_sub(&cs->count, 1, __ATOMIC_RELAXED);
|
||||
return 0;
|
||||
}
|
||||
if (si) memcpy(&cs->info[i], si, sizeof(*si));
|
||||
else memset(&cs->info[i], 0, sizeof(*si));
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&cs->sigs[i], sig, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ns_park(int sig, const siginfo_t *si, ucontext_t *uc) {
|
||||
int level = ns_cs.level;
|
||||
if (level < 0 || level >= NS_LEVELS) return 0;
|
||||
sigset_t one;
|
||||
sigemptyset(&one);
|
||||
sigaddset(&one, sig);
|
||||
int saved = errno;
|
||||
pthread_sigmask(SIG_BLOCK, &one, NULL);
|
||||
pid_t pid = getpid(), tid = (pid_t)ns_self();
|
||||
int ok = (si && syscall(SYS_rt_tgsigqueueinfo, pid, tid, sig, si) == 0) || syscall(SYS_tgkill, pid, tid, sig) == 0;
|
||||
if (!ok) pthread_sigmask(SIG_UNBLOCK, &one, NULL);
|
||||
errno = saved;
|
||||
if (!ok) return 0;
|
||||
sigaddset(&uc->uc_sigmask, sig);
|
||||
unsigned long *words = (unsigned long *)&ns_cs.held[level];
|
||||
unsigned bits = 8 * sizeof(unsigned long);
|
||||
__atomic_fetch_or(&words[(unsigned)(sig - 1) / bits], 1ul << ((unsigned)(sig - 1) % bits), __ATOMIC_RELAXED);
|
||||
__atomic_fetch_or(&ns_cs.held_levels, 1u << level, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ns_trampoline(int sig, siginfo_t *si, void *uc) {
|
||||
if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && (ns_defer(sig, si) || (uc && ns_park(sig, si, uc)))) return;
|
||||
uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
|
||||
if (!h) return;
|
||||
ns_cs.level++;
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc);
|
||||
else ((void (*)(int))h)(sig);
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
ns_cs.level--;
|
||||
}
|
||||
|
||||
static void ns_flush(void) {
|
||||
struct ns_cs *cs = &ns_cs;
|
||||
sigset_t all, mask;
|
||||
sigfillset(&all);
|
||||
int saved = errno;
|
||||
pthread_sigmask(SIG_BLOCK, &all, &mask);
|
||||
int n = cs->count < NS_DEFER ? cs->count : NS_DEFER;
|
||||
pid_t pid = getpid(), tid = (pid_t)ns_self();
|
||||
for (int i = 0; i < n; i++)
|
||||
if (cs->sigs[i] > 0 && syscall(SYS_rt_tgsigqueueinfo, pid, tid, cs->sigs[i], &cs->info[i]) != 0) syscall(SYS_tgkill, pid, tid, cs->sigs[i]);
|
||||
memset(cs->sigs, 0, sizeof(cs->sigs));
|
||||
cs->count = 0;
|
||||
int level = cs->level;
|
||||
if (level >= 0 && level < NS_LEVELS && (cs->held_levels & (1u << level))) {
|
||||
for (int sig = 1; sig < _NSIG; sig++)
|
||||
if (sigismember(&cs->held[level], sig) == 1) sigdelset(&mask, sig);
|
||||
sigemptyset(&cs->held[level]);
|
||||
cs->held_levels &= ~(1u << level);
|
||||
}
|
||||
pthread_sigmask(SIG_SETMASK, &mask, NULL);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static void ns_block(void) {
|
||||
ns_cs.depth++;
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
static void ns_restore(void) {
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (--ns_cs.depth == 0) {
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (ns_cs.count || (ns_cs.held_levels && ns_cs.level >= 0 && ns_cs.level < NS_LEVELS && (ns_cs.held_levels & (1u << ns_cs.level))))
|
||||
ns_flush();
|
||||
}
|
||||
}
|
||||
|
||||
int sigaction(int sig, const struct sigaction *act, struct sigaction *old) {
|
||||
static int (*real)(int, const struct sigaction *, struct sigaction *);
|
||||
if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
|
||||
if (!real) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old);
|
||||
uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
|
||||
uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED);
|
||||
struct sigaction mine;
|
||||
const struct sigaction *use = act;
|
||||
if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && !(act->sa_flags & SA_RESETHAND) &&
|
||||
bl_from_ntdll(__builtin_return_address(0))) {
|
||||
mine = *act;
|
||||
mine.sa_sigaction = ns_trampoline;
|
||||
mine.sa_flags |= SA_SIGINFO;
|
||||
__atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE);
|
||||
use = &mine;
|
||||
}
|
||||
int r = real(sig, use, old);
|
||||
if (r != 0 && use == &mine) {
|
||||
__atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE);
|
||||
}
|
||||
if (r == 0 && old && old->sa_sigaction == ns_trampoline) {
|
||||
old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev;
|
||||
if (!prev_info) old->sa_flags &= ~SA_SIGINFO;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) {
|
||||
@@ -378,10 +511,11 @@ static int ns_claim(struct ns_q *q, int32_t index) {
|
||||
if (dead) return 0;
|
||||
}
|
||||
int32_t want = -1;
|
||||
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
|
||||
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void ns_wake(struct ns_q *q) {
|
||||
if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return;
|
||||
int saved = errno;
|
||||
ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0);
|
||||
errno = saved;
|
||||
@@ -618,14 +752,13 @@ static void ns_dev_put_locked(struct ns_dev *d) {
|
||||
|
||||
static void ns_dev_put(struct ns_dev *d) {
|
||||
if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return;
|
||||
sigset_t old;
|
||||
int saved = errno;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
uint32_t zero = 0;
|
||||
if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d);
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
@@ -682,20 +815,18 @@ static void ns_forget_locked(int fd) {
|
||||
}
|
||||
|
||||
static void ns_forget(int fd) {
|
||||
sigset_t old;
|
||||
int saved = errno;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
ns_forget_locked(fd);
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) {
|
||||
uint64_t e = 0;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out;
|
||||
if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out;
|
||||
@@ -710,7 +841,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u
|
||||
ns_unlock(&o->lock);
|
||||
out:
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -719,8 +850,7 @@ static uint64_t ns_note_device(int fd) {
|
||||
uint64_t e = 0;
|
||||
if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0;
|
||||
if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL;
|
||||
if (d) {
|
||||
@@ -732,7 +862,7 @@ static uint64_t ns_note_device(int fd) {
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -773,15 +903,14 @@ static int ns_ready(struct ns_dev *d) {
|
||||
|
||||
static void ns_thread_gone(void *p) {
|
||||
struct ns_tls *t = p;
|
||||
if (!t || !t->d || !t->q) return;
|
||||
sigset_t old;
|
||||
if (!t || !t->d) return;
|
||||
struct ns_dev *d = t->d;
|
||||
ns_block(&old);
|
||||
ns_free_q(d->h, t->q - 1);
|
||||
ns_block();
|
||||
if (t->q) ns_free_q(d->h, t->q - 1);
|
||||
t->q = 0;
|
||||
t->waiting = 0;
|
||||
t->d = NULL;
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
ns_dev_put(d);
|
||||
}
|
||||
|
||||
@@ -789,24 +918,28 @@ static void ns_key_init(void) {
|
||||
pthread_key_create(&ns_key, ns_thread_gone);
|
||||
}
|
||||
|
||||
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
if (ns_tq.d && ns_tq.q && ns_tq.waiting) {
|
||||
ns_drop_q(ns_tq.d->h, ns_tq.q - 1);
|
||||
ns_tq.waiting = 0;
|
||||
}
|
||||
if (ns_tq.d == d && ns_tq.q) {
|
||||
*qi = ns_tq.q - 1;
|
||||
return ns_q(h, *qi);
|
||||
}
|
||||
if (ns_tq.d && ns_tq.q) {
|
||||
struct ns_dev *old = ns_tq.d;
|
||||
ns_free_q(old->h, ns_tq.q - 1);
|
||||
static int ns_hold(struct ns_dev *d) {
|
||||
if (ns_tq.d == d) return 1;
|
||||
if (!ns_dev_get(d)) return 0;
|
||||
if (ns_tq.sleep) return 2;
|
||||
struct ns_dev *old = ns_tq.d;
|
||||
if (old) {
|
||||
ns_block();
|
||||
if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1);
|
||||
ns_tq.q = 0;
|
||||
ns_tq.waiting = 0;
|
||||
ns_tq.d = NULL;
|
||||
ns_restore();
|
||||
ns_dev_put(old);
|
||||
}
|
||||
pthread_once(&ns_once, ns_key_init);
|
||||
ns_tq.d = d;
|
||||
pthread_setspecific(ns_key, &ns_tq);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static struct ns_q *ns_new_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
uint32_t i;
|
||||
if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) {
|
||||
ns_sweep_dead(h);
|
||||
@@ -815,22 +948,35 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_q *q = ns_q(h, i);
|
||||
q->proc = (uint32_t)d->proc;
|
||||
q->waiting = 0;
|
||||
q->sleeping = 0;
|
||||
q->inuse = 1;
|
||||
__atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL);
|
||||
ns_tq.d = d;
|
||||
ns_tq.q = i + 1;
|
||||
ns_tq.waiting = 0;
|
||||
pthread_setspecific(ns_key, &ns_tq);
|
||||
*qi = i;
|
||||
return q;
|
||||
}
|
||||
|
||||
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
if (ns_tq.d != d) return NULL;
|
||||
if (ns_tq.q && ns_tq.waiting) {
|
||||
ns_drop_q(h, ns_tq.q - 1);
|
||||
ns_tq.waiting = 0;
|
||||
}
|
||||
if (ns_tq.q) {
|
||||
*qi = ns_tq.q - 1;
|
||||
return ns_q(h, *qi);
|
||||
}
|
||||
struct ns_q *q = ns_new_q(d, qi);
|
||||
if (!q) return NULL;
|
||||
ns_tq.q = *qi + 1;
|
||||
ns_tq.waiting = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
|
||||
struct ns_head *h = d->h;
|
||||
sigset_t old;
|
||||
int fd = -1, err = 0;
|
||||
uint32_t s = 0;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
if (!ns_table() || ns_attach(d) < 0) {
|
||||
err = ENOMEM;
|
||||
@@ -879,7 +1025,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
|
||||
ns_unlock(&o->lock);
|
||||
out:
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
if (fd < 0) errno = err;
|
||||
return fd;
|
||||
}
|
||||
@@ -890,6 +1036,20 @@ static int ns_expired(const struct ns_wait_args *args) {
|
||||
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout;
|
||||
}
|
||||
|
||||
static uint64_t ns_now(void) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec;
|
||||
}
|
||||
|
||||
static void ns_spin(struct ns_q *q) {
|
||||
uint64_t end = ns_now() + NS_SPIN_NS;
|
||||
for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) {
|
||||
ns_relax();
|
||||
if (!(i & 63) && ns_now() >= end) return;
|
||||
}
|
||||
}
|
||||
|
||||
static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
|
||||
struct timespec ts, *tsp = NULL;
|
||||
int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0);
|
||||
@@ -898,14 +1058,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
|
||||
ts.tv_nsec = (long)(args->timeout % 1000000000ull);
|
||||
tsp = &ts;
|
||||
}
|
||||
while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
|
||||
if (tsp && ns_expired(args)) return ETIMEDOUT;
|
||||
if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) {
|
||||
if (errno == ETIMEDOUT) return ETIMEDOUT;
|
||||
if (errno == EINTR) return EINTR;
|
||||
}
|
||||
ns_spin(q);
|
||||
int ret = 0;
|
||||
__atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST);
|
||||
while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) {
|
||||
if (tsp && ns_expired(args)) ret = ETIMEDOUT;
|
||||
else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR))
|
||||
ret = errno;
|
||||
}
|
||||
return 0;
|
||||
__atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) {
|
||||
for (uint32_t i = 0; i < total; i++) {
|
||||
struct ns_obj *o = ns_obj(h, slots[i]);
|
||||
ns_lock(&o->lock);
|
||||
int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint;
|
||||
if (!busy && ns_signaled(o, owner)) {
|
||||
switch (o->type) {
|
||||
case NS_SEM:
|
||||
o->a--;
|
||||
break;
|
||||
case NS_MUTEX:
|
||||
*dead = (int)o->ownerdead;
|
||||
o->ownerdead = 0;
|
||||
o->a++;
|
||||
o->b = owner;
|
||||
break;
|
||||
case NS_EVENT:
|
||||
if (!o->a) o->b = 0;
|
||||
break;
|
||||
}
|
||||
ns_unlock(&o->lock);
|
||||
return (int)i;
|
||||
}
|
||||
ns_unlock(&o->lock);
|
||||
if (busy) return -1;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
@@ -927,12 +1118,24 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
for (uint32_t j = 0; j < i; j++)
|
||||
if (slots[j] == slots[i]) return EINVAL;
|
||||
}
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
int nested = ns_tq.sleep > 0;
|
||||
ns_block();
|
||||
if (!all && (nested || !ns_tq.waiting)) {
|
||||
int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead);
|
||||
if (got >= 0) {
|
||||
ns_restore();
|
||||
ua->index = (uint32_t)got;
|
||||
return dead ? EOWNERDEAD : 0;
|
||||
}
|
||||
if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) {
|
||||
ns_restore();
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
uint32_t qi;
|
||||
struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL;
|
||||
struct ns_q *q = !ns_ready(d) ? NULL : nested ? ns_new_q(d, &qi) : ns_thread_q(d, &qi);
|
||||
if (!q) {
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return ENOMEM;
|
||||
}
|
||||
ns_maybe_sweep(h);
|
||||
@@ -952,7 +1155,7 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
}
|
||||
q->total = total;
|
||||
q->waiting = 1;
|
||||
ns_tq.waiting = 1;
|
||||
if (!nested) ns_tq.waiting = 1;
|
||||
int bad = 0;
|
||||
if (!all) {
|
||||
for (uint32_t i = 0; i < total && !bad; i++) {
|
||||
@@ -1004,16 +1207,18 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
}
|
||||
int ret = 0;
|
||||
if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
|
||||
sigset_t held;
|
||||
ns_restore(&old);
|
||||
ns_tq.sleep++;
|
||||
ns_restore();
|
||||
ret = ns_sleep(q, &args);
|
||||
ns_block(&held);
|
||||
ns_block();
|
||||
ns_tq.sleep--;
|
||||
}
|
||||
ns_drop_q(h, qi);
|
||||
ns_tq.waiting = 0;
|
||||
if (!nested) ns_tq.waiting = 0;
|
||||
int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE);
|
||||
int dead = (int)q->ownerdead;
|
||||
ns_restore(&old);
|
||||
if (nested) ns_free_q(h, qi);
|
||||
ns_restore();
|
||||
if (bad) return EINVAL;
|
||||
if (got != -1) {
|
||||
ua->index = (uint32_t)got;
|
||||
@@ -1061,12 +1266,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
|
||||
return ENOTTY;
|
||||
}
|
||||
if (type != (uint32_t)want) return EINVAL;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
int all = ns_lock_obj(h, o);
|
||||
if (o->type != (uint32_t)want || o->gen != gen) {
|
||||
ns_unlock_obj(h, o, all);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return EINVAL;
|
||||
}
|
||||
switch (req) {
|
||||
@@ -1133,7 +1337,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
|
||||
}
|
||||
}
|
||||
ns_unlock_obj(h, o, all);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
if (ret) return ret;
|
||||
if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE)
|
||||
memcpy(arg, &prev, sizeof(prev));
|
||||
@@ -1176,9 +1380,10 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) {
|
||||
uint64_t e = ns_lookup(fd);
|
||||
if (!e) return 0;
|
||||
struct ns_dev *d = &ns_devs[(e >> 56) & 7];
|
||||
if (!ns_dev_get(d)) return 0;
|
||||
int held = ns_hold(d);
|
||||
if (!held) return 0;
|
||||
int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg);
|
||||
ns_dev_put(d);
|
||||
if (held == 2) ns_dev_put(d);
|
||||
if (err) {
|
||||
errno = err;
|
||||
*rc = -1;
|
||||
|
||||
@@ -329,6 +329,8 @@ static int execveat_is_execve(const long *args) {
|
||||
return path[0] == '/' || (int)args[0] == AT_FDCWD;
|
||||
}
|
||||
|
||||
int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) __attribute__((visibility("hidden")));
|
||||
|
||||
long syscall(long number, ...) {
|
||||
va_list ap;
|
||||
long args[6];
|
||||
@@ -354,6 +356,9 @@ long syscall(long number, ...) {
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
if (bl_writewatch_syscall(number, args, real_syscall, &handled)) {
|
||||
return handled;
|
||||
}
|
||||
if (number == SYS_openat2) {
|
||||
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
|
||||
long fd = real_syscall(number, args[0], args[1], args[2], args[3]);
|
||||
|
||||
@@ -51,9 +51,16 @@ static void rt_send(void *dpy, uint32_t action) {
|
||||
}
|
||||
}
|
||||
|
||||
static void *rt_xtst(const char *name) {
|
||||
void *fn = dlsym(RTLD_NEXT, name);
|
||||
if (fn) return fn;
|
||||
void *lib = dlopen("libXtst.so.6", RTLD_LAZY | RTLD_NOLOAD);
|
||||
return lib ? dlsym(lib, name) : NULL;
|
||||
}
|
||||
|
||||
int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long delay) {
|
||||
static int (*real)(void *, int, int, int, unsigned long);
|
||||
if (!real) real = (int (*)(void *, int, int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeMotionEvent");
|
||||
if (!real) real = (int (*)(void *, int, int, int, unsigned long))rt_xtst("XTestFakeMotionEvent");
|
||||
if (rt_enabled()) {
|
||||
pthread_mutex_lock(&rt_lock);
|
||||
rt_x = x;
|
||||
@@ -69,7 +76,7 @@ int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long dela
|
||||
|
||||
int XTestFakeButtonEvent(void *dpy, unsigned int button, int press, unsigned long delay) {
|
||||
static int (*real)(void *, unsigned int, int, unsigned long);
|
||||
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeButtonEvent");
|
||||
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))rt_xtst("XTestFakeButtonEvent");
|
||||
if (button == 1) {
|
||||
pthread_mutex_lock(&rt_lock);
|
||||
int handled = 0;
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#define STATUS_MAX 16384
|
||||
#define TRACER_FIELD "\nTracerPid:\t"
|
||||
|
||||
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
|
||||
|
||||
static const char *skip_digits(const char *s) {
|
||||
const char *end = s;
|
||||
while (isdigit((unsigned char)*end)) end++;
|
||||
@@ -77,7 +79,8 @@ __attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *p
|
||||
int out = -1;
|
||||
|
||||
long pid;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller)) return -1;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || bl_from_ntdll(caller))
|
||||
return -1;
|
||||
int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (in < 0) goto done;
|
||||
for (;;) {
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <link.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static uintptr_t ntdll_lo, ntdll_hi;
|
||||
|
||||
static int find_ntdll(struct dl_phdr_info *info, size_t size, void *data) {
|
||||
(void)size;
|
||||
(void)data;
|
||||
const char *slash = strrchr(info->dlpi_name, '/');
|
||||
if (strcmp(slash ? slash + 1 : info->dlpi_name, "ntdll.so") != 0) return 0;
|
||||
for (int i = 0; i < info->dlpi_phnum; i++) {
|
||||
const ElfW(Phdr) *ph = &info->dlpi_phdr[i];
|
||||
if (ph->p_type != PT_LOAD || !(ph->p_flags & PF_X)) continue;
|
||||
uintptr_t lo = info->dlpi_addr + ph->p_vaddr;
|
||||
__atomic_store_n(&ntdll_hi, lo + ph->p_memsz, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ntdll_lo, lo, __ATOMIC_RELEASE);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_from_ntdll(const void *caller) {
|
||||
uintptr_t pc = (uintptr_t)caller, lo = __atomic_load_n(&ntdll_lo, __ATOMIC_ACQUIRE);
|
||||
if (!lo) {
|
||||
dl_iterate_phdr(find_ntdll, NULL);
|
||||
lo = __atomic_load_n(&ntdll_lo, __ATOMIC_ACQUIRE);
|
||||
if (!lo) return 0;
|
||||
}
|
||||
return pc >= lo && pc < __atomic_load_n(&ntdll_hi, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
#if defined(__aarch64__)
|
||||
#include <asm/hwcap.h>
|
||||
#include <asm/sigcontext.h>
|
||||
|
||||
#define BL_STR2(x) #x
|
||||
#define BL_STR(x) BL_STR2(x)
|
||||
|
||||
#define BL_HWCAP_GCS (1UL << 32)
|
||||
#define BL_FRAME_SIZE 4688
|
||||
#define BL_REGS 312
|
||||
#define BL_SP 560
|
||||
#define BL_PC 568
|
||||
#define BL_PSTATE 576
|
||||
#define BL_FPSIMD 592
|
||||
|
||||
struct bl_sigframe {
|
||||
siginfo_t info;
|
||||
ucontext_t uc;
|
||||
};
|
||||
|
||||
struct bl_kernel_sigaction {
|
||||
void (*handler)(int, siginfo_t *, void *);
|
||||
unsigned long flags;
|
||||
void *restorer;
|
||||
uint64_t mask;
|
||||
};
|
||||
|
||||
struct bl_usr2_call {
|
||||
void (*handler)(int, siginfo_t *, void *);
|
||||
uint64_t old_mask;
|
||||
};
|
||||
|
||||
_Static_assert(sizeof(struct bl_sigframe) == BL_FRAME_SIZE, "rt_sigframe size");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.regs) == BL_REGS, "regs offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.sp) == BL_SP, "sp offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.pc) == BL_PC, "pc offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.pstate) == BL_PSTATE, "pstate offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.__reserved) == BL_FPSIMD, "reserved offset");
|
||||
_Static_assert(sizeof(struct fpsimd_context) + sizeof(struct _aarch64_ctx) <=
|
||||
sizeof(((mcontext_t *)0)->__reserved), "reserved space");
|
||||
|
||||
__attribute__((visibility("hidden"))) void bl_usr2_frame(struct bl_usr2_call *call);
|
||||
__attribute__((visibility("hidden"), used)) void bl_usr2_run(struct bl_usr2_call *call, struct bl_sigframe *frame);
|
||||
|
||||
__asm__(".text\n"
|
||||
".p2align 2\n"
|
||||
".type bl_usr2_frame, %function\n"
|
||||
"bl_usr2_frame:\n"
|
||||
" mov x16, #" BL_STR(BL_FRAME_SIZE) "\n"
|
||||
" sub sp, sp, x16\n"
|
||||
" add x17, sp, #" BL_STR(BL_REGS) "\n"
|
||||
" stp x0, x1, [x17, #0]\n"
|
||||
" stp x2, x3, [x17, #16]\n"
|
||||
" stp x4, x5, [x17, #32]\n"
|
||||
" stp x6, x7, [x17, #48]\n"
|
||||
" stp x8, x9, [x17, #64]\n"
|
||||
" stp x10, x11, [x17, #80]\n"
|
||||
" stp x12, x13, [x17, #96]\n"
|
||||
" stp x14, x15, [x17, #112]\n"
|
||||
" stp xzr, xzr, [x17, #128]\n"
|
||||
" stp x18, x19, [x17, #144]\n"
|
||||
" stp x20, x21, [x17, #160]\n"
|
||||
" stp x22, x23, [x17, #176]\n"
|
||||
" stp x24, x25, [x17, #192]\n"
|
||||
" stp x26, x27, [x17, #208]\n"
|
||||
" stp x28, x29, [x17, #224]\n"
|
||||
" str x30, [x17, #240]\n"
|
||||
" str xzr, [x17, #0]\n"
|
||||
" add x16, sp, x16\n"
|
||||
" str x16, [sp, #" BL_STR(BL_SP) "]\n"
|
||||
" adr x16, 1f\n"
|
||||
" str x16, [sp, #" BL_STR(BL_PC) "]\n"
|
||||
" mrs x16, nzcv\n"
|
||||
" str x16, [sp, #" BL_STR(BL_PSTATE) "]\n"
|
||||
" add x17, sp, #" BL_STR(BL_FPSIMD) "\n"
|
||||
" mrs x16, fpsr\n"
|
||||
" str w16, [x17, #8]\n"
|
||||
" mrs x16, fpcr\n"
|
||||
" str w16, [x17, #12]\n"
|
||||
" stp q0, q1, [x17, #16]\n"
|
||||
" stp q2, q3, [x17, #48]\n"
|
||||
" stp q4, q5, [x17, #80]\n"
|
||||
" stp q6, q7, [x17, #112]\n"
|
||||
" stp q8, q9, [x17, #144]\n"
|
||||
" stp q10, q11, [x17, #176]\n"
|
||||
" stp q12, q13, [x17, #208]\n"
|
||||
" stp q14, q15, [x17, #240]\n"
|
||||
" stp q16, q17, [x17, #272]\n"
|
||||
" stp q18, q19, [x17, #304]\n"
|
||||
" stp q20, q21, [x17, #336]\n"
|
||||
" stp q22, q23, [x17, #368]\n"
|
||||
" stp q24, q25, [x17, #400]\n"
|
||||
" stp q26, q27, [x17, #432]\n"
|
||||
" stp q28, q29, [x17, #464]\n"
|
||||
" stp q30, q31, [x17, #496]\n"
|
||||
" mov x1, sp\n"
|
||||
" bl bl_usr2_run\n"
|
||||
" mov x8, #" BL_STR(__NR_rt_sigreturn) "\n"
|
||||
" svc #0\n"
|
||||
" brk #0\n"
|
||||
"1:\n"
|
||||
" ret\n"
|
||||
".size bl_usr2_frame, .-bl_usr2_frame\n");
|
||||
|
||||
static int (*usr2_real_raise)(int);
|
||||
|
||||
static long usr2_sys(long nr, long a, long b, long c, long d) {
|
||||
register long x8 __asm__("x8") = nr;
|
||||
register long x0 __asm__("x0") = a;
|
||||
register long x1 __asm__("x1") = b;
|
||||
register long x2 __asm__("x2") = c;
|
||||
register long x3 __asm__("x3") = d;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
|
||||
return x0;
|
||||
}
|
||||
|
||||
static uint64_t usr2_ssbs(void) {
|
||||
uint64_t value = 0;
|
||||
if (getauxval(AT_HWCAP) & HWCAP_SSBS) __asm__ volatile("mrs %0, s3_3_c4_c2_6" : "=r"(value));
|
||||
return value & (1UL << 12);
|
||||
}
|
||||
|
||||
void bl_usr2_run(struct bl_usr2_call *call, struct bl_sigframe *frame) {
|
||||
struct fpsimd_context *fp = (struct fpsimd_context *)frame->uc.uc_mcontext.__reserved;
|
||||
struct _aarch64_ctx *end = (struct _aarch64_ctx *)(fp + 1);
|
||||
|
||||
memset(&frame->info, 0, sizeof(frame->info));
|
||||
frame->info.si_signo = SIGUSR2;
|
||||
frame->info.si_code = SI_TKILL;
|
||||
frame->info.si_pid = (pid_t)usr2_sys(__NR_getpid, 0, 0, 0, 0);
|
||||
frame->info.si_uid = (uid_t)usr2_sys(__NR_getuid, 0, 0, 0, 0);
|
||||
frame->uc.uc_flags = 0;
|
||||
frame->uc.uc_link = NULL;
|
||||
if (usr2_sys(__NR_sigaltstack, 0, (long)&frame->uc.uc_stack, 0, 0) != 0) {
|
||||
memset(&frame->uc.uc_stack, 0, sizeof(frame->uc.uc_stack));
|
||||
frame->uc.uc_stack.ss_flags = SS_DISABLE;
|
||||
}
|
||||
memset(&frame->uc.uc_sigmask, 0, sizeof(frame->uc.uc_sigmask));
|
||||
memcpy(&frame->uc.uc_sigmask, &call->old_mask, sizeof(call->old_mask));
|
||||
frame->uc.uc_mcontext.fault_address = 0;
|
||||
frame->uc.uc_mcontext.pstate |= usr2_ssbs();
|
||||
fp->head.magic = FPSIMD_MAGIC;
|
||||
fp->head.size = sizeof(*fp);
|
||||
end->magic = 0;
|
||||
end->size = 0;
|
||||
call->handler(SIGUSR2, &frame->info, &frame->uc);
|
||||
}
|
||||
|
||||
int raise(int sig) {
|
||||
int (*real)(int) = __atomic_load_n(&usr2_real_raise, __ATOMIC_ACQUIRE);
|
||||
if (!real) {
|
||||
real = (int (*)(int))dlsym(RTLD_NEXT, "raise");
|
||||
__atomic_store_n(&usr2_real_raise, real, __ATOMIC_RELEASE);
|
||||
}
|
||||
if (sig != SIGUSR2 || (getauxval(AT_HWCAP) & BL_HWCAP_GCS) || !bl_from_ntdll(__builtin_return_address(0))) {
|
||||
return real(sig);
|
||||
}
|
||||
|
||||
struct bl_kernel_sigaction action;
|
||||
if (usr2_sys(__NR_rt_sigaction, SIGUSR2, 0, (long)&action, sizeof(action.mask)) != 0) return real(sig);
|
||||
if (!(action.flags & SA_SIGINFO) || (action.flags & SA_RESETHAND) || (uintptr_t)action.handler <= 1) {
|
||||
return real(sig);
|
||||
}
|
||||
|
||||
uint64_t bit = 1ULL << (SIGUSR2 - 1);
|
||||
uint64_t block = action.mask | ((action.flags & SA_NODEFER) ? 0 : bit);
|
||||
struct bl_usr2_call call = {action.handler, 0};
|
||||
if (usr2_sys(__NR_rt_sigprocmask, SIG_BLOCK, (long)&block, (long)&call.old_mask, sizeof(block)) != 0) {
|
||||
return real(sig);
|
||||
}
|
||||
if (call.old_mask & bit) {
|
||||
usr2_sys(__NR_rt_sigprocmask, SIG_SETMASK, (long)&call.old_mask, 0, sizeof(call.old_mask));
|
||||
return real(sig);
|
||||
}
|
||||
bl_usr2_frame(&call);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,113 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/userfaultfd.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define WINE_ARM64_USERFAULTFD 374
|
||||
#define PM_UFFD_WP (1ULL << 57)
|
||||
#define PM_SWAP (1ULL << 62)
|
||||
#define PM_PRESENT (1ULL << 63)
|
||||
|
||||
#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
static int ww_uffd = -1;
|
||||
static int scan_blocked;
|
||||
#endif
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) {
|
||||
#if defined(__aarch64__) && defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
if (number != WINE_ARM64_USERFAULTFD || (args[0] & ~(long)(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY))) return 0;
|
||||
*rc = real(SYS_userfaultfd, args[0]);
|
||||
if (*rc >= 0) __atomic_store_n(&ww_uffd, (int)*rc, __ATOMIC_RELEASE);
|
||||
return 1;
|
||||
#else
|
||||
(void)number;
|
||||
(void)args;
|
||||
(void)real;
|
||||
(void)rc;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
static int protect(int uffd, uint64_t start, uint64_t end) {
|
||||
if (start == end) return 0;
|
||||
struct uffdio_writeprotect wp = { .range = { start, end - start }, .mode = UFFDIO_WRITEPROTECT_MODE_WP };
|
||||
return ioctl(uffd, UFFDIO_WRITEPROTECT, &wp);
|
||||
}
|
||||
|
||||
static long emulate_scan(int fd, int uffd, struct pm_scan_arg *a) {
|
||||
uint64_t page = (uint64_t)getpagesize();
|
||||
if (a->size < sizeof(*a) || (a->flags & ~(uint64_t)PM_SCAN_WP_MATCHING) || a->category_inverted || a->category_anyof_mask
|
||||
|| a->category_mask != PAGE_IS_WRITTEN || (a->return_mask & ~(uint64_t)PAGE_IS_WRITTEN) || a->end < a->start
|
||||
|| ((a->start | a->end) & (page - 1)) || (a->vec_len && !a->vec)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
struct page_region *vec = (struct page_region *)(uintptr_t)a->vec;
|
||||
uint64_t regions = 0, pages = 0, addr = a->start, wp_start = 0, wp_end = 0;
|
||||
uint64_t entries[512];
|
||||
int wp = (a->flags & PM_SCAN_WP_MATCHING) != 0;
|
||||
while (addr < a->end) {
|
||||
uint64_t want = (a->end - addr) / page;
|
||||
if (want > 512) want = 512;
|
||||
ssize_t got = pread(fd, entries, want * sizeof(entries[0]), (off_t)(addr / page * sizeof(entries[0])));
|
||||
if (got < 0) return -1;
|
||||
if (got == 0) break;
|
||||
for (uint64_t i = 0; i < (uint64_t)got / sizeof(entries[0]); i++, addr += page) {
|
||||
uint64_t e = entries[i];
|
||||
if (!(e & (PM_PRESENT | PM_SWAP)) || (e & PM_UFFD_WP)) continue;
|
||||
if (a->max_pages && pages >= a->max_pages) goto done;
|
||||
if (a->vec_len) {
|
||||
if (regions && vec[regions - 1].end == addr) {
|
||||
vec[regions - 1].end += page;
|
||||
} else {
|
||||
if (regions == a->vec_len) goto done;
|
||||
vec[regions].start = addr;
|
||||
vec[regions].end = addr + page;
|
||||
vec[regions].categories = PAGE_IS_WRITTEN & a->return_mask;
|
||||
regions++;
|
||||
}
|
||||
}
|
||||
pages++;
|
||||
if (!wp) continue;
|
||||
if (wp_end != addr) {
|
||||
if (protect(uffd, wp_start, wp_end) < 0) return -1;
|
||||
wp_start = addr;
|
||||
}
|
||||
wp_end = addr + page;
|
||||
}
|
||||
}
|
||||
done:
|
||||
if (wp && protect(uffd, wp_start, wp_end) < 0) return -1;
|
||||
a->walk_end = addr < a->end ? addr : a->end;
|
||||
return (long)regions;
|
||||
}
|
||||
#endif
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
|
||||
void *arg, int *rc) {
|
||||
#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
if (request != PAGEMAP_SCAN) return 0;
|
||||
int uffd = __atomic_load_n(&ww_uffd, __ATOMIC_ACQUIRE);
|
||||
if (uffd < 0) return 0;
|
||||
if (!__atomic_load_n(&scan_blocked, __ATOMIC_RELAXED)) {
|
||||
*rc = real(fd, request, arg);
|
||||
if (*rc >= 0 || errno != EACCES) return 1;
|
||||
__atomic_store_n(&scan_blocked, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
*rc = (int)emulate_scan(fd, uffd, (struct pm_scan_arg *)arg);
|
||||
return 1;
|
||||
#else
|
||||
(void)real;
|
||||
(void)fd;
|
||||
(void)request;
|
||||
(void)arg;
|
||||
(void)rc;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
@@ -126,6 +126,25 @@ static void zr_walk(int compress) {
|
||||
zr_sys(SYS_close, maps);
|
||||
}
|
||||
|
||||
// Before it stops a session for suspend, the app waits for <ZR_CTL>.ack.<pid> to appear (the handler
|
||||
// runs here, not one a program installed over it) and then to hold a byte (the page-out finished).
|
||||
static void zr_ack(int done) {
|
||||
static const char suffix[] = ".ack.";
|
||||
char path[sizeof(ZR_CTL) + sizeof(suffix) + 20];
|
||||
char digits[20];
|
||||
size_t n = 0, d = 0;
|
||||
for (const char *p = ZR_CTL; *p; p++) path[n++] = *p;
|
||||
for (const char *p = suffix; *p; p++) path[n++] = *p;
|
||||
long pid = zr_sys(SYS_getpid);
|
||||
do digits[d++] = (char)('0' + pid % 10); while ((pid /= 10) > 0);
|
||||
while (d > 0) path[n++] = digits[--d];
|
||||
path[n] = 0;
|
||||
int fd = (int)zr_sys(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||||
if (fd < 0) return;
|
||||
if (done) zr_sys(SYS_write, fd, "1", 1UL);
|
||||
zr_sys(SYS_close, fd);
|
||||
}
|
||||
|
||||
static void zr_handler(int sig, siginfo_t *info, void *context) {
|
||||
(void)sig;
|
||||
(void)context;
|
||||
@@ -136,7 +155,9 @@ static void zr_handler(int sig, siginfo_t *info, void *context) {
|
||||
&& !__atomic_exchange_n(&zr_busy, 1, __ATOMIC_SEQ_CST)) {
|
||||
__atomic_store_n(&zr_pending, 0, __ATOMIC_SEQ_CST);
|
||||
int mode = zr_mode();
|
||||
if (mode == 1) zr_ack(0);
|
||||
if (mode >= 0) zr_walk(mode);
|
||||
if (mode == 1) zr_ack(1);
|
||||
__atomic_store_n(&zr_busy, 0, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
errno = saved;
|
||||
@@ -148,8 +169,6 @@ static void zr_child(void) {
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void zr_init(void) {
|
||||
const char *name = program_invocation_short_name;
|
||||
if (strcmp(name, "steam") != 0 && strcmp(name, "steamwebhelper") != 0) return;
|
||||
void *libc = dlopen("libc.so.6", RTLD_LAZY | RTLD_NOLOAD);
|
||||
if (libc == NULL) return;
|
||||
zr_sys = (long (*)(long, ...))dlsym(libc, "syscall");
|
||||
|
||||
Reference in new issue
Block a user