kthread.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use crate::{
  2. arch::{asm::csr::CSR_SSTATUS, interrupt::TrapFrame},
  3. process::{
  4. fork::CloneFlags,
  5. kthread::{kernel_thread_bootstrap_stage2, KernelThreadCreateInfo, KernelThreadMechanism},
  6. ProcessManager, RawPid,
  7. },
  8. };
  9. use alloc::sync::Arc;
  10. use asm_macros::restore_from_x6_to_x31;
  11. use kdepends::memoffset::offset_of;
  12. use riscv::register::sstatus::SPP;
  13. use system_error::SystemError;
  14. impl KernelThreadMechanism {
  15. /// 伪造trapframe,创建内核线程
  16. ///
  17. /// ## 返回值
  18. ///
  19. /// 返回创建的内核线程的pid
  20. #[inline(never)]
  21. pub fn __inner_create(
  22. info: &Arc<KernelThreadCreateInfo>,
  23. clone_flags: CloneFlags,
  24. ) -> Result<RawPid, SystemError> {
  25. // WARNING: If create failed, we must drop the info manually or it will cause memory leak. (refcount will not decrease when create failed)
  26. let create_info: *const KernelThreadCreateInfo =
  27. KernelThreadCreateInfo::generate_unsafe_arc_ptr(info.clone());
  28. let mut frame = TrapFrame::new();
  29. frame.a2 = create_info as usize;
  30. // 使能中断
  31. frame.status.update_sie(true);
  32. frame.status.update_spp(SPP::Supervisor);
  33. frame.status.update_sum(true);
  34. frame.ra = kernel_thread_bootstrap_stage1 as usize;
  35. // fork失败的话,子线程不会执行。否则将导致内存安全问题。
  36. let pid = ProcessManager::fork(&frame, clone_flags).map_err(|e| {
  37. unsafe { KernelThreadCreateInfo::parse_unsafe_arc_ptr(create_info) };
  38. e
  39. })?;
  40. ProcessManager::find(pid)
  41. .unwrap()
  42. .set_name(info.name().clone());
  43. return Ok(pid);
  44. }
  45. }
  46. /// 内核线程引导函数的第一阶段
  47. ///
  48. /// 当内核线程开始执行时,会先执行这个函数,这个函数会将伪造的trapframe中的数据弹出,然后跳转到第二阶段
  49. ///
  50. /// 跳转之后,指向Box<KernelThreadClosure>的指针将传入到stage2的函数
  51. // #[naked]
  52. // pub(super) unsafe extern "C" fn kernel_thread_bootstrap_stage1() {
  53. // todo!()
  54. // }
  55. #[naked]
  56. pub(super) unsafe extern "C" fn kernel_thread_bootstrap_stage1() {
  57. // 这个函数要是naked的,只是因为现在还没有实现,而naked func不能打`unimplemented!()`
  58. // 所以先写成了普通函数
  59. core::arch::naked_asm!(concat!(
  60. "
  61. ld x3, {off_gp}(sp)
  62. ld x5, {off_t0}(sp)
  63. ",
  64. restore_from_x6_to_x31!(),
  65. "
  66. ld a0, {off_status}(sp)
  67. csrw {csr_status}, a0
  68. mv a0, a2
  69. j {stage2_func}
  70. "
  71. ),
  72. csr_status = const CSR_SSTATUS,
  73. off_status = const offset_of!(TrapFrame, status),
  74. off_gp = const offset_of!(TrapFrame, gp),
  75. off_t0 = const offset_of!(TrapFrame, t0),
  76. off_t1 = const offset_of!(TrapFrame, t1),
  77. off_t2 = const offset_of!(TrapFrame, t2),
  78. off_s0 = const offset_of!(TrapFrame, s0),
  79. off_s1 = const offset_of!(TrapFrame, s1),
  80. off_a0 = const offset_of!(TrapFrame, a0),
  81. off_a1 = const offset_of!(TrapFrame, a1),
  82. off_a2 = const offset_of!(TrapFrame, a2),
  83. off_a3 = const offset_of!(TrapFrame, a3),
  84. off_a4 = const offset_of!(TrapFrame, a4),
  85. off_a5 = const offset_of!(TrapFrame, a5),
  86. off_a6 = const offset_of!(TrapFrame, a6),
  87. off_a7 = const offset_of!(TrapFrame, a7),
  88. off_s2 = const offset_of!(TrapFrame, s2),
  89. off_s3 = const offset_of!(TrapFrame, s3),
  90. off_s4 = const offset_of!(TrapFrame, s4),
  91. off_s5 = const offset_of!(TrapFrame, s5),
  92. off_s6 = const offset_of!(TrapFrame, s6),
  93. off_s7 = const offset_of!(TrapFrame, s7),
  94. off_s8 = const offset_of!(TrapFrame, s8),
  95. off_s9 = const offset_of!(TrapFrame, s9),
  96. off_s10 = const offset_of!(TrapFrame, s10),
  97. off_s11 = const offset_of!(TrapFrame, s11),
  98. off_t3 = const offset_of!(TrapFrame, t3),
  99. off_t4 = const offset_of!(TrapFrame, t4),
  100. off_t5 = const offset_of!(TrapFrame, t5),
  101. off_t6 = const offset_of!(TrapFrame, t6),
  102. stage2_func = sym jump_to_stage2
  103. );
  104. }
  105. fn jump_to_stage2(ptr: *const KernelThreadCreateInfo) {
  106. unsafe { kernel_thread_bootstrap_stage2(ptr) };
  107. }