xref: /openbmc/qemu/target/i386/tcg/sysemu/excp_helper.c (revision de6c4c825959b7924e567afef1c856b3cd46e73c)
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"
2474781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
25e7f2670fSClaudio Fontana #include "tcg/helper-tcg.h"
26e7f2670fSClaudio Fontana 
273563362dSRichard Henderson typedef struct TranslateParams {
283563362dSRichard Henderson     target_ulong addr;
293563362dSRichard Henderson     target_ulong cr3;
303563362dSRichard Henderson     int pg_mode;
313563362dSRichard Henderson     int mmu_idx;
324a1e9d4dSRichard Henderson     int ptw_idx;
333563362dSRichard Henderson     MMUAccessType access_type;
343563362dSRichard Henderson } TranslateParams;
353563362dSRichard Henderson 
363563362dSRichard Henderson typedef struct TranslateResult {
373563362dSRichard Henderson     hwaddr paddr;
383563362dSRichard Henderson     int prot;
393563362dSRichard Henderson     int page_size;
403563362dSRichard Henderson } TranslateResult;
413563362dSRichard Henderson 
429bbcf372SRichard Henderson typedef enum TranslateFaultStage2 {
439bbcf372SRichard Henderson     S2_NONE,
449bbcf372SRichard Henderson     S2_GPA,
459bbcf372SRichard Henderson     S2_GPT,
469bbcf372SRichard Henderson } TranslateFaultStage2;
479bbcf372SRichard Henderson 
483563362dSRichard Henderson typedef struct TranslateFault {
493563362dSRichard Henderson     int exception_index;
503563362dSRichard Henderson     int error_code;
513563362dSRichard Henderson     target_ulong cr2;
529bbcf372SRichard Henderson     TranslateFaultStage2 stage2;
533563362dSRichard Henderson } TranslateFault;
54661ff487SPaolo Bonzini 
554a1e9d4dSRichard Henderson typedef struct PTETranslate {
564a1e9d4dSRichard Henderson     CPUX86State *env;
574a1e9d4dSRichard Henderson     TranslateFault *err;
584a1e9d4dSRichard Henderson     int ptw_idx;
594a1e9d4dSRichard Henderson     void *haddr;
604a1e9d4dSRichard Henderson     hwaddr gaddr;
614a1e9d4dSRichard Henderson } PTETranslate;
624a1e9d4dSRichard Henderson 
ptw_translate(PTETranslate * inout,hwaddr addr)639dab7bbbSGregory Price static bool ptw_translate(PTETranslate *inout, hwaddr addr)
644a1e9d4dSRichard Henderson {
654a1e9d4dSRichard Henderson     int flags;
664a1e9d4dSRichard Henderson 
674a1e9d4dSRichard Henderson     inout->gaddr = addr;
683a41aa82SRichard Henderson     flags = probe_access_full_mmu(inout->env, addr, 0, MMU_DATA_STORE,
693a41aa82SRichard Henderson                                   inout->ptw_idx, &inout->haddr, NULL);
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 
ptw_ldl(const PTETranslate * in,uint64_t ra)859dab7bbbSGregory Price static inline uint32_t ptw_ldl(const PTETranslate *in, uint64_t ra)
864a1e9d4dSRichard Henderson {
874a1e9d4dSRichard Henderson     if (likely(in->haddr)) {
884a1e9d4dSRichard Henderson         return ldl_p(in->haddr);
894a1e9d4dSRichard Henderson     }
909dab7bbbSGregory Price     return cpu_ldl_mmuidx_ra(in->env, in->gaddr, in->ptw_idx, ra);
914a1e9d4dSRichard Henderson }
924a1e9d4dSRichard Henderson 
ptw_ldq(const PTETranslate * in,uint64_t ra)939dab7bbbSGregory Price static inline uint64_t ptw_ldq(const PTETranslate *in, uint64_t ra)
944a1e9d4dSRichard Henderson {
954a1e9d4dSRichard Henderson     if (likely(in->haddr)) {
964a1e9d4dSRichard Henderson         return ldq_p(in->haddr);
974a1e9d4dSRichard Henderson     }
989dab7bbbSGregory Price     return cpu_ldq_mmuidx_ra(in->env, in->gaddr, in->ptw_idx, ra);
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  */
ptw_setl_slow(const PTETranslate * in,uint32_t old,uint32_t new)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 
110*de6c4c82SPierrick Bouvier     CPUState *cpu = env_cpu(in->env);
111*de6c4c82SPierrick Bouvier     /* We are in cpu_exec, and start_exclusive can't be called directly.*/
112*de6c4c82SPierrick Bouvier     g_assert(cpu->running);
113*de6c4c82SPierrick Bouvier     cpu_exec_end(cpu);
1144a1e9d4dSRichard Henderson     /* Does x86 really perform a rmw cycle on mmio for ptw? */
1154a1e9d4dSRichard Henderson     start_exclusive();
1164a1e9d4dSRichard Henderson     cmp = cpu_ldl_mmuidx_ra(in->env, in->gaddr, in->ptw_idx, 0);
1174a1e9d4dSRichard Henderson     if (cmp == old) {
1184a1e9d4dSRichard Henderson         cpu_stl_mmuidx_ra(in->env, in->gaddr, new, in->ptw_idx, 0);
1194a1e9d4dSRichard Henderson     }
1204a1e9d4dSRichard Henderson     end_exclusive();
121*de6c4c82SPierrick Bouvier     cpu_exec_start(cpu);
1224a1e9d4dSRichard Henderson     return cmp == old;
1234a1e9d4dSRichard Henderson }
1244a1e9d4dSRichard Henderson 
ptw_setl(const PTETranslate * in,uint32_t old,uint32_t set)1254a1e9d4dSRichard Henderson static inline bool ptw_setl(const PTETranslate *in, uint32_t old, uint32_t set)
1264a1e9d4dSRichard Henderson {
1274a1e9d4dSRichard Henderson     if (set & ~old) {
1284a1e9d4dSRichard Henderson         uint32_t new = old | set;
1294a1e9d4dSRichard Henderson         if (likely(in->haddr)) {
1304a1e9d4dSRichard Henderson             old = cpu_to_le32(old);
1314a1e9d4dSRichard Henderson             new = cpu_to_le32(new);
1324a1e9d4dSRichard Henderson             return qatomic_cmpxchg((uint32_t *)in->haddr, old, new) == old;
1334a1e9d4dSRichard Henderson         }
1344a1e9d4dSRichard Henderson         return ptw_setl_slow(in, old, new);
1354a1e9d4dSRichard Henderson     }
1364a1e9d4dSRichard Henderson     return true;
1374a1e9d4dSRichard Henderson }
13833ce155cSPaolo Bonzini 
mmu_translate(CPUX86State * env,const TranslateParams * in,TranslateResult * out,TranslateFault * err,uint64_t ra)1393563362dSRichard Henderson static bool mmu_translate(CPUX86State *env, const TranslateParams *in,
1409dab7bbbSGregory Price                           TranslateResult *out, TranslateFault *err,
1419dab7bbbSGregory Price                           uint64_t ra)
142e7f2670fSClaudio Fontana {
1433563362dSRichard Henderson     const target_ulong addr = in->addr;
1443563362dSRichard Henderson     const int pg_mode = in->pg_mode;
1455f97afe2SPaolo Bonzini     const bool is_user = is_mmu_index_user(in->mmu_idx);
1463563362dSRichard Henderson     const MMUAccessType access_type = in->access_type;
1474a1e9d4dSRichard Henderson     uint64_t ptep, pte, rsvd_mask;
1484a1e9d4dSRichard Henderson     PTETranslate pte_trans = {
1494a1e9d4dSRichard Henderson         .env = env,
1504a1e9d4dSRichard Henderson         .err = err,
1514a1e9d4dSRichard Henderson         .ptw_idx = in->ptw_idx,
1524a1e9d4dSRichard Henderson     };
1538629e77bSRichard Henderson     hwaddr pte_addr, paddr;
154e7f2670fSClaudio Fontana     uint32_t pkr;
1553563362dSRichard Henderson     int page_size;
156987b63f2SPeter Maydell     int error_code;
15701bfc2e2SAlexander Graf     int prot;
158e7f2670fSClaudio Fontana 
1594a1e9d4dSRichard Henderson  restart_all:
1604a1e9d4dSRichard Henderson     rsvd_mask = ~MAKE_64BIT_MASK(0, env_archcpu(env)->phys_bits);
1614a1e9d4dSRichard Henderson     rsvd_mask &= PG_ADDRESS_MASK;
16231dd35ebSPaolo Bonzini     if (!(pg_mode & PG_MODE_NXE)) {
163e7f2670fSClaudio Fontana         rsvd_mask |= PG_NX_MASK;
164e7f2670fSClaudio Fontana     }
165e7f2670fSClaudio Fontana 
16631dd35ebSPaolo Bonzini     if (pg_mode & PG_MODE_PAE) {
167e7f2670fSClaudio Fontana #ifdef TARGET_X86_64
16893eae358SPaolo Bonzini         if (pg_mode & PG_MODE_LMA) {
16911b4e971SRichard Henderson             if (pg_mode & PG_MODE_LA57) {
17011b4e971SRichard Henderson                 /*
17111b4e971SRichard Henderson                  * Page table level 5
17211b4e971SRichard Henderson                  */
173a28fe7dcSPaolo Bonzini                 pte_addr = (in->cr3 & ~0xfff) + (((addr >> 48) & 0x1ff) << 3);
1749dab7bbbSGregory Price                 if (!ptw_translate(&pte_trans, pte_addr)) {
1754a1e9d4dSRichard Henderson                     return false;
1764a1e9d4dSRichard Henderson                 }
1774a1e9d4dSRichard Henderson             restart_5:
1789dab7bbbSGregory Price                 pte = ptw_ldq(&pte_trans, ra);
17911b4e971SRichard Henderson                 if (!(pte & PG_PRESENT_MASK)) {
180e7f2670fSClaudio Fontana                     goto do_fault;
181e7f2670fSClaudio Fontana                 }
18211b4e971SRichard Henderson                 if (pte & (rsvd_mask | PG_PSE_MASK)) {
183e7f2670fSClaudio Fontana                     goto do_fault_rsvd;
184e7f2670fSClaudio Fontana                 }
1854a1e9d4dSRichard Henderson                 if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) {
1864a1e9d4dSRichard Henderson                     goto restart_5;
187e7f2670fSClaudio Fontana                 }
18811b4e971SRichard Henderson                 ptep = pte ^ PG_NX_MASK;
189e7f2670fSClaudio Fontana             } else {
19011b4e971SRichard Henderson                 pte = in->cr3;
191e7f2670fSClaudio Fontana                 ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
192e7f2670fSClaudio Fontana             }
193e7f2670fSClaudio Fontana 
19411b4e971SRichard Henderson             /*
19511b4e971SRichard Henderson              * Page table level 4
19611b4e971SRichard Henderson              */
197a28fe7dcSPaolo Bonzini             pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 39) & 0x1ff) << 3);
1989dab7bbbSGregory Price             if (!ptw_translate(&pte_trans, pte_addr)) {
1994a1e9d4dSRichard Henderson                 return false;
2004a1e9d4dSRichard Henderson             }
2014a1e9d4dSRichard Henderson         restart_4:
2029dab7bbbSGregory Price             pte = ptw_ldq(&pte_trans, ra);
20311b4e971SRichard Henderson             if (!(pte & PG_PRESENT_MASK)) {
204e7f2670fSClaudio Fontana                 goto do_fault;
205e7f2670fSClaudio Fontana             }
20611b4e971SRichard Henderson             if (pte & (rsvd_mask | PG_PSE_MASK)) {
207e7f2670fSClaudio Fontana                 goto do_fault_rsvd;
208e7f2670fSClaudio Fontana             }
2094a1e9d4dSRichard Henderson             if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) {
2104a1e9d4dSRichard Henderson                 goto restart_4;
211e7f2670fSClaudio Fontana             }
21211b4e971SRichard Henderson             ptep &= pte ^ PG_NX_MASK;
21311b4e971SRichard Henderson 
21411b4e971SRichard Henderson             /*
21511b4e971SRichard Henderson              * Page table level 3
21611b4e971SRichard Henderson              */
217a28fe7dcSPaolo Bonzini             pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 30) & 0x1ff) << 3);
2189dab7bbbSGregory Price             if (!ptw_translate(&pte_trans, pte_addr)) {
2194a1e9d4dSRichard Henderson                 return false;
2204a1e9d4dSRichard Henderson             }
2214a1e9d4dSRichard Henderson         restart_3_lma:
2229dab7bbbSGregory Price             pte = ptw_ldq(&pte_trans, ra);
22311b4e971SRichard Henderson             if (!(pte & PG_PRESENT_MASK)) {
224e7f2670fSClaudio Fontana                 goto do_fault;
225e7f2670fSClaudio Fontana             }
22611b4e971SRichard Henderson             if (pte & rsvd_mask) {
227e7f2670fSClaudio Fontana                 goto do_fault_rsvd;
228e7f2670fSClaudio Fontana             }
2294a1e9d4dSRichard Henderson             if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) {
2304a1e9d4dSRichard Henderson                 goto restart_3_lma;
231e7f2670fSClaudio Fontana             }
2324a1e9d4dSRichard Henderson             ptep &= pte ^ PG_NX_MASK;
23311b4e971SRichard Henderson             if (pte & PG_PSE_MASK) {
234e7f2670fSClaudio Fontana                 /* 1 GB page */
2353563362dSRichard Henderson                 page_size = 1024 * 1024 * 1024;
236e7f2670fSClaudio Fontana                 goto do_check_protect;
237e7f2670fSClaudio Fontana             }
238e7f2670fSClaudio Fontana         } else
239e7f2670fSClaudio Fontana #endif
240e7f2670fSClaudio Fontana         {
24111b4e971SRichard Henderson             /*
24211b4e971SRichard Henderson              * Page table level 3
24311b4e971SRichard Henderson              */
244a28fe7dcSPaolo Bonzini             pte_addr = (in->cr3 & 0xffffffe0ULL) + ((addr >> 27) & 0x18);
2459dab7bbbSGregory Price             if (!ptw_translate(&pte_trans, pte_addr)) {
2464a1e9d4dSRichard Henderson                 return false;
2474a1e9d4dSRichard Henderson             }
2484a1e9d4dSRichard Henderson             rsvd_mask |= PG_HI_USER_MASK;
2494a1e9d4dSRichard Henderson         restart_3_nolma:
2509dab7bbbSGregory Price             pte = ptw_ldq(&pte_trans, ra);
25111b4e971SRichard Henderson             if (!(pte & PG_PRESENT_MASK)) {
252e7f2670fSClaudio Fontana                 goto do_fault;
253e7f2670fSClaudio Fontana             }
25411b4e971SRichard Henderson             if (pte & (rsvd_mask | PG_NX_MASK)) {
255e7f2670fSClaudio Fontana                 goto do_fault_rsvd;
256e7f2670fSClaudio Fontana             }
2574a1e9d4dSRichard Henderson             if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) {
2584a1e9d4dSRichard Henderson                 goto restart_3_nolma;
2594a1e9d4dSRichard Henderson             }
260e7f2670fSClaudio Fontana             ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
261e7f2670fSClaudio Fontana         }
262e7f2670fSClaudio Fontana 
26311b4e971SRichard Henderson         /*
26411b4e971SRichard Henderson          * Page table level 2
26511b4e971SRichard Henderson          */
266a28fe7dcSPaolo Bonzini         pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) << 3);
2679dab7bbbSGregory Price         if (!ptw_translate(&pte_trans, pte_addr)) {
2684a1e9d4dSRichard Henderson             return false;
2694a1e9d4dSRichard Henderson         }
2704a1e9d4dSRichard Henderson     restart_2_pae:
2719dab7bbbSGregory Price         pte = ptw_ldq(&pte_trans, ra);
27211b4e971SRichard Henderson         if (!(pte & PG_PRESENT_MASK)) {
273e7f2670fSClaudio Fontana             goto do_fault;
274e7f2670fSClaudio Fontana         }
27511b4e971SRichard Henderson         if (pte & rsvd_mask) {
276e7f2670fSClaudio Fontana             goto do_fault_rsvd;
277e7f2670fSClaudio Fontana         }
27811b4e971SRichard Henderson         if (pte & PG_PSE_MASK) {
279e7f2670fSClaudio Fontana             /* 2 MB page */
2803563362dSRichard Henderson             page_size = 2048 * 1024;
2814a1e9d4dSRichard Henderson             ptep &= pte ^ PG_NX_MASK;
282e7f2670fSClaudio Fontana             goto do_check_protect;
283e7f2670fSClaudio Fontana         }
2844a1e9d4dSRichard Henderson         if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) {
2854a1e9d4dSRichard Henderson             goto restart_2_pae;
286e7f2670fSClaudio Fontana         }
2874a1e9d4dSRichard Henderson         ptep &= pte ^ PG_NX_MASK;
28811b4e971SRichard Henderson 
28911b4e971SRichard Henderson         /*
29011b4e971SRichard Henderson          * Page table level 1
29111b4e971SRichard Henderson          */
292a28fe7dcSPaolo Bonzini         pte_addr = (pte & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) << 3);
2939dab7bbbSGregory Price         if (!ptw_translate(&pte_trans, pte_addr)) {
2944a1e9d4dSRichard Henderson             return false;
2954a1e9d4dSRichard Henderson         }
2969dab7bbbSGregory Price         pte = ptw_ldq(&pte_trans, ra);
297e7f2670fSClaudio Fontana         if (!(pte & PG_PRESENT_MASK)) {
298e7f2670fSClaudio Fontana             goto do_fault;
299e7f2670fSClaudio Fontana         }
300e7f2670fSClaudio Fontana         if (pte & rsvd_mask) {
301e7f2670fSClaudio Fontana             goto do_fault_rsvd;
302e7f2670fSClaudio Fontana         }
303e7f2670fSClaudio Fontana         /* combine pde and pte nx, user and rw protections */
304e7f2670fSClaudio Fontana         ptep &= pte ^ PG_NX_MASK;
3053563362dSRichard Henderson         page_size = 4096;
306f7ff24a6SAlexander Graf     } else if (pg_mode & PG_MODE_PG) {
30711b4e971SRichard Henderson         /*
30811b4e971SRichard Henderson          * Page table level 2
30911b4e971SRichard Henderson          */
310a28fe7dcSPaolo Bonzini         pte_addr = (in->cr3 & 0xfffff000ULL) + ((addr >> 20) & 0xffc);
3119dab7bbbSGregory Price         if (!ptw_translate(&pte_trans, pte_addr)) {
3124a1e9d4dSRichard Henderson             return false;
3134a1e9d4dSRichard Henderson         }
3144a1e9d4dSRichard Henderson     restart_2_nopae:
3159dab7bbbSGregory Price         pte = ptw_ldl(&pte_trans, ra);
31611b4e971SRichard Henderson         if (!(pte & PG_PRESENT_MASK)) {
317e7f2670fSClaudio Fontana             goto do_fault;
318e7f2670fSClaudio Fontana         }
31911b4e971SRichard Henderson         ptep = pte | PG_NX_MASK;
320e7f2670fSClaudio Fontana 
321e7f2670fSClaudio Fontana         /* if PSE bit is set, then we use a 4MB page */
32211b4e971SRichard Henderson         if ((pte & PG_PSE_MASK) && (pg_mode & PG_MODE_PSE)) {
3233563362dSRichard Henderson             page_size = 4096 * 1024;
32411b4e971SRichard Henderson             /*
32511b4e971SRichard Henderson              * Bits 20-13 provide bits 39-32 of the address, bit 21 is reserved.
326e7f2670fSClaudio Fontana              * Leave bits 20-13 in place for setting accessed/dirty bits below.
327e7f2670fSClaudio Fontana              */
32811b4e971SRichard Henderson             pte = (uint32_t)pte | ((pte & 0x1fe000LL) << (32 - 13));
329e7f2670fSClaudio Fontana             rsvd_mask = 0x200000;
330e7f2670fSClaudio Fontana             goto do_check_protect_pse36;
331e7f2670fSClaudio Fontana         }
3324a1e9d4dSRichard Henderson         if (!ptw_setl(&pte_trans, pte, PG_ACCESSED_MASK)) {
3334a1e9d4dSRichard Henderson             goto restart_2_nopae;
334e7f2670fSClaudio Fontana         }
335e7f2670fSClaudio Fontana 
33611b4e971SRichard Henderson         /*
33711b4e971SRichard Henderson          * Page table level 1
33811b4e971SRichard Henderson          */
339a28fe7dcSPaolo Bonzini         pte_addr = (pte & ~0xfffu) + ((addr >> 10) & 0xffc);
3409dab7bbbSGregory Price         if (!ptw_translate(&pte_trans, pte_addr)) {
3414a1e9d4dSRichard Henderson             return false;
3424a1e9d4dSRichard Henderson         }
3439dab7bbbSGregory Price         pte = ptw_ldl(&pte_trans, ra);
344e7f2670fSClaudio Fontana         if (!(pte & PG_PRESENT_MASK)) {
345e7f2670fSClaudio Fontana             goto do_fault;
346e7f2670fSClaudio Fontana         }
347e7f2670fSClaudio Fontana         /* combine pde and pte user and rw protections */
348e7f2670fSClaudio Fontana         ptep &= pte | PG_NX_MASK;
3493563362dSRichard Henderson         page_size = 4096;
350e7f2670fSClaudio Fontana         rsvd_mask = 0;
35101bfc2e2SAlexander Graf     } else {
35201bfc2e2SAlexander Graf         /*
35301bfc2e2SAlexander Graf          * No paging (real mode), let's tentatively resolve the address as 1:1
35401bfc2e2SAlexander Graf          * here, but conditionally still perform an NPT walk on it later.
35501bfc2e2SAlexander Graf          */
35601bfc2e2SAlexander Graf         page_size = 0x40000000;
35701bfc2e2SAlexander Graf         paddr = in->addr;
35801bfc2e2SAlexander Graf         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
35901bfc2e2SAlexander Graf         goto stage2;
360e7f2670fSClaudio Fontana     }
361e7f2670fSClaudio Fontana 
362e7f2670fSClaudio Fontana do_check_protect:
3633563362dSRichard Henderson     rsvd_mask |= (page_size - 1) & PG_ADDRESS_MASK & ~PG_PSE_PAT_MASK;
364e7f2670fSClaudio Fontana do_check_protect_pse36:
365e7f2670fSClaudio Fontana     if (pte & rsvd_mask) {
366e7f2670fSClaudio Fontana         goto do_fault_rsvd;
367e7f2670fSClaudio Fontana     }
368e7f2670fSClaudio Fontana     ptep ^= PG_NX_MASK;
369e7f2670fSClaudio Fontana 
370e7f2670fSClaudio Fontana     /* can the page can be put in the TLB?  prot will tell us */
371e7f2670fSClaudio Fontana     if (is_user && !(ptep & PG_USER_MASK)) {
372e7f2670fSClaudio Fontana         goto do_fault_protect;
373e7f2670fSClaudio Fontana     }
374e7f2670fSClaudio Fontana 
37501bfc2e2SAlexander Graf     prot = 0;
3765f97afe2SPaolo Bonzini     if (!is_mmu_index_smap(in->mmu_idx) || !(ptep & PG_USER_MASK)) {
3773563362dSRichard Henderson         prot |= PAGE_READ;
37831dd35ebSPaolo Bonzini         if ((ptep & PG_RW_MASK) || !(is_user || (pg_mode & PG_MODE_WP))) {
3793563362dSRichard Henderson             prot |= PAGE_WRITE;
380e7f2670fSClaudio Fontana         }
381e7f2670fSClaudio Fontana     }
382e7f2670fSClaudio Fontana     if (!(ptep & PG_NX_MASK) &&
3833563362dSRichard Henderson         (is_user ||
38431dd35ebSPaolo Bonzini          !((pg_mode & PG_MODE_SMEP) && (ptep & PG_USER_MASK)))) {
3853563362dSRichard Henderson         prot |= PAGE_EXEC;
386e7f2670fSClaudio Fontana     }
387e7f2670fSClaudio Fontana 
388991ec976SPaolo Bonzini     if (ptep & PG_USER_MASK) {
38931dd35ebSPaolo Bonzini         pkr = pg_mode & PG_MODE_PKE ? env->pkru : 0;
390e7f2670fSClaudio Fontana     } else {
39131dd35ebSPaolo Bonzini         pkr = pg_mode & PG_MODE_PKS ? env->pkrs : 0;
392e7f2670fSClaudio Fontana     }
393e7f2670fSClaudio Fontana     if (pkr) {
394e7f2670fSClaudio Fontana         uint32_t pk = (pte & PG_PKRU_MASK) >> PG_PKRU_BIT;
395e7f2670fSClaudio Fontana         uint32_t pkr_ad = (pkr >> pk * 2) & 1;
396e7f2670fSClaudio Fontana         uint32_t pkr_wd = (pkr >> pk * 2) & 2;
397e7f2670fSClaudio Fontana         uint32_t pkr_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
398e7f2670fSClaudio Fontana 
399e7f2670fSClaudio Fontana         if (pkr_ad) {
400e7f2670fSClaudio Fontana             pkr_prot &= ~(PAGE_READ | PAGE_WRITE);
40131dd35ebSPaolo Bonzini         } else if (pkr_wd && (is_user || (pg_mode & PG_MODE_WP))) {
402e7f2670fSClaudio Fontana             pkr_prot &= ~PAGE_WRITE;
403e7f2670fSClaudio Fontana         }
404487d1133SRichard Henderson         if ((pkr_prot & (1 << access_type)) == 0) {
4053563362dSRichard Henderson             goto do_fault_pk_protect;
406e7f2670fSClaudio Fontana         }
4073563362dSRichard Henderson         prot &= pkr_prot;
408e7f2670fSClaudio Fontana     }
409e7f2670fSClaudio Fontana 
4103563362dSRichard Henderson     if ((prot & (1 << access_type)) == 0) {
411e7f2670fSClaudio Fontana         goto do_fault_protect;
412e7f2670fSClaudio Fontana     }
413e7f2670fSClaudio Fontana 
414e7f2670fSClaudio Fontana     /* yes, it can! */
4153563362dSRichard Henderson     {
4163563362dSRichard Henderson         uint32_t set = PG_ACCESSED_MASK;
4173563362dSRichard Henderson         if (access_type == MMU_DATA_STORE) {
4183563362dSRichard Henderson             set |= PG_DIRTY_MASK;
4194a1e9d4dSRichard Henderson         } else if (!(pte & PG_DIRTY_MASK)) {
4204a1e9d4dSRichard Henderson             /*
4214a1e9d4dSRichard Henderson              * Only set write access if already dirty...
4224a1e9d4dSRichard Henderson              * otherwise wait for dirty access.
4234a1e9d4dSRichard Henderson              */
4243563362dSRichard Henderson             prot &= ~PAGE_WRITE;
425e7f2670fSClaudio Fontana         }
4264a1e9d4dSRichard Henderson         if (!ptw_setl(&pte_trans, pte, set)) {
4274a1e9d4dSRichard Henderson             /*
4284a1e9d4dSRichard Henderson              * We can arrive here from any of 3 levels and 2 formats.
4294a1e9d4dSRichard Henderson              * The only safe thing is to restart the entire lookup.
4304a1e9d4dSRichard Henderson              */
4314a1e9d4dSRichard Henderson             goto restart_all;
4324a1e9d4dSRichard Henderson         }
4334a1e9d4dSRichard Henderson     }
434e7f2670fSClaudio Fontana 
435b5a9de32SPaolo Bonzini     /* merge offset within page */
436b5a9de32SPaolo Bonzini     paddr = (pte & PG_ADDRESS_MASK & ~(page_size - 1)) | (addr & (page_size - 1));
43701bfc2e2SAlexander Graf  stage2:
4389bbcf372SRichard Henderson 
439b5a9de32SPaolo Bonzini     /*
440b5a9de32SPaolo Bonzini      * Note that NPT is walked (for both paging structures and final guest
441b5a9de32SPaolo Bonzini      * addresses) using the address with the A20 bit set.
442b5a9de32SPaolo Bonzini      */
4434a1e9d4dSRichard Henderson     if (in->ptw_idx == MMU_NESTED_IDX) {
4448629e77bSRichard Henderson         CPUTLBEntryFull *full;
4458629e77bSRichard Henderson         int flags, nested_page_size;
4469bbcf372SRichard Henderson 
4473a41aa82SRichard Henderson         flags = probe_access_full_mmu(env, paddr, 0, access_type,
4483a41aa82SRichard Henderson                                       MMU_NESTED_IDX, &pte_trans.haddr, &full);
4498629e77bSRichard Henderson         if (unlikely(flags & TLB_INVALID_MASK)) {
4508218c048SRichard Henderson             *err = (TranslateFault){
4518218c048SRichard Henderson                 .error_code = env->error_code,
4528218c048SRichard Henderson                 .cr2 = paddr,
4538218c048SRichard Henderson                 .stage2 = S2_GPA,
4548218c048SRichard Henderson             };
4559bbcf372SRichard Henderson             return false;
4569bbcf372SRichard Henderson         }
4579bbcf372SRichard Henderson 
4589bbcf372SRichard Henderson         /* Merge stage1 & stage2 protection bits. */
4598629e77bSRichard Henderson         prot &= full->prot;
4609bbcf372SRichard Henderson 
4619bbcf372SRichard Henderson         /* Re-verify resulting protection. */
4629bbcf372SRichard Henderson         if ((prot & (1 << access_type)) == 0) {
4639bbcf372SRichard Henderson             goto do_fault_protect;
4649bbcf372SRichard Henderson         }
4658629e77bSRichard Henderson 
4668629e77bSRichard Henderson         /* Merge stage1 & stage2 addresses to final physical address. */
4678629e77bSRichard Henderson         nested_page_size = 1 << full->lg_page_size;
4688629e77bSRichard Henderson         paddr = (full->phys_addr & ~(nested_page_size - 1))
4698629e77bSRichard Henderson               | (paddr & (nested_page_size - 1));
4708629e77bSRichard Henderson 
4718629e77bSRichard Henderson         /*
4728629e77bSRichard Henderson          * Use the larger of stage1 & stage2 page sizes, so that
4738629e77bSRichard Henderson          * invalidation works.
4748629e77bSRichard Henderson          */
4758629e77bSRichard Henderson         if (nested_page_size > page_size) {
4768629e77bSRichard Henderson             page_size = nested_page_size;
4778629e77bSRichard Henderson         }
4789bbcf372SRichard Henderson     }
4799bbcf372SRichard Henderson 
480b5a9de32SPaolo Bonzini     out->paddr = paddr & x86_get_a20_mask(env);
4819bbcf372SRichard Henderson     out->prot = prot;
4829bbcf372SRichard Henderson     out->page_size = page_size;
4833563362dSRichard Henderson     return true;
484e7f2670fSClaudio Fontana 
485e7f2670fSClaudio Fontana  do_fault_rsvd:
4863563362dSRichard Henderson     error_code = PG_ERROR_RSVD_MASK;
4873563362dSRichard Henderson     goto do_fault_cont;
488e7f2670fSClaudio Fontana  do_fault_protect:
4893563362dSRichard Henderson     error_code = PG_ERROR_P_MASK;
4903563362dSRichard Henderson     goto do_fault_cont;
4913563362dSRichard Henderson  do_fault_pk_protect:
4923563362dSRichard Henderson     assert(access_type != MMU_INST_FETCH);
4933563362dSRichard Henderson     error_code = PG_ERROR_PK_MASK | PG_ERROR_P_MASK;
4943563362dSRichard Henderson     goto do_fault_cont;
495e7f2670fSClaudio Fontana  do_fault:
4963563362dSRichard Henderson     error_code = 0;
4973563362dSRichard Henderson  do_fault_cont:
4983563362dSRichard Henderson     if (is_user) {
499e7f2670fSClaudio Fontana         error_code |= PG_ERROR_U_MASK;
5003563362dSRichard Henderson     }
5013563362dSRichard Henderson     switch (access_type) {
5023563362dSRichard Henderson     case MMU_DATA_LOAD:
5033563362dSRichard Henderson         break;
5043563362dSRichard Henderson     case MMU_DATA_STORE:
5053563362dSRichard Henderson         error_code |= PG_ERROR_W_MASK;
5063563362dSRichard Henderson         break;
5073563362dSRichard Henderson     case MMU_INST_FETCH:
5083563362dSRichard Henderson         if (pg_mode & (PG_MODE_NXE | PG_MODE_SMEP)) {
509e7f2670fSClaudio Fontana             error_code |= PG_ERROR_I_D_MASK;
5103563362dSRichard Henderson         }
5113563362dSRichard Henderson         break;
5123563362dSRichard Henderson     }
5138218c048SRichard Henderson     *err = (TranslateFault){
5148218c048SRichard Henderson         .exception_index = EXCP0E_PAGE,
5158218c048SRichard Henderson         .error_code = error_code,
5168218c048SRichard Henderson         .cr2 = addr,
5178218c048SRichard Henderson     };
5183563362dSRichard Henderson     return false;
519661ff487SPaolo Bonzini }
520661ff487SPaolo Bonzini 
raise_stage2(CPUX86State * env,TranslateFault * err,uintptr_t retaddr)5219bbcf372SRichard Henderson static G_NORETURN void raise_stage2(CPUX86State *env, TranslateFault *err,
5229bbcf372SRichard Henderson                                     uintptr_t retaddr)
5239bbcf372SRichard Henderson {
5249bbcf372SRichard Henderson     uint64_t exit_info_1 = err->error_code;
5259bbcf372SRichard Henderson 
5269bbcf372SRichard Henderson     switch (err->stage2) {
5279bbcf372SRichard Henderson     case S2_GPT:
5289bbcf372SRichard Henderson         exit_info_1 |= SVM_NPTEXIT_GPT;
5299bbcf372SRichard Henderson         break;
5309bbcf372SRichard Henderson     case S2_GPA:
5319bbcf372SRichard Henderson         exit_info_1 |= SVM_NPTEXIT_GPA;
5329bbcf372SRichard Henderson         break;
5339bbcf372SRichard Henderson     default:
5349bbcf372SRichard Henderson         g_assert_not_reached();
5359bbcf372SRichard Henderson     }
5369bbcf372SRichard Henderson 
5379bbcf372SRichard Henderson     x86_stq_phys(env_cpu(env),
5389bbcf372SRichard Henderson                  env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
5399bbcf372SRichard Henderson                  err->cr2);
5409bbcf372SRichard Henderson     cpu_vmexit(env, SVM_EXIT_NPF, exit_info_1, retaddr);
5419bbcf372SRichard Henderson }
5429bbcf372SRichard Henderson 
get_physical_address(CPUX86State * env,vaddr addr,MMUAccessType access_type,int mmu_idx,TranslateResult * out,TranslateFault * err,uint64_t ra)5433563362dSRichard Henderson static bool get_physical_address(CPUX86State *env, vaddr addr,
5443563362dSRichard Henderson                                  MMUAccessType access_type, int mmu_idx,
5459dab7bbbSGregory Price                                  TranslateResult *out, TranslateFault *err,
5469dab7bbbSGregory Price                                  uint64_t ra)
547661ff487SPaolo Bonzini {
54898281984SRichard Henderson     TranslateParams in;
54998281984SRichard Henderson     bool use_stage2 = env->hflags2 & HF2_NPT_MASK;
5503563362dSRichard Henderson 
55198281984SRichard Henderson     in.addr = addr;
55298281984SRichard Henderson     in.access_type = access_type;
55398281984SRichard Henderson 
55498281984SRichard Henderson     switch (mmu_idx) {
55598281984SRichard Henderson     case MMU_PHYS_IDX:
55698281984SRichard Henderson         break;
55798281984SRichard Henderson 
55898281984SRichard Henderson     case MMU_NESTED_IDX:
55998281984SRichard Henderson         if (likely(use_stage2)) {
56098281984SRichard Henderson             in.cr3 = env->nested_cr3;
56198281984SRichard Henderson             in.pg_mode = env->nested_pg_mode;
56290f64153SPaolo Bonzini             in.mmu_idx =
56390f64153SPaolo Bonzini                 env->nested_pg_mode & PG_MODE_LMA ? MMU_USER64_IDX : MMU_USER32_IDX;
5644a1e9d4dSRichard Henderson             in.ptw_idx = MMU_PHYS_IDX;
56598281984SRichard Henderson 
5669dab7bbbSGregory Price             if (!mmu_translate(env, &in, out, err, ra)) {
56798281984SRichard Henderson                 err->stage2 = S2_GPA;
56898281984SRichard Henderson                 return false;
569661ff487SPaolo Bonzini             }
5703563362dSRichard Henderson             return true;
57198281984SRichard Henderson         }
57298281984SRichard Henderson         break;
573b04dc92eSPaolo Bonzini 
57498281984SRichard Henderson     default:
575b1661801SPaolo Bonzini         if (is_mmu_index_32(mmu_idx)) {
576b1661801SPaolo Bonzini             addr = (uint32_t)addr;
577b1661801SPaolo Bonzini         }
578b1661801SPaolo Bonzini 
57901bfc2e2SAlexander Graf         if (likely(env->cr[0] & CR0_PG_MASK || use_stage2)) {
58098281984SRichard Henderson             in.cr3 = env->cr[3];
58198281984SRichard Henderson             in.mmu_idx = mmu_idx;
5824a1e9d4dSRichard Henderson             in.ptw_idx = use_stage2 ? MMU_NESTED_IDX : MMU_PHYS_IDX;
58398281984SRichard Henderson             in.pg_mode = get_pg_mode(env);
58498281984SRichard Henderson 
5853563362dSRichard Henderson             if (in.pg_mode & PG_MODE_LMA) {
586b04dc92eSPaolo Bonzini                 /* test virtual address sign extension */
5873563362dSRichard Henderson                 int shift = in.pg_mode & PG_MODE_LA57 ? 56 : 47;
5883563362dSRichard Henderson                 int64_t sext = (int64_t)addr >> shift;
589b04dc92eSPaolo Bonzini                 if (sext != 0 && sext != -1) {
5908218c048SRichard Henderson                     *err = (TranslateFault){
5918218c048SRichard Henderson                         .exception_index = EXCP0D_GPF,
5928218c048SRichard Henderson                         .cr2 = addr,
5938218c048SRichard Henderson                     };
5943563362dSRichard Henderson                     return false;
595b04dc92eSPaolo Bonzini                 }
596b04dc92eSPaolo Bonzini             }
5979dab7bbbSGregory Price             return mmu_translate(env, &in, out, err, ra);
598e7f2670fSClaudio Fontana         }
59998281984SRichard Henderson         break;
60098281984SRichard Henderson     }
60198281984SRichard Henderson 
602b1661801SPaolo Bonzini     /* No translation needed. */
60398281984SRichard Henderson     out->paddr = addr & x86_get_a20_mask(env);
60498281984SRichard Henderson     out->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
60598281984SRichard Henderson     out->page_size = TARGET_PAGE_SIZE;
60698281984SRichard Henderson     return true;
607661ff487SPaolo Bonzini }
608e7f2670fSClaudio Fontana 
x86_cpu_tlb_fill(CPUState * cs,vaddr addr,int size,MMUAccessType access_type,int mmu_idx,bool probe,uintptr_t retaddr)609e7f2670fSClaudio Fontana bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
610e7f2670fSClaudio Fontana                       MMUAccessType access_type, int mmu_idx,
611e7f2670fSClaudio Fontana                       bool probe, uintptr_t retaddr)
612e7f2670fSClaudio Fontana {
613b77af26eSRichard Henderson     CPUX86State *env = cpu_env(cs);
6143563362dSRichard Henderson     TranslateResult out;
6153563362dSRichard Henderson     TranslateFault err;
616e7f2670fSClaudio Fontana 
6179dab7bbbSGregory Price     if (get_physical_address(env, addr, access_type, mmu_idx, &out, &err,
6189dab7bbbSGregory Price                              retaddr)) {
6193563362dSRichard Henderson         /*
6203563362dSRichard Henderson          * Even if 4MB pages, we map only one 4KB page in the cache to
6213563362dSRichard Henderson          * avoid filling it too fast.
6223563362dSRichard Henderson          */
6233563362dSRichard Henderson         assert(out.prot & (1 << access_type));
6243563362dSRichard Henderson         tlb_set_page_with_attrs(cs, addr & TARGET_PAGE_MASK,
6253563362dSRichard Henderson                                 out.paddr & TARGET_PAGE_MASK,
6263563362dSRichard Henderson                                 cpu_get_mem_attrs(env),
6273563362dSRichard Henderson                                 out.prot, mmu_idx, out.page_size);
6283563362dSRichard Henderson         return true;
6293563362dSRichard Henderson     }
6303563362dSRichard Henderson 
6319bbcf372SRichard Henderson     if (probe) {
6324a1e9d4dSRichard Henderson         /* This will be used if recursing for stage2 translation. */
6334a1e9d4dSRichard Henderson         env->error_code = err.error_code;
6349bbcf372SRichard Henderson         return false;
6359bbcf372SRichard Henderson     }
6369bbcf372SRichard Henderson 
6379bbcf372SRichard Henderson     if (err.stage2 != S2_NONE) {
6389bbcf372SRichard Henderson         raise_stage2(env, &err, retaddr);
6399bbcf372SRichard Henderson     }
6403563362dSRichard Henderson 
6413563362dSRichard Henderson     if (env->intercept_exceptions & (1 << err.exception_index)) {
6423563362dSRichard Henderson         /* cr2 is not modified in case of exceptions */
6433563362dSRichard Henderson         x86_stq_phys(cs, env->vm_vmcb +
6443563362dSRichard Henderson                      offsetof(struct vmcb, control.exit_info_2),
6453563362dSRichard Henderson                      err.cr2);
6463563362dSRichard Henderson     } else {
6473563362dSRichard Henderson         env->cr[2] = err.cr2;
648e7f2670fSClaudio Fontana     }
6493563362dSRichard Henderson     raise_exception_err_ra(env, err.exception_index, err.error_code, retaddr);
650e7f2670fSClaudio Fontana }
651958e1dd1SPaolo Bonzini 
x86_cpu_do_unaligned_access(CPUState * cs,vaddr vaddr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr)652958e1dd1SPaolo Bonzini G_NORETURN void x86_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
653958e1dd1SPaolo Bonzini                                             MMUAccessType access_type,
654958e1dd1SPaolo Bonzini                                             int mmu_idx, uintptr_t retaddr)
655958e1dd1SPaolo Bonzini {
656958e1dd1SPaolo Bonzini     X86CPU *cpu = X86_CPU(cs);
657958e1dd1SPaolo Bonzini     handle_unaligned_access(&cpu->env, vaddr, access_type, retaddr);
658958e1dd1SPaolo Bonzini }
659