From: "Laszlo Ersek" <lersek@redhat.com>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: devel@edk2.groups.io, jon@solid-run.com,
Hao A Wu <hao.a.wu@intel.com>,
Liming Gao <gaoliming@byosoft.com.cn>,
"Ard Biesheuvel (TianoCore)" <ardb+tianocore@kernel.org>,
"Leif Lindholm (Nuvia address)" <leif@nuviainc.com>
Subject: Re: [edk2-devel] Conflicting virtual addresses causing Runtime Services issues
Date: Fri, 12 Mar 2021 20:17:21 +0100 [thread overview]
Message-ID: <b8f12f82-cc98-1dbe-e936-83a66fec68df@redhat.com> (raw)
In-Reply-To: <CAMj1kXFnXZQ8=AJo_echynK_HgmKWGQY8pqw1MUoVkO8Sd63Yg@mail.gmail.com>
On 03/11/21 23:39, Ard Biesheuvel wrote:
> On Thu, 11 Mar 2021 at 23:25, Laszlo Ersek <lersek@redhat.com> wrote:
>>
>> Adding Ard and Leif, comments below:
>>
>> On 03/11/21 15:50, Laszlo Ersek wrote:
>>> On 03/11/21 10:48, Jon Nettleton wrote:
>>
>> [...]
>>
>>>> And this is where the pointer gets remapped again and into the MMIO
>>>> space of the nor flash. If I remove the calls to ConvertPointer for
>>>> the FvbProtocol I am still seeing those addresses getting remapped
>>>> but only once and runtime works as expected.
>>>>
>>>> I am seeing that in
>>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
>>>> &mVariableModuleGlobal->FvbInstance->* are all being converted. It
>>>> is possible this is a long standing bug and it just so happens that
>>>> our configuration has caused a conflict and exposed it.
>>>
>>> Yes, this is curious, I noticed it too yesterday, trying to see where
>>> the FVB protocol member function pointers were converted. I found that
>>> OVMF's flash driver (OvmfPkg/QemuFlashFvbServicesRuntimeDxe) didn't do
>>> it, but MdeModulePkg/Universal/Variable/RuntimeDxe did. That was
>>> certainly strange, as the variable driver is a consumer of the
>>> protocol (not the producer thereof), so I'd say it has no business
>>> poking new values into the protocol interface structure.
>>
>> [...]
>>
>>> ... Strangely, the other flash (FVB) driver in edk2,
>>> ArmPlatformPkg/Drivers/NorFlashDxe, *does* perform the conversion
>>> itself! See NorFlashVirtualNotifyEvent().
>>>
>>> I don't understand that. Is it possible that, with
>>> "ArmPlatformPkg/Drivers/NorFlashDxe" too, the conversion happens
>>> *twice*, but (at least) one of those mappings is "identity"?
>>
>> Confirmed.
>>
>> I had to write some elaborate debug patches for determining this,
>> because in ArmVirtQemu, I cannot produce DEBUG output from the
>> SetVirtualAddressMap() notification functions. So here's the approach I
>> took:
>>
>> (1) Introduce a new GUID-ed HOB structure in MdeModulePkg. The structure
>> itself lives in reserved memory, but its address is exposed in a GUID-ed
>> HOB. The structure is named FVB_ADDRESS_LIST, and it has the following
>> fields:
>>
>> - signature ("FVBADRLS" -- FVB Address List)
>> - 16 entries of:
>> - owner signature [what driver set this entry]
>> - address
>> - number of entries used (aka next entry to fill)
>>
>> (2) In PlatformPei, allocate and initialize this structure (in reserved
>> memory), and expose its address via the GUID-ed HOB. Furthermore,
>> produce a log message with the allocation address.
>>
>> (3) In NorFlashDxe, look up the structure via the GUID-ed HOB, in the
>> entry point function; remember the address in a global variable. In the
>> SetVirtualAddressMap() handler function, treat the conversion of the
>> "GetPhysicalAddress" FVB member function specially: via the global
>> variable pointer to FVB_ADDRESS_LIST in reserved memory, save both the
>> physical (original) and the virtual (converted) address of the
>> "GetPhysicalAddress" FVB member function, in new entries. As owner
>> signature in both entries, use "NORFLASH".
>>
>> (4) In the runtime DXE variable driver, do the exact same thing, just
>> use a different "owner signature" -- "VARIABLE".
>>
>> (5) Once the guest is up and running, run "efibootmgr --delete-timeout"
>> at a root prompt in the guest, deleting the existent "Timeout" UEFI
>> non-volatile variable, for verifying that the runtime variable (write)
>> service is functional.
>>
>> (6) Using the log message from point (2):
>>
>>> PlatformPeim: FvbAddressList @ 13FEC9000
>>
>> hexdump the guest memory containing the FVB_ADDRESS_LIST, as follows:
>>
>>> $ virsh qemu-monitor-command aavmf.rhel7.registered --hmp xp /268cb 0x13FEC9000
>>
>> Ccomments to the right of the hexdump:
>>
>>> 000000013fec9000: 'F' 'V' 'B' 'A' 'D' 'R' 'L' 'S' <- structure signature: FVBADRLS
>>> 000000013fec9008: 'N' 'O' 'R' 'F' 'L' 'A' 'S' 'H' <- entry[0], signature: NORFLASH
>>> 000000013fec9010: 'T' ' ' '\xc6' ';' '\x01' '\x00' '\x00' '\x00' <- entry[0], GetPhysicalAddress *physical*: 0x000000013bc62054
>>> 000000013fec9018: 'N' 'O' 'R' 'F' 'L' 'A' 'S' 'H' <- entry[1], signature: NORFLASH
>>> 000000013fec9020: 'T' ' ' 'N' '$' '\x00' '\x00' '\x00' '\x00' <- entry[1], GetPhysicalAddress *virtual*: 0x00000000244e2054
>>> 000000013fec9028: 'V' 'A' 'R' 'I' 'A' 'B' 'L' 'E' <- entry[2], signature: VARIABLE
>>> 000000013fec9030: 'T' ' ' 'N' '$' '\x00' '\x00' '\x00' '\x00' <- entry[2], GetPhysicalAddress *physical*: 0x00000000244e2054
>>> 000000013fec9038: 'V' 'A' 'R' 'I' 'A' 'B' 'L' 'E' <- entry[3], signature: VARIABLE
>>> 000000013fec9040: 'T' ' ' 'N' '$' '\x00' '\x00' '\x00' '\x00' <- entry[3], GetPhysicalAddress *virtual*: 0x00000000244e2054
>>> 000000013fec9048: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9050: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9058: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9060: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9068: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9070: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9078: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9080: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9088: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9090: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9098: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90a0: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90a8: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90b0: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90b8: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90c0: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90c8: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90d0: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90d8: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90e0: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90e8: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90f0: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec90f8: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9100: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00'
>>> 000000013fec9108: '\x04' '\x00' '\x00' '\x00' <- number of entries used: 4
>>
>> This shows the following:
>>
>> - both NorFlashDxe and the runtime DXE variable driver converted the
>> FVB.GetPhysicalAddress member function,
>>
>> - the NorFlashDxe driver acted first, the runtime DXE variable driver
>> acted second,
>>
>> - when the runtime DXE variable driver "converted" the "physical"
>> address to virtual address, there was no change (and no crash!),
>> because the virtual address map passed in by the Linux kernel
>> apparently identity maps this area -- just as I guessed.
>>
>> So we definitely have a bug (only Linux's page tables save us from the
>> crash); now the question is:
>>
>> Which driver is wrong to even attempt the conversion of the FVB member
>> functions?
>>
>> The answer must be documented somewhere highly visible.
>>
>> Debug patches attached, for the record (based on commit edd46cd407ea).
>>
>
> Thanks for inviting me to this party!
>
> So the tl;dr here is that some points get converted twice, which
> usually is not a problem because the virtual address resulting from
> the conversion is rarely mistaken for a physical address living in a
> EFI_MEMORY_RUNTIME region.
Ah, good point!
Where I assumed that an identity mapping must have existed, from the
OS's mappings, there's a much simpler explanation indeed:
If the "physical address" that's being converted simply doesn't fall
into a domain that's supposed to be runtime-mapped (per the "VirtualMap"
parameter of SetVirtualAddressMap()), the ConvertPointer() call simply
fails with EFI_NOT_FOUND, and the pointer is left intact.
> So I agree with Laszlo's assertion that the consumer of a protocol has
> no business updating its protocol pointers, so this should definitely
> be fixed in the core VariableRuntime driver. However, given the
> typical nature of the variable stack, i.e., a platform specfic NOR
> flash driver combined with the generic FTW and variable drivers, doing
> so would likely break many out of tree platforms where the NOR flash
> driver does not bother to update its pointers at all.
Yes, this is indeed the compatibility argument.
Where I see a gray area though is the PI spec. I checked PI v1.7
yesterday (all occurrences of "runtime"), and FVB drivers / protocols
are not required to be runtime drivers / protocol -- not even the
*possibility* is raised. The variable write arch protocol / driver must
be runtime, but how that may (or may not) translate to FVB is not
mentioned, as far as I recall.
FWIW, the variable driver bug goes back to historical commit
8a9e0b7274c69, dated 2009-03-09. The commit message is... obscure.
Hmmm... look at related commit 00f3851372eb ("retire FvbServiceLib class
in MdeModulePkg [...]", 2009-03-09). It looks like the ConvertPointer()
stuff was originally there? "Firmeware Volume BLock Service Library".
FvbServiceLib seems to have been a helper library for FVB drivers, and
so it was in its right to offer pointer conversion services...
FvbLibInitialize() was the constructor, and it registered
FvbVirtualAddressChangeNotifyEvent().
FvbServiceLib was originally added in commit 677472aae492, dated 2008-10-25.
I don't know why commit 8a9e0b7274c69 merged the pointer conversion into
the variable driver; that seems to have been wrong. But... it's been
with us for 12 years now :/
Thanks
Laszlo
prev parent reply other threads:[~2021-03-12 19:17 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-10 8:04 Conflicting virtual addresses causing Runtime Services issues Jon Nettleton
2021-03-10 14:52 ` [edk2-devel] " Laszlo Ersek
2021-03-11 6:53 ` Jon Nettleton
[not found] ` <166B374585A9D8FC.18699@groups.io>
2021-03-11 9:48 ` Jon Nettleton
2021-03-11 14:50 ` Laszlo Ersek
2021-03-11 15:49 ` Jon Nettleton
2021-03-11 22:25 ` Laszlo Ersek
2021-03-11 22:39 ` Ard Biesheuvel
2021-03-12 3:01 ` Jon Nettleton
[not found] ` <166B792D1514133B.31346@groups.io>
2021-03-12 5:59 ` Jon Nettleton
2021-03-12 19:30 ` Laszlo Ersek
2021-03-12 19:52 ` Ard Biesheuvel
2021-03-12 19:17 ` Laszlo Ersek [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b8f12f82-cc98-1dbe-e936-83a66fec68df@redhat.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox