Repository navigation
Expand file tree
/
Copy pathge_hooks.cpp
More file actions
2212 lines (2068 loc) · 111 KB
/
Copy pathge_hooks.cpp
File metadata and controls
2212 lines (2068 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ge - project mid-ASM hooks: 0x830E0xxx fragment reconstruction.
#include <thread>
#include <algorithm> // std::min/std::max (ge_mouse_camera aim/sway clamps)
#include <cmath> // sqrtf (ge_mouse_camera crosshair distance)
//
// 8 functions branch to 0x830E0xxx, ZERO in the static XEX (rexglue codegen
// stubs it) but at runtime real PPC code (identical to fragments IDA
// mis-coalesced into sub_821A9720). codegen prunes the code after the
// unconditional `b 0x830E0xxx`, so each continuation point is declared as its
// own ge_cont_* function. Each [[midasm_hook]] (return = true) replicates the
// fragment's register/memory effect, tail-invokes the continuation function,
// and the recompiled source function then returns.
#include <atomic>
#include <cstdint>
#include <cstring>
#include "ge_init.h" // PPCRegister/PPCContext + generated function decls
#include "ge_fps.h" // ge::FpsOnFrame (guest-FPS benchmark recorder)
#include "ge_gamestate.h" // ge::gamestate::OnFrame (game-state bridge pump)
#include "ge_dualscreen.h" // ge::DualScreen (second-screen weapon menu pacing)
#include <rex/cvar.h> // REXCVAR_DEFINE_BOOL / REXCVAR_GET (ge_freeze_diag)
#include <rex/perf/counter.h> // rex::perf frame-stage counters (GESPIKE)
#include <rex/hook.h> // ThreadState, kernel_state, memory
#include <rex/runtime.h>
#include <rex/system/fault_diag.h> // write-watch fault diagnostics (GEWATCHDOG dump)
#include <rex/system/xmemory.h>
#include <rex/graphics/graphics_system.h>
#include <rex/graphics/command_processor.h>
#include <rex/system/xthread.h>
#include <rex/system/kernel_state.h>
#include <rex/ui/keybinds.h> // ParseVirtualKey (keyboard rebinding)
#include <rex/ui/virtual_key.h>
#include <rex/ui/window.h> // mouse capture / cursor / warp (cross-platform)
#include <rex/ui/window_listener.h>
#include <rex/ui/ui_event.h>
#include <cstdio>
#include <mutex>
#include <string>
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <shellapi.h> // ShellExecuteW (WIN32_LEAN_AND_MEAN excludes it)
#endif
// Freeze watchdog + per-stall pipeline diagnostics. These were always-on while
// the frame-pacing freeze was being diagnosed: a 250ms-polling watchdog thread
// that, on a stall, walks every guest thread's stack (probing page readability
// via /dev/null on POSIX) and emits heavy logs, plus a per-present heartbeat.
// They cost CPU every frame and have no place in a shipping build, so they are
// gated behind this debug cvar (default OFF). Flip it on to investigate a stall.
REXCVAR_DEFINE_BOOL(ge_freeze_diag, false, "GPU",
"GoldenEye: enable the freeze watchdog + per-stall pipeline diagnostics "
"(heavy logging + guest-thread stack walks; OFF in shipping builds)")
.lifecycle(rex::cvar::Lifecycle::kHotReload);
namespace ge {
// Relaunch this same executable as a fresh, detached process. Used by the ONLINE
// pause-menu tab's "Save & Restart": the new instance reads the just-written
// ge.toml (new username / server / online-enable) at boot, then the caller tears
// the current process down. Launching a second instance of a running exe is fine
// on Windows -- the image file is opened share-read.
#if defined(_WIN32)
void LaunchSelfDetached() {
wchar_t exe_path[MAX_PATH];
DWORD n = GetModuleFileNameW(nullptr, exe_path, MAX_PATH);
if (n == 0 || n >= MAX_PATH) {
return; // can't resolve our own path; skip relaunch (caller still quits)
}
// Start it from the exe's own directory so a normal boot's relative paths hold.
std::wstring full(exe_path, exe_path + n);
size_t slash = full.find_last_of(L"\\/");
std::wstring workdir = (slash == std::wstring::npos) ? std::wstring() : full.substr(0, slash);
ShellExecuteW(nullptr, L"open", exe_path, nullptr,
workdir.empty() ? nullptr : workdir.c_str(), SW_SHOWNORMAL);
}
#else
// Non-Windows (incl. Android): no in-process relaunch. "Save & Restart" still
// writes the new config; the player relaunches the app manually. An Android
// auto-restart would require an Activity recreate via JNI - out of scope here.
void LaunchSelfDetached() {}
#endif
} // namespace ge
#if !defined(_WIN32)
#include <fcntl.h>
#include <unistd.h>
#endif
namespace {
// Returns a pointer p <= end <= p+want such that [p, end) is safely readable,
// or p (empty) if p itself is not. Lets the freeze watchdog walk the guest stack
// without faulting on uncommitted / guard pages. Windows uses VirtualQuery;
// POSIX (incl. Android) probes readability by write()ing each page to /dev/null,
// which returns EFAULT for an unreadable address instead of faulting.
const uint8_t* ge_readable_end(const uint8_t* p, size_t want) {
#if defined(_WIN32)
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery(p, &mbi, sizeof(mbi)) != sizeof(mbi) || mbi.State != MEM_COMMIT ||
(mbi.Protect & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ |
PAGE_EXECUTE_READWRITE)) == 0) {
return p;
}
const uint8_t* rend = static_cast<const uint8_t*>(mbi.BaseAddress) + mbi.RegionSize;
const uint8_t* send = p + want;
return send <= rend ? send : rend;
#else
static int null_fd = ::open("/dev/null", O_WRONLY | O_CLOEXEC);
if (null_fd < 0) {
return p;
}
constexpr uintptr_t kPage = 4096;
const uint8_t* end = p + want;
const uint8_t* readable_to = p;
for (const uint8_t* pg = reinterpret_cast<const uint8_t*>(
reinterpret_cast<uintptr_t>(p) & ~(kPage - 1));
pg < end; pg += kPage) {
if (::write(null_fd, pg, 1) != 1) {
break; // EFAULT: page not readable
}
readable_to = pg + kPage;
}
if (readable_to <= p) {
return p;
}
return readable_to < end ? readable_to : end;
#endif
}
} // namespace
// Probe to read CommandProcessor's protected ring read pointer (legal: a
// derived class may touch protected base members; we only reinterpret an
// existing CP* and call a non-virtual accessor -- no construction, no
// vtable use, layout-compatible single inheritance).
namespace {
struct CPProbe : rex::graphics::CommandProcessor {
uint32_t rpi() const { return read_ptr_index_; }
uint32_t wpi() const { return write_ptr_index_.load(std::memory_order_acquire); }
};
// rexglue CP swap counter sampled at the last guest present (sub_821996F8).
// "GPU finished the just-submitted frame" == counter advanced past this.
std::atomic<uint32_t> g_present_cpcnt{0};
// Guest tick at the last present, for a bounded completion wait.
std::atomic<uint32_t> g_present_tb{0};
// Count of 80ms GPU-wait skip-bit fallback EPISODES in ge_dbg_now (one per stall
// stretch, not per poll). Logged in the GEWATCHDOG STALL dump so a captured
// lock-up shows whether the render-side skip-bit fallback fired.
std::atomic<uint32_t> g_skipbit_fallback_fires{0};
inline rex::graphics::CommandProcessor* ge_cp() {
auto* ks = rex::system::kernel_state();
if (!ks) return nullptr;
auto* rt = ks->emulator();
if (!rt) return nullptr;
auto* igs = rt->graphics_system();
if (!igs) return nullptr;
return static_cast<rex::graphics::GraphicsSystem*>(igs)->command_processor();
}
inline rex::graphics::GraphicsSystem* ge_gs() {
auto* ks = rex::system::kernel_state();
if (!ks) return nullptr;
auto* rt = ks->emulator();
if (!rt) return nullptr;
auto* igs = rt->graphics_system();
if (!igs) return nullptr;
return static_cast<rex::graphics::GraphicsSystem*>(igs);
}
} // namespace
namespace {
inline void getcb(PPCContext*& ctx, uint8_t*& base) {
ctx = rex::runtime::ThreadState::Get()->context();
base = rex::system::kernel_state()->memory()->virtual_membase();
}
inline uint32_t LD32(uint8_t* b, uint32_t ga) {
uint32_t v; std::memcpy(&v, b + ga, 4); return __builtin_bswap32(v);
}
inline uint64_t LD64(uint8_t* b, uint32_t ga) {
uint64_t v; std::memcpy(&v, b + ga, 8); return __builtin_bswap64(v);
}
inline void ST32(uint8_t* b, uint32_t ga, uint32_t val) {
uint32_t v = __builtin_bswap32(val); std::memcpy(b + ga, &v, 4);
}
inline void STF32(uint8_t* b, uint32_t ga, float f) {
uint32_t v; std::memcpy(&v, &f, 4); v = __builtin_bswap32(v);
std::memcpy(b + ga, &v, 4);
}
inline float LDF32(uint8_t* b, uint32_t ga) {
uint32_t v; std::memcpy(&v, b + ga, 4); v = __builtin_bswap32(v);
float f; std::memcpy(&f, &v, 4); return f;
}
inline uint16_t LD16(uint8_t* b, uint32_t ga) {
uint16_t v; std::memcpy(&v, b + ga, 2); return __builtin_bswap16(v);
}
inline void ST16(uint8_t* b, uint32_t ga, uint16_t val) {
uint16_t v = __builtin_bswap16(val); std::memcpy(b + ga, &v, 2);
}
} // namespace
// rexglue CP WAIT_REG_MEM >60ms deadlock-breaker fire count (command_processor.cpp).
// Lets the watchdog report whether the CPU<->GPU fence breaker fired in a lock-up.
extern "C" uint32_t rex_ge_cp_wait_reg_mem_timeouts();
// rexglue CP drain-progress sequence (command_processor.cpp). Stagnant while the
// ring is non-empty = the CP worker isn't getting scheduled (starvation).
extern "C" uint64_t rex_ge_cp_progress_seq();
// Discovery diagnostic toggle, defined in ge_gamestate.cpp. Declared here so
// ge_dbg_weapon_apply (below) can gate its logging without a header dependency.
REXCVAR_DECLARE(bool, ge_gamestate_diag);
// CP-starvation episodes observed by the watchdog (ring non-empty but the CP
// progress seq did not advance across a 250ms watchdog tick). Coarse by design
// -- fine-grained starvation shows up as missing time in the per-frame CP
// counters instead. Read by the GESPIKE log line (ge_fps.cpp).
std::atomic<uint64_t> g_cp_starved_episodes{0};
namespace ge {
uint64_t GetCpStarvedEpisodes() {
return g_cp_starved_episodes.load(std::memory_order_relaxed);
}
} // namespace ge
// ===========================================================================
// Freeze watchdog. Auto-detects the visual freeze (the guest keeps presenting
// -- present# advancing -- but the GPU command ring stops advancing) and logs
// the exact pipeline state ONCE per stall episode, so we can read the mechanism
// off the log instead of capturing a live process. Zero gameplay effect.
// ===========================================================================
namespace {
std::atomic<uint32_t> g_ge_device{0}; // device struct (dev) seen by ge_dbg_now
std::atomic<uint32_t> g_ge_idblk{0}; // id-block (idblk) seen by ge_dbg_now
std::atomic<uint32_t> g_dbgnow_calls{0}; // increments each ge_dbg_now (guest polling sub_82198C28)
// Decode the SDK's write-watch fault diagnostics into ge.log. Called from the
// watchdog thread (normal context - logging is fine here). `full` also dumps
// the event ring; the summary line alone is cheap enough for periodic use.
void ge_dump_fault_diag(bool full) {
auto& d = rex::system::fault_diag();
REXKRNL_INFO(
"GEWWDIAG wwf={} early_abort={} cb_handled={} cb_unhandled={} recovered={} "
"recovery_fatal={} vetoes={} any_watched_false={} rearm={} protect_ro_watched={} "
"shadow_disagree={} failsafe={} consec_max={}",
d.ww_faults_total.load(), d.early_abort_hits.load(), d.cb_handled.load(),
d.cb_unhandled.load(), d.recovered.load(), d.recovery_fatal.load(),
d.unprotect_vetoes.load(), d.any_watched_false.load(), d.rearm_races.load(),
d.protect_ro_watched.load(), d.shadow_disagreements.load(), d.failsafe_fires.load(),
d.consecutive_max.load());
if (!full) return;
static const char* kPathNames[] = {
"none", "early_abort", "cb_handled", "cb_unhandled", "recovered",
"rec_fatal", "rec_lied", "veto", "no_watch", "rearm",
"protect_ro", "failsafe", "shadow_lied"};
// Oldest-first up to the ring size; skip never-written slots.
uint32_t next = d.ring_next.load(std::memory_order_relaxed);
for (uint32_t k = 0; k < rex::system::FaultDiag::kRingSize; ++k) {
const auto& r = d.ring[(next + k) & (rex::system::FaultDiag::kRingSize - 1)];
if (r.path == 0) continue;
const char* name =
r.path < sizeof(kPathNames) / sizeof(kPathNames[0]) ? kPathNames[r.path] : "?";
REXKRNL_INFO(
"GEWWRING seq={} t={}.{:06}s path={} host={:#x} guest={:#010x} pc={:#x} heap={:#010x} "
"gpage={} cur={} state={} detail={}",
r.fault_seq, r.timestamp_ns / 1000000000ull, (r.timestamp_ns % 1000000000ull) / 1000,
name, r.host_addr, r.guest_vaddr, r.pc, r.heap_base, r.guest_page, r.current_protect,
r.page_state, r.detail);
}
}
void ge_watchdog_thread() {
uint8_t* base = rex::system::kernel_state()->memory()->virtual_membase();
uint32_t last_wpi = 0xFFFFFFFFu, last_rpi = 0, last_present = 0, last_submit = 0;
uint32_t present_at_stall_start = 0, dbg_at_stall_start = 0, submit_at_stall_start = 0;
uint32_t stall = 0;
uint64_t last_cp_seq = 0;
bool logged = false;
bool recover_fired = false;
for (;;) {
std::this_thread::sleep_for(std::chrono::milliseconds(250));
auto* cp = ge_cp();
if (!cp) continue;
uint32_t wpi = static_cast<CPProbe*>(cp)->wpi();
uint32_t rpi = static_cast<CPProbe*>(cp)->rpi();
// CP starvation: work queued (ring non-empty) but the CP's drain-progress
// seq didn't move across a whole 250ms tick -> the worker isn't being
// scheduled. One episode per stagnant tick; surfaced in GESPIKE.
uint64_t cp_seq = rex_ge_cp_progress_seq();
if (rpi != wpi && cp_seq == last_cp_seq) {
g_cp_starved_episodes.fetch_add(1, std::memory_order_relaxed);
}
last_cp_seq = cp_seq;
// Audio-emitter pool occupancy (GEEFFPOOL). The 200x32B X3DAudio emitter
// pool at 0x83064E00 (allocator sub_82144058: slot word0 == 0 means free)
// is the suspected "sound drops after minutes of play" culprit: once it
// pins at 200/200, every new positional sound silently fails to spawn --
// and the spawner's unchecked NULL write is what fires GEWWFAILSAFE.
// Sample used slots each tick: WARN on the full transition, note the
// recovery, and heartbeat 1/5s so a session log shows occupancy vs time.
{
static uint32_t pool_prev_used = 0;
static int pool_log_div = 0;
uint32_t pool_used = 0;
for (uint32_t s = 0; s < 200u; ++s) {
if (LD32(base, 0x83064E00u + s * 32u) != 0u) ++pool_used;
}
if (pool_used == 200u && pool_prev_used < 200u) {
REXKRNL_WARN("GEEFFPOOL FULL 200/200 - new positional sounds drop until slots free");
} else if (pool_prev_used == 200u && pool_used < 200u) {
REXKRNL_INFO("GEEFFPOOL recovered {}/200", pool_used);
}
pool_prev_used = pool_used;
if (++pool_log_div >= 20) { // ~5s at the 250ms tick
pool_log_div = 0;
REXKRNL_INFO("GEEFFPOOL used={}/200", pool_used);
}
}
uint32_t present = g_present_cpcnt.load(std::memory_order_relaxed);
uint32_t dbg = g_dbgnow_calls.load(std::memory_order_relaxed);
uint32_t dev = g_ge_device.load(std::memory_order_relaxed);
uint32_t idblk = g_ge_idblk.load(std::memory_order_relaxed);
uint32_t submit = dev ? LD32(base, dev + 16544) : 0;
// TOTAL-FREEZE trigger. The original stall trigger below requires presents
// to stay ALIVE while the ring freezes -- a real 23-minute-session lockup
// (guest wedged in a SIGSEGV/maps-parse storm) stopped present# too and
// the watchdog stayed silent by design. Catch the everything-stopped case:
// the guest was clearly booted (dev known, submit>0) but neither submit
// nor present# moved for ~3s. Log once per episode with the state that
// identified the last one, plus the on-attach recipe.
{
static uint32_t tf_last_submit = 0, tf_last_present = 0, tf_ticks = 0;
static bool tf_logged = false;
if (dev && submit != 0 && submit == tf_last_submit && present == tf_last_present) {
if (++tf_ticks >= 12 && !tf_logged) { // 12 x 250ms = ~3s of nothing
tf_logged = true;
uint32_t presented = LD32(base, dev + 16552);
uint32_t rg = LD32(base, 0x8242043Cu);
REXKRNL_INFO(
"GEWATCHDOG TOTAL-FREEZE: no submit/present progress ~3s | submit={} presented={} "
"present#={} | ring rpi={:#x} wpi={:#x} [{}] | cp_seq={} dbgnow_polls={} | "
"render-gate={} | skipbit-fires={} wrm-timeouts={} starved={}",
submit, presented, present, rpi, wpi, (rpi == wpi ? "DRAINED" : "PENDING"), cp_seq,
dbg, rg, g_skipbit_fallback_fires.load(std::memory_order_relaxed),
rex_ge_cp_wait_reg_mem_timeouts(),
g_cp_starved_episodes.load(std::memory_order_relaxed));
REXKRNL_INFO(
"GEWATCHDOG TOTAL-FREEZE: last-frame stages cpexec={}us cpidle={}us wrm={}us "
"present={}us gwait={}us gpu={}us | if a guest thread is burning CPU, attach with: "
"run-as com.sunjaycy.goldeneye simpleperf record -g -p <pid>",
rex::perf::GetSnapshotCounter(rex::perf::CounterId::kCpExecuteUs),
rex::perf::GetSnapshotCounter(rex::perf::CounterId::kCpIdleUs),
rex::perf::GetSnapshotCounter(rex::perf::CounterId::kCpWaitRegMemUs),
rex::perf::GetSnapshotCounter(rex::perf::CounterId::kPresentBlockUs),
rex::perf::GetSnapshotCounter(rex::perf::CounterId::kGuestGpuWaitUs),
rex::perf::GetSnapshotCounter(rex::perf::CounterId::kGpuFrameUs));
// Full write-watch fault anatomy: if a guest thread is wedged in a
// fault loop, the ring holds the decisions of its last iterations.
ge_dump_fault_diag(/*full=*/true);
}
} else {
tf_ticks = 0;
tf_logged = false;
}
tf_last_submit = submit;
tf_last_present = present;
}
// Write-watch anomaly watcher: the failsafe, unprotect vetoes, shadow
// disagreements and recovery events should all be zero in a healthy
// session. Log (rate-limited) the moment any of them move so the event is
// timestamped against GESPIKE/GESHOWN context even when no freeze follows.
{
auto& wwd = rex::system::fault_diag();
static uint64_t ww_last_failsafe = 0, ww_last_anom = 0, ww_last_recovered = 0;
static uint32_t ww_cooldown = 0;
if (ww_cooldown) --ww_cooldown;
uint64_t failsafe = wwd.failsafe_fires.load(std::memory_order_relaxed);
uint64_t anom = wwd.unprotect_vetoes.load(std::memory_order_relaxed) +
wwd.shadow_disagreements.load(std::memory_order_relaxed) +
wwd.rearm_races.load(std::memory_order_relaxed) +
wwd.protect_ro_watched.load(std::memory_order_relaxed);
uint64_t recov = wwd.recovered.load(std::memory_order_relaxed) +
wwd.recovery_fatal.load(std::memory_order_relaxed);
if (failsafe != ww_last_failsafe) {
REXKRNL_INFO("GEWWFAILSAFE fired (total={}) - a fault-looping page was force-unprotected",
failsafe);
ge_dump_fault_diag(/*full=*/true);
ww_last_failsafe = failsafe;
ww_last_anom = anom;
ww_last_recovered = recov;
} else if ((anom != ww_last_anom || recov != ww_last_recovered) && ww_cooldown == 0) {
ge_dump_fault_diag(/*full=*/false);
ww_cooldown = 20; // at most one summary per ~5s
ww_last_anom = anom;
ww_last_recovered = recov;
}
}
bool present_alive = (present != last_present);
bool ring_moved = (wpi != last_wpi) || (rpi != last_rpi);
if (present_alive && !ring_moved) {
if (stall == 0) {
present_at_stall_start = present;
dbg_at_stall_start = dbg;
submit_at_stall_start = submit;
recover_fired = false;
}
++stall;
// AUTO-RECOVERY for the CPU<->GPU semaphore deadlock. The CP parks in a
// WAIT_REG_MEM polling idblk for ==0 (the semaphore the render writes 0 to
// release the CP). The render is parked waiting on GPU completion ->
// deadlock. Write 0 to release the CP: it drains the buffer, delivers the
// completion, the render resumes. Memory-only, no interrupt (safe).
// Active recovery WRITES guest memory, so it is opt-in via ge_freeze_diag;
// the default-on watchdog only observes + logs.
if (stall >= 2 && dev && idblk && idblk < 0xFFFFFFFEu && REXCVAR_GET(ge_freeze_diag)) {
ST32(base, idblk, 0u); // release the CP's WAIT_REG_MEM semaphore
if (!recover_fired) {
recover_fired = true;
REXKRNL_INFO("GEWATCHDOG RECOVERY: released CP semaphore (idblk={:#x} := 0)", idblk);
}
}
if (stall >= 6 && !logged) { // ~1.5s of present-but-no-ring
logged = true;
uint32_t presented = dev ? LD32(base, dev + 16552) : 0;
uint32_t target = dev ? LD32(base, dev + 10908) : 0;
// idblk+0 is the WAIT_REG_MEM CPU<->GPU semaphore (the render writes 0 to
// RELEASE the CP), NOT a completion counter -- so a value of 0 is the
// normal released state. Report it as "semaphore=" and never infer "GPU
// behind" from it (the old "submit > completed" test compared a frame
// count against this semaphore and so trivially read "GPU BEHIND").
uint32_t semaphore = idblk ? LD32(base, idblk + 0) : 0;
uint32_t skip = dev ? (base[dev + 10941] & 2) : 0;
REXKRNL_INFO(
"GEWATCHDOG STALL: ring rpi={:#x} wpi={:#x} [{}] | present#={} (+{}/stall) | "
"dbgnow_polls={} (+{}/stall) | submit={} presented={} target={} semaphore(idblk+0)={} "
"skipbit={} | dev={:#x} idblk={:#x}",
rpi, wpi, (rpi == wpi ? "DRAINED" : "PENDING"), present, present - present_at_stall_start,
dbg, dbg - dbg_at_stall_start, submit, presented, target, semaphore, skip, dev, idblk);
// Residual render-path stall mechanisms (cumulative fire counts): which one
// tripped tells us whether this lock-up is the CPU<->GPU fence deadlock
// (WAIT_REG_MEM breaker) or the guest GPU-wait giving up (skip-bit fallback).
REXKRNL_INFO(
"GEWATCHDOG -> residual fires: skipbit-fallback(80ms)={} | WAIT_REG_MEM-breaker(60ms)={}",
g_skipbit_fallback_fires.load(std::memory_order_relaxed),
rex_ge_cp_wait_reg_mem_timeouts());
REXKRNL_INFO(
"GEWATCHDOG -> render={} | presenting={} | producer={} | polling={}",
// The real freeze mechanism: the guest latched the skip bit and is
// bypassing the GPU-completion wait, so render is skipped every frame.
(skip ? "SKIP-BIT LATCHED (render skipped every frame)" : "skip-bit clear"),
(submit > presented ? "frames NOT presenting" : "caught up"),
(submit != submit_at_stall_start ? "ALIVE (submitting)" : "STALLED (not submitting)"),
(dbg != dbg_at_stall_start ? "guest spinning in sub_82198C28" : "guest NOT polling"));
// Render gate: frame loop runs render+present only when dword_8242043C&2
// (sub_8209E1C0). Set by sub_8209E1D0(mode): mode 3 at init (enabled),
// mode 1 = bit clear = render skipped every frame = freeze.
uint32_t rg = LD32(base, 0x8242043Cu);
REXKRNL_INFO("GEWATCHDOG -> render-gate dword_8242043C={} -> render+present {}", rg,
(rg & 2u) ? "ENABLED" : "DISABLED (frame loop skips render = FREEZE)");
// Device flags gating the present/submit (a1 = dev). +21516 != 0 => the
// present SKIPS VdSwap (no screen update) and sub_821A4D50 takes its alt
// path; +22280&4 gates the GPU-completion wait; +10941/+10943 = skip bits.
if (dev) {
uint32_t f21516 = LD32(base, dev + 21516u);
uint32_t f22280 = LD32(base, dev + 22280u);
uint32_t f22276 = LD32(base, dev + 22276u);
uint32_t f21604 = LD32(base, dev + 21604u);
uint32_t f21600 = LD32(base, dev + 21600u);
uint32_t b10941 = base[dev + 10941u];
uint32_t b10943 = base[dev + 10943u];
REXKRNL_INFO(
"GEWATCHDOG -> devflags +21516(VdSwap-skip if !=0)={:#x} | +22280&4(gpu-wait)={} | "
"+22276={:#x} | +21604={} +21600={} (ring) | +10941={:#x} +10943={:#x}",
f21516, (f22280 & 4u), f22276, f21604, f21600, b10941, b10943);
uint32_t vbl = LD32(base, dev + 16532u); // ctx[4133] vblank count
uint32_t fr = LD32(base, dev + 16684u); // ctx[4171] fence read idx
uint32_t fw = LD32(base, dev + 16688u); // ctx[4172] fence write idx
REXKRNL_INFO("GEWATCHDOG -> vblank ctx[4133]={} | GPU fences read={} write={} [{}]", vbl,
fr, fw, (fr != fw ? "PENDING -- fences NOT retiring" : "drained"));
}
// Frame counter dword_8308851C is updated each frame AFTER the frame-
// limiter (0x82189e64). Sample it twice: if FROZEN, the main thread never
// exits the frame-limiter (clock/timebase not advancing for it); if it
// ADVANCES, the main thread cycles and the render is skipped after.
uint32_t fc1 = LD32(base, 0x8308851Cu);
uint32_t tb1 = (uint32_t)REX_QUERY_TIMEBASE();
std::this_thread::sleep_for(std::chrono::milliseconds(120));
uint32_t fc2 = LD32(base, 0x8308851Cu);
uint32_t tb2 = (uint32_t)REX_QUERY_TIMEBASE();
REXKRNL_INFO(
"GEWATCHDOG -> frameCounter 0x8308851C {}->{} [{}] | guestTimebase {}->{} [{}]", fc1, fc2,
(fc1 != fc2 ? "ADVANCING (main thread cycles; render skipped after limiter)"
: "FROZEN (main thread STUCK in frame-limiter)"),
tb1, tb2, (tb1 != tb2 ? "advancing" : "FROZEN"));
// Dump every guest thread's jump state -- find WHERE the render workers
// (guest entry 0x821A4A68) are wedged inside sub_821A4750. lr = return
// addr, ctr = next indirect target, lastIndTgt = last REX_CALL_INDIRECT
// target, msr bit 0x8000 = interrupts enabled.
auto* ks2 = rex::system::kernel_state();
if (ks2) {
auto threads = ks2->object_table()->GetObjectsByType<rex::system::XThread>();
for (auto& th : threads) {
if (!th) continue;
auto* ts = th->thread_state();
if (!ts) continue;
auto* c = ts->context();
if (!c) continue;
uint32_t sa = th->creation_params()->start_address;
bool rw = (sa == 0x821A4A68u);
REXKRNL_INFO(
"GEWATCHDOG THREAD start={:#x}{} lr={:#x} ctr={:#x} lastIndTgt={:#x} msr={:#x} | "
"r3={:#x} r11={:#x} r28={:#x} r29={:#x} r30={:#x} r31={:#x}",
sa, rw ? " [RENDER-WORKER]" : "", (uint32_t)c->lr, c->ctr.u32,
c->last_indirect_target, c->msr, c->r3.u32, c->r11.u32, c->r28.u32, c->r29.u32,
c->r30.u32, c->r31.u32);
// Guest stack walk: scan [r1, r1+0x2400) for guest code addresses
// (0x82xxxxxx return addresses) -> the call chain, directly readable.
{
uint32_t sp = c->r1.u32;
if (sp >= 0x10000u && sp < 0xC0000000u) {
uint8_t* hsp = base + sp;
const uint8_t* send = ge_readable_end(hsp, 0x2400u);
if (send > hsp) {
char sbuf[500];
int soff = 0;
sbuf[0] = 0;
for (uint8_t* pp = hsp; pp + 4 <= send && soff < 460; pp += 4) {
uint32_t val;
std::memcpy(&val, pp, 4);
val = __builtin_bswap32(val);
if (val >= 0x82000000u && val < 0x84000000u) {
int n = std::snprintf(sbuf + soff, sizeof(sbuf) - soff, "%x ", val);
if (n > 0) soff += n;
}
}
REXKRNL_INFO("GEWATCHDOG STACK start={:#x} sp={:#x}: {}", sa, sp, sbuf);
}
}
}
if (rw) {
// a1 (worker struct) = r28; event = a1[2] = a1+0x20 (= r29);
// queue = a1[3]: Flink/submit = a1+0x38, Blink/processed = a1+0x3C.
// wait is INFINITE when a1->SignalState(a1+4) != *(v3+368), v3 = *a1.
uint32_t bw = c->r28.u32;
auto sLD = [&](uint32_t ga) -> uint32_t {
return (ga >= 0x1000u && ga < 0x50000000u) ? LD32(base, ga) : 0xDEADBEEFu;
};
uint32_t v3 = sLD(bw);
uint32_t sig = sLD(bw + 4);
uint32_t v3f = sLD(v3 + 368);
uint32_t subq = sLD(bw + 0x38);
uint32_t procq = sLD(bw + 0x3C);
REXKRNL_INFO(
"GEWATCHDOG WORKER a1={:#x} queue Flink/submit={} Blink/proc={} [{}] | "
"SignalState={} v3={:#x} *(v3+368)={} -> wait={}",
bw, subq, procq,
(subq == procq ? "EMPTY (producer stopped feeding)" : "PENDING (LOST WAKEUP!)"),
sig, v3, v3f, (sig != v3f ? "INFINITE" : "30ms-timeout"));
}
}
// Rapid-sample the main game thread (start 0x8235e4a8): it spends most
// time in the frame-limiter, so one snapshot misses the render path.
// Sample lr many times (yielding so it keeps running) -> the set of
// unique guest PCs = its per-frame code path, revealing which render
// subsystem call it reaches/skips.
for (auto& th : threads) {
if (!th) continue;
if (th->creation_params()->start_address != 0x8235E4A8u) continue;
auto* ts = th->thread_state();
if (!ts) continue;
auto* mc = ts->context();
if (!mc) continue;
uint32_t seen[96];
int ns = 0;
for (int it = 0; it < 8000 && ns < 94; it++) {
uint32_t pc = static_cast<uint32_t>(mc->lr);
if (pc >= 0x82000000u && pc < 0x84000000u) {
bool dup = false;
for (int j = 0; j < ns; j++)
if (seen[j] == pc) { dup = true; break; }
if (!dup) seen[ns++] = pc;
}
std::this_thread::yield();
}
char mb[760];
int mo = 0;
mb[0] = 0;
for (int j = 0; j < ns && mo < 720; j++) {
int n = std::snprintf(mb + mo, sizeof(mb) - mo, "%x ", seen[j]);
if (n > 0) mo += n;
}
REXKRNL_INFO("GEWATCHDOG MAINPATH (unique lr x{}): {}", ns, mb);
// Snapshot the main thread's full stack repeatedly with SLEEPS (no
// spinning -> doesn't starve it, it keeps cycling). Log only snapshots
// where it is OUTSIDE the frame-limiter -> in the per-frame render
// path -> the render call chain + the skipped 3D-submit branch.
{
int logged = 0;
for (int snap = 0; snap < 160 && logged < 12; snap++) {
uint32_t pc = static_cast<uint32_t>(mc->lr);
bool in_lim = (pc >= 0x823B3040u && pc <= 0x823B3540u) ||
(pc >= 0x82189DC0u && pc <= 0x82189E14u);
if (!in_lim && pc >= 0x82000000u && pc < 0x84000000u) {
uint32_t sp = mc->r1.u32;
char fb[620];
int fo = std::snprintf(fb, sizeof(fb), "lr=%x | ", pc);
if (sp >= 0x10000u && sp < 0xC0000000u) {
uint8_t* hsp = base + sp;
const uint8_t* send = ge_readable_end(hsp, 0x2800u);
if (send > hsp) {
for (uint8_t* pp = hsp; pp + 4 <= send && fo < 580; pp += 4) {
uint32_t v;
std::memcpy(&v, pp, 4);
v = __builtin_bswap32(v);
if (v >= 0x82000000u && v < 0x84000000u) {
int n = std::snprintf(fb + fo, sizeof(fb) - fo, "%x ", v);
if (n > 0) fo += n;
}
}
}
}
REXKRNL_INFO("GEWATCHDOG FRAMEWORK[{}] {}", logged, fb);
logged++;
}
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
}
break;
}
}
}
} else {
stall = 0;
logged = false;
recover_fired = false;
}
last_wpi = wpi; last_rpi = rpi; last_present = present; last_submit = submit;
}
}
inline void ge_start_watchdog_once() {
// Detection + logging run by DEFAULT (a 250ms-poll detached thread, negligible
// cost) so a rare in-the-wild lock-up is captured in ge.log without needing
// ge_freeze_diag set ahead of time. The active memory-write auto-recovery
// inside the loop stays gated behind ge_freeze_diag, so the default behaviour is
// purely observational.
static std::atomic<bool> started{false};
bool expected = false;
if (started.compare_exchange_strong(expected, true)) {
std::thread(ge_watchdog_thread).detach();
}
}
} // namespace
// NOTE: no frame-limiter / intro-wait hook. The post-intro freeze is a
// SYMPTOM of the rexglue GPU command-processor not consuming the ring (GPU
// hung -> game stops presenting -> guest time stops -> wait never clears).
// Any hook writing that shared time counter corrupts per-frame timing and
// slows the intros without fixing the GPU hang -> net-harmful, removed.
// Intros run at full speed; post-intro hits the rexglue GPU ceiling.
// sub_82198C28 frame-wait reads now = *(r9+0x58) (r9 = *(r13+0x100)), waits
// while (now - last) < 0x1388. That field is a hardware/kernel time the game
// only READS (no guest writer); rexglue never ticks it -> frozen at 0 ->
// infinite spin. Feed it the real guest tick clock (REX_QUERY_TIMEBASE, the
// same ~49.875MHz source mftb uses): write the live value to both the loaded
// register (so this iteration's compare sees it) and the memory field (so
// other readers/sub_8235EAA8 see a consistent advancing clock). (now-last)
// then measures real elapsed ticks exactly like console -> correct pacing.
void ge_dbg_now(PPCRegister& r9, PPCRegister& r30) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
uint32_t t = (uint32_t)REX_QUERY_TIMEBASE();
if (r9.u32) ST32(base, r9.u32 + 0x58, t);
r30.u32 = t;
uint32_t dev = ctx->r29.u32;
uint32_t idblk = ctx->r11.u32;
uint32_t ws = ctx->r31.u32;
// Feed the freeze watchdog (stash device pointers, count polls, start thread).
g_ge_device.store(dev, std::memory_order_relaxed);
g_ge_idblk.store(idblk, std::memory_order_relaxed);
g_dbgnow_calls.fetch_add(1, std::memory_order_relaxed);
ge_start_watchdog_once();
auto* cpp = ge_cp();
uint32_t cpc = cpp ? cpp->counter() : 0;
uint32_t rpi = cpp ? static_cast<CPProbe*>(cpp)->rpi() : 0;
uint32_t wpi = cpp ? static_cast<CPProbe*>(cpp)->wpi() : 0;
if (ws) ST32(base, ws + 12, t);
// Clear the GPU-completion fence once the just-submitted frame is drawn.
// Two race-free conditions:
// (a) CP swap counter advanced past the value sampled at the matching
// present -> the frame's swap executed; OR
// (b) the CP read pointer caught up to the write pointer -> the CP consumed
// every packet the game submitted (this frame's draws+resolve+swap), so
// it is drawn. (b) eliminates the cpc-sampling race (CP finishing before
// ge_diag_vdswap samples g_present_cpcnt) that intermittently left the
// wait spinning forever -> the random menu freeze. The CP advances rptr
// on its own worker thread and always catches up once the game stops
// feeding the ring, so (b) cannot deadlock.
bool drawn = (cpc != g_present_cpcnt.load(std::memory_order_relaxed)) ||
(wpi != 0u && rpi == wpi);
// (c) WATCHDOG. If neither (a) nor (b) has happened for a long stretch
// (~80ms of real wall time), the CP is genuinely stuck on this frame
// (e.g. blocked in a D3D12 op the backend can't complete for this
// title's scene). Force-complete so the guest's GPU-completion spin
// clears instead of deadlocking forever -- and, critically, releases the
// device spinlock other guest threads (sub_821A3A40) are blocked on.
// This turns a permanent freeze into a recoverable hitch. 80ms is far
// above any real frame time, so normal frames still clear via (a)/(b).
// sub_82198C28 checks the skip bit *(device+10941)&2 at its very top and
// returns 0 (proceed) before any fence/timeout logic -- the only GUARANTEED
// way out of the spin. Writing the completion fence does not always release
// it (it resets the routine's own timeout anchor, and only helps when
// submit>=target). So: when the wait has stalled for a real wall-clock
// stretch (~80ms, far above any frame), SET the skip bit so the guest stops
// blocking on a GPU completion the CP cannot deliver -- and, crucially,
// releases the device spinlock the rest of the guest threads are stuck on.
// When the CP is keeping up (drawn via (a)/(b)) CLEAR it again so waits are
// honored and frames stay visible. Net: visible when the GPU keeps up, a
// brief skipped (black) frame during a stall instead of a permanent freeze.
// Do NOT touch the skip bit on the normal/keeping-up path -- the game manages
// *(device+10941)&2 itself (sets it when it intends NOT to block, clears it
// when it wants to wait), and clearing it during early init hangs the boot.
// Only SET it when the wait has genuinely stalled (~80ms, far above any real
// frame): that forces sub_82198C28 to return 0 next iteration so the guest
// stops blocking on a GPU completion the CP cannot deliver -- breaking the
// spinlock cascade / freeze. The game re-clears it on its own next frame, so
// this stays a one-shot "proceed past this stall", not a permanent skip.
static thread_local uint32_t s_wait_start = 0;
static thread_local bool s_waiting = false;
static thread_local bool s_fallback_counted = false;
if (!drawn) {
if (!s_waiting) { s_waiting = true; s_wait_start = t; s_fallback_counted = false; }
else if ((uint32_t)(t - s_wait_start) > 4000000u) { // ~80ms @49.875MHz
drawn = true;
if (dev) base[dev + 10941u] |= 0x02u; // stalled: skip this GPU wait
// Count once per stall episode (this branch re-runs every poll while the
// stall persists); the watchdog reports it as a residual-mechanism signal.
if (!s_fallback_counted) {
s_fallback_counted = true;
g_skipbit_fallback_fires.fetch_add(1, std::memory_order_relaxed);
}
}
// The six GPU-completion waits poll this routine. Yield the core back so the
// rexglue CP worker (the thread that advances the ring read pointer / swap
// counter to satisfy (a)/(b)) can run; we re-check (a)/(b) on the next poll.
//
// This is now unified across arches. It used to be arch-gated: arm64 blocked
// on rex_ge_cp_wait_progress because a tight yield busy-spin from dozens of
// guest threads oversubscribed the few handheld cores and starved the CP
// worker (the fence never advanced -> freeze). But the blocking wait
// re-checks only every ~2ms, so the ~80ms skip-bit fallback above latches and the game
// wedges (render skipped every frame) -- the SAME freeze the desktop bisect
// (commit 9dd0258) pinned on the blocking wait. The real fix is to stop the
// starvation at its source: the CP worker is now created at kAboveNormal
// priority (command_processor.cpp), so it stays scheduled even while guest
// threads yield-spin -- making the proven-good desktop yield path correct on
// arm64 too. Keep the yield; the rexglue handshake API stays defined as a
// cheap escape hatch if the priority boost ever proves insufficient.
if (!drawn) {
std::this_thread::yield();
}
} else {
if (s_waiting) {
// Wait episode over: bill the yield-spin to the per-frame stage
// counters (guest timebase ticks -> us; /50 approximates the 49.875MHz
// timebase within 0.25%). Summed across ALL waiting guest threads, so
// it reads as total thread-time blocked on the GPU, not wall time.
rex::perf::IncrementCounter(rex::perf::CounterId::kGuestGpuWaitUs,
static_cast<int64_t>((uint32_t)(t - s_wait_start) / 50u));
}
s_waiting = false;
}
if (dev && idblk && idblk < 0xFFFFFFFEu && drawn) {
// DO NOT write idblk+0 here. idblk+0 is the CPU<->GPU semaphore the CP polls
// in WAIT_REG_MEM (waits for ==0; the render writes 0 to release the CP).
// Writing a non-zero "completed" value here HELD the semaphore -> the CP
// stalled in WAIT_REG_MEM -> CPU<->GPU deadlock -> the visual freeze. THIS
// self-inflicted write was the freeze. (Confirmed via the WAIT_REG_MEM
// >60ms deadlock-breaker log polling exactly this address.)
ST32(base, dev + 16552, LD32(base, dev + 16544)); // presented := submit
ST32(base, idblk + 60, rpi); // ring RPTR write-back
// Real-render heartbeat. This branch runs ONLY when the guest actually
// consumed a GPU completion (drawn) and advanced presented:=submit -- i.e. a
// frame really reached the screen. Unlike the "GEGPU present#" heartbeat (and
// the CP swap counter), which keep advancing on a FROZEN boot (VdSwap still
// fires, render skipped), this one stops the instant the game wedges. The
// Android boot loader (GoldenEyeActivity.hasStartedPresenting) greps for it so
// a frozen boot is detected and relaunched instead of mistaken for "live".
//
// Count REAL frames, not poll iterations: this branch runs thousands of times
// per frame (the guest polls the GPU wait in a tight loop), so gate on the
// submit counter (dev+16544) actually advancing, then log once per 64 frames.
uint32_t submit = LD32(base, dev + 16544);
// Dedup the frame transition ATOMICALLY. The six GPU-completion waits poll
// this branch from multiple guest threads; a plain read-modify-write let two
// of them observe the same submit advance and both run the body -- a benign
// dup for the heartbeat, but it double-counted the frame in the FPS recorder
// (two OnFrame() calls microseconds apart -> bogus sub-ms frames that wrecked
// the best-frame metric). A CAS makes exactly one caller win each distinct
// submit value, so OnFrame() fires once per real frame.
static std::atomic<uint32_t> s_last_submit{0};
static std::atomic<uint32_t> rendered{0};
uint32_t prev_submit = s_last_submit.load(std::memory_order_relaxed);
if (submit != prev_submit &&
s_last_submit.compare_exchange_strong(prev_submit, submit,
std::memory_order_relaxed)) {
// How many frames the counter really advanced by: a poll can miss an
// intermediate submit value (clear+present pairs, catch-up after a
// hitch), and telling the recorder lets it split the elapsed time into
// per-frame samples instead of booking one doubled "slow" frame. Wrap-
// safe u32 subtract; the recorder clamps to [1,4]. prev_submit==0 means
// first observation -> treat as a single frame.
uint32_t adv = (prev_submit != 0) ? (submit - prev_submit) : 1u;
ge::FpsOnFrame(adv); // feed the FPS benchmark recorder
uint32_t old = rendered.fetch_add(1, std::memory_order_relaxed);
if ((old & 0x3F) == 0) // log #1, #65, #129... (boot loader greps >= 65)
REXKRNL_INFO("GEGPU rendered#{} dev={:#x} submit={}", old + 1, dev, submit);
}
}
}
// ---------------------------------------------------------------------------
// GPU-completion fence (the real fix). Wired at the present path
// sub_821996F8 @ 0x82199948 (right after the kernel VdSwap), r31 = a1 (D3D
// device struct), r30 = v21 (cmd-buffer swap slot).
//
// At each guest present, sample rexglue's CP swap counter. The poll hook
// (ge_dbg_now) then treats the frame as GPU-complete only once the CP's
// counter has moved past this -- i.e. the just-submitted frame was really
// drawn -- so the game blocks for the real render (visible) but no longer.
void ge_diag_vdswap(PPCRegister& r31, PPCRegister& r30) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
(void)r30;
uint32_t a1 = r31.u32;
auto* cpp = ge_cp();
uint32_t cpc = cpp ? cpp->counter() : 0;
g_present_cpcnt.store(cpc, std::memory_order_relaxed);
g_present_tb.store((uint32_t)REX_QUERY_TIMEBASE(), std::memory_order_relaxed);
// Throttled present heartbeat. This is ALSO the boot-success signal the Android
// loader (GoldenEyeActivity.hasStartedPresenting) greps ge.log for ("present#N",
// N >= PRESENT_THRESHOLD); it must NOT be gated behind ge_freeze_diag (default
// off) or the loader never detects a live render loop and retries/spins forever
// even though the guest is presenting. One line per ~64 presents is negligible.
static uint32_t n = 0;
if ((n++ & 0x3F) == 0)
REXKRNL_INFO("GEGPU present#{} dev={:#x} cpcnt={}", n, a1, cpc);
// Pump the game-state bridge once per present (== once per displayed frame):
// snapshot carried weapons / ammo / equipped id for the second-screen weapon
// menu, and apply any pending equip request. Inert until the guest inventory
// layout is confirmed on-device (see ge_gamestate.cpp); safe to call here.
ge::gamestate::OnFrame(ctx, base);
// Pace the second-screen weapon menu: request one UI-thread paint of the
// secondary surface for this frame. Runs on the game thread, so it only posts
// a deferred UI-thread tick (the secondary surface must be touched only there)
// -- and it is a cheap no-op when there is no second screen / the feature is
// off, which is the single-screen fallback. See ge_dualscreen.cpp.
ge::DualScreen::Get().OnGuestPresent();
}
// F3 0x830E0670 (site 0x8209F5F0 sub_8209F5D8 -> ge_cont_8209F5F4)
void ge_hook_830E0670(PPCRegister& r3, PPCRegister& r11, PPCRegister& r28) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
r11.u32 = r3.u32 ^ 0x2Bu;
r28.u32 = 0x82420000u;
base[0x82420239u] = static_cast<uint8_t>(r11.u32 & 0xFFu);
r28.u32 = 0x82420239u;
r11.u32 = 0x82000000u;
ge_cont_8209F5F4(*ctx, base);
}
// F1 0x830E0630: the r30++ (with 3/6 skip) loop-increment fragment. Hooked at
// the branch site 0x820F774C; the config jump_address sends control back to
// 0x820F7750 (cmpwi r30,8 / blt loc_820F768C) IN THE PARENT sub_820F73F8, so the
// whole loop -- including the loop-back to 0x820F768C -- stays in one function
// and resolves. (Routing through a separate ge_cont_820F7750 left that loop-back
// branch cross-function -> REX_FATAL when the loop ran, e.g. at the main menu.)
bool ge_hook_830E0630(PPCRegister& r30) {
r30.u32 = r30.u32 + 1;
if (r30.s32 == 3 || r30.s32 == 6) r30.u32 = r30.u32 + 1;
// 0x820F7750: cmpwi r30,8 ; 0x820F7754: blt loc_820F768C (loop) else exit.
return r30.s32 < 8;
}
// F2 sub_820F7968: r26 0..8 loop. sub_820F7968 = prologue only (codegen sets
// constants + r26=0). ge_f2_driver fires after the last prologue instruction
// (0x820F79EC, after, return) and drives the loop:
// do { body; r26++; if(r26==3||r26==6) r26++; } while (r26 < 8); epilogue
// ge_body_820F79F0 = 0x820F79F0..0x820F7CFC (one iteration). Its skip branch
// (0x820F7A2C: clrlwi r11,r3,24; cmplwi r11,0; beq loc_820F7D00) becomes a
// return from the body via ge_f2_skip (return_on_true).
bool ge_f2_skip(PPCRegister& r3) {
return (r3.u32 & 0xFFu) == 0u; // beq loc_820F7D00 taken when (r3&0xFF)==0
}
void ge_f2_driver(PPCRegister& r26) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
for (;;) {
ge_body_820F79F0(*ctx, base); // 0x820F79F0 one iteration
r26.u32 = r26.u32 + 1; // 0x830E06B0 increment
if (r26.s32 == 3 || r26.s32 == 6) r26.u32 = r26.u32 + 1;
if (r26.s32 >= 8) break; // 0x820F7D08 blt not taken
}
ge_epi_820F7D0C(*ctx, base); // 0x820F7D0C epilogue (ret)
}
// F4 0x830E0200: loop-increment fragment (same shape as F1). Hooked at the
// branch site 0x820C4914; the config jump_address sends control back to
// 0x820C4918 IN THE PARENT sub_820C4630 so the loop-back to 0x820C4858 resolves
// in-function instead of crossing into a ge_cont_820C4918 (-> REX_FATAL).
bool ge_hook_830E0200(PPCRegister& r31, PPCRegister& r29, PPCRegister& r28,
PPCRegister& r11, PPCRegister& r23, PPCRegister& r21) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
if (r31.s32 == 3) { r31.u32 = r31.u32 + 1; r29.u32 = r29.u32 + 4; }
r28.u32 = r11.u32 + r28.u32;
// 0x820C4918: cmpw r31,r23 ; 0x820C491C: ble loc_820C4858 (loop) else exit.
// The loop top 0x820C4858 (lwz r11,-0x684(r21)) is skipped on first entry
// (0x820C4854 b loc_820C485C) -> only the loop-back reaches it, so it is not a
// standalone block. Do its r11 reload here and jump to 0x820C485C (which IS
// reachable / labeled) instead.
if (r31.s32 <= r23.s32) {
r11.u32 = LD32(base, r21.u32 - 0x684u); // 0x820C4858: lwz r11,-0x684(r21)
return true; // -> loc_820C485C (loop body)
}
return false; // -> loc_820C4920 (exit)
}
// F5 0x830E04D0 (site 0x820C7450 sub_820C7390 -> ge_cont_820C7454)
void ge_hook_830E04D0(PPCRegister& r11, PPCRegister& r10, PPCRegister& r9) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
if (r11.s32 == 3) r11.u32 = r11.u32 - 1;
ST32(base, r9.u32 - 0x644u, r10.u32);
ge_cont_820C7454(*ctx, base);
}
// F6 0x830E0560 (site 0x820C742C sub_820C7390 -> ge_cont_820C7430)
void ge_hook_830E0560(PPCRegister& r11, PPCRegister& r10, PPCRegister& r9) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
if (r11.s32 == 3) r11.u32 = r11.u32 + 1;
ST32(base, r9.u32 - 0x644u, r10.u32);
ge_cont_820C7430(*ctx, base);
}
// F7 0x830E0460 (site 0x820A3E50 sub_820A3C20 -> ge_cont_820A3E9C)
void ge_hook_830E0460(PPCRegister& r11, PPCRegister& r4, PPCRegister& r29,
PPCRegister& r7, PPCRegister& r28, PPCRegister& r6,
PPCRegister& r31, PPCRegister& r5, PPCRegister& r27,
PPCRegister& r3) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base);
r4.s64 = static_cast<int64_t>(static_cast<int16_t>(r11.u32 & 0xFFFFu));
r7.u64 = r29.u64;
r6.u32 = LD32(base, r28.u32 + 0x4DE8u);
r5.u64 = r31.u64;
r3.u32 = LD32(base, r27.u32 + 0x4DE0u);
sub_82144920(*ctx, base);
r3.u32 = 0;
ge_cont_820A3E9C(*ctx, base);
}
// F8 0x830E0750 (site 0x820B40E4 sub_820B40C0, returns; code ends in blr)
void ge_hook_830E0750(PPCRegister& r7, PPCRegister& r8, PPCRegister& r11,
PPCRegister& f1) {
PPCContext* ctx; uint8_t* base; getcb(ctx, base); (void)ctx;
uint32_t v32 = LD32(base, r8.u32 - 0x564u);
r7.u32 = v32;
if (v32 != 0) return;
uint64_t v64 = LD64(base, r8.u32 - 0x560u);
r7.u64 = v64;
if (v64 != 0) return;
STF32(base, r11.u32 + 0x1F0u, f1.f32);
uint32_t t = LD32(base, r11.u32 + 0x1E4u);
r8.u32 = t;
ST32(base, r11.u32 + 0x1ECu, t);
r8.u32 = 0;
ST32(base, r11.u32 + 0x200u, 0);
}
// ===========================================================================
// Mouse-look (real 1:1 keyboard/mouse looking, replacing the stick-emulation
// path). We inject raw mouse deltas straight into the guest's per-frame look
// state inside ge_bondview_control (sub_820B99E8), so the mouse drives the same
// heading/pitch the right stick does -- but without the analog deadzone/accel/
// turn-rate cap that makes the built-in MnK stick emulation feel terrible.
//
// The look injection is platform-neutral (pure guest-memory writes). The input