Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Implement r0 crate in assembly #123

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ s-mode = []
single-hart = []

[dependencies]
r0 = "1.0.0"
riscv = "0.10"
riscv-rt-macros = { path = "macros", version = "0.2.0" }

Expand Down
2 changes: 1 addition & 1 deletion link.x
romancardenas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SECTIONS
_edata = .;
} > REGION_DATA AT > REGION_RODATA

.bss (NOLOAD) :
.bss (NOLOAD) : ALIGN(4)
{
_sbss = .;
*(.sbss .sbss.* .bss .bss.*);
Expand Down
60 changes: 45 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@
#[cfg(riscv)]
mod asm;

use core::sync::atomic::{compiler_fence, Ordering};

#[cfg(feature = "s-mode")]
use riscv::register::{scause as xcause, stvec as xtvec, stvec::TrapMode as xTrapMode};

Expand All @@ -378,19 +380,6 @@ pub use riscv_rt_macros::{entry, pre_init};
#[doc(hidden)]
pub static __ONCE__: () = ();

extern "C" {
// Boundaries of the .bss section
static mut _ebss: u32;
static mut _sbss: u32;

// Boundaries of the .data section
static mut _edata: u32;
static mut _sdata: u32;

// Initial values of the .data section (stored in Flash)
static _sidata: u32;
}

/// Rust entry point (_start_rust)
///
/// Zeros bss section, initializes data section and calls main. This function never returns.
Expand Down Expand Up @@ -424,8 +413,49 @@ pub unsafe extern "C" fn start_rust(a0: usize, a1: usize, a2: usize) -> ! {
if _mp_hook(hartid) {
__pre_init();

r0::zero_bss(&mut _sbss, &mut _ebss);
r0::init_data(&mut _sdata, &mut _edata, &_sidata);
// Initialize RAM
// 1. Copy over .data from flash to RAM
// 2. Zero out .bss
core::arch::asm!(
"
// Copy over .data
la {start},_sdata
la {end},_edata
la {input},_sidata

bgeu {start},{end},2f
1:
lw {a},0({input})
addi {input},{input},4
sw {a},0({start})
addi {start},{start},4
bltu {start},{end},1b

2:
li {a},0

// Zero out .bss
la {start},_sbss
la {end},_ebss

bgeu {start},{end},3f
2:
sw zero,0({start})
addi {start},{start},4
bltu {start},{end},2b

3:
li {start},0
li {end},0
li {input},0
",
start = out(reg) _,
end = out(reg) _,
input = out(reg) _,
a = out(reg) _,
);

compiler_fence(Ordering::SeqCst);
}

// TODO: Enable FPU when available
Expand Down