Use cfg_if to group tdx cfg block

This commit is contained in:
Hsy-Intel 2024-08-09 12:27:03 +08:00 committed by Tate, Hongliang Tian
parent 9bad068215
commit ca41687a99
10 changed files with 123 additions and 69 deletions

View File

@ -1,20 +1,26 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use cfg_if::cfg_if;
mod null; mod null;
mod pty; mod pty;
mod random; mod random;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
mod tdxguest;
pub mod tty; pub mod tty;
mod urandom; mod urandom;
mod zero; mod zero;
cfg_if! {
if #[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] {
mod tdxguest;
use tdx_guest::tdx_is_enabled;
pub use tdxguest::TdxGuest;
}
}
pub use pty::{new_pty_pair, PtyMaster, PtySlave}; pub use pty::{new_pty_pair, PtyMaster, PtySlave};
pub use random::Random; pub use random::Random;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
use tdx_guest::tdx_is_enabled;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
pub use tdxguest::TdxGuest;
pub use urandom::Urandom; pub use urandom::Urandom;
use self::tty::get_n_tty; use self::tty::get_n_tty;
@ -34,11 +40,14 @@ pub fn init() -> Result<()> {
add_node(console, "console")?; add_node(console, "console")?;
let tty = Arc::new(tty::TtyDevice); let tty = Arc::new(tty::TtyDevice);
add_node(tty, "tty")?; add_node(tty, "tty")?;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] cfg_if! {
let tdx_guest = Arc::new(tdxguest::TdxGuest); if #[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] {
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] let tdx_guest = Arc::new(tdxguest::TdxGuest);
if tdx_is_enabled() {
add_node(tdx_guest, "tdx_guest")?; if tdx_is_enabled() {
add_node(tdx_guest, "tdx_guest")?;
}
}
} }
let random = Arc::new(random::Random); let random = Arc::new(random::Random);
add_node(random, "random")?; add_node(random, "random")?;

View File

@ -10,20 +10,24 @@ use core::{
}; };
use bitflags::bitflags; use bitflags::bitflags;
use cfg_if::cfg_if;
use log::debug; use log::debug;
#[cfg(feature = "intel_tdx")]
use tdx_guest::tdcall;
pub use trapframe::GeneralRegs as RawGeneralRegs; pub use trapframe::GeneralRegs as RawGeneralRegs;
use trapframe::UserContext as RawUserContext; use trapframe::UserContext as RawUserContext;
use x86_64::registers::rflags::RFlags; use x86_64::registers::rflags::RFlags;
#[cfg(feature = "intel_tdx")]
use crate::arch::tdx_guest::{handle_virtual_exception, TdxTrapFrame};
use crate::{ use crate::{
trap::call_irq_callback_functions, trap::call_irq_callback_functions,
user::{ReturnReason, UserContextApi, UserContextApiInternal}, user::{ReturnReason, UserContextApi, UserContextApiInternal},
}; };
cfg_if! {
if #[cfg(feature = "intel_tdx")] {
use tdx_guest::tdcall;
use crate::arch::tdx_guest::{handle_virtual_exception, TdxTrapFrame};
}
}
/// Cpu context, including both general-purpose registers and floating-point registers. /// Cpu context, including both general-purpose registers and floating-point registers.
#[derive(Clone, Default, Copy, Debug)] #[derive(Clone, Default, Copy, Debug)]
#[repr(C)] #[repr(C)]

View File

