diff --git a/debug.sh b/debug.sh new file mode 100644 index 0000000..77154a9 --- /dev/null +++ b/debug.sh @@ -0,0 +1,5 @@ +# Run run.sh before this! +set -euo pipefail +exec rust-lldb \ + -s lldb-init \ + target/aarch64-unknown-none/debug/kernel \ No newline at end of file diff --git a/kernel/src/console.rs b/kernel/src/console.rs index ce345ac..c55c9d1 100644 --- a/kernel/src/console.rs +++ b/kernel/src/console.rs @@ -1,14 +1,18 @@ use core::fmt::{self, Write}; -use crate::uart::uart_init; +use crate::{spinlock::SpinLock, uart::UART}; /// A wrapper around the console -pub struct Console {} +pub struct Console { + lock: SpinLock, + uart: UART, +} impl Console { pub fn init() -> Self { - uart_init(); - Console {} + let lock = SpinLock::init("console"); + let uart = UART::init(); + Console { lock, uart } } } diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 04d937b..e470826 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -14,13 +14,14 @@ mod console; mod io; mod memory_layout; mod panic; +mod spinlock; mod uart; global_asm!(include_str!("start.s")); #[no_mangle] pub extern "C" fn main() { - // let mut console = Console::init(); + let mut console = Console::init(); // let _ = writeln!(console, "eos0 is booting..."); let out_str = b"AArch64 Bare Metal"; diff --git a/kernel/src/spinlock.rs b/kernel/src/spinlock.rs new file mode 100644 index 0000000..39ded9d --- /dev/null +++ b/kernel/src/spinlock.rs @@ -0,0 +1,18 @@ +pub struct SpinLock { + locked: bool, + + /// The name of the lock, for debugging + name: [u8; 32], +} + +impl SpinLock { + pub fn init(name: &'static str) -> Self { + let mut this_name = [0; 32]; + this_name.copy_from_slice(&name.as_bytes()[..32]); + + SpinLock { + locked: false, + name: this_name, + } + } +} diff --git a/kernel/src/uart.rs b/kernel/src/uart.rs index a9d1db4..da4c1de 100644 --- a/kernel/src/uart.rs +++ b/kernel/src/uart.rs @@ -2,28 +2,39 @@ use crate::memory_layout::UART0; -/// http://byterunner.com/16550.html -enum Register {} +/// The UART Interface +pub struct UART {} -impl Register { - fn offset(&self) -> u64 { - todo!() +impl UART { + /// Most of this is just a port from xv6 right now, I have no idea why any of this works + pub fn init() -> Self { + unsafe { + UART::write_register(WriteRegister::IER, 0x00); + } + UART {} } + + #[inline] + pub unsafe fn write_register(reg: WriteRegister, val: u8) { + *reg.as_mut_ptr() = val; + } + + pub unsafe fn put_char(&self) {} } -/// the UART control registers are memory-mapped -/// at address UART0. this macro returns the -/// address of one of the registers -#[inline] -fn register(reg: Register) -> *mut u8 { - // (UART0 + reg.offset()) as *mut u8 - UART0 +/// +pub enum ReadRegister {} + +#[derive(Copy, Clone)] +pub enum WriteRegister { + IER = 1, +} + +impl WriteRegister { + pub fn as_mut_ptr(&self) -> *mut u8 { + unsafe { UART0.offset(*self as isize) } + } } #[inline] fn read_register() {} - -#[inline] -fn write_register() {} - -pub fn uart_init() {} diff --git a/lldb-init b/lldb-init new file mode 100644 index 0000000..b9bbdaa --- /dev/null +++ b/lldb-init @@ -0,0 +1,2 @@ +target create target/aarch64-unknown-none/debug/kernel +gdb-remote 1234 \ No newline at end of file diff --git a/run.sh b/run.sh index 3a62632..c596ac6 100755 --- a/run.sh +++ b/run.sh @@ -1,7 +1,11 @@ set -euo pipefail cargo xbuild --target=aarch64-unknown-none.json + +printf "Running with qemu...\n" +set -x exec qemu-system-aarch64 \ -machine virt \ + -s -S \ -m 1024M \ -cpu cortex-a53 \ -nographic \