Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 42DF3CD37BE for ; Tue, 12 May 2026 02:24:28 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wMcnF-0007bs-Vh; Mon, 11 May 2026 22:24:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wMcnE-0007bE-6r; Mon, 11 May 2026 22:24:20 -0400 Received: from out30-98.freemail.mail.aliyun.com ([115.124.30.98]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wMcnA-0006cl-Jt; Mon, 11 May 2026 22:24:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1778552645; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=decHTiCdSj9aBwe7XX1En6fSguPibbm1Nr+4Xi3d85M=; b=TgDXDzUqd0AyabDtHrP60QZPb8ZGZMCREDUAkJIUB/Uimk6c9p4RqEbSl/wgXmLi3kA+8GZhyC4bRGV3Qui0P9RqdaugUAAlxmSa3d10xB/eVI3knkhTJkKzOTvI0y/kEuWRIfRY+GwStLfeg1lqSpgQB17XU7wAjU7Ipgval9Q= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R121e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam011083073210; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=10; SR=0; TI=SMTPD_---0X2op2KD_1778552643; Received: from 30.166.64.212(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0X2op2KD_1778552643 cluster:ay36) by smtp.aliyun-inc.com; Tue, 12 May 2026 10:24:04 +0800 Message-ID: Date: Tue, 12 May 2026 10:24:03 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 3/6] target/riscv: Integrate SMMPT checks into MMU and TLB fill To: Alistair Francis Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org, chao.liu.zevorn@gmail.com, palmer@dabbelt.com, daniel.barboza@oss.qualcomm.com, liwei1518@gmail.com, Huang Tao , TANG Tiancheng , Daniel Henrique Barboza References: <20260408140635.42546-1-zhiwei_liu@linux.alibaba.com> <20260408140635.42546-4-zhiwei_liu@linux.alibaba.com> Content-Language: en-US From: LIU Zhiwei In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=115.124.30.98; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-98.freemail.mail.aliyun.com X-Spam_score_int: -174 X-Spam_score: -17.5 X-Spam_bar: ----------------- X-Spam_report: (-17.5 / 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, 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=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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Hi Alistair, On 4/10/26 9:44 AM, Alistair Francis wrote: > On Thu, Apr 9, 2026 at 12:08 AM LIU Zhiwei wrote: >> 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 | 76 ++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 75 insertions(+), 1 deletion(-) >> >> diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c >> index e3361fadae..2fab00e9b4 100644 >> --- a/target/riscv/cpu_helper.c >> +++ b/target/riscv/cpu_helper.c >> @@ -1161,6 +1161,60 @@ static bool check_svukte_addr(CPURISCVState *env, vaddr addr) >> return !high_bit; >> } >> >> +/* >> + * get_physical_address_mpt - check mpt permission for this physical address >> + * >> + * 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 == 0) { >> + *prot = 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 and >> + * encoded to mmu_idx, So we do not need to check it here. >> + */ >> + if (mode == PRV_M) { >> + *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; >> + return TRANSLATE_SUCCESS; >> + } >> + >> + mpt_has_access = smmpt_check_access(env, addr, >> + &mpt_access, access_type); >> + if (!mpt_has_access) { >> + *prot = 0; >> + return TRANSLATE_MPT_FAIL; >> + } >> + >> + *prot = smmpt_access_to_page_prot(mpt_access); >> + >> + return TRANSLATE_SUCCESS; >> +} >> + >> /* >> * get_physical_address - get the physical address for this virtual address >> * >> @@ -1355,6 +1409,13 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, >> pte_addr = base + idx * ptesize; >> } >> >> + int mpt_prot; >> + int mpt_ret = get_physical_address_mpt(env, &mpt_prot, pte_addr, >> + MMU_DATA_LOAD, PRV_S); >> + if (mpt_ret != TRANSLATE_SUCCESS) { >> + return TRANSLATE_MPT_FAIL; >> + } >> + >> int pmp_prot; >> int pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr, >> sxlen_bytes, >> @@ -1765,7 +1826,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, >> CPURISCVState *env = &cpu->env; >> vaddr im_address; >> hwaddr pa = 0; >> - int prot, prot2, prot_pmp; >> + int prot, prot2, prot_pmp, mpt_prot; >> bool pmp_violation = false; >> bool first_stage_error = true; >> bool two_stage_lookup = mmuidx_2stage(mmu_idx); >> @@ -1819,6 +1880,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, >> prot &= prot2; >> >> if (ret == TRANSLATE_SUCCESS) { >> + ret = get_physical_address_mpt(env, &mpt_prot, pa, >> + access_type, mode); >> + qemu_log_mask(CPU_LOG_MMU, >> + "%s MPT address=" HWADDR_FMT_plx " ret %d prot" >> + " %d\n", >> + __func__, pa, ret, mpt_prot); > We should only log if the extension is enabled Yes. Output it under the condition of riscv_cpu_cfg(env)->ext_smmpt in v6 patch set. Thanks, Zhiwei > > Alistair