Basic thread run routine
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2022-12-31 12:05:20 +01:00
parent 2522ece23f
commit 3a8167b6ad
6 changed files with 127 additions and 81 deletions

@ -65,11 +65,14 @@ impl FileSystem for VirtualFS {
loop { loop {
if let Some(fs) = map.find_exact(&path_split) { if let Some(fs) = map.find_exact(&path_split) {
// TODO, remove path prefix of the mount point // TODO, remove path prefix of the mount point
return fs.borrow_mut().open(mnt_relative_path.as_str(), flags).await; return fs
} .borrow_mut()
else { .open(mnt_relative_path.as_str(), flags)
.await;
} else {
let component = path_split.remove(path_split.len() - 1); let component = path_split.remove(path_split.len() - 1);
mnt_relative_path = String::from("/") + component.as_str() + mnt_relative_path.as_str(); mnt_relative_path =
String::from("/") + component.as_str() + mnt_relative_path.as_str();
} }
} }
} else { } else {

@ -23,6 +23,9 @@ use drivers::vga::{self, Color, ColorCode};
use multiboot2::BootInformation; use multiboot2::BootInformation;
use task::{executor::Executor, keyboard, Task}; use task::{executor::Executor, keyboard, Task};
use alloc::sync::Arc;
use core::cell::RefCell;
#[alloc_error_handler] #[alloc_error_handler]
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
panic!("Allocation error: {:?}", layout) panic!("Allocation error: {:?}", layout)
@ -48,7 +51,6 @@ pub fn init(boot_info: &BootInformation) {
memory::gdt::init_gdt(); memory::gdt::init_gdt();
interrupts::init_idt(); interrupts::init_idt();
vga::change_color(ColorCode::new(Color::LightGreen, Color::Black)); vga::change_color(ColorCode::new(Color::LightGreen, Color::Black));
println!("Init kernel main thread: {:?}", proc::thread::KERNEL_THREAD.try_lock().unwrap().id);
} }
#[no_mangle] #[no_mangle]
@ -86,6 +88,15 @@ async fn get_file() {
fd.borrow_mut().close().await; fd.borrow_mut().close().await;
let mut thread = proc::thread::Thread::new(); let thread = Arc::new(RefCell::new(proc::thread::Thread::new(
thread.start(proc::thread::exit as u64).await; proc::thread::exit as u64,
)));
proc::scheduler::SCHEDULER
.lock()
.await
.register(thread.clone());
unsafe {
(&mut*thread.as_ptr()).run();
}
} }

@ -1,2 +1,2 @@
pub mod thread;
pub mod scheduler; pub mod scheduler;
pub mod thread;

