Files
d2r-mobile/dm/host/hle_os.c
T
2026-10-12 16:51:08 +08:00

126 lines
9.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 宿主公共杂项:客体对象旁表、单调时钟、modules 都没定义的 C 库导入。
// (原 nn::os/mem/lmem/C++ ABI 的 136 条 HLE 已删:SDK 自带 nn::os,装载时这些条目一条都没被解析用上。)
// Host common miscellany: guest object side table, monotonic clock, C library imports that none of the modules define.
// (The former 136 HLE entries for nn::os/mem/lmem/C++ ABI have been deleted: the SDK brings its own nn::os, and at load time not one of these entries was resolved and used.)
#include <pthread.h>
#include "hle.h"
#include <mach/mach_time.h>
#include <stdatomic.h>
// ---------- 旁表 ----------
// ---------- Side table ----------
#define OM_SIZE (1u << 18)
static struct { const void *k; void *v; } g_om[OM_SIZE];
static pthread_mutex_t g_om_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned om_h(const void *k) { uint64_t x = (uint64_t)k; x ^= x >> 29; x *= 0xbf58476d1ce4e5b9ull; return (unsigned)(x >> 40) & (OM_SIZE - 1); }
void *objmap_get(const void *k) {
pthread_mutex_lock(&g_om_lock);
void *r = NULL;
for (unsigned i = om_h(k);; i = (i + 1) & (OM_SIZE - 1)) {
if (g_om[i].k == k) { r = g_om[i].v; break; }
if (!g_om[i].k) break;
}
pthread_mutex_unlock(&g_om_lock);
return r;
}
void objmap_put(const void *k, void *v) {
pthread_mutex_lock(&g_om_lock);
for (unsigned i = om_h(k);; i = (i + 1) & (OM_SIZE - 1))
if (g_om[i].k == k || !g_om[i].k || g_om[i].k == (void *)1) { g_om[i].k = k; g_om[i].v = v; break; }
pthread_mutex_unlock(&g_om_lock);
}
void *objmap_del(const void *k) {
pthread_mutex_lock(&g_om_lock);
void *r = NULL;
for (unsigned i = om_h(k);; i = (i + 1) & (OM_SIZE - 1)) {
if (g_om[i].k == k) { r = g_om[i].v; g_om[i].k = (void *)1; g_om[i].v = NULL; break; }
if (!g_om[i].k) break;
}
pthread_mutex_unlock(&g_om_lock);
return r;
}
uint64_t now_ns(void) {
static mach_timebase_info_data_t tb;
if (!tb.denom) mach_timebase_info(&tb);
return mach_absolute_time() * tb.numer / tb.denom;
}
// 客体时钟:客体能读到的系统计数器(mrs cntpct 门、svc GetSystemTick)都经这里。缺省 = now_ns()。
// 冻结(只供测试驱动 DM_TICK_HOLD 用):冻结期间客体时钟不走 ⇒ 游戏不会到期下一拍;解冻后扣掉冻结时长,客体时钟连续不跳
// DM_DETCLOCK=<步长 ns>(回放 / 测试局用;产品局不设 = 墙钟):首次出图起客体时钟改为「每次出图推进固定步长」,不跟墙钟——
// 客户端每帧 dt、服务端拍的补拍快照都由出图次数决定,同一回放两遍局内第 N 帧逐字节可比(画面逐字节证明的地基)。
// 首帧之前仍走墙钟(启动期有按时刻轮询的等待,冻住会卡);切换时从当时的客体时钟值接续,不跳。冻结期间不推进。
// Guest clock: the system counters the guest can read (mrs cntpct gate, svc GetSystemTick) all go through here. Default = now_ns().
// Freeze (only for the test driver DM_TICK_HOLD): while frozen the guest clock does not advance ⇒ the game's next tick never comes due; after unfreezing the frozen duration is subtracted, the guest clock is continuous with no jump
// DM_DETCLOCK=<step ns> (for replay / test runs; product runs do not set it = wall clock): from the first present on the guest clock becomes "advance a fixed step per present", not following the wall clock --
// the client's per-frame dt and the server tick catch-up snapshots are both determined by the present count, so frame N in a run is byte-for-byte comparable between two passes of the same replay (the foundation of byte-for-byte picture proof).
// Before the first frame the wall clock is still used (the boot period has waits that poll by time, freezing would hang them); at the switch it continues from the guest clock value at that time, no jump. It does not advance while frozen.
static _Atomic uint64_t g_gfrozen, g_glag, g_gdet, g_guest_frame; static _Atomic int g_gfz, g_gdet_on; static uint64_t g_gdet_step = 1;
uint64_t dm_guest_frame(void) { return atomic_load(&g_guest_frame); }
static int detclock_cfg(void) {
static int c = -1; if (c < 0) { const char *e = getenv("DM_DETCLOCK"); g_gdet_step = e ? strtoull(e, 0, 0) : 0; c = g_gdet_step > 0; }
return c;
}
static uint64_t wall_guest_ns(void) { return atomic_load(&g_gfz) ? atomic_load(&g_gfrozen) : now_ns() - atomic_load(&g_glag); }
uint64_t guest_ns(void) { return atomic_load(&g_gdet_on) ? atomic_load(&g_gdet) : wall_guest_ns(); }
int dm_detclock_enabled(void) { return detclock_cfg() && atomic_load(&g_gdet_on); }
// 进局对齐:前端 / 选角阶段出图帧数随真实时间变(装载在后台线程按墙钟推进),首帧起累计的客体时钟值因此每局不同——
// 依赖绝对时刻的画面(如生命 / 法力球液面)在同拍仍不同(实测拍 E + 2 帧两遍差约 1.8 万像素,集中在两侧球位)。
// 见到 UI 消息 FrontEndNavigation:ToGame(建局、服务端拍时限初始化之前)即把客体时钟设为固定值 2^52 ns(只许前跳),
// 此后纯按出图推进:拍 E 落在第几帧、那一刻的绝对时刻都只由步长决定。
// Run entry alignment: the number of presented frames in the front end / character select phase varies with real time (loading advances on a background thread by wall clock), so the guest clock value accumulated since the first frame differs every run --
// pictures that depend on absolute time (such as the liquid level of the life / mana orbs) still differ at the same tick (measured: at tick E + 2 frames two passes differ by about 18,000 pixels, concentrated at the two orb positions).
// On seeing the UI message FrontEndNavigation:ToGame (before the run is created and the server tick deadline is initialized) the guest clock is set to the fixed value 2^52 ns (forward jumps only),
// from then on it advances purely by presents: which frame tick E lands on and the absolute time at that moment are determined by the step alone.
#define DET_TOGAME_NS (1ull << 52)
static void det_on_uimsg(const char *cat, const char *act, const char *arg) {
(void)arg; if (strcmp(cat, "FrontEndNavigation") || strcmp(act, "ToGame")) return;
uint64_t cur = atomic_load(&g_gdet);
if (cur >= DET_TOGAME_NS) { LOG("【红】DM_DETCLOCK:进局时客体时钟 %llu ≥ 对齐值 2^52,不回拨(对齐未生效)", (unsigned long long)cur); return; }
atomic_store(&g_gdet, DET_TOGAME_NS);
LOG("DM_DETCLOCK:进局(ToGame)客体时钟对齐到 2^52 ns(前跳 %.3f s)", (DET_TOGAME_NS - cur) / 1e9);
}
void dm_guest_frame_tick(void) {
crash_watch_frame();
atomic_fetch_add(&g_guest_frame, 1);
if (!detclock_cfg()) return;
if (!atomic_load(&g_gdet_on)) {
atomic_store(&g_gdet, wall_guest_ns()); atomic_store(&g_gdet_on, 1);
void uimsg_subscribe(void (*)(const char *, const char *, const char *)); uimsg_subscribe(det_on_uimsg);
LOG("DM_DETCLOCK:首次出图起客体时钟按出图推进,步长 %llu ns;进局时对齐到 2^52 ns", (unsigned long long)g_gdet_step); return;
}
if (!atomic_load(&g_gfz)) { atomic_fetch_add(&g_gdet, g_gdet_step); dm_guest_clock_advanced(); }
}
static void guest_clock_freeze_apply(int on) {
if (on && !atomic_load(&g_gfz)) { atomic_store(&g_gfrozen, now_ns() - atomic_load(&g_glag)); atomic_store(&g_gfz, 1); }
else if (!on && atomic_load(&g_gfz)) { atomic_store(&g_glag, now_ns() - atomic_load(&g_gfrozen)); atomic_store(&g_gfz, 0); }
}
// 冻结有几个互不相干的原因(0 测试用的冻结拍 / 读取画面,1 App 在后台,2 系统键盘弹着):任一成立就冻结,全部解除才放行——
// 否则「键盘弹着时切后台再回来」会被回前台那次解冻提前放开
// Freezing has several unrelated reasons (0 freeze tick for testing / loading screen, 1 App in background, 2 system keyboard up): freeze if any holds, release only when all are lifted --
// otherwise "switch to background and back while the keyboard is up" would be released early by the unfreeze on returning to foreground
static pthread_mutex_t g_gfz_mu = PTHREAD_MUTEX_INITIALIZER; static unsigned g_gfz_reasons;
void guest_clock_freeze_for(int reason, int on) {
pthread_mutex_lock(&g_gfz_mu);
if (on) g_gfz_reasons |= 1u << reason; else g_gfz_reasons &= ~(1u << reason);
guest_clock_freeze_apply(g_gfz_reasons != 0);
pthread_mutex_unlock(&g_gfz_mu);
}
void guest_clock_freeze(int on) { guest_clock_freeze_for(0, on); }
// 熵源:缺省真随机(arc4random);DM_DETCLOCK 开时改为固定种子的 splitmix64 流(回放画面逐字节可比用,营地蓝火粒子两遍不同)。
// 两处出口(libc getentropy、svc GetInfo RandomEntropy)共用一条流;调用次数在日志里报(首次与每 1000 次)
// Entropy source: true random by default (arc4random); with DM_DETCLOCK on it becomes a fixed-seed splitmix64 stream (so that replay pictures are byte-for-byte comparable, the camp blue fire particles differed between two passes).
// The two outlets (libc getentropy, svc GetInfo RandomEntropy) share one stream; the call count is reported in the log (first call and every 1000 calls)
static _Atomic uint64_t g_detrng = 0x5357444554524e47ull; static _Atomic uint64_t g_detrng_n;
uint64_t dm_entropy64(void) {
if (!detclock_cfg()) return ((uint64_t)arc4random() << 32) | arc4random();
uint64_t z = atomic_fetch_add(&g_detrng, 0x9E3779B97F4A7C15ull) + 0x9E3779B97F4A7C15ull;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; z ^= z >> 31;
uint64_t n = atomic_fetch_add(&g_detrng_n, 1) + 1; if (n == 1 || n % 1000 == 0) LOG("DM_DETCLOCK:确定熵第 %llu 次", (unsigned long long)n);
return z;
}
HLE("getentropy", int, g_getentropy, void *p, size_t n) {
if (!detclock_cfg()) { arc4random_buf(p, n); return 0; }
for (size_t i = 0; i < n; i += 8) { uint64_t v = dm_entropy64(); memcpy((uint8_t *)p + i, &v, n - i < 8 ? n - i : 8); }
return 0;
}