1 #include "process.h"
2 #include <common/err.h>
3 #include <common/kthread.h>
4 #include <common/spinlock.h>
5
6 extern spinlock_t process_global_pid_write_lock;
7 extern long process_global_pid;
8
9 extern void kernel_thread_func(void);
10 extern uint64_t rs_procfs_register_pid(uint64_t);
11 extern uint64_t rs_procfs_unregister_pid(uint64_t);
12
13 extern int process_copy_files(uint64_t clone_flags, struct process_control_block *pcb);
14 int process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb);
15 int process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb);
16 int process_copy_thread(uint64_t clone_flags, struct process_control_block *pcb, uint64_t stack_start,
17 uint64_t stack_size, struct pt_regs *current_regs);
18
19 extern int process_copy_sighand(uint64_t clone_flags, struct process_control_block *pcb);
20 extern int process_copy_signal(uint64_t clone_flags, struct process_control_block *pcb);
21 extern void process_exit_sighand(struct process_control_block *pcb);
22 extern void process_exit_signal(struct process_control_block *pcb);
23
24 /**
25 * @brief fork当前进程
26 *
27 * @param regs 新的寄存器值
28 * @param clone_flags 克隆标志
29 * @param stack_start 堆栈开始地址
30 * @param stack_size 堆栈大小
31 * @return unsigned long
32 */
do_fork(struct pt_regs * regs,unsigned long clone_flags,unsigned long stack_start,unsigned long stack_size)33 unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start,
34 unsigned long stack_size)
35 {
36 int retval = 0;
37 struct process_control_block *tsk = NULL;
38
39 // 为新的进程分配栈空间,并将pcb放置在底部
40 tsk = (struct process_control_block *)kzalloc(STACK_SIZE, 0);
41 barrier();
42
43 if (tsk == NULL)
44 {
45 retval = -ENOMEM;
46 return retval;
47 }
48
49 barrier();
50 memset(tsk, 0, sizeof(struct process_control_block));
51 io_mfence();
52 // 将当前进程的pcb复制到新的pcb内
53 memcpy(tsk, current_pcb, sizeof(struct process_control_block));
54 tsk->worker_private = NULL;
55 io_mfence();
56
57 // 初始化进程的循环链表结点
58 list_init(&tsk->list);
59
60 io_mfence();
61 // 判断是否为内核态调用fork
62 if ((current_pcb->flags & PF_KTHREAD) && stack_start != 0)
63 tsk->flags |= PF_KFORK;
64
65 if (tsk->flags & PF_KTHREAD)
66 {
67 // 对于内核线程,设置其worker私有信息
68 retval = kthread_set_worker_private(tsk);
69 if (IS_ERR_VALUE(retval))
70 goto copy_flags_failed;
71 tsk->virtual_runtime = 0;
72 }
73 tsk->priority = 2;
74 tsk->preempt_count = 0;
75
76 // 增加全局的pid并赋值给新进程的pid
77 spin_lock(&process_global_pid_write_lock);
78 tsk->pid = process_global_pid++;
79 barrier();
80 // 加入到进程链表中
81 // todo: 对pcb_list_lock加锁
82 tsk->prev_pcb = &initial_proc_union.pcb;
83 barrier();
84 tsk->next_pcb = initial_proc_union.pcb.next_pcb;
85 barrier();
86 initial_proc_union.pcb.next_pcb = tsk;
87 barrier();
88 tsk->parent_pcb = current_pcb;
89 barrier();
90
91 spin_unlock(&process_global_pid_write_lock);
92
93 tsk->cpu_id = proc_current_cpu_id;
94 tsk->state = PROC_UNINTERRUPTIBLE;
95
96 tsk->parent_pcb = current_pcb;
97 wait_queue_init(&tsk->wait_child_proc_exit, NULL);
98 barrier();
99 list_init(&tsk->list);
100
101 retval = -ENOMEM;
102
103 // 拷贝标志位
104 retval = process_copy_flags(clone_flags, tsk);
105 if (retval)
106 goto copy_flags_failed;
107
108 // 拷贝内存空间分布结构体
109 retval = process_copy_mm(clone_flags, tsk);
110 if (retval)
111 goto copy_mm_failed;
112
113 // 拷贝文件
114 retval = process_copy_files(clone_flags, tsk);
115 if (retval)
116 goto copy_files_failed;
117
118 // 拷贝信号处理函数
119 retval = process_copy_sighand(clone_flags, tsk);
120 if (retval)
121 goto copy_sighand_failed;
122
123 retval = process_copy_signal(clone_flags, tsk);
124 if (retval)
125 goto copy_signal_failed;
126
127 // 拷贝线程结构体
128 retval = process_copy_thread(clone_flags, tsk, stack_start, stack_size, regs);
129 if (retval)
130 goto copy_thread_failed;
131
132 // 拷贝成功
133 retval = tsk->pid;
134
135 tsk->flags &= ~PF_KFORK;
136
137 // 唤醒进程
138 process_wakeup(tsk);
139
140 // 创建对应procfs文件
141 rs_procfs_register_pid(tsk->pid);
142
143 return retval;
144
145 copy_thread_failed:;
146 // 回收线程
147 process_exit_thread(tsk);
148 copy_files_failed:;
149 // 回收文件
150 process_exit_files(tsk);
151 rs_procfs_unregister_pid(tsk->pid);
152 copy_sighand_failed:;
153 process_exit_sighand(tsk);
154 copy_signal_failed:;
155 process_exit_signal(tsk);
156 copy_mm_failed:;
157 // 回收内存空间分布结构体
158 process_exit_mm(tsk);
159 copy_flags_failed:;
160 kfree(tsk);
161 return retval;
162 }
163
164 /**
165 * @brief 拷贝当前进程的标志位
166 *
167 * @param clone_flags 克隆标志位
168 * @param pcb 新的进程的pcb
169 * @return uint64_t
170 */
process_copy_flags(uint64_t clone_flags,struct process_control_block * pcb)171 int process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb)
172 {
173 if (clone_flags & CLONE_VM)
174 pcb->flags |= PF_VFORK;
175 return 0;
176 }
177
178
179 /**
180 * @brief 拷贝当前进程的内存空间分布结构体信息
181 *
182 * @param clone_flags 克隆标志位
183 * @param pcb 新的进程的pcb
184 * @return uint64_t
185 */
process_copy_mm(uint64_t clone_flags,struct process_control_block * pcb)186 int process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb)
187 {
188 int retval = 0;
189 // 与父进程共享内存空间
190 if (clone_flags & CLONE_VM)
191 {
192 pcb->mm = current_pcb->mm;
193
194 return retval;
195 }
196
197 // 分配新的内存空间分布结构体
198 struct mm_struct *new_mms = (struct mm_struct *)kmalloc(sizeof(struct mm_struct), 0);
199 memset(new_mms, 0, sizeof(struct mm_struct));
200
201 memcpy(new_mms, current_pcb->mm, sizeof(struct mm_struct));
202 new_mms->vmas = NULL;
203 pcb->mm = new_mms;
204
205 // 分配顶层页表, 并设置顶层页表的物理地址
206 new_mms->pgd = (pml4t_t *)virt_2_phys(kmalloc(PAGE_4K_SIZE, 0));
207 // 由于高2K部分为内核空间,在接下来需要覆盖其数据,因此不用清零
208 memset(phys_2_virt(new_mms->pgd), 0, PAGE_4K_SIZE / 2);
209
210 // 拷贝内核空间的页表指针
211 memcpy(phys_2_virt(new_mms->pgd) + 256, phys_2_virt(initial_proc[proc_current_cpu_id]->mm->pgd) + 256,
212 PAGE_4K_SIZE / 2);
213
214 uint64_t *current_pgd = (uint64_t *)phys_2_virt(current_pcb->mm->pgd);
215
216 uint64_t *new_pml4t = (uint64_t *)phys_2_virt(new_mms->pgd);
217
218 // 拷贝用户空间的vma
219 struct vm_area_struct *vma = current_pcb->mm->vmas;
220 while (vma != NULL)
221 {
222 if (vma->vm_end > USER_MAX_LINEAR_ADDR || vma->vm_flags & VM_DONTCOPY)
223 {
224 vma = vma->vm_next;
225 continue;
226 }
227
228 int64_t vma_size = vma->vm_end - vma->vm_start;
229 // kdebug("vma_size=%ld, vm_start=%#018lx", vma_size, vma->vm_start);
230 if (vma_size > PAGE_2M_SIZE / 2)
231 {
232 int page_to_alloc = (PAGE_2M_ALIGN(vma_size)) >> PAGE_2M_SHIFT;
233 for (int i = 0; i < page_to_alloc; ++i)
234 {
235 uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
236
237 struct vm_area_struct *new_vma = NULL;
238 int ret = mm_create_vma(new_mms, vma->vm_start + i * PAGE_2M_SIZE, PAGE_2M_SIZE, vma->vm_flags,
239 vma->vm_ops, &new_vma);
240 // 防止内存泄露
241 if (unlikely(ret == -EEXIST))
242 free_pages(Phy_to_2M_Page(pa), 1);
243 else
244 mm_map_vma(new_vma, pa, 0, PAGE_2M_SIZE);
245
246 memcpy((void *)phys_2_virt(pa), (void *)(vma->vm_start + i * PAGE_2M_SIZE),
247 (vma_size >= PAGE_2M_SIZE) ? PAGE_2M_SIZE : vma_size);
248 vma_size -= PAGE_2M_SIZE;
249 }
250 }
251 else
252 {
253 uint64_t map_size = PAGE_4K_ALIGN(vma_size);
254 uint64_t va = (uint64_t)kmalloc(map_size, 0);
255
256 struct vm_area_struct *new_vma = NULL;
257 int ret = mm_create_vma(new_mms, vma->vm_start, map_size, vma->vm_flags, vma->vm_ops, &new_vma);
258 // 防止内存泄露
259 if (unlikely(ret == -EEXIST))
260 kfree((void *)va);
261 else
262 mm_map_vma(new_vma, virt_2_phys(va), 0, map_size);
263
264 memcpy((void *)va, (void *)vma->vm_start, vma_size);
265 }
266 vma = vma->vm_next;
267 }
268
269 return retval;
270 }
271
272 /**
273 * @brief 重写内核栈中的rbp地址
274 *
275 * @param new_regs 子进程的reg
276 * @param new_pcb 子进程的pcb
277 * @return int
278 */
process_rewrite_rbp(struct pt_regs * new_regs,struct process_control_block * new_pcb)279 static int process_rewrite_rbp(struct pt_regs *new_regs, struct process_control_block *new_pcb)
280 {
281
282 uint64_t new_top = ((uint64_t)new_pcb) + STACK_SIZE;
283 uint64_t old_top = (uint64_t)(current_pcb) + STACK_SIZE;
284
285 uint64_t *rbp = &new_regs->rbp;
286 uint64_t *tmp = rbp;
287
288 // 超出内核栈范围
289 if ((uint64_t)*rbp >= old_top || (uint64_t)*rbp < (old_top - STACK_SIZE))
290 return 0;
291
292 while (1)
293 {
294 // 计算delta
295 uint64_t delta = old_top - *rbp;
296 // 计算新的rbp值
297 uint64_t newVal = new_top - delta;
298
299 // 新的值不合法
300 if (unlikely((uint64_t)newVal >= new_top || (uint64_t)newVal < (new_top - STACK_SIZE)))
301 break;
302 // 将新的值写入对应位置
303 *rbp = newVal;
304 // 跳转栈帧
305 rbp = (uint64_t *)*rbp;
306 }
307
308 // 设置内核态fork返回到enter_syscall_int()函数内的时候,rsp寄存器的值
309 new_regs->rsp = new_top - (old_top - new_regs->rsp);
310 return 0;
311 }
312
313 /**
314 * @brief 拷贝当前进程的线程结构体
315 *
316 * @param clone_flags 克隆标志位
317 * @param pcb 新的进程的pcb
318 * @return uint64_t
319 */
process_copy_thread(uint64_t clone_flags,struct process_control_block * pcb,uint64_t stack_start,uint64_t stack_size,struct pt_regs * current_regs)320 int process_copy_thread(uint64_t clone_flags, struct process_control_block *pcb, uint64_t stack_start,
321 uint64_t stack_size, struct pt_regs *current_regs)
322 {
323 // 将线程结构体放置在pcb后方
324 struct thread_struct *thd = (struct thread_struct *)(pcb + 1);
325 memset(thd, 0, sizeof(struct thread_struct));
326 pcb->thread = thd;
327
328 struct pt_regs *child_regs = NULL;
329 // 拷贝栈空间
330 if (pcb->flags & PF_KFORK) // 内核态下的fork
331 {
332 // 内核态下则拷贝整个内核栈
333 uint32_t size = ((uint64_t)current_pcb) + STACK_SIZE - (uint64_t)(current_regs);
334
335 child_regs = (struct pt_regs *)(((uint64_t)pcb) + STACK_SIZE - size);
336 memcpy(child_regs, (void *)current_regs, size);
337 barrier();
338 // 然后重写新的栈中,每个栈帧的rbp值
339 process_rewrite_rbp(child_regs, pcb);
340 }
341 else
342 {
343 child_regs = (struct pt_regs *)((uint64_t)pcb + STACK_SIZE - sizeof(struct pt_regs));
344 memcpy(child_regs, current_regs, sizeof(struct pt_regs));
345 barrier();
346 child_regs->rsp = stack_start;
347 }
348
349 // 设置子进程的返回值为0
350 child_regs->rax = 0;
351 if (pcb->flags & PF_KFORK)
352 thd->rbp =
353 (uint64_t)(child_regs + 1); // 设置新的内核线程开始执行时的rbp(也就是进入ret_from_system_call时的rbp)
354 else
355 thd->rbp = (uint64_t)pcb + STACK_SIZE;
356
357 // 设置新的内核线程开始执行的时候的rsp
358 thd->rsp = (uint64_t)child_regs;
359 thd->fs = current_pcb->thread->fs;
360 thd->gs = current_pcb->thread->gs;
361
362 // 根据是否为内核线程、是否在内核态fork,设置进程的开始执行的地址
363 if (pcb->flags & PF_KFORK)
364 thd->rip = (uint64_t)ret_from_system_call;
365 else if (pcb->flags & PF_KTHREAD && (!(pcb->flags & PF_KFORK)))
366 thd->rip = (uint64_t)kernel_thread_func;
367 else
368 thd->rip = (uint64_t)ret_from_system_call;
369
370 return 0;
371 }