From d7184738868d1d637054af68dbff9347f6ede918 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Fri, 22 Nov 2024 10:59:18 +0100 Subject: [PATCH] Initialize display sooner --- kernel/src/main.rs | 27 ++++++++++++--------------- kernel/src/misc/display.rs | 14 +++++++++++--- kernel/src/sys/sync.rs | 3 +++ loader/src/display.rs | 10 ++++++++-- loader/src/main.rs | 4 ++-- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 3cb9160..8e737cd 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -56,19 +56,18 @@ extern "C" fn early_main(temp_loader_struct: *const LoaderStruct) -> ! { unsafe { ALLOC.lock().init(KERNEL_HEAP_START as usize, KERNEL_HEAP_INITIAL_SIZE); } - { - let mut loader_struct = LOADER_STRUCT.lock(); - unsafe { - temp_loader_struct.copy_to(&mut *loader_struct, 1); - } - assert_eq!(loader_struct.magic, LOADER_STRUCT_MAGIC); - init_logger(loader_struct.vm != 0); - info!("Starting kernel..."); - setup_gdt(); - setup_idt(); - setup_paging(&loader_struct, loader_struct.phys_kernel_start, loader_struct.phys_heap_start); - RSDP_ADDRESS.store(loader_struct.rsdp_address, Ordering::SeqCst); + let mut loader_struct = LOADER_STRUCT.lock(); + unsafe { + temp_loader_struct.copy_to(&mut *loader_struct, 1); } + assert_eq!(loader_struct.magic, LOADER_STRUCT_MAGIC); + init_logger(loader_struct.vm != 0); + info!("Starting kernel..."); + setup_gdt(); + setup_idt(); + setup_paging(&loader_struct, loader_struct.phys_kernel_start, loader_struct.phys_heap_start); + setup_display(loader_struct.framebuffer); + RSDP_ADDRESS.store(loader_struct.rsdp_address, Ordering::SeqCst); disable_pic(); let early_acpi_tables; unsafe { @@ -81,8 +80,6 @@ extern "C" fn early_main(temp_loader_struct: *const LoaderStruct) -> ! { } fn main() { info!("Starting main kernel task"); - let loader_struct = LOADER_STRUCT.lock(); - setup_display(loader_struct.framebuffer); let mut status = unsafe { AcpiInitializeSubsystem() }; assert_eq!(status, AE_OK); status = unsafe { AcpiInitializeTables(null_mut(), 0, 0) }; @@ -99,7 +96,7 @@ fn panic(info: &PanicInfo) -> ! { cli(); LOCKS_HELD.fetch_add(1, Ordering::SeqCst); error!("{}", info); - let str = format!("PANIC at {}: {}\n", info.location().unwrap(), info.message().as_str().unwrap()); + let str = format!("{}", info); display_print(&str); loop { hlt(); diff --git a/kernel/src/misc/display.rs b/kernel/src/misc/display.rs index 94ab34c..d09d6d9 100644 --- a/kernel/src/misc/display.rs +++ b/kernel/src/misc/display.rs @@ -13,7 +13,11 @@ use embedded_graphics::{ use kernel_common::loader_struct::FramebufferInfo; use spin::Mutex; -use crate::{cpu::paging::map_physical, misc::draw_target::FramebufferTarget, sys::sync::Spinlock}; +use crate::{ + cpu::paging::map_physical, + misc::draw_target::FramebufferTarget, + sys::{sync::Spinlock, task::MULTITASKING_ENABLED}, +}; static FRAMEBUFFER: Mutex> = Mutex::new(None); static FRAMEBUFFER_LOCK: Spinlock = Spinlock::new(); @@ -53,7 +57,9 @@ pub fn display_print(str: &str) { if FRAMEBUFFER_ADDR.load(Ordering::SeqCst) == null_mut() { return; } - FRAMEBUFFER_LOCK.lock(); + if MULTITASKING_ENABLED.load(Ordering::SeqCst) { + FRAMEBUFFER_LOCK.lock(); + } let mut current_x = CURRENT_X.load(Ordering::SeqCst); let mut current_y = CURRENT_Y.load(Ordering::SeqCst); let width = WIDTH.load(Ordering::SeqCst); @@ -78,7 +84,9 @@ pub fn display_print(str: &str) { CURRENT_X.store(current_x, Ordering::SeqCst); CURRENT_Y.store(current_y, Ordering::SeqCst); copy_to_fb(); - FRAMEBUFFER_LOCK.unlock(); + if MULTITASKING_ENABLED.load(Ordering::SeqCst) { + FRAMEBUFFER_LOCK.unlock(); + } } pub fn setup_display(info: FramebufferInfo) { let addr = unsafe { map_physical(info.address, info.height * info.stride * 4, true) }; diff --git a/kernel/src/sys/sync.rs b/kernel/src/sys/sync.rs index 65a13f8..2f42608 100644 --- a/kernel/src/sys/sync.rs +++ b/kernel/src/sys/sync.rs @@ -4,6 +4,8 @@ use alloc::{collections::vec_deque::VecDeque, sync::Arc}; use kernel_common::instructions::{cli, sti}; use spin::Mutex; +use crate::sys::task::MULTITASKING_ENABLED; + use super::{ scheduler::{schedule_task, yield_task, SCHEDULER_LOCK}, task::{Task, TaskState, CURRENT_TASK, CURRENT_TASK_LOCK}, @@ -27,6 +29,7 @@ impl Spinlock { Self { locked: AtomicBool::new(false) } } pub fn lock(&self) { + assert!(MULTITASKING_ENABLED.load(Ordering::SeqCst)); cli(); while !self.locked.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok() {} LOCKS_HELD.fetch_add(1, Ordering::SeqCst); diff --git a/loader/src/display.rs b/loader/src/display.rs index 090c47d..f244cb5 100644 --- a/loader/src/display.rs +++ b/loader/src/display.rs @@ -1,9 +1,13 @@ +use core::sync::atomic::Ordering; + use kernel_common::loader_struct::FramebufferInfo; use uefi::{ boot::{get_handle_for_protocol, open_protocol_exclusive, ScopedProtocol}, proto::console::gop::{GraphicsOutput, PixelFormat}, }; +use crate::BOOT_SERVICES_ACTIVE; + pub fn setup_display() -> FramebufferInfo { let gop_handle = get_handle_for_protocol::().unwrap(); let mut gop: ScopedProtocol = open_protocol_exclusive(gop_handle).unwrap(); @@ -22,11 +26,13 @@ pub fn setup_display() -> FramebufferInfo { best_mode = Some(i); } } - gop.set_mode(&best_mode.unwrap()).unwrap(); + let best_mode = best_mode.unwrap(); + BOOT_SERVICES_ACTIVE.store(false, Ordering::SeqCst); + gop.set_mode(&best_mode).unwrap(); FramebufferInfo { address: gop.frame_buffer().as_mut_ptr() as u64, width: best_width as u64, height: best_height as u64, - stride: best_mode.unwrap().info().stride() as u64, + stride: best_mode.info().stride() as u64, } } diff --git a/loader/src/main.rs b/loader/src/main.rs index c666e3b..ebaeb44 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -37,7 +37,7 @@ mod paging; #[global_allocator] static ALLOC: Bump<[u8; 8 * 1024 * 1024]> = Bump::uninit(); -static BOOT_SERVICES_ACTIVE: AtomicBool = AtomicBool::new(true); +static BOOT_SERVICES_ACTIVE: AtomicBool = AtomicBool::new(false); const KERNEL: &[u8] = include_bytes!("../../target/x86_64-unknown-none/release/kernel"); @@ -48,6 +48,7 @@ fn main() -> Status { init_logger(features.has_hypervisor()); info!("Starting bootloader..."); uefi::helpers::init().unwrap(); + BOOT_SERVICES_ACTIVE.store(true, Ordering::SeqCst); assert!(features.has_rdrand()); let extended_features = cpuid.get_extended_feature_info().unwrap(); assert!(extended_features.has_smap()); @@ -65,7 +66,6 @@ fn main() -> Status { } }); assert_ne!(rsdp.load(Ordering::SeqCst), 0, "RSDP not found"); - BOOT_SERVICES_ACTIVE.store(false, Ordering::SeqCst); let framebuffer_info = setup_display(); let memory_map = unsafe { exit_boot_services(MemoryType::LOADER_DATA) }; let pml4 = setup_paging(&memory_map, heap_start);