Skip to content

Commit 0462b37

Browse files
committed
queue: introduce traits queue state operations
Abstract queue state operations as `trait QueueStateT`, so we could provide different implementations for single-threaded context and multi-threaded context. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
1 parent e987c81 commit 0462b37

File tree

3 files changed

+375
-187
lines changed

3 files changed

+375
-187
lines changed

crates/virtio-device/src/mmio.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::convert::TryInto;
1010
use std::sync::atomic::Ordering;
1111

1212
use log::warn;
13-
use vm_memory::{GuestAddress, GuestAddressSpace};
13+
use vm_memory::GuestAddressSpace;
1414

1515
use crate::{status, WithDriverSelect};
1616
use virtio_queue::Queue;
@@ -51,16 +51,6 @@ where
5151
}
5252
}
5353

54-
// Helper function that rewrites the most significant 4 bytes of the provided `GuestAddress`.
55-
fn set_high(v: &mut GuestAddress, hi: u32) {
56-
*v = (*v & 0xffff_ffff) | (u64::from(hi) << 32)
57-
}
58-
59-
// Helper function that rewrites the least significant 4 bytes of the provided `GuestAddress`.
60-
fn set_low(v: &mut GuestAddress, lo: u32) {
61-
*v = (*v & !0xffff_ffff) | u64::from(lo)
62-
}
63-
6454
/// A common interface for Virtio devices that use the MMIO transport, which also provides a
6555
/// default implementation of read and write operations from/to the device registers and
6656
/// configuration space.
@@ -99,7 +89,7 @@ pub trait VirtioMmioDevice<M: GuestAddressSpace>: WithDriverSelect<M> {
9989
.into(),
10090
0x44 => self
10191
.selected_queue()
102-
.map(|q| q.ready)
92+
.map(|q| q.ready())
10393
.unwrap_or(false)
10494
.into(),
10595
0x60 => self.interrupt_status().load(Ordering::SeqCst).into(),
@@ -159,8 +149,8 @@ pub trait VirtioMmioDevice<M: GuestAddressSpace>: WithDriverSelect<M> {
159149
// data type specified by the virtio standard (we simply use `as` conversion
160150
// for now).
161151
0x30 => self.set_queue_select(v as u16),
162-
0x38 => update_queue_field(self, |q| q.size = v as u16),
163-
0x44 => update_queue_field(self, |q| q.ready = v == 1),
152+
0x38 => update_queue_field(self, |q| q.set_size(v as u16)),
153+
0x44 => update_queue_field(self, |q| q.set_ready(v == 1)),
164154
0x50 => self.queue_notify(v),
165155
0x64 => {
166156
if self.check_device_status(status::DRIVER_OK, 0) {
@@ -169,12 +159,12 @@ pub trait VirtioMmioDevice<M: GuestAddressSpace>: WithDriverSelect<M> {
169159
}
170160
}
171161
0x70 => self.ack_device_status(v as u8),
172-
0x80 => update_queue_field(self, |q| set_low(&mut q.desc_table, v)),
173-
0x84 => update_queue_field(self, |q| set_high(&mut q.desc_table, v)),
174-
0x90 => update_queue_field(self, |q| set_low(&mut q.avail_ring, v)),
175-
0x94 => update_queue_field(self, |q| set_high(&mut q.avail_ring, v)),
176-
0xa0 => update_queue_field(self, |q| set_low(&mut q.used_ring, v)),
177-
0xa4 => update_queue_field(self, |q| set_high(&mut q.used_ring, v)),
162+
0x80 => update_queue_field(self, |q| q.set_desc_table_address(Some(v), None)),
163+
0x84 => update_queue_field(self, |q| q.set_desc_table_address(None, Some(v))),
164+
0x90 => update_queue_field(self, |q| q.set_avail_ring_address(Some(v), None)),
165+
0x94 => update_queue_field(self, |q| q.set_avail_ring_address(None, Some(v))),
166+
0xa0 => update_queue_field(self, |q| q.set_used_ring_address(Some(v), None)),
167+
0xa4 => update_queue_field(self, |q| q.set_used_ring_address(None, Some(v))),
178168
_ => {
179169
warn!("unknown virtio mmio register write: 0x{:x}", offset);
180170
}

0 commit comments

Comments
 (0)