From nobody Sun Sep 28 17:07:10 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757424754; cv=none; d=zohomail.com; s=zohoarc; b=XSBQTI1BvzVMG69/MjnPOvnMWUP7trsgj3hatfZHlcPYnEqbjTDe3MOFmo+SUPjmp/SFhEMx5Yz15mdaR3c29+ybzxgY+a3o1VK86vldC3XXzb49hSfkAmXS/NVBaA0J1DDuJ6uUwky4OrzLO5PP8cpUlY+sdfO23XSUW8ClXP8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757424754; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=56kzdKNRG71bPkG/4YMn6pcimop/cdvYexGOM9UznkY=; b=HEe7NfjT/LuYNfl/1YqBx7BgBhHFALBZS8MBlI1GH7pBYxhRIvTD5Ai25CVGWUs2eaapzMLwSWAkY5CyE5Eo1FDddQgbhMwBw6jgNtDfY/fpElx0j4mNqS0x7UoI9xNhzsdtZy1rHTmeaIuyWsWCho/KgKgA1txZiqiXujMWLWo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757424754767295.3253629221366; Tue, 9 Sep 2025 06:32:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvyRz-0005d7-OL; Tue, 09 Sep 2025 09:31:59 -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 1uvyRg-0005ZY-SQ; Tue, 09 Sep 2025 09:31:42 -0400 Received: from [115.124.30.118] (helo=out30-118.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvyRd-00016m-FL; Tue, 09 Sep 2025 09:31:40 -0400 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0Wne8Vdt_1757424372 cluster:ay36) by smtp.aliyun-inc.com; Tue, 09 Sep 2025 21:26:14 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757424688; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=56kzdKNRG71bPkG/4YMn6pcimop/cdvYexGOM9UznkY=; b=gOWbEktRr6Vm/w7/0xXxSAT4sAiZDLW8fZeD0J/LaAI90uBeSb/Cs3HKuuP5eabY8d5jGOQSCyJHQ/bVA9jRJ/4s+r2V97+pyKcplmmvf6zN0cMYne33MY2XamBs2qhAkuhCW049UO062Gjdx4TOWYACcQo/U7jfe12jFq+9eDs= From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, dbarboza@ventanamicro.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, Huang Tao , TANG Tiancheng Subject: [RFC PATCH 1/5] target/riscv: Add basic definitions and CSRs for SMMPT Date: Tue, 9 Sep 2025 21:25:29 +0800 Message-Id: <20250909132533.32205-2-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> References: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.118 (deferred) 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=115.124.30.118; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-118.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no 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-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757424755317116600 Content-Type: text/plain; charset="utf-8" This patch lays the groundwork for the SMMPT (Supervisor Domains Access Protection) extension by introducing its fundamental components. It adds: - New CPU configuration flags, `ext_smmpt` and `ext_smsdid`, to enable the extension. - Bit-field definitions for the `mmpt` CSR in `cpu_bits.h`. - The `mmpt` and `msdcfg` CSR numbers and their read/write handlers in `csr.c`. - New fields in `CPUArchState` to store the state of these new CSRs. - A new translation failure reason `TRANSLATE_MPT_FAIL`. This provides the necessary infrastructure for the core MPT logic and MMU integration that will follow. Co-authored-by: Huang Tao Co-authored-by: TANG Tiancheng Signed-off-by: LIU Zhiwei --- target/riscv/cpu.h | 9 +++- target/riscv/cpu_bits.h | 27 ++++++++++ target/riscv/cpu_cfg_fields.h.inc | 2 + target/riscv/csr.c | 83 +++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 4a862da615..fa7b804cb3 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -130,7 +130,8 @@ enum { TRANSLATE_SUCCESS, TRANSLATE_FAIL, TRANSLATE_PMP_FAIL, - TRANSLATE_G_STAGE_FAIL + TRANSLATE_G_STAGE_FAIL, + TRANSLATE_MPT_FAIL }; =20 /* Extension context status */ @@ -180,6 +181,7 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied= _rules[]; #if !defined(CONFIG_USER_ONLY) #include "pmp.h" #include "debug.h" +#include "riscv_smmpt.h" #endif =20 #define RV_VLEN_MAX 1024 @@ -486,6 +488,11 @@ struct CPUArchState { uint64_t hstateen[SMSTATEEN_MAX_COUNT]; uint64_t sstateen[SMSTATEEN_MAX_COUNT]; uint64_t henvcfg; + /* Smsdid */ + uint32_t mptmode; + uint32_t sdid; + uint64_t mptppn; + uint32_t msdcfg; #endif =20 /* Fields from here on are preserved across CPU reset. */ diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index b62dd82fe7..c6a34863d1 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -1164,4 +1164,31 @@ typedef enum CTRType { #define MCONTEXT64 0x0000000000001FFFULL #define MCONTEXT32_HCONTEXT 0x0000007F #define MCONTEXT64_HCONTEXT 0x0000000000003FFFULL + +/* Smsdid */ +#define CSR_MMPT 0xbc0 +#define CSR_MSDCFG 0xbd1 + +#define MMPT_MODE_MASK_32 0xC0000000 +#define MMPT_SDID_MASK_32 0x3F000000 +#define MMPT_PPN_MASK_32 0x003FFFFF + +#define MMPT_MODE_SHIFT_32 30 +#define MMPT_SDID_SHIFT_32 24 + +#define MMPT_MODE_MASK_64 0xF000000000000000ULL +#define MMPT_SDID_MASK_64 0x0FC0000000000000ULL +#define MMPT_PPN_MASK_64 0x000FFFFFFFFFFFFFULL + +#define MPTE_L3_VALID 0x0000100000000000ULL +#define MPTE_L3_RESERVED 0xFFFFE00000000000ULL + +#define MPTE_L2_RESERVED_64 0xFFFF800000000000ULL +#define MPTE_L2_RESERVED_32 0xFE000000 + +#define MPTE_L1_RESERVED_64 0xFFFFFFFF00000000ULL +#define MPTE_L1_RESERVED_32 0xFFFF0000 + +#define MMPT_MODE_SHIFT_64 60 +#define MMPT_SDID_SHIFT_64 54 #endif diff --git a/target/riscv/cpu_cfg_fields.h.inc b/target/riscv/cpu_cfg_field= s.h.inc index e2d116f0df..8c8a4ac236 100644 --- a/target/riscv/cpu_cfg_fields.h.inc +++ b/target/riscv/cpu_cfg_fields.h.inc @@ -60,6 +60,8 @@ BOOL_FIELD(ext_svpbmt) BOOL_FIELD(ext_svrsw60t59b) BOOL_FIELD(ext_svvptc) BOOL_FIELD(ext_svukte) +BOOL_FIELD(ext_smmpt) +BOOL_FIELD(ext_smsdid) BOOL_FIELD(ext_zdinx) BOOL_FIELD(ext_zaamo) BOOL_FIELD(ext_zacas) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 8842e07a73..77bc596ed3 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -793,6 +793,15 @@ static RISCVException rnmi(CPURISCVState *env, int csr= no) =20 return RISCV_EXCP_ILLEGAL_INST; } + +static RISCVException smsdid(CPURISCVState *env, int csrno) +{ + if (riscv_cpu_cfg(env)->ext_smsdid) { + return RISCV_EXCP_NONE; + } + + return RISCV_EXCP_ILLEGAL_INST; +} #endif =20 static RISCVException seed(CPURISCVState *env, int csrno) @@ -5470,6 +5479,77 @@ static RISCVException write_mnstatus(CPURISCVState *= env, int csrno, return RISCV_EXCP_NONE; } =20 +static RISCVException read_mmpt(CPURISCVState *env, int csrno, + target_ulong *val) +{ + if (riscv_cpu_xlen(env) =3D=3D 32) { + uint32_t value =3D 0; + value |=3D env->mptmode << MMPT_MODE_SHIFT_32; + value |=3D (env->sdid << MMPT_SDID_SHIFT_32) & MMPT_SDID_MASK_32; + value |=3D env->mptppn & MMPT_PPN_MASK_32; + *val =3D value; + } else if (riscv_cpu_xlen(env) =3D=3D 64) { + uint64_t value_64 =3D 0; + uint32_t mode_value =3D env->mptmode; + /* mpt_mode_t convert to mmpt.mode value */ + if (mode_value) { + mode_value -=3D SMMTT43 - SMMTT34; + } + value_64 |=3D (uint64_t)mode_value << MMPT_MODE_SHIFT_64; + value_64 |=3D ((uint64_t)env->sdid << MMPT_SDID_SHIFT_64) + & MMPT_SDID_MASK_64; + value_64 |=3D (uint64_t)env->mptppn & MMPT_PPN_MASK_64; + *val =3D value_64; + } else { + return RISCV_EXCP_ILLEGAL_INST; + } + return RISCV_EXCP_NONE; +} + +static RISCVException write_mmpt(CPURISCVState *env, int csrno, + target_ulong val, uintptr_t ra) +{ + /* Fixme: if mode is bare, the remaining fields in mmpt must be zero */ + if (riscv_cpu_xlen(env) =3D=3D 32) { + /* Only write the legal value */ + uint32_t mode_value =3D (val & MMPT_MODE_MASK_32) >> MMPT_MODE_SHI= FT_32; + if (mode_value <=3D SMMTT34) { + env->mptmode =3D mode_value; + } + env->sdid =3D (val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32; + env->mptppn =3D val & MMPT_PPN_MASK_32; + } else if (riscv_cpu_xlen(env) =3D=3D 64) { + uint32_t mode_value =3D (val & MMPT_MODE_MASK_64) >> MMPT_MODE_SHI= FT_64; + /* check legal value */ + if (mode_value < SMMTTMAX) { + /* convert to mpt_mode_t */ + if (mode_value) { + mode_value +=3D SMMTT43 - SMMTT34; + } + env->mptmode =3D mode_value; + } + env->sdid =3D (val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64; + env->mptppn =3D val & MMPT_PPN_MASK_64; + } else { + return RISCV_EXCP_ILLEGAL_INST; + } + return RISCV_EXCP_NONE; +} + +static RISCVException read_msdcfg(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->msdcfg; + return RISCV_EXCP_NONE; +} + +static RISCVException write_msdcfg(CPURISCVState *env, int csrno, + target_ulong val, uintptr_t ra) +{ + env->msdcfg =3D val; + return RISCV_EXCP_NONE; +} + #endif =20 /* Crypto Extension */ @@ -6666,6 +6746,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { write_mhpmcounterh }, [CSR_SCOUNTOVF] =3D { "scountovf", sscofpmf, read_scountovf, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + /* Supervisor Domain Identifier and Protection Registers */ + [CSR_MMPT] =3D { "mmpt", smsdid, read_mmpt, write_mmpt }, + [CSR_MSDCFG] =3D { "msdcfg", smsdid, read_msdcfg, write_msdcfg }, =20 #endif /* !CONFIG_USER_ONLY */ }; --=20 2.25.1 From nobody Sun Sep 28 17:07:10 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757424818; cv=none; d=zohomail.com; s=zohoarc; b=iDF+qjybOCQwNDoGWZmWjqePRl0Xujh6aL87g9ehKXi6JCoLmB4TgVQugydHUWzc0HMWc1PK3MxA+1r5ETO6gbigwm580IIufqFa0439SGj4tLnwdzd8rYj5/cagW1u0TSxHl7jkH0Xspkq+K+BPfxoEyaQtcbx0nnSBp4Rw2ls= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757424818; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+931NDxV71ChHyxzsiMduH5MqT002FNHALfaskCv47w=; b=XPGAgBvExGffIu1DZPXHY0WN0N8tpM65bezqG6niY75fe8TLmXtT/8LG2OzsIvUuugG1qna+Gf2w1AoZ0XeMILYwLyf7roZM45epJBzZgRhqg7sliMpQsqwqg0SBIFhhiSmIFzRE1v4mFcMe9Lg05vrvXqdkB7VaEC75CjLwC9g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757424818169490.45977552508396; Tue, 9 Sep 2025 06:33:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvySR-0005l2-40; Tue, 09 Sep 2025 09:32:29 -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 1uvySI-0005hO-Ft; Tue, 09 Sep 2025 09:32:18 -0400 Received: from [115.124.30.99] (helo=out30-99.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvyS8-0001Bb-Hq; Tue, 09 Sep 2025 09:32:18 -0400 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0WneAkoD_1757424404 cluster:ay36) by smtp.aliyun-inc.com; Tue, 09 Sep 2025 21:26:45 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757424719; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=+931NDxV71ChHyxzsiMduH5MqT002FNHALfaskCv47w=; b=RWY6tV+UpTO4aLMs4TIbj0oI7Lc9cAtjj0njd/lY/pFiqInAETQex0CwSQMeADmXlvqSBRw2AVeUzafzzLRnXgVnm1SlT8umJ3OyMHIMJfSIQSVL2m8+lxdORaRx0GqaPHJvAmb+f0kJOcJUc4AX6ooQ+6tOoKj3CWWO6O/6Zrk= From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, dbarboza@ventanamicro.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, Huang Tao , TANG Tiancheng Subject: [RFC PATCH 2/5] target/riscv: Implement core SMMPT lookup logic Date: Tue, 9 Sep 2025 21:25:30 +0800 Message-Id: <20250909132533.32205-3-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> References: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.99 (deferred) 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=115.124.30.99; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-99.freemail.mail.aliyun.com X-Spam_score_int: -86 X-Spam_score: -8.7 X-Spam_bar: -------- X-Spam_report: (-8.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=no 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-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757424820992116600 Content-Type: text/plain; charset="utf-8" This patch introduces the core implementation for the Memory Protection Tab= le (MPT) walk, which is the central mechanism of the SMMPT extension. A new file, `riscv_smmpt.c`, is added to encapsulate the MPT logic. It implements the `smmpt_lookup()` function, which performs a multi-level page table-like walk starting from the physical address specified in the `mptppn` CSR field. This walk determines the access permissions (read, write, execute) for a given physical address. The implementation supports various SMMPT modes (SMMTT34, SMMTT43, etc.) and correctly handles leaf and non-leaf entries, including reserved bit checks. Helper functions for parsing MPT entries and converting access permissions are also included in the new `riscv_smmpt.h` header. Co-authored-by: Huang Tao Co-authored-by: TANG Tiancheng Signed-off-by: LIU Zhiwei --- target/riscv/meson.build | 1 + target/riscv/pmp.h | 3 + target/riscv/riscv_smmpt.c | 273 +++++++++++++++++++++++++++++++++++++ target/riscv/riscv_smmpt.h | 38 ++++++ 4 files changed, 315 insertions(+) create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h diff --git a/target/riscv/meson.build b/target/riscv/meson.build index a4bd61e52a..e85b534a64 100644 --- a/target/riscv/meson.build +++ b/target/riscv/meson.build @@ -29,6 +29,7 @@ riscv_system_ss =3D ss.source_set() riscv_system_ss.add(files( 'arch_dump.c', 'pmp.c', + 'riscv_smmpt.c', 'debug.c', 'monitor.c', 'machine.c', diff --git a/target/riscv/pmp.h b/target/riscv/pmp.h index 271cf24169..d9c5e74345 100644 --- a/target/riscv/pmp.h +++ b/target/riscv/pmp.h @@ -85,6 +85,9 @@ void pmp_update_rule_nums(CPURISCVState *env); uint32_t pmp_get_num_rules(CPURISCVState *env); int pmp_priv_to_page_prot(pmp_priv_t pmp_priv); void pmp_unlock_entries(CPURISCVState *env); +int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr, + int size, MMUAccessType access_type, + int mode); =20 #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML) #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP) diff --git a/target/riscv/riscv_smmpt.c b/target/riscv/riscv_smmpt.c new file mode 100644 index 0000000000..09e3c66c7f --- /dev/null +++ b/target/riscv/riscv_smmpt.c @@ -0,0 +1,273 @@ +/* + * QEMU RISC-V Smmpt (Memory Protection Table) + * + * Copyright (c) 2024 Alibaba Group. All rights reserved. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "riscv_smmpt.h" +#include "pmp.h" +#include "system/memory.h" + +typedef uint64_t load_entry_fn(AddressSpace *, hwaddr, + MemTxAttrs, MemTxResult *); + +static uint64_t load_entry_32(AddressSpace *as, hwaddr addr, + MemTxAttrs attrs, MemTxResult *result) +{ + return address_space_ldl(as, addr, attrs, result); +} + +static uint64_t load_entry_64(AddressSpace *as, hwaddr addr, + MemTxAttrs attrs, MemTxResult *result) +{ + return address_space_ldq(as, addr, attrs, result); +} + +typedef union { + uint64_t raw; + struct { + uint32_t v:1; + uint32_t l:1; + uint32_t rsv1:5; + uint32_t perms:24; + uint32_t n:1; + } leaf32; + struct { + uint32_t v:1; + uint32_t l:1; + uint32_t rsv1:8; + uint32_t ppn:22; + } nonleaf32; + struct { + uint64_t v:1; + uint64_t l:1; + uint64_t rsv1:8; + uint64_t perms:48; + uint64_t rsv2:5; + uint64_t n:1; + } leaf64; + struct { + uint64_t v:1; + uint64_t l:1; + uint64_t rsv1:8; + uint64_t ppn:52; + uint64_t rsv2:1; + uint64_t n:1; + } nonleaf64; +} mpte_union_t; + +static inline bool mpte_is_leaf(uint64_t mpte) +{ + return mpte & 0x2; +} + +static inline bool mpte_is_valid(uint64_t mpte) +{ + return mpte & 0x1; +} + +static uint64_t mpte_get_rsv(CPURISCVState *env, uint64_t mpte) +{ + RISCVMXL mxl =3D riscv_cpu_mxl(env); + bool leaf =3D mpte_is_leaf(mpte); + mpte_union_t *u =3D (mpte_union_t *)&mpte; + + return (mxl =3D=3D MXL_RV32) + ? (leaf ? u->leaf32.rsv1 : u->nonleaf32.rsv1) + : (leaf ? (u->leaf64.rsv1 << 5) | u->leaf64.rsv2 + : (u->nonleaf64.rsv1 << 1) | u->nonleaf64.rsv2); +} + +static uint64_t mpte_get_perms(CPURISCVState *env, uint64_t mpte) +{ + RISCVMXL mxl =3D riscv_cpu_mxl(env); + mpte_union_t *u =3D (mpte_union_t *)&mpte; + + return (mxl =3D=3D MXL_RV32) ? u->leaf32.perms : u->leaf64.perms; +} + +static bool mpte_check_nlnapot(CPURISCVState *env, uint64_t mpte, bool *nl= napot) +{ + RISCVMXL mxl =3D riscv_cpu_mxl(env); + mpte_union_t *u =3D (mpte_union_t *)&mpte; + if (mxl =3D=3D MXL_RV32) { + *nlnapot =3D false; + return true; + } + *nlnapot =3D u->nonleaf64.n; + return u->nonleaf64.n ? (u->nonleaf64.ppn & 0x1ff) =3D=3D 0x100 : true; +} + +static uint64_t mpte_get_ppn(CPURISCVState *env, uint64_t mpte, int pn, + bool nlnapot) +{ + RISCVMXL mxl =3D riscv_cpu_mxl(env); + mpte_union_t *u =3D (mpte_union_t *)&mpte; + + if (nlnapot) { + return deposit64(u->nonleaf64.ppn, 0, 9, pn & 0x1ff); + } + return (mxl =3D=3D MXL_RV32) ? u->nonleaf32.ppn : u->nonleaf64.ppn; +} + +/* Caller should assert i before call this interface */ +static int mpt_get_pn(hwaddr addr, int i, mpt_mode_t mode) +{ + if (mode =3D=3D SMMTT34) { + return i =3D=3D 0 + ? extract64(addr, 15, 10) + : extract64(addr, 25, 9); + } else { + int offset =3D 16 + i * 9; + if ((mode =3D=3D SMMTT64) && (i =3D=3D 4)) { + return extract64(addr, offset, 12); + } else { + return extract64(addr, offset, 9); + } + } +} + +/* Caller should assert i before call this interface */ +static int mpt_get_pi(hwaddr addr, int i, mpt_mode_t mode) +{ + if (mode =3D=3D SMMTT34) { + return i =3D=3D 0 + ? extract64(addr, 12, 3) + : extract64(addr, 22, 3); + } else { + int offset =3D 16 + i * 9; + return extract64(addr, offset - 4, 4); + } +} + +static bool smmpt_lookup(CPURISCVState *env, hwaddr addr, mpt_mode_t mode, + mpt_access_t *allowed_access, + MMUAccessType access_type) +{ + MemTxResult res; + MemTxAttrs attrs =3D MEMTXATTRS_UNSPECIFIED; + CPUState *cs =3D env_cpu(env); + hwaddr mpte_addr, base =3D (hwaddr)env->mptppn << PGSHIFT; + load_entry_fn *load_entry; + uint32_t mptesize, levels, xwr; + int pn, pi, pmp_prot, pmp_ret; + uint64_t mpte, perms; + + switch (mode) { + case SMMTT34: + load_entry =3D &load_entry_32; levels =3D 2; mptesize =3D 4; break; + case SMMTT43: + load_entry =3D &load_entry_64; levels =3D 3; mptesize =3D 8; break; + break; + case SMMTT52: + load_entry =3D &load_entry_64; levels =3D 4; mptesize =3D 8; break; + case SMMTT64: + load_entry =3D &load_entry_64; levels =3D 5; mptesize =3D 8; break; + case SMMTTBARE: + *allowed_access =3D ACCESS_ALLOW_RWX; + return true; + default: + g_assert_not_reached(); + break; + } + for (int i =3D levels - 1; i >=3D 0 ; i--) { + /* 1. Get pn[i] as the mpt index */ + pn =3D mpt_get_pn(addr, i, mode); + /* 2. Get mpte address and get mpte */ + mpte_addr =3D base + pn * mptesize; + pmp_ret =3D get_physical_address_pmp(env, &pmp_prot, mpte_addr, + mptesize, MMU_DATA_LOAD, PRV_M); + if (pmp_ret !=3D TRANSLATE_SUCCESS) { + return false; + } + mpte =3D load_entry(cs->as, mpte_addr, attrs, &res); + /* 3. Check valid bit and reserve bits of mpte */ + if (!mpte_is_valid(mpte) || mpte_get_rsv(env, mpte)) { + return false; + } + + /* 4. Process non-leaf node */ + if (!mpte_is_leaf(mpte)) { + bool nlnapot =3D false; + if (i =3D=3D 0) { + return false; + } + if (!mpte_check_nlnapot(env, mpte, &nlnapot)) { + return false; + } + base =3D mpte_get_ppn(env, mpte, pn, nlnapot) << PGSHIFT; + continue; + } + + /* 5. Process leaf node */ + pi =3D mpt_get_pi(addr, i, mode); + perms =3D mpte_get_perms(env, mpte); + xwr =3D (perms >> (pi * 3)) & 0x7; + switch (xwr) { + case ACCESS_ALLOW_R: + *allowed_access =3D ACCESS_ALLOW_R; + return access_type =3D=3D MMU_DATA_LOAD; + case ACCESS_ALLOW_X: + *allowed_access =3D ACCESS_ALLOW_X; + return access_type =3D=3D MMU_INST_FETCH; + case ACCESS_ALLOW_RX: + *allowed_access =3D ACCESS_ALLOW_R; + return (access_type =3D=3D MMU_DATA_LOAD || + access_type =3D=3D MMU_INST_FETCH); + case ACCESS_ALLOW_RW: + *allowed_access =3D ACCESS_ALLOW_RW; + return (access_type =3D=3D MMU_DATA_LOAD || + access_type =3D=3D MMU_DATA_STORE); + case ACCESS_ALLOW_RWX: + *allowed_access =3D ACCESS_ALLOW_RWX; + return true; + default: + return false; + } + } + return false; +} + +bool smmpt_check_access(CPURISCVState *env, hwaddr addr, + mpt_access_t *allowed_access, MMUAccessType access= _type) +{ + bool mpt_has_access; + mpt_mode_t mode =3D env->mptmode; + + mpt_has_access =3D smmpt_lookup(env, addr, mode, + allowed_access, access_type); + return mpt_has_access; +} + +/* + * Convert MPT access to TLB page privilege. + */ +int smmpt_access_to_page_prot(mpt_access_t mpt_access) +{ + int prot; + switch (mpt_access) { + case ACCESS_ALLOW_R: + prot =3D PAGE_READ; + break; + case ACCESS_ALLOW_X: + prot =3D PAGE_EXEC; + break; + case ACCESS_ALLOW_RX: + prot =3D PAGE_READ | PAGE_EXEC; + break; + case ACCESS_ALLOW_RW: + prot =3D PAGE_READ | PAGE_WRITE; + break; + case ACCESS_ALLOW_RWX: + prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + break; + default: + prot =3D 0; + break; + } + + return prot; +} diff --git a/target/riscv/riscv_smmpt.h b/target/riscv/riscv_smmpt.h new file mode 100644 index 0000000000..d5797c6168 --- /dev/null +++ b/target/riscv/riscv_smmpt.h @@ -0,0 +1,38 @@ +/* + * QEMU RISC-V Smmpt (Memory Protection Table) + * + * Copyright (c) 2024 Alibaba Group. All rights reserved. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef RISCV_SMPT_H +#define RISCV_SMPT_H + +#include "cpu.h" +#include "exec/mmu-access-type.h" + +typedef enum { + SMMTTBARE =3D 0, + SMMTT34 =3D 1, + SMMTT43 =3D 2, + SMMTT52 =3D 3, + SMMTT64 =3D 4, + SMMTTMAX +} mpt_mode_t; + +typedef enum { + ACCESS_ALLOW_R =3D 0b001, + ACCESS_ALLOW_X =3D 0b100, + ACCESS_ALLOW_RX =3D 0b101 , + ACCESS_ALLOW_RW =3D 0b011, + ACCESS_ALLOW_RWX =3D 0b111, +} mpt_access_t; + + +int smmpt_access_to_page_prot(mpt_access_t mpt_access); +bool smmpt_check_access(CPURISCVState *env, hwaddr addr, + mpt_access_t *allowed_access, + MMUAccessType access_type); + +#endif --=20 2.25.1 From nobody Sun Sep 28 17:07:10 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757424824; cv=none; d=zohomail.com; s=zohoarc; b=Ht8F/6urua7pYHMGEacB5GbACx0v0fXxDVnfBy4IzJLkFa7cJ+c4vUW303AVxmVb/T1ymOIoY/tUEKlXx+SvtzHBemxMvnoAAJSV4id5JiQgcNfM7AjpvNNbZIb0a7bPMmb9LdaHbJKx1MqIdNnjL06tf0QRDYfyoWBsuMXPHeY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757424824; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gw6WrdEWOXCFYmJRkwfOwXhMJaiG9KJ+Gbqni+5Uea8=; b=gmb9rpq0f8TbI01cJYbNWLhae/q29v4AlxPxZxRcsxXTREZHfRF1cmMQfFJmS+09Oenrjg2wv46Kk/BYZznuGk3Y94gIgzfYJ9DS8ZS+gBmQAc8ZNDk9dmeXEFXsk8S52UCG+9z/r1+f3szYkHWBUSU1NPcsjLci5bzcaUmh3+Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757424824193899.4547702985196; Tue, 9 Sep 2025 06:33:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvyT8-0006KU-G6; Tue, 09 Sep 2025 09:33: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 1uvySv-0006BY-7U; Tue, 09 Sep 2025 09:32:58 -0400 Received: from [115.124.30.133] (helo=out30-133.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvySl-0001GX-Qw; Tue, 09 Sep 2025 09:32:56 -0400 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0WneAkv-_1757424436 cluster:ay36) by smtp.aliyun-inc.com; Tue, 09 Sep 2025 21:27:18 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757424760; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=gw6WrdEWOXCFYmJRkwfOwXhMJaiG9KJ+Gbqni+5Uea8=; b=inwzKV+ZEU8usZWq7CUtqZ9csrPgrjuX+k1ke/ObI7XVn59/QODlYZbcbB6ZY7sIkaWSRLgpXQEG6NabrvcsSZNEY/as3JAWfi3R7UUt7XM37eMb7ZQt7uKp67MPgEIfTRwZXr/4YVgRJcQVg8EgjjS9ym49wqCWLdVpD9PGFIQ= From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, dbarboza@ventanamicro.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, Huang Tao , TANG Tiancheng Subject: [RFC PATCH 3/5] target/riscv: Integrate SMMPT checks into MMU and TLB fill Date: Tue, 9 Sep 2025 21:25:31 +0800 Message-Id: <20250909132533.32205-4-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> References: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.133 (deferred) 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=115.124.30.133; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-133.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no 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-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757424826941116600 Content-Type: text/plain; charset="utf-8" With the core MPT lookup logic in place, this patch integrates the permission checks into QEMU's main MMU processing functions. A new helper, `get_physical_address_mpt`, is introduced to check the permissions for a given physical address against the MPT. This helper is then called at two critical points: 1. During page table walks (`get_physical_address`): The physical address of the Page Table Entry (PTE) itself is checked to ensure the supervisor has permission to read it. 2. After successful address translation (`riscv_cpu_tlb_fill`): The final guest-physical address is checked against the MPT before the access is allowed to proceed. This ensures that SMMPT protection is enforced for both the translation process and the final memory access, as required by the specification. Co-authored-by: Huang Tao Co-authored-by: TANG Tiancheng Signed-off-by: LIU Zhiwei Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu_helper.c | 81 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 3479a62cc7..f8ca74ef61 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1089,9 +1089,8 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ul= ong newpriv, bool virt_en) * @access_type: The type of MMU access * @mode: Indicates current privilege level. */ -static int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr = addr, - int size, MMUAccessType access_type, - int mode) +int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr, + int size, MMUAccessType access_type, int mode) { pmp_priv_t pmp_priv; bool pmp_has_privs; @@ -1162,6 +1161,60 @@ static bool check_svukte_addr(CPURISCVState *env, va= ddr addr) return !high_bit; } =20 +/* + * get_physical_address_mpt - check mpt permission for this physical addre= ss + * + * Lookup the Memory Protection Table and check permission for this + * physical address. Returns 0 if the permission checking was successful + * + * @env: CPURISCVState + * @prot: The returned protection attributes + * @addr: The physical address to be checked permission + * @access_type: The type of MMU access + * @mode: Indicates current privilege level. + */ +static int get_physical_address_mpt(CPURISCVState *env, int *prot, hwaddr = addr, + MMUAccessType access_type, int mode) +{ + mpt_access_t mpt_access; + bool mpt_has_access; + + /* + * If the extension is not supported or the mmpt.mode is Bare, + * there is no protection, return success. + */ + if (!riscv_cpu_cfg(env)->ext_smmpt || env->mptmode =3D=3D 0) { + *prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + return TRANSLATE_SUCCESS; + } + + /* + * MPT is checked for all accesses to physical memory, unless the + * effective privilege mode is M. + * + * Data accesses in M-mode when the MPRV bit in mstatus is set and + * the MPP field in mstatus contains S or U are subject to MPT checks. + * + * In riscv_env_mmu_index, The MPRV and MPP bits are already checked a= nd + * encoded to mmu_idx, So we do not need to check it here. + */ + if (mode =3D=3D PRV_M) { + *prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; + return TRANSLATE_SUCCESS; + } + + mpt_has_access =3D smmpt_check_access(env, addr, + &mpt_access, access_type); + if (!mpt_has_access) { + *prot =3D 0; + return TRANSLATE_MPT_FAIL; + } + + *prot =3D smmpt_access_to_page_prot(mpt_access); + + return TRANSLATE_SUCCESS; +} + /* * get_physical_address - get the physical address for this virtual address * @@ -1356,6 +1409,13 @@ static int get_physical_address(CPURISCVState *env, = hwaddr *physical, pte_addr =3D base + idx * ptesize; } =20 + int mpt_prot; + int mpt_ret =3D get_physical_address_mpt(env, &mpt_prot, pte_addr, + MMU_DATA_LOAD, PRV_S); + if (mpt_ret !=3D TRANSLATE_SUCCESS) { + return TRANSLATE_MPT_FAIL; + } + int pmp_prot; int pmp_ret =3D get_physical_address_pmp(env, &pmp_prot, pte_addr, sxlen_bytes, @@ -1766,7 +1826,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, CPURISCVState *env =3D &cpu->env; vaddr im_address; hwaddr pa =3D 0; - int prot, prot2, prot_pmp; + int prot, prot2, prot_pmp, mpt_prot; bool pmp_violation =3D false; bool first_stage_error =3D true; bool two_stage_lookup =3D mmuidx_2stage(mmu_idx); @@ -1820,6 +1880,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address,= int size, prot &=3D prot2; =20 if (ret =3D=3D TRANSLATE_SUCCESS) { + ret =3D get_physical_address_mpt(env, &mpt_prot, pa, + access_type, mode); + qemu_log_mask(CPU_LOG_MMU, + "%s MPT address=3D" HWADDR_FMT_plx " ret %d = prot" + " %d\n", + __func__, pa, ret, mpt_prot); + prot &=3D mpt_prot; ret =3D get_physical_address_pmp(env, &prot_pmp, pa, size, access_type, mode); tlb_size =3D pmp_get_tlb_size(env, pa); @@ -1855,6 +1922,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address,= int size, __func__, address, ret, pa, prot); =20 if (ret =3D=3D TRANSLATE_SUCCESS) { + ret =3D get_physical_address_mpt(env, &mpt_prot, pa, + access_type, mode); + qemu_log_mask(CPU_LOG_MMU, + "%s MPT address=3D" HWADDR_FMT_plx " ret %d prot= %d\n", + __func__, pa, ret, mpt_prot); + prot &=3D mpt_prot; ret =3D get_physical_address_pmp(env, &prot_pmp, pa, size, access_type, mode); tlb_size =3D pmp_get_tlb_size(env, pa); --=20 2.25.1 From nobody Sun Sep 28 17:07:10 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757424880; cv=none; d=zohomail.com; s=zohoarc; b=XlhIs5leDn6LVxaz244ljUO+6CT36kLZbr1Zuy4LGVxJR04ECMlXCbjfH96YU4KYaPwVLW4xtJHWK1pvD5BkyVbhnYY0P3zimBLY0xnpMAE/u5gAOXz18mRzYIeUWpboAuZNDJ+VHJt32wGcaOgzuawZY5VZS+QvmGk8okim3gU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757424880; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EZZo9vvOzdXYVfJlzbw5Ldq0ISDkkHtD4yrp9+oIuvc=; b=Btu47Tlg8TpJV2elPu2ORQgxMHQlUnWm+JB3MoQ2K8xnzCPoK6TQhpsAp2G/W1rdJjs7jNyC3T0vBak/rf1bUJh6zCALXIpyEQlvUh5TV6L5HyGMrykL9E2sGI6zVkRcgQMBQdA2pZQGFPhctn3AHTe15n6x58hy1YVtTVJBzdA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757424880140404.7382479591066; Tue, 9 Sep 2025 06:34:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvyTi-0006db-2M; Tue, 09 Sep 2025 09:33: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 1uvyTG-0006Uo-Fl; Tue, 09 Sep 2025 09:33:20 -0400 Received: from [115.124.30.118] (helo=out30-118.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvyT6-0001JJ-KQ; Tue, 09 Sep 2025 09:33:16 -0400 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0Wne8VxD_1757424469 cluster:ay36) by smtp.aliyun-inc.com; Tue, 09 Sep 2025 21:27:50 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757424781; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=EZZo9vvOzdXYVfJlzbw5Ldq0ISDkkHtD4yrp9+oIuvc=; b=YFd4ng5q+UJ1qcDhh2FYsY77/tFcRMx0cjA0V/bwgrBDAGCLEs8xj524byZr1XkpLc3+jDkR61F3ZwFza5CrbOerGmtHeSF1Axv5qBuxcnDc5y2KmXKLLG/+72UmHwzPo5P7J2JJg77kB01bQp+lwN1irhH6zR1MO7AvaWIqIws= From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, dbarboza@ventanamicro.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, Huang Tao , TANG Tiancheng Subject: [RFC PATCH 4/5] target/riscv: Implement SMMPT fence instructions Date: Tue, 9 Sep 2025 21:25:32 +0800 Message-Id: <20250909132533.32205-5-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> References: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.118 (deferred) 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=115.124.30.118; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-118.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no 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-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757424881272116600 Content-Type: text/plain; charset="utf-8" This patch completes the SMMPT implementation by adding support for the new fence instructions: `mfence.spa` and `minval.spa`. According to the specification, these instructions act as memory ordering fences for MPT updates. In QEMU's TCG model, this is conservatively implemented by flushing the entire TLB, which ensures that any subsequent memory accesses will re-evaluate permissions and see the effects of any pri= or MPT modifications. The instructions are privileged and will cause an illegal instruction exception if executed outside of M-mode. Co-authored-by: Huang Tao Co-authored-by: TANG Tiancheng Signed-off-by: LIU Zhiwei Reviewed-by: Daniel Henrique Barboza --- target/riscv/insn32.decode | 2 ++ .../riscv/insn_trans/trans_privileged.c.inc | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index cd23b1f3a9..cf58f1beee 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -120,6 +120,8 @@ sret 0001000 00010 00000 000 00000 1110011 mret 0011000 00010 00000 000 00000 1110011 wfi 0001000 00101 00000 000 00000 1110011 sfence_vma 0001001 ..... ..... 000 00000 1110011 @sfence_vma +mfence_spa 1000011 ..... ..... 000 00000 1110011 @sfence_vma +minval_spa 0000011 ..... ..... 000 00000 1110011 @sfence_vma =20 # *** NMI *** mnret 0111000 00010 00000 000 00000 1110011 diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index 8a62b4cfcd..5ec6bf5991 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -160,3 +160,33 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sf= ence_vma *a) #endif return false; } + +#define REQUIRE_SMSDID(ctx) do { \ + if (!ctx->cfg_ptr->ext_smsdid) { \ + return false; \ + } \ +} while (0) + +static bool do_mfence_spa(DisasContext *ctx) +{ +#ifndef CONFIG_USER_ONLY + REQUIRE_SMSDID(ctx); + if (ctx->priv !=3D PRV_M) { + return false; + } + decode_save_opc(ctx, 0); + gen_helper_tlb_flush_all(tcg_env); + return true; +#endif + return false; +} + +static bool trans_mfence_spa(DisasContext *ctx, arg_mfence_spa *a) +{ + return do_mfence_spa(ctx); +} + +static bool trans_minval_spa(DisasContext *ctx, arg_minval_spa *a) +{ + return do_mfence_spa(ctx); +} --=20 2.25.1 From nobody Sun Sep 28 17:07:10 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757424886; cv=none; d=zohomail.com; s=zohoarc; b=h/yQauIfX9pQbR1eJsQvVpV7iWKy5TF9Hk8MYZLz+CDEkEkq0OC8oWyFJqB5z2YtwRcrLNNG7DaOMPT2FWed+shfpviDxb9xECuih3HXnHNGU7962lHGJh3+mUJk3gLk+bRNc/emHFOlfKourw1SFdyXLljRPLCktKVs9MphOlo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757424886; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vUCzDG+qnvs+uu8Ojy11cwxgGaVShk/EfO9V1rHEnTw=; b=aqYvJlkzFJW653poWJ3LimYQSe2EY8LY+n/QXAmEU7as5aOvK1cYIeMK9opNWGOWuPXohrEFcJqrJX9yTqiWQAgHdhD4Z0d6gS57rx3+PpifbpJRnX0hVVno2xU+RCQEm3j+wc341aKRm7Oe8OYX29OCGoMb5Xj3NkBrff7cbvw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757424886601453.76185389186844; Tue, 9 Sep 2025 06:34:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvyU0-0007I1-Rc; Tue, 09 Sep 2025 09:34:05 -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 1uvyTj-0006mU-8k; Tue, 09 Sep 2025 09:33:49 -0400 Received: from [115.124.30.97] (helo=out30-97.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvyTc-0001Mv-FL; Tue, 09 Sep 2025 09:33:46 -0400 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0WneAl6u_1757424501 cluster:ay36) by smtp.aliyun-inc.com; Tue, 09 Sep 2025 21:28:22 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757424813; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=vUCzDG+qnvs+uu8Ojy11cwxgGaVShk/EfO9V1rHEnTw=; b=nXzCTZbrhOZazNq/mkUgz+ftVoOTwXc9L0K1+wBGTVmdShMjPGzj+3PmexEELlPmRxnHll/jUUNttjyQZEOMUzT2qMxo3E0OhLapAyKvFjvKPu32EfRmpDlecFc4xFjPZ3XoZX+yfcM2igYAKY89eNYroHFGmYuJjH3w9WNvdQI= From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, dbarboza@ventanamicro.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, Huang Tao , TANG Tiancheng Subject: [RFC PATCH 5/5] target/riscv: Enable SMMPT extension Date: Tue, 9 Sep 2025 21:25:33 +0800 Message-Id: <20250909132533.32205-6-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> References: <20250909132533.32205-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.97 (deferred) 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=115.124.30.97; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-97.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no 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-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757424887363116600 Content-Type: text/plain; charset="utf-8" Co-authored-by: Huang Tao Co-authored-by: TANG Tiancheng Signed-off-by: LIU Zhiwei --- target/riscv/cpu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index d055ddf462..eea0942cf5 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -204,7 +204,9 @@ const RISCVIsaExtData isa_edata_arr[] =3D { ISA_EXT_DATA_ENTRY(smdbltrp, PRIV_VERSION_1_13_0, ext_smdbltrp), ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp), ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi), + ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid), ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm), + ISA_EXT_DATA_ENTRY(smmpt, PRIV_VERSION_1_13_0, ext_smmpt), ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen), ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia), @@ -1279,6 +1281,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = =3D { MULTI_EXT_CFG_BOOL("smmpm", ext_smmpm, false), MULTI_EXT_CFG_BOOL("smnpm", ext_smnpm, false), MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false), + MULTI_EXT_CFG_BOOL("smsdid", ext_smsdid, false), + MULTI_EXT_CFG_BOOL("smmpt", ext_smmpt, false), MULTI_EXT_CFG_BOOL("ssaia", ext_ssaia, false), MULTI_EXT_CFG_BOOL("ssdbltrp", ext_ssdbltrp, false), MULTI_EXT_CFG_BOOL("svade", ext_svade, false), --=20 2.25.1