Add IO and Serial

This commit is contained in:
apio 2022-10-31 17:32:33 +01:00
parent d49823d5d0
commit a08a53c7f0
9 changed files with 109 additions and 2 deletions

9
moon/Cargo.lock generated
View File

@ -5,3 +5,12 @@ version = 3
[[package]]
name = "moon"
version = "0.1.0"
dependencies = [
"spin",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"

View File

@ -16,3 +16,4 @@ strip = "debuginfo"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
spin = "0.5.2"

34
moon/src/arch/io.rs Normal file
View File

@ -0,0 +1,34 @@
#[cfg(target_arch = "x86_64")]
pub use super::x86_64::io::*;
pub mod serial {
#[cfg(target_arch = "x86_64")]
pub use super::super::x86_64::io::*;
pub fn write(s: &str) -> () {
for byte in s.bytes() {
match byte {
0x20..=0x7e | b'\n' => serial::putchar(byte),
_ => serial::putchar(0xfe),
}
}
}
}
use core::fmt;
pub struct Serial
{
}
impl fmt::Write for Serial {
fn write_str(&mut self, s: &str) -> fmt::Result {
serial::write(s);
Ok(())
}
}
use spin::Mutex;
pub static SERIAL: Mutex<Serial> = Mutex::new(Serial {});

View File

@ -1,4 +1,5 @@
pub mod cpu;
pub mod io;
#[cfg(target_arch = "x86_64")]
mod x86_64;

View File

@ -0,0 +1,35 @@
#![cfg(target_arch = "x86_64")]
#![allow(dead_code)]
pub const COM1: u16 = 0x3f8;
use core::arch::asm;
pub unsafe fn outportb(port: u16, value: u8) -> () {
asm!("out dx, al", in("al") value, in("dx") port);
}
pub unsafe fn inportb(port: u16) -> u8 {
let value: u8;
asm!("in al, dx", out("al") value, in("dx") port);
return value;
}
pub unsafe fn io_delay() -> () {
outportb(0x80,0);
}
pub mod serial {
use super::*;
unsafe fn wait() -> () {
while (inportb(COM1 + 5) & 0x20) == 0 {
asm!("pause");
}
}
pub fn putchar(c: u8) -> () {
unsafe { wait(); }
unsafe { outportb(COM1, c); }
}
}

View File

@ -1 +1,3 @@
pub mod cpu;
#![cfg(target_arch = "x86_64")]
pub mod cpu;
pub mod io;

19
moon/src/log.rs Normal file
View File

@ -0,0 +1,19 @@
use crate::arch::io::SERIAL;
use core::fmt;
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::log::_print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
SERIAL.lock().write_fmt(args).unwrap();
}

View File

@ -9,6 +9,7 @@ mod panic;
mod util;
mod init;
mod arch;
mod log;
use arch::cpu;
@ -21,6 +22,8 @@ pub extern "C" fn _start() -> ! {
init::check_magic();
video::clear(Color::White);
println!("Hello, world!");
cpu::halt();

View File

@ -1,6 +1,9 @@
use core::panic::PanicInfo;
use crate::println;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! {
println!("Kernel {}", info);
loop {}
}