mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-11 03:24:58 -06:00
rust: add bindings for interrupt sources
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. Since thread-safety is provided by the BQL, what we want here is the newly-introduced BqlCell. A pointer to the contents of the BqlCell (an IRQState**, or equivalently qemu_irq*) is then passed to the C sysbus_init_irq function. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
28d0ad3d42
commit
4ed4da164c
5 changed files with 134 additions and 10 deletions
|
@ -13,6 +13,7 @@ use qemu_api::{
|
||||||
c_str,
|
c_str,
|
||||||
definitions::ObjectImpl,
|
definitions::ObjectImpl,
|
||||||
device_class::TYPE_SYS_BUS_DEVICE,
|
device_class::TYPE_SYS_BUS_DEVICE,
|
||||||
|
irq::InterruptSource,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -94,7 +95,7 @@ pub struct PL011State {
|
||||||
/// * sysbus IRQ 5: `UARTEINTR` (error interrupt line)
|
/// * sysbus IRQ 5: `UARTEINTR` (error interrupt line)
|
||||||
/// ```
|
/// ```
|
||||||
#[doc(alias = "irq")]
|
#[doc(alias = "irq")]
|
||||||
pub interrupts: [qemu_irq; 6usize],
|
pub interrupts: [InterruptSource; IRQMASK.len()],
|
||||||
#[doc(alias = "clk")]
|
#[doc(alias = "clk")]
|
||||||
pub clock: NonNull<Clock>,
|
pub clock: NonNull<Clock>,
|
||||||
#[doc(alias = "migrate_clk")]
|
#[doc(alias = "migrate_clk")]
|
||||||
|
@ -139,7 +140,8 @@ impl PL011State {
|
||||||
unsafe fn init(&mut self) {
|
unsafe fn init(&mut self) {
|
||||||
const CLK_NAME: &CStr = c_str!("clk");
|
const CLK_NAME: &CStr = c_str!("clk");
|
||||||
|
|
||||||
let dev = addr_of_mut!(*self).cast::<DeviceState>();
|
let sbd = unsafe { &mut *(addr_of_mut!(*self).cast::<SysBusDevice>()) };
|
||||||
|
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
//
|
//
|
||||||
// self and self.iomem are guaranteed to be valid at this point since callers
|
// self and self.iomem are guaranteed to be valid at this point since callers
|
||||||
|
@ -153,12 +155,15 @@ impl PL011State {
|
||||||
Self::TYPE_INFO.name,
|
Self::TYPE_INFO.name,
|
||||||
0x1000,
|
0x1000,
|
||||||
);
|
);
|
||||||
let sbd = addr_of_mut!(*self).cast::<SysBusDevice>();
|
|
||||||
sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
|
sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
|
||||||
for irq in self.interrupts.iter_mut() {
|
|
||||||
sysbus_init_irq(sbd, irq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for irq in self.interrupts.iter() {
|
||||||
|
sbd.init_irq(irq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let dev = addr_of_mut!(*self).cast::<DeviceState>();
|
||||||
|
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
//
|
//
|
||||||
// self.clock is not initialized at this point; but since `NonNull<_>` is Copy,
|
// self.clock is not initialized at this point; but since `NonNull<_>` is Copy,
|
||||||
|
@ -498,10 +503,7 @@ impl PL011State {
|
||||||
pub fn update(&self) {
|
pub fn update(&self) {
|
||||||
let flags = self.int_level & self.int_enabled;
|
let flags = self.int_level & self.int_enabled;
|
||||||
for (irq, i) in self.interrupts.iter().zip(IRQMASK) {
|
for (irq, i) in self.interrupts.iter().zip(IRQMASK) {
|
||||||
// SAFETY: self.interrupts have been initialized in init().
|
irq.set(flags & i != 0);
|
||||||
unsafe {
|
|
||||||
qemu_set_irq(*irq, i32::from(flags & i != 0));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,10 @@ _qemu_api_rs = static_library(
|
||||||
'src/c_str.rs',
|
'src/c_str.rs',
|
||||||
'src/definitions.rs',
|
'src/definitions.rs',
|
||||||
'src/device_class.rs',
|
'src/device_class.rs',
|
||||||
|
'src/irq.rs',
|
||||||
'src/offset_of.rs',
|
'src/offset_of.rs',
|
||||||
'src/prelude.rs',
|
'src/prelude.rs',
|
||||||
|
'src/sysbus.rs',
|
||||||
'src/vmstate.rs',
|
'src/vmstate.rs',
|
||||||
'src/zeroable.rs',
|
'src/zeroable.rs',
|
||||||
],
|
],
|
||||||
|
|
91
rust/qemu-api/src/irq.rs
Normal file
91
rust/qemu-api/src/irq.rs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
// Copyright 2024 Red Hat, Inc.
|
||||||
|
// Author(s): Paolo Bonzini <pbonzini@redhat.com>
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
//! Bindings for interrupt sources
|
||||||
|
|
||||||
|
use core::ptr;
|
||||||
|
use std::{marker::PhantomData, os::raw::c_int};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
bindings::{qemu_set_irq, IRQState},
|
||||||
|
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
|
||||||
|
/// GPIO controller.
|
||||||
|
///
|
||||||
|
/// As far as devices are concerned, interrupt sources are always active-high:
|
||||||
|
/// for example, `InterruptSource<bool>`'s [`raise`](InterruptSource::raise)
|
||||||
|
/// method sends a `true` value to the sink. If the guest has to see a
|
||||||
|
/// different polarity, that change is performed by the board between the
|
||||||
|
/// device and the interrupt controller.
|
||||||
|
///
|
||||||
|
/// Interrupts are implemented as a pointer to the interrupt "sink", which has
|
||||||
|
/// type [`IRQState`]. A device exposes its source as a QOM link property using
|
||||||
|
/// a function such as
|
||||||
|
/// [`SysBusDevice::init_irq`](crate::sysbus::SysBusDevice::init_irq), and
|
||||||
|
/// initially leaves the pointer to a NULL value, representing an unconnected
|
||||||
|
/// interrupt. To connect it, whoever creates the device fills the pointer with
|
||||||
|
/// the sink's `IRQState *`, for example using `sysbus_connect_irq`. Because
|
||||||
|
/// devices are generally shared objects, interrupt sources are an example of
|
||||||
|
/// the interior mutability pattern.
|
||||||
|
///
|
||||||
|
/// Interrupt sources can only be triggered under the Big QEMU Lock; `BqlCell`
|
||||||
|
/// allows access from whatever thread has it.
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[repr(transparent)]
|
||||||
|
pub struct InterruptSource<T = bool>
|
||||||
|
where
|
||||||
|
c_int: From<T>,
|
||||||
|
{
|
||||||
|
cell: BqlCell<*mut IRQState>,
|
||||||
|
_marker: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InterruptSource<bool> {
|
||||||
|
/// Send a low (`false`) value to the interrupt sink.
|
||||||
|
pub fn lower(&self) {
|
||||||
|
self.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a high-low pulse to the interrupt sink.
|
||||||
|
pub fn pulse(&self) {
|
||||||
|
self.set(true);
|
||||||
|
self.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a high (`true`) value to the interrupt sink.
|
||||||
|
pub fn raise(&self) {
|
||||||
|
self.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> InterruptSource<T>
|
||||||
|
where
|
||||||
|
c_int: From<T>,
|
||||||
|
{
|
||||||
|
/// Send `level` to the interrupt sink.
|
||||||
|
pub fn set(&self, level: T) {
|
||||||
|
let ptr = self.cell.get();
|
||||||
|
// SAFETY: the pointer is retrieved under the BQL and remains valid
|
||||||
|
// until the BQL is released, which is after qemu_set_irq() is entered.
|
||||||
|
unsafe {
|
||||||
|
qemu_set_irq(ptr, level.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) const fn as_ptr(&self) -> *mut *mut IRQState {
|
||||||
|
self.cell.as_ptr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for InterruptSource {
|
||||||
|
fn default() -> Self {
|
||||||
|
InterruptSource {
|
||||||
|
cell: BqlCell::new(ptr::null_mut()),
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,7 +16,9 @@ pub mod c_str;
|
||||||
pub mod cell;
|
pub mod cell;
|
||||||
pub mod definitions;
|
pub mod definitions;
|
||||||
pub mod device_class;
|
pub mod device_class;
|
||||||
|
pub mod irq;
|
||||||
pub mod offset_of;
|
pub mod offset_of;
|
||||||
|
pub mod sysbus;
|
||||||
pub mod vmstate;
|
pub mod vmstate;
|
||||||
pub mod zeroable;
|
pub mod zeroable;
|
||||||
|
|
||||||
|
|
27
rust/qemu-api/src/sysbus.rs
Normal file
27
rust/qemu-api/src/sysbus.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2024 Red Hat, Inc.
|
||||||
|
// Author(s): Paolo Bonzini <pbonzini@redhat.com>
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
use std::ptr::addr_of;
|
||||||
|
|
||||||
|
pub use bindings::{SysBusDevice, SysBusDeviceClass};
|
||||||
|
|
||||||
|
use crate::{bindings, cell::bql_locked, irq::InterruptSource};
|
||||||
|
|
||||||
|
impl SysBusDevice {
|
||||||
|
/// Return `self` cast to a mutable pointer, for use in calls to C code.
|
||||||
|
const fn as_mut_ptr(&self) -> *mut SysBusDevice {
|
||||||
|
addr_of!(*self) as *mut _
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Expose an interrupt source outside the device as a qdev GPIO output.
|
||||||
|
/// Note that the ordering of calls to `init_irq` is important, since
|
||||||
|
/// whoever creates the sysbus device will refer to the interrupts with
|
||||||
|
/// a number that corresponds to the order of calls to `init_irq`.
|
||||||
|
pub fn init_irq(&self, irq: &InterruptSource) {
|
||||||
|
assert!(bql_locked());
|
||||||
|
unsafe {
|
||||||
|
bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue