Does exactly what it says. Entirely vibecoded by Gemini in two minutes.
I only wrote the C++ code and made some minor edits (like this note).
// Part of the Sequoia Hypervisor project. Works on both Intel and AMD (because why wouldn't it).
UINT64 Hv::HvmmComputeVA(
IN UINT16 Pml4Index,
IN UINT16 PdpIndex,
IN UINT16 PdIndex,
IN UINT16 PtIndex,
IN UINT16 PageOffset
)
{
constexpr auto mask = [](UINT8 Bits) -> UINT16
{
return (UINT16(1) << Bits) - 1;
};
VIRTUAL_ADDRESS address = {
.Fields =
{
.InpageIndex = static_cast<UINT16>(PageOffset & mask(12)),
.PteIndex = static_cast<UINT16>(PtIndex & mask(9)),
.PdeIndex = static_cast<UINT16>(PdIndex & mask(9)),
.PdpeIndex = static_cast<UINT16>(PdpIndex & mask(9)),
.Pml4Index = static_cast<UINT16>(Pml4Index & mask(9)),
.Pml5Index = 0
}
};
// Handle sign extension if the address is in the higher half (Bit 47 / PML4 Bit 8 set)
if (Pml4Index & 0x100)
address.Contents |= 0xFFFF000000000000;
return address.Contents;
}