qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [RFC 1/5] rust: hw: core: Add the BusState of rust version
@ 2025-10-25 12:38 chenmiao
  2025-10-27  6:23 ` Zhao Liu
  0 siblings, 1 reply; 4+ messages in thread
From: chenmiao @ 2025-10-25 12:38 UTC (permalink / raw)
  To: pbonzini, manos.pitsidianakis, richard.henderson, philmd
  Cc: qemu-rust, qemu-devel, hust-os-kernel-patches

A Rust version implementation has been designed for BusState,
which will be used for the subsequent I2CBus implementation.

Signed-off-by: chenmiao <chenmiao@openatom.club>
---
 rust/hw/core/meson.build |  1 +
 rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
 rust/hw/core/src/lib.rs  |  3 +++
 3 files changed, 55 insertions(+)
 create mode 100644 rust/hw/core/src/bus.rs

diff --git a/rust/hw/core/meson.build b/rust/hw/core/meson.build
index 1560dd2..efcda50 100644
--- a/rust/hw/core/meson.build
+++ b/rust/hw/core/meson.build
@@ -50,6 +50,7 @@ _hwcore_rs = static_library(
     [
       'src/lib.rs',
       'src/bindings.rs',
+      'src/bus.rs',
       'src/irq.rs',
       'src/qdev.rs',
       'src/sysbus.rs',
diff --git a/rust/hw/core/src/bus.rs b/rust/hw/core/src/bus.rs
new file mode 100644
index 0000000..c87d18d
--- /dev/null
+++ b/rust/hw/core/src/bus.rs
@@ -0,0 +1,51 @@
+// Copyright 2025 HUST OpenAtom Open Source Club.
+// Author(s): Chen Miao <chenmiao@openatom.club>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+use std::{ffi::CStr, ptr::addr_of_mut};
+
+pub use bindings::BusClass;
+use common::Opaque;
+use qom::{qom_isa, IsA, Object, ObjectCast, ObjectDeref, ObjectType};
+
+use crate::{bindings, DeviceImpl};
+
+#[repr(transparent)]
+#[derive(Debug, common::Wrapper)]
+pub struct BusState(Opaque<bindings::BusState>);
+
+unsafe impl Send for BusState {}
+unsafe impl Sync for BusState {}
+
+unsafe impl ObjectType for BusState {
+    type Class = BusClass;
+    const TYPE_NAME: &'static std::ffi::CStr =
+        unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_BUS) };
+}
+
+qom_isa!(BusState: Object);
+
+pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
+
+impl BusClass {
+    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
+        self.parent_class.class_init::<T>();
+    }
+}
+
+pub trait BusStateMethods: ObjectDeref
+where
+    Self::Target: IsA<BusState>,
+{
+    fn bus_realize(&self) {
+        assert!(bql::is_locked());
+        unsafe {
+            bindings::qbus_realize(
+                self.upcast().as_mut_ptr(),
+                addr_of_mut!(util::bindings::error_fatal),
+            );
+        }
+    }
+}
+
+impl<R: ObjectDeref> BusStateMethods for R where R::Target: IsA<BusState> {}
diff --git a/rust/hw/core/src/lib.rs b/rust/hw/core/src/lib.rs
index b40801e..10cc516 100644
--- a/rust/hw/core/src/lib.rs
+++ b/rust/hw/core/src/lib.rs
@@ -13,3 +13,6 @@
 
 mod sysbus;
 pub use sysbus::*;
+
+mod bus;
+pub use bus::*;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC 1/5] rust: hw: core: Add the BusState of rust version
  2025-10-27  6:23 ` Zhao Liu
@ 2025-10-27  6:13   ` Chen Miao
  2025-10-27  6:51   ` Chao Liu
  1 sibling, 0 replies; 4+ messages in thread
From: Chen Miao @ 2025-10-27  6:13 UTC (permalink / raw)
  To: Zhao Liu
  Cc: pbonzini, manos.pitsidianakis, richard.henderson, philmd,
	qemu-rust, qemu-devel, hust-os-kernel-patches

On 10/27/2025 2:23 PM, Zhao Liu wrote:
> On Sat, Oct 25, 2025 at 12:38:49PM +0000, chenmiao wrote:
>> Date: Sat, 25 Oct 2025 12:38:49 +0000
>> From: chenmiao <chenmiao@openatom.club>
>> Subject: [RFC 1/5] rust: hw: core: Add the BusState of rust version
>> X-Mailer: git-send-email 2.43.0
>>
>> A Rust version implementation has been designed for BusState,
>> which will be used for the subsequent I2CBus implementation.
>>
>> Signed-off-by: chenmiao <chenmiao@openatom.club>
>> ---
>>   rust/hw/core/meson.build |  1 +
>>   rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
>>   rust/hw/core/src/lib.rs  |  3 +++
>>   3 files changed, 55 insertions(+)
>>   create mode 100644 rust/hw/core/src/bus.rs
> After a quick glance, I think this BusState is implemented quite well.
>
> Only a few minor nits inline:
>
> ...
>
>> +pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
>> +
>> +impl BusClass {
>> +    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
>> +        self.parent_class.class_init::<T>();
>> +    }
>> +}
>> +
>> +pub trait BusStateMethods: ObjectDeref
> This can be named BusMethods - just like DeviceMethods did.
>
>> +where
>> +    Self::Target: IsA<BusState>,
>> +{
>> +    fn bus_realize(&self) {
> "bus_" prefix is not needed:
>
> dummy_bus.realize() is clear and enough.
>
>> +        assert!(bql::is_locked());
> It's better to add safety comment from beginning (// SAFETY: xxx).
>
>> +        unsafe {
>> +            bindings::qbus_realize(
>> +                self.upcast().as_mut_ptr(),
>> +                addr_of_mut!(util::bindings::error_fatal),
>> +            );
>> +        }
>> +    }
>> +}
> Regards,
> Zhao

