From 9516a0c7d3411bd2f71664bbf668169cd8a01cce Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Thu, 5 Dec 2024 17:50:22 -0600 Subject: [PATCH] prints hello world successfully --- Cargo.toml | 3 +++ scripts/start.sh | 2 +- src/asm/entry.S | 15 +++++++++++++++ src/ld/kernel.ld | 18 +++++++++++++++--- src/main.rs | 19 ++++++++++--------- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 350045b..4b648f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,7 @@ edition = "2021" [profile.dev] panic = "abort" +[profile.release] +panic = "abort" + [dependencies] diff --git a/scripts/start.sh b/scripts/start.sh index c9d7e36..d6bd8d8 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -10,8 +10,8 @@ qemu-system-riscv64 \ -machine virt \ -bios none \ -m 3G \ - -d trace:exec_tb \ -kernel "$KERNEL" + # -d trace:exec_tb \ # -smp 3 \ # -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 \ diff --git a/src/asm/entry.S b/src/asm/entry.S index ea3b692..d35b39a 100644 --- a/src/asm/entry.S +++ b/src/asm/entry.S @@ -1,3 +1,18 @@ +.attribute arch, "rv64gc" + +.section .text.start .global _entry _entry: + la sp, stack0 + li a0, 1024*8 + csrr a1, mhartid + addi a1, a1, 1 + mul a0, a0, a1 + add sp, sp, a0 + call start + +.section .data +.align 4 +stack0: + .space 8192 * 8 # 8 is NCPU in param.rs diff --git a/src/ld/kernel.ld b/src/ld/kernel.ld index 03d4625..cf7eeef 100644 --- a/src/ld/kernel.ld +++ b/src/ld/kernel.ld @@ -6,12 +6,24 @@ SECTIONS { It seems that QEMU jumps to 0x80000000 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? - How to test this: Run QEMU using -d trace:exec_tb and see where the PC goes + How to find this: Run QEMU using -d trace:exec_tb and see where the PC goes */ . = 0x80000000; .text : { - *(.text) + *(.text.start) + *(.text .text.*) + } + + /* + Important: if this section was not included, the linker would put .rodata before .text + If the linker puts ANYTHING before .text, include it explicitly in the linker script AFTER + */ + .rodata : { + *(.rodata .rodata.*) + } + + .data : { + *(.data .data.*) } } diff --git a/src/main.rs b/src/main.rs index 5559c4f..24b1100 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,24 @@ #![no_std] #![no_main] -use core::panic::PanicInfo; +use core::{arch::global_asm, panic::PanicInfo, ptr}; + +global_asm!(include_str!("asm/entry.S")); #[panic_handler] fn panic_handler(_: &PanicInfo) -> ! { loop {} } -static HELLO: &[u8] = b"Hello World!"; +const UART0: usize = 0x10000000; +static HELLO: &[u8] = b"Hello World!\n"; -pub extern "C" fn start() -> ! { - let vga_buffer = 0xb8000 as *mut u8; +#[no_mangle] +pub unsafe extern "C" fn start() -> ! { + for c in HELLO.iter() { + while ptr::read_volatile((UART0 + 5) as *const u8) & (1 << 5) == 0 {} - 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; - } + ptr::write_volatile((UART0 + 0) as *mut u8, *c); } loop {}