@ -4,20 +4,24 @@
use alloc::{vec, vec::Vec}; use alloc::{vec, vec::Vec};
#[cfg(feature = "intel_tdx")]
use ::tdx_guest::tdx_is_enabled;
use acpi::PlatformInfo; use acpi::PlatformInfo;
use bit_field::BitField; use bit_field::BitField;
use cfg_if::cfg_if;
use log::info; use log::info;
use spin::Once; use spin::Once;
#[cfg(feature = "intel_tdx")]
use crate::arch::tdx_guest;
use crate::{ use crate::{
arch::x86::kernel::acpi::ACPI_TABLES, mm::paddr_to_vaddr, sync::SpinLock, trap::IrqLine, Error, arch::x86::kernel::acpi::ACPI_TABLES, mm::paddr_to_vaddr, sync::SpinLock, trap::IrqLine, Error,
Result, Result,
}; };
cfg_if! {
if #[cfg(feature = "intel_tdx")] {
use ::tdx_guest::tdx_is_enabled;
use crate::arch::tdx_guest;
}
}
/// I/O Advanced Programmable Interrupt Controller. It is used to distribute external interrupts /// I/O Advanced Programmable Interrupt Controller. It is used to distribute external interrupts
/// in a more advanced manner than that of the standard 8259 PIC. /// in a more advanced manner than that of the standard 8259 PIC.
/// ///

View File

@ -5,6 +5,7 @@
use alloc::fmt; use alloc::fmt;
use core::ops::Range; use core::ops::Range;
use cfg_if::cfg_if;
pub(crate) use util::__memcpy_fallible; pub(crate) use util::__memcpy_fallible;
use x86_64::{instructions::tlb, structures::paging::PhysFrame, VirtAddr}; use x86_64::{instructions::tlb, structures::paging::PhysFrame, VirtAddr};
@ -136,11 +137,13 @@ pub fn current_page_table_paddr() -> Paddr {
} }
impl PageTableEntry { impl PageTableEntry {
/// 51:12 cfg_if! {
#[cfg(not(feature = "intel_tdx"))] if #[cfg(feature = "intel_tdx")] {
const PHYS_ADDR_MASK: usize = 0xF_FFFF_FFFF_F000; const PHYS_ADDR_MASK: usize = 0x7_FFFF_FFFF_F000;
#[cfg(feature = "intel_tdx")] } else {
const PHYS_ADDR_MASK: usize = 0x7_FFFF_FFFF_F000; const PHYS_ADDR_MASK: usize = 0xF_FFFF_FFFF_F000;
}
}
const PROP_MASK: usize = !Self::PHYS_ADDR_MASK & !PageTableFlags::HUGE.bits(); const PROP_MASK: usize = !Self::PHYS_ADDR_MASK & !PageTableFlags::HUGE.bits();
} }

View File

