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 13, 2025
1 parent 76617af commit 0b1da35
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 71 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"
}
3 changes: 1 addition & 2 deletions src/devices/pci/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use bitflags::bitflags;
use core::convert::From;

use crate::mmio::{alloc_mmio32, alloc_mmio64};
use crate::{PciCommand, PciError, Result};
use crate::{PciCommand, PciError, Result, PCI_EX_BAR_BASE_ADDRESS};

pub const PCI_CONFIGURATION_ADDRESS_PORT: u16 = 0xCF8;
pub const PCI_CONFIGURATION_DATA_PORT: u16 = 0xCFC;
const PCI_EX_BAR_BASE_ADDRESS: u64 = 0xE0000000u64;
const PCI_MEM32_BASE_ADDRESS_MASK: u32 = 0xFFFF_FFF0;
const PCI_MEM64_BASE_ADDRESS_MASK: u64 = 0xFFFF_FFFF_FFFF_FFF0;

Expand Down
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
24 changes: 10 additions & 14 deletions src/devices/pci/src/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
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_SIZE, MMIO32_START, MMIO64_START};

Check warning on line 8 in src/devices/pci/src/mmio.rs

View workflow job for this annotation

GitHub Actions / Format

unused import: `MMIO32_SIZE`

Check warning

Code scanning / clippy

unused import: MMIO32_SIZE Warning

unused import: MMIO32\_SIZE

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")]
Expand All @@ -21,15 +18,12 @@ lazy_static! {
}

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
};
if end_of_ram > MMIO64_START {
panic!("Invalid MMIO64 configuration: MMIO64 space overlaps with the RAM space.");
}

*MMIO64.lock() = mmio64_start;
*MMIO32.lock() = MMIO32_START;
*MMIO64.lock() = MMIO64_START;
}

#[cfg(feature = "fuzz")]
Expand All @@ -45,6 +39,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
3 changes: 1 addition & 2 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 @@ -74,8 +75,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
2 changes: 1 addition & 1 deletion src/migtd/src/driver/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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
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 0b1da35

Please sign in to comment.