From nobody Mon Nov 25 03:23:53 2024 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; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1718182922; cv=none; d=zohomail.com; s=zohoarc; b=ZihcykToT1d4zfU3n/5rSu+Hf2X7XMsXMhl4ZBb4IqXLz46L7aUCooVOy3lHlc29riqSLnBeEIJ8jr7maPWdnjth6zyEolTbnIEcqVQ1BrjZgAXSWSF4GnqzoPlCer5TByHIgp7FCln/ACaIHfDy/AvygktjjrrKmbj6ldQUeoQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1718182922; 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:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=7y50lkfIEDncyr4cxt6zgChWJEg1WH3MlanODOCeRiE=; b=aFq7jYSFl/pgGaJCBB8STfQi6O5wWjWSnbDk+Z30zdHC0jS15ClEuBwn++YoR+2W8NdVft48ZLQVjoWr3bbBOhTKwBheEBRnbQGbsv9pdnvJF9yZY7cg3+f1+t8/+P6/x+TN80ZJLZSLghx1pSrnUFskOJCoIT5U4+RjRR78Pzo= ARC-Authentication-Results: i=1; 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; 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 1718182921992659.8881333427149; Wed, 12 Jun 2024 02:02:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sHJrb-0001P6-Tn; Wed, 12 Jun 2024 05:01: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 1sHJrZ-0001OL-CJ for qemu-devel@nongnu.org; Wed, 12 Jun 2024 05:01:49 -0400 Received: from 59-120-53-16.hinet-ip.hinet.net ([59.120.53.16] helo=mail.andestech.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sHJrX-0006Rp-0g for qemu-devel@nongnu.org; Wed, 12 Jun 2024 05:01:49 -0400 Received: from atcpcw16.andestech.com (10.0.1.106) by ATCPCS34.andestech.com (10.0.1.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.7; Wed, 12 Jun 2024 17:01:39 +0800 To: CC: , , , , , Ethan Chen Subject: [RFC PATCH] memory: Introduce memory region fetch operation Date: Wed, 12 Jun 2024 17:01:32 +0800 Message-ID: <20240612090132.3522566-1-ethan84@andestech.com> X-Mailer: git-send-email 2.42.0.345.gaab89be2eb.dirty MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.0.1.106] X-ClientProxiedBy: ATCPCS33.andestech.com (10.0.1.100) To ATCPCS34.andestech.com (10.0.1.134) 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=59.120.53.16; envelope-from=ethan84@andestech.com; helo=mail.andestech.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, RDNS_DYNAMIC=0.982, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TVD_RCVD_IP=0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Reply-to: Ethan Chen From: Ethan Chen via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1718182923872100002 Content-Type: text/plain; charset="utf-8" Allow the memory region to have different behaviors for read and fetch operations. For example RISCV IOPMP will raise interrupt when cpu try to fetch a non-excutable region. If fetch operation of a memory region is not implemented, it still uses the read operation for fetch. Signed-off-by: Ethan Chen --- accel/tcg/cputlb.c | 9 +++- include/exec/memory.h | 30 ++++++++++++ system/memory.c | 104 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 2 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index 117b516739..edb3715017 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -1942,8 +1942,13 @@ static uint64_t int_ld_mmio_beN(CPUState *cpu, CPUTL= BEntryFull *full, this_size =3D 1 << this_mop; this_mop |=3D MO_BE; =20 - r =3D memory_region_dispatch_read(mr, mr_offset, &val, - this_mop, full->attrs); + if (type =3D=3D MMU_INST_FETCH) { + r =3D memory_region_dispatch_fetch(mr, mr_offset, &val, + this_mop, full->attrs); + } else { + r =3D memory_region_dispatch_read(mr, mr_offset, &val, + this_mop, full->attrs); + } if (unlikely(r !=3D MEMTX_OK)) { io_failed(cpu, full, addr, this_size, type, mmu_idx, r, ra); } diff --git a/include/exec/memory.h b/include/exec/memory.h index 1be58f694c..6d61ec443e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -274,6 +274,13 @@ struct MemoryRegionOps { uint64_t data, unsigned size); =20 + /* Fetch to the memory region. @addr is relative to @mr; @size is + * in bytes. */ + uint64_t (*fetch)(void *opaque, + hwaddr addr, + unsigned size); + + MemTxResult (*read_with_attrs)(void *opaque, hwaddr addr, uint64_t *data, @@ -284,6 +291,12 @@ struct MemoryRegionOps { uint64_t data, unsigned size, MemTxAttrs attrs); + MemTxResult (*fetch_with_attrs)(void *opaque, + hwaddr addr, + uint64_t *data, + unsigned size, + MemTxAttrs attrs); + =20 enum device_endian endianness; /* Guest-visible constraints: */ @@ -2672,6 +2685,23 @@ MemTxResult memory_region_dispatch_write(MemoryRegio= n *mr, MemOp op, MemTxAttrs attrs); =20 + +/** + * memory_region_dispatch_fetch: perform a fetch directly to the specified + * MemoryRegion. + * + * @mr: #MemoryRegion to access + * @addr: address within that region + * @pval: pointer to uint64_t which the data is written to + * @op: size, sign, and endianness of the memory operation + * @attrs: memory transaction attributes to use for the access + */ +MemTxResult memory_region_dispatch_fetch(MemoryRegion *mr, + hwaddr addr, + uint64_t *pval, + MemOp op, + MemTxAttrs attrs); + /** * address_space_init: initializes an address space * diff --git a/system/memory.c b/system/memory.c index 74cd73ebc7..ecd3020b1c 100644 --- a/system/memory.c +++ b/system/memory.c @@ -477,6 +477,51 @@ static MemTxResult memory_region_read_with_attrs_acces= sor(MemoryRegion *mr, return r; } =20 +static MemTxResult memory_region_fetch_accessor(MemoryRegion *mr, + hwaddr addr, + uint64_t *value, + unsigned size, + signed shift, + uint64_t mask, + MemTxAttrs attrs) +{ + uint64_t tmp; + + tmp =3D mr->ops->fetch(mr->opaque, addr, size); + if (mr->subpage) { + trace_memory_region_subpage_fetch(get_cpu_index(), mr, addr, tmp, = size); + } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_FETC= H)) { + hwaddr abs_addr =3D memory_region_to_absolute_addr(mr, addr); + trace_memory_region_ops_fetch(get_cpu_index(), mr, abs_addr, tmp, = size, + memory_region_name(mr)); + } + memory_region_shift_read_access(value, shift, mask, tmp); + return MEMTX_OK; +} + +static MemTxResult memory_region_fetch_with_attrs_accessor(MemoryRegion *m= r, + hwaddr addr, + uint64_t *value, + unsigned size, + signed shift, + uint64_t mask, + MemTxAttrs attrs) +{ + uint64_t tmp =3D 0; + MemTxResult r; + + r =3D mr->ops->fetch_with_attrs(mr->opaque, addr, &tmp, size, attrs); + if (mr->subpage) { + trace_memory_region_subpage_fetch(get_cpu_index(), mr, addr, tmp, = size); + } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_FETC= H)) { + hwaddr abs_addr =3D memory_region_to_absolute_addr(mr, addr); + trace_memory_region_ops_fetch(get_cpu_index(), mr, abs_addr, tmp, = size, + memory_region_name(mr)); + } + memory_region_shift_read_access(value, shift, mask, tmp); + return r; +} + static MemTxResult memory_region_write_accessor(MemoryRegion *mr, hwaddr addr, uint64_t *value, @@ -1461,6 +1506,65 @@ MemTxResult memory_region_dispatch_read(MemoryRegion= *mr, return r; } =20 +static MemTxResult memory_region_dispatch_fetch1(MemoryRegion *mr, + hwaddr addr, + uint64_t *pval, + unsigned size, + MemTxAttrs attrs) +{ + *pval =3D 0; + + if (mr->ops->fetch) { + return access_with_adjusted_size(addr, pval, size, + mr->ops->impl.min_access_size, + mr->ops->impl.max_access_size, + memory_region_fetch_accessor, + mr, attrs); + } else if (mr->ops->fetch_with_attrs) { + return access_with_adjusted_size(addr, pval, size, + mr->ops->impl.min_access_size, + mr->ops->impl.max_access_size, + memory_region_fetch_with_attrs_accessor, + mr, attrs); + } else if (mr->ops->read) { + return access_with_adjusted_size(addr, pval, size, + mr->ops->impl.min_access_size, + mr->ops->impl.max_access_size, + memory_region_read_accessor, + mr, attrs); + } else { + return access_with_adjusted_size(addr, pval, size, + mr->ops->impl.min_access_size, + mr->ops->impl.max_access_size, + memory_region_read_with_attrs_acc= essor, + mr, attrs); + } +} + +MemTxResult memory_region_dispatch_fetch(MemoryRegion *mr, + hwaddr addr, + uint64_t *pval, + MemOp op, + MemTxAttrs attrs) +{ + unsigned size =3D memop_size(op); + MemTxResult r; + + if (mr->alias) { + return memory_region_dispatch_fetch(mr->alias, + mr->alias_offset + addr, + pval, op, attrs); + } + if (!memory_region_access_valid(mr, addr, size, false, attrs)) { + *pval =3D unassigned_mem_read(mr, addr, size); + return MEMTX_DECODE_ERROR; + } + + r =3D memory_region_dispatch_fetch1(mr, addr, pval, size, attrs); + adjust_endianness(mr, pval, op); + return r; +} + /* Return true if an eventfd was signalled */ static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr, hwaddr addr, --=20 2.34.1