From nobody Thu Nov 14 06:56:29 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+110654+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+110654+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031842; cv=none; d=zohomail.com; s=zohoarc; b=YORXB26J9paLGEcSULHqQ5yUkkqnOu6988D9bVT6fysdNe/2x+BqU51IwpGlUTP2Hxk/URvUsPZ5MTigv4m/N3if/RJDUpq6mJEa6qqZirZUZNRM8z1OYWxbCzxaiJ/gqRgd8Cf34MZjWasAAZZmZdAJFGVmgmwaQEGE/sJEHgQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031842; 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=/b65t1iEdee/7YnLE8fxDnSs6XaLOCNUAv4E1B3Lmnc=; b=hQuVnA+Wv9wg+Ar5TM5xa+swwHWbOPL9xePTdU0GsmesiL+goI8LYXDendeJHVmHwbczTgw6hiOqsKdzeQ0oQevVtmrHxVk2NMYmP7rnNd8+3FXJdzyG1gjhx6DShSUnQUNf0/UlZjXaPStO5btS9QBnIkWD7K/fQmPO1SaXvP0= 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+110654+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 1699031842895675.4479450699303; Fri, 3 Nov 2023 10:17:22 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=q+nT++fj3oOwJFcxeKLShOgTgjhXiouOmfh8Nhp//S8=; 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=1699031842; v=1; b=FB5agH8ETujNB8ji6ji+OlRb4Z0pMY9ozwiqOBXEhEXbLU+BcYb7s+6hLFyQ4qkljEmDWX9g bQ6QXiQ+vgsZLlmRcCE8sc+BT7GQD2yLOAA+x7CJVhLSae5djHH5cGjwmu7nkDAfqktks0ztdTG qz20K96mHEAMgw2boon+lP+g= X-Received: by 127.0.0.2 with SMTP id Ae1lYY1788612xx3cQ24WRAL; Fri, 03 Nov 2023 10:17:22 -0700 X-Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by mx.groups.io with SMTP id smtpd.web11.59066.1699031841752080475 for ; Fri, 03 Nov 2023 10:17:21 -0700 X-Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1cc3542e328so18126535ad.1 for ; Fri, 03 Nov 2023 10:17:21 -0700 (PDT) X-Gm-Message-State: I6xFFEswUcjmAz8cvIInNR0yx1787277AA= X-Google-Smtp-Source: AGHT+IF1r9joLJzdIlJvD85iMg1zoyoobpZ2v5JsJnANjSvgbMZ69V/Vcp/Zdg5g5YGUwWi32Cq7pQ== X-Received: by 2002:a17:903:104a:b0:1c9:d948:33ea with SMTP id f10-20020a170903104a00b001c9d94833eamr15475729plc.21.1699031841067; Fri, 03 Nov 2023 10:17:21 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:20 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 06/14] MdeModulePkg: Update MemoryAttributesTable.c to Reduce Global Variable Use Date: Fri, 3 Nov 2023 10:16:58 -0700 Message-ID: <20231103171706.148-7-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: 1699031844913100025 Content-Type: text/plain; charset="utf-8" This patch updates MemoryAttributesTable.c to reduce reliance on global variables and allow some logic to move to a 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 | 102 +++++++++++------= --- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index fd127ee167e1..64b0aa1ff5e5 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -541,8 +541,9 @@ EnforceMemoryMapAttribute ( /** Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. =20 - @param Buffer Start Address - @param Length Address length + @param Buffer Start Address + @param Length Address length + @param ImageRecordList Image record list =20 @return first image record covered by [buffer, length] **/ @@ -550,14 +551,12 @@ STATIC IMAGE_PROPERTIES_RECORD * GetImageRecordByAddress ( IN EFI_PHYSICAL_ADDRESS Buffer, - IN UINT64 Length + IN UINT64 Length, + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; =20 for (ImageRecordLink =3D ImageRecordList->ForwardLink; ImageRecordLink !=3D ImageRecordList; @@ -692,7 +691,8 @@ SetNewRecord ( STATIC UINTN GetMaxSplitRecordCount ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; @@ -705,7 +705,7 @@ GetMaxSplitRecordCount ( PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize (OldRecor= d->NumberOfPages); =20 do { - ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart); + ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart, ImageRecordList); if (ImageRecord =3D=3D NULL) { break; } @@ -725,13 +725,16 @@ GetMaxSplitRecordCount ( Split the memory map to new entries, according to one old entry, based upon PE code section and data section. =20 - @param OldRecord A pointer to one old memory map entry. - @param NewRecord A pointer to several new memory map entri= es. - The caller gurantee the buffer size be 1 + - (SplitRecordCount * DescriptorSize) calcu= lated - below. - @param MaxSplitRecordCount The max number of splitted entries - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + @param OldRecord A pointer to one old memory map entr= y. + @param NewRecord A pointer to several new memory map = entries. + The caller gurantee the buffer size = be 1 + + (SplitRecordCount * DescriptorSize) = calculated + below. + @param MaxSplitRecordCount The max number of splitted entries + @param DescriptorSize Size, in bytes, of an individual EFI= _MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD en= tries used when searching + for an image record contained by the= memory range described in + the existing EFI memory map descript= or OldRecord =20 @retval 0 no entry is splitted. @return the real number of splitted record. @@ -742,7 +745,8 @@ SplitRecord ( IN EFI_MEMORY_DESCRIPTOR *OldRecord, IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, IN UINTN MaxSplitRecordCount, - IN UINTN DescriptorSize + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList ) { EFI_MEMORY_DESCRIPTOR TempRecord; @@ -770,7 +774,7 @@ SplitRecord ( =20 ImageRecord =3D NULL; do { - NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart); + NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart, ImageRecordList); if (NewImageRecord =3D=3D NULL) { // // No more image covered by this range, stop @@ -867,23 +871,29 @@ SplitRecord ( | Record Y | +---------------+ =20 - @param MemoryMapSize A pointer to the size, in bytes, of the - MemoryMap buffer. On input, this is the s= ize of - old MemoryMap before split. The actual bu= ffer - size of MemoryMap is MemoryMapSize + - (AdditionalRecordCount * DescriptorSize) = calculated - below. On output, it is the size of new M= emoryMap - after split. - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + @param MemoryMapSize A pointer to the size, in bytes,= of the + MemoryMap buffer. On input, this= is the size of + old MemoryMap before split. The = actual buffer + size of MemoryMap is MemoryMapSi= ze + + (AdditionalRecordCount * Descrip= torSize) calculated + below. On output, it is the size= of new MemoryMap + after split. + @param MemoryMap A pointer to the buffer in which= firmware places + the current memory map. + @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching + for an image record contained by= the memory range described in + EFI memory map descriptors. + @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. **/ STATIC VOID SplitTable ( IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN DescriptorSize + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList, + IN UINTN NumberOfAdditionalDescriptors ) { INTN IndexOld; @@ -891,9 +901,6 @@ SplitTable ( UINTN MaxSplitRecordCount; UINTN RealSplitRecordCount; UINTN TotalSplitRecordCount; - UINTN AdditionalRecordCount; - - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; =20 TotalSplitRecordCount =3D 0; // @@ -903,9 +910,9 @@ SplitTable ( // // Let new record point to end of full MemoryMap buffer. // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordC= ount; + IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditiona= lDescriptors; for ( ; IndexOld >=3D 0; IndexOld--) { - MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize)); + MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); // // Split this MemoryMap record // @@ -914,7 +921,8 @@ SplitTable ( (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), MaxSplitRecordCount, - DescriptorSize + DescriptorSize, + ImageRecordList ); // // Adjust IndexNew according to real split. @@ -934,7 +942,7 @@ SplitTable ( // CopyMem ( MemoryMap, - (UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) *= DescriptorSize, + (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecord= Count) * DescriptorSize, (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize ); =20 @@ -1035,7 +1043,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( // // Split PE code/data // - SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize); + SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImageProper= tiesPrivateData.ImageRecordList, AdditionalRecordCount); } } =20 @@ -1233,11 +1241,13 @@ SwapImageRecord ( =20 /** Sort image record based upon the ImageBase from low to high. + + @param ImageRecordList Image record list to be sorted **/ STATIC VOID SortImageRecord ( - VOID + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; @@ -1245,9 +1255,6 @@ SortImageRecord ( LIST_ENTRY *ImageRecordLink; LIST_ENTRY *NextImageRecordLink; LIST_ENTRY *ImageRecordEndLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; =20 ImageRecordLink =3D ImageRecordList->ForwardLink; NextImageRecordLink =3D ImageRecordLink->ForwardLink; @@ -1456,7 +1463,7 @@ InsertImageRecord ( mImagePropertiesPrivateData.CodeSegmentCountMax =3D ImageRecord->CodeS= egmentCount; } =20 - SortImageRecord (); + SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: return; @@ -1465,8 +1472,9 @@ Finish: /** Find image record according to image base and size. =20 - @param ImageBase Base of PE image - @param ImageSize Size of PE image + @param ImageBase Base of PE image + @param ImageSize Size of PE image + @param ImageRecordList Image record list to be searched =20 @return image record **/ @@ -1474,14 +1482,12 @@ STATIC IMAGE_PROPERTIES_RECORD * FindImageRecord ( IN EFI_PHYSICAL_ADDRESS ImageBase, - IN UINT64 ImageSize + IN UINT64 ImageSize, + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; =20 for (ImageRecordLink =3D ImageRecordList->ForwardLink; ImageRecordLink !=3D ImageRecordList; @@ -1526,7 +1532,7 @@ RemoveImageRecord ( return; } =20 - ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize); + ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize, &mImagePropertiesPrivateData.ImageR= ecordList); if (ImageRecord =3D=3D NULL) { DEBUG ((DEBUG_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n")); 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 (#110654): https://edk2.groups.io/g/devel/message/110654 Mute This Topic: https://groups.io/mt/102368846/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-