I have a hypervisor that virtualizes the currently running system ( OS in my case is Windows). I tried to run a code below and it works perfectly. The function is executed in VMX Root mode when vm-exit occurs.
void Cr3Test()
{
UINT64 GuestCr3;
UINT64 HostCr3 = __readcr3();
UINT64 GuestRIP;
UINT64 PhysAddress;
__vmx_vmread(VMCS::GUEST_CR3, &GuestCr3);
__vmx_vmread(VMCS::GUEST_RIP, &GuestRIP);
__writecr3(GuestCr3);
PhysAddress = MmGetPhysicalAddress((PVOID)GuestRIP).QuadPart;
__writecr3(HostCr3);
}
And I have one question about it: after Cr3 swapping we have an access to the code fetching, stack and RIP relative data without any trouble. How it works ? Why didn't the program change previous physical addresses mapping to the new process context ? In Windows the system address space is available to an arbitrary process with some conditions or what ?