From 37d51d2afea4a926ceae9c4af87be4ccbf615338 Mon Sep 17 00:00:00 2001 From: wescande Date: Mon, 12 Feb 2018 17:08:06 +0100 Subject: [PATCH] init kfs_2 --- kernel-rs/Makefile | 1 + kernel-rs/src/arch/x86/boot.asm | 38 +++++++++++++++-------- kernel-rs/src/arch/x86/grub.cfg | 2 +- kernel-rs/src/arch/x86/linker.ld | 4 +++ kernel-rs/src/cpuio.rs | 30 ++++++++++-------- kernel-rs/src/lib.rs | 52 ++++++++++++++++++++++++++------ 6 files changed, 92 insertions(+), 35 deletions(-) diff --git a/kernel-rs/Makefile b/kernel-rs/Makefile index e42f45ee..96353929 100644 --- a/kernel-rs/Makefile +++ b/kernel-rs/Makefile @@ -12,6 +12,7 @@ project := bluesnow arch ?= x86 NASM := nasm -f elf LD := ld -m elf_i386 -n --gc-sections +# QEMU := qemu-system-x86_64 -device isa-debug-exit,iobase=0xf4,iosize=0x04 -gdb tcp::$(PORTG) -enable-kvm -monitor telnet:127.0.0.1:$(PORT),server,nowait QEMU := qemu-system-x86_64 -gdb tcp::$(PORTG) -enable-kvm -monitor telnet:127.0.0.1:$(PORT),server,nowait kernel := build/kernel-$(arch).bin diff --git a/kernel-rs/src/arch/x86/boot.asm b/kernel-rs/src/arch/x86/boot.asm index 8624af56..0dfa76b8 100644 --- a/kernel-rs/src/arch/x86/boot.asm +++ b/kernel-rs/src/arch/x86/boot.asm @@ -6,18 +6,30 @@ bits 32 start: ; print `OK` to screen ; mov dword [0xb8000], 0x2f4b2f4f - mov word [0xb8000], 0x0248 ; H - mov word [0xb8002], 0x0265 ; e - mov word [0xb8004], 0x026c ; l - mov word [0xb8006], 0x026c ; l - mov word [0xb8008], 0x026f ; o - mov word [0xb800a], 0x022c ; , - mov word [0xb800c], 0x0220 ; - mov word [0xb800e], 0x0277 ; w - mov word [0xb8010], 0x026f ; o - mov word [0xb8012], 0x0272 ; r - mov word [0xb8014], 0x026c ; l - mov word [0xb8016], 0x0264 ; d - mov word [0xb8018], 0x0221 ; ! + ; mov word [0xb8000], 0x0248 ; H + ; mov word [0xb8002], 0x0265 ; e + ; mov word [0xb8004], 0x026c ; l + ; mov word [0xb8006], 0x026c ; l + ; mov word [0xb8008], 0x026f ; o + ; mov word [0xb800a], 0x022c ; , + ; mov word [0xb800c], 0x0220 ; + ; mov word [0xb800e], 0x0277 ; w + ; mov word [0xb8010], 0x026f ; o + ; mov word [0xb8012], 0x0272 ; r + ; mov word [0xb8014], 0x026c ; l + ; mov word [0xb8016], 0x0264 ; d + ; mov word [0xb8018], 0x0221 ; ! + lgdt [gdt_info] call kmain hlt + +; WARNING: Do not insert random label/lines between gdt_xxx label +gdt_start: + ;TODO GDT entries, like null, kernel code, kernel data, user code, user data, TSS... +gdt_info: + dw gdt_info - gdt_start - 1 + dq gdt_start + +section .bss + resb 64 +head_stack: diff --git a/kernel-rs/src/arch/x86/grub.cfg b/kernel-rs/src/arch/x86/grub.cfg index 982ff586..4e624c0f 100644 --- a/kernel-rs/src/arch/x86/grub.cfg +++ b/kernel-rs/src/arch/x86/grub.cfg @@ -1,4 +1,4 @@ -set timeout=5 +set timeout=0 set default=0 menuentry "Blue Snow" { diff --git a/kernel-rs/src/arch/x86/linker.ld b/kernel-rs/src/arch/x86/linker.ld index 0e899042..22edafd1 100644 --- a/kernel-rs/src/arch/x86/linker.ld +++ b/kernel-rs/src/arch/x86/linker.ld @@ -12,4 +12,8 @@ SECTIONS { .text : { *(.text) } + + .stack : { + *(.bss) + } } diff --git a/kernel-rs/src/cpuio.rs b/kernel-rs/src/cpuio.rs index cec23ce5..5ce962a2 100644 --- a/kernel-rs/src/cpuio.rs +++ b/kernel-rs/src/cpuio.rs @@ -1,39 +1,45 @@ //! Rust wrappers around the x86-family I/O instructions. /// Read a `u8`-sized value from `port`. -pub unsafe fn inb(port: u16) -> u8 { +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; - asm!("inb %dx, %al" : "={al}"(result) : "{dx}"(port) :: "volatile"); + unsafe {asm!("inb %dx, %al" : "={al}"(result) : "{dx}"(port) :: "volatile")}; result } /// Write a `u8`-sized `value` to `port`. -pub unsafe fn outb(value: u8, port: u16) { - asm!("outb %al, %dx" :: "{dx}"(port), "{al}"(value) :: "volatile"); +pub fn outb(value: u8, port: u16) { + unsafe {asm!("outb %al, %dx" :: "{dx}"(port), "{al}"(value) :: "volatile")}; } /// Read a `u16`-sized value from `port`. -pub unsafe fn inw(port: u16) -> u16 { +pub fn inw(port: u16) -> u16 { let result: u16; - asm!("inw %dx, %ax" : "={ax}"(result) : "{dx}"(port) :: "volatile"); + unsafe {asm!("inw %dx, %ax" : "={ax}"(result) : "{dx}"(port) :: "volatile")}; result } /// Write a `u8`-sized `value` to `port`. -pub unsafe fn outw(value: u16, port: u16) { - asm!("outw %ax, %dx" :: "{dx}"(port), "{ax}"(value) :: "volatile"); +pub fn outw(value: u16, port: u16) { + unsafe {asm!("outw %ax, %dx" :: "{dx}"(port), "{ax}"(value) :: "volatile")}; } /// Read a `u32`-sized value from `port`. -pub unsafe fn inl(port: u16) -> u32 { +pub fn inl(port: u16) -> u32 { let result: u32; - asm!("inl %dx, %eax" : "={eax}"(result) : "{dx}"(port) :: "volatile"); + unsafe {asm!("inl %dx, %eax" : "={eax}"(result) : "{dx}"(port) :: "volatile")}; result } /// Write a `u32`-sized `value` to `port`. -pub unsafe fn outl(value: u32, port: u16) { - asm!("outl %eax, %dx" :: "{dx}"(port), "{eax}"(value) :: "volatile"); +pub fn outl(value: u32, port: u16) { + unsafe {asm!("outl %eax, %dx" :: "{dx}"(port), "{eax}"(value) :: "volatile")}; +} + +pub fn halt() -> ! { + loop { + unsafe {asm!("hlt")}; //TODO volatile ????? + } } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 1f0a7fe5..ce6f1035 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -19,15 +19,40 @@ mod keyboard; #[allow(dead_code)] mod cpuio; -// fn check_shift(key: u8) -> u8 { -// print!("{:b} vs {:b}\n", key as u8, (1<<7) as u8); -// if (key >> 7 & 1) == 1 { -// print!("MATCH"); -// key - (1 << 7) -// } else { -// key -// } -// } + +//TODO implement ACPI to have such functionality +/// Reboot the kernel +/// +/// If reboot failed, will loop on a halt cmd +/// +#[allow(dead_code)] +fn reboot() { + //TODO disable interrupt here something like : asm volatile ("cli"); + + // I will now clear the keyboard buffer + let mut buffer: u8 = 0x02; + while buffer == 0x02 { + buffer = cpuio::inb(0x64); + } + cpuio::outb(0x64, 0xFE);//Send reset value to CPU //TODO doesn't work + println!("Reicv reboot command. System cannot reboot yet, he is now halt\n"); + cpuio::halt(); +} + +/// Shutdown the kernel +/// +/// # Pre-requist: +/// Seems that he have to use following line command : +/// `-device isa-debug-exit,iobase=0xf4,iosize=0x04` +/// +/// If shutdown failed, will loop on a halt cmd +/// +#[allow(dead_code)] +fn shutdown() -> ! { + cpuio::outb(0xf4, 0x00);//TODO doesn't work :( + println!("Reicv shutdown command. System cannot shutdown properly yet, he is now halt\n"); + cpuio::halt(); +} #[no_mangle] pub extern fn kmain() -> ! { // use vga::VgaScreen; @@ -53,6 +78,15 @@ pub extern fn kmain() -> ! { loop { keyboard::kbd_callback(); } + // let control = unsafe { cpuio::inb(0x64) }; + // if (control & 1) == 1 { + // let keycode = unsafe { cpuio::inb(0x60) }; + // match keyboard::KEY_CODE_TO_ASCII.get(keycode as usize) { + // Some(ascii) => print!("{}", *ascii as char), + // None =>{}, + // // None => println!("nokey ctrl {:x}", control), + // } + // } } #[lang = "eh_personality"] #[no_mangle]