-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Gcd.c
2628 lines (2259 loc) · 83.9 KB
/
Gcd.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
The file contains the GCD related services in the EFI Boot Services Table.
The GCD services are used to manage the memory and I/O regions that
are accessible to the CPU that is executing the DXE core.
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "DxeMain.h"
#include "Gcd.h"
#include "Mem/HeapGuard.h"
#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
#define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
EFI_RESOURCE_ATTRIBUTE_TESTED | \
EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \
EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \
EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \
EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED | \
EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \
EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
EFI_RESOURCE_ATTRIBUTE_64_BIT_IO | \
EFI_RESOURCE_ATTRIBUTE_PERSISTENT )
#define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
EFI_RESOURCE_ATTRIBUTE_TESTED )
#define INITIALIZED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
EFI_RESOURCE_ATTRIBUTE_INITIALIZED )
#define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT)
#define EXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \
EFI_MEMORY_WT | EFI_MEMORY_WB | \
EFI_MEMORY_WP | EFI_MEMORY_UCE)
#define NONEXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | \
EFI_MEMORY_RO)
#define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff
//
// Module Variables
//
EFI_LOCK mGcdMemorySpaceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
EFI_LOCK mGcdIoSpaceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
LIST_ENTRY mGcdMemorySpaceMap = INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap);
LIST_ENTRY mGcdIoSpaceMap = INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap);
EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate = {
EFI_GCD_MAP_SIGNATURE,
{
NULL,
NULL
},
0,
0,
0,
0,
EfiGcdMemoryTypeNonExistent,
(EFI_GCD_IO_TYPE) 0,
NULL,
NULL
};
EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate = {
EFI_GCD_MAP_SIGNATURE,
{
NULL,
NULL
},
0,
0,
0,
0,
(EFI_GCD_MEMORY_TYPE) 0,
EfiGcdIoTypeNonExistent,
NULL,
NULL
};
GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable[] = {
{ EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE, EFI_MEMORY_UC, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED, EFI_MEMORY_UCE, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE, EFI_MEMORY_WC, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE, EFI_MEMORY_WT, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE, EFI_MEMORY_WB, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE, EFI_MEMORY_RP, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE, EFI_MEMORY_WP, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE, EFI_MEMORY_XP, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE, EFI_MEMORY_RO, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_PRESENT, EFI_MEMORY_PRESENT, FALSE },
{ EFI_RESOURCE_ATTRIBUTE_INITIALIZED, EFI_MEMORY_INITIALIZED, FALSE },
{ EFI_RESOURCE_ATTRIBUTE_TESTED, EFI_MEMORY_TESTED, FALSE },
{ EFI_RESOURCE_ATTRIBUTE_PERSISTABLE, EFI_MEMORY_NV, TRUE },
{ EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE, EFI_MEMORY_MORE_RELIABLE, TRUE },
{ 0, 0, FALSE }
};
///
/// Lookup table used to print GCD Memory Space Map
///
GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdMemoryTypeNames[] = {
"NonExist ", // EfiGcdMemoryTypeNonExistent
"Reserved ", // EfiGcdMemoryTypeReserved
"SystemMem", // EfiGcdMemoryTypeSystemMemory
"MMIO ", // EfiGcdMemoryTypeMemoryMappedIo
"PersisMem", // EfiGcdMemoryTypePersistent
"MoreRelia", // EfiGcdMemoryTypeMoreReliable
"Unknown " // EfiGcdMemoryTypeMaximum
};
///
/// Lookup table used to print GCD I/O Space Map
///
GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdIoTypeNames[] = {
"NonExist", // EfiGcdIoTypeNonExistent
"Reserved", // EfiGcdIoTypeReserved
"I/O ", // EfiGcdIoTypeIo
"Unknown " // EfiGcdIoTypeMaximum
};
///
/// Lookup table used to print GCD Allocation Types
///
GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdAllocationTypeNames[] = {
"AnySearchBottomUp ", // EfiGcdAllocateAnySearchBottomUp
"MaxAddressSearchBottomUp ", // EfiGcdAllocateMaxAddressSearchBottomUp
"AtAddress ", // EfiGcdAllocateAddress
"AnySearchTopDown ", // EfiGcdAllocateAnySearchTopDown
"MaxAddressSearchTopDown ", // EfiGcdAllocateMaxAddressSearchTopDown
"Unknown " // EfiGcdMaxAllocateType
};
/**
Dump the entire contents if the GCD Memory Space Map using DEBUG() macros when
PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
@param InitialMap TRUE if the initial GCD Memory Map is being dumped. Otherwise, FALSE.
**/
VOID
EFIAPI
CoreDumpGcdMemorySpaceMap (
BOOLEAN InitialMap
)
{
DEBUG_CODE (
EFI_STATUS Status;
UINTN NumberOfDescriptors;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;
UINTN Index;
Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);
ASSERT (Status == EFI_SUCCESS && MemorySpaceMap != NULL);
if (InitialMap) {
DEBUG ((DEBUG_GCD, "GCD:Initial GCD Memory Space Map\n"));
}
DEBUG ((DEBUG_GCD, "GCDMemType Range Capabilities Attributes \n"));
DEBUG ((DEBUG_GCD, "========== ================================= ================ ================\n"));
for (Index = 0; Index < NumberOfDescriptors; Index++) {
DEBUG ((DEBUG_GCD, "%a %016lx-%016lx %016lx %016lx%c\n",
mGcdMemoryTypeNames[MIN (MemorySpaceMap[Index].GcdMemoryType, EfiGcdMemoryTypeMaximum)],
MemorySpaceMap[Index].BaseAddress,
MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - 1,
MemorySpaceMap[Index].Capabilities,
MemorySpaceMap[Index].Attributes,
MemorySpaceMap[Index].ImageHandle == NULL ? ' ' : '*'
));
}
DEBUG ((DEBUG_GCD, "\n"));
FreePool (MemorySpaceMap);
);
}
/**
Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when
PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
@param InitialMap TRUE if the initial GCD I/O Map is being dumped. Otherwise, FALSE.
**/
VOID
EFIAPI
CoreDumpGcdIoSpaceMap (
BOOLEAN InitialMap
)
{
DEBUG_CODE (
EFI_STATUS Status;
UINTN NumberOfDescriptors;
EFI_GCD_IO_SPACE_DESCRIPTOR *IoSpaceMap;
UINTN Index;
Status = CoreGetIoSpaceMap (&NumberOfDescriptors, &IoSpaceMap);
ASSERT (Status == EFI_SUCCESS && IoSpaceMap != NULL);
if (InitialMap) {
DEBUG ((DEBUG_GCD, "GCD:Initial GCD I/O Space Map\n"));
}
DEBUG ((DEBUG_GCD, "GCDIoType Range \n"));
DEBUG ((DEBUG_GCD, "========== =================================\n"));
for (Index = 0; Index < NumberOfDescriptors; Index++) {
DEBUG ((DEBUG_GCD, "%a %016lx-%016lx%c\n",
mGcdIoTypeNames[MIN (IoSpaceMap[Index].GcdIoType, EfiGcdIoTypeMaximum)],
IoSpaceMap[Index].BaseAddress,
IoSpaceMap[Index].BaseAddress + IoSpaceMap[Index].Length - 1,
IoSpaceMap[Index].ImageHandle == NULL ? ' ' : '*'
));
}
DEBUG ((DEBUG_GCD, "\n"));
FreePool (IoSpaceMap);
);
}
/**
Validate resource descriptor HOB's attributes.
If Attributes includes some memory resource's settings, it should include
the corresponding capabilites also.
@param Attributes Resource descriptor HOB attributes.
**/
VOID
CoreValidateResourceDescriptorHobAttributes (
IN UINT64 Attributes
)
{
ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED) == 0) ||
((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE) != 0));
ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED) == 0) ||
((Attributes & EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE) != 0));
ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED) == 0) ||
((Attributes & EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE) != 0));
ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED) == 0) ||
((Attributes & EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE) != 0));
ASSERT (((Attributes & EFI_RESOURCE_ATTRIBUTE_PERSISTENT) == 0) ||
((Attributes & EFI_RESOURCE_ATTRIBUTE_PERSISTABLE) != 0));
}
/**
Acquire memory lock on mGcdMemorySpaceLock.
**/
VOID
CoreAcquireGcdMemoryLock (
VOID
)
{
CoreAcquireLock (&mGcdMemorySpaceLock);
}
/**
Release memory lock on mGcdMemorySpaceLock.
**/
VOID
CoreReleaseGcdMemoryLock (
VOID
)
{
CoreReleaseLock (&mGcdMemorySpaceLock);
}
/**
Acquire memory lock on mGcdIoSpaceLock.
**/
VOID
CoreAcquireGcdIoLock (
VOID
)
{
CoreAcquireLock (&mGcdIoSpaceLock);
}
/**
Release memory lock on mGcdIoSpaceLock.
**/
VOID
CoreReleaseGcdIoLock (
VOID
)
{
CoreReleaseLock (&mGcdIoSpaceLock);
}
//
// GCD Initialization Worker Functions
//
/**
Aligns a value to the specified boundary.
@param Value 64 bit value to align
@param Alignment Log base 2 of the boundary to align Value to
@param RoundUp TRUE if Value is to be rounded up to the nearest
aligned boundary. FALSE is Value is to be
rounded down to the nearest aligned boundary.
@return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
**/
UINT64
AlignValue (
IN UINT64 Value,
IN UINTN Alignment,
IN BOOLEAN RoundUp
)
{
UINT64 AlignmentMask;
AlignmentMask = LShiftU64 (1, Alignment) - 1;
if (RoundUp) {
Value += AlignmentMask;
}
return Value & (~AlignmentMask);
}
/**
Aligns address to the page boundary.
@param Value 64 bit address to align
@return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
**/
UINT64
PageAlignAddress (
IN UINT64 Value
)
{
return AlignValue (Value, EFI_PAGE_SHIFT, TRUE);
}
/**
Aligns length to the page boundary.
@param Value 64 bit length to align
@return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
**/
UINT64
PageAlignLength (
IN UINT64 Value
)
{
return AlignValue (Value, EFI_PAGE_SHIFT, FALSE);
}
//
// GCD Memory Space Worker Functions
//
/**
Allocate pool for two entries.
@param TopEntry An entry of GCD map
@param BottomEntry An entry of GCD map
@retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
@retval EFI_SUCCESS Both entries successfully allocated.
**/
EFI_STATUS
CoreAllocateGcdMapEntry (
IN OUT EFI_GCD_MAP_ENTRY **TopEntry,
IN OUT EFI_GCD_MAP_ENTRY **BottomEntry
)
{
//
// Set to mOnGuarding to TRUE before memory allocation. This will make sure
// that the entry memory is not "guarded" by HeapGuard. Otherwise it might
// cause problem when it's freed (if HeapGuard is enabled).
//
mOnGuarding = TRUE;
*TopEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));
mOnGuarding = FALSE;
if (*TopEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
mOnGuarding = TRUE;
*BottomEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));
mOnGuarding = FALSE;
if (*BottomEntry == NULL) {
CoreFreePool (*TopEntry);
return EFI_OUT_OF_RESOURCES;
}
return EFI_SUCCESS;
}
/**
Internal function. Inserts a new descriptor into a sorted list
@param Link The linked list to insert the range BaseAddress
and Length into
@param Entry A pointer to the entry that is inserted
@param BaseAddress The base address of the new range
@param Length The length of the new range in bytes
@param TopEntry Top pad entry to insert if needed.
@param BottomEntry Bottom pad entry to insert if needed.
@retval EFI_SUCCESS The new range was inserted into the linked list
**/
EFI_STATUS
CoreInsertGcdMapEntry (
IN LIST_ENTRY *Link,
IN EFI_GCD_MAP_ENTRY *Entry,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN EFI_GCD_MAP_ENTRY *TopEntry,
IN EFI_GCD_MAP_ENTRY *BottomEntry
)
{
ASSERT (Length != 0);
if (BaseAddress > Entry->BaseAddress) {
ASSERT (BottomEntry->Signature == 0);
CopyMem (BottomEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));
Entry->BaseAddress = BaseAddress;
BottomEntry->EndAddress = BaseAddress - 1;
InsertTailList (Link, &BottomEntry->Link);
}
if ((BaseAddress + Length - 1) < Entry->EndAddress) {
ASSERT (TopEntry->Signature == 0);
CopyMem (TopEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));
TopEntry->BaseAddress = BaseAddress + Length;
Entry->EndAddress = BaseAddress + Length - 1;
InsertHeadList (Link, &TopEntry->Link);
}
return EFI_SUCCESS;
}
/**
Merge the Gcd region specified by Link and its adjacent entry.
@param Link Specify the entry to be merged (with its
adjacent entry).
@param Forward Direction (forward or backward).
@param Map Boundary.
@retval EFI_SUCCESS Successfully returned.
@retval EFI_UNSUPPORTED These adjacent regions could not merge.
**/
EFI_STATUS
CoreMergeGcdMapEntry (
IN LIST_ENTRY *Link,
IN BOOLEAN Forward,
IN LIST_ENTRY *Map
)
{
LIST_ENTRY *AdjacentLink;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_MAP_ENTRY *AdjacentEntry;
//
// Get adjacent entry
//
if (Forward) {
AdjacentLink = Link->ForwardLink;
} else {
AdjacentLink = Link->BackLink;
}
//
// If AdjacentLink is the head of the list, then no merge can be performed
//
if (AdjacentLink == Map) {
return EFI_SUCCESS;
}
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
AdjacentEntry = CR (AdjacentLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
if (Entry->Capabilities != AdjacentEntry->Capabilities) {
return EFI_UNSUPPORTED;
}
if (Entry->Attributes != AdjacentEntry->Attributes) {
return EFI_UNSUPPORTED;
}
if (Entry->GcdMemoryType != AdjacentEntry->GcdMemoryType) {
return EFI_UNSUPPORTED;
}
if (Entry->GcdIoType != AdjacentEntry->GcdIoType) {
return EFI_UNSUPPORTED;
}
if (Entry->ImageHandle != AdjacentEntry->ImageHandle) {
return EFI_UNSUPPORTED;
}
if (Entry->DeviceHandle != AdjacentEntry->DeviceHandle) {
return EFI_UNSUPPORTED;
}
if (Forward) {
Entry->EndAddress = AdjacentEntry->EndAddress;
} else {
Entry->BaseAddress = AdjacentEntry->BaseAddress;
}
RemoveEntryList (AdjacentLink);
CoreFreePool (AdjacentEntry);
return EFI_SUCCESS;
}
/**
Merge adjacent entries on total chain.
@param TopEntry Top entry of GCD map.
@param BottomEntry Bottom entry of GCD map.
@param StartLink Start link of the list for this loop.
@param EndLink End link of the list for this loop.
@param Map Boundary.
@retval EFI_SUCCESS GCD map successfully cleaned up.
**/
EFI_STATUS
CoreCleanupGcdMapEntry (
IN EFI_GCD_MAP_ENTRY *TopEntry,
IN EFI_GCD_MAP_ENTRY *BottomEntry,
IN LIST_ENTRY *StartLink,
IN LIST_ENTRY *EndLink,
IN LIST_ENTRY *Map
)
{
LIST_ENTRY *Link;
if (TopEntry->Signature == 0) {
CoreFreePool (TopEntry);
}
if (BottomEntry->Signature == 0) {
CoreFreePool (BottomEntry);
}
Link = StartLink;
while (Link != EndLink->ForwardLink) {
CoreMergeGcdMapEntry (Link, FALSE, Map);
Link = Link->ForwardLink;
}
CoreMergeGcdMapEntry (EndLink, TRUE, Map);
return EFI_SUCCESS;
}
/**
Search a segment of memory space in GCD map. The result is a range of GCD entry list.
@param BaseAddress The start address of the segment.
@param Length The length of the segment.
@param StartLink The first GCD entry involves this segment of
memory space.
@param EndLink The first GCD entry involves this segment of
memory space.
@param Map Points to the start entry to search.
@retval EFI_SUCCESS Successfully found the entry.
@retval EFI_NOT_FOUND Not found.
**/
EFI_STATUS
CoreSearchGcdMapEntry (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
OUT LIST_ENTRY **StartLink,
OUT LIST_ENTRY **EndLink,
IN LIST_ENTRY *Map
)
{
LIST_ENTRY *Link;
EFI_GCD_MAP_ENTRY *Entry;
ASSERT (Length != 0);
*StartLink = NULL;
*EndLink = NULL;
Link = Map->ForwardLink;
while (Link != Map) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
if (BaseAddress >= Entry->BaseAddress && BaseAddress <= Entry->EndAddress) {
*StartLink = Link;
}
if (*StartLink != NULL) {
if ((BaseAddress + Length - 1) >= Entry->BaseAddress &&
(BaseAddress + Length - 1) <= Entry->EndAddress ) {
*EndLink = Link;
return EFI_SUCCESS;
}
}
Link = Link->ForwardLink;
}
return EFI_NOT_FOUND;
}
/**
Count the amount of GCD map entries.
@param Map Points to the start entry to do the count loop.
@return The count.
**/
UINTN
CoreCountGcdMapEntry (
IN LIST_ENTRY *Map
)
{
UINTN Count;
LIST_ENTRY *Link;
Count = 0;
Link = Map->ForwardLink;
while (Link != Map) {
Count++;
Link = Link->ForwardLink;
}
return Count;
}
/**
Return the memory attribute specified by Attributes
@param Attributes A num with some attribute bits on.
@return The enum value of memory attribute.
**/
UINT64
ConverToCpuArchAttributes (
UINT64 Attributes
)
{
UINT64 CpuArchAttributes;
CpuArchAttributes = Attributes & NONEXCLUSIVE_MEMORY_ATTRIBUTES;
if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {
CpuArchAttributes |= EFI_MEMORY_UC;
} else if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {
CpuArchAttributes |= EFI_MEMORY_WC;
} else if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {
CpuArchAttributes |= EFI_MEMORY_WT;
} else if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {
CpuArchAttributes |= EFI_MEMORY_WB;
} else if ( (Attributes & EFI_MEMORY_UCE) == EFI_MEMORY_UCE) {
CpuArchAttributes |= EFI_MEMORY_UCE;
} else if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {
CpuArchAttributes |= EFI_MEMORY_WP;
}
return CpuArchAttributes;
}
/**
Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
@param Operation The type of the operation
@param GcdMemoryType Additional information for the operation
@param GcdIoType Additional information for the operation
@param BaseAddress Start address of the segment
@param Length length of the segment
@param Capabilities The alterable attributes of a newly added entry
@param Attributes The attributes needs to be set
@retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
setting attribute.
@retval EFI_SUCCESS Action successfully done.
@retval EFI_UNSUPPORTED Could not find the proper descriptor on this
segment or set an upsupported attribute.
@retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
image.
@retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
space, and so on.
@retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
@retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol
is not available yet.
**/
EFI_STATUS
CoreConvertSpace (
IN UINTN Operation,
IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
IN EFI_GCD_IO_TYPE GcdIoType,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Capabilities,
IN UINT64 Attributes
)
{
EFI_STATUS Status;
LIST_ENTRY *Map;
LIST_ENTRY *Link;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_MAP_ENTRY *TopEntry;
EFI_GCD_MAP_ENTRY *BottomEntry;
LIST_ENTRY *StartLink;
LIST_ENTRY *EndLink;
UINT64 CpuArchAttributes;
if (Length == 0) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_INVALID_PARAMETER));
return EFI_INVALID_PARAMETER;
}
Map = NULL;
if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
CoreAcquireGcdMemoryLock ();
Map = &mGcdMemorySpaceMap;
} else if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {
CoreAcquireGcdIoLock ();
Map = &mGcdIoSpaceMap;
} else {
ASSERT (FALSE);
}
//
// Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
//
Status = CoreSearchGcdMapEntry (BaseAddress, Length, &StartLink, &EndLink, Map);
if (EFI_ERROR (Status)) {
Status = EFI_UNSUPPORTED;
goto Done;
}
ASSERT (StartLink != NULL && EndLink != NULL);
//
// Verify that the list of descriptors are unallocated non-existent memory.
//
Link = StartLink;
while (Link != EndLink->ForwardLink) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
switch (Operation) {
//
// Add operations
//
case GCD_ADD_MEMORY_OPERATION:
if (Entry->GcdMemoryType != EfiGcdMemoryTypeNonExistent ||
Entry->ImageHandle != NULL ) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
break;
case GCD_ADD_IO_OPERATION:
if (Entry->GcdIoType != EfiGcdIoTypeNonExistent ||
Entry->ImageHandle != NULL ) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
break;
//
// Free operations
//
case GCD_FREE_MEMORY_OPERATION:
case GCD_FREE_IO_OPERATION:
if (Entry->ImageHandle == NULL) {
Status = EFI_NOT_FOUND;
goto Done;
}
break;
//
// Remove operations
//
case GCD_REMOVE_MEMORY_OPERATION:
if (Entry->GcdMemoryType == EfiGcdMemoryTypeNonExistent) {
Status = EFI_NOT_FOUND;
goto Done;
}
if (Entry->ImageHandle != NULL) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
break;
case GCD_REMOVE_IO_OPERATION:
if (Entry->GcdIoType == EfiGcdIoTypeNonExistent) {
Status = EFI_NOT_FOUND;
goto Done;
}
if (Entry->ImageHandle != NULL) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
break;
//
// Set attributes operation
//
case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:
if ((Attributes & EFI_MEMORY_RUNTIME) != 0) {
if ((BaseAddress & EFI_PAGE_MASK) != 0 || (Length & EFI_PAGE_MASK) != 0) {
Status = EFI_INVALID_PARAMETER;
goto Done;
}
}
if ((Entry->Capabilities & Attributes) != Attributes) {
Status = EFI_UNSUPPORTED;
goto Done;
}
break;
//
// Set capabilities operation
//
case GCD_SET_CAPABILITIES_MEMORY_OPERATION:
if ((BaseAddress & EFI_PAGE_MASK) != 0 || (Length & EFI_PAGE_MASK) != 0) {
Status = EFI_INVALID_PARAMETER;
goto Done;
}
//
// Current attributes must still be supported with new capabilities
//
if ((Capabilities & Entry->Attributes) != Entry->Attributes) {
Status = EFI_UNSUPPORTED;
goto Done;
}
break;
}
Link = Link->ForwardLink;
}
//
// Allocate work space to perform this operation
//
Status = CoreAllocateGcdMapEntry (&TopEntry, &BottomEntry);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
ASSERT (TopEntry != NULL && BottomEntry != NULL);
if (Operation == GCD_SET_ATTRIBUTES_MEMORY_OPERATION) {
//
// Call CPU Arch Protocol to attempt to set attributes on the range
//
CpuArchAttributes = ConverToCpuArchAttributes (Attributes);
if (CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES) {
if (gCpu == NULL) {
Status = EFI_NOT_AVAILABLE_YET;
} else {
Status = gCpu->SetMemoryAttributes (
gCpu,
BaseAddress,
Length,
CpuArchAttributes
);
}
if (EFI_ERROR (Status)) {
CoreFreePool (TopEntry);
CoreFreePool (BottomEntry);
goto Done;
}
}
}
//
// Convert/Insert the list of descriptors from StartLink to EndLink
//
Link = StartLink;
while (Link != EndLink->ForwardLink) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
CoreInsertGcdMapEntry (Link, Entry, BaseAddress, Length, TopEntry, BottomEntry);
switch (Operation) {
//
// Add operations
//
case GCD_ADD_MEMORY_OPERATION:
Entry->GcdMemoryType = GcdMemoryType;
if (GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {
Entry->Capabilities = Capabilities | EFI_MEMORY_RUNTIME | EFI_MEMORY_PORT_IO;
} else {
Entry->Capabilities = Capabilities | EFI_MEMORY_RUNTIME;
}
break;
case GCD_ADD_IO_OPERATION:
Entry->GcdIoType = GcdIoType;
break;
//
// Free operations
//
case GCD_FREE_MEMORY_OPERATION:
case GCD_FREE_IO_OPERATION:
Entry->ImageHandle = NULL;
Entry->DeviceHandle = NULL;
break;
//
// Remove operations
//
case GCD_REMOVE_MEMORY_OPERATION:
Entry->GcdMemoryType = EfiGcdMemoryTypeNonExistent;
Entry->Capabilities = 0;
break;
case GCD_REMOVE_IO_OPERATION:
Entry->GcdIoType = EfiGcdIoTypeNonExistent;
break;
//
// Set attributes operation
//
case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:
Entry->Attributes = Attributes;
break;
//
// Set capabilities operation
//
case GCD_SET_CAPABILITIES_MEMORY_OPERATION:
Entry->Capabilities = Capabilities;
break;
}
Link = Link->ForwardLink;
}
//
// Cleanup
//
Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);
Done:
DEBUG ((DEBUG_GCD, " Status = %r\n", Status));
if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
CoreReleaseGcdMemoryLock ();
CoreDumpGcdMemorySpaceMap (FALSE);
}
if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {
CoreReleaseGcdIoLock ();
CoreDumpGcdIoSpaceMap (FALSE);
}
return Status;
}
/**
Check whether an entry could be used to allocate space.
@param Operation Allocate memory or IO
@param Entry The entry to be tested
@param GcdMemoryType The desired memory type
@param GcdIoType The desired IO type
@retval EFI_NOT_FOUND The memory type does not match or there's an
image handle on the entry.
@retval EFI_UNSUPPORTED The operation unsupported.
@retval EFI_SUCCESS It's ok for this entry to be used to allocate
space.
**/
EFI_STATUS
CoreAllocateSpaceCheckEntry (
IN UINTN Operation,
IN EFI_GCD_MAP_ENTRY *Entry,
IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
IN EFI_GCD_IO_TYPE GcdIoType
)
{
if (Entry->ImageHandle != NULL) {
return EFI_NOT_FOUND;
}
switch (Operation) {
case GCD_ALLOCATE_MEMORY_OPERATION:
if (Entry->GcdMemoryType != GcdMemoryType) {