-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- MMIO Device Discovery by the kernel command line - irq allocator Signed-off-by: sylvain-pierrot <[email protected]>
- Loading branch information
1 parent
99590cf
commit f07e5d1
Showing
10 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = ["src/api", "src/vmm", "src/cli"] | ||
members = ["src/api", "src/cli", "src/vmm"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
use std::io; | ||
|
||
pub(crate) mod serial; | ||
pub mod virtio; | ||
|
||
#[derive(Debug)] | ||
/// Devices errors. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use linux_loader::cmdline::Error; | ||
use std::result; | ||
use vm_memory::{Address, GuestAddress, GuestUsize}; | ||
|
||
pub mod net; | ||
|
||
pub type Result<T> = result::Result<T, Error>; | ||
|
||
pub fn register_mmio_device( | ||
size: GuestUsize, | ||
baseaddr: GuestAddress, | ||
irq: u32, | ||
id: Option<u32>, | ||
) -> Result<String> { | ||
// !TODO Register to MmioManager | ||
|
||
// Pass to kernel command line | ||
if size == 0 { | ||
return Err(Error::MmioSize); | ||
} | ||
|
||
let mut device_str = format!( | ||
"virtio_mmio.device={}@0x{:x?}:{}", | ||
guestusize_to_str(size), | ||
baseaddr.raw_value(), | ||
irq | ||
); | ||
if let Some(id) = id { | ||
device_str.push_str(format!(":{}", id).as_str()); | ||
} | ||
Ok(device_str) | ||
} | ||
|
||
fn guestusize_to_str(size: GuestUsize) -> String { | ||
const KB_MULT: u64 = 1 << 10; | ||
const MB_MULT: u64 = KB_MULT << 10; | ||
const GB_MULT: u64 = MB_MULT << 10; | ||
|
||
if size % GB_MULT == 0 { | ||
return format!("{}G", size / GB_MULT); | ||
} | ||
if size % MB_MULT == 0 { | ||
return format!("{}M", size / MB_MULT); | ||
} | ||
if size % KB_MULT == 0 { | ||
return format!("{}K", size / KB_MULT); | ||
} | ||
size.to_string() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use vm_memory::{GuestAddress, GuestUsize}; | ||
|
||
use crate::core::devices::virtio::register_mmio_device; | ||
|
||
pub struct Net { | ||
vmmio_parameter: String, | ||
} | ||
|
||
impl Net { | ||
pub fn new( | ||
size: GuestUsize, | ||
baseaddr: GuestAddress, | ||
irq: u32, | ||
) -> Result<Self, linux_loader::cmdline::Error> { | ||
let vmmio_parameter = register_mmio_device(size, baseaddr, irq, None).unwrap(); | ||
|
||
Ok(Self { vmmio_parameter }) | ||
} | ||
|
||
pub fn get_vmmio_parameter(&self) -> String { | ||
self.vmmio_parameter.clone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod device; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
use std::fmt; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum Error { | ||
InvalidValue, | ||
MaxIrq, | ||
IRQOverflowed, | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
/// An irq allocator which gives next available irq. | ||
/// It is mainly used for non-legacy devices. | ||
// There are a few reserved irq's on x86_64. We just skip all the inital | ||
// reserved irq to make the implementaion simple. This could be later extended | ||
// to cater more complex scenario. | ||
#[derive(Debug)] | ||
pub struct IrqAllocator { | ||
// Tracks the last allocated irq | ||
last_used_irq: u32, | ||
last_irq: u32, | ||
} | ||
|
||
impl IrqAllocator { | ||
pub fn new(last_used_irq: u32, last_irq: u32) -> Result<Self> { | ||
if last_used_irq >= last_irq { | ||
return Err(Error::InvalidValue); | ||
} | ||
Ok(IrqAllocator { | ||
last_used_irq, | ||
last_irq, | ||
}) | ||
} | ||
|
||
pub fn next_irq(&mut self) -> Result<u32> { | ||
self.last_used_irq | ||
.checked_add(1) | ||
.ok_or(Error::IRQOverflowed) | ||
.and_then(|irq| { | ||
if irq > self.last_irq { | ||
Err(Error::MaxIrq) | ||
} else { | ||
self.last_used_irq = irq; | ||
Ok(irq) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let err = match self { | ||
Error::MaxIrq => "last_irq IRQ limit reached", | ||
Error::IRQOverflowed => "IRQ overflowed", | ||
Error::InvalidValue => { | ||
"Check the value of last_used and last_irq. las_used should be less than last_irq" | ||
} | ||
}; | ||
write!(f, "{}", err) // user-facing output | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters