various small changes

This commit is contained in:
Zoe
2024-08-15 21:10:25 -05:00
parent 817aa66f62
commit bab094b9cd
5 changed files with 64 additions and 66 deletions

View File

@@ -1,54 +0,0 @@
use core::ops::Deref;
use super::Cell;
#[derive(PartialEq)]
enum LazyState<T, F = fn() -> T> {
Uninitialized(F),
Initializing,
Initialized(T),
}
pub struct LazyCell<T, F = fn() -> T> {
state: Cell<LazyState<T, F>>,
}
impl<T, F: Fn() -> T> LazyCell<T, F> {
pub const fn new(init_func: F) -> Self {
Self {
state: Cell::new(LazyState::Uninitialized(init_func)),
}
}
pub fn get(&self) -> Option<&T> {
match self.state.get() {
LazyState::Initialized(data) => Some(data),
_ => None,
}
}
}
impl<T, F: Fn() -> T> Deref for LazyCell<T, F> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self.state.get() {
LazyState::Uninitialized(func) => {
self.state.set(LazyState::Initializing);
// initialize and return value
let new_value = func();
self.state.set(LazyState::Initialized(new_value));
self.get().unwrap()
}
LazyState::Initialized(data) => data,
LazyState::Initializing => {
panic!("Attempted to initialize Lazy while initializing Lazy!")
}
}
}
}
unsafe impl<T, F: Fn() -> T + Send> Sync for LazyCell<T, F> {}

View File

@@ -1,9 +1,7 @@
use core::cell::UnsafeCell;
mod lazy;
mod once;
pub use lazy::LazyCell;
pub use once::OnceCell;
pub struct Cell<T: ?Sized> {

View File

@@ -39,25 +39,32 @@ impl<T> OnceCell<T> {
match self.state.get() {
OnceCellState::Uninitialized => {
self.set(func());
self.get()
self.get_unchecked()
}
OnceCellState::Initializing => panic!("Tried to get or set data that is initializing"),
OnceCellState::Initialized(data) => data,
}
}
fn get(&self) -> &T {
fn get_unchecked(&self) -> &T {
match self.state.get() {
OnceCellState::Initialized(data) => data,
_ => panic!("Attempted to access uninitialized data!"),
}
}
pub fn get(&self) -> Result<&T, ()> {
match self.state.get() {
OnceCellState::Initialized(data) => return Ok(data),
_ => return Err(()),
}
}
}
impl<T> Deref for OnceCell<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.get()
self.get_unchecked()
}
}

View File

@@ -3,6 +3,8 @@
#![no_std]
#![no_main]
use core::arch::x86_64::__cpuid;
use alloc::vec::Vec;
use limine::{request::KernelFileRequest, BaseRevision};
use mem::{LabelBytes, HHDM_OFFSET, PHYSICAL_MEMORY_MANAGER};
@@ -167,6 +169,7 @@ fn draw_gradient() {
}
fn print_boot_info() {
// I dont really like this 'a'
crate::println!("╔═╗───────────────────╔╗────╔═╗╔══╗");
crate::println!("║╔╝╔═╗ ╔═╗╔═╗╔╦╗╔═╗╔═╗╠╣╔═╦╗║║║║══╣");
crate::println!("║╚╗║╬╚╗║╬║║╬║║║║║═╣║═╣║║║║║║║║║╠══║");
@@ -183,7 +186,36 @@ fn print_boot_info() {
} else {
"release"
}
)
);
if unsafe { __cpuid(0x80000000).eax } >= 0x80000004 {
let processor_brand = get_processor_brand();
crate::println!("Detected CPU: {processor_brand}");
}
}
fn get_processor_brand() -> alloc::string::String {
let mut brand_buf = [0u8; 48];
let mut offset = 0;
for i in 0..=2 {
let cpuid_result = unsafe { __cpuid(0x80000002 + i) };
brand_buf[offset..offset + 4].copy_from_slice(&cpuid_result.eax.to_le_bytes());
brand_buf[(offset + 4)..(offset + 8)].copy_from_slice(&cpuid_result.ebx.to_le_bytes());
brand_buf[(offset + 8)..(offset + 12)].copy_from_slice(&cpuid_result.ecx.to_le_bytes());
brand_buf[(offset + 12)..(offset + 16)].copy_from_slice(&cpuid_result.edx.to_le_bytes());
offset += 16;
}
// there's probably a better way to do this, but wikipedia says to not rely on the null byte, so I cant use Cstr (and I dont really want to tbh) but if it's shorter than 48bytes it will be null terminated
let mut brand = alloc::string::String::new();
for char in brand_buf {
if char == 0 {
break;
}
brand.push(char as char);
}
brand
}
#[macro_export]
@@ -212,7 +244,13 @@ enum LogLevel {
#[macro_export]
macro_rules! log {
($level:expr, $($arg:tt)*) => {{
if ($level as u8) >= $crate::LOG_LEVEL {
let kernel_log_level = if let Ok(kernel_features) = $crate::KERNEL_FEATURES.get() {
kernel_features.log_level
} else {
$crate::LOG_LEVEL
};
if ($level as u8) >= kernel_log_level {
let color_code = match $level {
$crate::LogLevel::Trace => "\x1B[90m",
$crate::LogLevel::Debug => "\x1B[94m",
@@ -228,6 +266,7 @@ macro_rules! log {
#[derive(Debug)]
pub struct KernelFeatures {
pub log_level: u8,
pub fat_in_mem: bool,
}
@@ -235,6 +274,7 @@ impl KernelFeatures {
fn update_option(&mut self, option: &str, value: &str) {
#[allow(clippy::single_match)]
match option {
"log_level" => self.log_level = value.parse().unwrap_or(crate::LOG_LEVEL),
"fat_in_mem" => self.fat_in_mem = value == "true",
_ => {}
}
@@ -245,7 +285,10 @@ impl KernelFeatures {
pub static KERNEL_FEATURES: libs::cell::OnceCell<KernelFeatures> = libs::cell::OnceCell::new();
fn parse_kernel_cmdline() {
let mut kernel_features: KernelFeatures = KernelFeatures { fat_in_mem: true };
let mut kernel_features: KernelFeatures = KernelFeatures {
fat_in_mem: true,
log_level: crate::LOG_LEVEL,
};
let kernel_file_response = KERNEL_REQUEST.get_response();
if kernel_file_response.is_none() {

View File

@@ -1,7 +1,10 @@
pub mod allocator;
pub mod pmm;
use crate::libs::{cell::OnceCell, sync::Mutex};
use crate::{
libs::{cell::OnceCell, sync::Mutex},
// LogLevel,
};
use self::{allocator::LinkedListAllocator, pmm::PhysicalMemoryManager};
@@ -36,12 +39,13 @@ pub static ALLOCATOR: Mutex<LinkedListAllocator> = Mutex::new(LinkedListAllocato
// let memmap = memmap_request.unwrap().entries();
// crate::log_serial!("====== MEMORY MAP ======\n");
// crate::log!(LogLevel::Trace, "====== MEMORY MAP ======");
// for entry in memmap.iter() {
// let label = (entry.length as usize).label_bytes();
// crate::log_serial!(
// "[ {:#018X?} ] Type: {:?} Size: {}\n",
// crate::log!(
// LogLevel::Trace,
// "[ {:#018X?} ] Type: {:?} Size: {}",
// entry.base..entry.base + entry.length,
// entry.entry_type,
// label