From a179a012705cf13bb2ebc92074c32150cfbc3b23 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 28 Oct 2023 16:46:38 +0200 Subject: [PATCH] Simple hello world --- .cargo/config | 6 ++++++ .gitignore | 1 + Cargo.toml | 14 ++++++++++++++ aarch64-gem5.ld | 14 ++++++++++++++ src/main.rs | 32 ++++++++++++++++++++++++++++++++ src/start.s | 15 +++++++++++++++ src/uart.rs | 16 ++++++++++++++++ 7 files changed, 98 insertions(+) create mode 100644 .cargo/config create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 aarch64-gem5.ld create mode 100644 src/main.rs create mode 100644 src/start.s create mode 100644 src/uart.rs diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..84d4758 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,6 @@ +[build] +target = "aarch64-unknown-none" + +rustflags = [ + "-C", "link-arg=-Taarch64-gem5.ld", +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..eeabd12 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "aarch64" +version = "0.1.0" +edition = "2021" + +[dependencies] +aarch64-cpu = "9.4.0" +half = { version = "2.3.1", default-features = false } + +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" diff --git a/aarch64-gem5.ld b/aarch64-gem5.ld new file mode 100644 index 0000000..cf7a578 --- /dev/null +++ b/aarch64-gem5.ld @@ -0,0 +1,14 @@ +ENTRY(_start) +SECTIONS +{ + . = 0x80000000; + .text.boot : { *(.text.boot) } + .text : { *(.text) } + .data : { *(.data) } + .rodata : { *(.rodata) } + .bss : { *(.bss) } + + . = ALIGN(8); + . = . + 0x8000; + LD_STACK_PTR = .; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3595426 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,32 @@ +#![no_std] +#![no_main] + +use crate::uart::Uart0; +use core::sync::atomic::{self, Ordering}; +use core::{arch::global_asm, fmt::Write, panic::PanicInfo}; + +mod uart; + +global_asm!(include_str!("start.s")); + +#[no_mangle] +pub extern "C" fn entry() -> ! { + let mut uart = Uart0 {}; + + for i in 0..3 { + writeln!(&mut uart, "Hello from Rust {i}!").unwrap(); + } + + loop { + atomic::compiler_fence(Ordering::SeqCst); + } +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + writeln!(Uart0, "{info}").unwrap(); + + loop { + atomic::compiler_fence(Ordering::SeqCst); + } +} diff --git a/src/start.s b/src/start.s new file mode 100644 index 0000000..283a554 --- /dev/null +++ b/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 entry + +.equ PSCI_SYSTEM_OFF, 0x84000008 +.globl system_off +system_off: + ldr x0, =PSCI_SYSTEM_OFF + hvc #0 diff --git a/src/uart.rs b/src/uart.rs new file mode 100644 index 0000000..c743f42 --- /dev/null +++ b/src/uart.rs @@ -0,0 +1,16 @@ +use core::{fmt::Write, ptr::write_volatile}; + +const UART0_ADDR: *mut u32 = 0x1c090000 as _; + +pub struct Uart0; + +impl Write for Uart0 { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + for &byte in s.as_bytes() { + unsafe { + write_volatile(UART0_ADDR, byte as _); + } + } + Ok(()) + } +}