From nobody Sat May 30 17:44:12 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=oss.qualcomm.com ARC-Seal: i=1; a=rsa-sha256; t=1779470774; cv=none; d=zohomail.com; s=zohoarc; b=Mge+FVdTLXmnrw5p1mBW7+KS/wSoxJbDKHLERA9qKAQP8mnIojn992UacbxgZmboU+HNvGZ1OXaXpFFnEZsKnlaMjQvNBv6mM/SwVwtHB5+bNvSY+Agsznwwn2XXdX/o393Jw/Z5sr5UDXLlrGcjuOuOxaldBEu0xIK8gQwmXzs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1779470774; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=L1QJuou29xu3g0tnX1YPz/gvIfXQY74LadtdxGnVtec=; b=nJrwkfZui4QMtI0lyOxQ9MxHJgW86laI54G1ILXQc1tGYbpxVYPFFA9lC3jo2xvRDoM6tGhaNr/GZ3+7X40fRsiJuJYeGJMlDt54v5FMeYB/0VRvNl/DS1gC9FybF9KRFxytKBCTzLURfUsbvqrduTEL3sfJdHRzNBUNzYkoU+k= 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=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1779470774031215.2940430804773; Fri, 22 May 2026 10:26:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wQTcb-0000Dy-Jr; Fri, 22 May 2026 13:25:17 -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 1wQTca-0000D0-9w for qemu-devel@nongnu.org; Fri, 22 May 2026 13:25:16 -0400 Received: from mx0b-0031df01.pphosted.com ([205.220.180.131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wQTcX-0004Ad-Is for qemu-devel@nongnu.org; Fri, 22 May 2026 13:25:16 -0400 Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 64MGbHgx1734187 for ; Fri, 22 May 2026 17:25:10 GMT Received: from mail-vs1-f70.google.com (mail-vs1-f70.google.com [209.85.217.70]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4eattq86vu-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Fri, 22 May 2026 17:25:09 +0000 (GMT) Received: by mail-vs1-f70.google.com with SMTP id ada2fe7eead31-63144b84f14so11179496137.0 for ; Fri, 22 May 2026 10:25:09 -0700 (PDT) Received: from QCOM-UWl2o8bcGT.qualcomm.com ([191.202.238.222]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-67fda9015b0sm2118179137.4.2026.05.22.10.25.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 May 2026 10:25:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=L1QJuou29xu3g0tnX1YPz/ gvIfXQY74LadtdxGnVtec=; b=DhgPePR8kqzlC9yPUckKDX9n+84+PypDJTE7XQ 7yQqpoUgyJG4aPeWAFxcRHXYYAHad4HuGrJmaJ+dVpMmngk9ra/U4mjbElgxYbGm mEoitNGCmPTdFFuUit/gyTdifzZ0vXx2mkGlkbzzOx+1t1E1pzo3aJ4klji9C7Dg 05UI38Ax+5QghiLXHXNmz0FdyHO9nHzu0aOkdhCwxiqjETV3rMozJPKqhnvjfMUn pSzHWaQZ57IbIopHfL4HSA6lPIb62Np4i/+2R1bv/hDE5Q9gKway++fFq9HIN4LA NhdN3Bghq35nXPAvZJdaz/7Z/VhBS8mTkYparUVn2wdwTYfA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1779470709; x=1780075509; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=L1QJuou29xu3g0tnX1YPz/gvIfXQY74LadtdxGnVtec=; b=NO0O+GTEMfxy42Uvpw1OvB1ig7y/oFbO+AYv3U4HqvCqPC0BY6oSzzm1nl0l6XSSo+ 7AA1gsdzSkRbQP2c81LprhZ3bhDplGNx7SZfweVJO40gMXfRC69r7Dvdb36wBJ3St1RG hummaxmQpRKLoW7EruLmx5oJhB3QJsR+uRTN+l9EICAy/SwiGqACESBOj1YPQr2YsMo6 pfnl3/gAi1s+Blz5qUoU4Nt5tzmJtpi0f/zsj2qhb6CFQA5hN7FYsjSVD1AbSxjVgeTs Znz81EWY3CaVorkgJFLafdS4HS4BjLOXjeZADu+wPIaJG8Y9WlTK0+auNdbuy30yEK3E RWBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779470709; x=1780075509; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=L1QJuou29xu3g0tnX1YPz/gvIfXQY74LadtdxGnVtec=; b=YU3EKYTvC8xv2po4mPXmc4cfSkC90p6tNifRuKlYRR3WQz7aPko73IYSnBdUQs4YpK HE4YwHl1+K1ujYxvagmkT9GcFgB3fULRix+8C/1xj/gZn6OlltZp7RvPbbFCVTmvVFu7 DDQsoZMBVirXbn5CEtTtpJy184qjk5+DqzmmcpBwt+9aKumEkcEpfo7+pdB1bjHHuklK z+LynzBHUaT0lroH5CaeSSm7Z0RgBR+KjZC2tZ9JmrjvLwdrTjlk3WshrlKXppikLYgH 4XpjzdVD6FPEArnU1Fbc6xpKdQH2aXpTt4Z+CmjQ1dA3vKWHHMuFwB7iwOC+jT1dpXeX GQJw== X-Gm-Message-State: AOJu0Yx5Sa+6jT9gV/l+L09mP1WiYUGwvC/y176lyp6clSgpux7/PVZE G3IRZsY/lo+5YRYy6en7Cq4dpDFfEesZh7Bnjda/68CNY4t+F8dXbIWJFcAm9YoqzNJH+4zPVCx SK93G8wbdMFs/LatqXFOV3RVWm7cq2Iwh+RgUfrAcqUEHOvAB0YgQyahliAWB5Puv2Q== X-Gm-Gg: Acq92OGKjmXbx/GrTD8gy1iufqtdienzmOLZo9ZswNPwWlzdX21w8DT5l9KkYoRGEuk JQORasgsw7BXs/XP5X7rugUldhvXKbRdBApbvHpPc46A+sy01QjgqMhRQmRnzBbsLxDysciZBwp 9KS47kdfivsbCvaPJVITOkoqWOpMkBW5vIiMFPjzyQMUFxzVdpf0uYC3axUpeBD7P8KXGbh0SE0 GbNwoTo5MvWO6yL1Ney5dVKH/LBHWE/IwVxDleKKqIc6pqoslr0rAO/eq8OVmzuevp1Q/BMYgMb dFwhs1KxDSZyxP64Sf05xo7xCvn9iBlPdHihAefDUnARNDv195r3jXG1PSDU5y5y767ZDfS8iRL ZxV3iLbgw+WrB5B6Mq+50Au9pSuJNXnFTmIrl6agsoCxpBHf04X2DRY7dqlMrLJbnc7Ey6wk= X-Received: by 2002:a05:6102:5a89:b0:631:5ef5:8324 with SMTP id ada2fe7eead31-67c8d07131cmr2735284137.23.1779470709064; Fri, 22 May 2026 10:25:09 -0700 (PDT) X-Received: by 2002:a05:6102:5a89:b0:631:5ef5:8324 with SMTP id ada2fe7eead31-67c8d07131cmr2735260137.23.1779470708662; Fri, 22 May 2026 10:25:08 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, chao.liu.zevorn@gmail.com, Daniel Henrique Barboza Subject: [PATCH] target/riscv/cpu_helper.c: add PMA access fault Date: Fri, 22 May 2026 14:25:02 -0300 Message-ID: <20260522172502.320529-1-daniel.barboza@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTIyMDE3MyBTYWx0ZWRfXwaZWHjeRgGV9 yvMsazGOUe4idbf7IW4pgnhUJ0o7nauR2v/9a3ovTME+tmlDlzLtG61MU5MODhNpFuYCXXUP7mM YiF/6eDur7p3x9yj9oIiinxktRO270ozDNQYwogHSZ78riBH/HIjlea1NuxSwqga1Zu8Ywep7S/ hgovsaD89iHTklhkd93N7aRw3W3mUn74HKaUmKuDo5OBZwJXTmfiwAe80H+EI7qO0YeSJSScASW UoFVhV0fd4r30RnJqwZsnkQIt/vZh0y+NX7/sqsrzgzZxKgC/IefL1r9FWGQT9HUFOMW9GekblB wmeNSd4cJ5U2nWHGlz6Z8iRyzs9lUz7yN23wYCwmCao4ES5zMPkkgWFlURNEDC/fsrAlfDECoy7 //MxoUso6jhRNL4zJPV43nRXBgU00nYxUWPfo3jMp9mnVqZt1lzUveqS6qsyKjByg0LbfmTRvgX 78FcMvi+VWnbNwNMHSw== X-Proofpoint-GUID: mqSxSO50T1yhUx7MMJ404KgOp4-9m1TM X-Proofpoint-ORIG-GUID: mqSxSO50T1yhUx7MMJ404KgOp4-9m1TM X-Authority-Analysis: v=2.4 cv=S/zpBosP c=1 sm=1 tr=0 ts=6a109175 cx=c_pps a=N1BjEkVkxJi3uNfLdpvX3g==:117 a=etEqFfc6qwXwpI8uT8rzbw==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=_glEPmIy2e8OvE2BGh3C:22 a=p0WdMEafAAAA:8 a=EUspDBNiAAAA:8 a=KLl1PzbKQf_jOHGbhwkA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=crWF4MFLhNY0qMRaF8an:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-05-22_04,2026-05-18_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 phishscore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 adultscore=0 spamscore=0 priorityscore=1501 suspectscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2605130000 definitions=main-2605220173 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=lists1p.gnu.org; Received-SPF: pass client-ip=205.220.180.131; envelope-from=daniel.barboza@oss.qualcomm.com; helo=mx0b-0031df01.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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: qemu development 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 @qualcomm.com) X-ZM-MESSAGEID: 1779470777023154100 We're not doing anything special w.r.t PMA (Physical Memory Access) related faults, handling them like regular faults that will eventually turn to be regular page faults. Turns out we can't do that. Priv spec section "Virtual Address Translation Process" mentions: "If a store to the PTE at address a+va.vpn[i]=C3=97PTESIZE would violate a PMA or PMP check, raise an access-fault exception corresponding to the original access type." This means that we should handle PMA violations with access faults, like we're already doing with PMP. One clear code path where we should throw a PMA failure, exposed by [1], is the error return from address_space_ld* call. There's a separated issue with the error code being returned by them (it always return DECODE_ERROR even with 'rejected' reads) that we're going to work around it by assuming that we did a good job with the PTE address sanitization beforehand, and interpret that the error here is related to PMA. This is of course not ideal but fixing this QEMU API is out of scope for this work. All this said, we'll set the new pmp_pma_violation flag when we have either a PMP or a PMA fault, and everything else shall fall into place. [1] https://gitlab.com/qemu-project/qemu/-/work_items/3502 Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3502 Signed-off-by: Daniel Henrique Barboza Reviewed-by: Alistair Francis Reviewed-by: Chao Liu --- target/riscv/cpu.h | 3 ++- target/riscv/cpu_helper.c | 34 +++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 7d79c7a5a7..e5e1687177 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -131,7 +131,8 @@ enum { TRANSLATE_SUCCESS, TRANSLATE_FAIL, TRANSLATE_PMP_FAIL, - TRANSLATE_G_STAGE_FAIL + TRANSLATE_G_STAGE_FAIL, + TRANSLATE_PMA_FAIL, }; =20 /* Extension context status */ diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 17305e1bb7..5303d9f7f4 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1424,7 +1424,22 @@ static int get_physical_address(CPURISCVState *env, = hwaddr *physical, } =20 if (res !=3D MEMTX_OK) { - return TRANSLATE_FAIL; + /* + * The result of address_space_* APIs above does not take into + * consideration reject reads, putting all errors in the same + * cathegory (DECODE_ERROR), although there's a clear + * distinction between a rejected read versus other errors + * (see memory_region_dispatch_read() -> + * memory_region_access_valid()). This is something that + * we might have to deal with core QEMU logic some other + * day. + * + * For this particular error path, given that we made checks + * w.r.t legal PTE address before calling those APIs, we'll + * assume that anything !=3D MEMTX_OK means a rejected read, + * i.e. a PMA error. + */ + return TRANSLATE_PMA_FAIL; } =20 if (riscv_cpu_sxl(env) =3D=3D MXL_RV32) { @@ -1668,7 +1683,8 @@ static int get_physical_address(CPURISCVState *env, h= waddr *physical, } =20 static void raise_mmu_exception(CPURISCVState *env, target_ulong address, - MMUAccessType access_type, bool pmp_violat= ion, + MMUAccessType access_type, + bool pmp_pma_violation, bool first_stage, bool two_stage, bool two_stage_indirect) { @@ -1676,7 +1692,7 @@ static void raise_mmu_exception(CPURISCVState *env, t= arget_ulong address, =20 switch (access_type) { case MMU_INST_FETCH: - if (pmp_violation) { + if (pmp_pma_violation) { cs->exception_index =3D RISCV_EXCP_INST_ACCESS_FAULT; } else if (env->virt_enabled && !first_stage) { cs->exception_index =3D RISCV_EXCP_INST_GUEST_PAGE_FAULT; @@ -1685,7 +1701,7 @@ static void raise_mmu_exception(CPURISCVState *env, t= arget_ulong address, } break; case MMU_DATA_LOAD: - if (pmp_violation) { + if (pmp_pma_violation) { cs->exception_index =3D RISCV_EXCP_LOAD_ACCESS_FAULT; } else if (two_stage && !first_stage) { cs->exception_index =3D RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT; @@ -1694,7 +1710,7 @@ static void raise_mmu_exception(CPURISCVState *env, t= arget_ulong address, } break; case MMU_DATA_STORE: - if (pmp_violation) { + if (pmp_pma_violation) { cs->exception_index =3D RISCV_EXCP_STORE_AMO_ACCESS_FAULT; } else if (two_stage && !first_stage) { cs->exception_index =3D RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAUL= T; @@ -1820,7 +1836,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, vaddr im_address; hwaddr pa =3D 0; int prot, prot2, prot_pmp; - bool pmp_violation =3D false; + bool pmp_pma_violation =3D false; bool first_stage_error =3D true; bool two_stage_lookup =3D mmuidx_2stage(mmu_idx); bool two_stage_indirect_error =3D false; @@ -1921,8 +1937,8 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, } } =20 - if (ret =3D=3D TRANSLATE_PMP_FAIL) { - pmp_violation =3D true; + if (ret =3D=3D TRANSLATE_PMP_FAIL || ret =3D=3D TRANSLATE_PMA_FAIL) { + pmp_pma_violation =3D true; } =20 if (ret =3D=3D TRANSLATE_SUCCESS) { @@ -1949,7 +1965,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, cpu_check_watchpoint(cs, address, size, MEMTXATTRS_UNSPECIFIED, wp_access, retaddr); =20 - raise_mmu_exception(env, address, access_type, pmp_violation, + raise_mmu_exception(env, address, access_type, pmp_pma_violation, first_stage_error, two_stage_lookup, two_stage_indirect_error); cpu_loop_exit_restore(cs, retaddr); --=20 2.43.0