@ -1,22 +1,40 @@
use crate::utils::mutex::AsyncMutex;
use super::thread::{Thread, ThreadId}; use super::thread::{Thread, ThreadId};
use alloc::{collections::BTreeMap, sync::Arc}; use alloc::{collections::BTreeMap, sync::Arc};
use core::cell::RefCell;
use crossbeam_queue::ArrayQueue; use crossbeam_queue::ArrayQueue;
use lazy_static::lazy_static;
lazy_static! {
pub static ref SCHEDULER: AsyncMutex<Scheduler> = AsyncMutex::new(Scheduler::new());
}
pub type Threadt = Arc<RefCell<Thread>>;
pub struct Scheduler { pub struct Scheduler {
threads: BTreeMap<ThreadId, Arc<Thread>>, pub threads: BTreeMap<ThreadId, Threadt>,
thread_queue: Arc<ArrayQueue<ThreadId>>, thread_queue: Arc<ArrayQueue<ThreadId>>,
} }
impl Scheduler { impl Scheduler {
pub fn new() -> Self { pub fn new() -> Self {
Scheduler { let mut res = Scheduler {
threads: BTreeMap::new(), threads: BTreeMap::new(),
thread_queue: Arc::new(ArrayQueue::new(100)), thread_queue: Arc::new(ArrayQueue::new(100)),
} };
let k_thread: Thread = Thread {
id: ThreadId(0),
entry_point: 0,
started: true,
rsp: 0,
};
res.register(Arc::new(RefCell::new(k_thread)));
res
} }
pub fn schedule(&mut self) -> Option<Arc<Thread>> { pub fn schedule(&mut self) -> Option<Threadt> {
if let Ok(thread_id) = self.thread_queue.pop() { if let Ok(thread_id) = self.thread_queue.pop() {
self.thread_queue.push(thread_id); self.thread_queue.push(thread_id);
let thread = match self.threads.get_mut(&thread_id) { let thread = match self.threads.get_mut(&thread_id) {
@ -28,4 +46,14 @@ impl Scheduler {
None None
} }
} }
pub fn register(&mut self, thread: Threadt) {
let thread_id = thread.borrow().id;
if self.threads.insert(thread_id, thread).is_some() {
panic!("Duplicate thread ID")
}
self.thread_queue
.push(thread_id)
.expect("Thread queue full");
}
} }

@ -1,6 +1,8 @@
use crate::println; use crate::println;
use crate::utils::mutex::AsyncMutex; use crate::utils::mutex::AsyncMutex;
use super::scheduler::SCHEDULER;
use core::arch::asm; use core::arch::asm;
use core::sync::atomic::{AtomicU64, Ordering}; use core::sync::atomic::{AtomicU64, Ordering};
@ -11,25 +13,10 @@ const STACK_SIZE: usize = 4096 * 20;
lazy_static! { lazy_static! {
pub static ref RUNNING_THREAD: AsyncMutex<ThreadId> = AsyncMutex::new(ThreadId(0)); pub static ref RUNNING_THREAD: AsyncMutex<ThreadId> = AsyncMutex::new(ThreadId(0));
pub static ref KERNEL_THREAD: AsyncMutex<Thread> = {
let k_rsp: u64;
unsafe {
asm!(
"push rsp", // Recover current rsp
"pop {out}",
out = out(reg) k_rsp, // Save current rsp
);
}
let thread: Thread = Thread {
id: ThreadId(0),
rsp: k_rsp,
};
AsyncMutex::new(thread)
};
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ThreadId(u64); pub struct ThreadId(pub u64);
impl ThreadId { impl ThreadId {
fn new() -> Self { fn new() -> Self {
@ -39,83 +26,96 @@ impl ThreadId {
} }
pub fn exit() { pub fn exit() {
println!("Exiting"); println!("Exiting thread");
KERNEL_THREAD.try_lock().unwrap().run(); let mut scheduler = SCHEDULER.try_lock().unwrap();
let mut thread = scheduler
.threads
.get_mut(&ThreadId(0))
.unwrap()
.borrow_mut();
SCHEDULER.force_unlock();
thread.run();
} }
pub struct Thread { pub struct Thread {
pub id: ThreadId, pub id: ThreadId,
rsp: u64 pub entry_point: u64,
pub started: bool,
pub rsp: u64,
} }
impl Thread { impl Thread {
pub fn new() -> Self { pub fn new(entry_point: u64) -> Self {
unsafe { unsafe {
Thread { Thread {
id: ThreadId::new(), id: ThreadId::new(),
entry_point: entry_point,
started: false,
rsp: alloc(Layout::new::<[u8; STACK_SIZE]>()) as u64 + STACK_SIZE as u64 - 0x80, rsp: alloc(Layout::new::<[u8; STACK_SIZE]>()) as u64 + STACK_SIZE as u64 - 0x80,
} }
} }
} }
pub async fn start(&mut self, rip: u64) { pub fn run(&mut self) {
println!("Running thread {:?}", self.id);
unsafe { unsafe {
*RUNNING_THREAD.lock().await = self.id; let mut current_thread_guard = RUNNING_THREAD.try_lock().unwrap();
let current_rsp: u64;
asm!( asm!(
"push rax", // Save current thread regs
"push rbx",
"push rcx",
"push rdx",
"push rbp",
"push rsi",
"push rdi",
"push rsp", // Recover current rsp "push rsp", // Recover current rsp
"pop {out}", "pop {out}",
out = out(reg) KERNEL_THREAD.lock().await.rsp, // Save current rsp "sub {out}, 56", // Offset to saved registers
out = out(reg) current_rsp, // Save thread rsp
); );
asm!( let mut scheduler = SCHEDULER.try_lock().unwrap();
"push {rsp}", let current_thread = scheduler.threads.get_mut(&*current_thread_guard).unwrap();
"pop rsp", current_thread.borrow_mut().rsp = current_rsp;
"jmp {rip}",
rsp = in(reg) self.rsp, *current_thread_guard = self.id; // change running thread
rip = in(reg) rip, } // The scheduler and running thread guards is dropped here
);
}
}
pub async fn run(&mut self) {
unsafe { unsafe {
asm!( if self.started {
"push rax", // Save current thread regs asm!(
"push rbx", "push rax", // Save current thread regs
"push rcx", "push rbx",
"push rdx", "push rcx",
"push rbp", "push rdx",
"push rsi", "push rbp",
"push rdi", "push rsi",
"push rdi",
"push rsp", // Recover current rsp "push {rsp}", // Set stack pointer to the new thread
"pop {out}", "pop rsp",
out = out(reg) KERNEL_THREAD.lock().await.rsp, // Save current rsp
);
*RUNNING_THREAD.lock().await = self.id; // change running thread "pop rdi", // Restore new thread regs
"pop rsi",
"pop rbp",
"pop rdx",
"pop rcx",
"pop rbx",
"pop rax",
rsp = in(reg) self.rsp,
);
} else {
self.started = true;
asm!(
"push rax", // Save current thread regs
"push rbx",
"push rcx",
"push rdx",
"push rbp",
"push rsi",
"push rdi",
asm!( "push {rsp}",
"push {rsp}", // Set stack pointer to the new thread "pop rsp",
"pop rsp", "jmp {rip}",
rsp = in(reg) self.rsp,
"pop rdi", // Restore new thread regs rip = in(reg) self.entry_point,
"pop rsi", );
"pop rbp", }
"pop rdx",
"pop rcx",
"pop rbx",
"pop rax",
rsp = in(reg) self.rsp,
);
} }
} }
} }

@ -80,6 +80,10 @@ impl<T> AsyncMutex<T> {
} }
} }
pub fn force_unlock(&self) {
self.lock.drop();
}
pub async fn lock(&self) -> AsyncMutexGuard<'_, T> { pub async fn lock(&self) -> AsyncMutexGuard<'_, T> {
self.lock.clone().await; self.lock.clone().await;
AsyncMutexGuard { mutex: self } AsyncMutexGuard { mutex: self }