1e7f2670fSClaudio Fontana /* 2e7f2670fSClaudio Fontana * x86 exception helpers - sysemu code 3e7f2670fSClaudio Fontana * 4e7f2670fSClaudio Fontana * Copyright (c) 2003 Fabrice Bellard 5e7f2670fSClaudio Fontana * 6e7f2670fSClaudio Fontana * This library is free software; you can redistribute it and/or 7e7f2670fSClaudio Fontana * modify it under the terms of the GNU Lesser General Public 8e7f2670fSClaudio Fontana * License as published by the Free Software Foundation; either 9e7f2670fSClaudio Fontana * version 2.1 of the License, or (at your option) any later version. 10e7f2670fSClaudio Fontana * 11e7f2670fSClaudio Fontana * This library is distributed in the hope that it will be useful, 12e7f2670fSClaudio Fontana * but WITHOUT ANY WARRANTY; without even the implied warranty of 13e7f2670fSClaudio Fontana * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14e7f2670fSClaudio Fontana * Lesser General Public License for more details. 15e7f2670fSClaudio Fontana * 16e7f2670fSClaudio Fontana * You should have received a copy of the GNU Lesser General Public 17e7f2670fSClaudio Fontana * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18e7f2670fSClaudio Fontana */ 19e7f2670fSClaudio Fontana 20e7f2670fSClaudio Fontana #include "qemu/osdep.h" 21e7f2670fSClaudio Fontana #include "cpu.h" 22e7f2670fSClaudio Fontana #include "tcg/helper-tcg.h" 23e7f2670fSClaudio Fontana 24616a89eaSPaolo Bonzini int get_pg_mode(CPUX86State *env) 25616a89eaSPaolo Bonzini { 26616a89eaSPaolo Bonzini int pg_mode = 0; 27*31dd35ebSPaolo Bonzini if (env->cr[0] & CR0_WP_MASK) { 28*31dd35ebSPaolo Bonzini pg_mode |= PG_MODE_WP; 29*31dd35ebSPaolo Bonzini } 30616a89eaSPaolo Bonzini if (env->cr[4] & CR4_PAE_MASK) { 31616a89eaSPaolo Bonzini pg_mode |= PG_MODE_PAE; 32616a89eaSPaolo Bonzini } 33616a89eaSPaolo Bonzini if (env->cr[4] & CR4_PSE_MASK) { 34616a89eaSPaolo Bonzini pg_mode |= PG_MODE_PSE; 35616a89eaSPaolo Bonzini } 36*31dd35ebSPaolo Bonzini if (env->cr[4] & CR4_PKE_MASK) { 37*31dd35ebSPaolo Bonzini pg_mode |= PG_MODE_PKE; 38*31dd35ebSPaolo Bonzini } 39*31dd35ebSPaolo Bonzini if (env->cr[4] & CR4_PKS_MASK) { 40*31dd35ebSPaolo Bonzini pg_mode |= PG_MODE_PKS; 41*31dd35ebSPaolo Bonzini } 42*31dd35ebSPaolo Bonzini if (env->cr[4] & CR4_SMEP_MASK) { 43*31dd35ebSPaolo Bonzini pg_mode |= PG_MODE_SMEP; 44*31dd35ebSPaolo Bonzini } 45*31dd35ebSPaolo Bonzini if (env->cr[4] & CR4_LA57_MASK) { 46*31dd35ebSPaolo Bonzini pg_mode |= PG_MODE_LA57; 47*31dd35ebSPaolo Bonzini } 48616a89eaSPaolo Bonzini if (env->hflags & HF_LMA_MASK) { 49616a89eaSPaolo Bonzini pg_mode |= PG_MODE_LMA; 50616a89eaSPaolo Bonzini } 51616a89eaSPaolo Bonzini if (env->efer & MSR_EFER_NXE) { 52616a89eaSPaolo Bonzini pg_mode |= PG_MODE_NXE; 53616a89eaSPaolo Bonzini } 54616a89eaSPaolo Bonzini return pg_mode; 55616a89eaSPaolo Bonzini } 56616a89eaSPaolo Bonzini 57e7f2670fSClaudio Fontana static hwaddr get_hphys(CPUState *cs, hwaddr gphys, MMUAccessType access_type, 58e7f2670fSClaudio Fontana int *prot) 59e7f2670fSClaudio Fontana { 60e7f2670fSClaudio Fontana X86CPU *cpu = X86_CPU(cs); 61e7f2670fSClaudio Fontana CPUX86State *env = &cpu->env; 62e7f2670fSClaudio Fontana uint64_t rsvd_mask = PG_ADDRESS_MASK & ~MAKE_64BIT_MASK(0, cpu->phys_bits); 63e7f2670fSClaudio Fontana uint64_t ptep, pte; 64e7f2670fSClaudio Fontana uint64_t exit_info_1 = 0; 65e7f2670fSClaudio Fontana target_ulong pde_addr, pte_addr; 66e7f2670fSClaudio Fontana uint32_t page_offset; 67e7f2670fSClaudio Fontana int page_size; 68e7f2670fSClaudio Fontana 69e7f2670fSClaudio Fontana if (likely(!(env->hflags2 & HF2_NPT_MASK))) { 70e7f2670fSClaudio Fontana return gphys; 71e7f2670fSClaudio Fontana } 72e7f2670fSClaudio Fontana 73616a89eaSPaolo Bonzini if (!(env->nested_pg_mode & PG_MODE_NXE)) { 74e7f2670fSClaudio Fontana rsvd_mask |= PG_NX_MASK; 75e7f2670fSClaudio Fontana } 76e7f2670fSClaudio Fontana 77616a89eaSPaolo Bonzini if (env->nested_pg_mode & PG_MODE_PAE) { 78e7f2670fSClaudio Fontana uint64_t pde, pdpe; 79e7f2670fSClaudio Fontana target_ulong pdpe_addr; 80e7f2670fSClaudio Fontana 81e7f2670fSClaudio Fontana #ifdef TARGET_X86_64 82616a89eaSPaolo Bonzini if (env->nested_pg_mode & PG_MODE_LMA) { 83e7f2670fSClaudio Fontana uint64_t pml5e; 84e7f2670fSClaudio Fontana uint64_t pml4e_addr, pml4e; 85e7f2670fSClaudio Fontana 86e7f2670fSClaudio Fontana pml5e = env->nested_cr3; 87e7f2670fSClaudio Fontana ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; 88e7f2670fSClaudio Fontana 89e7f2670fSClaudio Fontana pml4e_addr = (pml5e & PG_ADDRESS_MASK) + 90e7f2670fSClaudio Fontana (((gphys >> 39) & 0x1ff) << 3); 91e7f2670fSClaudio Fontana pml4e = x86_ldq_phys(cs, pml4e_addr); 92e7f2670fSClaudio Fontana if (!(pml4e & PG_PRESENT_MASK)) { 93e7f2670fSClaudio Fontana goto do_fault; 94e7f2670fSClaudio Fontana } 95e7f2670fSClaudio Fontana if (pml4e & (rsvd_mask | PG_PSE_MASK)) { 96e7f2670fSClaudio Fontana goto do_fault_rsvd; 97e7f2670fSClaudio Fontana } 98e7f2670fSClaudio Fontana if (!(pml4e & PG_ACCESSED_MASK)) { 99e7f2670fSClaudio Fontana pml4e |= PG_ACCESSED_MASK; 100e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pml4e_addr, pml4e); 101e7f2670fSClaudio Fontana } 102e7f2670fSClaudio Fontana ptep &= pml4e ^ PG_NX_MASK; 103e7f2670fSClaudio Fontana pdpe_addr = (pml4e & PG_ADDRESS_MASK) + 104e7f2670fSClaudio Fontana (((gphys >> 30) & 0x1ff) << 3); 105e7f2670fSClaudio Fontana pdpe = x86_ldq_phys(cs, pdpe_addr); 106e7f2670fSClaudio Fontana if (!(pdpe & PG_PRESENT_MASK)) { 107e7f2670fSClaudio Fontana goto do_fault; 108e7f2670fSClaudio Fontana } 109e7f2670fSClaudio Fontana if (pdpe & rsvd_mask) { 110e7f2670fSClaudio Fontana goto do_fault_rsvd; 111e7f2670fSClaudio Fontana } 112e7f2670fSClaudio Fontana ptep &= pdpe ^ PG_NX_MASK; 113e7f2670fSClaudio Fontana if (!(pdpe & PG_ACCESSED_MASK)) { 114e7f2670fSClaudio Fontana pdpe |= PG_ACCESSED_MASK; 115e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pdpe_addr, pdpe); 116e7f2670fSClaudio Fontana } 117e7f2670fSClaudio Fontana if (pdpe & PG_PSE_MASK) { 118e7f2670fSClaudio Fontana /* 1 GB page */ 119e7f2670fSClaudio Fontana page_size = 1024 * 1024 * 1024; 120e7f2670fSClaudio Fontana pte_addr = pdpe_addr; 121e7f2670fSClaudio Fontana pte = pdpe; 122e7f2670fSClaudio Fontana goto do_check_protect; 123e7f2670fSClaudio Fontana } 124e7f2670fSClaudio Fontana } else 125e7f2670fSClaudio Fontana #endif 126e7f2670fSClaudio Fontana { 127e7f2670fSClaudio Fontana pdpe_addr = (env->nested_cr3 & ~0x1f) + ((gphys >> 27) & 0x18); 128e7f2670fSClaudio Fontana pdpe = x86_ldq_phys(cs, pdpe_addr); 129e7f2670fSClaudio Fontana if (!(pdpe & PG_PRESENT_MASK)) { 130e7f2670fSClaudio Fontana goto do_fault; 131e7f2670fSClaudio Fontana } 132e7f2670fSClaudio Fontana rsvd_mask |= PG_HI_USER_MASK; 133e7f2670fSClaudio Fontana if (pdpe & (rsvd_mask | PG_NX_MASK)) { 134e7f2670fSClaudio Fontana goto do_fault_rsvd; 135e7f2670fSClaudio Fontana } 136e7f2670fSClaudio Fontana ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; 137e7f2670fSClaudio Fontana } 138e7f2670fSClaudio Fontana 139e7f2670fSClaudio Fontana pde_addr = (pdpe & PG_ADDRESS_MASK) + (((gphys >> 21) & 0x1ff) << 3); 140e7f2670fSClaudio Fontana pde = x86_ldq_phys(cs, pde_addr); 141e7f2670fSClaudio Fontana if (!(pde & PG_PRESENT_MASK)) { 142e7f2670fSClaudio Fontana goto do_fault; 143e7f2670fSClaudio Fontana } 144e7f2670fSClaudio Fontana if (pde & rsvd_mask) { 145e7f2670fSClaudio Fontana goto do_fault_rsvd; 146e7f2670fSClaudio Fontana } 147e7f2670fSClaudio Fontana ptep &= pde ^ PG_NX_MASK; 148e7f2670fSClaudio Fontana if (pde & PG_PSE_MASK) { 149e7f2670fSClaudio Fontana /* 2 MB page */ 150e7f2670fSClaudio Fontana page_size = 2048 * 1024; 151e7f2670fSClaudio Fontana pte_addr = pde_addr; 152e7f2670fSClaudio Fontana pte = pde; 153e7f2670fSClaudio Fontana goto do_check_protect; 154e7f2670fSClaudio Fontana } 155e7f2670fSClaudio Fontana /* 4 KB page */ 156e7f2670fSClaudio Fontana if (!(pde & PG_ACCESSED_MASK)) { 157e7f2670fSClaudio Fontana pde |= PG_ACCESSED_MASK; 158e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pde_addr, pde); 159e7f2670fSClaudio Fontana } 160e7f2670fSClaudio Fontana pte_addr = (pde & PG_ADDRESS_MASK) + (((gphys >> 12) & 0x1ff) << 3); 161e7f2670fSClaudio Fontana pte = x86_ldq_phys(cs, pte_addr); 162e7f2670fSClaudio Fontana if (!(pte & PG_PRESENT_MASK)) { 163e7f2670fSClaudio Fontana goto do_fault; 164e7f2670fSClaudio Fontana } 165e7f2670fSClaudio Fontana if (pte & rsvd_mask) { 166e7f2670fSClaudio Fontana goto do_fault_rsvd; 167e7f2670fSClaudio Fontana } 168e7f2670fSClaudio Fontana /* combine pde and pte nx, user and rw protections */ 169e7f2670fSClaudio Fontana ptep &= pte ^ PG_NX_MASK; 170e7f2670fSClaudio Fontana page_size = 4096; 171e7f2670fSClaudio Fontana } else { 172e7f2670fSClaudio Fontana uint32_t pde; 173e7f2670fSClaudio Fontana 174e7f2670fSClaudio Fontana /* page directory entry */ 175e7f2670fSClaudio Fontana pde_addr = (env->nested_cr3 & ~0xfff) + ((gphys >> 20) & 0xffc); 176e7f2670fSClaudio Fontana pde = x86_ldl_phys(cs, pde_addr); 177e7f2670fSClaudio Fontana if (!(pde & PG_PRESENT_MASK)) { 178e7f2670fSClaudio Fontana goto do_fault; 179e7f2670fSClaudio Fontana } 180e7f2670fSClaudio Fontana ptep = pde | PG_NX_MASK; 181e7f2670fSClaudio Fontana 182e7f2670fSClaudio Fontana /* if host cr4 PSE bit is set, then we use a 4MB page */ 183616a89eaSPaolo Bonzini if ((pde & PG_PSE_MASK) && (env->nested_pg_mode & PG_MODE_PSE)) { 184e7f2670fSClaudio Fontana page_size = 4096 * 1024; 185e7f2670fSClaudio Fontana pte_addr = pde_addr; 186e7f2670fSClaudio Fontana 187e7f2670fSClaudio Fontana /* Bits 20-13 provide bits 39-32 of the address, bit 21 is reserved. 188e7f2670fSClaudio Fontana * Leave bits 20-13 in place for setting accessed/dirty bits below. 189e7f2670fSClaudio Fontana */ 190e7f2670fSClaudio Fontana pte = pde | ((pde & 0x1fe000LL) << (32 - 13)); 191e7f2670fSClaudio Fontana rsvd_mask = 0x200000; 192e7f2670fSClaudio Fontana goto do_check_protect_pse36; 193e7f2670fSClaudio Fontana } 194e7f2670fSClaudio Fontana 195e7f2670fSClaudio Fontana if (!(pde & PG_ACCESSED_MASK)) { 196e7f2670fSClaudio Fontana pde |= PG_ACCESSED_MASK; 197e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pde_addr, pde); 198e7f2670fSClaudio Fontana } 199e7f2670fSClaudio Fontana 200e7f2670fSClaudio Fontana /* page directory entry */ 201e7f2670fSClaudio Fontana pte_addr = (pde & ~0xfff) + ((gphys >> 10) & 0xffc); 202e7f2670fSClaudio Fontana pte = x86_ldl_phys(cs, pte_addr); 203e7f2670fSClaudio Fontana if (!(pte & PG_PRESENT_MASK)) { 204e7f2670fSClaudio Fontana goto do_fault; 205e7f2670fSClaudio Fontana } 206e7f2670fSClaudio Fontana /* combine pde and pte user and rw protections */ 207e7f2670fSClaudio Fontana ptep &= pte | PG_NX_MASK; 208e7f2670fSClaudio Fontana page_size = 4096; 209e7f2670fSClaudio Fontana rsvd_mask = 0; 210e7f2670fSClaudio Fontana } 211e7f2670fSClaudio Fontana 212e7f2670fSClaudio Fontana do_check_protect: 213e7f2670fSClaudio Fontana rsvd_mask |= (page_size - 1) & PG_ADDRESS_MASK & ~PG_PSE_PAT_MASK; 214e7f2670fSClaudio Fontana do_check_protect_pse36: 215e7f2670fSClaudio Fontana if (pte & rsvd_mask) { 216e7f2670fSClaudio Fontana goto do_fault_rsvd; 217e7f2670fSClaudio Fontana } 218e7f2670fSClaudio Fontana ptep ^= PG_NX_MASK; 219e7f2670fSClaudio Fontana 220e7f2670fSClaudio Fontana if (!(ptep & PG_USER_MASK)) { 221e7f2670fSClaudio Fontana goto do_fault_protect; 222e7f2670fSClaudio Fontana } 223e7f2670fSClaudio Fontana if (ptep & PG_NX_MASK) { 224e7f2670fSClaudio Fontana if (access_type == MMU_INST_FETCH) { 225e7f2670fSClaudio Fontana goto do_fault_protect; 226e7f2670fSClaudio Fontana } 227e7f2670fSClaudio Fontana *prot &= ~PAGE_EXEC; 228e7f2670fSClaudio Fontana } 229e7f2670fSClaudio Fontana if (!(ptep & PG_RW_MASK)) { 230e7f2670fSClaudio Fontana if (access_type == MMU_DATA_STORE) { 231e7f2670fSClaudio Fontana goto do_fault_protect; 232e7f2670fSClaudio Fontana } 233e7f2670fSClaudio Fontana *prot &= ~PAGE_WRITE; 234e7f2670fSClaudio Fontana } 235e7f2670fSClaudio Fontana 236e7f2670fSClaudio Fontana pte &= PG_ADDRESS_MASK & ~(page_size - 1); 237e7f2670fSClaudio Fontana page_offset = gphys & (page_size - 1); 238e7f2670fSClaudio Fontana return pte + page_offset; 239e7f2670fSClaudio Fontana 240e7f2670fSClaudio Fontana do_fault_rsvd: 2416ed6b0d3SPaolo Bonzini exit_info_1 |= PG_ERROR_RSVD_MASK; 242e7f2670fSClaudio Fontana do_fault_protect: 2436ed6b0d3SPaolo Bonzini exit_info_1 |= PG_ERROR_P_MASK; 244e7f2670fSClaudio Fontana do_fault: 245e7f2670fSClaudio Fontana x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 246e7f2670fSClaudio Fontana gphys); 2476ed6b0d3SPaolo Bonzini exit_info_1 |= PG_ERROR_U_MASK; 248e7f2670fSClaudio Fontana if (access_type == MMU_DATA_STORE) { 2496ed6b0d3SPaolo Bonzini exit_info_1 |= PG_ERROR_W_MASK; 250e7f2670fSClaudio Fontana } else if (access_type == MMU_INST_FETCH) { 2516ed6b0d3SPaolo Bonzini exit_info_1 |= PG_ERROR_I_D_MASK; 252e7f2670fSClaudio Fontana } 253e7f2670fSClaudio Fontana if (prot) { 254e7f2670fSClaudio Fontana exit_info_1 |= SVM_NPTEXIT_GPA; 255e7f2670fSClaudio Fontana } else { /* page table access */ 256e7f2670fSClaudio Fontana exit_info_1 |= SVM_NPTEXIT_GPT; 257e7f2670fSClaudio Fontana } 258e7f2670fSClaudio Fontana cpu_vmexit(env, SVM_EXIT_NPF, exit_info_1, env->retaddr); 259e7f2670fSClaudio Fontana } 260e7f2670fSClaudio Fontana 261661ff487SPaolo Bonzini #define PG_ERROR_OK (-1) 262661ff487SPaolo Bonzini 263661ff487SPaolo Bonzini static int mmu_translate(CPUState *cs, vaddr addr, 264*31dd35ebSPaolo Bonzini uint64_t cr3, int is_write1, int mmu_idx, int pg_mode, 265661ff487SPaolo Bonzini vaddr *xlat, int *page_size, int *prot) 266e7f2670fSClaudio Fontana { 267e7f2670fSClaudio Fontana X86CPU *cpu = X86_CPU(cs); 268e7f2670fSClaudio Fontana CPUX86State *env = &cpu->env; 269e7f2670fSClaudio Fontana uint64_t ptep, pte; 270e7f2670fSClaudio Fontana int32_t a20_mask; 271e7f2670fSClaudio Fontana target_ulong pde_addr, pte_addr; 272e7f2670fSClaudio Fontana int error_code = 0; 273661ff487SPaolo Bonzini int is_dirty, is_write, is_user; 274e7f2670fSClaudio Fontana uint64_t rsvd_mask = PG_ADDRESS_MASK & ~MAKE_64BIT_MASK(0, cpu->phys_bits); 275e7f2670fSClaudio Fontana uint32_t page_offset; 276e7f2670fSClaudio Fontana uint32_t pkr; 277e7f2670fSClaudio Fontana 278661ff487SPaolo Bonzini is_user = (mmu_idx == MMU_USER_IDX); 279e7f2670fSClaudio Fontana is_write = is_write1 & 1; 280e7f2670fSClaudio Fontana a20_mask = x86_get_a20_mask(env); 281e7f2670fSClaudio Fontana 282*31dd35ebSPaolo Bonzini if (!(pg_mode & PG_MODE_NXE)) { 283e7f2670fSClaudio Fontana rsvd_mask |= PG_NX_MASK; 284e7f2670fSClaudio Fontana } 285e7f2670fSClaudio Fontana 286*31dd35ebSPaolo Bonzini if (pg_mode & PG_MODE_PAE) { 287e7f2670fSClaudio Fontana uint64_t pde, pdpe; 288e7f2670fSClaudio Fontana target_ulong pdpe_addr; 289e7f2670fSClaudio Fontana 290e7f2670fSClaudio Fontana #ifdef TARGET_X86_64 291e7f2670fSClaudio Fontana if (env->hflags & HF_LMA_MASK) { 292*31dd35ebSPaolo Bonzini bool la57 = pg_mode & PG_MODE_LA57; 293e7f2670fSClaudio Fontana uint64_t pml5e_addr, pml5e; 294e7f2670fSClaudio Fontana uint64_t pml4e_addr, pml4e; 295e7f2670fSClaudio Fontana int32_t sext; 296e7f2670fSClaudio Fontana 297e7f2670fSClaudio Fontana /* test virtual address sign extension */ 298e7f2670fSClaudio Fontana sext = la57 ? (int64_t)addr >> 56 : (int64_t)addr >> 47; 299e7f2670fSClaudio Fontana if (sext != 0 && sext != -1) { 300e7f2670fSClaudio Fontana env->error_code = 0; 301e7f2670fSClaudio Fontana cs->exception_index = EXCP0D_GPF; 302e7f2670fSClaudio Fontana return 1; 303e7f2670fSClaudio Fontana } 304e7f2670fSClaudio Fontana 305e7f2670fSClaudio Fontana if (la57) { 306cd906d31SPaolo Bonzini pml5e_addr = ((cr3 & ~0xfff) + 307e7f2670fSClaudio Fontana (((addr >> 48) & 0x1ff) << 3)) & a20_mask; 308e7f2670fSClaudio Fontana pml5e_addr = get_hphys(cs, pml5e_addr, MMU_DATA_STORE, NULL); 309e7f2670fSClaudio Fontana pml5e = x86_ldq_phys(cs, pml5e_addr); 310e7f2670fSClaudio Fontana if (!(pml5e & PG_PRESENT_MASK)) { 311e7f2670fSClaudio Fontana goto do_fault; 312e7f2670fSClaudio Fontana } 313e7f2670fSClaudio Fontana if (pml5e & (rsvd_mask | PG_PSE_MASK)) { 314e7f2670fSClaudio Fontana goto do_fault_rsvd; 315e7f2670fSClaudio Fontana } 316e7f2670fSClaudio Fontana if (!(pml5e & PG_ACCESSED_MASK)) { 317e7f2670fSClaudio Fontana pml5e |= PG_ACCESSED_MASK; 318e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pml5e_addr, pml5e); 319e7f2670fSClaudio Fontana } 320e7f2670fSClaudio Fontana ptep = pml5e ^ PG_NX_MASK; 321e7f2670fSClaudio Fontana } else { 322cd906d31SPaolo Bonzini pml5e = cr3; 323e7f2670fSClaudio Fontana ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; 324e7f2670fSClaudio Fontana } 325e7f2670fSClaudio Fontana 326e7f2670fSClaudio Fontana pml4e_addr = ((pml5e & PG_ADDRESS_MASK) + 327e7f2670fSClaudio Fontana (((addr >> 39) & 0x1ff) << 3)) & a20_mask; 328e7f2670fSClaudio Fontana pml4e_addr = get_hphys(cs, pml4e_addr, MMU_DATA_STORE, false); 329e7f2670fSClaudio Fontana pml4e = x86_ldq_phys(cs, pml4e_addr); 330e7f2670fSClaudio Fontana if (!(pml4e & PG_PRESENT_MASK)) { 331e7f2670fSClaudio Fontana goto do_fault; 332e7f2670fSClaudio Fontana } 333e7f2670fSClaudio Fontana if (pml4e & (rsvd_mask | PG_PSE_MASK)) { 334e7f2670fSClaudio Fontana goto do_fault_rsvd; 335e7f2670fSClaudio Fontana } 336e7f2670fSClaudio Fontana if (!(pml4e & PG_ACCESSED_MASK)) { 337e7f2670fSClaudio Fontana pml4e |= PG_ACCESSED_MASK; 338e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pml4e_addr, pml4e); 339e7f2670fSClaudio Fontana } 340e7f2670fSClaudio Fontana ptep &= pml4e ^ PG_NX_MASK; 341e7f2670fSClaudio Fontana pdpe_addr = ((pml4e & PG_ADDRESS_MASK) + (((addr >> 30) & 0x1ff) << 3)) & 342e7f2670fSClaudio Fontana a20_mask; 343e7f2670fSClaudio Fontana pdpe_addr = get_hphys(cs, pdpe_addr, MMU_DATA_STORE, NULL); 344e7f2670fSClaudio Fontana pdpe = x86_ldq_phys(cs, pdpe_addr); 345e7f2670fSClaudio Fontana if (!(pdpe & PG_PRESENT_MASK)) { 346e7f2670fSClaudio Fontana goto do_fault; 347e7f2670fSClaudio Fontana } 348e7f2670fSClaudio Fontana if (pdpe & rsvd_mask) { 349e7f2670fSClaudio Fontana goto do_fault_rsvd; 350e7f2670fSClaudio Fontana } 351e7f2670fSClaudio Fontana ptep &= pdpe ^ PG_NX_MASK; 352e7f2670fSClaudio Fontana if (!(pdpe & PG_ACCESSED_MASK)) { 353e7f2670fSClaudio Fontana pdpe |= PG_ACCESSED_MASK; 354e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pdpe_addr, pdpe); 355e7f2670fSClaudio Fontana } 356e7f2670fSClaudio Fontana if (pdpe & PG_PSE_MASK) { 357e7f2670fSClaudio Fontana /* 1 GB page */ 358661ff487SPaolo Bonzini *page_size = 1024 * 1024 * 1024; 359e7f2670fSClaudio Fontana pte_addr = pdpe_addr; 360e7f2670fSClaudio Fontana pte = pdpe; 361e7f2670fSClaudio Fontana goto do_check_protect; 362e7f2670fSClaudio Fontana } 363e7f2670fSClaudio Fontana } else 364e7f2670fSClaudio Fontana #endif 365e7f2670fSClaudio Fontana { 366e7f2670fSClaudio Fontana /* XXX: load them when cr3 is loaded ? */ 367cd906d31SPaolo Bonzini pdpe_addr = ((cr3 & ~0x1f) + ((addr >> 27) & 0x18)) & 368e7f2670fSClaudio Fontana a20_mask; 369e7f2670fSClaudio Fontana pdpe_addr = get_hphys(cs, pdpe_addr, MMU_DATA_STORE, false); 370e7f2670fSClaudio Fontana pdpe = x86_ldq_phys(cs, pdpe_addr); 371e7f2670fSClaudio Fontana if (!(pdpe & PG_PRESENT_MASK)) { 372e7f2670fSClaudio Fontana goto do_fault; 373e7f2670fSClaudio Fontana } 374e7f2670fSClaudio Fontana rsvd_mask |= PG_HI_USER_MASK; 375e7f2670fSClaudio Fontana if (pdpe & (rsvd_mask | PG_NX_MASK)) { 376e7f2670fSClaudio Fontana goto do_fault_rsvd; 377e7f2670fSClaudio Fontana } 378e7f2670fSClaudio Fontana ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; 379e7f2670fSClaudio Fontana } 380e7f2670fSClaudio Fontana 381e7f2670fSClaudio Fontana pde_addr = ((pdpe & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) << 3)) & 382e7f2670fSClaudio Fontana a20_mask; 383e7f2670fSClaudio Fontana pde_addr = get_hphys(cs, pde_addr, MMU_DATA_STORE, NULL); 384e7f2670fSClaudio Fontana pde = x86_ldq_phys(cs, pde_addr); 385e7f2670fSClaudio Fontana if (!(pde & PG_PRESENT_MASK)) { 386e7f2670fSClaudio Fontana goto do_fault; 387e7f2670fSClaudio Fontana } 388e7f2670fSClaudio Fontana if (pde & rsvd_mask) { 389e7f2670fSClaudio Fontana goto do_fault_rsvd; 390e7f2670fSClaudio Fontana } 391e7f2670fSClaudio Fontana ptep &= pde ^ PG_NX_MASK; 392e7f2670fSClaudio Fontana if (pde & PG_PSE_MASK) { 393e7f2670fSClaudio Fontana /* 2 MB page */ 394661ff487SPaolo Bonzini *page_size = 2048 * 1024; 395e7f2670fSClaudio Fontana pte_addr = pde_addr; 396e7f2670fSClaudio Fontana pte = pde; 397e7f2670fSClaudio Fontana goto do_check_protect; 398e7f2670fSClaudio Fontana } 399e7f2670fSClaudio Fontana /* 4 KB page */ 400e7f2670fSClaudio Fontana if (!(pde & PG_ACCESSED_MASK)) { 401e7f2670fSClaudio Fontana pde |= PG_ACCESSED_MASK; 402e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pde_addr, pde); 403e7f2670fSClaudio Fontana } 404e7f2670fSClaudio Fontana pte_addr = ((pde & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) << 3)) & 405e7f2670fSClaudio Fontana a20_mask; 406e7f2670fSClaudio Fontana pte_addr = get_hphys(cs, pte_addr, MMU_DATA_STORE, NULL); 407e7f2670fSClaudio Fontana pte = x86_ldq_phys(cs, pte_addr); 408e7f2670fSClaudio Fontana if (!(pte & PG_PRESENT_MASK)) { 409e7f2670fSClaudio Fontana goto do_fault; 410e7f2670fSClaudio Fontana } 411e7f2670fSClaudio Fontana if (pte & rsvd_mask) { 412e7f2670fSClaudio Fontana goto do_fault_rsvd; 413e7f2670fSClaudio Fontana } 414e7f2670fSClaudio Fontana /* combine pde and pte nx, user and rw protections */ 415e7f2670fSClaudio Fontana ptep &= pte ^ PG_NX_MASK; 416661ff487SPaolo Bonzini *page_size = 4096; 417e7f2670fSClaudio Fontana } else { 418e7f2670fSClaudio Fontana uint32_t pde; 419e7f2670fSClaudio Fontana 420e7f2670fSClaudio Fontana /* page directory entry */ 421cd906d31SPaolo Bonzini pde_addr = ((cr3 & ~0xfff) + ((addr >> 20) & 0xffc)) & 422e7f2670fSClaudio Fontana a20_mask; 423e7f2670fSClaudio Fontana pde_addr = get_hphys(cs, pde_addr, MMU_DATA_STORE, NULL); 424e7f2670fSClaudio Fontana pde = x86_ldl_phys(cs, pde_addr); 425e7f2670fSClaudio Fontana if (!(pde & PG_PRESENT_MASK)) { 426e7f2670fSClaudio Fontana goto do_fault; 427e7f2670fSClaudio Fontana } 428e7f2670fSClaudio Fontana ptep = pde | PG_NX_MASK; 429e7f2670fSClaudio Fontana 430e7f2670fSClaudio Fontana /* if PSE bit is set, then we use a 4MB page */ 431*31dd35ebSPaolo Bonzini if ((pde & PG_PSE_MASK) && (pg_mode & PG_MODE_PSE)) { 432661ff487SPaolo Bonzini *page_size = 4096 * 1024; 433e7f2670fSClaudio Fontana pte_addr = pde_addr; 434e7f2670fSClaudio Fontana 435e7f2670fSClaudio Fontana /* Bits 20-13 provide bits 39-32 of the address, bit 21 is reserved. 436e7f2670fSClaudio Fontana * Leave bits 20-13 in place for setting accessed/dirty bits below. 437e7f2670fSClaudio Fontana */ 438e7f2670fSClaudio Fontana pte = pde | ((pde & 0x1fe000LL) << (32 - 13)); 439e7f2670fSClaudio Fontana rsvd_mask = 0x200000; 440e7f2670fSClaudio Fontana goto do_check_protect_pse36; 441e7f2670fSClaudio Fontana } 442e7f2670fSClaudio Fontana 443e7f2670fSClaudio Fontana if (!(pde & PG_ACCESSED_MASK)) { 444e7f2670fSClaudio Fontana pde |= PG_ACCESSED_MASK; 445e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pde_addr, pde); 446e7f2670fSClaudio Fontana } 447e7f2670fSClaudio Fontana 448e7f2670fSClaudio Fontana /* page directory entry */ 449e7f2670fSClaudio Fontana pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & 450e7f2670fSClaudio Fontana a20_mask; 451e7f2670fSClaudio Fontana pte_addr = get_hphys(cs, pte_addr, MMU_DATA_STORE, NULL); 452e7f2670fSClaudio Fontana pte = x86_ldl_phys(cs, pte_addr); 453e7f2670fSClaudio Fontana if (!(pte & PG_PRESENT_MASK)) { 454e7f2670fSClaudio Fontana goto do_fault; 455e7f2670fSClaudio Fontana } 456e7f2670fSClaudio Fontana /* combine pde and pte user and rw protections */ 457e7f2670fSClaudio Fontana ptep &= pte | PG_NX_MASK; 458661ff487SPaolo Bonzini *page_size = 4096; 459e7f2670fSClaudio Fontana rsvd_mask = 0; 460e7f2670fSClaudio Fontana } 461e7f2670fSClaudio Fontana 462e7f2670fSClaudio Fontana do_check_protect: 463661ff487SPaolo Bonzini rsvd_mask |= (*page_size - 1) & PG_ADDRESS_MASK & ~PG_PSE_PAT_MASK; 464e7f2670fSClaudio Fontana do_check_protect_pse36: 465e7f2670fSClaudio Fontana if (pte & rsvd_mask) { 466e7f2670fSClaudio Fontana goto do_fault_rsvd; 467e7f2670fSClaudio Fontana } 468e7f2670fSClaudio Fontana ptep ^= PG_NX_MASK; 469e7f2670fSClaudio Fontana 470e7f2670fSClaudio Fontana /* can the page can be put in the TLB? prot will tell us */ 471e7f2670fSClaudio Fontana if (is_user && !(ptep & PG_USER_MASK)) { 472e7f2670fSClaudio Fontana goto do_fault_protect; 473e7f2670fSClaudio Fontana } 474e7f2670fSClaudio Fontana 475661ff487SPaolo Bonzini *prot = 0; 476e7f2670fSClaudio Fontana if (mmu_idx != MMU_KSMAP_IDX || !(ptep & PG_USER_MASK)) { 477661ff487SPaolo Bonzini *prot |= PAGE_READ; 478*31dd35ebSPaolo Bonzini if ((ptep & PG_RW_MASK) || !(is_user || (pg_mode & PG_MODE_WP))) { 479661ff487SPaolo Bonzini *prot |= PAGE_WRITE; 480e7f2670fSClaudio Fontana } 481e7f2670fSClaudio Fontana } 482e7f2670fSClaudio Fontana if (!(ptep & PG_NX_MASK) && 483e7f2670fSClaudio Fontana (mmu_idx == MMU_USER_IDX || 484*31dd35ebSPaolo Bonzini !((pg_mode & PG_MODE_SMEP) && (ptep & PG_USER_MASK)))) { 485661ff487SPaolo Bonzini *prot |= PAGE_EXEC; 486e7f2670fSClaudio Fontana } 487e7f2670fSClaudio Fontana 488e7f2670fSClaudio Fontana if (!(env->hflags & HF_LMA_MASK)) { 489e7f2670fSClaudio Fontana pkr = 0; 490e7f2670fSClaudio Fontana } else if (ptep & PG_USER_MASK) { 491*31dd35ebSPaolo Bonzini pkr = pg_mode & PG_MODE_PKE ? env->pkru : 0; 492e7f2670fSClaudio Fontana } else { 493*31dd35ebSPaolo Bonzini pkr = pg_mode & PG_MODE_PKS ? env->pkrs : 0; 494e7f2670fSClaudio Fontana } 495e7f2670fSClaudio Fontana if (pkr) { 496e7f2670fSClaudio Fontana uint32_t pk = (pte & PG_PKRU_MASK) >> PG_PKRU_BIT; 497e7f2670fSClaudio Fontana uint32_t pkr_ad = (pkr >> pk * 2) & 1; 498e7f2670fSClaudio Fontana uint32_t pkr_wd = (pkr >> pk * 2) & 2; 499e7f2670fSClaudio Fontana uint32_t pkr_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 500e7f2670fSClaudio Fontana 501e7f2670fSClaudio Fontana if (pkr_ad) { 502e7f2670fSClaudio Fontana pkr_prot &= ~(PAGE_READ | PAGE_WRITE); 503*31dd35ebSPaolo Bonzini } else if (pkr_wd && (is_user || (pg_mode & PG_MODE_WP))) { 504e7f2670fSClaudio Fontana pkr_prot &= ~PAGE_WRITE; 505e7f2670fSClaudio Fontana } 506e7f2670fSClaudio Fontana 507661ff487SPaolo Bonzini *prot &= pkr_prot; 508e7f2670fSClaudio Fontana if ((pkr_prot & (1 << is_write1)) == 0) { 509e7f2670fSClaudio Fontana assert(is_write1 != 2); 510e7f2670fSClaudio Fontana error_code |= PG_ERROR_PK_MASK; 511e7f2670fSClaudio Fontana goto do_fault_protect; 512e7f2670fSClaudio Fontana } 513e7f2670fSClaudio Fontana } 514e7f2670fSClaudio Fontana 515661ff487SPaolo Bonzini if ((*prot & (1 << is_write1)) == 0) { 516e7f2670fSClaudio Fontana goto do_fault_protect; 517e7f2670fSClaudio Fontana } 518e7f2670fSClaudio Fontana 519e7f2670fSClaudio Fontana /* yes, it can! */ 520e7f2670fSClaudio Fontana is_dirty = is_write && !(pte & PG_DIRTY_MASK); 521e7f2670fSClaudio Fontana if (!(pte & PG_ACCESSED_MASK) || is_dirty) { 522e7f2670fSClaudio Fontana pte |= PG_ACCESSED_MASK; 523e7f2670fSClaudio Fontana if (is_dirty) { 524e7f2670fSClaudio Fontana pte |= PG_DIRTY_MASK; 525e7f2670fSClaudio Fontana } 526e7f2670fSClaudio Fontana x86_stl_phys_notdirty(cs, pte_addr, pte); 527e7f2670fSClaudio Fontana } 528e7f2670fSClaudio Fontana 529e7f2670fSClaudio Fontana if (!(pte & PG_DIRTY_MASK)) { 530e7f2670fSClaudio Fontana /* only set write access if already dirty... otherwise wait 531e7f2670fSClaudio Fontana for dirty access */ 532e7f2670fSClaudio Fontana assert(!is_write); 533661ff487SPaolo Bonzini *prot &= ~PAGE_WRITE; 534e7f2670fSClaudio Fontana } 535e7f2670fSClaudio Fontana 536e7f2670fSClaudio Fontana pte = pte & a20_mask; 537e7f2670fSClaudio Fontana 538e7f2670fSClaudio Fontana /* align to page_size */ 539661ff487SPaolo Bonzini pte &= PG_ADDRESS_MASK & ~(*page_size - 1); 540661ff487SPaolo Bonzini page_offset = addr & (*page_size - 1); 541661ff487SPaolo Bonzini *xlat = get_hphys(cs, pte + page_offset, is_write1, prot); 542661ff487SPaolo Bonzini return PG_ERROR_OK; 543e7f2670fSClaudio Fontana 544e7f2670fSClaudio Fontana do_fault_rsvd: 545e7f2670fSClaudio Fontana error_code |= PG_ERROR_RSVD_MASK; 546e7f2670fSClaudio Fontana do_fault_protect: 547e7f2670fSClaudio Fontana error_code |= PG_ERROR_P_MASK; 548e7f2670fSClaudio Fontana do_fault: 549e7f2670fSClaudio Fontana error_code |= (is_write << PG_ERROR_W_BIT); 550e7f2670fSClaudio Fontana if (is_user) 551e7f2670fSClaudio Fontana error_code |= PG_ERROR_U_MASK; 552e7f2670fSClaudio Fontana if (is_write1 == 2 && 553*31dd35ebSPaolo Bonzini (((pg_mode & PG_MODE_NXE) && (pg_mode & PG_MODE_PAE)) || 554*31dd35ebSPaolo Bonzini (pg_mode & PG_MODE_SMEP))) 555e7f2670fSClaudio Fontana error_code |= PG_ERROR_I_D_MASK; 556661ff487SPaolo Bonzini return error_code; 557661ff487SPaolo Bonzini } 558661ff487SPaolo Bonzini 559661ff487SPaolo Bonzini /* return value: 560661ff487SPaolo Bonzini * -1 = cannot handle fault 561661ff487SPaolo Bonzini * 0 = nothing more to do 562661ff487SPaolo Bonzini * 1 = generate PF fault 563661ff487SPaolo Bonzini */ 564661ff487SPaolo Bonzini static int handle_mmu_fault(CPUState *cs, vaddr addr, int size, 565661ff487SPaolo Bonzini int is_write1, int mmu_idx) 566661ff487SPaolo Bonzini { 567661ff487SPaolo Bonzini X86CPU *cpu = X86_CPU(cs); 568661ff487SPaolo Bonzini CPUX86State *env = &cpu->env; 569661ff487SPaolo Bonzini int error_code = PG_ERROR_OK; 570*31dd35ebSPaolo Bonzini int pg_mode, prot, page_size; 571661ff487SPaolo Bonzini hwaddr paddr; 572661ff487SPaolo Bonzini target_ulong vaddr; 573661ff487SPaolo Bonzini 574661ff487SPaolo Bonzini #if defined(DEBUG_MMU) 575661ff487SPaolo Bonzini printf("MMU fault: addr=%" VADDR_PRIx " w=%d mmu=%d eip=" TARGET_FMT_lx "\n", 576661ff487SPaolo Bonzini addr, is_write1, mmu_idx, env->eip); 577661ff487SPaolo Bonzini #endif 578661ff487SPaolo Bonzini 579661ff487SPaolo Bonzini if (!(env->cr[0] & CR0_PG_MASK)) { 580661ff487SPaolo Bonzini paddr = addr; 581661ff487SPaolo Bonzini #ifdef TARGET_X86_64 582661ff487SPaolo Bonzini if (!(env->hflags & HF_LMA_MASK)) { 583661ff487SPaolo Bonzini /* Without long mode we can only address 32bits in real mode */ 584661ff487SPaolo Bonzini paddr = (uint32_t)paddr; 585661ff487SPaolo Bonzini } 586661ff487SPaolo Bonzini #endif 587661ff487SPaolo Bonzini prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 588661ff487SPaolo Bonzini page_size = 4096; 589661ff487SPaolo Bonzini } else { 590*31dd35ebSPaolo Bonzini pg_mode = get_pg_mode(env); 591cd906d31SPaolo Bonzini error_code = mmu_translate(cs, addr, env->cr[3], is_write1, 592*31dd35ebSPaolo Bonzini mmu_idx, pg_mode, 593661ff487SPaolo Bonzini &paddr, &page_size, &prot); 594661ff487SPaolo Bonzini } 595661ff487SPaolo Bonzini 596661ff487SPaolo Bonzini if (error_code == PG_ERROR_OK) { 597661ff487SPaolo Bonzini /* Even if 4MB pages, we map only one 4KB page in the cache to 598661ff487SPaolo Bonzini avoid filling it too fast */ 599661ff487SPaolo Bonzini vaddr = addr & TARGET_PAGE_MASK; 600661ff487SPaolo Bonzini paddr &= TARGET_PAGE_MASK; 601661ff487SPaolo Bonzini 602661ff487SPaolo Bonzini assert(prot & (1 << is_write1)); 603661ff487SPaolo Bonzini tlb_set_page_with_attrs(cs, vaddr, paddr, cpu_get_mem_attrs(env), 604661ff487SPaolo Bonzini prot, mmu_idx, page_size); 605661ff487SPaolo Bonzini return 0; 606661ff487SPaolo Bonzini } else { 607e7f2670fSClaudio Fontana if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) { 608e7f2670fSClaudio Fontana /* cr2 is not modified in case of exceptions */ 609e7f2670fSClaudio Fontana x86_stq_phys(cs, 610e7f2670fSClaudio Fontana env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 611e7f2670fSClaudio Fontana addr); 612e7f2670fSClaudio Fontana } else { 613e7f2670fSClaudio Fontana env->cr[2] = addr; 614e7f2670fSClaudio Fontana } 615e7f2670fSClaudio Fontana env->error_code = error_code; 616e7f2670fSClaudio Fontana cs->exception_index = EXCP0E_PAGE; 617e7f2670fSClaudio Fontana return 1; 618e7f2670fSClaudio Fontana } 619661ff487SPaolo Bonzini } 620e7f2670fSClaudio Fontana 621e7f2670fSClaudio Fontana bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, 622e7f2670fSClaudio Fontana MMUAccessType access_type, int mmu_idx, 623e7f2670fSClaudio Fontana bool probe, uintptr_t retaddr) 624e7f2670fSClaudio Fontana { 625e7f2670fSClaudio Fontana X86CPU *cpu = X86_CPU(cs); 626e7f2670fSClaudio Fontana CPUX86State *env = &cpu->env; 627e7f2670fSClaudio Fontana 628e7f2670fSClaudio Fontana env->retaddr = retaddr; 629e7f2670fSClaudio Fontana if (handle_mmu_fault(cs, addr, size, access_type, mmu_idx)) { 630e7f2670fSClaudio Fontana /* FIXME: On error in get_hphys we have already jumped out. */ 631e7f2670fSClaudio Fontana g_assert(!probe); 632e7f2670fSClaudio Fontana raise_exception_err_ra(env, cs->exception_index, 633e7f2670fSClaudio Fontana env->error_code, retaddr); 634e7f2670fSClaudio Fontana } 635e7f2670fSClaudio Fontana return true; 636e7f2670fSClaudio Fontana } 637