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 B3436C43458 for ; Wed, 8 Jul 2026 07:45:31 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whMwy-00077W-UW; Wed, 08 Jul 2026 03:44:18 -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 1whMwq-0006Ls-6e; Wed, 08 Jul 2026 03:44:01 -0400 Received: from mgamail.intel.com ([198.175.65.16]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whMwk-0004ox-GZ; Wed, 08 Jul 2026 03:43:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1783496635; x=1815032635; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s01A4TYlsdhYxVJa19FuPv1TolkBLDQE//62uZNCv2k=; b=kcHr4z06f0PSZmfVY8zZhg0hPu5BGe37LxsaVWttZdtmsTuAyzkVa5dQ TfzuuiUpog3w30QDiZwg6aaseVWYL/IG0CNsQlgDLflEx/d14P39TaFQT xR9uXKD3SsNHP268uHwtkS8QMJRihd7FFdw8xcWRFOulGra5VMX8539o/ 9fTmWYYwf1XSiaHXLorEkLM4KZLmtjmOnEBcm9qhXIDPvJrpsY0HhD1Yy 2TVxsz4tQbPGlc+GU5dkSjxfnGwo/h51svRm4+/m70vHGkKECSStdcdVT HWhlRcpkepk3fSlQaRo5zwkeehwDpMEAcfktH6+5tDfsoyb4+yjlvHfo7 w==; X-CSE-ConnectionGUID: pa/0SJaWR3eAD7Iu30gqzg== X-CSE-MsgGUID: e4I6852HSwaD6Img99OKOQ== X-IronPort-AV: E=McAfee;i="6800,10657,11840"; a="84343391" X-IronPort-AV: E=Sophos;i="6.25,153,1779174000"; d="scan'208";a="84343391" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2026 00:43:43 -0700 X-CSE-ConnectionGUID: RLOYSd36QCGfXERFrUfH2w== X-CSE-MsgGUID: JADoR++sSh+dQN9BKwDmxg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,153,1779174000"; d="scan'208";a="251556068" Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.39]) by fmviesa008.fm.intel.com with ESMTP; 08 Jul 2026 00:43:38 -0700 From: Zhao Liu To: Paolo Bonzini , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Manos Pitsidianakis , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Hanna Reitz , =?UTF-8?q?Alex=20Benn=C3=A9e?= , "Michael S . Tsirkin" , Pierrick Bouvier , Fabiano Rosas , Laurent Vivier Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org, Junjie Mao , Dapeng Mi , Chuanxiao Dong , Zhao Liu Subject: [RFC v2 19/26] rust/system/memory: Implement vm_memory::GuestMemory for FlatView Date: Wed, 8 Jul 2026 16:10:45 +0800 Message-Id: <20260708081052.2276617-20-zhao1.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260708081052.2276617-1-zhao1.liu@intel.com> References: <20260708081052.2276617-1-zhao1.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=198.175.65.16; envelope-from=zhao1.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -47 X-Spam_score: -4.8 X-Spam_bar: ---- X-Spam_report: (-4.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-rust@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: QEMU Rust-related patches and discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-rust-bounces+qemu-rust=archiver.kernel.org@nongnu.org Sender: qemu-rust-bounces+qemu-rust=archiver.kernel.org@nongnu.org Implement vm_memory::GuestMemory for FlatView, and provide memory write/read/store/load bindings. Note that the semantics of Bytes::read and Bytes::write in FlatView differ from those of the C functions: on the Rust side, read/write operations stop once an error occurs, while on the C side, `MemTxResult` errors are accumulated and continue accessing to subsequent memory regions. This is because the current Rust loop implementation (try_access_full()) simply returns an error and aborts the operation. To achieve the consistent semantics as the C side, it would be necessary to support accumulating MemTxResult. This can be addressed as a future enhancement. Signed-off-by: Zhao Liu --- Changes since v1: * Implement Bytes<(MemoryRegionAddress, MemTxAttrs)> to attach MemTxAttrs. * Move GuestMemoryBackend::try_access() to FlatView::try_access_full(). * Use vm-memory::Permissions to replace is_write flag. --- rust/system/src/memory.rs | 428 +++++++++++++++++++++++++++++++++++++- 1 file changed, 418 insertions(+), 10 deletions(-) diff --git a/rust/system/src/memory.rs b/rust/system/src/memory.rs index 34c7b7ce04a1..9772d1444dfb 100644 --- a/rust/system/src/memory.rs +++ b/rust/system/src/memory.rs @@ -2,8 +2,8 @@ // Author(s): Paolo Bonzini // SPDX-License-Identifier: GPL-2.0-or-later -//! Bindings for `MemoryRegion`, `MemoryRegionOps`, `MemTxAttrs` and -//! `MemoryRegionSection`. +//! Bindings for `MemoryRegion`, `MemoryRegionOps`, `MemTxAttrs` +//! `MemoryRegionSection` and `FlatView`. use std::{ ffi::{c_uint, c_void, CStr, CString}, @@ -18,15 +18,16 @@ use qom::prelude::*; pub use vm_memory::GuestAddress; use vm_memory::{ - bitmap::BS, Address, AtomicAccess, Bytes, GuestMemoryError, GuestMemoryRegion, - GuestMemoryRegionBytes, GuestMemoryResult, GuestUsize, MemoryRegionAddress, ReadVolatile, - WriteVolatile, + bitmap::BS, Address, AtomicAccess, Bytes, GuestMemoryBackend, GuestMemoryError, + GuestMemoryRegion, GuestMemoryRegionBytes, GuestMemoryResult, GuestUsize, MemoryRegionAddress, + Permissions, ReadVolatile, WriteVolatile, }; use crate::bindings::{ - self, device_endian, memory_region_init_io, rust_section_load, rust_section_read_continue_step, - rust_section_store, rust_section_write_continue_step, section_access_allowed, - section_covers_region_addr, section_fuzz_dma_read, MemTxResult, + self, address_space_lookup_section, device_endian, flatview_translate_section, + memory_region_init_io, rust_section_load, rust_section_read_continue_step, rust_section_store, + rust_section_write_continue_step, section_access_allowed, section_covers_region_addr, + section_fuzz_dma_read, MemTxResult, }; // FIXME: Convert hwaddr to GuestAddress pub use crate::bindings::{hwaddr, MemTxAttrs}; @@ -260,7 +261,6 @@ impl MemoryRegionSection { /// A fuzz testing hook for DMA read. /// /// When `CONFIG_FUZZ` is not set, this hook will do nothing. - #[allow(dead_code)] fn fuzz_dma_read(&self, addr: GuestAddress, len: GuestUsize) -> &Self { // SAFETY: Opaque<> ensures the pointer is valid, and here it // takes into account the offset conversion between MemoryRegionSection @@ -280,7 +280,6 @@ fn fuzz_dma_read(&self, addr: GuestAddress, len: GuestUsize) -> &Self { /// A helper to check if the memory access is allowed. /// /// This is needed for memory write/read. - #[allow(dead_code)] fn is_access_allowed( &self, addr: MemoryRegionAddress, @@ -632,3 +631,412 @@ fn check_address(&self, addr: MemoryRegionAddress) -> Option