1393f6915SLoGin use core::{intrinsics::unlikely, sync::atomic::Ordering}; 2393f6915SLoGin 31496ba7bSLoGin use alloc::{string::ToString, sync::Arc}; 4c8025a88Slogin 5c8025a88Slogin use crate::{ 6393f6915SLoGin arch::{interrupt::TrapFrame, ipc::signal::Signal}, 7971462beSGnoCiYeH filesystem::procfs::procfs_register_pid, 8971462beSGnoCiYeH ipc::signal::flush_signal_handlers, 9971462beSGnoCiYeH libs::rwlock::RwLock, 10971462beSGnoCiYeH mm::VirtAddr, 11971462beSGnoCiYeH process::ProcessFlags, 12971462beSGnoCiYeH syscall::{user_access::UserBufferWriter, SystemError}, 13c8025a88Slogin }; 1466f67c6aSlogin 151496ba7bSLoGin use super::{ 161496ba7bSLoGin kthread::{KernelThreadPcbPrivate, WorkerPrivate}, 171496ba7bSLoGin KernelStack, Pid, ProcessControlBlock, ProcessManager, 181496ba7bSLoGin }; 191a2eaa40Slogin 201496ba7bSLoGin bitflags! { 211496ba7bSLoGin /// 进程克隆标志 22971462beSGnoCiYeH pub struct CloneFlags: u64 { 231496ba7bSLoGin /// 在进程间共享虚拟内存空间 24971462beSGnoCiYeH const CLONE_VM = 0x00000100; 25971462beSGnoCiYeH /// 在进程间共享文件系统信息 26971462beSGnoCiYeH const CLONE_FS = 0x00000200; 271496ba7bSLoGin /// 共享打开的文件 28971462beSGnoCiYeH const CLONE_FILES = 0x00000400; 29971462beSGnoCiYeH /// 克隆时,与父进程共享信号处理结构体 30971462beSGnoCiYeH const CLONE_SIGHAND = 0x00000800; 31971462beSGnoCiYeH /// 返回进程的文件描述符 32971462beSGnoCiYeH const CLONE_PIDFD = 0x00001000; 33971462beSGnoCiYeH /// 使克隆对象成为父进程的跟踪对象 34971462beSGnoCiYeH const CLONE_PTRACE = 0x00002000; 35971462beSGnoCiYeH /// 在执行 exec() 或 _exit() 之前挂起父进程的执行 36971462beSGnoCiYeH const CLONE_VFORK = 0x00004000; 37971462beSGnoCiYeH /// 使克隆对象的父进程为调用进程的父进程 38971462beSGnoCiYeH const CLONE_PARENT = 0x00008000; 39971462beSGnoCiYeH /// 拷贝线程 40971462beSGnoCiYeH const CLONE_THREAD = 0x00010000; 41971462beSGnoCiYeH /// 创建一个新的命名空间,其中包含独立的文件系统挂载点层次结构。 42971462beSGnoCiYeH const CLONE_NEWNS = 0x00020000; 43971462beSGnoCiYeH /// 与父进程共享 System V 信号量。 44971462beSGnoCiYeH const CLONE_SYSVSEM = 0x00040000; 45971462beSGnoCiYeH /// 设置其线程本地存储 46971462beSGnoCiYeH const CLONE_SETTLS = 0x00080000; 47971462beSGnoCiYeH /// 设置partent_tid地址为子进程线程 ID 48971462beSGnoCiYeH const CLONE_PARENT_SETTID = 0x00100000; 49971462beSGnoCiYeH /// 在子进程中设置一个清除线程 ID 的用户空间地址 50971462beSGnoCiYeH const CLONE_CHILD_CLEARTID = 0x00200000; 51971462beSGnoCiYeH /// 创建一个新线程,将其设置为分离状态 52971462beSGnoCiYeH const CLONE_DETACHED = 0x00400000; 53971462beSGnoCiYeH /// 使其在创建者进程或线程视角下成为无法跟踪的。 54971462beSGnoCiYeH const CLONE_UNTRACED = 0x00800000; 55971462beSGnoCiYeH /// 设置其子进程线程 ID 56971462beSGnoCiYeH const CLONE_CHILD_SETTID = 0x01000000; 57971462beSGnoCiYeH /// 将其放置在一个新的 cgroup 命名空间中 58971462beSGnoCiYeH const CLONE_NEWCGROUP = 0x02000000; 59971462beSGnoCiYeH /// 将其放置在一个新的 UTS 命名空间中 60971462beSGnoCiYeH const CLONE_NEWUTS = 0x04000000; 61971462beSGnoCiYeH /// 将其放置在一个新的 IPC 命名空间中 62971462beSGnoCiYeH const CLONE_NEWIPC = 0x08000000; 63971462beSGnoCiYeH /// 将其放置在一个新的用户命名空间中 64971462beSGnoCiYeH const CLONE_NEWUSER = 0x10000000; 65971462beSGnoCiYeH /// 将其放置在一个新的 PID 命名空间中 66971462beSGnoCiYeH const CLONE_NEWPID = 0x20000000; 67971462beSGnoCiYeH /// 将其放置在一个新的网络命名空间中 68971462beSGnoCiYeH const CLONE_NEWNET = 0x40000000; 69971462beSGnoCiYeH /// 在新的 I/O 上下文中运行它 70971462beSGnoCiYeH const CLONE_IO = 0x80000000; 71971462beSGnoCiYeH /// 克隆时,与父进程共享信号结构体 72971462beSGnoCiYeH const CLONE_SIGNAL = 0x00010000 | 0x00000800; 73971462beSGnoCiYeH /// 克隆时,将原本被设置为SIG_IGNORE的信号,设置回SIG_DEFAULT 74971462beSGnoCiYeH const CLONE_CLEAR_SIGHAND = 0x100000000; 75971462beSGnoCiYeH } 76971462beSGnoCiYeH } 77971462beSGnoCiYeH 78971462beSGnoCiYeH /// ## clone与clone3系统调用的参数载体 79971462beSGnoCiYeH /// 80971462beSGnoCiYeH /// 因为这两个系统调用的参数很多,所以有这样一个载体更灵活 81971462beSGnoCiYeH /// 82971462beSGnoCiYeH /// 仅仅作为参数传递 83971462beSGnoCiYeH #[derive(Debug, Clone, Copy)] 84971462beSGnoCiYeH pub struct KernelCloneArgs { 85971462beSGnoCiYeH pub flags: CloneFlags, 86971462beSGnoCiYeH 87971462beSGnoCiYeH // 下列属性均来自用户空间 88971462beSGnoCiYeH pub pidfd: VirtAddr, 89971462beSGnoCiYeH pub child_tid: VirtAddr, 90971462beSGnoCiYeH pub parent_tid: VirtAddr, 91971462beSGnoCiYeH pub set_tid: VirtAddr, 92971462beSGnoCiYeH 93393f6915SLoGin /// 进程退出时发送的信号 94393f6915SLoGin pub exit_signal: Signal, 95971462beSGnoCiYeH 96971462beSGnoCiYeH pub stack: usize, 97971462beSGnoCiYeH // clone3用到 98971462beSGnoCiYeH pub stack_size: usize, 99971462beSGnoCiYeH pub tls: usize, 100971462beSGnoCiYeH 101971462beSGnoCiYeH pub set_tid_size: usize, 102971462beSGnoCiYeH pub cgroup: i32, 103971462beSGnoCiYeH 104971462beSGnoCiYeH pub io_thread: bool, 105971462beSGnoCiYeH pub kthread: bool, 106971462beSGnoCiYeH pub idle: bool, 107971462beSGnoCiYeH pub func: VirtAddr, 108971462beSGnoCiYeH pub fn_arg: VirtAddr, 109971462beSGnoCiYeH // cgrp 和 cset? 110971462beSGnoCiYeH } 111971462beSGnoCiYeH 112971462beSGnoCiYeH impl KernelCloneArgs { 113971462beSGnoCiYeH pub fn new() -> Self { 114971462beSGnoCiYeH let null_addr = VirtAddr::new(0); 115971462beSGnoCiYeH Self { 116971462beSGnoCiYeH flags: unsafe { CloneFlags::from_bits_unchecked(0) }, 117971462beSGnoCiYeH pidfd: null_addr, 118971462beSGnoCiYeH child_tid: null_addr, 119971462beSGnoCiYeH parent_tid: null_addr, 120971462beSGnoCiYeH set_tid: null_addr, 121393f6915SLoGin exit_signal: Signal::SIGCHLD, 122971462beSGnoCiYeH stack: 0, 123971462beSGnoCiYeH stack_size: 0, 124971462beSGnoCiYeH tls: 0, 125971462beSGnoCiYeH set_tid_size: 0, 126971462beSGnoCiYeH cgroup: 0, 127971462beSGnoCiYeH io_thread: false, 128971462beSGnoCiYeH kthread: false, 129971462beSGnoCiYeH idle: false, 130971462beSGnoCiYeH func: null_addr, 131971462beSGnoCiYeH fn_arg: null_addr, 132971462beSGnoCiYeH } 133c8025a88Slogin } 13466f67c6aSlogin } 13566f67c6aSlogin 1361496ba7bSLoGin impl ProcessManager { 1371496ba7bSLoGin /// 创建一个新进程 1381496ba7bSLoGin /// 1391496ba7bSLoGin /// ## 参数 1401496ba7bSLoGin /// 1411496ba7bSLoGin /// - `current_trapframe`: 当前进程的trapframe 1421496ba7bSLoGin /// - `clone_flags`: 进程克隆标志 1431496ba7bSLoGin /// 1441496ba7bSLoGin /// ## 返回值 1451496ba7bSLoGin /// 1461496ba7bSLoGin /// - 成功:返回新进程的pid 1471496ba7bSLoGin /// - 失败:返回Err(SystemError),fork失败的话,子线程不会执行。 1481496ba7bSLoGin /// 1491496ba7bSLoGin /// ## Safety 1501496ba7bSLoGin /// 1511496ba7bSLoGin /// - fork失败的话,子线程不会执行。 1521496ba7bSLoGin pub fn fork( 1531496ba7bSLoGin current_trapframe: &mut TrapFrame, 1541496ba7bSLoGin clone_flags: CloneFlags, 1551496ba7bSLoGin ) -> Result<Pid, SystemError> { 1561496ba7bSLoGin let current_pcb = ProcessManager::current_pcb(); 157*4fda81ceSLoGin 158*4fda81ceSLoGin let new_kstack: KernelStack = KernelStack::new()?; 159*4fda81ceSLoGin 1601496ba7bSLoGin let name = current_pcb.basic().name().to_string(); 1611496ba7bSLoGin let pcb = ProcessControlBlock::new(name, new_kstack); 1621496ba7bSLoGin 163971462beSGnoCiYeH let mut args = KernelCloneArgs::new(); 164971462beSGnoCiYeH args.flags = clone_flags; 165393f6915SLoGin args.exit_signal = Signal::SIGCHLD; 166393f6915SLoGin 167971462beSGnoCiYeH Self::copy_process(¤t_pcb, &pcb, args, current_trapframe)?; 1681496ba7bSLoGin 1691496ba7bSLoGin ProcessManager::add_pcb(pcb.clone()); 1701496ba7bSLoGin 1711496ba7bSLoGin // 向procfs注册进程 1721496ba7bSLoGin procfs_register_pid(pcb.pid()).unwrap_or_else(|e| { 1731496ba7bSLoGin panic!( 1741496ba7bSLoGin "fork: Failed to register pid to procfs, pid: [{:?}]. Error: {:?}", 1751496ba7bSLoGin pcb.pid(), 1761496ba7bSLoGin e 1771496ba7bSLoGin ) 1781496ba7bSLoGin }); 1791496ba7bSLoGin 1801496ba7bSLoGin ProcessManager::wakeup(&pcb).unwrap_or_else(|e| { 1811496ba7bSLoGin panic!( 1821496ba7bSLoGin "fork: Failed to wakeup new process, pid: [{:?}]. Error: {:?}", 1831496ba7bSLoGin pcb.pid(), 1841496ba7bSLoGin e 1851496ba7bSLoGin ) 1861496ba7bSLoGin }); 1871496ba7bSLoGin 1881496ba7bSLoGin return Ok(pcb.pid()); 1891496ba7bSLoGin } 1901496ba7bSLoGin 1911496ba7bSLoGin fn copy_flags( 1921496ba7bSLoGin clone_flags: &CloneFlags, 1931496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 1941496ba7bSLoGin ) -> Result<(), SystemError> { 1951496ba7bSLoGin if clone_flags.contains(CloneFlags::CLONE_VM) { 1961496ba7bSLoGin new_pcb.flags().insert(ProcessFlags::VFORK); 1971496ba7bSLoGin } 19870a4e555SLoGin *new_pcb.flags.get_mut() = ProcessManager::current_pcb().flags().clone(); 1991496ba7bSLoGin return Ok(()); 20066f67c6aSlogin } 20140fe15e0SLoGin 20240fe15e0SLoGin /// 拷贝进程的地址空间 20340fe15e0SLoGin /// 20440fe15e0SLoGin /// ## 参数 20540fe15e0SLoGin /// 20640fe15e0SLoGin /// - `clone_vm`: 是否与父进程共享地址空间。true表示共享 20740fe15e0SLoGin /// - `new_pcb`: 新进程的pcb 20840fe15e0SLoGin /// 20940fe15e0SLoGin /// ## 返回值 21040fe15e0SLoGin /// 21140fe15e0SLoGin /// - 成功:返回Ok(()) 21240fe15e0SLoGin /// - 失败:返回Err(SystemError) 21340fe15e0SLoGin /// 21440fe15e0SLoGin /// ## Panic 21540fe15e0SLoGin /// 21640fe15e0SLoGin /// - 如果当前进程没有用户地址空间,则panic 217*4fda81ceSLoGin #[inline(never)] 2181496ba7bSLoGin fn copy_mm( 2191496ba7bSLoGin clone_flags: &CloneFlags, 2201496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2211496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2221496ba7bSLoGin ) -> Result<(), SystemError> { 2231496ba7bSLoGin let old_address_space = current_pcb.basic().user_vm().unwrap_or_else(|| { 2241496ba7bSLoGin panic!( 2251496ba7bSLoGin "copy_mm: Failed to get address space of current process, current pid: [{:?}]", 2261496ba7bSLoGin current_pcb.pid() 2271496ba7bSLoGin ) 2281496ba7bSLoGin }); 22940fe15e0SLoGin 2301496ba7bSLoGin if clone_flags.contains(CloneFlags::CLONE_VM) { 2311496ba7bSLoGin unsafe { new_pcb.basic_mut().set_user_vm(Some(old_address_space)) }; 23240fe15e0SLoGin return Ok(()); 23340fe15e0SLoGin } 23440fe15e0SLoGin let new_address_space = old_address_space.write().try_clone().unwrap_or_else(|e| { 23540fe15e0SLoGin panic!( 2361496ba7bSLoGin "copy_mm: Failed to clone address space of current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 2371496ba7bSLoGin current_pcb.pid(), new_pcb.pid(), e 23840fe15e0SLoGin ) 23940fe15e0SLoGin }); 2401496ba7bSLoGin unsafe { new_pcb.basic_mut().set_user_vm(Some(new_address_space)) }; 24140fe15e0SLoGin return Ok(()); 24240fe15e0SLoGin } 2431496ba7bSLoGin 2441496ba7bSLoGin fn copy_files( 2451496ba7bSLoGin clone_flags: &CloneFlags, 2461496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2471496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2481496ba7bSLoGin ) -> Result<(), SystemError> { 2491496ba7bSLoGin // 如果不共享文件描述符表,则拷贝文件描述符表 2501496ba7bSLoGin if !clone_flags.contains(CloneFlags::CLONE_FILES) { 2511496ba7bSLoGin let new_fd_table = current_pcb.basic().fd_table().unwrap().read().clone(); 2521496ba7bSLoGin let new_fd_table = Arc::new(RwLock::new(new_fd_table)); 2531496ba7bSLoGin new_pcb.basic_mut().set_fd_table(Some(new_fd_table)); 254bb0e4d41SGnoCiYeH } else { 2551496ba7bSLoGin // 如果共享文件描述符表,则直接拷贝指针 2561496ba7bSLoGin new_pcb 2571496ba7bSLoGin .basic_mut() 2581496ba7bSLoGin .set_fd_table(current_pcb.basic().fd_table().clone()); 259bb0e4d41SGnoCiYeH } 2601496ba7bSLoGin 2611496ba7bSLoGin return Ok(()); 2621496ba7bSLoGin } 2631496ba7bSLoGin 2641496ba7bSLoGin #[allow(dead_code)] 2651496ba7bSLoGin fn copy_sighand( 2663c82aa56SChiichen clone_flags: &CloneFlags, 2673c82aa56SChiichen current_pcb: &Arc<ProcessControlBlock>, 2683c82aa56SChiichen new_pcb: &Arc<ProcessControlBlock>, 2691496ba7bSLoGin ) -> Result<(), SystemError> { 2703c82aa56SChiichen // // 将信号的处理函数设置为default(除了那些被手动屏蔽的) 2713c82aa56SChiichen if clone_flags.contains(CloneFlags::CLONE_CLEAR_SIGHAND) { 2723c82aa56SChiichen flush_signal_handlers(new_pcb.clone(), false); 2733c82aa56SChiichen } 2743c82aa56SChiichen 2753c82aa56SChiichen if clone_flags.contains(CloneFlags::CLONE_SIGHAND) { 2763c82aa56SChiichen (*new_pcb.sig_struct()).handlers = current_pcb.sig_struct().handlers.clone(); 2773c82aa56SChiichen } 2781496ba7bSLoGin return Ok(()); 2791496ba7bSLoGin } 280971462beSGnoCiYeH 281971462beSGnoCiYeH /// 拷贝进程信息 282971462beSGnoCiYeH /// 283971462beSGnoCiYeH /// ## panic: 284971462beSGnoCiYeH /// 某一步拷贝失败时会引发panic 285971462beSGnoCiYeH /// 例如:copy_mm等失败时会触发panic 286971462beSGnoCiYeH /// 287971462beSGnoCiYeH /// ## 参数 288971462beSGnoCiYeH /// 289971462beSGnoCiYeH /// - clone_flags 标志位 290971462beSGnoCiYeH /// - des_pcb 目标pcb 291971462beSGnoCiYeH /// - src_pcb 拷贝源pcb 292971462beSGnoCiYeH /// 293971462beSGnoCiYeH /// ## return 294971462beSGnoCiYeH /// - 发生错误时返回Err(SystemError) 295*4fda81ceSLoGin #[inline(never)] 296971462beSGnoCiYeH pub fn copy_process( 297971462beSGnoCiYeH current_pcb: &Arc<ProcessControlBlock>, 298971462beSGnoCiYeH pcb: &Arc<ProcessControlBlock>, 299971462beSGnoCiYeH clone_args: KernelCloneArgs, 300971462beSGnoCiYeH current_trapframe: &mut TrapFrame, 301971462beSGnoCiYeH ) -> Result<(), SystemError> { 302971462beSGnoCiYeH let clone_flags = clone_args.flags; 303971462beSGnoCiYeH // 不允许与不同namespace的进程共享根目录 304971462beSGnoCiYeH if (clone_flags == (CloneFlags::CLONE_NEWNS | CloneFlags::CLONE_FS)) 305971462beSGnoCiYeH || clone_flags == (CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_FS) 306971462beSGnoCiYeH { 307971462beSGnoCiYeH return Err(SystemError::EINVAL); 308971462beSGnoCiYeH } 309971462beSGnoCiYeH 310971462beSGnoCiYeH // 线程组必须共享信号,分离线程只能在线程组内启动。 311971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD) 312971462beSGnoCiYeH && !clone_flags.contains(CloneFlags::CLONE_SIGHAND) 313971462beSGnoCiYeH { 314971462beSGnoCiYeH return Err(SystemError::EINVAL); 315971462beSGnoCiYeH } 316971462beSGnoCiYeH 317971462beSGnoCiYeH // 共享信号处理器意味着共享vm。 318971462beSGnoCiYeH // 线程组也意味着共享vm。阻止这种情况可以简化其他代码。 319971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_SIGHAND) 320971462beSGnoCiYeH && !clone_flags.contains(CloneFlags::CLONE_VM) 321971462beSGnoCiYeH { 322971462beSGnoCiYeH return Err(SystemError::EINVAL); 323971462beSGnoCiYeH } 324971462beSGnoCiYeH 325971462beSGnoCiYeH // TODO: 处理CLONE_PARENT 与 SIGNAL_UNKILLABLE的情况 326971462beSGnoCiYeH 327971462beSGnoCiYeH // 如果新进程使用不同的 pid 或 namespace, 328971462beSGnoCiYeH // 则不允许它与分叉任务共享线程组。 329971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD) { 330971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWPID) { 331971462beSGnoCiYeH return Err(SystemError::EINVAL); 332971462beSGnoCiYeH } 333971462beSGnoCiYeH // TODO: 判断新进程与当前进程namespace是否相同,不同则返回错误 334971462beSGnoCiYeH } 335971462beSGnoCiYeH 336971462beSGnoCiYeH // 如果新进程将处于不同的time namespace, 337971462beSGnoCiYeH // 则不能让它共享vm或线程组。 338971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM) { 339971462beSGnoCiYeH // TODO: 判断time namespace,不同则返回错误 340971462beSGnoCiYeH } 341971462beSGnoCiYeH 342971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_PIDFD) 343971462beSGnoCiYeH && clone_flags.contains(CloneFlags::CLONE_DETACHED | CloneFlags::CLONE_THREAD) 344971462beSGnoCiYeH { 345971462beSGnoCiYeH return Err(SystemError::EINVAL); 346971462beSGnoCiYeH } 347971462beSGnoCiYeH 348971462beSGnoCiYeH // TODO: 克隆前应该锁信号处理,等待克隆完成后再处理 349971462beSGnoCiYeH 350971462beSGnoCiYeH // 克隆架构相关 35116033951SGnoCiYeH let guard = current_pcb.arch_info_irqsave(); 35216033951SGnoCiYeH pcb.arch_info().clone_from(&guard); 35316033951SGnoCiYeH drop(guard); 354971462beSGnoCiYeH 355971462beSGnoCiYeH // 为内核线程设置WorkerPrivate 356971462beSGnoCiYeH if current_pcb.flags().contains(ProcessFlags::KTHREAD) { 357971462beSGnoCiYeH *pcb.worker_private() = 358971462beSGnoCiYeH Some(WorkerPrivate::KernelThread(KernelThreadPcbPrivate::new())); 359971462beSGnoCiYeH } 360971462beSGnoCiYeH 361971462beSGnoCiYeH // 设置clear_child_tid,在线程结束时将其置0以通知父进程 362971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_CHILD_CLEARTID) { 363971462beSGnoCiYeH pcb.thread.write().clear_child_tid = Some(clone_args.child_tid); 364971462beSGnoCiYeH } 365971462beSGnoCiYeH 366971462beSGnoCiYeH // 设置child_tid,意味着子线程能够知道自己的id 367971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_CHILD_SETTID) { 368971462beSGnoCiYeH pcb.thread.write().set_child_tid = Some(clone_args.child_tid); 369971462beSGnoCiYeH } 370971462beSGnoCiYeH 371971462beSGnoCiYeH // 将子进程/线程的id存储在用户态传进的地址中 372971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_PARENT_SETTID) { 373971462beSGnoCiYeH let mut writer = UserBufferWriter::new( 374971462beSGnoCiYeH clone_args.parent_tid.data() as *mut i32, 375971462beSGnoCiYeH core::mem::size_of::<i32>(), 376971462beSGnoCiYeH true, 377971462beSGnoCiYeH )?; 378971462beSGnoCiYeH 379971462beSGnoCiYeH writer.copy_one_to_user(&(pcb.pid().0 as i32), 0)?; 380971462beSGnoCiYeH } 381971462beSGnoCiYeH 382971462beSGnoCiYeH // 拷贝标志位 383971462beSGnoCiYeH Self::copy_flags(&clone_flags, &pcb).unwrap_or_else(|e| { 384971462beSGnoCiYeH panic!( 385971462beSGnoCiYeH "fork: Failed to copy flags from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 386971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 387971462beSGnoCiYeH ) 388971462beSGnoCiYeH }); 389971462beSGnoCiYeH 390971462beSGnoCiYeH // 拷贝用户地址空间 391971462beSGnoCiYeH Self::copy_mm(&clone_flags, ¤t_pcb, &pcb).unwrap_or_else(|e| { 392971462beSGnoCiYeH panic!( 393971462beSGnoCiYeH "fork: Failed to copy mm from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 394971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 395971462beSGnoCiYeH ) 396971462beSGnoCiYeH }); 397971462beSGnoCiYeH 398971462beSGnoCiYeH // 拷贝文件描述符表 399971462beSGnoCiYeH Self::copy_files(&clone_flags, ¤t_pcb, &pcb).unwrap_or_else(|e| { 400971462beSGnoCiYeH panic!( 401971462beSGnoCiYeH "fork: Failed to copy files from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 402971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 403971462beSGnoCiYeH ) 404971462beSGnoCiYeH }); 405971462beSGnoCiYeH 406393f6915SLoGin // 拷贝信号相关数据 407393f6915SLoGin Self::copy_sighand(&clone_flags, ¤t_pcb, &pcb).map_err(|e| { 408393f6915SLoGin panic!( 409393f6915SLoGin "fork: Failed to copy sighand from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 410393f6915SLoGin current_pcb.pid(), pcb.pid(), e 411393f6915SLoGin ) 412393f6915SLoGin })?; 413971462beSGnoCiYeH 414971462beSGnoCiYeH // 拷贝线程 415971462beSGnoCiYeH Self::copy_thread(¤t_pcb, &pcb, clone_args,¤t_trapframe).unwrap_or_else(|e| { 416971462beSGnoCiYeH panic!( 417971462beSGnoCiYeH "fork: Failed to copy thread from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 418971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 419971462beSGnoCiYeH ) 420971462beSGnoCiYeH }); 421971462beSGnoCiYeH 422393f6915SLoGin // 设置线程组id、组长 423393f6915SLoGin if clone_flags.contains(CloneFlags::CLONE_THREAD) { 424393f6915SLoGin pcb.thread.write().group_leader = current_pcb.thread.read().group_leader.clone(); 425393f6915SLoGin unsafe { 426393f6915SLoGin let ptr = pcb.as_ref() as *const ProcessControlBlock as *mut ProcessControlBlock; 427393f6915SLoGin (*ptr).tgid = current_pcb.tgid; 428393f6915SLoGin } 429393f6915SLoGin } else { 430393f6915SLoGin pcb.thread.write().group_leader = Arc::downgrade(&pcb); 431393f6915SLoGin unsafe { 432393f6915SLoGin let ptr = pcb.as_ref() as *const ProcessControlBlock as *mut ProcessControlBlock; 433393f6915SLoGin (*ptr).tgid = pcb.tgid; 434393f6915SLoGin } 435393f6915SLoGin } 436393f6915SLoGin 437393f6915SLoGin // CLONE_PARENT re-uses the old parent 438393f6915SLoGin if clone_flags.contains(CloneFlags::CLONE_PARENT | CloneFlags::CLONE_THREAD) { 439393f6915SLoGin *pcb.real_parent_pcb.write() = current_pcb.real_parent_pcb.read().clone(); 440393f6915SLoGin 441393f6915SLoGin if clone_flags.contains(CloneFlags::CLONE_THREAD) { 442393f6915SLoGin pcb.exit_signal.store(Signal::INVALID, Ordering::SeqCst); 443393f6915SLoGin } else { 444393f6915SLoGin let leader = current_pcb.thread.read().group_leader(); 445393f6915SLoGin if unlikely(leader.is_none()) { 446393f6915SLoGin panic!( 447393f6915SLoGin "fork: Failed to get leader of current process, current pid: [{:?}]", 448393f6915SLoGin current_pcb.pid() 449393f6915SLoGin ); 450393f6915SLoGin } 451393f6915SLoGin 452393f6915SLoGin pcb.exit_signal.store( 453393f6915SLoGin leader.unwrap().exit_signal.load(Ordering::SeqCst), 454393f6915SLoGin Ordering::SeqCst, 455393f6915SLoGin ); 456393f6915SLoGin } 457393f6915SLoGin } else { 458393f6915SLoGin // 新创建的进程,设置其父进程为当前进程 459393f6915SLoGin *pcb.real_parent_pcb.write() = Arc::downgrade(¤t_pcb); 460393f6915SLoGin pcb.exit_signal 461393f6915SLoGin .store(clone_args.exit_signal, Ordering::SeqCst); 462393f6915SLoGin } 463393f6915SLoGin 464393f6915SLoGin // todo: 增加线程组相关的逻辑。 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/kernel/fork.c#2437 465393f6915SLoGin 466971462beSGnoCiYeH Ok(()) 467971462beSGnoCiYeH } 4681496ba7bSLoGin } 469