-
Notifications
You must be signed in to change notification settings - Fork 11
/
psp-core.c
3407 lines (3013 loc) · 110 KB
/
psp-core.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 - Core API (interfacing with unicorn engine).
*/
/*
* Copyright (C) 2020 Alexander Eichner <[email protected]>
*
* 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 <string.h>
#include <unicorn/unicorn.h>
#include <common/types.h>
#include <common/cdefs.h>
#include <common/status.h>
#include <psp-core.h>
#include <psp-disasm.h>
#include <psp-trace.h>
/** Page size used in the PSP firmware. */
#define PSP_PAGE_SIZE _4K
#define PSP_PAGE_L1_IDX_SHIFT 20
/**
* A datum read/written.
*/
typedef union PSPDATUM
{
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
uint8_t ab[8];
} PSPDATUM;
typedef PSPDATUM *PPSPDATUM;
/**
* Currently pending exception.
*/
typedef enum PSPCOREEXCP
{
/** Invalid pending exception. */
PSPCOREEXCP_INVALID = 0,
/** No exception pending currently. */
PSPCOREEXCP_NONE,
/** SWI exception pending. */
PSPCOREEXCP_SWI,
/** SMC exception pending. */
PSPCOREEXCP_SMC,
/** IRQ exception pending. */
PSPCOREEXCP_IRQ,
/** FIQ exception pending. */
PSPCOREEXCP_FIQ,
/** 32bit hack. */
PSPCOREEXCP_32BIT_HACK = 0x7fffffff
} PSPCOREEXCP;
/** Pointer to a PSP core instance. */
typedef struct PSPCOREINT *PPSPCOREINT;
/** Pointer to a const PSP core instance. */
typedef const struct PSPCOREINT *PCPSPCOREINT;
/**
* A single trace point instance.
*/
typedef struct PSPCORETPINT
{
/** Next trace hook in the list. */
struct PSPCORETPINT *pNext;
/** Start PSP address. */
PSPADDR PspAddrStart;
/** End PSP address. */
PSPADDR PspAddrEnd;
/** The ASID to trigger on. */
ARMASID idAsid;
/** PSP core the hook belongs to. */
PPSPCOREINT pPspCore;
/** The trace callback to execute. */
PFNPSPCORETRACE pfnTrace;
/** Opaque user data to pass to the callback. */
void *pvUser;
/** The unicorn hook handle. */
uc_hook hUcHook;
} PSPCORETPINT;
/** Pointer to a trace hook. */
typedef PSPCORETPINT *PPSPCORETPINT;
/** Pointer to a const trace hook. */
typedef const PSPCORETPINT *PCPSPCORETPINT;
/**
* A single memory (RAM/MMIO) region registration.
*/
typedef struct PSPCOREMEMREGION
{
/** Next region in the list. */
struct PSPCOREMEMREGION *pNext;
/** Start PSP address. */
PSPADDR PspAddrStart;
/** Size of the region. */
size_t cbRegion;
/** PSP core the region belongs to. */
PPSPCOREINT pPspCore;
/** Flag whether this is a RAM of MMIO region. */
bool fMmio;
/** Flag whether the region is mapped directly in unicorn (for MMU disabled case). */
bool fMapped;
/** Region type dependent data. */
union
{
/** MMIO region. */
struct
{
/** MMIO read handler. */
PFNPSPCOREMMIOREAD pfnRead;
/** MMIO write handler. */
PFNPSPCOREMMIOWRITE pfnWrite;
/** Opaque user data to pass to the read/write callbacks. */
void *pvUser;
} Mmio;
/** RAM region. */
struct
{
/** The backing memory. */
void *pvBacking;
/** Protection flags assigned to this region. */
uint32_t fProt;
} Ram;
} u;
} PSPCOREMEMREGION;
/** Pointer to a trace hook. */
typedef PSPCOREMEMREGION *PPSPCOREMEMREGION;
/** Pointer to a const trace hook. */
typedef const PSPCOREMEMREGION *PCPSPCOREMEMREGION;
/**
* A MMU mapping.
*/
typedef struct PSPCOREMMUMAP
{
/** Pointer to the next MMU mapping structure in the list. */
struct PSPCOREMMUMAP *pNext;
/** PSP core the mapping belongs to. */
PPSPCOREINT pPspCore;
/** Virtual start address (aligned to a page). */
PSPVADDR PspAddrVStart;
/** The physical address it maps to. */
PSPPADDR PspAddrPStart;
/** Size of the region. */
size_t cbRegion;
/** Offset into the physical region the mapping starts at. */
PSPPADDR offPhysMap;
/** The physical memory region this mapping maps to. */
PCPSPCOREMEMREGION pMemRegion;
} PSPCOREMMUMAP;
/** Pointer to a MMU mapping structure. */
typedef PSPCOREMMUMAP *PPSPCOREMMUMAP;
/** Pointer to a const MMU mapping structure. */
typedef const PSPCOREMMUMAP *PCPSPCOREMMUMAP;
/**
* A set of banked Co-Processor registers.
*/
typedef struct PSPCORECPBANK
{
/** VBAR register. */
uint32_t u32RegVBar;
/** MVBAR register. */
uint32_t u32RegMVBar;
/** PAR register (VA to PA address translation). */
uint32_t u32RegPa;
/** TTBR0 register. */
uint32_t u32RegTtbr0;
/** TTBCR register. */
uint32_t u32RegTtbcr;
/** SCTRL register. */
uint32_t u32RegSctrl;
/** DFSR register. */
uint32_t u32RegDfsr;
/** IFSR register. */
uint32_t u32RegIfsr;
/** DFAR register. */
uint32_t u32RegDfar;
/** IFAR register. */
uint32_t u32RegIfar;
/** CONTEXTIDR register. */
uint32_t u32RegContextId;
/** TPIDRURW register. */
uint32_t u32RegTpIdURw;
/** TPIDRURO register. */
uint32_t u32RegTpIdURo;
/** TPIDRPRW register. */
uint32_t u32RegTpIdPRw;
} PSPCORECPBANK;
/** Pointer to a set of banked Co-Processor registers. */
typedef PSPCORECPBANK *PPSPCORECPBANK;
/** Pointer to a const of banked Co-Processor registers. */
typedef const PSPCORECPBANK *PCPSPCORECPBANK;
/** The index denoting the register bank when in secure world. */
#define PSP_CORE_CP_BANK_IDX_SECURE 0
/** The index denoting the register bank when in non-secure world. */
#define PSP_CORE_CP_BANK_IDX_NON_SECURE 1
/** Number of register banks available. */
#define PSP_CORE_CP_BANK_COUNT 2
/**
* Page table tracking structure.
*/
typedef struct PSPCOREPGTBLTRACK
{
/** Pointer to the next tracking structure. */
struct PSPCOREPGTBLTRACK *pNext;
/** Pointer to the owning core instance. */
PPSPCOREINT pThis;
/** Flag whether this tracks an L1 or L2 table. */
bool fL2PgTbl;
/** Unicorn hook handle to monitor writes. */
uc_hook hUcHookWrites;
/** The physical page table start address we are tracking. */
PSPPADDR PhysAddrPgTblStart;
/** Virtual address of the page tables (this is what the unicorn hook is registered with). */
PSPVADDR PspAddrVPgTbl;
/** Size of the page table we are tracking. */
size_t cbPgTbl;
} PSPCOREPGTBLTRACK;
/** Pointer to a page table tracking structure. */
typedef PSPCOREPGTBLTRACK *PPSPCOREPGTBLTRACK;
/** Pointer to a const page table tracking structure. */
typedef const PSPCOREPGTBLTRACK *PCPSPCOREPGTBLTRACK;
/**
* A single PSP core executing.
*/
typedef struct PSPCOREINT
{
/** The unicorn engine pointer. */
uc_engine *pUcEngine;
/** The initial CPU context state used for resetting. */
uc_context *pUcCtxReset;
/** The interrupt hook. */
uc_hook pUcHookIntr;
/** The next address to execute instructions from. */
PSPADDR PspAddrExecNext;
/** Flag whether the exeuction should stop. */
bool fExecStop;
/** The current CPU mode. */
PSPCOREMODE enmCoreMode;
/** Currently pending exception. */
PSPCOREEXCP enmExcpPending;
/** The CPSR change hook. */
uc_hook hUcHookCpsrChange;
/** The current CPSR value. */
uint32_t u32RegCpsr;
/** Head of registered trace points. */
PPSPCORETPINT pTpHead;
/** Head of memory regions. */
PPSPCOREMEMREGION pMemRegionsHead;
/** Lowest memory address assigned to a region (for faster lookup). */
PSPADDR PspAddrMemLowest;
/** Highest memory address assigned to a region (inclusive). */
PSPADDR PspAddrMemHighest;
/** The WFI reached callback if set. */
PFNPSPCOREWFI pfnWfiReached;
/** Opaque user data to pass to the WFI reached callback. */
void *pvWfiUser;
/** The SVC injection registartion record set, NULL if no overrides exist. */
PCPSPCORESVMCREG pSvcReg;
/** Opaque user data to pass to the SVC handlers. */
void *pvSvcUser;
/** The currently syscall number being executed. */
uint32_t idxSvc;
/** The hook for the after SVC breakpoint. */
uc_hook hUcHookSvcAfter;
/** The SMC injection registartion record set, NULL if no overrides exist. */
PCPSPCORESVMCREG pSmcReg;
/** Opaque user data to pass to the SMC handlers. */
void *pvSmcUser;
/** The current smc number being serviced. */
uint32_t idxSmc;
/** CP write hook. */
uc_hook hUcHookCpWrite;
/** CP read hook. */
uc_hook hUcHookCpRead;
/** Hook for invalid memory accesses. */
uc_hook hUcInvMemAcc;
/** Flag whether the MMU is currently set up for secure world. */
bool fMmuSecure;
/** Flag whether the MMU status has changed. */
bool fMmuChanged;
/** Flag whether the MMU is currently enabled. */
bool fMmuEnabled;
/** Flag whether the IRQ line is asserted. */
bool fIrq;
/** Flag whether the FIQ line is asserted. */
bool fFiq;
/** Head of MMU mappings sorted by virtual start address. */
PPSPCOREMMUMAP pMmuMappingsHead;
/** Head of page trable tracking structures to monitor writes to L1 and L2. */
PPSPCOREPGTBLTRACK pMmuPgTblTrackingHead;
/** @name Co-Processor 15 related registers.
* @{ */
struct
{
/** Secure Debug Configuration register. */
uint32_t u32RegScr;
/** Banked registers. */
PSPCORECPBANK aBankedRegs[PSP_CORE_CP_BANK_COUNT];
} Cp15;
/** @} */
} PSPCOREINT;
/**
* PSP Core register name to unicorn mapping.
*/
static const int g_aUcRegs[] =
{
0,
UC_ARM_REG_R0,
UC_ARM_REG_R1,
UC_ARM_REG_R2,
UC_ARM_REG_R3,
UC_ARM_REG_R4,
UC_ARM_REG_R5,
UC_ARM_REG_R6,
UC_ARM_REG_R7,
UC_ARM_REG_R8,
UC_ARM_REG_R9,
UC_ARM_REG_R10,
UC_ARM_REG_R11,
UC_ARM_REG_R12,
UC_ARM_REG_SP,
UC_ARM_REG_LR,
UC_ARM_REG_PC,
UC_ARM_REG_CPSR,
UC_ARM_REG_SPSR
};
/**
* Human readable error strings for unicorn status codes.
*/
static const char *g_apszUcErr[] =
{
"UC_ERR_OK",
"UC_ERR_NOMEM",
"UC_ERR_ARCH",
"UC_ERR_HANDLE",
"UC_ERR_MODE",
"UC_ERR_VERSION",
"UC_ERR_READ_UNMAPPED",
"UC_ERR_WRITE_UNMAPPED",
"UC_ERR_FETCH_UNMAPPED",
"UC_ERR_HOOK",
"UC_ERR_INSN_INVALID",
"UC_ERR_MAP",
"UC_ERR_WRITE_PROT",
"UC_ERR_READ_PROT",
"UC_ERR_FETCH_PROT",
"UC_ERR_ARG",
"UC_ERR_READ_UNALIGNED",
"UC_ERR_WRITE_UNALIGNED",
"UC_ERR_FETCH_UNALIGNED",
"UC_ERR_HOOK_EXIST",
"UC_ERR_RESOURCE",
"UC_ERR_EXCEPTION",
"UC_ERR_TIMEOUT"
};
/**
* The register set during a batch query for the state dump method.
*/
static const PSPCOREREG g_aenmRegQueryBatch[] =
{
PSPCOREREG_R0,
PSPCOREREG_R1,
PSPCOREREG_R2,
PSPCOREREG_R3,
PSPCOREREG_R4,
PSPCOREREG_R5,
PSPCOREREG_R6,
PSPCOREREG_R7,
PSPCOREREG_R8,
PSPCOREREG_R9,
PSPCOREREG_R10,
PSPCOREREG_R11,
PSPCOREREG_R12,
PSPCOREREG_SP,
PSPCOREREG_LR,
PSPCOREREG_PC,
PSPCOREREG_CPSR,
PSPCOREREG_SPSR
};
static int pspEmuCoreMmuPAddrQueryFromVAddr(PPSPCOREINT pThis, PSPVADDR PspVAddr, PSPPADDR *pPspPAddr, size_t *pcbRegion,
PPSPCOREPGTBLWALKSTS penmPgTblWalk);
static int pspEmuCoreMmuMappingsClear(PPSPCOREINT pThis);
/**
* Converts the PSP core register enum to the unicorn equivalent.
*
* @returns Unicorn register number.
* @param enmReg The register.
*/
static int pspEmuCoreReg2Uc(PSPCOREREG enmReg)
{
return g_aUcRegs[enmReg];
}
/**
* Converts a unicorn error to a general status code.
*
* @returns Status code.
* @param rcUc The unicorn status code to convert.
*/
static int pspEmuCoreErrConvertFromUcErr(uc_err rcUc)
{
if (rcUc == UC_ERR_OK)
return 0;
printf("rcUc=%u (%s)\n", rcUc, rcUc < ELEMENTS(g_apszUcErr) ? g_apszUcErr[rcUc] : "<UNKNOWN>");
return -1; /** @todo */
}
/**
* Returns the name of the given core mode.
*
* @returns Pointer to string for human readable core mode.
* @param enmCoreMode The core mode.
*/
static const char *pspEmuCoreModeToStr(PSPCOREMODE enmCoreMode)
{
switch (enmCoreMode)
{
case PSPCOREMODE_USR:
return "USR";
case PSPCOREMODE_FIQ:
return "FIQ";
case PSPCOREMODE_IRQ:
return "IRQ";
case PSPCOREMODE_SVC:
return "SVC";
case PSPCOREMODE_MON:
return "MON";
case PSPCOREMODE_ABRT:
return "ABRT";
case PSPCOREMODE_UNDEF:
return "UNDEF";
case PSPCOREMODE_SYS:
return "SYS";
default:
printf("pspEmuCoreModeToStr(): Invalid mode selected!\n");
}
return "<INVALID>";
}
/**
* Returns the internal mode value from the given CPSR.
*
* @returns Internal mode value.
* @param u32Cpsr The CPSR to convert from.
*/
static inline PSPCOREMODE pspEmuCoreModeFromCpsr(uint32_t u32Cpsr)
{
PSPCOREMODE enmCoreMode = PSPCOREMODE_INVALID;
switch (u32Cpsr & 0x1f)
{
case 0x10:
enmCoreMode = PSPCOREMODE_USR;
break;
case 0x11:
enmCoreMode = PSPCOREMODE_FIQ;
break;
case 0x12:
enmCoreMode = PSPCOREMODE_IRQ;
break;
case 0x13:
enmCoreMode = PSPCOREMODE_SVC;
break;
case 0x16:
enmCoreMode = PSPCOREMODE_MON;
break;
case 0x17:
enmCoreMode = PSPCOREMODE_ABRT;
break;
case 0x1b:
enmCoreMode = PSPCOREMODE_UNDEF;
break;
case 0x1f:
enmCoreMode = PSPCOREMODE_SYS;
break;
default:
printf("pspEmuCoreModeFromCpsr(): Invalid mode selected!\n");
}
return enmCoreMode;
}
/**
* Returns the CPSR mode bits from the given internal core mode.
*
* @returns CPSR mode bits.
* @param enmCoreMode THe internal core mode to convert.
*/
static inline uint32_t pspEmuCoreModeToCpsr(PSPCOREMODE enmCoreMode)
{
uint32_t uCpsr = 0;
switch (enmCoreMode)
{
case PSPCOREMODE_USR:
uCpsr = 0x10;
break;
case PSPCOREMODE_FIQ:
uCpsr = 0x11;
break;
case PSPCOREMODE_IRQ:
uCpsr = 0x12;
break;
case PSPCOREMODE_SVC:
uCpsr = 0x13;
break;
case PSPCOREMODE_MON:
uCpsr = 0x16;
break;
case PSPCOREMODE_ABRT:
uCpsr = 0x17;
break;
case PSPCOREMODE_UNDEF:
uCpsr = 0x1b;
break;
case PSPCOREMODE_SYS:
uCpsr = 0x1f;
break;
default:
printf("pspEmuCoreModeToCpsr(): Invalid mode selected!\n");
}
return uCpsr;
}
/**
* Returns flag whether the core is currently operating in the secure world.
*
* @returns Flag indicating whether the core is in secure world mode.
* @param pThis The PSP emulation core instance.
*/
static inline bool pspEmuCoreIsSecure(PPSPCOREINT pThis)
{
if ( pThis->Cp15.u32RegScr & 0x1
&& pThis->enmCoreMode != PSPCOREMODE_MON)
return false;
return true;
}
/**
* Returns the co-processor register bank based on the current processor state.
*
* @returns Pointer to the co-processor register bank.
* @param pThis The PSP emulation core instance.
*/
static PPSPCORECPBANK pspEmuCoreCpGetBank(PPSPCOREINT pThis)
{
/* Monitor mode is always executed in securre world regardless of the NS bit in SCR. */
if (!pspEmuCoreIsSecure(pThis))
return &pThis->Cp15.aBankedRegs[PSP_CORE_CP_BANK_IDX_NON_SECURE];
return &pThis->Cp15.aBankedRegs[PSP_CORE_CP_BANK_IDX_SECURE];
}
/**
* Returns whether the MMU is enabled in the current world SCTRL register.
*
* @returns Flag whether the MMU is enabled.
* @param pThis The PSP emulation core instance.
*/
static inline bool pspEmuCoreCpIsSctrlMmuEnabled(PPSPCOREINT pThis)
{
PCPSPCORECPBANK pCpBank = pspEmuCoreCpGetBank(pThis);
if (pCpBank->u32RegSctrl & BIT(0))
return true;
return false;
}
/**
* Checks whether an interrupt is pending and injects it.
*
* @returns nothing.
* @param pThis The PSP emulation core instance.
* @param PspAddrPc The PC causing the check.
* @param fWait Flag whether to wait for an interrupt (for wfi).
*/
static void pspEmuCoreIrqCheckAndInject(PPSPCOREINT pThis, PSPVADDR PspAddrPc, bool fWait)
{
bool fIrq = pThis->fIrq;
bool fFirq = pThis->fFiq;
if (pThis->pfnWfiReached)
pThis->pfnWfiReached(pThis, PspAddrPc, fWait ? 0 : PSPEMU_CORE_WFI_CHECK, &fIrq, &fFirq, pThis->pvWfiUser);
if (pThis->fIrq || pThis->fFiq)
{
if (pThis->enmExcpPending != PSPCOREEXCP_NONE)
printf("OVERWRITING another exception which should not happen!\n");
if (fFirq && !(pThis->u32RegCpsr & BIT(6)))
pThis->enmExcpPending = PSPCOREEXCP_FIQ;
else if (fIrq && !(pThis->u32RegCpsr & BIT(7)))
pThis->enmExcpPending = PSPCOREEXCP_IRQ;
if (pThis->enmExcpPending == PSPCOREEXCP_FIQ || pThis->enmExcpPending == PSPCOREEXCP_IRQ)
{
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CORE, "Injecting IRQ!\n");
if (!fWait)
uc_emu_stop(pThis->pUcEngine);
}
}
}
/**
* The trace hook wrapper called by unicorn.
*
* @returns nothing.
* @param pUcEngine The unicorn engine pointer.
* @param uAddr The address of the instruction triggering the hook.
* @param cbInsn Size of the instruction.
* @param pvUser Opaque user data.
*/
static void pspEmuCoreUcHookWrapper(uc_engine *pUcEngine, uint64_t uAddr, uint32_t cbInsn, void *pvUser)
{
PPSPCORETPINT pTp = (PPSPCORETPINT)pvUser;
PPSPCOREINT pThis = pTp->pPspCore;
PPSPCORECPBANK pCpBank = pspEmuCoreCpGetBank(pThis);
if ( pTp->idAsid == ARMASID_ANY
|| pTp->idAsid == pCpBank->u32RegContextId)
pTp->pfnTrace(pThis, pTp, PSPEMU_CORE_TRACE_F_EXEC, (PSPADDR)uAddr, cbInsn, NULL /*pvVal*/, pTp->pvUser);
}
/**
* The memory trace hook wrapper called by unicorn.
*
* @returns nothing.
* @param pUcEngine The unicorn engine pointer.
* @param uMemType Memory type.
* @param uAddr The address of the instruction triggering the hook.
* @param cb Size of the memory access.
* @param i64Val Value written during a write, ignored for a read.
* @param pvUser Opaque user data.
*/
static void pspEmuCoreUcHookMemWrapper(uc_engine *pUcEngine, uc_mem_type uMemType, uint64_t uAddr, int32_t cb, int64_t i64Val, void *pvUser)
{
PPSPCORETPINT pTp = (PPSPCORETPINT)pvUser;
PPSPCOREINT pThis = pTp->pPspCore;
PPSPCORECPBANK pCpBank = pspEmuCoreCpGetBank(pThis);
uint32_t fTpFlags = 0;
switch (uMemType)
{
case UC_MEM_READ:
case UC_MEM_READ_PROT:
fTpFlags |= PSPEMU_CORE_TRACE_F_READ;
break;
case UC_MEM_WRITE:
case UC_MEM_WRITE_PROT:
fTpFlags |= PSPEMU_CORE_TRACE_F_WRITE;
break;
default:
/** @todo Assert */
break;
}
if ( pTp->idAsid == ARMASID_ANY
|| pTp->idAsid == pCpBank->u32RegContextId)
{
PSPDATUM Datum;
switch (cb)
{
case 1:
Datum.u8 = (uint8_t)i64Val;
break;
case 2:
Datum.u16 = (uint16_t)i64Val;
break;
case 4:
Datum.u32 = (uint32_t)i64Val;
break;
case 8:
Datum.u64 = (uint64_t)i64Val;
break;
default:
/** @todo Assert */
break;
}
pTp->pfnTrace(pThis, pTp, fTpFlags, (PSPADDR)uAddr, cb, &Datum.ab[0], pTp->pvUser);
}
}
/**
* Unicorn MMIO read wrapper.
*
* @returns Data read.
* @param pUcEngine The unicorn engine pointer.
* @param pvUser Opaque user data.
* @param uAddr MMIO address read.
* @param cbInsn Size of the read (1, 2, 4 or 8 bytes).
*/
static uint64_t pspEmuCoreMmioRead(struct uc_struct* pUcEngine, void *pvUser, uint64_t uAddr, unsigned cb)
{
PCPSPCOREMEMREGION pRegion = (PCPSPCOREMEMREGION)pvUser;
PSPDATUM ValRead;
uint64_t uValRet = 0;
pRegion->u.Mmio.pfnRead(pRegion->pPspCore, (PSPADDR)uAddr, cb, &ValRead, pRegion->u.Mmio.pvUser);
switch (cb)
{
case 1:
uValRet = ValRead.u8;
break;
case 2:
uValRet = ValRead.u16;
break;
case 4:
uValRet = ValRead.u32;
break;
case 8:
uValRet = ValRead.u64;
break;
default:
/** @todo assert() */
uc_emu_stop(pUcEngine);
}
pspEmuCoreIrqCheckAndInject(pRegion->pPspCore, (PSPVADDR)uAddr, false /*fWait*/);
return uValRet;
}
/**
* Unicorn MMIO write wrapper.
*
* @returns nothing.
* @param pUcEngine The unicorn engine pointer.
* @param pvUser Opaque user data.
* @param uAddr MMIO address written.
* @param uVal Value written.
* @param cbInsn Size of the write (1, 2, 4 or 8 bytes).
*/
static void pspEmuCoreMmioWrite(struct uc_struct* pUcEngine, void *pvUser, uint64_t uAddr, uint64_t uVal, unsigned cb)
{
PCPSPCOREMEMREGION pRegion = (PCPSPCOREMEMREGION)pvUser;
PSPDATUM ValWrite;
switch (cb)
{
case 1:
ValWrite.u8 = (uint8_t)uVal;
break;
case 2:
ValWrite.u16 = (uint16_t)uVal;
break;
case 4:
ValWrite.u32 = (uint32_t)uVal;
break;
case 8:
ValWrite.u64 = uVal;
break;
default:
/** @todo assert() */
uc_emu_stop(pUcEngine);
}
pRegion->u.Mmio.pfnWrite(pRegion->pPspCore, (PSPADDR)uAddr, cb, &ValWrite, pRegion->u.Mmio.pvUser);
pspEmuCoreIrqCheckAndInject(pRegion->pPspCore, (PSPVADDR)uAddr, false /*fWait*/);
}
/**
* The exception wrapper to transition between CPU modes.
*
* @returns nothing.
* @param pUcEngine Pointer to the unicorn engine instance.
* @param uIntNo Interrupt/Exception number.
* @param pvUser Opaque user data passed when adding the hook.
*/
static void pspEmuCoreExcpWrapper(uc_engine *pUcEngine, uint32_t uIntNo, void *pvUser)
{
PPSPCOREINT pThis = (PPSPCOREINT)pvUser;
/*
* Set appropriate exception and stop emulation, we don't alter the vital CPU state
* (PC, CPSR, etc.) here as unicorn seems to be rather fragile in this regard
* when done from any hook callback.
*/
if (uIntNo == 2)
pThis->enmExcpPending = PSPCOREEXCP_SWI;
else if (uIntNo == 13)
pThis->enmExcpPending = PSPCOREEXCP_SMC;
uc_emu_stop(pUcEngine);
}
/**
* The CP write wrapper to keep track of the MMU status.
*
* @returns nothing.
* @param pUcEngine Pointer to the unicorn engine instance.
* @param uAddrPc The PC causing the write.
* @param uCp Co-Processor being accessed.
* @param uCrn cr<n> value.
* @param uCrm cr<m> value.
* @param uOpc0 Opcode 0.
* @param uOpc1 Opcode 1.
* @param uOpc2 Opcode 2.
* @param u64Val The value being written.
* @param pvUser Opaque user data passed when adding the hook.
*/
static bool pspEmuCoreCpWriteWrapper(struct uc_struct *pUcEngine, uint64_t uAddrPc, uint32_t uCp, uint32_t uCrn, uint32_t uCrm,
uint32_t uOpc0, uint32_t uOpc1, uint32_t uOpc2, uint64_t u64Val, void *pvUser)
{
PPSPCOREINT pThis = (PPSPCOREINT)pvUser;
PPSPCORECPBANK pCpBank = pspEmuCoreCpGetBank(pThis);
/*
* Check whether the MMU status changed and cause the emulation to stop so we
* can adjust the memory layout.
*/
bool fHandled = true;
if ( uCp == 15
&& uCrn == 1
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 0)
{
if ((pCpBank->u32RegSctrl & BIT(0)) != (u64Val & BIT(0)))
{
pThis->fMmuChanged = true;
uc_emu_stop(pUcEngine);
}
/* Store a copy of the SCTRL register. */
pCpBank->u32RegSctrl = (uint32_t)u64Val;
fHandled = false; /* To sync unicorns own copy. */
}
else if ( uCp == 15
&& uCrn == 2
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 0)
pCpBank->u32RegTtbr0 = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 2
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 2)
pCpBank->u32RegTtbcr = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 12
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 0)
pCpBank->u32RegVBar = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 12
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 1)
pCpBank->u32RegMVBar = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 7
&& uCrm == 4
&& uOpc1 == 0
&& uOpc2 == 0)
pCpBank->u32RegPa = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 7
&& uCrm == 8
&& uOpc1 == 0
&& uOpc2 == 0)
{
/* V2PCWPR, Privileged Read VA to PA translation */
PSPPADDR PspPAddrPg = 0;
size_t cbRegion = 0;
int rc = pspEmuCoreMmuPAddrQueryFromVAddr(pThis, (PSPVADDR)u64Val, &PspPAddrPg, &cbRegion, NULL /*penmPgTblWalk*/);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_DEBUG, PSPTRACEEVTORIGIN_CORE,
"pspEmuCoreMmuPAddrQueryFromVAddr(): VAddr=%#lx rc=%d PAddr=%#lx\n",
(PSPVADDR)u64Val, rc, PspPAddrPg);
if (!rc)
pCpBank->u32RegPa = PspPAddrPg;
else
pCpBank->u32RegPa = 0x1;
}
else if ( uCp == 15
&& uCrn == 7
&& uCrm == 8
&& uOpc1 == 0
&& uOpc2 == 2)
{
/* V2PCWUR, User Read VA to PA translation */
PSPPADDR PspPAddrPg = 0;
size_t cbRegion = 0;
int rc = pspEmuCoreMmuPAddrQueryFromVAddr(pThis, (PSPVADDR)u64Val, &PspPAddrPg, &cbRegion, NULL /*penmPgTblWalk*/);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_DEBUG, PSPTRACEEVTORIGIN_CORE,
"pspEmuCoreMmuPAddrQueryFromVAddr(): VAddr=%#lx rc=%d PAddr=%#lx\n",
(PSPVADDR)u64Val, rc, PspPAddrPg);
if (!rc)
pCpBank->u32RegPa = PspPAddrPg;
else
pCpBank->u32RegPa = 0x1;
}
else if ( uCp == 15
&& uCrn == 1
&& uCrm == 1
&& uOpc1 == 0
&& uOpc2 == 0)
{
/* Check for a world switch and reset the MMU. */
if ( (pThis->Cp15.u32RegScr & BIT(0)) != (u64Val & BIT(0))
&& pThis->enmCoreMode != PSPCOREMODE_MON)
{
pThis->fMmuChanged = true;
uc_emu_stop(pUcEngine);
}
pThis->Cp15.u32RegScr = (uint32_t)u64Val;
}
else if ( uCp == 15
&& uCrn == 5
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 0)
pCpBank->u32RegDfsr = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 5
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 1)
pCpBank->u32RegIfsr = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 6
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 0)
pCpBank->u32RegDfar = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 6
&& uCrm == 0
&& uOpc1 == 0
&& uOpc2 == 0)
pCpBank->u32RegIfar = (uint32_t)u64Val;
else if ( uCp == 15
&& uCrn == 13
&& uCrm == 0