[RFC v2 24/26] rust/hpet: Use safe binding to access address space

Zhao Liu <[email protected]> Wed, 8 Jul 2026 16:10:50 +0800
Newsgroups org.nongnu.qemu-rust,org.nongnu.qemu-devel
Message-ID <[email protected]>
Currently, HPET uses unsafe address_space_stl_le() to store MSI message.

Therefore, use the safe binding - AddressSpace::store() with Le32, to
access address space.

Since then, the last unsafe piece of HPET has been filled in.

Signed-off-by: Zhao Liu <[email protected]>
---
Changes since v1:
 * Explicitly specify endianness by Le32 and get rid of native
   endianness.
 * Drop expect() method to avoid abort() on error - consistent with the
   behavior on the C side.
---
 rust/hw/timer/hpet/src/device.rs | 29 ++++++-----------------------
 1 file changed, 6 insertions(+), 23 deletions(-)

diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 7c60cc0f10ab..e3a0fa25b14b 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -2,24 +2,14 @@
 // Author(s): Zhao Liu <[email protected]>
 // SPDX-License-Identifier: GPL-2.0-or-later
 
-use std::{
-    ffi::CStr,
-    mem::MaybeUninit,
-    pin::Pin,
-    ptr::{addr_of_mut, null_mut, NonNull},
-    slice::from_ref,
-};
+use std::{ffi::CStr, mem::MaybeUninit, pin::Pin, ptr::NonNull, slice::from_ref};
 
 use bql::prelude::*;
 use common::prelude::*;
 use hwcore::prelude::*;
 use migration::{self, prelude::*, ToMigrationStateShared};
 use qom::prelude::*;
-use system::{
-    bindings::{address_space_memory, address_space_stl_le},
-    prelude::*,
-    MEMTXATTRS_UNSPECIFIED,
-};
+use system::{prelude::*, GuestAddress, Le32, ADDRESS_SPACE_MEMORY};
 use util::prelude::*;
 
 use crate::fw_cfg::HPETFwConfig;
@@ -327,17 +317,10 @@ fn set_irq(&self, regs: &HPETRegisters, set: bool) {
 
         if set && tn_regs.is_int_enabled() && regs.is_hpet_enabled() {
             if tn_regs.is_fsb_route_enabled() {
-                // SAFETY:
-                // the parameters are valid.
-                unsafe {
-                    address_space_stl_le(
-                        addr_of_mut!(address_space_memory),
-                        tn_regs.fsb >> 32,  // Timer N FSB int addr
-                        tn_regs.fsb as u32, // Timer N FSB int value, truncate!
-                        MEMTXATTRS_UNSPECIFIED,
-                        null_mut(),
-                    );
-                }
+                let _ = ADDRESS_SPACE_MEMORY.store(
+                    GuestAddress(tn_regs.fsb >> 32), // Timer N FSB int addr
+                    Le32::from(tn_regs.fsb as u32),  // Timer N FSB int value, truncate!
+                );
             } else if tn_regs.is_int_level_triggered() {
                 self.get_state().irqs[route].raise();
             } else {
-- 
2.34.1