Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace loaderlog with log crate #153

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion src/arch/aarch64/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use core::arch::{asm, global_asm};

use log::info;

extern "C" {
fn loader_main();
}
Expand Down Expand Up @@ -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),);
Expand Down
3 changes: 2 additions & 1 deletion src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
Expand Down
21 changes: 11 additions & 10 deletions src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<BasePageSize>(page_address, page_address, 1, PageTableEntryFlags::WRITABLE);

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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::<BasePageSize>(
Expand Down Expand Up @@ -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
);
Expand Down
7 changes: 4 additions & 3 deletions src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use goblin::{
},
};
use hermit_entry::TlsInfo;
use log::info;
use plain::Plain;

/// A parsed kernel object ready for loading.
Expand Down Expand Up @@ -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::<Header>(elf).unwrap();
Expand Down Expand Up @@ -199,7 +200,7 @@ impl<'a> Object<'a> {

/// Loads the kernel into the provided memory.
pub fn load_kernel(&self, memory: &mut [MaybeUninit<u8>]) -> LoadInfo {
loaderlog!("Loading kernel to {memory:p}");
info!("Loading kernel to {memory:p}");

assert!(memory.len() >= self.mem_size());

Expand Down Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 0 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod macros;
mod arch;
mod console;
mod kernel;
mod log;
#[cfg(target_os = "none")]
mod none;
#[cfg(target_os = "uefi")]
Expand Down
8 changes: 5 additions & 3 deletions src/none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down