From db6356bf6f0a000030c68fb5c260587d803f1e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Mon, 1 Aug 2022 15:32:30 +0200 Subject: [PATCH] Replace loaderlog with log crate --- Cargo.lock | 1 + Cargo.toml | 1 + src/arch/aarch64/entry.rs | 4 +++- src/arch/aarch64/mod.rs | 3 ++- src/arch/x86_64/mod.rs | 21 +++++++++++---------- src/kernel.rs | 7 ++++--- src/log.rs | 31 +++++++++++++++++++++++++++++++ src/macros.rs | 8 -------- src/main.rs | 1 + src/none.rs | 8 +++++--- 10 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 src/log.rs diff --git a/Cargo.lock b/Cargo.lock index 0e16d7e8..9d22c2ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,6 +166,7 @@ dependencies = [ "cc", "goblin", "hermit-entry", + "log", "multiboot", "nasm-rs", "plain", diff --git a/Cargo.toml b/Cargo.toml index 134cd921..50e47caf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" [dependencies] goblin = { version = "0.5", default-features = false, features = ["elf64"] } hermit-entry = { version = "0.6", features = ["loader"] } +log = "0.4" plain = "0.2" [target.'cfg(all(target_os = "none", target_arch = "x86_64"))'.dependencies] diff --git a/src/arch/aarch64/entry.rs b/src/arch/aarch64/entry.rs index a9ea9ce1..8c37bfaf 100644 --- a/src/arch/aarch64/entry.rs +++ b/src/arch/aarch64/entry.rs @@ -2,6 +2,8 @@ use core::arch::{asm, global_asm}; +use log::info; + extern "C" { fn loader_main(); } @@ -55,7 +57,7 @@ pub unsafe fn _start_rust() -> ! { } unsafe fn pre_init() -> ! { - loaderlog!("Enter startup code"); + info!("Enter startup code"); /* disable interrupts */ asm!("msr daifset, 0b111", options(nostack),); diff --git a/src/arch/aarch64/mod.rs b/src/arch/aarch64/mod.rs index b47297af..58155d7b 100644 --- a/src/arch/aarch64/mod.rs +++ b/src/arch/aarch64/mod.rs @@ -5,6 +5,7 @@ pub mod serial; use core::arch::asm; use hermit_entry::{BootInfo, Entry, PlatformInfo, RawBootInfo, SerialPortBase, TlsInfo}; +use log::info; use crate::arch::paging::*; use crate::arch::serial::SerialPort; @@ -143,7 +144,7 @@ pub unsafe fn boot_kernel( }; // Jump to the kernel entry point and provide the Multiboot information to it. - loaderlog!( + info!( "Jumping to HermitCore Application Entry Point at {:#x}", entry_point ); diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index 5fb0d4c5..a893a5fd 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -7,6 +7,7 @@ use core::ptr::{copy, write_bytes}; use core::{cmp, mem, slice}; use hermit_entry::{BootInfo, Entry, PlatformInfo, RawBootInfo, SerialPortBase, TlsInfo}; +use log::info; #[cfg(target_os = "none")] use multiboot::information::{MemoryManagement, Multiboot, PAddr}; use uart_16550::SerialPort; @@ -81,7 +82,7 @@ pub unsafe fn boot_kernel( pub unsafe fn find_kernel() -> &'static [u8] { // Identity-map the Multiboot information. assert!(mb_info > 0, "Could not find Multiboot information"); - loaderlog!("Found Multiboot information at {:#x}", mb_info); + info!("Found Multiboot information at {:#x}", mb_info); let page_address = align_down!(mb_info, BasePageSize::SIZE); paging::map::(page_address, page_address, 1, PageTableEntryFlags::WRITABLE); @@ -115,10 +116,10 @@ pub unsafe fn find_kernel() -> &'static [u8] { } } - loaderlog!("Found module: [{:#x} - {:#x}]", start_address, end_address); + info!("Found module: [{:#x} - {:#x}]", start_address, end_address); let elf_start = start_address; let elf_len = end_address - start_address; - loaderlog!("Module length: {:#x}", elf_len); + info!("Module length: {:#x}", elf_len); let free_memory_address = align_up!(end_address, LargePageSize::SIZE); // TODO: Workaround for https://github.com/hermitcore/rusty-loader/issues/96 @@ -132,11 +133,11 @@ pub unsafe fn find_kernel() -> &'static [u8] { "Could not find a single module in the Multiboot information" ); assert!(start_address > 0); - loaderlog!("Found an ELF module at {:#x}", start_address); + info!("Found an ELF module at {:#x}", start_address); let page_address = align_down!(start_address, BasePageSize::SIZE); let counter = (align_up!(start_address, LargePageSize::SIZE) - page_address) / BasePageSize::SIZE; - loaderlog!( + info!( "Map {} pages at {:#x} (page size {} KByte)", counter, page_address, @@ -153,7 +154,7 @@ pub unsafe fn find_kernel() -> &'static [u8] { let address = align_up!(start_address, LargePageSize::SIZE); let counter = (align_up!(end_address, LargePageSize::SIZE) - address) / LargePageSize::SIZE; if counter > 0 { - loaderlog!( + info!( "Map {} pages at {:#x} (page size {} KByte)", counter, address, @@ -176,7 +177,7 @@ pub unsafe fn boot_kernel( let new_addr = match elf_address { Some(addr) => { if virtual_address != addr { - loaderlog!("Copy kernel from {:#x} to {:#x}", virtual_address, addr); + info!("Copy kernel from {:#x} to {:#x}", virtual_address, addr); // copy app to the new start address copy( @@ -222,7 +223,7 @@ pub unsafe fn boot_kernel( } let current_stack_address = new_stack.try_into().unwrap(); - loaderlog!("Use stack address {:#x}", current_stack_address); + info!("Use stack address {:#x}", current_stack_address); // map stack in the address space paging::map::( @@ -257,10 +258,10 @@ pub unsafe fn boot_kernel( raw_boot_info }; - loaderlog!("BootInfo located at {:#x}", &BOOT_INFO as *const _ as u64); + info!("BootInfo located at {:#x}", &BOOT_INFO as *const _ as u64); // Jump to the kernel entry point and provide the Multiboot information to it. - loaderlog!( + info!( "Jumping to HermitCore Application Entry Point at {:#x}", entry_point ); diff --git a/src/kernel.rs b/src/kernel.rs index 0f126294..81f5615f 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -18,6 +18,7 @@ use goblin::{ }, }; use hermit_entry::TlsInfo; +use log::info; use plain::Plain; /// A parsed kernel object ready for loading. @@ -80,7 +81,7 @@ impl<'a> Object<'a> { { let range = elf.as_ptr_range(); let len = elf.len(); - loaderlog!("Parsing kernel from ELF at {range:?} ({len} B)"); + info!("Parsing kernel from ELF at {range:?} ({len} B)"); } let header = plain::from_bytes::
(elf).unwrap(); @@ -199,7 +200,7 @@ impl<'a> Object<'a> { /// Loads the kernel into the provided memory. pub fn load_kernel(&self, memory: &mut [MaybeUninit]) -> LoadInfo { - loaderlog!("Loading kernel to {memory:p}"); + info!("Loading kernel to {memory:p}"); assert!(memory.len() >= self.mem_size()); @@ -287,6 +288,6 @@ fn parse_tls_info(header: &Header, ph: &ProgramHeader, start_addr: u64) -> TlsIn }; let range = tls_info.start as *const ()..(tls_info.start + tls_info.memsz) as *const (); let len = tls_info.memsz; - loaderlog!("TLS is at {range:?} ({len} B)",); + info!("TLS is at {range:?} ({len} B)",); tls_info } diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 00000000..d1f4fe46 --- /dev/null +++ b/src/log.rs @@ -0,0 +1,31 @@ +use log::{Level, LevelFilter, Metadata, Record}; + +struct Logger; + +impl log::Log for Logger { + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let level = option_env!("LOADER_LOG") + .map(|var| var.parse().unwrap()) + .unwrap_or(Level::Info); + metadata.level() <= level + } + + fn log(&self, record: &Record<'_>) { + if self.enabled(record.metadata()) { + let level = record.level(); + let args = record.args(); + println!("[LOADER][{level}] {args}"); + } + } + + fn flush(&self) {} +} + +pub fn init() { + static LOGGER: Logger = Logger; + log::set_logger(&LOGGER).unwrap(); + let level_filter = option_env!("LOADER_LOG") + .map(|var| var.parse().unwrap()) + .unwrap_or(LevelFilter::Info); + log::set_max_level(level_filter); +} diff --git a/src/macros.rs b/src/macros.rs index 0d14b9a4..f655f6f4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -32,14 +32,6 @@ macro_rules! println { }}; } -/// Print formatted loader log messages to our console, followed by a newline. -#[macro_export] -macro_rules! loaderlog { - ($($arg:tt)*) => {{ - print!("[LOADER] {}\n", ::core::format_args!($($arg)*)) - }}; -} - /// Prints and returns the value of a given expression for quick and dirty /// debugging. // Copied from std/macros.rs diff --git a/src/main.rs b/src/main.rs index 25f81570..9f7451ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ mod macros; mod arch; mod console; mod kernel; +mod log; #[cfg(target_os = "none")] mod none; #[cfg(target_os = "uefi")] diff --git a/src/none.rs b/src/none.rs index 3374cf64..0b9cb62f 100644 --- a/src/none.rs +++ b/src/none.rs @@ -4,6 +4,8 @@ use crate::kernel::{LoadInfo, Object}; use core::{fmt::Write, mem::MaybeUninit, ptr::addr_of_mut, slice}; +use log::info; + extern "C" { static kernel_end: u8; static kernel_start: u8; @@ -15,11 +17,11 @@ extern "C" { unsafe extern "C" fn loader_main() -> ! { init_bss(); arch::message_output_init(); + crate::log::init(); - loaderlog!( + info!( "Loader: [{:#x} - {:#x}]", - &kernel_start as *const u8 as usize, - &kernel_end as *const u8 as usize + &kernel_start as *const u8 as usize, &kernel_end as *const u8 as usize ); let kernel = Object::parse(arch::find_kernel());