Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals

- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF,
  AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib)
- android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port
  (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals),
  lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art
- touch: shared touch controls moved from ios/Sources to dm/touch
- patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults
- hle_audio: AAudio resume waits through STARTING and reopens once on failure
- .gitignore: local resources/ (original game data) never committed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-14 14:55:05 +09:00
co-authored by Claude Opus 5.5
parent 5bc5a01087
commit 4d91f80bae
106 changed files with 7009 additions and 181 deletions
+296
View File
@@ -0,0 +1,296 @@
// Linux / Android 兼容层实现:compat_linux.h 与 include/ 替身头里声明的函数。
// Linux / Android compatibility layer implementation: the functions declared in compat_linux.h and the stand-in headers under include/.
#include "compat_linux.h"
#include <CommonCrypto/CommonDigest.h>
#include <dirent.h>
#include <dispatch/dispatch.h>
#include <limits.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <sys/xattr.h>
// ---- 读自己进程的内存:process_vm_readv 读自己,不可读返回 EFAULT 而不是崩 ----
// ---- Reading our own memory: process_vm_readv on ourselves returns EFAULT for unreadable ranges instead of faulting ----
kern_return_t vm_read_overwrite(mach_port_t task, vm_address_t addr, vm_size_t size, vm_address_t data, vm_size_t *outsize) {
(void)task;
struct iovec l = {(void *)data, size}, r = {(void *)addr, size};
ssize_t n = process_vm_readv(getpid(), &l, 1, &r, 1, 0);
if (n < 0 && errno == ENOSYS) {
// 退路:写进管道再读出;write 遇不可读源地址返回 EFAULT | Fallback: write into a pipe and read back; write returns EFAULT on an unreadable source
int p[2]; if (pipe(p)) return KERN_FAILURE;
n = size <= 65536 ? write(p[1], (void *)addr, size) : -1;
if (n > 0 && read(p[0], (void *)data, (size_t)n) != n) n = -1;
close(p[0]); close(p[1]);
}
if (n != (ssize_t)size) return KERN_INVALID_ADDRESS;
if (outsize) *outsize = size;
return KERN_SUCCESS;
}
kern_return_t task_info(mach_port_t task, task_flavor_t flavor, task_info_t out, mach_msg_type_number_t *count) {
(void)task;
if (flavor != TASK_VM_INFO || *count < TASK_VM_INFO_COUNT) return KERN_FAILURE;
unsigned long long vsz = 0, rss = 0; FILE *f = fopen("/proc/self/statm", "r");
if (!f) return KERN_FAILURE;
int ok = fscanf(f, "%llu %llu", &vsz, &rss) == 2; fclose(f);
if (!ok) return KERN_FAILURE;
uint64_t pg = (uint64_t)sysconf(_SC_PAGESIZE);
task_vm_info_data_t *vi = (task_vm_info_data_t *)out;
vi->virtual_size = vsz * pg; vi->resident_size = vi->phys_footprint = rss * pg;
return KERN_SUCCESS;
}
// ---- pthread 栈:Darwin 的 stackaddr 是栈顶 ----
// ---- pthread stack: Darwin's stackaddr is the stack top ----
static int stack_of(pthread_t t, void **lo, size_t *sz) {
pthread_attr_t a;
if (pthread_getattr_np(t, &a)) return -1;
int r = pthread_attr_getstack(&a, lo, sz);
pthread_attr_destroy(&a);
return r;
}
void *pthread_get_stackaddr_np(pthread_t t) { void *lo; size_t sz; return stack_of(t, &lo, &sz) ? NULL : (char *)lo + sz; }
size_t pthread_get_stacksize_np(pthread_t t) { void *lo; size_t sz; return stack_of(t, &lo, &sz) ? 0 : sz; }
// ---- 文件 ----
// ---- Files ----
int dm_fcntl_getpath(int fd, char *out) {
char p[64]; snprintf(p, sizeof p, "/proc/self/fd/%d", fd);
ssize_t n = readlink(p, out, PATH_MAX - 1);
if (n < 0) return -1;
out[n] = 0; return 0;
}
// 缓冲一律放堆上:clonefile 由存档挂载(svc 门)调用,跑在客体线程的小栈上
// All buffers live on the heap: clonefile is called by save mounting (an svc gate) and runs on a guest thread's small stack
static int copy_xattrs(int sfd, int dfd) {
char *names = malloc(4096), *v = malloc(4096); int r = 0;
ssize_t n = names && v ? flistxattr(sfd, names, 4096) : -1;
for (char *k = names; n > 0 && k < names + n; k += strlen(k) + 1) {
if (strncmp(k, "user.", 5)) continue;
ssize_t vn = fgetxattr(sfd, k, v, 4096);
if (vn >= 0 && fsetxattr(dfd, k, v, (size_t)vn, 0)) { r = -1; break; }
}
free(names); free(v);
return r;
}
static int copy_file(const char *src, const char *dst, mode_t mode) {
int sfd = open(src, O_RDONLY), dfd = sfd < 0 ? -1 : open(dst, O_WRONLY | O_CREAT | O_EXCL, mode);
char *buf = malloc(65536);
int r = sfd < 0 || dfd < 0 || !buf ? -1 : 0; ssize_t n;
while (!r && (n = read(sfd, buf, 65536)) != 0) {
if (n < 0) { r = -1; break; }
for (ssize_t o = 0; o < n;) { ssize_t w = write(dfd, buf + o, (size_t)(n - o)); if (w < 0) { r = -1; break; } o += w; }
}
if (!r) r = copy_xattrs(sfd, dfd);
free(buf);
if (sfd >= 0) close(sfd);
if (dfd >= 0) close(dfd);
return r;
}
static int copy_tree(const char *src, const char *dst) {
struct stat st; if (lstat(src, &st)) return -1;
if (S_ISREG(st.st_mode)) return copy_file(src, dst, st.st_mode & 07777);
if (!S_ISDIR(st.st_mode)) { errno = ENOTSUP; return -1; }
if (mkdir(dst, st.st_mode & 07777)) return -1;
DIR *d = opendir(src); if (!d) return -1;
char *s2 = NULL, *d2 = NULL; struct dirent *e; int r = 0;
while (!r && (e = readdir(d))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
if (asprintf(&s2, "%s/%s", src, e->d_name) < 0 || asprintf(&d2, "%s/%s", dst, e->d_name) < 0) r = -1;
else r = copy_tree(s2, d2);
free(s2); free(d2); s2 = d2 = NULL;
}
closedir(d);
if (!r) { int sfd = open(src, O_RDONLY | O_DIRECTORY), dfd = open(dst, O_RDONLY | O_DIRECTORY);
if (sfd >= 0 && dfd >= 0) r = copy_xattrs(sfd, dfd);
if (sfd >= 0) close(sfd); if (dfd >= 0) close(dfd); }
return r;
}
int clonefile(const char *src, const char *dst, uint32_t flags) {
(void)flags;
struct stat st; if (!lstat(dst, &st)) { errno = EEXIST; return -1; }
return copy_tree(src, dst);
}
int renamex_np(const char *from, const char *to, unsigned flags) {
if (flags & RENAME_SWAP) return (int)syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, 2 /* RENAME_EXCHANGE */);
return rename(from, to);
}
// ---- 进程映像 ----
// ---- Process image ----
// 安卓 App 里 /proc/self/exe 是 app_process:GameActivity 在加载 libd2r.so 前设好 DM_ROOT(数据根),这里把「可执行文件」报成 <DM_ROOT>/dmhost,
// 与 adb 无头时 dmhost 和 dm/ 同目录的布局一致——库构造函数(dm/derived.tsv 的经验表等)在 chdir 之前就要用它
// In the Android app /proc/self/exe is app_process: GameActivity sets DM_ROOT (the data root) before loading libd2r.so, and the "executable" is reported as <DM_ROOT>/dmhost,
// matching the adb headless layout where dmhost sits next to dm/ -- library constructors (the EXP table from dm/derived.tsv, etc.) need it before the chdir
int _NSGetExecutablePath(char *buf, uint32_t *size) {
char p[PATH_MAX]; ssize_t n;
#ifdef __ANDROID__
const char *root = getenv("DM_ROOT");
if (root && *root) n = snprintf(p, sizeof p, "%s/dmhost", root); else
#endif
n = readlink("/proc/self/exe", p, sizeof p - 1);
if (n < 0 || n >= (ssize_t)sizeof p) return -1;
p[n] = 0;
if ((uint32_t)n + 1 > *size) { *size = (uint32_t)n + 1; return -1; }
memcpy(buf, p, (size_t)n + 1); return 0;
}
const struct mach_header_64 _mh_execute_header = {0xfeedfacf};
// Mach-O "__DATA,<名>" 登记段 → ELF 段 <名>(DM_SECT),链接器给 __start_<名> / __stop_<名>;未用到的段弱引用为空
// Mach-O "__DATA,<name>" registration section -> ELF section <name> (DM_SECT); the linker provides __start_<name> / __stop_<name>; unused sections are weak and empty
#define SECT(n) extern uint8_t __start_##n[] __attribute__((weak)), __stop_##n[] __attribute__((weak));
SECT(swhle) SECT(swforce) SECT(swnvn)
#undef SECT
uint8_t *getsectiondata(const struct mach_header_64 *mh, const char *seg, const char *sect, unsigned long *size) {
(void)mh; (void)seg;
uint8_t *a = NULL, *b = NULL;
if (!strcmp(sect, "swhle")) a = __start_swhle, b = __stop_swhle;
else if (!strcmp(sect, "swforce")) a = __start_swforce, b = __stop_swforce;
else if (!strcmp(sect, "swnvn")) a = __start_swnvn, b = __stop_swnvn;
*size = a && b ? (unsigned long)(b - a) : 0;
return *size ? a : NULL;
}
// ---- 摘要:MD5 / SHA-1 / SHA-256(只做校验与诊断用,不涉安全) ----
// ---- Digests: MD5 / SHA-1 / SHA-256 (only for checksums and diagnostics, not security) ----
#define ROL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
static void md5_block(uint32_t h[4], const uint8_t *p) {
static const uint32_t K[64] = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
static const uint8_t S[16] = {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21};
uint32_t m[16]; for (int i = 0; i < 16; i++) m[i] = p[4 * i] | p[4 * i + 1] << 8 | p[4 * i + 2] << 16 | (uint32_t)p[4 * i + 3] << 24;
uint32_t a = h[0], b = h[1], c = h[2], d = h[3];
for (int i = 0; i < 64; i++) {
uint32_t f; int g;
if (i < 16) f = (b & c) | (~b & d), g = i;
else if (i < 32) f = (d & b) | (~d & c), g = (5 * i + 1) & 15;
else if (i < 48) f = b ^ c ^ d, g = (3 * i + 5) & 15;
else f = c ^ (b | ~d), g = (7 * i) & 15;
uint32_t t = d; d = c; c = b;
b = b + ROL(a + f + K[i] + m[g], S[(i >> 4) * 4 + (i & 3)]);
a = t;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d;
}
// 通用 Merkle–Damgård 外壳:64 字节块,长度尾按 big 端或 little 端
// Generic Merkle-Damgard wrapper: 64-byte blocks, length trailer big- or little-endian
static void md_run(const uint8_t *p, uint64_t len, int big, void (*blk)(uint32_t *, const uint8_t *), uint32_t *h) {
uint64_t full = len & ~63ull;
for (uint64_t o = 0; o < full; o += 64) blk(h, p + o);
uint8_t t[128] = {0}; size_t r = (size_t)(len - full);
memcpy(t, p + full, r); t[r] = 0x80;
size_t tl = r + 9 <= 64 ? 64 : 128; uint64_t bits = len * 8;
for (int i = 0; i < 8; i++) t[tl - 8 + i] = big ? (uint8_t)(bits >> (56 - 8 * i)) : (uint8_t)(bits >> (8 * i));
for (size_t o = 0; o < tl; o += 64) blk(h, t + o);
}
static void md5_blk(uint32_t *h, const uint8_t *p) { md5_block(h, p); }
unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md) {
uint32_t h[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476};
md_run(data, len, 0, md5_blk, h);
for (int i = 0; i < 16; i++) md[i] = (uint8_t)(h[i / 4] >> (8 * (i & 3)));
return md;
}
static void sha1_blk(uint32_t *h, const uint8_t *p) {
uint32_t w[80];
for (int i = 0; i < 16; i++) w[i] = (uint32_t)p[4 * i] << 24 | p[4 * i + 1] << 16 | p[4 * i + 2] << 8 | p[4 * i + 3];
for (int i = 16; i < 80; i++) w[i] = ROL(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
for (int i = 0; i < 80; i++) {
uint32_t f, k;
if (i < 20) f = (b & c) | (~b & d), k = 0x5a827999;
else if (i < 40) f = b ^ c ^ d, k = 0x6ed9eba1;
else if (i < 60) f = (b & c) | (b & d) | (c & d), k = 0x8f1bbcdc;
else f = b ^ c ^ d, k = 0xca62c1d6;
uint32_t t = ROL(a, 5) + f + e + k + w[i]; e = d; d = c; c = ROL(b, 30); b = a; a = t;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
}
unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md) {
uint32_t h[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
md_run(data, len, 1, sha1_blk, h);
for (int i = 0; i < 20; i++) md[i] = (uint8_t)(h[i / 4] >> (24 - 8 * (i & 3)));
return md;
}
static void sha256_blk(uint32_t *h, const uint8_t *p) {
static const uint32_t K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
uint32_t w[64];
for (int i = 0; i < 16; i++) w[i] = (uint32_t)p[4 * i] << 24 | p[4 * i + 1] << 16 | p[4 * i + 2] << 8 | p[4 * i + 3];
for (int i = 16; i < 64; i++) {
uint32_t s0 = ROR(w[i - 15], 7) ^ ROR(w[i - 15], 18) ^ (w[i - 15] >> 3), s1 = ROR(w[i - 2], 17) ^ ROR(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], hh = h[7];
for (int i = 0; i < 64; i++) {
uint32_t t1 = hh + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i];
uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
hh = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; h[5] += f; h[6] += g; h[7] += hh;
}
int CC_SHA256_Init(CC_SHA256_CTX *c) {
static const uint32_t H0[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
memcpy(c->h, H0, sizeof H0); c->len = 0; c->n = 0; return 1;
}
int CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len) {
const uint8_t *p = data; c->len += len;
while (len) {
if (!c->n && len >= 64) { sha256_blk(c->h, p); p += 64; len -= 64; continue; }
uint32_t k = 64 - c->n < len ? 64 - c->n : len;
memcpy(c->buf + c->n, p, k); c->n += k; p += k; len -= k;
if (c->n == 64) { sha256_blk(c->h, c->buf); c->n = 0; }
}
return 1;
}
int CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c) {
uint8_t t[128] = {0}; memcpy(t, c->buf, c->n); t[c->n] = 0x80;
size_t tl = c->n + 9 <= 64 ? 64 : 128; uint64_t bits = c->len * 8;
for (int i = 0; i < 8; i++) t[tl - 8 + i] = (uint8_t)(bits >> (56 - 8 * i));
for (size_t o = 0; o < tl; o += 64) sha256_blk(c->h, t + o);
for (int i = 0; i < 32; i++) md[i] = (uint8_t)(c->h[i / 4] >> (24 - 8 * (i & 3)));
return 1;
}
// ---- dispatch 计数信号量 ----
// ---- dispatch counting semaphore ----
struct dm_dsema { sem_t s; };
dispatch_semaphore_t dispatch_semaphore_create(long value) {
dispatch_semaphore_t d = malloc(sizeof *d);
if (d && sem_init(&d->s, 0, (unsigned)value)) { free(d); d = NULL; }
return d;
}
long dispatch_semaphore_signal(dispatch_semaphore_t d) { sem_post(&d->s); return 0; }
long dispatch_semaphore_wait(dispatch_semaphore_t d, dispatch_time_t timeout) {
if (timeout == DISPATCH_TIME_FOREVER) { while (sem_wait(&d->s) && errno == EINTR) {} return 0; }
if (timeout == DISPATCH_TIME_NOW) return sem_trywait(&d->s) ? 1 : 0;
struct timespec ts = {(time_t)(timeout / 1000000000ull), (long)(timeout % 1000000000ull)}; // 绝对 CLOCK_REALTIME 纳秒 | absolute CLOCK_REALTIME ns
while (sem_timedwait(&d->s, &ts)) if (errno != EINTR) return 1;
return 0;
}
void dispatch_release(void *obj) { struct dm_dsema *d = obj; if (d) { sem_destroy(&d->s); free(d); } }
uint64_t mach_absolute_time(void) { return clock_gettime_nsec_np(CLOCK_MONOTONIC); }
+150
View File
@@ -0,0 +1,150 @@
// Linux / Android 兼容层:宿主源码按 Darwin 写,在 Linux 上用 -include 强制先包含本头,
// 再由 compat/linux/include 下的同名替身头(mach/mach.h、CommonCrypto/CommonDigest.h…)吃掉 Apple 头的 #include。
// 这里只补「同一语义、换个名字」的接口;语义不同的(线程暂停取寄存器、AudioUnit、GCD 信号源、xattr 参数)在源码里按 __APPLE__ 分支。
// Linux / Android compatibility layer: the host sources are written against Darwin; on Linux this header is force-included first with -include,
// and the same-named stand-in headers under compat/linux/include (mach/mach.h, CommonCrypto/CommonDigest.h, ...) absorb the #include of Apple headers.
// Only "same semantics, different name" interfaces are provided here; the ones with different semantics (suspending a thread to read its registers,
// AudioUnit, GCD signal sources, xattr arguments) branch on __APPLE__ in the sources.
#pragma once
#if !defined(__linux__)
#error "compat_linux.h 只给 Linux / Android 用 | compat_linux.h is for Linux / Android only"
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
// ---- TargetConditionals:Linux 既不是 iPhone 也不是 OSX;运行时可写代码(探针 / 计数桩)这类「非 iPhone」路径照常可用 ----
// ---- TargetConditionals: Linux is neither iPhone nor OSX; "non-iPhone" paths such as runtime-written code (probes / counting stubs) work as usual ----
#define TARGET_OS_IPHONE 0
#define TARGET_OS_IOS 0
#define TARGET_OS_OSX 0
#define TARGET_OS_MAC 0
#define TARGET_OS_SIMULATOR 0
#define TARGET_OS_MACCATALYST 0
#define DM_LINUX 1
// ---- 时钟:CLOCK_UPTIME_RAW(不含睡眠)= Linux CLOCK_MONOTONIC(同样不含挂起);mach 时基取 1/1 = 纳秒 ----
// ---- Clocks: CLOCK_UPTIME_RAW (excludes sleep) = Linux CLOCK_MONOTONIC (also excludes suspend); the mach time base is 1/1 = nanoseconds ----
#define CLOCK_UPTIME_RAW CLOCK_MONOTONIC
#define CLOCK_UPTIME_RAW_APPROX CLOCK_MONOTONIC_COARSE
#define CLOCK_MONOTONIC_RAW_APPROX CLOCK_MONOTONIC_COARSE
static inline uint64_t clock_gettime_nsec_np(clockid_t c) {
struct timespec ts; if (clock_gettime(c, &ts)) return 0;
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
typedef int kern_return_t;
#define KERN_SUCCESS 0
#define KERN_FAILURE 5
#define KERN_INVALID_ADDRESS 1
typedef struct { uint32_t numer, denom; } mach_timebase_info_data_t;
typedef mach_timebase_info_data_t *mach_timebase_info_t;
static inline kern_return_t mach_timebase_info(mach_timebase_info_t t) { t->numer = t->denom = 1; return KERN_SUCCESS; }
// 真符号:nvn_entry.S 直接 bl mach_absolute_time | a real symbol: nvn_entry.S calls mach_absolute_time directly
uint64_t mach_absolute_time(void);
static inline uint64_t mach_continuous_time(void) { return clock_gettime_nsec_np(CLOCK_BOOTTIME); }
static inline kern_return_t mach_wait_until(uint64_t deadline) {
struct timespec ts = {(time_t)(deadline / 1000000000ull), (long)(deadline % 1000000000ull)};
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR) {}
return KERN_SUCCESS;
}
// ---- 任务 / 内存:只有「读自己进程一段可能不可读的内存」这一种用法(诊断),用 process_vm_readv 读自己,缺页返回错误而不是崩 ----
// ---- Task / memory: the only use is "read a possibly unreadable range of our own process" (diagnostics); process_vm_readv on ourselves returns an error instead of faulting ----
typedef int mach_port_t;
typedef mach_port_t task_t, thread_t, thread_act_t;
typedef unsigned mach_msg_type_number_t;
typedef uintptr_t vm_address_t;
typedef size_t vm_size_t;
typedef int vm_prot_t;
static inline mach_port_t mach_task_self(void) { return 0; }
static inline mach_port_t mach_thread_self(void) { return (mach_port_t)gettid(); }
static inline mach_port_t pthread_mach_thread_np(pthread_t t) { return (mach_port_t)pthread_gettid_np(t); }
static inline kern_return_t mach_port_deallocate(mach_port_t task, mach_port_t p) { (void)task; (void)p; return KERN_SUCCESS; }
// task_info(TASK_VM_INFO):只给 phys_footprint(取 /proc/self/statm 的常驻页) | task_info(TASK_VM_INFO): only phys_footprint (resident pages from /proc/self/statm)
typedef int task_flavor_t;
typedef int *task_info_t;
#define TASK_VM_INFO 22
typedef struct { uint64_t virtual_size, resident_size, phys_footprint; } task_vm_info_data_t;
#define TASK_VM_INFO_COUNT ((mach_msg_type_number_t)(sizeof(task_vm_info_data_t) / sizeof(int)))
kern_return_t task_info(mach_port_t task, task_flavor_t flavor, task_info_t out, mach_msg_type_number_t *count);
kern_return_t vm_read_overwrite(mach_port_t task, vm_address_t addr, vm_size_t size, vm_address_t data, vm_size_t *outsize);
// ---- 指令缓存 ----
// ---- Instruction cache ----
static inline void sys_icache_invalidate(void *p, size_t n) { __builtin___clear_cache((char *)p, (char *)p + n); }
static inline void sys_dcache_flush(void *p, size_t n) { (void)p; (void)n; }
// ---- QoS:Linux 没有,按「不设」处理;查询返回 UNSPECIFIED ----
// ---- QoS: Linux has none; treated as "not set", queries return UNSPECIFIED ----
typedef enum {
QOS_CLASS_USER_INTERACTIVE = 0x21, QOS_CLASS_USER_INITIATED = 0x19, QOS_CLASS_DEFAULT = 0x15,
QOS_CLASS_UTILITY = 0x11, QOS_CLASS_BACKGROUND = 0x09, QOS_CLASS_UNSPECIFIED = 0x00,
} qos_class_t;
static inline int pthread_set_qos_class_self_np(qos_class_t q, int rel) { (void)q; (void)rel; return 0; }
static inline int pthread_get_qos_class_np(pthread_t t, qos_class_t *q, int *rel) { (void)t; if (q) *q = QOS_CLASS_UNSPECIFIED; if (rel) *rel = 0; return 0; }
static inline qos_class_t qos_class_self(void) { return QOS_CLASS_UNSPECIFIED; }
// ---- pthread 的 Darwin 扩展 ----
// ---- Darwin extensions of pthread ----
// Darwin pthread_setname_np 只给当前线程起名
// Darwin pthread_setname_np only names the current thread
#define pthread_setname_np(n) pthread_setname_np(pthread_self(), (n))
// Darwin 的 stackaddr 是栈顶(高地址)
// Darwin's stackaddr is the stack top (high address)
void *pthread_get_stackaddr_np(pthread_t t);
size_t pthread_get_stacksize_np(pthread_t t);
static inline int pthread_threadid_np(pthread_t t, uint64_t *id) { *id = (uint64_t)pthread_gettid_np(t ? t : pthread_self()); return 0; }
static inline int pthread_main_np(void) { return getpid() == gettid(); }
// ---- 文件 ----
// ---- Files ----
#define st_mtimespec st_mtim
#define st_atimespec st_atim
#define st_ctimespec st_ctim
static inline int getdtablesize(void) { return (int)sysconf(_SC_OPEN_MAX); }
#define F_GETPATH 0x7fff0032 // 由 dm_fcntl_getpath 处理(fcntl 不认识这个号) | handled by dm_fcntl_getpath (fcntl does not know this number)
#ifndef ENOATTR
#define ENOATTR ENODATA
#endif
int dm_fcntl_getpath(int fd, char *out);
// 整树复制(含 user.* 扩展属性);Linux 没有写时复制克隆,存档目录小,复制可接受
// Whole-tree copy (including user.* extended attributes); Linux has no copy-on-write clone, save directories are small so copying is acceptable
int clonefile(const char *src, const char *dst, uint32_t flags);
#define RENAME_SWAP 2u
int renamex_np(const char *from, const char *to, unsigned flags);
// ---- 进程映像:可执行文件路径;Mach-O 段登记表的 ELF 对应(__start_<段> / __stop_<段>) ----
// ---- Process image: executable path; the ELF counterpart of the Mach-O registration sections (__start_<sect> / __stop_<sect>) ----
int _NSGetExecutablePath(char *buf, uint32_t *size);
struct mach_header_64 { uint32_t magic; };
extern const struct mach_header_64 _mh_execute_header;
uint8_t *getsectiondata(const struct mach_header_64 *mh, const char *seg, const char *sect, unsigned long *size);
// ---- 随机数:bionic 自带 arc4random;glibc 2.36 起也有 ----
// ---- Random numbers: bionic ships arc4random; glibc has it since 2.36 ----
#include <stdlib.h>
#include <stdio.h>
#include <sys/ucontext.h>
// ---- 信号上下文:ESR 在 __reserved 的 esr_context 里(magic 0x45535201) ----
// ---- Signal context: ESR lives in the esr_context inside __reserved (magic 0x45535201) ----
static inline uint64_t dm_uc_esr(const ucontext_t *uc) {
const uint8_t *p = (const uint8_t *)uc->uc_mcontext.__reserved, *end = p + sizeof uc->uc_mcontext.__reserved;
while (p + 8 <= end) {
uint32_t magic, size; memcpy(&magic, p, 4); memcpy(&size, p + 4, 4);
if (!magic || size < 8) break;
if (magic == 0x45535201u) { uint64_t esr; memcpy(&esr, p + 8, 8); return esr; }
p += size;
}
return 0;
}
@@ -0,0 +1,15 @@
// Linux 替身:CommonCrypto/CommonDigest.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: CommonCrypto/CommonDigest.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
#include <stdint.h>
typedef uint32_t CC_LONG;
#define CC_MD5_DIGEST_LENGTH 16
#define CC_SHA1_DIGEST_LENGTH 20
#define CC_SHA256_DIGEST_LENGTH 32
typedef struct { uint32_t h[8]; uint64_t len; uint8_t buf[64]; uint32_t n; } CC_SHA256_CTX;
unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md);
unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md);
int CC_SHA256_Init(CC_SHA256_CTX *c);
int CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len);
int CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c);
@@ -0,0 +1,3 @@
// Linux 替身:TargetConditionals.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: TargetConditionals.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,15 @@
// Linux 替身:dispatch/dispatch.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: dispatch/dispatch.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
#include <stdint.h>
// 只实现计数信号量(hle_audio 用);队列 / 定时 / 信号源在源码里按 __APPLE__ 分支
// Only the counting semaphore is implemented (used by hle_audio); queues / timers / signal sources branch on __APPLE__ in the sources
typedef struct dm_dsema *dispatch_semaphore_t;
typedef uint64_t dispatch_time_t;
#define DISPATCH_TIME_NOW 0ull
#define DISPATCH_TIME_FOREVER (~0ull)
dispatch_semaphore_t dispatch_semaphore_create(long value);
long dispatch_semaphore_signal(dispatch_semaphore_t s);
long dispatch_semaphore_wait(dispatch_semaphore_t s, dispatch_time_t timeout);
void dispatch_release(void *obj);
@@ -0,0 +1,3 @@
// Linux 替身:libkern/OSCacheControl.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: libkern/OSCacheControl.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,4 @@
// Linux 替身:mach-o/dyld.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach-o/dyld.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
#include <dlfcn.h>
@@ -0,0 +1,3 @@
// Linux 替身:mach-o/getsect.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach-o/getsect.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,3 @@
// Linux 替身:mach-o/ldsyms.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach-o/ldsyms.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
+33
View File
@@ -0,0 +1,33 @@
// Linux 替身:mach/mach.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach/mach.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
#include <stdint.h>
// ---- 线程诊断接口:Linux 上一律返回失败,调用方走「拿不到」分支(暂停取寄存器、线程信息、实时策略都只用于诊断 / 调优) ----
// ---- Thread diagnostic interfaces: always fail on Linux, callers take their "unavailable" branch (suspend-and-read-registers, thread info and real-time policy are only for diagnostics / tuning) ----
typedef int boolean_t;
typedef int *thread_info_t, *thread_state_t, *thread_policy_t;
typedef int thread_flavor_t, thread_state_flavor_t, thread_policy_flavor_t;
typedef mach_port_t *thread_act_array_t;
typedef struct { int seconds, microseconds; } time_value_t;
typedef struct { time_value_t user_time, system_time; int cpu_usage, policy, run_state, flags, suspend_count, sleep_time; } thread_basic_info_data_t;
typedef struct { uint64_t pth_user_time, pth_system_time; int32_t pth_cpu_usage, pth_policy, pth_run_state, pth_flags, pth_sleep_time, pth_curpri, pth_priority, pth_maxpriority; char pth_name[64]; } thread_extended_info_data_t;
typedef struct { uint64_t __x[29], __fp, __lr, __sp, __pc; uint32_t __cpsr, __pad; } arm_thread_state64_t;
#define THREAD_BASIC_INFO 3
#define THREAD_BASIC_INFO_COUNT ((mach_msg_type_number_t)(sizeof(thread_basic_info_data_t) / sizeof(int)))
#define THREAD_EXTENDED_INFO 5
#define THREAD_EXTENDED_INFO_COUNT ((mach_msg_type_number_t)(sizeof(thread_extended_info_data_t) / sizeof(int)))
#define ARM_THREAD_STATE64 6
#define ARM_THREAD_STATE64_COUNT ((mach_msg_type_number_t)(sizeof(arm_thread_state64_t) / sizeof(int)))
#define __darwin_arm_thread_state64_get_pc(s) ((s).__pc)
#define __darwin_arm_thread_state64_get_lr(s) ((s).__lr)
#define __darwin_arm_thread_state64_get_fp(s) ((s).__fp)
#define __darwin_arm_thread_state64_get_sp(s) ((s).__sp)
static inline kern_return_t thread_info(mach_port_t t, thread_flavor_t f, thread_info_t o, mach_msg_type_number_t *n) { (void)t; (void)f; (void)o; (void)n; return KERN_FAILURE; }
static inline kern_return_t thread_suspend(mach_port_t t) { (void)t; return KERN_FAILURE; }
static inline kern_return_t thread_resume(mach_port_t t) { (void)t; return KERN_FAILURE; }
static inline kern_return_t thread_get_state(mach_port_t t, thread_state_flavor_t f, thread_state_t s, mach_msg_type_number_t *n) { (void)t; (void)f; (void)s; (void)n; return KERN_FAILURE; }
static inline kern_return_t task_threads(mach_port_t task, thread_act_array_t *a, mach_msg_type_number_t *n) { (void)task; *a = 0; *n = 0; return KERN_FAILURE; }
static inline kern_return_t vm_deallocate(mach_port_t task, vm_address_t a, vm_size_t n) { (void)task; (void)a; (void)n; return KERN_SUCCESS; }
// libSystem 私有:本线程 {指令, 周期};Linux 版给 0(可改用 perf_event_open) | libSystem private: this thread's {instructions, cycles}; Linux gives 0 (perf_event_open could be used instead)
static inline int thread_selfcounts(int type, void *buf, size_t nbytes) { (void)type; memset(buf, 0, nbytes); return -1; }
@@ -0,0 +1,3 @@
// Linux 替身:mach/mach_time.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach/mach_time.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,3 @@
// Linux 替身:mach/thread_info.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach/thread_info.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,15 @@
// Linux 替身:mach/thread_policy.h —— 实时线程策略在 Linux 上一律失败(调用方只记日志)
// Linux stand-in: mach/thread_policy.h -- real-time thread policy always fails on Linux (callers only log)
#pragma once
#include <mach/mach.h>
typedef struct { uint32_t period, computation, constraint; boolean_t preemptible; } thread_time_constraint_policy_data_t;
#define THREAD_TIME_CONSTRAINT_POLICY 2
#define THREAD_TIME_CONSTRAINT_POLICY_COUNT ((mach_msg_type_number_t)(sizeof(thread_time_constraint_policy_data_t) / sizeof(int)))
typedef struct { int importance; } thread_precedence_policy_data_t;
#define THREAD_PRECEDENCE_POLICY 3
#define THREAD_PRECEDENCE_POLICY_COUNT 1
typedef struct { boolean_t timeshare; } thread_extended_policy_data_t;
#define THREAD_EXTENDED_POLICY 1
#define THREAD_EXTENDED_POLICY_COUNT 1
static inline kern_return_t thread_policy_set(mach_port_t t, thread_policy_flavor_t f, thread_policy_t p, mach_msg_type_number_t n) { (void)t; (void)f; (void)p; (void)n; return KERN_FAILURE; }
static inline kern_return_t thread_policy_get(mach_port_t t, thread_policy_flavor_t f, thread_policy_t p, mach_msg_type_number_t *n, boolean_t *def) { (void)t; (void)f; (void)p; (void)n; if (def) *def = 1; return KERN_FAILURE; }
@@ -0,0 +1,3 @@
// Linux 替身:mach/vm_statistics.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: mach/vm_statistics.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,10 @@
// Linux 替身:malloc/malloc.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: malloc/malloc.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
#include <malloc.h>
typedef struct { unsigned blocks_in_use; size_t size_in_use, max_size_in_use, size_allocated; } malloc_statistics_t;
// 只填 size_in_use(mallinfo 的 uordblks);zone 参数忽略 | Only size_in_use is filled (mallinfo uordblks); the zone argument is ignored
static inline void malloc_zone_statistics(void *zone, malloc_statistics_t *s) {
(void)zone; struct mallinfo mi = mallinfo();
memset(s, 0, sizeof *s); s->size_in_use = s->size_allocated = (size_t)mi.uordblks;
}
@@ -0,0 +1,3 @@
// Linux 替身:pthread/qos.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: pthread/qos.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once
@@ -0,0 +1,3 @@
// Linux 替身:sys/clonefile.h —— 实际定义见 compat_linux.h(-include 强制先含)
// Linux stand-in: sys/clonefile.h -- the actual definitions are in compat_linux.h (force-included with -include)
#pragma once