]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmMemLib / X86StandaloneMmMemLibInternal.c
1 /** @file
2 Internal ARCH Specific file of MM memory check library.
3
4 MM memory check library implementation. This library consumes MM_ACCESS_PROTOCOL
5 to get MMRAM information. In order to use this library instance, the platform should produce
6 all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
7 and MM driver) and/or specific dedicated hardware.
8
9 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
10 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
11 Copyright (c) Microsoft Corporation.
12
13 SPDX-License-Identifier: BSD-2-Clause-Patent
14
15 **/
16 #include <PiMm.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/HobLib.h>
22
23 #include <Guid/MmCoreData.h>
24 #include <Guid/MmramMemoryReserve.h>
25
26 //
27 // Maximum support address used to check input buffer
28 //
29 extern EFI_PHYSICAL_ADDRESS mMmMemLibInternalMaximumSupportAddress;
30 extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
31 extern UINTN mMmMemLibInternalMmramCount;
32
33 /**
34 Calculate and save the maximum support address.
35
36 **/
37 VOID
38 MmMemLibInternalCalculateMaximumSupportAddress (
39 VOID
40 )
41 {
42 VOID *Hob;
43 UINT32 RegEax;
44 UINT8 PhysicalAddressBits;
45
46 //
47 // Get physical address bits supported.
48 //
49 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
50 if (Hob != NULL) {
51 PhysicalAddressBits = ((EFI_HOB_CPU *)Hob)->SizeOfMemorySpace;
52 } else {
53 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
54 if (RegEax >= 0x80000008) {
55 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
56 PhysicalAddressBits = (UINT8)RegEax;
57 } else {
58 PhysicalAddressBits = 36;
59 }
60 }
61
62 //
63 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
64 //
65 ASSERT (PhysicalAddressBits <= 52);
66 if (PhysicalAddressBits > 48) {
67 PhysicalAddressBits = 48;
68 }
69
70 //
71 // Save the maximum support address in one global variable
72 //
73 mMmMemLibInternalMaximumSupportAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
74 DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
75 }
76
77 /**
78 Initialize cached Mmram Ranges from HOB.
79
80 @retval EFI_UNSUPPORTED The routine is unable to extract MMRAM information.
81 @retval EFI_SUCCESS MmRanges are populated successfully.
82
83 **/
84 EFI_STATUS
85 MmMemLibInternalPopulateMmramRanges (
86 VOID
87 )
88 {
89 VOID *HobStart;
90 EFI_HOB_GUID_TYPE *GuidHob;
91 MM_CORE_DATA_HOB_DATA *DataInHob;
92 MM_CORE_PRIVATE_DATA *MmCorePrivateData;
93 EFI_HOB_GUID_TYPE *MmramRangesHob;
94 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *MmramRangesHobData;
95 EFI_MMRAM_DESCRIPTOR *MmramDescriptors;
96
97 HobStart = GetHobList ();
98 DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStart));
99
100 //
101 // Extract MM Core Private context from the Hob. If absent search for
102 // a Hob containing the MMRAM ranges
103 //
104 GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
105 if (GuidHob == NULL) {
106 MmramRangesHob = GetFirstGuidHob (&gEfiMmPeiMmramMemoryReserveGuid);
107 if (MmramRangesHob == NULL) {
108 return EFI_UNSUPPORTED;
109 }
110
111 MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
112 if ((MmramRangesHobData == NULL) || (MmramRangesHobData->Descriptor == NULL)) {
113 return EFI_UNSUPPORTED;
114 }
115
116 mMmMemLibInternalMmramCount = MmramRangesHobData->NumberOfMmReservedRegions;
117 MmramDescriptors = MmramRangesHobData->Descriptor;
118 } else {
119 DataInHob = GET_GUID_HOB_DATA (GuidHob);
120 if (DataInHob == NULL) {
121 return EFI_UNSUPPORTED;
122 }
123
124 MmCorePrivateData = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
125 if ((MmCorePrivateData == NULL) || (MmCorePrivateData->MmramRanges == 0)) {
126 return EFI_UNSUPPORTED;
127 }
128
129 mMmMemLibInternalMmramCount = (UINTN)MmCorePrivateData->MmramRangeCount;
130 MmramDescriptors = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivateData->MmramRanges;
131 }
132
133 mMmMemLibInternalMmramRanges = AllocatePool (mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));
134 if (mMmMemLibInternalMmramRanges) {
135 CopyMem (
136 mMmMemLibInternalMmramRanges,
137 MmramDescriptors,
138 mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR)
139 );
140 }
141
142 return EFI_SUCCESS;
143 }
144
145 /**
146 Deinitialize cached Mmram Ranges.
147
148 **/
149 VOID
150 MmMemLibInternalFreeMmramRanges (
151 VOID
152 )
153 {
154 if (mMmMemLibInternalMmramRanges != NULL) {
155 FreePool (mMmMemLibInternalMmramRanges);
156 }
157 }