19621ab16SLoGin use core::arch::asm;
24fda81ceSLoGin
3e8eab1acSLoGin use crate::{
49621ab16SLoGin arch::{asm::csr::CSR_SSTATUS, interrupt::TrapFrame},
5e8eab1acSLoGin process::{
64fda81ceSLoGin fork::CloneFlags,
79621ab16SLoGin kthread::{kernel_thread_bootstrap_stage2, KernelThreadCreateInfo, KernelThreadMechanism},
8e8eab1acSLoGin Pid, ProcessManager,
9e8eab1acSLoGin },
104fda81ceSLoGin };
119621ab16SLoGin use alloc::sync::Arc;
129621ab16SLoGin use asm_macros::restore_from_x6_to_x31;
139621ab16SLoGin use kdepends::memoffset::offset_of;
149621ab16SLoGin use riscv::register::sstatus::SPP;
159621ab16SLoGin use system_error::SystemError;
164fda81ceSLoGin
174fda81ceSLoGin impl KernelThreadMechanism {
184fda81ceSLoGin /// 伪造trapframe,创建内核线程
194fda81ceSLoGin ///
204fda81ceSLoGin /// ## 返回值
214fda81ceSLoGin ///
224fda81ceSLoGin /// 返回创建的内核线程的pid
23e8eab1acSLoGin #[inline(never)]
__inner_create( info: &Arc<KernelThreadCreateInfo>, clone_flags: CloneFlags, ) -> Result<Pid, SystemError>244fda81ceSLoGin pub fn __inner_create(
254fda81ceSLoGin info: &Arc<KernelThreadCreateInfo>,
264fda81ceSLoGin clone_flags: CloneFlags,
274fda81ceSLoGin ) -> Result<Pid, SystemError> {
28e8eab1acSLoGin // WARNING: If create failed, we must drop the info manually or it will cause memory leak. (refcount will not decrease when create failed)
29e8eab1acSLoGin let create_info: *const KernelThreadCreateInfo =
30e8eab1acSLoGin KernelThreadCreateInfo::generate_unsafe_arc_ptr(info.clone());
31e8eab1acSLoGin
32e8eab1acSLoGin let mut frame = TrapFrame::new();
339621ab16SLoGin frame.a2 = create_info as usize;
34e8eab1acSLoGin
35e8eab1acSLoGin // 使能中断
36e8eab1acSLoGin frame.status.update_sie(true);
37e8eab1acSLoGin frame.status.update_spp(SPP::Supervisor);
38*471d65cfSLoGin frame.status.update_sum(true);
39e8eab1acSLoGin
40e8eab1acSLoGin frame.ra = kernel_thread_bootstrap_stage1 as usize;
41e8eab1acSLoGin
42e8eab1acSLoGin // fork失败的话,子线程不会执行。否则将导致内存安全问题。
43e8eab1acSLoGin let pid = ProcessManager::fork(&frame, clone_flags).map_err(|e| {
44e8eab1acSLoGin unsafe { KernelThreadCreateInfo::parse_unsafe_arc_ptr(create_info) };
45e8eab1acSLoGin e
46e8eab1acSLoGin })?;
47e8eab1acSLoGin
48e8eab1acSLoGin ProcessManager::find(pid)
49e8eab1acSLoGin .unwrap()
50e8eab1acSLoGin .set_name(info.name().clone());
51e8eab1acSLoGin
52e8eab1acSLoGin return Ok(pid);
534fda81ceSLoGin }
544fda81ceSLoGin }
554fda81ceSLoGin
564fda81ceSLoGin /// 内核线程引导函数的第一阶段
574fda81ceSLoGin ///
584fda81ceSLoGin /// 当内核线程开始执行时,会先执行这个函数,这个函数会将伪造的trapframe中的数据弹出,然后跳转到第二阶段
594fda81ceSLoGin ///
604fda81ceSLoGin /// 跳转之后,指向Box<KernelThreadClosure>的指针将传入到stage2的函数
614fda81ceSLoGin // #[naked]
624fda81ceSLoGin // pub(super) unsafe extern "C" fn kernel_thread_bootstrap_stage1() {
634fda81ceSLoGin // todo!()
644fda81ceSLoGin // }
659621ab16SLoGin #[naked]
kernel_thread_bootstrap_stage1()664fda81ceSLoGin pub(super) unsafe extern "C" fn kernel_thread_bootstrap_stage1() {
674fda81ceSLoGin // 这个函数要是naked的,只是因为现在还没有实现,而naked func不能打`unimplemented!()`
684fda81ceSLoGin // 所以先写成了普通函数
699621ab16SLoGin asm!(concat!(
709621ab16SLoGin "
719621ab16SLoGin ld x3, {off_gp}(sp)
729621ab16SLoGin ld x5, {off_t0}(sp)
739621ab16SLoGin
749621ab16SLoGin ",
759621ab16SLoGin restore_from_x6_to_x31!(),
769621ab16SLoGin
779621ab16SLoGin "
789621ab16SLoGin ld a0, {off_status}(sp)
799621ab16SLoGin csrw {csr_status}, a0
809621ab16SLoGin mv a0, a2
819621ab16SLoGin j {stage2_func}
829621ab16SLoGin "
839621ab16SLoGin ),
849621ab16SLoGin csr_status = const CSR_SSTATUS,
859621ab16SLoGin off_status = const offset_of!(TrapFrame, status),
869621ab16SLoGin off_gp = const offset_of!(TrapFrame, gp),
879621ab16SLoGin off_t0 = const offset_of!(TrapFrame, t0),
889621ab16SLoGin off_t1 = const offset_of!(TrapFrame, t1),
899621ab16SLoGin off_t2 = const offset_of!(TrapFrame, t2),
909621ab16SLoGin off_s0 = const offset_of!(TrapFrame, s0),
919621ab16SLoGin off_s1 = const offset_of!(TrapFrame, s1),
929621ab16SLoGin off_a0 = const offset_of!(TrapFrame, a0),
939621ab16SLoGin off_a1 = const offset_of!(TrapFrame, a1),
949621ab16SLoGin off_a2 = const offset_of!(TrapFrame, a2),
959621ab16SLoGin off_a3 = const offset_of!(TrapFrame, a3),
969621ab16SLoGin off_a4 = const offset_of!(TrapFrame, a4),
979621ab16SLoGin off_a5 = const offset_of!(TrapFrame, a5),
989621ab16SLoGin off_a6 = const offset_of!(TrapFrame, a6),
999621ab16SLoGin off_a7 = const offset_of!(TrapFrame, a7),
1009621ab16SLoGin off_s2 = const offset_of!(TrapFrame, s2),
1019621ab16SLoGin off_s3 = const offset_of!(TrapFrame, s3),
1029621ab16SLoGin off_s4 = const offset_of!(TrapFrame, s4),
1039621ab16SLoGin off_s5 = const offset_of!(TrapFrame, s5),
1049621ab16SLoGin off_s6 = const offset_of!(TrapFrame, s6),
1059621ab16SLoGin off_s7 = const offset_of!(TrapFrame, s7),
1069621ab16SLoGin off_s8 = const offset_of!(TrapFrame, s8),
1079621ab16SLoGin off_s9 = const offset_of!(TrapFrame, s9),
1089621ab16SLoGin off_s10 = const offset_of!(TrapFrame, s10),
1099621ab16SLoGin off_s11 = const offset_of!(TrapFrame, s11),
1109621ab16SLoGin off_t3 = const offset_of!(TrapFrame, t3),
1119621ab16SLoGin off_t4 = const offset_of!(TrapFrame, t4),
1129621ab16SLoGin off_t5 = const offset_of!(TrapFrame, t5),
1139621ab16SLoGin off_t6 = const offset_of!(TrapFrame, t6),
1149621ab16SLoGin stage2_func = sym jump_to_stage2,
1159621ab16SLoGin options(noreturn),
1169621ab16SLoGin );
1179621ab16SLoGin }
1189621ab16SLoGin
jump_to_stage2(ptr: *const KernelThreadCreateInfo)1199621ab16SLoGin fn jump_to_stage2(ptr: *const KernelThreadCreateInfo) {
1209621ab16SLoGin unsafe { kernel_thread_bootstrap_stage2(ptr) };
1214fda81ceSLoGin }
122