From nobody Mon Feb 9 22:38:09 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666101030; cv=none; d=zohomail.com; s=zohoarc; b=iiLpO23ADMZWXyReZ4Qx2MrH4QSiPsEdzUWnbtxwukaJfFDViaY4C3H/USfUWsh/50U4hq9NpaqsvC8JsInNp0axklkv+r9YRl3W93JLxm/BgvfqNDCUUI7x2+IEDWUz6z9XPfRZl2PqUoPyGPFHxD27O5CDT8yvlrfJfIk7Pug= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666101030; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/E80xi7+DHvPCJRY31HtVvxNIdUIG8v2vBsVZ9TVoa0=; b=PDeK0VJOhSCeozrDb0zLJT8lAp1yk61nRy6aD0I7EraWZC8q7lTxxxR/HAykpwWTt8ikiRayQSftkmK80bM68XREF01e0ftCq/Is9TTcFVQUwDzaiSVismxR0+BeaotCM2H+wV34Ysr+of2nShezbkPNrEt8gQQQfA8A4d5CSi0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666101030161743.2869222330053; Tue, 18 Oct 2022 06:50:30 -0700 (PDT) Received: from localhost ([::1]:57982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1okmzC-0007Db-Ex for importer@patchew.org; Tue, 18 Oct 2022 09:50:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37158) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okmgy-0002NP-T4 for qemu-devel@nongnu.org; Tue, 18 Oct 2022 09:31:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43703) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okmgw-00051t-Hx for qemu-devel@nongnu.org; Tue, 18 Oct 2022 09:31:36 -0400 Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-390-sbg_BKqsMLWPz2sPAdlEFg-1; Tue, 18 Oct 2022 09:31:28 -0400 Received: by mail-ed1-f72.google.com with SMTP id w1-20020a056402268100b0045d06ea5575so10241546edd.6 for ; Tue, 18 Oct 2022 06:31:28 -0700 (PDT) Received: from avogadro.local ([2001:b07:6468:f312:2f4b:62da:3159:e077]) by smtp.gmail.com with ESMTPSA id p18-20020a17090653d200b0073dd1ac2fc8sm7473268ejo.195.2022.10.18.06.31.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 06:31:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666099889; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/E80xi7+DHvPCJRY31HtVvxNIdUIG8v2vBsVZ9TVoa0=; b=dVubEo+rUvW23YV3gldsdrlefN0qlSubukRjLxO3D3uE4VwEmJzzb7YAhN1/YnSalGg2XR Q/g0KF1BOWVk3p/m+GlBHwiTQkJyek/akLhsXTEKDmN+nAp7AcLjvKBJExvYD0doftijcL 1vQFOPyaHSZ/Tbz25DCsLDiHlnrmYwQ= X-MC-Unique: sbg_BKqsMLWPz2sPAdlEFg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/E80xi7+DHvPCJRY31HtVvxNIdUIG8v2vBsVZ9TVoa0=; b=dnc9bvEFvofcO5RKH0Ixk8CnYQprE5WgVfXFPyEhWA7WBRzq0To5cQ25dZcQMx92KI YAJx3dAs8LTVh0kBAQRd7iUtDLlJ/S+XyAazIW+dMpqtiegwsVLBd9awx2TZl0D9E+wz 9g+Kr+3RgPdXrnewdP2wjCG5XVJ74Y2Fn4DCVFHnCKMNld62PYZX18TVio2v8AiNrP+T FeQp9ptIWh311qbrlPICoyLWW0MYi0kvb/i73FjhUe67+73D9HDdAQ9noyJ3BuIX3OlZ 2lb7b9KGU4PwdTbznPHj9X4aI1AgG4xKf2JBEOgnQm2FFPBYY35a24PAxehiOdOfJPjm oY+g== X-Gm-Message-State: ACrzQf0eZLVI+ghQfSDHk9dKNBz/HjJnxR/nGf0vc7MieA6xO2YhkGki PIb7RCuH5ujeY02aLjzf4Y+fV43uLlMtL9wDIhE7Kie+afavYknXOWAWxAmU7RdDwf08gbPXhCb RxtdM8HqrpXjvblxgsC/p1+vq6ba+Y9734XVHGpeLSpb6eLI62Mnl1p/3j79rSSh4L6c= X-Received: by 2002:a05:6402:2706:b0:45d:aaae:e74a with SMTP id y6-20020a056402270600b0045daaaee74amr2591810edd.72.1666099886962; Tue, 18 Oct 2022 06:31:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5BNJkPzxsDR1qZ2N2cZCB2uxUmBEjOE9Hlrz/QymCjSy6ntJXVdD2ORs0ZyM57z/1rrN/NmQ== X-Received: by 2002:a05:6402:2706:b0:45d:aaae:e74a with SMTP id y6-20020a056402270600b0045daaaee74amr2591777edd.72.1666099886547; Tue, 18 Oct 2022 06:31:26 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson Subject: [PULL 12/53] target/i386: Reorg GET_HPHYS Date: Tue, 18 Oct 2022 15:30:01 +0200 Message-Id: <20221018133042.856368-13-pbonzini@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221018133042.856368-1-pbonzini@redhat.com> References: <20221018133042.856368-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.256, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666101030781100001 Content-Type: text/plain; charset="utf-8" From: Richard Henderson Replace with PTE_HPHYS for the page table walk, and a direct call to mmu_translate for the final stage2 translation. Hoist the check for HF2_NPT_MASK out to get_physical_address, which avoids the recursive call when stage2 is disabled. We can now return all the way out to x86_cpu_tlb_fill before raising an exception, which means probe works. Signed-off-by: Richard Henderson Message-Id: <20221002172956.265735-5-richard.henderson@linaro.org> Signed-off-by: Paolo Bonzini --- target/i386/tcg/sysemu/excp_helper.c | 123 +++++++++++++++++++++------ 1 file changed, 95 insertions(+), 28 deletions(-) diff --git a/target/i386/tcg/sysemu/excp_helper.c b/target/i386/tcg/sysemu/= excp_helper.c index 00ce4cf253..816b307547 100644 --- a/target/i386/tcg/sysemu/excp_helper.c +++ b/target/i386/tcg/sysemu/excp_helper.c @@ -37,18 +37,43 @@ typedef struct TranslateResult { int page_size; } TranslateResult; =20 +typedef enum TranslateFaultStage2 { + S2_NONE, + S2_GPA, + S2_GPT, +} TranslateFaultStage2; + typedef struct TranslateFault { int exception_index; int error_code; target_ulong cr2; + TranslateFaultStage2 stage2; } TranslateFault; =20 -#define GET_HPHYS(cs, gpa, access_type, prot) \ - (in->use_stage2 ? get_hphys(cs, gpa, access_type, prot) : gpa) +#define PTE_HPHYS(ADDR) \ + do { \ + if (in->use_stage2) { \ + nested_in.addr =3D (ADDR); \ + if (!mmu_translate(env, &nested_in, out, err)) { \ + err->stage2 =3D S2_GPT; \ + return false; \ + } \ + (ADDR) =3D out->paddr; \ + } \ + } while (0) =20 static bool mmu_translate(CPUX86State *env, const TranslateParams *in, TranslateResult *out, TranslateFault *err) { + TranslateParams nested_in =3D { + /* Use store for page table entries, to allow A/D flag updates. */ + .access_type =3D MMU_DATA_STORE, + .cr3 =3D env->nested_cr3, + .pg_mode =3D env->nested_pg_mode, + .mmu_idx =3D MMU_USER_IDX, + .use_stage2 =3D false, + }; + CPUState *cs =3D env_cpu(env); X86CPU *cpu =3D env_archcpu(env); const int32_t a20_mask =3D x86_get_a20_mask(env); @@ -79,7 +104,7 @@ static bool mmu_translate(CPUX86State *env, const Transl= ateParams *in, if (la57) { pml5e_addr =3D ((in->cr3 & ~0xfff) + (((addr >> 48) & 0x1ff) << 3)) & a20_mask; - pml5e_addr =3D GET_HPHYS(cs, pml5e_addr, MMU_DATA_STORE, N= ULL); + PTE_HPHYS(pml5e_addr); pml5e =3D x86_ldq_phys(cs, pml5e_addr); if (!(pml5e & PG_PRESENT_MASK)) { goto do_fault; @@ -99,7 +124,7 @@ static bool mmu_translate(CPUX86State *env, const Transl= ateParams *in, =20 pml4e_addr =3D ((pml5e & PG_ADDRESS_MASK) + (((addr >> 39) & 0x1ff) << 3)) & a20_mask; - pml4e_addr =3D GET_HPHYS(cs, pml4e_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pml4e_addr); pml4e =3D x86_ldq_phys(cs, pml4e_addr); if (!(pml4e & PG_PRESENT_MASK)) { goto do_fault; @@ -114,7 +139,7 @@ static bool mmu_translate(CPUX86State *env, const Trans= lateParams *in, ptep &=3D pml4e ^ PG_NX_MASK; pdpe_addr =3D ((pml4e & PG_ADDRESS_MASK) + (((addr >> 30) & 0x= 1ff) << 3)) & a20_mask; - pdpe_addr =3D GET_HPHYS(cs, pdpe_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pdpe_addr); pdpe =3D x86_ldq_phys(cs, pdpe_addr); if (!(pdpe & PG_PRESENT_MASK)) { goto do_fault; @@ -140,7 +165,7 @@ static bool mmu_translate(CPUX86State *env, const Trans= lateParams *in, /* XXX: load them when cr3 is loaded ? */ pdpe_addr =3D ((in->cr3 & ~0x1f) + ((addr >> 27) & 0x18)) & a20_mask; - pdpe_addr =3D GET_HPHYS(cs, pdpe_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pdpe_addr); pdpe =3D x86_ldq_phys(cs, pdpe_addr); if (!(pdpe & PG_PRESENT_MASK)) { goto do_fault; @@ -154,7 +179,7 @@ static bool mmu_translate(CPUX86State *env, const Trans= lateParams *in, =20 pde_addr =3D ((pdpe & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) <= < 3)) & a20_mask; - pde_addr =3D GET_HPHYS(cs, pde_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pde_addr); pde =3D x86_ldq_phys(cs, pde_addr); if (!(pde & PG_PRESENT_MASK)) { goto do_fault; @@ -177,7 +202,7 @@ static bool mmu_translate(CPUX86State *env, const Trans= lateParams *in, } pte_addr =3D ((pde & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) <<= 3)) & a20_mask; - pte_addr =3D GET_HPHYS(cs, pte_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pte_addr); pte =3D x86_ldq_phys(cs, pte_addr); if (!(pte & PG_PRESENT_MASK)) { goto do_fault; @@ -194,7 +219,7 @@ static bool mmu_translate(CPUX86State *env, const Trans= lateParams *in, /* page directory entry */ pde_addr =3D ((in->cr3 & ~0xfff) + ((addr >> 20) & 0xffc)) & a20_mask; - pde_addr =3D GET_HPHYS(cs, pde_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pde_addr); pde =3D x86_ldl_phys(cs, pde_addr); if (!(pde & PG_PRESENT_MASK)) { goto do_fault; @@ -222,7 +247,7 @@ static bool mmu_translate(CPUX86State *env, const Trans= lateParams *in, /* page directory entry */ pte_addr =3D ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & a20_mask; - pte_addr =3D GET_HPHYS(cs, pte_addr, MMU_DATA_STORE, NULL); + PTE_HPHYS(pte_addr); pte =3D x86_ldl_phys(cs, pte_addr); if (!(pte & PG_PRESENT_MASK)) { goto do_fault; @@ -303,13 +328,31 @@ do_check_protect_pse36: assert(access_type !=3D MMU_DATA_STORE); prot &=3D ~PAGE_WRITE; } - out->prot =3D prot; - out->page_size =3D page_size; =20 /* align to page_size */ out->paddr =3D (pte & a20_mask & PG_ADDRESS_MASK & ~(page_size - 1)) | (addr & (page_size - 1)); - out->paddr =3D GET_HPHYS(cs, out->paddr, access_type, &out->prot); + + if (in->use_stage2) { + nested_in.addr =3D out->paddr; + nested_in.access_type =3D access_type; + + if (!mmu_translate(env, &nested_in, out, err)) { + err->stage2 =3D S2_GPA; + return false; + } + + /* Merge stage1 & stage2 protection bits. */ + prot &=3D out->prot; + + /* Re-verify resulting protection. */ + if ((prot & (1 << access_type)) =3D=3D 0) { + goto do_fault_protect; + } + } + + out->prot =3D prot; + out->page_size =3D page_size; return true; =20 int error_code; @@ -344,13 +387,36 @@ do_check_protect_pse36: err->exception_index =3D EXCP0E_PAGE; err->error_code =3D error_code; err->cr2 =3D addr; + err->stage2 =3D S2_NONE; return false; } =20 +static G_NORETURN void raise_stage2(CPUX86State *env, TranslateFault *err, + uintptr_t retaddr) +{ + uint64_t exit_info_1 =3D err->error_code; + + switch (err->stage2) { + case S2_GPT: + exit_info_1 |=3D SVM_NPTEXIT_GPT; + break; + case S2_GPA: + exit_info_1 |=3D SVM_NPTEXIT_GPA; + break; + default: + g_assert_not_reached(); + } + + x86_stq_phys(env_cpu(env), + env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), + err->cr2); + cpu_vmexit(env, SVM_EXIT_NPF, exit_info_1, retaddr); +} + hwaddr get_hphys(CPUState *cs, hwaddr gphys, MMUAccessType access_type, int *prot) { - CPUX86State *env =3D &X86_CPU(cs)->env; + CPUX86State *env =3D cs->env_ptr; =20 if (likely(!(env->hflags2 & HF2_NPT_MASK))) { return gphys; @@ -365,20 +431,16 @@ hwaddr get_hphys(CPUState *cs, hwaddr gphys, MMUAcces= sType access_type, }; TranslateResult out; TranslateFault err; - uint64_t exit_info_1; =20 - if (mmu_translate(env, &in, &out, &err)) { - if (prot) { - *prot &=3D out.prot; - } - return out.paddr; + if (!mmu_translate(env, &in, &out, &err)) { + err.stage2 =3D prot ? SVM_NPTEXIT_GPA : SVM_NPTEXIT_GPT; + raise_stage2(env, &err, env->retaddr); } =20 - x86_stq_phys(cs, env->vm_vmcb + - offsetof(struct vmcb, control.exit_info_2), gphys); - exit_info_1 =3D err.error_code - | (prot ? SVM_NPTEXIT_GPA : SVM_NPTEXIT_GPT); - cpu_vmexit(env, SVM_EXIT_NPF, exit_info_1, env->retaddr); + if (prot) { + *prot &=3D out.prot; + } + return out.paddr; } } =20 @@ -405,7 +467,7 @@ static bool get_physical_address(CPUX86State *env, vadd= r addr, .pg_mode =3D get_pg_mode(env), .mmu_idx =3D mmu_idx, .access_type =3D access_type, - .use_stage2 =3D true + .use_stage2 =3D env->hflags2 & HF2_NPT_MASK, }; =20 if (in.pg_mode & PG_MODE_LMA) { @@ -444,8 +506,13 @@ bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int si= ze, return true; } =20 - /* FIXME: On error in get_hphys we have already jumped out. */ - g_assert(!probe); + if (probe) { + return false; + } + + if (err.stage2 !=3D S2_NONE) { + raise_stage2(env, &err, retaddr); + } =20 if (env->intercept_exceptions & (1 << err.exception_index)) { /* cr2 is not modified in case of exceptions */ --=20 2.37.3