Hi Zhao,

Thanks for your review, I'll revisions based on your suggestions after all the 
patches reviewed.

Regards,

Chen Miao


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC 1/5] rust: hw: core: Add the BusState of rust version
  2025-10-25 12:38 [RFC 1/5] rust: hw: core: Add the BusState of rust version chenmiao
@ 2025-10-27  6:23 ` Zhao Liu
  2025-10-27  6:13   ` Chen Miao
  2025-10-27  6:51   ` Chao Liu
  0 siblings, 2 replies; 4+ messages in thread
From: Zhao Liu @ 2025-10-27  6:23 UTC (permalink / raw)
  To: chenmiao
  Cc: pbonzini, manos.pitsidianakis, richard.henderson, philmd,
	qemu-rust, qemu-devel, hust-os-kernel-patches

On Sat, Oct 25, 2025 at 12:38:49PM +0000, chenmiao wrote:
> Date: Sat, 25 Oct 2025 12:38:49 +0000
> From: chenmiao <chenmiao@openatom.club>
> Subject: [RFC 1/5] rust: hw: core: Add the BusState of rust version
> X-Mailer: git-send-email 2.43.0
> 
> A Rust version implementation has been designed for BusState,
> which will be used for the subsequent I2CBus implementation.
> 
> Signed-off-by: chenmiao <chenmiao@openatom.club>
> ---
>  rust/hw/core/meson.build |  1 +
>  rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
>  rust/hw/core/src/lib.rs  |  3 +++
>  3 files changed, 55 insertions(+)
>  create mode 100644 rust/hw/core/src/bus.rs

After a quick glance, I think this BusState is implemented quite well.

Only a few minor nits inline:

...

> +pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
> +
> +impl BusClass {
> +    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
> +        self.parent_class.class_init::<T>();
> +    }
> +}
> +
> +pub trait BusStateMethods: ObjectDeref

This can be named BusMethods - just like DeviceMethods did.

> +where
> +    Self::Target: IsA<BusState>,
> +{
> +    fn bus_realize(&self) {

"bus_" prefix is not needed:

dummy_bus.realize() is clear and enough.

> +        assert!(bql::is_locked());

It's better to add safety comment from beginning (// SAFETY: xxx).

> +        unsafe {
> +            bindings::qbus_realize(
> +                self.upcast().as_mut_ptr(),
> +                addr_of_mut!(util::bindings::error_fatal),
> +            );
> +        }
> +    }
> +}

Regards,
Zhao




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC 1/5] rust: hw: core: Add the BusState of rust version
  2025-10-27  6:23 ` Zhao Liu
  2025-10-27  6:13   ` Chen Miao
@ 2025-10-27  6:51   ` Chao Liu
  1 sibling, 0 replies; 4+ messages in thread
From: Chao Liu @ 2025-10-27  6:51 UTC (permalink / raw)
  To: zhao1.liu
  Cc: chenmiao, hust-os-kernel-patches, manos.pitsidianakis, pbonzini,
	philmd, qemu-devel, qemu-rust, richard.henderson

On 10/27/2025 2:23 PM, Zhao Liu wrote:
> On Sat, Oct 25, 2025 at 12:38:49PM +0000, chenmiao wrote:
>> Date: Sat, 25 Oct 2025 12:38:49 +0000
>> From: chenmiao <chenmiao@openatom.club>
>> Subject: [RFC 1/5] rust: hw: core: Add the BusState of rust version
>> X-Mailer: git-send-email 2.43.0
>>
>> A Rust version implementation has been designed for BusState,
>> which will be used for the subsequent I2CBus implementation.
>>
>> Signed-off-by: chenmiao <chenmiao@openatom.club>
>> ---
>>   rust/hw/core/meson.build |  1 +
>>   rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
>>   rust/hw/core/src/lib.rs  |  3 +++
>>   3 files changed, 55 insertions(+)
>>   create mode 100644 rust/hw/core/src/bus.rs
> After a quick glance, I think this BusState is implemented quite well.
>
Thanks, hh. Paolo also provided many good suggestions regarding the
implementation of BusState.

>Only a few minor nits inline:
>
>...
>
>> +pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
>> +
>> +impl BusClass {
>> +    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
>> +        self.parent_class.class_init::<T>();
>> +    }
>> +}
>> +
>> +pub trait BusStateMethods: ObjectDeref
>
>This can be named BusMethods - just like DeviceMethods did.
>

Thank you for pointing this out. I’ve noted the Rust In QEMU docs also include
relevant coding styles, and we’ll update the related naming conventions in the
next version.

>> +where
>> +    Self::Target: IsA<BusState>,
>> +{
>> +    fn bus_realize(&self) {
>
>"bus_" prefix is not needed:
>
>dummy_bus.realize() is clear and enough.
>
Ok, qbus_realize() is internally called by the C part during object
instantiation, so there is indeed no need for additional exporting.

>> +        assert!(bql::is_locked());
>
>It's better to add safety comment from beginning (// SAFETY: xxx).
>
Get~

>> +        unsafe {
>> +            bindings::qbus_realize(
>> +                self.upcast().as_mut_ptr(),
>> +                addr_of_mut!(util::bindings::error_fatal),
>> +            );
>> +        }
>> +    }
>> +}
>
>Regards,
>Zhao
>

Thanks,
Chao


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-10-27  6:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-25 12:38 [RFC 1/5] rust: hw: core: Add the BusState of rust version chenmiao
2025-10-27  6:23 ` Zhao Liu
2025-10-27  6:13   ` Chen Miao
2025-10-27  6:51   ` Chao Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).