From 40696ce14232c43eab9b9ec431a5d36e0a84dbe1 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Sun, 29 Dec 2024 12:14:32 +0100 Subject: [PATCH] Add with_current_task() --- kernel/src/cpu/isr.rs | 4 ++-- kernel/src/misc/elf.rs | 13 ++++++------- kernel/src/sys/acpica_osl.rs | 9 ++++++--- kernel/src/sys/hpet.rs | 9 +++------ kernel/src/sys/sync.rs | 8 +++----- kernel/src/sys/syscall.rs | 17 ++++++----------- kernel/src/sys/task.rs | 9 ++++++--- 7 files changed, 32 insertions(+), 37 deletions(-) diff --git a/kernel/src/cpu/isr.rs b/kernel/src/cpu/isr.rs index f0a3e7e..9ca3181 100644 --- a/kernel/src/cpu/isr.rs +++ b/kernel/src/cpu/isr.rs @@ -13,7 +13,7 @@ use crate::{ madt::INTERRUPTS_SETUP, scheduler::scheduler, sync::{IN_ISR_HANDLER, LOCKS_HELD}, - task::{TaskState, CURRENT_TASKS}, + task::{with_current_task, TaskState}, }, BROADCASTED_PANIC, }; @@ -118,7 +118,7 @@ extern "C" fn isr_handler(state: &mut ISRState) { if state.isr < 32 { if state.cs == 0x23 && state.isr != 2 && state.isr != 8 && state.isr != 18 { debug!("Exception in usermode: {}", EXCEPTIONS[state.isr as usize]); - CURRENT_TASKS.lock()[get_current_lapic_id()].as_mut().unwrap().task_state = TaskState::Terminated; + with_current_task(|current_task| current_task.task_state = TaskState::Terminated); IN_ISR_HANDLER[lapic_id].store(false, Ordering::SeqCst); unsafe { asm!("int $254"); diff --git a/kernel/src/misc/elf.rs b/kernel/src/misc/elf.rs index abb4cb9..0f52d96 100644 --- a/kernel/src/misc/elf.rs +++ b/kernel/src/misc/elf.rs @@ -8,7 +8,7 @@ use crate::{ instructions::{disable_user_memory_access, enable_user_memory_access}, paging::USER_END, }, - sys::{lapic::get_current_lapic_id, task::CURRENT_TASKS}, + sys::task::with_current_task, }; extern "C" { @@ -19,13 +19,12 @@ extern "C" { pub fn load_binary() { let entry; { - let process; - { - let mut current_tasks = CURRENT_TASKS.lock(); - let task = current_tasks[get_current_lapic_id()].as_mut().unwrap(); - process = task.process.clone(); - } + let mut process = None; + with_current_task(|current_task| { + process = Some(current_task.process.clone()); + }); { + let process = process.unwrap(); let mut process = process.lock(); let binary = process.binary.unwrap(); let file: ElfBytes = ElfBytes::minimal_parse(binary).unwrap(); diff --git a/kernel/src/sys/acpica_osl.rs b/kernel/src/sys/acpica_osl.rs index adaa7bb..229f436 100644 --- a/kernel/src/sys/acpica_osl.rs +++ b/kernel/src/sys/acpica_osl.rs @@ -23,11 +23,10 @@ static SCI_CONTEXT: AtomicPtr = AtomicPtr::new(null_mut()); use super::{ hpet::get_current_time, - lapic::get_current_lapic_id, locks::Spinlock, process::get_kernel_process, sync::{create_semaphore, lock_semaphore, unlock_semaphore, RawSemaphore, RawSpinlock}, - task::{CURRENT_TASKS, MULTITASKING_ENABLED}, + task::{with_current_task, MULTITASKING_ENABLED}, }; pub const AE_OK: ACPI_STATUS = 0; @@ -91,7 +90,11 @@ extern "C" fn AcpiOsGetThreadId() -> UINT64 { if !MULTITASKING_ENABLED.load(Ordering::SeqCst) { return 1; } - CURRENT_TASKS.lock()[get_current_lapic_id()].as_ref().unwrap().id as UINT64 + let mut id = 0; + with_current_task(|current_task| { + id = current_task.id as UINT64; + }); + id } #[no_mangle] extern "C" fn AcpiOsGetTimer() -> UINT64 { diff --git a/kernel/src/sys/hpet.rs b/kernel/src/sys/hpet.rs index a66283f..5c5c6d0 100644 --- a/kernel/src/sys/hpet.rs +++ b/kernel/src/sys/hpet.rs @@ -10,11 +10,10 @@ use kernel_common::instructions::pause; use super::{ early_acpi::EarlyACPIHandler, ioapic::register_irq_handler, - lapic::get_current_lapic_id, locks::Spinlock, process::get_kernel_process, scheduler::{yield_task, SCHEDULER}, - task::{Task, TaskState, CURRENT_TASKS, MULTITASKING_ENABLED}, + task::{with_current_task, Task, TaskState, MULTITASKING_ENABLED}, }; const REGISTER_CAPABILITIES: usize = 0; @@ -88,12 +87,10 @@ pub fn get_current_time() -> usize { } pub fn sleep(us: usize) { if MULTITASKING_ENABLED.load(Ordering::SeqCst) { - { - let mut current_tasks = CURRENT_TASKS.lock(); - let current_task = current_tasks[get_current_lapic_id()].as_mut().unwrap(); + with_current_task(|current_task| { current_task.sleep_until_us = get_current_time() + us; current_task.task_state = TaskState::Sleeping; - } + }); yield_task(); } else { EARLY_SLEEP.store(true, Ordering::SeqCst); diff --git a/kernel/src/sys/sync.rs b/kernel/src/sys/sync.rs index aa3b52a..f044bfb 100644 --- a/kernel/src/sys/sync.rs +++ b/kernel/src/sys/sync.rs @@ -9,7 +9,7 @@ use super::{ lapic::get_current_lapic_id, locks::Spinlock, scheduler::{yield_task, SCHEDULER}, - task::{Task, TaskState, CURRENT_TASKS}, + task::{with_current_task, Task, TaskState}, }; pub static IN_ISR_HANDLER: [AtomicBool; 256] = [const { AtomicBool::new(false) }; 256]; @@ -88,13 +88,11 @@ pub fn lock_semaphore(semaphore: Arc, count: usize) { if success { return; } - { - let mut current_task = CURRENT_TASKS.lock(); - let current_task = current_task[get_current_lapic_id()].as_mut().unwrap(); + with_current_task(|current_task| { current_task.task_state = TaskState::SemaphoreBlocked; current_task.block_on_semaphore = Some(semaphore.clone()); current_task.semaphore_requested_count = count; - } + }); yield_task(); } } diff --git a/kernel/src/sys/syscall.rs b/kernel/src/sys/syscall.rs index 45aeb53..19c4d5a 100644 --- a/kernel/src/sys/syscall.rs +++ b/kernel/src/sys/syscall.rs @@ -8,10 +8,7 @@ use crate::cpu::{ paging::USER_END, }; -use super::{ - lapic::get_current_lapic_id, - task::{terminate_current_task, CURRENT_TASKS}, -}; +use super::task::{terminate_current_task, with_current_task}; struct SyscallArgs { arg1: usize, @@ -46,11 +43,9 @@ fn copy_from_user(start: u64, size: usize) -> Result, Box> { if end > USER_END { return Err("Invalid copy requested".into()); } - let process; - { - let tasks = CURRENT_TASKS.lock(); - process = tasks[get_current_lapic_id()].as_ref().unwrap().process.clone(); - } + let mut process = None; + with_current_task(|current_task| process = Some(current_task.process.clone())); + let process = process.unwrap(); let mut process = process.lock(); for i in start / 0x1000..(end + 0xfff) / 0x1000 { let page = process.address_space.get_page(i * 0x1000); @@ -80,8 +75,8 @@ fn syscall_debug_print(args: SyscallArgs) -> Result> { Ok(0) } fn syscall_get_pid(_args: SyscallArgs) -> Result> { - let tasks = CURRENT_TASKS.lock(); - let pid = tasks[get_current_lapic_id()].as_ref().unwrap().process.lock().pid; + let mut pid = 0; + with_current_task(|current_task| pid = current_task.process.lock().pid); Ok(pid) } #[no_mangle] diff --git a/kernel/src/sys/task.rs b/kernel/src/sys/task.rs index a5d2f14..b21578f 100644 --- a/kernel/src/sys/task.rs +++ b/kernel/src/sys/task.rs @@ -191,9 +191,7 @@ fn create_idle_task() { } } pub fn terminate_current_task() -> ! { - { - CURRENT_TASKS.lock()[get_current_lapic_id()].as_mut().unwrap().task_state = TaskState::Terminated; - } + with_current_task(|current_task| current_task.task_state = TaskState::Terminated); yield_task(); panic!("Failed to terminate task"); } @@ -221,6 +219,11 @@ fn idle_main() { hlt(); } } +pub fn with_current_task(func: F) { + let mut current_tasks = CURRENT_TASKS.lock(); + let lapic_id = get_current_lapic_id(); + func(current_tasks[lapic_id].as_mut().unwrap()); +} pub fn setup_multitasking() -> ! { let task; {