Don't force every printer to use if_tdx_enabled

This commit is contained in:
Ruihan Li
2025-03-20 23:37:54 +08:00
committed by Junyang Zhang
parent 92722aaebb
commit d56b7fa6ff
7 changed files with 19 additions and 31 deletions

View File

@ -33,6 +33,8 @@ impl Write for Stdout {
/// Initializes the serial port. /// Initializes the serial port.
pub(crate) fn init() {} pub(crate) fn init() {}
pub(crate) fn callback_init() {}
/// Sends a byte on the serial port. /// Sends a byte on the serial port.
pub fn send(data: u8) { pub fn send(data: u8) {
sbi_rt::console_write_byte(data); sbi_rt::console_write_byte(data);

View File

@ -41,11 +41,9 @@ use log::{info, warn};
#[cfg(feature = "cvm_guest")] #[cfg(feature = "cvm_guest")]
pub(crate) fn init_cvm_guest() { pub(crate) fn init_cvm_guest() {
use ::tdx_guest::serial_println;
match init_tdx() { match init_tdx() {
Ok(td_info) => { Ok(td_info) => {
serial_println!( crate::early_println!(
"[kernel] Intel TDX initialized\n[kernel] td gpaw: {}, td attributes: {:?}", "[kernel] Intel TDX initialized\n[kernel] td gpaw: {}, td attributes: {:?}",
td_info.gpaw, td_info.gpaw,
td_info.attributes td_info.attributes
@ -89,7 +87,6 @@ pub(crate) unsafe fn late_init_on_bsp() {
kernel::pic::enable(); kernel::pic::enable();
} }
} }
serial::callback_init();
kernel::tsc::init_tsc_freq(); kernel::tsc::init_tsc_freq();
timer::init_bsp(); timer::init_bsp();

View File

@ -4,9 +4,15 @@
use core::fmt::Arguments; use core::fmt::Arguments;
use crate::if_tdx_enabled;
/// Prints formatted arguments to the console. /// Prints formatted arguments to the console.
pub fn early_print(args: Arguments) { pub fn early_print(args: Arguments) {
if_tdx_enabled!({
tdx_guest::print(args);
} else {
crate::arch::serial::print(args); crate::arch::serial::print(args);
});
} }
/// Prints to the console. /// Prints to the console.

View File

@ -113,6 +113,7 @@ unsafe fn init() {
if_tdx_enabled!({ if_tdx_enabled!({
arch::serial::init(); arch::serial::init();
}); });
arch::serial::callback_init();
smp::init(); smp::init();

View File

@ -10,7 +10,7 @@ use super::{meta::AnyFrameMeta, segment::Segment, Frame};
use crate::{ use crate::{
boot::memory_region::MemoryRegionType, boot::memory_region::MemoryRegionType,
error::Error, error::Error,
if_tdx_enabled, impl_frame_meta_for, impl_frame_meta_for,
mm::{paddr_to_vaddr, Paddr, PAGE_SIZE}, mm::{paddr_to_vaddr, Paddr, PAGE_SIZE},
prelude::*, prelude::*,
util::range_difference, util::range_difference,
@ -212,13 +212,7 @@ pub(crate) unsafe fn init() {
// Truncate the early allocated frames if there is an overlap. // Truncate the early allocated frames if there is an overlap.
for r1 in range_difference(&(region.base()..region.end()), &range_1) { for r1 in range_difference(&(region.base()..region.end()), &range_1) {
for r2 in range_difference(&r1, &range_2) { for r2 in range_difference(&r1, &range_2) {
if_tdx_enabled!({
use tdx_guest::serial_println;
serial_println!("Adding free frames to the allocator: {:x?}", r2);
} else {
log::info!("Adding free frames to the allocator: {:x?}", r2); log::info!("Adding free frames to the allocator: {:x?}", r2);
});
get_global_frame_allocator().add_free_memory(r2.start, r2.len()); get_global_frame_allocator().add_free_memory(r2.start, r2.len());
} }
} }

View File

@ -53,7 +53,7 @@ use log::info;
use crate::{ use crate::{
arch::mm::PagingConsts, arch::mm::PagingConsts,
const_assert, if_tdx_enabled, const_assert,
mm::{ mm::{
frame::allocator::{self, EarlyAllocatedFrameMeta}, frame::allocator::{self, EarlyAllocatedFrameMeta},
kspace::LINEAR_MAPPING_BASE_VADDR, kspace::LINEAR_MAPPING_BASE_VADDR,
@ -447,15 +447,10 @@ pub(crate) unsafe fn init() -> Segment<MetaPageMeta> {
regions.iter().map(|r| r.base() + r.len()).max().unwrap() regions.iter().map(|r| r.base() + r.len()).max().unwrap()
}; };
if_tdx_enabled!({ info!(
use tdx_guest::serial_println; "Initializing frame metadata for physical memory up to {:x}",
max_paddr
serial_println!("Initializing frame metadata for physical memory up to {:x}", );
max_paddr)
} else {
info!("Initializing frame metadata for physical memory up to {:x}",
max_paddr);
});
add_temp_linear_mapping(max_paddr); add_temp_linear_mapping(max_paddr);

View File

@ -59,7 +59,6 @@ use super::{
use crate::{ use crate::{
arch::mm::{PageTableEntry, PagingConsts}, arch::mm::{PageTableEntry, PagingConsts},
boot::memory_region::MemoryRegionType, boot::memory_region::MemoryRegionType,
if_tdx_enabled,
}; };
/// The shortest supported address width is 39 bits. And the literal /// The shortest supported address width is 39 bits. And the literal
@ -133,13 +132,7 @@ pub static KERNEL_PAGE_TABLE: Once<PageTable<KernelMode, PageTableEntry, PagingC
/// This function should be called before: /// This function should be called before:
/// - any initializer that modifies the kernel page table. /// - any initializer that modifies the kernel page table.
pub fn init_kernel_page_table(meta_pages: Segment<MetaPageMeta>) { pub fn init_kernel_page_table(meta_pages: Segment<MetaPageMeta>) {
if_tdx_enabled!({
use tdx_guest::serial_println;
serial_println!("Initializing the kernel page table");
} else {
info!("Initializing the kernel page table"); info!("Initializing the kernel page table");
});
let regions = &crate::boot::EARLY_INFO.get().unwrap().memory_regions; let regions = &crate::boot::EARLY_INFO.get().unwrap().memory_regions;
let phys_mem_cap = regions.iter().map(|r| r.base() + r.len()).max().unwrap(); let phys_mem_cap = regions.iter().map(|r| r.base() + r.len()).max().unwrap();