From patchwork Tue Oct 18 13:30:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 616066 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp2651772pvb; Tue, 18 Oct 2022 07:05:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5KCA4HCIvWJkQhEcuOqP2kgUx5gAwfCbXg3WFSTfX+klQF04id10v8lgMRUfhYbhI1RA0z X-Received: by 2002:ac8:5d49:0:b0:39c:c082:1417 with SMTP id g9-20020ac85d49000000b0039cc0821417mr2180713qtx.687.1666101948900; Tue, 18 Oct 2022 07:05:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666101948; cv=none; d=google.com; s=arc-20160816; b=YjnvD030oIAmaD6qUNviECJ3UK02u4U5k++vfzg/o+TmTlpoYlg7na7DyNcoB5hPt/ Dlk8Z77h1BHmGvChlF5F+UpZBBBlkbn0ZtQcFCxF/FvtgXTKJgGvcHemzLhOCubOW1Un tBZi94oZNpE1mA6cZKzJaz6Yenc0M/1YfFDwqwp48U/3aW3qWGf/kqY4ygwtZvJx7gI1 RzfZQdGhAQa5l8/ob5cf7CSgXn9mpegn0fXPb+KeqkY+Yw2uWMSmfZ2RunYg0zU3G1tD R+r8QDGLcPs1Cwcb2z2P84Z+U9kLB/ob1gElFyBA+U1gBfZuk2xvpTvQFqyl3Uqqt/LO gdIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=v5dp7+qCUGzBtcpQe2Asqx5dhgaedWRqcldb8Y4LGrY=; b=TUVyMJOzsTcYvQoq9zV2hwYagT/fGEnDQZtYqRMv0exoJt2TqTSA7qP0tUAzEew6LH m1F9rJilgSCbjWP09sYSqOcZtBiaGpgp4ewhBjEg0mIDERvDKJzoOE4jeo2lnzthfYT2 DOBCzDqz30ZScwu8nF0Zwt7Cv97mzHbJl/iTbXjWS3nEEAVYzIp63/Eye7Vd7Bin9V2O 4xf5aS+L1l8dU8W0oNtojyGWzcj4WQtWbqDHQfkogSIpfIW5Y8zrmTnPjBREqKoxOuKy +rSx62CDXj450OJbmOIT0H6jgB1vu/HctTMVCETFgSbMRaRuuB64iDOYEkcIADUrx1v6 P2kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=W07nqTcA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id de31-20020a05620a371f00b006cebe06478dsi7777150qkb.659.2022.10.18.07.05.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 18 Oct 2022 07:05:48 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=W07nqTcA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([::1]:57310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oknE4-00055A-2s for patch@linaro.org; Tue, 18 Oct 2022 10:05:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37162) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okmh0-0002Re-M2 for qemu-devel@nongnu.org; Tue, 18 Oct 2022 09:31:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:51872) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okmgw-00052C-Pk for qemu-devel@nongnu.org; Tue, 18 Oct 2022 09:31:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666099894; 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=v5dp7+qCUGzBtcpQe2Asqx5dhgaedWRqcldb8Y4LGrY=; b=W07nqTcAJ75RfIansXUVP5Zq8ceiqNzTz8og1C0SGs3j2YaiLzzPAPv1z3MX47Zb9+e9aM FWiqbYNdujF6HzN+X/T9tOD/sVLU4BBMe9+T12+03ao1BGREfUQYk/t/KVqnRgESwMZhgr EJShVKsxWZu0bXxYR0Ac/dXuN95+e+o= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-504-Gh_sDQOzPZ6G9Q5VDz3Ebg-1; Tue, 18 Oct 2022 09:31:31 -0400 X-MC-Unique: Gh_sDQOzPZ6G9Q5VDz3Ebg-1 Received: by mail-ej1-f70.google.com with SMTP id xc12-20020a170907074c00b007416699ea14so6383939ejb.19 for ; Tue, 18 Oct 2022 06:31:31 -0700 (PDT) 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=v5dp7+qCUGzBtcpQe2Asqx5dhgaedWRqcldb8Y4LGrY=; b=oTCUPqSOySAR5VIfm3o1zBwnRgpyrxKvwTG9ieF/SzWYPzD6mr1OBBqyojAWd5LnNM f06Y/askZ/2706GUsjfnzZK//MUIUou/Hug2IhpTbGp4k09pRaVWX8eHZVBB12UzVbSl rbp4NMDxerkb70rwxgp4iN2/d++AdfGH3idiYe+eyz2uwI/F8pjDkK4CBmOW6y9hBJJA sN1vK/VBzNumaFEe4FVO/Xp/g5ssAGk+RZ1cuCkk7OZ27u33nqFfCwC0O5B21zpsAO5m HPdoWkp0H4/NL6WUCBOJuDrRut3KlbWLlo4vQ1i4HYGzVG2n4S4RX92swpk6IsDU20Wi yh+w== X-Gm-Message-State: ACrzQf1l3QsLS9jkfaHg9hL/kLNiHh/kDLGZ24ui2QKMqk5b8RrDKTe9 CS1vtm8IiCqiD/h4LKIEKYQdem9rBXk99bcZcfoy5f1C+MGhIAR45ET6rX27Ge8ltQfIt4SazKB jhg6jK6+Ck/BStQwLqGmu/ni1bhymTyhrzj2g3nZoUNWU05vfcrsabI6J2e/vn0izCiY= X-Received: by 2002:a05:6402:10c2:b0:45c:3c87:721f with SMTP id p2-20020a05640210c200b0045c3c87721fmr2731214edu.251.1666099889904; Tue, 18 Oct 2022 06:31:29 -0700 (PDT) X-Received: by 2002:a05:6402:10c2:b0:45c:3c87:721f with SMTP id p2-20020a05640210c200b0045c3c87721fmr2731183edu.251.1666099889557; Tue, 18 Oct 2022 06:31:29 -0700 (PDT) Received: from avogadro.local ([2001:b07:6468:f312:2f4b:62da:3159:e077]) by smtp.gmail.com with ESMTPSA id md9-20020a170906ae8900b0078defb88b0dsm7454622ejb.73.2022.10.18.06.31.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 06:31:28 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson Subject: [PULL 13/53] target/i386: Add MMU_PHYS_IDX and MMU_NESTED_IDX Date: Tue, 18 Oct 2022 15:30:02 +0200 Message-Id: <20221018133042.856368-14-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 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Richard Henderson These new mmu indexes will be helpful for improving paging and code throughout the target. Signed-off-by: Richard Henderson Message-Id: <20221002172956.265735-6-richard.henderson@linaro.org> Signed-off-by: Paolo Bonzini --- target/i386/cpu-param.h | 2 +- target/i386/cpu.h | 3 + target/i386/tcg/sysemu/excp_helper.c | 82 ++++++++++++++++++---------- target/i386/tcg/sysemu/svm_helper.c | 3 + 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/target/i386/cpu-param.h b/target/i386/cpu-param.h index 1e79389761..f579b16bd2 100644 --- a/target/i386/cpu-param.h +++ b/target/i386/cpu-param.h @@ -23,7 +23,7 @@ # define TARGET_VIRT_ADDR_SPACE_BITS 32 #endif #define TARGET_PAGE_BITS 12 -#define NB_MMU_MODES 3 +#define NB_MMU_MODES 5 #ifndef CONFIG_USER_ONLY # define TARGET_TB_PCREL 1 diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 4d21c5759d..3573107356 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2149,6 +2149,9 @@ uint64_t cpu_get_tsc(CPUX86State *env); #define MMU_KSMAP_IDX 0 #define MMU_USER_IDX 1 #define MMU_KNOSMAP_IDX 2 +#define MMU_NESTED_IDX 3 +#define MMU_PHYS_IDX 4 + static inline int cpu_mmu_index(CPUX86State *env, bool ifetch) { return (env->hflags & HF_CPL_MASK) == 3 ? MMU_USER_IDX : diff --git a/target/i386/tcg/sysemu/excp_helper.c b/target/i386/tcg/sysemu/excp_helper.c index 816b307547..494dc6d00c 100644 --- a/target/i386/tcg/sysemu/excp_helper.c +++ b/target/i386/tcg/sysemu/excp_helper.c @@ -448,41 +448,65 @@ static bool get_physical_address(CPUX86State *env, vaddr addr, MMUAccessType access_type, int mmu_idx, TranslateResult *out, TranslateFault *err) { - if (!(env->cr[0] & CR0_PG_MASK)) { - out->paddr = addr & x86_get_a20_mask(env); + TranslateParams in; + bool use_stage2 = env->hflags2 & HF2_NPT_MASK; -#ifdef TARGET_X86_64 - if (!(env->hflags & HF_LMA_MASK)) { - /* Without long mode we can only address 32bits in real mode */ - out->paddr = (uint32_t)out->paddr; - } -#endif - out->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; - out->page_size = TARGET_PAGE_SIZE; - return true; - } else { - TranslateParams in = { - .addr = addr, - .cr3 = env->cr[3], - .pg_mode = get_pg_mode(env), - .mmu_idx = mmu_idx, - .access_type = access_type, - .use_stage2 = env->hflags2 & HF2_NPT_MASK, - }; + in.addr = addr; + in.access_type = access_type; - if (in.pg_mode & PG_MODE_LMA) { - /* test virtual address sign extension */ - int shift = in.pg_mode & PG_MODE_LA57 ? 56 : 47; - int64_t sext = (int64_t)addr >> shift; - if (sext != 0 && sext != -1) { - err->exception_index = EXCP0D_GPF; - err->error_code = 0; - err->cr2 = addr; + switch (mmu_idx) { + case MMU_PHYS_IDX: + break; + + case MMU_NESTED_IDX: + if (likely(use_stage2)) { + in.cr3 = env->nested_cr3; + in.pg_mode = env->nested_pg_mode; + in.mmu_idx = MMU_USER_IDX; + in.use_stage2 = false; + + if (!mmu_translate(env, &in, out, err)) { + err->stage2 = S2_GPA; return false; } + return true; } - return mmu_translate(env, &in, out, err); + break; + + default: + in.cr3 = env->cr[3]; + in.mmu_idx = mmu_idx; + in.use_stage2 = use_stage2; + in.pg_mode = get_pg_mode(env); + + if (likely(in.pg_mode)) { + if (in.pg_mode & PG_MODE_LMA) { + /* test virtual address sign extension */ + int shift = in.pg_mode & PG_MODE_LA57 ? 56 : 47; + int64_t sext = (int64_t)addr >> shift; + if (sext != 0 && sext != -1) { + err->exception_index = EXCP0D_GPF; + err->error_code = 0; + err->cr2 = addr; + return false; + } + } + return mmu_translate(env, &in, out, err); + } + break; } + + /* Translation disabled. */ + out->paddr = addr & x86_get_a20_mask(env); +#ifdef TARGET_X86_64 + if (!(env->hflags & HF_LMA_MASK)) { + /* Without long mode we can only address 32bits in real mode */ + out->paddr = (uint32_t)out->paddr; + } +#endif + out->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; + out->page_size = TARGET_PAGE_SIZE; + return true; } bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, diff --git a/target/i386/tcg/sysemu/svm_helper.c b/target/i386/tcg/sysemu/svm_helper.c index 2b6f450af9..85b7741d94 100644 --- a/target/i386/tcg/sysemu/svm_helper.c +++ b/target/i386/tcg/sysemu/svm_helper.c @@ -271,6 +271,8 @@ void helper_vmrun(CPUX86State *env, int aflag, int next_eip_addend) env->hflags2 |= HF2_NPT_MASK; env->nested_pg_mode = get_pg_mode(env) & PG_MODE_SVM_MASK; + + tlb_flush_by_mmuidx(cs, 1 << MMU_NESTED_IDX); } /* enable intercepts */ @@ -720,6 +722,7 @@ void do_vmexit(CPUX86State *env) env->vm_vmcb + offsetof(struct vmcb, control.int_state), 0); } env->hflags2 &= ~HF2_NPT_MASK; + tlb_flush_by_mmuidx(cs, 1 << MMU_NESTED_IDX); /* Save the VM state in the vmcb */ svm_save_seg(env, env->vm_vmcb + offsetof(struct vmcb, save.es),