qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
To: Paolo Bonzini <pbonzini@redhat.com>, qemu-devel <qemu-devel@nongnu.org>
Cc: qemu-devel <qemu-devel@nongnu.org>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Mads Ynddal" <mads@ynddal.dk>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Alex Benné e" <alex.bennee@linaro.org>,
	"Daniel P. Berrangé " <berrange@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Philippe Mathieu-Daudé " <philmd@linaro.org>,
	"Zhao Liu" <zhao1.liu@intel.com>,
	"Gustavo Romero" <gustavo.romero@linaro.org>,
	"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
	rowan.hart@intel.com,
	"Richard Henderson" <richard.henderson@linaro.org>
Subject: Re: [RFC PATCH v5 0/8] Add Rust support, implement ARM PL011
Date: Wed, 31 Jul 2024 12:41:08 +0300	[thread overview]
Message-ID: <hhfb9.99e4vl1r79fa@linaro.org> (raw)
In-Reply-To: <CAAjaMXatq0jGrght=Fc-7TpZvuGzirhWyKsAsCRq1BW_U_CW=g@mail.gmail.com>

On Fri, 26 Jul 2024 12:26, Manos Pitsidianakis <manos.pitsidianakis@linaro.org> wrote:
>On Fri, 26 Jul 2024 at 11:19, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> As I said, I don't see the point in discussing this more, and I'm not
>> going to unless you provide a clear pointer to documentation that
>> states the opposite.
>
>Same here.

Next patch series version is taking more time than I expected because of 
unrelated issues, plus I'm on PTO these days, so I am posting the 
resolution (which I hope satisfies everyone) here early:

It's possible to refactor this code (and any other similar cases) to 
prevent recursive mutable calls:

diff --git a/rust/hw/char/pl011/src/device.rs 
b/rust/hw/char/pl011/src/device.rs
index 3643b7bdee..449930e34e 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -104,10 +127,14 @@ pub fn init(&mut self) {
         }
     }
 
-    pub fn read(&mut self, offset: hwaddr, _size: core::ffi::c_uint) -> u64 {
+    pub fn read(
+        &mut self,
+        offset: hwaddr,
+        _size: core::ffi::c_uint,
+    ) -> std::ops::ControlFlow<u64, u64> {
         use RegisterOffset::*;
 
-        match RegisterOffset::try_from(offset) {
+        std::ops::ControlFlow::Break(match RegisterOffset::try_from(offset) {
             Err(v) if (0x3f8..0x400).contains(&v) => {
                 u64::from(PL011_ID_ARM[((offset - 0xfe0) >> 2) as usize])
             }
@@ -134,10 +161,8 @@ pub fn read(&mut self, offset: hwaddr, _size: core::ffi::c_uint) -> u64 {
                 // Update error bits.
                 self.receive_status_error_clear = c.to_be_bytes()[3].into();
                 self.update();
-                // SAFETY: self.char_backend is a valid CharBackend instance after it's been
-                // initialized in realize().
-                unsafe { qemu_chr_fe_accept_input(&mut self.char_backend) };
-                c.into()
+                // Must call qemu_chr_fe_accept_input, so return Continue:
+                return std::ops::ControlFlow::Continue(c.into());
             }
             Ok(RSR) => u8::from(self.receive_status_error_clear).into(),
             Ok(FR) => u16::from(self.flags).into(),
@@ -159,7 +184,7 @@ pub fn read(&mut self, offset: hwaddr, _size: core::ffi::c_uint) -> u64 {
                 0
             }
             Ok(DMACR) => self.dmacr.into(),
-        }
+        })
     }
 
     pub fn write(&mut self, offset: hwaddr, value: u64) {
diff --git a/rust/hw/char/pl011/src/memory_ops.rs b/rust/hw/char/pl011/src/memory_ops.rs
index 6144d28586..5e185b7cd7 100644
--- a/rust/hw/char/pl011/src/memory_ops.rs
+++ b/rust/hw/char/pl011/src/memory_ops.rs
@@ -29,7 +29,18 @@
 ) -> u64 {
     assert!(!opaque.is_null());
     let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
-    state.as_mut().read(addr, size)
+    let val = state.as_mut().read(addr, size);
+    match val {
+        std::ops::ControlFlow::Break(val) => val,
+        std::ops::ControlFlow::Continue(val) => {
+            // SAFETY: self.char_backend is a valid CharBackend instance after it's been
+            // initialized in realize().
+            let cb_ptr = core::ptr::addr_of_mut!(state.as_mut().char_backend);
+            unsafe { qemu_chr_fe_accept_input(cb_ptr) };
+
+            val
+        }
+    }
 }

When we iterate the APIs further we will find better idiomatic solutions 
that do not rely on devices themselves to do this kind of stuff, but 
offer interfaces from the `qemu_api` library crate.

Manos


  reply	other threads:[~2024-07-31  9:50 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-22 11:43 [RFC PATCH v5 0/8] Add Rust support, implement ARM PL011 Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 1/8] build-sys: Add rust feature option Manos Pitsidianakis
2024-07-23  6:37   ` Zhao Liu
2024-07-23 10:13     ` Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 2/8] build deps: update lcitool to include rust bits Manos Pitsidianakis
2024-07-23  8:31   ` Richard Henderson
2024-07-23 10:11     ` Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 3/8] CI: Add build-system-rust-debian job Manos Pitsidianakis
2024-07-23  8:32   ` Richard Henderson
2024-07-23  8:39   ` Daniel P. Berrangé
2024-07-23 10:06     ` Manos Pitsidianakis
2024-07-23 10:11       ` Daniel P. Berrangé
2024-07-23 10:24         ` Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 4/8] rust: add bindgen step as a meson dependency Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 5/8] .gitattributes: add Rust diff and merge attributes Manos Pitsidianakis
2024-07-23  8:38   ` Zhao Liu
2024-07-22 11:43 ` [RFC PATCH v5 6/8] rust: add crate to expose bindings and interfaces Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 7/8] rust: add PL011 device model Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 8/8] rust/pl011: vendor dependencies Manos Pitsidianakis
2024-07-23  8:37   ` Zhao Liu
2024-07-23 10:19     ` Manos Pitsidianakis
2024-07-23 15:07 ` [RFC PATCH v5 0/8] Add Rust support, implement ARM PL011 Paolo Bonzini
2024-07-24  9:14   ` Manos Pitsidianakis
2024-07-24 10:34     ` Paolo Bonzini
2024-07-25  5:47       ` Manos Pitsidianakis
2024-07-25  9:50         ` Paolo Bonzini
2024-07-25 10:02           ` Manos Pitsidianakis
2024-07-25 11:19             ` Paolo Bonzini
2024-07-25 14:48               ` Manos Pitsidianakis
2024-07-25 15:15                 ` Paolo Bonzini
2024-07-26  7:12                   ` Manos Pitsidianakis
2024-07-26  8:19                     ` Paolo Bonzini
2024-07-26  9:26                       ` Manos Pitsidianakis
2024-07-31  9:41                         ` Manos Pitsidianakis [this message]
2024-07-31 10:35                           ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=hhfb9.99e4vl1r79fa@linaro.org \
    --to=manos.pitsidianakis@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=gustavo.romero@linaro.org \
    --cc=mads@ynddal.dk \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=pierrick.bouvier@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=rowan.hart@intel.com \
    --cc=stefanha@redhat.com \
    --cc=thuth@redhat.com \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).