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" 2209b07f28SPhilippe Mathieu-Daudé #include "exec/cpu_ldst.h" 23b28b366dSPhilippe Mathieu-Daudé #include "exec/exec-all.h" 24e7f2670fSClaudio Fontana #include "tcg/helper-tcg.h" 25e7f2670fSClaudio Fontana 263563362dSRichard Henderson typedef struct TranslateParams { 273563362dSRichard Henderson target_ulong addr; 283563362dSRichard Henderson target_ulong cr3; 293563362dSRichard Henderson int pg_mode; 303563362dSRichard Henderson int mmu_idx; 314a1e9d4dSRichard Henderson int ptw_idx; 323563362dSRichard Henderson MMUAccessType access_type; 333563362dSRichard Henderson } TranslateParams; 343563362dSRichard Henderson 353563362dSRichard Henderson typedef struct TranslateResult { 363563362dSRichard Henderson hwaddr paddr; 373563362dSRichard Henderson int prot; 383563362dSRichard Henderson int page_size; 393563362dSRichard Henderson } TranslateResult; 403563362dSRichard Henderson 419bbcf372SRichard Henderson typedef enum TranslateFaultStage2 { 429bbcf372SRichard Henderson S2_NONE, 439bbcf372SRichard Henderson S2_GPA, 449bbcf372SRichard Henderson S2_GPT, 459bbcf372SRichard Henderson } TranslateFaultStage2; 469bbcf372SRichard Henderson 473563362dSRichard Henderson typedef struct TranslateFault { 483563362dSRichard Henderson int exception_index; 493563362dSRichard Henderson int error_code; 503563362dSRichard Henderson target_ulong cr2; 519bbcf372SRichard Henderson TranslateFaultStage2 stage2; 523563362dSRichard Henderson } TranslateFault; 53661ff487SPaolo Bonzini 544a1e9d4dSRichard Henderson typedef struct PTETranslate { 554a1e9d4dSRichard Henderson CPUX86State *env; 564a1e9d4dSRichard Henderson TranslateFault *err; 574a1e9d4dSRichard Henderson int ptw_idx; 584a1e9d4dSRichard Henderson void *haddr; 594a1e9d4dSRichard Henderson hwaddr gaddr; 604a1e9d4dSRichard Henderson } PTETranslate; 614a1e9d4dSRichard Henderson 624a1e9d4dSRichard Henderson static bool ptw_translate(PTETranslate *inout, hwaddr addr) 634a1e9d4dSRichard Henderson { 644a1e9d4dSRichard Henderson CPUTLBEntryFull *full; 654a1e9d4dSRichard Henderson int flags; 664a1e9d4dSRichard Henderson 674a1e9d4dSRichard Henderson inout->gaddr = addr; 68d507e6c5SRichard Henderson flags = probe_access_full(inout->env, addr, 0, MMU_DATA_STORE, 694a1e9d4dSRichard Henderson inout->ptw_idx, true, &inout->haddr, &full, 0); 704a1e9d4dSRichard Henderson 714a1e9d4dSRichard Henderson if (unlikely(flags & TLB_INVALID_MASK)) { 724a1e9d4dSRichard Henderson TranslateFault *err = inout->err; 734a1e9d4dSRichard Henderson 744a1e9d4dSRichard Henderson assert(inout->ptw_idx == MMU_NESTED_IDX); 758218c048SRichard Henderson *err = (TranslateFault){ 768218c048SRichard Henderson .error_code = inout->env->error_code, 778218c048SRichard Henderson .cr2 = addr, 788218c048SRichard Henderson .stage2 = S2_GPT, 798218c048SRichard Henderson }; 804a1e9d4dSRichard Henderson return false; 814a1e9d4dSRichard Henderson } 824a1e9d4dSRichard Henderson return true; 834a1e9d4dSRichard Henderson } 844a1e9d4dSRichard Henderson 854a1e9d4dSRichard Henderson static inline uint32_t ptw_ldl(const PTETranslate *in) 864a1e9d4dSRichard Henderson { 874a1e9d4dSRichard Henderson if (likely(in->haddr)) { 884a1e9d4dSRichard Henderson return ldl_p(in->haddr); 894a1e9d4dSRichard Henderson } 904a1e9d4dSRichard Henderson return cpu_ldl_mmuidx_ra(in->env, in->gaddr, in->ptw_idx, 0); 914a1e9d4dSRichard Henderson } 924a1e9d4dSRichard Henderson 934a1e9d4dSRichard Henderson static inline uint64_t ptw_ldq(const PTETranslate *in) 944a1e9d4dSRichard Henderson { 954a1e9d4dSRichard Henderson if (likely(in->haddr)) { 964a1e9d4dSRichard Henderson return ldq_p(in->haddr); 974a1e9d4dSRichard Henderson } 984a1e9d4dSRichard Henderson return cpu_ldq_mmuidx_ra(in->env, in->gaddr, in->ptw_idx, 0); 994a1e9d4dSRichard Henderson } 1004a1e9d4dSRichard Henderson 1014a1e9d4dSRichard Henderson /* 1024a1e9d4dSRichard Henderson * Note that we can use a 32-bit cmpxchg for all page table entries, 1034a1e9d4dSRichard Henderson * even 64-bit ones, because PG_PRESENT_MASK, PG_ACCESSED_MASK and 1044a1e9d4dSRichard Henderson * PG_DIRTY_MASK are all in the low 32 bits. 1054a1e9d4dSRichard Henderson */ 1064a1e9d4dSRichard Henderson static bool ptw_setl_slow(const PTETranslate *in, uint32_t old, uint32_t new) 1074a1e9d4dSRichard Henderson { 1084a1e9d4dSRichard Henderson uint32_t cmp; 1094a1e9d4dSRichard Henderson 1104a1e9d4dSRichard Henderson /* Does x86 really perform a rmw cycle on mmio for ptw? */ 1114a1e9d4dSRichard Henderson start_exclusive(); 1124a1e9d4dSRichard Henderson cmp = cpu_ldl_mmuidx_ra(in->env, in->gaddr, in->ptw_idx, 0); 1134a1e9d4dSRichard Henderson if (cmp == old) { 1144a1e9d4dSRichard Henderson cpu_stl_mmuidx_ra(in->env, in->gaddr, new, in->ptw_idx, 0); 1154a1e9d4dSRichard Henderson } 1164a1e9d4dSRichard Henderson end_exclusive(); 1174a1e9d4dSRichard Henderson return cmp == old; 1184a1e9d4dSRichard Henderson } 1194a1e9d4dSRichard Henderson 1204a1e9d4dSRichard Henderson static inline bool ptw_setl(const PTETranslate *in, uint32_t old, uint32_t set) 1214a1e9d4dSRichard Henderson { 1224a1e9d4dSRichard Henderson if (set & ~old) { 1234a1e9d4dSRichard Henderson uint32_t new = old | set; 1244a1e9d4dSRichard Henderson if (likely(in->haddr)) { 1254a1e9d4dSRichard Henderson old = cpu_to_le32(old); 1264a1e9d4dSRichard Henderson new = cpu_to_le32(new); 1274a1e9d4dSRichard Henderson return qatomic_cmpxchg((uint32_t *)in->haddr, old, new) == old; 1284a1e9d4dSRichard Henderson } 1294a1e9d4dSRichard Henderson return ptw_setl_slow(in, old, new); 1304a1e9d4dSRichard Henderson } 1314a1e9d4dSRichard Henderson return true; 1324a1e9d4dSRichard Henderson } 13333ce155cSPaolo Bonzini 1343563362dSRichard Henderson static bool mmu_translate(CPUX86State *env, const TranslateParams *in, 1353563362dSRichard Henderson TranslateResult *out, TranslateFault *err) 136e7f2670fSClaudio Fontana { 1373563362dSRichard Henderson const target_ulong addr = in->addr; 1383563362dSRichard Henderson const int pg_mode = in->pg_mode; 1395f97afe2SPaolo Bonzini const bool is_user = is_mmu_index_user(in->mmu_idx); 1403563362dSRichard Henderson const MMUAccessType access_type = in->access_type; 1414a1e9d4dSRichard Henderson uint64_t ptep, pte, rsvd_mask; 1424a1e9d4dSRichard Henderson PTETranslate pte_trans = { 1434a1e9d4dSRichard Henderson .env = env, 1444a1e9d4dSRichard Henderson .err = err, 1454a1e9d4dSRichard Henderson .ptw_idx = in->ptw_idx, 1464a1e9d4dSRichard Henderson }; 1478629e77bSRichard Henderson hwaddr pte_addr, paddr; 148e7f2670fSClaudio Fontana uint32_t pkr; 1493563362dSRichard Henderson int page_size; 150987b63f2SPeter Maydell int error_code; 151e7f2670fSClaudio Fontana 1524a1e9d4dSRichard Henderson restart_all: 1534a1e9d4dSRichard Henderson rsvd_mask = ~MAKE_64BIT_MASK(0, env_archcpu(env)->phys_bits); 1544a1e9d4dSRichard Henderson rsvd_mask &= PG_ADDRESS_MASK; 15531dd35ebSPaolo Bonzini if (!(pg_mode & PG_MODE_NXE)) { 156e7f2670fSClaudio Fontana rsvd_mask |= PG_NX_MASK; 157e7f2670fSClaudio Fontana } 158e7f2670fSClaudio Fontana 15931dd35ebSPaolo Bonzini if (pg_mode & PG_MODE_PAE) { 160e7f2670fSClaudio Fontana #ifdef TARGET_X86_64 16193eae358SPaolo Bonzini if (pg_mode & PG_MODE_LMA) { 16211b4e971SRichard Henderson if (pg_mode & PG_MODE_LA57) { 16311b4e971SRichard Henderson /* 16411b4e971SRichard Henderson * Page table level 5 16511b4e971SRichard Henderson */ 166a28fe7dcSPaolo Bonzini pte_addr = (in->cr3 & ~0xfff) + (((addr >> 48) & 0x1ff) << 3); 1674a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 1684a1e9d4dSRichard Henderson return false; 1694a1e9d4dSRichard Henderson } 1704a1e9d4dSRichard Henderson restart_5: 1714a1e9d4dSRichard Henderson pte = ptw_ldq(&pte_trans); 17211b4e971SRichard Henderson if (!(pte & PG_PRESENT_MASK)) { 173e7f2670fSClaudio Fontana goto do_fault; 174e7f2670fSClaudio Fontana } 17511b4e971SRichard Henderson if (pte & (rsvd_mask | PG_PSE_MASK)) { 176e7f2670fSClaudio Fontana goto do_fault_rsvd; 177e7f2670fSClaudio Fontana } 1784a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) { 1794a1e9d4dSRichard Henderson goto restart_5; 180e7f2670fSClaudio Fontana } 18111b4e971SRichard Henderson ptep = pte ^ PG_NX_MASK; 182e7f2670fSClaudio Fontana } else { 18311b4e971SRichard Henderson pte = in->cr3; 184e7f2670fSClaudio Fontana ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; 185e7f2670fSClaudio Fontana } 186e7f2670fSClaudio Fontana 18711b4e971SRichard Henderson /* 18811b4e971SRichard Henderson * Page table level 4 18911b4e971SRichard Henderson */ 190a28fe7dcSPaolo Bonzini pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 39) & 0x1ff) << 3); 1914a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 1924a1e9d4dSRichard Henderson return false; 1934a1e9d4dSRichard Henderson } 1944a1e9d4dSRichard Henderson restart_4: 1954a1e9d4dSRichard Henderson pte = ptw_ldq(&pte_trans); 19611b4e971SRichard Henderson if (!(pte & PG_PRESENT_MASK)) { 197e7f2670fSClaudio Fontana goto do_fault; 198e7f2670fSClaudio Fontana } 19911b4e971SRichard Henderson if (pte & (rsvd_mask | PG_PSE_MASK)) { 200e7f2670fSClaudio Fontana goto do_fault_rsvd; 201e7f2670fSClaudio Fontana } 2024a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) { 2034a1e9d4dSRichard Henderson goto restart_4; 204e7f2670fSClaudio Fontana } 20511b4e971SRichard Henderson ptep &= pte ^ PG_NX_MASK; 20611b4e971SRichard Henderson 20711b4e971SRichard Henderson /* 20811b4e971SRichard Henderson * Page table level 3 20911b4e971SRichard Henderson */ 210a28fe7dcSPaolo Bonzini pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 30) & 0x1ff) << 3); 2114a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 2124a1e9d4dSRichard Henderson return false; 2134a1e9d4dSRichard Henderson } 2144a1e9d4dSRichard Henderson restart_3_lma: 2154a1e9d4dSRichard Henderson pte = ptw_ldq(&pte_trans); 21611b4e971SRichard Henderson if (!(pte & PG_PRESENT_MASK)) { 217e7f2670fSClaudio Fontana goto do_fault; 218e7f2670fSClaudio Fontana } 21911b4e971SRichard Henderson if (pte & rsvd_mask) { 220e7f2670fSClaudio Fontana goto do_fault_rsvd; 221e7f2670fSClaudio Fontana } 2224a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) { 2234a1e9d4dSRichard Henderson goto restart_3_lma; 224e7f2670fSClaudio Fontana } 2254a1e9d4dSRichard Henderson ptep &= pte ^ PG_NX_MASK; 22611b4e971SRichard Henderson if (pte & PG_PSE_MASK) { 227e7f2670fSClaudio Fontana /* 1 GB page */ 2283563362dSRichard Henderson page_size = 1024 * 1024 * 1024; 229e7f2670fSClaudio Fontana goto do_check_protect; 230e7f2670fSClaudio Fontana } 231e7f2670fSClaudio Fontana } else 232e7f2670fSClaudio Fontana #endif 233e7f2670fSClaudio Fontana { 23411b4e971SRichard Henderson /* 23511b4e971SRichard Henderson * Page table level 3 23611b4e971SRichard Henderson */ 237a28fe7dcSPaolo Bonzini pte_addr = (in->cr3 & 0xffffffe0ULL) + ((addr >> 27) & 0x18); 2384a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 2394a1e9d4dSRichard Henderson return false; 2404a1e9d4dSRichard Henderson } 2414a1e9d4dSRichard Henderson rsvd_mask |= PG_HI_USER_MASK; 2424a1e9d4dSRichard Henderson restart_3_nolma: 2434a1e9d4dSRichard Henderson pte = ptw_ldq(&pte_trans); 24411b4e971SRichard Henderson if (!(pte & PG_PRESENT_MASK)) { 245e7f2670fSClaudio Fontana goto do_fault; 246e7f2670fSClaudio Fontana } 24711b4e971SRichard Henderson if (pte & (rsvd_mask | PG_NX_MASK)) { 248e7f2670fSClaudio Fontana goto do_fault_rsvd; 249e7f2670fSClaudio Fontana } 2504a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) { 2514a1e9d4dSRichard Henderson goto restart_3_nolma; 2524a1e9d4dSRichard Henderson } 253e7f2670fSClaudio Fontana ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; 254e7f2670fSClaudio Fontana } 255e7f2670fSClaudio Fontana 25611b4e971SRichard Henderson /* 25711b4e971SRichard Henderson * Page table level 2 25811b4e971SRichard Henderson */ 259a28fe7dcSPaolo Bonzini pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) << 3); 2604a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 2614a1e9d4dSRichard Henderson return false; 2624a1e9d4dSRichard Henderson } 2634a1e9d4dSRichard Henderson restart_2_pae: 2644a1e9d4dSRichard Henderson pte = ptw_ldq(&pte_trans); 26511b4e971SRichard Henderson if (!(pte & PG_PRESENT_MASK)) { 266e7f2670fSClaudio Fontana goto do_fault; 267e7f2670fSClaudio Fontana } 26811b4e971SRichard Henderson if (pte & rsvd_mask) { 269e7f2670fSClaudio Fontana goto do_fault_rsvd; 270e7f2670fSClaudio Fontana } 27111b4e971SRichard Henderson if (pte & PG_PSE_MASK) { 272e7f2670fSClaudio Fontana /* 2 MB page */ 2733563362dSRichard Henderson page_size = 2048 * 1024; 2744a1e9d4dSRichard Henderson ptep &= pte ^ PG_NX_MASK; 275e7f2670fSClaudio Fontana goto do_check_protect; 276e7f2670fSClaudio Fontana } 2774a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) { 2784a1e9d4dSRichard Henderson goto restart_2_pae; 279e7f2670fSClaudio Fontana } 2804a1e9d4dSRichard Henderson ptep &= pte ^ PG_NX_MASK; 28111b4e971SRichard Henderson 28211b4e971SRichard Henderson /* 28311b4e971SRichard Henderson * Page table level 1 28411b4e971SRichard Henderson */ 285a28fe7dcSPaolo Bonzini pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) << 3); 2864a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 2874a1e9d4dSRichard Henderson return false; 2884a1e9d4dSRichard Henderson } 2894a1e9d4dSRichard Henderson pte = ptw_ldq(&pte_trans); 290e7f2670fSClaudio Fontana if (!(pte & PG_PRESENT_MASK)) { 291e7f2670fSClaudio Fontana goto do_fault; 292e7f2670fSClaudio Fontana } 293e7f2670fSClaudio Fontana if (pte & rsvd_mask) { 294e7f2670fSClaudio Fontana goto do_fault_rsvd; 295e7f2670fSClaudio Fontana } 296e7f2670fSClaudio Fontana /* combine pde and pte nx, user and rw protections */ 297e7f2670fSClaudio Fontana ptep &= pte ^ PG_NX_MASK; 2983563362dSRichard Henderson page_size = 4096; 299e7f2670fSClaudio Fontana } else { 30011b4e971SRichard Henderson /* 30111b4e971SRichard Henderson * Page table level 2 30211b4e971SRichard Henderson */ 303a28fe7dcSPaolo Bonzini pte_addr = (in->cr3 & 0xfffff000ULL) + ((addr >> 20) & 0xffc); 3044a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 3054a1e9d4dSRichard Henderson return false; 3064a1e9d4dSRichard Henderson } 3074a1e9d4dSRichard Henderson restart_2_nopae: 3084a1e9d4dSRichard Henderson pte = ptw_ldl(&pte_trans); 30911b4e971SRichard Henderson if (!(pte & PG_PRESENT_MASK)) { 310e7f2670fSClaudio Fontana goto do_fault; 311e7f2670fSClaudio Fontana } 31211b4e971SRichard Henderson ptep = pte | PG_NX_MASK; 313e7f2670fSClaudio Fontana 314e7f2670fSClaudio Fontana /* if PSE bit is set, then we use a 4MB page */ 31511b4e971SRichard Henderson if ((pte & PG_PSE_MASK) && (pg_mode & PG_MODE_PSE)) { 3163563362dSRichard Henderson page_size = 4096 * 1024; 31711b4e971SRichard Henderson /* 31811b4e971SRichard Henderson * Bits 20-13 provide bits 39-32 of the address, bit 21 is reserved. 319e7f2670fSClaudio Fontana * Leave bits 20-13 in place for setting accessed/dirty bits below. 320e7f2670fSClaudio Fontana */ 32111b4e971SRichard Henderson pte = (uint32_t)pte | ((pte & 0x1fe000LL) << (32 - 13)); 322e7f2670fSClaudio Fontana rsvd_mask = 0x200000; 323e7f2670fSClaudio Fontana goto do_check_protect_pse36; 324e7f2670fSClaudio Fontana } 3254a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) { 3264a1e9d4dSRichard Henderson goto restart_2_nopae; 327e7f2670fSClaudio Fontana } 328e7f2670fSClaudio Fontana 32911b4e971SRichard Henderson /* 33011b4e971SRichard Henderson * Page table level 1 33111b4e971SRichard Henderson */ 332a28fe7dcSPaolo Bonzini pte_addr = (pte & ~0xfffu) + ((addr >> 10) & 0xffc); 3334a1e9d4dSRichard Henderson if (!ptw_translate(&pte_trans, pte_addr)) { 3344a1e9d4dSRichard Henderson return false; 3354a1e9d4dSRichard Henderson } 3364a1e9d4dSRichard Henderson pte = ptw_ldl(&pte_trans); 337e7f2670fSClaudio Fontana if (!(pte & PG_PRESENT_MASK)) { 338e7f2670fSClaudio Fontana goto do_fault; 339e7f2670fSClaudio Fontana } 340e7f2670fSClaudio Fontana /* combine pde and pte user and rw protections */ 341e7f2670fSClaudio Fontana ptep &= pte | PG_NX_MASK; 3423563362dSRichard Henderson page_size = 4096; 343e7f2670fSClaudio Fontana rsvd_mask = 0; 344e7f2670fSClaudio Fontana } 345e7f2670fSClaudio Fontana 346e7f2670fSClaudio Fontana do_check_protect: 3473563362dSRichard Henderson rsvd_mask |= (page_size - 1) & PG_ADDRESS_MASK & ~PG_PSE_PAT_MASK; 348e7f2670fSClaudio Fontana do_check_protect_pse36: 349e7f2670fSClaudio Fontana if (pte & rsvd_mask) { 350e7f2670fSClaudio Fontana goto do_fault_rsvd; 351e7f2670fSClaudio Fontana } 352e7f2670fSClaudio Fontana ptep ^= PG_NX_MASK; 353e7f2670fSClaudio Fontana 354e7f2670fSClaudio Fontana /* can the page can be put in the TLB? prot will tell us */ 355e7f2670fSClaudio Fontana if (is_user && !(ptep & PG_USER_MASK)) { 356e7f2670fSClaudio Fontana goto do_fault_protect; 357e7f2670fSClaudio Fontana } 358e7f2670fSClaudio Fontana 3593563362dSRichard Henderson int prot = 0; 3605f97afe2SPaolo Bonzini if (!is_mmu_index_smap(in->mmu_idx) || !(ptep & PG_USER_MASK)) { 3613563362dSRichard Henderson prot |= PAGE_READ; 36231dd35ebSPaolo Bonzini if ((ptep & PG_RW_MASK) || !(is_user || (pg_mode & PG_MODE_WP))) { 3633563362dSRichard Henderson prot |= PAGE_WRITE; 364e7f2670fSClaudio Fontana } 365e7f2670fSClaudio Fontana } 366e7f2670fSClaudio Fontana if (!(ptep & PG_NX_MASK) && 3673563362dSRichard Henderson (is_user || 36831dd35ebSPaolo Bonzini !((pg_mode & PG_MODE_SMEP) && (ptep & PG_USER_MASK)))) { 3693563362dSRichard Henderson prot |= PAGE_EXEC; 370e7f2670fSClaudio Fontana } 371e7f2670fSClaudio Fontana 372991ec976SPaolo Bonzini if (ptep & PG_USER_MASK) { 37331dd35ebSPaolo Bonzini pkr = pg_mode & PG_MODE_PKE ? env->pkru : 0; 374e7f2670fSClaudio Fontana } else { 37531dd35ebSPaolo Bonzini pkr = pg_mode & PG_MODE_PKS ? env->pkrs : 0; 376e7f2670fSClaudio Fontana } 377e7f2670fSClaudio Fontana if (pkr) { 378e7f2670fSClaudio Fontana uint32_t pk = (pte & PG_PKRU_MASK) >> PG_PKRU_BIT; 379e7f2670fSClaudio Fontana uint32_t pkr_ad = (pkr >> pk * 2) & 1; 380e7f2670fSClaudio Fontana uint32_t pkr_wd = (pkr >> pk * 2) & 2; 381e7f2670fSClaudio Fontana uint32_t pkr_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 382e7f2670fSClaudio Fontana 383e7f2670fSClaudio Fontana if (pkr_ad) { 384e7f2670fSClaudio Fontana pkr_prot &= ~(PAGE_READ | PAGE_WRITE); 38531dd35ebSPaolo Bonzini } else if (pkr_wd && (is_user || (pg_mode & PG_MODE_WP))) { 386e7f2670fSClaudio Fontana pkr_prot &= ~PAGE_WRITE; 387e7f2670fSClaudio Fontana } 388487d1133SRichard Henderson if ((pkr_prot & (1 << access_type)) == 0) { 3893563362dSRichard Henderson goto do_fault_pk_protect; 390e7f2670fSClaudio Fontana } 3913563362dSRichard Henderson prot &= pkr_prot; 392e7f2670fSClaudio Fontana } 393e7f2670fSClaudio Fontana 3943563362dSRichard Henderson if ((prot & (1 << access_type)) == 0) { 395e7f2670fSClaudio Fontana goto do_fault_protect; 396e7f2670fSClaudio Fontana } 397e7f2670fSClaudio Fontana 398e7f2670fSClaudio Fontana /* yes, it can! */ 3993563362dSRichard Henderson { 4003563362dSRichard Henderson uint32_t set = PG_ACCESSED_MASK; 4013563362dSRichard Henderson if (access_type == MMU_DATA_STORE) { 4023563362dSRichard Henderson set |= PG_DIRTY_MASK; 4034a1e9d4dSRichard Henderson } else if (!(pte & PG_DIRTY_MASK)) { 4044a1e9d4dSRichard Henderson /* 4054a1e9d4dSRichard Henderson * Only set write access if already dirty... 4064a1e9d4dSRichard Henderson * otherwise wait for dirty access. 4074a1e9d4dSRichard Henderson */ 4083563362dSRichard Henderson prot &= ~PAGE_WRITE; 409e7f2670fSClaudio Fontana } 4104a1e9d4dSRichard Henderson if (!ptw_setl(&pte_trans, pte, set)) { 4114a1e9d4dSRichard Henderson /* 4124a1e9d4dSRichard Henderson * We can arrive here from any of 3 levels and 2 formats. 4134a1e9d4dSRichard Henderson * The only safe thing is to restart the entire lookup. 4144a1e9d4dSRichard Henderson */ 4154a1e9d4dSRichard Henderson goto restart_all; 4164a1e9d4dSRichard Henderson } 4174a1e9d4dSRichard Henderson } 418e7f2670fSClaudio Fontana 419*b5a9de32SPaolo Bonzini /* merge offset within page */ 420*b5a9de32SPaolo Bonzini paddr = (pte & PG_ADDRESS_MASK & ~(page_size - 1)) | (addr & (page_size - 1)); 4219bbcf372SRichard Henderson 422*b5a9de32SPaolo Bonzini /* 423*b5a9de32SPaolo Bonzini * Note that NPT is walked (for both paging structures and final guest 424*b5a9de32SPaolo Bonzini * addresses) using the address with the A20 bit set. 425*b5a9de32SPaolo Bonzini */ 4264a1e9d4dSRichard Henderson if (in->ptw_idx == MMU_NESTED_IDX) { 4278629e77bSRichard Henderson CPUTLBEntryFull *full; 4288629e77bSRichard Henderson int flags, nested_page_size; 4299bbcf372SRichard Henderson 430d507e6c5SRichard Henderson flags = probe_access_full(env, paddr, 0, access_type, 4318629e77bSRichard Henderson MMU_NESTED_IDX, true, 4328629e77bSRichard Henderson &pte_trans.haddr, &full, 0); 4338629e77bSRichard Henderson if (unlikely(flags & TLB_INVALID_MASK)) { 4348218c048SRichard Henderson *err = (TranslateFault){ 4358218c048SRichard Henderson .error_code = env->error_code, 4368218c048SRichard Henderson .cr2 = paddr, 4378218c048SRichard Henderson .stage2 = S2_GPA, 4388218c048SRichard Henderson }; 4399bbcf372SRichard Henderson return false; 4409bbcf372SRichard Henderson } 4419bbcf372SRichard Henderson 4429bbcf372SRichard Henderson /* Merge stage1 & stage2 protection bits. */ 4438629e77bSRichard Henderson prot &= full->prot; 4449bbcf372SRichard Henderson 4459bbcf372SRichard Henderson /* Re-verify resulting protection. */ 4469bbcf372SRichard Henderson if ((prot & (1 << access_type)) == 0) { 4479bbcf372SRichard Henderson goto do_fault_protect; 4489bbcf372SRichard Henderson } 4498629e77bSRichard Henderson 4508629e77bSRichard Henderson /* Merge stage1 & stage2 addresses to final physical address. */ 4518629e77bSRichard Henderson nested_page_size = 1 << full->lg_page_size; 4528629e77bSRichard Henderson paddr = (full->phys_addr & ~(nested_page_size - 1)) 4538629e77bSRichard Henderson | (paddr & (nested_page_size - 1)); 4548629e77bSRichard Henderson 4558629e77bSRichard Henderson /* 4568629e77bSRichard Henderson * Use the larger of stage1 & stage2 page sizes, so that 4578629e77bSRichard Henderson * invalidation works. 4588629e77bSRichard Henderson */ 4598629e77bSRichard Henderson if (nested_page_size > page_size) { 4608629e77bSRichard Henderson page_size = nested_page_size; 4618629e77bSRichard Henderson } 4629bbcf372SRichard Henderson } 4639bbcf372SRichard Henderson 464*b5a9de32SPaolo Bonzini out->paddr = paddr & x86_get_a20_mask(env); 4659bbcf372SRichard Henderson out->prot = prot; 4669bbcf372SRichard Henderson out->page_size = page_size; 4673563362dSRichard Henderson return true; 468e7f2670fSClaudio Fontana 469e7f2670fSClaudio Fontana do_fault_rsvd: 4703563362dSRichard Henderson error_code = PG_ERROR_RSVD_MASK; 4713563362dSRichard Henderson goto do_fault_cont; 472e7f2670fSClaudio Fontana do_fault_protect: 4733563362dSRichard Henderson error_code = PG_ERROR_P_MASK; 4743563362dSRichard Henderson goto do_fault_cont; 4753563362dSRichard Henderson do_fault_pk_protect: 4763563362dSRichard Henderson assert(access_type != MMU_INST_FETCH); 4773563362dSRichard Henderson error_code = PG_ERROR_PK_MASK | PG_ERROR_P_MASK; 4783563362dSRichard Henderson goto do_fault_cont; 479e7f2670fSClaudio Fontana do_fault: 4803563362dSRichard Henderson error_code = 0; 4813563362dSRichard Henderson do_fault_cont: 4823563362dSRichard Henderson if (is_user) { 483e7f2670fSClaudio Fontana error_code |= PG_ERROR_U_MASK; 4843563362dSRichard Henderson } 4853563362dSRichard Henderson switch (access_type) { 4863563362dSRichard Henderson case MMU_DATA_LOAD: 4873563362dSRichard Henderson break; 4883563362dSRichard Henderson case MMU_DATA_STORE: 4893563362dSRichard Henderson error_code |= PG_ERROR_W_MASK; 4903563362dSRichard Henderson break; 4913563362dSRichard Henderson case MMU_INST_FETCH: 4923563362dSRichard Henderson if (pg_mode & (PG_MODE_NXE | PG_MODE_SMEP)) { 493e7f2670fSClaudio Fontana error_code |= PG_ERROR_I_D_MASK; 4943563362dSRichard Henderson } 4953563362dSRichard Henderson break; 4963563362dSRichard Henderson } 4978218c048SRichard Henderson *err = (TranslateFault){ 4988218c048SRichard Henderson .exception_index = EXCP0E_PAGE, 4998218c048SRichard Henderson .error_code = error_code, 5008218c048SRichard Henderson .cr2 = addr, 5018218c048SRichard Henderson }; 5023563362dSRichard Henderson return false; 503661ff487SPaolo Bonzini } 504661ff487SPaolo Bonzini 5059bbcf372SRichard Henderson static G_NORETURN void raise_stage2(CPUX86State *env, TranslateFault *err, 5069bbcf372SRichard Henderson uintptr_t retaddr) 5079bbcf372SRichard Henderson { 5089bbcf372SRichard Henderson uint64_t exit_info_1 = err->error_code; 5099bbcf372SRichard Henderson 5109bbcf372SRichard Henderson switch (err->stage2) { 5119bbcf372SRichard Henderson case S2_GPT: 5129bbcf372SRichard Henderson exit_info_1 |= SVM_NPTEXIT_GPT; 5139bbcf372SRichard Henderson break; 5149bbcf372SRichard Henderson case S2_GPA: 5159bbcf372SRichard Henderson exit_info_1 |= SVM_NPTEXIT_GPA; 5169bbcf372SRichard Henderson break; 5179bbcf372SRichard Henderson default: 5189bbcf372SRichard Henderson g_assert_not_reached(); 5199bbcf372SRichard Henderson } 5209bbcf372SRichard Henderson 5219bbcf372SRichard Henderson x86_stq_phys(env_cpu(env), 5229bbcf372SRichard Henderson env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 5239bbcf372SRichard Henderson err->cr2); 5249bbcf372SRichard Henderson cpu_vmexit(env, SVM_EXIT_NPF, exit_info_1, retaddr); 5259bbcf372SRichard Henderson } 5269bbcf372SRichard Henderson 5273563362dSRichard Henderson static bool get_physical_address(CPUX86State *env, vaddr addr, 5283563362dSRichard Henderson MMUAccessType access_type, int mmu_idx, 5293563362dSRichard Henderson TranslateResult *out, TranslateFault *err) 530661ff487SPaolo Bonzini { 53198281984SRichard Henderson TranslateParams in; 53298281984SRichard Henderson bool use_stage2 = env->hflags2 & HF2_NPT_MASK; 5333563362dSRichard Henderson 53498281984SRichard Henderson in.addr = addr; 53598281984SRichard Henderson in.access_type = access_type; 53698281984SRichard Henderson 53798281984SRichard Henderson switch (mmu_idx) { 53898281984SRichard Henderson case MMU_PHYS_IDX: 53998281984SRichard Henderson break; 54098281984SRichard Henderson 54198281984SRichard Henderson case MMU_NESTED_IDX: 54298281984SRichard Henderson if (likely(use_stage2)) { 54398281984SRichard Henderson in.cr3 = env->nested_cr3; 54498281984SRichard Henderson in.pg_mode = env->nested_pg_mode; 54590f64153SPaolo Bonzini in.mmu_idx = 54690f64153SPaolo Bonzini env->nested_pg_mode & PG_MODE_LMA ? MMU_USER64_IDX : MMU_USER32_IDX; 5474a1e9d4dSRichard Henderson in.ptw_idx = MMU_PHYS_IDX; 54898281984SRichard Henderson 54998281984SRichard Henderson if (!mmu_translate(env, &in, out, err)) { 55098281984SRichard Henderson err->stage2 = S2_GPA; 55198281984SRichard Henderson return false; 552661ff487SPaolo Bonzini } 5533563362dSRichard Henderson return true; 55498281984SRichard Henderson } 55598281984SRichard Henderson break; 556b04dc92eSPaolo Bonzini 55798281984SRichard Henderson default: 558b1661801SPaolo Bonzini if (is_mmu_index_32(mmu_idx)) { 559b1661801SPaolo Bonzini addr = (uint32_t)addr; 560b1661801SPaolo Bonzini } 561b1661801SPaolo Bonzini 56203a60ae9SRichard Henderson if (likely(env->cr[0] & CR0_PG_MASK)) { 56398281984SRichard Henderson in.cr3 = env->cr[3]; 56498281984SRichard Henderson in.mmu_idx = mmu_idx; 5654a1e9d4dSRichard Henderson in.ptw_idx = use_stage2 ? MMU_NESTED_IDX : MMU_PHYS_IDX; 56698281984SRichard Henderson in.pg_mode = get_pg_mode(env); 56798281984SRichard Henderson 5683563362dSRichard Henderson if (in.pg_mode & PG_MODE_LMA) { 569b04dc92eSPaolo Bonzini /* test virtual address sign extension */ 5703563362dSRichard Henderson int shift = in.pg_mode & PG_MODE_LA57 ? 56 : 47; 5713563362dSRichard Henderson int64_t sext = (int64_t)addr >> shift; 572b04dc92eSPaolo Bonzini if (sext != 0 && sext != -1) { 5738218c048SRichard Henderson *err = (TranslateFault){ 5748218c048SRichard Henderson .exception_index = EXCP0D_GPF, 5758218c048SRichard Henderson .cr2 = addr, 5768218c048SRichard Henderson }; 5773563362dSRichard Henderson return false; 578b04dc92eSPaolo Bonzini } 579b04dc92eSPaolo Bonzini } 5803563362dSRichard Henderson return mmu_translate(env, &in, out, err); 581e7f2670fSClaudio Fontana } 58298281984SRichard Henderson break; 58398281984SRichard Henderson } 58498281984SRichard Henderson 585b1661801SPaolo Bonzini /* No translation needed. */ 58698281984SRichard Henderson out->paddr = addr & x86_get_a20_mask(env); 58798281984SRichard Henderson out->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 58898281984SRichard Henderson out->page_size = TARGET_PAGE_SIZE; 58998281984SRichard Henderson return true; 590661ff487SPaolo Bonzini } 591e7f2670fSClaudio Fontana 592e7f2670fSClaudio Fontana bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, 593e7f2670fSClaudio Fontana MMUAccessType access_type, int mmu_idx, 594e7f2670fSClaudio Fontana bool probe, uintptr_t retaddr) 595e7f2670fSClaudio Fontana { 596b77af26eSRichard Henderson CPUX86State *env = cpu_env(cs); 5973563362dSRichard Henderson TranslateResult out; 5983563362dSRichard Henderson TranslateFault err; 599e7f2670fSClaudio Fontana 6003563362dSRichard Henderson if (get_physical_address(env, addr, access_type, mmu_idx, &out, &err)) { 6013563362dSRichard Henderson /* 6023563362dSRichard Henderson * Even if 4MB pages, we map only one 4KB page in the cache to 6033563362dSRichard Henderson * avoid filling it too fast. 6043563362dSRichard Henderson */ 6053563362dSRichard Henderson assert(out.prot & (1 << access_type)); 6063563362dSRichard Henderson tlb_set_page_with_attrs(cs, addr & TARGET_PAGE_MASK, 6073563362dSRichard Henderson out.paddr & TARGET_PAGE_MASK, 6083563362dSRichard Henderson cpu_get_mem_attrs(env), 6093563362dSRichard Henderson out.prot, mmu_idx, out.page_size); 6103563362dSRichard Henderson return true; 6113563362dSRichard Henderson } 6123563362dSRichard Henderson 6139bbcf372SRichard Henderson if (probe) { 6144a1e9d4dSRichard Henderson /* This will be used if recursing for stage2 translation. */ 6154a1e9d4dSRichard Henderson env->error_code = err.error_code; 6169bbcf372SRichard Henderson return false; 6179bbcf372SRichard Henderson } 6189bbcf372SRichard Henderson 6199bbcf372SRichard Henderson if (err.stage2 != S2_NONE) { 6209bbcf372SRichard Henderson raise_stage2(env, &err, retaddr); 6219bbcf372SRichard Henderson } 6223563362dSRichard Henderson 6233563362dSRichard Henderson if (env->intercept_exceptions & (1 << err.exception_index)) { 6243563362dSRichard Henderson /* cr2 is not modified in case of exceptions */ 6253563362dSRichard Henderson x86_stq_phys(cs, env->vm_vmcb + 6263563362dSRichard Henderson offsetof(struct vmcb, control.exit_info_2), 6273563362dSRichard Henderson err.cr2); 6283563362dSRichard Henderson } else { 6293563362dSRichard Henderson env->cr[2] = err.cr2; 630e7f2670fSClaudio Fontana } 6313563362dSRichard Henderson raise_exception_err_ra(env, err.exception_index, err.error_code, retaddr); 632e7f2670fSClaudio Fontana } 633958e1dd1SPaolo Bonzini 634958e1dd1SPaolo Bonzini G_NORETURN void x86_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, 635958e1dd1SPaolo Bonzini MMUAccessType access_type, 636958e1dd1SPaolo Bonzini int mmu_idx, uintptr_t retaddr) 637958e1dd1SPaolo Bonzini { 638958e1dd1SPaolo Bonzini X86CPU *cpu = X86_CPU(cs); 639958e1dd1SPaolo Bonzini handle_unaligned_access(&cpu->env, vaddr, access_type, retaddr); 640958e1dd1SPaolo Bonzini } 641