feat(serial): add external serial driver
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2021-12-02 22:29:56 +01:00
parent be8f3f2a0f
commit 106e907438
6 changed files with 87 additions and 11 deletions

43
Cargo.lock generated

@ -2,13 +2,27 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "bit_field"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "julios" name = "julios"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"spin", "spin",
"volatile", "uart_16550",
"volatile 0.2.7",
"x86_64",
] ]
[[package]] [[package]]
@ -26,8 +40,35 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "uart_16550"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65ad019480ef5ff8ffe66d6f6a259cd87cf317649481394981db1739d844f374"
dependencies = [
"bitflags",
"x86_64",
]
[[package]] [[package]]
name = "volatile" name = "volatile"
version = "0.2.7" version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945" checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
[[package]]
name = "volatile"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
[[package]]
name = "x86_64"
version = "0.14.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbc6ed1ed2cd4536b083c34041aff7b84448ee25ac4aa5e9d54802ce226f9815"
dependencies = [
"bit_field",
"bitflags",
"volatile 0.4.4",
]

@ -11,6 +11,8 @@ crate-type = ["staticlib"]
[dependencies] [dependencies]
volatile = "0.2.6" volatile = "0.2.6"
spin = "0.5.2" spin = "0.5.2"
x86_64 = "0.14.2"
uart_16550 = "0.2.0"
[dependencies.lazy_static] [dependencies.lazy_static]
version = "1.0" version = "1.0"

@ -12,7 +12,7 @@ GRUB_CFG = grub/grub.cfg
all: $(ISO) all: $(ISO)
run: $(ISO) run: $(ISO)
qemu-system-x86_64 -cdrom $< qemu-system-x86_64 -cdrom $< -serial stdio
debug: $(ISO) debug: $(ISO)
bochs -q bochs -q

@ -2,6 +2,7 @@
#![no_main] #![no_main]
mod vga; mod vga;
mod serial;
use core::panic::PanicInfo; use core::panic::PanicInfo;
use vga::Color; use vga::Color;
@ -12,11 +13,10 @@ fn panic_handler(info: &PanicInfo) -> ! {
loop {} loop {}
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn julios_main() -> ! { pub extern "C" fn julios_main() -> ! {
println!("Hello World!"); println!("Hello World!");
println!("{}", "***JuliOS***"); println!("{}", "***JuliOS***");
panic!("Test panic"); serial_println!("Test serial");
loop {} panic!("Kernel end of flow");
} }

34
src/serial.rs Normal file

@ -0,0 +1,34 @@
use uart_16550::SerialPort;
use spin::Mutex;
use lazy_static::lazy_static;
lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = {
let mut serial_port = unsafe { SerialPort::new(0x3F8) };
serial_port.init();
Mutex::new(serial_port)
};
}
#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
SERIAL1.lock().write_fmt(args).expect("Printing to serial failed");
}
/// Prints to the host through the serial interface.
#[macro_export]
macro_rules! serial_print {
($($arg:tt)*) => {
$crate::serial::_print(format_args!($($arg)*));
};
}
/// Prints to the host through the serial interface, appending a newline.
#[macro_export]
macro_rules! serial_println {
() => ($crate::serial_print!("\n"));
($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
concat!($fmt, "\n"), $($arg)*));
}

@ -1,7 +1,7 @@
use volatile::Volatile;
use core::fmt; use core::fmt;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use spin::Mutex; use spin::Mutex;
use volatile::Volatile;
const BUFFER_HEIGHT: usize = 25; const BUFFER_HEIGHT: usize = 25;
const BUFFER_WIDTH: usize = 80; const BUFFER_WIDTH: usize = 80;
@ -9,7 +9,7 @@ const BUFFER_WIDTH: usize = 80;
lazy_static! { lazy_static! {
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer { pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
column: 0, column: 0,
color_code: ColorCode::new(Color::LightBlue, Color::Black), color_code: ColorCode::new(Color::LightGreen, Color::Black),
buffer: unsafe { &mut *(0xb8000 as *mut VgaBuffer) } buffer: unsafe { &mut *(0xb8000 as *mut VgaBuffer) }
}); });
} }
@ -34,7 +34,6 @@ pub fn change_color(fg: Color, bg: Color) {
WRITER.lock().change_color(ColorCode::new(fg, bg)) WRITER.lock().change_color(ColorCode::new(fg, bg))
} }
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
@ -90,7 +89,7 @@ impl Writer {
for byte in s.bytes() { for byte in s.bytes() {
match byte { match byte {
0x20..=0x7e | b'\n' => self.write_byte(byte), 0x20..=0x7e | b'\n' => self.write_byte(byte),
_ => self.write_byte(0xfe) _ => self.write_byte(0xfe),
} }
} }
} }
@ -109,7 +108,7 @@ impl Writer {
let color_code = self.color_code; let color_code = self.color_code;
self.buffer.chars[row][col].write(VgaChar { self.buffer.chars[row][col].write(VgaChar {
character: byte, character: byte,
color_code color_code,
}); });
self.column += 1; self.column += 1;
} }
@ -130,7 +129,7 @@ impl Writer {
fn clear_row(&mut self, row: usize) { fn clear_row(&mut self, row: usize) {
let blank = VgaChar { let blank = VgaChar {
character: b' ', character: b' ',
color_code: self.color_code color_code: self.color_code,
}; };
for col in 0..BUFFER_WIDTH { for col in 0..BUFFER_WIDTH {
self.buffer.chars[row][col].write(blank); self.buffer.chars[row][col].write(blank);