1 use core::{ffi::c_void, ptr::null_mut};
2
3 use alloc::boxed::Box;
4
5 use crate::{
6 arch::{asm::current::current_pcb, fpu::FpState},
7 include::bindings::bindings::process_control_block,
8 syscall::SystemError,
9 };
10
11 use super::{fork::copy_mm, process::init_stdio, process_init};
12
13 #[no_mangle]
rs_process_init()14 pub extern "C" fn rs_process_init() {
15 process_init();
16 }
17
18 #[no_mangle]
rs_process_copy_mm(clone_vm: bool, new_pcb: &mut process_control_block) -> usize19 pub extern "C" fn rs_process_copy_mm(clone_vm: bool, new_pcb: &mut process_control_block) -> usize {
20 return copy_mm(clone_vm, new_pcb)
21 .map(|_| 0)
22 .unwrap_or_else(|err| err.to_posix_errno() as usize);
23 }
24
25 /// @brief 初始化当前进程的文件描述符数组
26 /// 请注意,如果当前进程已经有文件描述符数组,那么本操作将被禁止
27 #[no_mangle]
process_init_files() -> i3228 pub extern "C" fn process_init_files() -> i32 {
29 let r = current_pcb().init_files();
30 if r.is_ok() {
31 return 0;
32 } else {
33 return r.unwrap_err().to_posix_errno();
34 }
35 }
36
37 #[no_mangle]
rs_drop_address_space(pcb: &'static mut process_control_block) -> i3238 pub extern "C" fn rs_drop_address_space(pcb: &'static mut process_control_block) -> i32 {
39 unsafe {
40 pcb.drop_address_space();
41 }
42 return 0;
43 }
44
45 /// @brief 拷贝当前进程的文件描述符信息
46 ///
47 /// @param clone_flags 克隆标志位
48 /// @param pcb 新的进程的pcb
49 #[no_mangle]
process_copy_files( clone_flags: u64, from: &'static process_control_block, ) -> i3250 pub extern "C" fn process_copy_files(
51 clone_flags: u64,
52 from: &'static process_control_block,
53 ) -> i32 {
54 let r = current_pcb().copy_files(clone_flags, from);
55 if r.is_ok() {
56 return 0;
57 } else {
58 return r.unwrap_err().to_posix_errno();
59 }
60 }
61
62 /// @brief 回收进程的文件描述符数组
63 ///
64 /// @param pcb 要被回收的进程的pcb
65 ///
66 /// @return i32
67 #[no_mangle]
process_exit_files(pcb: &'static mut process_control_block) -> i3268 pub extern "C" fn process_exit_files(pcb: &'static mut process_control_block) -> i32 {
69 let r: Result<(), SystemError> = pcb.exit_files();
70 if r.is_ok() {
71 return 0;
72 } else {
73 return r.unwrap_err().to_posix_errno();
74 }
75 }
76
77 /// @brief 复制当前进程的浮点状态
78 #[allow(dead_code)]
79 #[no_mangle]
rs_dup_fpstate() -> *mut c_void80 pub extern "C" fn rs_dup_fpstate() -> *mut c_void {
81 // 如果当前进程没有浮点状态,那么就返回一个默认的浮点状态
82 if current_pcb().fp_state == null_mut() {
83 return Box::leak(Box::new(FpState::default())) as *mut FpState as usize as *mut c_void;
84 } else {
85 // 如果当前进程有浮点状态,那么就复制一个新的浮点状态
86 let state = current_pcb().fp_state as usize as *mut FpState;
87 unsafe {
88 let s = state.as_ref().unwrap();
89 let state: &mut FpState = Box::leak(Box::new(s.clone()));
90
91 return state as *mut FpState as usize as *mut c_void;
92 }
93 }
94 }
95
96 /// @brief 释放进程的浮点状态所占用的内存
97 #[no_mangle]
rs_process_exit_fpstate(pcb: &'static mut process_control_block)98 pub extern "C" fn rs_process_exit_fpstate(pcb: &'static mut process_control_block) {
99 if pcb.fp_state != null_mut() {
100 let state = pcb.fp_state as usize as *mut FpState;
101 unsafe {
102 drop(Box::from_raw(state));
103 }
104 }
105 }
106
107 #[no_mangle]
rs_init_stdio() -> i32108 pub extern "C" fn rs_init_stdio() -> i32 {
109 let r = init_stdio();
110 if r.is_ok() {
111 return 0;
112 } else {
113 return r.unwrap_err().to_posix_errno();
114 }
115 }
116