From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web11.69427.1673263822264377288 for ; Mon, 09 Jan 2023 03:30:22 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AyriJ4Ib; spf=pass (domain: redhat.com, ip: 170.10.133.124, mailfrom: lersek@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673263821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NUUn4ufVqGIWSAMElgUZRZbmtDIDjIQxmz0M9NXbX8U=; b=AyriJ4IbIyeaD7B+vAfQkEvMVbU1JUs71KGKersJo3LfoNeh4UagG+9Y8Gbk3sstnLb3Qu GNbbx+7dz5JJ9WhLmeBeIC1hWF8YySfgXVc7jR/OONYnGBnbH3l32YI2SL+n+sHdOBLuLa ZrnQMOZ0s8//AoVOa9CsN9PKzA0wg3I= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-388-OkRykveAOkCixm5HLFHucQ-1; Mon, 09 Jan 2023 06:30:18 -0500 X-MC-Unique: OkRykveAOkCixm5HLFHucQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C99442999B25; Mon, 9 Jan 2023 11:30:17 +0000 (UTC) Received: from [10.39.192.104] (unknown [10.39.192.104]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 10EC92166B26; Mon, 9 Jan 2023 11:30:15 +0000 (UTC) Message-ID: <93f26d69-8edd-d061-ef27-c45db118f69b@redhat.com> Date: Mon, 9 Jan 2023 12:30:14 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [PATCH 1/2] OvmfPkg/PlatformInitLib: update PlatformScanOrAdd64BitE820Ram documentation To: Gerd Hoffmann Cc: devel@edk2.groups.io, Ard Biesheuvel , Jordan Justen , Pawel Polawski , Oliver Steffen , Jiewen Yao References: <20230106140403.2889131-1-kraxel@redhat.com> <20230106140403.2889131-2-kraxel@redhat.com> <20230109101505.ehipkirgssqyd3rr@sirius.home.kraxel.org> From: "Laszlo Ersek" In-Reply-To: <20230109101505.ehipkirgssqyd3rr@sirius.home.kraxel.org> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 1/9/23 11:15, Gerd Hoffmann wrote: >>> + @param[out] MaxAddress If MaxAddress is not NULL, then MaxAddress holds >>> + the highest exclusive >=4GB RAM address on output. >>> + If QEMU's fw_cfg E820 RAM map contains no RAM entry >>> + that starts outside of the 32-bit address range, >>> + then MaxAddress is exactly 4GB on output. >>> >>> @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed. >>> >> >> I've tried to review the function in its current state, but I don't >> understand the code either. Originally this function had two behaviors, >> reflected by its name as well ("Scan Or Add 64 Bit E820 Ram"), and its >> sole (NULL-able) parameter MaxAddress would switch between those two >> behaviors. The single "if" in the loop body, and the loop body in >> general was trivial -- and AddMemoryRangeHob() call on one branch, and a >> maximum search/comparison step on the other. Entry types other than >> EfiAcpiAddressRangeMemory were summarily ignored. >> >> Now the function does many more things, especially at the end of this >> series. It does things for EfiAcpiAddressRangeReserved, but only when >> AddHighHob is TRUE. It implements a maximum search for LowMemory as >> well. The function name "Scan Or Add 64 Bit E820 Ram" has become a >> misnomer. It's not just the function comment block that is out of date, >> but the function's name too. >> >> The function's initially simple structure can clealy not carry all its >> new tasks; I'm struggling to read the function definition. This is best >> shown by the multiple calls to the function in the code base, where we >> have a plethora of NULLs and TRUE/FALSE arguments, much obscuring the >> intended purpose of those calls. > > Well, it's not *that* different from the original. The implicit add-hob > request (via MaxAddress == NULL) has been replaced by an explicit bool. > MaxAddress works the same way it used to when non-NULL. > LowMemory has the same behavior (set to non-NULL to have the value returned). > > Handling reservation hobs and reservation conflicts too doesn't fit in > that well indeed. > >> The reason I originally wrote the function the way I did is that it >> would run in PEI. Small memory allocations go into HOBs in PEI, and >> cannot be freed (see FreePool() in >> "MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c"). Page >> allocations work, but I deemed that overkill, and there would be only >> two calls to this function anyway. Therefore, looping through the fw_cfg >> file twice, even using Port IO (for example in a SEV guest) would not be >> a big deal, not to mention when DMA would be available (the common case). >> >> But that no longer holds. We have a bunch of calls now. > > The code need to also work in SEC now. Sigh, yes. Thanks for the reminder. > Using a HOB should work too, and given that the number of e820 entries > is rather small (2-4 entries with 20 bytes each) it might not be that > much of a problem to have that permanently allocated. > > I think a page allocator is not available in SEC. Right. Another option (because the lifetime of the E820 table from QEMU is strongly limited, as far as the firmware is concerned) is perhaps just to have EFI_E820_ENTRY64 E820Entries[32]; somewhere higher up the call stack, as a local variable, and to pass it along with the platform info hob wherever it is needed. Then, once this "outer" function completes, the E820 map goes away too. ... Arguably, that would require changes to "PlatformInitLib.h" as well, and would introduce quite a bit of churn in clients of PlatformInitLib. Embedding the above fixed-size, 32-element array into EFI_HOB_PLATFORM_INFO might be simplest. Laszlo > >> (1) Perform an initial set of checks, for the existence & proper size, >> of the fw_cfg file. Allocate the necessary number of pages, download the >> file, before the first scan. Implement all the scans based on the >> downloaded file, with separate, open-coded loops at every current call >> site. After the last use, release the pages. > > Looks like the better option to me. > >> (2) Alternatively, keep the current, outermost, checking and looping >> logic in the function, so that we not need dynamic memory just like >> before. However, the internals should be broken out, by taking a >> callback function pointer as parameter. The callback function would have >> two parameters: the E820 Entry just found, and a "VOID *Context" pointer. > > Was thinking about that one, but I don't like passing around VOID > pointers and the overhead coming from the 4 callback functions. > > Maybe I can pass around EFI_HOB_PLATFORM_INFO pointers instead. > > take care, > Gerd >