-
Notifications
You must be signed in to change notification settings - Fork 11
/
psp-svc.c
1536 lines (1342 loc) · 63 KB
/
psp-svc.c
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
/** @file
* PSP Emulator - API for the emulated supervisor part (SVC)
*/
/*
* Copyright (C) 2019-2020 Alexander Eichner <alexander.eichner@campus.tu-berlin.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <common/types.h>
#include <common/cdefs.h>
#include <psp-fw/svc_id.h>
#include <psp-fw/err.h>
#include <sev/sev.h>
#include <psp-svc.h>
#include <psp-core.h>
#include <psp-trace.h>
#include <libpspproxy.h>
/** Pointer to the emulated supervisor firmware state. */
typedef struct PSPSVCINT *PPSPSVCINT;
/**
* x86 memory mapping slot.
*/
typedef struct PSPSVCX86MAPPING
{
/** The base X86 address being mapped (aligned to a 64MB boundary). */
X86PADDR PhysX86AddrBase;
/** The base PSP address returned from the proxied PSP (used for syncing the mappings). */
PSPADDR PspAddrProxyBase;
/** The memory type being used. */
uint32_t uMemType;
/** Reference counter for this mapping, the mapping gets cleaned up if it reaches 0. */
uint32_t cRefs;
/** The region handle associated with the active mapping. */
PSPIOMREGIONHANDLE hIoMgrRegion;
/** Size of the mapping in bytes. */
size_t cbMapping;
/** Reference to the internal SVC state (used in the fetch callback). */
PPSPSVCINT pThis;
} PSPSVCX86MAPPING;
/** Pointer to an x86 memory mapping slot. */
typedef PSPSVCX86MAPPING *PPSPSVCX86MAPPING;
/** Pointer to a const x86 memory mapping slot. */
typedef const PSPSVCX86MAPPING *PCPSPSVCX86MAPPING;
/**
* Emulated supervisor firmware state.
*/
typedef struct PSPSVCINT
{
/** Pointer to the PSP emulation core. */
PSPCORE hPspCore;
/** The I/O manager handle to manage x86 memory mappings. */
PSPIOM hIoMgr;
/** The PSP proxy to forward requests to. */
PSPPROXYCTX hProxyCtx;
/** Size of the state region. */
uint32_t cbStateRegion;
/** x86 memory mapping slots. */
PSPSVCX86MAPPING aX86MapSlots[15];
} PSPSVCINT;
static bool pspEmuSvcTrace(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcAppExit(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcAppInit(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcSmnMapEx(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcSmnMap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcSmnUnmap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcDbgLog(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcX86MemMap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcX86MemUnmap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcX86CopyToPsp(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcX86CopyFromPsp(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcX86MemMapEx(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcSmuMsg(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x32Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x33Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x35Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x36Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x38Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcRng(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcQuerySaveStateRegion(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x41Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvc0x42Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
static bool pspEmuSvcQuerySmmRegion(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser);
#define PSPEMU_CORE_SVC_INIT_NULL { NULL, NULL, 0 }
#define PSPEMU_CORE_SVC_INIT_DEF(a_Name, a_Handler) { a_Name, a_Handler, PSPEMU_CORE_SVMC_F_BEFORE }
/**
* The SVC descriptors table.
*/
static PSPCORESVMCDESC g_aSvcDescs[] =
{
PSPEMU_CORE_SVC_INIT_DEF("SvcAppExit", pspEmuSvcAppExit), /**< 0x00: Application exit. */
PSPEMU_CORE_SVC_INIT_DEF("SvcAppInit", pspEmuSvcAppInit), /**< 0x01: Initialize application stack. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x02: Load entry from flash. */
PSPEMU_CORE_SVC_INIT_DEF("SvcSmnMapEx", pspEmuSvcSmnMapEx), /**< 0x03: Map SMN address into memory. */
PSPEMU_CORE_SVC_INIT_DEF("SvcSmnMap", pspEmuSvcSmnMap), /**< 0x04: Map SMN address into memory, extended version. */
PSPEMU_CORE_SVC_INIT_DEF("SvcSmnUnmap", pspEmuSvcSmnUnmap), /**< 0x05: Unmap previously mapped SMN address. */
PSPEMU_CORE_SVC_INIT_DEF("SvcDbgLog", pspEmuSvcDbgLog), /**< 0x06: Debug log. */
PSPEMU_CORE_SVC_INIT_DEF("SvcX86MemMap", pspEmuSvcX86MemMap), /**< 0x07: Map x86 memory address into PSP memory space. */
PSPEMU_CORE_SVC_INIT_DEF("SvcX86MemUnmap", pspEmuSvcX86MemUnmap), /**< 0x08: Unmap previously mapped x86 memory address. */
PSPEMU_CORE_SVC_INIT_DEF("SvcX86CopyToPsp", pspEmuSvcX86CopyToPsp), /**< 0x09: Copy data from physical x86 memory space to PSP. */
PSPEMU_CORE_SVC_INIT_DEF("SvcX86CopyFromPsp", pspEmuSvcX86CopyFromPsp), /**< 0x0a: Write status code or data value to physical x86 memory space. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x0b: Invalidate/Clean memory. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x0c: Crypto request interfacing with CCP. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x0d: Unknown. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x0e: Unknown. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x0f: Unknown. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x10: Unknown. */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x11: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x12: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x13: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x14: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x15: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x16: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x17: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x18: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x19: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x1a: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x1b: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x1c: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x1d: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x1e: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x1f: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x20: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x21: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x22: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x23: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x24: */
PSPEMU_CORE_SVC_INIT_DEF("SvcX86MemMapEx", pspEmuSvcX86MemMapEx), /**< 0x25: Map physical x86 memory into PSP address space */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x26: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x27: */
PSPEMU_CORE_SVC_INIT_DEF("SvcSmuMsg", pspEmuSvcSmuMsg), /**< 0x28: Execute request on SMU */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x29: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x2a: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x2b: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x2c: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x2d: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x2e: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x2f: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x30: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x31: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x32Unk", pspEmuSvc0x32Unk), /**< 0x32: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x33Unk", pspEmuSvc0x33Unk), /**< 0x33: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x34: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x35Unk", pspEmuSvc0x35Unk), /**< 0x35: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x36Unk", pspEmuSvc0x36Unk), /**< 0x36: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x37: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x38Unk", pspEmuSvc0x38Unk), /**< 0x38: */
PSPEMU_CORE_SVC_INIT_DEF("SvcRng", pspEmuSvcRng), /**< 0x39: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x3a: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x3b: */
PSPEMU_CORE_SVC_INIT_DEF("SvcQuerySaveStateRegion", pspEmuSvcQuerySaveStateRegion), /**< 0x3c: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x3d: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x3e: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x3f: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x40: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x41Unk", pspEmuSvc0x41Unk), /**< 0x41: */
PSPEMU_CORE_SVC_INIT_DEF("Svc0x42Unk", pspEmuSvc0x42Unk), /**< 0x42: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x43: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x44: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x45: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x46: */
PSPEMU_CORE_SVC_INIT_NULL, /**< 0x47: */
PSPEMU_CORE_SVC_INIT_DEF("SvcQuerySmmRegion", pspEmuSvcQuerySmmRegion) /**< 0x48: */
};
/**
* SVC injection registration record.
*/
static const PSPCORESVMCREG g_SvcReg =
{
/** GlobalSvmc */
{
/** pszName */
"Trace",
/** pfnSvmcHnd */
pspEmuSvcTrace,
/** fFlags */
PSPEMU_CORE_SVMC_F_BEFORE | PSPEMU_CORE_SVMC_F_AFTER
},
/** cSvmcDescs */
ELEMENTS(g_aSvcDescs),
/** paSvmcDescs */
&g_aSvcDescs[0]
};
static bool pspEmuSvcTrace(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PSPEmuTraceEvtAddSvc(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_SVC, idxSyscall,
(fFlags & PSPEMU_CORE_SVMC_F_BEFORE)
? true
: false /* fEntry*/,
NULL /*pszMsg*/);
return false;
}
static bool pspEmuSvcAppExit(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
(void)idxSyscall;
/* Stop here as the app exited. */
uint32_t PspAddrStateRegion = 0;
#if 0 /** @todo */
int rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, SVC_GET_STATE_BUFFER, pThis->cbStateRegion, 0, 0, 0, &PspAddrStateRegion);
if (rc)
printf("Mapping memory region state failed with %d\n", rc);
rc = PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrStateRegion, pThis->X86MappingPrivState.pvMapping, pThis->cbStateRegion);
if (rc)
printf("Syncing SEV state to privileged DRAM failed with %d\n", rc);
#endif
PSPEmuCoreExecStop(hCore);
return true;
}
static bool pspEmuSvcAppInit(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
(void)idxSyscall;
uint32_t uSts = 0;
PSPADDR uStackTop = 0x62000;
PSPADDR UsrPtrStackAddr = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R2, &UsrPtrStackAddr);
if (!rc)
{
/* Map stack. */
/*rc = PSPEmuCoreMemAddRegion(pThis->hPspCore, 0x60000, 2 * _4K);*/ /** @todo Done already in the core for the other emulation modes. */
if (!rc)
rc = PSPEmuCoreMemWrite(pThis->hPspCore, UsrPtrStackAddr, &uStackTop, sizeof(uStackTop));
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
if (!rc)
rc = PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvcSmnMapEx(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t uSmnAddr = 0;
uint32_t idCcdTgt = 0;
uint32_t uSmnAddrMapped = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &uSmnAddr);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R1, &idCcdTgt);
if (!rc)
{
printf("Mapping SMN address %#x on CCD %#x\n", uSmnAddr, idCcdTgt);
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, uSmnAddr, idCcdTgt, 0, 0, &uSmnAddrMapped);
if (rc)
printf("Mapping SMN address failed with %d\n", rc);
}
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSmnAddrMapped);
return true;
}
static bool pspEmuSvcSmnMap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t uSmnAddr = 0;
uint32_t uSmnAddrMapped = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &uSmnAddr);
if (!rc)
{
printf("Mapping SMN address %#x\n", uSmnAddr);
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, uSmnAddr, 0, 0, 0, &uSmnAddrMapped);
if (rc)
printf("Mapping SMN address failed with %d\n", rc);
}
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSmnAddrMapped);
return true;
}
static bool pspEmuSvcSmnUnmap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t uAddr = 0;
uint32_t uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &uAddr);
if (!rc)
{
printf("Unmapping SMN address %#x\n", uAddr);
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, uAddr, 0, 0, 0, &uSts);
if (rc)
printf("Unmapping SMN address failed with %d\n", rc);
}
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvcDbgLog(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
/* Log the string. */
PSPADDR PspAddrStr = 0;
char achStr[512];
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &PspAddrStr);
if (!rc)
{
PSPEmuCoreMemRead(pThis->hPspCore, PspAddrStr, &achStr[0], 512);
achStr[512 - 1] = '\0'; /* Ensure termination. */
printf("PSP Log: %s\n", &achStr[0]);
}
return true;
}
/**
* The fetch callback for an existing x86 memory backing.
*
* @returns nothing.
* @param offX86Mem Offset to fetch from the beginning of the mapping.
* @param cbFetch Number of bytes to fetch.
* @param pvDst Where to store the fetched data.
* @param pvUser Opqaue user data passed during region creation.
*/
static void pspEmuIoMgrX86MappingFetch(X86PADDR offX86Mem, size_t cbFetch, void *pvDst, void *pvUser)
{
PPSPSVCX86MAPPING pMapping = (PPSPSVCX86MAPPING)pvUser;
int rc = PSPProxyCtxPspMemRead(pMapping->pThis->hProxyCtx, pMapping->PspAddrProxyBase + (uint32_t)offX86Mem, pvDst, cbFetch);
if (rc)
printf("Fetching memory content from %#x failed with %d\n", pMapping->PspAddrProxyBase + (uint32_t)offX86Mem, rc);
}
/**
* Worker for the x86 memory mapping syscalls.
*
* @returns Status code.
* @param pThis The SVC state instance.
* @param u32PhysX86AddrLow Low part of the physical x86 address to map.
* @param u32PhysX86AddrHigh High part of the physical x86 address to map.
* @param uMemType The type of memory to map (presumably).
* @param pPspAddrMapped Where to store the virtual PSP address of the mapping on success.
*/
static int pspEmuSvcX86MemMapWorker(PPSPSVCINT pThis, uint32_t u32PhysX86AddrLow, uint32_t u32PhysX86AddrHigh, uint32_t uMemType,
PSPADDR *pPspAddrMapped)
{
int rc = -1;
X86PADDR PhysX86Addr = (((uint64_t)u32PhysX86AddrLow << 32) | u32PhysX86AddrHigh);
X86PADDR PhysX86AddrBase = (PhysX86Addr & ~(_64M - 1));
uint32_t offStart = PhysX86Addr - PhysX86AddrBase;
size_t cbMapping = (PhysX86AddrBase + _64M) - PhysX86Addr;
printf("Mapping x86 address %#llx (64MB aligned base %#llx, memory target %u)\n", PhysX86Addr, PhysX86AddrBase, uMemType);
/* Search for a free mapping slot (unlike the real off chip bootloader we treat all slots equal). */
PPSPSVCX86MAPPING pMapping = NULL;
uint32_t idxSlot = 0;
for (uint32_t i = 0; i < ELEMENTS(pThis->aX86MapSlots); i++)
{
if ( pThis->aX86MapSlots[i].PhysX86AddrBase == NIL_X86PADDR
&& pThis->aX86MapSlots[i].cRefs == 0)
{
pMapping = &pThis->aX86MapSlots[i];
idxSlot = i;
break;
}
}
if (pMapping)
{
/* Map the address into the proxied PSP before creating the actual mapping. */
PSPADDR PspAddrProxyMap;
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, SVC_X86_HOST_MEMORY_MAP, u32PhysX86AddrLow, u32PhysX86AddrHigh, uMemType, 0, &PspAddrProxyMap);
if ( !rc
&& PspAddrProxyMap != 0)
{
pMapping->cRefs = 1;
pMapping->uMemType = uMemType;
pMapping->PhysX86AddrBase = PhysX86Addr; /* We cheat here and don't use the full 64MB mapping but cover only what the caller asked for. */
pMapping->PspAddrProxyBase = PspAddrProxyMap;
pMapping->cbMapping = cbMapping;
/* Create the x86 memory region. */
rc = PSPEmuIoMgrX86MemRegister(pThis->hIoMgr, PhysX86Addr, cbMapping, false /*fCanExec*/,
pspEmuIoMgrX86MappingFetch, pMapping, "SvcTmpMapping", &pMapping->hIoMgrRegion);
if (!rc)
{
/*
* This emulates the real behavior of the off chip bootloader, we program the x86 mapping registers
* of the emulated x86 mapping engine to point to our created x86 memory mapping.
*/
PSPADDR PspAddrSlotBase = 0x03230000 + idxSlot * 4 * sizeof(uint32_t);
uint32_t uTmp = 0;
/* Program base address. */
uTmp = ((PhysX86AddrBase >> 32) << 6) | ((PhysX86AddrBase >> 26) & 0x3f);
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 0, &uTmp, sizeof(uTmp));
/* Unknown but fixed value. */
uTmp = 0x12;
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 4, &uTmp, sizeof(uTmp));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 8, &uMemType, sizeof(uMemType));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 12, &uMemType, sizeof(uMemType));
uTmp = 0xffffffff;
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, 0x032303e0 + idxSlot * sizeof(uint32_t), &uTmp, sizeof(uTmp));
uTmp = 0xc0000000;
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, 0x032304d8 + idxSlot * sizeof(uint32_t), &uTmp, sizeof(uTmp));
if (!rc)
*pPspAddrMapped = 0x04000000 + idxSlot * _64M + offStart;
else
{
/* Something went wrong... */
/** @todo Undo everything. */
printf("Programming the x86 mapping control registers failed with %d\n", rc);
}
}
else
{
uint32_t uSts = 0;
int rc2 = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, SVC_X86_HOST_MEMORY_UNMAP, PspAddrProxyMap, 0, 0, 0, &uSts);
printf("Creating the x86 memory region failed with %d (Unmapping proxied memory yielded rc=%d uSts=%#x)\n", rc, rc2, uSts);
}
}
else
printf("Mapping %#llx on the proxied PSP failed with rc=%d PspAddrProxyMap=%#x\n", PhysX86Addr, rc, PspAddrProxyMap);
}
else
{
/* This should never happen as the real PSP has only 15 mapping slots. */
/** @todo Unmap the mapping on the proxied PSP. */
printf("Ran out of x86 mapping slots, impossible!\n");
rc = -1;
}
return rc;
}
static bool pspEmuSvcX86MemMap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t u32PhysX86AddrLow = 0;
uint32_t u32PhysX86AddrHigh = 0;
PSPADDR PspAddrMap = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &u32PhysX86AddrLow);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R1, &u32PhysX86AddrHigh);
if (!rc)
rc = pspEmuSvcX86MemMapWorker(pThis, u32PhysX86AddrLow, u32PhysX86AddrHigh, 4 /*From off chip bootloader*/,
&PspAddrMap);
if (rc)
PspAddrMap = 0;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, PspAddrMap);
return true;
}
static bool pspEmuSvcX86MemUnmap(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
PSPADDR PspAddrMap = 0;
uint32_t uSts = PSPSTATUS_SUCCESS;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &PspAddrMap);
if (!rc)
{
/* Extract slot and offset. */
PspAddrMap -= 0x04000000; /* Get rid of base address. */
uint32_t offMapping = PspAddrMap & (_64M - 1);
PspAddrMap &= ~(_64M - 1); /* Mask out the mapping offset. */
uint32_t idxSlot = PspAddrMap / _64M;
printf("Unmapping x86 address mapped at %#x (slot %u)\n", PspAddrMap, idxSlot);
if (idxSlot < ELEMENTS(pThis->aX86MapSlots))
{
PPSPSVCX86MAPPING pMapping = &pThis->aX86MapSlots[idxSlot];
if (pMapping->cRefs > 0)
{
pMapping->cRefs--;
/* If reference counter reached zero the mapping gets destroyed. */
if (!pMapping->cRefs)
{
/* Sync back memory content before unmapping on the proxied PSP. */
size_t cbSyncLeft = pMapping->cbMapping;
uint32_t offSync = 0;
while ( !rc
&& cbSyncLeft)
{
uint8_t abData[_4K];
size_t cbThisSync = MIN(sizeof(abData), cbSyncLeft);
rc = PSPEmuIoMgrX86MemRead(pMapping->hIoMgrRegion, offSync, &abData[0], cbThisSync);
if (!rc)
rc = PSPProxyCtxPspMemWrite(pThis->hProxyCtx, pMapping->PspAddrProxyBase, &abData[0], cbThisSync);
offSync += cbThisSync;
cbSyncLeft -= cbThisSync;
}
if (rc)
printf("Syncing the memory to the proxied PSP memory failed with %d\n", rc);
/* Unmap on the proxied PSP. */
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, pMapping->PspAddrProxyBase, 0, 0, 0, &uSts);
if (rc || uSts)
printf("Unmapping x86 address failed with rc=%d uSts=%d\n", rc, uSts);
/* Unmap the emulated x86 mapping. */
uint32_t uTmp = 0;
PSPADDR PspAddrSlotBase = 0x03230000 + idxSlot * 4 * sizeof(uint32_t);
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 0, &uTmp, sizeof(uTmp));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 4, &uTmp, sizeof(uTmp));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 8, &uTmp, sizeof(uTmp));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, PspAddrSlotBase + 12, &uTmp, sizeof(uTmp));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, 0x032303e0 + idxSlot * sizeof(uint32_t), &uTmp, sizeof(uTmp));
if (!rc)
rc = PSPEmuIoMgrPspAddrWrite(pThis->hIoMgr, 0x032304d8 + idxSlot * sizeof(uint32_t), &uTmp, sizeof(uTmp));
if (rc)
{
/* Something went wrong... */
printf("Programming the x86 mapping control registers failed with %d\n", rc);
}
/* Deregister the mapping. */
rc = PSPEmuIoMgrDeregister(pMapping->hIoMgrRegion);
if (rc)
printf("Deregistering the x86 memory region failed with %d\n", rc);
/* Clear the mapping slot. */
pMapping->PhysX86AddrBase = NIL_X86PADDR;
pMapping->PspAddrProxyBase = 0;
pMapping->uMemType = 0;
pMapping->cRefs = 0;
pMapping->hIoMgrRegion = NULL;
}
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvcX86CopyToPsp(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
#if 0
#endif
return true;
}
static bool pspEmuSvcX86CopyFromPsp(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
#if 0
#endif
return true;
}
static bool pspEmuSvcX86MemMapEx(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t u32PhysX86AddrLow = 0;
uint32_t u32PhysX86AddrHigh = 0;
uint32_t uMemType = 0;
PSPADDR PspAddrMap = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &u32PhysX86AddrLow);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R1, &u32PhysX86AddrHigh);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R2, &uMemType);
if (!rc)
rc = pspEmuSvcX86MemMapWorker(pThis, u32PhysX86AddrLow, u32PhysX86AddrHigh, uMemType,
&PspAddrMap);
if (rc)
PspAddrMap = 0;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, PspAddrMap);
return true;
}
static bool pspEmuSvcSmuMsg(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t idMsg = 0;
uint32_t uArg0 = 0;
PSPADDR UsrPtrReturnMsg;
uint32_t uSts = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &idMsg);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R1, &uArg0);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R2, &UsrPtrReturnMsg);
if (!rc)
{
PSPADDR PspAddrScratch;
uint32_t u32Ret;
rc = PSPProxyCtxScratchSpaceAlloc(pThis->hProxyCtx, sizeof(u32Ret), &PspAddrScratch);
if (!rc)
{
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, idMsg, uArg0,
UsrPtrReturnMsg != 0
? PspAddrScratch
: 0,
0, &uSts);
if ( !rc
&& UsrPtrReturnMsg != 0)
{
/* Sync back the return value. */
rc = PSPProxyCtxPspMemRead(pThis->hProxyCtx, PspAddrScratch, &u32Ret, sizeof(u32Ret));
if (!rc)
{
rc = PSPEmuCoreMemWrite(pThis->hPspCore, UsrPtrReturnMsg, &u32Ret, sizeof(u32Ret));
if (rc)
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
PSPProxyCtxScratchSpaceFree(pThis->hProxyCtx, PspAddrScratch, sizeof(u32Ret));
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvc0x32Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
PSPADDR PspAddrUnk = 0;
uint32_t cbUnk = 0;
uint32_t uSts = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &PspAddrUnk);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R1, &cbUnk);
if (!rc)
{
void *pvTmp = malloc(cbUnk);
if (pvTmp)
{
rc = PSPEmuCoreMemRead(pThis->hPspCore, PspAddrUnk, pvTmp, cbUnk);
if (!rc)
{
PSPADDR PspAddrProxy;
rc = PSPProxyCtxScratchSpaceAlloc(pThis->hProxyCtx, cbUnk, &PspAddrProxy);
if (!rc)
{
rc = PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy, pvTmp, cbUnk);
if (!rc)
{
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, PspAddrProxy, cbUnk, 0, 0, &uSts);
if (!rc && uSts == 0)
{
/* Sync memory back. */
rc = PSPProxyCtxPspMemRead(pThis->hProxyCtx, PspAddrProxy, pvTmp, cbUnk);
if (!rc)
{
rc = PSPEmuCoreMemWrite(pThis->hPspCore, PspAddrUnk, pvTmp, cbUnk);
if (rc)
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
{
printf("Syscall failed with rc=%d uSts=%#x\n", rc, uSts);
if (rc)
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPProxyCtxScratchSpaceFree(pThis->hProxyCtx, PspAddrProxy, cbUnk);
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
free(pvTmp);
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvc0x33Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
PSPADDR PspAddrUnk = 0;
uint32_t cbUnk = 0;
uint32_t uSts = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &PspAddrUnk);
if (!rc)
rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R1, &cbUnk);
if (!rc)
{
printf("Unknown syscall 0x33 with parameters: PspAddrUnk=%#x cbUnk=%#x\n", PspAddrUnk, cbUnk);
void *pvTmp = malloc(cbUnk);
if (pvTmp)
{
rc = PSPEmuCoreMemRead(pThis->hPspCore, PspAddrUnk, pvTmp, cbUnk);
if (!rc)
{
PSPADDR PspAddrProxy;
rc = PSPProxyCtxScratchSpaceAlloc(pThis->hProxyCtx, cbUnk, &PspAddrProxy);
if (!rc)
{
rc = PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy, pvTmp, cbUnk);
if (!rc)
{
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, PspAddrProxy, cbUnk, 0, 0, &uSts);
if (!rc && uSts == 0)
{
/* Sync memory back. */
rc = PSPProxyCtxPspMemRead(pThis->hProxyCtx, PspAddrProxy, pvTmp, cbUnk);
if (!rc)
{
rc = PSPEmuCoreMemWrite(pThis->hPspCore, PspAddrUnk, pvTmp, cbUnk);
if (rc)
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
{
printf("Syscall failed with rc=%d uSts=%#x\n", rc, uSts);
if (rc)
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPProxyCtxScratchSpaceFree(pThis->hProxyCtx, PspAddrProxy, cbUnk);
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
free(pvTmp);
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvcPlatformReset(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
PPSPSVCINT pThis = (PPSPSVCINT)pvUser;
uint32_t uArgUnk = 0;
uint32_t uSts = 0;
int rc = PSPEmuCoreQueryReg(pThis->hPspCore, PSPCOREREG_R0, &uArgUnk);
if (!rc)
{
rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, uArgUnk, 0, 0, 0, &uSts);
if (rc)
{
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
printf("Platform reset failed with %d\n", rc);
}
}
else
uSts = PSPSTATUS_GENERAL_MEMORY_ERROR;
PSPEmuCoreSetReg(pThis->hPspCore, PSPCOREREG_R0, uSts);
return true;
}
static bool pspEmuSvc0x35Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
#if 0
uint32_t au32Req[8];
uint32_t uSts = 0;
PSPADDR PspAddrReq;
uc_reg_read(uc, UC_ARM_REG_R0, &PspAddrReq);
uc_mem_read(pThis->pUcEngine, PspAddrReq, &au32Req[0], sizeof(au32Req));
printf("Syscall 0x35 request:\n"
"au32Req[0]: %#x\n"
"au32Req[1]: %#x\n"
"au32Req[2]: %#x\n"
"au32Req[3]: %#x\n"
"au32Req[4]: %#x\n"
"au32Req[5]: %#x\n"
"au32Req[6]: %#x\n"
"au32Req[7]: %#x\n",
au32Req[0], au32Req[1], au32Req[2], au32Req[3],
au32Req[4], au32Req[5], au32Req[6], au32Req[7]);
if (au32Req[2] == 0 && au32Req[3] == 0)
{
uint32_t au32ReqProxy[8];
uint8_t abTmp[128];
PSPADDR PspAddrProxy1 = 0x20000;
PSPADDR PspAddrProxy2 = PspAddrProxy1 + au32Req[1];
PSPADDR PspAddrProxy3 = PspAddrProxy2 + au32Req[5];
uc_mem_read(pThis->pUcEngine, au32Req[0], &abTmp[0], au32Req[1]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy1, &abTmp[0], au32Req[1]);
uc_mem_read(pThis->pUcEngine, au32Req[4], &abTmp[0], au32Req[5]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy2, &abTmp[0], au32Req[5]);
uc_mem_read(pThis->pUcEngine, au32Req[6], &abTmp[0], au32Req[7]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy3, &abTmp[0], au32Req[7]);
au32ReqProxy[0] = PspAddrProxy1;
au32ReqProxy[1] = au32Req[1];
au32ReqProxy[2] = au32Req[2];
au32ReqProxy[3] = au32Req[3];
au32ReqProxy[4] = PspAddrProxy2;
au32ReqProxy[5] = au32Req[5];
au32ReqProxy[6] = PspAddrProxy3;
au32ReqProxy[7] = au32Req[7];
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, 0x23000, &au32ReqProxy[0], sizeof(au32ReqProxy));
int rc = PSPProxyCtxPspSvcCall(pThis->hProxyCtx, idxSyscall, 0x23000, 0, 0, 0, &uSts);
if (!rc && uSts == 0)
{
/* Sync memory back. */
PSPProxyCtxPspMemRead(pThis->hProxyCtx, PspAddrProxy1, &abTmp[0], au32Req[1]);
uc_mem_write(pThis->pUcEngine, au32Req[0], &abTmp[0], au32Req[1]);
PSPProxyCtxPspMemRead(pThis->hProxyCtx, PspAddrProxy2, &abTmp[0], au32Req[5]);
uc_mem_write(pThis->pUcEngine, au32Req[4], &abTmp[0], au32Req[5]);
PSPProxyCtxPspMemRead(pThis->hProxyCtx, PspAddrProxy3, &abTmp[0], au32Req[7]);
uc_mem_write(pThis->pUcEngine, au32Req[6], &abTmp[0], au32Req[7]);
}
else
{
printf("Syscall failed with %d uSts=%#x\n", rc, uSts);
if (rc)
uSts = 0x9;
}
}
else
{
printf("Request not implemented\n");
uSts = 0x9;
}
uc_reg_write(uc, UC_ARM_REG_R0, &uSts);
#endif
return true;
}
static bool pspEmuSvc0x36Unk(PSPCORE hCore, uint32_t idxSyscall, uint32_t fFlags, void *pvUser)
{
#if 0
uint32_t au32Req[13];
uint32_t uSts = 0x9;
PSPADDR PspAddrReq;
uc_reg_read(uc, UC_ARM_REG_R0, &PspAddrReq);
uc_mem_read(pThis->pUcEngine, PspAddrReq, &au32Req[0], sizeof(au32Req));
printf("Syscall 0x36 request:\n"
"au32Req[0]: %#x\n"
"au32Req[1]: %#x\n"
"au32Req[2]: %#x\n"
"au32Req[3]: %#x\n"
"au32Req[4]: %#x\n"
"au32Req[5]: %#x\n"
"au32Req[6]: %#x\n"
"au32Req[7]: %#x\n"
"au32Req[8]: %#x\n"
"au32Req[9]: %#x\n"
"au32Req[10]: %#x\n"
"au32Req[11]: %#x\n"
"au32Req[12]: %#x\n",
au32Req[0], au32Req[1], au32Req[2], au32Req[3],
au32Req[4], au32Req[5], au32Req[6], au32Req[7],
au32Req[8], au32Req[9], au32Req[10], au32Req[11],
au32Req[12]);
uint32_t au32ReqProxy[13];
void *pvTmp = malloc(_256K);
PSPADDR PspAddrProxy1 = 0x20000;
PSPADDR PspAddrProxy2 = PspAddrProxy1 + au32Req[1];
PSPADDR PspAddrProxy3 = PspAddrProxy2 + au32Req[3];
PSPADDR PspAddrProxy4 = PspAddrProxy3 + au32Req[6];
uc_mem_read(pThis->pUcEngine, au32Req[0], pvTmp, au32Req[1]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy1, pvTmp, au32Req[1]);
uc_mem_read(pThis->pUcEngine, au32Req[2], pvTmp, au32Req[3]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy2, pvTmp, au32Req[3]);
uc_mem_read(pThis->pUcEngine, au32Req[5], pvTmp, au32Req[6]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy3, pvTmp, au32Req[6]);
uc_mem_read(pThis->pUcEngine, au32Req[8], pvTmp, au32Req[9]);
PSPProxyCtxPspMemWrite(pThis->hProxyCtx, PspAddrProxy4, pvTmp, au32Req[9]);
au32ReqProxy[0] = PspAddrProxy1;
au32ReqProxy[1] = au32Req[1];
au32ReqProxy[2] = PspAddrProxy2;
au32ReqProxy[3] = au32Req[3];
au32ReqProxy[4] = au32Req[4];