From cdea287ecd8ec1cc5ec520d370f26ab734f9711f Mon Sep 17 00:00:00 2001 From: wescande Date: Mon, 19 Mar 2018 11:23:53 +0100 Subject: [PATCH] acpi can run in kfs-3 --- kernel-rs/Makefile | 8 ++++---- kernel-rs/src/acpi/dsdt.rs | 1 + kernel-rs/src/acpi/fadt.rs | 19 +++++++++++-------- kernel-rs/src/acpi/rsdp.rs | 10 ++++++---- kernel-rs/src/arch/x86/boot.asm | 4 ++-- kernel-rs/src/lib.rs | 6 ++++++ 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/kernel-rs/Makefile b/kernel-rs/Makefile index a26b3e36..6e832f9d 100644 --- a/kernel-rs/Makefile +++ b/kernel-rs/Makefile @@ -31,13 +31,13 @@ asm_object := $(patsubst src/arch/$(arch)/%.asm, build/arch/$(arch)/%.o, $(asm_ KERNEL_RUN := $(QEMU) -curses -cdrom $(iso) MONITOR := sleep 0.5;\ telnet 127.0.0.1 $(PORT);\ - kill \`ps -x | grep \"[g]db\" | cut -d \ -f 1 \`;\ - kill \`ps -x | grep \"[g]db\" | cut -d \ -f 2 \` + kill \`ps -x | grep \"[g]db -q\" | cut -d \ -f 1 \`;\ + kill \`ps -x | grep \"[g]db -q\" | cut -d \ -f 2 \` GDB := gdb -q\ -ex \"set arch i386:x86-64\"\ -ex \"file $(kernel)\"\ - -ex \"target remote :$(PORTG)\" - # -ex \"continue\" + -ex \"target remote :$(PORTG)\"\ + -ex \"continue\" all: $(kernel) diff --git a/kernel-rs/src/acpi/dsdt.rs b/kernel-rs/src/acpi/dsdt.rs index 66c187b0..aa3456e2 100644 --- a/kernel-rs/src/acpi/dsdt.rs +++ b/kernel-rs/src/acpi/dsdt.rs @@ -50,6 +50,7 @@ impl DSDT { self.slp_typ_b = (unsafe {*(ptr as *const u8)} as u16) << 10; } } + fn is_init() -> Result <(), &'static str> { match unsafe {DSDT.valid} { true => Ok(()), diff --git a/kernel-rs/src/acpi/fadt.rs b/kernel-rs/src/acpi/fadt.rs index c06d0a16..ed5da011 100644 --- a/kernel-rs/src/acpi/fadt.rs +++ b/kernel-rs/src/acpi/fadt.rs @@ -2,7 +2,7 @@ use super::{ACPISDTHeader,ACPISDTIter}; use cpuio; #[repr(C)] -#[derive(Debug)] +#[derive(Debug, Clone)] struct GenericAddressStructure { addressspace: u8, bitwidth: u8, @@ -13,7 +13,7 @@ struct GenericAddressStructure { } #[repr(C)] -#[derive(Debug)] +#[derive(Debug, Clone)] struct FADT { header: ACPISDTHeader, @@ -83,15 +83,15 @@ struct FADT } -static mut FADT: Option<&'static FADT> = None; +static mut FADT: Option = None; /// ## Initialize Fixed ACPI Description Table (FADT) /// input param addr is contain in other ptr of rsdt pub fn init(sdt_iter: ACPISDTIter) -> Result <(), &'static str> { for sdt_ptr in sdt_iter { if ACPISDTHeader::valid(sdt_ptr, "FACP") { // Where is "FADT"? Shut up is magic - let ref fadt_tmp: &FADT = unsafe{ &(*(sdt_ptr as *const FADT)) }; - unsafe {FADT = Some(fadt_tmp)}; + let fadt_tmp: FADT = unsafe{ (*(sdt_ptr as *const FADT)).clone() }; + unsafe {FADT = Some(fadt_tmp.clone())}; if !is_enable()? { // 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; @@ -103,8 +103,8 @@ pub fn init(sdt_iter: ACPISDTIter) -> Result <(), &'static str> { return Err("Can not find Fixed ACPI Description Table (FADT)."); } -fn is_init() -> Result <&'static FADT, &'static str> { - match unsafe {FADT} { +fn is_init() -> Result { + match unsafe {FADT.clone()} { Some(fadt) => Ok(fadt), None => Err("Fixed ACPI Description Table (FADT) is not initialized") } @@ -117,7 +117,7 @@ pub fn dsdtaddr() -> Result { return Ok(fadt.dsdt); } -fn get_cnt(fadt: &'static FADT) -> [u16; 2] { +fn get_cnt(fadt: FADT) -> [u16; 2] { [fadt.pm1acontrolblock as u16, fadt.pm1bcontrolblock as u16] // TODO WHY DO I NEED THIS FUCKING CAST } @@ -138,6 +138,9 @@ 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 } } diff --git a/kernel-rs/src/acpi/rsdp.rs b/kernel-rs/src/acpi/rsdp.rs index 85e72418..5d1e2dc8 100644 --- a/kernel-rs/src/acpi/rsdp.rs +++ b/kernel-rs/src/acpi/rsdp.rs @@ -2,6 +2,7 @@ use super::{check_signature,check_checksum}; use core::mem; #[repr(C)] +#[derive(Clone)] struct RSDP { signature: [u8; 8], checksum: u8, @@ -11,6 +12,7 @@ struct RSDP { } #[repr(C)] +#[derive(Clone)] pub struct RSDP20 { rsdp: RSDP, length: u32, @@ -19,7 +21,7 @@ pub struct RSDP20 { reserved: [u8; 3], } -static mut RSDPTR: Option<&'static RSDP20> = None; +static mut RSDPTR: Option = None; /// RSDP load will check is RSDP is present at the addr sent. /// Return a bool @@ -27,7 +29,7 @@ static mut RSDPTR: Option<&'static RSDP20> = None; /// false => RSDP is V1 pub fn load(addr: u32) -> Result { if check_signature(addr, "RSD PTR ") { - let ref rsdp_tmp: &RSDP20 = unsafe{ &(*(addr as *const RSDP20)) }; + let rsdp_tmp: RSDP20 = unsafe{ (*(addr as *const RSDP20)).clone() }; let revision = rsdp_tmp.rsdp.revision; if (revision == 0 && check_checksum(addr, mem::size_of::())) || (revision == 2 && check_checksum(addr, mem::size_of::())) { unsafe {RSDPTR = Some(rsdp_tmp)}; @@ -53,8 +55,8 @@ fn memory_finding(bound: u32) -> Result { Err("Can not find Root System Description Pointer (RSDP).") } -fn is_init() -> Result <&'static RSDP20, &'static str> { - match unsafe {RSDPTR} { +fn is_init() -> Result { + match unsafe {RSDPTR.clone()} { Some(rsdptr) => Ok(rsdptr), None => Err("Root System Description Pointer (RSDP) is not initialized") } diff --git a/kernel-rs/src/arch/x86/boot.asm b/kernel-rs/src/arch/x86/boot.asm index 0a05aa51..8e6f56da 100644 --- a/kernel-rs/src/arch/x86/boot.asm +++ b/kernel-rs/src/arch/x86/boot.asm @@ -48,6 +48,8 @@ set_up_page_tables: cmp ecx, 20 ; if counter == 1023, the whole P2 table is mapped jne .map_p2_table ; else map the next entry + mov eax, p2_table + mov cr3, eax ret ; PSE (Page Size Extension) allows huge pages to exist @@ -61,8 +63,6 @@ enable_pse: enable_paging: ; load P2 to cr3 register (cpu uses this to access the P2 table) - mov eax, p2_table - mov cr3, eax ; enable paging in the cr0 register mov eax, cr0 diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 5ebdd124..69020f85 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -32,7 +32,9 @@ pub mod x86; #[no_mangle] pub extern fn kmain(multiboot_info_addr: usize) -> ! { acpi::init().unwrap(); + let boot_info = unsafe { multiboot2::load(multiboot_info_addr) }; + enable_paging(); enable_write_protect_bit(); memory::init(&boot_info); @@ -53,6 +55,10 @@ pub extern fn kmain(multiboot_info_addr: usize) -> ! { loop { keyboard::kbd_callback(); } } +fn enable_paging() { + unsafe { x86::cr0_write(x86::cr0() | (1 << 31)) }; +} + fn enable_write_protect_bit() { unsafe { x86::cr0_write(x86::cr0() | (1 << 16)) }; }