mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Compare commits
10 Commits
post-12-sc
...
post-12-as
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d485535ae | ||
|
|
08582948c5 | ||
|
|
b3ba0ba4e9 | ||
|
|
c3ea4190ca | ||
|
|
f75d63853f | ||
|
|
378159ce76 | ||
|
|
a5ff4261a0 | ||
|
|
ea83d905fe | ||
|
|
786a7a6922 | ||
|
|
6329274f02 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -39,6 +39,8 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "bootloader"
|
name = "bootloader"
|
||||||
version = "0.8.8"
|
version = "0.8.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c3ed4f735c4e455ba86a3d2939b1c0729414153642106c9d035693355630a42c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
|
|||||||
@@ -31,15 +31,15 @@ version = "0.2.1"
|
|||||||
default-features = false
|
default-features = false
|
||||||
features = ["alloc"]
|
features = ["alloc"]
|
||||||
|
|
||||||
[dependencies.conquer-once]
|
|
||||||
version = "0.2.0"
|
|
||||||
default-features = false
|
|
||||||
|
|
||||||
[dependencies.futures-util]
|
[dependencies.futures-util]
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["alloc", "async-await"]
|
features = ["alloc", "async-await"]
|
||||||
|
|
||||||
|
[dependencies.conquer-once]
|
||||||
|
version = "0.2.0"
|
||||||
|
default-features = false
|
||||||
|
|
||||||
[package.metadata.bootimage]
|
[package.metadata.bootimage]
|
||||||
test-args = [
|
test-args = [
|
||||||
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||||
|
|||||||
75
src/driver/keyboard.rs
Normal file
75
src/driver/keyboard.rs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
use crate::{print, println, task::interrupt_wake};
|
||||||
|
use conquer_once::spin::OnceCell;
|
||||||
|
use core::future::Future;
|
||||||
|
use core::{
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use crossbeam_queue::ArrayQueue;
|
||||||
|
use futures_util::task::AtomicWaker;
|
||||||
|
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
||||||
|
|
||||||
|
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||||
|
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
SCANCODE_QUEUE
|
||||||
|
.try_init_once(|| ArrayQueue::new(10))
|
||||||
|
.expect("failed to init scancode queue");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called by the keyboard interrupt handler
|
||||||
|
///
|
||||||
|
/// Must not block (including spinlocks).
|
||||||
|
pub(crate) fn keyboard_scancode(scancode: u8) {
|
||||||
|
let scancode_queue = SCANCODE_QUEUE
|
||||||
|
.try_get()
|
||||||
|
.expect("scancode queue not initialized");
|
||||||
|
if let Err(_) = scancode_queue.push(scancode) {
|
||||||
|
println!("WARNING: dropping keyboard input");
|
||||||
|
}
|
||||||
|
if let Some(waker) = WAKER.take() {
|
||||||
|
interrupt_wake(waker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_scancode() -> impl Future<Output = u8> {
|
||||||
|
NextScancode
|
||||||
|
}
|
||||||
|
|
||||||
|
struct NextScancode;
|
||||||
|
|
||||||
|
impl Future for NextScancode {
|
||||||
|
type Output = u8;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<u8> {
|
||||||
|
let scancodes = SCANCODE_QUEUE
|
||||||
|
.try_get()
|
||||||
|
.expect("scancode queue not initialized");
|
||||||
|
// fast path
|
||||||
|
if let Ok(scancode) = scancodes.pop() {
|
||||||
|
return Poll::Ready(scancode);
|
||||||
|
}
|
||||||
|
|
||||||
|
WAKER.register(&cx.waker());
|
||||||
|
match scancodes.pop() {
|
||||||
|
Ok(scancode) => Poll::Ready(scancode),
|
||||||
|
Err(crossbeam_queue::PopError) => Poll::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn keyboard_task() {
|
||||||
|
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Ok(Some(key_event)) = keyboard.add_byte(next_scancode().await) {
|
||||||
|
if let Some(key) = keyboard.process_keyevent(key_event) {
|
||||||
|
match key {
|
||||||
|
DecodedKey::Unicode(character) => print!("{}", character),
|
||||||
|
DecodedKey::RawKey(key) => print!("{:?}", key),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/driver/mod.rs
Normal file
6
src/driver/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub mod keyboard;
|
||||||
|
pub mod timer;
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
keyboard::init();
|
||||||
|
}
|
||||||
55
src/driver/timer.rs
Normal file
55
src/driver/timer.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use crate::{print, task::interrupt_wake};
|
||||||
|
use core::future::Future;
|
||||||
|
use core::{
|
||||||
|
pin::Pin,
|
||||||
|
sync::atomic::{AtomicU64, Ordering},
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use futures_util::task::AtomicWaker;
|
||||||
|
|
||||||
|
static TICKS: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||||
|
|
||||||
|
/// Called by the timer interrupt handler
|
||||||
|
///
|
||||||
|
/// Must not block (including spinlocks).
|
||||||
|
pub(crate) fn tick() {
|
||||||
|
TICKS.fetch_add(1, Ordering::Release);
|
||||||
|
if let Some(waker) = WAKER.take() {
|
||||||
|
interrupt_wake(waker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_tick() -> impl Future<Output = u64> {
|
||||||
|
static NEXT_TICK: AtomicU64 = AtomicU64::new(1);
|
||||||
|
|
||||||
|
NextTick {
|
||||||
|
ticks: NEXT_TICK.fetch_add(1, Ordering::Release),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct NextTick {
|
||||||
|
ticks: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Future for NextTick {
|
||||||
|
type Output = u64;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<u64> {
|
||||||
|
WAKER.register(&cx.waker());
|
||||||
|
let current_ticks = TICKS.load(Ordering::Acquire);
|
||||||
|
if self.ticks < current_ticks {
|
||||||
|
self.ticks += 1;
|
||||||
|
Poll::Ready(self.ticks)
|
||||||
|
} else {
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn timer_task() {
|
||||||
|
loop {
|
||||||
|
next_tick().await;
|
||||||
|
print!(".");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{gdt, hlt_loop, print, println};
|
use crate::{gdt, hlt_loop, println};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use pic8259_simple::ChainedPics;
|
use pic8259_simple::ChainedPics;
|
||||||
use spin;
|
use spin;
|
||||||
@@ -72,7 +72,8 @@ extern "x86-interrupt" fn double_fault_handler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||||
print!(".");
|
crate::driver::timer::tick();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
PICS.lock()
|
PICS.lock()
|
||||||
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
|
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
|
||||||
@@ -80,28 +81,11 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||||
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
|
||||||
use spin::Mutex;
|
|
||||||
use x86_64::instructions::port::Port;
|
use x86_64::instructions::port::Port;
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> = Mutex::new(
|
|
||||||
Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut keyboard = KEYBOARD.lock();
|
|
||||||
let mut port = Port::new(0x60);
|
let mut port = Port::new(0x60);
|
||||||
|
|
||||||
let scancode: u8 = unsafe { port.read() };
|
let scancode: u8 = unsafe { port.read() };
|
||||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
|
||||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
crate::driver::keyboard::keyboard_scancode(scancode);
|
||||||
match key {
|
|
||||||
DecodedKey::Unicode(character) => print!("{}", character),
|
|
||||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
PICS.lock()
|
PICS.lock()
|
||||||
|
|||||||
12
src/lib.rs
12
src/lib.rs
@@ -5,9 +5,10 @@
|
|||||||
#![feature(alloc_error_handler)]
|
#![feature(alloc_error_handler)]
|
||||||
#![feature(const_fn)]
|
#![feature(const_fn)]
|
||||||
#![feature(alloc_layout_extra)]
|
#![feature(alloc_layout_extra)]
|
||||||
#![feature(const_in_array_repeat_expressions)]
|
|
||||||
#![feature(wake_trait)]
|
#![feature(wake_trait)]
|
||||||
#![feature(async_closure)]
|
#![feature(const_in_array_repeat_expressions)]
|
||||||
|
#![feature(type_alias_impl_trait)]
|
||||||
|
#![feature(asm)]
|
||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ extern crate alloc;
|
|||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
pub mod allocator;
|
pub mod allocator;
|
||||||
|
pub mod driver;
|
||||||
pub mod gdt;
|
pub mod gdt;
|
||||||
pub mod interrupts;
|
pub mod interrupts;
|
||||||
pub mod memory;
|
pub mod memory;
|
||||||
@@ -27,7 +29,11 @@ pub fn init() {
|
|||||||
gdt::init();
|
gdt::init();
|
||||||
interrupts::init_idt();
|
interrupts::init_idt();
|
||||||
unsafe { interrupts::PICS.lock().initialize() };
|
unsafe { interrupts::PICS.lock().initialize() };
|
||||||
x86_64::instructions::interrupts::enable();
|
}
|
||||||
|
|
||||||
|
pub fn init_heap_structures() {
|
||||||
|
task::init();
|
||||||
|
driver::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_runner(tests: &[&dyn Fn()]) {
|
pub fn test_runner(tests: &[&dyn Fn()]) {
|
||||||
|
|||||||
74
src/main.rs
74
src/main.rs
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
|
||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
use bootloader::{entry_point, BootInfo};
|
use bootloader::{entry_point, BootInfo};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
@@ -15,7 +16,6 @@ entry_point!(kernel_main);
|
|||||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||||
use blog_os::allocator;
|
use blog_os::allocator;
|
||||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||||
use blog_os::task::{simple_executor::SimpleExecutor, Task, keyboard};
|
|
||||||
use x86_64::VirtAddr;
|
use x86_64::VirtAddr;
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
@@ -26,26 +26,50 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
|||||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||||
|
|
||||||
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||||
|
blog_os::init_heap_structures();
|
||||||
|
|
||||||
|
// allocate a number on the heap
|
||||||
|
let heap_value = Box::new(41);
|
||||||
|
println!("heap_value at {:p}", heap_value);
|
||||||
|
|
||||||
|
// create a dynamically sized vector
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
for i in 0..500 {
|
||||||
|
vec.push(i);
|
||||||
|
}
|
||||||
|
println!("vec at {:p}", vec.as_slice());
|
||||||
|
|
||||||
|
// create a reference counted vector -> will be freed when count reaches 0
|
||||||
|
let reference_counted = Rc::new(vec![1, 2, 3]);
|
||||||
|
let cloned_reference = reference_counted.clone();
|
||||||
|
println!(
|
||||||
|
"current reference count is {}",
|
||||||
|
Rc::strong_count(&cloned_reference)
|
||||||
|
);
|
||||||
|
core::mem::drop(reference_counted);
|
||||||
|
println!(
|
||||||
|
"reference count is {} now",
|
||||||
|
Rc::strong_count(&cloned_reference)
|
||||||
|
);
|
||||||
|
|
||||||
|
use blog_os::task::executor::Executor;
|
||||||
|
|
||||||
|
let mut executor = Executor::new();
|
||||||
|
executor.spawn(bar());
|
||||||
|
|
||||||
|
executor.spawn(async {
|
||||||
|
#[cfg(test)]
|
||||||
|
test_main();
|
||||||
|
});
|
||||||
|
|
||||||
|
executor.spawn(async {
|
||||||
|
println!("It did not crash!");
|
||||||
|
});
|
||||||
|
|
||||||
|
executor.spawn(blog_os::driver::timer::timer_task());
|
||||||
|
executor.spawn(blog_os::driver::keyboard::keyboard_task());
|
||||||
|
|
||||||
let mut executor = SimpleExecutor::new();
|
|
||||||
executor.spawn(Task::new(example_task()));
|
|
||||||
executor.spawn(Task::new(keyboard::print_keypresses()));
|
|
||||||
executor.run();
|
executor.run();
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
test_main();
|
|
||||||
|
|
||||||
println!("It did not crash!");
|
|
||||||
blog_os::hlt_loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn async_number() -> u32 {
|
|
||||||
42
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn example_task() {
|
|
||||||
let number = async_number().await;
|
|
||||||
println!("async number: {}", number);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is called on panic.
|
/// This function is called on panic.
|
||||||
@@ -61,3 +85,15 @@ fn panic(info: &PanicInfo) -> ! {
|
|||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
blog_os::test_panic_handler(info)
|
blog_os::test_panic_handler(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn test() -> u32 {
|
||||||
|
42
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn foo() -> u32 {
|
||||||
|
test().await * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn bar() {
|
||||||
|
println!("foo result: {}", foo().await);
|
||||||
|
}
|
||||||
|
|||||||
127
src/task/executor.rs
Normal file
127
src/task/executor.rs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
use super::interrupt_wakeups::interrupt_wakeups;
|
||||||
|
use crate::println;
|
||||||
|
use alloc::{
|
||||||
|
boxed::Box,
|
||||||
|
collections::{BTreeMap, VecDeque},
|
||||||
|
sync::Arc,
|
||||||
|
task::Wake,
|
||||||
|
};
|
||||||
|
use core::{
|
||||||
|
future::Future,
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use crossbeam_queue::SegQueue;
|
||||||
|
|
||||||
|
pub type Task = Pin<Box<dyn Future<Output = ()>>>;
|
||||||
|
type TaskId = usize;
|
||||||
|
|
||||||
|
pub struct Executor {
|
||||||
|
task_queue: VecDeque<Task>,
|
||||||
|
wake_queue: Arc<SegQueue<TaskId>>,
|
||||||
|
pending_tasks: BTreeMap<TaskId, Task>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Executor {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Executor {
|
||||||
|
task_queue: VecDeque::new(),
|
||||||
|
wake_queue: Arc::new(SegQueue::new()),
|
||||||
|
pending_tasks: BTreeMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spawn(&mut self, task: impl Future<Output = ()> + 'static) {
|
||||||
|
self.task_queue.push_back(Box::pin(task))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self) -> ! {
|
||||||
|
loop {
|
||||||
|
self.run_ready_tasks();
|
||||||
|
self.apply_interrupt_wakeups();
|
||||||
|
self.wake_waiting_tasks();
|
||||||
|
self.hlt_if_idle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_ready_tasks(&mut self) {
|
||||||
|
while let Some(mut task) = self.task_queue.pop_front() {
|
||||||
|
let waker = self.create_waker(&task).into();
|
||||||
|
let mut context = Context::from_waker(&waker);
|
||||||
|
match task.as_mut().poll(&mut context) {
|
||||||
|
Poll::Ready(()) => {} // task done
|
||||||
|
Poll::Pending => {
|
||||||
|
// add task to pending_tasks and wait for wakeup
|
||||||
|
let task_id = Self::task_id(&task);
|
||||||
|
if self.pending_tasks.insert(task_id, task).is_some() {
|
||||||
|
panic!("Task with same ID already in pending_tasks");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invoke wakers for tasks woken by interrupts
|
||||||
|
///
|
||||||
|
/// The interrupt handlers can't invoke the waker directly since wakers
|
||||||
|
/// might execute arbitrary code, e.g. allocate, which should not be done
|
||||||
|
/// in interrupt handlers to avoid deadlocks.
|
||||||
|
fn apply_interrupt_wakeups(&mut self) {
|
||||||
|
while let Ok(waker) = interrupt_wakeups().pop() {
|
||||||
|
waker.wake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wake_waiting_tasks(&mut self) {
|
||||||
|
while let Ok(task_id) = self.wake_queue.pop() {
|
||||||
|
if let Some(task) = self.pending_tasks.remove(&task_id) {
|
||||||
|
self.task_queue.push_back(task);
|
||||||
|
} else {
|
||||||
|
println!("WARNING: woken task not found in pending_tasks");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Executes the `hlt` instruction if there are no ready tasks
|
||||||
|
fn hlt_if_idle(&self) {
|
||||||
|
if self.task_queue.is_empty() {
|
||||||
|
// disable interrupts to avoid races
|
||||||
|
x86_64::instructions::interrupts::disable();
|
||||||
|
// check if relevant interrupts occured since the last check
|
||||||
|
if interrupt_wakeups().is_empty() {
|
||||||
|
// no interrupts occured -> hlt to wait for next interrupt
|
||||||
|
x86_64::instructions::interrupts::enable_interrupts_and_hlt();
|
||||||
|
} else {
|
||||||
|
// there were some new wakeups -> continue execution
|
||||||
|
x86_64::instructions::interrupts::enable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn task_id(task: &Task) -> TaskId {
|
||||||
|
let future_ref: &dyn Future<Output = ()> = &**task;
|
||||||
|
future_ref as *const _ as *const () as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_waker(&self, task: &Task) -> Arc<Waker> {
|
||||||
|
Arc::new(Waker {
|
||||||
|
wake_queue: self.wake_queue.clone(),
|
||||||
|
task_id: Self::task_id(task),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Waker {
|
||||||
|
wake_queue: Arc<SegQueue<TaskId>>,
|
||||||
|
task_id: TaskId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Wake for Waker {
|
||||||
|
fn wake(self: Arc<Self>) {
|
||||||
|
self.wake_by_ref();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wake_by_ref(self: &Arc<Self>) {
|
||||||
|
self.wake_queue.push(self.task_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/task/interrupt_wakeups.rs
Normal file
25
src/task/interrupt_wakeups.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
use crate::println;
|
||||||
|
use conquer_once::spin::OnceCell;
|
||||||
|
use core::task::Waker;
|
||||||
|
use crossbeam_queue::ArrayQueue;
|
||||||
|
|
||||||
|
static INTERRUPT_WAKEUPS: OnceCell<ArrayQueue<Waker>> = OnceCell::uninit();
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
INTERRUPT_WAKEUPS
|
||||||
|
.try_init_once(|| ArrayQueue::new(10))
|
||||||
|
.expect("failed to init interrupt wakeup queue");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Queues a waker for waking in an interrupt-safe way
|
||||||
|
pub(crate) fn interrupt_wake(waker: Waker) {
|
||||||
|
if let Err(_) = interrupt_wakeups().push(waker) {
|
||||||
|
println!("WARNING: dropping interrupt wakeup");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn interrupt_wakeups() -> &'static ArrayQueue<Waker> {
|
||||||
|
INTERRUPT_WAKEUPS
|
||||||
|
.try_get()
|
||||||
|
.expect("interrupt wakeup queue not initialized")
|
||||||
|
}
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
use conquer_once::spin::OnceCell;
|
|
||||||
use crossbeam_queue::ArrayQueue;
|
|
||||||
use futures_util::stream::{Stream, StreamExt};
|
|
||||||
use core::{pin::Pin, task::{Context, Poll}};
|
|
||||||
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
|
||||||
use crate::print;
|
|
||||||
|
|
||||||
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
|
||||||
|
|
||||||
pub struct ScancodeStream {
|
|
||||||
_private: (),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ScancodeStream {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
SCANCODE_QUEUE.try_init_once(|| ArrayQueue::new(100))
|
|
||||||
.expect("ScancodeStream::new should only be called once");
|
|
||||||
ScancodeStream {
|
|
||||||
_private: (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Stream for ScancodeStream {
|
|
||||||
type Item = u8;
|
|
||||||
|
|
||||||
fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll<Option<u8>> {
|
|
||||||
let queue = SCANCODE_QUEUE
|
|
||||||
.try_get()
|
|
||||||
.expect("scancode queue not initialized");
|
|
||||||
match queue.pop() {
|
|
||||||
Ok(scancode) => Poll::Ready(Some(scancode)),
|
|
||||||
Err(crossbeam_queue::PopError) => Poll::Pending,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn print_keypresses() {
|
|
||||||
let mut scancodes = ScancodeStream::new();
|
|
||||||
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore);
|
|
||||||
|
|
||||||
while let Some(scancode) = scancodes.next().await {
|
|
||||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
|
||||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
|
||||||
match key {
|
|
||||||
DecodedKey::Unicode(character) => print!("{}", character),
|
|
||||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +1,8 @@
|
|||||||
use alloc::boxed::Box;
|
pub(crate) use interrupt_wakeups::interrupt_wake;
|
||||||
use core::task::{Context, Poll};
|
|
||||||
use core::{future::Future, pin::Pin};
|
|
||||||
|
|
||||||
pub mod keyboard;
|
pub mod executor;
|
||||||
pub mod simple_executor;
|
mod interrupt_wakeups;
|
||||||
|
|
||||||
pub struct Task {
|
pub fn init() {
|
||||||
future: Pin<Box<dyn Future<Output = ()>>>,
|
interrupt_wakeups::init();
|
||||||
}
|
|
||||||
|
|
||||||
impl Task {
|
|
||||||
pub fn new(future: impl Future<Output = ()> + 'static) -> Task {
|
|
||||||
Task {
|
|
||||||
future: Box::pin(future),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll(&mut self, context: &mut Context) -> Poll<()> {
|
|
||||||
self.future.as_mut().poll(context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
use super::Task;
|
|
||||||
use alloc::{collections::VecDeque, sync::Arc, task::Wake};
|
|
||||||
use core::task::{Context, Poll, Waker};
|
|
||||||
|
|
||||||
pub struct SimpleExecutor {
|
|
||||||
task_queue: VecDeque<Task>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SimpleExecutor {
|
|
||||||
pub fn new() -> SimpleExecutor {
|
|
||||||
SimpleExecutor {
|
|
||||||
task_queue: VecDeque::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spawn(&mut self, task: Task) {
|
|
||||||
self.task_queue.push_back(task)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
|
||||||
while let Some(mut task) = self.task_queue.pop_front() {
|
|
||||||
let waker = DummyWaker.to_waker();
|
|
||||||
let mut context = Context::from_waker(&waker);
|
|
||||||
match task.poll(&mut context) {
|
|
||||||
Poll::Ready(()) => {} // task done
|
|
||||||
Poll::Pending => self.task_queue.push_back(task),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DummyWaker;
|
|
||||||
|
|
||||||
impl Wake for DummyWaker {
|
|
||||||
fn wake(self: Arc<Self>) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DummyWaker {
|
|
||||||
fn to_waker(self) -> Waker {
|
|
||||||
Waker::from(Arc::new(self))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user