From nobody Sun Dec 14 05:52:16 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=1754568860; cv=none; d=zohomail.com; s=zohoarc; b=anQilF+BPVAVy6MWa5TivwmI1OiKaZMQQlZfq6QhFXqk0s5Pcoo7P6zC0B3hd9zSWiRto/S1gOW2rCt2ErnVEm5C7fcrH9zijFGWIsrZ/q0WhvEOZPHXAfFW6CDVuZqPflqdJoiiO6VPY/zhqrdo2fhauy9h7J31JO3Bx9nXBN0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1754568860; 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=cupwHwBPO3x9nWNTjYB8iFvEE8UWoHPXtG5zzvNdpDw=; b=YpvJKAZue6xJ0lf+Zwp+cT/IEWhBvxkzqxknhApFqUZgOoih8pjxZaC8wH6tk3cfiI5r3DdeTjm0vG3e43IbZVQNw7GeuzKucHNKK5o5pO84869P6mHA8UYcKsTQe0HNchRVGhBBnIWqz1W/ANCklEF/waqMIPK9fUUvUuRbDGI= 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 1754568860267554.5375419571727; Thu, 7 Aug 2025 05:14:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ujzTD-0004yN-RC; Thu, 07 Aug 2025 08:11:49 -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 1ujzSP-0004ES-46; Thu, 07 Aug 2025 08:10:53 -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 1ujzSN-0005Cn-0B; Thu, 07 Aug 2025 08:10:52 -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:26 -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:24 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1754568651; x=1786104651; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YO4ppsApMppmvSjO1TZkcO3ACCoVl7NKzejbdkLlymk=; b=BNggH4HZLYdV3gCOHmA1ZNCcsU0gb1IFJDWqggeMv96IOfJMwYvc25Kx V3fr/mx8MT5ZVQ75CeQ/pF4y1Z/CuNQ8vcU88FOJNwVt4rH7DHhyCIeGz RFamCEGxlH8AvmAO7RF13VbowBKbOYN+aY8wVPJum5amBwNJsKDQpn4gU EQxQGSWuQk/rz8K8JXfxEtQJ7bK+pMjE1283JdOb6p0S9LVaWtVb3J37+ 6J0BGj1OOpIXgz3rfQ67UbmVXeEAdY0qL7ugNqG/jaRPyuM0/15vGEr6C JQ6z57gKoH7AEzi469RqU6zu7aZFFgI67giAXjfns8iYRNwIF/uOqDGCE g==; X-CSE-ConnectionGUID: /QATM6S7Re2vBUZh1vZbpw== X-CSE-MsgGUID: rTO61MxoTBmfFxb3/Ia+tA== X-IronPort-AV: E=McAfee;i="6800,10657,11514"; a="57036906" X-IronPort-AV: E=Sophos;i="6.17,271,1747724400"; d="scan'208";a="57036906" X-CSE-ConnectionGUID: QnB0AqLoTyOiW+nlg5knlw== X-CSE-MsgGUID: SGplsYhWTzKtdQ0BoEubNA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.17,271,1747724400"; d="scan'208";a="164701368" 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 25/26] rust/memory: Add binding to check target endian Date: Thu, 7 Aug 2025 20:30:26 +0800 Message-Id: <20250807123027.2910950-26-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: 1754568862321124100 Content-Type: text/plain; charset="utf-8" Add a binding (target_is_big_endian()) to check whether target is big endian or not. This could help user to adjust endian before calling AddresssSpace::store() or after calling AddressSpace::load(). Add the example in the documentation of AddresssSpace::store() to help explain how to use it. Signed-off-by: Zhao Liu Reviewed-by: Manos Pitsidianakis --- rust/qemu-api/src/memory.rs | 28 +++++++++++++++++++++++++--- rust/qemu-api/wrapper.h | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/rust/qemu-api/src/memory.rs b/rust/qemu-api/src/memory.rs index 42bba23cf3f8..a8eb83c95ead 100644 --- a/rust/qemu-api/src/memory.rs +++ b/rust/qemu-api/src/memory.rs @@ -31,7 +31,7 @@ memory_region_init_io, section_access_allowed, section_covers_regi= on_addr, section_fuzz_dma_read, section_get_host_addr, section_rust_load, section_rust_read_continue_step, section_rust_store, section_rust_= write_continue_step, - MEMTX_OK, + target_big_endian, MEMTX_OK, }, callbacks::FnCall, cell::Opaque, @@ -1107,9 +1107,25 @@ pub fn read(&self, buf: &mut [u8], addr: GuestAddres= s) -> Result { /// This function is similar to `address_space_st{size}` in C side. /// /// But it only assumes @val follows target-endian by default. So ensu= re - /// the endian of `val` aligned with target, before using this method. + /// the endian of `val` aligned with target, before using this method.= The + /// taget-endian can be checked with [`target_is_big_endian`]. /// /// And it assumes the memory attributes is MEMTXATTRS_UNSPECIFIED. + /// + /// # Examples + /// + /// ``` + /// use qemu_api::memory::{ADDRESS_SPACE_MEMORY, target_is_big_endian}; + /// + /// let addr =3D GuestAddress(0x123438000); + /// let val: u32 =3D 5; + /// let val_end =3D if target_is_big_endian() { + /// val.to_be() + /// } else { + /// val.to_le() + /// } + /// + /// assert!(ADDRESS_SPACE_MEMORY.store(addr, val_end).is_ok()); pub fn store(&self, addr: GuestAddress, val: T) -> Re= sult<()> { rcu_read_lock(); let r =3D self.memory().deref().store(val, addr, Ordering::Relaxed= ); @@ -1122,7 +1138,8 @@ pub fn store(&self, addr: GuestAddre= ss, val: T) -> Result<()> { /// This function is similar to `address_space_ld{size}` in C side. /// /// But it only support target-endian by default. The returned value = is - /// with target-endian. + /// with target-endian. The taget-endian can be checked with + /// [`target_is_big_endian`]. /// /// And it assumes the memory attributes is MEMTXATTRS_UNSPECIFIED. pub fn load(&self, addr: GuestAddress) -> Result { @@ -1147,3 +1164,8 @@ pub fn load(&self, addr: GuestAddres= s) -> Result { // the whole QEMU life. &*wrapper_ptr }; + +pub fn target_is_big_endian() -> bool { + // SAFETY: the return value is boolean, so it is always valid. + unsafe { target_big_endian() } +} diff --git a/rust/qemu-api/wrapper.h b/rust/qemu-api/wrapper.h index ce0ac8d3f550..c466b93054aa 100644 --- a/rust/qemu-api/wrapper.h +++ b/rust/qemu-api/wrapper.h @@ -70,3 +70,4 @@ typedef enum memory_order { #include "system/address-spaces.h" #include "hw/char/pl011.h" #include "qemu/rcu.h" +#include "qemu/target-info.h" --=20 2.34.1