From nobody Sun Dec 14 05:52:14 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1754568804; cv=none; d=zohomail.com; s=zohoarc; b=GZrKm3eyjolNORKxcOb/fjEHqEwIDtIfk0lYf4idcIASSV6pA+kyhETbTpXyPd/co5cS/wxZ0vGHUtqnTzhv+xZi6wFdx3WJ7tj+A09vVHGyaduCg4RlgfNm9v3b2uGwrCMCp64lwFh8pp2M1W8dnCOnVhwjQ26IRw2/CzojI+c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1754568804; 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=WjDRxZ45m4vh1E95zZvJx3O1FHWeDsYKIQmLIwpZFNw=; b=coLK5Cv+TgJm+3vfaN0MJ1pqVBcWhCYZvLFIIQRcqp/e//U/s3ihAaw6SD+cnhFpXyK4zzopdRw/jeSyEVs6UH1qlToykyb5FKt6/yn85deTfiru1/t3v9R6KLdCRVWY4LEjilpCC27XRzHaxijSQd69iPdT9fwg5kAOA4SLB+A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.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 1754568804307177.4548215577795; Thu, 7 Aug 2025 05:13:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ujzSA-0003DI-1L; Thu, 07 Aug 2025 08:10: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 1ujzRy-0002HO-CL; Thu, 07 Aug 2025 08:10:26 -0400 Received: from mgamail.intel.com ([192.198.163.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ujzRv-0005Bp-6r; Thu, 07 Aug 2025 08:10:26 -0400 Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2025 05:10:05 -0700 Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.39]) by orviesa009.jf.intel.com with ESMTP; 07 Aug 2025 05:10:02 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1754568623; x=1786104623; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zfMJb0g3wXJyLqYM/+6g21n/rbDXZL8PB4EtRlUBfCI=; b=cjrPpsSq8SoJRt3DOpM7dreupnJUpeYWsnNXS1kI4oAwv8jf6sfAL55g nIjM0HKWmie6nQwm6qE2BLaxFXlGwJ97k6dlgAEtfTNtyJlpwtqVhKhpK ykVRkmED5Rt+J3VNzz+FwxdPVCj7gWMZpE0YMoZUsLQyAQogQJhDjg2vK DiY72oMO0i59sVUXQ1Yqd9bdNAAHvPuWY85z0vDcfn5fMIlesd81BqNq9 ZZNgLx0yoZVkyY7nRqX9kBNMqESNxf5ymHVjBAblZY/wLmSjfOq68Kbgk nkhSSk75B0En7vEGQGVGgZzb1Wxy/6fU5XT8cle8KuJoSNQ6sMnTZ4vK9 g==; X-CSE-ConnectionGUID: wo9g8YbnSM6/F6wDmYGlWg== X-CSE-MsgGUID: GnXvD7CpRvyh78dCgjPo5A== X-IronPort-AV: E=McAfee;i="6800,10657,11514"; a="57036846" X-IronPort-AV: E=Sophos;i="6.17,271,1747724400"; d="scan'208";a="57036846" X-CSE-ConnectionGUID: CufdCPrmTwSW55ApQmSRqA== X-CSE-MsgGUID: RMGy/1g2TpKZrhebzThF1g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.17,271,1747724400"; d="scan'208";a="164701168" From: Zhao Liu To: Paolo Bonzini , Peter Xu , David Hildenbrand , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Manos Pitsidianakis , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Thomas Huth , Junjie Mao Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org, Dapeng Mi , Chuanxiao Dong , Zhao Liu Subject: [RFC 19/26] memory: Add MemoryRegionSection based misc helpers Date: Thu, 7 Aug 2025 20:30:20 +0800 Message-Id: <20250807123027.2910950-20-zhao1.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250807123027.2910950-1-zhao1.liu@intel.com> References: <20250807123027.2910950-1-zhao1.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=192.198.163.15; envelope-from=zhao1.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1754568806172116600 Content-Type: text/plain; charset="utf-8" Add the following helpers: * section_access_allowed() - used to check access in GuestMemory::try_access(). * section_covers_region_addr() - used to implement GuestMemoryRegion::check_address(). * section_get_host_addr() - used to implement GuestMemoryRegion::get_host_address(). * section_fuzz_dma_read() - used to insert fuzz hook before read/load. Signed-off-by: Zhao Liu --- include/system/memory.h | 56 +++++++++++++++++++++++++++++++++++++++++ system/physmem.c | 30 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/include/system/memory.h b/include/system/memory.h index eab69e15e10f..110ad0a3b590 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -3357,6 +3357,62 @@ address_space_write_cached(MemoryRegionCache *cache,= hwaddr addr, MemTxResult address_space_set(AddressSpace *as, hwaddr addr, uint8_t c, hwaddr len, MemTxAttrs attrs); =20 +/** + * section_access_allowed + * + * @section: #MemoryRegionSection to be accessed. + * @attrs: memory transaction attributes. + * @addr: address within that memory region. + * @len: the number of bytes to access. + * + * Check if a memory transaction is allowed. + * + * Returns: true if transaction is allowed, false if denied. + */ +bool section_access_allowed(MemoryRegionSection *section, + MemTxAttrs attrs, hwaddr addr, + hwaddr len); + +/** + * section_covers_region_addr + * + * @section: #MemoryRegionSection to be accessed. + * @region_addr: memory region address within the region, which is + * pointed by #MemoryRegionSection. + * + * Check if a region address is coverd by #MemoryRegionSection. + * + * Returns: true if transaction is allowed, false if denied. + */ +bool section_covers_region_addr(const MemoryRegionSection *section, + hwaddr region_addr); + +/** + * section_get_host_addr + * + * @section: #MemoryRegionSection to be accessed. + * @region_addr: memory region address within the region, which is + * pointed by #MemoryRegionSection. + * + * Get the pointer to the host address. + * + * Returns: pointer to the host address. + */ +uint8_t *section_get_host_addr(const MemoryRegionSection *section, + hwaddr region_addr); + +/** + * section_fuzz_dma_read + * + * @section: #MemoryRegionSection to be accessed. + * @addr: memory address to be fuzzed. + * @len: length of the memory + * + * This function is wrapper of fuzz_dma_read_cb(). + */ +void section_fuzz_dma_read(MemoryRegionSection *section, + hwaddr addr, hwaddr len); + /* * Inhibit technologies that require discarding of pages in RAM blocks, e.= g., * to manage the actual amount of memory consumed by the VM (then, the mem= ory diff --git a/system/physmem.c b/system/physmem.c index 8aaaab4d3a74..e06633f4d8a2 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -320,6 +320,29 @@ static inline bool section_covers_addr(const MemoryReg= ionSection *section, int128_getlo(section->size), addr); } =20 +bool section_covers_region_addr(const MemoryRegionSection *section, + hwaddr region_addr) +{ + return section->offset_within_region <=3D region_addr && + section->offset_within_region + int128_get64(section->size) >= =3D region_addr; +} + +uint8_t *section_get_host_addr(const MemoryRegionSection *section, + hwaddr region_addr) +{ + MemoryRegion *mr =3D section->mr; + assert(mr && mr->ram_block); + + return qemu_map_ram_ptr(mr->ram_block, + section->offset_within_region + region_addr); +} + +void section_fuzz_dma_read(MemoryRegionSection *section, + hwaddr addr, hwaddr len) +{ + fuzz_dma_read_cb(addr, len, section->mr); +} + static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr= addr) { PhysPageEntry lp =3D d->phys_map, *p; @@ -2947,6 +2970,13 @@ static bool memory_region_access_allowed(MemoryRegio= n *mr, MemTxAttrs attrs, return false; } =20 +bool section_access_allowed(MemoryRegionSection *section, + MemTxAttrs attrs, hwaddr addr, + hwaddr len) +{ + return memory_region_access_allowed(section->mr, attrs, addr, len); +} + static MemTxResult flatview_write_continue_step(MemTxAttrs attrs, const uint8_t *buf, hwaddr len, hwaddr mr_addr, --=20 2.34.1