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