Skip to content

Commit

Permalink
config: add MMIO layout
Browse files Browse the repository at this point in the history
The new configuration file contains the address and size of MMIO space
and PCIE config space.

Signed-off-by: Jiaqi Gao <jiaqi.gao@intel.com>
  • Loading branch information
gaojiaqi7 committed Feb 11, 2025
1 parent 76617af commit d72b805
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 85 deletions.
58 changes: 6 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/mmio_layout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Mmio32Start": "0xC0000000",
"Mmio32Size": "0x20000000",
"PcieConfigBaseAddress": "0xE0000000",
"PcieConfigSize": "0x10000000",
"Mmio64Start": "0x100000000",
"Mmio64Size": "0x40000000"
}
10 changes: 10 additions & 0 deletions src/devices/pci/src/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2024 Intel Corporation
//
// SPDX-License-Identifier: BSD-2-Clause-Patent

pub const MMIO32_START: u32 = 0xC0000000;
pub const MMIO32_SIZE: u32 = 0x20000000;
pub const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000;
pub const PCI_EX_BAR_SIZE: u64 = 0x10000000;
pub const MMIO64_START: u64 = 0x100000000;
pub const MMIO64_SIZE: u64 = 0x40000000;
2 changes: 2 additions & 0 deletions src/devices/pci/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

mod config;
mod consts;
mod layout;
mod mmio;
pub use config::*;
pub use consts::*;
pub use layout::*;
pub use mmio::*;

#[cfg(feature = "fuzz")]
Expand Down
23 changes: 5 additions & 18 deletions src/devices/pci/src/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,18 @@
use lazy_static::lazy_static;
use spin::Mutex;

use crate::{PciError, Result};

pub const MMIO32_START: u32 = 0xC000_0000;
pub const MMIO32_SIZE: u32 = 0x2000_0000;
use crate::{PciError, Result, MMIO32_START, MMIO64_START};

lazy_static! {
static ref MMIO32: Mutex<u32> = Mutex::new(0);
static ref MMIO64: Mutex<u64> = Mutex::new(0);
static ref MMIO32: Mutex<u32> = Mutex::new(MMIO32_START);
static ref MMIO64: Mutex<u64> = Mutex::new(MMIO64_START);
}

#[cfg(feature = "fuzz")]
lazy_static! {
static ref MMIO_OFFSET: Mutex<u64> = Mutex::new(0);
}

pub fn init_mmio(end_of_ram: u64) {
*MMIO32.lock() = MMIO32_START;

let mmio64_start = if end_of_ram > u32::MAX as u64 {
end_of_ram
} else {
u32::MAX as u64 + 1
};

*MMIO64.lock() = mmio64_start;
}

