11496ba7bSLoGin use alloc::{string::ToString, sync::Arc}; 2c8025a88Slogin 3c8025a88Slogin use crate::{ 4*971462beSGnoCiYeH arch::interrupt::TrapFrame, 5*971462beSGnoCiYeH filesystem::procfs::procfs_register_pid, 6*971462beSGnoCiYeH ipc::signal::flush_signal_handlers, 7*971462beSGnoCiYeH libs::rwlock::RwLock, 8*971462beSGnoCiYeH mm::VirtAddr, 9*971462beSGnoCiYeH process::ProcessFlags, 10*971462beSGnoCiYeH syscall::{user_access::UserBufferWriter, SystemError}, 11c8025a88Slogin }; 1266f67c6aSlogin 131496ba7bSLoGin use super::{ 141496ba7bSLoGin kthread::{KernelThreadPcbPrivate, WorkerPrivate}, 151496ba7bSLoGin KernelStack, Pid, ProcessControlBlock, ProcessManager, 161496ba7bSLoGin }; 171a2eaa40Slogin 181496ba7bSLoGin bitflags! { 191496ba7bSLoGin /// 进程克隆标志 20*971462beSGnoCiYeH pub struct CloneFlags: u64 { 211496ba7bSLoGin /// 在进程间共享虚拟内存空间 22*971462beSGnoCiYeH const CLONE_VM = 0x00000100; 23*971462beSGnoCiYeH /// 在进程间共享文件系统信息 24*971462beSGnoCiYeH const CLONE_FS = 0x00000200; 251496ba7bSLoGin /// 共享打开的文件 26*971462beSGnoCiYeH const CLONE_FILES = 0x00000400; 27*971462beSGnoCiYeH /// 克隆时,与父进程共享信号处理结构体 28*971462beSGnoCiYeH const CLONE_SIGHAND = 0x00000800; 29*971462beSGnoCiYeH /// 返回进程的文件描述符 30*971462beSGnoCiYeH const CLONE_PIDFD = 0x00001000; 31*971462beSGnoCiYeH /// 使克隆对象成为父进程的跟踪对象 32*971462beSGnoCiYeH const CLONE_PTRACE = 0x00002000; 33*971462beSGnoCiYeH /// 在执行 exec() 或 _exit() 之前挂起父进程的执行 34*971462beSGnoCiYeH const CLONE_VFORK = 0x00004000; 35*971462beSGnoCiYeH /// 使克隆对象的父进程为调用进程的父进程 36*971462beSGnoCiYeH const CLONE_PARENT = 0x00008000; 37*971462beSGnoCiYeH /// 拷贝线程 38*971462beSGnoCiYeH const CLONE_THREAD = 0x00010000; 39*971462beSGnoCiYeH /// 创建一个新的命名空间,其中包含独立的文件系统挂载点层次结构。 40*971462beSGnoCiYeH const CLONE_NEWNS = 0x00020000; 41*971462beSGnoCiYeH /// 与父进程共享 System V 信号量。 42*971462beSGnoCiYeH const CLONE_SYSVSEM = 0x00040000; 43*971462beSGnoCiYeH /// 设置其线程本地存储 44*971462beSGnoCiYeH const CLONE_SETTLS = 0x00080000; 45*971462beSGnoCiYeH /// 设置partent_tid地址为子进程线程 ID 46*971462beSGnoCiYeH const CLONE_PARENT_SETTID = 0x00100000; 47*971462beSGnoCiYeH /// 在子进程中设置一个清除线程 ID 的用户空间地址 48*971462beSGnoCiYeH const CLONE_CHILD_CLEARTID = 0x00200000; 49*971462beSGnoCiYeH /// 创建一个新线程,将其设置为分离状态 50*971462beSGnoCiYeH const CLONE_DETACHED = 0x00400000; 51*971462beSGnoCiYeH /// 使其在创建者进程或线程视角下成为无法跟踪的。 52*971462beSGnoCiYeH const CLONE_UNTRACED = 0x00800000; 53*971462beSGnoCiYeH /// 设置其子进程线程 ID 54*971462beSGnoCiYeH const CLONE_CHILD_SETTID = 0x01000000; 55*971462beSGnoCiYeH /// 将其放置在一个新的 cgroup 命名空间中 56*971462beSGnoCiYeH const CLONE_NEWCGROUP = 0x02000000; 57*971462beSGnoCiYeH /// 将其放置在一个新的 UTS 命名空间中 58*971462beSGnoCiYeH const CLONE_NEWUTS = 0x04000000; 59*971462beSGnoCiYeH /// 将其放置在一个新的 IPC 命名空间中 60*971462beSGnoCiYeH const CLONE_NEWIPC = 0x08000000; 61*971462beSGnoCiYeH /// 将其放置在一个新的用户命名空间中 62*971462beSGnoCiYeH const CLONE_NEWUSER = 0x10000000; 63*971462beSGnoCiYeH /// 将其放置在一个新的 PID 命名空间中 64*971462beSGnoCiYeH const CLONE_NEWPID = 0x20000000; 65*971462beSGnoCiYeH /// 将其放置在一个新的网络命名空间中 66*971462beSGnoCiYeH const CLONE_NEWNET = 0x40000000; 67*971462beSGnoCiYeH /// 在新的 I/O 上下文中运行它 68*971462beSGnoCiYeH const CLONE_IO = 0x80000000; 69*971462beSGnoCiYeH /// 克隆时,与父进程共享信号结构体 70*971462beSGnoCiYeH const CLONE_SIGNAL = 0x00010000 | 0x00000800; 71*971462beSGnoCiYeH /// 克隆时,将原本被设置为SIG_IGNORE的信号,设置回SIG_DEFAULT 72*971462beSGnoCiYeH const CLONE_CLEAR_SIGHAND = 0x100000000; 73*971462beSGnoCiYeH } 74*971462beSGnoCiYeH } 75*971462beSGnoCiYeH 76*971462beSGnoCiYeH /// ## clone与clone3系统调用的参数载体 77*971462beSGnoCiYeH /// 78*971462beSGnoCiYeH /// 因为这两个系统调用的参数很多,所以有这样一个载体更灵活 79*971462beSGnoCiYeH /// 80*971462beSGnoCiYeH /// 仅仅作为参数传递 81*971462beSGnoCiYeH #[derive(Debug, Clone, Copy)] 82*971462beSGnoCiYeH pub struct KernelCloneArgs { 83*971462beSGnoCiYeH pub flags: CloneFlags, 84*971462beSGnoCiYeH 85*971462beSGnoCiYeH // 下列属性均来自用户空间 86*971462beSGnoCiYeH pub pidfd: VirtAddr, 87*971462beSGnoCiYeH pub child_tid: VirtAddr, 88*971462beSGnoCiYeH pub parent_tid: VirtAddr, 89*971462beSGnoCiYeH pub set_tid: VirtAddr, 90*971462beSGnoCiYeH 91*971462beSGnoCiYeH pub exit_signal: i32, 92*971462beSGnoCiYeH 93*971462beSGnoCiYeH pub stack: usize, 94*971462beSGnoCiYeH // clone3用到 95*971462beSGnoCiYeH pub stack_size: usize, 96*971462beSGnoCiYeH pub tls: usize, 97*971462beSGnoCiYeH 98*971462beSGnoCiYeH pub set_tid_size: usize, 99*971462beSGnoCiYeH pub cgroup: i32, 100*971462beSGnoCiYeH 101*971462beSGnoCiYeH pub io_thread: bool, 102*971462beSGnoCiYeH pub kthread: bool, 103*971462beSGnoCiYeH pub idle: bool, 104*971462beSGnoCiYeH pub func: VirtAddr, 105*971462beSGnoCiYeH pub fn_arg: VirtAddr, 106*971462beSGnoCiYeH // cgrp 和 cset? 107*971462beSGnoCiYeH } 108*971462beSGnoCiYeH 109*971462beSGnoCiYeH impl KernelCloneArgs { 110*971462beSGnoCiYeH pub fn new() -> Self { 111*971462beSGnoCiYeH let null_addr = VirtAddr::new(0); 112*971462beSGnoCiYeH Self { 113*971462beSGnoCiYeH flags: unsafe { CloneFlags::from_bits_unchecked(0) }, 114*971462beSGnoCiYeH pidfd: null_addr, 115*971462beSGnoCiYeH child_tid: null_addr, 116*971462beSGnoCiYeH parent_tid: null_addr, 117*971462beSGnoCiYeH set_tid: null_addr, 118*971462beSGnoCiYeH exit_signal: 0, 119*971462beSGnoCiYeH stack: 0, 120*971462beSGnoCiYeH stack_size: 0, 121*971462beSGnoCiYeH tls: 0, 122*971462beSGnoCiYeH set_tid_size: 0, 123*971462beSGnoCiYeH cgroup: 0, 124*971462beSGnoCiYeH io_thread: false, 125*971462beSGnoCiYeH kthread: false, 126*971462beSGnoCiYeH idle: false, 127*971462beSGnoCiYeH func: null_addr, 128*971462beSGnoCiYeH fn_arg: null_addr, 129*971462beSGnoCiYeH } 130c8025a88Slogin } 13166f67c6aSlogin } 13266f67c6aSlogin 1331496ba7bSLoGin impl ProcessManager { 1341496ba7bSLoGin /// 创建一个新进程 1351496ba7bSLoGin /// 1361496ba7bSLoGin /// ## 参数 1371496ba7bSLoGin /// 1381496ba7bSLoGin /// - `current_trapframe`: 当前进程的trapframe 1391496ba7bSLoGin /// - `clone_flags`: 进程克隆标志 1401496ba7bSLoGin /// 1411496ba7bSLoGin /// ## 返回值 1421496ba7bSLoGin /// 1431496ba7bSLoGin /// - 成功:返回新进程的pid 1441496ba7bSLoGin /// - 失败:返回Err(SystemError),fork失败的话,子线程不会执行。 1451496ba7bSLoGin /// 1461496ba7bSLoGin /// ## Safety 1471496ba7bSLoGin /// 1481496ba7bSLoGin /// - fork失败的话,子线程不会执行。 1491496ba7bSLoGin pub fn fork( 1501496ba7bSLoGin current_trapframe: &mut TrapFrame, 1511496ba7bSLoGin clone_flags: CloneFlags, 1521496ba7bSLoGin ) -> Result<Pid, SystemError> { 1531496ba7bSLoGin let current_pcb = ProcessManager::current_pcb(); 1541496ba7bSLoGin let new_kstack = KernelStack::new()?; 1551496ba7bSLoGin let name = current_pcb.basic().name().to_string(); 1561496ba7bSLoGin let pcb = ProcessControlBlock::new(name, new_kstack); 1571496ba7bSLoGin 158*971462beSGnoCiYeH let mut args = KernelCloneArgs::new(); 159*971462beSGnoCiYeH args.flags = clone_flags; 160*971462beSGnoCiYeH Self::copy_process(¤t_pcb, &pcb, args, current_trapframe)?; 1611496ba7bSLoGin 1621496ba7bSLoGin ProcessManager::add_pcb(pcb.clone()); 1631496ba7bSLoGin 1641496ba7bSLoGin // 向procfs注册进程 1651496ba7bSLoGin procfs_register_pid(pcb.pid()).unwrap_or_else(|e| { 1661496ba7bSLoGin panic!( 1671496ba7bSLoGin "fork: Failed to register pid to procfs, pid: [{:?}]. Error: {:?}", 1681496ba7bSLoGin pcb.pid(), 1691496ba7bSLoGin e 1701496ba7bSLoGin ) 1711496ba7bSLoGin }); 1721496ba7bSLoGin 1731496ba7bSLoGin ProcessManager::wakeup(&pcb).unwrap_or_else(|e| { 1741496ba7bSLoGin panic!( 1751496ba7bSLoGin "fork: Failed to wakeup new process, pid: [{:?}]. Error: {:?}", 1761496ba7bSLoGin pcb.pid(), 1771496ba7bSLoGin e 1781496ba7bSLoGin ) 1791496ba7bSLoGin }); 1801496ba7bSLoGin 1811496ba7bSLoGin return Ok(pcb.pid()); 1821496ba7bSLoGin } 1831496ba7bSLoGin 1841496ba7bSLoGin fn copy_flags( 1851496ba7bSLoGin clone_flags: &CloneFlags, 1861496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 1871496ba7bSLoGin ) -> Result<(), SystemError> { 1881496ba7bSLoGin if clone_flags.contains(CloneFlags::CLONE_VM) { 1891496ba7bSLoGin new_pcb.flags().insert(ProcessFlags::VFORK); 1901496ba7bSLoGin } 1911496ba7bSLoGin *new_pcb.flags.lock() = ProcessManager::current_pcb().flags().clone(); 1921496ba7bSLoGin return Ok(()); 19366f67c6aSlogin } 19440fe15e0SLoGin 19540fe15e0SLoGin /// 拷贝进程的地址空间 19640fe15e0SLoGin /// 19740fe15e0SLoGin /// ## 参数 19840fe15e0SLoGin /// 19940fe15e0SLoGin /// - `clone_vm`: 是否与父进程共享地址空间。true表示共享 20040fe15e0SLoGin /// - `new_pcb`: 新进程的pcb 20140fe15e0SLoGin /// 20240fe15e0SLoGin /// ## 返回值 20340fe15e0SLoGin /// 20440fe15e0SLoGin /// - 成功:返回Ok(()) 20540fe15e0SLoGin /// - 失败:返回Err(SystemError) 20640fe15e0SLoGin /// 20740fe15e0SLoGin /// ## Panic 20840fe15e0SLoGin /// 20940fe15e0SLoGin /// - 如果当前进程没有用户地址空间,则panic 2101496ba7bSLoGin fn copy_mm( 2111496ba7bSLoGin clone_flags: &CloneFlags, 2121496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2131496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2141496ba7bSLoGin ) -> Result<(), SystemError> { 2151496ba7bSLoGin let old_address_space = current_pcb.basic().user_vm().unwrap_or_else(|| { 2161496ba7bSLoGin panic!( 2171496ba7bSLoGin "copy_mm: Failed to get address space of current process, current pid: [{:?}]", 2181496ba7bSLoGin current_pcb.pid() 2191496ba7bSLoGin ) 2201496ba7bSLoGin }); 22140fe15e0SLoGin 2221496ba7bSLoGin if clone_flags.contains(CloneFlags::CLONE_VM) { 2231496ba7bSLoGin unsafe { new_pcb.basic_mut().set_user_vm(Some(old_address_space)) }; 22440fe15e0SLoGin return Ok(()); 22540fe15e0SLoGin } 22640fe15e0SLoGin let new_address_space = old_address_space.write().try_clone().unwrap_or_else(|e| { 22740fe15e0SLoGin panic!( 2281496ba7bSLoGin "copy_mm: Failed to clone address space of current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 2291496ba7bSLoGin current_pcb.pid(), new_pcb.pid(), e 23040fe15e0SLoGin ) 23140fe15e0SLoGin }); 2321496ba7bSLoGin unsafe { new_pcb.basic_mut().set_user_vm(Some(new_address_space)) }; 23340fe15e0SLoGin return Ok(()); 23440fe15e0SLoGin } 2351496ba7bSLoGin 2361496ba7bSLoGin fn copy_files( 2371496ba7bSLoGin clone_flags: &CloneFlags, 2381496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2391496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2401496ba7bSLoGin ) -> Result<(), SystemError> { 2411496ba7bSLoGin // 如果不共享文件描述符表,则拷贝文件描述符表 2421496ba7bSLoGin if !clone_flags.contains(CloneFlags::CLONE_FILES) { 2431496ba7bSLoGin let new_fd_table = current_pcb.basic().fd_table().unwrap().read().clone(); 2441496ba7bSLoGin let new_fd_table = Arc::new(RwLock::new(new_fd_table)); 2451496ba7bSLoGin new_pcb.basic_mut().set_fd_table(Some(new_fd_table)); 246bb0e4d41SGnoCiYeH } else { 2471496ba7bSLoGin // 如果共享文件描述符表,则直接拷贝指针 2481496ba7bSLoGin new_pcb 2491496ba7bSLoGin .basic_mut() 2501496ba7bSLoGin .set_fd_table(current_pcb.basic().fd_table().clone()); 251bb0e4d41SGnoCiYeH } 2521496ba7bSLoGin 2531496ba7bSLoGin return Ok(()); 2541496ba7bSLoGin } 2551496ba7bSLoGin 2561496ba7bSLoGin #[allow(dead_code)] 2571496ba7bSLoGin fn copy_sighand( 2583c82aa56SChiichen clone_flags: &CloneFlags, 2593c82aa56SChiichen current_pcb: &Arc<ProcessControlBlock>, 2603c82aa56SChiichen new_pcb: &Arc<ProcessControlBlock>, 2611496ba7bSLoGin ) -> Result<(), SystemError> { 2623c82aa56SChiichen // // 将信号的处理函数设置为default(除了那些被手动屏蔽的) 2633c82aa56SChiichen if clone_flags.contains(CloneFlags::CLONE_CLEAR_SIGHAND) { 2643c82aa56SChiichen flush_signal_handlers(new_pcb.clone(), false); 2653c82aa56SChiichen } 2663c82aa56SChiichen 2673c82aa56SChiichen if clone_flags.contains(CloneFlags::CLONE_SIGHAND) { 2683c82aa56SChiichen (*new_pcb.sig_struct()).handlers = current_pcb.sig_struct().handlers.clone(); 2693c82aa56SChiichen } 2701496ba7bSLoGin return Ok(()); 2711496ba7bSLoGin } 272*971462beSGnoCiYeH 273*971462beSGnoCiYeH /// 拷贝进程信息 274*971462beSGnoCiYeH /// 275*971462beSGnoCiYeH /// ## panic: 276*971462beSGnoCiYeH /// 某一步拷贝失败时会引发panic 277*971462beSGnoCiYeH /// 例如:copy_mm等失败时会触发panic 278*971462beSGnoCiYeH /// 279*971462beSGnoCiYeH /// ## 参数 280*971462beSGnoCiYeH /// 281*971462beSGnoCiYeH /// - clone_flags 标志位 282*971462beSGnoCiYeH /// - des_pcb 目标pcb 283*971462beSGnoCiYeH /// - src_pcb 拷贝源pcb 284*971462beSGnoCiYeH /// 285*971462beSGnoCiYeH /// ## return 286*971462beSGnoCiYeH /// - 发生错误时返回Err(SystemError) 287*971462beSGnoCiYeH pub fn copy_process( 288*971462beSGnoCiYeH current_pcb: &Arc<ProcessControlBlock>, 289*971462beSGnoCiYeH pcb: &Arc<ProcessControlBlock>, 290*971462beSGnoCiYeH clone_args: KernelCloneArgs, 291*971462beSGnoCiYeH current_trapframe: &mut TrapFrame, 292*971462beSGnoCiYeH ) -> Result<(), SystemError> { 293*971462beSGnoCiYeH let clone_flags = clone_args.flags; 294*971462beSGnoCiYeH // 不允许与不同namespace的进程共享根目录 295*971462beSGnoCiYeH if (clone_flags == (CloneFlags::CLONE_NEWNS | CloneFlags::CLONE_FS)) 296*971462beSGnoCiYeH || clone_flags == (CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_FS) 297*971462beSGnoCiYeH { 298*971462beSGnoCiYeH return Err(SystemError::EINVAL); 299*971462beSGnoCiYeH } 300*971462beSGnoCiYeH 301*971462beSGnoCiYeH // 线程组必须共享信号,分离线程只能在线程组内启动。 302*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD) 303*971462beSGnoCiYeH && !clone_flags.contains(CloneFlags::CLONE_SIGHAND) 304*971462beSGnoCiYeH { 305*971462beSGnoCiYeH return Err(SystemError::EINVAL); 306*971462beSGnoCiYeH } 307*971462beSGnoCiYeH 308*971462beSGnoCiYeH // 共享信号处理器意味着共享vm。 309*971462beSGnoCiYeH // 线程组也意味着共享vm。阻止这种情况可以简化其他代码。 310*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_SIGHAND) 311*971462beSGnoCiYeH && !clone_flags.contains(CloneFlags::CLONE_VM) 312*971462beSGnoCiYeH { 313*971462beSGnoCiYeH return Err(SystemError::EINVAL); 314*971462beSGnoCiYeH } 315*971462beSGnoCiYeH 316*971462beSGnoCiYeH // TODO: 处理CLONE_PARENT 与 SIGNAL_UNKILLABLE的情况 317*971462beSGnoCiYeH 318*971462beSGnoCiYeH // 如果新进程使用不同的 pid 或 namespace, 319*971462beSGnoCiYeH // 则不允许它与分叉任务共享线程组。 320*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD) { 321*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWPID) { 322*971462beSGnoCiYeH return Err(SystemError::EINVAL); 323*971462beSGnoCiYeH } 324*971462beSGnoCiYeH // TODO: 判断新进程与当前进程namespace是否相同,不同则返回错误 325*971462beSGnoCiYeH } 326*971462beSGnoCiYeH 327*971462beSGnoCiYeH // 如果新进程将处于不同的time namespace, 328*971462beSGnoCiYeH // 则不能让它共享vm或线程组。 329*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM) { 330*971462beSGnoCiYeH // TODO: 判断time namespace,不同则返回错误 331*971462beSGnoCiYeH } 332*971462beSGnoCiYeH 333*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_PIDFD) 334*971462beSGnoCiYeH && clone_flags.contains(CloneFlags::CLONE_DETACHED | CloneFlags::CLONE_THREAD) 335*971462beSGnoCiYeH { 336*971462beSGnoCiYeH return Err(SystemError::EINVAL); 337*971462beSGnoCiYeH } 338*971462beSGnoCiYeH 339*971462beSGnoCiYeH // TODO: 克隆前应该锁信号处理,等待克隆完成后再处理 340*971462beSGnoCiYeH 341*971462beSGnoCiYeH // 克隆架构相关 342*971462beSGnoCiYeH *pcb.arch_info() = current_pcb.arch_info_irqsave().clone(); 343*971462beSGnoCiYeH 344*971462beSGnoCiYeH // 为内核线程设置WorkerPrivate 345*971462beSGnoCiYeH if current_pcb.flags().contains(ProcessFlags::KTHREAD) { 346*971462beSGnoCiYeH *pcb.worker_private() = 347*971462beSGnoCiYeH Some(WorkerPrivate::KernelThread(KernelThreadPcbPrivate::new())); 348*971462beSGnoCiYeH } 349*971462beSGnoCiYeH 350*971462beSGnoCiYeH // 设置clear_child_tid,在线程结束时将其置0以通知父进程 351*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_CHILD_CLEARTID) { 352*971462beSGnoCiYeH pcb.thread.write().clear_child_tid = Some(clone_args.child_tid); 353*971462beSGnoCiYeH } 354*971462beSGnoCiYeH 355*971462beSGnoCiYeH // 设置child_tid,意味着子线程能够知道自己的id 356*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_CHILD_SETTID) { 357*971462beSGnoCiYeH pcb.thread.write().set_child_tid = Some(clone_args.child_tid); 358*971462beSGnoCiYeH } 359*971462beSGnoCiYeH 360*971462beSGnoCiYeH // 将子进程/线程的id存储在用户态传进的地址中 361*971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_PARENT_SETTID) { 362*971462beSGnoCiYeH let mut writer = UserBufferWriter::new( 363*971462beSGnoCiYeH clone_args.parent_tid.data() as *mut i32, 364*971462beSGnoCiYeH core::mem::size_of::<i32>(), 365*971462beSGnoCiYeH true, 366*971462beSGnoCiYeH )?; 367*971462beSGnoCiYeH 368*971462beSGnoCiYeH writer.copy_one_to_user(&(pcb.pid().0 as i32), 0)?; 369*971462beSGnoCiYeH } 370*971462beSGnoCiYeH 371*971462beSGnoCiYeH // 拷贝标志位 372*971462beSGnoCiYeH Self::copy_flags(&clone_flags, &pcb).unwrap_or_else(|e| { 373*971462beSGnoCiYeH panic!( 374*971462beSGnoCiYeH "fork: Failed to copy flags from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 375*971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 376*971462beSGnoCiYeH ) 377*971462beSGnoCiYeH }); 378*971462beSGnoCiYeH 379*971462beSGnoCiYeH // 拷贝用户地址空间 380*971462beSGnoCiYeH Self::copy_mm(&clone_flags, ¤t_pcb, &pcb).unwrap_or_else(|e| { 381*971462beSGnoCiYeH panic!( 382*971462beSGnoCiYeH "fork: Failed to copy mm from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 383*971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 384*971462beSGnoCiYeH ) 385*971462beSGnoCiYeH }); 386*971462beSGnoCiYeH 387*971462beSGnoCiYeH // 拷贝文件描述符表 388*971462beSGnoCiYeH Self::copy_files(&clone_flags, ¤t_pcb, &pcb).unwrap_or_else(|e| { 389*971462beSGnoCiYeH panic!( 390*971462beSGnoCiYeH "fork: Failed to copy files from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 391*971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 392*971462beSGnoCiYeH ) 393*971462beSGnoCiYeH }); 394*971462beSGnoCiYeH 395*971462beSGnoCiYeH // todo: 拷贝信号相关数据 396*971462beSGnoCiYeH 397*971462beSGnoCiYeH // 拷贝线程 398*971462beSGnoCiYeH Self::copy_thread(¤t_pcb, &pcb, clone_args,¤t_trapframe).unwrap_or_else(|e| { 399*971462beSGnoCiYeH panic!( 400*971462beSGnoCiYeH "fork: Failed to copy thread from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 401*971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 402*971462beSGnoCiYeH ) 403*971462beSGnoCiYeH }); 404*971462beSGnoCiYeH 405*971462beSGnoCiYeH Ok(()) 406*971462beSGnoCiYeH } 4071496ba7bSLoGin } 408