rust-hypervisor-firmware icon indicating copy to clipboard operation
rust-hypervisor-firmware copied to clipboard

UEFI: correctly handle SetVirtualAddressMap

Open josephlr opened this issue 5 years ago • 0 comments
trafficstars

Right now, SetVirtualAddressMap in the UEFI compatibility layer just adjusts the virtual_start for the MemoryDescriptors returned by the allocator. However, this isn't what we should be doing (if I'm reading the spec correctly).

From the UEFI Specification 2.8 (Errata A), Section 8.4:

  • SetVirtualAddressMap should only be called exactly once during runtime (i.e. after calling ExitBootServices)
  • As Boot Services cannot be active when this is called, we don't need to modify the allocator at all (it won't be used again).
  • We do need to fixup any RuntimeServices code/data so that it can be called w/ non-identity paging.

The basic idea here would be to have separate ELF sections for EfiRuntimeServicesCode and EfiRuntimeServicesData. This would allow the remaining firmware to be unmapped by the OS. The EfiRuntimeServicesCode would need to be built with "relocation-model": "pic".

On a call to SetVirtualAddressMap, the code would then need to also fixup any pointers in static memory to use the new memory mapping. This can be automated by having the linker emit the necessary relocation entries.

EDK2's Implementation: https://github.com/tianocore/edk2/blob/3806e1fd139775610d8f2e7541a916c3a91ad989/MdeModulePkg/Core/RuntimeDxe/Runtime.c#L232

josephlr avatar Jun 29 '20 23:06 josephlr