Skip to content
This repository was archived by the owner on Jul 17, 2025. It is now read-only.

Commit 40ec03a

Browse files
committed
Change ShmemDevice to retain handle to PciDevice structure
1 parent e32fbd1 commit 40ec03a

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel/src/transport/shmem.rs

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use alloc::boxed::Box;
55
use alloc::sync::Arc;
6-
use driverkit::pci::{CapabilityId, CapabilityType, MsiXTableEntry};
6+
use driverkit::pci::{CapabilityId, CapabilityType, PciDevice};
77
use kpi::KERNEL_BASE;
88
use lazy_static::lazy_static;
99
use rpc::rpc::MAX_BUFF_LEN;
@@ -16,7 +16,7 @@ use {crate::arch::rackscale::controller::FrameCacheMemslice, rpc::transport::Shm
1616
use crate::cmdline::Transport;
1717
use crate::error::{KError, KResult};
1818
use crate::memory::vspace::MapAction;
19-
use crate::memory::{paddr_to_kernel_vaddr, Frame, PAddr, VAddr, BASE_PAGE_SIZE};
19+
use crate::memory::{paddr_to_kernel_vaddr, Frame, PAddr, BASE_PAGE_SIZE};
2020
use crate::pci::claim_device;
2121

2222
// Register information from:
@@ -44,11 +44,8 @@ pub(crate) struct ShmemDevice {
4444
// Doorbell address protected by arc<mutex<>> to prevent concurrent writes
4545
doorbell: Arc<Mutex<u64>>,
4646

47-
// MSI-X table protected by arc<mutex<>> to prevent concurrent writes
48-
msix_table_addr: Arc<Mutex<VAddr>>,
49-
50-
// Number of vectors in the MSI-X table
51-
pub(crate) msix_vector_count: usize,
47+
// ivshmem PciDevice
48+
device: Arc<Mutex<PciDevice>>,
5249
}
5350

5451
impl ShmemDevice {
@@ -144,19 +141,14 @@ impl ShmemDevice {
144141
)
145142
.expect("Failed to map MSI-X table");
146143

147-
let (table_addr, table_entries) = ivshmem_device
148-
.get_msix_irq_table_info(&paddr_to_kernel_vaddr)
149-
.expect("Failed to parse MSI-X table");
150-
151144
Some(ShmemDevice {
152145
mem_addr: mem_region.address,
153146
mem_size: mem_region.size,
154147
id,
155148
doorbell: Arc::new(Mutex::new(
156149
register_region.address + KERNEL_BASE + SHMEM_DOORBELL_OFFSET,
157150
)),
158-
msix_table_addr: Arc::new(Mutex::new(table_addr)),
159-
msix_vector_count: table_entries,
151+
device: Arc::new(Mutex::new(ivshmem_device)),
160152
})
161153
} else {
162154
log::error!("Unable to find IVSHMEM device");
@@ -186,30 +178,22 @@ impl ShmemDevice {
186178
destination_id: u8,
187179
int_vector: u8,
188180
) {
189-
assert!(table_vector < self.msix_vector_count);
190181
assert!(int_vector >= 0x10);
191182
// TODO(correctness): not sure if upper range is exclusive or not, erring on side of caution?
192183
assert!(int_vector < 0xFE);
193184
// TODO(correctness): how to validate destination?
194185

195-
let msix_table_addr = self.msix_table_addr.lock();
196-
// Safety:
197-
// - We're casting the part of the memory to a MSI-X table according to the spec
198-
// - It's just plain-old-data
199-
// - We assume it is only accessed while we hold the mutex on self.table_addr
200-
// - We have &mut self when giving out a mut reference to the table
201-
// - Sanity check that we're within `bar`'s range (TODO)
202-
// - Check that `addr` satisfies alignment for [MsiXTableEntry] (TODO)
203-
let mut msix_table = unsafe {
204-
core::slice::from_raw_parts_mut(
205-
msix_table_addr.as_mut_ptr::<MsiXTableEntry>(),
206-
self.msix_vector_count,
207-
)
208-
};
186+
let mut device = self.device.lock();
187+
let tbl_paddr = device
188+
.get_msix_irq_table_mut(&paddr_to_kernel_vaddr)
189+
.expect("Failed to get MSI-x Table from ivshmem PciDevice");
190+
log::debug!("MSI-X table {:?}", tbl_paddr);
191+
assert!(table_vector < tbl_paddr.len());
192+
209193
log::info!(
210194
"Original MSI entry {:?} is {:?}",
211195
table_vector,
212-
msix_table[table_vector]
196+
tbl_paddr[table_vector]
213197
);
214198

215199
// Use this to construct the message address register (lower 32-bits)
@@ -236,8 +220,8 @@ impl ShmemDevice {
236220
// Mask for reserved bits 11-4 is: 0111_1111_0000b = 0x07F0
237221
let reserved_mask = 0x00_00_07_F0;
238222
let set_addr_high = 0xFF_FF_FF_FF << 32;
239-
msix_table[table_vector as usize].addr =
240-
(msix_table[table_vector].addr & reserved_mask) | address_register | set_addr_high;
223+
tbl_paddr[table_vector as usize].addr =
224+
(tbl_paddr[table_vector].addr & reserved_mask) | address_register | set_addr_high;
241225

242226
// Use this to construct the new message address register
243227
let mut data_register: u32 = 0;
@@ -260,16 +244,16 @@ impl ShmemDevice {
260244
// Reserved bits are: 63 - 32, 31 - 16, 13 - 11
261245
// So the low bits of the mask will look like this: 0011_1000_0000_0000b
262246
let reserved_mask = 0xFF_FF_38_00;
263-
msix_table[table_vector].data =
264-
(msix_table[table_vector].data & reserved_mask) | data_register;
247+
tbl_paddr[table_vector].data =
248+
(tbl_paddr[table_vector].data & reserved_mask) | data_register;
265249

266250
// Toggle the interrupt mask for this vector
267-
msix_table[table_vector].vector_control ^= 0x1;
251+
tbl_paddr[table_vector].vector_control ^= 0x1;
268252

269253
log::info!(
270254
"New MSI entry {:?} is {:?}",
271255
table_vector,
272-
msix_table[table_vector]
256+
tbl_paddr[table_vector]
273257
);
274258
}
275259
}

0 commit comments

Comments
 (0)