From e210f6f5ac81808b7dce4dcfd9d2bc40270b68a3 Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Thu, 9 Jan 2025 00:50:33 +0700 Subject: [PATCH] Initializes uma_keg (#1232) --- kernel/src/console/mod.rs | 57 ++++++++++++++++++------------------- kernel/src/main.rs | 2 +- kernel/src/malloc/stage2.rs | 2 +- kernel/src/uma/keg.rs | 2 ++ kernel/src/uma/mod.rs | 16 +++++++---- kernel/src/uma/zone.rs | 13 +++++++-- 6 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 kernel/src/uma/keg.rs diff --git a/kernel/src/console/mod.rs b/kernel/src/console/mod.rs index d56f4ed1b..c3439b9f6 100644 --- a/kernel/src/console/mod.rs +++ b/kernel/src/console/mod.rs @@ -1,5 +1,5 @@ use crate::config::boot_env; -use anstyle::{AnsiColor, Color, Style}; +use anstyle::{AnsiColor, Color, Effects, Style}; use core::fmt::{Display, Formatter}; use obconf::{BootEnv, ConsoleType}; @@ -30,49 +30,47 @@ macro_rules! info { /// This function does not require a CPU context as long as [`Display`] implementation on `msg` does /// not. /// -/// # Interupt safety +/// # Interrupt safety /// This function is interupt safe as long as [`Display`] implementation on `msg` are interupt safe /// (e.g. no heap allocation). #[inline(never)] pub fn info(file: &str, line: u32, msg: impl Display) { - print( - ConsoleType::Info, - Log { - style: Style::new().fg_color(Some(Color::Ansi(AnsiColor::BrightCyan))), - cat: 'I', - file, - line, - msg, - }, - ); + let msg = Log { + style: Style::new().effects(Effects::DIMMED), + cat: 'I', + file, + line, + msg, + }; + + print(ConsoleType::Info, msg); } /// # Context safety /// This function does not require a CPU context as long as [`Display`] implementation on `msg` does /// not. /// -/// # Interupt safety +/// # Interrupt safety /// This function is interupt safe as long as [`Display`] implementation on `msg` are interupt safe /// (e.g. no heap allocation). #[inline(never)] pub fn error(file: &str, line: u32, msg: impl Display) { - print( - ConsoleType::Error, - Log { - style: Style::new().fg_color(Some(Color::Ansi(AnsiColor::BrightRed))), - cat: 'E', - file, - line, - msg, - }, - ) + let msg = Log { + style: Style::new().fg_color(Some(Color::Ansi(AnsiColor::BrightRed))), + cat: 'E', + file, + line, + msg, + }; + + print(ConsoleType::Error, msg) } /// # Context safety /// This function does not require a CPU context as long as [`Display`] implementation on `msg` does /// not. /// -/// # Interupt safety +/// # Interrupt safety /// This function is interupt safe as long as [`Display`] implementation on `msg` are interupt safe /// (e.g. no heap allocation). fn print(ty: ConsoleType, msg: impl Display) { @@ -96,12 +94,11 @@ struct Log<'a, M: Display> { impl Display for Log<'_, M> { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - writeln!( - f, - "{}++++++++++++++++++ {} {}:{}{0:#}", - self.style, self.cat, self.file, self.line - )?; - self.msg.fmt(f)?; + let info = Style::new().effects(Effects::DIMMED); + + writeln!(f, "{}[{}]:{0:#} {}", self.style, self.cat, self.msg)?; + write!(f, " {}{}:{}{0:#}", info, self.file, self.line)?; + Ok(()) } } diff --git a/kernel/src/main.rs b/kernel/src/main.rs index b96f47dcc..756cbb0d4 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -134,7 +134,7 @@ fn panic(i: &PanicInfo) -> ! { }; // Print the message. - crate::console::error(file, line, i.message()); + crate::console::error(file, line, format_args!("Kernel panic - {}.", i.message())); crate::panic::panic(); } diff --git a/kernel/src/malloc/stage2.rs b/kernel/src/malloc/stage2.rs index 27d3ee381..fe4a056a9 100644 --- a/kernel/src/malloc/stage2.rs +++ b/kernel/src/malloc/stage2.rs @@ -52,7 +52,7 @@ impl Stage2 { // Create zone. let zone = Arc::new(uma.create_zone( - size.to_string().into(), + size.to_string(), size, align - 1, UmaFlags::new().with_malloc(true), diff --git a/kernel/src/uma/keg.rs b/kernel/src/uma/keg.rs new file mode 100644 index 000000000..e03969d03 --- /dev/null +++ b/kernel/src/uma/keg.rs @@ -0,0 +1,2 @@ +/// Implementation of `uma_keg` structure. +pub struct UmaKeg {} diff --git a/kernel/src/uma/mod.rs b/kernel/src/uma/mod.rs index c00c7ce44..1e6183f26 100644 --- a/kernel/src/uma/mod.rs +++ b/kernel/src/uma/mod.rs @@ -1,9 +1,11 @@ pub use self::zone::*; -use alloc::borrow::Cow; + +use alloc::string::String; use bitfield_struct::bitfield; use core::num::NonZero; mod bucket; +mod keg; mod zone; /// Implementation of UMA system. @@ -35,14 +37,14 @@ impl Uma { /// |PS4 11.00|0x13DC80| pub fn create_zone( &mut self, - _: Cow<'static, str>, + _: impl Into, _: NonZero, _: usize, - _: UmaFlags, + flags: UmaFlags, ) -> UmaZone { // The Orbis will allocate a new zone from masterzone_z. We choose to remove this since it // does not idomatic to Rust, which mean our uma_zone itself can live on the stack. - UmaZone::new() + UmaZone::new(None, flags) } } @@ -53,6 +55,10 @@ pub struct UmaFlags { __: u8, /// `UMA_ZONE_MALLOC`. pub malloc: bool, - #[bits(27)] + #[bits(4)] + __: u8, + /// `UMA_ZONE_SECONDARY`. + pub secondary: bool, + #[bits(22)] __: u32, } diff --git a/kernel/src/uma/zone.rs b/kernel/src/uma/zone.rs index 107ab25aa..ba3f4db91 100644 --- a/kernel/src/uma/zone.rs +++ b/kernel/src/uma/zone.rs @@ -1,4 +1,6 @@ use super::bucket::UmaBucket; +use super::keg::UmaKeg; +use super::UmaFlags; use crate::context::{current_thread, CpuLocal}; use crate::lock::Gutex; use alloc::collections::VecDeque; @@ -27,8 +29,15 @@ impl UmaZone { /// | Version | Offset | /// |---------|--------| /// |PS4 11.00|0x13D490| - pub(super) fn new() -> Self { - todo!() + pub(super) fn new(keg: Option, flags: UmaFlags) -> Self { + if flags.secondary() { + todo!() + } else { + match keg { + Some(_) => todo!(), + None => todo!(), + } + } } pub fn size(&self) -> NonZero {