Repository navigation
Expand file tree
/
Copy pathkernel_bridge.c
More file actions
3590 lines (3235 loc) · 136 KB
/
Copy pathkernel_bridge.c
File metadata and controls
3590 lines (3235 loc) · 136 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
/**
* kernel_bridge.c - Bridge between translated game code and kernel functions
*
* Problem:
* Translated game code calls kernel functions via indirect calls through
* the kernel thunk table at VA 0x0036B7C0. In the XBE file, these entries
* contain unresolved ordinals (0x80000000 | ordinal). On real Xbox hardware,
* the kernel loader replaces these with actual function pointers before the
* game runs.
*
* Solution:
* 1. After xbox_MemoryLayoutInit copies .rdata, call xbox_kernel_bridge_init()
* 2. Replace each ordinal entry in Xbox memory with a synthetic VA
* 3. When RECOMP_ICALL encounters a synthetic VA, route it to a per-ordinal
* bridge function that reads args from the simulated Xbox stack, translates
* pointer arguments from Xbox VA->native, and calls the kernel function.
*
* Synthetic VA scheme:
* Each thunk slot i gets VA 0xFE000000 + i*4
* The lookup function checks this range and dispatches appropriately.
*
* Why per-ordinal bridges instead of a generic trampoline:
* Kernel functions receive Xbox pointers (32-bit VAs) that must be translated
* to native pointers by adding g_xbox_mem_offset. Different functions have
* different parameter layouts (pointer vs value), so each needs its own bridge.
*/
#include "kernel.h"
#include "xbox_memory_layout.h"
#include <stdio.h>
#include <float.h>
#include <stdlib.h>
#include <string.h>
/* Access to recompiled code globals */
extern uint32_t g_eax, g_ecx, g_edx, g_esp;
extern uint32_t g_ebx, g_esi, g_edi;
extern uint32_t g_seh_ebp;
extern double g_fp_stack[8];
extern uint32_t g_fp_top;
extern uint16_t g_x87_control_word;
extern uint16_t g_x87_status_word;
extern float g_xmm0[4], g_xmm1[4], g_xmm2[4], g_xmm3[4];
extern float g_xmm4[4], g_xmm5[4], g_xmm6[4], g_xmm7[4];
extern uint64_t g_mm0, g_mm1, g_mm2, g_mm3;
extern uint64_t g_mm4, g_mm5, g_mm6, g_mm7;
extern volatile uint32_t g_recomp_current_func;
extern volatile uint32_t g_recomp_recent_funcs[64];
extern volatile uint32_t g_recomp_recent_func_idx;
extern volatile uint32_t g_recomp_recent_game_funcs[256];
extern volatile uint32_t g_recomp_recent_game_func_idx;
extern volatile uint32_t g_recomp_trace_dump_requested;
extern volatile uint32_t g_recomp_func_trace_armed;
extern ptrdiff_t g_xbox_mem_offset;
/* Dispatch table lookup (for function pointer args) */
typedef void (*recomp_func_t)(void);
recomp_func_t recomp_lookup(uint32_t xbox_va);
recomp_func_t recomp_lookup_manual(uint32_t xbox_va);
recomp_func_t recomp_lookup_kernel(uint32_t xbox_va);
/* Memory access - same as recomp_types.h MEM32 but without the #define guard */
#define BRIDGE_MEM32(addr) (*(volatile uint32_t *)((uintptr_t)(addr) + g_xbox_mem_offset))
#define BRIDGE_MEM16(addr) (*(volatile uint16_t *)((uintptr_t)(addr) + g_xbox_mem_offset))
#define BRIDGE_MEM8(addr) (*(volatile uint8_t *)((uintptr_t)(addr) + g_xbox_mem_offset))
static int bridge_trace_kernel_calls_enabled(void)
{
static int enabled = -1;
if (enabled < 0) {
const char *value = getenv("MERCENARIES_TRACE_KERNEL_CALLS");
enabled = value != NULL && value[0] != '\0' && value[0] != '0';
}
return enabled;
}
static int bridge_trace_interrupts_enabled(void)
{
static int enabled = -1;
if (enabled < 0) {
const char *value = getenv("MERCENARIES_TRACE_INTERRUPTS");
enabled = value != NULL && value[0] != '\0' && value[0] != '0';
}
return enabled;
}
/* Translate Xbox VA to native pointer (NULL-safe: 0 -> NULL) */
#define XBOX_TO_NATIVE(va) ((va) ? (void*)((uintptr_t)(va) + g_xbox_mem_offset) : NULL)
/* NtReadFile completes synchronously on the host, but Xbox ReadFileEx expects
* its guest APC to run only when the thread enters an alertable wait. Keep the
* completed guest IOSB/context tuple until SleepEx reaches KeDelayExecutionThread. */
#define BRIDGE_GUEST_APC_CAPACITY 64u
typedef struct bridge_guest_apc {
uint32_t routine_va;
uint32_t context_va;
uint32_t iostatus_va;
} bridge_guest_apc;
static bridge_guest_apc g_guest_apcs[BRIDGE_GUEST_APC_CAPACITY];
static uint32_t g_guest_apc_head;
static uint32_t g_guest_apc_tail;
static uint32_t g_guest_apc_log_count;
#define BRIDGE_GUEST_INTERRUPT_CAPACITY 16u
typedef struct bridge_guest_interrupt {
uint32_t interrupt_va;
uint32_t service_routine_va;
uint32_t service_context_va;
uint32_t bus_level;
uint32_t vector;
uint32_t irql;
volatile LONG connected;
} bridge_guest_interrupt;
static bridge_guest_interrupt
g_guest_interrupts[BRIDGE_GUEST_INTERRUPT_CAPACITY];
static uint32_t g_guest_interrupt_count;
static uint32_t g_last_interrupt_bus_level;
/* Read directly by the translated-function fast path. Producers still use
* interlocked operations; a plain aligned 32-bit read avoids a bridge call
* when no interrupt is pending. */
volatile int32_t g_kernel_pending_hardware_interrupts;
static int g_delivering_hardware_interrupt;
static uint32_t g_guest_interrupt_log_count;
/* Only the translated CPU thread manipulates this queue. Host interrupt/timer
* producers set their existing pending flags; they never execute guest code.
* Queue insertion must not call a DPC from the middle of an ISR/list update. */
#define BRIDGE_GUEST_DPC_CAPACITY 256u
typedef struct bridge_guest_dpc {
uint32_t dpc_va, argument1, argument2;
} bridge_guest_dpc;
static bridge_guest_dpc g_guest_dpcs[BRIDGE_GUEST_DPC_CAPACITY];
static uint32_t g_guest_dpc_count;
static int g_delivering_guest_dpcs;
volatile int32_t g_kernel_pending_guest_dpcs;
static BOOLEAN bridge_queue_guest_dpc(uint32_t dpc_va, uint32_t argument1,
uint32_t argument2);
static BOOLEAN bridge_remove_guest_dpc(uint32_t dpc_va);
static int bridge_deliver_guest_dpcs(void);
#define BRIDGE_GUEST_TIMER_CAPACITY 64u
typedef struct bridge_guest_timer {
uint32_t timer_va;
uint32_t dpc_va;
int64_t due_time_100ns;
uint32_t period_ms;
int active;
} bridge_guest_timer;
static bridge_guest_timer g_guest_timers[BRIDGE_GUEST_TIMER_CAPACITY];
static int g_delivering_guest_timers;
__declspec(align(8)) static volatile LONG64 g_guest_timer_next_due_100ns;
volatile int32_t g_kernel_pending_guest_timers;
typedef struct bridge_guest_cpu_context {
uint32_t eax, ecx, edx, esp, ebx, esi, edi, seh_ebp;
double fp_stack[8];
uint32_t fp_top;
uint16_t x87_control_word;
uint16_t x87_status_word;
float xmm[8][4];
uint64_t mm[8];
uint32_t current_func;
} bridge_guest_cpu_context;
static void bridge_capture_guest_context(bridge_guest_cpu_context *context)
{
context->eax = g_eax; context->ecx = g_ecx; context->edx = g_edx;
context->esp = g_esp; context->ebx = g_ebx; context->esi = g_esi;
context->edi = g_edi; context->seh_ebp = g_seh_ebp;
memcpy(context->fp_stack, g_fp_stack, sizeof(context->fp_stack));
context->fp_top = g_fp_top;
context->x87_control_word = g_x87_control_word;
context->x87_status_word = g_x87_status_word;
memcpy(context->xmm[0], g_xmm0, sizeof(context->xmm[0]));
memcpy(context->xmm[1], g_xmm1, sizeof(context->xmm[1]));
memcpy(context->xmm[2], g_xmm2, sizeof(context->xmm[2]));
memcpy(context->xmm[3], g_xmm3, sizeof(context->xmm[3]));
memcpy(context->xmm[4], g_xmm4, sizeof(context->xmm[4]));
memcpy(context->xmm[5], g_xmm5, sizeof(context->xmm[5]));
memcpy(context->xmm[6], g_xmm6, sizeof(context->xmm[6]));
memcpy(context->xmm[7], g_xmm7, sizeof(context->xmm[7]));
context->mm[0] = g_mm0; context->mm[1] = g_mm1;
context->mm[2] = g_mm2; context->mm[3] = g_mm3;
context->mm[4] = g_mm4; context->mm[5] = g_mm5;
context->mm[6] = g_mm6; context->mm[7] = g_mm7;
context->current_func = g_recomp_current_func;
}
static void bridge_restore_guest_context(const bridge_guest_cpu_context *context)
{
g_eax = context->eax; g_ecx = context->ecx; g_edx = context->edx;
g_esp = context->esp; g_ebx = context->ebx; g_esi = context->esi;
g_edi = context->edi; g_seh_ebp = context->seh_ebp;
memcpy(g_fp_stack, context->fp_stack, sizeof(context->fp_stack));
g_fp_top = context->fp_top;
g_x87_control_word = context->x87_control_word;
g_x87_status_word = context->x87_status_word;
memcpy(g_xmm0, context->xmm[0], sizeof(context->xmm[0]));
memcpy(g_xmm1, context->xmm[1], sizeof(context->xmm[1]));
memcpy(g_xmm2, context->xmm[2], sizeof(context->xmm[2]));
memcpy(g_xmm3, context->xmm[3], sizeof(context->xmm[3]));
memcpy(g_xmm4, context->xmm[4], sizeof(context->xmm[4]));
memcpy(g_xmm5, context->xmm[5], sizeof(context->xmm[5]));
memcpy(g_xmm6, context->xmm[6], sizeof(context->xmm[6]));
memcpy(g_xmm7, context->xmm[7], sizeof(context->xmm[7]));
g_mm0 = context->mm[0]; g_mm1 = context->mm[1];
g_mm2 = context->mm[2]; g_mm3 = context->mm[3];
g_mm4 = context->mm[4]; g_mm5 = context->mm[5];
g_mm6 = context->mm[6]; g_mm7 = context->mm[7];
g_recomp_current_func = context->current_func;
}
static void bridge_NtUserIoApcDispatcher(void);
static int bridge_queue_guest_apc(uint32_t routine_va, uint32_t context_va,
uint32_t iostatus_va)
{
const uint32_t next =
(g_guest_apc_tail + 1u) % BRIDGE_GUEST_APC_CAPACITY;
if (next == g_guest_apc_head) {
fprintf(stderr, "[GUEST-APC] queue full; dropping routine=%08X\n",
routine_va);
return 0;
}
g_guest_apcs[g_guest_apc_tail].routine_va = routine_va;
g_guest_apcs[g_guest_apc_tail].context_va = context_va;
g_guest_apcs[g_guest_apc_tail].iostatus_va = iostatus_va;
g_guest_apc_tail = next;
if (g_guest_apc_log_count < 12u) {
fprintf(stderr,
"[GUEST-APC] queued routine=%08X context=%08X iosb=%08X\n",
routine_va, context_va, iostatus_va);
++g_guest_apc_log_count;
}
return 1;
}
static int bridge_deliver_guest_apc(void)
{
bridge_guest_apc apc;
bridge_guest_cpu_context saved_context;
recomp_func_t fn;
uint32_t saved_esp;
if (g_guest_apc_head == g_guest_apc_tail)
return 0;
apc = g_guest_apcs[g_guest_apc_head];
g_guest_apc_head =
(g_guest_apc_head + 1u) % BRIDGE_GUEST_APC_CAPACITY;
fn = recomp_lookup_manual(apc.routine_va);
if (!fn) fn = recomp_lookup(apc.routine_va);
if (!fn) fn = recomp_lookup_kernel(apc.routine_va);
if (!fn) {
fprintf(stderr, "[GUEST-APC] unresolved routine=%08X\n", apc.routine_va);
return 1;
}
bridge_capture_guest_context(&saved_context);
saved_esp = saved_context.esp;
g_esp -= 4; BRIDGE_MEM32(g_esp) = 0u; /* Reserved */
g_esp -= 4; BRIDGE_MEM32(g_esp) = apc.iostatus_va; /* IoStatusBlock */
g_esp -= 4; BRIDGE_MEM32(g_esp) = apc.context_va; /* ApcContext */
g_esp -= 4; BRIDGE_MEM32(g_esp) = 0u; /* return address */
fn();
if (g_esp != saved_esp) {
fprintf(stderr,
"[GUEST-APC] stack mismatch routine=%08X expected=%08X got=%08X\n",
apc.routine_va, saved_esp, g_esp);
}
/* KiUserApcDispatcher returns through NtContinue on Xbox, restoring the
* interrupted thread's complete CPU context. Callback register changes
* must not leak into the alertable waiter. */
bridge_restore_guest_context(&saved_context);
if (g_guest_apc_log_count < 24u) {
fprintf(stderr, "[GUEST-APC] delivered routine=%08X\n", apc.routine_va);
++g_guest_apc_log_count;
}
return 1;
}
void xbox_kernel_raise_hardware_interrupt(uint32_t bus_level)
{
if (bus_level < 32u)
InterlockedOr((volatile LONG *)&g_kernel_pending_hardware_interrupts,
(LONG)(1u << bus_level));
}
void xbox_kernel_lower_hardware_interrupt(uint32_t bus_level)
{
if (bus_level < 32u)
InterlockedAnd((volatile LONG *)&g_kernel_pending_hardware_interrupts,
(LONG)~(1u << bus_level));
}
static recomp_func_t bridge_lookup_guest_function(uint32_t va)
{
recomp_func_t fn = recomp_lookup_manual(va);
if (!fn) fn = recomp_lookup(va);
if (!fn) fn = recomp_lookup_kernel(va);
return fn;
}
static int bridge_deliver_hardware_interrupts(void)
{
LONG pending;
uint32_t bus_level;
uint32_t i;
int delivered = 0;
if (g_delivering_hardware_interrupt)
return 0;
pending = InterlockedExchange(
(volatile LONG *)&g_kernel_pending_hardware_interrupts, 0);
if (!pending)
return 0;
g_delivering_hardware_interrupt = 1;
for (bus_level = 0; bus_level < 32u; ++bus_level) {
const uint32_t mask = 1u << bus_level;
int registered = 0;
if (!((uint32_t)pending & mask))
continue;
for (i = 0; i < g_guest_interrupt_count; ++i) {
bridge_guest_interrupt *interrupt = &g_guest_interrupts[i];
bridge_guest_cpu_context saved_context;
recomp_func_t fn;
uint32_t saved_esp;
uint32_t handled;
KIRQL old_irql;
if (interrupt->bus_level != bus_level ||
!InterlockedCompareExchange(&interrupt->connected, 0, 0))
continue;
registered = 1;
if (BRIDGE_MEM8(0x24u) >= interrupt->irql) {
InterlockedOr((volatile LONG *)&g_kernel_pending_hardware_interrupts,
(LONG)mask);
continue;
}
fn = bridge_lookup_guest_function(interrupt->service_routine_va);
if (!fn) {
fprintf(stderr,
"[GUEST-IRQ] unresolved ISR bus=%u routine=%08X\n",
bus_level, interrupt->service_routine_va);
continue;
}
bridge_capture_guest_context(&saved_context);
saved_esp = saved_context.esp;
old_irql = xbox_KfRaiseIrql((KIRQL)interrupt->irql);
BRIDGE_MEM8(0x24u) = (uint8_t)interrupt->irql;
g_esp -= 4; BRIDGE_MEM32(g_esp) = interrupt->service_context_va;
g_esp -= 4; BRIDGE_MEM32(g_esp) = interrupt->interrupt_va;
g_esp -= 4; BRIDGE_MEM32(g_esp) = 0u;
fn();
handled = g_eax;
xbox_KfLowerIrql(old_irql);
BRIDGE_MEM8(0x24u) = old_irql;
if (g_esp != saved_esp && g_guest_interrupt_log_count < 32u) {
fprintf(stderr,
"[GUEST-IRQ] ISR stack mismatch routine=%08X expected=%08X got=%08X\n",
interrupt->service_routine_va, saved_esp, g_esp);
}
bridge_restore_guest_context(&saved_context);
delivered = 1;
if (bridge_trace_interrupts_enabled() &&
g_guest_interrupt_log_count < 64u) {
fprintf(stderr,
"[GUEST-IRQ] delivered bus=%u object=%08X isr=%08X handled=%u\n",
bus_level, interrupt->interrupt_va,
interrupt->service_routine_va, handled != 0u);
++g_guest_interrupt_log_count;
}
}
if (!registered && bridge_trace_interrupts_enabled() &&
g_guest_interrupt_log_count < 64u) {
fprintf(stderr, "[GUEST-IRQ] no connected ISR for bus=%u\n",
bus_level);
++g_guest_interrupt_log_count;
}
}
g_delivering_hardware_interrupt = 0;
bridge_deliver_guest_dpcs();
return delivered;
}
void xbox_kernel_service_hardware_interrupts(void)
{
if (InterlockedCompareExchange(
(volatile LONG *)&g_kernel_pending_hardware_interrupts, 0, 0) != 0)
bridge_deliver_hardware_interrupts();
}
/* -- Synthetic VA range (for function exports) ----------- */
#define KERNEL_VA_BASE 0xFE000000u
#define KERNEL_VA_END (KERNEL_VA_BASE + XBOX_KERNEL_THUNK_TABLE_SIZE * 4)
/* -- Kernel data exports ----------------------------------
*
* Some kernel ordinals are DATA exports (structs/variables), not functions.
* The game reads their thunk entries and dereferences the result to access
* the data. These cannot use synthetic VAs -- they must point to real,
* dereferenceable addresses in the Xbox VA space.
*
* We allocate a "kernel data area" at XBOX_KERNEL_DATA_BASE and populate
* it with the expected structures.
*/
/**
* Get the Xbox VA of data for a kernel DATA export ordinal.
* Returns 0 if the ordinal is not a data export (i.e., it's a function).
*/
static uint32_t kernel_data_va_for_ordinal(ULONG ordinal)
{
if (ordinal == 357) return XBOX_KERNEL_DATA_BASE + KDATA_IDEX_CHANNEL_DATA;
switch (ordinal) {
case 16: return XBOX_KERNEL_DATA_BASE + KDATA_EVENT_OBJ_TYPE;
case 40: return XBOX_KERNEL_DATA_BASE + KDATA_HAL_CACHE_PARTITIONS;
case 64: return XBOX_KERNEL_DATA_BASE + KDATA_IO_COMPLETION_TYPE;
case 70: return XBOX_KERNEL_DATA_BASE + KDATA_IO_DEVICE_TYPE;
case 71: return XBOX_KERNEL_DATA_BASE + KDATA_IO_FILE_TYPE;
case 156: return XBOX_KERNEL_DATA_BASE + KDATA_TICK_COUNT;
case 164: return XBOX_KERNEL_DATA_BASE + KDATA_LAUNCH_DATA_PAGE;
case 259: return XBOX_KERNEL_DATA_BASE + KDATA_THREAD_OBJ_TYPE;
case 321: return XBOX_KERNEL_DATA_BASE + KDATA_EEPROM_KEY;
case 322: return XBOX_KERNEL_DATA_BASE + KDATA_HARDWARE_INFO;
case 323: return XBOX_KERNEL_DATA_BASE + KDATA_HD_KEY;
case 324: return XBOX_KERNEL_DATA_BASE + KDATA_KRNL_VERSION;
case 325: return XBOX_KERNEL_DATA_BASE + KDATA_SIGNATURE_KEY;
case 326: return XBOX_KERNEL_DATA_BASE + KDATA_XE_IMAGE_FILENAME;
case 353: return XBOX_KERNEL_DATA_BASE + KDATA_LAN_KEY;
case 354: return XBOX_KERNEL_DATA_BASE + KDATA_ALT_SIGNATURE_KEYS;
case 355: return XBOX_KERNEL_DATA_BASE + KDATA_XE_PUBLIC_KEY;
case 356: return XBOX_KERNEL_DATA_BASE + KDATA_HAL_BOOT_VIDEO_MODE;
case 357: return XBOX_KERNEL_DATA_BASE + KDATA_IDEX_CHANNEL_OBJECT;
default: return 0; /* Not a data export */
}
}
/**
* Initialize kernel data export values at the kernel data area.
* Called during bridge init, after Xbox memory is mapped.
*/
static void kernel_data_init(void)
{
/* XboxHardwareInfo (ordinal 322) - XBOX_HARDWARE_INFO
* +0: ULONG Flags (0 = retail, 0x20 = devkit)
* +4: UCHAR GpuRevision
* +5: UCHAR McpRevision
*/
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_HARDWARE_INFO + 0) = 0; /* Retail */
BRIDGE_MEM8(XBOX_KERNEL_DATA_BASE + KDATA_HARDWARE_INFO + 4) = 0xA1; /* NV2A A1 */
BRIDGE_MEM8(XBOX_KERNEL_DATA_BASE + KDATA_HARDWARE_INFO + 5) = 0xB1; /* MCPX B1 */
/* XboxKrnlVersion (ordinal 324) - XBOX_KRNL_VERSION
* +0: USHORT Major (1)
* +2: USHORT Minor (0)
* +4: USHORT Build (5849 = XDK version)
* +6: USHORT Qfe (0)
*/
BRIDGE_MEM16(XBOX_KERNEL_DATA_BASE + KDATA_KRNL_VERSION + 0) = 1;
BRIDGE_MEM16(XBOX_KERNEL_DATA_BASE + KDATA_KRNL_VERSION + 2) = 0;
BRIDGE_MEM16(XBOX_KERNEL_DATA_BASE + KDATA_KRNL_VERSION + 4) = 5849;
BRIDGE_MEM16(XBOX_KERNEL_DATA_BASE + KDATA_KRNL_VERSION + 6) = 0;
/* KeTickCount (ordinal 156) - initialized to current tick count.
* A background thread in main.c updates this every ~1ms. */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_TICK_COUNT) = GetTickCount();
/* LaunchDataPage (ordinal 164) - NULL (no launch data) */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_LAUNCH_DATA_PAGE) = 0;
/* PsThreadObjectType (ordinal 259) - type object (stub: 0) */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_THREAD_OBJ_TYPE) = 0;
/* ExEventObjectType (ordinal 16) - type object (stub: 0) */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_EVENT_OBJ_TYPE) = 0;
/* I/O manager object-type exports (ordinals 64, 70, and 71). */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_IO_COMPLETION_TYPE) = 0;
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_IO_DEVICE_TYPE) = 0;
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_IO_FILE_TYPE) = 0;
/* HAL globals used by the XDK startup code. */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_HAL_CACHE_PARTITIONS) = 3;
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_HAL_BOOT_VIDEO_MODE) = 0;
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_IDEX_CHANNEL_DATA) = 0x49444558; /* 'IDEX' */
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_IDEX_CHANNEL_OBJECT) =
XBOX_KERNEL_DATA_BASE + KDATA_IDEX_CHANNEL_DATA;
{
uint32_t list_head = XBOX_KERNEL_DATA_BASE + KDATA_IDEX_CHANNEL_DATA + 0x28;
BRIDGE_MEM32(list_head + 0) = list_head;
BRIDGE_MEM32(list_head + 4) = list_head;
}
/* Per-console keys are deterministic zeroes in the PC runtime. */
memset((void*)((uintptr_t)(XBOX_KERNEL_DATA_BASE + KDATA_EEPROM_KEY) + g_xbox_mem_offset), 0, 16);
/* XboxHDKey (ordinal 323) - 16 bytes of zeros (no key) */
memset((void*)((uintptr_t)(XBOX_KERNEL_DATA_BASE + KDATA_HD_KEY) + g_xbox_mem_offset), 0, 16);
/* XboxSignatureKey (ordinal 325) - 16 bytes of zeros */
memset((void*)((uintptr_t)(XBOX_KERNEL_DATA_BASE + KDATA_SIGNATURE_KEY) + g_xbox_mem_offset), 0, 16);
/* XboxLANKey (ordinal 353) - 16 bytes of zeros */
memset((void*)((uintptr_t)(XBOX_KERNEL_DATA_BASE + KDATA_LAN_KEY) + g_xbox_mem_offset), 0, 16);
/* XboxAlternateSignatureKeys (ordinal 354) - 256 bytes of zeros */
memset((void*)((uintptr_t)(XBOX_KERNEL_DATA_BASE + KDATA_ALT_SIGNATURE_KEYS) + g_xbox_mem_offset), 0, 256);
/* XePublicKeyData (ordinal 355) - 284 bytes of zeros */
memset((void*)((uintptr_t)(XBOX_KERNEL_DATA_BASE + KDATA_XE_PUBLIC_KEY) + g_xbox_mem_offset), 0, 284);
/* XeImageFileName (ordinal 326) - guest-layout ANSI_STRING. */
{
static const char image_filename[] = "\\Device\\CdRom0\\default.xbe";
uint32_t buffer_va = XBOX_KERNEL_DATA_BASE + KDATA_XE_IMAGE_BUFFER;
memcpy((void*)((uintptr_t)buffer_va + g_xbox_mem_offset),
image_filename, sizeof(image_filename));
BRIDGE_MEM16(XBOX_KERNEL_DATA_BASE + KDATA_XE_IMAGE_FILENAME + 0) =
(uint16_t)(sizeof(image_filename) - 1);
BRIDGE_MEM16(XBOX_KERNEL_DATA_BASE + KDATA_XE_IMAGE_FILENAME + 2) =
(uint16_t)sizeof(image_filename);
BRIDGE_MEM32(XBOX_KERNEL_DATA_BASE + KDATA_XE_IMAGE_FILENAME + 4) =
buffer_va;
}
fprintf(stderr, " Kernel data exports: initialized at Xbox VA 0x%08X\n",
XBOX_KERNEL_DATA_BASE);
}
/* -- Per-slot ordinal and bridge function ------------------ */
/* Ordinal for each slot (read from Xbox memory during init) */
static ULONG g_slot_ordinals[XBOX_KERNEL_THUNK_TABLE_SIZE];
/* Log counter - limit output to avoid flooding */
static int g_kernel_call_count = 0;
/* Read Xbox stack arg as uint32_t.
* After kernel_thunk_dispatch pops the dummy return address (g_esp += 4),
* arg0 is at g_esp+0, arg1 at g_esp+4, etc. */
#define STACK_ARG(n) ((uint32_t)BRIDGE_MEM32(g_esp + (n) * 4))
/* -- Per-ordinal bridge functions -------------------------
*
* Each bridge reads args from the Xbox stack, translates pointer
* args from Xbox VA->native, calls the kernel function, and stores
* the result in g_eax.
*
* Xbox cdecl: args pushed right-to-left, caller cleans stack.
* Xbox stdcall: args pushed right-to-left, callee cleans stack.
* In our case the caller (translated code) does "PUSH32" for each arg
* before calling, and the kernel function's ret-N is handled by the
* translated code's own stack adjustment.
*/
/* -- PsCreateSystemThreadEx (ordinal 255) ----------------
* NTSTATUS PsCreateSystemThreadEx(
* PHANDLE ThreadHandle, // arg0: Xbox VA -> pointer
* ULONG ThreadExtraSize, // arg1: value
* ULONG KernelStackSize, // arg2: value
* ULONG TlsDataSize, // arg3: value
* PULONG ThreadId, // arg4: Xbox VA -> pointer (can be NULL)
* PVOID StartContext1, // arg5: Xbox VA -> opaque
* PVOID StartContext2, // arg6: Xbox VA -> opaque
* BOOLEAN CreateSuspended, // arg7: value
* BOOLEAN DebugStack, // arg8: value
* PXBOX_SYSTEM_ROUTINE StartRoutine // arg9: Xbox function pointer
* )
*
* For static recompilation, we don't create a real thread.
* Instead we call the StartRoutine synchronously via RECOMP_ICALL.
* This is correct because on Xbox, the entry point creates a system
* thread and returns, and the thread runs the actual game.
*/
static int g_thread_call_count = 0;
static void bridge_PsCreateSystemThreadEx(void)
{
uint32_t xbox_handle_ptr = STACK_ARG(0);
uint32_t start_context1 = STACK_ARG(5);
uint32_t start_context2 = STACK_ARG(6);
uint32_t start_routine = STACK_ARG(9);
int is_first_call = (g_thread_call_count == 0);
g_thread_call_count++;
fprintf(stderr, " [KERNEL] PsCreateSystemThreadEx #%d: routine=0x%08X ctx1=0x%08X ctx2=0x%08X\n",
g_thread_call_count, start_routine, start_context1, start_context2);
fflush(stderr);
/* Write a fake handle to the output pointer */
if (xbox_handle_ptr) {
BRIDGE_MEM32(xbox_handle_ptr) = 0xBEEF0001; /* fake handle */
}
/* Call the start routine synchronously through the recomp dispatch.
* Xbox thread start routines receive two parameters:
* void ThreadRoutine(PVOID StartContext1, PVOID StartContext2)
* We push both onto the simulated stack (right-to-left).
*
* First call: the game's main thread entry point. Must run synchronously
* and inherit the current register state (this IS the game starting).
*
* Subsequent calls: worker threads. Must save/restore ALL global registers
* because on real Xbox each thread has its own register set. Without this,
* the worker clobbers the caller's g_esi, g_ebx, etc. */
if (start_routine) {
recomp_func_t fn = recomp_lookup(start_routine);
if (!fn) fn = recomp_lookup_manual(start_routine);
if (fn) {
if (is_first_call) {
/* Main game thread: run directly, inheriting register state */
g_esp -= 4; BRIDGE_MEM32(g_esp) = start_context2;
g_esp -= 4; BRIDGE_MEM32(g_esp) = start_context1;
g_esp -= 4; BRIDGE_MEM32(g_esp) = 0;
fn();
g_esp += 12;
fprintf(stderr, " [KERNEL] PsCreateSystemThreadEx: main thread returned (g_eax=0x%08X)\n", g_eax);
fflush(stderr);
} else {
/* Worker thread: run synchronously but save/restore all
* global registers (each Xbox thread has its own register set).
* The XIP file loader runs as a worker and must complete
* before the scene graph can be built. */
bridge_guest_cpu_context saved_context;
bridge_capture_guest_context(&saved_context);
fprintf(stderr, " [KERNEL] PsCreateSystemThreadEx: running worker 0x%08X (ctx=0x%08X)\n",
start_routine, start_context1);
fflush(stderr);
g_esp -= 4; BRIDGE_MEM32(g_esp) = start_context2;
g_esp -= 4; BRIDGE_MEM32(g_esp) = start_context1;
g_esp -= 4; BRIDGE_MEM32(g_esp) = 0;
g_seh_ebp = g_esp;
fn();
fprintf(stderr, " [KERNEL] PsCreateSystemThreadEx: worker returned\n");
fflush(stderr);
/* Restore all registers */
bridge_restore_guest_context(&saved_context);
}
} else {
fprintf(stderr, " [KERNEL] PsCreateSystemThreadEx: start routine 0x%08X not found in dispatch!\n",
start_routine);
}
}
g_eax = 0; /* STATUS_SUCCESS */
}
/* -- NtClose (ordinal 187) -------------------------------
* NTSTATUS NtClose(HANDLE Handle)
* Handle is a value (not a pointer), so safe for generic call.
*/
/* Handle-table helpers; defined further below. Xbox memory slots are 32-bit
* but native HANDLEs are 64-bit pointers, so handles are kept in a table and
* referenced by tagged 32-bit tokens. */
static void bridge_write_handle(uint32_t handle_va, HANDLE h);
static HANDLE bridge_read_handle(uint32_t token);
static HANDLE bridge_take_handle(uint32_t token);
static int bridge_trace_save_io_enabled(void);
static uint32_t s_save_trace_token;
static void bridge_NtClose(void)
{
uint32_t raw_handle = STACK_ARG(0);
if (bridge_trace_kernel_calls_enabled() && g_kernel_call_count <= 200) {
fprintf(stderr, " [KERNEL] NtClose: handle=0x%08X\n", raw_handle);
fflush(stderr);
}
g_eax = 0; /* STATUS_SUCCESS */
/* Close real handles but skip fake/synthetic ones. Route all native
* handles through the shared implementation so per-handle kernel state
* (including directory enumeration contexts) is released before Windows
* can reuse the handle value. */
if (raw_handle && raw_handle != 0xDEAD0001u && raw_handle != 0xBEEF0010u) {
if (bridge_trace_save_io_enabled() && raw_handle == s_save_trace_token) {
fprintf(stderr,
"[SAVE-IO] close func=%08X token=%08X host=%p\n",
g_recomp_current_func, raw_handle,
bridge_read_handle(raw_handle));
fflush(stderr);
s_save_trace_token = 0u;
}
HANDLE h = bridge_take_handle(raw_handle);
if (h && h != INVALID_HANDLE_VALUE)
g_eax = (uint32_t)xbox_NtClose(h);
}
}
static uint32_t s_trace_contig_36k_allocs[160];
static uint32_t s_trace_contig_sizes[160];
static uint32_t s_trace_contig_36k_count;
static int bridge_trace_contig_36k_enabled(void)
{
static int enabled = -1;
if (enabled < 0)
enabled = getenv("MERCENARIES_TRACE_CONTIG_36K") != NULL;
return enabled;
}
static int bridge_trace_contig_surfaces_enabled(void)
{
static int enabled = -1;
if (enabled < 0)
enabled = getenv("MERCENARIES_TRACE_CONTIG_SURFACES") != NULL;
return enabled;
}
static int bridge_is_traced_contig_size(uint32_t size)
{
if (bridge_trace_contig_36k_enabled() &&
(size == 18432u || size == 36864u))
return 1;
if (bridge_trace_contig_surfaces_enabled() &&
(size == 614400u || size == 1228800u || size == 2457600u))
return 1;
return 0;
}
static void bridge_trace_contig_36k_ring(const char *phase, uint32_t address,
uint32_t size, uint32_t alignment)
{
const uint32_t game_end = g_recomp_recent_game_func_idx;
const uint32_t game_count = game_end < 24u ? game_end : 24u;
const uint32_t full_end = g_recomp_recent_func_idx;
const uint32_t full_count = full_end < 48u ? full_end : 48u;
uint32_t i;
fprintf(stderr, "[CONTIG-TRACE] %s=%08X size=%u align=%u guest=%08X game=",
phase, address, size, alignment, g_recomp_current_func);
for (i = 0u; i < game_count; ++i)
fprintf(stderr, "%s%08X", i != 0u ? "," : "",
g_recomp_recent_game_funcs[(game_end - game_count + i) & 255u]);
fprintf(stderr, " full=");
for (i = 0u; i < full_count; ++i)
fprintf(stderr, "%s%08X", i != 0u ? "," : "",
g_recomp_recent_funcs[(full_end - full_count + i) & 63u]);
fputc('\n', stderr);
fflush(stderr);
}
/* -- MmAllocateContiguousMemory (ordinal 165) -------------
* PVOID MmAllocateContiguousMemory(ULONG NumberOfBytes)
*/
static void bridge_MmAllocateContiguousMemory(void)
{
uint32_t size = STACK_ARG(0);
/* Reserve every physical page touched by a contiguous allocation. */
uint32_t xbox_va = xbox_HeapAllocPageRounded(size, 4096);
if (g_kernel_call_count <= 100) {
fprintf(stderr, " [KERNEL] MmAllocateContiguousMemory: size=%u -> Xbox VA 0x%08X\n",
size, xbox_va);
fflush(stderr);
}
g_eax = xbox_va;
}
/* -- MmAllocateContiguousMemoryEx (ordinal 166) -----------
* PVOID MmAllocateContiguousMemoryEx(SIZE_T size, ULONG_PTR low, ULONG_PTR high,
* ULONG alignment, ULONG protect)
*/
static void bridge_MmAllocateContiguousMemoryEx(void)
{
uint32_t size = STACK_ARG(0);
uint32_t low = STACK_ARG(1);
uint32_t high = STACK_ARG(2);
uint32_t align = STACK_ARG(3);
uint32_t prot = STACK_ARG(4);
/* Allocate from Xbox heap with requested alignment */
if (align < 4096) align = 4096;
uint32_t xbox_va = xbox_HeapAllocPageRounded(size, align);
if (bridge_is_traced_contig_size(size) &&
s_trace_contig_36k_count < 160u) {
s_trace_contig_36k_allocs[s_trace_contig_36k_count++] = xbox_va;
s_trace_contig_sizes[s_trace_contig_36k_count - 1u] = size;
bridge_trace_contig_36k_ring("alloc", xbox_va, size, align);
}
if (g_kernel_call_count <= 100) {
fprintf(stderr, " [KERNEL] MmAllocateContiguousMemoryEx: size=%u align=%u -> Xbox VA 0x%08X\n",
size, align, xbox_va);
fflush(stderr);
}
g_eax = xbox_va;
}
/* PVOID MmAllocateSystemMemory(ULONG NumberOfBytes, ULONG Protect)
* (ordinal 167)
*
* The host-side xbox_MmAllocateSystemMemory implementation returns a native
* pointer. Recompiled code must instead receive an Xbox VA so MEM8/MEM32 can
* translate it through the shared guest-memory mapping. Keep system-memory
* allocations in the same tracked guest heap as the other bridge allocators.
*/
static void bridge_MmAllocateSystemMemory(void)
{
const uint32_t size = STACK_ARG(0);
const uint32_t protect = STACK_ARG(1);
const uint32_t xbox_va = xbox_HeapAlloc(size, 4096);
XBOX_TRACE(XBOX_LOG_MEM,
"MmAllocateSystemMemory: size=%u protect=0x%08X -> Xbox VA 0x%08X",
size, protect, xbox_va);
g_eax = xbox_va;
}
/* ULONG MmFreeSystemMemory(PVOID BaseAddress, ULONG NumberOfBytes)
* (ordinal 172). The guest heap records the allocation size, so the Xbox
* NumberOfBytes argument is retained for ABI fidelity but is not needed by
* the host allocator.
*/
static void bridge_MmFreeSystemMemory(void)
{
const uint32_t xbox_va = STACK_ARG(0);
const uint32_t size = STACK_ARG(1);
const uint32_t allocation_size = xbox_HeapGetAllocationSize(xbox_va);
const uint32_t freed_pages = (allocation_size + 4095u) / 4096u;
XBOX_TRACE(XBOX_LOG_MEM,
"MmFreeSystemMemory: Xbox VA 0x%08X size=%u pages=%u",
xbox_va, size, freed_pages);
xbox_HeapFree(xbox_va);
g_eax = freed_pages;
}
/* ULONG MmQueryAllocationSize(PVOID BaseAddress) (ordinal 180) */
static void bridge_MmQueryAllocationSize(void)
{
uint32_t xbox_va = STACK_ARG(0);
uint32_t size = xbox_HeapGetAllocationSize(xbox_va);
XBOX_TRACE(XBOX_LOG_MEM,
"MmQueryAllocationSize(0x%08X) = %u", xbox_va, size);
g_eax = size;
}
/* -- MmFreeContiguousMemory (ordinal 171) -----------------
* VOID MmFreeContiguousMemory(PVOID BaseAddress)
*/
static void bridge_MmFreeContiguousMemory(void)
{
uint32_t addr = STACK_ARG(0);
uint32_t i;
if (bridge_trace_contig_36k_enabled() ||
bridge_trace_contig_surfaces_enabled()) {
for (i = 0u; i < s_trace_contig_36k_count; ++i) {
if (s_trace_contig_36k_allocs[i] == addr) {
s_trace_contig_36k_allocs[i] = 0u;
bridge_trace_contig_36k_ring(
"free", addr, s_trace_contig_sizes[i], 0u);
break;
}
}
}
xbox_HeapFree(addr);
g_eax = 0;
}
/* MmClaimGpuInstanceMemory (ordinal 168)
* PVOID MmClaimGpuInstanceMemory(ULONG NumberOfBytes,
* PULONG NumberOfPaddingBytes)
*/
static void bridge_MmClaimGpuInstanceMemory(void)
{
const uint32_t size = STACK_ARG(0);
const uint32_t padding_va = STACK_ARG(1);
ULONG padding = 0;
PVOID result = xbox_MmClaimGpuInstanceMemory(size, &padding);
if (padding_va >= 0x00010000u && padding_va < 0x04000000u)
BRIDGE_MEM32(padding_va) = padding;
if (bridge_trace_kernel_calls_enabled() && g_kernel_call_count <= 200) {
fprintf(stderr,
" [KERNEL] MmClaimGpuInstanceMemory: size=%u "
"padding_va=%08X padding=%08X result=%08X\n",
size, padding_va, padding, (uint32_t)(uintptr_t)result);
fflush(stderr);
}
g_eax = (uint32_t)(uintptr_t)result;
}
/* -- NtAllocateVirtualMemory (ordinal 184) ----------------
* NTSTATUS NtAllocateVirtualMemory(PVOID *BaseAddress, ULONG ZeroBits,
* PULONG AllocationSize, ULONG AllocationType, ULONG Protect)
*/
static void bridge_NtAllocateVirtualMemory(void)
{
uint32_t base_ptr = STACK_ARG(0); /* PVOID* in Xbox VA */
uint32_t zero_bits = STACK_ARG(1);
uint32_t size_ptr = STACK_ARG(2); /* PULONG in Xbox VA */
uint32_t alloc_type = STACK_ARG(3);
uint32_t protect = STACK_ARG(4);
/* Read the requested size from Xbox memory */
uint32_t size = size_ptr ? BRIDGE_MEM32(size_ptr) : 0;
/* Read the base address hint (0 = let kernel choose) */
uint32_t base_hint = base_ptr ? BRIDGE_MEM32(base_ptr) : 0;
if (bridge_trace_kernel_calls_enabled() && g_kernel_call_count <= 200) {
fprintf(stderr, " [KERNEL] NtAllocateVirtualMemory: base=0x%08X size=%u type=0x%X prot=0x%X\n",
base_hint, size, alloc_type, protect);
fflush(stderr);
}
if (size == 0) {
g_eax = 0xC0000045u; /* STATUS_INVALID_PAGE_PROTECTION */
return;
}
/*
* Xbox NtAllocateVirtualMemory supports two modes:
* - MEM_RESERVE (0x2000): Reserve virtual address space
* - MEM_COMMIT (0x1000): Commit pages within a reserved region
* - MEM_RESERVE|MEM_COMMIT (0x3000): Both in one call
*
* Our Xbox heap (bump allocator) always commits memory immediately,
* so MEM_COMMIT on an already-reserved region is a no-op.
* Only allocate new memory when MEM_RESERVE is requested.
*/
if (base_hint != 0 && (alloc_type & 0x2000) == 0) {
/* MEM_COMMIT only, on an already-reserved region.
* The memory is already committed by our bump allocator.
* Don't change the base address - just return success. */
if (bridge_trace_kernel_calls_enabled() && g_kernel_call_count <= 200) {
fprintf(stderr, " [KERNEL] -> MEM_COMMIT on existing region 0x%08X, no-op\n", base_hint);
fflush(stderr);
}
g_eax = 0; /* STATUS_SUCCESS */
return;
}
/* Allocate from Xbox heap (MEM_RESERVE or MEM_RESERVE|MEM_COMMIT) */
uint32_t xbox_va = xbox_HeapAlloc(size, 4096);
if (!xbox_va) {
g_eax = 0xC0000017u; /* STATUS_NO_MEMORY */
return;
}
/* Write back the allocated address and actual size */
if (base_ptr) BRIDGE_MEM32(base_ptr) = xbox_va;
if (size_ptr) BRIDGE_MEM32(size_ptr) = size;
g_eax = 0; /* STATUS_SUCCESS */
}
/* -- NtFreeVirtualMemory (ordinal 199) --------------------
* NTSTATUS NtFreeVirtualMemory(PVOID *BaseAddress, PULONG FreeSize,
* ULONG FreeType)
*/
static void bridge_NtFreeVirtualMemory(void)
{
uint32_t base_ptr = STACK_ARG(0);
uint32_t size_ptr = STACK_ARG(1);
uint32_t free_type = STACK_ARG(2);
g_eax = (uint32_t)xbox_NtFreeVirtualMemory(
XBOX_TO_NATIVE(base_ptr), XBOX_TO_NATIVE(size_ptr), free_type);
}
/* -- ExAllocatePool / ExAllocatePoolWithTag (ordinals 14, 15) -
* Must allocate from Xbox heap so the returned pointer is an Xbox VA
* that can be accessed via MEM32(). Native HeapAlloc returns 64-bit
* pointers that get truncated and produce garbage Xbox VAs.
*/
static void bridge_ExAllocatePool(void)
{
uint32_t size = STACK_ARG(0);
uint32_t xbox_va = xbox_HeapAlloc(size, 16);
if (bridge_trace_kernel_calls_enabled() && g_kernel_call_count <= 200) {
fprintf(stderr, " [KERNEL] ExAllocatePool: size=%u -> Xbox VA 0x%08X\n",
size, xbox_va);
fflush(stderr);
}
g_eax = xbox_va;
}
static void bridge_ExAllocatePoolWithTag(void)
{
uint32_t size = STACK_ARG(0);
uint32_t tag = STACK_ARG(1);
uint32_t xbox_va = xbox_HeapAlloc(size, 16);
if (bridge_trace_kernel_calls_enabled() && g_kernel_call_count <= 200) {