Make keyboard lock-free (and fix Doom deadlock) by only pushing

scancodes to a spsc queue from the interrupts and only process them
inside syscalls. Also make CURRENT_PID atomic to use less locking
This commit is contained in:
csd4ni3l
2026-04-19 18:22:16 +02:00
parent 2b8a965c92
commit f4c2657b94
11 changed files with 218 additions and 131 deletions

48
kernel/Cargo.lock generated
View File

@@ -7,11 +7,13 @@ name = "XunilOS"
version = "0.1.0"
dependencies = [
"font8x8",
"heapless",
"lazy_static",
"limine",
"pc-keyboard",
"pic8259",
"spin 0.10.0",
"static_cell",
"x86_64",
]
@@ -27,6 +29,12 @@ version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "const_fn"
version = "0.4.12"
@@ -39,6 +47,25 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "875488b8711a968268c7cf5d139578713097ca4635a76044e8fe8eedf831d07e"
[[package]]
name = "hash32"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2af2455f757db2b292a9b1768c4b70186d443bcb3b316252d6b540aec1cd89ed"
dependencies = [
"hash32",
"stable_deref_trait",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
@@ -81,6 +108,12 @@ dependencies = [
"x86_64",
]
[[package]]
name = "portable-atomic"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
[[package]]
name = "rustversion"
version = "1.0.22"
@@ -108,6 +141,21 @@ dependencies = [
"lock_api",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "static_cell"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0530892bb4fa575ee0da4b86f86c667132a94b74bb72160f58ee5a4afec74c23"
dependencies = [
"portable-atomic",
]
[[package]]
name = "volatile"
version = "0.4.6"

View File

@@ -10,11 +10,13 @@ bench = false
[dependencies]
font8x8 = { version = "0.3.1", default-features = false }
heapless = "0.9.2"
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
limine = "0.5"
pc-keyboard = "0.8.0"
pic8259 = "0.11.0"
spin = "0.10.0"
static_cell = "2.1.1"
[target.'cfg(target_arch = "x86_64")'.dependencies]
x86_64 = "0.15.4"

View File

@@ -1,6 +1,6 @@
use crate::{
arch::x86_64::{gdt, mouse::mouse_interrupt},
driver::{keyboard::process_keyboard_event, mouse::MOUSE, timer::TIMER},
driver::{keyboard::push_scancode, mouse::MOUSE, timer::TIMER},
println,
};
use lazy_static::lazy_static;
@@ -129,7 +129,7 @@ pub extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: Interrupt
let mut port = Port::new(0x60);
let scancode: u8 = unsafe { port.read() };
process_keyboard_event(scancode);
push_scancode(scancode);
unsafe {
PICS.lock()

View File

@@ -105,5 +105,5 @@ pub fn with_framebuffer<F: FnOnce(&mut Framebuffer)>(f: F) {
if let Some(fb) = guard.as_mut() {
f(fb);
}
})
});
}

View File

