Compare commits
0
Commits
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
mod fd;
|
mod fd;
|
||||||
pub mod iso9660;
|
pub mod iso9660;
|
||||||
|
|
||||||
|
use crate::serial_println;
|
||||||
|
|
||||||
use crate::drivers::atapi::read_block;
|
use crate::drivers::atapi::read_block;
|
||||||
use crate::fd::FDt;
|
use crate::fd::FDt;
|
||||||
use crate::utils::unserialize;
|
use crate::utils::unserialize;
|
||||||
|
|||||||
+4
-7
@@ -65,14 +65,11 @@ 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
|
return fs.borrow_mut().open(mnt_relative_path.as_str(), flags).await;
|
||||||
.borrow_mut()
|
}
|
||||||
.open(mnt_relative_path.as_str(), flags)
|
else {
|
||||||
.await;
|
|
||||||
} else {
|
|
||||||
let component = path_split.remove(path_split.len() - 1);
|
let component = path_split.remove(path_split.len() - 1);
|
||||||
mnt_relative_path =
|
mnt_relative_path = String::from("/") + component.as_str() + mnt_relative_path.as_str();
|
||||||
String::from("/") + component.as_str() + mnt_relative_path.as_str();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ use crate::drivers::vga::{self, Color, ColorCode};
|
|||||||
use crate::hlt_loop;
|
use crate::hlt_loop;
|
||||||
use crate::memory::gdt;
|
use crate::memory::gdt;
|
||||||
use crate::println;
|
use crate::println;
|
||||||
|
|
||||||
use core::arch::asm;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use pic::{
|
use pic::{
|
||||||
disk1_interrupt_handler, disk2_interrupt_handler, init_pic, keyboard_interrupt_handler,
|
disk1_interrupt_handler, disk2_interrupt_handler, init_pic, keyboard_interrupt_handler,
|
||||||
@@ -14,8 +12,6 @@ use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
|||||||
|
|
||||||
pub mod pic;
|
pub mod pic;
|
||||||
|
|
||||||
const SYSCALL_32_INTERRUPT_NUMBER: usize = 0x80;
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref IDT: InterruptDescriptorTable = {
|
static ref IDT: InterruptDescriptorTable = {
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
let mut idt = InterruptDescriptorTable::new();
|
||||||
@@ -30,7 +26,6 @@ lazy_static! {
|
|||||||
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
|
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
|
||||||
idt[InterruptIndex::HardDisk1.as_usize()].set_handler_fn(disk1_interrupt_handler);
|
idt[InterruptIndex::HardDisk1.as_usize()].set_handler_fn(disk1_interrupt_handler);
|
||||||
idt[InterruptIndex::HardDisk2.as_usize()].set_handler_fn(disk2_interrupt_handler);
|
idt[InterruptIndex::HardDisk2.as_usize()].set_handler_fn(disk2_interrupt_handler);
|
||||||
idt[SYSCALL_32_INTERRUPT_NUMBER].set_handler_fn(syscall_handler_32);
|
|
||||||
idt
|
idt
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -52,44 +47,6 @@ extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
|||||||
vga::change_color(color);
|
vga::change_color(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "x86-interrupt" fn syscall_handler_32(_stack_frame: InterruptStackFrame) {
|
|
||||||
let rax: u64;
|
|
||||||
let rbx: u64;
|
|
||||||
let rcx: u64;
|
|
||||||
let rdx: u64;
|
|
||||||
let rsi: u64;
|
|
||||||
let rdi: u64;
|
|
||||||
let rbp: u64;
|
|
||||||
unsafe {
|
|
||||||
asm!(
|
|
||||||
"push rax",
|
|
||||||
"push rbx",
|
|
||||||
"push rcx",
|
|
||||||
"push rdx",
|
|
||||||
"push rsi",
|
|
||||||
"push rdi",
|
|
||||||
"push rbp",
|
|
||||||
|
|
||||||
"pop {rbp}",
|
|
||||||
"pop {rdi}",
|
|
||||||
"pop {rsi}",
|
|
||||||
"pop {rdx}",
|
|
||||||
"pop {rcx}",
|
|
||||||
"pop {rbx}",
|
|
||||||
"pop {rax}",
|
|
||||||
rax = out(reg) rax,
|
|
||||||
rbx = out(reg) rbx,
|
|
||||||
rcx = out(reg) rcx,
|
|
||||||
rdx = out(reg) rdx,
|
|
||||||
rsi = out(reg) rsi,
|
|
||||||
rdi = out(reg) rdi,
|
|
||||||
rbp = out(reg) rbp,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
println!("Received syscall");
|
|
||||||
crate::syscalls::syscall_routine(rax);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn page_fault_handler(
|
extern "x86-interrupt" fn page_fault_handler(
|
||||||
stack_frame: InterruptStackFrame,
|
stack_frame: InterruptStackFrame,
|
||||||
error_code: PageFaultErrorCode,
|
error_code: PageFaultErrorCode,
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ pub fn gettick() -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
pub extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
||||||
// TODO: thread preemption
|
|
||||||
unsafe {
|
unsafe {
|
||||||
TICKS += 1;
|
TICKS += 1;
|
||||||
PICS.lock()
|
PICS.lock()
|
||||||
|
|||||||
+2
-17
@@ -21,10 +21,7 @@ use crate::fs::FileSystem;
|
|||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use drivers::vga::{self, Color, ColorCode};
|
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) -> ! {
|
||||||
@@ -61,14 +58,10 @@ pub extern "C" fn julios_main(multiboot_info_addr: usize) -> ! {
|
|||||||
println!("***JuliOS V0.1.0***");
|
println!("***JuliOS V0.1.0***");
|
||||||
serial_println!("Hello serial");
|
serial_println!("Hello serial");
|
||||||
|
|
||||||
let mut executor = EXECUTOR.try_lock().unwrap();
|
let mut executor = Executor::new();
|
||||||
executor.spawn(Task::new(drivers::atapi::init()));
|
executor.spawn(Task::new(drivers::atapi::init()));
|
||||||
executor.spawn(Task::new(keyboard::print_keypresses()));
|
executor.spawn(Task::new(keyboard::print_keypresses()));
|
||||||
executor.spawn(Task::new(get_file()));
|
executor.spawn(Task::new(get_file()));
|
||||||
executor.spawn(Task::new(proc::scheduler::scheduler_run()));
|
|
||||||
|
|
||||||
EXECUTOR.force_unlock(); // Ouioui t'inquietes
|
|
||||||
|
|
||||||
executor.run();
|
executor.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,12 +84,4 @@ async fn get_file() {
|
|||||||
serial_println!("{}", alloc::str::from_utf8(&buf).unwrap());
|
serial_println!("{}", alloc::str::from_utf8(&buf).unwrap());
|
||||||
|
|
||||||
fd.borrow_mut().close().await;
|
fd.borrow_mut().close().await;
|
||||||
|
|
||||||
let thread = Arc::new(RefCell::new(proc::thread::Thread::new(
|
|
||||||
proc::thread::routine as u64,
|
|
||||||
)));
|
|
||||||
proc::scheduler::SCHEDULER
|
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.register(thread.clone());
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
pub mod scheduler;
|
|
||||||
pub mod thread;
|
pub mod thread;
|
||||||
|
pub mod scheduler;
|
||||||
+3
-148
@@ -1,152 +1,7 @@
|
|||||||
use crate::println;
|
use super::thread::Thread;
|
||||||
use crate::utils::mutex::AsyncMutex;
|
|
||||||
|
|
||||||
use super::thread::{Thread, ThreadId};
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
use alloc::{collections::BTreeMap, sync::Arc};
|
|
||||||
use core::cell::RefCell;
|
|
||||||
use core::{
|
|
||||||
pin::Pin,
|
|
||||||
task::{Context, Poll},
|
|
||||||
};
|
|
||||||
use crossbeam_queue::ArrayQueue;
|
|
||||||
use futures_util::stream::{Stream, StreamExt};
|
|
||||||
use futures_util::task::AtomicWaker;
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
// TODO: put this in a spin mutex instead
|
|
||||||
pub static ref SCHEDULER: AsyncMutex<Scheduler> = AsyncMutex::new(Scheduler::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Threadt = Arc<RefCell<Thread>>;
|
|
||||||
|
|
||||||
pub const K_THREAD_ID: ThreadId = ThreadId(0); // Kernel main thread identifier
|
|
||||||
|
|
||||||
struct ThreadStream {
|
|
||||||
// TODO: add the double scheduler queue
|
|
||||||
ids: ArrayQueue<ThreadId>,
|
|
||||||
waker: AtomicWaker,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ThreadStream {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
ThreadStream {
|
|
||||||
ids: ArrayQueue::new(100),
|
|
||||||
waker: AtomicWaker::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn register(&mut self, id: ThreadId) {
|
|
||||||
self.ids.push(id).expect("Thread queue full");
|
|
||||||
self.waker.wake();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Stream for ThreadStream {
|
|
||||||
type Item = ThreadId;
|
|
||||||
|
|
||||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
||||||
if let Ok(id) = self.ids.pop() {
|
|
||||||
return Poll::Ready(Some(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.waker.register(&cx.waker());
|
|
||||||
|
|
||||||
match self.ids.pop() {
|
|
||||||
Ok(id) => {
|
|
||||||
self.waker.take();
|
|
||||||
Poll::Ready(Some(id))
|
|
||||||
}
|
|
||||||
Err(crossbeam_queue::PopError) => Poll::Pending,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Scheduler {
|
pub struct Scheduler {
|
||||||
pub threads: BTreeMap<ThreadId, Threadt>,
|
threads: Vec<Thread>,
|
||||||
thread_queue: ThreadStream,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Scheduler {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let mut res = Scheduler {
|
|
||||||
threads: BTreeMap::new(),
|
|
||||||
thread_queue: ThreadStream::new(),
|
|
||||||
};
|
|
||||||
let k_thread: Thread = Thread {
|
|
||||||
id: K_THREAD_ID,
|
|
||||||
entry_point: 0,
|
|
||||||
started: true,
|
|
||||||
rsp: 0,
|
|
||||||
base_stack: 0,
|
|
||||||
is_blocked: false,
|
|
||||||
};
|
|
||||||
res.register(Arc::new(RefCell::new(k_thread)));
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn run(&mut self) {
|
|
||||||
while let Some(id) = self.thread_queue.next().await {
|
|
||||||
if let Some(thread) = self.get_thread(id) {
|
|
||||||
// Thread still exists
|
|
||||||
let blocked: bool;
|
|
||||||
{
|
|
||||||
blocked = thread.borrow().is_blocked;
|
|
||||||
} // Drop thread borrow
|
|
||||||
if !blocked {
|
|
||||||
unsafe {
|
|
||||||
(&mut *thread.as_ptr()).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
if !thread.borrow().is_blocked {
|
|
||||||
self.thread_queue.register(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
if thread_id != K_THREAD_ID {
|
|
||||||
self.thread_queue.register(thread_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn block(&mut self, id: ThreadId) {
|
|
||||||
if let Some(thread) = self.get_thread(id) {
|
|
||||||
thread.borrow_mut().is_blocked = true;
|
|
||||||
println!("Blocked thread {:?}", id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unblock(&mut self, id: ThreadId) {
|
|
||||||
if let Some(thread) = self.get_thread(id) {
|
|
||||||
thread.borrow_mut().is_blocked = false;
|
|
||||||
self.thread_queue.register(id);
|
|
||||||
println!("Unblocked thread {:?}", id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exit(&mut self, id: ThreadId) {
|
|
||||||
self.threads.remove(&id).unwrap().borrow().exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_thread(&mut self, id: ThreadId) -> Option<Threadt> {
|
|
||||||
if let Some(thread) = self.threads.get_mut(&id) {
|
|
||||||
Some(thread.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn scheduler_run() {
|
|
||||||
let mut scheduler = SCHEDULER.lock().await;
|
|
||||||
SCHEDULER.force_unlock();
|
|
||||||
scheduler.run().await;
|
|
||||||
}
|
}
|
||||||
+3
-145
@@ -1,158 +1,16 @@
|
|||||||
use crate::println;
|
|
||||||
use crate::utils::mutex::AsyncMutex;
|
|
||||||
|
|
||||||
use super::scheduler::{K_THREAD_ID, SCHEDULER};
|
|
||||||
|
|
||||||
use core::arch::asm;
|
|
||||||
use core::sync::atomic::{AtomicU64, Ordering};
|
|
||||||
|
|
||||||
use alloc::alloc::{alloc, dealloc, Layout};
|
use alloc::alloc::{alloc, dealloc, Layout};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
const STACK_SIZE: usize = 4096 * 20;
|
const STACK_SIZE: usize = 4096 * 20;
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
pub static ref RUNNING_THREAD: AsyncMutex<ThreadId> = AsyncMutex::new(ThreadId(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub struct ThreadId(pub u64);
|
|
||||||
|
|
||||||
impl ThreadId {
|
|
||||||
fn new() -> Self {
|
|
||||||
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
|
|
||||||
ThreadId(NEXT_ID.fetch_add(1, Ordering::Relaxed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resume_k_thread() {
|
|
||||||
let k_thread: *mut Thread;
|
|
||||||
{
|
|
||||||
let mut scheduler = SCHEDULER.try_lock().unwrap();
|
|
||||||
k_thread = scheduler.get_thread(K_THREAD_ID).unwrap().as_ptr();
|
|
||||||
} // Drop scheduler mutex guard
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
(&mut *k_thread).run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn routine() {
|
|
||||||
println!("Routine executed");
|
|
||||||
crate::syscalls::syscall_routine(crate::syscalls::EXIT_ID); // Call exit
|
|
||||||
println!("SHOULD NEVER BE DISPLAYED");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Thread {
|
pub struct Thread {
|
||||||
pub id: ThreadId,
|
rsp: u64
|
||||||
pub entry_point: u64,
|
|
||||||
pub started: bool,
|
|
||||||
pub rsp: u64,
|
|
||||||
pub base_stack: u64,
|
|
||||||
pub is_blocked: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Thread {
|
impl Thread {
|
||||||
pub fn new(entry_point: u64) -> Self {
|
pub fn new() -> Self {
|
||||||
unsafe {
|
unsafe {
|
||||||
let stack_bottom = alloc(Layout::new::<[u8; STACK_SIZE]>()) as u64;
|
|
||||||
Thread {
|
Thread {
|
||||||
id: ThreadId::new(),
|
rsp: alloc(Layout::new::<[u8; STACK_SIZE]>()) as u64,
|
||||||
entry_point: entry_point,
|
|
||||||
started: false,
|
|
||||||
rsp: stack_bottom + STACK_SIZE as u64,
|
|
||||||
base_stack: stack_bottom,
|
|
||||||
is_blocked: false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exit(&self) {
|
|
||||||
unsafe {
|
|
||||||
dealloc(
|
|
||||||
self.base_stack as *mut u8,
|
|
||||||
Layout::new::<[u8; STACK_SIZE]>(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
|
||||||
println!("Running thread {:?}", self.id);
|
|
||||||
unsafe {
|
|
||||||
let mut current_thread_guard = RUNNING_THREAD.try_lock().unwrap();
|
|
||||||
|
|
||||||
let mut scheduler = SCHEDULER.try_lock().unwrap();
|
|
||||||
if let Some(current_thread) = scheduler.get_thread(*current_thread_guard) {
|
|
||||||
let current_rsp: u64;
|
|
||||||
asm!(
|
|
||||||
"push rsp", // Recover current rsp
|
|
||||||
"pop {out}",
|
|
||||||
"sub {out}, 56", // Offset to saved registers
|
|
||||||
out = out(reg) current_rsp, // Save thread rsp
|
|
||||||
);
|
|
||||||
current_thread.borrow_mut().rsp = current_rsp;
|
|
||||||
} else {
|
|
||||||
// Thread does not exists anymore
|
|
||||||
*current_thread_guard = self.id; // change running thread
|
|
||||||
asm!( // Just switch to new thead without saving registers
|
|
||||||
"push {rsp}", // Set stack pointer to the new thread
|
|
||||||
"pop rsp",
|
|
||||||
|
|
||||||
"pop rdi", // Restore new thread regs
|
|
||||||
"pop rsi",
|
|
||||||
"pop rbp",
|
|
||||||
"pop rdx",
|
|
||||||
"pop rcx",
|
|
||||||
"pop rbx",
|
|
||||||
"pop rax",
|
|
||||||
rsp = in(reg) self.rsp,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*current_thread_guard = self.id; // change running thread
|
|
||||||
} // The scheduler and running thread guards are dropped here
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
if self.started {
|
|
||||||
asm!(
|
|
||||||
"push rax", // Save current thread regs
|
|
||||||
"push rbx",
|
|
||||||
"push rcx",
|
|
||||||
"push rdx",
|
|
||||||
"push rbp",
|
|
||||||
"push rsi",
|
|
||||||
"push rdi",
|
|
||||||
|
|
||||||
"push {rsp}", // Set stack pointer to the new thread
|
|
||||||
"pop rsp",
|
|
||||||
|
|
||||||
"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",
|
|
||||||
|
|
||||||
"push {rsp}", // Set stack pointer to the new thread
|
|
||||||
"pop rsp",
|
|
||||||
"jmp {rip}", // Jump to thread routine
|
|
||||||
rsp = in(reg) self.rsp,
|
|
||||||
rip = in(reg) self.entry_point,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
pub type SyscallId = u64;
|
|
||||||
|
|
||||||
pub const EXIT_ID: SyscallId = 0;
|
|
||||||
@@ -1,60 +1 @@
|
|||||||
use crate::println;
|
|
||||||
use crate::proc::thread::{resume_k_thread, RUNNING_THREAD};
|
|
||||||
use crate::proc::scheduler::SCHEDULER;
|
|
||||||
use crate::task::yield_executor::YieldExecutor;
|
|
||||||
use crate::task::Task;
|
|
||||||
|
|
||||||
pub use ids::*;
|
|
||||||
|
|
||||||
use alloc::sync::Arc;
|
|
||||||
use core::cell::RefCell;
|
|
||||||
|
|
||||||
pub mod ids;
|
|
||||||
pub mod io;
|
pub mod io;
|
||||||
pub mod proc;
|
|
||||||
|
|
||||||
pub type SyscallContextT = Arc<RefCell<SyscallContext>>;
|
|
||||||
|
|
||||||
pub struct SyscallContext {
|
|
||||||
id: SyscallId,
|
|
||||||
res: u64,
|
|
||||||
thread_id: crate::proc::thread::ThreadId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SyscallContext {
|
|
||||||
pub async fn run(&mut self) {
|
|
||||||
println!("Running async syscall runner for {:?}", self.id);
|
|
||||||
self.dispatch().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn dispatch(&mut self) {
|
|
||||||
match self.id {
|
|
||||||
EXIT_ID => proc::exit(self).await,
|
|
||||||
_ => bad_syscall().await,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async fn syscall_runner(context: SyscallContextT) {
|
|
||||||
context.borrow_mut().run().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn syscall_routine(syscall_id: SyscallId) -> u64 {
|
|
||||||
println!("Running syscall interrupt handler");
|
|
||||||
let context: SyscallContextT = Arc::new(RefCell::new(SyscallContext {
|
|
||||||
id: syscall_id,
|
|
||||||
res: 0,
|
|
||||||
thread_id: *RUNNING_THREAD.try_lock().unwrap(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
println!("Spawning async syscall runner");
|
|
||||||
let mut executor = YieldExecutor::new(context.borrow().thread_id);
|
|
||||||
executor.spawn(Task::new(syscall_runner(context.clone())));
|
|
||||||
executor.run();
|
|
||||||
|
|
||||||
let res = context.borrow().res;
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn bad_syscall() {}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
use crate::println;
|
|
||||||
use crate::proc::scheduler::SCHEDULER;
|
|
||||||
use crate::proc::thread::resume_k_thread;
|
|
||||||
|
|
||||||
use super::SyscallContext;
|
|
||||||
|
|
||||||
pub async fn exit(context: &SyscallContext) {
|
|
||||||
println!("Running exit(2)");
|
|
||||||
SCHEDULER.lock().await.exit(context.thread_id);
|
|
||||||
resume_k_thread();
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,7 @@
|
|||||||
use crate::utils::AsyncMutex;
|
|
||||||
|
|
||||||
use super::{Task, TaskId};
|
use super::{Task, TaskId};
|
||||||
|
|
||||||
use alloc::{collections::BTreeMap, sync::Arc, task::Wake};
|
use alloc::{collections::BTreeMap, sync::Arc, task::Wake};
|
||||||
use core::task::{Context, Poll, Waker};
|
use core::task::{Context, Poll, Waker};
|
||||||
use crossbeam_queue::ArrayQueue;
|
use crossbeam_queue::ArrayQueue;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
pub static ref EXECUTOR: AsyncMutex<Executor> = AsyncMutex::new(Executor::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Executor {
|
pub struct Executor {
|
||||||
tasks: BTreeMap<TaskId, Task>,
|
tasks: BTreeMap<TaskId, Task>,
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use core::sync::atomic::{AtomicU64, Ordering};
|
|||||||
use core::task::{Context, Poll};
|
use core::task::{Context, Poll};
|
||||||
|
|
||||||
pub mod executor;
|
pub mod executor;
|
||||||
pub mod yield_executor;
|
|
||||||
pub mod keyboard;
|
pub mod keyboard;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
|||||||
@@ -1,119 +0,0 @@
|
|||||||
|
|
||||||
use crate::println;
|
|
||||||
use crate::proc::thread::{ThreadId, resume_k_thread};
|
|
||||||
use crate::proc::scheduler::SCHEDULER;
|
|
||||||
|
|
||||||
use super::{Task, TaskId};
|
|
||||||
|
|
||||||
use alloc::{collections::BTreeMap, sync::Arc, task::Wake};
|
|
||||||
use core::task::{Context, Poll, Waker};
|
|
||||||
use crossbeam_queue::ArrayQueue;
|
|
||||||
|
|
||||||
pub struct YieldExecutor {
|
|
||||||
tasks: BTreeMap<TaskId, Task>,
|
|
||||||
task_queue: Arc<ArrayQueue<TaskId>>,
|
|
||||||
waker_cache: BTreeMap<TaskId, Waker>,
|
|
||||||
thread_id: ThreadId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl YieldExecutor {
|
|
||||||
pub fn new(thread_id: ThreadId) -> Self {
|
|
||||||
YieldExecutor {
|
|
||||||
tasks: BTreeMap::new(),
|
|
||||||
task_queue: Arc::new(ArrayQueue::new(100)),
|
|
||||||
waker_cache: BTreeMap::new(),
|
|
||||||
thread_id: thread_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
|
||||||
loop {
|
|
||||||
self.run_ready_tasks();
|
|
||||||
if self.done() {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
println!("Blocking thread");
|
|
||||||
SCHEDULER
|
|
||||||
.try_lock()
|
|
||||||
.unwrap()
|
|
||||||
.block(self.thread_id);
|
|
||||||
println!("Returning to scheduler");
|
|
||||||
resume_k_thread();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn done(&self) -> bool {
|
|
||||||
self.tasks.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spawn(&mut self, task: Task) {
|
|
||||||
let task_id = task.id;
|
|
||||||
if self.tasks.insert(task.id, task).is_some() {
|
|
||||||
panic!("Duplicate task ID");
|
|
||||||
}
|
|
||||||
self.task_queue.push(task_id).expect("Task queue full");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_ready_tasks(&mut self) {
|
|
||||||
let Self {
|
|
||||||
tasks,
|
|
||||||
task_queue,
|
|
||||||
waker_cache,
|
|
||||||
thread_id,
|
|
||||||
} = self; // Executor destructuring
|
|
||||||
|
|
||||||
while let Ok(task_id) = task_queue.pop() {
|
|
||||||
let task = match tasks.get_mut(&task_id) {
|
|
||||||
Some(task) => task,
|
|
||||||
None => continue, // Task does not exist anymore
|
|
||||||
};
|
|
||||||
let waker = waker_cache
|
|
||||||
.entry(task_id)
|
|
||||||
.or_insert_with(|| YieldTaskWaker::new(task_id, task_queue.clone(), *thread_id));
|
|
||||||
let mut context = Context::from_waker(waker);
|
|
||||||
match task.poll(&mut context) {
|
|
||||||
Poll::Ready(()) => {
|
|
||||||
// task is done
|
|
||||||
tasks.remove(&task_id);
|
|
||||||
waker_cache.remove(&task_id);
|
|
||||||
}
|
|
||||||
Poll::Pending => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct YieldTaskWaker {
|
|
||||||
task_id: TaskId,
|
|
||||||
task_queue: Arc<ArrayQueue<TaskId>>,
|
|
||||||
thread_id: ThreadId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl YieldTaskWaker {
|
|
||||||
fn new(task_id: TaskId, task_queue: Arc<ArrayQueue<TaskId>>, thread_id: ThreadId) -> Waker {
|
|
||||||
Waker::from(Arc::new(YieldTaskWaker {
|
|
||||||
task_id,
|
|
||||||
task_queue,
|
|
||||||
thread_id
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wake_task(&self) {
|
|
||||||
self.task_queue.push(self.task_id).expect("Task queue full");
|
|
||||||
SCHEDULER
|
|
||||||
.try_lock()
|
|
||||||
.unwrap()
|
|
||||||
.unblock(self.thread_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Wake for YieldTaskWaker {
|
|
||||||
fn wake(self: Arc<Self>) {
|
|
||||||
self.wake_task();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wake_by_ref(self: &Arc<Self>) {
|
|
||||||
self.wake_task();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-6
@@ -11,7 +11,6 @@ use futures_util::task::AtomicWaker;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Lock {
|
struct Lock {
|
||||||
lock: Arc<AtomicBool>,
|
lock: Arc<AtomicBool>,
|
||||||
// TODO: Make a queue of wakers
|
|
||||||
waker: Arc<AtomicWaker>,
|
waker: Arc<AtomicWaker>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,17 +73,13 @@ impl<T> AsyncMutex<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_lock(&self) -> Option<AsyncMutexGuard<T>> {
|
pub fn try_lock(&self) -> Option<AsyncMutexGuard<T>> {
|
||||||
if !self.lock.try_lock() {
|
if self.lock.try_lock() {
|
||||||
Some(AsyncMutexGuard { mutex: self })
|
Some(AsyncMutexGuard { mutex: self })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }
|
||||||
|
|||||||