From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407695324911.6145710134507; Thu, 24 Jul 2025 18:41:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PS-0001zz-Dp; Thu, 24 Jul 2025 21:39:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nf-0000gy-Dh for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:51 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nc-0002Z6-B2 for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:51 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Axz3Pl34Jof5AxAQ--.34148S3; Fri, 25 Jul 2025 09:37:41 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S3; Fri, 25 Jul 2025 09:37:40 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 01/17] target/loongarch: Move some function definition to kvm directory Date: Fri, 25 Jul 2025 09:37:23 +0800 Message-Id: <20250725013739.994437-2-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407696047116600 Content-Type: text/plain; charset="utf-8" Move function definition specified with kvm to the corresponding directory. Also remove header file "cpu.h" including outside of macro QEMU_KVM_LOONGARCH_H. Signed-off-by: Bibo Mao Reviewed-by: Richard Henderson --- hw/loongarch/virt.c | 1 + target/loongarch/cpu.h | 9 --------- target/loongarch/kvm/kvm_loongarch.h | 4 ++-- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c index b15ada2078..31215b7785 100644 --- a/hw/loongarch/virt.c +++ b/hw/loongarch/virt.c @@ -46,6 +46,7 @@ #include "hw/block/flash.h" #include "hw/virtio/virtio-iommu.h" #include "qemu/error-report.h" +#include "kvm/kvm_loongarch.h" =20 static void virt_get_veiointc(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h index 9538e8d61d..bbe6db33f1 100644 --- a/target/loongarch/cpu.h +++ b/target/loongarch/cpu.h @@ -496,13 +496,4 @@ static inline void set_pc(CPULoongArchState *env, uint= 64_t value) =20 void loongarch_cpu_post_init(Object *obj); =20 -#ifdef CONFIG_KVM -void kvm_loongarch_cpu_post_init(LoongArchCPU *cpu); -#else -static inline void kvm_loongarch_cpu_post_init(LoongArchCPU *cpu) -{ -} -#endif -void kvm_loongarch_init_irq_routing(void); - #endif /* LOONGARCH_CPU_H */ diff --git a/target/loongarch/kvm/kvm_loongarch.h b/target/loongarch/kvm/kv= m_loongarch.h index 1051a341ec..51475675d6 100644 --- a/target/loongarch/kvm/kvm_loongarch.h +++ b/target/loongarch/kvm/kvm_loongarch.h @@ -5,11 +5,11 @@ * Copyright (c) 2023 Loongson Technology Corporation Limited */ =20 -#include "cpu.h" - #ifndef QEMU_KVM_LOONGARCH_H #define QEMU_KVM_LOONGARCH_H =20 +void kvm_loongarch_cpu_post_init(LoongArchCPU *cpu); +void kvm_loongarch_init_irq_routing(void); int kvm_loongarch_set_interrupt(LoongArchCPU *cpu, int irq, int level); void kvm_arch_reset_vcpu(CPUState *cs); =20 --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407760909907.3095895961544; Thu, 24 Jul 2025 18:42:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PP-0001xL-LW; Thu, 24 Jul 2025 21:39:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nh-0000jS-1w for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nc-0002Z7-Rn for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:52 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8BxnnPl34JogpAxAQ--.64627S3; Fri, 25 Jul 2025 09:37:41 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S4; Fri, 25 Jul 2025 09:37:41 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 02/17] target/loongarch: Define function loongarch_cpu_post_init as static Date: Fri, 25 Jul 2025 09:37:24 +0800 Message-Id: <20250725013739.994437-3-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S4 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407762737116600 Content-Type: text/plain; charset="utf-8" Function loongarch_cpu_post_init() is implemented and used in the same file target/loongarch/cpu.c, it can be defined as static function. This patch moves implementation about function loongarch_cpu_post_init() before it is referenced. And it is only code movement, no function change. Signed-off-by: Bibo Mao Reviewed-by: Richard Henderson --- target/loongarch/cpu.c | 180 ++++++++++++++++++++--------------------- target/loongarch/cpu.h | 2 - 2 files changed, 90 insertions(+), 92 deletions(-) diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c index abad84c054..b96429ffb1 100644 --- a/target/loongarch/cpu.c +++ b/target/loongarch/cpu.c @@ -422,6 +422,96 @@ static void loongarch_la464_init_csr(Object *obj) #endif } =20 +static bool loongarch_get_lsx(Object *obj, Error **errp) +{ + return LOONGARCH_CPU(obj)->lsx !=3D ON_OFF_AUTO_OFF; +} + +static void loongarch_set_lsx(Object *obj, bool value, Error **errp) +{ + LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); + uint32_t val; + + cpu->lsx =3D value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; + if (cpu->lsx =3D=3D ON_OFF_AUTO_OFF) { + cpu->lasx =3D ON_OFF_AUTO_OFF; + if (cpu->lasx =3D=3D ON_OFF_AUTO_ON) { + error_setg(errp, "Failed to disable LSX since LASX is enabled"= ); + return; + } + } + + if (kvm_enabled()) { + /* kvm feature detection in function kvm_arch_init_vcpu */ + return; + } + + /* LSX feature detection in TCG mode */ + val =3D cpu->env.cpucfg[2]; + if (cpu->lsx =3D=3D ON_OFF_AUTO_ON) { + if (FIELD_EX32(val, CPUCFG2, LSX) =3D=3D 0) { + error_setg(errp, "Failed to enable LSX in TCG mode"); + return; + } + } else { + cpu->env.cpucfg[2] =3D FIELD_DP32(val, CPUCFG2, LASX, 0); + val =3D cpu->env.cpucfg[2]; + } + + cpu->env.cpucfg[2] =3D FIELD_DP32(val, CPUCFG2, LSX, value); +} + +static bool loongarch_get_lasx(Object *obj, Error **errp) +{ + return LOONGARCH_CPU(obj)->lasx !=3D ON_OFF_AUTO_OFF; +} + +static void loongarch_set_lasx(Object *obj, bool value, Error **errp) +{ + LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); + uint32_t val; + + cpu->lasx =3D value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; + if ((cpu->lsx =3D=3D ON_OFF_AUTO_OFF) && (cpu->lasx =3D=3D ON_OFF_AUTO= _ON)) { + error_setg(errp, "Failed to enable LASX since lSX is disabled"); + return; + } + + if (kvm_enabled()) { + /* kvm feature detection in function kvm_arch_init_vcpu */ + return; + } + + /* LASX feature detection in TCG mode */ + val =3D cpu->env.cpucfg[2]; + if (cpu->lasx =3D=3D ON_OFF_AUTO_ON) { + if (FIELD_EX32(val, CPUCFG2, LASX) =3D=3D 0) { + error_setg(errp, "Failed to enable LASX in TCG mode"); + return; + } + } + + cpu->env.cpucfg[2] =3D FIELD_DP32(val, CPUCFG2, LASX, value); +} + +static void loongarch_cpu_post_init(Object *obj) +{ + LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); + + cpu->lbt =3D ON_OFF_AUTO_OFF; + cpu->pmu =3D ON_OFF_AUTO_OFF; + cpu->lsx =3D ON_OFF_AUTO_AUTO; + cpu->lasx =3D ON_OFF_AUTO_AUTO; + object_property_add_bool(obj, "lsx", loongarch_get_lsx, + loongarch_set_lsx); + object_property_add_bool(obj, "lasx", loongarch_get_lasx, + loongarch_set_lasx); + /* lbt is enabled only in kvm mode, not supported in tcg mode */ + if (kvm_enabled()) { + kvm_loongarch_cpu_post_init(cpu); + } +} + static void loongarch_la464_initfn(Object *obj) { LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); @@ -683,96 +773,6 @@ static void loongarch_cpu_unrealizefn(DeviceState *dev) lacc->parent_unrealize(dev); } =20 -static bool loongarch_get_lsx(Object *obj, Error **errp) -{ - return LOONGARCH_CPU(obj)->lsx !=3D ON_OFF_AUTO_OFF; -} - -static void loongarch_set_lsx(Object *obj, bool value, Error **errp) -{ - LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); - uint32_t val; - - cpu->lsx =3D value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; - if (cpu->lsx =3D=3D ON_OFF_AUTO_OFF) { - cpu->lasx =3D ON_OFF_AUTO_OFF; - if (cpu->lasx =3D=3D ON_OFF_AUTO_ON) { - error_setg(errp, "Failed to disable LSX since LASX is enabled"= ); - return; - } - } - - if (kvm_enabled()) { - /* kvm feature detection in function kvm_arch_init_vcpu */ - return; - } - - /* LSX feature detection in TCG mode */ - val =3D cpu->env.cpucfg[2]; - if (cpu->lsx =3D=3D ON_OFF_AUTO_ON) { - if (FIELD_EX32(val, CPUCFG2, LSX) =3D=3D 0) { - error_setg(errp, "Failed to enable LSX in TCG mode"); - return; - } - } else { - cpu->env.cpucfg[2] =3D FIELD_DP32(val, CPUCFG2, LASX, 0); - val =3D cpu->env.cpucfg[2]; - } - - cpu->env.cpucfg[2] =3D FIELD_DP32(val, CPUCFG2, LSX, value); -} - -static bool loongarch_get_lasx(Object *obj, Error **errp) -{ - return LOONGARCH_CPU(obj)->lasx !=3D ON_OFF_AUTO_OFF; -} - -static void loongarch_set_lasx(Object *obj, bool value, Error **errp) -{ - LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); - uint32_t val; - - cpu->lasx =3D value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; - if ((cpu->lsx =3D=3D ON_OFF_AUTO_OFF) && (cpu->lasx =3D=3D ON_OFF_AUTO= _ON)) { - error_setg(errp, "Failed to enable LASX since lSX is disabled"); - return; - } - - if (kvm_enabled()) { - /* kvm feature detection in function kvm_arch_init_vcpu */ - return; - } - - /* LASX feature detection in TCG mode */ - val =3D cpu->env.cpucfg[2]; - if (cpu->lasx =3D=3D ON_OFF_AUTO_ON) { - if (FIELD_EX32(val, CPUCFG2, LASX) =3D=3D 0) { - error_setg(errp, "Failed to enable LASX in TCG mode"); - return; - } - } - - cpu->env.cpucfg[2] =3D FIELD_DP32(val, CPUCFG2, LASX, value); -} - -void loongarch_cpu_post_init(Object *obj) -{ - LoongArchCPU *cpu =3D LOONGARCH_CPU(obj); - - cpu->lbt =3D ON_OFF_AUTO_OFF; - cpu->pmu =3D ON_OFF_AUTO_OFF; - cpu->lsx =3D ON_OFF_AUTO_AUTO; - cpu->lasx =3D ON_OFF_AUTO_AUTO; - object_property_add_bool(obj, "lsx", loongarch_get_lsx, - loongarch_set_lsx); - object_property_add_bool(obj, "lasx", loongarch_get_lasx, - loongarch_set_lasx); - /* lbt is enabled only in kvm mode, not supported in tcg mode */ - if (kvm_enabled()) { - kvm_loongarch_cpu_post_init(cpu); - } -} - static void loongarch_cpu_init(Object *obj) { #ifndef CONFIG_USER_ONLY diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h index bbe6db33f1..7731f6acdc 100644 --- a/target/loongarch/cpu.h +++ b/target/loongarch/cpu.h @@ -494,6 +494,4 @@ static inline void set_pc(CPULoongArchState *env, uint6= 4_t value) =20 #define CPU_RESOLVING_TYPE TYPE_LOONGARCH_CPU =20 -void loongarch_cpu_post_init(Object *obj); - #endif /* LOONGARCH_CPU_H */ --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407615855755.7025032110311; Thu, 24 Jul 2025 18:40:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Pk-0002Ik-Ks; Thu, 24 Jul 2025 21:40:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nf-0000hJ-Pz for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:52 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nc-0002Z8-BY for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:51 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8DxQK_l34Jog5AxAQ--.65435S3; Fri, 25 Jul 2025 09:37:41 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S5; Fri, 25 Jul 2025 09:37:41 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 03/17] target/loongarch: Set page size in TLB misc with STLB Date: Fri, 25 Jul 2025 09:37:25 +0800 Message-Id: <20250725013739.994437-4-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S5 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407617492116600 Content-Type: text/plain; charset="utf-8" With VTLB different TLB entry may have different page size, and page size is set in PS field of TLB misc. However with STLB, all the TLB entries have the same page size, page size comes from register CSR_STLBPS, PS field of TLB misc is not used. Here PS field of TLB misc is used with all TLB entries, even with STLB, it is convenient with TLB maintainance operation. Signed-off-by: Bibo Mao --- target/loongarch/tcg/tlb_helper.c | 41 ++++++++----------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 8872593ff0..3ea0e153b1 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -110,11 +110,8 @@ static void invalidate_tlb_entry(CPULoongArchState *en= v, int index) if (!tlb_e) { return; } - if (index >=3D LOONGARCH_STLB) { - tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); - } else { - tlb_ps =3D FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS); - } + + tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); pagesize =3D MAKE_64BIT_MASK(tlb_ps, 1); mask =3D MAKE_64BIT_MASK(0, tlb_ps + 1); =20 @@ -173,11 +170,8 @@ static void fill_tlb_entry(CPULoongArchState *env, int= index) lo1 =3D env->CSR_TLBELO1; } =20 - /* Only MTLB has the ps fields */ - if (index >=3D LOONGARCH_STLB) { - tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, PS, csr_ps); - } - + /* Store page size in field PS */ + tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, PS, csr_ps); tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, VPPN, csr_vppn); tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 1); csr_asid =3D FIELD_EX64(env->CSR_ASID, CSR_ASID, ASID); @@ -283,12 +277,7 @@ void helper_tlbrd(CPULoongArchState *env) =20 index =3D FIELD_EX64(env->CSR_TLBIDX, CSR_TLBIDX, INDEX); tlb =3D &env->tlb[index]; - - if (index >=3D LOONGARCH_STLB) { - tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); - } else { - tlb_ps =3D FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS); - } + tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); tlb_e =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, E); =20 if (!tlb_e) { @@ -476,11 +465,8 @@ void helper_invtlb_page_asid(CPULoongArchState *env, t= arget_ulong info, if (!tlb_e) { continue; } - if (i >=3D LOONGARCH_STLB) { - tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); - } else { - tlb_ps =3D FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS); - } + + tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); tlb_vppn =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, VPPN); vpn =3D (addr & TARGET_VIRT_MASK) >> (tlb_ps + 1); compare_shift =3D tlb_ps + 1 - R_TLB_MISC_VPPN_SHIFT; @@ -509,11 +495,8 @@ void helper_invtlb_page_asid_or_g(CPULoongArchState *e= nv, if (!tlb_e) { continue; } - if (i >=3D LOONGARCH_STLB) { - tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); - } else { - tlb_ps =3D FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS); - } + + tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); tlb_vppn =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, VPPN); vpn =3D (addr & TARGET_VIRT_MASK) >> (tlb_ps + 1); compare_shift =3D tlb_ps + 1 - R_TLB_MISC_VPPN_SHIFT; @@ -673,11 +656,7 @@ static int loongarch_map_tlb_entry(CPULoongArchState *= env, hwaddr *physical, uint64_t tlb_entry, tlb_ppn; uint8_t tlb_ps, n, tlb_v, tlb_d, tlb_plv, tlb_nx, tlb_nr, tlb_rplv; =20 - if (index >=3D LOONGARCH_STLB) { - tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); - } else { - tlb_ps =3D FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS); - } + tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); n =3D (address >> tlb_ps) & 0x1;/* Odd or even */ =20 tlb_entry =3D n ? tlb->tlb_entry1 : tlb->tlb_entry0; --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407600606124.1055595389521; Thu, 24 Jul 2025 18:40:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PK-0001qr-Lw; Thu, 24 Jul 2025 21:39:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nf-0000hI-PN for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:52 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nc-0002Z9-BH for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:51 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8CxdXDm34JohpAxAQ--.59441S3; Fri, 25 Jul 2025 09:37:42 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S6; Fri, 25 Jul 2025 09:37:41 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 04/17] target/loongarch: Add header file cpu-mmu.h Date: Fri, 25 Jul 2025 09:37:26 +0800 Message-Id: <20250725013739.994437-5-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S6 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407604185116600 Content-Type: text/plain; charset="utf-8" New header file cpu-mmu.h is added and move mmu relative function declaration to this file. Signed-off-by: Bibo Mao Reviewed-by: Richard Henderson --- target/loongarch/cpu-mmu.h | 30 ++++++++++++++++++++++++++++++ target/loongarch/cpu.c | 1 + target/loongarch/cpu_helper.c | 1 + target/loongarch/internals.h | 20 -------------------- target/loongarch/tcg/csr_helper.c | 1 + target/loongarch/tcg/tlb_helper.c | 1 + 6 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 target/loongarch/cpu-mmu.h diff --git a/target/loongarch/cpu-mmu.h b/target/loongarch/cpu-mmu.h new file mode 100644 index 0000000000..4c5cbd7425 --- /dev/null +++ b/target/loongarch/cpu-mmu.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * LoongArch CPU parameters for QEMU. + * + * Copyright (c) 2025 Loongson Technology Corporation Limited + */ + +#ifndef LOONGARCH_CPU_MMU_H +#define LOONGARCH_CPU_MMU_H + +enum { + TLBRET_MATCH =3D 0, + TLBRET_BADADDR =3D 1, + TLBRET_NOMATCH =3D 2, + TLBRET_INVALID =3D 3, + TLBRET_DIRTY =3D 4, + TLBRET_RI =3D 5, + TLBRET_XI =3D 6, + TLBRET_PE =3D 7, +}; + +bool check_ps(CPULoongArchState *ent, uint8_t ps); +int get_physical_address(CPULoongArchState *env, hwaddr *physical, + int *prot, target_ulong address, + MMUAccessType access_type, int mmu_idx, int is_de= bug); +void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base, + uint64_t *dir_width, target_ulong level); +hwaddr loongarch_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr); + +#endif /* LOONGARCH_CPU_MMU_H */ diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c index b96429ffb1..990985708e 100644 --- a/target/loongarch/cpu.c +++ b/target/loongarch/cpu.c @@ -17,6 +17,7 @@ #include "hw/qdev-properties.h" #include "exec/translation-block.h" #include "cpu.h" +#include "cpu-mmu.h" #include "internals.h" #include "fpu/softfloat-helpers.h" #include "csr.h" diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index e172b11ce1..2e8d3d7cfb 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -13,6 +13,7 @@ #include "exec/target_page.h" #include "internals.h" #include "cpu-csr.h" +#include "cpu-mmu.h" #include "tcg/tcg_loongarch.h" =20 void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base, diff --git a/target/loongarch/internals.h b/target/loongarch/internals.h index a7384b0d31..e50d109767 100644 --- a/target/loongarch/internals.h +++ b/target/loongarch/internals.h @@ -32,19 +32,6 @@ void restore_fp_status(CPULoongArchState *env); #endif =20 #ifndef CONFIG_USER_ONLY -enum { - TLBRET_MATCH =3D 0, - TLBRET_BADADDR =3D 1, - TLBRET_NOMATCH =3D 2, - TLBRET_INVALID =3D 3, - TLBRET_DIRTY =3D 4, - TLBRET_RI =3D 5, - TLBRET_XI =3D 6, - TLBRET_PE =3D 7, -}; - -bool check_ps(CPULoongArchState *ent, uint8_t ps); - extern const VMStateDescription vmstate_loongarch_cpu; =20 void loongarch_cpu_set_irq(void *opaque, int irq, int level); @@ -54,13 +41,6 @@ uint64_t cpu_loongarch_get_constant_timer_counter(LoongA= rchCPU *cpu); uint64_t cpu_loongarch_get_constant_timer_ticks(LoongArchCPU *cpu); void cpu_loongarch_store_constant_timer_config(LoongArchCPU *cpu, uint64_t value); -int get_physical_address(CPULoongArchState *env, hwaddr *physical, - int *prot, target_ulong address, - MMUAccessType access_type, int mmu_idx, int is_de= bug); -void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base, - uint64_t *dir_width, target_ulong level); -hwaddr loongarch_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr); - #endif /* !CONFIG_USER_ONLY */ =20 uint64_t read_fcc(CPULoongArchState *env); diff --git a/target/loongarch/tcg/csr_helper.c b/target/loongarch/tcg/csr_h= elper.c index 28b1bb86bd..0d99e2c92b 100644 --- a/target/loongarch/tcg/csr_helper.c +++ b/target/loongarch/tcg/csr_helper.c @@ -16,6 +16,7 @@ #include "accel/tcg/cpu-ldst.h" #include "hw/irq.h" #include "cpu-csr.h" +#include "cpu-mmu.h" =20 target_ulong helper_csrwr_stlbps(CPULoongArchState *env, target_ulong val) { diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 3ea0e153b1..1f49619e7f 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -10,6 +10,7 @@ #include "qemu/guest-random.h" =20 #include "cpu.h" +#include "cpu-mmu.h" #include "internals.h" #include "exec/helper-proto.h" #include "exec/cputlb.h" --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407672471440.05704915530885; Thu, 24 Jul 2025 18:41:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PX-00024W-6D; Thu, 24 Jul 2025 21:39:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nh-0000l7-G9 for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nd-0002ab-4N for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8CxPuPm34JoiJAxAQ--.64959S3; Fri, 25 Jul 2025 09:37:42 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S7; Fri, 25 Jul 2025 09:37:42 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 05/17] target/loongarch: Add common function loongarch_check_pte() Date: Fri, 25 Jul 2025 09:37:27 +0800 Message-Id: <20250725013739.994437-6-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S7 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407674200116600 Content-Type: text/plain; charset="utf-8" Common function loongarch_check_pte() is to check tlb entry, return the physical address and access priviledge. Also it can be used with page table entry, which is used in page table walker. Signed-off-by: Bibo Mao --- target/loongarch/cpu-mmu.h | 10 +++++ target/loongarch/cpu_helper.c | 62 ++++++++++++++++++++++++++++++ target/loongarch/tcg/tlb_helper.c | 63 ++++++------------------------- 3 files changed, 84 insertions(+), 51 deletions(-) diff --git a/target/loongarch/cpu-mmu.h b/target/loongarch/cpu-mmu.h index 4c5cbd7425..62b3acfbc7 100644 --- a/target/loongarch/cpu-mmu.h +++ b/target/loongarch/cpu-mmu.h @@ -19,7 +19,17 @@ enum { TLBRET_PE =3D 7, }; =20 +typedef struct mmu_context { + target_ulong vaddr; + uint64_t pte; + hwaddr physical; + int ps; /* page size shift */ + int prot; +} mmu_context; + bool check_ps(CPULoongArchState *ent, uint8_t ps); +int loongarch_check_pte(CPULoongArchState *env, mmu_context *context, + int access_type, int mmu_idx); int get_physical_address(CPULoongArchState *env, hwaddr *physical, int *prot, target_ulong address, MMUAccessType access_type, int mmu_idx, int is_de= bug); diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 2e8d3d7cfb..1b3dfaf15d 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -44,6 +44,68 @@ void get_dir_base_width(CPULoongArchState *env, uint64_t= *dir_base, } } =20 +int loongarch_check_pte(CPULoongArchState *env, mmu_context *context, + int access_type, int mmu_idx) +{ + uint64_t plv =3D mmu_idx; + uint64_t tlb_entry, tlb_ppn; + uint8_t tlb_ps, tlb_v, tlb_d, tlb_plv, tlb_nx, tlb_nr, tlb_rplv; + + tlb_entry =3D context->pte; + tlb_ps =3D context->ps; + tlb_v =3D FIELD_EX64(tlb_entry, TLBENTRY, V); + tlb_d =3D FIELD_EX64(tlb_entry, TLBENTRY, D); + tlb_plv =3D FIELD_EX64(tlb_entry, TLBENTRY, PLV); + if (is_la64(env)) { + tlb_ppn =3D FIELD_EX64(tlb_entry, TLBENTRY_64, PPN); + tlb_nx =3D FIELD_EX64(tlb_entry, TLBENTRY_64, NX); + tlb_nr =3D FIELD_EX64(tlb_entry, TLBENTRY_64, NR); + tlb_rplv =3D FIELD_EX64(tlb_entry, TLBENTRY_64, RPLV); + } else { + tlb_ppn =3D FIELD_EX64(tlb_entry, TLBENTRY_32, PPN); + tlb_nx =3D 0; + tlb_nr =3D 0; + tlb_rplv =3D 0; + } + + /* Check access rights */ + if (!tlb_v) { + return TLBRET_INVALID; + } + + if (access_type =3D=3D MMU_INST_FETCH && tlb_nx) { + return TLBRET_XI; + } + + if (access_type =3D=3D MMU_DATA_LOAD && tlb_nr) { + return TLBRET_RI; + } + + if (((tlb_rplv =3D=3D 0) && (plv > tlb_plv)) || + ((tlb_rplv =3D=3D 1) && (plv !=3D tlb_plv))) { + return TLBRET_PE; + } + + if ((access_type =3D=3D MMU_DATA_STORE) && !tlb_d) { + return TLBRET_DIRTY; + } + + /* Remove sw bit between bit12 -- bit PS*/ + tlb_ppn =3D tlb_ppn & ~(((0x1UL << (tlb_ps - 12)) - 1)); + context->physical =3D (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) | + (context->vaddr & MAKE_64BIT_MASK(0, tlb_ps)); + context->prot =3D PAGE_READ; + if (tlb_d) { + context->prot |=3D PAGE_WRITE; + } + + if (!tlb_nx) { + context->prot |=3D PAGE_EXEC; + } + + return TLBRET_MATCH; +} + static int loongarch_page_table_walker(CPULoongArchState *env, hwaddr *phy= sical, int *prot, target_ulong address) { diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 1f49619e7f..dd99a063cc 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -653,64 +653,25 @@ static int loongarch_map_tlb_entry(CPULoongArchState = *env, hwaddr *physical, int access_type, int index, int mmu_idx) { LoongArchTLB *tlb =3D &env->tlb[index]; - uint64_t plv =3D mmu_idx; - uint64_t tlb_entry, tlb_ppn; - uint8_t tlb_ps, n, tlb_v, tlb_d, tlb_plv, tlb_nx, tlb_nr, tlb_rplv; + uint64_t tlb_entry; + uint8_t tlb_ps, n; + mmu_context context; + int ret; =20 tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); n =3D (address >> tlb_ps) & 0x1;/* Odd or even */ =20 tlb_entry =3D n ? tlb->tlb_entry1 : tlb->tlb_entry0; - tlb_v =3D FIELD_EX64(tlb_entry, TLBENTRY, V); - tlb_d =3D FIELD_EX64(tlb_entry, TLBENTRY, D); - tlb_plv =3D FIELD_EX64(tlb_entry, TLBENTRY, PLV); - if (is_la64(env)) { - tlb_ppn =3D FIELD_EX64(tlb_entry, TLBENTRY_64, PPN); - tlb_nx =3D FIELD_EX64(tlb_entry, TLBENTRY_64, NX); - tlb_nr =3D FIELD_EX64(tlb_entry, TLBENTRY_64, NR); - tlb_rplv =3D FIELD_EX64(tlb_entry, TLBENTRY_64, RPLV); - } else { - tlb_ppn =3D FIELD_EX64(tlb_entry, TLBENTRY_32, PPN); - tlb_nx =3D 0; - tlb_nr =3D 0; - tlb_rplv =3D 0; - } - - /* Remove sw bit between bit12 -- bit PS*/ - tlb_ppn =3D tlb_ppn & ~(((0x1UL << (tlb_ps - 12)) - 1)); - - /* Check access rights */ - if (!tlb_v) { - return TLBRET_INVALID; - } - - if (access_type =3D=3D MMU_INST_FETCH && tlb_nx) { - return TLBRET_XI; - } - - if (access_type =3D=3D MMU_DATA_LOAD && tlb_nr) { - return TLBRET_RI; - } - - if (((tlb_rplv =3D=3D 0) && (plv > tlb_plv)) || - ((tlb_rplv =3D=3D 1) && (plv !=3D tlb_plv))) { - return TLBRET_PE; - } - - if ((access_type =3D=3D MMU_DATA_STORE) && !tlb_d) { - return TLBRET_DIRTY; + context.vaddr =3D address; + context.ps =3D tlb_ps; + context.pte =3D tlb_entry; + ret =3D loongarch_check_pte(env, &context, access_type, mmu_idx); + if (ret =3D=3D TLBRET_MATCH) { + *physical =3D context.physical; + *prot =3D context.prot; } =20 - *physical =3D (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) | - (address & MAKE_64BIT_MASK(0, tlb_ps)); - *prot =3D PAGE_READ; - if (tlb_d) { - *prot |=3D PAGE_WRITE; - } - if (!tlb_nx) { - *prot |=3D PAGE_EXEC; - } - return TLBRET_MATCH; + return ret; } =20 int loongarch_get_addr_from_tlb(CPULoongArchState *env, hwaddr *physical, --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 175340766176523.976801815998556; Thu, 24 Jul 2025 18:41:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PO-0001ut-VS; Thu, 24 Jul 2025 21:39:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nf-0000hf-V7 for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:52 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nc-0002aq-Dl for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:51 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Cx_eLn34JoipAxAQ--.59802S3; Fri, 25 Jul 2025 09:37:43 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S8; Fri, 25 Jul 2025 09:37:42 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 06/17] target/loongarch: Use loongarch_check_pte() with page table walking Date: Fri, 25 Jul 2025 09:37:28 +0800 Message-Id: <20250725013739.994437-7-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S8 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407663778116600 Content-Type: text/plain; charset="utf-8" Function loongarch_check_pte() can get physical address and access priviledge, it works on both TLB entry and pte entry. It can be used at page table walking. Signed-off-by: Bibo Mao Reviewed-by: Richard Henderson --- target/loongarch/cpu_helper.c | 38 +++++++++++++---------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 1b3dfaf15d..9e6de2908f 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -107,13 +107,15 @@ int loongarch_check_pte(CPULoongArchState *env, mmu_c= ontext *context, } =20 static int loongarch_page_table_walker(CPULoongArchState *env, hwaddr *phy= sical, - int *prot, target_ulong address) + int *prot, target_ulong address, + int access_type, int mmu_idx) { CPUState *cs =3D env_cpu(env); target_ulong index, phys; uint64_t dir_base, dir_width; uint64_t base; - int level; + int level, ret; + mmu_context context; =20 if ((address >> 63) & 0x1) { base =3D env->CSR_PGDH; @@ -156,29 +158,16 @@ static int loongarch_page_table_walker(CPULoongArchSt= ate *env, hwaddr *physical, base =3D ldq_phys(cs->as, phys); } =20 - /* TODO: check plv and other bits? */ - - /* base is pte, in normal pte format */ - if (!FIELD_EX64(base, TLBENTRY, V)) { - return TLBRET_NOMATCH; + context.vaddr =3D address; + context.ps =3D dir_base; + context.pte =3D base; + ret =3D loongarch_check_pte(env, &context, access_type, mmu_idx); + if (ret =3D=3D TLBRET_MATCH) { + *physical =3D context.physical; + *prot =3D context.prot; } =20 - if (!FIELD_EX64(base, TLBENTRY, D)) { - *prot =3D PAGE_READ; - } else { - *prot =3D PAGE_READ | PAGE_WRITE; - } - - /* get TARGET_PAGE_SIZE aligned physical address */ - base +=3D (address & TARGET_PHYS_MASK) & ((1 << dir_base) - 1); - /* mask RPLV, NX, NR bits */ - base =3D FIELD_DP64(base, TLBENTRY_64, RPLV, 0); - base =3D FIELD_DP64(base, TLBENTRY_64, NX, 0); - base =3D FIELD_DP64(base, TLBENTRY_64, NR, 0); - /* mask other attribute bits */ - *physical =3D base & TARGET_PAGE_MASK; - - return 0; + return ret; } =20 static int loongarch_map_address(CPULoongArchState *env, hwaddr *physical, @@ -202,7 +191,8 @@ static int loongarch_map_address(CPULoongArchState *env= , hwaddr *physical, * legal mapping, even if the mapping is not yet in TLB. return 0 = if * there is a valid map, else none zero. */ - return loongarch_page_table_walker(env, physical, prot, address); + return loongarch_page_table_walker(env, physical, prot, address, + access_type, mmu_idx); } =20 return TLBRET_NOMATCH; --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407600531777.0263239200499; Thu, 24 Jul 2025 18:40:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Ox-0001bl-5E; Thu, 24 Jul 2025 21:39:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Ng-0000jR-UH for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nc-0002az-FQ for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:52 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Bxlmno34JojJAxAQ--.34631S3; Fri, 25 Jul 2025 09:37:44 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S9; Fri, 25 Jul 2025 09:37:43 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 07/17] target/loongarch: Add parameter mmu_context with loongarch_page_table_walker Date: Fri, 25 Jul 2025 09:37:29 +0800 Message-Id: <20250725013739.994437-8-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S9 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407603974116600 Content-Type: text/plain; charset="utf-8" With function loongarch_page_table_walker(), some output parameters such as physical address and prot can be moved to structure mmu_context. Signed-off-by: Bibo Mao --- target/loongarch/cpu_helper.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 9e6de2908f..a26bb8b11b 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -106,17 +106,18 @@ int loongarch_check_pte(CPULoongArchState *env, mmu_c= ontext *context, return TLBRET_MATCH; } =20 -static int loongarch_page_table_walker(CPULoongArchState *env, hwaddr *phy= sical, - int *prot, target_ulong address, +static int loongarch_page_table_walker(CPULoongArchState *env, + mmu_context *context, int access_type, int mmu_idx) { CPUState *cs =3D env_cpu(env); target_ulong index, phys; uint64_t dir_base, dir_width; uint64_t base; - int level, ret; - mmu_context context; + int level; + target_ulong address; =20 + address =3D context->vaddr; if ((address >> 63) & 0x1) { base =3D env->CSR_PGDH; } else { @@ -158,16 +159,9 @@ static int loongarch_page_table_walker(CPULoongArchSta= te *env, hwaddr *physical, base =3D ldq_phys(cs->as, phys); } =20 - context.vaddr =3D address; - context.ps =3D dir_base; - context.pte =3D base; - ret =3D loongarch_check_pte(env, &context, access_type, mmu_idx); - if (ret =3D=3D TLBRET_MATCH) { - *physical =3D context.physical; - *prot =3D context.prot; - } - - return ret; + context->ps =3D dir_base; + context->pte =3D base; + return loongarch_check_pte(env, context, access_type, mmu_idx); } =20 static int loongarch_map_address(CPULoongArchState *env, hwaddr *physical, @@ -176,7 +170,9 @@ static int loongarch_map_address(CPULoongArchState *env= , hwaddr *physical, int is_debug) { int ret; + mmu_context context; =20 + context.vaddr =3D address; if (tcg_enabled()) { ret =3D loongarch_get_addr_from_tlb(env, physical, prot, address, access_type, mmu_idx); @@ -191,8 +187,7 @@ static int loongarch_map_address(CPULoongArchState *env= , hwaddr *physical, * legal mapping, even if the mapping is not yet in TLB. return 0 = if * there is a valid map, else none zero. */ - return loongarch_page_table_walker(env, physical, prot, address, - access_type, mmu_idx); + return loongarch_page_table_walker(env, &context, access_type, mmu= _idx); } =20 return TLBRET_NOMATCH; --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407674411129.13857522370893; Thu, 24 Jul 2025 18:41:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PU-00022I-T7; Thu, 24 Jul 2025 21:39:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nh-0000kF-5a for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nd-0002b4-0H for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:52 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Bx22ro34JojpAxAQ--.65177S3; Fri, 25 Jul 2025 09:37:44 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S10; Fri, 25 Jul 2025 09:37:43 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 08/17] target/loongarch: Add parameter mmu_context with loongarch_map_tlb_entry Date: Fri, 25 Jul 2025 09:37:30 +0800 Message-Id: <20250725013739.994437-9-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S10 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407675841116600 Content-Type: text/plain; charset="utf-8" With function loongarch_map_tlb_entry(), parameter mmu_context is added and remove parameter physical and prot. Signed-off-by: Bibo Mao --- target/loongarch/tcg/tlb_helper.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index dd99a063cc..460e7c00c5 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -648,30 +648,22 @@ void helper_ldpte(CPULoongArchState *env, target_ulon= g base, target_ulong odd, env->CSR_TLBREHI =3D FIELD_DP64(env->CSR_TLBREHI, CSR_TLBREHI, PS, ps); } =20 -static int loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physica= l, - int *prot, target_ulong address, +static int loongarch_map_tlb_entry(CPULoongArchState *env, mmu_context *co= ntext, int access_type, int index, int mmu_idx) { LoongArchTLB *tlb =3D &env->tlb[index]; uint64_t tlb_entry; uint8_t tlb_ps, n; - mmu_context context; - int ret; + target_ulong address; =20 + address =3D context->vaddr; tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); n =3D (address >> tlb_ps) & 0x1;/* Odd or even */ =20 tlb_entry =3D n ? tlb->tlb_entry1 : tlb->tlb_entry0; - context.vaddr =3D address; - context.ps =3D tlb_ps; - context.pte =3D tlb_entry; - ret =3D loongarch_check_pte(env, &context, access_type, mmu_idx); - if (ret =3D=3D TLBRET_MATCH) { - *physical =3D context.physical; - *prot =3D context.prot; - } - - return ret; + context->ps =3D tlb_ps; + context->pte =3D tlb_entry; + return loongarch_check_pte(env, context, access_type, mmu_idx); } =20 int loongarch_get_addr_from_tlb(CPULoongArchState *env, hwaddr *physical, @@ -679,11 +671,18 @@ int loongarch_get_addr_from_tlb(CPULoongArchState *en= v, hwaddr *physical, MMUAccessType access_type, int mmu_idx) { int index, match; + mmu_context context; =20 + context.vaddr =3D address; match =3D loongarch_tlb_search(env, address, &index); if (match) { - return loongarch_map_tlb_entry(env, physical, prot, - address, access_type, index, mmu_id= x); + match =3D loongarch_map_tlb_entry(env, &context, + access_type, index, mmu_idx); + if (match =3D=3D TLBRET_MATCH) { + *physical =3D context.physical; + *prot =3D context.prot; + } + return match; } =20 return TLBRET_NOMATCH; --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407632064654.6447777833029; Thu, 24 Jul 2025 18:40:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Q9-0003QI-6T; Thu, 24 Jul 2025 21:40:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Ov-0001bW-Im for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:39:09 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Ot-0002qu-Dp for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:39:09 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8AxlnDo34JokJAxAQ--.34107S3; Fri, 25 Jul 2025 09:37:44 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S11; Fri, 25 Jul 2025 09:37:44 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 09/17] target/loongarch: Add parameter mmu_context with loongarch_get_addr_from_tlb Date: Fri, 25 Jul 2025 09:37:31 +0800 Message-Id: <20250725013739.994437-10-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S11 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407633730116600 Content-Type: text/plain; charset="utf-8" With function loongarch_get_addr_from_tlb(), parameter mmu_context is added and remove parameter physical and prot. Signed-off-by: Bibo Mao --- target/loongarch/cpu_helper.c | 7 +++++-- target/loongarch/tcg/tcg_loongarch.h | 4 ++-- target/loongarch/tcg/tlb_helper.c | 16 ++++------------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index a26bb8b11b..1e97687e1f 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -174,9 +174,12 @@ static int loongarch_map_address(CPULoongArchState *en= v, hwaddr *physical, =20 context.vaddr =3D address; if (tcg_enabled()) { - ret =3D loongarch_get_addr_from_tlb(env, physical, prot, address, - access_type, mmu_idx); + ret =3D loongarch_get_addr_from_tlb(env, &context, access_type, mm= u_idx); if (ret !=3D TLBRET_NOMATCH) { + if (ret =3D=3D TLBRET_MATCH) { + *physical =3D context.physical; + *prot =3D context.prot; + } return ret; } } diff --git a/target/loongarch/tcg/tcg_loongarch.h b/target/loongarch/tcg/tc= g_loongarch.h index fd4e116022..a27d77c5ba 100644 --- a/target/loongarch/tcg/tcg_loongarch.h +++ b/target/loongarch/tcg/tcg_loongarch.h @@ -7,6 +7,7 @@ #ifndef TARGET_LOONGARCH_TCG_LOONGARCH_H #define TARGET_LOONGARCH_TCG_LOONGARCH_H #include "cpu.h" +#include "cpu-mmu.h" =20 void loongarch_csr_translate_init(void); =20 @@ -14,8 +15,7 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, MMUAccessType access_type, int mmu_idx, bool probe, uintptr_t retaddr); =20 -int loongarch_get_addr_from_tlb(CPULoongArchState *env, hwaddr *physical, - int *prot, target_ulong address, +int loongarch_get_addr_from_tlb(CPULoongArchState *env, mmu_context *conte= xt, MMUAccessType access_type, int mmu_idx); =20 #endif /* TARGET_LOONGARCH_TCG_LOONGARCH_H */ diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 460e7c00c5..a875ac251e 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -666,23 +666,15 @@ static int loongarch_map_tlb_entry(CPULoongArchState = *env, mmu_context *context, return loongarch_check_pte(env, context, access_type, mmu_idx); } =20 -int loongarch_get_addr_from_tlb(CPULoongArchState *env, hwaddr *physical, - int *prot, target_ulong address, +int loongarch_get_addr_from_tlb(CPULoongArchState *env, mmu_context *conte= xt, MMUAccessType access_type, int mmu_idx) { int index, match; - mmu_context context; =20 - context.vaddr =3D address; - match =3D loongarch_tlb_search(env, address, &index); + match =3D loongarch_tlb_search(env, context->vaddr, &index); if (match) { - match =3D loongarch_map_tlb_entry(env, &context, - access_type, index, mmu_idx); - if (match =3D=3D TLBRET_MATCH) { - *physical =3D context.physical; - *prot =3D context.prot; - } - return match; + return loongarch_map_tlb_entry(env, context, + access_type, index, mmu_idx); } =20 return TLBRET_NOMATCH; --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407600914325.7025028783762; Thu, 24 Jul 2025 18:40:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7PD-0001m5-8N; Thu, 24 Jul 2025 21:39:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nh-0000ku-CE for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nd-0002b8-7l for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:37:53 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Bxjazo34JokZAxAQ--.1129S3; Fri, 25 Jul 2025 09:37:44 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S12; Fri, 25 Jul 2025 09:37:44 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 10/17] target/loongarch: Add parameter mmu_context with loongarch_map_address Date: Fri, 25 Jul 2025 09:37:32 +0800 Message-Id: <20250725013739.994437-11-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S12 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407604312116600 Content-Type: text/plain; charset="utf-8" With function loongarch_map_address(), parameter mmu_context is added and remove parameter address, prot and address. Signed-off-by: Bibo Mao --- target/loongarch/cpu_helper.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 1e97687e1f..6abd7aa152 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -164,22 +164,15 @@ static int loongarch_page_table_walker(CPULoongArchSt= ate *env, return loongarch_check_pte(env, context, access_type, mmu_idx); } =20 -static int loongarch_map_address(CPULoongArchState *env, hwaddr *physical, - int *prot, target_ulong address, +static int loongarch_map_address(CPULoongArchState *env, mmu_context *cont= ext, MMUAccessType access_type, int mmu_idx, int is_debug) { int ret; - mmu_context context; =20 - context.vaddr =3D address; if (tcg_enabled()) { - ret =3D loongarch_get_addr_from_tlb(env, &context, access_type, mm= u_idx); + ret =3D loongarch_get_addr_from_tlb(env, context, access_type, mmu= _idx); if (ret !=3D TLBRET_NOMATCH) { - if (ret =3D=3D TLBRET_MATCH) { - *physical =3D context.physical; - *prot =3D context.prot; - } return ret; } } @@ -190,7 +183,7 @@ static int loongarch_map_address(CPULoongArchState *env= , hwaddr *physical, * legal mapping, even if the mapping is not yet in TLB. return 0 = if * there is a valid map, else none zero. */ - return loongarch_page_table_walker(env, &context, access_type, mmu= _idx); + return loongarch_page_table_walker(env, context, access_type, mmu_= idx); } =20 return TLBRET_NOMATCH; @@ -218,8 +211,11 @@ int get_physical_address(CPULoongArchState *env, hwadd= r *physical, int64_t addr_high; uint8_t da =3D FIELD_EX64(env->CSR_CRMD, CSR_CRMD, DA); uint8_t pg =3D FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PG); + mmu_context context; + int ret; =20 /* Check PG and DA */ + context.vaddr =3D address; if (da & !pg) { *physical =3D address & TARGET_PHYS_MASK; *prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; @@ -253,8 +249,12 @@ int get_physical_address(CPULoongArchState *env, hwadd= r *physical, } =20 /* Mapped address */ - return loongarch_map_address(env, physical, prot, address, - access_type, mmu_idx, is_debug); + ret =3D loongarch_map_address(env, &context, access_type, mmu_idx, is_= debug); + if (ret =3D=3D TLBRET_MATCH) { + *physical =3D context.physical; + *prot =3D context.prot; + } + return ret; } =20 hwaddr loongarch_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407661087939.4131365906206; Thu, 24 Jul 2025 18:41:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Pb-00027s-2A; Thu, 24 Jul 2025 21:39:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nq-0000rH-HW for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:38:09 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nl-0002bE-BQ for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:38:01 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8BxJHDp34JolJAxAQ--.59173S3; Fri, 25 Jul 2025 09:37:45 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S13; Fri, 25 Jul 2025 09:37:44 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 11/17] target/loongarch: Add parameter mmu_context with get_physical_address Date: Fri, 25 Jul 2025 09:37:33 +0800 Message-Id: <20250725013739.994437-12-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S13 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407661780116600 Content-Type: text/plain; charset="utf-8" With function get_physical_address(), parameter mmu_context is added and remove parameter address, prot and address. Signed-off-by: Bibo Mao --- target/loongarch/cpu-mmu.h | 3 +-- target/loongarch/cpu_helper.c | 31 ++++++++++++------------------- target/loongarch/tcg/tlb_helper.c | 8 +++++--- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/target/loongarch/cpu-mmu.h b/target/loongarch/cpu-mmu.h index 62b3acfbc7..9d909d36ec 100644 --- a/target/loongarch/cpu-mmu.h +++ b/target/loongarch/cpu-mmu.h @@ -30,8 +30,7 @@ typedef struct mmu_context { bool check_ps(CPULoongArchState *ent, uint8_t ps); int loongarch_check_pte(CPULoongArchState *env, mmu_context *context, int access_type, int mmu_idx); -int get_physical_address(CPULoongArchState *env, hwaddr *physical, - int *prot, target_ulong address, +int get_physical_address(CPULoongArchState *env, mmu_context *context, MMUAccessType access_type, int mmu_idx, int is_de= bug); void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base, uint64_t *dir_width, target_ulong level); diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 6abd7aa152..9f64cb40cf 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -201,8 +201,7 @@ static hwaddr dmw_va2pa(CPULoongArchState *env, target_= ulong va, } } =20 -int get_physical_address(CPULoongArchState *env, hwaddr *physical, - int *prot, target_ulong address, +int get_physical_address(CPULoongArchState *env, mmu_context *context, MMUAccessType access_type, int mmu_idx, int is_de= bug) { int user_mode =3D mmu_idx =3D=3D MMU_USER_IDX; @@ -211,14 +210,13 @@ int get_physical_address(CPULoongArchState *env, hwad= dr *physical, int64_t addr_high; uint8_t da =3D FIELD_EX64(env->CSR_CRMD, CSR_CRMD, DA); uint8_t pg =3D FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PG); - mmu_context context; - int ret; + target_ulong address; =20 /* Check PG and DA */ - context.vaddr =3D address; + address =3D context->vaddr; if (da & !pg) { - *physical =3D address & TARGET_PHYS_MASK; - *prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + context->physical =3D address & TARGET_PHYS_MASK; + context->prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; return TLBRET_MATCH; } =20 @@ -236,8 +234,8 @@ int get_physical_address(CPULoongArchState *env, hwaddr= *physical, base_c =3D FIELD_EX64(env->CSR_DMW[i], CSR_DMW_32, VSEG); } if ((plv & env->CSR_DMW[i]) && (base_c =3D=3D base_v)) { - *physical =3D dmw_va2pa(env, address, env->CSR_DMW[i]); - *prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + context->physical =3D dmw_va2pa(env, address, env->CSR_DMW[i]); + context->prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; return TLBRET_MATCH; } } @@ -249,23 +247,18 @@ int get_physical_address(CPULoongArchState *env, hwad= dr *physical, } =20 /* Mapped address */ - ret =3D loongarch_map_address(env, &context, access_type, mmu_idx, is_= debug); - if (ret =3D=3D TLBRET_MATCH) { - *physical =3D context.physical; - *prot =3D context.prot; - } - return ret; + return loongarch_map_address(env, context, access_type, mmu_idx, is_de= bug); } =20 hwaddr loongarch_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) { CPULoongArchState *env =3D cpu_env(cs); - hwaddr phys_addr; - int prot; + mmu_context context; =20 - if (get_physical_address(env, &phys_addr, &prot, addr, MMU_DATA_LOAD, + context.vaddr =3D addr; + if (get_physical_address(env, &context, MMU_DATA_LOAD, cpu_mmu_index(cs, false), 1) !=3D 0) { return -1; } - return phys_addr; + return context.physical; } diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index a875ac251e..d1d19c5e70 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -515,15 +515,17 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr addre= ss, int size, bool probe, uintptr_t retaddr) { CPULoongArchState *env =3D cpu_env(cs); + mmu_context context; hwaddr physical; int prot; int ret; =20 /* Data access */ - ret =3D get_physical_address(env, &physical, &prot, address, - access_type, mmu_idx, 0); - + context.vaddr =3D address; + ret =3D get_physical_address(env, &context, access_type, mmu_idx, 0); if (ret =3D=3D TLBRET_MATCH) { + physical =3D context.physical; + prot =3D context.prot; tlb_set_page(cs, address & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, prot, mmu_idx, TARGET_PAGE_SIZE); --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 175340768490016.13944768969975; Thu, 24 Jul 2025 18:41:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Qx-0005bD-Tj; Thu, 24 Jul 2025 21:41:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Qr-0005LA-OL for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:41:10 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Qp-0003aL-FL for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:41:09 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8AxLOLp34JolpAxAQ--.59063S3; Fri, 25 Jul 2025 09:37:45 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S14; Fri, 25 Jul 2025 09:37:45 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 12/17] target/loongarch: Track user mode address accessed in kernel mode Date: Fri, 25 Jul 2025 09:37:34 +0800 Message-Id: <20250725013739.994437-13-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S14 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407686100116600 Content-Type: text/plain; charset="utf-8" The concept of mmu idx between QEMU TLB and LoongArch TLB emulation is different: mmu idx in QEMU TLB comes from currently working mode of vCPU mmu idx from LoongArch TLB is page priviledge level With copy_from_user() executed in system, vCPU is in kernel mode however PLV of PTE is user mode. Here field KM is added in TLB MISC to track whether the lo0/lo1 pte entry is accessed in kernel mode. If set, when LoongArch TLB is flushed, need flush QEMU TLB with mmu idx MMU_KERNEL_IDX. Signed-off-by: Bibo Mao --- target/loongarch/cpu-mmu.h | 2 ++ target/loongarch/cpu.h | 11 +++++++++++ target/loongarch/cpu_helper.c | 3 +++ target/loongarch/tcg/tlb_helper.c | 26 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/target/loongarch/cpu-mmu.h b/target/loongarch/cpu-mmu.h index 9d909d36ec..3de707dd56 100644 --- a/target/loongarch/cpu-mmu.h +++ b/target/loongarch/cpu-mmu.h @@ -25,6 +25,8 @@ typedef struct mmu_context { hwaddr physical; int ps; /* page size shift */ int prot; + int tlb_index; + int mmu_index; } mmu_context; =20 bool check_ps(CPULoongArchState *ent, uint8_t ps); diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h index 7731f6acdc..2ae5f655a6 100644 --- a/target/loongarch/cpu.h +++ b/target/loongarch/cpu.h @@ -253,6 +253,17 @@ FIELD(TLB_MISC, E, 0, 1) FIELD(TLB_MISC, ASID, 1, 10) FIELD(TLB_MISC, VPPN, 13, 35) FIELD(TLB_MISC, PS, 48, 6) +/* + * Used by QEMU software, concept of mmu idx between QEMU TLB and LoongArch + * TLB emulation is different: + * mmu idx in QEMU TLB is current working mode of vCPU + * mmu idx in LoongArch TLB is PLV access level + * When funtion copy_from_user() executed with system emulation method, + * vCPU is in kernel mode however accessed address is user memory space. + * + * TLB lo0/lo1 entry mask with PLV MMU_USER_IDX accessed in kernel mode + */ +FIELD(TLB_MISC, KM, 54, 2) =20 #define LSX_LEN (128) #define LASX_LEN (256) diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 9f64cb40cf..afdf05649b 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -95,6 +95,7 @@ int loongarch_check_pte(CPULoongArchState *env, mmu_conte= xt *context, context->physical =3D (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) | (context->vaddr & MAKE_64BIT_MASK(0, tlb_ps)); context->prot =3D PAGE_READ; + context->mmu_index =3D tlb_plv; if (tlb_d) { context->prot |=3D PAGE_WRITE; } @@ -217,6 +218,7 @@ int get_physical_address(CPULoongArchState *env, mmu_co= ntext *context, if (da & !pg) { context->physical =3D address & TARGET_PHYS_MASK; context->prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + context->mmu_index =3D MMU_DA_IDX; return TLBRET_MATCH; } =20 @@ -236,6 +238,7 @@ int get_physical_address(CPULoongArchState *env, mmu_co= ntext *context, if ((plv & env->CSR_DMW[i]) && (base_c =3D=3D base_v)) { context->physical =3D dmw_va2pa(env, address, env->CSR_DMW[i]); context->prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + context->mmu_index =3D MMU_DA_IDX; return TLBRET_MATCH; } } diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index d1d19c5e70..715c5a20da 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -172,6 +172,7 @@ static void fill_tlb_entry(CPULoongArchState *env, int = index) } =20 /* Store page size in field PS */ + tlb->tlb_misc =3D 0; tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, PS, csr_ps); tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, VPPN, csr_vppn); tlb->tlb_misc =3D FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 1); @@ -510,6 +511,24 @@ void helper_invtlb_page_asid_or_g(CPULoongArchState *e= nv, tlb_flush(env_cpu(env)); } =20 +/* + * Record tlb entry with virtual address from user mode accessed from + * vCPU kernel mode. + * + * If set, when LoongArch TLB is flushed, need flush QEMU TLB with mmu + * idx MMU_KERNEL_IDX + */ +static inline void tlb_set_accessed(CPULoongArchState *env, vaddr address, + int index) +{ + LoongArchTLB *tlb =3D &env->tlb[index]; + uint8_t tlb_ps, n; + + tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); + n =3D (address >> tlb_ps) & 0x1;/* Odd or even */ + tlb->tlb_misc |=3D BIT_ULL(R_TLB_MISC_KM_SHIFT + n); +} + bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size, MMUAccessType access_type, int mmu_idx, bool probe, uintptr_t retaddr) @@ -529,6 +548,12 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr addres= s, int size, tlb_set_page(cs, address & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, prot, mmu_idx, TARGET_PAGE_SIZE); + + /* user mode address space is accessed in vCPU kernel mode */ + if (mmu_idx =3D=3D MMU_KERNEL_IDX && context.mmu_index =3D=3D MMU_= USER_IDX) { + tlb_set_accessed(env, address, context.tlb_index); + } + qemu_log_mask(CPU_LOG_MMU, "%s address=3D%" VADDR_PRIx " physical " HWADDR_FMT_= plx " prot %d\n", __func__, address, physical, prot); @@ -665,6 +690,7 @@ static int loongarch_map_tlb_entry(CPULoongArchState *e= nv, mmu_context *context, tlb_entry =3D n ? tlb->tlb_entry1 : tlb->tlb_entry0; context->ps =3D tlb_ps; context->pte =3D tlb_entry; + context->tlb_index =3D index; return loongarch_check_pte(env, context, access_type, mmu_idx); } =20 --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407607786250.51624016696485; Thu, 24 Jul 2025 18:40:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Pc-00028i-UI; Thu, 24 Jul 2025 21:39:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Nq-0000rG-HB for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:38:09 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Nm-0002bV-3Q for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:38:01 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8DxbKzq34JomJAxAQ--.35109S3; Fri, 25 Jul 2025 09:37:46 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S15; Fri, 25 Jul 2025 09:37:45 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 13/17] target/loongarch: Use correct address when flush tlb Date: Fri, 25 Jul 2025 09:37:35 +0800 Message-Id: <20250725013739.994437-14-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S15 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407609573116600 Content-Type: text/plain; charset="utf-8" With tlb_flush_range_by_mmuidx(), the virtual address is 64 bit. However on LoongArch TLB emulation system, virtual address is 48 bit. It is necessary to convert 48 bit address to 64 bit when flush tlb, also fix address calculation issue with odd page. Signed-off-by: Bibo Mao --- target/loongarch/tcg/tlb_helper.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 715c5a20da..61cc19610e 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -96,6 +96,15 @@ static void raise_mmu_exception(CPULoongArchState *env, = target_ulong address, } } =20 +/* Convert 48 bit virtual address from LoongArch TLB to 64 bit VA */ +static inline target_ulong __vaddr(target_ulong addr) +{ + target_ulong high; + + high =3D -(addr >> (TARGET_VIRT_ADDR_SPACE_BITS - 1)); + return addr + (high << TARGET_VIRT_ADDR_SPACE_BITS); +} + static void invalidate_tlb_entry(CPULoongArchState *env, int index) { target_ulong addr, mask, pagesize; @@ -115,16 +124,15 @@ static void invalidate_tlb_entry(CPULoongArchState *e= nv, int index) tlb_ps =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS); pagesize =3D MAKE_64BIT_MASK(tlb_ps, 1); mask =3D MAKE_64BIT_MASK(0, tlb_ps + 1); + addr =3D __vaddr((tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & ~mask); =20 if (tlb_v0) { - addr =3D (tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & ~mask; /* even */ tlb_flush_range_by_mmuidx(env_cpu(env), addr, pagesize, mmu_idx, TARGET_LONG_BITS); } =20 if (tlb_v1) { - addr =3D (tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & pagesize; /* odd= */ - tlb_flush_range_by_mmuidx(env_cpu(env), addr, pagesize, + tlb_flush_range_by_mmuidx(env_cpu(env), addr + pagesize, pagesize, mmu_idx, TARGET_LONG_BITS); } } --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753407711128596.9712911169948; Thu, 24 Jul 2025 18:41:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Q3-00032D-0z; Thu, 24 Jul 2025 21:40:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Ov-0001bg-PI for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:39:09 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Ot-0002qw-E8 for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:39:09 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8AxDGvq34JooJAxAQ--.34903S3; Fri, 25 Jul 2025 09:37:46 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxdOTk34Joz5wlAA--.62171S16; Fri, 25 Jul 2025 09:37:45 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 14/17] target/loongarch: Use mmu idx bitmap method when flush tlb Date: Fri, 25 Jul 2025 09:37:36 +0800 Message-Id: <20250725013739.994437-15-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxdOTk34Joz5wlAA--.62171S16 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753407712150116600 Content-Type: text/plain; charset="utf-8" With API tlb_flush_range_by_mmuidx(), bitmap method of mmu idx should be used rather than itself. And mmu idx comes from page table entry information rather current running mode. Also field KM in TLB misc records bitmap mask of TLB entry which is access in kernel mode. If set, MMU_KERNEL_IDX should be added to flush tlb. Signed-off-by: Bibo Mao --- target/loongarch/tcg/tlb_helper.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 61cc19610e..d18b382e56 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -110,12 +110,12 @@ static void invalidate_tlb_entry(CPULoongArchState *e= nv, int index) target_ulong addr, mask, pagesize; uint8_t tlb_ps; LoongArchTLB *tlb =3D &env->tlb[index]; - - int mmu_idx =3D cpu_mmu_index(env_cpu(env), false); + int mmu_idx; uint8_t tlb_v0 =3D FIELD_EX64(tlb->tlb_entry0, TLBENTRY, V); uint8_t tlb_v1 =3D FIELD_EX64(tlb->tlb_entry1, TLBENTRY, V); uint64_t tlb_vppn =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, VPPN); uint8_t tlb_e =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, E); + uint16_t tlb_g, tlb_km; =20 if (!tlb_e) { return; @@ -125,13 +125,28 @@ static void invalidate_tlb_entry(CPULoongArchState *e= nv, int index) pagesize =3D MAKE_64BIT_MASK(tlb_ps, 1); mask =3D MAKE_64BIT_MASK(0, tlb_ps + 1); addr =3D __vaddr((tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & ~mask); + tlb_g =3D FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G); + tlb_km =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, KM); + if (tlb_g) { + mmu_idx =3D BIT(MMU_KERNEL_IDX); + } else { + mmu_idx =3D BIT(MMU_USER_IDX); + } =20 if (tlb_v0) { + /* Even page is accessed in kernel mode */ + if (tlb_km & 0x1) { + mmu_idx |=3D BIT(MMU_KERNEL_IDX); + } tlb_flush_range_by_mmuidx(env_cpu(env), addr, pagesize, mmu_idx, TARGET_LONG_BITS); } =20 if (tlb_v1) { + /* Odd page is accessed in kernel mode */ + if (tlb_km & 0x2) { + mmu_idx |=3D BIT(MMU_KERNEL_IDX); + } tlb_flush_range_by_mmuidx(env_cpu(env), addr + pagesize, pagesize, mmu_idx, TARGET_LONG_BITS); } --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753408062078764.4386563177201; Thu, 24 Jul 2025 18:47:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Wq-0007GZ-T1; Thu, 24 Jul 2025 21:47:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Wm-0007EP-Vj for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:47:17 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Wk-00053Y-Un for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:47:16 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8CxaWog4oJo3ZExAQ--.38534S3; Fri, 25 Jul 2025 09:47:12 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJDx_8Mf4oJosZ8lAA--.61777S2; Fri, 25 Jul 2025 09:47:11 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 15/17] target/loongarch: Add parameter tlb pointer with fill_tlb_entry Date: Fri, 25 Jul 2025 09:47:11 +0800 Message-Id: <20250725014711.1025794-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJDx_8Mf4oJosZ8lAA--.61777S2 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753408063380116600 Content-Type: text/plain; charset="utf-8" With function fill_tlb_entry(), it will update LoongArch emulated TLB information. Here parameter tlb pointer is added so that TLB entry will be updated based on relative TLB CSR registers. Signed-off-by: Bibo Mao Reviewed-by: Richard Henderson --- target/loongarch/tcg/tlb_helper.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index d18b382e56..1ed2471e0a 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -167,9 +167,8 @@ static void invalidate_tlb(CPULoongArchState *env, int = index) invalidate_tlb_entry(env, index); } =20 -static void fill_tlb_entry(CPULoongArchState *env, int index) +static void fill_tlb_entry(CPULoongArchState *env, LoongArchTLB *tlb) { - LoongArchTLB *tlb =3D &env->tlb[index]; uint64_t lo0, lo1, csr_vppn; uint16_t csr_asid; uint8_t csr_ps; @@ -337,7 +336,7 @@ void helper_tlbwr(CPULoongArchState *env) return; } =20 - fill_tlb_entry(env, index); + fill_tlb_entry(env, env->tlb + index); } =20 void helper_tlbfill(CPULoongArchState *env) @@ -375,7 +374,7 @@ void helper_tlbfill(CPULoongArchState *env) } =20 invalidate_tlb(env, index); - fill_tlb_entry(env, index); + fill_tlb_entry(env, env->tlb + index); } =20 void helper_tlbclr(CPULoongArchState *env) --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753408080869158.7376007364452; Thu, 24 Jul 2025 18:48:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7XO-0007mZ-QU; Thu, 24 Jul 2025 21:47:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7XH-0007kR-6V for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:47:47 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7XE-00058G-Oi for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:47:46 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8DxDeM+4oJo8pExAQ--.64564S3; Fri, 25 Jul 2025 09:47:42 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxocI74oJo3Z8lAA--.61220S2; Fri, 25 Jul 2025 09:47:40 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 16/17] target/loongarch: Reduce TLB flush with helper_tlbwr Date: Fri, 25 Jul 2025 09:47:39 +0800 Message-Id: <20250725014739.1031030-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxocI74oJo3Z8lAA--.61220S2 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753408081268116600 Content-Type: text/plain; charset="utf-8" With function helper_tlbwr(), specified LoongArch TLB entry will be updated. There are two PTE pages in one TLB entry, it is not necessary to flush QEMU TLB when one PTE page keeps unchanged and ther other PTE page is newly added. Here check whether PTE page is the same or not, TLB flush can be skipped if both are the same or newly added. Signed-off-by: Bibo Mao --- target/loongarch/tcg/tlb_helper.c | 36 ++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 1ed2471e0a..6ac102862b 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -327,16 +327,42 @@ void helper_tlbrd(CPULoongArchState *env) void helper_tlbwr(CPULoongArchState *env) { int index =3D FIELD_EX64(env->CSR_TLBIDX, CSR_TLBIDX, INDEX); + LoongArchTLB *old, new; + int skip_inv =3D 0; + uint8_t tlb_v; =20 - invalidate_tlb(env, index); - + old =3D env->tlb + index; if (FIELD_EX64(env->CSR_TLBIDX, CSR_TLBIDX, NE)) { - env->tlb[index].tlb_misc =3D FIELD_DP64(env->tlb[index].tlb_misc, - TLB_MISC, E, 0); + invalidate_tlb(env, index); + old->tlb_misc =3D FIELD_DP64(old->tlb_misc, TLB_MISC, E, 0); return; } =20 - fill_tlb_entry(env, env->tlb + index); + new.tlb_misc =3D 0; + new.tlb_entry0 =3D 0; + new.tlb_entry1 =3D 0; + fill_tlb_entry(env, &new); + /* Check whether ASID/VPPN is the same */ + if (old->tlb_misc =3D=3D new.tlb_misc) { + tlb_v =3D FIELD_EX64(old->tlb_entry0, TLBENTRY, V); + /* Check whether even pte the same or invalid */ + if (!tlb_v || new.tlb_entry0 =3D=3D old->tlb_entry0) { + skip_inv =3D 1; + } + + /* Check whether odd pte the same or invalid */ + tlb_v =3D FIELD_EX64(old->tlb_entry1, TLBENTRY, V); + if (!tlb_v || new.tlb_entry1 =3D=3D old->tlb_entry1) { + skip_inv &=3D 1; + } + } + + if (!skip_inv) { + invalidate_tlb(env, index); + } + old->tlb_misc =3D new.tlb_misc; + old->tlb_entry0 =3D new.tlb_entry0; + old->tlb_entry1 =3D new.tlb_entry1; } =20 void helper_tlbfill(CPULoongArchState *env) --=20 2.39.3 From nobody Sat Nov 15 10:42:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753408126950500.45261569962975; Thu, 24 Jul 2025 18:48:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7YC-0000eK-QX; Thu, 24 Jul 2025 21:48:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uf7Xh-0008KE-Dj for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:48:16 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uf7Xd-0005CC-FJ for qemu-devel@nongnu.org; Thu, 24 Jul 2025 21:48:12 -0400 Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8AxlnBU4oJoBJIxAQ--.34120S3; Fri, 25 Jul 2025 09:48:04 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJAxQMJR4oJo958lAA--.56842S2; Fri, 25 Jul 2025 09:48:02 +0800 (CST) From: Bibo Mao To: Song Gao Cc: Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH v3 17/17] target/loongarch: Update TLB index selection method Date: Fri, 25 Jul 2025 09:48:00 +0800 Message-Id: <20250725014800.1034372-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250725013739.994437-1-maobibo@loongson.cn> References: <20250725013739.994437-1-maobibo@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJAxQMJR4oJo958lAA--.56842S2 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1753408127601116600 Content-Type: text/plain; charset="utf-8" With function helper_tlbfill(), since there is no suitable TLB entry, new TLB will be added and invalid one old TLB entry. The old TLB entry index is selected randomly. Firstly all TLB entries can be searched with such method: 1. invalid TLB entry can be selected at firstly. 2. TLB entry with other ASID can be selected secondly 3. random method is used by last. Signed-off-by: Bibo Mao --- target/loongarch/tcg/tlb_helper.c | 50 ++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index 6ac102862b..99b04550b5 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -368,8 +368,11 @@ void helper_tlbwr(CPULoongArchState *env) void helper_tlbfill(CPULoongArchState *env) { uint64_t address, entryhi; - int index, set, stlb_idx; + int index, i, stlb_idx; uint16_t pagesize, stlb_ps; + uint16_t asid, tlb_asid; + LoongArchTLB *tlb; + uint8_t tlb_e; =20 if (FIELD_EX64(env->CSR_TLBRERA, CSR_TLBRERA, ISTLBR)) { entryhi =3D env->CSR_TLBREHI; @@ -383,20 +386,51 @@ void helper_tlbfill(CPULoongArchState *env) =20 /* Validity of stlb_ps is checked in helper_csrwr_stlbps() */ stlb_ps =3D FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS); + asid =3D FIELD_EX64(env->CSR_ASID, CSR_ASID, ASID); + index =3D -1; if (pagesize =3D=3D stlb_ps) { /* Only write into STLB bits [47:13] */ address =3D entryhi & ~MAKE_64BIT_MASK(0, R_CSR_TLBEHI_64_VPPN_SHI= FT); - - /* Choose one set ramdomly */ - set =3D get_random_tlb(0, 7); - - /* Index in one set */ stlb_idx =3D (address >> (stlb_ps + 1)) & 0xff; /* [0,255] */ + for (i =3D 0; i < 8; ++i) { + tlb =3D &env->tlb[i * 256 + stlb_idx]; + tlb_e =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, E); + if (!tlb_e) { + index =3D i; + break; + } + + tlb_asid =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID); + if (asid !=3D tlb_asid) { + index =3D i; + } + } =20 - index =3D set * 256 + stlb_idx; + /* Choose one set randomly */ + if (index < 0) { + index =3D get_random_tlb(0, 7); + } + index =3D index * 256 + stlb_idx; } else { /* Only write into MTLB */ - index =3D get_random_tlb(LOONGARCH_STLB, LOONGARCH_TLB_MAX - 1); + for (i =3D LOONGARCH_STLB; i < LOONGARCH_TLB_MAX; i++) { + tlb =3D &env->tlb[i]; + tlb_e =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, E); + + if (!tlb_e) { + index =3D i; + break; + } + + tlb_asid =3D FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID); + if (asid !=3D tlb_asid) { + index =3D i; + } + } + + if (index < 0) { + index =3D get_random_tlb(LOONGARCH_STLB, LOONGARCH_TLB_MAX - 1= ); + } } =20 invalidate_tlb(env, index); --=20 2.39.3