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) |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 34 | #define PT_LEVEL_BITS PT64_LEVEL_BITS |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 35 | #ifdef CONFIG_X86_64 |
| 36 | #define PT_MAX_FULL_LEVELS 4 |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 37 | #define CMPXCHG cmpxchg |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 38 | #else |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 39 | #define CMPXCHG cmpxchg64 |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 40 | #define PT_MAX_FULL_LEVELS 2 |
| 41 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | #elif PTTYPE == 32 |
| 43 | #define pt_element_t u32 |
| 44 | #define guest_walker guest_walker32 |
| 45 | #define FNAME(name) paging##32_##name |
| 46 | #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK |
| 47 | #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK |
| 48 | #define PT_INDEX(addr, level) PT32_INDEX(addr, level) |
| 49 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
| 50 | #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level) |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 51 | #define PT_LEVEL_BITS PT32_LEVEL_BITS |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 52 | #define PT_MAX_FULL_LEVELS 2 |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 53 | #define CMPXCHG cmpxchg |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 54 | #else |
| 55 | #error Invalid PTTYPE value |
| 56 | #endif |
| 57 | |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 58 | #define gpte_to_gfn FNAME(gpte_to_gfn) |
| 59 | #define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde) |
| 60 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 61 | /* |
| 62 | * The guest_walker structure emulates the behavior of the hardware page |
| 63 | * table walker. |
| 64 | */ |
| 65 | struct guest_walker { |
| 66 | int level; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 67 | gfn_t table_gfn[PT_MAX_FULL_LEVELS]; |
Shaohua Li | fe551881 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 68 | pt_element_t pte; |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 69 | unsigned pt_access; |
| 70 | unsigned pte_access; |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 71 | gfn_t gfn; |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 72 | u32 error_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 75 | static gfn_t gpte_to_gfn(pt_element_t gpte) |
| 76 | { |
| 77 | return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT; |
| 78 | } |
| 79 | |
| 80 | static gfn_t gpte_to_gfn_pde(pt_element_t gpte) |
| 81 | { |
| 82 | return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT; |
| 83 | } |
| 84 | |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 85 | static bool FNAME(cmpxchg_gpte)(struct kvm *kvm, |
| 86 | gfn_t table_gfn, unsigned index, |
| 87 | pt_element_t orig_pte, pt_element_t new_pte) |
| 88 | { |
| 89 | pt_element_t ret; |
| 90 | pt_element_t *table; |
| 91 | struct page *page; |
| 92 | |
| 93 | page = gfn_to_page(kvm, table_gfn); |
| 94 | table = kmap_atomic(page, KM_USER0); |
| 95 | |
| 96 | ret = CMPXCHG(&table[index], orig_pte, new_pte); |
| 97 | |
| 98 | kunmap_atomic(table, KM_USER0); |
| 99 | |
| 100 | kvm_release_page_dirty(page); |
| 101 | |
| 102 | return (ret != orig_pte); |
| 103 | } |
| 104 | |
Avi Kivity | bedbe4e | 2007-12-09 16:52:56 +0200 | [diff] [blame] | 105 | static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte) |
| 106 | { |
| 107 | unsigned access; |
| 108 | |
| 109 | access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK; |
| 110 | #if PTTYPE == 64 |
| 111 | if (is_nx(vcpu)) |
| 112 | access &= ~(gpte >> PT64_NX_SHIFT); |
| 113 | #endif |
| 114 | return access; |
| 115 | } |
| 116 | |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 117 | /* |
| 118 | * Fetch a guest pte for a guest virtual address |
| 119 | */ |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 120 | static int FNAME(walk_addr)(struct guest_walker *walker, |
| 121 | struct kvm_vcpu *vcpu, gva_t addr, |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 122 | int write_fault, int user_fault, int fetch_fault) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 123 | { |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 124 | pt_element_t pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 125 | gfn_t table_gfn; |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 126 | unsigned index, pt_access, pte_access; |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 127 | gpa_t pte_gpa; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 128 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 129 | pgprintk("%s: addr %lx\n", __FUNCTION__, addr); |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 130 | walk: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 131 | walker->level = vcpu->mmu.root_level; |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 132 | pte = vcpu->cr3; |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 133 | #if PTTYPE == 64 |
| 134 | if (!is_long_mode(vcpu)) { |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 135 | pte = vcpu->pdptrs[(addr >> 30) & 3]; |
| 136 | if (!is_present_pte(pte)) |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 137 | goto not_present; |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 138 | --walker->level; |
| 139 | } |
| 140 | #endif |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 141 | ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) || |
Rusty Russell | f802a30 | 2007-07-17 23:32:55 +1000 | [diff] [blame] | 142 | (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 143 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 144 | pt_access = ACC_ALL; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 145 | |
| 146 | for (;;) { |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 147 | index = PT_INDEX(addr, walker->level); |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 148 | |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 149 | table_gfn = gpte_to_gfn(pte); |
Avi Kivity | 1755fbc | 2007-11-21 14:44:45 +0200 | [diff] [blame] | 150 | pte_gpa = gfn_to_gpa(table_gfn); |
Izik Eidus | ec8d4ea | 2007-11-19 11:28:19 +0200 | [diff] [blame] | 151 | pte_gpa += index * sizeof(pt_element_t); |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 152 | walker->table_gfn[walker->level - 1] = table_gfn; |
| 153 | pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__, |
| 154 | walker->level - 1, table_gfn); |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 155 | |
Izik Eidus | ec8d4ea | 2007-11-19 11:28:19 +0200 | [diff] [blame] | 156 | kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte)); |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 157 | |
| 158 | if (!is_present_pte(pte)) |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 159 | goto not_present; |
| 160 | |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 161 | if (write_fault && !is_writeble_pte(pte)) |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 162 | if (user_fault || is_write_protection(vcpu)) |
| 163 | goto access_error; |
| 164 | |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 165 | if (user_fault && !(pte & PT_USER_MASK)) |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 166 | goto access_error; |
| 167 | |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 168 | #if PTTYPE == 64 |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 169 | if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK)) |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 170 | goto access_error; |
| 171 | #endif |
| 172 | |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 173 | if (!(pte & PT_ACCESSED_MASK)) { |
Avi Kivity | bf3f8e8 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 174 | mark_page_dirty(vcpu->kvm, table_gfn); |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 175 | if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, |
| 176 | index, pte, pte|PT_ACCESSED_MASK)) |
| 177 | goto walk; |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 178 | pte |= PT_ACCESSED_MASK; |
Avi Kivity | bf3f8e8 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 179 | } |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 180 | |
Avi Kivity | bedbe4e | 2007-12-09 16:52:56 +0200 | [diff] [blame] | 181 | pte_access = pt_access & FNAME(gpte_access)(vcpu, pte); |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 182 | |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 183 | if (walker->level == PT_PAGE_TABLE_LEVEL) { |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 184 | walker->gfn = gpte_to_gfn(pte); |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | |
| 188 | if (walker->level == PT_DIRECTORY_LEVEL |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 189 | && (pte & PT_PAGE_SIZE_MASK) |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 190 | && (PTTYPE == 64 || is_pse(vcpu))) { |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 191 | walker->gfn = gpte_to_gfn_pde(pte); |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 192 | walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL); |
Avi Kivity | da928521 | 2007-11-21 13:54:47 +0200 | [diff] [blame] | 193 | if (PTTYPE == 32 && is_cpuid_PSE36()) |
| 194 | walker->gfn += pse36_gfn_delta(pte); |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 198 | pt_access = pte_access; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 199 | --walker->level; |
| 200 | } |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 201 | |
| 202 | if (write_fault && !is_dirty_pte(pte)) { |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 203 | bool ret; |
| 204 | |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 205 | mark_page_dirty(vcpu->kvm, table_gfn); |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 206 | ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte, |
| 207 | pte|PT_DIRTY_MASK); |
| 208 | if (ret) |
| 209 | goto walk; |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 210 | pte |= PT_DIRTY_MASK; |
Avi Kivity | 42bf3f0 | 2007-10-17 12:18:47 +0200 | [diff] [blame] | 211 | kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte)); |
| 212 | } |
| 213 | |
| 214 | walker->pte = pte; |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 215 | walker->pt_access = pt_access; |
| 216 | walker->pte_access = pte_access; |
| 217 | pgprintk("%s: pte %llx pte_access %x pt_access %x\n", |
| 218 | __FUNCTION__, (u64)pte, pt_access, pte_access); |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 219 | return 1; |
| 220 | |
| 221 | not_present: |
| 222 | walker->error_code = 0; |
| 223 | goto err; |
| 224 | |
| 225 | access_error: |
| 226 | walker->error_code = PFERR_PRESENT_MASK; |
| 227 | |
| 228 | err: |
| 229 | if (write_fault) |
| 230 | walker->error_code |= PFERR_WRITE_MASK; |
| 231 | if (user_fault) |
| 232 | walker->error_code |= PFERR_USER_MASK; |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 233 | if (fetch_fault) |
| 234 | walker->error_code |= PFERR_FETCH_MASK; |
Shaohua Li | fe551881 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 235 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Avi Kivity | 230c9a8 | 2007-11-21 14:16:30 +0200 | [diff] [blame] | 238 | static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t gpte, |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 239 | u64 *shadow_pte, unsigned pt_access, |
| 240 | unsigned pte_access, |
Avi Kivity | 230c9a8 | 2007-11-21 14:16:30 +0200 | [diff] [blame] | 241 | int user_fault, int write_fault, |
| 242 | int *ptwrite, struct guest_walker *walker, |
| 243 | gfn_t gfn) |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 244 | { |
Shaohua Li | fe551881 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 245 | int dirty = gpte & PT_DIRTY_MASK; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 246 | u64 spte; |
| 247 | int was_rmapped = is_rmap_pte(*shadow_pte); |
Izik Eidus | b238f7b | 2007-11-20 12:02:12 +0200 | [diff] [blame] | 248 | struct page *page; |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 249 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 250 | pgprintk("%s: spte %llx gpte %llx access %x write_fault %d" |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 251 | " user_fault %d gfn %lx\n", |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 252 | __FUNCTION__, *shadow_pte, (u64)gpte, pt_access, |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 253 | write_fault, user_fault, gfn); |
| 254 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 255 | /* |
| 256 | * We don't set the accessed bit, since we sometimes want to see |
| 257 | * whether the guest actually used the pte (in order to detect |
| 258 | * demand paging). |
| 259 | */ |
| 260 | spte = PT_PRESENT_MASK | PT_DIRTY_MASK; |
Shaohua Li | fe551881 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 261 | spte |= gpte & PT64_NX_MASK; |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 262 | if (!dirty) |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 263 | pte_access &= ~ACC_WRITE_MASK; |
Avi Kivity | 8d87a03 | 2007-12-09 16:37:36 +0200 | [diff] [blame] | 264 | if (!(pte_access & ACC_EXEC_MASK)) |
| 265 | spte |= PT64_NX_MASK; |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 266 | |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 267 | page = gfn_to_page(vcpu->kvm, gfn); |
Izik Eidus | b238f7b | 2007-11-20 12:02:12 +0200 | [diff] [blame] | 268 | |
Avi Kivity | 0d551bb | 2007-05-31 15:23:35 +0300 | [diff] [blame] | 269 | spte |= PT_PRESENT_MASK; |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 270 | if (pte_access & ACC_USER_MASK) |
Avi Kivity | 0d551bb | 2007-05-31 15:23:35 +0300 | [diff] [blame] | 271 | spte |= PT_USER_MASK; |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 272 | |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 273 | if (is_error_page(page)) { |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 274 | set_shadow_pte(shadow_pte, |
| 275 | shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK); |
Izik Eidus | b238f7b | 2007-11-20 12:02:12 +0200 | [diff] [blame] | 276 | kvm_release_page_clean(page); |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 280 | spte |= page_to_phys(page); |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 281 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 282 | if ((pte_access & ACC_WRITE_MASK) |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 283 | || (write_fault && !is_write_protection(vcpu) && !user_fault)) { |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 284 | struct kvm_mmu_page *shadow; |
| 285 | |
Avi Kivity | 0d551bb | 2007-05-31 15:23:35 +0300 | [diff] [blame] | 286 | spte |= PT_WRITABLE_MASK; |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 287 | if (user_fault) { |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 288 | mmu_unshadow(vcpu->kvm, gfn); |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 289 | goto unshadowed; |
| 290 | } |
| 291 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 292 | shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn); |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 293 | if (shadow) { |
| 294 | pgprintk("%s: found shadow page for %lx, marking ro\n", |
| 295 | __FUNCTION__, gfn); |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 296 | pte_access &= ~ACC_WRITE_MASK; |
Avi Kivity | 0d551bb | 2007-05-31 15:23:35 +0300 | [diff] [blame] | 297 | if (is_writeble_pte(spte)) { |
| 298 | spte &= ~PT_WRITABLE_MASK; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 299 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 300 | } |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 301 | if (write_fault) |
| 302 | *ptwrite = 1; |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 306 | unshadowed: |
| 307 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 308 | if (pte_access & ACC_WRITE_MASK) |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 309 | mark_page_dirty(vcpu->kvm, gfn); |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 310 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 311 | pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte); |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 312 | set_shadow_pte(shadow_pte, spte); |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 313 | page_header_update_slot(vcpu->kvm, shadow_pte, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 314 | if (!was_rmapped) { |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 315 | rmap_add(vcpu, shadow_pte, gfn); |
Izik Eidus | b238f7b | 2007-11-20 12:02:12 +0200 | [diff] [blame] | 316 | if (!is_rmap_pte(*shadow_pte)) |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 317 | kvm_release_page_clean(page); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 318 | } |
| 319 | else |
Izik Eidus | b238f7b | 2007-11-20 12:02:12 +0200 | [diff] [blame] | 320 | kvm_release_page_clean(page); |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 321 | if (!ptwrite || !*ptwrite) |
| 322 | vcpu->last_pte_updated = shadow_pte; |
Avi Kivity | e60d75e | 2007-05-30 19:31:17 +0300 | [diff] [blame] | 323 | } |
| 324 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 325 | static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page, |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 326 | u64 *spte, const void *pte, int bytes, |
| 327 | int offset_in_pte) |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 328 | { |
| 329 | pt_element_t gpte; |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame^] | 330 | unsigned pte_access; |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 331 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 332 | gpte = *(const pt_element_t *)pte; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 333 | if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) { |
| 334 | if (!offset_in_pte && !is_present_pte(gpte)) |
| 335 | set_shadow_pte(spte, shadow_notrap_nonpresent_pte); |
| 336 | return; |
| 337 | } |
| 338 | if (bytes < sizeof(pt_element_t)) |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 339 | return; |
| 340 | pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte); |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame^] | 341 | pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte); |
| 342 | FNAME(set_pte)(vcpu, gpte, spte, page->role.access, pte_access, |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 343 | 0, 0, NULL, NULL, gpte_to_gfn(gpte)); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 344 | } |
| 345 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 346 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 347 | * Fetch a shadow pte for a specific level in the paging hierarchy. |
| 348 | */ |
| 349 | static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 350 | struct guest_walker *walker, |
| 351 | int user_fault, int write_fault, int *ptwrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 352 | { |
| 353 | hpa_t shadow_addr; |
| 354 | int level; |
Avi Kivity | ef0197e | 2007-05-30 14:21:51 +0300 | [diff] [blame] | 355 | u64 *shadow_ent; |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 356 | unsigned access = walker->pt_access; |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 357 | |
Shaohua Li | fe551881 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 358 | if (!is_present_pte(walker->pte)) |
Avi Kivity | ac79c97 | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 359 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 360 | |
| 361 | shadow_addr = vcpu->mmu.root_hpa; |
| 362 | level = vcpu->mmu.shadow_root_level; |
Avi Kivity | aef3d3f | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 363 | if (level == PT32E_ROOT_LEVEL) { |
| 364 | shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3]; |
| 365 | shadow_addr &= PT64_BASE_ADDR_MASK; |
| 366 | --level; |
| 367 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 368 | |
| 369 | for (; ; level--) { |
| 370 | u32 index = SHADOW_PT_INDEX(addr, level); |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 371 | struct kvm_mmu_page *shadow_page; |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 372 | u64 shadow_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 373 | int metaphysical; |
| 374 | gfn_t table_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 375 | |
Avi Kivity | ef0197e | 2007-05-30 14:21:51 +0300 | [diff] [blame] | 376 | shadow_ent = ((u64 *)__va(shadow_addr)) + index; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 377 | if (is_shadow_present_pte(*shadow_ent)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 378 | if (level == PT_PAGE_TABLE_LEVEL) |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 379 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 380 | shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 381 | continue; |
| 382 | } |
| 383 | |
Avi Kivity | ef0197e | 2007-05-30 14:21:51 +0300 | [diff] [blame] | 384 | if (level == PT_PAGE_TABLE_LEVEL) |
| 385 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 386 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 387 | if (level - 1 == PT_PAGE_TABLE_LEVEL |
| 388 | && walker->level == PT_DIRECTORY_LEVEL) { |
| 389 | metaphysical = 1; |
Avi Kivity | cc70e73 | 2007-10-11 15:12:24 +0200 | [diff] [blame] | 390 | if (!is_dirty_pte(walker->pte)) |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 391 | access &= ~ACC_WRITE_MASK; |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 392 | table_gfn = gpte_to_gfn(walker->pte); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 393 | } else { |
| 394 | metaphysical = 0; |
| 395 | table_gfn = walker->table_gfn[level - 2]; |
| 396 | } |
| 397 | shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1, |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 398 | metaphysical, access, |
Avi Kivity | d28c6cfb | 2007-03-23 09:55:25 +0200 | [diff] [blame] | 399 | shadow_ent); |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 400 | shadow_addr = __pa(shadow_page->spt); |
Avi Kivity | aef3d3f | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 401 | shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK |
| 402 | | PT_WRITABLE_MASK | PT_USER_MASK; |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 403 | *shadow_ent = shadow_pte; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 404 | } |
Avi Kivity | ef0197e | 2007-05-30 14:21:51 +0300 | [diff] [blame] | 405 | |
Avi Kivity | 050e649 | 2007-11-21 14:11:49 +0200 | [diff] [blame] | 406 | FNAME(set_pte)(vcpu, walker->pte, shadow_ent, |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 407 | access, walker->pte_access & access, |
| 408 | user_fault, write_fault, |
Avi Kivity | 050e649 | 2007-11-21 14:11:49 +0200 | [diff] [blame] | 409 | ptwrite, walker, walker->gfn); |
| 410 | |
Avi Kivity | ef0197e | 2007-05-30 14:21:51 +0300 | [diff] [blame] | 411 | return shadow_ent; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 415 | * Page fault handler. There are several causes for a page fault: |
| 416 | * - there is no shadow pte for the guest pte |
| 417 | * - write access through a shadow pte marked read only so that we can set |
| 418 | * the dirty bit |
| 419 | * - write access to a shadow pte marked read only so we can update the page |
| 420 | * dirty bitmap, when userspace requests it |
| 421 | * - mmio access; in this case we will never install a present shadow pte |
| 422 | * - normal guest page fault due to the guest pte marked not present, not |
| 423 | * writable, or not executable |
| 424 | * |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 425 | * Returns: 1 if we need to emulate the instruction, 0 otherwise, or |
| 426 | * a negative value on error. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 427 | */ |
| 428 | static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, |
| 429 | u32 error_code) |
| 430 | { |
| 431 | int write_fault = error_code & PFERR_WRITE_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 432 | int user_fault = error_code & PFERR_USER_MASK; |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 433 | int fetch_fault = error_code & PFERR_FETCH_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 434 | struct guest_walker walker; |
| 435 | u64 *shadow_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 436 | int write_pt = 0; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 437 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 438 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 439 | pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 440 | kvm_mmu_audit(vcpu, "pre page fault"); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 441 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 442 | r = mmu_topup_memory_caches(vcpu); |
| 443 | if (r) |
| 444 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 445 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 446 | /* |
| 447 | * Look up the shadow pte for the faulting address. |
| 448 | */ |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 449 | r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault, |
| 450 | fetch_fault); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * The page is not mapped by the guest. Let the guest handle it. |
| 454 | */ |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 455 | if (!r) { |
| 456 | pgprintk("%s: guest page fault\n", __FUNCTION__); |
| 457 | inject_page_fault(vcpu, addr, walker.error_code); |
Avi Kivity | a25f7e1 | 2007-04-30 17:05:38 +0300 | [diff] [blame] | 458 | vcpu->last_pt_write_count = 0; /* reset fork detector */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 462 | shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault, |
| 463 | &write_pt); |
| 464 | pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__, |
| 465 | shadow_pte, *shadow_pte, write_pt); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 466 | |
Avi Kivity | a25f7e1 | 2007-04-30 17:05:38 +0300 | [diff] [blame] | 467 | if (!write_pt) |
| 468 | vcpu->last_pt_write_count = 0; /* reset fork detector */ |
| 469 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 470 | /* |
| 471 | * mmio: emulate if accessible, otherwise its a guest fault. |
| 472 | */ |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 473 | if (is_io_pte(*shadow_pte)) |
Avi Kivity | 7993ba4 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 474 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 475 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 476 | ++vcpu->stat.pf_fixed; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 477 | kvm_mmu_audit(vcpu, "post page fault (fixed)"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 478 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 479 | return write_pt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr) |
| 483 | { |
| 484 | struct guest_walker walker; |
Avi Kivity | e119d11 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 485 | gpa_t gpa = UNMAPPED_GVA; |
| 486 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 487 | |
Avi Kivity | e119d11 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 488 | r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 489 | |
Avi Kivity | e119d11 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 490 | if (r) { |
Avi Kivity | 1755fbc | 2007-11-21 14:44:45 +0200 | [diff] [blame] | 491 | gpa = gfn_to_gpa(walker.gfn); |
Avi Kivity | e119d11 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 492 | gpa |= vaddr & ~PAGE_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return gpa; |
| 496 | } |
| 497 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 498 | static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu, |
| 499 | struct kvm_mmu_page *sp) |
| 500 | { |
Avi Kivity | e5a4c8c | 2007-11-20 21:39:54 +0200 | [diff] [blame] | 501 | int i, offset = 0; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 502 | pt_element_t *gpt; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 503 | struct page *page; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 504 | |
Avi Kivity | e5a4c8c | 2007-11-20 21:39:54 +0200 | [diff] [blame] | 505 | if (sp->role.metaphysical |
| 506 | || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) { |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 507 | nonpaging_prefetch_page(vcpu, sp); |
| 508 | return; |
| 509 | } |
| 510 | |
Avi Kivity | e5a4c8c | 2007-11-20 21:39:54 +0200 | [diff] [blame] | 511 | if (PTTYPE == 32) |
| 512 | offset = sp->role.quadrant << PT64_LEVEL_BITS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 513 | page = gfn_to_page(vcpu->kvm, sp->gfn); |
| 514 | gpt = kmap_atomic(page, KM_USER0); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 515 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
Avi Kivity | e5a4c8c | 2007-11-20 21:39:54 +0200 | [diff] [blame] | 516 | if (is_present_pte(gpt[offset + i])) |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 517 | sp->spt[i] = shadow_trap_nonpresent_pte; |
| 518 | else |
| 519 | sp->spt[i] = shadow_notrap_nonpresent_pte; |
| 520 | kunmap_atomic(gpt, KM_USER0); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 521 | kvm_release_page_clean(page); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 524 | #undef pt_element_t |
| 525 | #undef guest_walker |
| 526 | #undef FNAME |
| 527 | #undef PT_BASE_ADDR_MASK |
| 528 | #undef PT_INDEX |
| 529 | #undef SHADOW_PT_INDEX |
| 530 | #undef PT_LEVEL_MASK |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 531 | #undef PT_DIR_BASE_ADDR_MASK |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 532 | #undef PT_LEVEL_BITS |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 533 | #undef PT_MAX_FULL_LEVELS |
Avi Kivity | 5fb07dd | 2007-11-21 12:35:07 +0200 | [diff] [blame] | 534 | #undef gpte_to_gfn |
| 535 | #undef gpte_to_gfn_pde |
Marcelo Tosatti | b3e4e63 | 2007-12-07 07:56:58 -0500 | [diff] [blame] | 536 | #undef CMPXCHG |