Skip to contentSkip to conversation

rust: pl011 cleanups + chardev bindings

PATCH In review Paolo Bonzini 2 participants 12 messages Feb 27, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rust: pl011 cleanups + chardev bindings

With this series, the only remaining use of unsafe is for vmstate's post load callback, which is small and self contained. All functionality used by pl011 and HPET devices are wrapped in Rust APIs, so they look like what a "real" from-scratch Rust device would be.

Patch 2 is best reviewed with "git diff -b --color-moved" or similar.

Paolo

Paolo Bonzini (5):
rust: chardev: provide basic bindings to character devices
rust: pl011: move register definitions out of lib.rs
rust: pl011: clean up visibilities
rust: pl011: switch to safe chardev operation
rust: pl011: pass around registers::Data

 rust/hw/char/pl011/src/device.rs    | 146 +++-----
 rust/hw/char/pl011/src/lib.rs       | 509 +---------------------------
 rust/hw/char/pl011/src/registers.rs | 507 +++++++++++++++++++++++++++
 rust/qemu-api/meson.build           |  17 +-
 rust/qemu-api/src/chardev.rs        | 237 ++++++++++++-
 rust/qemu-api/src/zeroable.rs       |   1 +
 6 files changed, 800 insertions(+), 617 deletions(-)
 create mode 100644 rust/hw/char/pl011/src/registers.rs
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/5 rust: chardev: provide basic bindings to character devices

Most of the character device API is pretty simple, with "0 or -errno" or "number of bytes or -errno" as the convention for return codes. Add safe wrappers for the API to the CharBackend bindgen-generated struct.

The API is not complete, but it covers the parts that are used by the PL011 device, plus qemu_chr_fe_write which is needed to implement the standard library Write trait.

---
 rust/qemu-api/meson.build     |  17 ++-
 rust/qemu-api/src/chardev.rs  | 237 +++++++++++++++++++++++++++++++++-
 rust/qemu-api/src/zeroable.rs |   1 +
 3 files changed, 250 insertions(+), 5 deletions(-)
