uEFI Virtual to Physical Memory translation

2.6k views Asked by At

I'm doing some experimenting with UEFI and haven't been able to wrap my head around the virtual addressing..

I have written uefi application that contains the string "CatsAreAwesome". I have the application print the virtual address of that string. It varies each execution so i'll stick with one specific example in this. The code prints that the string is at virtual address 0x120ac3c0. If I pause the VM and scan the vmem file I find two instances of the string at address 0x1209e410 and 0x12ab000

From calling getmemorymap in UEFI I find that the memory section those two fall in are

TYPE                     PhysStart        PhysEnd     VirtStart      VirtEnd
EfiConventionalMemory    1209C000       120A4000          0             8000
EfiLoaderCode            120A4000       120B1000          0             D000

I don't understand how the translation is working. Virtual start for those two sections are 0, which I would have thought meant identity mapped, but the virtual and physical addresses don't line up so that's obviously not correct. Can someone explain to me how the translation is working? How would I go from virtual to physical or vise versa?

My application prints the string, and gathers the memorymap so the map was gathered while the application was running. The application then waits for user input, I paused the vm during this time, so the physical addresses were found while the application was running.

2

There are 2 answers

0
haggai_e On

Looking at the EDK2 code, it seems that VirtualStart is always set to zero until the SetVirtualAddressMap runtime services function is called.

1
unixsmurf On

During Boot Services, UEFI and its applications are always running identity mapped. UEFI does not run address-translated, but permits bits that have been left resident to run at a virtual address assigned to them by an external agent.

After a successful call to ExitBootServices(), you can call SetVirtualAddressMap() to re-apply relocations and make the code possible to run at a given virtual address. The expected use-case for this is for providing Runtime Services while in an operating system context.