mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-18 15:27:50 +00:00
Use crossbeam-queue and AtomicWaker for async keypress handling
This commit is contained in:
48
src/driver/keyboard.rs
Normal file
48
src/driver/keyboard.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::{interrupts, print};
|
||||
use core::future::Future;
|
||||
use core::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use pc_keyboard::{layouts, DecodedKey, Keyboard, ScancodeSet1};
|
||||
|
||||
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 = interrupts::SCANCODE_QUEUE
|
||||
.try_get()
|
||||
.expect("scancode queue not initialized");
|
||||
// fast path
|
||||
if let Ok(scancode) = scancodes.pop() {
|
||||
return Poll::Ready(scancode);
|
||||
}
|
||||
|
||||
interrupts::KEYBOARD_INTERRUPT_WAKER.register(&cx.waker());
|
||||
match scancodes.pop() {
|
||||
Ok(scancode) => Poll::Ready(scancode),
|
||||
Err(crossbeam_queue::PopError) => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn print_keypresses() {
|
||||
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1);
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/driver/mod.rs
Normal file
1
src/driver/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod keyboard;
|
||||
@@ -1,4 +1,8 @@
|
||||
use crate::{gdt, hlt_loop, print, println};
|
||||
use conquer_once::spin::OnceCell;
|
||||
use core::task::Waker;
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use futures_util::task::AtomicWaker;
|
||||
use lazy_static::lazy_static;
|
||||
use pic8259_simple::ChainedPics;
|
||||
use spin;
|
||||
@@ -47,6 +51,23 @@ pub fn init_idt() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
pub fn init_queues() {
|
||||
INTERRUPT_WAKEUPS
|
||||
.try_init_once(|| ArrayQueue::new(10))
|
||||
.expect("failed to init interrupt wakeup queue");
|
||||
SCANCODE_QUEUE
|
||||
.try_init_once(|| ArrayQueue::new(10))
|
||||
.expect("failed to init scancode queue");
|
||||
}
|
||||
|
||||
static INTERRUPT_WAKEUPS: OnceCell<ArrayQueue<Waker>> = OnceCell::uninit();
|
||||
|
||||
pub(crate) fn interrupt_wakeups() -> &'static ArrayQueue<Waker> {
|
||||
INTERRUPT_WAKEUPS
|
||||
.try_get()
|
||||
.expect("interrupt wakeup queue not initialized")
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
|
||||
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
||||
}
|
||||
@@ -79,26 +100,24 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptSt
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
||||
pub(crate) static KEYBOARD_INTERRUPT_WAKER: AtomicWaker = AtomicWaker::new();
|
||||
|
||||
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||
use pc_keyboard::{layouts, DecodedKey, Keyboard, ScancodeSet1};
|
||||
use spin::Mutex;
|
||||
use x86_64::instructions::port::Port;
|
||||
|
||||
lazy_static! {
|
||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
|
||||
Mutex::new(Keyboard::new(layouts::Us104Key, ScancodeSet1));
|
||||
}
|
||||
|
||||
let mut keyboard = KEYBOARD.lock();
|
||||
let mut port = Port::new(0x60);
|
||||
|
||||
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) {
|
||||
match key {
|
||||
DecodedKey::Unicode(character) => print!("{}", character),
|
||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
||||
}
|
||||
|
||||
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) = KEYBOARD_INTERRUPT_WAKER.take() {
|
||||
if let Err(_) = interrupt_wakeups().push(waker) {
|
||||
println!("WARNING: dropping interrupt wakeup");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(wake_trait)]
|
||||
#![feature(const_in_array_repeat_expressions)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(asm)]
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
@@ -15,6 +17,7 @@ extern crate alloc;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
pub mod allocator;
|
||||
pub mod driver;
|
||||
pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
@@ -26,7 +29,6 @@ pub fn init() {
|
||||
gdt::init();
|
||||
interrupts::init_idt();
|
||||
unsafe { interrupts::PICS.lock().initialize() };
|
||||
x86_64::instructions::interrupts::enable();
|
||||
}
|
||||
|
||||
pub fn test_runner(tests: &[&dyn Fn()]) {
|
||||
|
||||
@@ -26,6 +26,8 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||
|
||||
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||
blog_os::interrupts::init_queues();
|
||||
x86_64::instructions::interrupts::enable();
|
||||
|
||||
// allocate a number on the heap
|
||||
let heap_value = Box::new(41);
|
||||
@@ -66,6 +68,8 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
println!("It did not crash!");
|
||||
});
|
||||
|
||||
spawner.spawn(blog_os::driver::keyboard::print_keypresses());
|
||||
|
||||
executor.run();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::{interrupts, println};
|
||||
use alloc::{boxed::Box, collections::BTreeMap, sync::Arc, task::Wake};
|
||||
use core::{
|
||||
future::Future,
|
||||
@@ -34,13 +35,19 @@ impl Executor {
|
||||
|
||||
pub fn run(&mut self) -> ! {
|
||||
loop {
|
||||
// perform wakeups caused by interrupts
|
||||
// the interrupt handlers can't do it themselves since wakers might execute
|
||||
// arbitrary code, e.g. allocate
|
||||
while let Ok(waker) = interrupts::interrupt_wakeups().pop() {
|
||||
waker.wake();
|
||||
}
|
||||
// wakeup waiting tasks
|
||||
while let Ok(task_id) = self.wake_queue.pop() {
|
||||
let task = self
|
||||
.pending_tasks
|
||||
.remove(&task_id)
|
||||
.expect("woken task not found in pending_tasks");
|
||||
self.task_queue.push(task);
|
||||
if let Some(task) = self.pending_tasks.remove(&task_id) {
|
||||
self.task_queue.push(task);
|
||||
} else {
|
||||
println!("WARNING: woken task not found in pending_tasks");
|
||||
}
|
||||
}
|
||||
// run ready tasks
|
||||
while let Ok(mut task) = self.task_queue.pop() {
|
||||
@@ -57,7 +64,12 @@ impl Executor {
|
||||
}
|
||||
// wait for next interrupt if there is nothing left to do
|
||||
if self.wake_queue.is_empty() {
|
||||
crate::hlt_loop();
|
||||
unsafe { asm!("cli") };
|
||||
if self.wake_queue.is_empty() {
|
||||
unsafe { asm!("sti; hlt") };
|
||||
} else {
|
||||
unsafe { asm!("sti") };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user