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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
|
using ChocolArm64.Memory;
using Ryujinx.HLE.Memory;
using System;
using System.Collections.Generic;
using static Ryujinx.HLE.HOS.ErrorCode;
namespace Ryujinx.HLE.HOS.Kernel
{
class KMemoryManager
{
public const int PageSize = 0x1000;
private LinkedList<KMemoryBlock> Blocks;
private MemoryManager CpuMemory;
private ArenaAllocator Allocator;
public long AddrSpaceStart { get; private set; }
public long AddrSpaceEnd { get; private set; }
public long CodeRegionStart { get; private set; }
public long CodeRegionEnd { get; private set; }
public long MapRegionStart { get; private set; }
public long MapRegionEnd { get; private set; }
public long HeapRegionStart { get; private set; }
public long HeapRegionEnd { get; private set; }
public long NewMapRegionStart { get; private set; }
public long NewMapRegionEnd { get; private set; }
public long TlsIoRegionStart { get; private set; }
public long TlsIoRegionEnd { get; private set; }
public long PersonalMmHeapUsage { get; private set; }
private long CurrentHeapAddr;
public KMemoryManager(Process Process)
{
CpuMemory = Process.Memory;
Allocator = Process.Device.Memory.Allocator;
long CodeRegionSize;
long MapRegionSize;
long HeapRegionSize;
long NewMapRegionSize;
long TlsIoRegionSize;
int AddrSpaceWidth;
AddressSpaceType AddrType = AddressSpaceType.Addr39Bits;
if (Process.MetaData != null)
{
AddrType = (AddressSpaceType)Process.MetaData.AddressSpaceWidth;
}
switch (AddrType)
{
case AddressSpaceType.Addr32Bits:
CodeRegionStart = 0x200000;
CodeRegionSize = 0x3fe00000;
MapRegionSize = 0x40000000;
HeapRegionSize = 0x40000000;
NewMapRegionSize = 0;
TlsIoRegionSize = 0;
AddrSpaceWidth = 32;
break;
case AddressSpaceType.Addr36Bits:
CodeRegionStart = 0x8000000;
CodeRegionSize = 0x78000000;
MapRegionSize = 0x180000000;
HeapRegionSize = 0x180000000;
NewMapRegionSize = 0;
TlsIoRegionSize = 0;
AddrSpaceWidth = 36;
break;
case AddressSpaceType.Addr36BitsNoMap:
CodeRegionStart = 0x200000;
CodeRegionSize = 0x3fe00000;
MapRegionSize = 0;
HeapRegionSize = 0x80000000;
NewMapRegionSize = 0;
TlsIoRegionSize = 0;
AddrSpaceWidth = 36;
break;
case AddressSpaceType.Addr39Bits:
CodeRegionStart = 0x8000000;
CodeRegionSize = 0x80000000;
MapRegionSize = 0x1000000000;
HeapRegionSize = 0x180000000;
NewMapRegionSize = 0x80000000;
TlsIoRegionSize = 0x1000000000;
AddrSpaceWidth = 39;
break;
default: throw new InvalidOperationException();
}
AddrSpaceStart = 0;
AddrSpaceEnd = 1L << AddrSpaceWidth;
CodeRegionEnd = CodeRegionStart + CodeRegionSize;
MapRegionStart = CodeRegionEnd;
MapRegionEnd = CodeRegionEnd + MapRegionSize;
HeapRegionStart = MapRegionEnd;
HeapRegionEnd = MapRegionEnd + HeapRegionSize;
NewMapRegionStart = HeapRegionEnd;
NewMapRegionEnd = HeapRegionEnd + NewMapRegionSize;
TlsIoRegionStart = NewMapRegionEnd;
TlsIoRegionEnd = NewMapRegionEnd + TlsIoRegionSize;
CurrentHeapAddr = HeapRegionStart;
if (NewMapRegionSize == 0)
{
NewMapRegionStart = AddrSpaceStart;
NewMapRegionEnd = AddrSpaceEnd;
}
Blocks = new LinkedList<KMemoryBlock>();
long AddrSpacePagesCount = (AddrSpaceEnd - AddrSpaceStart) / PageSize;
InsertBlock(AddrSpaceStart, AddrSpacePagesCount, MemoryState.Unmapped);
}
public void HleMapProcessCode(long Position, long Size)
{
long PagesCount = Size / PageSize;
if (!Allocator.TryAllocate(Size, out long PA))
{
throw new InvalidOperationException();
}
lock (Blocks)
{
InsertBlock(Position, PagesCount, MemoryState.CodeStatic, MemoryPermission.ReadAndExecute);
CpuMemory.Map(Position, PA, Size);
}
}
public long MapProcessCodeMemory(long Dst, long Src, long Size)
{
lock (Blocks)
{
long PagesCount = Size / PageSize;
bool Success = IsUnmapped(Dst, Size);
Success &= CheckRange(
Src,
Size,
MemoryState.Mask,
MemoryState.Heap,
MemoryPermission.Mask,
MemoryPermission.ReadAndWrite,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out _,
out _,
out _);
if (Success)
{
long PA = CpuMemory.GetPhysicalAddress(Src);
InsertBlock(Dst, PagesCount, MemoryState.CodeStatic, MemoryPermission.ReadAndExecute);
InsertBlock(Src, PagesCount, MemoryState.Heap, MemoryPermission.None);
CpuMemory.Map(Dst, PA, Size);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long UnmapProcessCodeMemory(long Dst, long Src, long Size)
{
lock (Blocks)
{
long PagesCount = Size / PageSize;
bool Success = CheckRange(
Dst,
Size,
MemoryState.Mask,
MemoryState.CodeStatic,
MemoryPermission.None,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out _,
out _,
out _);
Success &= CheckRange(
Src,
Size,
MemoryState.Mask,
MemoryState.Heap,
MemoryPermission.Mask,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out _,
out _,
out _);
if (Success)
{
InsertBlock(Dst, PagesCount, MemoryState.Unmapped);
InsertBlock(Src, PagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
CpuMemory.Unmap(Dst, Size);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public void HleMapCustom(long Position, long Size, MemoryState State, MemoryPermission Permission)
{
long PagesCount = Size / PageSize;
if (!Allocator.TryAllocate(Size, out long PA))
{
throw new InvalidOperationException();
}
lock (Blocks)
{
InsertBlock(Position, PagesCount, State, Permission);
CpuMemory.Map(Position, PA, Size);
}
}
public long HleMapTlsPage()
{
bool HasTlsIoRegion = TlsIoRegionStart != TlsIoRegionEnd;
long Position = HasTlsIoRegion ? TlsIoRegionStart : CodeRegionStart;
lock (Blocks)
{
while (Position < (HasTlsIoRegion ? TlsIoRegionEnd : CodeRegionEnd))
{
if (FindBlock(Position).State == MemoryState.Unmapped)
{
InsertBlock(Position, 1, MemoryState.ThreadLocal, MemoryPermission.ReadAndWrite);
if (!Allocator.TryAllocate(PageSize, out long PA))
{
throw new InvalidOperationException();
}
CpuMemory.Map(Position, PA, PageSize);
return Position;
}
Position += PageSize;
}
throw new InvalidOperationException();
}
}
public long TrySetHeapSize(long Size, out long Position)
{
Position = 0;
if ((ulong)Size > (ulong)(HeapRegionEnd - HeapRegionStart))
{
return MakeError(ErrorModule.Kernel, KernelErr.OutOfMemory);
}
bool Success = false;
long CurrentHeapSize = GetHeapSize();
if ((ulong)CurrentHeapSize <= (ulong)Size)
{
//Expand.
long DiffSize = Size - CurrentHeapSize;
lock (Blocks)
{
if (Success = IsUnmapped(CurrentHeapAddr, DiffSize))
{
if (!Allocator.TryAllocate(DiffSize, out long PA))
{
return MakeError(ErrorModule.Kernel, KernelErr.OutOfMemory);
}
long PagesCount = DiffSize / PageSize;
InsertBlock(CurrentHeapAddr, PagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
CpuMemory.Map(CurrentHeapAddr, PA, DiffSize);
}
}
}
else
{
//Shrink.
long FreeAddr = HeapRegionStart + Size;
long DiffSize = CurrentHeapSize - Size;
lock (Blocks)
{
Success = CheckRange(
FreeAddr,
DiffSize,
MemoryState.Mask,
MemoryState.Heap,
MemoryPermission.Mask,
MemoryPermission.ReadAndWrite,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out _,
out _,
out _);
if (Success)
{
long PagesCount = DiffSize / PageSize;
InsertBlock(FreeAddr, PagesCount, MemoryState.Unmapped);
FreePages(FreeAddr, PagesCount);
CpuMemory.Unmap(FreeAddr, DiffSize);
}
}
}
CurrentHeapAddr = HeapRegionStart + Size;
if (Success)
{
Position = HeapRegionStart;
return 0;
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long GetHeapSize()
{
return CurrentHeapAddr - HeapRegionStart;
}
public long SetMemoryAttribute(
long Position,
long Size,
MemoryAttribute AttributeMask,
MemoryAttribute AttributeValue)
{
lock (Blocks)
{
if (CheckRange(
Position,
Size,
MemoryState.AttributeChangeAllowed,
MemoryState.AttributeChangeAllowed,
MemoryPermission.None,
MemoryPermission.None,
MemoryAttribute.BorrowedAndIpcMapped,
MemoryAttribute.None,
MemoryAttribute.DeviceMappedAndUncached,
out MemoryState State,
out MemoryPermission Permission,
out MemoryAttribute Attribute))
{
long PagesCount = Size / PageSize;
Attribute &= ~AttributeMask;
Attribute |= AttributeMask & AttributeValue;
InsertBlock(Position, PagesCount, State, Permission, Attribute);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public KMemoryInfo QueryMemory(long Position)
{
if ((ulong)Position >= (ulong)AddrSpaceStart &&
(ulong)Position < (ulong)AddrSpaceEnd)
{
lock (Blocks)
{
return FindBlock(Position).GetInfo();
}
}
else
{
return new KMemoryInfo(
AddrSpaceEnd,
-AddrSpaceEnd,
MemoryState.Reserved,
MemoryPermission.None,
MemoryAttribute.None,
0,
0);
}
}
public long Map(long Src, long Dst, long Size)
{
bool Success;
lock (Blocks)
{
Success = CheckRange(
Src,
Size,
MemoryState.MapAllowed,
MemoryState.MapAllowed,
MemoryPermission.Mask,
MemoryPermission.ReadAndWrite,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out MemoryState SrcState,
out _,
out _);
Success &= IsUnmapped(Dst, Size);
if (Success)
{
long PagesCount = Size / PageSize;
InsertBlock(Src, PagesCount, SrcState, MemoryPermission.None, MemoryAttribute.Borrowed);
InsertBlock(Dst, PagesCount, MemoryState.MappedMemory, MemoryPermission.ReadAndWrite);
long PA = CpuMemory.GetPhysicalAddress(Src);
CpuMemory.Map(Dst, PA, Size);
}
}
return Success ? 0 : MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long Unmap(long Src, long Dst, long Size)
{
bool Success;
lock (Blocks)
{
Success = CheckRange(
Src,
Size,
MemoryState.MapAllowed,
MemoryState.MapAllowed,
MemoryPermission.Mask,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.Borrowed,
MemoryAttribute.IpcAndDeviceMapped,
out MemoryState SrcState,
out _,
out _);
Success &= CheckRange(
Dst,
Size,
MemoryState.Mask,
MemoryState.MappedMemory,
MemoryPermission.None,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out _,
out _,
out _);
if (Success)
{
long PagesCount = Size / PageSize;
InsertBlock(Src, PagesCount, SrcState, MemoryPermission.ReadAndWrite);
InsertBlock(Dst, PagesCount, MemoryState.Unmapped);
CpuMemory.Unmap(Dst, Size);
}
}
return Success ? 0 : MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long MapSharedMemory(KSharedMemory SharedMemory, MemoryPermission Permission, long Position)
{
lock (Blocks)
{
if (IsUnmapped(Position, SharedMemory.Size))
{
long PagesCount = SharedMemory.Size / PageSize;
InsertBlock(Position, PagesCount, MemoryState.SharedMemory, Permission);
CpuMemory.Map(Position, SharedMemory.PA, SharedMemory.Size);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long UnmapSharedMemory(long Position, long Size)
{
lock (Blocks)
{
if (CheckRange(
Position,
Size,
MemoryState.Mask,
MemoryState.SharedMemory,
MemoryPermission.None,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out MemoryState State,
out _,
out _))
{
long PagesCount = Size / PageSize;
InsertBlock(Position, PagesCount, MemoryState.Unmapped);
CpuMemory.Unmap(Position, Size);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long ReserveTransferMemory(long Position, long Size, MemoryPermission Permission)
{
lock (Blocks)
{
if (CheckRange(
Position,
Size,
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
MemoryPermission.Mask,
MemoryPermission.ReadAndWrite,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out MemoryState State,
out _,
out MemoryAttribute Attribute))
{
long PagesCount = Size / PageSize;
Attribute |= MemoryAttribute.Borrowed;
InsertBlock(Position, PagesCount, State, Permission, Attribute);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long ResetTransferMemory(long Position, long Size)
{
lock (Blocks)
{
if (CheckRange(
Position,
Size,
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
MemoryPermission.None,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.Borrowed,
MemoryAttribute.IpcAndDeviceMapped,
out MemoryState State,
out _,
out _))
{
long PagesCount = Size / PageSize;
InsertBlock(Position, PagesCount, State, MemoryPermission.ReadAndWrite);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long SetProcessMemoryPermission(long Position, long Size, MemoryPermission Permission)
{
lock (Blocks)
{
if (CheckRange(
Position,
Size,
MemoryState.ProcessPermissionChangeAllowed,
MemoryState.ProcessPermissionChangeAllowed,
MemoryPermission.None,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out MemoryState State,
out _,
out _))
{
if (State == MemoryState.CodeStatic)
{
State = MemoryState.CodeMutable;
}
else if (State == MemoryState.ModCodeStatic)
{
State = MemoryState.ModCodeMutable;
}
else
{
throw new InvalidOperationException();
}
long PagesCount = Size / PageSize;
InsertBlock(Position, PagesCount, State, Permission);
return 0;
}
}
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
public long MapPhysicalMemory(long Position, long Size)
{
long End = Position + Size;
lock (Blocks)
{
long MappedSize = 0;
KMemoryInfo Info;
LinkedListNode<KMemoryBlock> BaseNode = FindBlockNode(Position);
LinkedListNode<KMemoryBlock> Node = BaseNode;
do
{
Info = Node.Value.GetInfo();
if (Info.State != MemoryState.Unmapped)
{
MappedSize += GetSizeInRange(Info, Position, End);
}
Node = Node.Next;
}
while ((ulong)(Info.Position + Info.Size) < (ulong)End && Node != null);
if (MappedSize == Size)
{
return 0;
}
long RemainingSize = Size - MappedSize;
if (!Allocator.TryAllocate(RemainingSize, out long PA))
{
return MakeError(ErrorModule.Kernel, KernelErr.OutOfMemory);
}
Node = BaseNode;
do
{
Info = Node.Value.GetInfo();
if (Info.State == MemoryState.Unmapped)
{
long CurrSize = GetSizeInRange(Info, Position, End);
long MapPosition = Info.Position;
if ((ulong)MapPosition < (ulong)Position)
{
MapPosition = Position;
}
CpuMemory.Map(MapPosition, PA, CurrSize);
PA += CurrSize;
}
Node = Node.Next;
}
while ((ulong)(Info.Position + Info.Size) < (ulong)End && Node != null);
PersonalMmHeapUsage += RemainingSize;
long PagesCount = Size / PageSize;
InsertBlock(
Position,
PagesCount,
MemoryState.Unmapped,
MemoryPermission.None,
MemoryAttribute.None,
MemoryState.Heap,
MemoryPermission.ReadAndWrite,
MemoryAttribute.None);
}
return 0;
}
public long UnmapPhysicalMemory(long Position, long Size)
{
long End = Position + Size;
lock (Blocks)
{
long HeapMappedSize = 0;
long CurrPosition = Position;
KMemoryInfo Info;
LinkedListNode<KMemoryBlock> Node = FindBlockNode(CurrPosition);
do
{
Info = Node.Value.GetInfo();
if (Info.State == MemoryState.Heap)
{
if (Info.Attribute != MemoryAttribute.None)
{
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
HeapMappedSize += GetSizeInRange(Info, Position, End);
}
else if (Info.State != MemoryState.Unmapped)
{
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
}
Node = Node.Next;
}
while ((ulong)(Info.Position + Info.Size) < (ulong)End && Node != null);
if (HeapMappedSize == 0)
{
return 0;
}
PersonalMmHeapUsage -= HeapMappedSize;
long PagesCount = Size / PageSize;
InsertBlock(Position, PagesCount, MemoryState.Unmapped);
FreePages(Position, PagesCount);
CpuMemory.Unmap(Position, Size);
return 0;
}
}
private long GetSizeInRange(KMemoryInfo Info, long Start, long End)
{
long CurrEnd = Info.Size + Info.Position;
long CurrSize = Info.Size;
if ((ulong)Info.Position < (ulong)Start)
{
CurrSize -= Start - Info.Position;
}
if ((ulong)CurrEnd > (ulong)End)
{
CurrSize -= CurrEnd - End;
}
return CurrSize;
}
private void FreePages(long Position, long PagesCount)
{
for (long Page = 0; Page < PagesCount; Page++)
{
long VA = Position + Page * PageSize;
if (!CpuMemory.IsMapped(VA))
{
continue;
}
long PA = CpuMemory.GetPhysicalAddress(VA);
Allocator.Free(PA, PageSize);
}
}
public bool HleIsUnmapped(long Position, long Size)
{
bool Result = false;
lock (Blocks)
{
Result = IsUnmapped(Position, Size);
}
return Result;
}
private bool IsUnmapped(long Position, long Size)
{
return CheckRange(
Position,
Size,
MemoryState.Mask,
MemoryState.Unmapped,
MemoryPermission.Mask,
MemoryPermission.None,
MemoryAttribute.Mask,
MemoryAttribute.None,
MemoryAttribute.IpcAndDeviceMapped,
out _,
out _,
out _);
}
private bool CheckRange(
long Position,
long Size,
MemoryState StateMask,
MemoryState StateExpected,
MemoryPermission PermissionMask,
MemoryPermission PermissionExpected,
MemoryAttribute AttributeMask,
MemoryAttribute AttributeExpected,
MemoryAttribute AttributeIgnoreMask,
out MemoryState OutState,
out MemoryPermission OutPermission,
out MemoryAttribute OutAttribute)
{
KMemoryInfo BlkInfo = FindBlock(Position).GetInfo();
ulong Start = (ulong)Position;
ulong End = (ulong)Size + Start;
if (End <= (ulong)(BlkInfo.Position + BlkInfo.Size))
{
if ((BlkInfo.Attribute & AttributeMask) == AttributeExpected &&
(BlkInfo.State & StateMask) == StateExpected &&
(BlkInfo.Permission & PermissionMask) == PermissionExpected)
{
OutState = BlkInfo.State;
OutPermission = BlkInfo.Permission;
OutAttribute = BlkInfo.Attribute & ~AttributeIgnoreMask;
return true;
}
}
OutState = MemoryState.Unmapped;
OutPermission = MemoryPermission.None;
OutAttribute = MemoryAttribute.None;
return false;
}
private void InsertBlock(
long BasePosition,
long PagesCount,
MemoryState OldState,
MemoryPermission OldPermission,
MemoryAttribute OldAttribute,
MemoryState NewState,
MemoryPermission NewPermission,
MemoryAttribute NewAttribute)
{
//Insert new block on the list only on areas where the state
//of the block matches the state specified on the Old* state
//arguments, otherwise leave it as is.
OldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
ulong Start = (ulong)BasePosition;
ulong End = (ulong)PagesCount * PageSize + Start;
LinkedListNode<KMemoryBlock> Node = Blocks.First;
while (Node != null)
{
LinkedListNode<KMemoryBlock> NewNode = Node;
LinkedListNode<KMemoryBlock> NextNode = Node.Next;
KMemoryBlock CurrBlock = Node.Value;
ulong CurrStart = (ulong)CurrBlock.BasePosition;
ulong CurrEnd = (ulong)CurrBlock.PagesCount * PageSize + CurrStart;
if (Start < CurrEnd && CurrStart < End)
{
MemoryAttribute CurrBlockAttr = CurrBlock.Attribute | MemoryAttribute.IpcAndDeviceMapped;
if (CurrBlock.State != OldState ||
CurrBlock.Permission != OldPermission ||
CurrBlockAttr != OldAttribute)
{
Node = NextNode;
continue;
}
if (CurrStart >= Start && CurrEnd <= End)
{
CurrBlock.State = NewState;
CurrBlock.Permission = NewPermission;
CurrBlock.Attribute &= ~MemoryAttribute.IpcAndDeviceMapped;
CurrBlock.Attribute |= NewAttribute;
}
else if (CurrStart >= Start)
{
CurrBlock.BasePosition = (long)End;
CurrBlock.PagesCount = (long)((CurrEnd - End) / PageSize);
long NewPagesCount = (long)((End - CurrStart) / PageSize);
NewNode = Blocks.AddBefore(Node, new KMemoryBlock(
(long)CurrStart,
NewPagesCount,
NewState,
NewPermission,
NewAttribute));
}
else if (CurrEnd <= End)
{
CurrBlock.PagesCount = (long)((Start - CurrStart) / PageSize);
long NewPagesCount = (long)((CurrEnd - Start) / PageSize);
NewNode = Blocks.AddAfter(Node, new KMemoryBlock(
BasePosition,
NewPagesCount,
NewState,
NewPermission,
NewAttribute));
}
else
{
CurrBlock.PagesCount = (long)((Start - CurrStart) / PageSize);
long NextPagesCount = (long)((CurrEnd - End) / PageSize);
NewNode = Blocks.AddAfter(Node, new KMemoryBlock(
BasePosition,
PagesCount,
NewState,
NewPermission,
NewAttribute));
Blocks.AddAfter(NewNode, new KMemoryBlock(
(long)End,
NextPagesCount,
CurrBlock.State,
CurrBlock.Permission,
CurrBlock.Attribute));
NextNode = null;
}
MergeEqualStateNeighbours(NewNode);
}
Node = NextNode;
}
}
private void InsertBlock(
long BasePosition,
long PagesCount,
MemoryState State,
MemoryPermission Permission = MemoryPermission.None,
MemoryAttribute Attribute = MemoryAttribute.None)
{
//Inserts new block at the list, replacing and spliting
//existing blocks as needed.
KMemoryBlock Block = new KMemoryBlock(BasePosition, PagesCount, State, Permission, Attribute);
ulong Start = (ulong)BasePosition;
ulong End = (ulong)PagesCount * PageSize + Start;
LinkedListNode<KMemoryBlock> NewNode = null;
LinkedListNode<KMemoryBlock> Node = Blocks.First;
while (Node != null)
{
KMemoryBlock CurrBlock = Node.Value;
LinkedListNode<KMemoryBlock> NextNode = Node.Next;
ulong CurrStart = (ulong)CurrBlock.BasePosition;
ulong CurrEnd = (ulong)CurrBlock.PagesCount * PageSize + CurrStart;
if (Start < CurrEnd && CurrStart < End)
{
if (Start >= CurrStart && End <= CurrEnd)
{
Block.Attribute |= CurrBlock.Attribute & MemoryAttribute.IpcAndDeviceMapped;
}
if (Start > CurrStart && End < CurrEnd)
{
CurrBlock.PagesCount = (long)((Start - CurrStart) / PageSize);
long NextPagesCount = (long)((CurrEnd - End) / PageSize);
NewNode = Blocks.AddAfter(Node, Block);
Blocks.AddAfter(NewNode, new KMemoryBlock(
(long)End,
NextPagesCount,
CurrBlock.State,
CurrBlock.Permission,
CurrBlock.Attribute));
break;
}
else if (Start <= CurrStart && End < CurrEnd)
{
CurrBlock.BasePosition = (long)End;
CurrBlock.PagesCount = (long)((CurrEnd - End) / PageSize);
if (NewNode == null)
{
NewNode = Blocks.AddBefore(Node, Block);
}
}
else if (Start > CurrStart && End >= CurrEnd)
{
CurrBlock.PagesCount = (long)((Start - CurrStart) / PageSize);
if (NewNode == null)
{
NewNode = Blocks.AddAfter(Node, Block);
}
}
else
{
if (NewNode == null)
{
NewNode = Blocks.AddBefore(Node, Block);
}
Blocks.Remove(Node);
}
}
Node = NextNode;
}
if (NewNode == null)
{
NewNode = Blocks.AddFirst(Block);
}
MergeEqualStateNeighbours(NewNode);
}
private void MergeEqualStateNeighbours(LinkedListNode<KMemoryBlock> Node)
{
KMemoryBlock Block = Node.Value;
ulong Start = (ulong)Block.BasePosition;
ulong End = (ulong)Block.PagesCount * PageSize + Start;
if (Node.Previous != null)
{
KMemoryBlock Previous = Node.Previous.Value;
if (BlockStateEquals(Block, Previous))
{
Blocks.Remove(Node.Previous);
Block.BasePosition = Previous.BasePosition;
Start = (ulong)Block.BasePosition;
}
}
if (Node.Next != null)
{
KMemoryBlock Next = Node.Next.Value;
if (BlockStateEquals(Block, Next))
{
Blocks.Remove(Node.Next);
End = (ulong)(Next.BasePosition + Next.PagesCount * PageSize);
}
}
Block.PagesCount = (long)((End - Start) / PageSize);
}
private static bool BlockStateEquals(KMemoryBlock LHS, KMemoryBlock RHS)
{
return LHS.State == RHS.State &&
LHS.Permission == RHS.Permission &&
LHS.Attribute == RHS.Attribute &&
LHS.DeviceRefCount == RHS.DeviceRefCount &&
LHS.IpcRefCount == RHS.IpcRefCount;
}
private KMemoryBlock FindBlock(long Position)
{
return FindBlockNode(Position)?.Value;
}
private LinkedListNode<KMemoryBlock> FindBlockNode(long Position)
{
ulong Addr = (ulong)Position;
lock (Blocks)
{
LinkedListNode<KMemoryBlock> Node = Blocks.First;
while (Node != null)
{
KMemoryBlock Block = Node.Value;
ulong Start = (ulong)Block.BasePosition;
ulong End = (ulong)Block.PagesCount * PageSize + Start;
if (Start <= Addr && End - 1 >= Addr)
{
return Node;
}
Node = Node.Next;
}
}
return null;
}
}
}
|