refactor: unsafe only in adapter + libkos; forbid(unsafe_code) everywhere else; libkos Window (volatile, bounds-checked MMIO/DMA/shared mem), entry!, thread::spawn; MapRegion returns pages; kernel const-built (with_cores/install_platform); newtype addrs
This commit is contained in:
@@ -1,33 +1,33 @@
|
||||
#![no_std]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use libkos::Window;
|
||||
use port_input_device::{InputDevice, InputEvent, Key};
|
||||
use types::address::VirtualAddress;
|
||||
|
||||
const UART_VA: usize = 0x20000;
|
||||
const UART_VA: VirtualAddress = VirtualAddress::from_raw(0x20000);
|
||||
const UART_IER_OFFSET: usize = 1;
|
||||
const UART_LSR_OFFSET: usize = 5;
|
||||
/// IER bit: interrupt when a received byte is waiting (cleared by reading
|
||||
/// it, so `acknowledge` has nothing to do).
|
||||
const IER_RX_READY: u8 = 1;
|
||||
|
||||
pub struct UartInputDevice;
|
||||
/// The UART's registers, one byte each.
|
||||
pub struct UartInputDevice {
|
||||
regs: Window,
|
||||
}
|
||||
|
||||
impl UartInputDevice {
|
||||
/// Maps the 16550 UART whose region `mmio` names and enables its
|
||||
/// receive interrupt.
|
||||
pub fn new(mmio: types::bounded::CapSlot) -> Option<Self> {
|
||||
libkos::device::map_mmio(mmio, UART_VA).ok()?;
|
||||
// SAFETY: UART_VA now maps the UART's registers.
|
||||
unsafe { core::ptr::write_volatile((UART_VA + UART_IER_OFFSET) as *mut u8, IER_RX_READY) };
|
||||
Some(Self)
|
||||
let regs = libkos::device::map_mmio(mmio, UART_VA).ok()?;
|
||||
regs.write(UART_IER_OFFSET, IER_RX_READY);
|
||||
Some(Self { regs })
|
||||
}
|
||||
|
||||
fn read_byte(&self) -> Option<u8> {
|
||||
let lsr = unsafe { core::ptr::read_volatile((UART_VA + UART_LSR_OFFSET) as *const u8) };
|
||||
if lsr & 1 != 0 {
|
||||
Some(unsafe { core::ptr::read_volatile(UART_VA as *const u8) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
self.has_event().then(|| self.regs.read(0))
|
||||
}
|
||||
|
||||
fn byte_to_key(byte: u8) -> Key {
|
||||
@@ -50,7 +50,6 @@ impl InputDevice for UartInputDevice {
|
||||
}
|
||||
|
||||
fn has_event(&self) -> bool {
|
||||
let lsr = unsafe { core::ptr::read_volatile((UART_VA + UART_LSR_OFFSET) as *const u8) };
|
||||
lsr & 1 != 0
|
||||
self.regs.read::<u8>(UART_LSR_OFFSET) & 1 != 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user