2 Memory Detection for Virtual Machines.
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // The package level header files this module uses
25 // The Library classes this module consumes
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/HobLib.h>
30 #include <Library/IoLib.h>
31 #include <Library/PcdLib.h>
32 #include <Library/PeimEntryPoint.h>
33 #include <Library/ResourcePublicationLib.h>
34 #include <Library/MtrrLib.h>
39 UINT8 mPhysMemAddressWidth
;
42 GetSystemMemorySizeBelow4gb (
50 // CMOS 0x34/0x35 specifies the system memory above 16 MB.
51 // * CMOS(0x35) is the high byte
52 // * CMOS(0x34) is the low byte
53 // * The size is specified in 64kb chunks
54 // * Since this is memory above 16MB, the 16MB must be added
55 // into the calculation to get the total memory size.
58 Cmos0x34
= (UINT8
) CmosRead8 (0x34);
59 Cmos0x35
= (UINT8
) CmosRead8 (0x35);
61 return (UINT32
) (((UINTN
)((Cmos0x35
<< 8) + Cmos0x34
) << 16) + SIZE_16MB
);
67 GetSystemMemorySizeAbove4gb (
74 // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.
75 // * CMOS(0x5d) is the most significant size byte
76 // * CMOS(0x5c) is the middle size byte
77 // * CMOS(0x5b) is the least significant size byte
78 // * The size is specified in 64kb chunks
82 for (CmosIndex
= 0x5d; CmosIndex
>= 0x5b; CmosIndex
--) {
83 Size
= (UINT32
) (Size
<< 8) + (UINT32
) CmosRead8 (CmosIndex
);
86 return LShiftU64 (Size
, 16);
91 Initialize the mPhysMemAddressWidth variable, based on guest RAM size.
94 AddressWidthInitialization (
98 UINT64 FirstNonAddress
;
101 // As guest-physical memory size grows, the permanent PEI RAM requirements
102 // are dominated by the identity-mapping page tables built by the DXE IPL.
103 // The DXL IPL keys off of the physical address bits advertized in the CPU
104 // HOB. To conserve memory, we calculate the minimum address width here.
106 FirstNonAddress
= BASE_4GB
+ GetSystemMemorySizeAbove4gb ();
107 mPhysMemAddressWidth
= (UINT8
)HighBitSet64 (FirstNonAddress
);
110 // If FirstNonAddress is not an integral power of two, then we need an
113 if ((FirstNonAddress
& (FirstNonAddress
- 1)) != 0) {
114 ++mPhysMemAddressWidth
;
118 // The minimum address width is 36 (covers up to and excluding 64 GB, which
119 // is the maximum for Ia32 + PAE). The theoretical architecture maximum for
120 // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We
121 // can simply assert that here, since 48 bits are good enough for 256 TB.
123 if (mPhysMemAddressWidth
<= 36) {
124 mPhysMemAddressWidth
= 36;
126 ASSERT (mPhysMemAddressWidth
<= 48);
131 Calculate the cap for the permanent PEI memory.
139 BOOLEAN Page1GSupport
;
147 // If DXE is 32-bit, then just return the traditional 64 MB cap.
150 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
156 // Dependent on physical address width, PEI memory allocations can be
157 // dominated by the page tables built for 64-bit DXE. So we key the cap off
158 // of those. The code below is based on CreateIdentityMappingPageTables() in
159 // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".
161 Page1GSupport
= FALSE
;
162 if (PcdGetBool (PcdUse1GPageTable
)) {
163 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
164 if (RegEax
>= 0x80000001) {
165 AsmCpuid (0x80000001, NULL
, NULL
, NULL
, &RegEdx
);
166 if ((RegEdx
& BIT26
) != 0) {
167 Page1GSupport
= TRUE
;
172 if (mPhysMemAddressWidth
<= 39) {
174 PdpEntries
= 1 << (mPhysMemAddressWidth
- 30);
175 ASSERT (PdpEntries
<= 0x200);
177 Pml4Entries
= 1 << (mPhysMemAddressWidth
- 39);
178 ASSERT (Pml4Entries
<= 0x200);
182 TotalPages
= Page1GSupport
? Pml4Entries
+ 1 :
183 (PdpEntries
+ 1) * Pml4Entries
+ 1;
184 ASSERT (TotalPages
<= 0x40201);
187 // Add 64 MB for miscellaneous allocations. Note that for
188 // mPhysMemAddressWidth values close to 36, the cap will actually be
189 // dominated by this increment.
191 return (UINT32
)(EFI_PAGES_TO_SIZE (TotalPages
) + SIZE_64MB
);
196 Publish PEI core memory
198 @return EFI_SUCCESS The PEIM initialized successfully.
207 EFI_PHYSICAL_ADDRESS MemoryBase
;
209 UINT64 LowerMemorySize
;
212 if (mBootMode
== BOOT_ON_S3_RESUME
) {
213 MemoryBase
= PcdGet32 (PcdS3AcpiReservedMemoryBase
);
214 MemorySize
= PcdGet32 (PcdS3AcpiReservedMemorySize
);
216 LowerMemorySize
= GetSystemMemorySizeBelow4gb ();
218 PeiMemoryCap
= GetPeiMemoryCap ();
219 DEBUG ((EFI_D_INFO
, "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",
220 __FUNCTION__
, mPhysMemAddressWidth
, PeiMemoryCap
>> 10));
223 // Determine the range of memory to use during PEI
225 // Technically we could lay the permanent PEI RAM over SEC's temporary
226 // decompression and scratch buffer even if "secure S3" is needed, since
227 // their lifetimes don't overlap. However, PeiFvInitialization() will cover
228 // RAM up to PcdOvmfDecompressionScratchEnd with an EfiACPIMemoryNVS memory
229 // allocation HOB, and other allocations served from the permanent PEI RAM
230 // shouldn't overlap with that HOB.
232 MemoryBase
= mS3Supported
&& FeaturePcdGet (PcdSmmSmramRequire
) ?
233 PcdGet32 (PcdOvmfDecompressionScratchEnd
) :
234 PcdGet32 (PcdOvmfDxeMemFvBase
) + PcdGet32 (PcdOvmfDxeMemFvSize
);
235 MemorySize
= LowerMemorySize
- MemoryBase
;
236 if (MemorySize
> PeiMemoryCap
) {
237 MemoryBase
= LowerMemorySize
- PeiMemoryCap
;
238 MemorySize
= PeiMemoryCap
;
243 // Publish this memory to the PEI Core
245 Status
= PublishSystemMemory(MemoryBase
, MemorySize
);
246 ASSERT_EFI_ERROR (Status
);
253 Peform Memory Detection for QEMU / KVM
262 UINT64 LowerMemorySize
;
263 UINT64 UpperMemorySize
;
264 MTRR_SETTINGS MtrrSettings
;
267 DEBUG ((EFI_D_INFO
, "%a called\n", __FUNCTION__
));
270 // Determine total memory size available
272 LowerMemorySize
= GetSystemMemorySizeBelow4gb ();
273 UpperMemorySize
= GetSystemMemorySizeAbove4gb ();
275 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
277 // Create memory HOBs
279 AddMemoryRangeHob (0, BASE_512KB
+ BASE_128KB
);
280 AddMemoryRangeHob (BASE_1MB
, LowerMemorySize
);
281 if (UpperMemorySize
!= 0) {
282 AddUntestedMemoryBaseSizeHob (BASE_4GB
, UpperMemorySize
);
287 // We'd like to keep the following ranges uncached:
289 // - [LowerMemorySize, 4 GB)
291 // Everything else should be WB. Unfortunately, programming the inverse (ie.
292 // keeping the default UC, and configuring the complement set of the above as
293 // WB) is not reliable in general, because the end of the upper RAM can have
294 // practically any alignment, and we may not have enough variable MTRRs to
297 if (IsMtrrSupported ()) {
298 MtrrGetAllMtrrs (&MtrrSettings
);
301 // MTRRs disabled, fixed MTRRs disabled, default type is uncached
303 ASSERT ((MtrrSettings
.MtrrDefType
& BIT11
) == 0);
304 ASSERT ((MtrrSettings
.MtrrDefType
& BIT10
) == 0);
305 ASSERT ((MtrrSettings
.MtrrDefType
& 0xFF) == 0);
308 // flip default type to writeback
310 SetMem (&MtrrSettings
.Fixed
, sizeof MtrrSettings
.Fixed
, 0x06);
311 ZeroMem (&MtrrSettings
.Variables
, sizeof MtrrSettings
.Variables
);
312 MtrrSettings
.MtrrDefType
|= BIT11
| BIT10
| 6;
313 MtrrSetAllMtrrs (&MtrrSettings
);
316 // Set memory range from 640KB to 1MB to uncacheable
318 Status
= MtrrSetMemoryAttribute (BASE_512KB
+ BASE_128KB
,
319 BASE_1MB
- (BASE_512KB
+ BASE_128KB
), CacheUncacheable
);
320 ASSERT_EFI_ERROR (Status
);
323 // Set memory range from the "top of lower RAM" (RAM below 4GB) to 4GB as
326 Status
= MtrrSetMemoryAttribute (LowerMemorySize
,
327 SIZE_4GB
- LowerMemorySize
, CacheUncacheable
);
328 ASSERT_EFI_ERROR (Status
);
333 Publish system RAM and reserve memory regions
337 InitializeRamRegions (
342 QemuInitializeRam ();
344 XenPublishRamRegions ();
347 if (mS3Supported
&& mBootMode
!= BOOT_ON_S3_RESUME
) {
349 // This is the memory range that will be used for PEI on S3 resume
351 BuildMemoryAllocationHob (
352 (EFI_PHYSICAL_ADDRESS
)(UINTN
) PcdGet32 (PcdS3AcpiReservedMemoryBase
),
353 (UINT64
)(UINTN
) PcdGet32 (PcdS3AcpiReservedMemorySize
),
358 // Cover the initial RAM area used as stack and temporary PEI heap.
360 // This is reserved as ACPI NVS so it can be used on S3 resume.
362 BuildMemoryAllocationHob (
363 PcdGet32 (PcdOvmfSecPeiTempRamBase
),
364 PcdGet32 (PcdOvmfSecPeiTempRamSize
),
369 // SEC stores its table of GUIDed section handlers here.
371 BuildMemoryAllocationHob (
372 PcdGet64 (PcdGuidedExtractHandlerTableAddress
),
373 PcdGet32 (PcdGuidedExtractHandlerTableSize
),
379 // Reserve the initial page tables built by the reset vector code.
381 // Since this memory range will be used by the Reset Vector on S3
382 // resume, it must be reserved as ACPI NVS.
384 BuildMemoryAllocationHob (
385 (EFI_PHYSICAL_ADDRESS
)(UINTN
) PcdGet32 (PcdOvmfSecPageTablesBase
),
386 (UINT64
)(UINTN
) PcdGet32 (PcdOvmfSecPageTablesSize
),
392 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
394 // Reserve the lock box storage area
396 // Since this memory range will be used on S3 resume, it must be
397 // reserved as ACPI NVS.
399 // If S3 is unsupported, then various drivers might still write to the
400 // LockBox area. We ought to prevent DXE from serving allocation requests
401 // such that they would overlap the LockBox storage.
404 (VOID
*)(UINTN
) PcdGet32 (PcdOvmfLockBoxStorageBase
),
405 (UINTN
) PcdGet32 (PcdOvmfLockBoxStorageSize
)
407 BuildMemoryAllocationHob (
408 (EFI_PHYSICAL_ADDRESS
)(UINTN
) PcdGet32 (PcdOvmfLockBoxStorageBase
),
409 (UINT64
)(UINTN
) PcdGet32 (PcdOvmfLockBoxStorageSize
),
410 mS3Supported
? EfiACPIMemoryNVS
: EfiBootServicesData