Inverse mapping in KVM

I am pursuing a guest virtual address (gVA) retrieval mechanism based on guest physical address (gPA) in KVM. I am completely new to this area with very limited knowledge of KVM / linux memory management, so please bear with me while I try to describe the problem in detail.

Starting with linux-2.6, a mechanism called object-based inverse mapping (objrmap) is used to invalidate all PTEs that reference a page frame before unloading the page from memory. I understand that the anon_vma data structure cleverly collects all memory area descriptors relative to a given page frame, and it is mainly used the fact that these memory region descriptors have a pointer to a page table containing all the PTEs that reference the page frame to invalidate them. In your own setup (without virtualization), you can easily use the anon_vma data structure to search for a virtual address for a given page frame in the linear address space of the process by doing some simple arithmetic: combine vm_start from the area descriptor (vm_area_struct) and page->index from the page descriptor (struct page).

VA = anon_vma->vma_area_struct->vm_start + page->index

Here is an image from Understanding the Linux Kernel, edition 3, describing this mechanism for anonymous pages.

enter image description here

I was hoping to do something similar to find gVA, given the gPA in KVM, assuming two-dimensional paging (TDP) is enabled. However, when I looked at the KVM code, I see that KVM implements a reverse mapping scheme that stores a pointer to all the PTEs that reference the GFN (Guest Frame Number), as opposed to the objrmap implementation. Since x86 (4kB, 2MB, 1GB) requires 3 different page sizes, it stores pointers to guest PTE L1, L2, and L3. Since I cannot get a handle to the memory region, I cannot use the above mechanism in which I combine vm_start and page_index.

I have the following questions.

  • PTE L4, :

gVA[47:39] = (Address of L4 PTE - guest CR3)/PTE size

gVA[38:30] = (Address of L3 PTE - L4 PTE PFN)/PTE size

gVA[29:21] = (Address of L2 PTE - L3 PTE PFN)/PTE size

gVA[20:12] = (Address of L1 PTE - L2 PTE PFN)/PTE size

? , PTE L4?

  • - , kvm_mmu_page? , .

? , . .

+4

Source: https://habr.com/ru/post/1694274/


All Articles