From nobody Wed Nov 13 07:09:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110657+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110657+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031846; cv=none; d=zohomail.com; s=zohoarc; b=KF+YQIzQZBIf6BAvIlFJSBk28Xup5kvPhoBLJvyKzq8yeg+DeDg4eP3VJ8MdaKxZpIp7IwZ6AhDg6ttsjiKq/N1XAHtrtFS473Or0W6KL9MhCyUamvtkP0gAq3ERFQ03Hx8HVIeDNKPyEMKHWja49eP7a8EOskgPmM3uX9TGlG4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031846; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=xrlWGHFcS7JSZK75b2uAmd4PWkGqkLK1oxSp1YGGY/o=; b=CTgrJz/fZJba+nUcIvn+b9DYOP5GkjQflE0mKGezNEsypWz/4vOPk+W0KYvMOSzcHD9yIoA2gTLOC2ssg6WncfYTw3lhypHGUM8RDbUAvuOl9fbLMikn9cUgVie14PoNcT1PaVQumcns2QyNXJARrJ/ujvSVGSJjvMizXyEvBe0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110657+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031846602867.9213335462075; Fri, 3 Nov 2023 10:17:26 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=dggdGxWBJjjKCnNe9QxOMTnP8GwF0ClydLkccN05VbA=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1699031846; v=1; b=B05ETV3Jr/GjIUTZWnTJwYIqdORIqlx5y+xWzV4luOR2bKzJyYPMAnxoCKwo30F4qq4rADVu XKAdbqVIF+91GR1czOJk1FLLZNJU+GeiEkuzSI+mNt7IGTnuB062E7B4DxEkF8S1wub55prJObz CipMEi0P4mgOqtYCZU/u6Xa4= X-Received: by 127.0.0.2 with SMTP id s1QvYY1788612xFh5JWbHisn; Fri, 03 Nov 2023 10:17:26 -0700 X-Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by mx.groups.io with SMTP id smtpd.web10.59754.1699031843531937435 for ; Fri, 03 Nov 2023 10:17:23 -0700 X-Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1cc1ee2d8dfso20656655ad.3 for ; Fri, 03 Nov 2023 10:17:23 -0700 (PDT) X-Gm-Message-State: sGo8IygqsDkkP3vgARjlcfRjx1787277AA= X-Google-Smtp-Source: AGHT+IFAKgq4MM+OIpGM4VAxKf9v4L6YdnVnvgoT6L4enryW+8eAIJCj6aK8Hfn/dHgZuof2pOkK0A== X-Received: by 2002:a17:902:e5c2:b0:1cc:3fc9:7d09 with SMTP id u2-20020a170902e5c200b001cc3fc97d09mr18941341plf.15.1699031842856; Fri, 03 Nov 2023 10:17:22 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:22 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 09/14] MdeModulePkg: Fix Bugs in MAT Logic Date: Fri, 3 Nov 2023 10:17:01 -0700 Message-ID: <20231103171706.148-10-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@gmail.com> MIME-Version: 1.0 Precedence: Bulk 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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848944100039 Content-Type: text/plain; charset="utf-8" Fix the bugs in the MAT logic before switching the UEFI and SMM MAT logic to use the new library. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- 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/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index 51630f504ea1..af6c26244cc0 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -517,7 +517,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( =20 CoreAcquiremMemoryAttributesTableLock (); =20 - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; + AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 3) * mImagePropertiesPrivateData.ImageRecordCount; =20 OldMemoryMapSize =3D *MemoryMapSize; Status =3D CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey,= DescriptorSize, DescriptorVersion); diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index 9fb3b922038f..379eb0c6cccd 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -277,7 +277,7 @@ GetMaxSplitRecordCount ( break; } =20 - SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 1); + SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 3); PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); =20 @@ -323,7 +323,6 @@ SplitRecord ( UINT64 PhysicalEnd; UINTN NewRecordCount; UINTN TotalNewRecordCount; - BOOLEAN IsLastRecordData; =20 if (MaxSplitRecordCount =3D=3D 0) { CopyMem (NewRecord, OldRecord, DescriptorSize); @@ -344,35 +343,16 @@ SplitRecord ( NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart, ImageRecordList); if (NewImageRecord =3D=3D NULL) { // - // No more image covered by this range, stop + // No more images cover this range, check if we've reached the end o= f the old descriptor. If not, + // add the remaining range to the new descriptor list. // - if ((PhysicalEnd > PhysicalStart) && (ImageRecord !=3D NULL)) { - // - // If this is still address in this record, need record. - // - NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, Descri= ptorSize); - IsLastRecordData =3D FALSE; - if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { - IsLastRecordData =3D TRUE; - } - - if (IsLastRecordData) { - // - // Last record is DATA, just merge it. - // - NewRecord->NumberOfPages =3D EfiSizeToPages (PhysicalEnd - NewRe= cord->PhysicalStart); - } else { - // - // Last record is CODE, create a new DATA entry. - // - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, = DescriptorSize); - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_X= P; - TotalNewRecordCount++; - } + if (PhysicalEnd > PhysicalStart) { + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (PhysicalEnd - Physica= lStart); + NewRecord->Attribute =3D TempRecord.Attribute; + TotalNewRecordCount++; } =20 break; @@ -380,6 +360,24 @@ SplitRecord ( =20 ImageRecord =3D NewImageRecord; =20 + // + // Update PhysicalStart to exclude the portion before the image buffer + // + if (TempRecord.PhysicalStart < ImageRecord->ImageBase) { + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecord->ImageBase = - TempRecord.PhysicalStart); + NewRecord->Attribute =3D TempRecord.Attribute; + TotalNewRecordCount++; + + PhysicalStart =3D ImageRecord->ImageBase; + TempRecord.PhysicalStart =3D PhysicalStart; + TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalS= tart); + + NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord + Descrip= torSize); + } + // // Set new record // @@ -465,11 +463,12 @@ SplitTable ( { INTN IndexOld; INTN IndexNew; + INTN IndexNewStarting; UINTN MaxSplitRecordCount; UINTN RealSplitRecordCount; - UINTN TotalSplitRecordCount; + UINTN TotalSkippedRecords; =20 - TotalSplitRecordCount =3D 0; + TotalSkippedRecords =3D 0; // // Let old record point to end of valid MemoryMap buffer. // @@ -477,7 +476,8 @@ SplitTable ( // // Let new record point to end of full MemoryMap buffer. // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditiona= lDescriptors; + IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfA= dditionalDescriptors; + IndexNewStarting =3D IndexNew; for ( ; IndexOld >=3D 0; IndexOld--) { MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); // @@ -491,16 +491,14 @@ SplitTable ( DescriptorSize, ImageRecordList ); - // - // Adjust IndexNew according to real split. - // - CopyMem ( - ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRec= ordCount) * DescriptorSize), - ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), - RealSplitRecordCount * DescriptorSize - ); - IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitR= ecordCount; - TotalSplitRecordCount +=3D 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 sor= ting. + for ( ; RealSplitRecordCount < MaxSplitRecordCount; RealSplitRecordCou= nt++) { + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + ((IndexNew + RealSpl= itRecordCount + 1) * DescriptorSize)))->PhysicalStart =3D MAX_ADDRESS; + TotalSkippedRecords++; + } + IndexNew--; } =20 @@ -509,16 +507,16 @@ SplitTable ( // CopyMem ( MemoryMap, - (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecord= Count) * DescriptorSize, - (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize + (UINT8 *)MemoryMap + ((IndexNew + 1) * DescriptorSize), + (IndexNewStarting - IndexNew) * DescriptorSize ); =20 - *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; + // + // Sort from low to high to filter out the MAX_ADDRESS records. + // + SortMemoryMap (MemoryMap, (IndexNewStarting - IndexNew) * DescriptorSize= , DescriptorSize); =20 - // - // Sort from low to high (Just in case) - // - SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); + *MemoryMapSize =3D (IndexNewStarting - IndexNew - TotalSkippedRecords) *= DescriptorSize; =20 return; } --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110657): https://edk2.groups.io/g/devel/message/110657 Mute This Topic: https://groups.io/mt/102368849/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-