openhcl_boot/
boot_logger.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Logging support for the bootshim.
5//!
6//! The bootshim performs no filtering of its logging messages when running in
7//! a confidential VM. This is because it runs before any keys can be accessed
8//! or any guest code is executed, and therefore it can not leak anything
9//! sensitive.
10
11#[cfg(target_arch = "x86_64")]
12use crate::arch::tdx::TdxIoAccess;
13use crate::host_params::shim_params::IsolationType;
14use crate::single_threaded::SingleThreaded;
15use core::cell::RefCell;
16use core::fmt;
17use core::fmt::Write;
18use memory_range::MemoryRange;
19#[cfg(target_arch = "x86_64")]
20use minimal_rt::arch::InstrIoAccess;
21use minimal_rt::arch::Serial;
22use string_page_buf::StringBuffer;
23
24enum Logger {
25    #[cfg(target_arch = "x86_64")]
26    Serial(Serial<InstrIoAccess>),
27    #[cfg(target_arch = "aarch64")]
28    Serial(Serial),
29    #[cfg(target_arch = "x86_64")]
30    TdxSerial(Serial<TdxIoAccess>),
31    None,
32}
33
34impl Logger {
35    fn write_str(&mut self, s: &str) -> fmt::Result {
36        match self {
37            Logger::Serial(serial) => serial.write_str(s),
38            #[cfg(target_arch = "x86_64")]
39            Logger::TdxSerial(serial) => serial.write_str(s),
40            Logger::None => Ok(()),
41        }
42    }
43}
44
45pub struct BootLogger {
46    logger: SingleThreaded<RefCell<Logger>>,
47    in_memory_logger: SingleThreaded<RefCell<Option<StringBuffer<'static>>>>,
48}
49
50pub static BOOT_LOGGER: BootLogger = BootLogger {
51    logger: SingleThreaded(RefCell::new(Logger::None)),
52    in_memory_logger: SingleThreaded(RefCell::new(None)),
53};
54
55/// Initialize the in-memory log buffer. This range must be identity mapped, and
56/// unused by anything else.
57pub fn boot_logger_memory_init(buffer: MemoryRange) {
58    if buffer.is_empty() {
59        return;
60    }
61
62    let log_buffer_ptr = buffer.start() as *mut u8;
63    // SAFETY: At file build time, this range is enforced to be unused by
64    // anything else. The rest of the bootshim will mark this range as reserved
65    // and not free to be used by anything else.
66    //
67    // The VA is valid as we are identity mapped.
68    let log_buffer_slice =
69        unsafe { core::slice::from_raw_parts_mut(log_buffer_ptr, buffer.len() as usize) };
70
71    *BOOT_LOGGER.in_memory_logger.borrow_mut() = Some(
72        StringBuffer::new(log_buffer_slice)
73            .expect("log buffer should be valid from fixed at build config"),
74    );
75}
76
77/// Initialize the runtime boot logger, for logging to serial or other outputs.
78///
79/// If a runtime logger was initialized, emit any in-memory log to the
80/// configured runtime output.
81pub fn boot_logger_runtime_init(isolation_type: IsolationType, com3_serial_available: bool) {
82    let mut logger = BOOT_LOGGER.logger.borrow_mut();
83
84    *logger = match (isolation_type, com3_serial_available) {
85        #[cfg(target_arch = "x86_64")]
86        (IsolationType::None, true) => Logger::Serial(Serial::init(InstrIoAccess)),
87        #[cfg(target_arch = "aarch64")]
88        (IsolationType::None, true) => Logger::Serial(Serial::init()),
89        #[cfg(target_arch = "x86_64")]
90        (IsolationType::Tdx, true) => Logger::TdxSerial(Serial::init(TdxIoAccess)),
91        _ => Logger::None,
92    };
93
94    // Emit any in-memory log to the runtime logger.
95    if let Some(buf) = BOOT_LOGGER.in_memory_logger.borrow_mut().as_mut() {
96        let _ = logger.write_str(buf.contents());
97    }
98}
99
100impl Write for &BootLogger {
101    fn write_str(&mut self, s: &str) -> fmt::Result {
102        if let Some(buf) = self.in_memory_logger.borrow_mut().as_mut() {
103            // Ignore the errors from the in memory logger.
104            let _ = buf.append(s);
105        }
106        self.logger.borrow_mut().write_str(s)
107    }
108}
109
110impl log::Log for BootLogger {
111    fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool {
112        // TODO: filter level
113        true
114    }
115
116    fn log(&self, record: &log::Record<'_>) {
117        let _ = writeln!(&*self, "[{}] {}", record.level(), record.args());
118    }
119
120    fn flush(&self) {}
121}