Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/boot_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where
fn load(
&self,
virt: &mut V,
memory: &mut MemoryAddressSpace<V::Memory>,
memory: &MemoryAddressSpace<V::Memory>,
irq_chip: &dyn InterruptController,
devices: Iter<'_, Box<dyn MmioDevice>>,
) -> Result<()>;
Expand Down
8 changes: 4 additions & 4 deletions crates/vm-bootloader/src/boot_loader/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl AArch64BootLoader {
fn load_image<C>(
&self,
layout: &mut AArch64Layout,
memory: &mut MemoryAddressSpace<C>,
memory: &MemoryAddressSpace<C>,
) -> Result<()>
where
C: MemoryContainer,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl AArch64BootLoader {
fn load_initrd<C>(
&self,
layout: &mut AArch64Layout,
memory: &mut MemoryAddressSpace<C>,
memory: &MemoryAddressSpace<C>,
) -> Result<()>
where
C: MemoryContainer,
Expand All @@ -88,7 +88,7 @@ impl AArch64BootLoader {
fn load_dtb<C>(
&self,
layout: &mut AArch64Layout,
memory: &mut MemoryAddressSpace<C>,
memory: &MemoryAddressSpace<C>,
dtb: Vec<u8>,
) -> Result<()>
where
Expand Down Expand Up @@ -256,7 +256,7 @@ where
fn load(
&self,
virt: &mut V,
memory: &mut MemoryAddressSpace<V::Memory>,
memory: &MemoryAddressSpace<V::Memory>,
irq_chip: &dyn InterruptController,
devices: Iter<'_, Box<dyn MmioDevice>>,
) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/boot_loader/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
fn load(
&self,
_virt: &mut V,
_memory: &mut MemoryAddressSpace<V::Memory>,
_memory: &MemoryAddressSpace<V::Memory>,
_irq_chip: &dyn InterruptController,
_devices: Iter<'_, Box<dyn MmioDevice>>,
) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/initrd_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl InitrdLoader {
Ok(InitrdLoader { initrd })
}

pub fn load<C>(&self, addr: u64, memory: &mut MemoryAddressSpace<C>) -> Result<LoadResult>
pub fn load<C>(&self, addr: u64, memory: &MemoryAddressSpace<C>) -> Result<LoadResult>
where
C: MemoryContainer,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/kernel_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ pub trait KernelLoader<C> {
fn load(
&self,
boot_params: &Self::BootParams,
memory: &mut MemoryAddressSpace<C>,
memory: &MemoryAddressSpace<C>,
) -> Result<LoadResult>;
}
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/kernel_loader/linux/bzimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ where
fn load(
&self,
_boot_params: &Self::BootParams,
_memory: &mut MemoryAddressSpace<C>,
_memory: &MemoryAddressSpace<C>,
) -> Result<LoadResult> {
todo!()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/kernel_loader/linux/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where
fn load(
&self,
boot_params: &AArch64BootParams,
memory: &mut MemoryAddressSpace<C>,
memory: &MemoryAddressSpace<C>,
) -> Result<LoadResult> {
let header = self.get_header()?;

Expand Down
2 changes: 1 addition & 1 deletion crates/vm-core/src/virt/hvp/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unsafe impl Send for MemoryWrapper {}
unsafe impl Sync for MemoryWrapper {}

impl MemoryContainer for MemoryWrapper {
fn to_hva(&mut self) -> *mut u8 {
fn to_hva(&self) -> *mut u8 {
self.0.host_addr()
}
}
Expand Down
19 changes: 9 additions & 10 deletions crates/vm-device/src/device/virtio/virtio_blk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Arc;
use std::sync::Mutex;

use tokio::sync::Notify;
use vm_core::arch::irq::InterruptController;
Expand All @@ -22,7 +21,7 @@ use zerocopy::IntoBytes;
pub struct VirtIoBlkDevice<C> {
irq: u32,
irq_chip: Arc<dyn InterruptController>,
mm: Arc<Mutex<MemoryAddressSpace<C>>>,
mm: Arc<MemoryAddressSpace<C>>,
cfg: VirtioBlkConfig,
}

Expand All @@ -33,7 +32,7 @@ where
pub fn new(
irq: u32,
irq_chip: Arc<dyn InterruptController>,
mm: Arc<Mutex<MemoryAddressSpace<C>>>,
mm: Arc<MemoryAddressSpace<C>>,
) -> Self {
let cfg = VirtioBlkConfig {
capacity: 50,
Expand Down Expand Up @@ -88,33 +87,33 @@ where
notify.notified().await;

let mut dev = dev.lock().unwrap();
let mut mm = mm.lock().unwrap();

let q = dev.get_virt_queue_mut(queue_sel).unwrap();

let avail_ring = q.avail_ring(&mut mm).unwrap();
let desc_ring = q.desc_table_ref(&mut mm).unwrap();
let mut used_ring = q.used_ring(&mut mm).unwrap();
let avail_ring = q.avail_ring(&mm).unwrap();
let desc_ring = q.desc_table_ref(&mm).unwrap();
let mut used_ring = q.used_ring(&mm).unwrap();

let mut updated = false;

while q.last_available_idx() != avail_ring.idx() {
let last_available_idx = q.last_available_idx();
let desc_id = avail_ring.ring(last_available_idx);
let desc_entry = desc_ring.get(desc_id);
let req = desc_entry.addr(&mut mm).unwrap();
let req = desc_entry.addr(&mm).unwrap();
let req = unsafe { &*(req.as_ptr() as *const VirtioBlkReq) };

match req.r#type {
VirtIoBlkReqType::VirtioBlkTIn => {
let chains = desc_ring.get_chain(desc_id);

let data = chains[1];
let data_hva = data.addr(&mut mm).unwrap();
let data_hva = data.addr(&mm).unwrap();
let data_len = data.len;
unsafe { data_hva.write_bytes(0xff, data_len.try_into().unwrap()) };

let status = chains[2];
let mut status_hva = status.addr(&mut mm).unwrap();
let mut status_hva = status.addr(&mm).unwrap();
*unsafe { status_hva.as_mut() } = 0;

let used_idx = used_ring.idx();
Expand Down
5 changes: 2 additions & 3 deletions crates/vm-machine/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Arc;
use std::sync::Mutex;

use vm_core::arch::irq::InterruptController;
use vm_core::device::device_manager::DeviceManager;
Expand All @@ -18,7 +17,7 @@ use crate::error::Error;
pub trait InitDevice {
fn init_devices<C>(
&mut self,
mm: Arc<Mutex<MemoryAddressSpace<C>>>,
mm: Arc<MemoryAddressSpace<C>>,
devices: Vec<Device>,
irq_chip: Option<Arc<dyn InterruptController>>,
) -> Result<(), Error>
Expand All @@ -29,7 +28,7 @@ pub trait InitDevice {
impl InitDevice for DeviceManager {
fn init_devices<C>(
&mut self,
mm: Arc<Mutex<MemoryAddressSpace<C>>>,
mm: Arc<MemoryAddressSpace<C>>,
devices: Vec<Device>,
irq_chip: Option<Arc<dyn InterruptController>>,
) -> Result<(), Error>
Expand Down
6 changes: 2 additions & 4 deletions crates/vm-machine/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::Error;
use crate::error::Result;

pub struct Vm<V: Virt> {
pub(crate) memory: Arc<Mutex<MemoryAddressSpace<V::Memory>>>,
pub(crate) memory: Arc<MemoryAddressSpace<V::Memory>>,
pub(crate) virt: V,
pub(crate) device_manager: Arc<Mutex<DeviceManager>>,
pub(crate) gdb_stub: Option<GdbStub>,
Expand All @@ -23,13 +23,11 @@ where
{
pub fn run(&mut self, boot_loader: &dyn BootLoader<V>) -> Result<()> {
{
let mut memory = self.memory.lock().unwrap();

let device_manager = self.device_manager.lock().unwrap();

boot_loader.load(
&mut self.virt,
&mut memory,
&self.memory,
device_manager
.get_irq_chip()
.ok_or_else(|| Error::InitIrqchip("irq_chip is not exists".to_string()))?
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-machine/src/vm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl VmBuilder {

let mut memory_regions = MemoryAddressSpace::default();
virt.init_memory(&mut memory_regions, self.memory_size)?;
let memory = Arc::new(Mutex::new(memory_regions));
let memory = Arc::new(memory_regions);

let layout = virt.get_layout();
let mmio_layout = MmioLayout::new(layout.get_mmio_start(), layout.get_mmio_len());
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-mm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::error::Error;
pub mod mmap_allocator;

pub trait MemoryContainer: Send + Sync + 'static {
fn to_hva(&mut self) -> *mut u8;
fn to_hva(&self) -> *mut u8;
}

pub trait Allocator {
Expand Down
4 changes: 2 additions & 2 deletions crates/vm-mm/src/allocator/mmap_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mod container {
use crate::allocator::MemoryContainer;

impl MemoryContainer for MmapMut {
fn to_hva(&mut self) -> *mut u8 {
self.as_mut_ptr()
fn to_hva(&self) -> *mut u8 {
self.as_ptr() as *mut u8
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions crates/vm-mm/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ where
Ok(())
}

pub fn gpa_to_hva(&mut self, gpa: u64) -> Result<*mut u8, Error> {
let region = self.try_get_region_by_gpa_mut(gpa)?;
pub fn gpa_to_hva(&self, gpa: u64) -> Result<*mut u8, Error> {
let region = self.try_get_region_by_gpa(gpa)?;
let hva = region.to_hva();

let offset = gpa - region.gpa;

unsafe { Ok(hva.add(offset as usize)) }
}

pub fn memset(&mut self, gpa: u64, val: u8, len: usize) -> Result<(), Error> {
let region = self.try_get_region_by_gpa_mut(gpa)?;
pub fn memset(&self, gpa: u64, val: u8, len: usize) -> Result<(), Error> {
let region = self.try_get_region_by_gpa(gpa)?;
let hva = region.to_hva();
let offset = gpa - region.gpa;

Expand All @@ -63,8 +63,8 @@ where
Ok(())
}

pub fn copy_from_slice(&mut self, gpa: u64, buf: &[u8], len: usize) -> Result<(), Error> {
let region = self.try_get_region_by_gpa_mut(gpa)?;
pub fn copy_from_slice(&self, gpa: u64, buf: &[u8], len: usize) -> Result<(), Error> {
let region = self.try_get_region_by_gpa(gpa)?;
let hva = region.to_hva();
let offset = gpa - region.gpa;

Expand All @@ -90,15 +90,15 @@ where
})
}

fn get_mut_by_gpa(&mut self, gpa: u64) -> Option<&mut MemoryRegion<C>> {
fn get_by_gpa(&self, gpa: u64) -> Option<&MemoryRegion<C>> {
self.regions
.values_mut()
.values()
.find(|region| gpa >= region.gpa && gpa < region.gpa + region.len as u64)
.map(|v| v as _)
}

fn try_get_region_by_gpa_mut(&mut self, gpa: u64) -> Result<&mut MemoryRegion<C>, Error> {
self.get_mut_by_gpa(gpa).ok_or(Error::AccessInvalidGpa(gpa))
fn try_get_region_by_gpa(&self, gpa: u64) -> Result<&MemoryRegion<C>, Error> {
self.get_by_gpa(gpa).ok_or(Error::AccessInvalidGpa(gpa))
}
}

Expand Down Expand Up @@ -144,7 +144,7 @@ mod tests {
let mut memory_as = MemoryAddressSpace::<MmapMut>::default();
let allocator = MmapAllocator;

let mut region = MemoryRegion::new(GPA, LEN, allocator.alloc(LEN, None)?);
let region = MemoryRegion::new(GPA, LEN, allocator.alloc(LEN, None)?);

let hva = region.to_hva();

Expand Down
2 changes: 1 addition & 1 deletion crates/vm-mm/src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
MemoryRegion { gpa, len, memory }
}

pub fn to_hva(&mut self) -> *mut u8 {
pub fn to_hva(&self) -> *mut u8 {
self.memory.to_hva()
}
}
6 changes: 3 additions & 3 deletions crates/vm-virtio/src/virt_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl VirtQueue {
}
}

pub fn desc_table_ref<C>(&self, mm: &mut MemoryAddressSpace<C>) -> Result<VirtqDescTableRef>
pub fn desc_table_ref<C>(&self, mm: &MemoryAddressSpace<C>) -> Result<VirtqDescTableRef>
where
C: MemoryContainer,
{
Expand All @@ -135,7 +135,7 @@ impl VirtQueue {
Ok(VirtqDescTableRef::new(self.queue_size, hva))
}

pub fn avail_ring<C>(&self, mm: &mut MemoryAddressSpace<C>) -> Result<VirtqAvail>
pub fn avail_ring<C>(&self, mm: &MemoryAddressSpace<C>) -> Result<VirtqAvail>
where
C: MemoryContainer,
{
Expand All @@ -149,7 +149,7 @@ impl VirtQueue {
Ok(VirtqAvail::new(self.queue_size, hva as *const u16))
}

pub fn used_ring<C>(&self, mm: &mut MemoryAddressSpace<C>) -> Result<VirtqUsed>
pub fn used_ring<C>(&self, mm: &MemoryAddressSpace<C>) -> Result<VirtqUsed>
where
C: MemoryContainer,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-virtio/src/virt_queue/virtq_desc_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct VirtqDesc {

impl VirtqDesc {
/// Get hva of the buf
pub fn addr<C>(&self, mm: &mut MemoryAddressSpace<C>) -> Result<NonNull<u8>>
pub fn addr<C>(&self, mm: &MemoryAddressSpace<C>) -> Result<NonNull<u8>>
where
C: MemoryContainer,
{
Expand Down
Loading