@@ -1,9 +1,9 @@
use core::sync::atomic::{AtomicU64, Ordering};
use heapless::spsc::{Consumer, Producer, Queue};
use pc_keyboard::{DecodedKey, HandleControl, KeyState, Keyboard, ScancodeSet2, layouts};
use spin::mutex::Mutex;
use x86_64::instructions::interrupts::without_interrupts;
use crate::task::scheduler::SCHEDULER;
use static_cell::StaticCell;
use crate::task::scheduler::{SCHEDULER, current_pid};
#[repr(C)]
#[derive(Clone, Debug, Copy, Default)]
pub struct KeyboardEvent {
@@ -15,65 +15,85 @@ pub struct KeyboardEvent {
pub unicode: u32,
}
pub fn process_keyboard_event(scancode: u8) {
let mut keyboard = without_interrupts(|| KEYBOARD.lock());
static SCANCODE_QUEUE: StaticCell<Queue<u8, 256>> = StaticCell::new();
static mut SCANCODE_PROD: Option<Producer<'static, u8>> = None;
static mut SCANCODE_CONS: Option<Consumer<'static, u8>> = None;
static mut KEYBOARD: Option<Keyboard<layouts::Us104Key, ScancodeSet2>> = None;
let scheduler = without_interrupts(|| SCHEDULER.lock());
let pid = scheduler.current_process;
static DROPPED_SCANCODES: AtomicU64 = AtomicU64::new(0);
if pid < 0 {
return;
}
pub fn init_keyboard() {
let q = SCANCODE_QUEUE.init(Queue::new());
let (p, c) = q.split();
drop(scheduler);
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
let keycode = key_event.code;
let keystate = key_event.state;
if let Some(key) = keyboard.process_keyevent(key_event) {
match key {
DecodedKey::Unicode(character) => {
SCHEDULER.with_process(pid as u64, |process| {
process.kbd_buffer.push(KeyboardEvent {
state: if keystate == KeyState::Down { 1 } else { 0 },
_pad1: 0,
key: keycode as u16,
mods: 0,
_pad2: 0,
unicode: character as u32,
})
});
}
DecodedKey::RawKey(_) => {
SCHEDULER.with_process(pid as u64, |process| {
process.kbd_buffer.push(KeyboardEvent {
state: if keystate == KeyState::Down { 1 } else { 0 },
_pad1: 0,
key: keycode as u16,
mods: 0,
_pad2: 0,
unicode: 0,
})
});
}
}
} else {
SCHEDULER.with_process(pid as u64, |process| {
process.kbd_buffer.push(KeyboardEvent {
state: if keystate == KeyState::Down { 1 } else { 0 },
_pad1: 0,
key: keycode as u16,
mods: 0,
_pad2: 0,
unicode: 0,
})
});
}
}
}
pub static KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet2>> = Mutex::new(Keyboard::new(
unsafe {
SCANCODE_PROD = Some(p);
SCANCODE_CONS = Some(c);
KEYBOARD = Some(Keyboard::new(
ScancodeSet2::new(),
layouts::Us104Key,
HandleControl::Ignore,
));
))
}
}
pub fn push_scancode(scancode: u8) {
let pushed = unsafe {
#[allow(static_mut_refs)]
match SCANCODE_PROD.as_mut() {
Some(prod) => prod.enqueue(scancode).is_ok(),
_ => false,
}
};
if !pushed {
DROPPED_SCANCODES.fetch_add(1, Ordering::Relaxed);
}
}
pub fn process_scancodes() {
loop {
let scancode = unsafe {
#[allow(static_mut_refs)]
match SCANCODE_CONS.as_mut() {
Some(cons) => match cons.dequeue() {
Some(b) => b,
None => break,
},
None => break,
}
};
if let Some(kbd_event) = process_scancode(scancode) {
let pid = current_pid().unwrap_or(0);
if pid == 0 {
continue;
}
SCHEDULER.with_process(pid, |process| process.kbd_buffer.push(kbd_event));
}
}
}
pub fn process_scancode(scancode: u8) -> Option<KeyboardEvent> {
#[allow(static_mut_refs)]
let kbd = unsafe { KEYBOARD.as_mut().expect("keyboard not initialized") };
if let Ok(Some(key_event)) = kbd.add_byte(scancode) {
let keycode = key_event.code;
let keystate = key_event.state;
let (unicode, state) = match (kbd.process_keyevent(key_event), keystate) {
(Some(DecodedKey::Unicode(ch)), st) => (ch as u32, st),
_ => (0, keystate),
};
return Some(KeyboardEvent {
state: if state == KeyState::Down { 1 } else { 0 },
_pad1: 0,
key: keycode as u16,
mods: 0,
_pad2: 0,
unicode,
});
} else {
return None;
}
}

View File

@@ -16,7 +16,7 @@ pub struct ConsoleWriter<'a> {
impl Write for ConsoleWriter<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
serial_print(s);
// self.console.render_text(self.fb, s, 2, false);
self.console.render_text(self.fb, s, 2, false);
Ok(())
}
}

View File

