Purpose of UEFI SetVirtualAddressMap Service

The SetVirtualAddressMap service in UEFI (Unified Extensible Firmware Interface) is a runtime service responsible for converting UEFI runtime code and data pointers from physical memory addresses to virtual memory addresses. During system boot, firmware executes in a flat, physically addressed environment. Once an operating system enables paging and establishes its own virtual memory management, it calls SetVirtualAddressMap to rebind remaining UEFI runtime functions—such as NVRAM variable access and system resets—into the operating system's virtual address space, ensuring the kernel can safely execute firmware code without memory conflicts.

During the pre-boot phase, UEFI operates in identity-mapped physical memory. In this state, it provides two categories of services: Boot Services and Runtime Services. When the operating system loader takes control of the machine, it calls ExitBootServices(). This call terminates all boot-time services and reclaims the memory they occupied for general operating system use. However, UEFI Runtime Services are designed to persist and remain callable by the operating system throughout the entire duration of the system's operation.

Modern operating systems execute within virtual address spaces managed by memory management units (MMUs) and page tables. Because the operating system dictates how memory is mapped and protected, the static physical addresses originally used by the UEFI runtime drivers are no longer directly usable within the kernel's execution context.

To bridge this gap, the operating system uses SetVirtualAddressMap. The process operates as follows:

  1. Memory Map Retrieval: Before calling ExitBootServices, the OS obtains the UEFI memory map via GetMemoryMap(). It identifies all memory regions marked as EfiRuntimeServicesCode and EfiRuntimeServicesData.
  2. Virtual Mapping Allocation: The OS kernel reserves a portion of its virtual address space and assigns specific virtual addresses to each physical runtime region.
  3. Descriptor Passing: The operating system calls SetVirtualAddressMap, passing an array of memory descriptors that explicitly map each physical memory range to its designated virtual address.
  4. Internal Pointer Fixing: The firmware parses this table and updates all its internal pointers, data references, and function entry points to point to the new virtual locations.

SetVirtualAddressMap has strict usage constraints defined by the UEFI specification. It can only be called once per system boot, and only after ExitBootServices has completed. Once the call returns successfully, the firmware's runtime services can no longer be called via physical addresses; any subsequent calls to services like GetVariable or ResetSystem must use the newly mapped virtual addresses.

By handling the transition from physical addressing to the operating system's virtual addressing model, SetVirtualAddressMap ensures that essential low-level hardware and firmware capabilities remain integrated with the operating system without compromising modern memory protection mechanisms.