rust/qemu-api/meson.build +14 -3
index 6e52c4bad74..a3f226ccc2a 100644
@@ -54,7 +54,19 @@ qemu_api = declare_dependency(link_with: _qemu_api_rs)
 rust_qemu_api_objs = static_library(
     'rust_qemu_api_objs',
     objects: [libqom.extract_all_objects(recursive: false),
-              libhwcore.extract_all_objects(recursive: false)])
+              libhwcore.extract_all_objects(recursive: false),
+              libchardev.extract_all_objects(recursive: false),
+              libcrypto.extract_all_objects(recursive: false),
+              libauthz.extract_all_objects(recursive: false),
+              libio.extract_all_objects(recursive: false)])
+rust_qemu_api_deps = declare_dependency(
+    dependencies: [
+      qom_ss.dependencies(),
+      chardev_ss.dependencies(),
+      crypto_ss.dependencies(),
+      authz_ss.dependencies(),
+      io_ss.dependencies()],
+    link_whole: [rust_qemu_api_objs, libqemuutil])
 
 test('rust-qemu-api-integration',
     executable(
@@ -63,8 +75,7 @@ test('rust-qemu-api-integration',
         override_options: ['rust_std=2021', 'build.rust_std=2021'],
         rust_args: ['--test'],
         install: false,
-        dependencies: [qemu_api, qemu_api_macros],
-        link_whole: [rust_qemu_api_objs, libqemuutil]),
+        dependencies: [qemu_api, qemu_api_macros, rust_qemu_api_deps]),
     args: [
         '--test', '--test-threads', '1',
         '--format', 'pretty',
rust/qemu-api/src/chardev.rs +235 -2

251 lines not on this page · raw message

rust/qemu-api/src/zeroable.rs +1 -0
index 47b6977828d..a3415a2ebcc 100644
@@ -106,3 +106,4 @@ fn default() -> Self {
 impl_zeroable!(crate::bindings::MemoryRegionOps__bindgen_ty_2);
 impl_zeroable!(crate::bindings::MemoryRegionOps);
 impl_zeroable!(crate::bindings::MemTxAttrs);
+impl_zeroable!(crate::bindings::CharBackend);
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/5 rust: pl011: move register definitions out of lib.rs

In reply to Paolo Bonzini
---
 rust/hw/char/pl011/src/device.rs    |   7 +-
 rust/hw/char/pl011/src/lib.rs       | 509 +---------------------------
 rust/hw/char/pl011/src/registers.rs | 507 +++++++++++++++++++++++++++
 3 files changed, 513 insertions(+), 510 deletions(-)
 create mode 100644 rust/hw/char/pl011/src/registers.rs
rust/hw/char/pl011/src/device.rs +5 -2
index d0857b470c9..01540654cc9 100644
@@ -26,10 +26,13 @@
 
 use crate::{
     device_class,
-    registers::{self, Interrupt},
-    RegisterOffset,
+    registers::{self, Interrupt, RegisterOffset},
 };
 
+// TODO: You must disable the UART before any of the control registers are
+// reprogrammed. When the UART is disabled in the middle of transmission or
+// reception, it completes the current character before stopping
+
 /// Integer Baud Rate Divider, `UARTIBRD`
 const IBRD_MASK: u32 = 0xffff;
 
rust/hw/char/pl011/src/lib.rs +1 -508

517 lines not on this page · raw message

rust/hw/char/pl011/src/registers.rs new file +507 -0

507 lines not on this page · raw message

Signature · 1 line
2.48.1
#3 lore raw

Re: [PATCH 2/5] rust: pl011: move register definitions out of lib.rs

Quoted 8 lines · Paolo Bonzini
---
 rust/hw/char/pl011/src/device.rs    |   7 +-
 rust/hw/char/pl011/src/lib.rs       | 509 +---------------------------
 rust/hw/char/pl011/src/registers.rs | 507 +++++++++++++++++++++++++++
 3 files changed, 513 insertions(+), 510 deletions(-)
 create mode 100644 rust/hw/char/pl011/src/registers.rs

Looking at this patch I'm sorely tempted to suggest significantly trimming down the commentary in these comments: it contains rather more text cut-n-pasted from the PL011 TRM than I'm entirely comfortable with, and much of it is detail that is irrelevant to QEMU. I don't think we should be trying to make it unnecessary for somebody working on the QEMU device models to ever look at the hardware reference manuals.

thanks
-- PMM

#4 lore raw

Re: [PATCH 2/5] rust: pl011: move register definitions out of lib.rs

Quoted 10 lines · Peter Maydell
Quoted 8 lines · Paolo Bonzini
---
  rust/hw/char/pl011/src/device.rs    |   7 +-
  rust/hw/char/pl011/src/lib.rs       | 509 +---------------------------
  rust/hw/char/pl011/src/registers.rs | 507 +++++++++++++++++++++++++++
  3 files changed, 513 insertions(+), 510 deletions(-)

create mode 100644 rust/hw/char/pl011/src/registers.rs

Quoted 5 lines

Looking at this patch I'm sorely tempted to suggest significantly trimming down the commentary in these comments: it contains rather more text cut-n-pasted from the PL011 TRM than I'm entirely comfortable with, and much of it is detail that is irrelevant to QEMU.

Yeah, that was a point that was made on the call last week, too. I kind of agree, but it wasn't a decision I really wanted to take or suggest.

Also, some of the stuff does not belong in the structs but could be added to lib.rs, too, with more fair-use justification than in registers.rs. So perhaps delay removing it until more aspects of the FIFO are modeled correctly, so that one does not have to reinvent the wording from scratch.

Paolo

#5 lore raw

Re: [PATCH 2/5] rust: pl011: move register definitions out of lib.rs

In reply to Peter Maydell
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Paolo Bonzini
---
  rust/hw/char/pl011/src/device.rs    |   7 +-
  rust/hw/char/pl011/src/lib.rs       | 509 +---------------------------
  rust/hw/char/pl011/src/registers.rs | 507 +++++++++++++++++++++++++++
  3 files changed, 513 insertions(+), 510 deletions(-)

create mode 100644 rust/hw/char/pl011/src/registers.rs

Quoted 5 lines

Looking at this patch I'm sorely tempted to suggest significantly trimming down the commentary in these comments: it contains rather more text cut-n-pasted from the PL011 TRM than I'm entirely comfortable with, and much of it is detail that is irrelevant to QEMU.

Yeah, that was a point that was made on the call last week, too. I think I agree, but it wasn't a decision I really wanted to take or suggest myself...

That said, some of the stuff does not belong in the structs but could be added to lib.rs, too, with more fair-use justification than in registers.rs. So perhaps we could delay removing it until more aspects of the FIFO are modeled correctly, so that one does not have to reinvent the wording from scratch.

Paolo

#6 lore raw

PATCH 3/5 rust: pl011: clean up visibilities of callbacks

In reply to Paolo Bonzini

Do not make callbacks unnecessarily "pub", they are only used through function pointers.

---
 rust/hw/char/pl011/src/device.rs | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
rust/hw/char/pl011/src/device.rs +5 -5
index 01540654cc9..4cdbbf4b73d 100644
@@ -537,7 +537,7 @@ fn post_init(&self) {
         }
     }
 
-    pub fn read(&self, offset: hwaddr, _size: u32) -> u64 {
+    fn read(&self, offset: hwaddr, _size: u32) -> u64 {
         match RegisterOffset::try_from(offset) {
             Err(v) if (0x3f8..0x400).contains(&(v >> 2)) => {
                 let device_id = self.get_class().device_id;
@@ -560,7 +560,7 @@ pub fn read(&self, offset: hwaddr, _size: u32) -> u64 {
         }
     }
 
-    pub fn write(&self, offset: hwaddr, value: u64, _size: u32) {
+    fn write(&self, offset: hwaddr, value: u64, _size: u32) {
         let mut update_irq = false;
         if let Ok(field) = RegisterOffset::try_from(offset) {
             // qemu_chr_fe_write_all() calls into the can_receive
@@ -621,7 +621,7 @@ pub fn event(&self, event: QEMUChrEvent) {
         }
     }
 
-    pub fn realize(&self) {
+    fn realize(&self) {
         // SAFETY: self.char_backend has the correct size and alignment for a
         // CharBackend object, and its callbacks are of the correct types.
         unsafe {
@@ -638,11 +638,11 @@ pub fn realize(&self) {
         }
     }
 
-    pub fn reset_hold(&self, _type: ResetType) {
+    fn reset_hold(&self, _type: ResetType) {
         self.regs.borrow_mut().reset();
     }
 
-    pub fn update(&self) {
+    fn update(&self) {
         let regs = self.regs.borrow();
         let flags = regs.int_level & regs.int_enabled;
         for (irq, i) in self.interrupts.iter().zip(IRQMASK) {
Signature · 1 line
2.48.1
#7 lore raw

PATCH 4/5 rust: pl011: switch to safe chardev operation

In reply to Paolo Bonzini

Switch bindings::CharBackend with chardev::CharBackend. This removes occurrences of "unsafe" due to FFI and switches the wrappers for receive, can_receive and event callbacks to the common ones implemented by chardev::CharBackend.

---
 rust/hw/char/pl011/src/device.rs | 116 +++++++------------------------
 1 file changed, 25 insertions(+), 91 deletions(-)
rust/hw/char/pl011/src/device.rs +25 -91
index 4cdbbf4b73d..2283bae71eb 100644
@@ -4,16 +4,11 @@
 
 use std::{
     ffi::CStr,
-    os::raw::{c_int, c_void},
-    ptr::{addr_of, addr_of_mut, NonNull},
+    ptr::addr_of_mut,
 };
 
 use qemu_api::{
-    bindings::{
-        qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers,
-        qemu_chr_fe_write_all, CharBackend, QEMUChrEvent, CHR_IOCTL_SERIAL_SET_BREAK,
-    },
-    chardev::Chardev,
+    chardev::{CharBackend, Chardev, Event},
     impl_vmstate_forward,
     irq::{IRQState, InterruptSource},
     memory::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder},
@@ -235,7 +230,7 @@ pub(self) fn write(
         &mut self,
         offset: RegisterOffset,
         value: u32,
-        char_backend: *mut CharBackend,
+        char_backend: &CharBackend,
     ) -> bool {
         // eprintln!("write offset {offset} value {value}");
         use RegisterOffset::*;
@@ -269,17 +264,9 @@ pub(self) fn write(
                     self.reset_tx_fifo();
                 }
                 let update = (self.line_control.send_break() != new_val.send_break()) && {
-                    let mut break_enable: c_int = new_val.send_break().into();
-                    // SAFETY: self.char_backend is a valid CharBackend instance after it's been
-                    // initialized in realize().
-                    unsafe {
-                        qemu_chr_fe_ioctl(
-                            char_backend,
-                            CHR_IOCTL_SERIAL_SET_BREAK as i32,
-                            addr_of_mut!(break_enable).cast::<c_void>(),
-                        );
-                    }
-                    self.loopback_break(break_enable > 0)
+                    let break_enable = new_val.send_break();
+                    let _ = char_backend.send_break(break_enable);
+                    self.loopback_break(break_enable)
                 };
                 self.line_control = new_val;
                 self.set_read_trigger();
@@ -551,9 +538,7 @@ fn read(&self, offset: hwaddr, _size: u32) -> u64 {
                 let (update_irq, result) = self.regs.borrow_mut().read(field);
                 if update_irq {
                     self.update();
-                    unsafe {
-                        qemu_chr_fe_accept_input(addr_of!(self.char_backend) as *mut _);
-                    }
+                    self.char_backend.accept_input();
                 }
                 result.into()
             }
@@ -567,21 +552,16 @@ fn write(&self, offset: hwaddr, value: u64, _size: u32) {
             // callback, so handle writes before entering PL011Registers.
             if field == RegisterOffset::DR {
                 // ??? Check if transmitter is enabled.
-                let ch: u8 = value as u8;
-                // SAFETY: char_backend is a valid CharBackend instance after it's been
-                // initialized in realize().
+                let ch: [u8; 1] = [value as u8];
                 // XXX this blocks entire thread. Rewrite to use
                 // qemu_chr_fe_write and background I/O callbacks
-                unsafe {
-                    qemu_chr_fe_write_all(addr_of!(self.char_backend) as *mut _, &ch, 1);
-                }
+                let _ = self.char_backend.write_all(&ch);
             }
 
-            update_irq = self.regs.borrow_mut().write(
-                field,
-                value as u32,
-                addr_of!(self.char_backend) as *mut _,
-            );
+            update_irq = self
+                .regs
+                .borrow_mut()
+                .write(field, value as u32, &self.char_backend);
         } else {
             eprintln!("write bad offset {offset} value {value}");
         }
@@ -590,15 +570,18 @@ fn write(&self, offset: hwaddr, value: u64, _size: u32) {
         }
     }
 
-    pub fn can_receive(&self) -> bool {
-        // trace_pl011_can_receive(s->lcr, s->read_count, r);
+    fn can_receive(&self) -> u32 {
         let regs = self.regs.borrow();
-        regs.read_count < regs.fifo_depth()
+        // trace_pl011_can_receive(s->lcr, s->read_count, r);
+        u32::from(regs.read_count < regs.fifo_depth())
     }
 
-    pub fn receive(&self, ch: u32) {
+    fn receive(&self, buf: &[u8]) {
+        if buf.is_empty() {
+            return;
+        }
         let mut regs = self.regs.borrow_mut();
-        let update_irq = !regs.loopback_enabled() && regs.put_fifo(ch);
+        let update_irq = !regs.loopback_enabled() && regs.put_fifo(buf[0].into());
         // Release the BqlRefCell before calling self.update()
         drop(regs);
 
@@ -607,10 +590,10 @@ pub fn receive(&self, ch: u32) {
         }
     }
 
-    pub fn event(&self, event: QEMUChrEvent) {
+    fn event(&self, event: Event) {
         let mut update_irq = false;
         let mut regs = self.regs.borrow_mut();
-        if event == QEMUChrEvent::CHR_EVENT_BREAK && !regs.loopback_enabled() {
+        if event == Event::CHR_EVENT_BREAK && !regs.loopback_enabled() {
             update_irq = regs.put_fifo(registers::Data::BREAK.into());
         }
         // Release the BqlRefCell before calling self.update()
@@ -622,20 +605,8 @@ pub fn event(&self, event: QEMUChrEvent) {
     }
 
     fn realize(&self) {
-        // SAFETY: self.char_backend has the correct size and alignment for a
-        // CharBackend object, and its callbacks are of the correct types.
-        unsafe {
-            qemu_chr_fe_set_handlers(
-                addr_of!(self.char_backend) as *mut CharBackend,
-                Some(pl011_can_receive),
-                Some(pl011_receive),
-                Some(pl011_event),
-                None,
-                addr_of!(*self).cast::<c_void>() as *mut c_void,
-                core::ptr::null_mut(),
-                true,
-            );
-        }
+        self.char_backend
+            .enable_handlers(self, Self::can_receive, Self::receive, Self::event);
     }
 
     fn reset_hold(&self, _type: ResetType) {
@@ -666,43 +637,6 @@ pub fn post_load(&self, _version_id: u32) -> Result<(), ()> {
     Interrupt::E.0,
 ];
 
-/// # Safety
-///
-/// We expect the FFI user of this function to pass a valid pointer, that has
-/// the same size as [`PL011State`]. We also expect the device is
-/// readable/writeable from one thread at any time.
-pub unsafe extern "C" fn pl011_can_receive(opaque: *mut c_void) -> c_int {
-    let state = NonNull::new(opaque).unwrap().cast::<PL011State>();
-    unsafe { state.as_ref().can_receive().into() }
-}
-
-/// # Safety
-///
-/// We expect the FFI user of this function to pass a valid pointer, that has
-/// the same size as [`PL011State`]. We also expect the device is
-/// readable/writeable from one thread at any time.
-///
-/// The buffer and size arguments must also be valid.
-pub unsafe extern "C" fn pl011_receive(opaque: *mut c_void, buf: *const u8, size: c_int) {
-    let state = NonNull::new(opaque).unwrap().cast::<PL011State>();
-    unsafe {
-        if size > 0 {
-            debug_assert!(!buf.is_null());
-            state.as_ref().receive(u32::from(buf.read_volatile()));
-        }
-    }
-}
-
-/// # Safety
-///
-/// We expect the FFI user of this function to pass a valid pointer, that has
-/// the same size as [`PL011State`]. We also expect the device is
-/// readable/writeable from one thread at any time.
-pub unsafe extern "C" fn pl011_event(opaque: *mut c_void, event: QEMUChrEvent) {
-    let state = NonNull::new(opaque).unwrap().cast::<PL011State>();
-    unsafe { state.as_ref().event(event) }
-}
-
 /// # Safety
 ///
 /// We expect the FFI user of this function to pass a valid pointer for `chr`
Signature · 1 line
2.48.1
#8 lore raw

Re: [PATCH 4/5] rust: pl011: switch to safe chardev operation

Quoted 13 lines · Paolo Bonzini

Switch bindings::CharBackend with chardev::CharBackend. This removes occurrences of "unsafe" due to FFI and switches the wrappers for receive, can_receive and event callbacks to the common ones implemented by chardev::CharBackend.

rust/hw/char/pl011/src/device.rs +0 -2
@@ -567,21 +552,16 @@ fn write(&self, offset: hwaddr, value: u64, _size: u32) {
 
-            update_irq = self.regs.borrow_mut().write(
-                field,

rust/hw/char/pl011/src/device.rs fn write(&self, offset: hwaddr, value: u64, _size: u32) { · line 559

Quoted 10 lines
rust/hw/char/pl011/src/device.rs +4 -3
-                value as u32,
-                addr_of!(self.char_backend) as *mut _,
-            );
+            update_irq = self
+                .regs
+                .borrow_mut()
+                .write(field, value as u32, &self.char_backend);
         } else {
             eprintln!("write bad offset {offset} value {value}");
         }

Entirely unrelated to this patch, but seeing this go past reminded me that I had a question I didn't get round to asking in the community call the other day. In this PL011State::write function, we delegate the job of updating the register state to PL011Registers::write, which returns a bool to tell us whether to call update().

I guess the underlying design idea here is "the register object updates itself and tells the device object what kinds of updates to the outside world it needs to do" ? But then, why is the irq output something that PL011State needs to handle itself whereas the chardev backend is something we can pass into PL011Registers ?

In the C version, we just call pl011_update() where we need to; we could validly call it unconditionally for any write, we're just being (possibly prematurely) efficient by avoiding a call when we happen to know that the register write didn't touch any of the state that pl011_update() cares about. So it feels a bit odd to me that in the Rust version this "we happen to know that sometimes it would be unnecessary to call the update function" has been kind of promoted to being part of an interface between the two different types PL011Registers and PL011State.

Thinking about other devices, presumably for more complex devices we might need to pass more than just a single 'bool' back from PL011Registers::write. What other kinds of thing might we need to do in the FooState function, and (since the pl011 code is presumably going to be used as a template for those other devices) is it worth having something that expresses that better than just a raw 'bool' return ?

thanks
-- PMM

#9 lore raw

Re: [PATCH 4/5] rust: pl011: switch to safe chardev operation

Quoted 30 lines · Peter Maydell
Quoted 11 lines · Paolo Bonzini

Switch bindings::CharBackend with chardev::CharBackend. This removes occurrences of "unsafe" due to FFI and switches the wrappers for receive, can_receive and event callbacks to the common ones implemented by chardev::CharBackend.

rust/hw/char/pl011/src/device.rs +0 -2
@@ -567,21 +552,16 @@ fn write(&self, offset: hwaddr, value: u64, _size: u32) {
 
-            update_irq = self.regs.borrow_mut().write(
-                field,

rust/hw/char/pl011/src/device.rs fn write(&self, offset: hwaddr, value: u64, _size: u32) { · line 559

Quoted 10 lines
rust/hw/char/pl011/src/device.rs +4 -3
-                value as u32,
-                addr_of!(self.char_backend) as *mut _,
-            );
+            update_irq = self
+                .regs
+                .borrow_mut()
+                .write(field, value as u32, &self.char_backend);
         } else {
             eprintln!("write bad offset {offset} value {value}");
         }

Entirely unrelated to this patch, but seeing this go past reminded me that I had a question I didn't get round to asking in the community call the other day. In this PL011State::write function, we delegate the job of updating the register state to PL011Registers::write, which returns a bool to tell us whether to call update().

Quoted 6 lines

I guess the underlying design idea here is "the register object updates itself and tells the device object what kinds of updates to the outside world it needs to do" ? But then, why is the irq output something that PL011State needs to handle itself whereas the chardev backend is something we can pass into PL011Registers ?

Just because the IRQ update is needed in many places and the chardev backend only in one place.

Quoted 10 lines

In the C version, we just call pl011_update() where we need to; we could validly call it unconditionally for any write, we're just being (possibly prematurely) efficient by avoiding a call when we happen to know that the register write didn't touch any of the state that pl011_update() cares about. So it feels a bit odd to me that in the Rust version this "we happen to know that sometimes it would be unnecessary to call the update function" has been kind of promoted to being part of an interface between the two different types PL011Registers and PL011State.

Yeah, if I was writing from scratch I would probably call update() unconditionally. If it turns out to be inefficient you could cache the current value of

        let flags = regs.int_level & regs.int_enabled;

in PL011State as a BqlCell.

Quoted 7 lines

Thinking about other devices, presumably for more complex devices we might need to pass more than just a single 'bool' back from PL011Registers::write. What other kinds of thing might we need to do in the FooState function, and (since the pl011 code is presumably going to be used as a template for those other devices) is it worth having something that expresses that better than just a raw 'bool' return ?

Ideally nothing, especially considering that more modern devices have edge-triggered interrupts like MSIs, instead of level-triggered interrupts that need qemu_set_irq() calls. But if you have something a lot more complex than a bool I would pass down the PL011State and do something like pl011.schedule_update_irq() which updates a BqlCell<>. The device could then use a bottom half or process them after "drop(regs)".

HPET has another approach, which is to store a backpointer from HPETTimer to the HPETState, so that it can do

    self.get_state().irqs[route].pulse();

without passing down anything. The reason for this is that it has multiple timers on the same routine, and it assigns the timers to separate HPETTimers. I would not use it for PL011 because all accesses to the PL011Registers go through the PL011State.

A while ago I checked how OpenVMM does it, and basically it does not have the PL011State/PL011Registers separation at all: the devices are automatically wrapped with a Mutex and memory accesses take a &mut. That removes some of the complexity, but also a lot of flexibility.

Unfortunately, before being able to reason on how to make peace with the limitations of safe Rust, it was necessary to spend a lot of time writing API abstractions, otherwise you don't actually experience the limitations. But that means that the number of lines of code in qemu_api is not representative of my experience using it. :( I am sorry this isn't a great answer yet; certainly some aspects of the PL011 or HPET devices could be treated as a blueprint for future devices, but which and how to document that is something where I would like to consult with an actual Rust maven.

Paolo

#10 lore raw

Re: [PATCH 4/5] rust: pl011: switch to safe chardev operation

Quoted 21 lines · Paolo Bonzini
Quoted 7 lines · Peter Maydell

Thinking about other devices, presumably for more complex devices we might need to pass more than just a single 'bool' back from PL011Registers::write. What other kinds of thing might we need to do in the FooState function, and (since the pl011 code is presumably going to be used as a template for those other devices) is it worth having something that expresses that better than just a raw 'bool' return ?

Ideally nothing, especially considering that more modern devices have edge-triggered interrupts like MSIs, instead of level-triggered interrupts that need qemu_set_irq() calls. But if you have something a lot more complex than a bool I would pass down the PL011State and do something like pl011.schedule_update_irq() which updates a BqlCell<>. The device could then use a bottom half or process them after "drop(regs)".

HPET has another approach, which is to store a backpointer from HPETTimer to the HPETState, so that it can do

Quoted 6 lines
    self.get_state().irqs[route].pulse();

without passing down anything. The reason for this is that it has multiple timers on the same routine, and it assigns the timers to separate HPETTimers. I would not use it for PL011 because all accesses to the PL011Registers go through the PL011State.

I think the idea I vaguely have in the back of my mind is that maybe it's a nice idea to have a coding structure that enforces "you update your own internal state, and only then do things that might involve calling out to the outside world, and if there's something that you need to do with the result of that callout, there's an easy mechanism for 'this is what I will want to do next after that' continuations".

The fact that our C device implementations don't do that is kind of the underlying cause of all the "recursive entry back into the device via DMA" problems that we squashed with the big hammer of "just forbid it entirely". It's also in a way the problem underlying the race condition segfault here:
https://lore.kernel.org/qemu-devel/CAFEAcA9odnPo2LPip295Uztri7JfoVnQbkJ=Wn+k8dQneB_ynQ@mail.gmail.com/T/#u
(memory_region_snapshot_and_clear_dirty() drops the BKL, no callers expect that, segfaults in the calling code in the framebuffer device models if something else gets in and e.g. resizes the framebuffer in the middle of a display update).

So I was sort of wondering if the pl011 structure was aiming at providing that kind of separation of "internal state" and "external interactions", such that the compiler would complain if you tried to do an externally-interacting thing while your internal state was not consistent.

thanks
-- PMM

#11 lore raw

PATCH 5/5 rust: pl011: pass around registers::Data

In reply to Paolo Bonzini

The values stored in the Fifo are instances of the bitfield-struct registers::Data. Convert as soon as possible the value written into DR, and always refer to the bitfield struct; it's generally cleaner other than PL011State::receive having to do a double conversion u8=>u32=>registers::Data.

---
 rust/hw/char/pl011/src/device.rs | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
rust/hw/char/pl011/src/device.rs +8 -7
index 2283bae71eb..a679f6295f5 100644
@@ -237,7 +237,7 @@ pub(self) fn write(
         match offset {
             DR => {
                 // interrupts always checked
-                let _ = self.loopback_tx(value);
+                let _ = self.loopback_tx(value.into());
                 self.int_level |= Interrupt::TX.0;
                 return true;
             }
@@ -304,7 +304,7 @@ pub(self) fn write(
 
     #[inline]
     #[must_use]
-    fn loopback_tx(&mut self, value: u32) -> bool {
+    fn loopback_tx(&mut self, value: registers::Data) -> bool {
         // Caveat:
         //
         // In real hardware, TX loopback happens at the serial-bit level
@@ -373,7 +373,7 @@ fn loopback_mdmctrl(&mut self) -> bool {
     }
 
     fn loopback_break(&mut self, enable: bool) -> bool {
-        enable && self.loopback_tx(registers::Data::BREAK.into())
+        enable && self.loopback_tx(registers::Data::BREAK)
     }
 
     fn set_read_trigger(&mut self) {
@@ -432,11 +432,11 @@ pub fn fifo_depth(&self) -> u32 {
     }
 
     #[must_use]
-    pub fn put_fifo(&mut self, value: u32) -> bool {
+    pub fn put_fifo(&mut self, value: registers::Data) -> bool {
         let depth = self.fifo_depth();
         assert!(depth > 0);
         let slot = (self.read_pos + self.read_count) & (depth - 1);
-        self.read_fifo[slot] = registers::Data::from(value);
+        self.read_fifo[slot] = value;
         self.read_count += 1;
         self.flags.set_receive_fifo_empty(false);
         if self.read_count == depth {
@@ -581,7 +581,8 @@ fn receive(&self, buf: &[u8]) {
             return;
         }
         let mut regs = self.regs.borrow_mut();
-        let update_irq = !regs.loopback_enabled() && regs.put_fifo(buf[0].into());
+        let c: u32 = buf[0].into();
+        let update_irq = !regs.loopback_enabled() && regs.put_fifo(c.into());
         // Release the BqlRefCell before calling self.update()
         drop(regs);
 
@@ -594,7 +595,7 @@ fn event(&self, event: Event) {
         let mut update_irq = false;
         let mut regs = self.regs.borrow_mut();
         if event == Event::CHR_EVENT_BREAK && !regs.loopback_enabled() {
-            update_irq = regs.put_fifo(registers::Data::BREAK.into());
+            update_irq = regs.put_fifo(registers::Data::BREAK);
         }
         // Release the BqlRefCell before calling self.update()
         drop(regs);
Signature · 1 line
2.48.1