forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_spu.cpp
2185 lines (1647 loc) · 48.1 KB
/
sys_spu.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 "sys_spu.h"
#include "Emu/System.h"
#include "Emu/VFS.h"
#include "Emu/IdManager.h"
#include "Crypto/unself.h"
#include "Crypto/unedat.h"
#include "Crypto/sha1.h"
#include "Loader/ELF.h"
#include "Utilities/bin_patch.h"
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/PPUModule.h"
#include "Emu/Cell/RawSPUThread.h"
#include "sys_interrupt.h"
#include "sys_process.h"
#include "sys_memory.h"
#include "sys_mmapper.h"
#include "sys_event.h"
#include "sys_fs.h"
LOG_CHANNEL(sys_spu);
extern u64 get_timebased_time();
template <>
void fmt_class_string<spu_group_status>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](spu_group_status value)
{
switch (value)
{
case SPU_THREAD_GROUP_STATUS_NOT_INITIALIZED: return "uninitialized";
case SPU_THREAD_GROUP_STATUS_INITIALIZED: return "initialized";
case SPU_THREAD_GROUP_STATUS_READY: return "ready";
case SPU_THREAD_GROUP_STATUS_WAITING: return "waiting";
case SPU_THREAD_GROUP_STATUS_SUSPENDED: return "suspended";
case SPU_THREAD_GROUP_STATUS_WAITING_AND_SUSPENDED: return "waiting and suspended";
case SPU_THREAD_GROUP_STATUS_RUNNING: return "running";
case SPU_THREAD_GROUP_STATUS_STOPPED: return "stopped";
case SPU_THREAD_GROUP_STATUS_UNKNOWN: break;
}
return unknown;
});
}
void sys_spu_image::load(const fs::file& stream)
{
const spu_exec_object obj{stream, 0, elf_opt::no_sections + elf_opt::no_data};
if (obj != elf_error::ok)
{
fmt::throw_exception("Failed to load SPU image: %s" HERE, obj.get_error());
}
for (const auto& shdr : obj.shdrs)
{
spu_log.notice("** Section: sh_type=0x%x, addr=0x%llx, size=0x%llx, flags=0x%x", shdr.sh_type, shdr.sh_addr, shdr.sh_size, shdr.sh_flags);
}
for (const auto& prog : obj.progs)
{
spu_log.notice("** Segment: p_type=0x%x, p_vaddr=0x%llx, p_filesz=0x%llx, p_memsz=0x%llx, flags=0x%x", prog.p_type, prog.p_vaddr, prog.p_filesz, prog.p_memsz, prog.p_flags);
if (prog.p_type != u32{SYS_SPU_SEGMENT_TYPE_COPY} && prog.p_type != u32{SYS_SPU_SEGMENT_TYPE_INFO})
{
spu_log.error("Unknown program type (0x%x)", prog.p_type);
}
}
this->type = SYS_SPU_IMAGE_TYPE_KERNEL;
const s32 nsegs = sys_spu_image::get_nsegs(obj.progs);
const u32 mem_size = nsegs * sizeof(sys_spu_segment) + ::size32(stream);
const vm::ptr<sys_spu_segment> segs = vm::cast(vm::alloc(mem_size, vm::main));
const u32 entry = obj.header.e_entry;
const u32 src = (segs + nsegs).addr();
stream.seek(0);
stream.read(vm::base(src), stream.size());
if (nsegs <= 0 || nsegs > 0x20 || sys_spu_image::fill(segs, nsegs, obj.progs, src) != nsegs)
{
fmt::throw_exception("Failed to load SPU segments (%d)" HERE, nsegs);
}
// Write ID and save entry
this->entry_point = idm::make<lv2_obj, lv2_spu_image>(+obj.header.e_entry, segs, nsegs);
// Unused and set to 0
this->nsegs = 0;
this->segs = vm::null;
vm::page_protect(segs.addr(), ::align(mem_size, 4096), 0, 0, vm::page_writable);
}
void sys_spu_image::free()
{
if (type == SYS_SPU_IMAGE_TYPE_KERNEL)
{
vm::dealloc_verbose_nothrow(segs.addr(), vm::main);
}
}
void sys_spu_image::deploy(u32 loc, sys_spu_segment* segs, u32 nsegs)
{
// Segment info dump
std::string dump;
// Executable hash
sha1_context sha;
sha1_starts(&sha);
u8 sha1_hash[20];
for (u32 i = 0; i < nsegs; i++)
{
auto& seg = segs[i];
fmt::append(dump, "\n\t[%d] t=0x%x, ls=0x%x, size=0x%x, addr=0x%x", i, seg.type, seg.ls, seg.size, seg.addr);
sha1_update(&sha, reinterpret_cast<uchar*>(&seg.type), sizeof(seg.type));
// Hash big-endian values
if (seg.type == SYS_SPU_SEGMENT_TYPE_COPY)
{
std::memcpy(vm::base(loc + seg.ls), vm::base(seg.addr), seg.size);
sha1_update(&sha, reinterpret_cast<uchar*>(&seg.size), sizeof(seg.size));
sha1_update(&sha, reinterpret_cast<uchar*>(&seg.ls), sizeof(seg.ls));
sha1_update(&sha, vm::_ptr<uchar>(seg.addr), seg.size);
}
else if (seg.type == SYS_SPU_SEGMENT_TYPE_FILL)
{
if ((seg.ls | seg.size) % 4)
{
spu_log.error("Unaligned SPU FILL type segment (ls=0x%x, size=0x%x)", seg.ls, seg.size);
}
std::fill_n(vm::_ptr<u32>(loc + seg.ls), seg.size / 4, seg.addr);
sha1_update(&sha, reinterpret_cast<uchar*>(&seg.size), sizeof(seg.size));
sha1_update(&sha, reinterpret_cast<uchar*>(&seg.ls), sizeof(seg.ls));
sha1_update(&sha, reinterpret_cast<uchar*>(&seg.addr), sizeof(seg.addr));
}
else if (seg.type == SYS_SPU_SEGMENT_TYPE_INFO)
{
const be_t<u32> size = seg.size + 0x14; // Workaround
sha1_update(&sha, reinterpret_cast<const uchar*>(&size), sizeof(size));
}
}
sha1_finish(&sha, sha1_hash);
// Format patch name
std::string hash("SPU-0000000000000000000000000000000000000000");
for (u32 i = 0; i < sizeof(sha1_hash); i++)
{
constexpr auto pal = "0123456789abcdef";
hash[4 + i * 2] = pal[sha1_hash[i] >> 4];
hash[5 + i * 2] = pal[sha1_hash[i] & 15];
}
// Apply the patch
auto applied = g_fxo->get<patch_engine>()->apply(hash, vm::_ptr<u8>(loc));
if (!Emu.GetTitleID().empty())
{
// Alternative patch
applied += g_fxo->get<patch_engine>()->apply(Emu.GetTitleID() + '-' + hash, vm::_ptr<u8>(loc));
}
spu_log.notice("Loaded SPU image: %s (<- %u)%s", hash, applied, dump);
}
// Get spu thread ptr, returns group ptr as well for refcounting
std::pair<named_thread<spu_thread>*, std::shared_ptr<lv2_spu_group>> lv2_spu_group::get_thread(u32 id)
{
if (id >= 0x06000000)
{
// thread index is out of range (5 max)
return {};
}
// Bits 0-23 contain group id (without id base)
decltype(get_thread(0)) res{nullptr, idm::get<lv2_spu_group>((id & 0xFFFFFF) | (lv2_spu_group::id_base & ~0xFFFFFF))};
// Bits 24-31 contain thread index within the group
const u32 index = id >> 24;
if (auto group = res.second.get(); group && group->init > index)
{
res.first = group->threads[index].get();
}
return res;
}
error_code sys_spu_initialize(ppu_thread& ppu, u32 max_usable_spu, u32 max_raw_spu)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_initialize(max_usable_spu=%d, max_raw_spu=%d)", max_usable_spu, max_raw_spu);
if (max_raw_spu > 5)
{
return CELL_EINVAL;
}
return CELL_OK;
}
error_code _sys_spu_image_get_information(ppu_thread& ppu, vm::ptr<sys_spu_image> img, vm::ptr<u32> entry_point, vm::ptr<s32> nsegs)
{
vm::temporary_unlock(ppu);
sys_spu.warning("_sys_spu_image_get_information(img=*0x%x, entry_point=*0x%x, nsegs=*0x%x)", img, entry_point, nsegs);
if (img->type != SYS_SPU_IMAGE_TYPE_KERNEL)
{
return CELL_EINVAL;
}
const auto image = idm::get<lv2_obj, lv2_spu_image>(img->entry_point);
if (!image)
{
return CELL_ESRCH;
}
*entry_point = image->e_entry;
*nsegs = image->nsegs;
return CELL_OK;
}
error_code sys_spu_image_open(ppu_thread& ppu, vm::ptr<sys_spu_image> img, vm::cptr<char> path)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_image_open(img=*0x%x, path=%s)", img, path);
auto [fs_error, ppath, file] = lv2_file::open(path.get_ptr(), 0, 0);
if (fs_error)
{
return {fs_error, path};
}
const fs::file elf_file = decrypt_self(std::move(file), g_fxo->get<loaded_npdrm_keys>()->devKlic.data());
if (!elf_file)
{
sys_spu.error("sys_spu_image_open(): file %s is illegal for SPU image!", path);
return {CELL_ENOEXEC, path};
}
img->load(elf_file);
return CELL_OK;
}
error_code _sys_spu_image_import(ppu_thread& ppu, vm::ptr<sys_spu_image> img, u32 src, u32 size, u32 arg4)
{
vm::temporary_unlock(ppu);
sys_spu.warning("_sys_spu_image_import(img=*0x%x, src=*0x%x, size=0x%x, arg4=0x%x)", img, src, size, arg4);
img->load(fs::file{vm::base(src), size});
return CELL_OK;
}
error_code _sys_spu_image_close(ppu_thread& ppu, vm::ptr<sys_spu_image> img)
{
vm::temporary_unlock(ppu);
sys_spu.warning("_sys_spu_image_close(img=*0x%x)", img);
if (img->type != SYS_SPU_IMAGE_TYPE_KERNEL)
{
return CELL_EINVAL;
}
const auto handle = idm::withdraw<lv2_obj, lv2_spu_image>(img->entry_point);
if (!handle)
{
return CELL_ESRCH;
}
verify(HERE), vm::dealloc(handle->segs.addr(), vm::main);
return CELL_OK;
}
error_code _sys_spu_image_get_segments(ppu_thread& ppu, vm::ptr<sys_spu_image> img, vm::ptr<sys_spu_segment> segments, s32 nseg)
{
vm::temporary_unlock(ppu);
sys_spu.error("_sys_spu_image_get_segments(img=*0x%x, segments=*0x%x, nseg=%d)", img, segments, nseg);
if (nseg <= 0 || nseg > 0x20 || img->type != SYS_SPU_IMAGE_TYPE_KERNEL)
{
return CELL_EINVAL;
}
const auto handle = idm::get<lv2_obj, lv2_spu_image>(img->entry_point);
if (!handle)
{
return CELL_ESRCH;
}
// TODO: apply SPU patches
std::memcpy(segments.get_ptr(), handle->segs.get_ptr(), sizeof(sys_spu_segment) * std::min<s32>(nseg, handle->nsegs));
return CELL_OK;
}
error_code sys_spu_thread_initialize(ppu_thread& ppu, vm::ptr<u32> thread, u32 group_id, u32 spu_num, vm::ptr<sys_spu_image> img, vm::ptr<sys_spu_thread_attribute> attr, vm::ptr<sys_spu_thread_argument> arg)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_thread_initialize(thread=*0x%x, group=0x%x, spu_num=%d, img=*0x%x, attr=*0x%x, arg=*0x%x)", thread, group_id, spu_num, img, attr, arg);
if (attr->name_len > 0x80 || attr->option & ~(SYS_SPU_THREAD_OPTION_DEC_SYNC_TB_ENABLE | SYS_SPU_THREAD_OPTION_ASYNC_INTR_ENABLE))
{
return CELL_EINVAL;
}
sys_spu_image image;
switch (img->type)
{
case SYS_SPU_IMAGE_TYPE_KERNEL:
{
const auto handle = idm::get<lv2_obj, lv2_spu_image>(img->entry_point);
if (!handle)
{
return CELL_ESRCH;
}
// Image information is stored in IDM
image.entry_point = handle->e_entry;
image.nsegs = handle->nsegs;
image.segs = handle->segs;
image.type = SYS_SPU_IMAGE_TYPE_KERNEL;
break;
}
case SYS_SPU_IMAGE_TYPE_USER:
{
if (img->entry_point > 0x3fffc || img->nsegs <= 0 || img->nsegs > 0x20)
{
return CELL_EINVAL;
}
image = *img;
break;
}
default: return CELL_EINVAL;
}
// Read thread name
const std::string thread_name(attr->name.get_ptr(), std::max<u32>(attr->name_len, 1) - 1);
const auto group = idm::get<lv2_spu_group>(group_id);
if (!group)
{
return CELL_ESRCH;
}
if (spu_num >= group->threads_map.size())
{
return CELL_EINVAL;
}
std::lock_guard lock(group->mutex);
if (group->threads_map[spu_num] != -1 || group->run_state != SPU_THREAD_GROUP_STATUS_NOT_INITIALIZED)
{
return CELL_EBUSY;
}
if (u32 option = attr->option)
{
sys_spu.warning("Unimplemented SPU Thread options (0x%x)", option);
}
const vm::addr_t ls_addr{verify("SPU LS" HERE, vm::alloc(0x80000, vm::main))};
const u32 inited = group->init;
const u32 tid = (inited << 24) | (group_id & 0xffffff);
verify(HERE), idm::import<named_thread<spu_thread>>([&]()
{
std::string full_name = fmt::format("SPU[0x%07x] Thread", tid);
if (!thread_name.empty())
{
fmt::append(full_name, " (%s)", thread_name);
}
const auto spu = std::make_shared<named_thread<spu_thread>>(full_name, ls_addr, group.get(), spu_num, thread_name, tid);
group->threads[inited] = spu;
group->threads_map[spu_num] = static_cast<s8>(inited);
return spu;
});
*thread = tid;
group->args[inited] = {arg->arg1, arg->arg2, arg->arg3, arg->arg4};
group->imgs[inited].first = image;
group->imgs[inited].second.assign(image.segs.get_ptr(), image.segs.get_ptr() + image.nsegs);
if (++group->init == group->max_num)
{
if (g_cfg.core.max_spurs_threads < 6 && group->max_num > 0u + g_cfg.core.max_spurs_threads)
{
if (group->name.ends_with("CellSpursKernelGroup"))
{
// Hack: don't run more SPURS threads than specified.
group->max_run = g_cfg.core.max_spurs_threads;
spu_log.success("HACK: '%s' (0x%x) limited to %u threads.", group->name, group_id, +g_cfg.core.max_spurs_threads);
}
}
group->run_state = SPU_THREAD_GROUP_STATUS_INITIALIZED;
}
sys_spu.warning(u8"sys_spu_thread_initialize(): Thread “%s” created (id=0x%x)", thread_name, tid);
return CELL_OK;
}
error_code sys_spu_thread_set_argument(ppu_thread& ppu, u32 id, vm::ptr<sys_spu_thread_argument> arg)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_thread_set_argument(id=0x%x, arg=*0x%x)", id, arg);
const auto [thread, group] = lv2_spu_group::get_thread(id);
if (!thread) [[unlikely]]
{
return CELL_ESRCH;
}
std::lock_guard lock(group->mutex);
group->args[id >> 24] = {arg->arg1, arg->arg2, arg->arg3, arg->arg4};
return CELL_OK;
}
error_code sys_spu_thread_get_exit_status(ppu_thread& ppu, u32 id, vm::ptr<u32> status)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_thread_get_exit_status(id=0x%x, status=*0x%x)", id, status);
const auto [thread, group] = lv2_spu_group::get_thread(id);
if (!thread) [[unlikely]]
{
return CELL_ESRCH;
}
if (thread->status_npc.load().status & SPU_STATUS_STOPPED_BY_STOP)
{
*status = thread->ch_out_mbox.get_value();
return CELL_OK;
}
return CELL_ESTAT;
}
error_code sys_spu_thread_group_create(ppu_thread& ppu, vm::ptr<u32> id, u32 num, s32 prio, vm::ptr<sys_spu_thread_group_attribute> attr)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_thread_group_create(id=*0x%x, num=%d, prio=%d, attr=*0x%x)", id, num, prio, attr);
const s32 min_prio = g_ps3_process_info.has_root_perm() ? 0 : 16;
if (attr->nsize > 0x80 || !num)
{
return CELL_EINVAL;
}
const s32 type = attr->type;
bool use_scheduler = true;
bool use_memct = !!(type & SYS_SPU_THREAD_GROUP_TYPE_MEMORY_FROM_CONTAINER);
bool needs_root = false;
u32 max_threads = 6; // TODO: max num value should be affected by sys_spu_initialize() settings
u32 min_threads = 1;
u32 mem_size = 0;
lv2_memory_container* ct{};
if (type)
{
sys_spu.warning("sys_spu_thread_group_create(): SPU Thread Group type (0x%x)", type);
}
switch (type)
{
case 0x0:
case 0x4:
case 0x18:
{
break;
}
case 0x20:
case 0x22:
case 0x24:
case 0x26:
{
if (type == 0x22 || type == 0x26)
{
needs_root = true;
}
min_threads = 2; // That's what appears from reversing
break;
}
case 0x2:
case 0x6:
case 0xA:
case 0x102:
case 0x106:
case 0x10A:
case 0x202:
case 0x206:
case 0x20A:
case 0x902:
case 0x906:
case 0xA02:
case 0xA06:
case 0xC02:
case 0xC06:
{
if (type & 0x700)
{
max_threads = 1;
}
needs_root = true;
break;
}
default: return CELL_EINVAL;
}
if (type & SYS_SPU_THREAD_GROUP_TYPE_COOPERATE_WITH_SYSTEM)
{
// Constant size, unknown what it means but it's definitely not for each spu thread alone
mem_size = 0x40000;
use_scheduler = false;
}
else if (type & SYS_SPU_THREAD_GROUP_TYPE_NON_CONTEXT)
{
// No memory consumed
mem_size = 0;
use_scheduler = false;
}
else
{
// 256kb for each spu thread, probably for saving and restoring SPU LS (used by scheduler?)
mem_size = 0x40000 * num;
}
if (num < min_threads || num > max_threads ||
(needs_root && min_prio == 0x10) || (use_scheduler && (prio > 255 || prio < min_prio)))
{
return CELL_EINVAL;
}
if (use_memct && mem_size)
{
const auto sct = idm::get<lv2_memory_container>(attr->ct);
if (!sct)
{
return CELL_ESRCH;
}
if (sct->take(mem_size) != mem_size)
{
return CELL_ENOMEM;
}
ct = sct.get();
}
else
{
ct = g_fxo->get<lv2_memory_container>();
if (ct->take(mem_size) != mem_size)
{
return CELL_ENOMEM;
}
}
const auto group = idm::make_ptr<lv2_spu_group>(std::string(attr->name.get_ptr(), std::max<u32>(attr->nsize, 1) - 1), num, prio, type, ct, use_scheduler, mem_size);
if (!group)
{
ct->used -= mem_size;
return CELL_EAGAIN;
}
*id = idm::last_id();
sys_spu.warning(u8"sys_spu_thread_group_create(): Thread group “%s” created (id=0x%x)", group->name, idm::last_id());
return CELL_OK;
}
error_code sys_spu_thread_group_destroy(ppu_thread& ppu, u32 id)
{
vm::temporary_unlock(ppu);
sys_spu.warning("sys_spu_thread_group_destroy(id=0x%x)", id);
const auto group = idm::withdraw<lv2_spu_group>(id, [](lv2_spu_group& group) -> CellError
{
const auto _old = group.run_state.compare_and_swap(SPU_THREAD_GROUP_STATUS_INITIALIZED, SPU_THREAD_GROUP_STATUS_NOT_INITIALIZED);
if (_old > SPU_THREAD_GROUP_STATUS_INITIALIZED)
{
return CELL_EBUSY;
}
group.ct->used -= group.mem_size;
return {};
});
if (!group)
{
return CELL_ESRCH;
}
if (group.ret)
{
return group.ret;
}
for (const auto& t : group->threads)
{
if (auto thread = t.get())
{
// Remove ID from IDM (destruction will occur in group destructor)
idm::remove<named_thread<spu_thread>>(thread->id);
}
}
return CELL_OK;
}
error_code sys_spu_thread_group_start(ppu_thread& ppu, u32 id)
{
vm::temporary_unlock(ppu);
sys_spu.trace("sys_spu_thread_group_start(id=0x%x)", id);
const auto group = idm::get<lv2_spu_group>(id, [](lv2_spu_group& group)
{
// SPU_THREAD_GROUP_STATUS_READY state is not used
return group.run_state.compare_and_swap_test(SPU_THREAD_GROUP_STATUS_INITIALIZED, SPU_THREAD_GROUP_STATUS_RUNNING);
});
if (!group)
{
return CELL_ESRCH;
}
if (!group.ret)
{
return CELL_ESTAT;
}
std::lock_guard lock(group->mutex);
const u32 max_threads = group->max_run;
group->join_state = 0;
group->running = max_threads;
group->set_terminate = false;
for (auto& thread : group->threads)
{
if (thread)
{
auto& args = group->args[thread->lv2_id >> 24];
auto& img = group->imgs[thread->lv2_id >> 24];
sys_spu_image::deploy(thread->offset, img.second.data(), img.first.nsegs);
thread->cpu_init();
thread->gpr[3] = v128::from64(0, args[0]);
thread->gpr[4] = v128::from64(0, args[1]);
thread->gpr[5] = v128::from64(0, args[2]);
thread->gpr[6] = v128::from64(0, args[3]);
thread->status_npc = {SPU_STATUS_RUNNING, img.first.entry_point};
}
}
// Because SPU_THREAD_GROUP_STATUS_READY is not possible, run event is delivered immediately
// TODO: check data2 and data3
group->send_run_event(id, 0, 0);
u32 ran_threads = max_threads;
for (auto& thread : group->threads)
{
if (!ran_threads)
{
break;
}
if (thread && ran_threads--)
{
thread->state -= cpu_flag::stop;
thread_ctrl::notify(*thread);
}
}
return CELL_OK;
}
error_code sys_spu_thread_group_suspend(ppu_thread& ppu, u32 id)
{
vm::temporary_unlock(ppu);
sys_spu.trace("sys_spu_thread_group_suspend(id=0x%x)", id);
const auto group = idm::get<lv2_spu_group>(id);
if (!group)
{
return CELL_ESRCH;
}
if (!group->has_scheduler_context || group->type & 0xf00)
{
return CELL_EINVAL;
}
std::lock_guard lock(group->mutex);
if (group->run_state <= SPU_THREAD_GROUP_STATUS_INITIALIZED || group->run_state == SPU_THREAD_GROUP_STATUS_STOPPED)
{
return CELL_ESTAT;
}
// SPU_THREAD_GROUP_STATUS_READY state is not used
if (group->run_state == SPU_THREAD_GROUP_STATUS_RUNNING)
{
group->run_state = SPU_THREAD_GROUP_STATUS_SUSPENDED;
}
else if (group->run_state == SPU_THREAD_GROUP_STATUS_WAITING)
{
group->run_state = SPU_THREAD_GROUP_STATUS_WAITING_AND_SUSPENDED;
}
else if (group->run_state == SPU_THREAD_GROUP_STATUS_SUSPENDED || group->run_state == SPU_THREAD_GROUP_STATUS_WAITING_AND_SUSPENDED)
{
return CELL_OK;
}
else
{
return CELL_ESTAT;
}
for (auto& thread : group->threads)
{
if (thread)
{
thread->state += cpu_flag::suspend;
}
}
return CELL_OK;
}
error_code sys_spu_thread_group_resume(ppu_thread& ppu, u32 id)
{
vm::temporary_unlock(ppu);
sys_spu.trace("sys_spu_thread_group_resume(id=0x%x)", id);
const auto group = idm::get<lv2_spu_group>(id);
if (!group)
{
return CELL_ESRCH;
}
if (!group->has_scheduler_context || group->type & 0xf00)
{
return CELL_EINVAL;
}
std::lock_guard lock(group->mutex);
// SPU_THREAD_GROUP_STATUS_READY state is not used
if (group->run_state == SPU_THREAD_GROUP_STATUS_SUSPENDED)
{
group->run_state = SPU_THREAD_GROUP_STATUS_RUNNING;
}
else if (group->run_state == SPU_THREAD_GROUP_STATUS_WAITING_AND_SUSPENDED)
{
group->run_state = SPU_THREAD_GROUP_STATUS_WAITING;
}
else
{
return CELL_ESTAT;
}
for (auto& thread : group->threads)
{
if (thread)
{
thread->state -= cpu_flag::suspend;
thread_ctrl::notify(*thread);
}
}
return CELL_OK;
}
error_code sys_spu_thread_group_yield(ppu_thread& ppu, u32 id)
{
vm::temporary_unlock(ppu);
sys_spu.trace("sys_spu_thread_group_yield(id=0x%x)", id);
const auto group = idm::get<lv2_spu_group>(id);
if (!group)
{
return CELL_ESRCH;
}
// No effect on these group types
if (!group->has_scheduler_context || group->type & 0xf00)
{
return CELL_OK;
}
if (group->run_state != SPU_THREAD_GROUP_STATUS_RUNNING)
{
return CELL_ESTAT;
}
// SPU_THREAD_GROUP_STATUS_READY state is not used, so this function does nothing
return CELL_OK;
}
error_code sys_spu_thread_group_terminate(ppu_thread& ppu, u32 id, s32 value)
{
vm::temporary_unlock(ppu);
sys_spu.trace("sys_spu_thread_group_terminate(id=0x%x, value=0x%x)", id, value);
const auto group = idm::get<lv2_spu_group>(id);
if (!group)
{
return CELL_ESRCH;
}
std::unique_lock lock(group->mutex);
if (group->run_state <= SPU_THREAD_GROUP_STATUS_INITIALIZED ||
group->run_state == SPU_THREAD_GROUP_STATUS_WAITING ||
group->run_state == SPU_THREAD_GROUP_STATUS_WAITING_AND_SUSPENDED ||
group->set_terminate)
{
return CELL_ESTAT;
}
group->set_terminate = true;
for (auto& thread : group->threads)
{
if (thread)
{
thread->state += cpu_flag::stop;
}
}
for (auto& thread : group->threads)
{
if (thread && group->running)
{
thread_ctrl::notify(*thread);
}
}
group->exit_status = value;
group->join_state = SYS_SPU_THREAD_GROUP_JOIN_TERMINATED;
// Wait until the threads are actually stopped
const u64 last_stop = group->stop_count - !group->running;
while (group->stop_count == last_stop)
{
group->cond.wait(lock);
}
return CELL_OK;
}
error_code sys_spu_thread_group_join(ppu_thread& ppu, u32 id, vm::ptr<u32> cause, vm::ptr<u32> status)
{
vm::temporary_unlock(ppu);
sys_spu.trace("sys_spu_thread_group_join(id=0x%x, cause=*0x%x, status=*0x%x)", id, cause, status);
const auto group = idm::get<lv2_spu_group>(id);
if (!group)
{
return CELL_ESRCH;
}
do
{
std::unique_lock lock(group->mutex);
if (group->run_state < SPU_THREAD_GROUP_STATUS_INITIALIZED)
{
return CELL_ESTAT;
}
if (group->waiter)
{
// another PPU thread is joining this thread group
return CELL_EBUSY;
}
if (group->join_state && group->run_state == SPU_THREAD_GROUP_STATUS_INITIALIZED)
{
// Already signaled
ppu.gpr[4] = group->join_state;
ppu.gpr[5] = group->exit_status;
group->join_state.release(0);
break;
}
else
{
// Subscribe to receive status in r4-r5
ppu.gpr[4] = 0;
group->waiter = &ppu;
}
lv2_obj::sleep(ppu);
while (!ppu.gpr[4])
{
if (ppu.is_stopped())
{
return 0;
}
group->cond.wait(lock);
}
}
while (0);
if (ppu.test_stopped())
{
return 0;
}
if (!cause)
{
if (status)
{
// Report unwritten data
return CELL_EFAULT;
}
return not_an_error(CELL_EFAULT);
}
*cause = static_cast<u32>(ppu.gpr[4]);
if (!status)
{
return not_an_error(CELL_EFAULT);