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
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
|
/** @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 - 2014, 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"
#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_16_BIT_IO | \
EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
EFI_RESOURCE_ATTRIBUTE_64_BIT_IO )
#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 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_PRESENT, EFI_MEMORY_PRESENT, FALSE },
{ EFI_RESOURCE_ATTRIBUTE_INITIALIZED, EFI_MEMORY_INITIALIZED, FALSE },
{ EFI_RESOURCE_ATTRIBUTE_TESTED, EFI_MEMORY_TESTED, FALSE },
{ 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
"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));
}
/**
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
)
{
*TopEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));
if (*TopEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
*BottomEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));
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
)
{
if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {
return EFI_MEMORY_UC;
}
if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {
return EFI_MEMORY_WC;
}
if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {
return EFI_MEMORY_WT;
}
if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {
return EFI_MEMORY_WB;
}
if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {
return EFI_MEMORY_WP;
}
return INVALID_CPU_ARCH_ATTRIBUTES;
}
/**
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) {
return EFI_NOT_FOUND;
}
break;
case GCD_ALLOCATE_IO_OPERATION:
if (Entry->GcdIoType != GcdIoType) {
return EFI_NOT_FOUND;
}
break;
default:
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}
/**
Allocate space on specified address and length.
@param Operation The type of operation (memory or IO)
@param GcdAllocateType The type of allocate operation
@param GcdMemoryType The desired memory type
@param GcdIoType The desired IO type
@param Alignment Align with 2^Alignment
@param Length Length to allocate
@param BaseAddress Base address to allocate
@param ImageHandle The image handle consume the allocated space.
@param DeviceHandle The device handle consume the allocated space.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_NOT_FOUND No descriptor for the desired space exists.
@retval EFI_SUCCESS Space successfully allocated.
**/
EFI_STATUS
CoreAllocateSpace (
IN UINTN Operation,
IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,
IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
IN EFI_GCD_IO_TYPE GcdIoType,
IN UINTN Alignment,
IN UINT64 Length,
IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
IN EFI_HANDLE ImageHandle,
IN EFI_HANDLE DeviceHandle OPTIONAL
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS AlignmentMask;
EFI_PHYSICAL_ADDRESS MaxAddress;
LIST_ENTRY *Map;
LIST_ENTRY *Link;
LIST_ENTRY *SubLink;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_MAP_ENTRY *TopEntry;
EFI_GCD_MAP_ENTRY *BottomEntry;
LIST_ENTRY *StartLink;
LIST_ENTRY *EndLink;
BOOLEAN Found;
//
// Make sure parameters are valid
//
if ((UINT32)GcdAllocateType >= EfiGcdMaxAllocateType) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_INVALID_PARAMETER));
return EFI_INVALID_PARAMETER;
}
if ((UINT32)GcdMemoryType >= EfiGcdMemoryTypeMaximum) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_INVALID_PARAMETER));
return EFI_INVALID_PARAMETER;
}
if ((UINT32)GcdIoType >= EfiGcdIoTypeMaximum) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_INVALID_PARAMETER));
return EFI_INVALID_PARAMETER;
}
if (BaseAddress == NULL) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_INVALID_PARAMETER));
return EFI_INVALID_PARAMETER;
}
if (ImageHandle == NULL) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_INVALID_PARAMETER));
return EFI_INVALID_PARAMETER;
}
if (Alignment >= 64) {
DEBUG ((DEBUG_GCD, " Status = %r\n", EFI_NOT_FOUND));
return EFI_NOT_FOUND;
}
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);
}
Found = FALSE;
StartLink = NULL;
EndLink = NULL;
//
// Compute alignment bit mask
//
AlignmentMask = LShiftU64 (1, Alignment) - 1;
if (GcdAllocateType == EfiGcdAllocateAddress) {
//
// Verify that the BaseAddress passed in is aligned correctly
//
if ((*BaseAddress & AlignmentMask) != 0) {
Status = EFI_NOT_FOUND;
goto Done;
}
//
// 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_NOT_FOUND;
goto Done;
}
ASSERT (StartLink != NULL && EndLink != NULL);
//
// Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
//
Link = StartLink;
while (Link != EndLink->ForwardLink) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
Link = Link->ForwardLink;
Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);
if (EFI_ERROR (Status)) {
goto Done;
}
}
Found = TRUE;
} else {
Entry = CR (Map->BackLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
//
// Compute the maximum address to use in the search algorithm
//
if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchBottomUp ||
GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ) {
MaxAddress = *BaseAddress;
} else {
MaxAddress = Entry->EndAddress;
}
//
// Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
//
if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||
GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {
Link = Map->BackLink;
} else {
Link = Map->ForwardLink;
}
while (Link != Map) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||
GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {
Link = Link->BackLink;
} else {
Link = Link->ForwardLink;
}
Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);
if (EFI_ERROR (Status)) {
continue;
}
if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||
GcdAllocateType == EfiGcdAllocateAnySearchTopDown) {
if ((Entry->BaseAddress + Length) > MaxAddress) {
continue;
}
if (Length > (Entry->EndAddress + 1)) {
Status = EFI_NOT_FOUND;
goto Done;
}
if (Entry->EndAddress > MaxAddress) {
*BaseAddress = MaxAddress;
} else {
*BaseAddress = Entry->EndAddress;
}
*BaseAddress = (*BaseAddress + 1 - Length) & (~AlignmentMask);
} else {
*BaseAddress = (Entry->BaseAddress + AlignmentMask) & (~AlignmentMask);
if ((*BaseAddress + Length - 1) > MaxAddress) {
Status = EFI_NOT_FOUND;
goto Done;
}
}
//
// 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_NOT_FOUND;
goto Done;
}
ASSERT (StartLink != NULL && EndLink != NULL);
Link = StartLink;
//
// Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
//
Found = TRUE;
SubLink = StartLink;
while (SubLink != EndLink->ForwardLink) {
Entry = CR (SubLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);
if (EFI_ERROR (Status)) {
Link = SubLink;
Found = FALSE;
break;
}
SubLink = SubLink->ForwardLink;
}
if (Found) {
break;
}
}
}
if (!Found) {
Status = EFI_NOT_FOUND;
goto Done;
}
//
// 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);
//
// 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);
Entry->ImageHandle = ImageHandle;
Entry->DeviceHandle = DeviceHandle;
Link = Link->ForwardLink;
}
//
// Cleanup
//
Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);
Done:
DEBUG ((DEBUG_GCD, " Status = %r", Status));
if (!EFI_ERROR (Status)) {
DEBUG ((DEBUG_GCD, " (BaseAddress = %016lx)", *BaseAddress));
}
DEBUG ((DEBUG_GCD, "\n"));
if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
CoreReleaseGcdMemoryLock ();
CoreDumpGcdMemorySpaceMap (FALSE);
}
if ((Operation & GCD_IO_SPACE_OPERATION) !=0) {
CoreReleaseGcdIoLock ();
CoreDumpGcdIoSpaceMap (FALSE);
}
return Status;
}
/**
Add a segment of memory to GCD map.
@param GcdMemoryType Memory type of the segment.
@param BaseAddress Base address of the segment.
@param Length Length of the segment.
@param Capabilities alterable attributes of the segment.
@retval EFI_INVALID_PARAMETER Invalid parameters.
@retval EFI_SUCCESS Successfully add a segment of memory space.
**/
EFI_STATUS
CoreInternalAddMemorySpace (
IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Capabilities
)
{
DEBUG ((DEBUG_GCD, "GCD:AddMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
DEBUG ((DEBUG_GCD, " GcdMemoryType = %a\n", mGcdMemoryTypeNames[MIN (GcdMemoryType, EfiGcdMemoryTypeMaximum)]));
DEBUG ((DEBUG_GCD, " Capabilities = %016lx\n", Capabilities));
//
// Make sure parameters are valid
//
if (GcdMemoryType <= EfiGcdMemoryTypeNonExistent || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {
return EFI_INVALID_PARAMETER;
}
return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION, GcdMemoryType, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, Capabilities, 0);
}
//
// GCD Core Services
//
/**
Allocates nonexistent memory, reserved memory, system memory, or memorymapped
I/O resources from the global coherency domain of the processor.
@param GcdAllocateType The type of allocate operation
@param GcdMemoryType The desired memory type
@param Alignment Align with 2^Alignment
@param Length Length to allocate
@param BaseAddress Base address to allocate
@param ImageHandle The image handle consume the allocated space.
@param DeviceHandle The device handle consume the allocated space.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_NOT_FOUND No descriptor contains the desired space.
@retval EFI_SUCCESS Memory space successfully allocated.
**/
EFI_STATUS
EFIAPI
CoreAllocateMemorySpace (
IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,
IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
IN UINTN Alignment,
IN UINT64 Length,
IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
IN EFI_HANDLE ImageHandle,
IN EFI_HANDLE DeviceHandle OPTIONAL
)
{
DEBUG ((DEBUG_GCD, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress, Length));
DEBUG ((DEBUG_GCD, " GcdAllocateType = %a\n", mGcdAllocationTypeNames[MIN (GcdAllocateType, EfiGcdMaxAllocateType)]));
DEBUG ((DEBUG_GCD, " GcdMemoryType = %a\n", mGcdMemoryTypeNames[MIN (GcdMemoryType, EfiGcdMemoryTypeMaximum)]));
DEBUG ((DEBUG_GCD, " Alignment = %016lx\n", LShiftU64 (1, Alignment)));
DEBUG ((DEBUG_GCD, " ImageHandle = %p\n", ImageHandle));
DEBUG ((DEBUG_GCD, " DeviceHandle = %p\n", DeviceHandle));
return CoreAllocateSpace (
GCD_ALLOCATE_MEMORY_OPERATION,
GcdAllocateType,
GcdMemoryType,
(EFI_GCD_IO_TYPE) 0,
Alignment,
Length,
BaseAddress,
ImageHandle,
DeviceHandle
);
}
/**
Adds reserved memory, system memory, or memory-mapped I/O resources to the
global coherency domain of the processor.
@param GcdMemoryType Memory type of the memory space.
@param BaseAddress Base address of the memory space.
@param Length Length of the memory space.
@param Capabilities alterable attributes of the memory space.
@retval EFI_SUCCESS Merged this memory space into GCD map.
**/
EFI_STATUS
EFIAPI
CoreAddMemorySpace (
IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Capabilities
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS PageBaseAddress;
UINT64 PageLength;
Status = CoreInternalAddMemorySpace (GcdMemoryType, BaseAddress, Length, Capabilities);
if (!EFI_ERROR (Status) && GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
PageBaseAddress = PageAlignLength (BaseAddress);
PageLength = PageAlignLength (BaseAddress + Length - PageBaseAddress);
Status = CoreAllocateMemorySpace (
EfiGcdAllocateAddress,
GcdMemoryType,
EFI_PAGE_SHIFT,
PageLength,
&PageBaseAddress,
gDxeCoreImageHandle,
NULL
);
if (!EFI_ERROR (Status)) {
CoreAddMemoryDescriptor (
EfiConventionalMemory,
PageBaseAddress,
RShiftU64 (PageLength, EFI_PAGE_SHIFT),
Capabilities
);
} else {
for (; PageLength != 0; PageLength -= EFI_PAGE_SIZE, PageBaseAddress += EFI_PAGE_SIZE) {
Status = CoreAllocateMemorySpace (
EfiGcdAllocateAddress,
GcdMemoryType,
EFI_PAGE_SHIFT,
EFI_PAGE_SIZE,
&PageBaseAddress,
gDxeCoreImageHandle,
NULL
);
if (!EFI_ERROR (Status)) {
CoreAddMemoryDescriptor (
EfiConventionalMemory,
PageBaseAddress,
1,
Capabilities
);
}
}
}
}
return Status;
}
/**
Frees nonexistent memory, reserved memory, system memory, or memory-mapped
I/O resources from the global coherency domain of the processor.
@param BaseAddress Base address of the memory space.
@param Length Length of the memory space.
@retval EFI_SUCCESS Space successfully freed.
**/
EFI_STATUS
EFIAPI
CoreFreeMemorySpace (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length
)
{
DEBUG ((DEBUG_GCD, "GCD:FreeMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
}
/**
Removes reserved memory, system memory, or memory-mapped I/O resources from
the global coherency domain of the processor.
@param BaseAddress Base address of the memory space.
@param Length Length of the memory space.
@retval EFI_SUCCESS Successfully remove a segment of memory space.
**/
EFI_STATUS
EFIAPI
CoreRemoveMemorySpace (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length
)
{
DEBUG ((DEBUG_GCD, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
}
/**
Build a memory descriptor according to an entry.
@param Descriptor The descriptor to be built
@param Entry According to this entry
**/
VOID
BuildMemoryDescriptor (
IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor,
IN EFI_GCD_MAP_ENTRY *Entry
)
{
Descriptor->BaseAddress = Entry->BaseAddress;
Descriptor->Length = Entry->EndAddress - Entry->BaseAddress + 1;
Descriptor->Capabilities = Entry->Capabilities;
Descriptor->Attributes = Entry->Attributes;
Descriptor->GcdMemoryType = Entry->GcdMemoryType;
Descriptor->ImageHandle = Entry->ImageHandle;
Descriptor->DeviceHandle = Entry->DeviceHandle;
}
/**
Retrieves the descriptor for a memory region containing a specified address.
@param BaseAddress Specified start address
@param Descriptor Specified length
@retval EFI_INVALID_PARAMETER Invalid parameter
@retval EFI_SUCCESS Successfully get memory space descriptor.
**/
EFI_STATUS
EFIAPI
CoreGetMemorySpaceDescriptor (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor
)
{
EFI_STATUS Status;
LIST_ENTRY *StartLink;
LIST_ENTRY *EndLink;
EFI_GCD_MAP_ENTRY *Entry;
//
// Make sure parameters are valid
//
if (Descriptor == NULL) {
return EFI_INVALID_PARAMETER;
}
CoreAcquireGcdMemoryLock ();
//
// Search for the list of descriptors that contain BaseAddress
//
Status = CoreSearchGcdMapEntry (BaseAddress, 1, &StartLink, &EndLink, &mGcdMemorySpaceMap);
if (EFI_ERROR (Status)) {
Status = EFI_NOT_FOUND;
} else {
ASSERT (StartLink != NULL && EndLink != NULL);
//
// Copy the contents of the found descriptor into Descriptor
//
Entry = CR (StartLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
BuildMemoryDescriptor (Descriptor, Entry);
}
CoreReleaseGcdMemoryLock ();
return Status;
}
/**
Modifies the attributes for a memory region in the global coherency domain of the
processor.
@param BaseAddress Specified start address
@param Length Specified length
@param Attributes Specified attributes
@retval EFI_SUCCESS The attributes were set for the memory region.
@retval EFI_INVALID_PARAMETER Length is zero.
@retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
resource range specified by BaseAddress and Length.
@retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource
range specified by BaseAddress and Length.
@retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by
BaseAddress and Length cannot be modified.
@retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
the memory resource range.
@retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol is
not available yet.
**/
EFI_STATUS
EFIAPI
CoreSetMemorySpaceAttributes (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes
)
{
DEBUG ((DEBUG_GCD, "GCD:SetMemorySpaceAttributes(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
DEBUG ((DEBUG_GCD, " Attributes = %016lx\n", Attributes));
return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, Attributes);
}
/**
Modifies the capabilities for a memory region in the global coherency domain of the
processor.
@param BaseAddress The physical address that is the start address of a memory region.
@param Length The size in bytes of the memory region.
@param Capabilities The bit mask of capabilities that the memory region supports.
@retval EFI_SUCCESS The capabilities were set for the memory region.
@retval EFI_INVALID_PARAMETER Length is zero.
@retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the
memory region attributes currently in use.
@retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by
BaseAddress and Length cannot be modified.
@retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities
of the memory resource range.
**/
EFI_STATUS
EFIAPI
CoreSetMemorySpaceCapabilities (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Capabilities
)
{
EFI_STATUS Status;
DEBUG ((DEBUG_GCD, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
DEBUG ((DEBUG_GCD, " Capabilities = %016lx\n", Capabilities));
Status = CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, Capabilities, 0);
if (!EFI_ERROR(Status)) {
CoreUpdateMemoryAttributes(BaseAddress, RShiftU64(Length, EFI_PAGE_SHIFT), Capabilities);
}
return Status;
}
/**
Returns a map of the memory resources in the global coherency domain of the
processor.
@param NumberOfDescriptors Number of descriptors.
@param MemorySpaceMap Descriptor array
@retval EFI_INVALID_PARAMETER Invalid parameter
@retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
@retval EFI_SUCCESS Successfully get memory space map.
**/
EFI_STATUS
EFIAPI
CoreGetMemorySpaceMap (
OUT UINTN *NumberOfDescriptors,
OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR **MemorySpaceMap
)
{
EFI_STATUS Status;
LIST_ENTRY *Link;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor;
//
// Make sure parameters are valid
//
if (NumberOfDescriptors == NULL) {
return EFI_INVALID_PARAMETER;
}
if (MemorySpaceMap == NULL) {
return EFI_INVALID_PARAMETER;
}
CoreAcquireGcdMemoryLock ();
//
// Count the number of descriptors
//
*NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdMemorySpaceMap);
//
// Allocate the MemorySpaceMap
//
*MemorySpaceMap = AllocatePool (*NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
if (*MemorySpaceMap == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
//
// Fill in the MemorySpaceMap
//
Descriptor = *MemorySpaceMap;
Link = mGcdMemorySpaceMap.ForwardLink;
while (Link != &mGcdMemorySpaceMap) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
BuildMemoryDescriptor (Descriptor, Entry);
Descriptor++;
Link = Link->ForwardLink;
}
Status = EFI_SUCCESS;
Done:
CoreReleaseGcdMemoryLock ();
return Status;
}
/**
Adds reserved I/O or I/O resources to the global coherency domain of the processor.
@param GcdIoType IO type of the segment.
@param BaseAddress Base address of the segment.
@param Length Length of the segment.
@retval EFI_SUCCESS Merged this segment into GCD map.
@retval EFI_INVALID_PARAMETER Parameter not valid
**/
EFI_STATUS
EFIAPI
CoreAddIoSpace (
IN EFI_GCD_IO_TYPE GcdIoType,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length
)
{
DEBUG ((DEBUG_GCD, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
DEBUG ((DEBUG_GCD, " GcdIoType = %a\n", mGcdIoTypeNames[MIN (GcdIoType, EfiGcdIoTypeMaximum)]));
//
// Make sure parameters are valid
//
if (GcdIoType <= EfiGcdIoTypeNonExistent || GcdIoType >= EfiGcdIoTypeMaximum) {
return EFI_INVALID_PARAMETER;
}
return CoreConvertSpace (GCD_ADD_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, GcdIoType, BaseAddress, Length, 0, 0);
}
/**
Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
domain of the processor.
@param GcdAllocateType The type of allocate operation
@param GcdIoType The desired IO type
@param Alignment Align with 2^Alignment
@param Length Length to allocate
@param BaseAddress Base address to allocate
@param ImageHandle The image handle consume the allocated space.
@param DeviceHandle The device handle consume the allocated space.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_NOT_FOUND No descriptor contains the desired space.
@retval EFI_SUCCESS IO space successfully allocated.
**/
EFI_STATUS
EFIAPI
CoreAllocateIoSpace (
IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,
IN EFI_GCD_IO_TYPE GcdIoType,
IN UINTN Alignment,
IN UINT64 Length,
IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
IN EFI_HANDLE ImageHandle,
IN EFI_HANDLE DeviceHandle OPTIONAL
)
{
DEBUG ((DEBUG_GCD, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress, Length));
DEBUG ((DEBUG_GCD, " GcdAllocateType = %a\n", mGcdAllocationTypeNames[MIN (GcdAllocateType, EfiGcdMaxAllocateType)]));
DEBUG ((DEBUG_GCD, " GcdIoType = %a\n", mGcdIoTypeNames[MIN (GcdIoType, EfiGcdIoTypeMaximum)]));
DEBUG ((DEBUG_GCD, " Alignment = %016lx\n", LShiftU64 (1, Alignment)));
DEBUG ((DEBUG_GCD, " ImageHandle = %p\n", ImageHandle));
DEBUG ((DEBUG_GCD, " DeviceHandle = %p\n", DeviceHandle));
return CoreAllocateSpace (
GCD_ALLOCATE_IO_OPERATION,
GcdAllocateType,
(EFI_GCD_MEMORY_TYPE) 0,
GcdIoType,
Alignment,
Length,
BaseAddress,
ImageHandle,
DeviceHandle
);
}
/**
Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
domain of the processor.
@param BaseAddress Base address of the segment.
@param Length Length of the segment.
@retval EFI_SUCCESS Space successfully freed.
**/
EFI_STATUS
EFIAPI
CoreFreeIoSpace (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length
)
{
DEBUG ((DEBUG_GCD, "GCD:FreeIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
return CoreConvertSpace (GCD_FREE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
}
/**
Removes reserved I/O or I/O resources from the global coherency domain of the
processor.
@param BaseAddress Base address of the segment.
@param Length Length of the segment.
@retval EFI_SUCCESS Successfully removed a segment of IO space.
**/
EFI_STATUS
EFIAPI
CoreRemoveIoSpace (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length
)
{
DEBUG ((DEBUG_GCD, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));
return CoreConvertSpace (GCD_REMOVE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
}
/**
Build a IO descriptor according to an entry.
@param Descriptor The descriptor to be built
@param Entry According to this entry
**/
VOID
BuildIoDescriptor (
IN EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor,
IN EFI_GCD_MAP_ENTRY *Entry
)
{
Descriptor->BaseAddress = Entry->BaseAddress;
Descriptor->Length = Entry->EndAddress - Entry->BaseAddress + 1;
Descriptor->GcdIoType = Entry->GcdIoType;
Descriptor->ImageHandle = Entry->ImageHandle;
Descriptor->DeviceHandle = Entry->DeviceHandle;
}
/**
Retrieves the descriptor for an I/O region containing a specified address.
@param BaseAddress Specified start address
@param Descriptor Specified length
@retval EFI_INVALID_PARAMETER Descriptor is NULL.
@retval EFI_SUCCESS Successfully get the IO space descriptor.
**/
EFI_STATUS
EFIAPI
CoreGetIoSpaceDescriptor (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
OUT EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor
)
{
EFI_STATUS Status;
LIST_ENTRY *StartLink;
LIST_ENTRY *EndLink;
EFI_GCD_MAP_ENTRY *Entry;
//
// Make sure parameters are valid
//
if (Descriptor == NULL) {
return EFI_INVALID_PARAMETER;
}
CoreAcquireGcdIoLock ();
//
// Search for the list of descriptors that contain BaseAddress
//
Status = CoreSearchGcdMapEntry (BaseAddress, 1, &StartLink, &EndLink, &mGcdIoSpaceMap);
if (EFI_ERROR (Status)) {
Status = EFI_NOT_FOUND;
} else {
ASSERT (StartLink != NULL && EndLink != NULL);
//
// Copy the contents of the found descriptor into Descriptor
//
Entry = CR (StartLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
BuildIoDescriptor (Descriptor, Entry);
}
CoreReleaseGcdIoLock ();
return Status;
}
/**
Returns a map of the I/O resources in the global coherency domain of the processor.
@param NumberOfDescriptors Number of descriptors.
@param IoSpaceMap Descriptor array
@retval EFI_INVALID_PARAMETER Invalid parameter
@retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
@retval EFI_SUCCESS Successfully get IO space map.
**/
EFI_STATUS
EFIAPI
CoreGetIoSpaceMap (
OUT UINTN *NumberOfDescriptors,
OUT EFI_GCD_IO_SPACE_DESCRIPTOR **IoSpaceMap
)
{
EFI_STATUS Status;
LIST_ENTRY *Link;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor;
//
// Make sure parameters are valid
//
if (NumberOfDescriptors == NULL) {
return EFI_INVALID_PARAMETER;
}
if (IoSpaceMap == NULL) {
return EFI_INVALID_PARAMETER;
}
CoreAcquireGcdIoLock ();
//
// Count the number of descriptors
//
*NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdIoSpaceMap);
//
// Allocate the IoSpaceMap
//
*IoSpaceMap = AllocatePool (*NumberOfDescriptors * sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR));
if (*IoSpaceMap == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
//
// Fill in the IoSpaceMap
//
Descriptor = *IoSpaceMap;
Link = mGcdIoSpaceMap.ForwardLink;
while (Link != &mGcdIoSpaceMap) {
Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
BuildIoDescriptor (Descriptor, Entry);
Descriptor++;
Link = Link->ForwardLink;
}
Status = EFI_SUCCESS;
Done:
CoreReleaseGcdIoLock ();
return Status;
}
/**
Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
capabilities mask
@param GcdMemoryType Type of resource in the GCD memory map.
@param Attributes The attribute mask in the Resource Descriptor
HOB.
@return The capabilities mask for an EFI Memory Descriptor.
**/
UINT64
CoreConvertResourceDescriptorHobAttributesToCapabilities (
EFI_GCD_MEMORY_TYPE GcdMemoryType,
UINT64 Attributes
)
{
UINT64 Capabilities;
GCD_ATTRIBUTE_CONVERSION_ENTRY *Conversion;
//
// Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
//
for (Capabilities = 0, Conversion = mAttributeConversionTable; Conversion->Attribute != 0; Conversion++) {
if (Conversion->Memory || (GcdMemoryType != EfiGcdMemoryTypeSystemMemory)) {
if (Attributes & Conversion->Attribute) {
Capabilities |= Conversion->Capability;
}
}
}
return Capabilities;
}
/**
External function. Initializes memory services based on the memory
descriptor HOBs. This function is responsible for priming the memory
map, so memory allocations and resource allocations can be made.
The first part of this function can not depend on any memory services
until at least one memory descriptor is provided to the memory services.
@param HobStart The start address of the HOB.
@param MemoryBaseAddress Start address of memory region found to init DXE
core.
@param MemoryLength Length of memory region found to init DXE core.
@retval EFI_SUCCESS Memory services successfully initialized.
**/
EFI_STATUS
CoreInitializeMemoryServices (
IN VOID **HobStart,
OUT EFI_PHYSICAL_ADDRESS *MemoryBaseAddress,
OUT UINT64 *MemoryLength
)
{
EFI_PEI_HOB_POINTERS Hob;
EFI_MEMORY_TYPE_INFORMATION *EfiMemoryTypeInformation;
UINTN DataSize;
BOOLEAN Found;
EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob;
EFI_PHYSICAL_ADDRESS BaseAddress;
UINT64 Length;
UINT64 Attributes;
UINT64 Capabilities;
EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress;
UINT64 MaxMemoryLength;
UINT64 MaxMemoryAttributes;
EFI_PHYSICAL_ADDRESS TestedMemoryBaseAddress;
UINT64 TestedMemoryLength;
EFI_PHYSICAL_ADDRESS HighAddress;
EFI_HOB_GUID_TYPE *GuidHob;
UINT32 ReservedCodePageNumber;
//
// Point at the first HOB. This must be the PHIT HOB.
//
Hob.Raw = *HobStart;
ASSERT (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_HANDOFF);
//
// Initialize the spin locks and maps in the memory services.
// Also fill in the memory services into the EFI Boot Services Table
//
CoreInitializePool ();
//
// Initialize Local Variables
//
PhitResourceHob = NULL;
ResourceHob = NULL;
BaseAddress = 0;
Length = 0;
Attributes = 0;
MaxMemoryBaseAddress = 0;
MaxMemoryLength = 0;
MaxMemoryAttributes = 0;
//
// Cache the PHIT HOB for later use
//
PhitHob = Hob.HandoffInformationTable;
if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
ReservedCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);
ReservedCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);
//
// cache the Top address for loading modules at Fixed Address
//
gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress = PhitHob->EfiMemoryTop
+ EFI_PAGES_TO_SIZE(ReservedCodePageNumber);
}
//
// See if a Memory Type Information HOB is available
//
GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
if (GuidHob != NULL) {
EfiMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
if (EfiMemoryTypeInformation != NULL && DataSize > 0 && DataSize <= (EfiMaxMemoryType + 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION)) {
CopyMem (&gMemoryTypeInformation, EfiMemoryTypeInformation, DataSize);
}
}
//
// Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
//
Length = 0;
Found = FALSE;
for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
//
// Skip all HOBs except Resource Descriptor HOBs
//
if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
continue;
}
//
// Skip Resource Descriptor HOBs that do not describe tested system memory
//
ResourceHob = Hob.ResourceDescriptor;
if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {
continue;
}
if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {
continue;
}
//
// Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
//
if (PhitHob->EfiFreeMemoryBottom < ResourceHob->PhysicalStart) {
continue;
}
if (PhitHob->EfiFreeMemoryTop > (ResourceHob->PhysicalStart + ResourceHob->ResourceLength)) {
continue;
}
//
// Cache the resource descriptor HOB for the memory region described by the PHIT HOB
//
PhitResourceHob = ResourceHob;
Found = TRUE;
//
// Compute range between PHIT EfiFreeMemoryTop and the end of the Resource Descriptor HOB
//
Attributes = PhitResourceHob->ResourceAttribute;
BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);
Length = PageAlignLength (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);
if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {
//
// If that range is not large enough to intialize the DXE Core, then
// Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop
//
BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);
Length = PageAlignLength (PhitHob->EfiFreeMemoryTop - BaseAddress);
if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {
//
// If that range is not large enough to intialize the DXE Core, then
// Compute range between the start of the Resource Descriptor HOB and the start of the HOB List
//
BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);
Length = PageAlignLength ((UINT64)((UINTN)*HobStart - BaseAddress));
}
}
break;
}
//
// Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
//
ASSERT (Found);
//
// Search all the resource descriptor HOBs from the highest possible addresses down for a memory
// region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
// The max address must be within the physically addressible range for the processor.
//
HighAddress = MAX_ADDRESS;
for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
//
// Skip the Resource Descriptor HOB that contains the PHIT
//
if (Hob.ResourceDescriptor == PhitResourceHob) {
continue;
}
//
// Skip all HOBs except Resource Descriptor HOBs
//
if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
continue;
}
//
// Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS
//
ResourceHob = Hob.ResourceDescriptor;
if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {
continue;
}
if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {
continue;
}
if ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength) > (EFI_PHYSICAL_ADDRESS)MAX_ADDRESS) {
continue;
}
//
// Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB
//
if (HighAddress != (EFI_PHYSICAL_ADDRESS)MAX_ADDRESS && ResourceHob->PhysicalStart <= HighAddress) {
continue;
}
//
// Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core
//
TestedMemoryBaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);
TestedMemoryLength = PageAlignLength (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - TestedMemoryBaseAddress);
if (TestedMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE) {
continue;
}
//
// Save the Resource Descriptor HOB context that is large enough to initilize the DXE Core
//
MaxMemoryBaseAddress = TestedMemoryBaseAddress;
MaxMemoryLength = TestedMemoryLength;
MaxMemoryAttributes = ResourceHob->ResourceAttribute;
HighAddress = ResourceHob->PhysicalStart;
}
//
// If Length is not large enough to initialize the DXE Core or a Resource
// Descriptor HOB was found above the PHIT HOB that is large enough to initialize
// the DXE Core, then use the range described by the Resource Descriptor
// HOB that was found above the PHIT HOB.
//
if ((Length < MINIMUM_INITIAL_MEMORY_SIZE) ||
(MaxMemoryBaseAddress > BaseAddress && MaxMemoryLength >= MINIMUM_INITIAL_MEMORY_SIZE)) {
BaseAddress = MaxMemoryBaseAddress;
Length = MaxMemoryLength;
Attributes = MaxMemoryAttributes;
}
//
// If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
//
ASSERT (Length >= MINIMUM_INITIAL_MEMORY_SIZE);
//
// Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
//
Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory, Attributes);
//
// Declare the very first memory region, so the EFI Memory Services are available.
//
CoreAddMemoryDescriptor (
EfiConventionalMemory,
BaseAddress,
RShiftU64 (Length, EFI_PAGE_SHIFT),
Capabilities
);
*MemoryBaseAddress = BaseAddress;
*MemoryLength = Length;
return EFI_SUCCESS;
}
/**
External function. Initializes the GCD and memory services based on the memory
descriptor HOBs. This function is responsible for priming the GCD map and the
memory map, so memory allocations and resource allocations can be made. The
HobStart will be relocated to a pool buffer.
@param HobStart The start address of the HOB
@param MemoryBaseAddress Start address of memory region found to init DXE
core.
@param MemoryLength Length of memory region found to init DXE core.
@retval EFI_SUCCESS GCD services successfully initialized.
**/
EFI_STATUS
CoreInitializeGcdServices (
IN OUT VOID **HobStart,
IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress,
IN UINT64 MemoryLength
)
{
EFI_PEI_HOB_POINTERS Hob;
VOID *NewHobList;
EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
UINT8 SizeOfMemorySpace;
UINT8 SizeOfIoSpace;
EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
EFI_PHYSICAL_ADDRESS BaseAddress;
UINT64 Length;
EFI_STATUS Status;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_MEMORY_TYPE GcdMemoryType;
EFI_GCD_IO_TYPE GcdIoType;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
EFI_HOB_MEMORY_ALLOCATION *MemoryHob;
EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;
UINTN NumberOfDescriptors;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;
UINTN Index;
UINT64 Capabilities;
EFI_HOB_CPU * CpuHob;
//
// Cache the PHIT HOB for later use
//
PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)(*HobStart);
//
// Get the number of address lines in the I/O and Memory space for the CPU
//
CpuHob = GetFirstHob (EFI_HOB_TYPE_CPU);
ASSERT (CpuHob != NULL);
SizeOfMemorySpace = CpuHob->SizeOfMemorySpace;
SizeOfIoSpace = CpuHob->SizeOfIoSpace;
//
// Initialize the GCD Memory Space Map
//
Entry = AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY), &mGcdMemorySpaceMapEntryTemplate);
ASSERT (Entry != NULL);
Entry->EndAddress = LShiftU64 (1, SizeOfMemorySpace) - 1;
InsertHeadList (&mGcdMemorySpaceMap, &Entry->Link);
CoreDumpGcdMemorySpaceMap (TRUE);
//
// Initialize the GCD I/O Space Map
//
Entry = AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY), &mGcdIoSpaceMapEntryTemplate);
ASSERT (Entry != NULL);
Entry->EndAddress = LShiftU64 (1, SizeOfIoSpace) - 1;
InsertHeadList (&mGcdIoSpaceMap, &Entry->Link);
CoreDumpGcdIoSpaceMap (TRUE);
//
// Walk the HOB list and add all resource descriptors to the GCD
//
for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
GcdMemoryType = EfiGcdMemoryTypeNonExistent;
GcdIoType = EfiGcdIoTypeNonExistent;
if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
ResourceHob = Hob.ResourceDescriptor;
switch (ResourceHob->ResourceType) {
case EFI_RESOURCE_SYSTEM_MEMORY:
if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES) {
GcdMemoryType = EfiGcdMemoryTypeSystemMemory;
}
if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == INITIALIZED_MEMORY_ATTRIBUTES) {
GcdMemoryType = EfiGcdMemoryTypeReserved;
}
if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == PRESENT_MEMORY_ATTRIBUTES) {
GcdMemoryType = EfiGcdMemoryTypeReserved;
}
break;
case EFI_RESOURCE_MEMORY_MAPPED_IO:
case EFI_RESOURCE_FIRMWARE_DEVICE:
GcdMemoryType = EfiGcdMemoryTypeMemoryMappedIo;
break;
case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT:
case EFI_RESOURCE_MEMORY_RESERVED:
GcdMemoryType = EfiGcdMemoryTypeReserved;
break;
case EFI_RESOURCE_IO:
GcdIoType = EfiGcdIoTypeIo;
break;
case EFI_RESOURCE_IO_RESERVED:
GcdIoType = EfiGcdIoTypeReserved;
break;
}
if (GcdMemoryType != EfiGcdMemoryTypeNonExistent) {
//
// Validate the Resource HOB Attributes
//
CoreValidateResourceDescriptorHobAttributes (ResourceHob->ResourceAttribute);
//
// Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
//
Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (
GcdMemoryType,
ResourceHob->ResourceAttribute
);
Status = CoreInternalAddMemorySpace (
GcdMemoryType,
ResourceHob->PhysicalStart,
ResourceHob->ResourceLength,
Capabilities
);
}
if (GcdIoType != EfiGcdIoTypeNonExistent) {
Status = CoreAddIoSpace (
GcdIoType,
ResourceHob->PhysicalStart,
ResourceHob->ResourceLength
);
}
}
}
//
// Allocate first memory region from the GCD by the DXE core
//
Status = CoreAllocateMemorySpace (
EfiGcdAllocateAddress,
EfiGcdMemoryTypeSystemMemory,
0,
MemoryLength,
&MemoryBaseAddress,
gDxeCoreImageHandle,
NULL
);
//
// Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
// and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
//
for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
MemoryHob = Hob.MemoryAllocation;
BaseAddress = MemoryHob->AllocDescriptor.MemoryBaseAddress;
Status = CoreGetMemorySpaceDescriptor (BaseAddress, &Descriptor);
if (!EFI_ERROR (Status)) {
Status = CoreAllocateMemorySpace (
EfiGcdAllocateAddress,
Descriptor.GcdMemoryType,
0,
MemoryHob->AllocDescriptor.MemoryLength,
&BaseAddress,
gDxeCoreImageHandle,
NULL
);
if (!EFI_ERROR (Status) && Descriptor.GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
CoreAddMemoryDescriptor (
MemoryHob->AllocDescriptor.MemoryType,
MemoryHob->AllocDescriptor.MemoryBaseAddress,
RShiftU64 (MemoryHob->AllocDescriptor.MemoryLength, EFI_PAGE_SHIFT),
Descriptor.Capabilities & (~EFI_MEMORY_RUNTIME)
);
}
}
}
if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {
FirmwareVolumeHob = Hob.FirmwareVolume;
BaseAddress = FirmwareVolumeHob->BaseAddress;
Status = CoreAllocateMemorySpace (
EfiGcdAllocateAddress,
EfiGcdMemoryTypeMemoryMappedIo,
0,
FirmwareVolumeHob->Length,
&BaseAddress,
gDxeCoreImageHandle,
NULL
);
}
}
//
// Relocate HOB List to an allocated pool buffer.
//
NewHobList = AllocateCopyPool (
(UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart),
*HobStart
);
ASSERT (NewHobList != NULL);
*HobStart = NewHobList;
gHobList = NewHobList;
//
// Add and allocate the remaining unallocated system memory to the memory services.
//
Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);
ASSERT (Status == EFI_SUCCESS);
for (Index = 0; Index < NumberOfDescriptors; Index++) {
if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
if (MemorySpaceMap[Index].ImageHandle == NULL) {
BaseAddress = PageAlignAddress (MemorySpaceMap[Index].BaseAddress);
Length = PageAlignLength (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - BaseAddress);
if (Length == 0 || MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length < BaseAddress) {
continue;
}
CoreAddMemoryDescriptor (
EfiConventionalMemory,
BaseAddress,
RShiftU64 (Length, EFI_PAGE_SHIFT),
MemorySpaceMap[Index].Capabilities & (~EFI_MEMORY_RUNTIME)
);
Status = CoreAllocateMemorySpace (
EfiGcdAllocateAddress,
EfiGcdMemoryTypeSystemMemory,
0,
Length,
&BaseAddress,
gDxeCoreImageHandle,
NULL
);
}
}
}
CoreFreePool (MemorySpaceMap);
return EFI_SUCCESS;
}
|