-
Notifications
You must be signed in to change notification settings - Fork 128
feat: add PS/2 keyboard interrupt driver #2532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ default = [ | |
| "dhcpv4", | ||
| "fsgsbase", | ||
| "kernel-stack", | ||
| "keyboard", | ||
| "pci-ids", | ||
| "pci", | ||
| "smp", | ||
|
|
@@ -216,6 +217,11 @@ virtio-vsock = ["virtio"] | |
| ## This is only useful on PCs (x86-64). | ||
| vga = [] | ||
|
|
||
| ## Enables the PS/2 keyboard driver. | ||
| ## | ||
| ## This is only useful on PCs (x86-64). | ||
| keyboard = [] | ||
|
Comment on lines
+220
to
+223
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you rename this to |
||
|
|
||
| #! ### Performance Features | ||
|
|
||
| ## Disables putting the CPU to sleep. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -157,7 +157,14 @@ pub(crate) fn install() { | |||||||
| IRQ_NAMES.lock().insert(7, "FPU"); | ||||||||
| } | ||||||||
|
|
||||||||
| pub(crate) fn install_handlers(handlers: InterruptHandlerMap) { | ||||||||
| pub(crate) fn install_handlers(#[allow(unused_mut)] mut handlers: InterruptHandlerMap) { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the allow to the function level instead of having it inline like this. |
||||||||
| #[cfg(feature = "keyboard")] | ||||||||
| { | ||||||||
| use crate::arch::kernel::keyboard::get_keyboard_handler; | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| let (irq, handler) = get_keyboard_handler(); | ||||||||
| handlers.entry(irq).or_default().push_back(handler); | ||||||||
| } | ||||||||
|
|
||||||||
| IRQ_HANDLERS.set(handlers).unwrap(); | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the pc-keyboard crate help here in any way? I'd like to avoid reimplementing logic if the ecosystem already has a well-established crate for (parts of) this. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||||||||
| use core::sync::atomic::{AtomicU8, AtomicUsize, Ordering}; | ||||||||||||||||
|
|
||||||||||||||||
| use x86_64::instructions::port::Port; | ||||||||||||||||
|
|
||||||||||||||||
| use crate::kernel::interrupts; | ||||||||||||||||
|
|
||||||||||||||||
| const BUFFER_SIZE: usize = 256; | ||||||||||||||||
| #[allow(clippy::declare_interior_mutable_const)] | ||||||||||||||||
| const ATOMIC_ZERO: AtomicU8 = AtomicU8::new(0); | ||||||||||||||||
| const PS2_DATA_PORT: u16 = 0x60; | ||||||||||||||||
| const PS2_CMD_PORT: u16 = 0x64; | ||||||||||||||||
| const PS2_CMD_READ_CNFG: u8 = 0x20; | ||||||||||||||||
| const PS2_CMD_WRITE_CNFG: u8 = 0x60; | ||||||||||||||||
| const PS2_CMD_DISABLE_KEYBOARD: u8 = 0xad; | ||||||||||||||||
| const PS2_CMD_DISABLE_MOUSE: u8 = 0xa7; | ||||||||||||||||
| const PS2_CMD_ENABLE_KEYBOARD: u8 = 0xae; | ||||||||||||||||
| const PS2_CNFG_ENABLE_KEYBOARD_INTERRUPT: u8 = 0x01; | ||||||||||||||||
| const PS2_BUFFER_FULL: u8 = 0x01; | ||||||||||||||||
|
|
||||||||||||||||
| static KEYBOARD_BUFFER: [AtomicU8; BUFFER_SIZE] = [ATOMIC_ZERO; BUFFER_SIZE]; | ||||||||||||||||
| static WRITE_INDEX: AtomicUsize = AtomicUsize::new(0); | ||||||||||||||||
| static READ_INDEX: AtomicUsize = AtomicUsize::new(0); | ||||||||||||||||
|
Comment on lines
+20
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While atomics prevent data races and deadlocks here, I am pretty sure we still have undesirable race conditions or possible data loss like this. What about having a proper struct behind a mutex for this? That way, we can also use kernel/src/arch/x86_64/kernel/serial.rs Lines 15 to 21 in 274120e
|
||||||||||||||||
|
|
||||||||||||||||
| pub(crate) fn get_keyboard_handler() -> (u8, fn()) { | ||||||||||||||||
| unsafe { | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try to have as few unsafe operations per block as possible. Ideally, only one. |
||||||||||||||||
| let mut cmd_port = Port::<u8>::new(PS2_CMD_PORT); | ||||||||||||||||
| let mut data_port = Port::<u8>::new(PS2_DATA_PORT); | ||||||||||||||||
|
Comment on lines
+26
to
+27
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think an abstraction similar to the proposal for BGA would make sense? struct Ps2;
impl Ps2 {
fn read_data() -> u8;
fn write_data(data: u8);
fn status() -> u8;
fn command(command: u8);
} |
||||||||||||||||
| cmd_port.write(PS2_CMD_DISABLE_KEYBOARD); | ||||||||||||||||
| cmd_port.write(PS2_CMD_DISABLE_MOUSE); | ||||||||||||||||
|
|
||||||||||||||||
| while (cmd_port.read() & PS2_BUFFER_FULL) != 0 { | ||||||||||||||||
| let _ = data_port.read(); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+31
to
+33
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we discard the device buffer instead of filling our read buffer? |
||||||||||||||||
|
|
||||||||||||||||
| cmd_port.write(PS2_CMD_READ_CNFG); | ||||||||||||||||
| let mut config = data_port.read(); | ||||||||||||||||
|
|
||||||||||||||||
| config |= PS2_CNFG_ENABLE_KEYBOARD_INTERRUPT; | ||||||||||||||||
|
|
||||||||||||||||
| cmd_port.write(PS2_CMD_WRITE_CNFG); | ||||||||||||||||
| data_port.write(config); | ||||||||||||||||
| cmd_port.write(PS2_CMD_ENABLE_KEYBOARD); | ||||||||||||||||
| } | ||||||||||||||||
| fn keyboard_handler() { | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the handler to the top level outside of this function. |
||||||||||||||||
| let mut data_port = Port::<u8>::new(PS2_DATA_PORT); | ||||||||||||||||
| let scancode = unsafe { data_port.read() }; | ||||||||||||||||
|
|
||||||||||||||||
| let write_idx = WRITE_INDEX.load(Ordering::Relaxed); | ||||||||||||||||
| let next_write_idx = write_idx.wrapping_add(1) % BUFFER_SIZE; | ||||||||||||||||
|
|
||||||||||||||||
| let read_idx = READ_INDEX.load(Ordering::Acquire); | ||||||||||||||||
| if next_write_idx != read_idx { | ||||||||||||||||
| KEYBOARD_BUFFER[write_idx].store(scancode, Ordering::Release); | ||||||||||||||||
| WRITE_INDEX.store(next_write_idx, Ordering::Release); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| interrupts::add_irq_name(1, "PS/2 Keyboard"); | ||||||||||||||||
|
|
||||||||||||||||
| (1, keyboard_handler) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /// Pops a scancode from the keyboard buffer, returning None if the buffer is empty. | ||||||||||||||||
| pub fn pop_scancode() -> Option<u8> { | ||||||||||||||||
|
Comment on lines
+63
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A scancode can never be zero, right? Returning |
||||||||||||||||
| let read_idx = READ_INDEX.load(Ordering::Relaxed); | ||||||||||||||||
| let write_idx = WRITE_INDEX.load(Ordering::Acquire); | ||||||||||||||||
|
|
||||||||||||||||
| if read_idx == write_idx { | ||||||||||||||||
| None | ||||||||||||||||
| } else { | ||||||||||||||||
| let scancode = KEYBOARD_BUFFER[read_idx].load(Ordering::Acquire); | ||||||||||||||||
| READ_INDEX.store(read_idx.wrapping_add(1) % BUFFER_SIZE, Ordering::Release); | ||||||||||||||||
| Some(scancode) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,17 @@ use crate::arch::mm::paging::{BasePageSize, PageSize}; | |
| pub extern "C" fn sys_getpagesize() -> i32 { | ||
| BasePageSize::SIZE.try_into().unwrap() | ||
| } | ||
|
|
||
| #[cfg(all(target_arch = "x86_64", feature = "keyboard"))] | ||
| #[hermit_macro::system] | ||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn sys_read_keyboard() -> u8 { | ||
| crate::kernel::keyboard::pop_scancode().unwrap_or(0) | ||
| } | ||
|
Comment on lines
+10
to
+15
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not too sure about this API. Is the application supposed to busy loop on this and retrieve one event at a time? What about doing something similar to Linux's event device (evdev) interface (Linux docs)? Reading from |
||
|
|
||
| #[cfg(not(all(target_arch = "x86_64", feature = "keyboard")))] | ||
| #[hermit_macro::system] | ||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn sys_read_keyboard() -> u8 { | ||
| 0 | ||
| } | ||
|
Comment on lines
+17
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the framebuffer PR, please either merge the function definitions or (preferrably) just remove the symbol when the feature is disabled. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be a default feature.