@ -14,11 +14,22 @@ pub(crate) mod pci;
pub mod qemu; pub mod qemu;
pub mod serial; pub mod serial;
pub mod task; pub mod task;
#[cfg(feature = "intel_tdx")]
pub(crate) mod tdx_guest;
pub mod timer; pub mod timer;
pub mod trap; pub mod trap;
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "intel_tdx")] {
pub(crate) mod tdx_guest;
use {
crate::early_println,
::tdx_guest::{init_tdx, tdcall::InitError, tdx_is_enabled},
};
}
}
use core::{ use core::{
arch::x86_64::{_rdrand64_step, _rdtsc}, arch::x86_64::{_rdrand64_step, _rdtsc},
sync::atomic::Ordering, sync::atomic::Ordering,
@ -26,11 +37,6 @@ use core::{
use kernel::apic::ioapic; use kernel::apic::ioapic;
use log::{info, warn}; use log::{info, warn};
#[cfg(feature = "intel_tdx")]
use {
crate::early_println,
::tdx_guest::{init_tdx, tdcall::InitError, tdx_is_enabled},
};
#[cfg(feature = "intel_tdx")] #[cfg(feature = "intel_tdx")]
pub(crate) fn check_tdx_init() { pub(crate) fn check_tdx_init() {
@ -79,18 +85,22 @@ pub(crate) fn init_on_bsp() {
timer::init(); timer::init();
#[cfg(feature = "intel_tdx")] cfg_if! {
if !tdx_is_enabled() { if #[cfg(feature = "intel_tdx")] {
match iommu::init() { if !tdx_is_enabled() {
Ok(_) => {} match iommu::init() {
Err(err) => warn!("IOMMU initialization error:{:?}", err), Ok(_) => {}
Err(err) => warn!("IOMMU initialization error:{:?}", err),
}
}
} else {
match iommu::init() {
Ok(_) => {}
Err(err) => warn!("IOMMU initialization error:{:?}", err),
}
} }
} }
#[cfg(not(feature = "intel_tdx"))]
match iommu::init() {
Ok(_) => {}
Err(err) => warn!("IOMMU initialization error:{:?}", err),
}
// Some driver like serial may use PIC // Some driver like serial may use PIC
kernel::pic::init(); kernel::pic::init();
} }

View File

@ -3,14 +3,11 @@
//! Handles trap. //! Handles trap.
use align_ext::AlignExt; use align_ext::AlignExt;
use cfg_if::cfg_if;
use log::debug; use log::debug;
#[cfg(feature = "intel_tdx")]
use tdx_guest::{tdcall, tdx_is_enabled};
use trapframe::TrapFrame; use trapframe::TrapFrame;
use super::ex_table::ExTable; use super::ex_table::ExTable;
#[cfg(feature = "intel_tdx")]
use crate::arch::{cpu::VIRTUALIZATION_EXCEPTION, tdx_guest::handle_virtual_exception};
use crate::{ use crate::{
cpu::{CpuException, CpuExceptionInfo, PageFaultErrorCode, PAGE_FAULT}, cpu::{CpuException, CpuExceptionInfo, PageFaultErrorCode, PAGE_FAULT},
cpu_local_cell, cpu_local_cell,
@ -23,6 +20,13 @@ use crate::{
trap::call_irq_callback_functions, trap::call_irq_callback_functions,
}; };
cfg_if! {
if #[cfg(feature = "intel_tdx")] {
use tdx_guest::{tdcall, tdx_is_enabled};
use crate::arch::{cpu::VIRTUALIZATION_EXCEPTION, tdx_guest::handle_virtual_exception};
}
}
cpu_local_cell! { cpu_local_cell! {
static IS_KERNEL_INTERRUPTED: bool = false; static IS_KERNEL_INTERRUPTED: bool = false;
} }
@ -134,14 +138,18 @@ fn handle_kernel_page_fault(f: &TrapFrame, page_fault_vaddr: u64) {
let vaddr = (page_fault_vaddr as usize).align_down(PAGE_SIZE); let vaddr = (page_fault_vaddr as usize).align_down(PAGE_SIZE);
let paddr = vaddr - LINEAR_MAPPING_BASE_VADDR; let paddr = vaddr - LINEAR_MAPPING_BASE_VADDR;
#[cfg(not(feature = "intel_tdx"))] cfg_if! {
let priv_flags = PrivFlags::GLOBAL; if #[cfg(feature = "intel_tdx")] {
#[cfg(feature = "intel_tdx")] let priv_flags = if tdx_is_enabled() {
let priv_flags = if tdx_is_enabled() { PrivFlags::SHARED | PrivFlags::GLOBAL
PrivFlags::SHARED | PrivFlags::GLOBAL } else {
} else { PrivFlags::GLOBAL
PrivFlags::GLOBAL };
}; } else {
let priv_flags = PrivFlags::GLOBAL;
}
}
// SAFETY: // SAFETY:
// 1. We have checked that the page fault address falls within the address range of the direct // 1. We have checked that the page fault address falls within the address range of the direct
// mapping of physical memory. // mapping of physical memory.

View File

