Skip to content

Commit eb03dd0

Browse files
committed
Merge branch 'riscv-rt-asm' of github.com:rust-embedded/riscv into riscv-rt-asm
2 parents 9356b4b + 173ea43 commit eb03dd0

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Moved all the assembly code to `asm.rs`
13+
14+
### Removed
15+
16+
- start_rust is no longer needed, as it is now wrtten in assembly
17+
1018
## [v0.12.1] - 2024-01-24
1119

1220
### Added

riscv-rt/src/asm.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,110 @@ riscv_rt_macros::loop_global_asm!(" fmv.d.x f{}, x0", 32);
203203
#[cfg(all(riscvf, not(riscvd)))]
204204
riscv_rt_macros::loop_global_asm!(" fmv.w.x f{}, x0", 32);
205205

206+
// SET UP INTERRUPTS, RESTORE a0..a2, AND JUMP TO MAIN RUST FUNCTION
207+
cfg_global_asm!(
208+
"call _setup_interrupts",
209+
#[cfg(riscv32)]
210+
"lw a0, 4 * 0(sp)
211+
lw a1, 4 * 1(sp)
212+
lw a2, 4 * 2(sp)
213+
addi sp, sp, 4 * 3",
214+
#[cfg(riscv64)]
215+
"ld a0, 8 * 0(sp)
216+
ld a1, 8 * 1(sp)
217+
ld a2, 8 * 2(sp)
218+
addi sp, sp, 8 * 3",
219+
"jal zero, main
220+
"andi sp, t1, -16 // align stack to 16-bytes
221+
add s0, sp, zero",
222+
);
223+
224+
// STORE A0..A2 IN THE STACK, AS THEY WILL BE NEEDED LATER BY main
225+
cfg_global_asm!(
226+
#[cfg(riscv32)]
227+
"addi sp, sp, -4 * 3
228+
sw a0, 4 * 0(sp)
229+
sw a1, 4 * 1(sp)
230+
sw a2, 4 * 2(sp)",
231+
#[cfg(riscv64)]
232+
"addi sp, sp, -8 * 3
233+
sd a0, 8 * 0(sp)
234+
sd a1, 8 * 1(sp)
235+
sd a2, 8 * 2(sp)",
236+
);
237+
238+
// SKIP RAM INITIALIZATION IF CURRENT HART IS NOT THE BOOT HART
239+
#[cfg(not(feature = "single-hart"))]
240+
cfg_global_asm!(
241+
#[cfg(not(feature = "s-mode"))]
242+
"csrr a0, mhartid",
243+
"call _mp_hook
244+
mv t0, a0
245+
246+
beqz a0, 4f",
247+
);
248+
// IF CURRENT HART IS THE BOOT HART CALL __pre_init AND INITIALIZE RAM
249+
cfg_global_asm!(
250+
"call __pre_init
251+
// Copy .data from flash to RAM
252+
la t0, _sdata
253+
la t2, _edata
254+
la t1, _sidata
255+
bgeu t0, t2, 2f
256+
1: ",
257+
#[cfg(target_arch = "riscv32")]
258+
"lw t3, 0(t1)
259+
addi t1, t1, 4
260+
sw t3, 0(t0)
261+
addi t0, t0, 4
262+
bltu t0, t2, 1b",
263+
#[cfg(target_arch = "riscv64")]
264+
"ld t3, 0(t1)
265+
addi t1, t1, 8
266+
sd t3, 0(t0)
267+
addi t0, t0, 8
268+
bltu t0, t2, 1b",
269+
"
270+
2: // Zero out .bss
271+
la t0, _sbss
272+
la t2, _ebss
273+
bgeu t0, t2, 4f
274+
3: ",
275+
#[cfg(target_arch = "riscv32")]
276+
"sw zero, 0(t0)
277+
addi t0, t0, 4
278+
bltu t0, t2, 3b",
279+
#[cfg(target_arch = "riscv64")]
280+
"sd zero, 0(t0)
281+
addi t0, t0, 8
282+
bltu t0, t2, 3b",
283+
"
284+
4: // RAM initilized",
285+
);
286+
287+
// INITIALIZE FLOATING POINT UNIT
288+
#[cfg(any(riscvf, riscvd))]
289+
cfg_global_asm!(
290+
"
291+
li t0, 0x4000 // bit 14 is FS most significant bit
292+
li t2, 0x2000 // bit 13 is FS least significant bit
293+
",
294+
#[cfg(feature = "s-mode")]
295+
"csrrc x0, sstatus, t0
296+
csrrs x0, sstatus, t2",
297+
#[cfg(not(feature = "s-mode"))]
298+
"csrrc x0, mstatus, t0
299+
csrrs x0, mstatus, t2",
300+
"fscsr x0",
301+
);
302+
// ZERO OUT FLOATING POINT REGISTERS
303+
#[cfg(all(riscv32, riscvd))]
304+
riscv_rt_macros::loop_global_asm!(" fcvt.d.w f{}, x0", 32);
305+
#[cfg(all(riscv64, riscvd))]
306+
riscv_rt_macros::loop_global_asm!(" fmv.d.x f{}, x0", 32);
307+
#[cfg(all(riscvf, not(riscvd)))]
308+
riscv_rt_macros::loop_global_asm!(" fmv.w.x f{}, x0", 32);
309+
206310
// SET UP INTERRUPTS, RESTORE a0..a2, AND JUMP TO MAIN RUST FUNCTION
207311
cfg_global_asm!(
208312
"call _setup_interrupts",

0 commit comments

Comments
 (0)