diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..74fc137 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,9 @@ +[build] +target = "riscv64gc-unknown-none-elf" + +[target.riscv64gc-unknown-none-elf] +rustflags = ["-C", "link-arg=-Tsrc/ld/kernel.ld"] +runner = "./scripts/start.sh" + +[term] +verbose = true diff --git a/.gitignore b/.gitignore index 82448cc..37d52b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,9 @@ /target /build + + +# Added by cargo +# +# already existing elements were commented out + +#/target diff --git a/Cargo.lock b/Cargo.lock index 0dd1539..09719f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "kernel" +name = "eos0" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1820d3f..3045943 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ -workspace.members = ["kernel"] -workspace.resolver = "2" +[package] +name = "eos0" +version = "0.1.0" +edition = "2021" -profile.dev.panic = "abort" -profile.release.panic = "abort" +[dependencies] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..030026e --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +KERNEL = target/riscv64gc-unknown-none-elf/debug/xv6-riscv-rust +SOURCES := $(shell find -name "*.rs") + +$(KERNEL): $(SOURCES) + cargo build + +fs.img: mkfs/mkfs README.md $(UPROGS) + mkfs/mkfs fs.img README.md $(UPROGS) diff --git a/README.md b/README.md deleted file mode 100644 index 445709a..0000000 --- a/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# EOS0 - -This is mainly a port of MIT's xv6 operating system, for educational purposes. - -You can consider the "e" in EOS to be either educational or experimental. - -## References - -- https://lowenware.com/blog/aarch64-bare-metal-program-in-rust/ diff --git a/aarch64-qemu.ld b/aarch64-qemu.ld deleted file mode 100644 index e1c876e..0000000 --- a/aarch64-qemu.ld +++ /dev/null @@ -1,14 +0,0 @@ -ENTRY(_start) -SECTIONS -{ - . = 0x40080000; - .text.boot : { *(.text.boot) } - .text : { *(.text) } - .data : { *(.data) } - .rodata : { *(.rodata) } - .bss : { *(.bss) } - - . = ALIGN(8); - . = . + 0x4000; - LD_STACK_PTR = .; -} diff --git a/aarch64-unknown-none.json b/aarch64-unknown-none.json deleted file mode 100644 index 47445c6..0000000 --- a/aarch64-unknown-none.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "arch": "aarch64", - "crt-objects-fallback": "false", - "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", - "disable-redzone": true, - "features": "-align,-neon", - "linker": "rust-lld", - "linker-flavor": "gnu-lld", - "llvm-target": "aarch64-unknown-none", - "max-atomic-width": 128, - "panic-strategy": "abort", - "pre-link-args": { - "gnu": ["--fix-cortex-a53-843419"], - "gnu-lld": ["--fix-cortex-a53-843419", "-Taarch64-qemu.ld"], - "ld.lld": ["-Taarch64-qemu.ld"] - }, - "relocation-model": "static", - "stack-probes": { - "kind": "inline" - }, - "supported-sanitizers": ["kcfi", "kernel-address"], - "target-pointer-width": "64" -} diff --git a/debug.sh b/debug.sh deleted file mode 100755 index 77154a9..0000000 --- a/debug.sh +++ /dev/null @@ -1,5 +0,0 @@ -# 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/Cargo.toml b/kernel/Cargo.toml deleted file mode 100644 index abbacf8..0000000 --- a/kernel/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "kernel" -version = "0.1.0" -edition = "2021" - -# [lib] -# crate-type = ["staticlib"] - -[dependencies] diff --git a/kernel/src/console.rs b/kernel/src/console.rs deleted file mode 100644 index c55c9d1..0000000 --- a/kernel/src/console.rs +++ /dev/null @@ -1,27 +0,0 @@ -use core::fmt::{self, Write}; - -use crate::{spinlock::SpinLock, uart::UART}; - -/// A wrapper around the console -pub struct Console { - lock: SpinLock, - uart: UART, -} - -impl Console { - pub fn init() -> Self { - let lock = SpinLock::init("console"); - let uart = UART::init(); - Console { lock, uart } - } -} - -impl Write for Console { - fn write_str(&mut self, s: &str) -> fmt::Result { - // TODO: Lock - - Ok(()) - - // TODO: Release lock - } -} diff --git a/kernel/src/io.rs b/kernel/src/io.rs deleted file mode 100644 index 8b13789..0000000 --- a/kernel/src/io.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/kernel/src/macros.rs b/kernel/src/macros.rs deleted file mode 100644 index 8b13789..0000000 --- a/kernel/src/macros.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/kernel/src/main.rs b/kernel/src/main.rs deleted file mode 100644 index 8902626..0000000 --- a/kernel/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![no_std] -#![no_main] - -use core::fmt::Write; -use core::{arch::global_asm, ptr}; - -use crate::console::Console; -use crate::memory_layout::UART0; - -#[macro_use] -mod macros; - -mod console; -mod io; -mod memory_layout; -mod panic; -mod spinlock; -mod uart; - -// global_asm!(include_str!("start.s")); -global_asm!(include_str!("riscv64gc/start.s")); - -#[no_mangle] -pub extern "C" fn main() { - let mut console = Console::init(); - // let _ = writeln!(console, "eos0 is booting..."); - - let out_str = b"AArch64 Bare Metal"; - for byte in out_str { - unsafe { - ptr::write_volatile(UART0, *byte); - } - } -} diff --git a/kernel/src/memory_layout.rs b/kernel/src/memory_layout.rs deleted file mode 100644 index 947a0e1..0000000 --- a/kernel/src/memory_layout.rs +++ /dev/null @@ -1 +0,0 @@ -pub const UART0: *mut u8 = 0x0900_0000 as *mut u8; diff --git a/kernel/src/panic.rs b/kernel/src/panic.rs deleted file mode 100644 index 7289d41..0000000 --- a/kernel/src/panic.rs +++ /dev/null @@ -1,7 +0,0 @@ -use core::panic::PanicInfo; - -#[cfg(not(test))] -#[panic_handler] -fn on_panic(_info: &PanicInfo) -> ! { - loop {} -} diff --git a/kernel/src/riscv64gc/start.s b/kernel/src/riscv64gc/start.s deleted file mode 100644 index bf0ce40..0000000 --- a/kernel/src/riscv64gc/start.s +++ /dev/null @@ -1,15 +0,0 @@ -.section .text - -.global _entry - -_entry: - la sp, stack0 - li a0, 1024*4 - csrr a1, mhartid - addi a1, a1, 1 - mul a0, a0, a1 - add sp, sp, a0 - call main - -spin: - j spin \ No newline at end of file diff --git a/kernel/src/spinlock.rs b/kernel/src/spinlock.rs deleted file mode 100644 index 97a71b5..0000000 --- a/kernel/src/spinlock.rs +++ /dev/null @@ -1,18 +0,0 @@ -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.min(name.len())]); - - SpinLock { - locked: false, - name: this_name, - } - } -} diff --git a/kernel/src/start.s b/kernel/src/start.s deleted file mode 100644 index 88f0196..0000000 --- a/kernel/src/start.s +++ /dev/null @@ -1,15 +0,0 @@ -.globl _start -.extern LD_STACK_PTR - -.section ".text.boot" - -_start: - ldr x30, =LD_STACK_PTR - mov sp, x30 - bl main - -.equ PSCI_SYSTEM_OFF, 0x84000008 -.globl system_off -system_off: - ldr x0, =PSCI_SYSTEM_OFF - hvc #0 diff --git a/kernel/src/uart.rs b/kernel/src/uart.rs deleted file mode 100644 index da4c1de..0000000 --- a/kernel/src/uart.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! UART (Universal Asynchronous Receiver-Transmitter) - -use crate::memory_layout::UART0; - -/// The UART Interface -pub struct UART {} - -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) {} -} - -/// -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() {} diff --git a/lldb-init b/lldb-init deleted file mode 100644 index b9bbdaa..0000000 --- a/lldb-init +++ /dev/null @@ -1,2 +0,0 @@ -target create target/aarch64-unknown-none/debug/kernel -gdb-remote 1234 \ No newline at end of file diff --git a/riscv64gc-unknown-none-elf.json b/riscv64gc-unknown-none-elf.json deleted file mode 100644 index b70a5cb..0000000 --- a/riscv64gc-unknown-none-elf.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "arch": "riscv64", - "code-model": "medium", - "cpu": "generic-rv64", - "crt-objects-fallback": "false", - "data-layout": "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128", - "eh-frame-header": false, - "emit-debug-gdb-scripts": true, - "features": "+m,+a,+f,+d,+c", - "linker": "rust-lld", - "linker-flavor": "gnu-lld", - "llvm-abiname": "lp64d", - "llvm-target": "riscv64", - "max-atomic-width": 64, - "panic-strategy": "abort", - "relocation-model": "static", - "pre-link-args": { - "gnu-lld": ["-Taarch64-qemu.ld"] - }, - "supported-sanitizers": ["kernel-address"], - "target-pointer-width": "64" -} diff --git a/run.sh b/run.sh deleted file mode 100755 index 0f4730c..0000000 --- a/run.sh +++ /dev/null @@ -1,16 +0,0 @@ -set -euo pipefail -cargo xbuild --target=aarch64-unknown-none.json - -DEBUG= -DEBUG="-s -S -d exec" - -printf "Running with qemu...\n" -set -x -exec qemu-system-aarch64 \ - -machine virt \ - $DEBUG \ - -m 1024M \ - -cpu cortex-a53 \ - -no-reboot \ - -nographic \ - -kernel target/aarch64-unknown-none/debug/kernel diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index bf867e0..0000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 205c72c..0000000 --- a/rustfmt.toml +++ /dev/null @@ -1,2 +0,0 @@ -tab_spaces = 2 -max_width = 80 diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100755 index 0000000..d4adc4f --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +KERNEL=$1 +shift + +qemu-system-riscv64 \ + -nographic \ + -machine virt \ + -bios none \ + -m 3G \ + -smp 3 \ + # -drive file=fs.img,if=none,format=raw,id=x0 \ + -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 \ + -kernel "$KERNEL" diff --git a/src/ld/kernel.ld b/src/ld/kernel.ld new file mode 100644 index 0000000..9207f40 --- /dev/null +++ b/src/ld/kernel.ld @@ -0,0 +1,12 @@ +OUTPUT_ARCH("riscv") +ENTRY(_entry) + +SECTIONS { + /* + It seems that QEMU jumps to 0x8000000 after setting up memory, so we want + our kernel code to begin here. + + TODO: Is there an authoritative source that says that this is the address? + */ + . = 0x8000000; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0587db5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,25 @@ +#![no_std] +#![no_main] + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic_handler(_: &PanicInfo) -> ! { + loop {} +} + +static HELLO: &[u8] = b"Hello World!"; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + let vga_buffer = 0xb8000 as *mut u8; + + for (i, &byte) in HELLO.iter().enumerate() { + unsafe { + *vga_buffer.offset(i as isize * 2) = byte; + *vga_buffer.offset(i as isize * 2 + 1) = 0xb; + } + } + + loop {} +} diff --git a/xv6-book.pdf b/xv6-book.pdf new file mode 100644 index 0000000..f26dd3f Binary files /dev/null and b/xv6-book.pdf differ