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
|
/**@file
Memory Detection for Virtual Machines.
Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
Module Name:
MemDetect.c
**/
//
// The package level header files this module uses
//
#include <IndustryStandard/E820.h>
#include <IndustryStandard/I440FxPiix4.h>
#include <IndustryStandard/Q35MchIch9.h>
#include <IndustryStandard/CloudHv.h>
#include <IndustryStandard/Xen/arch-x86/hvm/start_info.h>
#include <PiPei.h>
#include <Register/Intel/SmramSaveStateMap.h>
//
// The Library classes this module consumes
//
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/CcProbeLib.h>
#include <Library/DebugLib.h>
#include <Library/HardwareInfoLib.h>
#include <Library/HobLib.h>
#include <Library/IoLib.h>
#include <Library/MemEncryptSevLib.h>
#include <Library/PcdLib.h>
#include <Library/PciLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/ResourcePublicationLib.h>
#include <Library/MtrrLib.h>
#include <Library/QemuFwCfgLib.h>
#include <Library/QemuFwCfgSimpleParserLib.h>
#include <Library/TdxLib.h>
#include <Library/PlatformInitLib.h>
#include <Guid/AcpiS3Context.h>
#include <Guid/SmramMemoryReserve.h>
#define MEGABYTE_SHIFT 20
VOID
EFIAPI
PlatformQemuUc32BaseInitialization (
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {
return;
}
if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {
PlatformInfoHob->Uc32Size = CLOUDHV_MMIO_HOLE_SIZE;
PlatformInfoHob->Uc32Base = CLOUDHV_MMIO_HOLE_ADDRESS;
return;
}
ASSERT (
PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID ||
PlatformInfoHob->HostBridgeDevId == INTEL_82441_DEVICE_ID
);
PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {
ASSERT (PcdGet64 (PcdPciExpressBaseAddress) <= MAX_UINT32);
ASSERT (PcdGet64 (PcdPciExpressBaseAddress) >= PlatformInfoHob->LowMemory);
}
//
// Start with the [LowerMemorySize, 4GB) range. Make sure one
// variable MTRR suffices by truncating the size to a whole power of two,
// while keeping the end affixed to 4GB. This will round the base up.
//
PlatformInfoHob->Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - PlatformInfoHob->LowMemory));
PlatformInfoHob->Uc32Base = (UINT32)(SIZE_4GB - PlatformInfoHob->Uc32Size);
//
// Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.
// Therefore Uc32Base is at least 2GB.
//
ASSERT (PlatformInfoHob->Uc32Base >= BASE_2GB);
if (PlatformInfoHob->Uc32Base != PlatformInfoHob->LowMemory) {
DEBUG ((
DEBUG_VERBOSE,
"%a: rounded UC32 base from 0x%x up to 0x%x, for "
"an UC32 size of 0x%x\n",
__func__,
PlatformInfoHob->LowMemory,
PlatformInfoHob->Uc32Base,
PlatformInfoHob->Uc32Size
));
}
}
typedef VOID (*E820_SCAN_CALLBACK) (
EFI_E820_ENTRY64 *E820Entry,
EFI_HOB_PLATFORM_INFO *PlatformInfoHob
);
STATIC
EFI_STATUS
PlatformScanE820Tdx (
IN E820_SCAN_CALLBACK Callback,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
EFI_E820_ENTRY64 E820Entry;
EFI_PEI_HOB_POINTERS Hob;
Hob.Raw = (UINT8 *)(UINTN)FixedPcdGet32 (PcdOvmfSecGhcbBase);
while (!END_OF_HOB_LIST (Hob)) {
if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_UNACCEPTED) ||
(Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY))
{
E820Entry.BaseAddr = Hob.ResourceDescriptor->PhysicalStart;
E820Entry.Length = Hob.ResourceDescriptor->ResourceLength;
E820Entry.Type = EfiAcpiAddressRangeMemory;
Callback (&E820Entry, PlatformInfoHob);
}
}
Hob.Raw = (UINT8 *)(Hob.Raw + Hob.Header->HobLength);
}
return EFI_SUCCESS;
}
/**
Store first address not used by e820 RAM entries in
PlatformInfoHob->FirstNonAddress
**/
STATIC
VOID
PlatformGetFirstNonAddressCB (
IN EFI_E820_ENTRY64 *E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT64 Candidate;
if (E820Entry->Type != EfiAcpiAddressRangeMemory) {
return;
}
Candidate = E820Entry->BaseAddr + E820Entry->Length;
if (PlatformInfoHob->FirstNonAddress < Candidate) {
DEBUG ((DEBUG_INFO, "%a: FirstNonAddress=0x%Lx\n", __func__, Candidate));
PlatformInfoHob->FirstNonAddress = Candidate;
}
}
/**
Store the low (below 4G) memory size in
PlatformInfoHob->LowMemory
**/
STATIC
VOID
PlatformGetLowMemoryCB (
IN EFI_E820_ENTRY64 *E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT64 Candidate;
if (E820Entry->Type != EfiAcpiAddressRangeMemory) {
return;
}
Candidate = E820Entry->BaseAddr + E820Entry->Length;
if (Candidate >= BASE_4GB) {
return;
}
if (PlatformInfoHob->LowMemory < Candidate) {
DEBUG ((DEBUG_INFO, "%a: LowMemory=0x%Lx\n", __func__, Candidate));
PlatformInfoHob->LowMemory = (UINT32)Candidate;
}
}
/**
Create HOBs for reservations and RAM (except low memory).
**/
STATIC
VOID
PlatformAddHobCB (
IN EFI_E820_ENTRY64 *E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT64 Base, End;
Base = E820Entry->BaseAddr;
End = E820Entry->BaseAddr + E820Entry->Length;
switch (E820Entry->Type) {
case EfiAcpiAddressRangeMemory:
if (Base >= BASE_4GB) {
//
// Round up the start address, and round down the end address.
//
Base = ALIGN_VALUE (Base, (UINT64)EFI_PAGE_SIZE);
End = End & ~(UINT64)EFI_PAGE_MASK;
if (Base < End) {
DEBUG ((DEBUG_INFO, "%a: HighMemory [0x%Lx, 0x%Lx)\n", __func__, Base, End));
PlatformAddMemoryRangeHob (Base, End);
}
}
break;
case EfiAcpiAddressRangeReserved:
BuildResourceDescriptorHob (EFI_RESOURCE_MEMORY_RESERVED, 0, Base, End - Base);
DEBUG ((DEBUG_INFO, "%a: Reserved [0x%Lx, 0x%Lx)\n", __func__, Base, End));
break;
default:
DEBUG ((
DEBUG_WARN,
"%a: Type %u [0x%Lx, 0x%Lx) (NOT HANDLED)\n",
__func__,
E820Entry->Type,
Base,
End
));
break;
}
}
/**
Check whenever the 64bit PCI MMIO window overlaps with a reservation
from qemu. If so move down the MMIO window to resolve the conflict.
This happens on (virtual) AMD machines with 1TB address space,
because the AMD IOMMU uses an address window just below 1TB.
**/
STATIC
VOID
PlatformReservationConflictCB (
IN EFI_E820_ENTRY64 *E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT64 IntersectionBase;
UINT64 IntersectionEnd;
UINT64 NewBase;
IntersectionBase = MAX (
E820Entry->BaseAddr,
PlatformInfoHob->PcdPciMmio64Base
);
IntersectionEnd = MIN (
E820Entry->BaseAddr + E820Entry->Length,
PlatformInfoHob->PcdPciMmio64Base +
PlatformInfoHob->PcdPciMmio64Size
);
if (IntersectionBase >= IntersectionEnd) {
return; // no overlap
}
NewBase = E820Entry->BaseAddr - PlatformInfoHob->PcdPciMmio64Size;
NewBase = NewBase & ~(PlatformInfoHob->PcdPciMmio64Size - 1);
DEBUG ((
DEBUG_INFO,
"%a: move mmio: 0x%Lx => %Lx\n",
__func__,
PlatformInfoHob->PcdPciMmio64Base,
NewBase
));
PlatformInfoHob->PcdPciMmio64Base = NewBase;
}
/**
Returns PVH memmap
@param Entries Pointer to PVH memmap
@param Count Number of entries
@return EFI_STATUS
**/
EFI_STATUS
GetPvhMemmapEntries (
struct hvm_memmap_table_entry **Entries,
UINT32 *Count
)
{
UINT32 *PVHResetVectorData;
struct hvm_start_info *pvh_start_info;
PVHResetVectorData = (VOID *)(UINTN)PcdGet32 (PcdXenPvhStartOfDayStructPtr);
if (PVHResetVectorData == 0) {
return EFI_NOT_FOUND;
}
pvh_start_info = (struct hvm_start_info *)(UINTN)PVHResetVectorData[0];
*Entries = (struct hvm_memmap_table_entry *)(UINTN)pvh_start_info->memmap_paddr;
*Count = pvh_start_info->memmap_entries;
return EFI_SUCCESS;
}
STATIC
EFI_STATUS
PlatformScanE820Pvh (
IN E820_SCAN_CALLBACK Callback,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
struct hvm_memmap_table_entry *Memmap;
UINT32 MemmapEntriesCount;
struct hvm_memmap_table_entry *Entry;
EFI_E820_ENTRY64 E820Entry;
EFI_STATUS Status;
UINT32 Loop;
Status = GetPvhMemmapEntries (&Memmap, &MemmapEntriesCount);
if (EFI_ERROR (Status)) {
return Status;
}
for (Loop = 0; Loop < MemmapEntriesCount; Loop++) {
Entry = Memmap + Loop;
if (Entry->type == XEN_HVM_MEMMAP_TYPE_RAM) {
E820Entry.BaseAddr = Entry->addr;
E820Entry.Length = Entry->size;
E820Entry.Type = Entry->type;
Callback (&E820Entry, PlatformInfoHob);
}
}
return EFI_SUCCESS;
}
/**
Iterate over the entries in QEMU's fw_cfg E820 RAM map, call the
passed callback for each entry.
@param[in] Callback The callback function to be called.
@param[in out] PlatformInfoHob PlatformInfo struct which is passed
through to the callback.
@retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.
@retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a
whole multiple of sizeof(EFI_E820_ENTRY64). No
RAM entry was processed.
@return Error codes from QemuFwCfgFindFile(). No RAM
entry was processed.
**/
STATIC
EFI_STATUS
PlatformScanE820 (
IN E820_SCAN_CALLBACK Callback,
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
EFI_STATUS Status;
FIRMWARE_CONFIG_ITEM FwCfgItem;
UINTN FwCfgSize;
EFI_E820_ENTRY64 E820Entry;
UINTN Processed;
if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {
return PlatformScanE820Pvh (Callback, PlatformInfoHob);
}
if (TdIsEnabled ()) {
return PlatformScanE820Tdx (Callback, PlatformInfoHob);
}
Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);
if (EFI_ERROR (Status)) {
return Status;
}
if (FwCfgSize % sizeof E820Entry != 0) {
return EFI_PROTOCOL_ERROR;
}
QemuFwCfgSelectItem (FwCfgItem);
for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {
QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);
Callback (&E820Entry, PlatformInfoHob);
}
return EFI_SUCCESS;
}
STATIC
UINT64
GetHighestSystemMemoryAddressFromPvhMemmap (
BOOLEAN Below4gb
)
{
struct hvm_memmap_table_entry *Memmap;
UINT32 MemmapEntriesCount;
struct hvm_memmap_table_entry *Entry;
EFI_STATUS Status;
UINT32 Loop;
UINT64 HighestAddress;
UINT64 EntryEnd;
HighestAddress = 0;
Status = GetPvhMemmapEntries (&Memmap, &MemmapEntriesCount);
ASSERT_EFI_ERROR (Status);
for (Loop = 0; Loop < MemmapEntriesCount; Loop++) {
Entry = Memmap + Loop;
EntryEnd = Entry->addr + Entry->size;
if ((Entry->type == XEN_HVM_MEMMAP_TYPE_RAM) &&
(EntryEnd > HighestAddress))
{
if (Below4gb && (EntryEnd <= BASE_4GB)) {
HighestAddress = EntryEnd;
} else if (!Below4gb && (EntryEnd >= BASE_4GB)) {
HighestAddress = EntryEnd;
}
}
}
return HighestAddress;
}
VOID
EFIAPI
PlatformGetSystemMemorySizeBelow4gb (
IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
EFI_STATUS Status;
UINT8 Cmos0x34;
UINT8 Cmos0x35;
if ((PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) &&
(CcProbe () != CcGuestTypeIntelTdx))
{
// Get the information from PVH memmap
PlatformInfoHob->LowMemory = (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);
return;
}
Status = PlatformScanE820 (PlatformGetLowMemoryCB, PlatformInfoHob);
if (!EFI_ERROR (Status) && (PlatformInfoHob->LowMemory > 0)) {
return;
}
//
// CMOS 0x34/0x35 specifies the system memory above 16 MB.
// * CMOS(0x35) is the high byte
// * CMOS(0x34) is the low byte
// * The size is specified in 64kb chunks
// * Since this is memory above 16MB, the 16MB must be added
// into the calculation to get the total memory size.
//
Cmos0x34 = (UINT8)PlatformCmosRead8 (0x34);
Cmos0x35 = (UINT8)PlatformCmosRead8 (0x35);
PlatformInfoHob->LowMemory = (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);
}
STATIC
UINT64
PlatformGetSystemMemorySizeAbove4gb (
)
{
UINT32 Size;
UINTN CmosIndex;
//
// CMOS 0x5b-0x5d specifies the system memory above 4GB MB.
// * CMOS(0x5d) is the most significant size byte
// * CMOS(0x5c) is the middle size byte
// * CMOS(0x5b) is the least significant size byte
// * The size is specified in 64kb chunks
//
Size = 0;
for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {
Size = (UINT32)(Size << 8) + (UINT32)PlatformCmosRead8 (CmosIndex);
}
return LShiftU64 (Size, 16);
}
/**
Return the highest address that DXE could possibly use, plus one.
**/
STATIC
VOID
PlatformGetFirstNonAddress (
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT32 FwCfgPciMmio64Mb;
EFI_STATUS Status;
FIRMWARE_CONFIG_ITEM FwCfgItem;
UINTN FwCfgSize;
UINT64 HotPlugMemoryEnd;
//
// If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM
// address from it. This can express an address >= 4GB+1TB.
//
// Otherwise, get the flat size of the memory above 4GB from the CMOS (which
// can only express a size smaller than 1TB), and add it to 4GB.
//
PlatformInfoHob->FirstNonAddress = BASE_4GB;
Status = PlatformScanE820 (PlatformGetFirstNonAddressCB, PlatformInfoHob);
if (EFI_ERROR (Status)) {
PlatformInfoHob->FirstNonAddress = BASE_4GB + PlatformGetSystemMemorySizeAbove4gb ();
}
//
// If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO
// resources to 32-bit anyway. See DegradeResource() in
// "PciResourceSupport.c".
//
#ifdef MDE_CPU_IA32
if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
return;
}
#endif
//
// See if the user specified the number of megabytes for the 64-bit PCI host
// aperture. Accept an aperture size up to 16TB.
//
// As signaled by the "X-" prefix, this knob is experimental, and might go
// away at any time.
//
Status = QemuFwCfgParseUint32 (
"opt/ovmf/X-PciMmio64Mb",
FALSE,
&FwCfgPciMmio64Mb
);
switch (Status) {
case EFI_UNSUPPORTED:
case EFI_NOT_FOUND:
break;
case EFI_SUCCESS:
if (FwCfgPciMmio64Mb <= 0x1000000) {
PlatformInfoHob->PcdPciMmio64Size = LShiftU64 (FwCfgPciMmio64Mb, 20);
PlatformInfoHob->PcdPciMmio64Override = TRUE;
break;
}
//
// fall through
//
default:
DEBUG ((
DEBUG_WARN,
"%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",
__func__
));
break;
}
if (PlatformInfoHob->PcdPciMmio64Size == 0) {
if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {
DEBUG ((
DEBUG_INFO,
"%a: disabling 64-bit PCI host aperture\n",
__func__
));
}
//
// There's nothing more to do; the amount of memory above 4GB fully
// determines the highest address plus one. The memory hotplug area (see
// below) plays no role for the firmware in this case.
//
return;
}
//
// The "etc/reserved-memory-end" fw_cfg file, when present, contains an
// absolute, exclusive end address for the memory hotplug area. This area
// starts right at the end of the memory above 4GB. The 64-bit PCI host
// aperture must be placed above it.
//
Status = QemuFwCfgFindFile (
"etc/reserved-memory-end",
&FwCfgItem,
&FwCfgSize
);
if (!EFI_ERROR (Status) && (FwCfgSize == sizeof HotPlugMemoryEnd)) {
QemuFwCfgSelectItem (FwCfgItem);
QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);
DEBUG ((
DEBUG_VERBOSE,
"%a: HotPlugMemoryEnd=0x%Lx\n",
__func__,
HotPlugMemoryEnd
));
ASSERT (HotPlugMemoryEnd >= PlatformInfoHob->FirstNonAddress);
PlatformInfoHob->FirstNonAddress = HotPlugMemoryEnd;
}
//
// SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so
// that the host can map it with 1GB hugepages. Follow suit.
//
PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (PlatformInfoHob->FirstNonAddress, (UINT64)SIZE_1GB);
PlatformInfoHob->PcdPciMmio64Size = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Size, (UINT64)SIZE_1GB);
//
// The 64-bit PCI host aperture should also be "naturally" aligned. The
// alignment is determined by rounding the size of the aperture down to the
// next smaller or equal power of two. That is, align the aperture by the
// largest BAR size that can fit into it.
//
PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Base, GetPowerOfTwo64 (PlatformInfoHob->PcdPciMmio64Size));
//
// The useful address space ends with the 64-bit PCI host aperture.
//
PlatformInfoHob->FirstNonAddress = PlatformInfoHob->PcdPciMmio64Base + PlatformInfoHob->PcdPciMmio64Size;
return;
}
/*
* Use CPUID to figure physical address width.
*
* Does *not* work reliable on qemu. For historical reasons qemu
* returns phys-bits=40 by default even in case the host machine
* supports less than that.
*
* So we apply the following rules (which can be enabled/disabled
* using the QemuQuirk parameter) to figure whenever we can work with
* the returned physical address width or not:
*
* (1) If it is 41 or higher consider it valid.
* (2) If it is 40 or lower consider it valid in case it matches a
* known-good value for the CPU vendor, which is:
* -> 36 or 39 for Intel
* -> 40 for AMD
* (3) Otherwise consider it invalid.
*
* Recommendation: Run qemu with host-phys-bits=on. That will make
* sure guest phys-bits is not larger than host phys-bits. Some
* distro builds do that by default.
*/
VOID
EFIAPI
PlatformAddressWidthFromCpuid (
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob,
IN BOOLEAN QemuQuirk
)
{
UINT32 RegEax, RegEbx, RegEcx, RegEdx, Max;
UINT8 PhysBits;
UINT8 GuestPhysBits;
CHAR8 Signature[13];
IA32_CR4 Cr4;
BOOLEAN Valid = FALSE;
BOOLEAN Page1GSupport = FALSE;
ZeroMem (Signature, sizeof (Signature));
AsmCpuid (0x80000000, &RegEax, &RegEbx, &RegEcx, &RegEdx);
*(UINT32 *)(Signature + 0) = RegEbx;
*(UINT32 *)(Signature + 4) = RegEdx;
*(UINT32 *)(Signature + 8) = RegEcx;
Max = RegEax;
if (Max >= 0x80000001) {
AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
if ((RegEdx & BIT26) != 0) {
Page1GSupport = TRUE;
}
}
if (Max >= 0x80000008) {
AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
PhysBits = (UINT8)RegEax;
GuestPhysBits = (UINT8)(RegEax >> 16);
} else {
PhysBits = 36;
GuestPhysBits = 0;
}
if (!QemuQuirk) {
Valid = TRUE;
} else if (GuestPhysBits) {
Valid = TRUE;
} else if (PhysBits >= 41) {
Valid = TRUE;
} else if (AsciiStrCmp (Signature, "GenuineIntel") == 0) {
if ((PhysBits == 36) || (PhysBits == 39)) {
Valid = TRUE;
}
} else if (AsciiStrCmp (Signature, "AuthenticAMD") == 0) {
if (PhysBits == 40) {
Valid = TRUE;
}
}
Cr4.UintN = AsmReadCr4 ();
DEBUG ((
DEBUG_INFO,
"%a: Signature: '%a', PhysBits: %d, GuestPhysBits: %d, QemuQuirk: %a, la57: %a, Valid: %a\n",
__func__,
Signature,
PhysBits,
GuestPhysBits,
QemuQuirk ? "On" : "Off",
Cr4.Bits.LA57 ? "On" : "Off",
Valid ? "Yes" : "No"
));
if (GuestPhysBits && (PhysBits > GuestPhysBits)) {
DEBUG ((DEBUG_INFO, "%a: limit PhysBits to %d (GuestPhysBits)\n", __func__, GuestPhysBits));
PhysBits = GuestPhysBits;
}
if (Valid) {
/*
* Due to the sign extension we can use only the lower half of the
* virtual address space to identity-map physical address space,
* which gives us a 47 bit wide address space with 4 paging levels
* and a 56 bit wide address space with 5 paging levels.
*/
if (Cr4.Bits.LA57) {
if ((PhysBits > 48) && !GuestPhysBits) {
/*
* Some Intel CPUs support 5-level paging, have more than 48
* phys-bits but support only 4-level EPT, which effectively
* limits guest phys-bits to 48.
*
* AMD Processors have a different but somewhat related
* problem: They can handle guest phys-bits larger than 48
* only in case the host runs in 5-level paging mode.
*
* GuestPhysBits is used to communicate that kind of
* limitations from hypervisor to guest. If GuestPhysBits is
* not set play safe and limit phys-bits to 48.
*/
DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 48 (5-level paging, no GuestPhysBits)\n", __func__));
PhysBits = 48;
}
} else {
if (PhysBits > 46) {
/*
* Some older linux kernels apparently have problems handling
* phys-bits > 46 correctly, so use that instead of 47 as
* limit.
*/
DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 46 (4-level paging)\n", __func__));
PhysBits = 46;
}
}
if (!Page1GSupport && (PhysBits > 40)) {
DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 40 (no 1G pages available)\n", __func__));
PhysBits = 40;
}
if (!FixedPcdGetBool (PcdUse1GPageTable) && (PhysBits > 40)) {
DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 40 (PcdUse1GPageTable is false)\n", __func__));
PhysBits = 40;
}
PlatformInfoHob->PhysMemAddressWidth = PhysBits;
PlatformInfoHob->FirstNonAddress = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth);
}
}
VOID
EFIAPI
PlatformDynamicMmioWindow (
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT64 AddrSpace, MmioSpace;
AddrSpace = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth);
MmioSpace = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth - 3);
if (PlatformInfoHob->PcdPciMmio64Override) {
DEBUG ((DEBUG_INFO, "%a: using fwcfg override for mmio window\n", __func__));
} else if ((PlatformInfoHob->PcdPciMmio64Size < MmioSpace) &&
(PlatformInfoHob->PcdPciMmio64Base + MmioSpace < AddrSpace))
{
DEBUG ((DEBUG_INFO, "%a: using dynamic mmio window\n", __func__));
DEBUG ((DEBUG_INFO, "%a: Addr Space 0x%Lx (%Ld GB)\n", __func__, AddrSpace, RShiftU64 (AddrSpace, 30)));
DEBUG ((DEBUG_INFO, "%a: MMIO Space 0x%Lx (%Ld GB)\n", __func__, MmioSpace, RShiftU64 (MmioSpace, 30)));
PlatformInfoHob->PcdPciMmio64Size = MmioSpace;
PlatformInfoHob->PcdPciMmio64Base = AddrSpace - MmioSpace;
PlatformScanE820 (PlatformReservationConflictCB, PlatformInfoHob);
} else {
DEBUG ((DEBUG_INFO, "%a: using classic mmio window\n", __func__));
}
DEBUG ((DEBUG_INFO, "%a: Pci64 Base 0x%Lx\n", __func__, PlatformInfoHob->PcdPciMmio64Base));
DEBUG ((DEBUG_INFO, "%a: Pci64 Size 0x%Lx\n", __func__, PlatformInfoHob->PcdPciMmio64Size));
}
/**
Iterate over the PCI host bridges resources information optionally provided
in fw-cfg and find the highest address contained in the PCI MMIO windows. If
the information is found, return the exclusive end; one past the last usable
address.
@param[out] PciMmioAddressEnd Pointer to one-after End Address updated with
information extracted from host-provided data
or zero if no information available or an
error happened
@retval EFI_SUCCESS PCI information was read and the output
parameter updated with the last valid
address in the 64-bit MMIO range.
@retval EFI_INVALID_PARAMETER Pointer parameter is invalid
@retval EFI_INCOMPATIBLE_VERSION Hardware information found in fw-cfg
has an incompatible format
@retval EFI_UNSUPPORTED Fw-cfg is not supported, thus host
provided information, if any, cannot be
read
@retval EFI_NOT_FOUND No PCI host bridge information provided
by the host.
**/
STATIC
EFI_STATUS
PlatformScanHostProvided64BitPciMmioEnd (
OUT UINT64 *PciMmioAddressEnd
)
{
EFI_STATUS Status;
HOST_BRIDGE_INFO HostBridge;
FIRMWARE_CONFIG_ITEM FwCfgItem;
UINTN FwCfgSize;
UINTN FwCfgReadIndex;
UINTN ReadDataSize;
UINT64 Above4GMmioEnd;
if (PciMmioAddressEnd == NULL) {
return EFI_INVALID_PARAMETER;
}
*PciMmioAddressEnd = 0;
Above4GMmioEnd = 0;
Status = QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSize);
if (EFI_ERROR (Status)) {
return Status;
}
QemuFwCfgSelectItem (FwCfgItem);
FwCfgReadIndex = 0;
while (FwCfgReadIndex < FwCfgSize) {
Status = QemuFwCfgReadNextHardwareInfoByType (
HardwareInfoTypeHostBridge,
sizeof (HostBridge),
FwCfgSize,
&HostBridge,
&ReadDataSize,
&FwCfgReadIndex
);
if (Status != EFI_SUCCESS) {
//
// No more data available to read in the file, break
// loop and finish process
//
break;
}
Status = HardwareInfoPciHostBridgeLastMmioAddress (
&HostBridge,
ReadDataSize,
TRUE,
&Above4GMmioEnd
);
if (Status != EFI_SUCCESS) {
//
// Error parsing MMIO apertures and extracting last MMIO
// address, reset PciMmioAddressEnd as if no information was
// found, to avoid moving forward with incomplete data, and
// bail out
//
DEBUG ((
DEBUG_ERROR,
"%a: ignoring malformed hardware information from fw_cfg\n",
__func__
));
*PciMmioAddressEnd = 0;
return Status;
}
if (Above4GMmioEnd > *PciMmioAddressEnd) {
*PciMmioAddressEnd = Above4GMmioEnd;
}
}
if (*PciMmioAddressEnd > 0) {
//
// Host-provided PCI information was found and a MMIO window end
// derived from it.
// Increase the End address by one to have the output pointing to
// one after the address in use (exclusive end).
//
*PciMmioAddressEnd += 1;
DEBUG ((
DEBUG_INFO,
"%a: Pci64End=0x%Lx\n",
__func__,
*PciMmioAddressEnd
));
return EFI_SUCCESS;
}
return EFI_NOT_FOUND;
}
VOID
EFIAPI
Switch4Level (
VOID
);
/**
Configure x64 paging levels.
The OVMF ResetVector code will enter long mode with 5-level paging if the
following conditions are true:
(1) OVMF has been built with PcdUse5LevelPageTable = TRUE, and
(2) the CPU supports 5-level paging (aka la57), and
(3) the CPU supports gigabyte pages, and
(4) the VM is not running in SEV mode.
Condition (4) is a temporary stopgap for BaseMemEncryptSevLib not supporting
5-level paging yet.
This function looks at the virtual machine configuration, then decides
whenever it will continue to use 5-level paging or downgrade to 4-level
paging for better compatibility with older guest OS versions.
There is a fw_cfg config option to explicitly request 4 or 5-level paging
using 'qemu -fw_cfg name=opt/org.tianocode/PagingLevel,string=4|5'. If the
option is present the requested paging level will be used.
Should that not be the case the function checks the size of the address space
needed, which is the RAM installed plus fw_cfg reservations. The downgrade
to 4-level paging will happen for small guests where the address space needed
is lower than 1TB.
This function will also log the paging level used and the reason for that.
**/
STATIC
VOID
PlatformSetupPagingLevel (
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
#ifdef MDE_CPU_X64
UINT32 PagingLevel;
EFI_STATUS Status;
IA32_CR4 Cr4;
Cr4.UintN = AsmReadCr4 ();
if (!Cr4.Bits.LA57) {
/* The OvmfPkg ResetVector has NOT turned on 5-level paging, log the reason. */
if (!PcdGetBool (PcdUse5LevelPageTable)) {
DEBUG ((DEBUG_INFO, "%a: using 4-level paging (PcdUse5LevelPageTable disabled)\n", __func__));
} else {
DEBUG ((DEBUG_INFO, "%a: using 4-level paging (la57 not supported by cpu)\n", __func__));
}
return;
}
Status = QemuFwCfgParseUint32 (
"opt/org.tianocode/PagingLevel",
FALSE,
&PagingLevel
);
switch (Status) {
case EFI_NOT_FOUND:
if (PlatformInfoHob->FirstNonAddress < (1ll << 40)) {
//
// If the highest address actually used is below 1TB switch back into
// 4-level paging mode for better compatibility with older guests.
//
DEBUG ((DEBUG_INFO, "%a: using 4-level paging (default for small guest)\n", __func__));
PagingLevel = 4;
} else {
DEBUG ((DEBUG_INFO, "%a: using 5-level paging (default for large guest)\n", __func__));
PagingLevel = 5;
}
break;
case EFI_SUCCESS:
if ((PagingLevel != 4) && (PagingLevel != 5)) {
DEBUG ((DEBUG_INFO, "%a: invalid paging level in fw_cfg: %d\n", __func__, PagingLevel));
return;
}
DEBUG ((DEBUG_INFO, "%a: using %d-level paging (fw_cfg override)\n", __func__, PagingLevel));
break;
default:
DEBUG ((DEBUG_WARN, "%a: QemuFwCfgParseUint32: %r\n", __func__, Status));
return;
}
if (PagingLevel == 4) {
Switch4Level ();
}
if (PagingLevel == 5) {
/* The OvmfPkg ResetVector has turned on 5-level paging, nothing to do here. */
}
#endif
}
/**
Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size.
**/
VOID
EFIAPI
PlatformAddressWidthInitialization (
IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT8 PhysMemAddressWidth;
EFI_STATUS Status;
if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {
PlatformAddressWidthFromCpuid (PlatformInfoHob, FALSE);
return;
} else if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {
PlatformInfoHob->FirstNonAddress = BASE_4GB;
Status = PlatformScanE820 (PlatformGetFirstNonAddressCB, PlatformInfoHob);
if (EFI_ERROR (Status)) {
PlatformInfoHob->FirstNonAddress = BASE_4GB + PlatformGetSystemMemorySizeAbove4gb ();
}
PlatformInfoHob->PcdPciMmio64Base = PlatformInfoHob->FirstNonAddress;
PlatformAddressWidthFromCpuid (PlatformInfoHob, FALSE);
PlatformInfoHob->PcdPciMmio64Size = PlatformInfoHob->FirstNonAddress - PlatformInfoHob->PcdPciMmio64Base;
return;
}
//
// First scan host-provided hardware information to assess if the address
// space is already known. If so, guest must use those values.
//
Status = PlatformScanHostProvided64BitPciMmioEnd (&PlatformInfoHob->FirstNonAddress);
if (EFI_ERROR (Status)) {
//
// If the host did not provide valid hardware information leading to a
// hard-defined 64-bit MMIO end, fold back to calculating the minimum range
// needed.
// As guest-physical memory size grows, the permanent PEI RAM requirements
// are dominated by the identity-mapping page tables built by the DXE IPL.
// The DXL IPL keys off of the physical address bits advertized in the CPU
// HOB. To conserve memory, we calculate the minimum address width here.
//
PlatformGetFirstNonAddress (PlatformInfoHob);
}
PlatformSetupPagingLevel (PlatformInfoHob);
PlatformAddressWidthFromCpuid (PlatformInfoHob, TRUE);
if (PlatformInfoHob->PhysMemAddressWidth != 0) {
// physical address width is known
PlatformDynamicMmioWindow (PlatformInfoHob);
return;
}
//
// physical address width is NOT known
// -> do some guess work, mostly based on installed memory
// -> try be conservstibe to stay below the guaranteed minimum of
// 36 phys bits (aka 64 GB).
//
PhysMemAddressWidth = (UINT8)HighBitSet64 (PlatformInfoHob->FirstNonAddress);
//
// If FirstNonAddress is not an integral power of two, then we need an
// additional bit.
//
if ((PlatformInfoHob->FirstNonAddress & (PlatformInfoHob->FirstNonAddress - 1)) != 0) {
++PhysMemAddressWidth;
}
//
// The minimum address width is 36 (covers up to and excluding 64 GB, which
// is the maximum for Ia32 + PAE). The theoretical architecture maximum for
// X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We
// can simply assert that here, since 48 bits are good enough for 256 TB.
//
if (PhysMemAddressWidth <= 36) {
PhysMemAddressWidth = 36;
}
#if defined (MDE_CPU_X64)
if (TdIsEnabled ()) {
if (TdSharedPageMask () == (1ULL << 47)) {
PhysMemAddressWidth = 48;
} else {
PhysMemAddressWidth = 52;
}
}
ASSERT (PhysMemAddressWidth <= 52);
#else
ASSERT (PhysMemAddressWidth <= 48);
#endif
PlatformInfoHob->PhysMemAddressWidth = PhysMemAddressWidth;
}
/**
Create gEfiSmmSmramMemoryGuid HOB defined in the PI specification Vol. 3,
section 5, which is used to describe the SMRAM memory regions supported
by the platform.
@param[in] StartAddress StartAddress of smram.
@param[in] Size Size of smram.
**/
STATIC
VOID
CreateSmmSmramMemoryHob (
IN EFI_PHYSICAL_ADDRESS StartAddress,
IN UINT32 Size
)
{
UINTN BufferSize;
UINT8 SmramRanges;
EFI_PEI_HOB_POINTERS Hob;
EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *SmramHobDescriptorBlock;
VOID *GuidHob;
SmramRanges = 2;
BufferSize = sizeof (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK) + (SmramRanges - 1) * sizeof (EFI_SMRAM_DESCRIPTOR);
Hob.Raw = BuildGuidHob (
&gEfiSmmSmramMemoryGuid,
BufferSize
);
ASSERT (Hob.Raw);
SmramHobDescriptorBlock = (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *)(Hob.Raw);
SmramHobDescriptorBlock->NumberOfSmmReservedRegions = SmramRanges;
//
// 1. Create first SMRAM descriptor, which contains data structures used in S3 resume.
// One page is enough for the data structure
//
SmramHobDescriptorBlock->Descriptor[0].PhysicalStart = StartAddress;
SmramHobDescriptorBlock->Descriptor[0].CpuStart = StartAddress;
SmramHobDescriptorBlock->Descriptor[0].PhysicalSize = EFI_PAGE_SIZE;
SmramHobDescriptorBlock->Descriptor[0].RegionState = EFI_SMRAM_CLOSED | EFI_CACHEABLE | EFI_ALLOCATED;
//
// 1.1 Create gEfiAcpiVariableGuid according SmramHobDescriptorBlock->Descriptor[0] since it's used in S3 resume.
//
GuidHob = BuildGuidHob (&gEfiAcpiVariableGuid, sizeof (EFI_SMRAM_DESCRIPTOR));
ASSERT (GuidHob != NULL);
CopyMem (GuidHob, &SmramHobDescriptorBlock->Descriptor[0], sizeof (EFI_SMRAM_DESCRIPTOR));
//
// 2. Create second SMRAM descriptor, which is free and will be used by SMM foundation.
//
SmramHobDescriptorBlock->Descriptor[1].PhysicalStart = SmramHobDescriptorBlock->Descriptor[0].PhysicalStart + EFI_PAGE_SIZE;
SmramHobDescriptorBlock->Descriptor[1].CpuStart = SmramHobDescriptorBlock->Descriptor[0].CpuStart + EFI_PAGE_SIZE;
SmramHobDescriptorBlock->Descriptor[1].PhysicalSize = Size - EFI_PAGE_SIZE;
SmramHobDescriptorBlock->Descriptor[1].RegionState = EFI_SMRAM_CLOSED | EFI_CACHEABLE;
}
STATIC
VOID
QemuInitializeRamBelow1gb (
IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
if (PlatformInfoHob->SmmSmramRequire && PlatformInfoHob->Q35SmramAtDefaultSmbase) {
PlatformAddMemoryRangeHob (0, SMM_DEFAULT_SMBASE);
PlatformAddReservedMemoryBaseSizeHob (
SMM_DEFAULT_SMBASE,
MCH_DEFAULT_SMBASE_SIZE,
TRUE /* Cacheable */
);
STATIC_ASSERT (
SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE < BASE_512KB + BASE_128KB,
"end of SMRAM at default SMBASE ends at, or exceeds, 640KB"
);
PlatformAddMemoryRangeHob (
SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE,
BASE_512KB + BASE_128KB
);
} else {
PlatformAddMemoryRangeHob (0, BASE_512KB + BASE_128KB);
}
}
/**
Peform Memory Detection for QEMU / KVM
**/
VOID
EFIAPI
PlatformQemuInitializeRam (
IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
UINT64 UpperMemorySize;
MTRR_SETTINGS MtrrSettings;
EFI_STATUS Status;
DEBUG ((DEBUG_INFO, "%a called\n", __func__));
//
// Determine total memory size available
//
PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
//
// CpuMpPei saves the original contents of the borrowed area in permanent
// PEI RAM, in a backup buffer allocated with the normal PEI services.
// CpuMpPei restores the original contents ("returns" the borrowed area) at
// End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before
// transferring control to the OS's wakeup vector in the FACS.
//
// We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to
// restore the original contents. Furthermore, we expect all such PEIMs
// (CpuMpPei included) to claim the borrowed areas by producing memory
// allocation HOBs, and to honor preexistent memory allocation HOBs when
// looking for an area to borrow.
//
QemuInitializeRamBelow1gb (PlatformInfoHob);
if (PlatformInfoHob->SmmSmramRequire) {
UINT32 TsegSize;
EFI_PHYSICAL_ADDRESS TsegBase;
TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;
TsegBase = PlatformInfoHob->LowMemory - TsegSize;
PlatformAddMemoryRangeHob (BASE_1MB, TsegBase);
PlatformAddReservedMemoryBaseSizeHob (
TsegBase,
TsegSize,
TRUE
);
//
// Create gEfiSmmSmramMemoryGuid HOB
//
CreateSmmSmramMemoryHob (TsegBase, TsegSize);
} else {
PlatformAddMemoryRangeHob (BASE_1MB, PlatformInfoHob->LowMemory);
}
if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {
//
// If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM
// entries. Otherwise, create a single memory HOB with the flat >=4GB
// memory size read from the CMOS.
//
Status = PlatformScanE820 (PlatformAddHobCB, PlatformInfoHob);
if (EFI_ERROR (Status)) {
UpperMemorySize = PlatformGetSystemMemorySizeAbove4gb ();
if (UpperMemorySize != 0) {
PlatformAddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
}
}
}
//
// We'd like to keep the following ranges uncached:
// - [640 KB, 1 MB)
// - [Uc32Base, 4 GB)
//
// Everything else should be WB. Unfortunately, programming the inverse (ie.
// keeping the default UC, and configuring the complement set of the above as
// WB) is not reliable in general, because the end of the upper RAM can have
// practically any alignment, and we may not have enough variable MTRRs to
// cover it exactly.
//
// Because of that PlatformQemuUc32BaseInitialization() will round
// up PlatformInfoHob->LowMemory to make sure a single mtrr register
// is enough. The the result will be stored in
// PlatformInfoHob->Uc32Base. On a typical qemu configuration with
// gigabyte-alignment being used LowMemory will be 2 or 3 GB and no
// rounding is needed, so LowMemory and Uc32Base will be identical.
//
if (IsMtrrSupported () && (PlatformInfoHob->HostBridgeDevId != CLOUDHV_DEVICE_ID)) {
MtrrGetAllMtrrs (&MtrrSettings);
//
// See SecMtrrSetup(), default type should be write back
//
ASSERT ((MtrrSettings.MtrrDefType & BIT11) != 0);
ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);
ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == MTRR_CACHE_WRITE_BACK);
//
// flip default type to writeback
//
SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, MTRR_CACHE_WRITE_BACK);
ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);
MtrrSettings.MtrrDefType |= BIT10;
MtrrSetAllMtrrs (&MtrrSettings);
//
// Set memory range from 640KB to 1MB to uncacheable
//
Status = MtrrSetMemoryAttribute (
BASE_512KB + BASE_128KB,
BASE_1MB - (BASE_512KB + BASE_128KB),
CacheUncacheable
);
ASSERT_EFI_ERROR (Status);
//
// Set the memory range from the start of the 32-bit PCI MMIO
// aperture to 4GB as uncacheable.
//
Status = MtrrSetMemoryAttribute (
PlatformInfoHob->Uc32Base,
SIZE_4GB - PlatformInfoHob->Uc32Base,
CacheUncacheable
);
ASSERT_EFI_ERROR (Status);
}
}
VOID
EFIAPI
PlatformQemuInitializeRamForS3 (
IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob
)
{
if (PlatformInfoHob->S3Supported && (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME)) {
//
// This is the memory range that will be used for PEI on S3 resume
//
BuildMemoryAllocationHob (
PlatformInfoHob->S3AcpiReservedMemoryBase,
PlatformInfoHob->S3AcpiReservedMemorySize,
EfiACPIMemoryNVS
);
//
// Cover the initial RAM area used as stack and temporary PEI heap.
//
// This is reserved as ACPI NVS so it can be used on S3 resume.
//
BuildMemoryAllocationHob (
PcdGet32 (PcdOvmfSecPeiTempRamBase),
PcdGet32 (PcdOvmfSecPeiTempRamSize),
EfiACPIMemoryNVS
);
//
// SEC stores its table of GUIDed section handlers here.
//
BuildMemoryAllocationHob (
PcdGet64 (PcdGuidedExtractHandlerTableAddress),
PcdGet32 (PcdGuidedExtractHandlerTableSize),
EfiACPIMemoryNVS
);
#ifdef MDE_CPU_X64
//
// Reserve the initial page tables built by the reset vector code.
//
// Since this memory range will be used by the Reset Vector on S3
// resume, it must be reserved as ACPI NVS.
//
BuildMemoryAllocationHob (
(EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecPageTablesBase),
(UINT64)(UINTN)PcdGet32 (PcdOvmfSecPageTablesSize),
EfiACPIMemoryNVS
);
if (PlatformInfoHob->SevEsIsEnabled) {
//
// If SEV-ES is enabled, reserve the GHCB-related memory area. This
// includes the extra page table used to break down the 2MB page
// mapping into 4KB page entries where the GHCB resides and the
// GHCB area itself.
//
// Since this memory range will be used by the Reset Vector on S3
// resume, it must be reserved as ACPI NVS.
//
BuildMemoryAllocationHob (
(EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableBase),
(UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableSize),
EfiACPIMemoryNVS
);
BuildMemoryAllocationHob (
(EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBase),
(UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbSize),
EfiACPIMemoryNVS
);
BuildMemoryAllocationHob (
(EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase),
(UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupSize),
EfiACPIMemoryNVS
);
}
#endif
}
if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {
if (!PlatformInfoHob->SmmSmramRequire) {
//
// Reserve the lock box storage area
//
// Since this memory range will be used on S3 resume, it must be
// reserved as ACPI NVS.
//
// If S3 is unsupported, then various drivers might still write to the
// LockBox area. We ought to prevent DXE from serving allocation requests
// such that they would overlap the LockBox storage.
//
ZeroMem (
(VOID *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),
(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize)
);
BuildMemoryAllocationHob (
(EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),
(UINT64)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize),
PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData
);
}
if (PlatformInfoHob->SmmSmramRequire) {
UINT32 TsegSize;
//
// Make sure the TSEG area that we reported as a reserved memory resource
// cannot be used for reserved memory allocations.
//
PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;
BuildMemoryAllocationHob (
PlatformInfoHob->LowMemory - TsegSize,
TsegSize,
EfiReservedMemoryType
);
//
// Similarly, allocate away the (already reserved) SMRAM at the default
// SMBASE, if it exists.
//
if (PlatformInfoHob->Q35SmramAtDefaultSmbase) {
BuildMemoryAllocationHob (
SMM_DEFAULT_SMBASE,
MCH_DEFAULT_SMBASE_SIZE,
EfiReservedMemoryType
);
}
}
#ifdef MDE_CPU_X64
if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {
//
// Reserve the work area.
//
// Since this memory range will be used by the Reset Vector on S3
// resume, it must be reserved as ACPI NVS.
//
// If S3 is unsupported, then various drivers might still write to the
// work area. We ought to prevent DXE from serving allocation requests
// such that they would overlap the work area.
//
BuildMemoryAllocationHob (
(EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),
(UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),
PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData
);
}
#endif
}
}
|