rust: Add HPET timer device
Subsystems (2)
cover rust: Add HPET timer device
Hi folks,
This is Rust HPET normal patch series, and you can find the previous RFC at [1].
This series is based on Paolo's rust-next branch at the commit 441f501bc611 ("rust: bindings for MemoryRegionOps") along with 2 seperate patches:
* https://lore.kernel.org/qemu-devel/[email protected]/
* https://lore.kernel.org/qemu-devel/[email protected]/
Overall, HPET in Rust maintains the same logic as the original C version, adhering to the Intel IA PC-HPET spec v1.0a [2]. While keeping the logic unchanged, it attempts to keep up with the current development progress of Rust for QEMU, leveraging the latest and ongoing Rust binding updates as much as possible, such as BqlCell / BqlRefCell, qom & qdev enhancements, irq binding, and more.
*** This series further utilizes bitops, callback, memory, and Resettable bindings compared to the RFC. ***
Additionally, it introduces new bindings, including gpio_{in|out}, memattrs, and timer.
What surprised me was that while working on this version of the patch, I clearly felt the improvements in Rust QOM and QAPI. They are much more user-friendly in many ways. :-) Almost everything related with API has been simplified a lot!
(At the end of this cover letter, I briefly talked about my thoughts as I was once again faced with the choice between BqlCell and BqlRefCell.)
Welcome your comments and feedback!
Introduction
============
.
│
...
└── timer
├── hpet
│ ├── Cargo.toml
│ ├── meson.build
│ └── src
│ ├── fw_cfg.rs
│ ├── hpet.rs
│ ├── lib.rs
├── Kconfig
└── meson.buildHPET emulation contains 2 parts:
* HPET device emulation:
- hpet.rs:
It includes basic operations for the HPET timer and HPET state (which actually represents the HPET timer block).
Here, similar to the C implementation, it directly defines the registers and bit shifts as const variables, without a complete register space structure.
And, it implements various QEMU qdev/qom required traits for the HPETState.
* Global HPET firmwrie configuration:
- fw_cfg.rs
In the C code, there is a variable hpet_fw_cfg (old name: hpet_cfg) used to configure the number of HPET timer blocks and the basic HPET firmware configuration. It is defined in .c file and is referenced as extern in the .h file.
For the Rust HPET, fw_cfg.rs also implementes hpet_fw_cfg so that the .h file can still reference it.
Next Work
=========
* vmstate support.
- Utilize the recent vmstate improvement.
Additional Experience
=====================
PL011 provides a pattern to group all registers in one BqlRefCell instead of multiple BqlCells.
I also tried to leverage this design to HPET, but it didn't fit very well with this issue:
* HPETState abstracts many helpers to check register bit and tell caller about the state, e.g., is_legacy_mode(), is_timer_int_active(), etc.
But this also means these helpers can't be used in BqlRefCell::
borrow_mut() context again, otherwise, they would cause the runtime bql check panic.
- Some cases are easy to fix, i.e, use borrow_mut to modify the registers after the helpers' call.
- Some cases would by tricky, like memory write callback, since it has complex logic and it's hard to decouple register changes from the reset logic as clearly as PL011 does.
The fix for this case is either to avoid using register helpers again in the borrow_mut context of write(), or to use BqlCell instead of BqlRefCell to get finer-grained access control to avoid refactoring code logic.
I chose the latter.
So I think this might be a practical lesson that the choice of BqlCell and BqlRefCell is also indeed related to code logic: If the code logic is too complex to decouple borrow() and borrow_mut() (and the compiler doesn't check this, only the runtime's panic_already_borrowed() will complains!) , then BqlCell should be chosen. Because fine-grained access is easier to control and avoid errors. :-)
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf
Thanks and Best Rgards,
Zhao
---
Zhao Liu (10):
i386/fw_cfg: move hpet_cfg definition to hpet.c
rust/qdev: add the macro to define bit property
rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
rust: add bindings for gpio_{in|out} initialization
rust: add bindings for memattrs
rust: add bindings for timer
rust/timer/hpet: define hpet_cfg
rust/timer/hpet: add basic HPET timer and HPETState rust/timer/hpet: add qom and qdev APIs support
i386: enable rust hpet for pc when rust is enabled
configs/devices/i386-softmmu/default.mak | 1 + hw/i386/fw_cfg.c | 6 +- hw/i386/pc.c | 2 +- hw/timer/Kconfig | 6 +- hw/timer/hpet.c | 14 +- include/hw/timer/hpet.h | 2 +- meson.build | 7 + rust/Cargo.lock | 8 + rust/Cargo.toml | 1 + rust/hw/Kconfig | 1 + rust/hw/meson.build | 1 + rust/hw/timer/Kconfig | 2 + rust/hw/timer/hpet/Cargo.toml | 14 + rust/hw/timer/hpet/meson.build | 18 + rust/hw/timer/hpet/src/fw_cfg.rs | 85 +++ rust/hw/timer/hpet/src/hpet.rs | 900 +++++++++++++++++++++++ rust/hw/timer/hpet/src/lib.rs | 18 + rust/hw/timer/meson.build | 1 + rust/qemu-api/meson.build | 1 + rust/qemu-api/src/irq.rs | 18 +- rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/memory.rs | 16 +- rust/qemu-api/src/qdev.rs | 49 +- rust/qemu-api/src/timer.rs | 92 +++ rust/qemu-api/src/zeroable.rs | 7 +- rust/wrapper.h | 3 + tests/qtest/meson.build | 3 +- 27 files changed, 1252 insertions(+), 25 deletions(-) create mode 100644 rust/hw/timer/Kconfig create mode 100644 rust/hw/timer/hpet/Cargo.toml create mode 100644 rust/hw/timer/hpet/meson.build create mode 100644 rust/hw/timer/hpet/src/fw_cfg.rs create mode 100644 rust/hw/timer/hpet/src/hpet.rs create mode 100644 rust/hw/timer/hpet/src/lib.rs create mode 100644 rust/hw/timer/meson.build create mode 100644 rust/qemu-api/src/timer.rs
Signature · 1 line
2.34.1
PATCH 1/10 i386/fw_cfg: move hpet_cfg definition to hpet.c
HPET device needs to access and update hpet_cfg variable, but now it is defined in hw/i386/fw_cfg.c and Rust code can't access it.
Move hpet_cfg definition to hpet.c (and rename it to hpet_fw_cfg). This allows Rust HPET device implements its own global hpet_fw_cfg variable, and will further reduce the use of unsafe C code access and calls in the Rust HPET implementation.
--- hw/i386/fw_cfg.c | 4 +--- hw/timer/hpet.c | 14 ++++++++------ include/hw/timer/hpet.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-)
hw/i386/fw_cfg.c +1 -3
@@ -26,8 +26,6 @@#include CONFIG_DEVICES #include "target/i386/cpu.h" -struct hpet_fw_config hpet_cfg = {.count =UINT8_MAX }; -const char *fw_cfg_arch_key_name (uint16_t key) { static const struct {
@@ -153,7 +151,7 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms,PCMachineState *pcms = (PCMachineState *)object_dynamic_cast (OBJECT (ms),TYPE_PC_MACHINE );if (pcms && pcms->hpet_enabled) { -fw_cfg_add_bytes (fw_cfg,FW_CFG_HPET , &hpet_cfg,sizeof (hpet_cfg)); +fw_cfg_add_bytes (fw_cfg,FW_CFG_HPET , &hpet_fw_cfg,sizeof (hpet_fw_cfg)); }#endif
hw/timer/hpet.c +8 -6
@@ -40,6 +40,8 @@#include "qom/object.h" #include "trace.h" +struct hpet_fw_config hpet_fw_cfg = {.count =UINT8_MAX }; +#define HPET_MSI_SUPPORT 0OBJECT_DECLARE_SIMPLE_TYPE (HPETState,HPET )
@@ -655,8 +657,8 @@ static void hpet_reset(DeviceState *d) s->hpet_counter =0ULL ; s->hpet_offset =0ULL ; s->config =0ULL ; - hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t )s->capability; - hpet_cfg.hpet[s->hpet_id].address = sbd->mmio[0 ].addr; + hpet_fw_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t )s->capability; + hpet_fw_cfg.hpet[s->hpet_id].address = sbd->mmio[0 ].addr;/* to document that the RTC lowers its output on reset as well */ s->rtc_irq_level =0 ;
@@ -698,17 +700,17 @@ static void hpet_realize(DeviceState *dev, Error **errp)if (!s->intcap) {warn_report ("Hpet's intcap not initialized" ); } -if (hpet_cfg.count ==UINT8_MAX ) { +if (hpet_fw_cfg.count ==UINT8_MAX ) {/* first instance */ - hpet_cfg.count =0 ; + hpet_fw_cfg.count =0 ; } -if (hpet_cfg.count ==8 ) { +if (hpet_fw_cfg.count ==8 ) {error_setg (errp,"Only 8 instances of HPET is allowed" );return ; } - s->hpet_id = hpet_cfg.count++; + s->hpet_id = hpet_fw_cfg.count++;for (i =0 ; i <HPET_NUM_IRQ_ROUTES ; i++) {sysbus_init_irq (sbd, &s->irqs[i]);
include/hw/timer/hpet.h +1 -1
@@ -73,7 +73,7 @@ struct hpet_fw_configstruct hpet_fw_entry hpet[8 ]; }QEMU_PACKED ; -extern struct hpet_fw_config hpet_cfg; +extern struct hpet_fw_config hpet_fw_cfg;#define TYPE_HPET "hpet"
Signature · 1 line
2.34.1
PATCH 2/10 rust/qdev: add the macro to define bit property
In reply to Zhao LiuHPET device (Rust device) needs to define the bit type property.
Add a variant of define_property macro to define bit type property.
--- rust/qemu-api/src/qdev.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+)
rust/qemu-api/src/qdev.rs +12 -0
@@ -166,6 +166,18 @@ fn class_init(dc: &mut DeviceClass) {#[macro_export] macro_rules! define_property { + ($name:expr, $state:ty, $field:ident, $prop:expr, $type:ty, bit = $bitnr:expr,default = $defval:expr$(,)*) => { + $crate::bindings::Property { +// use associated function syntax for type checking + name: ::std::ffi::CStr ::as_ptr ($name), + info: $prop, + offset: $crate::offset_of!($state, $field)as isize , + bitnr: $bitnr, + set_default:true , + defval: $crate::bindings::Property__bindgen_ty_1 { u: $defvalas u64 }, + ..$crate::zeroable::Zeroable ::ZERO + } + }; ($name:expr, $state:ty, $field:ident, $prop:expr, $type:ty,default = $defval:expr$(,)*) => { $crate::bindings::Property {// use associated function syntax for type checking
Signature · 1 line
2.34.1
PATCH 3/10 rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
In reply to Zhao LiuThis is useful to hanlde InterruptSource slice and pass it to C bindings.
---
Changes since RFC:
* New commit.
--- rust/qemu-api/src/irq.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
rust/qemu-api/src/irq.rs +13 -5
@@ -5,12 +5,10 @@//! Bindings for interrupt sources use core::ptr; -use std::{marker::PhantomData , os::raw::c_int}; +use std::{marker::PhantomData , os::raw::c_int, slice}; -use crate ::{ - bindings::{qemu_set_irq,IRQState }, - prelude::*, -}; +pub (crate )use crate ::bindings::IRQState ; +use crate ::{bindings::qemu_set_irq, prelude::*};/// Interrupt sources are used by devices to pass changes to a value (typically /// a boolean). The interrupt sink is usually an interrupt controller or
@@ -81,6 +79,16 @@ pub fn set(&self, level: T) {pub (crate )const fn as_ptr (&self) -> *mut *mut IRQState { self.cell.as_ptr () } + +#[allow(dead_code)] +pub (crate )fn as_slice_of_qemu_irq (slice: &[Self ]) -> &[*mut IRQState ] { +unsafe { +// SAFETY: InterruptSource has the same memory layout as *mut IRQState. +// Additionally, the slice parameter itself, as a reference to a slice type, +// can be converted into a valid pointer and has a valid length. + slice::from_raw_parts (slice.as_ptr ().cast ::<*mut IRQState >(), slice.len ()) + } + } }impl Default for InterruptSource {
Signature · 1 line
2.34.1
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Quoted 5 lines · Zhao Liu
This is useful to hanlde InterruptSource slice and pass it to C bindings.
Suggested-by Paolo Bonzini <[email protected]>
This may be a bad suggestion, after all. clippy complains that you're casting &[*mut IRQState] to mutable (https://rust-lang.github.io/rust-
clippy/master/#as_ptr_cast_mut).
I can think of two solutions:
1) add #[allow(clippy::as_ptr_cast_mut)] and explain with a comment
// Casting to *mut *mut IRQState is valid, because
// the source slice `pins` uses interior mutability.2) drop as_slice_of_qemu_irq() and replace it with something like
pub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut IRQState {
slice[0].as_ptr()
}You choose.
Paolo
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Quoted 23 lines · Paolo Bonzini
Date: Wed, 29 Jan 2025 11:51:02 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 03/10]rust/irq: Add a helper to convert
[InterruptSource] to [*mut IRQState]Quoted 5 lines · Zhao Liu
This is useful to hanlde InterruptSource slice and pass it to C bindings.
Suggested-by Paolo Bonzini <[email protected]>This may be a bad suggestion, after all. clippy complains that you're casting &[*mut IRQState] to mutable (https://rust-lang.github.io/rust-
clippy/master/#as_ptr_cast_mut).I can think of two solutions:
1) add #[allow(clippy::as_ptr_cast_mut)] and explain with a comment
// Casting to *mut *mut IRQState is valid, because // the source slice `pins` uses interior mutability.
Quoted 8 lines
2) drop
as_slice_of_qemu_irq()and replace it with something likepub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut IRQState { slice[0].as_ptr() }You choose.
:) I choose the second way, which goes back to the initial codes.
Though an empty slice w/o any check would cause a runtime error like:
"thread '<unnamed>' panicked at 'index out of bounds: the len is 0 but the index is 0'"
Adding an assert to ensure a non-empty slice is better.
Thanks,
Zhao
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Quoted 5 lines · Zhao Liu
Date: Fri, 7 Feb 2025 15:10:53 +0800
From: Zhao Liu <[email protected]>
Subject: Re: [PATCH 03/10]rust/irq: Add a helper to convert
[InterruptSource] to [*mut IRQState]
Quoted 24 lines
Quoted 23 lines · Paolo Bonzini
Date: Wed, 29 Jan 2025 11:51:02 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 03/10]rust/irq: Add a helper to convert
[InterruptSource] to [*mut IRQState]Quoted 5 lines · Zhao Liu
This is useful to hanlde InterruptSource slice and pass it to C bindings.
Suggested-by Paolo Bonzini <[email protected]>This may be a bad suggestion, after all. clippy complains that you're casting &[*mut IRQState] to mutable (https://rust-lang.github.io/rust-
clippy/master/#as_ptr_cast_mut).I can think of two solutions:
1) add #[allow(clippy::as_ptr_cast_mut)] and explain with a comment
// Casting to *mut *mut IRQState is valid, because // the source slice `pins` uses interior mutability.
I agree it's the best to use interior mutability.
Just to confirm, I check with cargo +nightly clippy but it doesn't complain about this case. Should I switch to another version of clippy when I do such check? (currently I'm using v0.1.63 clippy as well, to match rustc.)
Thanks,
Zhao
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Il ven 7 feb 2025, 08:25 Zhao Liu <[email protected]> ha scritto:
Quoted 5 lines
Just to confirm, I check with
cargo +nightly clippybut it doesn't complain about this case. Should I switch to another version of clippy when I do such check? (currently I'm using v0.1.63 clippy as well, to match rustc.)
I don't remember exactly how I noticed it—I am pretty sure it broke in CI though. Maybe the change to add rust_version hid it.
To answer your question, generally the idea is that we use the latest version of the developer tools (cargo, rustfmt, clippy). In particular old versions of cargo don't support retrieving clippy settings from Cargo.toml.
Paolo
Quoted 4 lines
Thanks,
Zhao
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Quoted 13 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 10:57:11 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 03/10]rust/irq: Add a helper to convert
[InterruptSource] to [*mut IRQState]Il ven 7 feb 2025, 08:25 Zhao Liu <[email protected]> ha scritto:
Quoted 5 lines
Just to confirm, I check with
cargo +nightly clippybut it doesn't complain about this case. Should I switch to another version of clippy when I do such check? (currently I'm using v0.1.63 clippy as well, to match rustc.)
Quoted 6 lines
I don't remember exactly how I noticed it—I am pretty sure it broke in CI though. Maybe the change to add
rust_versionhid it.To answer your question, generally the idea is that we use the latest version of the developer tools (cargo, rustfmt, clippy). In particular old versions of cargo don't support retrieving clippy settings from Cargo.toml.
This one inspired me. I'm thinking that even though we deleted the README of pl011, a gneral guide or doc section (at somewhere, e.g., docs/devel/style.rst or docs/devel/submitting-a-patch.rst) would be helpful.
At least, that could remind contributor to check patches with latest toolchain instead of letting the maintainer's CI smoking fail.
Thanks,
Zhao
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Il sab 8 feb 2025, 11:55 Zhao Liu <[email protected]> ha scritto:
Quoted 23 lines · Paolo Bonzini
Quoted 13 lines
Date: Fri, 7 Feb 2025 10:57:11 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 03/10]rust/irq: Add a helper to convert
[InterruptSource] to [*mut IRQState]Il ven 7 feb 2025, 08:25 Zhao Liu <[email protected]> ha scritto:
Quoted 5 lines
Just to confirm, I check with
cargo +nightly clippybut it doesn't complain about this case. Should I switch to another version of clippy when I do such check? (currently I'm using v0.1.63 clippy as well, to match rustc.)Quoted 5 lines
I don't remember exactly how I noticed it—I am pretty sure it broke in CI though. Maybe the change to add
rust_versionhid it.To answer your question, generally the idea is that we use the latest version of the developer tools (cargo, rustfmt, clippy). In particular
old
Quoted 1 line
versions of cargo don't support retrieving clippy settings from
Cargo.toml.
Quoted 8 lines
This one inspired me. I'm thinking that even though we deleted the README of pl011, a gneral guide or doc section (at somewhere, e.g.,
docs/devel/style.rstordocs/devel/submitting-a-patch.rst) would be helpful.At least, that could remind contributor to check patches with latest toolchain instead of letting the maintainer's CI smoking fail.
I have sent a docs/devel/rust.rst sometime last week, it will be in the next pull request and then you can send a patch on top.
Paolo
Thanks,
Quoted 3 lines
Zhao
Re: [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState]
Quoted 3 lines
I have sent a
docs/devel/rust.rstsometime last week, it will be in the next pull request and then you can send a patch on top.
Nice, I'll also have a look at that patch
And I plan to post v2 tomorrow... as I'll leave at next week from Tuesday. Try to catch your pull request train if possible. :-)
Thanks,
Zhao
PATCH 4/10 rust: add bindings for gpio_{in|out} initialization
In reply to Zhao LiuWrap qdev_init_gpio_{in|out} as methods in DeviceMethods. And for qdev_init_gpio_in, based on FnCall, it can support idiomatic Rust callback without the need for C style wrapper.
---
Changes since RFC:
* Use FnCall to support gpio in callback.
* Place gpio_{in|out} in DeviceMethods.
* Accept &[InterruptSource] as the parameter of gpio_out.
--- rust/qemu-api/src/qdev.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-)
rust/qemu-api/src/qdev.rs +35 -2
@@ -6,16 +6,17 @@use std::{ ffi::{CStr ,CString }, - os::raw::c_void, + os::raw::{c_int, c_void}, ptr::NonNull , };pub use bindings::{Clock ,ClockEvent ,DeviceClass ,DeviceState ,Property ,ResetType };use crate ::{ - bindings::{self,Error ,ResettableClass }, + bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out,Error ,ResettableClass }, callbacks::FnCall , cell::bql_locked, + irq::{IRQState ,InterruptSource }, prelude::*, qom::{ClassInitImpl ,ObjectClass ,ObjectImpl ,Owned }, vmstate::VMStateDescription ,
@@ -278,6 +279,38 @@ fn do_init_clock_in(// IsA<DeviceState> bound. do_init_clock_in (unsafe { self.as_mut_ptr () }, name, cb, events) } + +fn init_gpio_in <F :for <'a>FnCall <(&'aSelf ::Target ,u32 ,u32 )>>(&self, num_lines:u32 , _f:F ) { +unsafe extern "C" fn rust_irq_handler <T ,F :for <'a>FnCall <(&'aT ,u32 ,u32 )>>( + opaque: *mut c_void , + line:c_int , + level:c_int , + ) { +// SAFETY: the opaque was passed as a reference to `T` +F ::call ((unsafe { &*(opaque.cast ::<T >()) }, lineas u32 , levelas u32 )) + } + +let gpio_in_cb:unsafe extern "C" fn (*mut c_void ,c_int ,c_int ) = +rust_irq_handler ::<Self ::Target ,F >; + +unsafe { +qdev_init_gpio_in ( + self.as_mut_ptr ::<DeviceState >(), +Some (gpio_in_cb), + num_linesas c_int , + ); + } + } + +fn init_gpio_out (&self, pins: &[InterruptSource ]) { +unsafe { +qdev_init_gpio_out ( + self.as_mut_ptr ::<DeviceState >(), +InterruptSource ::as_slice_of_qemu_irq (pins).as_ptr ()as *mut *mut IRQState , + pins.len ()as c_int , + ); + } + } }impl <R :ObjectDeref >DeviceMethods for R where R ::Target :IsA <DeviceState > {}
Signature · 1 line
2.34.1
Re: [PATCH 04/10] rust: add bindings for gpio_{in|out} initialization
rust/qemu-api/src/qdev.rs fn do_init_clock_in( · line 294
Quoted 12 lines · Zhao Liu
rust/qemu-api/src/qdev.rs +12
-0+fn init_gpio_in <F :for <'a>FnCall <(&'aSelf ::Target ,u32 ,u32 )>>(&self, num_lines:u32 , _f:F ) { +unsafe extern "C" fn rust_irq_handler <T ,F :for <'a>FnCall <(&'aT ,u32 ,u32 )>>( + opaque: *mut c_void , + line:c_int , + level:c_int , + ) { +// SAFETY: the opaque was passed as a reference to `T` +F ::call ((unsafe { &*(opaque.cast ::<T >()) }, lineas u32 , levelas u32 )) + } + +let gpio_in_cb:unsafe extern "C" fn (*mut c_void ,c_int ,c_int ) = +rust_irq_handler ::<Self ::Target ,F >;
Please add "let _: () = F::ASSERT_IS_SOME;", which is added by the qdev_init_clock_in() patch.
Paolo
Re: [PATCH 04/10] rust: add bindings for gpio_{in|out} initialization
Quoted 21 lines · Paolo Bonzini
Date: Wed, 29 Jan 2025 11:59:04 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 04/10] rust: add bindings for gpio_{in|out}
initializationrust/qemu-api/src/qdev.rs fn do_init_clock_in( · line 294
Quoted 12 lines · Zhao Liu
rust/qemu-api/src/qdev.rs +12
-0+fn init_gpio_in <F :for <'a>FnCall <(&'aSelf ::Target ,u32 ,u32 )>>(&self, num_lines:u32 , _f:F ) { +unsafe extern "C" fn rust_irq_handler <T ,F :for <'a>FnCall <(&'aT ,u32 ,u32 )>>( + opaque: *mut c_void , + line:c_int , + level:c_int , + ) { +// SAFETY: the opaque was passed as a reference to `T` +F ::call ((unsafe { &*(opaque.cast ::<T >()) }, lineas u32 , levelas u32 )) + } + +let gpio_in_cb:unsafe extern "C" fn (*mut c_void ,c_int ,c_int ) = +rust_irq_handler ::<Self ::Target ,F >;
Quoted 3 lines
Please add "let _: () =
F::ASSERT_IS_SOME;", which is added by theqdev_init_clock_in()patch.
Okay.
I would add assert!(F::is_some()); at the beginning of init_gpio_in().
There's a difference with origianl C version:
In C side, qdev_get_gpio_in() family could accept a NULL handler, but there's no such case in current QEMU:
* qdev_get_gpio_in
* qdev_init_gpio_in_named
* qdev_init_gpio_in_named_with_opaque
And from code logic view, creating an input GPIO line but doing nothing on input, sounds also unusual.
So, for simplicity, in the Rust version I make the handler non-optional.
Re: [PATCH 04/10] rust: add bindings for gpio_{in|out} initialization
Il ven 7 feb 2025, 09:24 Zhao Liu <[email protected]> ha scritto:
Quoted 8 lines
Quoted 3 lines
Please add "let _: () =
F::ASSERT_IS_SOME;", which is added by theqdev_init_clock_in()patch.Okay.
I would add
assert!(F::is_some());at the beginning ofinit_gpio_in().
Use the "let" so that it's caught at compile time.
There's a difference with origianl C version:
Quoted 11 lines
In C side,
qdev_get_gpio_in()family could accept a NULL handler, but there's no such case in current QEMU:*
qdev_get_gpio_in
*qdev_init_gpio_in_named
*qdev_init_gpio_in_named_with_opaqueAnd from code logic view, creating an input GPIO line but doing nothing on input, sounds also unusual.
Wouldn't it then crash in qemu_set_irq?
Paolo
So, for simplicity, in the Rust version I make the handler non-optional.
Quoted 3 lines
Re: [PATCH 04/10] rust: add bindings for gpio_{in|out} initialization
Quoted 1 line
Use the "let" so that it's caught at compile time.
Thanks! Fixed.
Quoted 9 lines
There's a difference with origianl C version:
Quoted 8 lines
In C side,
qdev_get_gpio_in()family could accept a NULL handler, but there's no such case in current QEMU:*
qdev_get_gpio_in
*qdev_init_gpio_in_named
*qdev_init_gpio_in_named_with_opaque
Quoted 6 lines
Quoted 3 lines
And from code logic view, creating an input GPIO line but doing nothing on input, sounds also unusual.
Wouldn't it then crash in
qemu_set_irq?
Yes! Thank you for the education.
Regards,
Zhao
PATCH 5/10 rust: add bindings for memattrs
In reply to Zhao LiuThe MemTxAttrs structure contains bitfield members, and bindgen is unable to generate an equivalent macro definition for MEMTXATTRS_UNSPECIFIED.
Therefore, manually define a global constant variable MEMTXATTRS_UNSPECIFIED to support calls from Rust code.
---
Changes since RFC:
* With a boolean type unspecified field, no need to add once_cell.
* Merge memattrs binding to memory.rs.
--- rust/qemu-api/src/memory.rs | 16 ++++++++++++++-- rust/qemu-api/src/zeroable.rs | 1 + rust/wrapper.h | 1 + 3 files changed, 16 insertions(+), 2 deletions(-)
rust/qemu-api/src/memory.rs +14 -2
@@ -2,7 +2,7 @@// Author(s): Paolo Bonzini <[email protected]> // SPDX-License-Identifier: GPL-2.0-or-later -//! Bindings for `MemoryRegion` and `MemoryRegionOps` +//! Bindings for `MemoryRegion`, `MemoryRegionOps` and `MemTxAttrs` use std::{ ffi::{CStr ,CString },
@@ -11,7 +11,7 @@ ptr::addr_of, }; -pub use bindings::hwaddr; +pub use bindings::{hwaddr,MemTxAttrs };use crate ::{ bindings::{self, device_endian, memory_region_init_io},
@@ -189,3 +189,15 @@ unsafe impl ObjectType for MemoryRegion {unsafe {CStr ::from_bytes_with_nul_unchecked (bindings::TYPE_MEMORY_REGION ) }; }qom_isa! (MemoryRegion :Object ); + +/// A special `MemTxAttrs` constant, used to indicate that no memary +/// attributes are specified. +/// +/// Bus masters which don't specify any attributes will get this, +/// which has all attribute bits clear except the topmost one +/// (so that we can distinguish "all attributes deliberately clear" +/// from "didn't specify" if necessary). +pub const MEMTXATTRS_UNSPECIFIED :MemTxAttrs =MemTxAttrs { + unspecified:true , + ..Zeroable ::ZERO +};
rust/qemu-api/src/zeroable.rs +1 -0
@@ -101,3 +101,4 @@ fn default() -> Self {impl_zeroable! (crate ::bindings::MemoryRegionOps__bindgen_ty_1 );impl_zeroable! (crate ::bindings::MemoryRegionOps__bindgen_ty_2 );impl_zeroable! (crate ::bindings::MemoryRegionOps ); +impl_zeroable! (crate ::bindings::MemTxAttrs );
rust/wrapper.h +1 -0
@@ -62,3 +62,4 @@ typedef enum memory_order {#include "qapi/error.h" #include "migration/vmstate.h" #include "chardev/char-serial.h" +#include "exec/memattrs.h"
Signature · 1 line
2.34.1
PATCH 6/10 rust: add bindings for timer
In reply to Zhao LiuAdd timer bindings to help handle idiomatic Rust callbacks.
Additionally, wrap QEMUClockType in ClockType binding to avoid unsafe calls in device code.
---
Changes since RFC:
* Use FnCall to support timer callback.
* Only add timer_init_full() binding. timer_new() is unnecessary since device (HPET) could create and allocate QEMUTimer.
* Implement Drop for QEMUTimer.
* Add ClockType binding.
--- meson.build | 7 +++ rust/qemu-api/meson.build | 1 + rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/timer.rs | 92 ++++++++++++++++++++++++++++++++++++++ rust/wrapper.h | 1 + 5 files changed, 102 insertions(+) create mode 100644 rust/qemu-api/src/timer.rs
meson.build +7 -0
@@ -4079,6 +4079,13 @@ if have_rust foreach enum : c_bitfields bindgen_args += ['--bitfield-enum', enum] endforeach + c_nocopy = [ + 'QEMUTimer', + ] + # Used to customize Drop trait + foreach struct : c_nocopy + bindgen_args += ['--no-copy', struct] + endforeach # TODO: Remove this comment when the clang/libclang mismatch issue is solved. #
rust/qemu-api/meson.build +1 -0
@@ -29,6 +29,7 @@ _qemu_api_rs = static_library( 'src/qdev.rs', 'src/qom.rs', 'src/sysbus.rs', + 'src/timer.rs', 'src/vmstate.rs', 'src/zeroable.rs', ],
rust/qemu-api/src/lib.rs +1 -0
@@ -24,6 +24,7 @@pub mod qdev;pub mod qom;pub mod sysbus; +pub mod timer;pub mod vmstate;pub mod zeroable;
rust/qemu-api/src/timer.rs new file +92 -0
92 lines not on this page · raw message
rust/wrapper.h +1 -0
@@ -63,3 +63,4 @@ typedef enum memory_order {#include "migration/vmstate.h" #include "chardev/char-serial.h" #include "exec/memattrs.h" +#include "qemu/timer.h"
Signature · 1 line
2.34.1
Re: [PATCH 06/10] rust: add bindings for timer
meson.build if have_rust · line 4088
Quoted 7 lines · Zhao Liu
meson.build +7
-0+ c_nocopy = [ + 'QEMUTimer', + ] + # Used to customize Drop trait + foreach struct : c_nocopy + bindgen_args += ['--no-copy', struct] + endforeach
Nice.
Quoted 6 lines
rust/qemu-api/src/timer.rs new file +6
-0+pub use bindings::QEMUTimer ; + +use crate ::{ + bindings::{ + self, qemu_clock_get_ns, timer_del, timer_init_full, timer_mod,QEMUClockType , +QEMUTimerListGroup ,
rust/qemu-api/src/timer.rs line 22
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ }, + callbacks::FnCall, +}; + +impl QEMUTimer { +pub fn new () ->Self { +Default ::default () + } + + pub fn timer_init_full<'timer, 'opaque: 'timer, T, F>(
General question - should the names:
- include the "timer" part, matching QEMU C code, or exclude it to avoid repetition? I would say remove it, but I'm open to suggestions and other opinions
- include the "QEMU" part? I'd say remove it, similar to ClockType below that is:
-pub use bindings::QEMUTimer;
+pub use bindings::QEMUTimer as Timer;
+pub use bindings::QEMUTimerList as TimerList; +pub use bindings::QEMUTimerListGroup as TimerListGroup;
rust/qemu-api/src/timer.rs line 25
Quoted 3 lines
rust/qemu-api/src/timer.rs new file +3
-0+ &'timer mut self, + timer_list_group: Option<&QEMUTimerListGroup>, + clk_type: QEMUClockType,
Please take a ClockType instead.
Quoted 4 lines
rust/qemu-api/src/timer.rs new file +4
-0+ scale: u32, + attributes: u32, + _f: F, + opaque: &'opaque T,
rust/qemu-api/src/timer.rs line 39
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ ) where +F :for <'a>FnCall <(&'aT ,)>, + { +/// timer expiration callback +unsafe extern "C" fn rust_timer_handler <T ,F :for <'a>FnCall <(&'aT ,)>>( + opaque: *mut c_void , + ) { +// SAFETY: the opaque was passed as a reference to `T`. +F ::call ((unsafe { &*(opaque.cast ::<T >()) },)) + }
Please add "let _: () = F::ASSERT_IS_SOME;", which is added by the qdev_init_clock_in() patch.
Quoted 13 lines
rust/qemu-api/src/timer.rs new file +13
-0+let timer_cb:unsafe extern "C" fn (*mut c_void ) =rust_timer_handler ::<T ,F >; + +// SAFETY: the opaque outlives the timer +unsafe { +timer_init_full ( + self, +if let Some (g) = timer_list_group { + gas *const QEMUTimerListGroup as *mut QEMUTimerListGroup + }else { + ::core::ptr::null_mut () + }, + clk_type, + scale as c_int,
rust/qemu-api/src/timer.rs line 63
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ attributes as c_int, + Some(timer_cb), + (opaque as *const T).cast::<c_void>() as *mut c_void, + ) + } + } + +pub fn timer_mod (&mut self, expire_time:u64 ) { +unsafe {timer_mod (selfas *mut QEMUTimer , expire_timeas i64 ) } + }
This can take &self, because timers are thread-safe:
pub fn timer_mod(&self, expire_time: u64) {
unsafe { timer_mod(self.as_mut_ptr(), expire_time as i64) }
}
const fn as_mut_ptr(&self) -> *mut Self {
self as *const QEMUTimer as *mut _
}rust/qemu-api/src/timer.rs line 69
Quoted 6 lines
rust/qemu-api/src/timer.rs new file +6
-0+} + +impl Drop for QEMUTimer { +fn drop (&mut self) { +unsafe {timer_del (selfas *mut QEMUTimer ) } + }
timer_del() can be useful even outside Drop, so
pub fn timer_del(&self) {
unsafe { timer_del(self.as_mut_ptr()) }
}and just use self.timer_del() here.
rust/qemu-api/src/timer.rs line 76
Quoted 7 lines
rust/qemu-api/src/timer.rs new file +7
-0+} + +pub fn qemu_clock_get_virtual_ns () ->u64 { +// SAFETY: +// Valid parameter. + (unsafe {qemu_clock_get_ns (QEMUClockType ::QEMU_CLOCK_VIRTUAL ) })as u64 +}
Not needed.
rust/qemu-api/src/timer.rs line 80
Quoted 3 lines
rust/qemu-api/src/timer.rs new file +3
-0+pub struct ClockType { +pub id:QEMUClockType , +}
The field does not have to be "pub" (maybe "pub(self)", I'm not sure offhand).
Paolo
Quoted 23 lines
rust/qemu-api/src/timer.rs new file +11
-0+impl ClockType { +pub fn get_ns (&self) ->u64 { +// SAFETY: cannot be created outside this module, therefore id +// is valid + (unsafe {qemu_clock_get_ns (self.id) })as u64 + } +} + +pub const CLOCK_VIRTUAL :ClockType =ClockType { + id:QEMUClockType ::QEMU_CLOCK_VIRTUAL , +};rust/wrapper.h +1
-1@@ -63,3 +63,4 @@ typedef enum memory_order {#include "migration/vmstate.h" #include "chardev/char-serial.h" #include "exec/memattrs.h" +#include "qemu/timer.h" --2.34.1
Re: [PATCH 06/10] rust: add bindings for timer
Quoted 19 lines
Quoted 6 lines
rust/qemu-api/src/timer.rs new file +6
-0+pub use bindings::QEMUTimer ; + +use crate ::{ + bindings::{ + self, qemu_clock_get_ns, timer_del, timer_init_full, timer_mod,QEMUClockType , +QEMUTimerListGroup ,rust/qemu-api/src/timer.rs line 22
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ }, + callbacks::FnCall, +}; + +impl QEMUTimer { +pub fn new () ->Self { +Default ::default () + } + + pub fn timer_init_full<'timer, 'opaque: 'timer, T, F>(General question - should the names:
Quoted 2 lines
- include the "timer" part, matching QEMU C code, or exclude it to avoid repetition? I would say remove it,
I agree and I would name it "init()" instead of "init_full()".
Review
Quoted 10 lines
but I'm open to suggestions and other
opinions- include the "QEMU" part? I'd say remove it, similar to ClockType below that is:
(quoted diff) +3
-1-pub use bindings::QEMUTimer; +pub use bindings::QEMUTimer as Timer; +pub use bindings::QEMUTimerList as TimerList; +pub use bindings::QEMUTimerListGroup as TimerListGroup;
I notice you've picked another way for IRQState, so I could follow that like:
pub type Timer = bindings::QEMUTimer;
This style make it easy to add doc (timer binding currently lacks doc, but I will add it as much as possible).
Another option may be to wrap QEMUTimer as what MemoryRegionOps did, but timer has only 1 callback so I think it's not necessary.
Quoted 5 lines
rust/qemu-api/src/timer.rs line 25
Quoted 3 lines
rust/qemu-api/src/timer.rs new file +3
-0+ &'timer mut self, + timer_list_group: Option<&QEMUTimerListGroup>, + clk_type: QEMUClockType,Please take a ClockType instead.
Sure.
Quoted 15 lines
Quoted 4 lines
rust/qemu-api/src/timer.rs new file +4
-0+ scale: u32, + attributes: u32, + _f: F, + opaque: &'opaque T,rust/qemu-api/src/timer.rs line 39
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ ) where +F :for <'a>FnCall <(&'aT ,)>, + { +/// timer expiration callback +unsafe extern "C" fn rust_timer_handler <T ,F :for <'a>FnCall <(&'aT ,)>>( + opaque: *mut c_void , + ) { +// SAFETY: the opaque was passed as a reference to `T`. +F ::call ((unsafe { &*(opaque.cast ::<T >()) },)) + }
Quoted 2 lines
Please add "let _: () =
F::ASSERT_IS_SOME;", which is added by theqdev_init_clock_in()patch.
Sure. Added it to the beginning of this function.
Quoted 26 lines
Quoted 13 lines
rust/qemu-api/src/timer.rs new file +13
-0+let timer_cb:unsafe extern "C" fn (*mut c_void ) =rust_timer_handler ::<T ,F >; + +// SAFETY: the opaque outlives the timer +unsafe { +timer_init_full ( + self, +if let Some (g) = timer_list_group { + gas *const QEMUTimerListGroup as *mut QEMUTimerListGroup + }else { + ::core::ptr::null_mut () + }, + clk_type, + scale as c_int,rust/qemu-api/src/timer.rs line 63
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ attributes as c_int, + Some(timer_cb), + (opaque as *const T).cast::<c_void>() as *mut c_void, + ) + } + } + +pub fn timer_mod (&mut self, expire_time:u64 ) { +unsafe {timer_mod (selfas *mut QEMUTimer , expire_timeas i64 ) } + }This can take &self, because timers are thread-safe:
Quoted 3 lines
pub fn timer_mod(&self, expire_time: u64) {
unsafe { timer_mod(self.as_mut_ptr(), expire_time as i64) }
}timer_mod means "modify a timer", so I'd rename this method to "modify"
Quoted 3 lines
const fn as_mut_ptr(&self) -> *mut Self {
self as *const QEMUTimer as *mut _
}Thanks!
Quoted 9 lines
rust/qemu-api/src/timer.rs line 69
Quoted 6 lines
rust/qemu-api/src/timer.rs new file +6
-0+} + +impl Drop for QEMUTimer { +fn drop (&mut self) { +unsafe {timer_del (selfas *mut QEMUTimer ) } + }
timer_del()can be useful even outside Drop, so
Quoted 5 lines
pub fn timer_del(&self) { unsafe { timer_del(self.as_mut_ptr()) } }and just use
self.timer_del()here.
OK, will also rename "timer_del" to "delete".
Quoted 9 lines
rust/qemu-api/src/timer.rs line 76
Quoted 7 lines
rust/qemu-api/src/timer.rs new file +7
-0+} + +pub fn qemu_clock_get_virtual_ns () ->u64 { +// SAFETY: +// Valid parameter. + (unsafe {qemu_clock_get_ns (QEMUClockType ::QEMU_CLOCK_VIRTUAL ) })as u64 +}Not needed.
Yes!
Quoted 7 lines
rust/qemu-api/src/timer.rs line 80
Quoted 3 lines
rust/qemu-api/src/timer.rs new file +3
-0+pub struct ClockType { +pub id:QEMUClockType , +}The field does not have to be "pub" (maybe "pub(self)", I'm not sure offhand).
You're right! Making it private is enough, since I should also make timer_init_full accept ClockType instead of QEMUClockType.
Thanks,
Zhao
Re: [PATCH 06/10] rust: add bindings for timer
Quoted 19 lines · Zhao Liu
Quoted 19 lines
Quoted 6 lines
rust/qemu-api/src/timer.rs new file +6
-0+pub use bindings::QEMUTimer ; + +use crate ::{ + bindings::{ + self, qemu_clock_get_ns, timer_del, timer_init_full, timer_mod,QEMUClockType , +QEMUTimerListGroup ,rust/qemu-api/src/timer.rs line 22
Quoted 10 lines
rust/qemu-api/src/timer.rs new file +10
-0+ }, + callbacks::FnCall, +}; + +impl QEMUTimer { +pub fn new () ->Self { +Default ::default () + } + + pub fn timer_init_full<'timer, 'opaque: 'timer, T, F>(General question - should the names:
Quoted 4 lines
Quoted 2 lines
- include the "timer" part, matching QEMU C code, or exclude it to avoid repetition? I would say remove it,
I agree and I would name it "
init()" instead of "init_full()".
Please keep init_full(); init() would be a version without some of the arguments (e.g. the TimerListGroup, or the attributes).
Quoted 7 lines
I notice you've picked another way for IRQState, so I could follow that like:
pub type Timer =
bindings::QEMUTimer;This style make it easy to add doc (timer binding currently lacks doc, but I will add it as much as possible).
Good point.
Quoted 2 lines
Another option may be to wrap QEMUTimer as what MemoryRegionOps did, but timer has only 1 callback so I think it's not necessary.
Yes, and we actually should do it sooner or later to add a PhantomPinned field, because timers can't move in memory! But no need to do it now.
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ scale: u32,
While at it, can you add constants for the scale, i.e.
pub const NS: u32 = bindings::SCALE_NS;
pub const US: u32 = bindings::SCALE_US;
pub const MS: u32 = bindings::SCALE_MS;? Using Timer::NS is clear enough and removes the need to import from "bindings". At least in theory, bindings should not even have to be "pub" (or at least that's where the code should move towards).
Quoted 11 lines
Quoted 9 lines
rust/qemu-api/src/timer.rs line 63
Quoted 3 lines
rust/qemu-api/src/timer.rs new file +3
-0+pub fn timer_mod (&mut self, expire_time:u64 ) { +unsafe {timer_mod (selfas *mut QEMUTimer , expire_timeas i64 ) } + }This can take &self, because timers are thread-safe:
pub fn timer_mod(&self, expire_time: u64) { unsafe { timer_mod(self.as_mut_ptr(), expire_time as i64) } }
timer_modmeans "modify a timer", so I'd rename this method to "modify"
Yeah, changing mod/del to modify/delete is fine!
Paolo
Re: [PATCH 06/10] rust: add bindings for timer
Quoted 2 lines
Please keep
init_full();init()would be a version without some of the arguments (e.g. the TimerListGroup, or the attributes).
Done.
...
Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ scale: u32,
While at it, can you add constants for the scale, i.e.
pub const NS: u32 = bindings::SCALE_NS; pub const US: u32 = bindings::SCALE_US; pub const MS: u32 = bindings::SCALE_MS;? Using
Timer::NSis clear enough and removes the need to import from "bindings". At least in theory, bindings should not even have to be "pub" (or at least that's where the code should move towards).
Great! I realized this case, too.
Thanks,
Zhao
PATCH 7/10 rust/timer/hpet: define hpet_cfg
In reply to Zhao LiuDefine HPETFwEntry structure with the same memory layout as hpet_fw_entry in C.
Further, define the global hpet_cfg variable in Rust which is the same as the C version. This hpet_cfg variable in Rust will replace the C version one and allows both Rust code and C code to access it.
The Rust version of hpet_cfg is self-contained, avoiding unsafe access to C code.
---
Changes since RFC:
* hpet_fw_cfg doesn't need a BqlRefCell.
--- rust/Cargo.lock | 8 +++ rust/Cargo.toml | 1 + rust/hw/meson.build | 1 + rust/hw/timer/hpet/Cargo.toml | 14 +++++ rust/hw/timer/hpet/meson.build | 18 +++++++ rust/hw/timer/hpet/src/fw_cfg.rs | 87 ++++++++++++++++++++++++++++++++ rust/hw/timer/hpet/src/lib.rs | 13 +++++ rust/hw/timer/meson.build | 1 + rust/qemu-api/src/zeroable.rs | 6 ++- 9 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 rust/hw/timer/hpet/Cargo.toml create mode 100644 rust/hw/timer/hpet/meson.build create mode 100644 rust/hw/timer/hpet/src/fw_cfg.rs create mode 100644 rust/hw/timer/hpet/src/lib.rs create mode 100644 rust/hw/timer/meson.build
rust/Cargo.lock +8 -0
@@ -37,6 +37,14 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "hpet" +version = "0.1.0" +dependencies = [ + "qemu_api", + "qemu_api_macros", +] + [[package]] name = "itertools" version = "0.11.0"
rust/Cargo.toml +1 -0
@@ -4,6 +4,7 @@ members = [ "qemu-api-macros", "qemu-api", "hw/char/pl011", + "hw/timer/hpet", ] [workspace.lints.rust]
rust/hw/meson.build +1 -0
@@ -1 +1,2 @@ subdir('char') +subdir('timer')
rust/hw/timer/hpet/Cargo.toml new file +14 -0
@@ -0,0 +1,14 @@ +[package] +name = "hpet" +version = "0.1.0" +edition = "2021" +authors = ["Zhao Liu <[email protected]>"] +license = "GPL-2.0-or-later" +description = "IA-PC High Precision Event Timer emulation in Rust" + +[lib] +crate-type = ["staticlib"] + +[dependencies] +qemu_api = { path = "../../../qemu-api" } +qemu_api_macros = { path = "../../../qemu-api-macros" }
rust/hw/timer/hpet/meson.build new file +18 -0
@@ -0,0 +1,18 @@ +_libhpet_rs = static_library( + 'hpet', + files('src/lib.rs'), + override_options: ['rust_std=2021', 'build.rust_std=2021'], + rust_abi: 'rust', + dependencies: [ + qemu_api, + qemu_api_macros, + ], +) + +rust_devices_ss.add(when: 'CONFIG_X_HPET_RUST', if_true: [declare_dependency( + link_whole: [_libhpet_rs], + # Putting proc macro crates in `dependencies` is necessary for Meson to find + # them when compiling the root per-target static rust lib. + dependencies: [qemu_api_macros], + variables: {'crate': 'hpet'}, +)])
rust/hw/timer/hpet/src/fw_cfg.rs new file +87 -0
87 lines not on this page · raw message
rust/hw/timer/hpet/src/lib.rs new file +13 -0
@@ -0,0 +1,13 @@ +// Copyright (C) 2024 Intel Corporation. +// Author(s): Zhao Liu <[email protected]> +// SPDX-License-Identifier: GPL-2.0-or-later +// +// HPET QEMU Device Model +// +// This library implements a device model for the IA-PC HPET (High +// Precision Event Timers) device in QEMU. +// + +#![deny(unsafe_op_in_unsafe_fn)] + +pub mod fw_cfg;
rust/hw/timer/meson.build new file +1 -0
@@ -0,0 +1 @@ +subdir('hpet')
rust/qemu-api/src/zeroable.rs +4 -2
@@ -56,6 +56,7 @@ pub unsafe trait Zeroable: Default {/// ## Differences with `core::mem::zeroed` /// /// `const_zero` zeroes padding bits, while `core::mem::zeroed` doesn't +#[macro_export] macro_rules! const_zero {// This macro to produce a type-generic zero constant is taken from the // const_zero crate (v0.1.1):
@@ -77,10 +78,11 @@ union TypeAsBytes { }/// A wrapper to implement the `Zeroable` trait through the `const_zero` macro. +#[macro_export] macro_rules! impl_zeroable { ($type:ty) => { -unsafe impl Zeroable for $type { -const ZERO :Self =unsafe {const_zero! ($type ) }; +unsafe impl $crate::zeroable::Zeroable for $type { +const ZERO :Self =unsafe { $crate::const_zero!($type ) }; } }; }
Signature · 1 line
2.34.1
Re: [PATCH 07/10] rust/timer/hpet: define hpet_cfg
Review edition = "2021" · line 7
Quoted 4 lines · Zhao Liu
(quoted diff) +0
-0@@ -5,6 +5,7 @@ edition = "2021" authors = ["Zhao Liu <[email protected]>"] license = "GPL-2.0-or-later" description = "IA-PC High Precision Event Timer emulation in Rust"
Please add
rust-version = "1.63.0"
here.
rust/hw/timer/hpet/src/fw_cfg.rs line 52
Quoted 3 lines
rust/hw/timer/hpet/src/fw_cfg.rs new file +3
-0+// SAFETY: all accesses go through these methods, which guarantee +// that the accesses are protected by the BQL. +let fw_cfg =unsafe { &mut hpet_fw_cfg };
Clippy complains about references to static mut; use
let mut fw_cfg = unsafe { *addr_of_mut!(hpet_fw_cfg) };to make it happy; same below in update_hpet_cfg().
Paolo
Re: [PATCH 07/10] rust/timer/hpet: define hpet_cfg
Quoted 14 lines · Paolo Bonzini
Date: Wed, 29 Jan 2025 11:58:46 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 07/10]rust/timer/hpet: definehpet_cfgReview edition = "2021" · line 7
Quoted 4 lines · Zhao Liu
(quoted diff) +0
-0@@ -5,6 +5,7 @@ edition = "2021" authors = ["Zhao Liu <[email protected]>"] license = "GPL-2.0-or-later" description = "IA-PC High Precision Event Timer emulation in Rust"Please add
Quoted 3 lines
rust-version = "1.63.0"
here.
Done.
Quoted 10 lines
rust/hw/timer/hpet/src/fw_cfg.rs line 52
Quoted 3 lines
rust/hw/timer/hpet/src/fw_cfg.rs new file +3
-0+// SAFETY: all accesses go through these methods, which guarantee +// that the accesses are protected by the BQL. +let fw_cfg =unsafe { &mut hpet_fw_cfg };Clippy complains about references to static mut; use
let mut fw_cfg = unsafe { *addr_of_mut!(hpet_fw_cfg) };to make it happy; same below in
update_hpet_cfg().
Fixed. Thanks!!
Regards,
Zhao
PATCH 8/10 rust/timer/hpet: add basic HPET timer and HPETState
In reply to Zhao LiuAdd the HPETTimer and HPETState (HPET timer block), along with their basic methods and register definitions.
This is in preparation for supporting the QAPI interfaces.
Note, wrap all items in HPETState that may be changed in the callback called by C code into the BqlCell/BqlRefCell.
---
Changes since RFC:
* Don't wrap HPETState.flags in a BqlCell.
* Consolidate inmutable &self and QOM casting.
* Prefer timer iterator over loop.
* Use BqlRefCell<HPETTimer> directly to store timers in HPETState.
--- rust/hw/timer/hpet/src/hpet.rs | 633 +++++++++++++++++++++++++++++++++ rust/hw/timer/hpet/src/lib.rs | 1 + rust/wrapper.h | 1 + 3 files changed, 635 insertions(+) create mode 100644 rust/hw/timer/hpet/src/hpet.rs
rust/hw/timer/hpet/src/hpet.rs new file +633 -0
@@ -0,0 +1,633 @@ +// Copyright (C) 2024 Intel Corporation. +// Author(s): Zhao Liu <[email protected]> +// SPDX-License-Identifier: GPL-2.0-or-later + +#![allow(dead_code)] + +use core::ptr::{null_mut,NonNull }; + +use qemu_api::{ + bindings::{address_space_memory, address_space_stl_le,MemoryRegion ,SCALE_NS }, + cell::{BqlCell ,BqlRefCell }, + irq::InterruptSource , + memory::MEMTXATTRS_UNSPECIFIED , + prelude::*, + qom::ParentField , + sysbus::SysBusDevice , + timer::{QEMUTimer ,CLOCK_VIRTUAL }, +}; + +// Register space for each timer block. (HPET_BASE isn't defined here.) +const HPET_REG_SPACE_LEN :u64 =0x400 ;// 1024 bytes + +const HPET_MIN_TIMERS :usize =3 ;// Minimum recommended hardware implementation. +const HPET_MAX_TIMERS :usize =32 ;// Maximum timers in each timer block. + +// Flags that HPETState.flags supports. +const HPET_FLAG_MSI_SUPPORT_SHIFT :usize =0 ; + +const HPET_NUM_IRQ_ROUTES :usize =32 ; +const HPET_LEGACY_PIT_INT :u32 =0 ;// HPET_LEGACY_RTC_INT isn't defined here. +const RTC_ISA_IRQ :usize =8 ; + +const HPET_CLK_PERIOD :u64 =10 ;// 10 ns +const FS_PER_NS :u64 =1000000 ;// 1000000 femtoseconds == 1 ns + +// General Capabilities and ID Register +const HPET_CAP_REG :u64 =0x000 ; +// Revision ID (bits 0:7) +const HPET_CAP_REV_ID_VALUE :u64 =0x1 ;// Revision 1 is implemented (refer to v1.0a spec). +const HPET_CAP_REV_ID_SHIFT :usize =0 ; +// Number of Timers (bits 8:12) +const HPET_CAP_NUM_TIM_SHIFT :usize =8 ; +// Counter Size (bit 13) +const HPET_CAP_COUNT_SIZE_CAP_SHIFT :usize =13 ; +// LegacyReplacement Route Capable (bit 15) +const HPET_CAP_LEG_RT_CAP_SHIFT :usize =15 ; +// Vendor ID (bits 16:31) +const HPET_CAP_VENDER_ID_VALUE :u64 =0x8086 ; +const HPET_CAP_VENDER_ID_SHIFT :usize =16 ; +// Main Counter Tick Period (bits 32:63) +const HPET_CAP_CNT_CLK_PERIOD_SHIFT :usize =32 ; + +// General Configuration Register +const HPET_CFG_REG :u64 =0x010 ; +// Overall Enable (bit 0) +const HPET_CFG_ENABLE_SHIFT :usize =0 ; +// LegacyReplacement Route (bit 1) +const HPET_CFG_LEG_RT_SHIFT :usize =1 ; +// Other bits are reserved. +const HPET_CFG_WRITE_MASK :u64 =0x003 ; + +// General Interrupt Status Register +const HPET_INT_STATUS_REG :u64 =0x020 ; + +// Main Counter Value Register +const HPET_COUNTER_REG :u64 =0x0f0 ; + +// Timer N Configuration and Capability Register (masked by 0x18) +const HPET_TN_CFG_REG :u64 =0x000 ; +// bit 0, 7, and bits 16:31 are reserved. +// bit 4, 5, 15, and bits 32:64 are read-only. +const HPET_TN_CFG_WRITE_MASK :u64 =0x7f4e ; +// Timer N Interrupt Type (bit 1) +const HPET_TN_CFG_INT_TYPE_SHIFT :usize =1 ; +// Timer N Interrupt Enable (bit 2) +const HPET_TN_CFG_INT_ENABLE_SHIFT :usize =2 ; +// Timer N Type (Periodic enabled or not, bit 3) +const HPET_TN_CFG_PERIODIC_SHIFT :usize =3 ; +// Timer N Periodic Interrupt Capable (support Periodic or not, bit 4) +const HPET_TN_CFG_PERIODIC_CAP_SHIFT :usize =4 ; +// Timer N Size (timer size is 64-bits or 32 bits, bit 5) +const HPET_TN_CFG_SIZE_CAP_SHIFT :usize =5 ; +// Timer N Value Set (bit 6) +const HPET_TN_CFG_SETVAL_SHIFT :usize =6 ; +// Timer N 32-bit Mode (bit 8) +const HPET_TN_CFG_32BIT_SHIFT :usize =8 ; +// Timer N Interrupt Rout (bits 9:13) +const HPET_TN_CFG_INT_ROUTE_MASK :u64 =0x3e00 ; +const HPET_TN_CFG_INT_ROUTE_SHIFT :usize =9 ; +// Timer N FSB Interrupt Enable (bit 14) +const HPET_TN_CFG_FSB_ENABLE_SHIFT :usize =14 ; +// Timer N FSB Interrupt Delivery (bit 15) +const HPET_TN_CFG_FSB_CAP_SHIFT :usize =15 ; +// Timer N Interrupt Routing Capability (bits 32:63) +const HPET_TN_CFG_INT_ROUTE_CAP_SHIFT :usize =32 ; + +// Timer N Comparator Value Register (masked by 0x18) +const HPET_TN_CMP_REG :u64 =0x008 ; + +// Timer N FSB Interrupt Route Register (masked by 0x18) +const HPET_TN_FSB_ROUTE_REG :u64 =0x010 ; + +fn hpet_next_wrap (cur_tick:u64 ) ->u64 { + (cur_tick |0xffffffff ) +1 +} + +fn hpet_time_after (a:u64 , b:u64 ) ->bool { + ((b - a)as i64 ) <0 +} + +fn ticks_to_ns (value:u64 ) ->u64 { + value *HPET_CLK_PERIOD +} + +fn ns_to_ticks (value:u64 ) ->u64 { + value /HPET_CLK_PERIOD +} + +// Avoid touching the bits that cannot be written. +fn hpet_fixup_reg (new:u64 , old:u64 , mask:u64 ) ->u64 { + (new & mask) | (old & !mask) +} + +fn activating_bit (old:u64 , new:u64 , shift:usize ) ->bool { +let mask:u64 =1 << shift; + (old & mask ==0 ) && (new & mask !=0 ) +} + +fn deactivating_bit (old:u64 , new:u64 , shift:usize ) ->bool { +let mask:u64 =1 << shift; + (old & mask !=0 ) && (new & mask ==0 ) +} + +fn timer_handler (timer_cell: &BqlRefCell <HPETTimer >) { + timer_cell.borrow_mut ().callback () +} + +/// HPET Timer Abstraction +#[repr( C )] +#[derive( Debug , Default , qemu_api_macros::offsets)] +pub struct HPETTimer { +/// timer N index within the timer block (HPETState) +#[doc(alias = "tn" )] + index:usize , + qemu_timer:Option <Box <QEMUTimer >>, +/// timer block abstraction containing this timer + state:Option <NonNull <HPETState >>, + +/// Memory-mapped, software visible timer registers + +/// Timer N Configuration and Capability Register + config:u64 , +/// Timer N Comparator Value Register + cmp:u64 , +/// Timer N FSB Interrupt Route Register + fsb:u64 , + +/// Hidden register state + +/// comparator (extended to counter width) + cmp64:u64 , +/// Last value written to comparator + period:u64 , +/// timer pop will indicate wrap for one-shot 32-bit +/// mode. Next pop will be actual timer expiration. + wrap_flag:u8 , +/// last value armed, to avoid timer storms + last:u64 , +} + +impl HPETTimer { +fn init (&mut self, index:usize , state_ptr: *mut HPETState ) -> &mut Self { + *self =HPETTimer ::default (); + self.index = index; + self.state =NonNull ::new (state_ptr); + self + } + +fn init_timer_with_state (&mut self) { + self.qemu_timer =Some (Box ::new ({ +let mut t =QEMUTimer ::new (); + t.timer_init_full ( +None , +CLOCK_VIRTUAL .id, +SCALE_NS , +0 , + timer_handler, + &self.get_state ().timers[self.index], + ); + t + })); + } + +fn get_state (&self) -> &HPETState { +// SAFETY: +// the pointer is convertible to a reference +unsafe { self.state.unwrap ().as_ref () } + } + +fn is_int_active (&self) ->bool { + self.get_state ().is_timer_int_active (self.index) + } + +fn is_fsb_route_enabled (&self) ->bool { + self.config &1 <<HPET_TN_CFG_FSB_ENABLE_SHIFT !=0 + } + +fn is_periodic (&self) ->bool { + self.config &1 <<HPET_TN_CFG_PERIODIC_SHIFT !=0 + } + +fn is_int_enabled (&self) ->bool { + self.config &1 <<HPET_TN_CFG_INT_ENABLE_SHIFT !=0 + } + +fn is_32bit_mod (&self) ->bool { + self.config &1 <<HPET_TN_CFG_32BIT_SHIFT !=0 + } + +fn is_valset_enabled (&self) ->bool { + self.config &1 <<HPET_TN_CFG_SETVAL_SHIFT !=0 + } + +fn clear_valset (&mut self) { + self.config &= !(1 <<HPET_TN_CFG_SETVAL_SHIFT ); + } + +/// True if timer interrupt is level triggered; otherwise, edge triggered. +fn is_int_level_triggered (&self) ->bool { + self.config &1 <<HPET_TN_CFG_INT_TYPE_SHIFT !=0 + } + +/// calculate next value of the general counter that matches the +/// target (either entirely, or the low 32-bit only depending on +/// the timer mode). +fn calculate_cmp64 (&self, cur_tick:u64 , target:u64 ) ->u64 { +if self.is_32bit_mod () { +let mut result:u64 = cur_tick.deposit (0 ,32 , target); +if result < cur_tick { + result +=0x100000000 ; + } + result + }else { + target + } + } + +fn get_individual_route (&self) ->usize { + ((self.config &HPET_TN_CFG_INT_ROUTE_MASK ) >>HPET_TN_CFG_INT_ROUTE_SHIFT )as usize + } + +fn get_int_route (&self) ->usize { +if self.index <=1 && self.get_state ().is_legacy_mode () { +// If LegacyReplacement Route bit is set, HPET specification requires +// timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC, +// timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC. +// +// If the LegacyReplacement Route bit is set, the individual routing +// bits for timers 0 and 1 (APIC or FSB) will have no impact. +// +// FIXME: Consider I/O APIC case. +if self.index ==0 { +0 + }else { +RTC_ISA_IRQ + } + }else { +// (If the LegacyReplacement Route bit is set) Timer 2-n will be +// routed as per the routing in the timer n config registers. +// ... +// If the LegacyReplacement Route bit is not set, the individual +// routing bits for each of the timers are used. + self.get_individual_route () + } + } + +fn set_irq (&mut self, set:bool ) { +let route = self.get_int_route (); + +if set && self.is_int_enabled () && self.get_state ().is_hpet_enabled () { +if self.is_fsb_route_enabled () { +// SAFETY: +// the parameters are valid. +unsafe { +address_space_stl_le ( + &mut address_space_memory, + self.fsb >>32 ,// Timer N FSB int addr + self.fsbas u32 ,// Timer N FSB int value, truncate! +MEMTXATTRS_UNSPECIFIED , +null_mut (), + ); + } + }else if self.is_int_level_triggered () { + self.get_state ().irqs[route].raise (); + }else { + self.get_state ().irqs[route].pulse (); + } + }else if !self.is_fsb_route_enabled () { + self.get_state ().irqs[route].lower (); + } + } + +fn update_irq (&mut self, set:bool ) { +// If Timer N Interrupt Enable bit is 0, "the timer will +// still operate and generate appropriate status bits, but +// will not cause an interrupt" + self.get_state () + .update_int_status (self.indexas u32 , set && self.is_int_level_triggered ()); + self.set_irq (set); + } + +fn arm_timer (&mut self, tick:u64 ) { +let mut ns = self.get_state ().get_ns (tick); + +// Clamp period to reasonable min value (1 us) +if self.is_periodic () && ns - self.last <1000 { + ns = self.last +1000 ; + } + + self.last = ns; +if let Some (timer) = self.qemu_timer.as_mut () { + timer.timer_mod (self.last); + } + } + +fn set_timer (&mut self) { +let cur_tick:u64 = self.get_state ().get_ticks (); + + self.wrap_flag =0 ; + self.cmp64 = self.calculate_cmp64 (cur_tick, self.cmp); +if self.is_32bit_mod () { +// HPET spec says in one-shot 32-bit mode, generate an interrupt when +// counter wraps in addition to an interrupt with comparator match. +if !self.is_periodic () && self.cmp64 >hpet_next_wrap (cur_tick) { + self.wrap_flag =1 ; + self.arm_timer (hpet_next_wrap (cur_tick)); +return ; + } + } + self.arm_timer (self.cmp64); + } + +fn del_timer (&mut self) { +if let Some (timer) = self.qemu_timer.take () { + std::mem::drop (timer); + } + +if self.is_int_active () { +// For level-triggered interrupt, this leaves interrupt status +// register set but lowers irq. + self.update_irq (true ); + } + } + +// Configuration and Capability Register +fn set_tn_cfg_reg (&mut self, shift:u32 , len:u32 , val:u64 ) { +// TODO: Add trace point - trace_hpet_ram_write_tn_cfg(addr & 4) +let old_val:u64 = self.config; +let mut new_val:u64 = old_val.deposit (shift, len, val); + new_val =hpet_fixup_reg (new_val, old_val,HPET_TN_CFG_WRITE_MASK ); + +// Switch level-type interrupt to edge-type. +if deactivating_bit (old_val, new_val,HPET_TN_CFG_INT_TYPE_SHIFT ) { +// Do this before changing timer.config; otherwise, if +// HPET_TN_FSB is set, update_irq will not lower the qemu_irq. + self.update_irq (false ); + } + + self.config = new_val; + +if activating_bit (old_val, new_val,HPET_TN_CFG_INT_ENABLE_SHIFT ) && self.is_int_active () { + self.update_irq (true ); + } + +if self.is_32bit_mod () { + self.cmp = self.cmpas u32 as u64 ;// truncate! + self.period = self.periodas u32 as u64 ;// truncate! + } + +if self.get_state ().is_hpet_enabled () { + self.set_timer (); + } + } + +/// Comparator Value Register +fn set_tn_cmp_reg (&mut self, shift:u32 , len:u32 , val:u64 ) { +let mut length = len; +let mut value = val; + +// TODO: Add trace point - trace_hpet_ram_write_tn_cmp(addr & 4) +if self.is_32bit_mod () { +// High 32-bits are zero, leave them untouched. +if shift !=0 { +// TODO: Add trace point - trace_hpet_ram_write_invalid_tn_cmp() +return ; + } + length =64 ; + value = valueas u32 as u64 ;// truncate! + } + +if !self.is_periodic () || self.is_valset_enabled () { + self.cmp = self.cmp.deposit (shift, length, value); + } + +if self.is_periodic () { + self.period = self.period.deposit (shift, length, value); + } + + self.clear_valset (); +if self.get_state ().is_hpet_enabled () { + self.set_timer (); + } + } + +/// FSB Interrupt Route Register +fn set_tn_fsb_route_reg (&mut self, shift:u32 , len:u32 , val:u64 ) { + self.fsb = self.fsb.deposit (shift, len, val); + } + +fn reset (&mut self) { + self.del_timer (); + self.cmp = u64::MAX ;// Comparator Match Registers reset to all 1's. + self.config =1 <<HPET_TN_CFG_PERIODIC_CAP_SHIFT |1 <<HPET_TN_CFG_SIZE_CAP_SHIFT ; +if self.get_state ().has_msi_flag () { + self.config |=1 <<HPET_TN_CFG_FSB_CAP_SHIFT ; + } +// advertise availability of ioapic int + self.config |= (self.get_state ().int_route_capas u64 ) <<HPET_TN_CFG_INT_ROUTE_CAP_SHIFT ; + self.period =0 ; + self.wrap_flag =0 ; + } + +/// timer expiration callback +fn callback (&mut self) { +let period:u64 = self.period; +let cur_tick:u64 = self.get_state ().get_ticks (); + +if self.is_periodic () && period !=0 { +while hpet_time_after (cur_tick, self.cmp64) { + self.cmp64 += period; + } +if self.is_32bit_mod () { + self.cmp = self.cmp64as u32 as u64 ; + }else { + self.cmp = self.cmp64; + } + self.arm_timer (self.cmp64); + }else if self.wrap_flag !=0 { + self.wrap_flag =0 ; + self.arm_timer (self.cmp64); + } + self.update_irq (true ); + } +} + +/// HPET Event Timer Block Abstraction +/// Note: Wrap all items that may be changed in the callback called by C +/// into the BqlCell/BqlRefCell. +#[repr( C )] +#[derive(qemu_api_macros::offsets)] +pub struct HPETState { + parent_obj:ParentField <SysBusDevice >, + iomem:MemoryRegion , + +/// HPET block Registers: Memory-mapped, software visible registers + +/// General Capabilities and ID Register + capability:BqlCell <u64 >, +/// General Configuration Register + config:BqlCell <u64 >, +/// General Interrupt Status Register +#[doc(alias = "isr" )] + int_status:BqlCell <u64 >, +/// Main Counter Value Register +#[doc(alias = "hpet_counter" )] + counter:BqlCell <u64 >, + +/// Internal state + +/// Capabilities that QEMU HPET supports. +/// bit 0: MSI (or FSB) support. + flags:u32 , + +/// Offset of main counter relative to qemu clock. + hpet_offset:BqlCell <u64 >, + hpet_offset_saved:bool , + + irqs: [InterruptSource ;HPET_NUM_IRQ_ROUTES ], + rtc_irq_level:BqlCell <u32 >, + pit_enabled:InterruptSource , + +/// Interrupt Routing Capability. +/// This field indicates to which interrupts in the I/O (x) APIC +/// the timers' interrupt can be routed, and is encoded in the +/// bits 32:64 of timer N's config register: +#[doc(alias = "intcap" )] + int_route_cap:u32 , + +/// HPET timer array managed by this timer block. +#[doc(alias = "timer" )] + timers: [BqlRefCell <HPETTimer >;HPET_MAX_TIMERS ], + num_timers:BqlCell <usize >, + +/// Instance id (HPET timer block ID). + hpet_id:BqlCell <usize >, +} + +impl HPETState { +fn has_msi_flag (&self) ->bool { + self.flags &1 <<HPET_FLAG_MSI_SUPPORT_SHIFT !=0 + } + +fn is_legacy_mode (&self) ->bool { + self.config.get () &1 <<HPET_CFG_LEG_RT_SHIFT !=0 + } + +fn is_hpet_enabled (&self) ->bool { + self.config.get () &1 <<HPET_CFG_ENABLE_SHIFT !=0 + } + +fn is_timer_int_active (&self, index:usize ) ->bool { + self.int_status.get () &1 << index !=0 + } + +fn get_ticks (&self) ->u64 { +ns_to_ticks (CLOCK_VIRTUAL .get_ns () + self.hpet_offset.get ()) + } + +fn get_ns (&self, tick:u64 ) ->u64 { +ticks_to_ns (tick) - self.hpet_offset.get () + } + +fn handle_legacy_irq (&self, irq:u32 , level:u32 ) { +if irq ==HPET_LEGACY_PIT_INT { +if !self.is_legacy_mode () { + self.irqs[0 ].set (level !=0 ); + } + }else { + self.rtc_irq_level.set (level); +if !self.is_legacy_mode () { + self.irqs[RTC_ISA_IRQ ].set (level !=0 ); + } + } + } + +fn init_timer (&self) { +let raw_ptr: *mut HPETState = selfas *const HPETState as *mut HPETState ; + +for (index, timer)in self.timers.iter ().enumerate () { + timer + .borrow_mut () + .init (index, raw_ptr) + .init_timer_with_state (); + } + } + +fn update_int_status (&self, index:u32 , level:bool ) { + self.int_status + .set (self.int_status.get ().deposit (index,1 , levelas u64 )); + } + +/// General Configuration Register +fn set_cfg_reg (&self, shift:u32 , len:u32 , val:u64 ) { +let old_val = self.config.get (); +let mut new_val = old_val.deposit (shift, len, val); + + new_val =hpet_fixup_reg (new_val, old_val,HPET_CFG_WRITE_MASK ); + self.config.set (new_val); + +if activating_bit (old_val, new_val,HPET_CFG_ENABLE_SHIFT ) { +// Enable main counter and interrupt generation. + self.hpet_offset + .set (ticks_to_ns (self.counter.get ()) -CLOCK_VIRTUAL .get_ns ()); + +for timerin self.timers.iter ().take (self.num_timers.get ()) { +let mut t = timer.borrow_mut (); + +if t.is_int_enabled () && t.is_int_active () { + t.update_irq (true ); + } + t.set_timer (); + } + }else if deactivating_bit (old_val, new_val,HPET_CFG_ENABLE_SHIFT ) { +// Halt main counter and disable interrupt generation. + self.counter.set (self.get_ticks ()); + +for timerin self.timers.iter ().take (self.num_timers.get ()) { + timer.borrow_mut ().del_timer (); + } + } + +// i8254 and RTC output pins are disabled when HPET is in legacy mode +if activating_bit (old_val, new_val,HPET_CFG_LEG_RT_SHIFT ) { + self.pit_enabled.set (false ); + self.irqs[0 ].lower (); + self.irqs[RTC_ISA_IRQ ].lower (); + }else if deactivating_bit (old_val, new_val,HPET_CFG_LEG_RT_SHIFT ) { +// TODO: Add irq binding: qemu_irq_lower(s->irqs[0]) + self.irqs[0 ].lower (); + self.pit_enabled.set (true ); + self.irqs[RTC_ISA_IRQ ].set (self.rtc_irq_level.get () !=0 ); + } + } + +/// General Interrupt Status Register: Read/Write Clear +fn set_int_status_reg (&self, shift:u32 , _len:u32 , val:u64 ) { +let new_val = val << shift; +let cleared = new_val & self.int_status.get (); + +for (index, timer)in self.timers.iter ().take (self.num_timers.get ()).enumerate () { +if cleared &1 << index !=0 { + timer.borrow_mut ().update_irq (false ); + } + } + } + +/// Main Counter Value Register +fn set_counter_reg (&self, shift:u32 , len:u32 , val:u64 ) { +if self.is_hpet_enabled () { +// TODO: Add trace point - +// trace_hpet_ram_write_counter_write_while_enabled() +// +// HPET spec says that writes to this register should only be +// done while the counter is halted. So this is an undefined +// behavior. There's no need to forbid it, but when HPET is +// enabled, the changed counter value will not affect the +// tick count (i.e., the previously calculated offset will +// not be changed as well). + } + self.counter + .set (self.counter.get ().deposit (shift, len, val)); + } +}
rust/hw/timer/hpet/src/lib.rs +1 -0
@@ -11,3 +11,4 @@#![deny(unsafe_op_in_unsafe_fn)] pub mod fw_cfg; +pub mod hpet;
rust/wrapper.h +1 -0
@@ -64,3 +64,4 @@ typedef enum memory_order {#include "chardev/char-serial.h" #include "exec/memattrs.h" #include "qemu/timer.h" +#include "exec/address-spaces.h"
Signature · 1 line
2.34.1
Re: [PATCH 08/10] rust/timer/hpet: add basic HPET timer and HPETState
rust/hw/timer/hpet/src/hpet.rs line 21
Quoted 2 lines · Zhao Liu
rust/hw/timer/hpet/src/hpet.rs new file +2
-0+// Register space for each timer block. (HPET_BASE isn't defined here.) +const HPET_REG_SPACE_LEN :u64 =0x400 ;// 1024 bytes
Use doc comments "///"...
rust/hw/timer/hpet/src/hpet.rs line 101
Quoted 2 lines
rust/hw/timer/hpet/src/hpet.rs new file +2
-0+// Timer N FSB Interrupt Route Register (masked by 0x18) +const HPET_TN_FSB_ROUTE_REG :u64 =0x010 ;
... all the way to here.
rust/hw/timer/hpet/src/hpet.rs line 149
Quoted 12 lines
rust/hw/timer/hpet/src/hpet.rs new file +12
-0+/// HPET Timer Abstraction +#[repr( C )] +#[derive( Debug , Default , qemu_api_macros::offsets)] +pub struct HPETTimer { +/// timer N index within the timer block (HPETState) +#[doc(alias = "tn" )] + index:usize , + qemu_timer:Option <Box <QEMUTimer >>, +/// timer block abstraction containing this timer + state:Option <NonNull <HPETState >>, + +/// Memory-mapped, software visible timer registers
These "headings" cannot be doc comments, because they would be attached to the field after. Same below:
- /// Hidden register state
+ // Hidden register state
- /// HPET block Registers: Memory-mapped, software visible registers
+ // HPET block Registers: Memory-mapped, software visible registers
- /// Internal state
+ // Internal state
rust/hw/timer/hpet/src/hpet.rs line 198
Quoted 5 lines
rust/hw/timer/hpet/src/hpet.rs new file +5
-0+fn get_state (&self) -> &HPETState { +// SAFETY: +// the pointer is convertible to a reference +unsafe { self.state.unwrap ().as_ref () } + }
Note for the future: I looked briefly into why a NonNull<> is needed and the reasons are two. First, the offset_of! replacement does not support lifetime parameters. Second, it's messy to pass a &HPETState to the &HPETTimer, because the HPETTimer still has to be written into the HPETState. This may be worth revisiting when QEMU starts using pinned-init is added, but for now is self-contained.
rust/hw/timer/hpet/src/hpet.rs line 323
Quoted 3 lines
rust/hw/timer/hpet/src/hpet.rs new file +3
-0+if let Some (timer) = self.qemu_timer.as_mut () { + timer.timer_mod (self.last); + }
I think this can be just "timer.unwrap().timer_mod(self.last)" (not sure if it needs an as_ref() too).
Paolo
Re: [PATCH 08/10] rust/timer/hpet: add basic HPET timer and HPETState
Quoted 7 lines · Paolo Bonzini
Date: Wed, 29 Jan 2025 11:57:18 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 08/10]rust/timer/hpet: add basic HPET timer and
HPETState
Quoted 5 lines
rust/hw/timer/hpet/src/hpet.rs line 21
Quoted 2 lines · Zhao Liu
rust/hw/timer/hpet/src/hpet.rs new file +2
-0+// Register space for each timer block. (HPET_BASE isn't defined here.) +const HPET_REG_SPACE_LEN :u64 =0x400 ;// 1024 bytes Use doc comments "///"...
OK,
Quoted 4 lines
rust/hw/timer/hpet/src/hpet.rs line 101
Quoted 2 lines
rust/hw/timer/hpet/src/hpet.rs new file +2
-0+// Timer N FSB Interrupt Route Register (masked by 0x18) +const HPET_TN_FSB_ROUTE_REG :u64 =0x010 ;... all the way to here.
Done.
Quoted 16 lines
rust/hw/timer/hpet/src/hpet.rs line 149
Quoted 12 lines
rust/hw/timer/hpet/src/hpet.rs new file +12
-0+/// HPET Timer Abstraction +#[repr( C )] +#[derive( Debug , Default , qemu_api_macros::offsets)] +pub struct HPETTimer { +/// timer N index within the timer block (HPETState) +#[doc(alias = "tn" )] + index:usize , + qemu_timer:Option <Box <QEMUTimer >>, +/// timer block abstraction containing this timer + state:Option <NonNull <HPETState >>, + +/// Memory-mapped, software visible timer registers These "headings" cannot be doc comments, because they would be attached to the field after. Same below:
Review
Quoted 8 lines
(quoted diff) +3
-3- /// Hidden register state + // Hidden register state - /// HPET block Registers: Memory-mapped, software visible registers + // HPET block Registers: Memory-mapped, software visible registers - /// Internal state + // Internal state
Fixed. Thanks!
Quoted 11 lines
rust/hw/timer/hpet/src/hpet.rs line 198
Quoted 5 lines
rust/hw/timer/hpet/src/hpet.rs new file +5
-0+fn get_state (&self) -> &HPETState { +// SAFETY: +// the pointer is convertible to a reference +unsafe { self.state.unwrap ().as_ref () } + }Note for the future: I looked briefly into why a NonNull<> is needed and the reasons are two. First, the
offset_of! replacement does not support lifetime parameters. Second, it's messy to pass a &HPETState to the &HPETTimer, because the HPETTimer still has to be written into the HPETState.
Yes, and the second use would be a common case in practice. So maybe we need some wrapper on this, to provide someting like HPETTimer::get_state(), which returns a reference of the parent structure.
In addition, for underlying library, NonNull<> is nice thinks to its low overhead. But for device implementation, I'm not sure if it's the good choice. Rust uses RefCell<Weak<>> (for QEMU, it could be BqlRefCell<Weak<>>) to handle this cycle reference case.
As I mentioned in RFC,
The device instance is not created in Rust side, and there's only init() method to initialize created device instance. This way, it's hard to be compatible with the pattern of creating weak references (at least I failed).
To address this issue, the method from or from_raw seems necessary, like Owned::from, but it's tricky... So I don't have a good idea to replace NonNull<> yet!
Quoted 10 lines
This may be worth revisiting when QEMU starts using pinned-init is added, but for now is self-contained.
rust/hw/timer/hpet/src/hpet.rs line 323
Quoted 3 lines
rust/hw/timer/hpet/src/hpet.rs new file +3
-0+if let Some (timer) = self.qemu_timer.as_mut () { + timer.timer_mod (self.last); + }I think this can be just "timer.unwrap().timer_mod(self.last)" (not sure if it needs an
as_ref()too).
Yes, it needs as_ref(). A direct unwrap() consumes the Some() since inter type of qemu_timer (Box<Timer>) is non-Copy.
Another example is
fn get_state(&self) -> &HPETState {
// SAFETY:
// the pointer is convertible to a reference
unsafe { self.state.unwrap().as_ref() }
}Because the state (Option<NonNull<HPETState>>) has Copy so that this case doesn't need as_ref before unwrap.
Thanks,
Zhao
PATCH 9/10 rust/timer/hpet: add qom and qdev APIs support
In reply to Zhao LiuImplement QOM & QAPI support for HPET device.
---
Changes since RFC:
* Merge qdev.rs to hpet.rs.
* Apply memory and Resettable bindings.
* Consolidate inmutable &self and QOM casting.
* Prefer timer iterator over loop.
* Move init_mmio() and init_irq() to post_init().
--- rust/hw/timer/hpet/src/fw_cfg.rs | 2 - rust/hw/timer/hpet/src/hpet.rs | 283 ++++++++++++++++++++++++++++++- rust/hw/timer/hpet/src/lib.rs | 4 + 3 files changed, 279 insertions(+), 10 deletions(-)
rust/hw/timer/hpet/src/fw_cfg.rs +0 -2
@@ -2,8 +2,6 @@// Author(s): Zhao Liu <[email protected]> // SPDX-License-Identifier: GPL-2.0-or-later -#![allow(dead_code)] -use qemu_api::{cell::bql_locked, impl_zeroable, zeroable::Zeroable };// Each HPETState represents a Event Timer Block. The v1 spec supports
rust/hw/timer/hpet/src/hpet.rs +275 -8
313 lines not on this page · raw message
rust/hw/timer/hpet/src/lib.rs +4 -0
@@ -10,5 +10,9 @@#![deny(unsafe_op_in_unsafe_fn)] +use qemu_api::c_str; +pub mod fw_cfg;pub mod hpet; + +pub const TYPE_HPET : &::std::ffi::CStr =c_str! ("hpet" );
Signature · 1 line
2.34.1
Re: [PATCH 09/10] rust/timer/hpet: add qom and qdev APIs support
Quoted 1 line · Zhao Liu
fn read(&mut self, addr: hwaddr, _size: u32) -> u64 {This can be &self.
rust/hw/timer/hpet/src/hpet.rs fn callback(&mut self) { · line 468
Quoted 4 lines
let shift: u64 = (addr & 4) * 8;rust/hw/timer/hpet/src/hpet.rs +2
-0+match addr { +HPET_TN_CFG_REG => self.config >> shift,// including interrupt capabilities
This needs to be "match addr & !4".
Quoted 6 lines
rust/hw/timer/hpet/src/hpet.rs +6
-0+ HPET_TN_CMP_REG => self.cmp >> shift, // comparator register + HPET_TN_FSB_ROUTE_REG => self.fsb >> shift, + _ => { +// TODO: Add trace point - trace_hpet_ram_read_invalid() +// Reserved. +0
rust/hw/timer/hpet/src/hpet.rs fn callback(&mut self) { · line 484
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+ } + } + } + +fn write (&mut self, addr:hwaddr , value:u64 , size:u32 ) { +let shift = ((addr &4 ) *8 )as u32 ; +let len = std::cmp::min (size *8 ,64 - shift); + +match addr { +HPET_TN_CFG_REG => self.set_tn_cfg_reg (shift, len, value),
Likewise.
rust/hw/timer/hpet/src/hpet.rs fn set_counter_reg(&self, shift: u32, len: u32, val: u64) {
Quoted 6 lines
rust/hw/timer/hpet/src/hpet.rs +6
-0+ fn write(&self, addr: hwaddr, value: u64, size: u32) { + let shift = ((addr & 4) * 8) as u32; + let len = std::cmp::min(size * 8, 64 - shift); + + // TODO: Add trace point - trace_hpet_ram_write(addr, value) + if (0x100..=0x3ff).contains(&addr
) {
Review
Quoted 2 lines
(quoted diff) +2
-0+ match self.timer_and_addr(addr) { + None => return, // Reserved.
Clippy complains about an unnecessary return, just replace it with "()".
Quoted 11 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+ Some((timer, addr)) => timer.borrow_mut().write(addr, value, size), + } + +fn reset_hold (&self, _type:ResetType ) { +let sbd = self.upcast ::<SysBusDevice >(); + +for timerin self.timers.iter ().take (self.num_timers.get ()) { + timer.borrow_mut ().reset (); + } +
rust/hw/timer/hpet/src/hpet.rs fn set_counter_reg(&self, shift: u32, len: u32, val: u64) { · line 747
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+ self.counter.set (0 ); + self.config.set (0 ); + self.pit_enabled.set (true ); + self.hpet_offset.set (0 ); + +HPETFwConfig ::update_hpet_cfg ( + self.hpet_id.get (), +Some (self.capability.get ()as u32 ), +Some ((*sbd).mmio[0 ].addr), + );
This can be simply sbd.mmio[0].addr, without the (*...).
Also, you can change update_hpet_cfg to take arguments without the Option<> around them.
rust/hw/timer/hpet/src/hpet.rs fn set_counter_reg(&self, shift: u32, len: u32, val: u64) { · line 837
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+// TODO: add OBJECT_DECLARE_SIMPLE_TYPE. +#[repr( C )] +pub struct HPETClass { + parent_class: <SysBusDevice as ObjectType >::Class , +} + +unsafe impl ObjectType for HPETState { +type Class =HPETClass ; +const TYPE_NAME : &'staticCStr =crate ::TYPE_HPET ; +}
No need for HPETClass (and for ClassInitImpl<HPETClass>), just do
unsafe impl ObjectType for HPETState {
type Class = <SysBusDevice as ObjectType>::Class;
const TYPE_NAME: &'static CStr = crate::TYPE_HPET;
}which is indeed more similar to OBJECT_DECLARE_SIMPLE_TYPE().
Paolo
Re: [PATCH 09/10] rust/timer/hpet: add qom and qdev APIs support
Quoted 10 lines · Paolo Bonzini
Date: Wed, 29 Jan 2025 11:58:14 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 09/10]rust/timer/hpet: add qom and qdev APIs supportQuoted 1 line · Zhao Liu
fn read(&mut self, addr: hwaddr, _size: u32) -> u64 {This can be &self.
Done.
Quoted 6 lines
rust/hw/timer/hpet/src/hpet.rs fn callback(&mut self) { · line 468
Quoted 4 lines
let shift: u64 = (addr & 4) * 8;rust/hw/timer/hpet/src/hpet.rs +2
-0+match addr { +HPET_TN_CFG_REG => self.config >> shift,// including interrupt capabilities This needs to be "match addr & !4".
I understand it's not necessary:
In timer_and_addr(), I've masked the address with 0x18.
fn timer_and_addr(&self, addr: hwaddr) -> Option<(&BqlRefCell<HPETTimer>, hwaddr)> {
let timer_id: usize = ((addr - 0x100) / 0x20) as usize;
if timer_id > self.num_timers.get() {
None
} else {
Some((&self.timers[timer_id], addr & 0x18))
}
}And in HPETState::read(), I pass the masked address to HPETTimer::read():
fn read(&self, addr: hwaddr, size: u32) -> u64 {
let shift: u64 = (addr & 4) * 8;
if (0x100..=0x3ff).contains(&addr) {
match self.timer_and_addr(addr) {
None => 0, // Reserved,
Some((timer, addr)) => timer.borrow_mut().read(addr, size),
}
}Ah, the addr variable in Some() is misleading, and I should use anothor name like tn_addr.
0x18 is a subset of !0x4, so the bitwise & operation with !0x4 can be omitted (I understand that this is why you also omitted this in the C side in the commit c2366567378dd :-) ).
But match addr & !4 is also reasonable, which explicitly emphasize & !4 makes the code clearer. I would do this.
Quoted 10 lines
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+ HPET_TN_CMP_REG => self.cmp >> shift, // comparator register + HPET_TN_FSB_ROUTE_REG => self.fsb >> shift, + _ => { +// TODO: Add trace point - trace_hpet_ram_read_invalid() +// Reserved. +0 + } + } + } +
Quoted 8 lines
rust/hw/timer/hpet/src/hpet.rs fn callback(&mut self) { · line 484
Quoted 6 lines
rust/hw/timer/hpet/src/hpet.rs +6
-0+fn write (&mut self, addr:hwaddr , value:u64 , size:u32 ) { +let shift = ((addr &4 ) *8 )as u32 ; +let len = std::cmp::min (size *8 ,64 - shift); + +match addr { +HPET_TN_CFG_REG => self.set_tn_cfg_reg (shift, len, value),Likewise.
Done. Thanks!
Quoted 11 lines
rust/hw/timer/hpet/src/hpet.rs fn set_counter_reg(&self, shift: u32, len: u32, val: u64) {
Quoted 6 lines
rust/hw/timer/hpet/src/hpet.rs +6
-0+ fn write(&self, addr: hwaddr, value: u64, size: u32) { + let shift = ((addr & 4) * 8) as u32; + let len = std::cmp::min(size * 8, 64 - shift); + + // TODO: Add trace point - trace_hpet_ram_write(addr, value) + if (0x100..=0x3ff).contains(&addr) {
Review
Quoted 2 lines
(quoted diff) +2
-0+ match self.timer_and_addr(addr) { + None => return, // Reserved.Clippy complains about an unnecessary return, just replace it with "()".
Fixed.
Quoted 24 lines
Quoted 11 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+ Some((timer, addr)) => timer.borrow_mut().write(addr, value, size), + } + +fn reset_hold (&self, _type:ResetType ) { +let sbd = self.upcast ::<SysBusDevice >(); + +for timerin self.timers.iter ().take (self.num_timers.get ()) { + timer.borrow_mut ().reset (); + } +rust/hw/timer/hpet/src/hpet.rs fn set_counter_reg(&self, shift: u32, len: u32, val: u64) { · line 747
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+ self.counter.set (0 ); + self.config.set (0 ); + self.pit_enabled.set (true ); + self.hpet_offset.set (0 ); + +HPETFwConfig ::update_hpet_cfg ( + self.hpet_id.get (), +Some (self.capability.get ()as u32 ), +Some ((*sbd).mmio[0 ].addr), + );This can be simply sbd.mmio[0].addr, without the (*...).
Quoted 2 lines
Also, you can change
update_hpet_cfgto take arguments without the Option<> around them.
Good idea! Done.
Quoted 13 lines
rust/hw/timer/hpet/src/hpet.rs fn set_counter_reg(&self, shift: u32, len: u32, val: u64) { · line 837
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +10
-0+// TODO: add OBJECT_DECLARE_SIMPLE_TYPE. +#[repr( C )] +pub struct HPETClass { + parent_class: <SysBusDevice as ObjectType >::Class , +} + +unsafe impl ObjectType for HPETState { +type Class =HPETClass ; +const TYPE_NAME : &'staticCStr =crate ::TYPE_HPET ; +}No need for HPETClass (and for ClassInitImpl<HPETClass>), just do
Quoted 1 line
unsafe impl ObjectType for HPETState {
Pls let me add a comment here:
// No need for HPETClass. Just like OBJECT_DECLARE_SIMPLE_TYPE in C.
Then this can be "grep", as a reference.
Quoted 5 lines
type Class = <SysBusDevice as ObjectType>::Class; const TYPE_NAME: &'static CStr = crate::TYPE_HPET; }which is indeed more similar to
OBJECT_DECLARE_SIMPLE_TYPE().
Awesome! Thanks.
Regards,
Zhao
Re: [PATCH 09/10] rust/timer/hpet: add qom and qdev APIs support
Il sab 8 feb 2025, 11:36 Zhao Liu <[email protected]> ha scritto:
Quoted 26 lines · Paolo Bonzini
Quoted 10 lines
Date: Wed, 29 Jan 2025 11:58:14 +0100
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH 09/10]rust/timer/hpet: add qom and qdev APIs supportQuoted 1 line · Zhao Liu
fn read(&mut self, addr: hwaddr, _size: u32) -> u64 {This can be &self.
Done.
Quoted 4 lines
Review
Quoted 4 lines
let shift: u64 = (addr & 4) * 8;(quoted diff) +2
-0+ match addr { + HPET_TN_CFG_REG => self.config >> shift, // includinginterrupt capabilities
Quoted 2 lines
This needs to be "match addr & !4".
I understand it's not necessary:
In
timer_and_addr(), I've masked the address with0x18.
Quoted 9 lines
fn timer_and_addr(&self, addr: hwaddr) ->
Option<(&BqlRefCell<HPETTimer>, hwaddr)> {
let timer_id: usize = ((addr - 0x100) / 0x20) as usize;
if timer_id > self.num_timers.get() {
None
} else {
Some((&self.timers[timer_id], addr & 0x18))Ah, this should be 0x1C (or perhaps 0x1F). Otherwise there is a bug in accessing the high 32 bits of a 64-bit register; shift will always be 0 in HPETTimer::read and write.
// No need for HPETClass. Just like OBJECT_DECLARE_SIMPLE_TYPE in C.
Quoted 3 lines
Then this can be "grep", as a reference.
Sure.
Thanks,
Paolo
Quoted 12 lines
Quoted 5 lines
type Class = <SysBusDevice as ObjectType>::Class; const TYPE_NAME: &'static CStr = crate::TYPE_HPET; }which is indeed more similar to
OBJECT_DECLARE_SIMPLE_TYPE().Awesome! Thanks.
Regards,
Zhao
Re: [PATCH 09/10] rust/timer/hpet: add qom and qdev APIs support
Quoted 16 lines
Quoted 6 lines
Quoted 1 line
This needs to be "match addr & !4".
I understand it's not necessary:
In
timer_and_addr(), I've masked the address with0x18.Quoted 9 lines
fn timer_and_addr(&self, addr: hwaddr) -> Option<(&BqlRefCell<HPETTimer>, hwaddr)> { let timer_id: usize = ((addr - 0x100) / 0x20) as usize; if timer_id > self.num_timers.get() { None } else { Some((&self.timers[timer_id], addr & 0x18))
Quoted 3 lines
Ah, this should be
0x1C(or perhaps0x1F). Otherwise there is a bug in accessing the high 32 bits of a 64-bit register; shift will always be 0 inHPETTimer::readand write.
Yes, you're right. Then we should use 0x1F so that invalid access could detected (or loged in future) and ignored.
Based on the similar reason, C side also need to use "addr & (0x1f | ~4)" instead of 0x18 to catch invalid access. If I'm right, I can submit a fix.
Thanks,
Zhao
PATCH 10/10 i386: enable rust hpet for pc when rust is enabled
In reply to Zhao LiuAdd HPET configuration in PC's Kconfig options, and select HPET device (Rust version) if Rust is supported.
---
Changes since RFC:
* Fix rust hpet not being optional.
* Fix bios-tables-test missing rust hpet.
--- configs/devices/i386-softmmu/default.mak | 1 + hw/i386/fw_cfg.c | 2 +- hw/i386/pc.c | 2 +- hw/timer/Kconfig | 6 +++++- rust/hw/Kconfig | 1 + rust/hw/timer/Kconfig | 2 ++ tests/qtest/meson.build | 3 ++- 7 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 rust/hw/timer/Kconfig
configs/devices/i386-softmmu/default.mak +1 -0
@@ -6,6 +6,7 @@ #CONFIG_APPLESMC=n #CONFIG_FDC=n #CONFIG_HPET=n +#CONFIG_X_HPET_RUST=n #CONFIG_HYPERV=n #CONFIG_ISA_DEBUG=n #CONFIG_ISA_IPMI_BT=n
hw/i386/fw_cfg.c +1 -1
@@ -147,7 +147,7 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms,#endif fw_cfg_add_i32 (fw_cfg,FW_CFG_IRQ0_OVERRIDE ,1 ); -#ifdef CONFIG_HPET +#if defined(CONFIG_HPET ) || defined(CONFIG_X_HPET_RUST )PCMachineState *pcms = (PCMachineState *)object_dynamic_cast (OBJECT (ms),TYPE_PC_MACHINE );if (pcms && pcms->hpet_enabled) {
hw/i386/pc.c +1 -1
@@ -1701,7 +1701,7 @@ static void pc_machine_initfn(Object *obj) pcms->sata_enabled = true; pcms->i8042_enabled = true; pcms->max_fw_size =8 * MiB; -#ifdef CONFIG_HPET +#if defined(CONFIG_HPET ) || defined(CONFIG_X_HPET_RUST ) pcms->hpet_enabled = true;#endif pcms->fd_bootchk = true;
hw/timer/Kconfig +5 -1
@@ -11,7 +11,11 @@ config A9_GTIMER config HPET bool - default y if PC + default y if PC && !HAVE_RUST + +config X_HPET_RUST + bool + default y if PC && HAVE_RUST config I8254 bool
rust/hw/Kconfig +1 -0
@@ -1,2 +1,3 @@ # devices Kconfig source char/Kconfig +source timer/Kconfig
rust/hw/timer/Kconfig new file +2 -0
@@ -0,0 +1,2 @@ +config X_HPET_RUST + bool
tests/qtest/meson.build +2 -1
@@ -102,7 +102,8 @@ qtests_i386 = \ config_all_devices.has_key('CONFIG_VIRTIO_PCI') and \ slirp.found() ? ['virtio-net-failover'] : []) + \ (unpack_edk2_blobs and \ - config_all_devices.has_key('CONFIG_HPET') and \ + (config_all_devices.has_key('CONFIG_HPET') or \ + config_all_devices.has_key('CONFIG_X_HPET_RUST')) and \ config_all_devices.has_key('CONFIG_PARALLEL') ? ['bios-tables-test'] : []) + \ qtests_pci + \ qtests_cxl + \
Signature · 1 line
2.34.1