1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun====================== 4*4882a593SmuzhiyunThe x86 kvm shadow mmu 5*4882a593Smuzhiyun====================== 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunThe mmu (in arch/x86/kvm, files mmu.[ch] and paging_tmpl.h) is responsible 8*4882a593Smuzhiyunfor presenting a standard x86 mmu to the guest, while translating guest 9*4882a593Smuzhiyunphysical addresses to host physical addresses. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunThe mmu code attempts to satisfy the following requirements: 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun- correctness: 14*4882a593Smuzhiyun the guest should not be able to determine that it is running 15*4882a593Smuzhiyun on an emulated mmu except for timing (we attempt to comply 16*4882a593Smuzhiyun with the specification, not emulate the characteristics of 17*4882a593Smuzhiyun a particular implementation such as tlb size) 18*4882a593Smuzhiyun- security: 19*4882a593Smuzhiyun the guest must not be able to touch host memory not assigned 20*4882a593Smuzhiyun to it 21*4882a593Smuzhiyun- performance: 22*4882a593Smuzhiyun minimize the performance penalty imposed by the mmu 23*4882a593Smuzhiyun- scaling: 24*4882a593Smuzhiyun need to scale to large memory and large vcpu guests 25*4882a593Smuzhiyun- hardware: 26*4882a593Smuzhiyun support the full range of x86 virtualization hardware 27*4882a593Smuzhiyun- integration: 28*4882a593Smuzhiyun Linux memory management code must be in control of guest memory 29*4882a593Smuzhiyun so that swapping, page migration, page merging, transparent 30*4882a593Smuzhiyun hugepages, and similar features work without change 31*4882a593Smuzhiyun- dirty tracking: 32*4882a593Smuzhiyun report writes to guest memory to enable live migration 33*4882a593Smuzhiyun and framebuffer-based displays 34*4882a593Smuzhiyun- footprint: 35*4882a593Smuzhiyun keep the amount of pinned kernel memory low (most memory 36*4882a593Smuzhiyun should be shrinkable) 37*4882a593Smuzhiyun- reliability: 38*4882a593Smuzhiyun avoid multipage or GFP_ATOMIC allocations 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunAcronyms 41*4882a593Smuzhiyun======== 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun==== ==================================================================== 44*4882a593Smuzhiyunpfn host page frame number 45*4882a593Smuzhiyunhpa host physical address 46*4882a593Smuzhiyunhva host virtual address 47*4882a593Smuzhiyungfn guest frame number 48*4882a593Smuzhiyungpa guest physical address 49*4882a593Smuzhiyungva guest virtual address 50*4882a593Smuzhiyunngpa nested guest physical address 51*4882a593Smuzhiyunngva nested guest virtual address 52*4882a593Smuzhiyunpte page table entry (used also to refer generically to paging structure 53*4882a593Smuzhiyun entries) 54*4882a593Smuzhiyungpte guest pte (referring to gfns) 55*4882a593Smuzhiyunspte shadow pte (referring to pfns) 56*4882a593Smuzhiyuntdp two dimensional paging (vendor neutral term for NPT and EPT) 57*4882a593Smuzhiyun==== ==================================================================== 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunVirtual and real hardware supported 60*4882a593Smuzhiyun=================================== 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunThe mmu supports first-generation mmu hardware, which allows an atomic switch 63*4882a593Smuzhiyunof the current paging mode and cr3 during guest entry, as well as 64*4882a593Smuzhiyuntwo-dimensional paging (AMD's NPT and Intel's EPT). The emulated hardware 65*4882a593Smuzhiyunit exposes is the traditional 2/3/4 level x86 mmu, with support for global 66*4882a593Smuzhiyunpages, pae, pse, pse36, cr0.wp, and 1GB pages. Emulated hardware also 67*4882a593Smuzhiyunable to expose NPT capable hardware on NPT capable hosts. 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunTranslation 70*4882a593Smuzhiyun=========== 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunThe primary job of the mmu is to program the processor's mmu to translate 73*4882a593Smuzhiyunaddresses for the guest. Different translations are required at different 74*4882a593Smuzhiyuntimes: 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun- when guest paging is disabled, we translate guest physical addresses to 77*4882a593Smuzhiyun host physical addresses (gpa->hpa) 78*4882a593Smuzhiyun- when guest paging is enabled, we translate guest virtual addresses, to 79*4882a593Smuzhiyun guest physical addresses, to host physical addresses (gva->gpa->hpa) 80*4882a593Smuzhiyun- when the guest launches a guest of its own, we translate nested guest 81*4882a593Smuzhiyun virtual addresses, to nested guest physical addresses, to guest physical 82*4882a593Smuzhiyun addresses, to host physical addresses (ngva->ngpa->gpa->hpa) 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunThe primary challenge is to encode between 1 and 3 translations into hardware 85*4882a593Smuzhiyunthat support only 1 (traditional) and 2 (tdp) translations. When the 86*4882a593Smuzhiyunnumber of required translations matches the hardware, the mmu operates in 87*4882a593Smuzhiyundirect mode; otherwise it operates in shadow mode (see below). 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunMemory 90*4882a593Smuzhiyun====== 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunGuest memory (gpa) is part of the user address space of the process that is 93*4882a593Smuzhiyunusing kvm. Userspace defines the translation between guest addresses and user 94*4882a593Smuzhiyunaddresses (gpa->hva); note that two gpas may alias to the same hva, but not 95*4882a593Smuzhiyunvice versa. 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunThese hvas may be backed using any method available to the host: anonymous 98*4882a593Smuzhiyunmemory, file backed memory, and device memory. Memory might be paged by the 99*4882a593Smuzhiyunhost at any time. 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunEvents 102*4882a593Smuzhiyun====== 103*4882a593Smuzhiyun 104*4882a593SmuzhiyunThe mmu is driven by events, some from the guest, some from the host. 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunGuest generated events: 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun- writes to control registers (especially cr3) 109*4882a593Smuzhiyun- invlpg/invlpga instruction execution 110*4882a593Smuzhiyun- access to missing or protected translations 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunHost generated events: 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun- changes in the gpa->hpa translation (either through gpa->hva changes or 115*4882a593Smuzhiyun through hva->hpa changes) 116*4882a593Smuzhiyun- memory pressure (the shrinker) 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunShadow pages 119*4882a593Smuzhiyun============ 120*4882a593Smuzhiyun 121*4882a593SmuzhiyunThe principal data structure is the shadow page, 'struct kvm_mmu_page'. A 122*4882a593Smuzhiyunshadow page contains 512 sptes, which can be either leaf or nonleaf sptes. A 123*4882a593Smuzhiyunshadow page may contain a mix of leaf and nonleaf sptes. 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunA nonleaf spte allows the hardware mmu to reach the leaf pages and 126*4882a593Smuzhiyunis not related to a translation directly. It points to other shadow pages. 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunA leaf spte corresponds to either one or two translations encoded into 129*4882a593Smuzhiyunone paging structure entry. These are always the lowest level of the 130*4882a593Smuzhiyuntranslation stack, with optional higher level translations left to NPT/EPT. 131*4882a593SmuzhiyunLeaf ptes point at guest pages. 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunThe following table shows translations encoded by leaf ptes, with higher-level 134*4882a593Smuzhiyuntranslations in parentheses: 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun Non-nested guests:: 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun nonpaging: gpa->hpa 139*4882a593Smuzhiyun paging: gva->gpa->hpa 140*4882a593Smuzhiyun paging, tdp: (gva->)gpa->hpa 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun Nested guests:: 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun non-tdp: ngva->gpa->hpa (*) 145*4882a593Smuzhiyun tdp: (ngva->)ngpa->gpa->hpa 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun (*) the guest hypervisor will encode the ngva->gpa translation into its page 148*4882a593Smuzhiyun tables if npt is not present 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunShadow pages contain the following information: 151*4882a593Smuzhiyun role.level: 152*4882a593Smuzhiyun The level in the shadow paging hierarchy that this shadow page belongs to. 153*4882a593Smuzhiyun 1=4k sptes, 2=2M sptes, 3=1G sptes, etc. 154*4882a593Smuzhiyun role.direct: 155*4882a593Smuzhiyun If set, leaf sptes reachable from this page are for a linear range. 156*4882a593Smuzhiyun Examples include real mode translation, large guest pages backed by small 157*4882a593Smuzhiyun host pages, and gpa->hpa translations when NPT or EPT is active. 158*4882a593Smuzhiyun The linear range starts at (gfn << PAGE_SHIFT) and its size is determined 159*4882a593Smuzhiyun by role.level (2MB for first level, 1GB for second level, 0.5TB for third 160*4882a593Smuzhiyun level, 256TB for fourth level) 161*4882a593Smuzhiyun If clear, this page corresponds to a guest page table denoted by the gfn 162*4882a593Smuzhiyun field. 163*4882a593Smuzhiyun role.quadrant: 164*4882a593Smuzhiyun When role.gpte_is_8_bytes=0, the guest uses 32-bit gptes while the host uses 64-bit 165*4882a593Smuzhiyun sptes. That means a guest page table contains more ptes than the host, 166*4882a593Smuzhiyun so multiple shadow pages are needed to shadow one guest page. 167*4882a593Smuzhiyun For first-level shadow pages, role.quadrant can be 0 or 1 and denotes the 168*4882a593Smuzhiyun first or second 512-gpte block in the guest page table. For second-level 169*4882a593Smuzhiyun page tables, each 32-bit gpte is converted to two 64-bit sptes 170*4882a593Smuzhiyun (since each first-level guest page is shadowed by two first-level 171*4882a593Smuzhiyun shadow pages) so role.quadrant takes values in the range 0..3. Each 172*4882a593Smuzhiyun quadrant maps 1GB virtual address space. 173*4882a593Smuzhiyun role.access: 174*4882a593Smuzhiyun Inherited guest access permissions from the parent ptes in the form uwx. 175*4882a593Smuzhiyun Note execute permission is positive, not negative. 176*4882a593Smuzhiyun role.invalid: 177*4882a593Smuzhiyun The page is invalid and should not be used. It is a root page that is 178*4882a593Smuzhiyun currently pinned (by a cpu hardware register pointing to it); once it is 179*4882a593Smuzhiyun unpinned it will be destroyed. 180*4882a593Smuzhiyun role.gpte_is_8_bytes: 181*4882a593Smuzhiyun Reflects the size of the guest PTE for which the page is valid, i.e. '1' 182*4882a593Smuzhiyun if 64-bit gptes are in use, '0' if 32-bit gptes are in use. 183*4882a593Smuzhiyun role.nxe: 184*4882a593Smuzhiyun Contains the value of efer.nxe for which the page is valid. 185*4882a593Smuzhiyun role.cr0_wp: 186*4882a593Smuzhiyun Contains the value of cr0.wp for which the page is valid. 187*4882a593Smuzhiyun role.smep_andnot_wp: 188*4882a593Smuzhiyun Contains the value of cr4.smep && !cr0.wp for which the page is valid 189*4882a593Smuzhiyun (pages for which this is true are different from other pages; see the 190*4882a593Smuzhiyun treatment of cr0.wp=0 below). 191*4882a593Smuzhiyun role.smap_andnot_wp: 192*4882a593Smuzhiyun Contains the value of cr4.smap && !cr0.wp for which the page is valid 193*4882a593Smuzhiyun (pages for which this is true are different from other pages; see the 194*4882a593Smuzhiyun treatment of cr0.wp=0 below). 195*4882a593Smuzhiyun role.ept_sp: 196*4882a593Smuzhiyun This is a virtual flag to denote a shadowed nested EPT page. ept_sp 197*4882a593Smuzhiyun is true if "cr0_wp && smap_andnot_wp", an otherwise invalid combination. 198*4882a593Smuzhiyun role.smm: 199*4882a593Smuzhiyun Is 1 if the page is valid in system management mode. This field 200*4882a593Smuzhiyun determines which of the kvm_memslots array was used to build this 201*4882a593Smuzhiyun shadow page; it is also used to go back from a struct kvm_mmu_page 202*4882a593Smuzhiyun to a memslot, through the kvm_memslots_for_spte_role macro and 203*4882a593Smuzhiyun __gfn_to_memslot. 204*4882a593Smuzhiyun role.ad_disabled: 205*4882a593Smuzhiyun Is 1 if the MMU instance cannot use A/D bits. EPT did not have A/D 206*4882a593Smuzhiyun bits before Haswell; shadow EPT page tables also cannot use A/D bits 207*4882a593Smuzhiyun if the L1 hypervisor does not enable them. 208*4882a593Smuzhiyun gfn: 209*4882a593Smuzhiyun Either the guest page table containing the translations shadowed by this 210*4882a593Smuzhiyun page, or the base page frame for linear translations. See role.direct. 211*4882a593Smuzhiyun spt: 212*4882a593Smuzhiyun A pageful of 64-bit sptes containing the translations for this page. 213*4882a593Smuzhiyun Accessed by both kvm and hardware. 214*4882a593Smuzhiyun The page pointed to by spt will have its page->private pointing back 215*4882a593Smuzhiyun at the shadow page structure. 216*4882a593Smuzhiyun sptes in spt point either at guest pages, or at lower-level shadow pages. 217*4882a593Smuzhiyun Specifically, if sp1 and sp2 are shadow pages, then sp1->spt[n] may point 218*4882a593Smuzhiyun at __pa(sp2->spt). sp2 will point back at sp1 through parent_pte. 219*4882a593Smuzhiyun The spt array forms a DAG structure with the shadow page as a node, and 220*4882a593Smuzhiyun guest pages as leaves. 221*4882a593Smuzhiyun gfns: 222*4882a593Smuzhiyun An array of 512 guest frame numbers, one for each present pte. Used to 223*4882a593Smuzhiyun perform a reverse map from a pte to a gfn. When role.direct is set, any 224*4882a593Smuzhiyun element of this array can be calculated from the gfn field when used, in 225*4882a593Smuzhiyun this case, the array of gfns is not allocated. See role.direct and gfn. 226*4882a593Smuzhiyun root_count: 227*4882a593Smuzhiyun A counter keeping track of how many hardware registers (guest cr3 or 228*4882a593Smuzhiyun pdptrs) are now pointing at the page. While this counter is nonzero, the 229*4882a593Smuzhiyun page cannot be destroyed. See role.invalid. 230*4882a593Smuzhiyun parent_ptes: 231*4882a593Smuzhiyun The reverse mapping for the pte/ptes pointing at this page's spt. If 232*4882a593Smuzhiyun parent_ptes bit 0 is zero, only one spte points at this page and 233*4882a593Smuzhiyun parent_ptes points at this single spte, otherwise, there exists multiple 234*4882a593Smuzhiyun sptes pointing at this page and (parent_ptes & ~0x1) points at a data 235*4882a593Smuzhiyun structure with a list of parent sptes. 236*4882a593Smuzhiyun unsync: 237*4882a593Smuzhiyun If true, then the translations in this page may not match the guest's 238*4882a593Smuzhiyun translation. This is equivalent to the state of the tlb when a pte is 239*4882a593Smuzhiyun changed but before the tlb entry is flushed. Accordingly, unsync ptes 240*4882a593Smuzhiyun are synchronized when the guest executes invlpg or flushes its tlb by 241*4882a593Smuzhiyun other means. Valid for leaf pages. 242*4882a593Smuzhiyun unsync_children: 243*4882a593Smuzhiyun How many sptes in the page point at pages that are unsync (or have 244*4882a593Smuzhiyun unsynchronized children). 245*4882a593Smuzhiyun unsync_child_bitmap: 246*4882a593Smuzhiyun A bitmap indicating which sptes in spt point (directly or indirectly) at 247*4882a593Smuzhiyun pages that may be unsynchronized. Used to quickly locate all unsychronized 248*4882a593Smuzhiyun pages reachable from a given page. 249*4882a593Smuzhiyun clear_spte_count: 250*4882a593Smuzhiyun Only present on 32-bit hosts, where a 64-bit spte cannot be written 251*4882a593Smuzhiyun atomically. The reader uses this while running out of the MMU lock 252*4882a593Smuzhiyun to detect in-progress updates and retry them until the writer has 253*4882a593Smuzhiyun finished the write. 254*4882a593Smuzhiyun write_flooding_count: 255*4882a593Smuzhiyun A guest may write to a page table many times, causing a lot of 256*4882a593Smuzhiyun emulations if the page needs to be write-protected (see "Synchronized 257*4882a593Smuzhiyun and unsynchronized pages" below). Leaf pages can be unsynchronized 258*4882a593Smuzhiyun so that they do not trigger frequent emulation, but this is not 259*4882a593Smuzhiyun possible for non-leafs. This field counts the number of emulations 260*4882a593Smuzhiyun since the last time the page table was actually used; if emulation 261*4882a593Smuzhiyun is triggered too frequently on this page, KVM will unmap the page 262*4882a593Smuzhiyun to avoid emulation in the future. 263*4882a593Smuzhiyun 264*4882a593SmuzhiyunReverse map 265*4882a593Smuzhiyun=========== 266*4882a593Smuzhiyun 267*4882a593SmuzhiyunThe mmu maintains a reverse mapping whereby all ptes mapping a page can be 268*4882a593Smuzhiyunreached given its gfn. This is used, for example, when swapping out a page. 269*4882a593Smuzhiyun 270*4882a593SmuzhiyunSynchronized and unsynchronized pages 271*4882a593Smuzhiyun===================================== 272*4882a593Smuzhiyun 273*4882a593SmuzhiyunThe guest uses two events to synchronize its tlb and page tables: tlb flushes 274*4882a593Smuzhiyunand page invalidations (invlpg). 275*4882a593Smuzhiyun 276*4882a593SmuzhiyunA tlb flush means that we need to synchronize all sptes reachable from the 277*4882a593Smuzhiyunguest's cr3. This is expensive, so we keep all guest page tables write 278*4882a593Smuzhiyunprotected, and synchronize sptes to gptes when a gpte is written. 279*4882a593Smuzhiyun 280*4882a593SmuzhiyunA special case is when a guest page table is reachable from the current 281*4882a593Smuzhiyunguest cr3. In this case, the guest is obliged to issue an invlpg instruction 282*4882a593Smuzhiyunbefore using the translation. We take advantage of that by removing write 283*4882a593Smuzhiyunprotection from the guest page, and allowing the guest to modify it freely. 284*4882a593SmuzhiyunWe synchronize modified gptes when the guest invokes invlpg. This reduces 285*4882a593Smuzhiyunthe amount of emulation we have to do when the guest modifies multiple gptes, 286*4882a593Smuzhiyunor when the a guest page is no longer used as a page table and is used for 287*4882a593Smuzhiyunrandom guest data. 288*4882a593Smuzhiyun 289*4882a593SmuzhiyunAs a side effect we have to resynchronize all reachable unsynchronized shadow 290*4882a593Smuzhiyunpages on a tlb flush. 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun 293*4882a593SmuzhiyunReaction to events 294*4882a593Smuzhiyun================== 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun- guest page fault (or npt page fault, or ept violation) 297*4882a593Smuzhiyun 298*4882a593SmuzhiyunThis is the most complicated event. The cause of a page fault can be: 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun - a true guest fault (the guest translation won't allow the access) (*) 301*4882a593Smuzhiyun - access to a missing translation 302*4882a593Smuzhiyun - access to a protected translation 303*4882a593Smuzhiyun - when logging dirty pages, memory is write protected 304*4882a593Smuzhiyun - synchronized shadow pages are write protected (*) 305*4882a593Smuzhiyun - access to untranslatable memory (mmio) 306*4882a593Smuzhiyun 307*4882a593Smuzhiyun (*) not applicable in direct mode 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunHandling a page fault is performed as follows: 310*4882a593Smuzhiyun 311*4882a593Smuzhiyun - if the RSV bit of the error code is set, the page fault is caused by guest 312*4882a593Smuzhiyun accessing MMIO and cached MMIO information is available. 313*4882a593Smuzhiyun 314*4882a593Smuzhiyun - walk shadow page table 315*4882a593Smuzhiyun - check for valid generation number in the spte (see "Fast invalidation of 316*4882a593Smuzhiyun MMIO sptes" below) 317*4882a593Smuzhiyun - cache the information to vcpu->arch.mmio_gva, vcpu->arch.mmio_access and 318*4882a593Smuzhiyun vcpu->arch.mmio_gfn, and call the emulator 319*4882a593Smuzhiyun 320*4882a593Smuzhiyun - If both P bit and R/W bit of error code are set, this could possibly 321*4882a593Smuzhiyun be handled as a "fast page fault" (fixed without taking the MMU lock). See 322*4882a593Smuzhiyun the description in Documentation/virt/kvm/locking.rst. 323*4882a593Smuzhiyun 324*4882a593Smuzhiyun - if needed, walk the guest page tables to determine the guest translation 325*4882a593Smuzhiyun (gva->gpa or ngpa->gpa) 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun - if permissions are insufficient, reflect the fault back to the guest 328*4882a593Smuzhiyun 329*4882a593Smuzhiyun - determine the host page 330*4882a593Smuzhiyun 331*4882a593Smuzhiyun - if this is an mmio request, there is no host page; cache the info to 332*4882a593Smuzhiyun vcpu->arch.mmio_gva, vcpu->arch.mmio_access and vcpu->arch.mmio_gfn 333*4882a593Smuzhiyun 334*4882a593Smuzhiyun - walk the shadow page table to find the spte for the translation, 335*4882a593Smuzhiyun instantiating missing intermediate page tables as necessary 336*4882a593Smuzhiyun 337*4882a593Smuzhiyun - If this is an mmio request, cache the mmio info to the spte and set some 338*4882a593Smuzhiyun reserved bit on the spte (see callers of kvm_mmu_set_mmio_spte_mask) 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun - try to unsynchronize the page 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun - if successful, we can let the guest continue and modify the gpte 343*4882a593Smuzhiyun 344*4882a593Smuzhiyun - emulate the instruction 345*4882a593Smuzhiyun 346*4882a593Smuzhiyun - if failed, unshadow the page and let the guest continue 347*4882a593Smuzhiyun 348*4882a593Smuzhiyun - update any translations that were modified by the instruction 349*4882a593Smuzhiyun 350*4882a593Smuzhiyuninvlpg handling: 351*4882a593Smuzhiyun 352*4882a593Smuzhiyun - walk the shadow page hierarchy and drop affected translations 353*4882a593Smuzhiyun - try to reinstantiate the indicated translation in the hope that the 354*4882a593Smuzhiyun guest will use it in the near future 355*4882a593Smuzhiyun 356*4882a593SmuzhiyunGuest control register updates: 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun- mov to cr3 359*4882a593Smuzhiyun 360*4882a593Smuzhiyun - look up new shadow roots 361*4882a593Smuzhiyun - synchronize newly reachable shadow pages 362*4882a593Smuzhiyun 363*4882a593Smuzhiyun- mov to cr0/cr4/efer 364*4882a593Smuzhiyun 365*4882a593Smuzhiyun - set up mmu context for new paging mode 366*4882a593Smuzhiyun - look up new shadow roots 367*4882a593Smuzhiyun - synchronize newly reachable shadow pages 368*4882a593Smuzhiyun 369*4882a593SmuzhiyunHost translation updates: 370*4882a593Smuzhiyun 371*4882a593Smuzhiyun - mmu notifier called with updated hva 372*4882a593Smuzhiyun - look up affected sptes through reverse map 373*4882a593Smuzhiyun - drop (or update) translations 374*4882a593Smuzhiyun 375*4882a593SmuzhiyunEmulating cr0.wp 376*4882a593Smuzhiyun================ 377*4882a593Smuzhiyun 378*4882a593SmuzhiyunIf tdp is not enabled, the host must keep cr0.wp=1 so page write protection 379*4882a593Smuzhiyunworks for the guest kernel, not guest guest userspace. When the guest 380*4882a593Smuzhiyuncr0.wp=1, this does not present a problem. However when the guest cr0.wp=0, 381*4882a593Smuzhiyunwe cannot map the permissions for gpte.u=1, gpte.w=0 to any spte (the 382*4882a593Smuzhiyunsemantics require allowing any guest kernel access plus user read access). 383*4882a593Smuzhiyun 384*4882a593SmuzhiyunWe handle this by mapping the permissions to two possible sptes, depending 385*4882a593Smuzhiyunon fault type: 386*4882a593Smuzhiyun 387*4882a593Smuzhiyun- kernel write fault: spte.u=0, spte.w=1 (allows full kernel access, 388*4882a593Smuzhiyun disallows user access) 389*4882a593Smuzhiyun- read fault: spte.u=1, spte.w=0 (allows full read access, disallows kernel 390*4882a593Smuzhiyun write access) 391*4882a593Smuzhiyun 392*4882a593Smuzhiyun(user write faults generate a #PF) 393*4882a593Smuzhiyun 394*4882a593SmuzhiyunIn the first case there are two additional complications: 395*4882a593Smuzhiyun 396*4882a593Smuzhiyun- if CR4.SMEP is enabled: since we've turned the page into a kernel page, 397*4882a593Smuzhiyun the kernel may now execute it. We handle this by also setting spte.nx. 398*4882a593Smuzhiyun If we get a user fetch or read fault, we'll change spte.u=1 and 399*4882a593Smuzhiyun spte.nx=gpte.nx back. For this to work, KVM forces EFER.NX to 1 when 400*4882a593Smuzhiyun shadow paging is in use. 401*4882a593Smuzhiyun- if CR4.SMAP is disabled: since the page has been changed to a kernel 402*4882a593Smuzhiyun page, it can not be reused when CR4.SMAP is enabled. We set 403*4882a593Smuzhiyun CR4.SMAP && !CR0.WP into shadow page's role to avoid this case. Note, 404*4882a593Smuzhiyun here we do not care the case that CR4.SMAP is enabled since KVM will 405*4882a593Smuzhiyun directly inject #PF to guest due to failed permission check. 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunTo prevent an spte that was converted into a kernel page with cr0.wp=0 408*4882a593Smuzhiyunfrom being written by the kernel after cr0.wp has changed to 1, we make 409*4882a593Smuzhiyunthe value of cr0.wp part of the page role. This means that an spte created 410*4882a593Smuzhiyunwith one value of cr0.wp cannot be used when cr0.wp has a different value - 411*4882a593Smuzhiyunit will simply be missed by the shadow page lookup code. A similar issue 412*4882a593Smuzhiyunexists when an spte created with cr0.wp=0 and cr4.smep=0 is used after 413*4882a593Smuzhiyunchanging cr4.smep to 1. To avoid this, the value of !cr0.wp && cr4.smep 414*4882a593Smuzhiyunis also made a part of the page role. 415*4882a593Smuzhiyun 416*4882a593SmuzhiyunLarge pages 417*4882a593Smuzhiyun=========== 418*4882a593Smuzhiyun 419*4882a593SmuzhiyunThe mmu supports all combinations of large and small guest and host pages. 420*4882a593SmuzhiyunSupported page sizes include 4k, 2M, 4M, and 1G. 4M pages are treated as 421*4882a593Smuzhiyuntwo separate 2M pages, on both guest and host, since the mmu always uses PAE 422*4882a593Smuzhiyunpaging. 423*4882a593Smuzhiyun 424*4882a593SmuzhiyunTo instantiate a large spte, four constraints must be satisfied: 425*4882a593Smuzhiyun 426*4882a593Smuzhiyun- the spte must point to a large host page 427*4882a593Smuzhiyun- the guest pte must be a large pte of at least equivalent size (if tdp is 428*4882a593Smuzhiyun enabled, there is no guest pte and this condition is satisfied) 429*4882a593Smuzhiyun- if the spte will be writeable, the large page frame may not overlap any 430*4882a593Smuzhiyun write-protected pages 431*4882a593Smuzhiyun- the guest page must be wholly contained by a single memory slot 432*4882a593Smuzhiyun 433*4882a593SmuzhiyunTo check the last two conditions, the mmu maintains a ->disallow_lpage set of 434*4882a593Smuzhiyunarrays for each memory slot and large page size. Every write protected page 435*4882a593Smuzhiyuncauses its disallow_lpage to be incremented, thus preventing instantiation of 436*4882a593Smuzhiyuna large spte. The frames at the end of an unaligned memory slot have 437*4882a593Smuzhiyunartificially inflated ->disallow_lpages so they can never be instantiated. 438*4882a593Smuzhiyun 439*4882a593SmuzhiyunFast invalidation of MMIO sptes 440*4882a593Smuzhiyun=============================== 441*4882a593Smuzhiyun 442*4882a593SmuzhiyunAs mentioned in "Reaction to events" above, kvm will cache MMIO 443*4882a593Smuzhiyuninformation in leaf sptes. When a new memslot is added or an existing 444*4882a593Smuzhiyunmemslot is changed, this information may become stale and needs to be 445*4882a593Smuzhiyuninvalidated. This also needs to hold the MMU lock while walking all 446*4882a593Smuzhiyunshadow pages, and is made more scalable with a similar technique. 447*4882a593Smuzhiyun 448*4882a593SmuzhiyunMMIO sptes have a few spare bits, which are used to store a 449*4882a593Smuzhiyungeneration number. The global generation number is stored in 450*4882a593Smuzhiyunkvm_memslots(kvm)->generation, and increased whenever guest memory info 451*4882a593Smuzhiyunchanges. 452*4882a593Smuzhiyun 453*4882a593SmuzhiyunWhen KVM finds an MMIO spte, it checks the generation number of the spte. 454*4882a593SmuzhiyunIf the generation number of the spte does not equal the global generation 455*4882a593Smuzhiyunnumber, it will ignore the cached MMIO information and handle the page 456*4882a593Smuzhiyunfault through the slow path. 457*4882a593Smuzhiyun 458*4882a593SmuzhiyunSince only 18 bits are used to store generation-number on mmio spte, all 459*4882a593Smuzhiyunpages are zapped when there is an overflow. 460*4882a593Smuzhiyun 461*4882a593SmuzhiyunUnfortunately, a single memory access might access kvm_memslots(kvm) multiple 462*4882a593Smuzhiyuntimes, the last one happening when the generation number is retrieved and 463*4882a593Smuzhiyunstored into the MMIO spte. Thus, the MMIO spte might be created based on 464*4882a593Smuzhiyunout-of-date information, but with an up-to-date generation number. 465*4882a593Smuzhiyun 466*4882a593SmuzhiyunTo avoid this, the generation number is incremented again after synchronize_srcu 467*4882a593Smuzhiyunreturns; thus, bit 63 of kvm_memslots(kvm)->generation set to 1 only during a 468*4882a593Smuzhiyunmemslot update, while some SRCU readers might be using the old copy. We do not 469*4882a593Smuzhiyunwant to use an MMIO sptes created with an odd generation number, and we can do 470*4882a593Smuzhiyunthis without losing a bit in the MMIO spte. The "update in-progress" bit of the 471*4882a593Smuzhiyungeneration is not stored in MMIO spte, and is so is implicitly zero when the 472*4882a593Smuzhiyungeneration is extracted out of the spte. If KVM is unlucky and creates an MMIO 473*4882a593Smuzhiyunspte while an update is in-progress, the next access to the spte will always be 474*4882a593Smuzhiyuna cache miss. For example, a subsequent access during the update window will 475*4882a593Smuzhiyunmiss due to the in-progress flag diverging, while an access after the update 476*4882a593Smuzhiyunwindow closes will have a higher generation number (as compared to the spte). 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun 479*4882a593SmuzhiyunFurther reading 480*4882a593Smuzhiyun=============== 481*4882a593Smuzhiyun 482*4882a593Smuzhiyun- NPT presentation from KVM Forum 2008 483*4882a593Smuzhiyun https://www.linux-kvm.org/images/c/c8/KvmForum2008%24kdf2008_21.pdf 484