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
|
/** @file
UEFI MemoryAttributesTable support
Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiDxe.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiLib.h>
#include <Library/ImagePropertiesRecordLib.h>
#include <Guid/EventGroup.h>
#include <Guid/MemoryAttributesTable.h>
#include "DxeMain.h"
#include "HeapGuard.h"
/**
This function for GetMemoryMap() with properties table capability.
It calls original GetMemoryMap() to get the original memory map information. Then
plus the additional memory map entries for PE Code/Data seperation.
@param MemoryMapSize A pointer to the size, in bytes, of the
MemoryMap buffer. On input, this is the size of
the buffer allocated by the caller. On output,
it is the size of the buffer returned by the
firmware if the buffer was large enough, or the
size of the buffer needed to contain the map if
the buffer was too small.
@param MemoryMap A pointer to the buffer in which firmware places
the current memory map.
@param MapKey A pointer to the location in which firmware
returns the key for the current memory map.
@param DescriptorSize A pointer to the location in which firmware
returns the size, in bytes, of an individual
EFI_MEMORY_DESCRIPTOR.
@param DescriptorVersion A pointer to the location in which firmware
returns the version number associated with the
EFI_MEMORY_DESCRIPTOR.
@retval EFI_SUCCESS The memory map was returned in the MemoryMap
buffer.
@retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
buffer size needed to hold the memory map is
returned in MemoryMapSize.
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
**/
EFI_STATUS
EFIAPI
CoreGetMemoryMapWithSeparatedImageSection (
IN OUT UINTN *MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
OUT UINTN *MapKey,
OUT UINTN *DescriptorSize,
OUT UINT32 *DescriptorVersion
);
#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
#define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')
typedef struct {
UINT32 Signature;
UINTN ImageRecordCount;
UINTN CodeSegmentCountMax;
LIST_ENTRY ImageRecordList;
} IMAGE_PROPERTIES_PRIVATE_DATA;
STATIC IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData = {
IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE,
0,
0,
INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData.ImageRecordList)
};
STATIC EFI_LOCK mMemoryAttributesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
BOOLEAN mMemoryAttributesTableEnable = TRUE;
BOOLEAN mMemoryAttributesTableEndOfDxe = FALSE;
EFI_MEMORY_ATTRIBUTES_TABLE *mMemoryAttributesTable = NULL;
BOOLEAN mMemoryAttributesTableReadyToBoot = FALSE;
BOOLEAN gMemoryAttributesTableForwardCfi = TRUE;
/**
Install MemoryAttributesTable.
**/
VOID
InstallMemoryAttributesTable (
VOID
)
{
UINTN MemoryMapSize;
EFI_MEMORY_DESCRIPTOR *MemoryMap;
EFI_MEMORY_DESCRIPTOR *MemoryMapStart;
UINTN MapKey;
UINTN DescriptorSize;
UINT32 DescriptorVersion;
UINTN Index;
EFI_STATUS Status;
UINT32 RuntimeEntryCount;
EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;
EFI_MEMORY_DESCRIPTOR *MemoryAttributesEntry;
if (gMemoryMapTerminated) {
//
// Directly return after MemoryMap terminated.
//
return;
}
if (!mMemoryAttributesTableEnable) {
DEBUG ((DEBUG_VERBOSE, "Cannot install Memory Attributes Table "));
DEBUG ((DEBUG_VERBOSE, "because Runtime Driver Section Alignment is not %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));
return;
}
if (mMemoryAttributesTable == NULL) {
//
// InstallConfigurationTable here to occupy one entry for MemoryAttributesTable
// before GetMemoryMap below, as InstallConfigurationTable may allocate runtime
// memory for the new entry.
//
Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID *)(UINTN)MAX_ADDRESS);
ASSERT_EFI_ERROR (Status);
}
MemoryMapSize = 0;
MemoryMap = NULL;
Status = CoreGetMemoryMapWithSeparatedImageSection (
&MemoryMapSize,
MemoryMap,
&MapKey,
&DescriptorSize,
&DescriptorVersion
);
ASSERT (Status == EFI_BUFFER_TOO_SMALL);
do {
MemoryMap = AllocatePool (MemoryMapSize);
ASSERT (MemoryMap != NULL);
Status = CoreGetMemoryMapWithSeparatedImageSection (
&MemoryMapSize,
MemoryMap,
&MapKey,
&DescriptorSize,
&DescriptorVersion
);
if (EFI_ERROR (Status)) {
FreePool (MemoryMap);
}
} while (Status == EFI_BUFFER_TOO_SMALL);
MemoryMapStart = MemoryMap;
RuntimeEntryCount = 0;
for (Index = 0; Index < MemoryMapSize/DescriptorSize; Index++) {
switch (MemoryMap->Type) {
case EfiRuntimeServicesCode:
case EfiRuntimeServicesData:
RuntimeEntryCount++;
break;
}
MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize);
}
//
// Allocate MemoryAttributesTable
//
MemoryAttributesTable = AllocatePool (sizeof (EFI_MEMORY_ATTRIBUTES_TABLE) + DescriptorSize * RuntimeEntryCount);
ASSERT (MemoryAttributesTable != NULL);
MemoryAttributesTable->Version = EFI_MEMORY_ATTRIBUTES_TABLE_VERSION;
MemoryAttributesTable->NumberOfEntries = RuntimeEntryCount;
MemoryAttributesTable->DescriptorSize = (UINT32)DescriptorSize;
if (gMemoryAttributesTableForwardCfi) {
MemoryAttributesTable->Flags = EFI_MEMORY_ATTRIBUTES_FLAGS_RT_FORWARD_CONTROL_FLOW_GUARD;
} else {
MemoryAttributesTable->Flags = 0;
}
DEBUG ((DEBUG_VERBOSE, "MemoryAttributesTable:\n"));
DEBUG ((DEBUG_VERBOSE, " Version - 0x%08x\n", MemoryAttributesTable->Version));
DEBUG ((DEBUG_VERBOSE, " NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));
DEBUG ((DEBUG_VERBOSE, " DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));
MemoryAttributesEntry = (EFI_MEMORY_DESCRIPTOR *)(MemoryAttributesTable + 1);
MemoryMap = MemoryMapStart;
for (Index = 0; Index < MemoryMapSize/DescriptorSize; Index++) {
switch (MemoryMap->Type) {
case EfiRuntimeServicesCode:
case EfiRuntimeServicesData:
CopyMem (MemoryAttributesEntry, MemoryMap, DescriptorSize);
MemoryAttributesEntry->Attribute &= (EFI_MEMORY_RO|EFI_MEMORY_XP|EFI_MEMORY_RUNTIME);
DEBUG ((DEBUG_VERBOSE, "Entry (0x%x)\n", MemoryAttributesEntry));
DEBUG ((DEBUG_VERBOSE, " Type - 0x%x\n", MemoryAttributesEntry->Type));
DEBUG ((DEBUG_VERBOSE, " PhysicalStart - 0x%016lx\n", MemoryAttributesEntry->PhysicalStart));
DEBUG ((DEBUG_VERBOSE, " VirtualStart - 0x%016lx\n", MemoryAttributesEntry->VirtualStart));
DEBUG ((DEBUG_VERBOSE, " NumberOfPages - 0x%016lx\n", MemoryAttributesEntry->NumberOfPages));
DEBUG ((DEBUG_VERBOSE, " Attribute - 0x%016lx\n", MemoryAttributesEntry->Attribute));
MemoryAttributesEntry = NEXT_MEMORY_DESCRIPTOR (MemoryAttributesEntry, DescriptorSize);
break;
}
MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize);
}
MemoryMap = MemoryMapStart;
FreePool (MemoryMap);
//
// Update configuratoin table for MemoryAttributesTable.
//
Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, MemoryAttributesTable);
ASSERT_EFI_ERROR (Status);
if (mMemoryAttributesTable != NULL) {
FreePool (mMemoryAttributesTable);
}
mMemoryAttributesTable = MemoryAttributesTable;
}
/**
Install MemoryAttributesTable on memory allocation.
@param[in] MemoryType EFI memory type.
**/
VOID
InstallMemoryAttributesTableOnMemoryAllocation (
IN EFI_MEMORY_TYPE MemoryType
)
{
//
// Install MemoryAttributesTable after ReadyToBoot on runtime memory allocation.
//
if (mMemoryAttributesTableReadyToBoot &&
((MemoryType == EfiRuntimeServicesCode) || (MemoryType == EfiRuntimeServicesData)))
{
InstallMemoryAttributesTable ();
}
}
/**
Install MemoryAttributesTable on ReadyToBoot.
@param[in] Event The Event this notify function registered to.
@param[in] Context Pointer to the context data registered to the Event.
**/
VOID
EFIAPI
InstallMemoryAttributesTableOnReadyToBoot (
IN EFI_EVENT Event,
IN VOID *Context
)
{
InstallMemoryAttributesTable ();
mMemoryAttributesTableReadyToBoot = TRUE;
}
/**
Install initial MemoryAttributesTable on EndOfDxe.
Then SMM can consume this information.
@param[in] Event The Event this notify function registered to.
@param[in] Context Pointer to the context data registered to the Event.
**/
VOID
EFIAPI
InstallMemoryAttributesTableOnEndOfDxe (
IN EFI_EVENT Event,
IN VOID *Context
)
{
mMemoryAttributesTableEndOfDxe = TRUE;
InstallMemoryAttributesTable ();
DEBUG_CODE_BEGIN ();
if ( mImagePropertiesPrivateData.ImageRecordCount > 0) {
DEBUG ((DEBUG_INFO, "DXE - Total Runtime Image Count: 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));
DEBUG ((DEBUG_INFO, "DXE - Dump Runtime Image Records:\n"));
DumpImageRecords (&mImagePropertiesPrivateData.ImageRecordList);
}
DEBUG_CODE_END ();
}
/**
Initialize MemoryAttrubutesTable support.
**/
VOID
EFIAPI
CoreInitializeMemoryAttributesTable (
VOID
)
{
EFI_STATUS Status;
EFI_EVENT ReadyToBootEvent;
EFI_EVENT EndOfDxeEvent;
//
// Construct the table at ReadyToBoot.
//
Status = CoreCreateEventInternal (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
InstallMemoryAttributesTableOnReadyToBoot,
NULL,
&gEfiEventReadyToBootGuid,
&ReadyToBootEvent
);
ASSERT_EFI_ERROR (Status);
//
// Construct the initial table at EndOfDxe,
// then SMM can consume this information.
// Use TPL_NOTIFY here, as such SMM code (TPL_CALLBACK)
// can run after it.
//
Status = CoreCreateEventInternal (
EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
InstallMemoryAttributesTableOnEndOfDxe,
NULL,
&gEfiEndOfDxeEventGroupGuid,
&EndOfDxeEvent
);
ASSERT_EFI_ERROR (Status);
return;
}
//
// Below functions are for MemoryMap
//
/**
Acquire memory lock on mMemoryAttributesTableLock.
**/
STATIC
VOID
CoreAcquiremMemoryAttributesTableLock (
VOID
)
{
CoreAcquireLock (&mMemoryAttributesTableLock);
}
/**
Release memory lock on mMemoryAttributesTableLock.
**/
STATIC
VOID
CoreReleasemMemoryAttributesTableLock (
VOID
)
{
CoreReleaseLock (&mMemoryAttributesTableLock);
}
/**
Merge continous memory map entries whose have same attributes.
@param MemoryMap A pointer to the buffer in which firmware places
the current memory map.
@param MemoryMapSize A pointer to the size, in bytes, of the
MemoryMap buffer. On input, this is the size of
the current memory map. On output,
it is the size of new memory map after merge.
@param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
**/
VOID
MergeMemoryMap (
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
IN OUT UINTN *MemoryMapSize,
IN UINTN DescriptorSize
)
{
EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
UINT64 MemoryBlockLength;
EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry;
EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;
MemoryMapEntry = MemoryMap;
NewMemoryMapEntry = MemoryMap;
MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + *MemoryMapSize);
while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof (EFI_MEMORY_DESCRIPTOR));
NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
do {
MergeGuardPages (NewMemoryMapEntry, NextMemoryMapEntry->PhysicalStart);
MemoryBlockLength = LShiftU64 (NewMemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT);
if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&
(NewMemoryMapEntry->Type == NextMemoryMapEntry->Type) &&
(NewMemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&
((NewMemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart))
{
NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;
NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
continue;
} else {
MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
break;
}
} while (TRUE);
MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);
}
*MemoryMapSize = (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap;
return;
}
/**
Enforce memory map attributes.
This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP.
@param MemoryMap A pointer to the buffer in which firmware places
the current memory map.
@param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
@param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
**/
STATIC
VOID
EnforceMemoryMapAttribute (
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
IN UINTN MemoryMapSize,
IN UINTN DescriptorSize
)
{
EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
MemoryMapEntry = MemoryMap;
MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + MemoryMapSize);
while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
if ((MemoryMapEntry->Attribute & EFI_MEMORY_ACCESS_MASK) == 0) {
switch (MemoryMapEntry->Type) {
case EfiRuntimeServicesCode:
// If at this point the attributes have not been set on an EfiRuntimeServicesCode
// region, the memory range must not contain a loaded image. It's possible these
// non-image EfiRuntimeServicesCode regions are part of the unused memory bucket.
// It could also be that this region was explicitly allocated outside of the PE
// loader but the UEFI spec requires that all EfiRuntimeServicesCode regions contain
// EFI modules. In either case, set the attributes to RO and XP.
MemoryMapEntry->Attribute |= (EFI_MEMORY_RO | EFI_MEMORY_XP);
break;
case EfiRuntimeServicesData:
MemoryMapEntry->Attribute |= EFI_MEMORY_XP;
break;
default:
break;
}
}
MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
}
return;
}
/**
This function for GetMemoryMap() with properties table capability.
It calls original GetMemoryMap() to get the original memory map information. Then
plus the additional memory map entries for PE Code/Data seperation.
@param MemoryMapSize A pointer to the size, in bytes, of the
MemoryMap buffer. On input, this is the size of
the buffer allocated by the caller. On output,
it is the size of the buffer returned by the
firmware if the buffer was large enough, or the
size of the buffer needed to contain the map if
the buffer was too small.
@param MemoryMap A pointer to the buffer in which firmware places
the current memory map.
@param MapKey A pointer to the location in which firmware
returns the key for the current memory map.
@param DescriptorSize A pointer to the location in which firmware
returns the size, in bytes, of an individual
EFI_MEMORY_DESCRIPTOR.
@param DescriptorVersion A pointer to the location in which firmware
returns the version number associated with the
EFI_MEMORY_DESCRIPTOR.
@retval EFI_SUCCESS The memory map was returned in the MemoryMap
buffer.
@retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
buffer size needed to hold the memory map is
returned in MemoryMapSize.
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
**/
EFI_STATUS
EFIAPI
CoreGetMemoryMapWithSeparatedImageSection (
IN OUT UINTN *MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
OUT UINTN *MapKey,
OUT UINTN *DescriptorSize,
OUT UINT32 *DescriptorVersion
)
{
EFI_STATUS Status;
UINTN OldMemoryMapSize;
UINTN AdditionalRecordCount;
//
// If PE code/data is not aligned, just return.
//
if (!mMemoryAttributesTableEnable) {
return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
}
if (MemoryMapSize == NULL) {
return EFI_INVALID_PARAMETER;
}
CoreAcquiremMemoryAttributesTableLock ();
AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 3) * mImagePropertiesPrivateData.ImageRecordCount;
OldMemoryMapSize = *MemoryMapSize;
Status = CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
if (Status == EFI_BUFFER_TOO_SMALL) {
*MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
} else if (Status == EFI_SUCCESS) {
ASSERT (MemoryMap != NULL);
if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * AdditionalRecordCount) {
*MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
//
// Need update status to buffer too small
//
Status = EFI_BUFFER_TOO_SMALL;
} else {
//
// Split PE code/data
//
SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImagePropertiesPrivateData.ImageRecordList, AdditionalRecordCount);
//
// Set RuntimeData to XP
//
EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, *DescriptorSize);
//
// Merge same type to save entry size
//
MergeMemoryMap (MemoryMap, MemoryMapSize, *DescriptorSize);
}
}
CoreReleasemMemoryAttributesTableLock ();
return Status;
}
//
// Below functions are for ImageRecord
//
/**
Insert image record.
@param RuntimeImage Runtime image information
**/
VOID
InsertImageRecord (
IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage
)
{
EFI_STATUS Status;
IMAGE_PROPERTIES_RECORD *ImageRecord;
CHAR8 *PdbPointer;
UINT32 RequiredAlignment;
DEBUG ((DEBUG_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage));
if (mMemoryAttributesTableEndOfDxe) {
DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe\n"));
return;
}
ImageRecord = AllocatePool (sizeof (*ImageRecord));
if (ImageRecord == NULL) {
return;
}
InitializeListHead (&ImageRecord->Link);
InitializeListHead (&ImageRecord->CodeSegmentList);
PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)RuntimeImage->ImageBase);
if (PdbPointer != NULL) {
DEBUG ((DEBUG_VERBOSE, " Image - %a\n", PdbPointer));
}
RequiredAlignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
Status = CreateImagePropertiesRecord (
RuntimeImage->ImageBase,
RuntimeImage->ImageSize,
&RequiredAlignment,
ImageRecord
);
if (EFI_ERROR (Status)) {
if (Status == EFI_ABORTED) {
mMemoryAttributesTableEnable = FALSE;
}
Status = EFI_ABORTED;
goto Finish;
}
if (ImageRecord->CodeSegmentCount == 0) {
mMemoryAttributesTableEnable = FALSE;
DEBUG ((DEBUG_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount is 0 !!!!!!!!\n"));
if (PdbPointer != NULL) {
DEBUG ((DEBUG_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer));
}
Status = EFI_ABORTED;
goto Finish;
}
//
// Check overlap all section in ImageBase/Size
//
if (!IsImageRecordCodeSectionValid (ImageRecord)) {
DEBUG ((DEBUG_ERROR, "IsImageRecordCodeSectionValid - FAIL\n"));
Status = EFI_ABORTED;
goto Finish;
}
InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageRecord->Link);
mImagePropertiesPrivateData.ImageRecordCount++;
if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeSegmentCount) {
mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount;
}
SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList);
Finish:
if (EFI_ERROR (Status) && (ImageRecord != NULL)) {
DeleteImagePropertiesRecord (ImageRecord);
}
return;
}
/**
Remove Image record.
@param RuntimeImage Runtime image information
**/
VOID
RemoveImageRecord (
IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage
)
{
IMAGE_PROPERTIES_RECORD *ImageRecord;
DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage));
DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));
if (mMemoryAttributesTableEndOfDxe) {
DEBUG ((DEBUG_INFO, "Do not remove runtime image record after EndOfDxe\n"));
return;
}
ImageRecord = FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize, &mImagePropertiesPrivateData.ImageRecordList);
if (ImageRecord == NULL) {
DEBUG ((DEBUG_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));
return;
}
DeleteImagePropertiesRecord (ImageRecord);
mImagePropertiesPrivateData.ImageRecordCount--;
}
|