1393f6915SLoGin use core::{intrinsics::unlikely, sync::atomic::Ordering}; 2393f6915SLoGin 31496ba7bSLoGin use alloc::{string::ToString, sync::Arc}; 4*2eab6dd7S曾俊 use log::error; 591e9d4abSLoGin use system_error::SystemError; 6c8025a88Slogin 7c8025a88Slogin use crate::{ 8393f6915SLoGin arch::{interrupt::TrapFrame, ipc::signal::Signal}, 9971462beSGnoCiYeH filesystem::procfs::procfs_register_pid, 10971462beSGnoCiYeH ipc::signal::flush_signal_handlers, 11971462beSGnoCiYeH libs::rwlock::RwLock, 12971462beSGnoCiYeH mm::VirtAddr, 13971462beSGnoCiYeH process::ProcessFlags, 14f0c87a89SGnoCiYeH sched::{sched_cgroup_fork, sched_fork}, 15f0c87a89SGnoCiYeH smp::core::smp_get_processor_id, 1691e9d4abSLoGin syscall::user_access::UserBufferWriter, 17c8025a88Slogin }; 1866f67c6aSlogin 191496ba7bSLoGin use super::{ 201496ba7bSLoGin kthread::{KernelThreadPcbPrivate, WorkerPrivate}, 211496ba7bSLoGin KernelStack, Pid, ProcessControlBlock, ProcessManager, 221496ba7bSLoGin }; 231a2eaa40Slogin 241496ba7bSLoGin bitflags! { 251496ba7bSLoGin /// 进程克隆标志 26971462beSGnoCiYeH pub struct CloneFlags: u64 { 271496ba7bSLoGin /// 在进程间共享虚拟内存空间 28971462beSGnoCiYeH const CLONE_VM = 0x00000100; 29971462beSGnoCiYeH /// 在进程间共享文件系统信息 30971462beSGnoCiYeH const CLONE_FS = 0x00000200; 311496ba7bSLoGin /// 共享打开的文件 32971462beSGnoCiYeH const CLONE_FILES = 0x00000400; 33971462beSGnoCiYeH /// 克隆时,与父进程共享信号处理结构体 34971462beSGnoCiYeH const CLONE_SIGHAND = 0x00000800; 35971462beSGnoCiYeH /// 返回进程的文件描述符 36971462beSGnoCiYeH const CLONE_PIDFD = 0x00001000; 37971462beSGnoCiYeH /// 使克隆对象成为父进程的跟踪对象 38971462beSGnoCiYeH const CLONE_PTRACE = 0x00002000; 39971462beSGnoCiYeH /// 在执行 exec() 或 _exit() 之前挂起父进程的执行 40971462beSGnoCiYeH const CLONE_VFORK = 0x00004000; 41971462beSGnoCiYeH /// 使克隆对象的父进程为调用进程的父进程 42971462beSGnoCiYeH const CLONE_PARENT = 0x00008000; 43971462beSGnoCiYeH /// 拷贝线程 44971462beSGnoCiYeH const CLONE_THREAD = 0x00010000; 45971462beSGnoCiYeH /// 创建一个新的命名空间,其中包含独立的文件系统挂载点层次结构。 46971462beSGnoCiYeH const CLONE_NEWNS = 0x00020000; 47971462beSGnoCiYeH /// 与父进程共享 System V 信号量。 48971462beSGnoCiYeH const CLONE_SYSVSEM = 0x00040000; 49971462beSGnoCiYeH /// 设置其线程本地存储 50971462beSGnoCiYeH const CLONE_SETTLS = 0x00080000; 51971462beSGnoCiYeH /// 设置partent_tid地址为子进程线程 ID 52971462beSGnoCiYeH const CLONE_PARENT_SETTID = 0x00100000; 53971462beSGnoCiYeH /// 在子进程中设置一个清除线程 ID 的用户空间地址 54971462beSGnoCiYeH const CLONE_CHILD_CLEARTID = 0x00200000; 55971462beSGnoCiYeH /// 创建一个新线程,将其设置为分离状态 56971462beSGnoCiYeH const CLONE_DETACHED = 0x00400000; 57971462beSGnoCiYeH /// 使其在创建者进程或线程视角下成为无法跟踪的。 58971462beSGnoCiYeH const CLONE_UNTRACED = 0x00800000; 59971462beSGnoCiYeH /// 设置其子进程线程 ID 60971462beSGnoCiYeH const CLONE_CHILD_SETTID = 0x01000000; 61971462beSGnoCiYeH /// 将其放置在一个新的 cgroup 命名空间中 62971462beSGnoCiYeH const CLONE_NEWCGROUP = 0x02000000; 63971462beSGnoCiYeH /// 将其放置在一个新的 UTS 命名空间中 64971462beSGnoCiYeH const CLONE_NEWUTS = 0x04000000; 65971462beSGnoCiYeH /// 将其放置在一个新的 IPC 命名空间中 66971462beSGnoCiYeH const CLONE_NEWIPC = 0x08000000; 67971462beSGnoCiYeH /// 将其放置在一个新的用户命名空间中 68971462beSGnoCiYeH const CLONE_NEWUSER = 0x10000000; 69971462beSGnoCiYeH /// 将其放置在一个新的 PID 命名空间中 70971462beSGnoCiYeH const CLONE_NEWPID = 0x20000000; 71971462beSGnoCiYeH /// 将其放置在一个新的网络命名空间中 72971462beSGnoCiYeH const CLONE_NEWNET = 0x40000000; 73971462beSGnoCiYeH /// 在新的 I/O 上下文中运行它 74971462beSGnoCiYeH const CLONE_IO = 0x80000000; 75971462beSGnoCiYeH /// 克隆时,与父进程共享信号结构体 76971462beSGnoCiYeH const CLONE_SIGNAL = 0x00010000 | 0x00000800; 77971462beSGnoCiYeH /// 克隆时,将原本被设置为SIG_IGNORE的信号,设置回SIG_DEFAULT 78971462beSGnoCiYeH const CLONE_CLEAR_SIGHAND = 0x100000000; 79971462beSGnoCiYeH } 80971462beSGnoCiYeH } 81971462beSGnoCiYeH 82971462beSGnoCiYeH /// ## clone与clone3系统调用的参数载体 83971462beSGnoCiYeH /// 84971462beSGnoCiYeH /// 因为这两个系统调用的参数很多,所以有这样一个载体更灵活 85971462beSGnoCiYeH /// 86971462beSGnoCiYeH /// 仅仅作为参数传递 87971462beSGnoCiYeH #[derive(Debug, Clone, Copy)] 88971462beSGnoCiYeH pub struct KernelCloneArgs { 89971462beSGnoCiYeH pub flags: CloneFlags, 90971462beSGnoCiYeH 91971462beSGnoCiYeH // 下列属性均来自用户空间 92971462beSGnoCiYeH pub pidfd: VirtAddr, 93971462beSGnoCiYeH pub child_tid: VirtAddr, 94971462beSGnoCiYeH pub parent_tid: VirtAddr, 95971462beSGnoCiYeH pub set_tid: VirtAddr, 96971462beSGnoCiYeH 97393f6915SLoGin /// 进程退出时发送的信号 98393f6915SLoGin pub exit_signal: Signal, 99971462beSGnoCiYeH 100971462beSGnoCiYeH pub stack: usize, 101971462beSGnoCiYeH // clone3用到 102971462beSGnoCiYeH pub stack_size: usize, 103971462beSGnoCiYeH pub tls: usize, 104971462beSGnoCiYeH 105971462beSGnoCiYeH pub set_tid_size: usize, 106971462beSGnoCiYeH pub cgroup: i32, 107971462beSGnoCiYeH 108971462beSGnoCiYeH pub io_thread: bool, 109971462beSGnoCiYeH pub kthread: bool, 110971462beSGnoCiYeH pub idle: bool, 111971462beSGnoCiYeH pub func: VirtAddr, 112971462beSGnoCiYeH pub fn_arg: VirtAddr, 113971462beSGnoCiYeH // cgrp 和 cset? 114971462beSGnoCiYeH } 115971462beSGnoCiYeH 116971462beSGnoCiYeH impl KernelCloneArgs { 117971462beSGnoCiYeH pub fn new() -> Self { 118971462beSGnoCiYeH let null_addr = VirtAddr::new(0); 119971462beSGnoCiYeH Self { 120971462beSGnoCiYeH flags: unsafe { CloneFlags::from_bits_unchecked(0) }, 121971462beSGnoCiYeH pidfd: null_addr, 122971462beSGnoCiYeH child_tid: null_addr, 123971462beSGnoCiYeH parent_tid: null_addr, 124971462beSGnoCiYeH set_tid: null_addr, 125393f6915SLoGin exit_signal: Signal::SIGCHLD, 126971462beSGnoCiYeH stack: 0, 127971462beSGnoCiYeH stack_size: 0, 128971462beSGnoCiYeH tls: 0, 129971462beSGnoCiYeH set_tid_size: 0, 130971462beSGnoCiYeH cgroup: 0, 131971462beSGnoCiYeH io_thread: false, 132971462beSGnoCiYeH kthread: false, 133971462beSGnoCiYeH idle: false, 134971462beSGnoCiYeH func: null_addr, 135971462beSGnoCiYeH fn_arg: null_addr, 136971462beSGnoCiYeH } 137c8025a88Slogin } 13866f67c6aSlogin } 13966f67c6aSlogin 1401496ba7bSLoGin impl ProcessManager { 1411496ba7bSLoGin /// 创建一个新进程 1421496ba7bSLoGin /// 1431496ba7bSLoGin /// ## 参数 1441496ba7bSLoGin /// 1451496ba7bSLoGin /// - `current_trapframe`: 当前进程的trapframe 1461496ba7bSLoGin /// - `clone_flags`: 进程克隆标志 1471496ba7bSLoGin /// 1481496ba7bSLoGin /// ## 返回值 1491496ba7bSLoGin /// 1501496ba7bSLoGin /// - 成功:返回新进程的pid 1511496ba7bSLoGin /// - 失败:返回Err(SystemError),fork失败的话,子线程不会执行。 1521496ba7bSLoGin /// 1531496ba7bSLoGin /// ## Safety 1541496ba7bSLoGin /// 1551496ba7bSLoGin /// - fork失败的话,子线程不会执行。 1561496ba7bSLoGin pub fn fork( 157840045afSLoGin current_trapframe: &TrapFrame, 1581496ba7bSLoGin clone_flags: CloneFlags, 1591496ba7bSLoGin ) -> Result<Pid, SystemError> { 1601496ba7bSLoGin let current_pcb = ProcessManager::current_pcb(); 1614fda81ceSLoGin 1624fda81ceSLoGin let new_kstack: KernelStack = KernelStack::new()?; 1634fda81ceSLoGin 1641496ba7bSLoGin let name = current_pcb.basic().name().to_string(); 1650d6cf65aSLoGin 1661496ba7bSLoGin let pcb = ProcessControlBlock::new(name, new_kstack); 1671496ba7bSLoGin 168971462beSGnoCiYeH let mut args = KernelCloneArgs::new(); 169971462beSGnoCiYeH args.flags = clone_flags; 170393f6915SLoGin args.exit_signal = Signal::SIGCHLD; 17159fdb447SLoGin Self::copy_process(¤t_pcb, &pcb, args, current_trapframe).map_err(|e| { 172*2eab6dd7S曾俊 error!( 17359fdb447SLoGin "fork: Failed to copy process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 17459fdb447SLoGin current_pcb.pid(), 17559fdb447SLoGin pcb.pid(), 17659fdb447SLoGin e 17759fdb447SLoGin ); 17859fdb447SLoGin e 17959fdb447SLoGin })?; 1801496ba7bSLoGin ProcessManager::add_pcb(pcb.clone()); 1811496ba7bSLoGin 1821496ba7bSLoGin // 向procfs注册进程 1831496ba7bSLoGin procfs_register_pid(pcb.pid()).unwrap_or_else(|e| { 1841496ba7bSLoGin panic!( 1851496ba7bSLoGin "fork: Failed to register pid to procfs, pid: [{:?}]. Error: {:?}", 1861496ba7bSLoGin pcb.pid(), 1871496ba7bSLoGin e 1881496ba7bSLoGin ) 1891496ba7bSLoGin }); 1901496ba7bSLoGin 191f0c87a89SGnoCiYeH pcb.sched_info().set_on_cpu(Some(smp_get_processor_id())); 192f0c87a89SGnoCiYeH 1931496ba7bSLoGin ProcessManager::wakeup(&pcb).unwrap_or_else(|e| { 1941496ba7bSLoGin panic!( 1951496ba7bSLoGin "fork: Failed to wakeup new process, pid: [{:?}]. Error: {:?}", 1961496ba7bSLoGin pcb.pid(), 1971496ba7bSLoGin e 1981496ba7bSLoGin ) 1991496ba7bSLoGin }); 2001496ba7bSLoGin 2011496ba7bSLoGin return Ok(pcb.pid()); 2021496ba7bSLoGin } 2031496ba7bSLoGin 2041496ba7bSLoGin fn copy_flags( 2051496ba7bSLoGin clone_flags: &CloneFlags, 2061496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2071496ba7bSLoGin ) -> Result<(), SystemError> { 2081496ba7bSLoGin if clone_flags.contains(CloneFlags::CLONE_VM) { 2091496ba7bSLoGin new_pcb.flags().insert(ProcessFlags::VFORK); 2101496ba7bSLoGin } 211b5b571e0SLoGin *new_pcb.flags.get_mut() = *ProcessManager::current_pcb().flags(); 2121496ba7bSLoGin return Ok(()); 21366f67c6aSlogin } 21440fe15e0SLoGin 21540fe15e0SLoGin /// 拷贝进程的地址空间 21640fe15e0SLoGin /// 21740fe15e0SLoGin /// ## 参数 21840fe15e0SLoGin /// 21940fe15e0SLoGin /// - `clone_vm`: 是否与父进程共享地址空间。true表示共享 22040fe15e0SLoGin /// - `new_pcb`: 新进程的pcb 22140fe15e0SLoGin /// 22240fe15e0SLoGin /// ## 返回值 22340fe15e0SLoGin /// 22440fe15e0SLoGin /// - 成功:返回Ok(()) 22540fe15e0SLoGin /// - 失败:返回Err(SystemError) 22640fe15e0SLoGin /// 22740fe15e0SLoGin /// ## Panic 22840fe15e0SLoGin /// 22940fe15e0SLoGin /// - 如果当前进程没有用户地址空间,则panic 2304fda81ceSLoGin #[inline(never)] 2311496ba7bSLoGin fn copy_mm( 2321496ba7bSLoGin clone_flags: &CloneFlags, 2331496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2341496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2351496ba7bSLoGin ) -> Result<(), SystemError> { 2361496ba7bSLoGin let old_address_space = current_pcb.basic().user_vm().unwrap_or_else(|| { 2371496ba7bSLoGin panic!( 2381496ba7bSLoGin "copy_mm: Failed to get address space of current process, current pid: [{:?}]", 2391496ba7bSLoGin current_pcb.pid() 2401496ba7bSLoGin ) 2411496ba7bSLoGin }); 24240fe15e0SLoGin 2431496ba7bSLoGin if clone_flags.contains(CloneFlags::CLONE_VM) { 2441496ba7bSLoGin unsafe { new_pcb.basic_mut().set_user_vm(Some(old_address_space)) }; 24540fe15e0SLoGin return Ok(()); 24640fe15e0SLoGin } 2470d6cf65aSLoGin let new_address_space = old_address_space.write_irqsave().try_clone().unwrap_or_else(|e| { 24840fe15e0SLoGin panic!( 2491496ba7bSLoGin "copy_mm: Failed to clone address space of current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 2501496ba7bSLoGin current_pcb.pid(), new_pcb.pid(), e 25140fe15e0SLoGin ) 25240fe15e0SLoGin }); 2531496ba7bSLoGin unsafe { new_pcb.basic_mut().set_user_vm(Some(new_address_space)) }; 25440fe15e0SLoGin return Ok(()); 25540fe15e0SLoGin } 2561496ba7bSLoGin 2570d6cf65aSLoGin #[inline(never)] 2581496ba7bSLoGin fn copy_files( 2591496ba7bSLoGin clone_flags: &CloneFlags, 2601496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2611496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2621496ba7bSLoGin ) -> Result<(), SystemError> { 2631496ba7bSLoGin // 如果不共享文件描述符表,则拷贝文件描述符表 2641496ba7bSLoGin if !clone_flags.contains(CloneFlags::CLONE_FILES) { 2651496ba7bSLoGin let new_fd_table = current_pcb.basic().fd_table().unwrap().read().clone(); 2661496ba7bSLoGin let new_fd_table = Arc::new(RwLock::new(new_fd_table)); 2671496ba7bSLoGin new_pcb.basic_mut().set_fd_table(Some(new_fd_table)); 268bb0e4d41SGnoCiYeH } else { 2691496ba7bSLoGin // 如果共享文件描述符表,则直接拷贝指针 2701496ba7bSLoGin new_pcb 2711496ba7bSLoGin .basic_mut() 2721496ba7bSLoGin .set_fd_table(current_pcb.basic().fd_table().clone()); 273bb0e4d41SGnoCiYeH } 2741496ba7bSLoGin 2751496ba7bSLoGin return Ok(()); 2761496ba7bSLoGin } 2771496ba7bSLoGin 2781496ba7bSLoGin #[allow(dead_code)] 2791496ba7bSLoGin fn copy_sighand( 2803c82aa56SChiichen clone_flags: &CloneFlags, 2813c82aa56SChiichen current_pcb: &Arc<ProcessControlBlock>, 2823c82aa56SChiichen new_pcb: &Arc<ProcessControlBlock>, 2831496ba7bSLoGin ) -> Result<(), SystemError> { 2843c82aa56SChiichen // // 将信号的处理函数设置为default(除了那些被手动屏蔽的) 2853c82aa56SChiichen if clone_flags.contains(CloneFlags::CLONE_CLEAR_SIGHAND) { 2863c82aa56SChiichen flush_signal_handlers(new_pcb.clone(), false); 2873c82aa56SChiichen } 2883c82aa56SChiichen 2893c82aa56SChiichen if clone_flags.contains(CloneFlags::CLONE_SIGHAND) { 290b5b571e0SLoGin new_pcb.sig_struct_irqsave().handlers = current_pcb.sig_struct_irqsave().handlers; 2913c82aa56SChiichen } 2921496ba7bSLoGin return Ok(()); 2931496ba7bSLoGin } 294971462beSGnoCiYeH 295971462beSGnoCiYeH /// 拷贝进程信息 296971462beSGnoCiYeH /// 297971462beSGnoCiYeH /// ## panic: 298971462beSGnoCiYeH /// 某一步拷贝失败时会引发panic 299971462beSGnoCiYeH /// 例如:copy_mm等失败时会触发panic 300971462beSGnoCiYeH /// 301971462beSGnoCiYeH /// ## 参数 302971462beSGnoCiYeH /// 303971462beSGnoCiYeH /// - clone_flags 标志位 3040d6cf65aSLoGin /// - current_pcb 拷贝源pcb 3050d6cf65aSLoGin /// - pcb 目标pcb 306971462beSGnoCiYeH /// 307971462beSGnoCiYeH /// ## return 308971462beSGnoCiYeH /// - 发生错误时返回Err(SystemError) 3094fda81ceSLoGin #[inline(never)] 310971462beSGnoCiYeH pub fn copy_process( 311971462beSGnoCiYeH current_pcb: &Arc<ProcessControlBlock>, 312971462beSGnoCiYeH pcb: &Arc<ProcessControlBlock>, 313971462beSGnoCiYeH clone_args: KernelCloneArgs, 314840045afSLoGin current_trapframe: &TrapFrame, 315971462beSGnoCiYeH ) -> Result<(), SystemError> { 316971462beSGnoCiYeH let clone_flags = clone_args.flags; 317971462beSGnoCiYeH // 不允许与不同namespace的进程共享根目录 318971462beSGnoCiYeH if (clone_flags == (CloneFlags::CLONE_NEWNS | CloneFlags::CLONE_FS)) 319971462beSGnoCiYeH || clone_flags == (CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_FS) 320971462beSGnoCiYeH { 321971462beSGnoCiYeH return Err(SystemError::EINVAL); 322971462beSGnoCiYeH } 323971462beSGnoCiYeH 324971462beSGnoCiYeH // 线程组必须共享信号,分离线程只能在线程组内启动。 325971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD) 326971462beSGnoCiYeH && !clone_flags.contains(CloneFlags::CLONE_SIGHAND) 327971462beSGnoCiYeH { 328971462beSGnoCiYeH return Err(SystemError::EINVAL); 329971462beSGnoCiYeH } 330971462beSGnoCiYeH 331971462beSGnoCiYeH // 共享信号处理器意味着共享vm。 332971462beSGnoCiYeH // 线程组也意味着共享vm。阻止这种情况可以简化其他代码。 333971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_SIGHAND) 334971462beSGnoCiYeH && !clone_flags.contains(CloneFlags::CLONE_VM) 335971462beSGnoCiYeH { 336971462beSGnoCiYeH return Err(SystemError::EINVAL); 337971462beSGnoCiYeH } 338971462beSGnoCiYeH 339971462beSGnoCiYeH // TODO: 处理CLONE_PARENT 与 SIGNAL_UNKILLABLE的情况 340971462beSGnoCiYeH 341971462beSGnoCiYeH // 如果新进程使用不同的 pid 或 namespace, 342971462beSGnoCiYeH // 则不允许它与分叉任务共享线程组。 343b5b571e0SLoGin if clone_flags.contains(CloneFlags::CLONE_THREAD) 344b5b571e0SLoGin && clone_flags.contains(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWPID) 345b5b571e0SLoGin { 346971462beSGnoCiYeH return Err(SystemError::EINVAL); 347971462beSGnoCiYeH // TODO: 判断新进程与当前进程namespace是否相同,不同则返回错误 348971462beSGnoCiYeH } 349971462beSGnoCiYeH 350971462beSGnoCiYeH // 如果新进程将处于不同的time namespace, 351971462beSGnoCiYeH // 则不能让它共享vm或线程组。 352971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM) { 353971462beSGnoCiYeH // TODO: 判断time namespace,不同则返回错误 354971462beSGnoCiYeH } 355971462beSGnoCiYeH 356971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_PIDFD) 357971462beSGnoCiYeH && clone_flags.contains(CloneFlags::CLONE_DETACHED | CloneFlags::CLONE_THREAD) 358971462beSGnoCiYeH { 359971462beSGnoCiYeH return Err(SystemError::EINVAL); 360971462beSGnoCiYeH } 361971462beSGnoCiYeH 362971462beSGnoCiYeH // TODO: 克隆前应该锁信号处理,等待克隆完成后再处理 363971462beSGnoCiYeH 364971462beSGnoCiYeH // 克隆架构相关 36516033951SGnoCiYeH let guard = current_pcb.arch_info_irqsave(); 3660d6cf65aSLoGin unsafe { pcb.arch_info().clone_from(&guard) }; 36716033951SGnoCiYeH drop(guard); 368971462beSGnoCiYeH 369971462beSGnoCiYeH // 为内核线程设置WorkerPrivate 370971462beSGnoCiYeH if current_pcb.flags().contains(ProcessFlags::KTHREAD) { 371971462beSGnoCiYeH *pcb.worker_private() = 372971462beSGnoCiYeH Some(WorkerPrivate::KernelThread(KernelThreadPcbPrivate::new())); 373971462beSGnoCiYeH } 374971462beSGnoCiYeH 375971462beSGnoCiYeH // 设置clear_child_tid,在线程结束时将其置0以通知父进程 376971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_CHILD_CLEARTID) { 37752bcb59eSGnoCiYeH pcb.thread.write_irqsave().clear_child_tid = Some(clone_args.child_tid); 378971462beSGnoCiYeH } 379971462beSGnoCiYeH 380971462beSGnoCiYeH // 设置child_tid,意味着子线程能够知道自己的id 381971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_CHILD_SETTID) { 38252bcb59eSGnoCiYeH pcb.thread.write_irqsave().set_child_tid = Some(clone_args.child_tid); 383971462beSGnoCiYeH } 384971462beSGnoCiYeH 385971462beSGnoCiYeH // 将子进程/线程的id存储在用户态传进的地址中 386971462beSGnoCiYeH if clone_flags.contains(CloneFlags::CLONE_PARENT_SETTID) { 387971462beSGnoCiYeH let mut writer = UserBufferWriter::new( 388971462beSGnoCiYeH clone_args.parent_tid.data() as *mut i32, 389971462beSGnoCiYeH core::mem::size_of::<i32>(), 390971462beSGnoCiYeH true, 391971462beSGnoCiYeH )?; 392971462beSGnoCiYeH 393971462beSGnoCiYeH writer.copy_one_to_user(&(pcb.pid().0 as i32), 0)?; 394971462beSGnoCiYeH } 395971462beSGnoCiYeH 396f0c87a89SGnoCiYeH sched_fork(pcb).unwrap_or_else(|e| { 397f0c87a89SGnoCiYeH panic!( 398f0c87a89SGnoCiYeH "fork: Failed to set sched info from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 399f0c87a89SGnoCiYeH current_pcb.pid(), pcb.pid(), e 400f0c87a89SGnoCiYeH ) 401f0c87a89SGnoCiYeH }); 402f0c87a89SGnoCiYeH 403971462beSGnoCiYeH // 拷贝标志位 404b5b571e0SLoGin Self::copy_flags(&clone_flags, pcb).unwrap_or_else(|e| { 405971462beSGnoCiYeH panic!( 406971462beSGnoCiYeH "fork: Failed to copy flags from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 407971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 408971462beSGnoCiYeH ) 409971462beSGnoCiYeH }); 410971462beSGnoCiYeH 411971462beSGnoCiYeH // 拷贝用户地址空间 412b5b571e0SLoGin Self::copy_mm(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| { 413971462beSGnoCiYeH panic!( 414971462beSGnoCiYeH "fork: Failed to copy mm from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 415971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 416971462beSGnoCiYeH ) 417971462beSGnoCiYeH }); 418971462beSGnoCiYeH 419971462beSGnoCiYeH // 拷贝文件描述符表 420b5b571e0SLoGin Self::copy_files(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| { 421971462beSGnoCiYeH panic!( 422971462beSGnoCiYeH "fork: Failed to copy files from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 423971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 424971462beSGnoCiYeH ) 425971462beSGnoCiYeH }); 426971462beSGnoCiYeH 427393f6915SLoGin // 拷贝信号相关数据 428b5b571e0SLoGin Self::copy_sighand(&clone_flags, current_pcb, pcb).unwrap_or_else(|e| { 429393f6915SLoGin panic!( 430393f6915SLoGin "fork: Failed to copy sighand from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 431393f6915SLoGin current_pcb.pid(), pcb.pid(), e 432393f6915SLoGin ) 433b5b571e0SLoGin }); 434971462beSGnoCiYeH 435971462beSGnoCiYeH // 拷贝线程 436b5b571e0SLoGin Self::copy_thread(current_pcb, pcb, clone_args,current_trapframe).unwrap_or_else(|e| { 437971462beSGnoCiYeH panic!( 438971462beSGnoCiYeH "fork: Failed to copy thread from current process, current pid: [{:?}], new pid: [{:?}]. Error: {:?}", 439971462beSGnoCiYeH current_pcb.pid(), pcb.pid(), e 440971462beSGnoCiYeH ) 441971462beSGnoCiYeH }); 442971462beSGnoCiYeH 443393f6915SLoGin // 设置线程组id、组长 444393f6915SLoGin if clone_flags.contains(CloneFlags::CLONE_THREAD) { 44552bcb59eSGnoCiYeH pcb.thread.write_irqsave().group_leader = 44652bcb59eSGnoCiYeH current_pcb.thread.read_irqsave().group_leader.clone(); 447393f6915SLoGin unsafe { 448393f6915SLoGin let ptr = pcb.as_ref() as *const ProcessControlBlock as *mut ProcessControlBlock; 449393f6915SLoGin (*ptr).tgid = current_pcb.tgid; 450393f6915SLoGin } 451393f6915SLoGin } else { 452b5b571e0SLoGin pcb.thread.write_irqsave().group_leader = Arc::downgrade(pcb); 453393f6915SLoGin unsafe { 454393f6915SLoGin let ptr = pcb.as_ref() as *const ProcessControlBlock as *mut ProcessControlBlock; 455393f6915SLoGin (*ptr).tgid = pcb.tgid; 456393f6915SLoGin } 457393f6915SLoGin } 458393f6915SLoGin 459393f6915SLoGin // CLONE_PARENT re-uses the old parent 460393f6915SLoGin if clone_flags.contains(CloneFlags::CLONE_PARENT | CloneFlags::CLONE_THREAD) { 46152bcb59eSGnoCiYeH *pcb.real_parent_pcb.write_irqsave() = 46252bcb59eSGnoCiYeH current_pcb.real_parent_pcb.read_irqsave().clone(); 463393f6915SLoGin 464393f6915SLoGin if clone_flags.contains(CloneFlags::CLONE_THREAD) { 465393f6915SLoGin pcb.exit_signal.store(Signal::INVALID, Ordering::SeqCst); 466393f6915SLoGin } else { 46752bcb59eSGnoCiYeH let leader = current_pcb.thread.read_irqsave().group_leader(); 468393f6915SLoGin if unlikely(leader.is_none()) { 469393f6915SLoGin panic!( 470393f6915SLoGin "fork: Failed to get leader of current process, current pid: [{:?}]", 471393f6915SLoGin current_pcb.pid() 472393f6915SLoGin ); 473393f6915SLoGin } 474393f6915SLoGin 475393f6915SLoGin pcb.exit_signal.store( 476393f6915SLoGin leader.unwrap().exit_signal.load(Ordering::SeqCst), 477393f6915SLoGin Ordering::SeqCst, 478393f6915SLoGin ); 479393f6915SLoGin } 480393f6915SLoGin } else { 481393f6915SLoGin // 新创建的进程,设置其父进程为当前进程 482b5b571e0SLoGin *pcb.real_parent_pcb.write_irqsave() = Arc::downgrade(current_pcb); 483393f6915SLoGin pcb.exit_signal 484393f6915SLoGin .store(clone_args.exit_signal, Ordering::SeqCst); 485393f6915SLoGin } 486393f6915SLoGin 487e7071df6SLoGin // todo: 增加线程组相关的逻辑。 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/fork.c#2437 488393f6915SLoGin 489f0c87a89SGnoCiYeH sched_cgroup_fork(pcb); 490f0c87a89SGnoCiYeH 491971462beSGnoCiYeH Ok(()) 492971462beSGnoCiYeH } 4931496ba7bSLoGin } 494