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