Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ default = [
"dhcpv4",
"fsgsbase",
"kernel-stack",
"keyboard",

Copy link
Copy Markdown
Member

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.

"pci-ids",
"pci",
"smp",
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename this to pc-keyboard? Also, please make sure that the docs don't imply that this is needed for keyboard support in general. Serial-based keyboard support is separate from this. Similar to the framebuffer, please also put a disclaimer that this does not make the kernel use the driver and instead exposes an API for applications that need to be ported.


#! ### Performance Features

## Disables putting the CPU to sleep.
Expand Down
9 changes: 8 additions & 1 deletion src/arch/x86_64/kernel/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use crate::arch::kernel::keyboard::get_keyboard_handler;
use crate::arch::kernel::keyboard::get_keyboard_handler;

let (irq, handler) = get_keyboard_handler();
handlers.entry(irq).or_default().push_back(handler);
}

IRQ_HANDLERS.set(handlers).unwrap();
}

Expand Down
75 changes: 75 additions & 0 deletions src/arch/x86_64/kernel/keyboard.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 VecDeque instead of having to reimplement one ourselves. You can take a look at the serial input for reference:

static UART_DEVICE: Lazy<InterruptTicketMutex<UartDevice>> =
Lazy::new(|| unsafe { InterruptTicketMutex::new(UartDevice::new()) });
struct UartDevice {
pub uart: Uart16550<PioBackend>,
pub buffer: VecDeque<u8>,
}


pub(crate) fn get_keyboard_handler() -> (u8, fn()) {
unsafe {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A scancode can never be zero, right? Returning Option<NonZero<u8>> would be preferable in that case.

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)
}
}
2 changes: 2 additions & 0 deletions src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod gdt;
pub mod interrupts;
#[cfg(feature = "kernel-stack")]
pub mod kernel_stack;
#[cfg(feature = "keyboard")]
pub mod keyboard;
#[cfg(all(not(feature = "pci"), feature = "virtio"))]
pub mod mmio;
#[cfg(feature = "pci")]
Expand Down
14 changes: 14 additions & 0 deletions src/syscalls/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 /dev/input/event0 would then fill a user buffer with input events and blocks if no events are there unless opened with O_NONBLOCK.


#[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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Loading