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

log: Introduce log crate #339

Merged
merged 3 commits into from
Jul 7, 2024
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
7 changes: 7 additions & 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 @@ -30,6 +30,7 @@ bitflags = "2.6.0"
atomic_refcell = "0.1.13"
r-efi = { version = "4.5.0", features = ["efiapi"] }
heapless = "0.8.0"
log = "0.4.22"

[target.'cfg(target_arch = "aarch64")'.dependencies]
tock-registers = "0.9.0"
Expand Down
5 changes: 3 additions & 2 deletions src/arch/x86_64/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2020 Google LLC

use core::cell::SyncUnsafeCell;
use log::info;
use x86_64::{
registers::control::Cr3,
structures::paging::{PageSize, PageTable, PageTableFlags, PhysFrame, Size2MiB},
Expand All @@ -25,7 +26,7 @@ pub fn setup() {
// SAFETY: This function is idempontent and only writes to static memory and
// CR3. Thus, it is safe to run multiple times or on multiple threads.
let (l4, l3, l2s) = unsafe { (L4_TABLE.get_mut(), L3_TABLE.get_mut(), L2_TABLES.get_mut()) };
log!("Setting up {} GiB identity mapping", ADDRESS_SPACE_GIB);
info!("Setting up {} GiB identity mapping", ADDRESS_SPACE_GIB);
let pt_flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;

// Setup Identity map using L2 huge pages
Expand All @@ -51,7 +52,7 @@ pub fn setup() {
if cr3_frame != l4_frame {
unsafe { Cr3::write(l4_frame, cr3_flags) };
}
log!("Page tables setup");
info!("Page tables setup");
}

// Map a virtual address to a PhysAddr (assumes identity mapping)
Expand Down
3 changes: 2 additions & 1 deletion src/efi/device_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::{
ptr::null_mut,
};

use log::error;
use r_efi::{
efi::{self, MemoryType, Status},
protocols::device_path::Protocol as DevicePathProtocol,
Expand Down Expand Up @@ -49,7 +50,7 @@ impl DevicePath {
}

if dpp.r#type == r_efi::protocols::device_path::TYPE_END && dpp.sub_type == 0xff {
log!("Unexpected end of device path");
error!("Unexpected end of device path");
return DevicePath::Unsupported;
}
let len = unsafe { core::mem::transmute::<[u8; 2], u16>(dpp.length) };
Expand Down
3 changes: 2 additions & 1 deletion src/efi/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use core::ffi::c_void;

use log::error;
use r_efi::{
efi::{self, Char16, Guid, Status},
protocols::{
Expand Down Expand Up @@ -51,7 +52,7 @@ pub extern "efiapi" fn open(
match &wrapper.node {
crate::fat::Node::Directory(d) => d,
_ => {
log!("Attempt to open from non-directory is unsupported");
error!("Attempt to open from non-directory is unsupported");
return Status::UNSUPPORTED;
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Akira Moroo

pub struct Logger;

impl log::Log for Logger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= log::Level::Info
}

fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
log!("[{}] {}", record.level(), record.args());
}
}

fn flush(&self) {}
}

pub fn init() {
log::set_logger(&Logger).expect("Failed to set logger");
log::set_max_level(log::LevelFilter::Info);
}
39 changes: 22 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#[cfg(all(not(test), not(feature = "integration_tests")))]
use core::panic::PanicInfo;

use log::{error, info, warn};
#[cfg(all(
not(test),
not(feature = "integration_tests"),
Expand Down Expand Up @@ -55,6 +56,7 @@ mod fdt;
mod integration;
mod layout;
mod loader;
mod logger;
mod mem;
mod part;
mod pci;
Expand Down Expand Up @@ -111,70 +113,70 @@ fn boot_from_device(
info: &dyn bootinfo::Info,
) -> Result<(), Error> {
if let Err(err) = device.init() {
log!("Error configuring block device: {:?}", err);
error!("Error configuring block device: {:?}", err);
return Err(Error::Virtio(err));
}
log!(
info!(
"Virtio block device configured. Capacity: {} sectors",
device.get_capacity()
);

let (start, end) = match part::find_efi_partition(device) {
Ok(p) => p,
Err(err) => {
log!("Failed to find EFI partition: {:?}", err);
error!("Failed to find EFI partition: {:?}", err);
return Err(Error::Partition(err));
}
};
log!("Found EFI partition");
info!("Found EFI partition");

let mut f = fat::Filesystem::new(device, start, end);
if let Err(err) = f.init() {
log!("Failed to create filesystem: {:?}", err);
error!("Failed to create filesystem: {:?}", err);
return Err(Error::Fat(err));
}
log!("Filesystem ready");
info!("Filesystem ready");

match loader::load_default_entry(&f, info) {
Ok(mut kernel) => {
log!("Jumping to kernel");
info!("Jumping to kernel");
kernel.boot();
return Ok(());
}
Err(err) => {
log!("Error loading default entry: {:?}", err);
warn!("Error loading default entry: {:?}", err);
// Fall through to EFI boot
}
}

log!("Using EFI boot.");
info!("Using EFI boot.");

let mut file = match f.open(efi::EFI_BOOT_PATH) {
Ok(file) => file,
Err(err) => {
log!("Failed to load default EFI binary: {:?}", err);
error!("Failed to load default EFI binary: {:?}", err);
return Err(Error::Fat(err));
}
};
log!("Found bootloader: {}", efi::EFI_BOOT_PATH);
info!("Found bootloader: {}", efi::EFI_BOOT_PATH);

let mut l = pe::Loader::new(&mut file);

let (entry_addr, load_addr, size) = match l.load(info.kernel_load_addr()) {
Ok(load_info) => load_info,
Err(err) => {
log!("Error loading executable: {:?}", err);
error!("Error loading executable: {:?}", err);
return Err(Error::Pe(err));
}
};

#[cfg(target_arch = "aarch64")]
if code_range().start < (info.kernel_load_addr() + size) as usize {
log!("Error Boot Image is too large");
error!("Error Boot Image is too large");
return Err(Error::ImageTooLarge);
}

log!("Executable loaded");
info!("Executable loaded");
efi::efi_exec(entry_addr, load_addr, size, info, &f, device);
Ok(())
}
Expand All @@ -183,6 +185,7 @@ fn boot_from_device(
#[no_mangle]
pub extern "C" fn rust64_start(#[cfg(not(feature = "coreboot"))] pvh_info: &pvh::StartInfo) -> ! {
serial::PORT.borrow_mut().init();
logger::init();

arch::x86_64::sse::enable_sse();
arch::x86_64::paging::setup();
Expand All @@ -204,6 +207,7 @@ pub extern "C" fn rust64_start(x0: *const u8) -> ! {

// Use atomic operation before MMU enabled may cause exception, see https://www.ipshop.xyz/5909.html
serial::PORT.borrow_mut().init();
logger::init();

let info = fdt::StartInfo::new(
x0,
Expand All @@ -226,8 +230,9 @@ pub extern "C" fn rust64_start(a0: u64, a1: *const u8) -> ! {
use crate::bootinfo::{EntryType, Info, MemoryEntry};

serial::PORT.borrow_mut().init();
logger::init();

log!("Starting on RV64 0x{:x} 0x{:x}", a0, a1 as u64,);
info!("Starting on RV64 0x{:x} 0x{:x}", a0, a1 as u64,);

let info = fdt::StartInfo::new(
a1,
Expand All @@ -243,7 +248,7 @@ pub extern "C" fn rust64_start(a0: u64, a1: *const u8) -> ! {

for i in 0..info.num_entries() {
let region = info.entry(i);
log!(
info!(
"Memory region {}MiB@0x{:x}",
region.size / 1024 / 1024,
region.addr
Expand All @@ -258,7 +263,7 @@ pub extern "C" fn rust64_start(a0: u64, a1: *const u8) -> ! {
}

fn main(info: &dyn bootinfo::Info) -> ! {
log!("\nBooting with {}", info.name());
info!("Booting with {}", info.name());

pci::print_bus();

Expand Down
29 changes: 10 additions & 19 deletions src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use atomic_refcell::AtomicRefCell;

use log::{info, warn};
#[cfg(target_arch = "x86_64")]
use x86_64::instructions::port::{Port, PortWriteOnly};

Expand Down Expand Up @@ -152,11 +153,9 @@ pub fn print_bus() {
if vendor_id == INVALID_VENDOR_ID {
continue;
}
log!(
info!(
"Found PCI device vendor={:x} device={:x} in slot={}",
vendor_id,
device_id,
device
vendor_id, device_id, device
);
}
}
Expand Down Expand Up @@ -251,13 +250,9 @@ impl PciDevice {
self.vendor_id = vendor_id;
self.device_id = device_id;

log!(
info!(
"PCI Device: {}:{}.{} {:x}:{:x}",
self.bus,
self.device,
self.func,
self.vendor_id,
self.device_id
self.bus, self.device, self.func, self.vendor_id, self.device_id
);

// Enable responses in memory space
Expand Down Expand Up @@ -328,11 +323,9 @@ impl PciDevice {

#[allow(clippy::disallowed_names)]
for bar in &self.bars {
log!(
info!(
"Bar: type={:?} address=0x{:x} size=0x{:x}",
bar.bar_type,
bar.address,
bar.size
bar.bar_type, bar.address, bar.size
);
}
}
Expand Down Expand Up @@ -379,11 +372,9 @@ impl PciDevice {

#[allow(clippy::disallowed_names)]
for bar in &self.bars {
log!(
info!(
"Updated BARs: type={:?} address={:x} size={:x}",
bar.bar_type,
bar.address,
bar.size
bar.bar_type, bar.address, bar.size
);
}

Expand Down Expand Up @@ -445,7 +436,7 @@ impl VirtioTransport for VirtioPciTransport {

// bit 4 of status is capability bit
if status & 1 << 4 == 0 {
log!("No capabilities detected");
warn!("No capabilities detected");
return Err(VirtioError::UnsupportedDevice);
}

Expand Down
Loading