From d15fa664f2dd4af2c0cbe262da920e5135f8e0ea Mon Sep 17 00:00:00 2001 From: wescande Date: Sun, 15 Apr 2018 11:31:40 +0200 Subject: [PATCH] No more cpuio, juste pio in io module. (change in acpi for that => need refactor) --- kernel-rs/src/acpi/dsdt.rs | 9 ++++-- kernel-rs/src/acpi/fadt.rs | 30 ++++++++++++++----- kernel-rs/src/acpi/mod.rs | 12 +++++++- kernel-rs/src/console.rs | 24 ++++++++++----- kernel-rs/src/cpuio.rs | 61 -------------------------------------- kernel-rs/src/io/mod.rs | 11 +++++++ kernel-rs/src/keyboard.rs | 6 ++-- kernel-rs/src/lib.rs | 3 +- 8 files changed, 70 insertions(+), 86 deletions(-) delete mode 100644 kernel-rs/src/cpuio.rs diff --git a/kernel-rs/src/acpi/dsdt.rs b/kernel-rs/src/acpi/dsdt.rs index 32309cf1..e20cd3d8 100644 --- a/kernel-rs/src/acpi/dsdt.rs +++ b/kernel-rs/src/acpi/dsdt.rs @@ -1,6 +1,6 @@ use super::{check_signature, ACPISDTHeader}; use core::mem; -use cpuio; +use io::{Pio,Io}; static mut DSDT: DSDT = DSDT { valid: false, @@ -86,10 +86,13 @@ pub fn init(addr: u32) -> Result<(), &'static str> { pub fn shutdown(pm1_cnt: [u16; 2]) -> Result<(), &'static str> { is_init()?; let slp_typ = unsafe { DSDT.slp_typ_a } | (1 << 13); - cpuio::outw(pm1_cnt[0], slp_typ); + let mut pin: Pio = Pio::new(pm1_cnt[0]); + pin.write(slp_typ); if pm1_cnt[1] != 0 { let slp_typ = unsafe { DSDT.slp_typ_b } | (1 << 13); - cpuio::outw(pm1_cnt[1], slp_typ); + let mut pin: Pio = Pio::new(pm1_cnt[1]); + pin.write(slp_typ); + // cpuio::outw(pm1_cnt[1], slp_typ); } Ok(()) } diff --git a/kernel-rs/src/acpi/fadt.rs b/kernel-rs/src/acpi/fadt.rs index edbdcbd2..94105bcb 100644 --- a/kernel-rs/src/acpi/fadt.rs +++ b/kernel-rs/src/acpi/fadt.rs @@ -1,5 +1,5 @@ use super::{ACPISDTHeader, ACPISDTIter}; -use cpuio; +use io::{Io,Pio}; #[repr(C)] #[derive(Debug, Clone)] @@ -95,7 +95,10 @@ pub fn init(sdt_iter: ACPISDTIter) -> Result<(), &'static str> { // TODO do i have to check if enabled before init ??? let smi_cmd = fadt_tmp.smi_commandport as u16; // TODO WHY DO I NEED THIS FUCKING CAST let acpi_enable = fadt_tmp.acpi_enable; - cpuio::outb(smi_cmd, acpi_enable); // send acpi enable command + //TODO not sexy it ! + let mut pin: Pio = Pio::new(smi_cmd); + pin.write(acpi_enable); + // cpuio::outb(smi_cmd, acpi_enable); // send acpi enable command } return Ok(()); } @@ -125,10 +128,14 @@ fn get_cnt(fadt: FADT) -> [u16; 2] { pub fn is_enable() -> Result { let fadt = is_init()?; let pm1_cnt = get_cnt(fadt); + let pin: Pio = Pio::new(pm1_cnt[0]); if pm1_cnt[1] == 0 { - Ok(cpuio::inw(pm1_cnt[0]) & 0x1 == 0x1) + Ok(pin.read() & 0x1 == 0x1) + // Ok(cpuio::inw(pm1_cnt[0]) & 0x1 == 0x1) } else { - Ok(cpuio::inw(pm1_cnt[0]) & 0x1 == 0x1 || cpuio::inw(pm1_cnt[1]) & 0x1 == 0x1) + let pin2: Pio = Pio::new(pm1_cnt[1]); + Ok(pin.read() & 0x1 == 0x1 || pin2.read() & 0x1 == 0x1) + // Ok(cpuio::inw(pm1_cnt[0]) & 0x1 == 0x1 || cpuio::inw(pm1_cnt[1]) & 0x1 == 0x1) } } @@ -138,9 +145,18 @@ pub fn get_controlblock() -> Result<[u16; 2], &'static str> { if !is_enable()? { Err("ACPI is not enabled") } else { - // println!("HALT"); - // flush!(); - // cpuio::halt(); Ok(get_cnt(is_init()?)) // TODO redondant call to is_init } } +pub fn reboot() -> Result<(), &'static str> { + if !is_enable()? { + Err("ACPI is not enabled") + } else { + let fadt = is_init()?; + println!("fadt on {} ({}), value is {}", fadt.resetreg.address as u32, fadt.resetreg.address as u16, fadt.resetvalue); + let mut pin: Pio = Pio::new(fadt.resetreg.address as u16); + pin.write(fadt.resetvalue); + // cpuio::outb(fadt.resetreg.address as u16, fadt.resetvalue); //TODO do it work + Ok(()) + } +} diff --git a/kernel-rs/src/acpi/mod.rs b/kernel-rs/src/acpi/mod.rs index 3b0544a8..b1ab85ca 100644 --- a/kernel-rs/src/acpi/mod.rs +++ b/kernel-rs/src/acpi/mod.rs @@ -6,7 +6,6 @@ mod dsdt; use core; use core::mem; -// use cpuio; #[repr(C)] #[derive(Debug, Clone, Copy)] @@ -158,6 +157,17 @@ pub fn shutdown() -> Result<(), &'static str> { dsdt::shutdown(fadt::get_controlblock()?) } +/// Proceed to ACPI reboot +/// This function need ACPI in v2 +pub fn reboot() -> Result<(), &'static str> { + is_init()?; + if unsafe {ACPI.v2} { + fadt::reboot() + } else { + Err("ACPI reboot only available in ACPI v2+") + } +} + /// Display state of ACPI pub fn info() -> Result<(), &'static str> { is_init()?; diff --git a/kernel-rs/src/console.rs b/kernel-rs/src/console.rs index 3d5ca557..f44b444e 100644 --- a/kernel-rs/src/console.rs +++ b/kernel-rs/src/console.rs @@ -3,7 +3,7 @@ extern crate core; use acpi; use keyboard::PS2; -use cpuio; +use io; use core::char; use vga::*; @@ -60,11 +60,15 @@ fn help() -> Result<(), &'static str> { /// If reboot failed, will loop on a halt cmd /// fn reboot() -> ! { - // acpi::reboot()?; - // println!("Unable to perform ACPI reboot."); + match acpi::reboot() { + Err(msg) => println!("{}", msg), + _ => println!("Unable to perform ACPI reboot."), + } + flush!(); unsafe {PS2.ps2_8042_reset()};// TODO unsafe println!("Unable to perform 8042 reboot. Kernel will be halted"); - cpuio::halt(); + flush!(); + io::halt(); } /// Shutdown the kernel @@ -72,10 +76,13 @@ fn reboot() -> ! { /// If shutdown is performed but failed, will loop on a halt cmd /// If shutdown cannot be called, return a Err(&str) /// -fn shutdown() -> Result<(), &'static str> { - acpi::shutdown()?; - println!("Unable to perform ACPI shutdown. Kernel will be halted"); - cpuio::halt(); +fn shutdown() -> ! { + match acpi::shutdown() { + Err(msg) => println!("{}", msg), + _ => println!("Unable to perform ACPI shutdown. Kernel will be halted"), + } + flush!(); + io::halt(); } fn hexdump(start: usize, end: usize) { @@ -121,6 +128,7 @@ pub fn print_stack() -> Result<(), &'static str> { println!("ebp = {:#x}", ebp); println!("size = {:#X} bytes", ebp - esp); hexdump(esp, ebp); + flush!(); Ok(()) } diff --git a/kernel-rs/src/cpuio.rs b/kernel-rs/src/cpuio.rs deleted file mode 100644 index 50793228..00000000 --- a/kernel-rs/src/cpuio.rs +++ /dev/null @@ -1,61 +0,0 @@ -#![allow(dead_code)] - -/// Read a `u8`-sized value from `port`. -pub fn inb(port: u16) -> u8 { - // The registers for the `in` and `out` instructions are always the - // same: `a` for value, and `d` for the port address. - let result: u8; - unsafe { asm!("inb %dx, %al" : "={al}"(result) : "{dx}"(port) :: "volatile") }; - result -} - -/// Write a `u8`-sized `value` to `port`. -pub fn outb(port: u16, value: u8) { - unsafe { asm!("outb %al, %dx" :: "{dx}"(port), "{al}"(value) :: "volatile") }; -} - -/// Read a `u16`-sized value from `port`. -pub fn inw(port: u16) -> u16 { - let result: u16; - unsafe { asm!("inw %dx, %ax" : "={ax}"(result) : "{dx}"(port) :: "volatile") }; - result -} - -/// Write a `u8`-sized `value` to `port`. -pub fn outw(port: u16, value: u16) { - unsafe { asm!("outw %ax, %dx" :: "{dx}"(port), "{ax}"(value) :: "volatile") }; -} - -/// Read a `u32`-sized value from `port`. -pub fn inl(port: u16) -> u32 { - let result: u32; - unsafe { asm!("inl %dx, %eax" : "={eax}"(result) : "{dx}"(port) :: "volatile") }; - result -} - -/// Write a `u32`-sized `value` to `port`. -pub fn outl(port: u16, value: u32) { - unsafe { asm!("outl %eax, %dx" :: "{dx}"(port), "{eax}"(value) :: "volatile") }; -} - -/// Disable interruption -pub fn cli() { - unsafe { asm!("cli" : : : : "volatile") }; -} - -/// Halt system -pub fn halt() -> ! { - cli(); - loop { - unsafe { asm!("hlt" : : : : "volatile") }; - } -} - -/// wait for an io operation to complete -pub fn io_wait() { - unsafe { - asm!("jmp 1f\n\t - 1:jmp 2f\n\t - 2:" : : : : "volatile") - } -} diff --git a/kernel-rs/src/io/mod.rs b/kernel-rs/src/io/mod.rs index 0382a50a..8c4c4543 100644 --- a/kernel-rs/src/io/mod.rs +++ b/kernel-rs/src/io/mod.rs @@ -28,3 +28,14 @@ pub trait Io { self.write(tmp); } } + +pub fn cli() { + unsafe { asm!("cli" : : : : "volatile") }; +} + +pub fn halt() -> ! { + cli(); + loop { + unsafe { asm!("hlt" : : : : "volatile") }; + } +} diff --git a/kernel-rs/src/keyboard.rs b/kernel-rs/src/keyboard.rs index 64daa163..8151114b 100644 --- a/kernel-rs/src/keyboard.rs +++ b/kernel-rs/src/keyboard.rs @@ -1,9 +1,7 @@ extern crate core; -use cpuio; use vga; -use io::Pio; -use io::Io; +use io::{self,Pio,Io}; const MAX_KEYS: usize = 59; const KEYMAP_US: [[u8; 2]; MAX_KEYS] = [ @@ -93,7 +91,7 @@ impl Ps2 { } pub fn ps2_8042_reset(&mut self) { - cpuio::cli(); + io::cli(); self.clear_buffer(); self.status.write(0xFE); } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 04c00217..20f23d2a 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -33,8 +33,7 @@ pub mod vga; pub mod keyboard; /// simplisitc kernel commands pub mod console; -/// rust wrappers around cpu I/O instructions., cpuio.rs needs to go in favour of io module -pub mod cpuio; +/// rust wrappers around cpu I/O instructions. pub mod io; /// ACPI self contained module pub mod acpi;