Add with_current_task()
All checks were successful
Build / build (push) Successful in 2m40s

This commit is contained in:
Mathieu Strypsteen 2024-12-29 12:14:32 +01:00
parent c8eea7d45b
commit 40696ce142
7 changed files with 32 additions and 37 deletions

View file

@ -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");

View file

@ -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<LittleEndian> = ElfBytes::minimal_parse(binary).unwrap();

View file

@ -23,11 +23,10 @@ static SCI_CONTEXT: AtomicPtr<c_void> = 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 {

View file

@ -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);

View file

@ -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<RawSemaphore>, 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();
}
}

View file

@ -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<Vec<u8>, Box<dyn Error>> {
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<usize, Box<dyn Error>> {
Ok(0)
}
fn syscall_get_pid(_args: SyscallArgs) -> Result<usize, Box<dyn Error>> {
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]

View file

@ -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<F: FnOnce(&mut 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;
{