From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 428BCD80A13 for ; Tue, 18 Jul 2023 18:40:24 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=49ZvcLElHA0VTlylv10SUi6ROU4AU5G/6LIXjSdDULk=; c=relaxed/simple; d=groups.io; h=X-Received:X-Received:X-Received:X-Gm-Message-State:X-Google-Smtp-Source:X-Received:X-Received:From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Unsubscribe:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:Content-Transfer-Encoding; s=20140610; t=1689705622; v=1; b=HPvEpuPR74nNGXoWl+KTwqUDGjWFdI5B6owgtcwgZqImrCBwN0qtV0cGmW7384UjzadEFxwZ kwdqn2771mj2Vykie9r7iQfxYO2djAniuCG/SJeYMLSFzaIvq1c4FxYn59mgbCVL1S81lboeglS zc6yS28TswnPdSkx0mu/mMzA= X-Received: by 127.0.0.2 with SMTP id rE0lYY7687511xeyRLyr4GFE; Tue, 18 Jul 2023 11:40:22 -0700 X-Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by mx.groups.io with SMTP id smtpd.web11.3673.1689705621410069997 for ; Tue, 18 Jul 2023 11:40:21 -0700 X-Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-666e6541c98so6085010b3a.2 for ; Tue, 18 Jul 2023 11:40:21 -0700 (PDT) X-Gm-Message-State: 5PcEbuHkiUQZ2SbTD6vidBOPx7686176AA= X-Google-Smtp-Source: APBJJlE9XvbTsnLekgvvwX3nW9/U+Kmq4V++AlB96v38FK6EiDxZ9yVUjzWLYhgc7y43O4g44sGHSA== X-Received: by 2002:a05:6a00:240f:b0:64c:4f2f:a235 with SMTP id z15-20020a056a00240f00b0064c4f2fa235mr18848225pfh.30.1689705620737; Tue, 18 Jul 2023 11:40:20 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.230.135]) by smtp.gmail.com with ESMTPSA id d18-20020aa78692000000b006827d86ca0csm1863153pfo.55.2023.07.18.11.40.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 11:40:20 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v1 3/9] MdeModulePkg: Fix Bugs in MAT Logic Date: Tue, 18 Jul 2023 11:39:59 -0700 Message-ID: <20230718184006.534-4-t@taylorbeebe.com> In-Reply-To: <20230718184006.534-1-t@taylorbeebe.com> References: <20230718184006.534-1-t@taylorbeebe.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,t@taylorbeebe.com Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=HPvEpuPR; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=none Fix the bugs in ImagePropertiesRecordLib (which is a copy of the current UEFI MAT logic) before switching the UEFI and SMM MAT logic to use the new library. Signed-off-by: Taylor Beebe Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c | 2 +- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c | 98 ++++++++++---------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c index fd127ee167e1..6e15a76cccae 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -1017,7 +1017,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( CoreAcquiremMemoryAttributesTableLock (); - AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; + AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 3) * mImagePropertiesPrivateData.ImageRecordCount; OldMemoryMapSize = *MemoryMapSize; Status = CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion); diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c index 46f4f401a4d5..a787e376b551 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c @@ -519,7 +519,7 @@ GetMaxSplitRecordCount ( break; } - SplitRecordCount += (2 * ImageRecord->CodeSegmentCount + 1); + SplitRecordCount += (2 * ImageRecord->CodeSegmentCount + 3); PhysicalStart = ImageRecord->ImageBase + ImageRecord->ImageSize; } while ((ImageRecord != NULL) && (PhysicalStart < PhysicalEnd)); @@ -565,7 +565,6 @@ SplitRecord ( UINT64 PhysicalEnd; UINTN NewRecordCount; UINTN TotalNewRecordCount; - BOOLEAN IsLastRecordData; if (MaxSplitRecordCount == 0) { CopyMem (NewRecord, OldRecord, DescriptorSize); @@ -586,35 +585,16 @@ SplitRecord ( NewImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart, ImageRecordList); if (NewImageRecord == NULL) { // - // No more image covered by this range, stop + // No more images cover this range, check if we've reached the end of the old descriptor. If not, + // add the remaining range to the new descriptor list. // - if ((PhysicalEnd > PhysicalStart) && (ImageRecord != NULL)) { - // - // If this is still address in this record, need record. - // - NewRecord = PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - IsLastRecordData = FALSE; - if ((NewRecord->Attribute & EFI_MEMORY_XP) != 0) { - IsLastRecordData = TRUE; - } - - if (IsLastRecordData) { - // - // Last record is DATA, just merge it. - // - NewRecord->NumberOfPages = EfiSizeToPages (PhysicalEnd - NewRecord->PhysicalStart); - } else { - // - // Last record is CODE, create a new DATA entry. - // - NewRecord = NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecord->Type = TempRecord.Type; - NewRecord->PhysicalStart = TempRecord.PhysicalStart; - NewRecord->VirtualStart = 0; - NewRecord->NumberOfPages = TempRecord.NumberOfPages; - NewRecord->Attribute = TempRecord.Attribute | EFI_MEMORY_XP; - TotalNewRecordCount++; - } + if (PhysicalEnd > PhysicalStart) { + NewRecord->Type = TempRecord.Type; + NewRecord->PhysicalStart = PhysicalStart; + NewRecord->VirtualStart = 0; + NewRecord->NumberOfPages = EfiSizeToPages (PhysicalEnd - PhysicalStart); + NewRecord->Attribute = TempRecord.Attribute; + TotalNewRecordCount++; } break; @@ -622,6 +602,24 @@ SplitRecord ( ImageRecord = NewImageRecord; + // + // Update PhysicalStart to exclude the portion before the image buffer + // + if (TempRecord.PhysicalStart < ImageRecord->ImageBase) { + NewRecord->Type = TempRecord.Type; + NewRecord->PhysicalStart = TempRecord.PhysicalStart; + NewRecord->VirtualStart = 0; + NewRecord->NumberOfPages = EfiSizeToPages (ImageRecord->ImageBase - TempRecord.PhysicalStart); + NewRecord->Attribute = TempRecord.Attribute; + TotalNewRecordCount++; + + PhysicalStart = ImageRecord->ImageBase; + TempRecord.PhysicalStart = PhysicalStart; + TempRecord.NumberOfPages = EfiSizeToPages (PhysicalEnd - PhysicalStart); + + NewRecord = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord + DescriptorSize); + } + // // Set new record // @@ -707,11 +705,12 @@ SplitTable ( { INTN IndexOld; INTN IndexNew; + INTN IndexNewStarting; UINTN MaxSplitRecordCount; UINTN RealSplitRecordCount; - UINTN TotalSplitRecordCount; + UINTN TotalSkippedRecords; - TotalSplitRecordCount = 0; + TotalSkippedRecords = 0; // // Let old record point to end of valid MemoryMap buffer. // @@ -719,7 +718,8 @@ SplitTable ( // // Let new record point to end of full MemoryMap buffer. // - IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditionalDescriptors; + IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditionalDescriptors; + IndexNewStarting = IndexNew; for ( ; IndexOld >= 0; IndexOld--) { MaxSplitRecordCount = GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); // @@ -733,16 +733,14 @@ SplitTable ( DescriptorSize, ImageRecordList ); - // - // Adjust IndexNew according to real split. - // - CopyMem ( - ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRecordCount) * DescriptorSize), - ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), - RealSplitRecordCount * DescriptorSize - ); - IndexNew = IndexNew + MaxSplitRecordCount - RealSplitRecordCount; - TotalSplitRecordCount += RealSplitRecordCount; + + // If we didn't utilize all the extra allocated descriptor slots, set the physical address of the unused slots + // to MAX_ADDRESS so they are moved to the bottom of the list when sorting. + for ( ; RealSplitRecordCount < MaxSplitRecordCount; RealSplitRecordCount++) { + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + ((IndexNew + RealSplitRecordCount + 1) * DescriptorSize)))->PhysicalStart = MAX_ADDRESS; + TotalSkippedRecords++; + } + IndexNew--; } @@ -751,16 +749,16 @@ SplitTable ( // CopyMem ( MemoryMap, - (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecordCount) * DescriptorSize, - (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize + (UINT8 *)MemoryMap + ((IndexNew + 1) * DescriptorSize), + (IndexNewStarting - IndexNew) * DescriptorSize ); - *MemoryMapSize = (*MemoryMapSize) + DescriptorSize * TotalSplitRecordCount; + // + // Sort from low to high to filter out the MAX_ADDRESS records. + // + SortMemoryMap (MemoryMap, (IndexNewStarting - IndexNew) * DescriptorSize, DescriptorSize); - // - // Sort from low to high (Just in case) - // - SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); + *MemoryMapSize = (IndexNewStarting - IndexNew - TotalSkippedRecords) * DescriptorSize; return; } -- 2.41.0.windows.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#107024): https://edk2.groups.io/g/devel/message/107024 Mute This Topic: https://groups.io/mt/100221559/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-