diff --git a/Cargo.lock b/Cargo.lock index 7eddfcf..5de4289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,11 @@ name = "cc" version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "goblin" version = "0.2.1" @@ -31,6 +36,7 @@ dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "goblin 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "linked_list_allocator 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "paste 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "raw-cpuid 8.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.2.0", @@ -64,6 +70,14 @@ dependencies = [ "scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "log" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "paste" version = "0.1.18" @@ -194,10 +208,12 @@ dependencies = [ "checksum bit_field 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a165d606cf084741d4ac3a28fb6e9b1eb0bd31f6cd999098cfddb0b2ab381dc0" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum cc 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)" = "c3d87b23d6a92cd03af510a5ade527033f6aa6fa92161e2d5863a907d4c5e31d" +"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum goblin 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ddd5e3132801a1ac34ac53b97acde50c4685414dd2f291b9ea52afa6f07468c8" "checksum linked_list_allocator 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "47de1a43fad0250ee197e9e124e5b5deab3d7b39d4428ae8a6d741ceb340c362" "checksum linked_list_allocator 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e70e46c13c0e8374c26cec5752e3347ca1087d9711de8f45aa513a7700efd73d" "checksum lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" +"checksum log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" "checksum paste 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" "checksum paste-impl 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" "checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" diff --git a/Cargo.toml b/Cargo.toml index 8630f68..9e650f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ crate-type = ["staticlib"] [dependencies] bitflags = "1.2.1" linked_list_allocator = "0.8.4" +log = { version = "0.4" } raw-cpuid = "8.0.0" redox_syscall = { path = "syscall" } slab_allocator = { path = "slab_allocator", optional = true } diff --git a/src/acpi/mod.rs b/src/acpi/mod.rs index 25e64eb..a604e14 100644 --- a/src/acpi/mod.rs +++ b/src/acpi/mod.rs @@ -12,6 +12,7 @@ use spin::RwLock; use crate::stop::kstop; +use crate::log::info; use crate::memory::Frame; use crate::paging::{ActivePageTable, Page, PhysicalAddress, VirtualAddress}; use crate::paging::entry::EntryFlags; @@ -124,7 +125,7 @@ pub unsafe fn init(active_table: &mut ActivePageTable, already_supplied_rsdps: O // Search for RSDP if let Some(rsdp) = RSDP::get_rsdp(active_table, already_supplied_rsdps) { - println!("RSDP: {:?}", rsdp); + info!("RSDP: {:?}", rsdp); let rxsdt = get_sdt(rsdp.sdt_address(), active_table); for &c in rxsdt.signature.iter() { diff --git a/src/arch/x86_64/start.rs b/src/arch/x86_64/start.rs index 5696997..505d089 100644 --- a/src/arch/x86_64/start.rs +++ b/src/arch/x86_64/start.rs @@ -17,7 +17,7 @@ use crate::device; use crate::gdt; use crate::idt; use crate::interrupt; -use crate::log; +use crate::log::{self, info}; use crate::memory; use crate::paging; @@ -83,10 +83,23 @@ pub unsafe extern fn kstart(args_ptr: *const KernelArgs) -> ! { KERNEL_BASE.store(kernel_base, Ordering::SeqCst); KERNEL_SIZE.store(kernel_size, Ordering::SeqCst); - println!("Kernel: {:X}:{:X}", kernel_base, kernel_base + kernel_size); - println!("Stack: {:X}:{:X}", stack_base, stack_base + stack_size); - println!("Env: {:X}:{:X}", env_base, env_base + env_size); - println!("RSDPs: {:X}:{:X}", acpi_rsdps_base, acpi_rsdps_base + acpi_rsdps_size); + // Initialize logger + log::init_logger(|r| { + use core::fmt::Write; + let _ = write!( + crate::arch::x86_64::debug::Writer::new(), + "{}:{} -- {}\n", + r.target(), + r.level(), + r.args() + ); + }); + + info!("Redox OS starting..."); + info!("Kernel: {:X}:{:X}", kernel_base, kernel_base + kernel_size); + info!("Stack: {:X}:{:X}", stack_base, stack_base + stack_size); + info!("Env: {:X}:{:X}", env_base, env_base + env_size); + info!("RSDPs: {:X}:{:X}", acpi_rsdps_base, acpi_rsdps_base + acpi_rsdps_size); let ext_mem_ranges = if args.acpi_rsdps_base != 0 && args.acpi_rsdps_size > 0 { Some([(acpi_rsdps_base as usize, acpi_rsdps_size as usize)]) diff --git a/src/log.rs b/src/log.rs index d11dbfa..e1dd1aa 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1,4 +1,5 @@ use alloc::collections::VecDeque; +use core::sync::atomic::{AtomicBool, Ordering}; use spin::Mutex; pub static LOG: Mutex> = Mutex::new(None); @@ -33,3 +34,42 @@ impl Log { } } } + +struct RedoxLogger { + log_func: fn(&log::Record), + pub initialized: AtomicBool, +} + +impl ::log::Log for RedoxLogger { + fn enabled(&self, _: &log::Metadata<'_>) -> bool { + false + } + fn log(&self, record: &log::Record<'_>) { + (self.log_func)(&record) + } + fn flush(&self) {} +} + +pub fn init_logger(func: fn(&log::Record)) { + unsafe { + match LOGGER.initialized.load(Ordering::SeqCst) { + false => { + ::log::set_max_level(::log::LevelFilter::Info); + LOGGER.log_func = func; + match ::log::set_logger(&LOGGER) { + Ok(_) => ::log::info!("Logger initialized."), + Err(e) => println!("Logger setup failed! error: {}", e), + } + LOGGER.initialized.store(true, Ordering::SeqCst); + }, + true => ::log::info!("Tried to reinitialize the logger, which is not possible. Ignoring."), + } + } +} + +static mut LOGGER: RedoxLogger = RedoxLogger { + log_func: |_| {}, + initialized: AtomicBool::new(false), +}; + +pub use log::{debug, error, info, set_max_level, warn}; diff --git a/src/memory/mod.rs b/src/memory/mod.rs index bb45e93..82bea17 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -1,6 +1,7 @@ //! # Memory management //! Some code was borrowed from [Phil Opp's Blog](http://os.phil-opp.com/allocating-frames.html) +use crate::log::info; pub use crate::paging::{PAGE_SIZE, PhysicalAddress}; use self::bump::BumpAllocator; @@ -76,7 +77,7 @@ pub unsafe fn init(kernel_start: usize, kernel_end: usize) { for (i, entry) in MEMORY_MAP.iter_mut().enumerate() { *entry = *(0x500 as *const MemoryArea).add(i); if entry._type != MEMORY_AREA_NULL { - println!("{:?}", entry); + info!("{:?}", entry); } }