#[cfg(feature = "fuzz")]
pub fn alloc_mmio32(size: u32) -> Result<u32> {
let cur = *MMIO_OFFSET.lock();
Expand All @@ -45,6 +30,8 @@ pub fn alloc_mmio32(size: u32) -> Result<u32> {

#[cfg(not(feature = "fuzz"))]
pub fn alloc_mmio32(size: u32) -> Result<u32> {
use crate::MMIO32_SIZE;

let cur = *MMIO32.lock();
let addr = align_up(cur as u64, size as u64).ok_or(PciError::InvalidParameter)?;

Expand Down
8 changes: 2 additions & 6 deletions src/migtd/src/driver/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use alloc::boxed::Box;
use core::sync::atomic::AtomicBool;
use pci::PCI_EX_BAR_BASE_ADDRESS;
use td_payload::mm::shared::{alloc_shared_pages, free_shared_pages};
use virtio_serial::*;

Expand Down Expand Up @@ -49,12 +50,9 @@ impl Timer for SerailTimer {
}

// #[cfg(feature = "virtio-serial")]
pub fn virtio_serial_device_init(end_of_ram: u64) {
pub fn virtio_serial_device_init() {
pci_ex_bar_initialization();

// Initialize MMIO space
pci::init_mmio(end_of_ram);

// Enumerate the virtio device
let (_b, dev, _f) = pci::find_device(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID).unwrap();

Expand All @@ -74,8 +72,6 @@ pub fn virtio_serial_device_init(end_of_ram: u64) {
}

pub fn pci_ex_bar_initialization() {
const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000u64;

// PcdPciExpressBaseAddress TBD
let pci_exbar_base = PCI_EX_BAR_BASE_ADDRESS;

Expand Down
7 changes: 2 additions & 5 deletions src/migtd/src/driver/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,9 @@ pub fn vmcall_vsock_device_init(mid: u64, cid: u64) {
}

#[cfg(feature = "virtio-vsock")]
pub fn virtio_vsock_device_init(end_of_ram: u64) {
pub fn virtio_vsock_device_init() {
pci_ex_bar_initialization();

// Initialize MMIO space
pci::init_mmio(end_of_ram);

// Enumerate the virtio device
let (_b, dev, _f) = pci::find_device(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID).unwrap();

Expand All @@ -95,7 +92,7 @@ pub fn virtio_vsock_device_init(end_of_ram: u64) {

#[cfg(feature = "virtio-vsock")]
pub fn pci_ex_bar_initialization() {
const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000u64;
use pci::PCI_EX_BAR_BASE_ADDRESS;

// PcdPciExpressBaseAddress TBD
let pci_exbar_base = PCI_EX_BAR_BASE_ADDRESS;
Expand Down
6 changes: 2 additions & 4 deletions src/migtd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub mod ratls;
#[cfg(target_os = "none")]
pub extern "C" fn _start(hob: u64, payload: u64) -> ! {
use td_payload::arch;
#[cfg(any(feature = "virtio-serial", feature = "virtio-vsock"))]
use td_payload::mm::end_of_ram;
use td_payload::mm::layout::*;

const STACK_SIZE: usize = 0x1_0000;
Expand Down Expand Up @@ -57,11 +55,11 @@ pub extern "C" fn _start(hob: u64, payload: u64) -> ! {
driver::timer::init_timer();

#[cfg(feature = "virtio-serial")]
driver::serial::virtio_serial_device_init(end_of_ram() as u64);
driver::serial::virtio_serial_device_init();

// Init the vsock-virtio device
#[cfg(feature = "virtio-vsock")]
driver::vsock::virtio_vsock_device_init(end_of_ram() as u64);
driver::vsock::virtio_vsock_device_init();

arch::init::init(&layout, main);
}
Expand Down
2 changes: 2 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ clap = { version = "4.0", features = ["derive"] }
xshell = "0.2"
lazy_static = "1.4.0"
anyhow = "1.0"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
14 changes: 14 additions & 0 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::{
};
use xshell::{cmd, Shell};

use crate::config;

const MIGTD_DEFAULT_FEATURES: &str = "stack-guard,virtio-vsock";
const MIGTD_KVM_FEATURES: &str = MIGTD_DEFAULT_FEATURES;
const DEFAULT_IMAGE_NAME: &str = "migtd.bin";
Expand All @@ -27,6 +29,7 @@ lazy_static! {
static ref DEFAULT_SHIM_LAYOUT: PathBuf = PROJECT_ROOT.join("config/shim_layout.json");
static ref DEFAULT_IMAGE_LAYOUT: PathBuf = PROJECT_ROOT.join("config/image_layout.json");
static ref DEFAULT_SERVTD_INFO: PathBuf = PROJECT_ROOT.join("config/servtd_info.json");
static ref MMIO_LAYOUT_SOURCE: PathBuf = PROJECT_ROOT.join("src/devices/pci/src/layout.rs");
}

#[derive(Clone, Args)]
Expand Down Expand Up @@ -66,6 +69,9 @@ pub(crate) struct BuildArgs {
/// Log level control in migtd, default value is `off` for release and `info` for debug
#[clap(short, long)]
log_level: Option<LogLevel>,
/// MMIO space layout configuration for migtd
#[clap(long)]
mmio_config: Option<PathBuf>,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
Expand Down Expand Up @@ -112,6 +118,7 @@ impl LogLevel {

impl BuildArgs {
pub fn build(&self) -> Result<PathBuf> {
self.create_mmio_config()?;
let (reset_vector, shim) = self.build_shim()?;
let migtd = self.build_migtd()?;
let bin = self.build_final(reset_vector.as_path(), shim.as_path(), migtd.as_path())?;
Expand Down Expand Up @@ -165,6 +172,13 @@ impl BuildArgs {
Ok(())
}

fn create_mmio_config(&self) -> Result<()> {
if let Some(json_path) = &self.mmio_config {
config::generate_mmio_config(json_path, &MMIO_LAYOUT_SOURCE)?;
}
Ok(())
}

fn build_migtd(&self) -> Result<PathBuf> {
let sh = Shell::new()?;
sh.set_var("CC_x86_64_unknown_none", "clang");
Expand Down
Loading

0 comments on commit d72b805

Please sign in to comment.