From 5443c61db2b65cf67be256249024e55705f8f833 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Thu, 22 Feb 2024 21:31:25 -0600 Subject: [PATCH] initial --- .gitignore | 2 ++ CMakeLists.txt | 13 +++++++++++++ Cargo.lock | 7 +++++++ Cargo.toml | 2 ++ README.md | 3 +++ aarch64-qemu.ld | 14 ++++++++++++++ aarch64-unknown-none.json | 33 +++++++++++++++++++++++++++++++++ kernel/Cargo.toml | 12 ++++++++++++ kernel/src/main.rs | 19 +++++++++++++++++++ kernel/src/panic.rs | 6 ++++++ kernel/src/start.s | 15 +++++++++++++++ run.sh | 5 +++++ rust-toolchain | 1 + 13 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 aarch64-qemu.ld create mode 100644 aarch64-unknown-none.json create mode 100644 kernel/Cargo.toml create mode 100644 kernel/src/main.rs create mode 100644 kernel/src/panic.rs create mode 100644 kernel/src/start.s create mode 100755 run.sh create mode 100644 rust-toolchain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82448cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f6485c4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.25) + +project(EOS0 CXX) + +include(FetchContent) +FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.4 # Optionally specify a commit hash, version tag or branch here +) +FetchContent_MakeAvailable(Corrosion) + +corrosion_import_crate(MANIFEST_PATH kernel/Cargo.toml) diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0dd1539 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "kernel" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7a0a86f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,2 @@ +workspace.members = ["kernel"] +workspace.resolver = "2" diff --git a/README.md b/README.md new file mode 100644 index 0000000..4fd1d1a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +References: + +- https://lowenware.com/blog/aarch64-bare-metal-program-in-rust/ diff --git a/aarch64-qemu.ld b/aarch64-qemu.ld new file mode 100644 index 0000000..e1c876e --- /dev/null +++ b/aarch64-qemu.ld @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000..ca8f942 --- /dev/null +++ b/aarch64-unknown-none.json @@ -0,0 +1,33 @@ +{ + "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": "+v8a,+strict-align,+neon,+fp-armv8", + "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/kernel/Cargo.toml b/kernel/Cargo.toml new file mode 100644 index 0000000..ad17cd9 --- /dev/null +++ b/kernel/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "kernel" +version = "0.1.0" +edition = "2021" + +profile.dev.panic = "abort" +profile.release.panic = "abort" + +# [lib] +# crate-type = ["staticlib"] + +[dependencies] diff --git a/kernel/src/main.rs b/kernel/src/main.rs new file mode 100644 index 0000000..c20feff --- /dev/null +++ b/kernel/src/main.rs @@ -0,0 +1,19 @@ +#![no_std] +#![no_main] + +use core::{arch::global_asm, ptr}; + +mod panic; + +global_asm!(include_str!("start.s")); + +#[no_mangle] +pub extern "C" fn not_main() { + const UART0: *mut u8 = 0x0900_0000 as *mut u8; + let out_str = b"AArch64 Bare Metal"; + for byte in out_str { + unsafe { + ptr::write_volatile(UART0, *byte); + } + } +} diff --git a/kernel/src/panic.rs b/kernel/src/panic.rs new file mode 100644 index 0000000..739f039 --- /dev/null +++ b/kernel/src/panic.rs @@ -0,0 +1,6 @@ +use core::panic::PanicInfo; + +#[panic_handler] +fn on_panic(_info: &PanicInfo) -> ! { + loop {} +} diff --git a/kernel/src/start.s b/kernel/src/start.s new file mode 100644 index 0000000..40680ed --- /dev/null +++ b/kernel/src/start.s @@ -0,0 +1,15 @@ +.globl _start +.extern LD_STACK_PTR + +.section ".text.boot" + +_start: + ldr x30, =LD_STACK_PTR + mov sp, x30 + bl not_main + +.equ PSCI_SYSTEM_OFF, 0x84000008 +.globl system_off +system_off: + ldr x0, =PSCI_SYSTEM_OFF + hvc #0 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..4191a50 --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +exec qemu-system-aarch64 -machine virt \ + -m 1024M \ + -cpu cortex-a53 \ + -nographic \ + -kernel target/aarch64-unknown-none/debug/kernel diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly