Merge pull request #7 from redox-os/user_irq
Userspace Schemes and Drivers
This commit is contained in:
commit
a4ede1d23d
51 changed files with 1433 additions and 650 deletions
|
|
@ -10,6 +10,7 @@ crate-type = ["staticlib"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "*"
|
bitflags = "*"
|
||||||
spin = "*"
|
spin = "*"
|
||||||
|
syscall = { path = "syscall/" }
|
||||||
|
|
||||||
[dependencies.goblin]
|
[dependencies.goblin]
|
||||||
git = "https://github.com/m4b/goblin.git"
|
git = "https://github.com/m4b/goblin.git"
|
||||||
|
|
|
||||||
12
Makefile
12
Makefile
|
|
@ -26,7 +26,9 @@ clean:
|
||||||
cargo clean --manifest-path libstd/Cargo.toml
|
cargo clean --manifest-path libstd/Cargo.toml
|
||||||
cargo clean --manifest-path init/Cargo.toml
|
cargo clean --manifest-path init/Cargo.toml
|
||||||
cargo clean --manifest-path ion/Cargo.toml
|
cargo clean --manifest-path ion/Cargo.toml
|
||||||
|
cargo clean --manifest-path drivers/ps2d/Cargo.toml
|
||||||
cargo clean --manifest-path drivers/pcid/Cargo.toml
|
cargo clean --manifest-path drivers/pcid/Cargo.toml
|
||||||
|
cargo clean --manifest-path schemes/example/Cargo.toml
|
||||||
rm -rf build
|
rm -rf build
|
||||||
|
|
||||||
FORCE:
|
FORCE:
|
||||||
|
|
@ -133,4 +135,12 @@ $(BUILD)/pcid: drivers/pcid/Cargo.toml drivers/pcid/src/** $(BUILD)/libstd.rlib
|
||||||
$(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@
|
$(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@
|
||||||
strip $@
|
strip $@
|
||||||
|
|
||||||
$(BUILD)/initfs.rs: $(BUILD)/init $(BUILD)/ion $(BUILD)/pcid
|
$(BUILD)/ps2d: drivers/ps2d/Cargo.toml drivers/ps2d/src/** $(BUILD)/libstd.rlib
|
||||||
|
$(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@
|
||||||
|
strip $@
|
||||||
|
|
||||||
|
$(BUILD)/example: schemes/example/Cargo.toml schemes/example/src/** $(BUILD)/libstd.rlib
|
||||||
|
$(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@
|
||||||
|
strip $@
|
||||||
|
|
||||||
|
$(BUILD)/initfs.rs: $(BUILD)/init $(BUILD)/ion $(BUILD)/pcid $(BUILD)/ps2d $(BUILD)/example
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ version = "0.1.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "*"
|
bitflags = "*"
|
||||||
hole_list_allocator = { path = "../../alloc/hole_list_allocator" }
|
hole_list_allocator = { path = "../../alloc/hole_list_allocator" }
|
||||||
|
io = { path = "../../drivers/io" }
|
||||||
ransid = { git = "https://github.com/redox-os/ransid.git", branch = "new_api" }
|
ransid = { git = "https://github.com/redox-os/ransid.git", branch = "new_api" }
|
||||||
spin = "*"
|
spin = "*"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,11 +59,11 @@ pub fn init_sdt(sdt: &'static Sdt, active_table: &mut ActivePageTable) {
|
||||||
if ap_local_apic.flags & 1 == 1 {
|
if ap_local_apic.flags & 1 == 1 {
|
||||||
// Allocate a stack
|
// Allocate a stack
|
||||||
// TODO: Allocate contiguous
|
// TODO: Allocate contiguous
|
||||||
let stack_start = allocate_frame().expect("no more frames in acpi stack_start").start_address().get();
|
let stack_start = allocate_frame().expect("no more frames in acpi stack_start").start_address().get() + ::KERNEL_OFFSET;
|
||||||
for _i in 0..62 {
|
for _i in 0..62 {
|
||||||
allocate_frame().expect("no more frames in acpi stack");
|
allocate_frame().expect("no more frames in acpi stack");
|
||||||
}
|
}
|
||||||
let stack_end = allocate_frame().expect("no more frames in acpi stack_end").start_address().get() + 4096;
|
let stack_end = allocate_frame().expect("no more frames in acpi stack_end").start_address().get() + 4096 + ::KERNEL_OFFSET;
|
||||||
|
|
||||||
let ap_ready = TRAMPOLINE as *mut u64;
|
let ap_ready = TRAMPOLINE as *mut u64;
|
||||||
let ap_cpu_id = unsafe { ap_ready.offset(1) };
|
let ap_cpu_id = unsafe { ap_ready.offset(1) };
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,10 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_page_table(&self) -> usize {
|
||||||
|
self.cr3
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_page_table(&mut self, address: usize) {
|
pub fn set_page_table(&mut self, address: usize) {
|
||||||
self.cr3 = address;
|
self.cr3 = address;
|
||||||
}
|
}
|
||||||
|
|
@ -52,6 +56,7 @@ impl Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Switch to the next context by restoring its stack and registers
|
/// Switch to the next context by restoring its stack and registers
|
||||||
|
#[cold]
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
#[naked]
|
#[naked]
|
||||||
pub unsafe fn switch_to(&mut self, next: &mut Context) {
|
pub unsafe fn switch_to(&mut self, next: &mut Context) {
|
||||||
|
|
@ -94,7 +99,12 @@ impl Context {
|
||||||
asm!("mov $0, rbp" : "=r"(self.rbp) : : "memory" : "intel", "volatile");
|
asm!("mov $0, rbp" : "=r"(self.rbp) : : "memory" : "intel", "volatile");
|
||||||
asm!("mov rbp, $0" : : "r"(next.rbp) : "memory" : "intel", "volatile");
|
asm!("mov rbp, $0" : : "r"(next.rbp) : "memory" : "intel", "volatile");
|
||||||
|
|
||||||
// Unset global lock, set inside of kernel
|
asm!("call context_switch_unlock" : : : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unset global lock, set inside of kernel
|
||||||
|
#[no_mangle]
|
||||||
|
extern fn context_switch_unlock(){
|
||||||
CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
|
CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
use paging::ActivePageTable;
|
use paging::ActivePageTable;
|
||||||
|
|
||||||
pub mod display;
|
pub mod display;
|
||||||
pub mod ps2;
|
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
|
|
||||||
pub unsafe fn init(active_table: &mut ActivePageTable){
|
pub unsafe fn init(active_table: &mut ActivePageTable){
|
||||||
serial::init();
|
serial::init();
|
||||||
display::init(active_table);
|
display::init(active_table);
|
||||||
ps2::init();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
use spin::Mutex;
|
use spin::Mutex;
|
||||||
use x86::io;
|
use x86::io;
|
||||||
|
|
||||||
use device::ps2::{PS2_KEYBOARD, PS2_MOUSE};
|
|
||||||
use device::serial::{COM1, COM2};
|
use device::serial::{COM1, COM2};
|
||||||
|
|
||||||
|
pub static ACKS: Mutex<[usize; 16]> = Mutex::new([0; 16]);
|
||||||
pub static COUNTS: Mutex<[usize; 16]> = Mutex::new([0; 16]);
|
pub static COUNTS: Mutex<[usize; 16]> = Mutex::new([0; 16]);
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
|
@ -17,6 +17,14 @@ unsafe fn slave_ack() {
|
||||||
master_ack();
|
master_ack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub unsafe fn acknowledge(irq: usize) {
|
||||||
|
if irq >= 8 {
|
||||||
|
slave_ack();
|
||||||
|
} else {
|
||||||
|
master_ack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interrupt!(pit, {
|
interrupt!(pit, {
|
||||||
COUNTS.lock()[0] += 1;
|
COUNTS.lock()[0] += 1;
|
||||||
master_ack();
|
master_ack();
|
||||||
|
|
@ -24,10 +32,6 @@ interrupt!(pit, {
|
||||||
|
|
||||||
interrupt!(keyboard, {
|
interrupt!(keyboard, {
|
||||||
COUNTS.lock()[1] += 1;
|
COUNTS.lock()[1] += 1;
|
||||||
if let Some(ref mut keyboard) = *PS2_KEYBOARD.lock(){
|
|
||||||
keyboard.on_irq();
|
|
||||||
}
|
|
||||||
master_ack();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
interrupt!(cascade, {
|
interrupt!(cascade, {
|
||||||
|
|
@ -84,10 +88,6 @@ interrupt!(pci3, {
|
||||||
|
|
||||||
interrupt!(mouse, {
|
interrupt!(mouse, {
|
||||||
COUNTS.lock()[12] += 1;
|
COUNTS.lock()[12] += 1;
|
||||||
if let Some(ref mut mouse) = *PS2_MOUSE.lock() {
|
|
||||||
mouse.on_irq();
|
|
||||||
}
|
|
||||||
slave_ack();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
interrupt!(fpu, {
|
interrupt!(fpu, {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
/// I/O functions
|
|
||||||
|
|
||||||
pub use self::io::*;
|
|
||||||
pub use self::mmio::*;
|
|
||||||
pub use self::pio::*;
|
|
||||||
|
|
||||||
mod io;
|
|
||||||
mod mmio;
|
|
||||||
mod pio;
|
|
||||||
|
|
@ -15,6 +15,7 @@ extern crate hole_list_allocator as allocator;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate bitflags;
|
extern crate bitflags;
|
||||||
|
extern crate io;
|
||||||
extern crate ransid;
|
extern crate ransid;
|
||||||
extern crate spin;
|
extern crate spin;
|
||||||
pub extern crate x86;
|
pub extern crate x86;
|
||||||
|
|
@ -58,6 +59,9 @@ pub extern crate x86;
|
||||||
/// Size of user stack
|
/// Size of user stack
|
||||||
pub const USER_STACK_SIZE: usize = 1024 * 1024; // 1 MB
|
pub const USER_STACK_SIZE: usize = 1024 * 1024; // 1 MB
|
||||||
|
|
||||||
|
/// Offset to user grants
|
||||||
|
pub const USER_GRANT_OFFSET: usize = USER_STACK_OFFSET + PML4_SIZE;
|
||||||
|
|
||||||
/// Offset to user temporary image (used when cloning)
|
/// Offset to user temporary image (used when cloning)
|
||||||
pub const USER_TMP_OFFSET: usize = USER_STACK_OFFSET + PML4_SIZE;
|
pub const USER_TMP_OFFSET: usize = USER_STACK_OFFSET + PML4_SIZE;
|
||||||
|
|
||||||
|
|
@ -67,14 +71,16 @@ pub extern crate x86;
|
||||||
/// Offset to user temporary stack (used when cloning)
|
/// Offset to user temporary stack (used when cloning)
|
||||||
pub const USER_TMP_STACK_OFFSET: usize = USER_TMP_HEAP_OFFSET + PML4_SIZE;
|
pub const USER_TMP_STACK_OFFSET: usize = USER_TMP_HEAP_OFFSET + PML4_SIZE;
|
||||||
|
|
||||||
|
/// Offset to user temporary page for grants
|
||||||
|
pub const USER_TMP_GRANT_OFFSET: usize = USER_TMP_STACK_OFFSET + PML4_SIZE;
|
||||||
|
|
||||||
|
|
||||||
/// Print to console
|
/// Print to console
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! print {
|
macro_rules! print {
|
||||||
($($arg:tt)*) => ({
|
($($arg:tt)*) => ({
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
let mut console = $crate::console::CONSOLE.lock();
|
let _ = write!($crate::console::CONSOLE.lock(), $($arg)*);
|
||||||
let _ = write!(console, $($arg)*);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -199,9 +205,6 @@ pub mod gdt;
|
||||||
/// Interrupt descriptor table
|
/// Interrupt descriptor table
|
||||||
pub mod idt;
|
pub mod idt;
|
||||||
|
|
||||||
/// IO Handling
|
|
||||||
pub mod io;
|
|
||||||
|
|
||||||
/// Interrupt instructions
|
/// Interrupt instructions
|
||||||
pub mod interrupt;
|
pub mod interrupt;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,13 @@ impl Mapper {
|
||||||
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn translate_page_flags(&self, page: Page) -> Option<EntryFlags> {
|
||||||
|
self.p4().next_table(page.p4_index())
|
||||||
|
.and_then(|p3| p3.next_table(page.p3_index()))
|
||||||
|
.and_then(|p2| p2.next_table(page.p2_index()))
|
||||||
|
.and_then(|p1| Some(p1[page.p1_index()].flags()))
|
||||||
|
}
|
||||||
|
|
||||||
/// Translate a virtual address to a physical one
|
/// Translate a virtual address to a physical one
|
||||||
pub fn translate(&self, virtual_address: VirtualAddress) -> Option<PhysicalAddress> {
|
pub fn translate(&self, virtual_address: VirtualAddress) -> Option<PhysicalAddress> {
|
||||||
let offset = virtual_address.get() % PAGE_SIZE;
|
let offset = virtual_address.get() % PAGE_SIZE;
|
||||||
|
|
|
||||||
|
|
@ -133,23 +133,23 @@ pub unsafe fn init(cpu_id: usize, stack_start: usize, stack_end: usize) -> (Acti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut remap = |start: usize, end: usize, flags: EntryFlags, offset: usize| {
|
let mut remap = |start: usize, end: usize, flags: EntryFlags| {
|
||||||
if end > start {
|
if end > start {
|
||||||
let start_frame = Frame::containing_address(PhysicalAddress::new(start));
|
let start_frame = Frame::containing_address(PhysicalAddress::new(start));
|
||||||
let end_frame = Frame::containing_address(PhysicalAddress::new(end - 1));
|
let end_frame = Frame::containing_address(PhysicalAddress::new(end - 1));
|
||||||
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
||||||
let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + offset));
|
let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + ::KERNEL_OFFSET));
|
||||||
mapper.map_to(page, frame, flags);
|
mapper.map_to(page, frame, flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Remap stack writable, no execute
|
// Remap stack writable, no execute
|
||||||
remap(stack_start, stack_end, PRESENT | NO_EXECUTE | WRITABLE, 0);
|
remap(stack_start - ::KERNEL_OFFSET, stack_end - ::KERNEL_OFFSET, PRESENT | NO_EXECUTE | WRITABLE);
|
||||||
|
|
||||||
// Remap a section with `flags`
|
// Remap a section with `flags`
|
||||||
let mut remap_section = |start: &u8, end: &u8, flags: EntryFlags| {
|
let mut remap_section = |start: &u8, end: &u8, flags: EntryFlags| {
|
||||||
remap(start as *const _ as usize - ::KERNEL_OFFSET, end as *const _ as usize - ::KERNEL_OFFSET, flags, ::KERNEL_OFFSET);
|
remap(start as *const _ as usize - ::KERNEL_OFFSET, end as *const _ as usize - ::KERNEL_OFFSET, flags);
|
||||||
};
|
};
|
||||||
// Remap text read-only
|
// Remap text read-only
|
||||||
remap_section(& __text_start, & __text_end, PRESENT);
|
remap_section(& __text_start, & __text_end, PRESENT);
|
||||||
|
|
@ -211,19 +211,19 @@ pub unsafe fn init_ap(cpu_id: usize, stack_start: usize, stack_end: usize, kerne
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut remap = |start: usize, end: usize, flags: EntryFlags, offset: usize| {
|
let mut remap = |start: usize, end: usize, flags: EntryFlags| {
|
||||||
if end > start {
|
if end > start {
|
||||||
let start_frame = Frame::containing_address(PhysicalAddress::new(start));
|
let start_frame = Frame::containing_address(PhysicalAddress::new(start));
|
||||||
let end_frame = Frame::containing_address(PhysicalAddress::new(end - 1));
|
let end_frame = Frame::containing_address(PhysicalAddress::new(end - 1));
|
||||||
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
||||||
let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + offset));
|
let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + ::KERNEL_OFFSET));
|
||||||
mapper.map_to(page, frame, flags);
|
mapper.map_to(page, frame, flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Remap stack writable, no execute
|
// Remap stack writable, no execute
|
||||||
remap(stack_start, stack_end, PRESENT | NO_EXECUTE | WRITABLE, 0);
|
remap(stack_start - ::KERNEL_OFFSET, stack_end - ::KERNEL_OFFSET, PRESENT | NO_EXECUTE | WRITABLE);
|
||||||
});
|
});
|
||||||
|
|
||||||
active_table.switch(new_table);
|
active_table.switch(new_table);
|
||||||
|
|
@ -328,6 +328,10 @@ impl InactivePageTable {
|
||||||
InactivePageTable { p4_frame: frame }
|
InactivePageTable { p4_frame: frame }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub unsafe fn from_address(cr3: usize) -> InactivePageTable {
|
||||||
|
InactivePageTable { p4_frame: Frame::containing_address(PhysicalAddress::new(cr3)) }
|
||||||
|
}
|
||||||
|
|
||||||
pub unsafe fn address(&self) -> usize {
|
pub unsafe fn address(&self) -> usize {
|
||||||
self.p4_frame.start_address().get()
|
self.p4_frame.start_address().get()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@ use externs::memset;
|
||||||
use gdt;
|
use gdt;
|
||||||
use idt;
|
use idt;
|
||||||
use interrupt;
|
use interrupt;
|
||||||
use memory::{self, Frame};
|
use memory;
|
||||||
use paging::{self, entry, Page, PhysicalAddress, VirtualAddress};
|
use paging::{self, entry, Page, VirtualAddress};
|
||||||
|
|
||||||
/// Test of zero values in BSS.
|
/// Test of zero values in BSS.
|
||||||
static BSS_TEST_ZERO: usize = 0;
|
static BSS_TEST_ZERO: usize = 0;
|
||||||
|
|
@ -68,8 +68,8 @@ pub unsafe extern fn kstart() -> ! {
|
||||||
memory::init(0, &__end as *const u8 as usize - ::KERNEL_OFFSET);
|
memory::init(0, &__end as *const u8 as usize - ::KERNEL_OFFSET);
|
||||||
|
|
||||||
// TODO: allocate a stack
|
// TODO: allocate a stack
|
||||||
let stack_start = 0x00080000;
|
let stack_start = 0x00080000 + ::KERNEL_OFFSET;
|
||||||
let stack_end = 0x0009F000;
|
let stack_end = 0x0009F000 + ::KERNEL_OFFSET;
|
||||||
|
|
||||||
// Initialize paging
|
// Initialize paging
|
||||||
let (mut active_table, tcb_offset) = paging::init(0, stack_start, stack_end);
|
let (mut active_table, tcb_offset) = paging::init(0, stack_start, stack_end);
|
||||||
|
|
@ -148,7 +148,7 @@ pub unsafe extern fn kstart_ap(cpu_id: usize, page_table: usize, stack_start: us
|
||||||
let kernel_table = KERNEL_TABLE.load(Ordering::SeqCst);
|
let kernel_table = KERNEL_TABLE.load(Ordering::SeqCst);
|
||||||
|
|
||||||
// Initialize paging
|
// Initialize paging
|
||||||
let (mut active_table, tcb_offset) = paging::init_ap(cpu_id, stack_start, stack_end, kernel_table);
|
let (active_table, tcb_offset) = paging::init_ap(cpu_id, stack_start, stack_end, kernel_table);
|
||||||
|
|
||||||
// Set up GDT for AP
|
// Set up GDT for AP
|
||||||
gdt::init(tcb_offset, stack_end);
|
gdt::init(tcb_offset, stack_end);
|
||||||
|
|
|
||||||
|
|
@ -122,10 +122,11 @@ long_mode:
|
||||||
mov gs, rax
|
mov gs, rax
|
||||||
mov ss, rax
|
mov ss, rax
|
||||||
|
|
||||||
mov rsp, 0x0009F000
|
mov rsp, 0xFFFFFF000009F000
|
||||||
|
|
||||||
;rust init
|
;rust init
|
||||||
mov rax, [kernel_base + 0x18]
|
mov rax, [kernel_base + 0x18]
|
||||||
|
xchg bx, bx
|
||||||
jmp rax
|
jmp rax
|
||||||
|
|
||||||
long_mode_ap:
|
long_mode_ap:
|
||||||
|
|
|
||||||
3
drivers/io/Cargo.toml
Normal file
3
drivers/io/Cargo.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
[package]
|
||||||
|
name = "io"
|
||||||
|
version = "0.1.0"
|
||||||
14
drivers/io/src/lib.rs
Normal file
14
drivers/io/src/lib.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
//! I/O functions
|
||||||
|
|
||||||
|
#![feature(asm)]
|
||||||
|
#![feature(const_fn)]
|
||||||
|
#![feature(core_intrinsics)]
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
|
pub use self::io::*;
|
||||||
|
pub use self::mmio::*;
|
||||||
|
pub use self::pio::*;
|
||||||
|
|
||||||
|
mod io;
|
||||||
|
mod mmio;
|
||||||
|
mod pio;
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
use x86::io;
|
|
||||||
|
|
||||||
use super::io::Io;
|
use super::io::Io;
|
||||||
|
|
||||||
|
|
@ -27,13 +26,19 @@ impl Io for Pio<u8> {
|
||||||
/// Read
|
/// Read
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read(&self) -> u8 {
|
fn read(&self) -> u8 {
|
||||||
unsafe { io::inb(self.port) }
|
let value: u8;
|
||||||
|
unsafe {
|
||||||
|
asm!("in $0, $1" : "={al}"(value) : "{dx}"(self.port) : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write
|
/// Write
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn write(&mut self, value: u8) {
|
fn write(&mut self, value: u8) {
|
||||||
unsafe { io::outb(self.port, value) }
|
unsafe {
|
||||||
|
asm!("out $1, $0" : : "{al}"(value), "{dx}"(self.port) : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,13 +49,19 @@ impl Io for Pio<u16> {
|
||||||
/// Read
|
/// Read
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read(&self) -> u16 {
|
fn read(&self) -> u16 {
|
||||||
unsafe { io::inw(self.port) }
|
let value: u16;
|
||||||
|
unsafe {
|
||||||
|
asm!("in $0, $1" : "={ax}"(value) : "{dx}"(self.port) : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write
|
/// Write
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn write(&mut self, value: u16) {
|
fn write(&mut self, value: u16) {
|
||||||
unsafe { io::outw(self.port, value) }
|
unsafe {
|
||||||
|
asm!("out $1, $0" : : "{ax}"(value), "{dx}"(self.port) : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,12 +72,18 @@ impl Io for Pio<u32> {
|
||||||
/// Read
|
/// Read
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read(&self) -> u32 {
|
fn read(&self) -> u32 {
|
||||||
unsafe { io::inl(self.port) }
|
let value: u32;
|
||||||
|
unsafe {
|
||||||
|
asm!("in $0, $1" : "={eax}"(value) : "{dx}"(self.port) : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write
|
/// Write
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn write(&mut self, value: u32) {
|
fn write(&mut self, value: u32) {
|
||||||
unsafe { io::outl(self.port, value) }
|
unsafe {
|
||||||
|
asm!("out $1, $0" : : "{eax}"(value), "{dx}"(self.port) : "memory" : "intel", "volatile");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
9
drivers/ps2d/Cargo.toml
Normal file
9
drivers/ps2d/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "ps2d"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bitflags = "*"
|
||||||
|
io = { path = "../io/" }
|
||||||
|
spin = "*"
|
||||||
|
syscall = { path = "../../syscall/" }
|
||||||
|
|
@ -1,17 +1,5 @@
|
||||||
use core::cmp;
|
|
||||||
use spin::Mutex;
|
|
||||||
|
|
||||||
use io::{Io, Pio, ReadOnly, WriteOnly};
|
use io::{Io, Pio, ReadOnly, WriteOnly};
|
||||||
|
|
||||||
pub static PS2_KEYBOARD: Mutex<Option<Ps2Keyboard>> = Mutex::new(None);
|
|
||||||
pub static PS2_MOUSE: Mutex<Option<Ps2Mouse>> = Mutex::new(None);
|
|
||||||
|
|
||||||
pub unsafe fn init() {
|
|
||||||
let (keyboard, mouse) = Ps2::new().init();
|
|
||||||
*PS2_KEYBOARD.lock() = keyboard;
|
|
||||||
*PS2_MOUSE.lock() = mouse;
|
|
||||||
}
|
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
flags StatusFlags: u8 {
|
flags StatusFlags: u8 {
|
||||||
const OUTPUT_FULL = 1,
|
const OUTPUT_FULL = 1,
|
||||||
|
|
@ -85,114 +73,6 @@ enum MouseCommandData {
|
||||||
SetSampleRate = 0xF3,
|
SetSampleRate = 0xF3,
|
||||||
}
|
}
|
||||||
|
|
||||||
bitflags! {
|
|
||||||
flags MousePacketFlags: u8 {
|
|
||||||
const LEFT_BUTTON = 1,
|
|
||||||
const RIGHT_BUTTON = 1 << 1,
|
|
||||||
const MIDDLE_BUTTON = 1 << 2,
|
|
||||||
const ALWAYS_ON = 1 << 3,
|
|
||||||
const X_SIGN = 1 << 4,
|
|
||||||
const Y_SIGN = 1 << 5,
|
|
||||||
const X_OVERFLOW = 1 << 6,
|
|
||||||
const Y_OVERFLOW = 1 << 7
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Ps2Keyboard {
|
|
||||||
data: ReadOnly<Pio<u8>>,
|
|
||||||
key: [u8; 3],
|
|
||||||
key_i: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ps2Keyboard {
|
|
||||||
fn new() -> Self {
|
|
||||||
Ps2Keyboard {
|
|
||||||
data: ReadOnly::new(Pio::new(0x60)),
|
|
||||||
key: [0; 3],
|
|
||||||
key_i: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn on_irq(&mut self) {
|
|
||||||
let scancode = self.data.read();
|
|
||||||
self.key[self.key_i] = scancode;
|
|
||||||
self.key_i += 1;
|
|
||||||
if self.key_i >= self.key.len() || scancode < 0xE0 {
|
|
||||||
println!("KEY: {:X} {:X} {:X}", self.key[0], self.key[1], self.key[2]);
|
|
||||||
|
|
||||||
self.key = [0; 3];
|
|
||||||
self.key_i = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Ps2Mouse {
|
|
||||||
data: ReadOnly<Pio<u8>>,
|
|
||||||
mouse: [u8; 4],
|
|
||||||
mouse_i: usize,
|
|
||||||
mouse_extra: bool,
|
|
||||||
mouse_x: usize,
|
|
||||||
mouse_y: usize
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ps2Mouse {
|
|
||||||
fn new(mouse_extra: bool) -> Self {
|
|
||||||
Ps2Mouse {
|
|
||||||
data: ReadOnly::new(Pio::new(0x60)),
|
|
||||||
mouse: [0; 4],
|
|
||||||
mouse_i: 0,
|
|
||||||
mouse_extra: mouse_extra,
|
|
||||||
mouse_x: 0,
|
|
||||||
mouse_y: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn on_irq(&mut self) {
|
|
||||||
self.mouse[self.mouse_i] = self.data.read();
|
|
||||||
self.mouse_i += 1;
|
|
||||||
|
|
||||||
let flags = MousePacketFlags::from_bits_truncate(self.mouse[0]);
|
|
||||||
if ! flags.contains(ALWAYS_ON) {
|
|
||||||
println!("MOUSE MISALIGN {:X}", self.mouse[0]);
|
|
||||||
|
|
||||||
self.mouse = [0; 4];
|
|
||||||
self.mouse_i = 0;
|
|
||||||
} else if self.mouse_i >= self.mouse.len() || (!self.mouse_extra && self.mouse_i >= 3) {
|
|
||||||
if ! flags.contains(X_OVERFLOW) && ! flags.contains(Y_OVERFLOW) {
|
|
||||||
let mut dx = self.mouse[1] as isize;
|
|
||||||
if flags.contains(X_SIGN) {
|
|
||||||
dx -= 0x100;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut dy = self.mouse[2] as isize;
|
|
||||||
if flags.contains(Y_SIGN) {
|
|
||||||
dy -= 0x100;
|
|
||||||
}
|
|
||||||
|
|
||||||
let _extra = if self.mouse_extra {
|
|
||||||
self.mouse[3]
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
};
|
|
||||||
|
|
||||||
//print!("MOUSE {:?}, {}, {}, {}\n", flags, dx, dy, extra);
|
|
||||||
|
|
||||||
if let Some(ref mut display) = *super::display::DISPLAY.lock() {
|
|
||||||
self.mouse_x = cmp::max(0, cmp::min(display.width as isize - 1, self.mouse_x as isize + dx)) as usize;
|
|
||||||
self.mouse_y = cmp::max(0, cmp::min(display.height as isize - 1, self.mouse_y as isize - dy)) as usize;
|
|
||||||
let offset = self.mouse_y * display.width + self.mouse_x;
|
|
||||||
display.onscreen[offset as usize] = 0xFF0000;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("MOUSE OVERFLOW {:X} {:X} {:X} {:X}", self.mouse[0], self.mouse[1], self.mouse[2], self.mouse[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.mouse = [0; 4];
|
|
||||||
self.mouse_i = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Ps2 {
|
pub struct Ps2 {
|
||||||
data: Pio<u8>,
|
data: Pio<u8>,
|
||||||
status: ReadOnly<Pio<u8>>,
|
status: ReadOnly<Pio<u8>>,
|
||||||
|
|
@ -200,7 +80,7 @@ pub struct Ps2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ps2 {
|
impl Ps2 {
|
||||||
const fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Ps2 {
|
Ps2 {
|
||||||
data: Pio::new(0x60),
|
data: Pio::new(0x60),
|
||||||
status: ReadOnly::new(Pio::new(0x64)),
|
status: ReadOnly::new(Pio::new(0x64)),
|
||||||
|
|
@ -278,7 +158,7 @@ impl Ps2 {
|
||||||
self.read()
|
self.read()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(&mut self) -> (Option<Ps2Keyboard>, Option<Ps2Mouse>) {
|
pub fn init(&mut self) -> bool {
|
||||||
// Disable devices
|
// Disable devices
|
||||||
self.command(Command::DisableFirst);
|
self.command(Command::DisableFirst);
|
||||||
self.command(Command::DisableSecond);
|
self.command(Command::DisableSecond);
|
||||||
|
|
@ -328,18 +208,6 @@ impl Ps2 {
|
||||||
let mouse_id = self.read();
|
let mouse_id = self.read();
|
||||||
let mouse_extra = mouse_id == 3;
|
let mouse_extra = mouse_id == 3;
|
||||||
|
|
||||||
// Enable extra buttons, TODO
|
|
||||||
/*
|
|
||||||
if self.mouse_extra {
|
|
||||||
print!("SAMPLE 200 {:X}\n", self.mouse_command_data(MouseCommandData::SetSampleRate, 200));
|
|
||||||
print!("SAMPLE 200 {:X}\n", self.mouse_command_data(MouseCommandData::SetSampleRate, 200));
|
|
||||||
print!("SAMPLE 80 {:X}\n", self.mouse_command_data(MouseCommandData::SetSampleRate, 80));
|
|
||||||
print!("GET ID {:X}\n", self.mouse_command(MouseCommand::GetDeviceId));
|
|
||||||
let mouse_id = self.read();
|
|
||||||
print!("MOUSE ID: {:X} == 0x04\n", mouse_id);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Set sample rate to maximum
|
// Set sample rate to maximum
|
||||||
assert_eq!(self.mouse_command_data(MouseCommandData::SetSampleRate, 200), 0xFA);
|
assert_eq!(self.mouse_command_data(MouseCommandData::SetSampleRate, 200), 0xFA);
|
||||||
|
|
||||||
|
|
@ -352,6 +220,7 @@ impl Ps2 {
|
||||||
let mut config = self.config();
|
let mut config = self.config();
|
||||||
config.remove(FIRST_DISABLED);
|
config.remove(FIRST_DISABLED);
|
||||||
config.remove(SECOND_DISABLED);
|
config.remove(SECOND_DISABLED);
|
||||||
|
config.insert(FIRST_TRANSLATE);
|
||||||
config.insert(FIRST_INTERRUPT);
|
config.insert(FIRST_INTERRUPT);
|
||||||
config.insert(SECOND_INTERRUPT);
|
config.insert(SECOND_INTERRUPT);
|
||||||
self.set_config(config);
|
self.set_config(config);
|
||||||
|
|
@ -359,6 +228,6 @@ impl Ps2 {
|
||||||
|
|
||||||
self.flush_read();
|
self.flush_read();
|
||||||
|
|
||||||
(Some(Ps2Keyboard::new()), Some(Ps2Mouse::new(mouse_extra)))
|
mouse_extra
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue