VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiCpuPkg/PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c@ 108794

Last change on this file since 108794 was 108794, checked in by vboxsync, 2 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1/** @file
2
3Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
4SPDX-License-Identifier: BSD-2-Clause-Patent
5
6**/
7
8#include "PiSmmCpuCommon.h"
9
10/**
11 Get SmmProfileData.
12
13 @param[in, out] Size Return Size of SmmProfileData.
14 0 means the gMmProfileDataHobGuid does not exist.
15
16 @return Address of SmmProfileData
17
18**/
19EFI_PHYSICAL_ADDRESS
20GetSmmProfileData (
21 IN OUT UINT64 *Size
22 )
23{
24 EFI_PEI_HOB_POINTERS SmmProfileDataHob;
25
26 ASSERT (Size != NULL);
27
28 //
29 // Get Smm Profile Base from Memory Allocation HOB
30 //
31 SmmProfileDataHob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
32 while (SmmProfileDataHob.Raw != NULL) {
33 //
34 // Find gMmProfileDataHobGuid
35 //
36 if (CompareGuid (&SmmProfileDataHob.MemoryAllocation->AllocDescriptor.Name, &gMmProfileDataHobGuid)) {
37 break;
38 }
39
40 SmmProfileDataHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (SmmProfileDataHob));
41 }
42
43 if (SmmProfileDataHob.Raw == NULL) {
44 *Size = 0;
45 return 0;
46 }
47
48 *Size = SmmProfileDataHob.MemoryAllocation->AllocDescriptor.MemoryLength;
49
50 return SmmProfileDataHob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress;
51}
52
53/**
54 Return if the Address is the NonMmram logging Address.
55
56 @param[in] Address the address to be checked
57
58 @return TRUE The address is the NonMmram logging Address.
59 @return FALSE The address is not the NonMmram logging Address.
60**/
61BOOLEAN
62IsNonMmramLoggingAddress (
63 IN UINT64 Address
64 )
65{
66 EFI_PEI_HOB_POINTERS Hob;
67
68 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
69 while (Hob.Raw != NULL) {
70 if ((Address >= Hob.ResourceDescriptor->PhysicalStart) && (Address < Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength)) {
71 if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) != 0) {
72 return TRUE;
73 }
74
75 return FALSE;
76 }
77
78 Hob.Raw = GET_NEXT_HOB (Hob);
79 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
80 }
81
82 return FALSE;
83}
84
85/**
86 Return if the Address is forbidden as SMM communication buffer.
87
88 @param[in] Address the address to be checked
89
90 @return TRUE The address is forbidden as SMM communication buffer.
91 @return FALSE The address is allowed as SMM communication buffer.
92**/
93BOOLEAN
94IsSmmCommBufferForbiddenAddress (
95 IN UINT64 Address
96 )
97{
98 EFI_PEI_HOB_POINTERS Hob;
99
100 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
101 while (Hob.Raw != NULL) {
102 if ((Address >= Hob.ResourceDescriptor->PhysicalStart) && (Address < Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength)) {
103 return FALSE;
104 }
105
106 Hob.Raw = GET_NEXT_HOB (Hob);
107 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
108 }
109
110 return TRUE;
111}
112
113/**
114 Build Memory Region from ResourceDescriptor HOBs by excluding Logging attribute range.
115
116 @param[out] MemoryRegion Returned Non-Mmram Memory regions.
117 @param[out] MemoryRegionCount A pointer to the number of Memory regions.
118**/
119VOID
120BuildMemoryMapFromResDescHobs (
121 OUT MM_CPU_MEMORY_REGION **MemoryRegion,
122 OUT UINTN *MemoryRegionCount
123 )
124{
125 EFI_PEI_HOB_POINTERS Hob;
126 UINTN Count;
127 UINTN Index;
128 EFI_PHYSICAL_ADDRESS MaxPhysicalAddress;
129 EFI_PHYSICAL_ADDRESS ResourceHobEnd;
130
131 ASSERT (MemoryRegion != NULL && MemoryRegionCount != NULL);
132
133 *MemoryRegion = NULL;
134 *MemoryRegionCount = 0;
135 MaxPhysicalAddress = LShiftU64 (1, mPhysicalAddressBits);
136
137 //
138 // Get the count.
139 //
140 Count = 0;
141 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
142 while (Hob.Raw != NULL) {
143 if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) == 0) {
144 ResourceHobEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;
145
146 ASSERT (ResourceHobEnd <= MaxPhysicalAddress);
147 if (ResourceHobEnd > MaxPhysicalAddress) {
148 CpuDeadLoop ();
149 }
150
151 //
152 // Resource HOBs describe all accessible non-smram regions.
153 // Logging attribute range is treated as not present. Not-present ranges are not included in this memory map.
154 //
155 Count++;
156 }
157
158 Hob.Raw = GET_NEXT_HOB (Hob);
159 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
160 }
161
162 *MemoryRegionCount = Count;
163
164 *MemoryRegion = (MM_CPU_MEMORY_REGION *)AllocateZeroPool (sizeof (MM_CPU_MEMORY_REGION) * Count);
165 ASSERT (*MemoryRegion != NULL);
166
167 Index = 0;
168 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
169 while (Hob.Raw != NULL) {
170 if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) == 0) {
171 ASSERT (Index < Count);
172 (*MemoryRegion)[Index].Base = Hob.ResourceDescriptor->PhysicalStart;
173 (*MemoryRegion)[Index].Length = Hob.ResourceDescriptor->ResourceLength;
174 (*MemoryRegion)[Index].Attribute = EFI_MEMORY_XP;
175 if (Hob.ResourceDescriptor->ResourceAttribute == EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED) {
176 (*MemoryRegion)[Index].Attribute |= EFI_MEMORY_RO;
177 }
178
179 Index++;
180 }
181
182 Hob.Raw = GET_NEXT_HOB (Hob);
183 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
184 }
185
186 return;
187}
188
189/**
190 Build extended protection MemoryRegion.
191
192 The caller is responsible for freeing MemoryRegion via FreePool().
193
194 @param[out] MemoryRegion Returned Non-Mmram Memory regions.
195 @param[out] MemoryRegionCount A pointer to the number of Memory regions.
196**/
197VOID
198CreateExtendedProtectionRange (
199 OUT MM_CPU_MEMORY_REGION **MemoryRegion,
200 OUT UINTN *MemoryRegionCount
201 )
202{
203 BuildMemoryMapFromResDescHobs (MemoryRegion, MemoryRegionCount);
204}
205
206/**
207 Create the Non-Mmram Memory Region within the ResourceDescriptor HOBs
208 without Logging attribute.
209
210 The caller is responsible for freeing MemoryRegion via FreePool().
211
212 @param[in] PhysicalAddressBits The bits of physical address to map.
213 @param[out] MemoryRegion Returned Non-Mmram Memory regions.
214 @param[out] MemoryRegionCount A pointer to the number of Memory regions.
215**/
216VOID
217CreateNonMmramMemMap (
218 IN UINT8 PhysicalAddressBits,
219 OUT MM_CPU_MEMORY_REGION **MemoryRegion,
220 OUT UINTN *MemoryRegionCount
221 )
222{
223 BuildMemoryMapFromResDescHobs (MemoryRegion, MemoryRegionCount);
224}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette