Commit b68a4aab authored by Nifou's avatar Nifou

Add a switch function in the SchedulerAlgo struct which executes the

next thread
parent 608aa158
......@@ -106,33 +106,7 @@ pub extern "x86-interrupt" fn timer_interrupt_handler(_: &mut InterruptStackFram
// Signal the end of the interrupt
end(IrqId::TIMER);
// Choose the next thread
if let Some(thread) = SCHEDULER.get().choose() {
// Change the stack to use the new context
unsafe { llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile"); }
// Load the new context
unsafe {
llvm_asm!("pop r11
mov cr3, r11
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rdx
pop rcx
pop rbx
pop rax"
: : : : "intel", "volatile");
}
}
else {
loop {
unsafe { llvm_asm!("sti; hlt; cli"); }
}
}
SCHEDULER.get().switch();
}
/// The keyboard interrupt handler
......
......@@ -67,35 +67,6 @@ impl Syscall {
crate::memory::switch_to_kernel_page_table();
SCHEDULER.get().remove_current();
if let Some(thread) = SCHEDULER.get().choose() {
// Change the stack to use the new context
unsafe { llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile"); }
// Load the new context
unsafe {
llvm_asm!("pop r11
mov cr3, r11
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rdx
pop rcx
pop rbx
pop rax
iretq"
: : : : "intel", "volatile");
}
}
else {
loop {
unsafe { llvm_asm!("sti; hlt; cli"); }
}
}
unreachable!();
}
......@@ -109,35 +80,6 @@ impl Syscall {
crate::memory::switch_to_kernel_page_table();
SCHEDULER.get().remove_current();
if let Some(thread) = SCHEDULER.get().choose() {
// Change the stack to use the new context
unsafe { llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile"); }
// Load the new context
unsafe {
llvm_asm!("pop r11
mov cr3, r11
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rdx
pop rcx
pop rbx
pop rax
iretq"
: : : : "intel", "volatile");
}
}
else {
loop {
unsafe { llvm_asm!("sti; hlt; cli"); }
}
}
unreachable!();
}
......
......@@ -23,7 +23,10 @@ pub trait SchedulerAlgo {
fn add(&mut self, thread: Thread);
/// Remove the current thread in the list of threads
fn remove_current(&mut self);
fn remove_current(&mut self) {
self.remove(self.current().expect("No current process").tid);
self.switch();
}
/// Remove the thread with tid `tid` in the list of threads
fn remove(&mut self, tid: Tid);
......@@ -42,4 +45,37 @@ pub trait SchedulerAlgo {
/// Get the mutable thread with the TID `tid`
fn get_mut(&mut self, tid: Tid) -> Option<&mut Thread>;
/// Switch to the next process
fn switch(&mut self) {
// Choose the next thread
if let Some(thread) = self.choose() {
unsafe {
// Change the stack to use the new context
llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile");
// Load the new context
llvm_asm!("pop r11
mov cr3, r11
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rdx
pop rcx
pop rbx
pop rax
iretq"
: : : : "intel", "volatile");
}
}
else {
loop {
unsafe { llvm_asm!("sti; hlt; cli"); }
}
}
unreachable!();
}
}
......@@ -57,10 +57,6 @@ impl SchedulerAlgo for RoundRobinScheduler {
thread
}
fn remove_current(&mut self) {
self.remove(self.current);
}
fn remove(&mut self, tid: Tid) {
assert!(!self.threads.is_empty());
let mut index = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment