kernel/arch/riscv64/boot/
entry.rs

1use core::arch::naked_asm;
2
3use crate::{device::fdt::FdtManager, environment::STACK_SIZE, start_kernel};
4
5/// Entry point for the primary core
6#[unsafe(link_section = ".init")]
7#[unsafe(export_name = "_entry")]
8#[unsafe(naked)]
9pub extern "C" fn _entry() {
10    unsafe {
11        naked_asm!("
12        .attribute arch, \"rv64gc\"
13        .option norvc
14        .option norelax
15        .align 8
16                // a0 = hartid     
17                li      t0, {}
18                mv      t1, a0
19                addi    t1, t1, 1
20                mul     t1, t1, a0          
21                la      sp, KERNEL_STACK
22                add     sp, sp, t0
23
24                la     t0, arch_start_kernel
25                jr      t0
26        ", const STACK_SIZE
27        );
28    }
29}
30
31/// Entry point for the secondary cores
32#[unsafe(link_section = ".init")]
33#[unsafe(export_name = "_entry_ap")]
34#[unsafe(naked)]
35pub extern "C" fn _entry_ap() {
36    unsafe {
37        naked_asm!("
38        .attribute arch, \"rv64gc\"
39        .option norvc
40        .option norelax
41        .align 8
42                // a0 = hartid     
43                li      t0, {}
44                mv      t1, a0
45                addi    t1, t1, 1
46                mul     t1, t1, a0          
47                la      sp, KERNEL_STACK
48                add     sp, sp, t0
49
50                // Use indirect jump to avoid JAL range limitation
51                la      t0, start_ap
52                jr      t0
53        ", const STACK_SIZE
54        );
55    }
56}
57
58
59#[unsafe(no_mangle)]
60pub extern "C" fn arch_start_kernel(hartid: usize, fdt_ptr: usize) {
61    unsafe { 
62        FdtManager::set_fdt_addr(fdt_ptr);
63    }
64    start_kernel(hartid);
65}