xref: /DragonOS/kernel/src/process/c_adapter.rs (revision 013ffb708fab7760ea999c1edf462c69ac68f0ac)
1 use crate::smp::core::smp_get_processor_id;
2 
3 use super::{process_init, ProcessManager, __PROCESS_MANAGEMENT_INIT_DONE};
4 
5 #[no_mangle]
6 unsafe extern "C" fn rs_process_init() {
7     process_init();
8 }
9 
10 /// 临时用于获取空闲进程的栈顶的函数,这个函数是为了旧的smp模块的初始化而写在这的
11 #[no_mangle]
12 unsafe extern "C" fn rs_get_idle_stack_top(cpu_id: u32) -> usize {
13     return ProcessManager::idle_pcb()[cpu_id as usize]
14         .kernel_stack()
15         .stack_max_address()
16         .data();
17 }
18 
19 #[no_mangle]
20 unsafe extern "C" fn rs_current_pcb_cpuid() -> u32 {
21     return smp_get_processor_id().data();
22 }
23 
24 #[no_mangle]
25 unsafe extern "C" fn rs_current_pcb_pid() -> u32 {
26     if unsafe { __PROCESS_MANAGEMENT_INIT_DONE } {
27         return ProcessManager::current_pcb().pid().0 as u32;
28     }
29     return 0;
30 }
31 
32 #[no_mangle]
33 unsafe extern "C" fn rs_current_pcb_preempt_count() -> u32 {
34     if unsafe { !__PROCESS_MANAGEMENT_INIT_DONE } {
35         return 0;
36     }
37     return ProcessManager::current_pcb().preempt_count() as u32;
38 }
39 
40 #[no_mangle]
41 unsafe extern "C" fn rs_current_pcb_flags() -> u32 {
42     if unsafe { !__PROCESS_MANAGEMENT_INIT_DONE } {
43         return 0;
44     }
45     return ProcessManager::current_pcb().flags().bits() as u32;
46 }
47 
48 #[no_mangle]
49 #[cfg(target_arch = "x86_64")]
50 unsafe extern "C" fn rs_current_pcb_thread_rbp() -> u64 {
51     if unsafe { !__PROCESS_MANAGEMENT_INIT_DONE } {
52         return 0;
53     }
54     return ProcessManager::current_pcb().arch_info_irqsave().rbp() as u64;
55 }
56 
57 #[no_mangle]
58 #[cfg(target_arch = "riscv64")]
59 unsafe extern "C" fn rs_current_pcb_thread_rbp() -> u64 {
60     // 不应该实现这个函数
61     unimplemented!("rs_current_pcb_thread_rbp")
62 }
63 
64 #[no_mangle]
65 unsafe extern "C" fn rs_preempt_disable() {
66     return ProcessManager::preempt_disable();
67 }
68 
69 #[no_mangle]
70 unsafe extern "C" fn rs_preempt_enable() {
71     return ProcessManager::preempt_enable();
72 }
73 
74 #[no_mangle]
75 unsafe extern "C" fn rs_process_do_exit(exit_code: usize) -> usize {
76     if unsafe { !__PROCESS_MANAGEMENT_INIT_DONE } {
77         return 0;
78     }
79     ProcessManager::exit(exit_code);
80 }
81