qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Zhao Liu <zhao1.liu@intel.com>
Cc: qemu-devel@nongnu.org, junjie.mao@hotmail.com, qemu-rust@nongnu.org
Subject: Re: [PATCH 2/2] rust: add bindings for interrupt sources
Date: Tue, 26 Nov 2024 14:35:22 +0100	[thread overview]
Message-ID: <7a59cb8a-3da2-45d8-9c8a-92132374cecb@redhat.com> (raw)
In-Reply-To: <Z0XQ79uYWNUrbmR2@intel.com>

On 11/26/24 14:45, Zhao Liu wrote:
> On Fri, Nov 22, 2024 at 08:47:56AM +0100, Paolo Bonzini wrote:
>> Date: Fri, 22 Nov 2024 08:47:56 +0100
>> From: Paolo Bonzini <pbonzini@redhat.com>
>> Subject: [PATCH 2/2] rust: add bindings for interrupt sources
>> X-Mailer: git-send-email 2.47.0
>>
>> The InterruptSource bindings let us call qemu_set_irq() and sysbus_init_irq()
>> as safe code.
>>
>> Interrupt sources, qemu_irq in C code, are pointers to IRQState objects.
>> They are QOM link properties and can be written to outside the control
>> of the device (i.e. from a shared reference); therefore they must be
>> interior-mutable in Rust.
> 
> Out of curiosity, are there any examples of this situation?

Yes, qdev_connect_gpio_out_named() changes the pointer that is stored 
into the InterruptSource.

>> +    /// Send `level` to the interrupt sink.
>> +    pub fn set(&self, level: bool) {
>> +        unsafe {
>> +            qemu_set_irq(self.0.get(), level.into());
>> +        }
>> +    }
> 
> Regarding the boolean discussion, the c_int/i32->boolean conversion
> seems unavoidable if it is changed to a boolean, for example, the
> level parameter in qemu_irq_handler is declared to be c_int, and
> there is a pattern of setting the level in qemu_irq_handler with the
> level irq:
> * hpet_handle_legacy_irq
> * split_irq_handler
> * a9mp_priv_set_irq
> ...
> 
> So it feels like a more direct way to follow the use of c_int or i32?
> Inconsistent types for level are always confusing. Maybe we can change
> the type of rust after the C version can be standardized to boolean?

The problem is that auditing the C version would be quite some work, 
which is why I proposed adding the generic argument to Rust.

On the other hand, it's relatively common in C to write int when you 
mean bool, because bool is a relatively recent ("only" 25 years :)) 
addition to C.

>> +    pub(crate) const fn as_ptr(&self) -> *mut *mut IRQState {
>> +        self.0.as_ptr()
>> +    }
>> +}
>> +
>> +impl Default for InterruptSource {
>> +    fn default() -> Self {
>> +        InterruptSource(BqlCell::new(ptr::null_mut()))
>> +    }
>> +}
>> +
> 
> I like this idea and this binding is very useful!
> 
> HPET also needs qdev_init_gpio_in() and qdev_init_gpio_out().
> Should these two safe binding wrappers be implemented as methods of
> DeviceState, or just the public functions?

qdev_init_gpio_out() is basically the same as sysbus_init_irq() and it 
can be added to DeviceState.

qdev_init_gpio_in() is more complicated because it includes a function 
pointer.  For now please keep it as a direct call to 
bindings::qdev_init_gpio_in(), but we'll have to tackle callbacks soon 
because they appear in all of chardev, GPIO inputs and timers.

Paolo



      reply	other threads:[~2024-11-26 13:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-22  7:47 [PATCH 0/2] rust: safe wrappers for interrupt sources Paolo Bonzini
2024-11-22  7:47 ` [PATCH 1/2] rust: add BQL-enforcing Cell variant Paolo Bonzini
2024-11-26 14:56   ` Zhao Liu
2024-11-26 16:11     ` Paolo Bonzini
2024-11-27  6:35       ` Zhao Liu
2024-11-27  8:31         ` Paolo Bonzini
2024-11-27  6:54   ` Junjie Mao
2024-11-22  7:47 ` [PATCH 2/2] rust: add bindings for interrupt sources Paolo Bonzini
2024-11-22  8:28   ` Philippe Mathieu-Daudé
2024-11-22  8:32     ` Paolo Bonzini
2024-11-22 10:30       ` Philippe Mathieu-Daudé
2024-11-22 10:53         ` Paolo Bonzini
2024-11-22 11:07           ` Philippe Mathieu-Daudé
2024-11-26 13:45   ` Zhao Liu
2024-11-26 13:35     ` Paolo Bonzini [this message]

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=7a59cb8a-3da2-45d8-9c8a-92132374cecb@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=junjie.mao@hotmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-rust@nongnu.org \
    --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).