14 int* child_tid,
void* tls,
16 auto* parent = GetCurrentTask();
28 "Clone: kCloneThread requires kCloneVm, kCloneFiles, kCloneSighand");
34 Pid new_pid = AllocatePid();
36 klog::Err(
"Clone: Failed to allocate PID");
41 auto child_ptr = kstd::make_unique<TaskControlBlock>();
43 klog::Err(
"Clone: Failed to allocate child task");
46 auto* child = child_ptr.get();
53 child->name = parent->name;
54 child->policy = parent->policy;
55 child->sched_info = parent->sched_info;
60 child->aux->parent_pid = parent->aux->parent_pid;
62 child->aux->parent_pid = parent->pid;
68 child->aux->tgid = parent->aux->tgid;
69 child->aux->pgid = parent->aux->pgid;
70 child->aux->sid = parent->aux->sid;
73 if (parent->IsThreadGroupLeader()) {
74 child->JoinThreadGroup(parent);
79 child->JoinThreadGroup(leader);
81 klog::Warn(
"Clone: Thread group leader not found for tgid={}",
88 child->aux->tgid = new_pid;
89 child->aux->pgid = parent->aux->pgid;
90 child->aux->sid = parent->aux->sid;
99 klog::Debug(
"Clone: sharing file descriptor table (not implemented)");
101 klog::Debug(
"Clone: copying file descriptor table (not implemented)");
107 klog::Debug(
"Clone: sharing signal handlers (not implemented)");
109 klog::Debug(
"Clone: copying signal handlers (not implemented)");
115 klog::Debug(
"Clone: sharing filesystem info (not implemented)");
117 klog::Debug(
"Clone: copying filesystem info (not implemented)");
123 child->page_table = parent->page_table;
125 reinterpret_cast<uintptr_t
>(child->page_table));
128 if (parent->page_table) {
130 auto result = VirtualMemorySingleton::instance().ClonePageDirectory(
131 parent->page_table,
true);
132 if (!result.has_value()) {
133 klog::Err(
"Clone: Failed to clone page table: {}",
134 result.error().message());
138 VirtualMemorySingleton::instance().DestroyPageDirectory(
139 child->page_table,
false);
140 child->page_table =
nullptr;
144 child->page_table =
reinterpret_cast<uint64_t*
>(result.value());
145 klog::Debug(
"Clone: cloned page table from {:#x} to {:#x}",
146 reinterpret_cast<uintptr_t
>(parent->page_table),
147 reinterpret_cast<uintptr_t
>(child->page_table));
150 child->page_table =
nullptr;
155 child->kernel_stack =
static_cast<uint8_t*
>(
158 if (!child->kernel_stack) {
159 klog::Err(
"Clone: Failed to allocate kernel stack");
162 VirtualMemorySingleton::instance().DestroyPageDirectory(child->page_table,
164 child->page_table =
nullptr;
170 kstd::memset(child->kernel_stack, 0,
177 kstd::memcpy(child->trap_context_ptr, &parent_context,
182 child->trap_context_ptr->UserStackPointer() =
183 reinterpret_cast<uint64_t
>(user_stack);
188 child->trap_context_ptr->ThreadPointer() =
reinterpret_cast<uint64_t
>(tls);
192 parent_context.ReturnValue() = new_pid;
194 child->trap_context_ptr->ReturnValue() = 0;
198 *parent_tid = new_pid;
201 *child_tid = new_pid;
205 AddTask(std::move(child_ptr));
211 "Clone: created {} - parent={}, child={}, tgid={}, vm={}, flags={:#x}",
212 clone_type, parent->pid, new_pid, child->aux->tgid, vm_type,
213 static_cast<uint64_t
>(flags));
auto Clone(uint64_t flags, void *user_stack, int *parent_tid, int *child_tid, void *tls, cpu_io::TrapContext &parent_context) -> Expected< Pid >
克隆当前任务 (fork/clone 系统调用)