From nobody Fri May 3 04:07:11 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+76669+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+76669+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=9elements.com ARC-Seal: i=1; a=rsa-sha256; t=1623932738; cv=none; d=zohomail.com; s=zohoarc; b=K6CIIX/ctphp7qgpeh30Ue2kg3oV13qlNKHQS0bpBEfRq3J4f5qD+r/fKl0eFdmqLyr4uyC1ydKq6pjkGB4YP8Sa3L63Vdf7r1DLPbdmCnN0J0nbQK4179ULb09jT7LqdjpTt3uMLl1B7E3GiD0IKWNTwrA/vf7osUGDkTtkLGE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623932738; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=1HiXAmNaSKI/yLqawTxTv2yk4lr/hFUQPknL/Irp+7k=; b=A3xMAJ+NFyfxsKRkdtYw2SS9QOf19ub/iVKSpcUmBsV6deDK4oLzvkAwG/JpevURvudn//ZMa+PDI7gPK9hUCrpAUI5ZJUa4HCH+u1LOMDMpYJNVQgjUEwf/8plMW0jU0qlaPtz7Ylgl9DOcktYdW4nL23JCTcw1o8tm8Gp2VnE= 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+76669+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 1623932738062172.47379372636362; Thu, 17 Jun 2021 05:25:38 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id PW73YY1788612xQCMVZeuf9U; Thu, 17 Jun 2021 05:25:37 -0700 X-Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by mx.groups.io with SMTP id smtpd.web09.6775.1623932731901954826 for ; Thu, 17 Jun 2021 05:25:32 -0700 X-Received: by mail-ej1-f48.google.com with SMTP id ho18so9556122ejc.8 for ; Thu, 17 Jun 2021 05:25:31 -0700 (PDT) X-Gm-Message-State: dhLnJtSpM5scWztJJgcsJ5JSx1787277AA= X-Google-Smtp-Source: ABdhPJwjdvW5bGw1qJbQqzOt2ysp/rBVdYHv5er5nmBxRVMreZ1akWo6QMlPxBK7o1+cnVV8obQRjg== X-Received: by 2002:a17:906:2ec6:: with SMTP id s6mr4828214eji.65.1623932730314; Thu, 17 Jun 2021 05:25:30 -0700 (PDT) X-Received: from rudolphp-notebook.9e.network (ip-178-202-79-247.hsi09.unitymediagroup.de. [178.202.79.247]) by smtp.gmail.com with ESMTPSA id h7sm3709230ejp.24.2021.06.17.05.25.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 05:25:29 -0700 (PDT) From: "Patrick Rudolph" To: devel@edk2.groups.io Cc: maurice.ma@intel.com, guo.dong@intel.com, benjamin.you@intel.com Subject: [edk2-devel] [PATCH v2] UefiPayloadPkg/UefiPayloadEntry: Improve bootloader memrange parsing Date: Thu, 17 Jun 2021 14:25:28 +0200 Message-Id: <20210617122528.280133-1-patrick.rudolph@9elements.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,patrick.rudolph@9elements.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1623932737; bh=NT3uq39YmdCux18euApfp0wHA9e6JMmylqmXydcrktU=; h=Cc:Date:From:Reply-To:Subject:To; b=Q6KZBN8FuSE7qPSW2cLGXMJxaqCr4YU/x185KgggdDa7G2KV/VfQnTi43Noedcs6Ocz gadwkIwsQmofiKDnfS+xwbRTIkWsg4dOkIB3o0VonPgjLR2L/VXHW3ou4cufl9F73HJCE m1rPtne1b6XXQSSMUaCTEaN43jqAECdfDJk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" Currently several DXE crash due to invalid memory resource settings. The PciHostBridgeDxe which expects the MMCONF and PCI Aperature to be EfiMemoryMappedIO, but currently those regions are (partly) mapped as EfiReservedMemoryType. coreboot and slimbootloader provide an e820 compatible memory map, which doesn't work well with EDK2 as the e820 spec is missing MMIO regions. In e820 'reserved' could either mean "DRAM used by boot firmware" or "MMIO in use and not detectable by OS". Guess Top of lower usable DRAM (TOLUD) by walking the bootloader provided memory ranges. Memory types of RAM, ACPI and ACPI NVS below 4 GiB are used to increment TOLUD and reserved memory ranges touching TOLUD at the base are also assumed to be reserved DRAM, which increment TOLUD. Then mark everything reserved below TOLUD as EfiReservedMemoryType and everything reserved above TOLUD as EfiMemoryMappedIO. This fixes assertions seen in PciHostBridgeDxe. Signed-off-by: Patrick Rudolph --- .../UefiPayloadEntry/UefiPayloadEntry.c | 190 +++++++++++++++++- .../UefiPayloadEntry/UefiPayloadEntry.h | 10 + 2 files changed, 197 insertions(+), 3 deletions(-) diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c b/UefiPaylo= adPkg/UefiPayloadEntry/UefiPayloadEntry.c index 805f5448d9..1b2a674401 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c +++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c @@ -7,10 +7,159 @@ =20 #include "UefiPayloadEntry.h" =20 +STATIC UINT32 mTopOfLowerUsableDram =3D 0; + /** Callback function to build resource descriptor HOB =20 This function build a HOB based on the memory map entry info. + It creates only EFI_RESOURCE_MEMORY_MAPPED_IO and EFI_RESOURCE_MEMORY_R= ESERVED + resources. + + @param MemoryMapEntry Memory map entry info got from bootloader. + @param Params A pointer to ACPI_BOARD_INFO. + + @retval EFI_SUCCESS Successfully build a HOB. + @retval EFI_INVALID_PARAMETER Invalid parameter provided. +**/ +EFI_STATUS +MemInfoCallbackMmio ( + IN MEMROY_MAP_ENTRY *MemoryMapEntry, + IN VOID *Params + ) +{ + EFI_PHYSICAL_ADDRESS Base; + EFI_RESOURCE_TYPE Type; + UINT64 Size; + EFI_RESOURCE_ATTRIBUTE_TYPE Attribue; + ACPI_BOARD_INFO *AcpiBoardInfo; + + AcpiBoardInfo =3D (ACPI_BOARD_INFO *)Params; + if (AcpiBoardInfo =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Skip types already handled in MemInfoCallback + // + if (MemoryMapEntry->Type =3D=3D E820_RAM || MemoryMapEntry->Type =3D=3D = E820_ACPI) { + return EFI_SUCCESS; + } + + if (MemoryMapEntry->Base =3D=3D AcpiBoardInfo->PcieBaseAddress) { + // + // MMCONF is always MMIO + // + Type =3D EFI_RESOURCE_MEMORY_MAPPED_IO; + } else if (MemoryMapEntry->Base < mTopOfLowerUsableDram) { + // + // It's in DRAM and thus must be reserved + // + Type =3D EFI_RESOURCE_MEMORY_RESERVED; + } else if ((MemoryMapEntry->Base < 0x100000000ULL) && (MemoryMapEntry->B= ase >=3D mTopOfLowerUsableDram)) { + // + // It's not in DRAM, must be MMIO + // + Type =3D EFI_RESOURCE_MEMORY_MAPPED_IO; + } else { + Type =3D EFI_RESOURCE_MEMORY_RESERVED; + } + + Base =3D MemoryMapEntry->Base; + Size =3D MemoryMapEntry->Size; + + Attribue =3D EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_TESTED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE; + + BuildResourceDescriptorHob (Type, Attribue, (EFI_PHYSICAL_ADDRESS)Base, = Size); + DEBUG ((DEBUG_INFO , "buildhob: base =3D 0x%lx, size =3D 0x%lx, type =3D= 0x%x\n", Base, Size, Type)); + + if (MemoryMapEntry->Type =3D=3D E820_UNUSABLE || + MemoryMapEntry->Type =3D=3D E820_DISABLED) { + BuildMemoryAllocationHob (Base, Size, EfiUnusableMemory); + } else if (MemoryMapEntry->Type =3D=3D E820_PMEM) { + BuildMemoryAllocationHob (Base, Size, EfiPersistentMemory); + } + + return EFI_SUCCESS; +} + + +/** + Callback function to find TOLUD (Top of Lower Usable DRAM) + + Estimate where TOLUD (Top of Lower Usable DRAM) resides. The exact posi= tion + would require platform specific code. + + @param MemoryMapEntry Memory map entry info got from bootloader. + @param Params Not used for now. + + @retval EFI_SUCCESS Successfully updated mTopOfLowerUsableDra= m. +**/ +EFI_STATUS +FindToludCallback ( + IN MEMROY_MAP_ENTRY *MemoryMapEntry, + IN VOID *Params + ) +{ + // + // This code assumes that the memory map on this x86 machine below 4GiB = is continous + // until TOLUD. In addition it assumes that the bootloader provided memo= ry tables have + // no "holes" and thus the first memory range not covered by e820 marks = the end of + // usable DRAM. In addition it's assumed that every reserved memory regi= on touching + // usable RAM is also covering DRAM, everything else that is marked rese= rved thus must be + // MMIO not detectable by bootloader/OS + // + + // + // Skip memory types not RAM or reserved + // + if ((MemoryMapEntry->Type =3D=3D E820_UNUSABLE) || (MemoryMapEntry->Type= =3D=3D E820_DISABLED) || + (MemoryMapEntry->Type =3D=3D E820_PMEM)) { + return EFI_SUCCESS; + } + + // + // Skip resources above 4GiB + // + if (MemoryMapEntry->Base >=3D 0x100000000ULL) { + return EFI_SUCCESS; + } + + if ((MemoryMapEntry->Type =3D=3D E820_RAM) || (MemoryMapEntry->Type =3D= =3D E820_ACPI) || + (MemoryMapEntry->Type =3D=3D E820_NVS)) { + // + // It's usable DRAM. Update TOLUD. + // + if (mTopOfLowerUsableDram < (MemoryMapEntry->Base + MemoryMapEntry->Si= ze)) { + mTopOfLowerUsableDram =3D MemoryMapEntry->Base + MemoryMapEntry->Siz= e; + } + } else { + // + // It might be reserved DRAM or MMIO. + // + // If it touches usable DRAM at Base assume it's DRAM as well, + // as it could be bootloader installed tables, TSEG, GTT, ... + // + if (mTopOfLowerUsableDram =3D=3D MemoryMapEntry->Base) { + mTopOfLowerUsableDram =3D MemoryMapEntry->Base + MemoryMapEntry->Siz= e; + } + } + + return EFI_SUCCESS; +} + + +/** + Callback function to build resource descriptor HOB + + This function build a HOB based on the memory map entry info. + Only add EFI_RESOURCE_SYSTEM_MEMORY. =20 @param MemoryMapEntry Memory map entry info got from bootloader. @param Params Not used for now. @@ -28,7 +177,16 @@ MemInfoCallback ( UINT64 Size; EFI_RESOURCE_ATTRIBUTE_TYPE Attribue; =20 - Type =3D (MemoryMapEntry->Type =3D=3D 1) ? EFI_RESOURCE_SYSTEM_MEMORY= : EFI_RESOURCE_MEMORY_RESERVED; + // + // Skip everything not known to be usable DRAM. + // It will be added later. + // + if ((MemoryMapEntry->Type !=3D E820_RAM) && (MemoryMapEntry->Type !=3D E= 820_ACPI) && + (MemoryMapEntry->Type !=3D E820_NVS)) { + return RETURN_SUCCESS; + } + + Type =3D EFI_RESOURCE_SYSTEM_MEMORY; Base =3D MemoryMapEntry->Base; Size =3D MemoryMapEntry->Size; =20 @@ -40,7 +198,7 @@ MemInfoCallback ( EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE; =20 - if (Base >=3D BASE_4GB ) { + if (Base >=3D BASE_4GB) { // Remove tested attribute to avoid DXE core to dispatch driver to mem= ory above 4GB Attribue &=3D ~EFI_RESOURCE_ATTRIBUTE_TESTED; } @@ -48,6 +206,12 @@ MemInfoCallback ( BuildResourceDescriptorHob (Type, Attribue, (EFI_PHYSICAL_ADDRESS)Base, = Size); DEBUG ((DEBUG_INFO , "buildhob: base =3D 0x%lx, size =3D 0x%lx, type =3D= 0x%x\n", Base, Size, Type)); =20 + if (MemoryMapEntry->Type =3D=3D E820_ACPI) { + BuildMemoryAllocationHob (Base, Size, EfiACPIReclaimMemory); + } else if (MemoryMapEntry->Type =3D=3D E820_NVS) { + BuildMemoryAllocationHob (Base, Size, EfiACPIMemoryNVS); + } + return RETURN_SUCCESS; } =20 @@ -236,8 +400,19 @@ BuildHobFromBl ( EFI_PEI_GRAPHICS_DEVICE_INFO_HOB *NewGfxDeviceInfo; =20 // - // Parse memory info and build memory HOBs + // First find TOLUD + // + DEBUG ((DEBUG_INFO , "Guessing Top of Lower Usable DRAM:\n")); + Status =3D ParseMemoryInfo (FindToludCallback, NULL); + if (EFI_ERROR(Status)) { + return Status; + } + DEBUG ((DEBUG_INFO , "Assuming TOLUD =3D 0x%x\n", mTopOfLowerUsableDram)= ); + + // + // Parse memory info and build memory HOBs for Usable RAM // + DEBUG ((DEBUG_INFO , "Building ResourceDescriptorHobs for usable memory:= \n")); Status =3D ParseMemoryInfo (MemInfoCallback, NULL); if (EFI_ERROR(Status)) { return Status; @@ -289,6 +464,15 @@ BuildHobFromBl ( DEBUG ((DEBUG_INFO, "Create acpi board info guid hob\n")); } =20 + // + // Parse memory info and build memory HOBs for reserved DRAM and MMIO + // + DEBUG ((DEBUG_INFO , "Building ResourceDescriptorHobs for reserved memor= y:\n")); + Status =3D ParseMemoryInfo (MemInfoCallbackMmio, &AcpiBoardInfo); + if (EFI_ERROR(Status)) { + return Status; + } + // // Parse platform specific information. // diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h b/UefiPaylo= adPkg/UefiPayloadEntry/UefiPayloadEntry.h index 2c84d6ed53..4fd50e47cd 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h +++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h @@ -38,6 +38,16 @@ #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \ ((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((= Alignment) - 1))) =20 + +#define E820_RAM 1 +#define E820_RESERVED 2 +#define E820_ACPI 3 +#define E820_NVS 4 +#define E820_UNUSABLE 5 +#define E820_DISABLED 6 +#define E820_PMEM 7 +#define E820_UNDEFINED 8 + /** Auto-generated function that calls the library constructors for all of t= he module's dependent libraries. --=20 2.30.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 (#76669): https://edk2.groups.io/g/devel/message/76669 Mute This Topic: https://groups.io/mt/83602735/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-