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