Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * MMU support |
| 8 | * |
| 9 | * Copyright (C) 2006 Qumranet, Inc. |
| 10 | * |
| 11 | * Authors: |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * |
| 15 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 16 | * the COPYING file in the top-level directory. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * We need the mmu code to access both 32-bit and 64-bit guest ptes, |
| 22 | * so the code in this file is compiled twice, once per pte size. |
| 23 | */ |
| 24 | |
| 25 | #if PTTYPE == 64 |
| 26 | #define pt_element_t u64 |
| 27 | #define guest_walker guest_walker64 |
| 28 | #define FNAME(name) paging##64_##name |
| 29 | #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK |
| 30 | #define PT_DIR_BASE_ADDR_MASK PT64_DIR_BASE_ADDR_MASK |
| 31 | #define PT_INDEX(addr, level) PT64_INDEX(addr, level) |
| 32 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
| 33 | #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level) |
| 34 | #define PT_PTE_COPY_MASK PT64_PTE_COPY_MASK |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 35 | #ifdef CONFIG_X86_64 |
| 36 | #define PT_MAX_FULL_LEVELS 4 |
| 37 | #else |
| 38 | #define PT_MAX_FULL_LEVELS 2 |
| 39 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 40 | #elif PTTYPE == 32 |
| 41 | #define pt_element_t u32 |
| 42 | #define guest_walker guest_walker32 |
| 43 | #define FNAME(name) paging##32_##name |
| 44 | #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK |
| 45 | #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK |
| 46 | #define PT_INDEX(addr, level) PT32_INDEX(addr, level) |
| 47 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
| 48 | #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level) |
| 49 | #define PT_PTE_COPY_MASK PT32_PTE_COPY_MASK |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 50 | #define PT_MAX_FULL_LEVELS 2 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 51 | #else |
| 52 | #error Invalid PTTYPE value |
| 53 | #endif |
| 54 | |
| 55 | /* |
| 56 | * The guest_walker structure emulates the behavior of the hardware page |
| 57 | * table walker. |
| 58 | */ |
| 59 | struct guest_walker { |
| 60 | int level; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 61 | gfn_t table_gfn[PT_MAX_FULL_LEVELS]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 62 | pt_element_t *table; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 63 | pt_element_t *ptep; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 64 | pt_element_t inherited_ar; |
| 65 | }; |
| 66 | |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 67 | /* |
| 68 | * Fetch a guest pte for a guest virtual address |
| 69 | */ |
| 70 | static void FNAME(walk_addr)(struct guest_walker *walker, |
| 71 | struct kvm_vcpu *vcpu, gva_t addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | { |
| 73 | hpa_t hpa; |
| 74 | struct kvm_memory_slot *slot; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 75 | pt_element_t *ptep; |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 76 | pt_element_t root; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 77 | gfn_t table_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 78 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 79 | pgprintk("%s: addr %lx\n", __FUNCTION__, addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 80 | walker->level = vcpu->mmu.root_level; |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 81 | walker->table = NULL; |
| 82 | root = vcpu->cr3; |
| 83 | #if PTTYPE == 64 |
| 84 | if (!is_long_mode(vcpu)) { |
| 85 | walker->ptep = &vcpu->pdptrs[(addr >> 30) & 3]; |
| 86 | root = *walker->ptep; |
| 87 | if (!(root & PT_PRESENT_MASK)) |
| 88 | return; |
| 89 | --walker->level; |
| 90 | } |
| 91 | #endif |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 92 | table_gfn = (root & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
| 93 | walker->table_gfn[walker->level - 1] = table_gfn; |
| 94 | pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__, |
| 95 | walker->level - 1, table_gfn); |
| 96 | slot = gfn_to_memslot(vcpu->kvm, table_gfn); |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 97 | hpa = safe_gpa_to_hpa(vcpu, root & PT64_BASE_ADDR_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 98 | walker->table = kmap_atomic(pfn_to_page(hpa >> PAGE_SHIFT), KM_USER0); |
| 99 | |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 100 | ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) || |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 101 | (vcpu->cr3 & ~(PAGE_MASK | CR3_FLAGS_MASK)) == 0); |
| 102 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 103 | walker->inherited_ar = PT_USER_MASK | PT_WRITABLE_MASK; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 104 | |
| 105 | for (;;) { |
| 106 | int index = PT_INDEX(addr, walker->level); |
| 107 | hpa_t paddr; |
| 108 | |
| 109 | ptep = &walker->table[index]; |
| 110 | ASSERT(((unsigned long)walker->table & PAGE_MASK) == |
| 111 | ((unsigned long)ptep & PAGE_MASK)); |
| 112 | |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 113 | if (is_present_pte(*ptep) && !(*ptep & PT_ACCESSED_MASK)) |
| 114 | *ptep |= PT_ACCESSED_MASK; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 115 | |
| 116 | if (!is_present_pte(*ptep) || |
| 117 | walker->level == PT_PAGE_TABLE_LEVEL || |
| 118 | (walker->level == PT_DIRECTORY_LEVEL && |
| 119 | (*ptep & PT_PAGE_SIZE_MASK) && |
| 120 | (PTTYPE == 64 || is_pse(vcpu)))) |
| 121 | break; |
| 122 | |
| 123 | if (walker->level != 3 || is_long_mode(vcpu)) |
| 124 | walker->inherited_ar &= walker->table[index]; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 125 | table_gfn = (*ptep & PT_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 126 | paddr = safe_gpa_to_hpa(vcpu, *ptep & PT_BASE_ADDR_MASK); |
| 127 | kunmap_atomic(walker->table, KM_USER0); |
| 128 | walker->table = kmap_atomic(pfn_to_page(paddr >> PAGE_SHIFT), |
| 129 | KM_USER0); |
| 130 | --walker->level; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 131 | walker->table_gfn[walker->level - 1 ] = table_gfn; |
| 132 | pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__, |
| 133 | walker->level - 1, table_gfn); |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 134 | } |
| 135 | walker->ptep = ptep; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void FNAME(release_walker)(struct guest_walker *walker) |
| 139 | { |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 140 | if (walker->table) |
| 141 | kunmap_atomic(walker->table, KM_USER0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void FNAME(set_pte)(struct kvm_vcpu *vcpu, u64 guest_pte, |
| 145 | u64 *shadow_pte, u64 access_bits) |
| 146 | { |
| 147 | ASSERT(*shadow_pte == 0); |
| 148 | access_bits &= guest_pte; |
| 149 | *shadow_pte = (guest_pte & PT_PTE_COPY_MASK); |
| 150 | set_pte_common(vcpu, shadow_pte, guest_pte & PT_BASE_ADDR_MASK, |
| 151 | guest_pte & PT_DIRTY_MASK, access_bits); |
| 152 | } |
| 153 | |
| 154 | static void FNAME(set_pde)(struct kvm_vcpu *vcpu, u64 guest_pde, |
| 155 | u64 *shadow_pte, u64 access_bits, |
| 156 | int index) |
| 157 | { |
| 158 | gpa_t gaddr; |
| 159 | |
| 160 | ASSERT(*shadow_pte == 0); |
| 161 | access_bits &= guest_pde; |
| 162 | gaddr = (guest_pde & PT_DIR_BASE_ADDR_MASK) + PAGE_SIZE * index; |
| 163 | if (PTTYPE == 32 && is_cpuid_PSE36()) |
| 164 | gaddr |= (guest_pde & PT32_DIR_PSE36_MASK) << |
| 165 | (32 - PT32_DIR_PSE36_SHIFT); |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 166 | *shadow_pte = guest_pde & PT_PTE_COPY_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 167 | set_pte_common(vcpu, shadow_pte, gaddr, |
| 168 | guest_pde & PT_DIRTY_MASK, access_bits); |
| 169 | } |
| 170 | |
| 171 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 172 | * Fetch a shadow pte for a specific level in the paging hierarchy. |
| 173 | */ |
| 174 | static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, |
| 175 | struct guest_walker *walker) |
| 176 | { |
| 177 | hpa_t shadow_addr; |
| 178 | int level; |
| 179 | u64 *prev_shadow_ent = NULL; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 180 | pt_element_t *guest_ent = walker->ptep; |
| 181 | |
| 182 | if (!is_present_pte(*guest_ent)) |
| 183 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 184 | |
| 185 | shadow_addr = vcpu->mmu.root_hpa; |
| 186 | level = vcpu->mmu.shadow_root_level; |
Avi Kivity | aef3d3f | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 187 | if (level == PT32E_ROOT_LEVEL) { |
| 188 | shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3]; |
| 189 | shadow_addr &= PT64_BASE_ADDR_MASK; |
| 190 | --level; |
| 191 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 192 | |
| 193 | for (; ; level--) { |
| 194 | u32 index = SHADOW_PT_INDEX(addr, level); |
| 195 | u64 *shadow_ent = ((u64 *)__va(shadow_addr)) + index; |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 196 | struct kvm_mmu_page *shadow_page; |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 197 | u64 shadow_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 198 | int metaphysical; |
| 199 | gfn_t table_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 200 | |
| 201 | if (is_present_pte(*shadow_ent) || is_io_pte(*shadow_ent)) { |
| 202 | if (level == PT_PAGE_TABLE_LEVEL) |
| 203 | return shadow_ent; |
| 204 | shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK; |
| 205 | prev_shadow_ent = shadow_ent; |
| 206 | continue; |
| 207 | } |
| 208 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 209 | if (level == PT_PAGE_TABLE_LEVEL) { |
| 210 | |
| 211 | if (walker->level == PT_DIRECTORY_LEVEL) { |
| 212 | if (prev_shadow_ent) |
| 213 | *prev_shadow_ent |= PT_SHADOW_PS_MARK; |
| 214 | FNAME(set_pde)(vcpu, *guest_ent, shadow_ent, |
| 215 | walker->inherited_ar, |
| 216 | PT_INDEX(addr, PT_PAGE_TABLE_LEVEL)); |
| 217 | } else { |
| 218 | ASSERT(walker->level == PT_PAGE_TABLE_LEVEL); |
| 219 | FNAME(set_pte)(vcpu, *guest_ent, shadow_ent, walker->inherited_ar); |
| 220 | } |
| 221 | return shadow_ent; |
| 222 | } |
| 223 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 224 | if (level - 1 == PT_PAGE_TABLE_LEVEL |
| 225 | && walker->level == PT_DIRECTORY_LEVEL) { |
| 226 | metaphysical = 1; |
| 227 | table_gfn = (*guest_ent & PT_BASE_ADDR_MASK) |
| 228 | >> PAGE_SHIFT; |
| 229 | } else { |
| 230 | metaphysical = 0; |
| 231 | table_gfn = walker->table_gfn[level - 2]; |
| 232 | } |
| 233 | shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1, |
| 234 | metaphysical, shadow_ent); |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 235 | if (!shadow_page) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 236 | return ERR_PTR(-ENOMEM); |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 237 | shadow_addr = shadow_page->page_hpa; |
Avi Kivity | aef3d3f | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 238 | shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK |
| 239 | | PT_WRITABLE_MASK | PT_USER_MASK; |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 240 | *shadow_ent = shadow_pte; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 241 | prev_shadow_ent = shadow_ent; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * The guest faulted for write. We need to |
| 247 | * |
| 248 | * - check write permissions |
| 249 | * - update the guest pte dirty bit |
| 250 | * - update our own dirty page tracking structures |
| 251 | */ |
| 252 | static int FNAME(fix_write_pf)(struct kvm_vcpu *vcpu, |
| 253 | u64 *shadow_ent, |
| 254 | struct guest_walker *walker, |
| 255 | gva_t addr, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 256 | int user, |
| 257 | int *write_pt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 258 | { |
| 259 | pt_element_t *guest_ent; |
| 260 | int writable_shadow; |
| 261 | gfn_t gfn; |
| 262 | |
| 263 | if (is_writeble_pte(*shadow_ent)) |
| 264 | return 0; |
| 265 | |
| 266 | writable_shadow = *shadow_ent & PT_SHADOW_WRITABLE_MASK; |
| 267 | if (user) { |
| 268 | /* |
| 269 | * User mode access. Fail if it's a kernel page or a read-only |
| 270 | * page. |
| 271 | */ |
| 272 | if (!(*shadow_ent & PT_SHADOW_USER_MASK) || !writable_shadow) |
| 273 | return 0; |
| 274 | ASSERT(*shadow_ent & PT_USER_MASK); |
| 275 | } else |
| 276 | /* |
| 277 | * Kernel mode access. Fail if it's a read-only page and |
| 278 | * supervisor write protection is enabled. |
| 279 | */ |
| 280 | if (!writable_shadow) { |
| 281 | if (is_write_protection(vcpu)) |
| 282 | return 0; |
| 283 | *shadow_ent &= ~PT_USER_MASK; |
| 284 | } |
| 285 | |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 286 | guest_ent = walker->ptep; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 287 | |
| 288 | if (!is_present_pte(*guest_ent)) { |
| 289 | *shadow_ent = 0; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | gfn = (*guest_ent & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 294 | if (kvm_mmu_lookup_page(vcpu, gfn)) { |
| 295 | pgprintk("%s: found shadow page for %lx, marking ro\n", |
| 296 | __FUNCTION__, gfn); |
| 297 | *write_pt = 1; |
| 298 | return 0; |
| 299 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 300 | mark_page_dirty(vcpu->kvm, gfn); |
| 301 | *shadow_ent |= PT_WRITABLE_MASK; |
| 302 | *guest_ent |= PT_DIRTY_MASK; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 303 | rmap_add(vcpu->kvm, shadow_ent); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 304 | |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Page fault handler. There are several causes for a page fault: |
| 310 | * - there is no shadow pte for the guest pte |
| 311 | * - write access through a shadow pte marked read only so that we can set |
| 312 | * the dirty bit |
| 313 | * - write access to a shadow pte marked read only so we can update the page |
| 314 | * dirty bitmap, when userspace requests it |
| 315 | * - mmio access; in this case we will never install a present shadow pte |
| 316 | * - normal guest page fault due to the guest pte marked not present, not |
| 317 | * writable, or not executable |
| 318 | * |
| 319 | * Returns: 1 if we need to emulate the instruction, 0 otherwise |
| 320 | */ |
| 321 | static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, |
| 322 | u32 error_code) |
| 323 | { |
| 324 | int write_fault = error_code & PFERR_WRITE_MASK; |
| 325 | int pte_present = error_code & PFERR_PRESENT_MASK; |
| 326 | int user_fault = error_code & PFERR_USER_MASK; |
| 327 | struct guest_walker walker; |
| 328 | u64 *shadow_pte; |
| 329 | int fixed; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 330 | int write_pt = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 331 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 332 | pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 333 | /* |
| 334 | * Look up the shadow pte for the faulting address. |
| 335 | */ |
| 336 | for (;;) { |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 337 | FNAME(walk_addr)(&walker, vcpu, addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 338 | shadow_pte = FNAME(fetch)(vcpu, addr, &walker); |
| 339 | if (IS_ERR(shadow_pte)) { /* must be -ENOMEM */ |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 340 | printk("%s: oom\n", __FUNCTION__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 341 | nonpaging_flush(vcpu); |
| 342 | FNAME(release_walker)(&walker); |
| 343 | continue; |
| 344 | } |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * The page is not mapped by the guest. Let the guest handle it. |
| 350 | */ |
| 351 | if (!shadow_pte) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 352 | pgprintk("%s: not mapped\n", __FUNCTION__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 353 | inject_page_fault(vcpu, addr, error_code); |
| 354 | FNAME(release_walker)(&walker); |
| 355 | return 0; |
| 356 | } |
| 357 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 358 | pgprintk("%s: shadow pte %p %llx\n", __FUNCTION__, |
| 359 | shadow_pte, *shadow_pte); |
| 360 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 361 | /* |
| 362 | * Update the shadow pte. |
| 363 | */ |
| 364 | if (write_fault) |
| 365 | fixed = FNAME(fix_write_pf)(vcpu, shadow_pte, &walker, addr, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 366 | user_fault, &write_pt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 367 | else |
| 368 | fixed = fix_read_pf(shadow_pte); |
| 369 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 370 | pgprintk("%s: updated shadow pte %p %llx\n", __FUNCTION__, |
| 371 | shadow_pte, *shadow_pte); |
| 372 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 373 | FNAME(release_walker)(&walker); |
| 374 | |
| 375 | /* |
| 376 | * mmio: emulate if accessible, otherwise its a guest fault. |
| 377 | */ |
| 378 | if (is_io_pte(*shadow_pte)) { |
| 379 | if (may_access(*shadow_pte, write_fault, user_fault)) |
| 380 | return 1; |
| 381 | pgprintk("%s: io work, no access\n", __FUNCTION__); |
| 382 | inject_page_fault(vcpu, addr, |
| 383 | error_code | PFERR_PRESENT_MASK); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * pte not present, guest page fault. |
| 389 | */ |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 390 | if (pte_present && !fixed && !write_pt) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 391 | inject_page_fault(vcpu, addr, error_code); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | ++kvm_stat.pf_fixed; |
| 396 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 397 | return write_pt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr) |
| 401 | { |
| 402 | struct guest_walker walker; |
| 403 | pt_element_t guest_pte; |
| 404 | gpa_t gpa; |
| 405 | |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 406 | FNAME(walk_addr)(&walker, vcpu, vaddr); |
| 407 | guest_pte = *walker.ptep; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 408 | FNAME(release_walker)(&walker); |
| 409 | |
| 410 | if (!is_present_pte(guest_pte)) |
| 411 | return UNMAPPED_GVA; |
| 412 | |
| 413 | if (walker.level == PT_DIRECTORY_LEVEL) { |
| 414 | ASSERT((guest_pte & PT_PAGE_SIZE_MASK)); |
| 415 | ASSERT(PTTYPE == 64 || is_pse(vcpu)); |
| 416 | |
| 417 | gpa = (guest_pte & PT_DIR_BASE_ADDR_MASK) | (vaddr & |
| 418 | (PT_LEVEL_MASK(PT_PAGE_TABLE_LEVEL) | ~PAGE_MASK)); |
| 419 | |
| 420 | if (PTTYPE == 32 && is_cpuid_PSE36()) |
| 421 | gpa |= (guest_pte & PT32_DIR_PSE36_MASK) << |
| 422 | (32 - PT32_DIR_PSE36_SHIFT); |
| 423 | } else { |
| 424 | gpa = (guest_pte & PT_BASE_ADDR_MASK); |
| 425 | gpa |= (vaddr & ~PAGE_MASK); |
| 426 | } |
| 427 | |
| 428 | return gpa; |
| 429 | } |
| 430 | |
| 431 | #undef pt_element_t |
| 432 | #undef guest_walker |
| 433 | #undef FNAME |
| 434 | #undef PT_BASE_ADDR_MASK |
| 435 | #undef PT_INDEX |
| 436 | #undef SHADOW_PT_INDEX |
| 437 | #undef PT_LEVEL_MASK |
| 438 | #undef PT_PTE_COPY_MASK |
| 439 | #undef PT_NON_PTE_COPY_MASK |
| 440 | #undef PT_DIR_BASE_ADDR_MASK |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame^] | 441 | #undef PT_MAX_FULL_LEVELS |