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" version = "0.1.0"
dependencies = [ dependencies = [
"font8x8", "font8x8",
"heapless",
"lazy_static", "lazy_static",
"limine", "limine",
"pc-keyboard", "pc-keyboard",
"pic8259", "pic8259",
"spin 0.10.0", "spin 0.10.0",
"static_cell",
"x86_64", "x86_64",
] ]
@@ -27,6 +29,12 @@ version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "const_fn" name = "const_fn"
version = "0.4.12" version = "0.4.12"
@@ -39,6 +47,25 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "875488b8711a968268c7cf5d139578713097ca4635a76044e8fe8eedf831d07e" 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]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.5.0" version = "1.5.0"
@@ -81,6 +108,12 @@ dependencies = [
"x86_64", "x86_64",
] ]
[[package]]
name = "portable-atomic"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.22" version = "1.0.22"
@@ -108,6 +141,21 @@ dependencies = [
"lock_api", "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]] [[package]]
name = "volatile" name = "volatile"
version = "0.4.6" version = "0.4.6"

View File

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

View File

@@ -1,6 +1,6 @@
use crate::{ use crate::{
arch::x86_64::{gdt, mouse::mouse_interrupt}, 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, println,
}; };
use lazy_static::lazy_static; 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 mut port = Port::new(0x60);
let scancode: u8 = unsafe { port.read() }; let scancode: u8 = unsafe { port.read() };
process_keyboard_event(scancode); push_scancode(scancode);
unsafe { unsafe {
PICS.lock() 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() { if let Some(fb) = guard.as_mut() {
f(fb); 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 pc_keyboard::{DecodedKey, HandleControl, KeyState, Keyboard, ScancodeSet2, layouts};
use spin::mutex::Mutex; use static_cell::StaticCell;
use x86_64::instructions::interrupts::without_interrupts;
use crate::task::scheduler::SCHEDULER;
use crate::task::scheduler::{SCHEDULER, current_pid};
#[repr(C)] #[repr(C)]
#[derive(Clone, Debug, Copy, Default)] #[derive(Clone, Debug, Copy, Default)]
pub struct KeyboardEvent { pub struct KeyboardEvent {
@@ -15,65 +15,85 @@ pub struct KeyboardEvent {
pub unicode: u32, pub unicode: u32,
} }
pub fn process_keyboard_event(scancode: u8) { static SCANCODE_QUEUE: StaticCell<Queue<u8, 256>> = StaticCell::new();
let mut keyboard = without_interrupts(|| KEYBOARD.lock()); 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()); static DROPPED_SCANCODES: AtomicU64 = AtomicU64::new(0);
let pid = scheduler.current_process;
if pid < 0 { pub fn init_keyboard() {
return; let q = SCANCODE_QUEUE.init(Queue::new());
let (p, c) = q.split();
unsafe {
SCANCODE_PROD = Some(p);
SCANCODE_CONS = Some(c);
KEYBOARD = Some(Keyboard::new(
ScancodeSet2::new(),
layouts::Us104Key,
HandleControl::Ignore,
))
} }
}
drop(scheduler); 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 let Ok(Some(key_event)) = keyboard.add_byte(scancode) { if !pushed {
let keycode = key_event.code; DROPPED_SCANCODES.fetch_add(1, Ordering::Relaxed);
let keystate = key_event.state; }
if let Some(key) = keyboard.process_keyevent(key_event) { }
match key {
DecodedKey::Unicode(character) => { pub fn process_scancodes() {
SCHEDULER.with_process(pid as u64, |process| { loop {
process.kbd_buffer.push(KeyboardEvent { let scancode = unsafe {
state: if keystate == KeyState::Down { 1 } else { 0 }, #[allow(static_mut_refs)]
_pad1: 0, match SCANCODE_CONS.as_mut() {
key: keycode as u16, Some(cons) => match cons.dequeue() {
mods: 0, Some(b) => b,
_pad2: 0, None => break,
unicode: character as u32, },
}) None => break,
});
}
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 { if let Some(kbd_event) = process_scancode(scancode) {
state: if keystate == KeyState::Down { 1 } else { 0 }, let pid = current_pid().unwrap_or(0);
_pad1: 0, if pid == 0 {
key: keycode as u16, continue;
mods: 0, }
_pad2: 0, SCHEDULER.with_process(pid, |process| process.kbd_buffer.push(kbd_event));
unicode: 0,
})
});
} }
} }
} }
pub static KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet2>> = Mutex::new(Keyboard::new( pub fn process_scancode(scancode: u8) -> Option<KeyboardEvent> {
ScancodeSet2::new(), #[allow(static_mut_refs)]
layouts::Us104Key, let kbd = unsafe { KEYBOARD.as_mut().expect("keyboard not initialized") };
HandleControl::Ignore, 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<'_> { impl Write for ConsoleWriter<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
serial_print(s); serial_print(s);
// self.console.render_text(self.fb, s, 2, false); self.console.render_text(self.fb, s, 2, false);
Ok(()) Ok(())
} }
} }

View File

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

View File

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

View File

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

View File

@@ -100,74 +100,76 @@ pub fn userspace_init(mapper: &mut OffsetPageTable) -> ! {
// let mut mouse_x = 100; // let mut mouse_x = 100;
// let mut mouse_y = 100; // let mut mouse_y = 100;
// let mut i = 0;
// loop { // loop {
// with_serial_console(|serial_console| serial_console.clear(0, 0)); // with_serial_console(|serial_console| serial_console.clear(0, 0));
// with_framebuffer(|fb| fb.clear(rgb(77, 171, 245))); // with_framebuffer(|fb| fb.clear(rgb(77, 171, 245)));
// test_performance(|| {
// mouse_status = MOUSE.get_status();
// with_framebuffer(|mut fb| {
// width = fb.width;
// height = fb.height;
// let (x_delta, y_delta) = MOUSE.take_motion(); // // mouse_status = MOUSE.get_status();
// with_framebuffer(|mut fb| {
// width = fb.width;
// height = fb.height;
// if x_delta != 0 { // let (x_delta, y_delta) = MOUSE.take_motion();
// mouse_x = (mouse_x as isize + x_delta as isize).max(0) as usize;
// }
// if y_delta != 0 {
// mouse_y = (mouse_y as isize + y_delta as isize).max(0) as usize;
// }
// if mouse_x > width {
// mouse_x = width - CURSOR_W;
// }
// if mouse_y > height {
// mouse_y = height - CURSOR_H;
// }
// rectangle_filled(&mut fb, 700, 400, 200, 200, rgb(0, 0, 0)); // if x_delta != 0 {
// rectangle_outline(&mut fb, 400, 400, 100, 100, rgb(0, 0, 0)); // mouse_x = (mouse_x as isize + x_delta as isize).max(0) as usize;
// circle_filled(&mut fb, 200, 200, 100, rgb(0, 0, 0)); // }
// circle_outline(&mut fb, 400, 200, 100, rgb(0, 0, 0)); // if y_delta != 0 {
// triangle_outline(&mut fb, 100, 400, 200, 400, 150, 600, rgb(0, 0, 0)); // mouse_y = (mouse_y as isize + y_delta as isize).max(0) as usize;
// }
// if mouse_x > width {
// mouse_x = width - CURSOR_W;
// }
// if mouse_y > height {
// mouse_y = height - CURSOR_H;
// }
// let pixels = &CURSOR_BYTES[BMP_HEADER_SIZE..]; // remove header // rectangle_filled(&mut fb, 700, 400, 200, 200, rgb(0, 0, 0));
// rectangle_outline(&mut fb, 400, 400, 100, 100, rgb(0, 0, 0));
// circle_filled(&mut fb, 200, 200, 100, rgb(0, 0, 0));
// circle_outline(&mut fb, 400, 200, 100, rgb(0, 0, 0));
// triangle_outline(&mut fb, 100, 400, 200, 400, 150, 600, rgb(0, 0, 0));
// for row in 0..CURSOR_H { // let pixels = &CURSOR_BYTES[BMP_HEADER_SIZE..]; // remove header
// let src_row = (CURSOR_H - 1 - row) * CURSOR_W * 4;
// for col in 0..CURSOR_W {
// let i = src_row + col * 4; // 4 because rgba
// let b = pixels[i]; // for row in 0..CURSOR_H {
// let g = pixels[i + 1]; // let src_row = (CURSOR_H - 1 - row) * CURSOR_W * 4;
// let r = pixels[i + 2]; // for col in 0..CURSOR_W {
// let a = pixels[i + 3]; // let i = src_row + col * 4; // 4 because rgba
// if a < 255 { // let b = pixels[i];
// continue; // let g = pixels[i + 1];
// } // let r = pixels[i + 2];
// let a = pixels[i + 3];
// let color = rgb(r, g, b); // if a < 255 {
// continue;
// fb.put_pixel((mouse_x + col) as usize, (mouse_y + row) as usize, color);
// } // }
// let color = rgb(r, g, b);
// fb.put_pixel((mouse_x + col) as usize, (mouse_y + row) as usize, color);
// } // }
// }); // }
// let (hours, minutes, seconds) =
// unix_to_hms(TIMER.get_date_at_boot() + (TIMER.now().elapsed()) / 1000);
// print!(
// "{:?}:{:?}:{:?}\nMouse status: {:?}\nDesktop Size: {:?}",
// hours,
// minutes,
// seconds,
// mouse_status,
// (width, height)
// );
// }); // });
// let (hours, minutes, seconds) =
// unix_to_hms(TIMER.get_date_at_boot() + (TIMER.now().elapsed()) / 1000);
// print!(
// "{:?}:{:?}:{:?}\nMouse status: {:?}\nDesktop Size: {:?}",
// hours,
// minutes,
// seconds,
// mouse_status,
// (width, height)
// );
// with_framebuffer(|fb| { // with_framebuffer(|fb| {
// fb.swap(); // fb.swap();
// }); // });
// sleep(1000 / 165); // sleep(1000 / 165);
// } // }
} }