@@ -5,7 +5,6 @@ use core::{
use x86_64::{
VirtAddr,
instructions::interrupts::without_interrupts,
structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB},
};
@@ -13,12 +12,12 @@ use crate::{
arch::arch::{FRAME_ALLOCATOR, get_allocator, infinite_idle, sleep},
driver::{
graphics::{framebuffer::with_framebuffer, primitives::rectangle_filled},
keyboard::KeyboardEvent,
keyboard::{KeyboardEvent, process_scancodes},
timer::TIMER,
},
mm::usercopy::copy_to_user,
print, println,
task::scheduler::SCHEDULER,
task::scheduler::{SCHEDULER, current_pid},
util::align_up,
};
@@ -85,16 +84,14 @@ pub unsafe fn memset(ptr: *mut u8, val: u8, count: usize) {
}
fn kbd_read(user_ptr: *mut KeyboardEvent, max_events: isize) -> isize {
process_scancodes();
if max_events <= 0 || user_ptr.is_null() {
return -1;
}
let pid = without_interrupts(|| {
let scheduler = SCHEDULER.lock();
scheduler.current_process
});
let pid = current_pid().unwrap_or(0);
if pid < 0 {
if pid == 0 {
return -1;
}
@@ -117,12 +114,11 @@ fn kbd_read(user_ptr: *mut KeyboardEvent, max_events: isize) -> isize {
}
pub unsafe fn sbrk(increment: isize) -> isize {
let scheduler = without_interrupts(|| SCHEDULER.lock());
if scheduler.current_process == -1 {
let pid = current_pid().unwrap_or(0);
if pid == 0 {
return -1;
}
let pid = scheduler.current_process as u64;
drop(scheduler);
let mut frame_allocator = FRAME_ALLOCATOR.lock();
return SCHEDULER

View File

@@ -20,6 +20,7 @@ pub mod util;
use crate::arch::arch::{infinite_idle, init, kernel_crash};
use crate::driver::graphics::base::rgb;
use crate::driver::graphics::framebuffer::{init_framebuffer, with_framebuffer};
use crate::driver::keyboard::init_keyboard;
use crate::driver::serial::{ConsoleWriter, init_serial_console, with_serial_console};
use crate::driver::timer::TIMER;
use crate::userspace_stub::userspace_init;
@@ -105,7 +106,7 @@ unsafe extern "C" fn kmain() -> ! {
kernel_crash(); // Could not get required info from Limine's memory map.
}
} else {
kernel_crash(); // Could not get required info from the Limine's higher-half direct mapping.
kernel_crash(); // Could not get required info from Limine's higher-half direct mapping.
}
if let Some(framebuffer_response) = FRAMEBUFFER_REQUEST.get_response() {
@@ -116,6 +117,8 @@ unsafe extern "C" fn kmain() -> ! {
init_serial_console(0, 0);
init_keyboard();
if let Some(date_at_boot_response) = DATE_AT_BOOT_REQUEST.get_response() {
TIMER.set_date_at_boot(date_at_boot_response.timestamp().as_secs());
} else {

View File

@@ -1,11 +1,27 @@
use core::sync::atomic::{AtomicU64, Ordering};
use alloc::collections::btree_map::BTreeMap;
use x86_64::instructions::interrupts::without_interrupts;
use crate::{arch::arch::enter_usermode, task::process::Process, util::Locked};
pub static CURRENT_PID: AtomicU64 = AtomicU64::new(0);
#[inline]
pub fn current_pid() -> Option<u64> {
match CURRENT_PID.load(Ordering::Relaxed) {
0 => None,
pid => Some(pid),
}
}
#[inline]
pub fn set_current_pid(pid: Option<u64>) {
CURRENT_PID.store(pid.unwrap_or(0), Ordering::Relaxed);
}
pub struct Scheduler {
pub processes: BTreeMap<u64, Process>,
pub current_process: i64,
next_pid: u64,
}
@@ -13,7 +29,6 @@ impl Scheduler {
pub const fn new() -> Scheduler {
Scheduler {
processes: BTreeMap::new(),
current_process: -1,
next_pid: 1,
}
}
@@ -31,11 +46,12 @@ impl Locked<Scheduler> {
}
pub fn run_process(&self, pid: u64, entry_point: *const u8) {
let mut guard = without_interrupts(|| self.lock());
let stack_top = guard.processes[&pid].stack_top;
guard.current_process = pid as i64;
let stack_top = {
let guard = without_interrupts(|| self.lock());
guard.processes[&pid].stack_top
};
drop(guard);
set_current_pid(Some(pid));
enter_usermode(entry_point as u64, (stack_top & !0xF) - 8);
}

View File

@@ -100,11 +100,13 @@ pub fn userspace_init(mapper: &mut OffsetPageTable) -> ! {
// let mut mouse_x = 100;
// let mut mouse_y = 100;
// let mut i = 0;
// loop {
// with_serial_console(|serial_console| serial_console.clear(0, 0));
// with_framebuffer(|fb| fb.clear(rgb(77, 171, 245)));
// test_performance(|| {
// mouse_status = MOUSE.get_status();
// // mouse_status = MOUSE.get_status();
// with_framebuffer(|mut fb| {
// width = fb.width;
// height = fb.height;
@@ -155,7 +157,6 @@ pub fn userspace_init(mapper: &mut OffsetPageTable) -> ! {
// let (hours, minutes, seconds) =
// unix_to_hms(TIMER.get_date_at_boot() + (TIMER.now().elapsed()) / 1000);
// print!(
// "{:?}:{:?}:{:?}\nMouse status: {:?}\nDesktop Size: {:?}",
// hours,
@@ -164,10 +165,11 @@ pub fn userspace_init(mapper: &mut OffsetPageTable) -> ! {
// mouse_status,
// (width, height)
// );
// });
// with_framebuffer(|fb| {
// fb.swap();
// });
// sleep(1000 / 165);
// }
}