@ -10,18 +10,22 @@ pub mod common_device;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::ops::Range; use core::ops::Range;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] use cfg_if::cfg_if;
use ::tdx_guest::tdx_is_enabled;
use log::debug; use log::debug;
use self::bus::MmioBus; use self::bus::MmioBus;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
use crate::arch::tdx_guest;
use crate::{ use crate::{
arch::kernel::IO_APIC, bus::mmio::common_device::MmioCommonDevice, mm::paddr_to_vaddr, arch::kernel::IO_APIC, bus::mmio::common_device::MmioCommonDevice, mm::paddr_to_vaddr,
sync::SpinLock, trap::IrqLine, sync::SpinLock, trap::IrqLine,
}; };
cfg_if! {
if #[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] {
use ::tdx_guest::tdx_is_enabled;
use crate::arch::tdx_guest;
}
}
const VIRTIO_MMIO_MAGIC: u32 = 0x74726976; const VIRTIO_MMIO_MAGIC: u32 = 0x74726976;
/// MMIO bus instance /// MMIO bus instance

View File

@ -7,11 +7,8 @@
use alloc::{sync::Arc, vec::Vec}; use alloc::{sync::Arc, vec::Vec};
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] use cfg_if::cfg_if;
use ::tdx_guest::tdx_is_enabled;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
use crate::arch::tdx_guest;
use crate::{ use crate::{
bus::pci::{ bus::pci::{
cfg_space::{Bar, Command, MemoryBar}, cfg_space::{Bar, Command, MemoryBar},
@ -22,6 +19,13 @@ use crate::{
trap::IrqLine, trap::IrqLine,
}; };
cfg_if! {
if #[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] {
use ::tdx_guest::tdx_is_enabled;
use crate::arch::tdx_guest;
}
}
/// MSI-X capability. It will set the BAR space it uses to be hidden. /// MSI-X capability. It will set the BAR space it uses to be hidden.
#[derive(Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]

View File

@ -3,12 +3,9 @@
use alloc::sync::Arc; use alloc::sync::Arc;
use core::ops::Deref; use core::ops::Deref;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] use cfg_if::cfg_if;
use ::tdx_guest::tdx_is_enabled;
use super::{check_and_insert_dma_mapping, remove_dma_mapping, DmaError, HasDaddr}; use super::{check_and_insert_dma_mapping, remove_dma_mapping, DmaError, HasDaddr};
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
use crate::arch::tdx_guest;
use crate::{ use crate::{
arch::{iommu, mm::tlb_flush_addr_range}, arch::{iommu, mm::tlb_flush_addr_range},
mm::{ mm::{
@ -21,6 +18,13 @@ use crate::{
prelude::*, prelude::*,
}; };
cfg_if! {
if #[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] {
use ::tdx_guest::tdx_is_enabled;
use crate::arch::tdx_guest;
}
}
/// A coherent (or consistent) DMA mapping, /// A coherent (or consistent) DMA mapping,
/// which guarantees that the device and the CPU can /// which guarantees that the device and the CPU can
/// access the data in parallel. /// access the data in parallel.

View File

@ -3,12 +3,9 @@
use alloc::sync::Arc; use alloc::sync::Arc;
use core::ops::Range; use core::ops::Range;
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] use cfg_if::cfg_if;
use ::tdx_guest::tdx_is_enabled;
use super::{check_and_insert_dma_mapping, remove_dma_mapping, DmaError, HasDaddr}; use super::{check_and_insert_dma_mapping, remove_dma_mapping, DmaError, HasDaddr};
#[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))]
use crate::arch::tdx_guest;
use crate::{ use crate::{
arch::iommu, arch::iommu,
error::Error, error::Error,
@ -18,6 +15,13 @@ use crate::{
}, },
}; };
cfg_if! {
if #[cfg(all(target_arch = "x86_64", feature = "intel_tdx"))] {
use ::tdx_guest::tdx_is_enabled;
use crate::arch::tdx_guest;
}
}
/// A streaming DMA mapping. Users must synchronize data /// A streaming DMA mapping. Users must synchronize data
/// before reading or after writing to ensure consistency. /// before reading or after writing to ensure consistency.
/// ///