/
PPUModule.cpp
2492 lines (2040 loc) · 66.9 KB
/
PPUModule.cpp
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
#include "stdafx.h"
#include "Emu/Cell/PPUModule.h"
#include "Utilities/bin_patch.h"
#include "Utilities/StrUtil.h"
#include "Utilities/address_range.h"
#include "util/serialization.hpp"
#include "Crypto/sha1.h"
#include "Crypto/unself.h"
#include "Loader/ELF.h"
#include "Emu/System.h"
#include "Emu/VFS.h"
#include "Emu/Cell/PPUOpcodes.h"
#include "Emu/Cell/PPUAnalyser.h"
#include "Emu/Cell/lv2/sys_process.h"
#include "Emu/Cell/lv2/sys_prx.h"
#include "Emu/Cell/lv2/sys_memory.h"
#include "Emu/Cell/lv2/sys_overlay.h"
#include "Emu/Cell/Modules/StaticHLE.h"
#include <map>
#include <set>
#include <algorithm>
#include "util/asm.hpp"
LOG_CHANNEL(ppu_loader);
extern std::string ppu_get_function_name(const std::string& _module, u32 fnid);
extern std::string ppu_get_variable_name(const std::string& _module, u32 vnid);
extern void ppu_register_range(u32 addr, u32 size);
extern void ppu_register_function_at(u32 addr, u32 size, ppu_intrp_func_t ptr);
extern void sys_initialize_tls(ppu_thread&, u64, u32, u32, u32);
// HLE function name cache
std::vector<std::string> g_ppu_function_names;
atomic_t<u32> liblv2_begin = 0, liblv2_end = 0;
extern u32 ppu_generate_id(std::string_view name)
{
// Symbol name suffix
constexpr auto suffix = "\x67\x59\x65\x99\x04\x25\x04\x90\x56\x64\x27\x49\x94\x89\x74\x1A"sv;
sha1_context ctx;
u8 output[20];
// Compute SHA-1 hash
sha1_starts(&ctx);
sha1_update(&ctx, reinterpret_cast<const u8*>(name.data()), name.size());
sha1_update(&ctx, reinterpret_cast<const u8*>(suffix.data()), suffix.size());
sha1_finish(&ctx, output);
le_t<u32> result = 0;
std::memcpy(&result, output, sizeof(result));
return result;
}
ppu_static_module::ppu_static_module(const char* name)
: name(name)
{
ppu_module_manager::register_module(this);
}
void ppu_static_module::add_init_func(void(*func)(ppu_static_module*))
{
m_on_init.emplace_back(func);
}
void ppu_static_module::initialize()
{
for (auto func : m_on_init)
{
func(this);
}
}
void ppu_module_manager::register_module(ppu_static_module* _module)
{
ppu_module_manager::s_module_map.emplace(_module->name, _module);
}
ppu_static_function& ppu_module_manager::access_static_function(const char* _module, u32 fnid)
{
auto& res = ppu_module_manager::s_module_map.at(_module)->functions[fnid];
if (res.name)
{
fmt::throw_exception("PPU FNID duplication in module %s (%s, 0x%x)", _module, res.name, fnid);
}
return res;
}
ppu_static_variable& ppu_module_manager::access_static_variable(const char* _module, u32 vnid)
{
auto& res = ppu_module_manager::s_module_map.at(_module)->variables[vnid];
if (res.name)
{
fmt::throw_exception("PPU VNID duplication in module %s (%s, 0x%x)", _module, res.name, vnid);
}
return res;
}
const ppu_static_module* ppu_module_manager::get_module(const std::string& name)
{
const auto& map = ppu_module_manager::s_module_map;
const auto found = map.find(name);
return found != map.end() ? found->second : nullptr;
}
void ppu_module_manager::initialize_modules()
{
for (auto& _module : s_module_map)
{
_module.second->initialize();
}
}
// Global linkage information
struct ppu_linkage_info
{
ppu_linkage_info() = default;
ppu_linkage_info(const ppu_linkage_info&) = delete;
ppu_linkage_info& operator=(const ppu_linkage_info&) = delete;
struct module_data
{
struct info
{
ppu_static_function* static_func = nullptr;
ppu_static_variable* static_var = nullptr;
u32 export_addr = 0;
std::set<u32> imports{};
std::set<u32> frefss{};
};
// FNID -> (export; [imports...])
std::unordered_map<u32, info, value_hash<u32>> functions{};
std::unordered_map<u32, info, value_hash<u32>> variables{};
// Obsolete
bool imported = false;
};
// Module map
std::unordered_map<std::string, module_data> modules{};
};
// Initialize static modules.
static void ppu_initialize_modules(ppu_linkage_info* link, utils::serial* ar = nullptr)
{
if (!link->modules.empty())
{
return;
}
ppu_module_manager::initialize_modules();
const std::initializer_list<const ppu_static_module*> registered
{
&ppu_module_manager::cellAdec,
&ppu_module_manager::cellAtrac,
&ppu_module_manager::cellAtracMulti,
&ppu_module_manager::cellAudio,
&ppu_module_manager::cellAvconfExt,
&ppu_module_manager::cellAuthDialogUtility,
&ppu_module_manager::cellBGDL,
&ppu_module_manager::cellCamera,
&ppu_module_manager::cellCelp8Enc,
&ppu_module_manager::cellCelpEnc,
&ppu_module_manager::cellCrossController,
&ppu_module_manager::cellDaisy,
&ppu_module_manager::cellDmux,
&ppu_module_manager::cellDtcpIpUtility,
&ppu_module_manager::cellFiber,
&ppu_module_manager::cellFont,
&ppu_module_manager::cellFontFT,
&ppu_module_manager::cell_FreeType2,
&ppu_module_manager::cellFs,
&ppu_module_manager::cellGame,
&ppu_module_manager::cellGameExec,
&ppu_module_manager::cellGcmSys,
&ppu_module_manager::cellGem,
&ppu_module_manager::cellGifDec,
&ppu_module_manager::cellHttp,
&ppu_module_manager::cellHttps,
&ppu_module_manager::cellHttpUtil,
&ppu_module_manager::cellImeJp,
&ppu_module_manager::cellJpgDec,
&ppu_module_manager::cellJpgEnc,
&ppu_module_manager::cellKey2char,
&ppu_module_manager::cellL10n,
&ppu_module_manager::cellLibprof,
&ppu_module_manager::cellMic,
&ppu_module_manager::cellMusic,
&ppu_module_manager::cellMusicDecode,
&ppu_module_manager::cellMusicExport,
&ppu_module_manager::cellNetAoi,
&ppu_module_manager::cellNetCtl,
&ppu_module_manager::cellOskDialog,
&ppu_module_manager::cellOvis,
&ppu_module_manager::cellPamf,
&ppu_module_manager::cellPesmUtility,
&ppu_module_manager::cellPhotoDecode,
&ppu_module_manager::cellPhotoExport,
&ppu_module_manager::cellPhotoImportUtil,
&ppu_module_manager::cellPngDec,
&ppu_module_manager::cellPngEnc,
&ppu_module_manager::cellPrint,
&ppu_module_manager::cellRec,
&ppu_module_manager::cellRemotePlay,
&ppu_module_manager::cellResc,
&ppu_module_manager::cellRtc,
&ppu_module_manager::cellRtcAlarm,
&ppu_module_manager::cellRudp,
&ppu_module_manager::cellSail,
&ppu_module_manager::cellSailRec,
&ppu_module_manager::cellSaveData,
&ppu_module_manager::cellMinisSaveData,
&ppu_module_manager::cellScreenShot,
&ppu_module_manager::cellSearch,
&ppu_module_manager::cellSheap,
&ppu_module_manager::cellSpudll,
&ppu_module_manager::cellSpurs,
&ppu_module_manager::cellSpursJq,
&ppu_module_manager::cellSsl,
&ppu_module_manager::cellSubDisplay,
&ppu_module_manager::cellSync,
&ppu_module_manager::cellSync2,
&ppu_module_manager::cellSysconf,
&ppu_module_manager::cellSysmodule,
&ppu_module_manager::cellSysutil,
&ppu_module_manager::cellSysutilAp,
&ppu_module_manager::cellSysutilAvc2,
&ppu_module_manager::cellSysutilAvcExt,
&ppu_module_manager::cellSysutilNpEula,
&ppu_module_manager::cellSysutilMisc,
&ppu_module_manager::cellUsbd,
&ppu_module_manager::cellUsbPspcm,
&ppu_module_manager::cellUserInfo,
&ppu_module_manager::cellVdec,
&ppu_module_manager::cellVideoExport,
&ppu_module_manager::cellVideoPlayerUtility,
&ppu_module_manager::cellVideoUpload,
&ppu_module_manager::cellVoice,
&ppu_module_manager::cellVpost,
&ppu_module_manager::libad_async,
&ppu_module_manager::libad_core,
&ppu_module_manager::libfs_utility_init,
&ppu_module_manager::libmedi,
&ppu_module_manager::libmixer,
&ppu_module_manager::libsnd3,
&ppu_module_manager::libsynth2,
&ppu_module_manager::sceNp,
&ppu_module_manager::sceNp2,
&ppu_module_manager::sceNpClans,
&ppu_module_manager::sceNpCommerce2,
&ppu_module_manager::sceNpMatchingInt,
&ppu_module_manager::sceNpSns,
&ppu_module_manager::sceNpTrophy,
&ppu_module_manager::sceNpTus,
&ppu_module_manager::sceNpUtil,
&ppu_module_manager::sys_crashdump,
&ppu_module_manager::sys_io,
&ppu_module_manager::sys_net,
&ppu_module_manager::sysPrxForUser,
&ppu_module_manager::sys_libc,
&ppu_module_manager::sys_lv2dbg,
&ppu_module_manager::static_hle,
};
// Initialize double-purpose fake OPD array for HLE functions
const auto& hle_funcs = ppu_function_manager::get(g_cfg.core.ppu_decoder != ppu_decoder_type::_static);
u32& hle_funcs_addr = g_fxo->get<ppu_function_manager>().addr;
// Allocate memory for the array (must be called after fixed allocations)
if (!hle_funcs_addr)
hle_funcs_addr = vm::alloc(::size32(hle_funcs) * 8, vm::main);
else
vm::page_protect(hle_funcs_addr, utils::align(::size32(hle_funcs) * 8, 0x1000), 0, vm::page_writable);
// Initialize as PPU executable code
ppu_register_range(hle_funcs_addr, ::size32(hle_funcs) * 8);
// Fill the array (visible data: self address and function index)
for (u32 addr = hle_funcs_addr, index = 0; index < hle_funcs.size(); addr += 8, index++)
{
// Function address = next CIA, RTOC = 0 (vm::null)
vm::write32(addr + 0, addr + 4);
vm::write32(addr + 4, 0);
// Register the HLE function directly
ppu_register_function_at(addr + 0, 4, nullptr);
ppu_register_function_at(addr + 4, 4, hle_funcs[index]);
}
// Set memory protection to read-only
vm::page_protect(hle_funcs_addr, utils::align(::size32(hle_funcs) * 8, 0x1000), 0, 0, vm::page_writable);
// Initialize function names
const bool is_first = g_ppu_function_names.empty();
if (is_first)
{
g_ppu_function_names.resize(hle_funcs.size());
g_ppu_function_names[0] = "INVALID";
g_ppu_function_names[1] = "HLE RETURN";
}
// For HLE variable allocation
u32 alloc_addr = 0;
// "Use" all the modules for correct linkage
for (auto& _module : registered)
{
ppu_loader.trace("Registered static module: %s", _module->name);
}
struct hle_vars_save
{
hle_vars_save() = default;
hle_vars_save(const hle_vars_save&) = delete;
hle_vars_save& operator =(const hle_vars_save&) = delete;
hle_vars_save(utils::serial& ar)
{
auto& manager = ppu_module_manager::get();
while (true)
{
const std::string name = ar.operator std::string();
if (name.empty())
{
// Null termination
break;
}
const auto _module = manager.at(name);
auto& variable = _module->variables;
for (u32 i = 0, end = ar.operator usz(); i < end; i++)
{
auto* ptr = &variable.at(ar.operator u32());
ptr->addr = ar.operator u32();
ensure(!!ptr->var);
}
}
}
void save(utils::serial& ar)
{
for (auto& pair : ppu_module_manager::get())
{
const auto _module = pair.second;
if (_module->variables.empty())
{
continue;
}
ar(_module->name);
ar(_module->variables.size());
for (auto& variable : _module->variables)
{
ar(variable.first, variable.second.addr);
}
}
// Null terminator
ar(std::string{});
}
};
if (ar)
{
g_fxo->init<hle_vars_save>(*ar);
}
else
{
g_fxo->init<hle_vars_save>();
}
for (auto& pair : ppu_module_manager::get())
{
const auto _module = pair.second;
auto& linkage = link->modules[_module->name];
for (auto& function : _module->functions)
{
ppu_loader.trace("** 0x%08X: %s", function.first, function.second.name);
if (is_first)
{
g_ppu_function_names[function.second.index] = fmt::format("%s:%s", function.second.name, _module->name);
}
auto& flink = linkage.functions[function.first];
flink.static_func = &function.second;
flink.export_addr = g_fxo->get<ppu_function_manager>().func_addr(function.second.index);
function.second.export_addr = &flink.export_addr;
}
for (auto& variable : _module->variables)
{
ppu_loader.trace("** &0x%08X: %s (size=0x%x, align=0x%x)", variable.first, variable.second.name, variable.second.size, variable.second.align);
// Allocate HLE variable
if (ar)
{
// Already loaded
}
else if (variable.second.size >= 0x10000 || variable.second.align >= 0x10000)
{
variable.second.addr = vm::alloc(variable.second.size, vm::main, std::max<u32>(variable.second.align, 0x10000));
}
else
{
const u32 next = utils::align(alloc_addr, variable.second.align);
const u32 end = next + variable.second.size - 1;
if (!next || (end >> 16 != alloc_addr >> 16))
{
alloc_addr = vm::alloc(0x10000, vm::main);
}
else
{
alloc_addr = next;
}
variable.second.addr = alloc_addr;
alloc_addr += variable.second.size;
}
*variable.second.var = variable.second.addr;
ppu_loader.trace("Allocated HLE variable %s.%s at 0x%x", _module->name, variable.second.name, *variable.second.var);
// Initialize HLE variable
if (variable.second.init)
{
variable.second.init();
}
if ((variable.second.flags & MFF_HIDDEN) == 0)
{
auto& vlink = linkage.variables[variable.first];
vlink.static_var = &variable.second;
vlink.export_addr = variable.second.addr;
variable.second.export_addr = &vlink.export_addr;
}
}
}
}
// For the debugger (g_ppu_function_names shouldn't change, string_view should suffice)
extern const std::unordered_map<u32, std::string_view>& get_exported_function_names_as_addr_indexed_map()
{
struct info_t
{
std::unordered_map<u32, std::string_view> res;
u64 update_time = 0;
};
static thread_local std::unique_ptr<info_t> info;
if (!info)
{
info = std::make_unique<info_t>();
info->res.reserve(ppu_module_manager::get().size());
}
auto& [res, update_time] = *info;
const auto link = g_fxo->try_get<ppu_linkage_info>();
const auto hle_funcs = g_fxo->try_get<ppu_function_manager>();
if (!link || !hle_funcs)
{
res.clear();
return res;
}
const u64 current_time = get_system_time();
// Update list every >=0.1 seconds
if (current_time - update_time < 100'000)
{
return res;
}
update_time = current_time;
res.clear();
for (auto& pair : ppu_module_manager::get())
{
const auto _module = pair.second;
auto& linkage = link->modules[_module->name];
for (auto& function : _module->functions)
{
auto& flink = linkage.functions[function.first];
u32 addr = flink.export_addr;
if (vm::check_addr<4>(addr, vm::page_readable) && addr != hle_funcs->func_addr(function.second.index))
{
addr = vm::read32(addr);
if (!(addr % 4) && vm::check_addr<4>(addr, vm::page_executable))
{
res.try_emplace(addr, g_ppu_function_names[function.second.index]);
}
}
}
}
return res;
}
// Resolve relocations for variable/function linkage.
static void ppu_patch_refs(std::vector<ppu_reloc>* out_relocs, u32 fref, u32 faddr)
{
struct ref_t
{
be_t<u32> type;
be_t<u32> addr;
be_t<u32> addend; // Note: Treating it as addend seems to be correct for now, but still unknown if theres more in this variable
};
for (auto ref = vm::ptr<ref_t>::make(fref); ref->type; ref++)
{
if (ref->addend) ppu_loader.warning("**** REF(%u): Addend value(0x%x, 0x%x)", ref->type, ref->addr, ref->addend);
const u32 raddr = ref->addr;
const u32 rtype = ref->type;
const u32 rdata = faddr + ref->addend;
if (out_relocs)
{
// Register relocation with unpredictable target (data=0)
ppu_reloc _rel;
_rel.addr = raddr;
_rel.type = rtype;
_rel.data = 0;
out_relocs->emplace_back(_rel);
}
// OPs must be similar to relocations
switch (rtype)
{
case 1:
{
const u32 value = vm::_ref<u32>(ref->addr) = rdata;
ppu_loader.trace("**** REF(1): 0x%x <- 0x%x", ref->addr, value);
break;
}
case 4:
{
const u16 value = vm::_ref<u16>(ref->addr) = static_cast<u16>(rdata);
ppu_loader.trace("**** REF(4): 0x%x <- 0x%04x (0x%llx)", ref->addr, value, faddr);
break;
}
case 6:
{
const u16 value = vm::_ref<u16>(ref->addr) = static_cast<u16>(rdata >> 16) + (rdata & 0x8000 ? 1 : 0);
ppu_loader.trace("**** REF(6): 0x%x <- 0x%04x (0x%llx)", ref->addr, value, faddr);
break;
}
case 57:
{
const u16 value = vm::_ref<ppu_bf_t<be_t<u16>, 0, 14>>(ref->addr) = static_cast<u16>(rdata) >> 2;
ppu_loader.trace("**** REF(57): 0x%x <- 0x%04x (0x%llx)", ref->addr, value, faddr);
break;
}
default: ppu_loader.error("**** REF(%u): Unknown/Illegal type (0x%x, 0x%x)", rtype, raddr, ref->addend);
}
}
}
// Export or import module struct
struct ppu_prx_module_info
{
u8 size;
u8 unk0;
be_t<u16> version;
be_t<u16> attributes;
be_t<u16> num_func;
be_t<u16> num_var;
be_t<u16> num_tlsvar;
u8 info_hash;
u8 info_tlshash;
u8 unk1[2];
vm::bcptr<char> name;
vm::bcptr<u32> nids; // Imported FNIDs, Exported NIDs
vm::bptr<u32> addrs;
vm::bcptr<u32> vnids; // Imported VNIDs
vm::bcptr<u32> vstubs;
be_t<u32> unk4;
be_t<u32> unk5;
};
bool ppu_form_branch_to_code(u32 entry, u32 target);
extern u32 ppu_get_exported_func_addr(u32 fnid, const std::string& module_name)
{
return g_fxo->get<ppu_linkage_info>().modules[module_name].functions[fnid].export_addr;
}
// Load and register exports; return special exports found (nameless module)
static auto ppu_load_exports(ppu_linkage_info* link, u32 exports_start, u32 exports_end)
{
std::unordered_map<u32, u32> result;
for (u32 addr = exports_start; addr < exports_end;)
{
const auto& lib = vm::_ref<const ppu_prx_module_info>(addr);
if (!lib.name)
{
// Set special exports
for (u32 i = 0, end = lib.num_func + lib.num_var; i < end; i++)
{
const u32 nid = lib.nids[i];
const u32 addr = lib.addrs[i];
if (i < lib.num_func)
{
ppu_loader.notice("** Special: [%s] at 0x%x [0x%x, 0x%x]", ppu_get_function_name({}, nid), addr, vm::_ref<u32>(addr), vm::_ref<u32>(addr + 4));
}
else
{
ppu_loader.notice("** Special: &[%s] at 0x%x", ppu_get_variable_name({}, nid), addr);
}
result.emplace(nid, addr);
}
addr += lib.size ? lib.size : sizeof(ppu_prx_module_info);
continue;
}
const std::string module_name(lib.name.get_ptr());
ppu_loader.notice("** Exported module '%s' (0x%x, 0x%x, 0x%x, 0x%x)", module_name, lib.vnids, lib.vstubs, lib.unk4, lib.unk5);
if (lib.num_tlsvar)
{
ppu_loader.fatal("Unexpected num_tlsvar (%u)!", lib.num_tlsvar);
}
// Static module
const auto _sm = ppu_module_manager::get_module(module_name);
// Module linkage
auto& mlink = link->modules[module_name];
const auto fnids = +lib.nids;
const auto faddrs = +lib.addrs;
// Get functions
for (u32 i = 0, end = lib.num_func; i < end; i++)
{
const u32 fnid = fnids[i];
const u32 faddr = faddrs[i];
ppu_loader.notice("**** %s export: [%s] (0x%08x) at 0x%x [at:0x%x]", module_name, ppu_get_function_name(module_name, fnid), fnid, faddr, vm::read32(faddr));
// Function linkage info
auto& flink = mlink.functions[fnid];
if (flink.static_func && flink.export_addr == g_fxo->get<ppu_function_manager>().func_addr(flink.static_func->index))
{
flink.export_addr = 0;
}
if (flink.export_addr)
{
ppu_loader.error("Already linked function '%s' in module '%s'", ppu_get_function_name(module_name, fnid), module_name);
}
//else
{
// Static function
const auto _sf = _sm && _sm->functions.count(fnid) ? &_sm->functions.at(fnid) : nullptr;
if (_sf && (_sf->flags & MFF_FORCED_HLE))
{
// Inject a branch to the HLE implementation
const u32 target = g_fxo->get<ppu_function_manager>().func_addr(_sf->index) + 4;
// Set exported function
flink.export_addr = target - 4;
ppu_form_branch_to_code(vm::read32(faddr), target);
}
else
{
// Set exported function
flink.export_addr = faddr;
// Fix imports
for (const u32 addr : flink.imports)
{
vm::write32(addr, faddr);
//ppu_loader.warning("Exported function '%s' in module '%s'", ppu_get_function_name(module_name, fnid), module_name);
}
for (const u32 fref : flink.frefss)
{
ppu_patch_refs(nullptr, fref, faddr);
}
}
}
}
const auto vnids = lib.nids + lib.num_func;
const auto vaddrs = lib.addrs + lib.num_func;
// Get variables
for (u32 i = 0, end = lib.num_var; i < end; i++)
{
const u32 vnid = vnids[i];
const u32 vaddr = vaddrs[i];
ppu_loader.notice("**** %s export: &[%s] at 0x%x", module_name, ppu_get_variable_name(module_name, vnid), vaddr);
// Variable linkage info
auto& vlink = mlink.variables[vnid];
if (vlink.static_var && vlink.export_addr == vlink.static_var->addr)
{
vlink.export_addr = 0;
}
if (vlink.export_addr)
{
ppu_loader.error("Already linked variable '%s' in module '%s'", ppu_get_variable_name(module_name, vnid), module_name);
}
//else
{
// Set exported variable
vlink.export_addr = vaddr;
// Fix imports
for (const auto vref : vlink.imports)
{
ppu_patch_refs(nullptr, vref, vaddr);
//ppu_loader.warning("Exported variable '%s' in module '%s'", ppu_get_variable_name(module_name, vnid), module_name);
}
}
}
addr += lib.size ? lib.size : sizeof(ppu_prx_module_info);
}
return result;
}
static auto ppu_load_imports(std::vector<ppu_reloc>& relocs, ppu_linkage_info* link, u32 imports_start, u32 imports_end)
{
std::unordered_map<u32, void*> result;
for (u32 addr = imports_start; addr < imports_end;)
{
const auto& lib = vm::_ref<const ppu_prx_module_info>(addr);
const std::string module_name(lib.name.get_ptr());
ppu_loader.notice("** Imported module '%s' (ver=0x%x, attr=0x%x, 0x%x, 0x%x) [0x%x]", module_name, lib.version, lib.attributes, lib.unk4, lib.unk5, addr);
if (lib.num_tlsvar)
{
ppu_loader.fatal("Unexpected num_tlsvar (%u)!", lib.num_tlsvar);
}
// Static module
//const auto _sm = ppu_module_manager::get_module(module_name);
// Module linkage
auto& mlink = link->modules[module_name];
const auto fnids = +lib.nids;
const auto faddrs = +lib.addrs;
for (u32 i = 0, end = lib.num_func; i < end; i++)
{
const u32 fnid = fnids[i];
const u32 fstub = faddrs[i];
const u32 faddr = (faddrs + i).addr();
ppu_loader.notice("**** %s import: [%s] (0x%08x) -> 0x%x", module_name, ppu_get_function_name(module_name, fnid), fnid, fstub);
// Function linkage info
auto& flink = link->modules[module_name].functions[fnid];
// Add new import
result.emplace(faddr, &flink);
flink.imports.emplace(faddr);
mlink.imported = true;
// Link address (special HLE function by default)
const u32 link_addr = flink.export_addr ? flink.export_addr : g_fxo->get<ppu_function_manager>().addr;
// Write import table
vm::write32(faddr, link_addr);
// Patch refs if necessary (0x2000 seems to be correct flag indicating the presence of additional info)
if (const u32 frefs = (lib.attributes & 0x2000) ? +fnids[i + lib.num_func] : 0)
{
result.emplace(frefs, &flink);
flink.frefss.emplace(frefs);
ppu_patch_refs(&relocs, frefs, link_addr);
}
//ppu_loader.warning("Imported function '%s' in module '%s' (0x%x)", ppu_get_function_name(module_name, fnid), module_name, faddr);
}
const auto vnids = +lib.vnids;
const auto vstubs = +lib.vstubs;
for (u32 i = 0, end = lib.num_var; i < end; i++)
{
const u32 vnid = vnids[i];
const u32 vref = vstubs[i];
ppu_loader.notice("**** %s import: &[%s] (ref=*0x%x)", module_name, ppu_get_variable_name(module_name, vnid), vref);
// Variable linkage info
auto& vlink = link->modules[module_name].variables[vnid];
// Add new import
result.emplace(vref, &vlink);
vlink.imports.emplace(vref);
mlink.imported = true;
// Link if available
ppu_patch_refs(&relocs, vref, vlink.export_addr);
//ppu_loader.warning("Imported variable '%s' in module '%s' (0x%x)", ppu_get_variable_name(module_name, vnid), module_name, vlink.first);
}
addr += lib.size ? lib.size : sizeof(ppu_prx_module_info);
}
return result;
}
// For _sys_prx_register_module
void ppu_manual_load_imports_exports(u32 imports_start, u32 imports_size, u32 exports_start, u32 exports_size)
{
auto& _main = g_fxo->get<ppu_module>();
auto& link = g_fxo->get<ppu_linkage_info>();
ppu_load_exports(&link, exports_start, exports_start + exports_size);
ppu_load_imports(_main.relocs, &link, imports_start, imports_start + imports_size);
}
// For savestates
extern bool is_memory_compatible_for_copy_from_executable_optimization(u32 addr, u32 size)
{
if (g_cfg.savestate.state_inspection_mode)
{
return false;
}
static ppu_exec_object s_ppu_exec;
static std::vector<char> zeroes;
if (!addr)
{
// A call for cleanup
s_ppu_exec.clear();
zeroes = {};
return false;
}
if (s_ppu_exec != elf_error::ok)
{
if (s_ppu_exec != elf_error::stream)
{
// Failed before
return false;
}
s_ppu_exec.open(decrypt_self(fs::file(Emu.GetBoot()), Emu.klic.empty() ? nullptr : reinterpret_cast<u8*>(&Emu.klic[0])));
if (s_ppu_exec != elf_error::ok)
{
return false;
}
}
for (const auto& prog : s_ppu_exec.progs)
{
const u32 vaddr = static_cast<u32>(prog.p_vaddr);
const u32 seg_size = static_cast<u32>(prog.p_filesz);
const u32 aligned_vaddr = vaddr & -0x10000;
const u32 vaddr_offs = vaddr & 0xffff;
// Check if the address is a start of segment within the executable
if (prog.p_type == 0x1u /* LOAD */ && seg_size && aligned_vaddr == addr && prog.p_vaddr == prog.p_paddr && vaddr_offs + seg_size <= size)
{
zeroes.resize(std::max<usz>({zeroes.size(), usz{addr + size - (vaddr + seg_size)}, usz{vaddr_offs}}));
// Check if gaps between segment and allocation bounds are still zeroes-only
if (!std::memcmp(vm::_ptr<char>(aligned_vaddr), zeroes.data(), vaddr_offs) &&
!std::memcmp(vm::_ptr<char>(vaddr + seg_size), zeroes.data(), (addr + size - (vaddr + seg_size))))
{
// Test memory equality
return !std::memcmp(prog.bin.data(), vm::base(vaddr), seg_size);
}
}
}
return false;
}
void init_ppu_functions(utils::serial* ar, bool full = false)
{
g_fxo->need<ppu_linkage_info>();
if (ar)
{
ensure(vm::check_addr(g_fxo->init<ppu_function_manager>(*ar)->addr));
}
else
g_fxo->init<ppu_function_manager>();
if (full)
{
ensure(ar);
// Initialize HLE modules
ppu_initialize_modules(&g_fxo->get<ppu_linkage_info>(), ar);
}
}
static void ppu_check_patch_spu_images(const ppu_segment& seg)
{
const std::string_view seg_view{vm::get_super_ptr<char>(seg.addr), seg.size};
for (usz i = seg_view.find("\177ELF"); i < seg.size; i = seg_view.find("\177ELF", i + 4))
{
const auto elf_header = vm::get_super_ptr<u8>(seg.addr + i);
// Try to load SPU image
const spu_exec_object obj(fs::file(elf_header, seg.size - i));
if (obj != elf_error::ok)
{
// This address does not have an SPU elf
continue;
}
// Segment info dump
std::string name;
std::string dump;
std::basic_string<u32> applied;
// Executable hash
sha1_context sha2;
sha1_starts(&sha2);
u8 sha1_hash[20];
for (const auto& prog : obj.progs)
{
// Only hash the data, we are not loading it
sha1_update(&sha2, reinterpret_cast<const uchar*>(&prog.p_vaddr), sizeof(prog.p_vaddr));
sha1_update(&sha2, reinterpret_cast<const uchar*>(&prog.p_memsz), sizeof(prog.p_memsz));
sha1_update(&sha2, reinterpret_cast<const uchar*>(&prog.p_filesz), sizeof(prog.p_filesz));
fmt::append(dump, "\n\tSegment: p_type=0x%x, p_vaddr=0x%llx, p_filesz=0x%llx, p_memsz=0x%llx, p_offset=0x%llx", prog.p_type, prog.p_vaddr, prog.p_filesz, prog.p_memsz, prog.p_offset);
if (prog.p_type == 0x1u /* LOAD */ && prog.p_filesz > 0u)
{
sha1_update(&sha2, (elf_header + prog.p_offset), prog.p_filesz);
}
else if (prog.p_type == 0x4u /* NOTE */ && prog.p_filesz > 0u)
{
sha1_update(&sha2, (elf_header + prog.p_offset), prog.p_filesz);
// We assume that the string SPUNAME exists 0x14 bytes into the NOTE segment
name = reinterpret_cast<char*>(elf_header + prog.p_offset + 0x14);