Add new modules

This commit is contained in:
Philipp Oppermann
2020-01-22 17:24:17 +01:00
parent c3450b6df7
commit b75406b37e
3 changed files with 70 additions and 0 deletions

View File

@@ -33,3 +33,6 @@ test-args = [
"-display", "none"
]
test-success-exit-code = 33 # (0x10 << 1) | 1
[profile.release]
lto = true

54
src/multitasking.rs Normal file
View File

@@ -0,0 +1,54 @@
use alloc::collections::VecDeque;
use lazy_static::lazy_static;
use x86_64::structures::paging::{FrameAllocator, Mapper, Size4KiB};
use x86_64::VirtAddr;
global_asm!(include_str!("multitasking/context_switch.s"));
pub unsafe fn context_switch(stack_pointer: VirtAddr) {
asm!(
"call asm_context_switch"
:
: "{rdi}"(stack_pointer.as_u64())
: "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rpb", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15", "rflags", "memory"
: "intel", "volatile"
);
}
pub fn scheduler() {
let next = PAUSED_THREADS.try_lock().and_then(|mut t| t.pop_front());
if let Some(next) = next {
unsafe { context_switch(next) };
}
}
lazy_static! {
static ref PAUSED_THREADS: spin::Mutex<VecDeque<VirtAddr>> = spin::Mutex::new(VecDeque::new());
}
#[no_mangle]
fn add_paused_thread(stack_pointer: VirtAddr) {
add_thread(stack_pointer)
}
fn add_thread(stack_pointer: VirtAddr) {
PAUSED_THREADS.lock().push_back(stack_pointer);
}
pub fn create_thread(
f: fn() -> !,
stack_size: u64,
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) {
let mut stack = crate::memory::alloc_stack(stack_size, mapper, frame_allocator).unwrap();
stack -= core::mem::size_of::<u64>();
let ptr: *mut u64 = stack.as_mut_ptr();
unsafe { ptr.write(f as u64) };
stack -= core::mem::size_of::<u64>();
let ptr: *mut u64 = stack.as_mut_ptr();
let rflags = 0x200;
unsafe { ptr.write(rflags) };
unsafe { add_thread(stack) };
}

View File

@@ -0,0 +1,13 @@
.intel_syntax noprefix
asm_context_switch:
pushfq
mov rax, rsp
mov rsp, rdi
mov rdi, rax
call add_paused_thread
popfq
ret