process.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. #include "process.h"
  2. #include <common/printk.h>
  3. #include <common/kprint.h>
  4. #include <common/stdio.h>
  5. #include <common/string.h>
  6. #include <common/compiler.h>
  7. #include <common/libELF/elf.h>
  8. #include <common/time.h>
  9. #include <common/sys/wait.h>
  10. #include <driver/video/video.h>
  11. #include <driver/usb/usb.h>
  12. #include <exception/gate.h>
  13. #include <filesystem/fat32/fat32.h>
  14. #include <mm/slab.h>
  15. #include <common/spinlock.h>
  16. #include <syscall/syscall.h>
  17. #include <syscall/syscall_num.h>
  18. #include <sched/sched.h>
  19. #include <common/unistd.h>
  20. #include <debug/traceback/traceback.h>
  21. #include <ktest/ktest.h>
  22. // #pragma GCC push_options
  23. // #pragma GCC optimize("O0")
  24. spinlock_t process_global_pid_write_lock; // 增加pid的写锁
  25. long process_global_pid = 1; // 系统中最大的pid
  26. extern void system_call(void);
  27. extern void kernel_thread_func(void);
  28. ul _stack_start; // initial proc的栈基地址(虚拟地址)
  29. struct mm_struct initial_mm = {0};
  30. struct thread_struct initial_thread =
  31. {
  32. .rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
  33. .rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
  34. .fs = KERNEL_DS,
  35. .gs = KERNEL_DS,
  36. .cr2 = 0,
  37. .trap_num = 0,
  38. .err_code = 0};
  39. // 初始化 初始进程的union ,并将其链接到.data.init_proc段内
  40. union proc_union initial_proc_union __attribute__((__section__(".data.init_proc_union"))) = {INITIAL_PROC(initial_proc_union.pcb)};
  41. struct process_control_block *initial_proc[MAX_CPU_NUM] = {&initial_proc_union.pcb, 0};
  42. // 为每个核心初始化初始进程的tss
  43. struct tss_struct initial_tss[MAX_CPU_NUM] = {[0 ... MAX_CPU_NUM - 1] = INITIAL_TSS};
  44. /**
  45. * @brief 拷贝当前进程的标志位
  46. *
  47. * @param clone_flags 克隆标志位
  48. * @param pcb 新的进程的pcb
  49. * @return uint64_t
  50. */
  51. uint64_t process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb);
  52. /**
  53. * @brief 拷贝当前进程的文件描述符等信息
  54. *
  55. * @param clone_flags 克隆标志位
  56. * @param pcb 新的进程的pcb
  57. * @return uint64_t
  58. */
  59. uint64_t process_copy_files(uint64_t clone_flags, struct process_control_block *pcb);
  60. /**
  61. * @brief 回收进程的所有文件描述符
  62. *
  63. * @param pcb 要被回收的进程的pcb
  64. * @return uint64_t
  65. */
  66. uint64_t process_exit_files(struct process_control_block *pcb);
  67. /**
  68. * @brief 拷贝当前进程的内存空间分布结构体信息
  69. *
  70. * @param clone_flags 克隆标志位
  71. * @param pcb 新的进程的pcb
  72. * @return uint64_t
  73. */
  74. uint64_t process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb);
  75. /**
  76. * @brief 释放进程的页表
  77. *
  78. * @param pcb 要被释放页表的进程
  79. * @return uint64_t
  80. */
  81. uint64_t process_exit_mm(struct process_control_block *pcb);
  82. /**
  83. * @brief 拷贝当前进程的线程结构体
  84. *
  85. * @param clone_flags 克隆标志位
  86. * @param pcb 新的进程的pcb
  87. * @return uint64_t
  88. */
  89. uint64_t 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);
  90. void process_exit_thread(struct process_control_block *pcb);
  91. /**
  92. * @brief 切换进程
  93. *
  94. * @param prev 上一个进程的pcb
  95. * @param next 将要切换到的进程的pcb
  96. * 由于程序在进入内核的时候已经保存了寄存器,因此这里不需要保存寄存器。
  97. * 这里切换fs和gs寄存器
  98. */
  99. #pragma GCC push_options
  100. #pragma GCC optimize("O0")
  101. void __switch_to(struct process_control_block *prev, struct process_control_block *next)
  102. {
  103. initial_tss[proc_current_cpu_id].rsp0 = next->thread->rbp;
  104. // kdebug("next_rsp = %#018lx ", next->thread->rsp);
  105. // set_tss64((uint *)phys_2_virt(TSS64_Table), initial_tss[0].rsp0, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1,
  106. // initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7);
  107. __asm__ __volatile__("movq %%fs, %0 \n\t"
  108. : "=a"(prev->thread->fs));
  109. __asm__ __volatile__("movq %%gs, %0 \n\t"
  110. : "=a"(prev->thread->gs));
  111. __asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
  112. __asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
  113. }
  114. #pragma GCC pop_options
  115. /**
  116. * @brief 打开要执行的程序文件
  117. *
  118. * @param path
  119. * @return struct vfs_file_t*
  120. */
  121. struct vfs_file_t *process_open_exec_file(char *path)
  122. {
  123. struct vfs_dir_entry_t *dentry = NULL;
  124. struct vfs_file_t *filp = NULL;
  125. dentry = vfs_path_walk(path, 0);
  126. if (dentry == NULL)
  127. return (void *)-ENOENT;
  128. if (dentry->dir_inode->attribute == VFS_ATTR_DIR)
  129. return (void *)-ENOTDIR;
  130. filp = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
  131. if (filp == NULL)
  132. return (void *)-ENOMEM;
  133. filp->position = 0;
  134. filp->mode = 0;
  135. filp->dEntry = dentry;
  136. filp->mode = ATTR_READ_ONLY;
  137. filp->file_ops = dentry->dir_inode->file_ops;
  138. return filp;
  139. }
  140. /**
  141. * @brief 加载elf格式的程序文件到内存中,并设置regs
  142. *
  143. * @param regs 寄存器
  144. * @param path 文件路径
  145. * @return int
  146. */
  147. static int process_load_elf_file(struct pt_regs *regs, char *path)
  148. {
  149. int retval = 0;
  150. struct vfs_file_t *filp = process_open_exec_file(path);
  151. if ((long)filp <= 0 && (long)filp >= -255)
  152. {
  153. // kdebug("(long)filp=%ld", (long)filp);
  154. return (unsigned long)filp;
  155. }
  156. void *buf = kmalloc(PAGE_4K_SIZE, 0);
  157. memset(buf, 0, PAGE_4K_SIZE);
  158. uint64_t pos = 0;
  159. pos = filp->file_ops->lseek(filp, 0, SEEK_SET);
  160. retval = filp->file_ops->read(filp, (char *)buf, sizeof(Elf64_Ehdr), &pos);
  161. retval = 0;
  162. if (!elf_check(buf))
  163. {
  164. kerror("Not an ELF file: %s", path);
  165. retval = -ENOTSUP;
  166. goto load_elf_failed;
  167. }
  168. #if ARCH(X86_64)
  169. // 暂时只支持64位的文件
  170. if (((Elf32_Ehdr *)buf)->e_ident[EI_CLASS] != ELFCLASS64)
  171. {
  172. kdebug("((Elf32_Ehdr *)buf)->e_ident[EI_CLASS]=%d", ((Elf32_Ehdr *)buf)->e_ident[EI_CLASS]);
  173. retval = -EUNSUPPORTED;
  174. goto load_elf_failed;
  175. }
  176. Elf64_Ehdr ehdr = *(Elf64_Ehdr *)buf;
  177. // 暂时只支持AMD64架构
  178. if (ehdr.e_machine != EM_AMD64)
  179. {
  180. kerror("e_machine=%d", ehdr.e_machine);
  181. retval = -EUNSUPPORTED;
  182. goto load_elf_failed;
  183. }
  184. #else
  185. #error Unsupported architecture!
  186. #endif
  187. if (ehdr.e_type != ET_EXEC)
  188. {
  189. kerror("Not executable file! filename=%s\tehdr->e_type=%d", path, ehdr.e_type);
  190. retval = -EUNSUPPORTED;
  191. goto load_elf_failed;
  192. }
  193. // kdebug("filename=%s:\te_entry=%#018lx", path, ehdr.e_entry);
  194. regs->rip = ehdr.e_entry;
  195. current_pcb->mm->code_addr_start = ehdr.e_entry;
  196. // kdebug("ehdr.e_phoff=%#018lx\t ehdr.e_phentsize=%d, ehdr.e_phnum=%d", ehdr.e_phoff, ehdr.e_phentsize, ehdr.e_phnum);
  197. // 将指针移动到program header处
  198. pos = ehdr.e_phoff;
  199. // 读取所有的phdr
  200. pos = filp->file_ops->lseek(filp, pos, SEEK_SET);
  201. filp->file_ops->read(filp, (char *)buf, (uint64_t)ehdr.e_phentsize * (uint64_t)ehdr.e_phnum, &pos);
  202. if ((unsigned long)filp <= 0)
  203. {
  204. kdebug("(unsigned long)filp=%d", (long)filp);
  205. retval = -ENOEXEC;
  206. goto load_elf_failed;
  207. }
  208. Elf64_Phdr *phdr = buf;
  209. // 将程序加载到内存中
  210. for (int i = 0; i < ehdr.e_phnum; ++i, ++phdr)
  211. {
  212. // kdebug("phdr[%d] phdr->p_offset=%#018lx phdr->p_vaddr=%#018lx phdr->p_memsz=%ld phdr->p_filesz=%ld phdr->p_type=%d", i, phdr->p_offset, phdr->p_vaddr, phdr->p_memsz, phdr->p_filesz, phdr->p_type);
  213. // 不是可加载的段
  214. if (phdr->p_type != PT_LOAD)
  215. continue;
  216. int64_t remain_mem_size = phdr->p_memsz;
  217. int64_t remain_file_size = phdr->p_filesz;
  218. pos = phdr->p_offset;
  219. uint64_t virt_base = phdr->p_vaddr;
  220. // kdebug("virt_base = %#018lx, &memory_management_struct=%#018lx", virt_base, &memory_management_struct);
  221. while (remain_mem_size > 0)
  222. {
  223. // todo: 改用slab分配4K大小内存块并映射到4K页
  224. if (!mm_check_mapped((uint64_t)current_pcb->mm->pgd, virt_base)) // 未映射,则新增物理页
  225. {
  226. mm_map_proc_page_table((uint64_t)current_pcb->mm->pgd, true, virt_base, alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys, PAGE_2M_SIZE, PAGE_USER_PAGE, true, true, false);
  227. memset((void *)virt_base, 0, PAGE_2M_SIZE);
  228. }
  229. pos = filp->file_ops->lseek(filp, pos, SEEK_SET);
  230. int64_t val = 0;
  231. if (remain_file_size != 0)
  232. {
  233. int64_t to_trans = (remain_file_size > PAGE_2M_SIZE) ? PAGE_2M_SIZE : remain_file_size;
  234. val = filp->file_ops->read(filp, (char *)virt_base, to_trans, &pos);
  235. }
  236. if (val < 0)
  237. goto load_elf_failed;
  238. remain_mem_size -= PAGE_2M_SIZE;
  239. remain_file_size -= val;
  240. virt_base += PAGE_2M_SIZE;
  241. }
  242. }
  243. // 分配2MB的栈内存空间
  244. regs->rsp = current_pcb->mm->stack_start;
  245. regs->rbp = current_pcb->mm->stack_start;
  246. uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
  247. mm_map_proc_page_table((uint64_t)current_pcb->mm->pgd, true, current_pcb->mm->stack_start - PAGE_2M_SIZE, pa, PAGE_2M_SIZE, PAGE_USER_PAGE, true, true, false);
  248. // 清空栈空间
  249. memset((void *)(current_pcb->mm->stack_start - PAGE_2M_SIZE), 0, PAGE_2M_SIZE);
  250. load_elf_failed:;
  251. if (buf != NULL)
  252. kfree(buf);
  253. return retval;
  254. }
  255. /**
  256. * @brief 使当前进程去执行新的代码
  257. *
  258. * @param regs 当前进程的寄存器
  259. * @param path 可执行程序的路径
  260. * @param argv 参数列表
  261. * @param envp 环境变量
  262. * @return ul 错误码
  263. */
  264. #pragma GCC push_options
  265. #pragma GCC optimize("O0")
  266. ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[])
  267. {
  268. // kdebug("do_execve is running...");
  269. // 当前进程正在与父进程共享地址空间,需要创建
  270. // 独立的地址空间才能使新程序正常运行
  271. if (current_pcb->flags & PF_VFORK)
  272. {
  273. kdebug("proc:%d creating new mem space", current_pcb->pid);
  274. // 分配新的内存空间分布结构体
  275. struct mm_struct *new_mms = (struct mm_struct *)kmalloc(sizeof(struct mm_struct), 0);
  276. memset(new_mms, 0, sizeof(struct mm_struct));
  277. current_pcb->mm = new_mms;
  278. // 分配顶层页表, 并设置顶层页表的物理地址
  279. new_mms->pgd = (pml4t_t *)virt_2_phys(kmalloc(PAGE_4K_SIZE, 0));
  280. // 由于高2K部分为内核空间,在接下来需要覆盖其数据,因此不用清零
  281. memset(phys_2_virt(new_mms->pgd), 0, PAGE_4K_SIZE / 2);
  282. // 拷贝内核空间的页表指针
  283. memcpy(phys_2_virt(new_mms->pgd) + 256, phys_2_virt(initial_proc[proc_current_cpu_id]) + 256, PAGE_4K_SIZE / 2);
  284. }
  285. // 设置用户栈和用户堆的基地址
  286. unsigned long stack_start_addr = 0x6ffff0a00000UL;
  287. const uint64_t brk_start_addr = 0x700000000000UL;
  288. process_switch_mm(current_pcb);
  289. // 为用户态程序设置地址边界
  290. if (!(current_pcb->flags & PF_KTHREAD))
  291. current_pcb->addr_limit = USER_MAX_LINEAR_ADDR;
  292. current_pcb->mm->code_addr_end = 0;
  293. current_pcb->mm->data_addr_start = 0;
  294. current_pcb->mm->data_addr_end = 0;
  295. current_pcb->mm->rodata_addr_start = 0;
  296. current_pcb->mm->rodata_addr_end = 0;
  297. current_pcb->mm->bss_start = 0;
  298. current_pcb->mm->bss_end = 0;
  299. current_pcb->mm->brk_start = brk_start_addr;
  300. current_pcb->mm->brk_end = brk_start_addr;
  301. current_pcb->mm->stack_start = stack_start_addr;
  302. // 关闭之前的文件描述符
  303. process_exit_files(current_pcb);
  304. // 清除进程的vfork标志位
  305. current_pcb->flags &= ~PF_VFORK;
  306. // 加载elf格式的可执行文件
  307. int tmp = process_load_elf_file(regs, path);
  308. if (tmp < 0)
  309. goto exec_failed;
  310. // 拷贝参数列表
  311. if (argv != NULL)
  312. {
  313. int argc = 0;
  314. // 目标程序的argv基地址指针,最大8个参数
  315. char **dst_argv = (char **)(stack_start_addr - (sizeof(char **) << 3));
  316. uint64_t str_addr = (uint64_t)dst_argv;
  317. for (argc = 0; argc < 8 && argv[argc] != NULL; ++argc)
  318. {
  319. if (*argv[argc] == NULL)
  320. break;
  321. // 测量参数的长度(最大1023)
  322. int argv_len = strnlen_user(argv[argc], 1023) + 1;
  323. strncpy((char *)(str_addr - argv_len), argv[argc], argv_len - 1);
  324. str_addr -= argv_len;
  325. dst_argv[argc] = (char *)str_addr;
  326. // 字符串加上结尾字符
  327. ((char *)str_addr)[argv_len] = '\0';
  328. }
  329. // 重新设定栈基址,并预留空间防止越界
  330. stack_start_addr = str_addr - 8;
  331. current_pcb->mm->stack_start = stack_start_addr;
  332. regs->rsp = regs->rbp = stack_start_addr;
  333. // 传递参数
  334. regs->rdi = argc;
  335. regs->rsi = (uint64_t)dst_argv;
  336. }
  337. // kdebug("execve ok");
  338. regs->cs = USER_CS | 3;
  339. regs->ds = USER_DS | 3;
  340. regs->ss = USER_DS | 0x3;
  341. regs->rflags = 0x200246;
  342. regs->rax = 1;
  343. regs->es = 0;
  344. return 0;
  345. exec_failed:;
  346. process_do_exit(tmp);
  347. }
  348. #pragma GCC pop_options
  349. /**
  350. * @brief 内核init进程
  351. *
  352. * @param arg
  353. * @return ul 参数
  354. */
  355. #pragma GCC push_options
  356. #pragma GCC optimize("O0")
  357. ul initial_kernel_thread(ul arg)
  358. {
  359. // kinfo("initial proc running...\targ:%#018lx", arg);
  360. fat32_init();
  361. usb_init();
  362. // 对一些组件进行单元测试
  363. uint64_t tpid[] = {
  364. ktest_start(ktest_test_bitree, 0),
  365. ktest_start(ktest_test_kfifo, 0),
  366. ktest_start(ktest_test_mutex, 0),
  367. };
  368. kinfo("Waiting test thread exit...");
  369. // 等待测试进程退出
  370. for (int i = 0; i < sizeof(tpid) / sizeof(uint64_t); ++i)
  371. waitpid(tpid[i], NULL, NULL);
  372. kinfo("All test done.");
  373. // pid_t p = fork();
  374. // if (p == 0)
  375. // {
  376. // kdebug("in subproc, rflags=%#018lx", get_rflags());
  377. // while (1)
  378. // usleep(1000);
  379. // }
  380. // kdebug("subprocess pid=%d", p);
  381. // 准备切换到用户态
  382. struct pt_regs *regs;
  383. // 若在后面这段代码中触发中断,return时会导致段选择子错误,从而触发#GP,因此这里需要cli
  384. cli();
  385. current_pcb->thread->rip = (ul)ret_from_system_call;
  386. current_pcb->thread->rsp = (ul)current_pcb + STACK_SIZE - sizeof(struct pt_regs);
  387. current_pcb->thread->fs = USER_DS | 0x3;
  388. barrier();
  389. current_pcb->thread->gs = USER_DS | 0x3;
  390. // 主动放弃内核线程身份
  391. current_pcb->flags &= (~PF_KTHREAD);
  392. kdebug("in initial_kernel_thread: flags=%ld", current_pcb->flags);
  393. regs = (struct pt_regs *)current_pcb->thread->rsp;
  394. // kdebug("current_pcb->thread->rsp=%#018lx", current_pcb->thread->rsp);
  395. current_pcb->flags = 0;
  396. // 将返回用户层的代码压入堆栈,向rdx传入regs的地址,然后jmp到do_execve这个系统调用api的处理函数 这里的设计思路和switch_proc类似
  397. // 加载用户态程序:shell.elf
  398. char init_path[] = "/shell.elf";
  399. uint64_t addr = (uint64_t)&init_path;
  400. __asm__ __volatile__("movq %1, %%rsp \n\t"
  401. "pushq %2 \n\t"
  402. "jmp do_execve \n\t" ::"D"(current_pcb->thread->rsp),
  403. "m"(current_pcb->thread->rsp), "m"(current_pcb->thread->rip), "S"("/shell.elf"), "c"(NULL), "d"(NULL)
  404. : "memory");
  405. return 1;
  406. }
  407. #pragma GCC pop_options
  408. /**
  409. * @brief 当子进程退出后向父进程发送通知
  410. *
  411. */
  412. void process_exit_notify()
  413. {
  414. wait_queue_wakeup(&current_pcb->parent_pcb->wait_child_proc_exit, PROC_INTERRUPTIBLE);
  415. }
  416. /**
  417. * @brief 进程退出时执行的函数
  418. *
  419. * @param code 返回码
  420. * @return ul
  421. */
  422. ul process_do_exit(ul code)
  423. {
  424. // kinfo("process exiting..., code is %ld.", (long)code);
  425. cli();
  426. struct process_control_block *pcb = current_pcb;
  427. // 进程退出时释放资源
  428. process_exit_files(pcb);
  429. process_exit_thread(pcb);
  430. // todo: 可否在这里释放内存结构体?(在判断共享页引用问题之后)
  431. pcb->state = PROC_ZOMBIE;
  432. pcb->exit_code = code;
  433. sti();
  434. process_exit_notify();
  435. sched_cfs();
  436. while (1)
  437. pause();
  438. }
  439. /**
  440. * @brief 初始化内核进程
  441. *
  442. * @param fn 目标程序的地址
  443. * @param arg 向目标程序传入的参数
  444. * @param flags
  445. * @return int
  446. */
  447. int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags)
  448. {
  449. struct pt_regs regs;
  450. barrier();
  451. memset(&regs, 0, sizeof(regs));
  452. barrier();
  453. // 在rbx寄存器中保存进程的入口地址
  454. regs.rbx = (ul)fn;
  455. // 在rdx寄存器中保存传入的参数
  456. regs.rdx = (ul)arg;
  457. barrier();
  458. regs.ds = KERNEL_DS;
  459. barrier();
  460. regs.es = KERNEL_DS;
  461. barrier();
  462. regs.cs = KERNEL_CS;
  463. barrier();
  464. regs.ss = KERNEL_DS;
  465. barrier();
  466. // 置位中断使能标志位
  467. regs.rflags = (1 << 9);
  468. barrier();
  469. // rip寄存器指向内核线程的引导程序
  470. regs.rip = (ul)kernel_thread_func;
  471. barrier();
  472. // kdebug("kernel_thread_func=%#018lx", kernel_thread_func);
  473. // kdebug("&kernel_thread_func=%#018lx", &kernel_thread_func);
  474. // kdebug("1111\tregs.rip = %#018lx", regs.rip);
  475. return do_fork(&regs, flags | CLONE_VM, 0, 0);
  476. }
  477. /**
  478. * @brief 初始化进程模块
  479. * ☆前置条件:已完成系统调用模块的初始化
  480. */
  481. void process_init()
  482. {
  483. kinfo("Initializing process...");
  484. initial_mm.pgd = (pml4t_t *)get_CR3();
  485. initial_mm.code_addr_start = memory_management_struct.kernel_code_start;
  486. initial_mm.code_addr_end = memory_management_struct.kernel_code_end;
  487. initial_mm.data_addr_start = (ul)&_data;
  488. initial_mm.data_addr_end = memory_management_struct.kernel_data_end;
  489. initial_mm.rodata_addr_start = (ul)&_rodata;
  490. initial_mm.rodata_addr_end = (ul)&_erodata;
  491. initial_mm.bss_start = (uint64_t)&_bss;
  492. initial_mm.bss_end = (uint64_t)&_ebss;
  493. initial_mm.brk_start = memory_management_struct.start_brk;
  494. initial_mm.brk_end = current_pcb->addr_limit;
  495. initial_mm.stack_start = _stack_start;
  496. initial_tss[proc_current_cpu_id].rsp0 = initial_thread.rbp;
  497. // ========= 在IDLE进程的顶层页表中添加对内核地址空间的映射 =====================
  498. // 由于IDLE进程的顶层页表的高地址部分会被后续进程所复制,为了使所有进程能够共享相同的内核空间,
  499. // 因此需要先在IDLE进程的顶层页表内映射二级页表
  500. uint64_t *idle_pml4t_vaddr = (uint64_t *)phys_2_virt((uint64_t)get_CR3() & (~0xfffUL));
  501. for (int i = 256; i < 512; ++i)
  502. {
  503. uint64_t *tmp = idle_pml4t_vaddr + i;
  504. barrier();
  505. if (*tmp == 0)
  506. {
  507. void *pdpt = kmalloc(PAGE_4K_SIZE, 0);
  508. barrier();
  509. memset(pdpt, 0, PAGE_4K_SIZE);
  510. barrier();
  511. set_pml4t(tmp, mk_pml4t(virt_2_phys(pdpt), PAGE_KERNEL_PGT));
  512. }
  513. }
  514. barrier();
  515. flush_tlb();
  516. /*
  517. kdebug("initial_thread.rbp=%#018lx", initial_thread.rbp);
  518. kdebug("initial_tss[0].rsp1=%#018lx", initial_tss[0].rsp1);
  519. kdebug("initial_tss[0].ist1=%#018lx", initial_tss[0].ist1);
  520. */
  521. // 初始化pid的写锁
  522. spin_init(&process_global_pid_write_lock);
  523. // 初始化进程的循环链表
  524. list_init(&initial_proc_union.pcb.list);
  525. barrier();
  526. kernel_thread(initial_kernel_thread, 10, CLONE_FS | CLONE_SIGNAL); // 初始化内核线程
  527. barrier();
  528. initial_proc_union.pcb.state = PROC_RUNNING;
  529. initial_proc_union.pcb.preempt_count = 0;
  530. initial_proc_union.pcb.cpu_id = 0;
  531. initial_proc_union.pcb.virtual_runtime = (1UL << 60);
  532. current_pcb->virtual_runtime = (1UL << 60);
  533. }
  534. /**
  535. * @brief fork当前进程
  536. *
  537. * @param regs 新的寄存器值
  538. * @param clone_flags 克隆标志
  539. * @param stack_start 堆栈开始地址
  540. * @param stack_size 堆栈大小
  541. * @return unsigned long
  542. */
  543. unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size)
  544. {
  545. int retval = 0;
  546. struct process_control_block *tsk = NULL;
  547. // 为新的进程分配栈空间,并将pcb放置在底部
  548. tsk = (struct process_control_block *)kmalloc(STACK_SIZE, 0);
  549. barrier();
  550. if (tsk == NULL)
  551. {
  552. retval = -ENOMEM;
  553. return retval;
  554. }
  555. barrier();
  556. memset(tsk, 0, sizeof(struct process_control_block));
  557. io_mfence();
  558. // 将当前进程的pcb复制到新的pcb内
  559. memcpy(tsk, current_pcb, sizeof(struct process_control_block));
  560. io_mfence();
  561. // 初始化进程的循环链表结点
  562. list_init(&tsk->list);
  563. io_mfence();
  564. // 判断是否为内核态调用fork
  565. if (current_pcb->flags & PF_KTHREAD && stack_start != 0)
  566. tsk->flags |= PF_KFORK;
  567. tsk->priority = 2;
  568. tsk->preempt_count = 0;
  569. // 增加全局的pid并赋值给新进程的pid
  570. spin_lock(&process_global_pid_write_lock);
  571. tsk->pid = process_global_pid++;
  572. barrier();
  573. // 加入到进程链表中
  574. tsk->next_pcb = initial_proc_union.pcb.next_pcb;
  575. barrier();
  576. initial_proc_union.pcb.next_pcb = tsk;
  577. barrier();
  578. tsk->parent_pcb = current_pcb;
  579. barrier();
  580. spin_unlock(&process_global_pid_write_lock);
  581. tsk->cpu_id = proc_current_cpu_id;
  582. tsk->state = PROC_UNINTERRUPTIBLE;
  583. tsk->parent_pcb = current_pcb;
  584. wait_queue_init(&tsk->wait_child_proc_exit, NULL);
  585. barrier();
  586. list_init(&tsk->list);
  587. retval = -ENOMEM;
  588. // 拷贝标志位
  589. if (process_copy_flags(clone_flags, tsk))
  590. goto copy_flags_failed;
  591. // 拷贝内存空间分布结构体
  592. if (process_copy_mm(clone_flags, tsk))
  593. goto copy_mm_failed;
  594. // 拷贝文件
  595. if (process_copy_files(clone_flags, tsk))
  596. goto copy_files_failed;
  597. // 拷贝线程结构体
  598. if (process_copy_thread(clone_flags, tsk, stack_start, stack_size, regs))
  599. goto copy_thread_failed;
  600. // 拷贝成功
  601. retval = tsk->pid;
  602. tsk->flags &= ~PF_KFORK;
  603. // 唤醒进程
  604. process_wakeup(tsk);
  605. return retval;
  606. copy_thread_failed:;
  607. // 回收线程
  608. process_exit_thread(tsk);
  609. copy_files_failed:;
  610. // 回收文件
  611. process_exit_files(tsk);
  612. copy_mm_failed:;
  613. // 回收内存空间分布结构体
  614. process_exit_mm(tsk);
  615. copy_flags_failed:;
  616. kfree(tsk);
  617. return retval;
  618. return 0;
  619. }
  620. /**
  621. * @brief 根据pid获取进程的pcb
  622. *
  623. * @param pid
  624. * @return struct process_control_block*
  625. */
  626. struct process_control_block *process_get_pcb(long pid)
  627. {
  628. struct process_control_block *pcb = initial_proc_union.pcb.next_pcb;
  629. // 使用蛮力法搜索指定pid的pcb
  630. // todo: 使用哈希表来管理pcb
  631. for (; pcb != &initial_proc_union.pcb; pcb = pcb->next_pcb)
  632. {
  633. if (pcb->pid == pid)
  634. return pcb;
  635. }
  636. return NULL;
  637. }
  638. /**
  639. * @brief 将进程加入到调度器的就绪队列中
  640. *
  641. * @param pcb 进程的pcb
  642. */
  643. void process_wakeup(struct process_control_block *pcb)
  644. {
  645. pcb->state = PROC_RUNNING;
  646. sched_cfs_enqueue(pcb);
  647. }
  648. /**
  649. * @brief 将进程加入到调度器的就绪队列中,并标志当前进程需要被调度
  650. *
  651. * @param pcb 进程的pcb
  652. */
  653. void process_wakeup_immediately(struct process_control_block *pcb)
  654. {
  655. pcb->state = PROC_RUNNING;
  656. sched_cfs_enqueue(pcb);
  657. // 将当前进程标志为需要调度,缩短新进程被wakeup的时间
  658. current_pcb->flags |= PF_NEED_SCHED;
  659. }
  660. /**
  661. * @brief 拷贝当前进程的标志位
  662. *
  663. * @param clone_flags 克隆标志位
  664. * @param pcb 新的进程的pcb
  665. * @return uint64_t
  666. */
  667. uint64_t process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb)
  668. {
  669. if (clone_flags & CLONE_VM)
  670. pcb->flags |= PF_VFORK;
  671. return 0;
  672. }
  673. /**
  674. * @brief 拷贝当前进程的文件描述符等信息
  675. *
  676. * @param clone_flags 克隆标志位
  677. * @param pcb 新的进程的pcb
  678. * @return uint64_t
  679. */
  680. uint64_t process_copy_files(uint64_t clone_flags, struct process_control_block *pcb)
  681. {
  682. int retval = 0;
  683. // 如果CLONE_FS被置位,那么子进程与父进程共享文件描述符
  684. // 文件描述符已经在复制pcb时被拷贝
  685. if (clone_flags & CLONE_FS)
  686. return retval;
  687. // 为新进程拷贝新的文件描述符
  688. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  689. {
  690. if (current_pcb->fds[i] == NULL)
  691. continue;
  692. pcb->fds[i] = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
  693. memcpy(pcb->fds[i], current_pcb->fds[i], sizeof(struct vfs_file_t));
  694. }
  695. return retval;
  696. }
  697. /**
  698. * @brief 回收进程的所有文件描述符
  699. *
  700. * @param pcb 要被回收的进程的pcb
  701. * @return uint64_t
  702. */
  703. uint64_t process_exit_files(struct process_control_block *pcb)
  704. {
  705. // 不与父进程共享文件描述符
  706. if (!(pcb->flags & PF_VFORK))
  707. {
  708. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  709. {
  710. if (pcb->fds[i] == NULL)
  711. continue;
  712. kfree(pcb->fds[i]);
  713. }
  714. }
  715. // 清空当前进程的文件描述符列表
  716. memset(pcb->fds, 0, sizeof(struct vfs_file_t *) * PROC_MAX_FD_NUM);
  717. }
  718. /**
  719. * @brief 拷贝当前进程的内存空间分布结构体信息
  720. *
  721. * @param clone_flags 克隆标志位
  722. * @param pcb 新的进程的pcb
  723. * @return uint64_t
  724. */
  725. uint64_t process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb)
  726. {
  727. int retval = 0;
  728. // 与父进程共享内存空间
  729. if (clone_flags & CLONE_VM)
  730. {
  731. pcb->mm = current_pcb->mm;
  732. return retval;
  733. }
  734. // 分配新的内存空间分布结构体
  735. struct mm_struct *new_mms = (struct mm_struct *)kmalloc(sizeof(struct mm_struct), 0);
  736. memset(new_mms, 0, sizeof(struct mm_struct));
  737. memcpy(new_mms, current_pcb->mm, sizeof(struct mm_struct));
  738. pcb->mm = new_mms;
  739. // 分配顶层页表, 并设置顶层页表的物理地址
  740. new_mms->pgd = (pml4t_t *)virt_2_phys(kmalloc(PAGE_4K_SIZE, 0));
  741. // 由于高2K部分为内核空间,在接下来需要覆盖其数据,因此不用清零
  742. memset(phys_2_virt(new_mms->pgd), 0, PAGE_4K_SIZE / 2);
  743. // 拷贝内核空间的页表指针
  744. memcpy(phys_2_virt(new_mms->pgd) + 256, phys_2_virt(initial_proc[proc_current_cpu_id]->mm->pgd) + 256, PAGE_4K_SIZE / 2);
  745. uint64_t *current_pgd = (uint64_t *)phys_2_virt(current_pcb->mm->pgd);
  746. uint64_t *new_pml4t = (uint64_t *)phys_2_virt(new_mms->pgd);
  747. // 迭代地拷贝用户空间
  748. for (int i = 0; i <= 255; ++i)
  749. {
  750. // 当前页表项为空
  751. if ((*(uint64_t *)(current_pgd + i)) == 0)
  752. continue;
  753. // 分配新的二级页表
  754. uint64_t *new_pdpt = (uint64_t *)kmalloc(PAGE_4K_SIZE, 0);
  755. memset(new_pdpt, 0, PAGE_4K_SIZE);
  756. // 在新的一级页表中设置新的二级页表表项
  757. set_pml4t(new_pml4t + i, mk_pml4t(virt_2_phys(new_pdpt), (*(current_pgd + i)) & 0xfffUL));
  758. uint64_t *current_pdpt = (uint64_t *)phys_2_virt((*(uint64_t *)(current_pgd + i)) & (~0xfffUL));
  759. // kdebug("current_pdpt=%#018lx, current_pid=%d", current_pdpt, current_pcb->pid);
  760. for (int j = 0; j < 512; ++j)
  761. {
  762. if (*(current_pdpt + j) == 0)
  763. continue;
  764. // 分配新的三级页表
  765. uint64_t *new_pdt = (uint64_t *)kmalloc(PAGE_4K_SIZE, 0);
  766. memset(new_pdt, 0, PAGE_4K_SIZE);
  767. // 在二级页表中填写新的三级页表
  768. // 在新的二级页表中设置三级页表的表项
  769. set_pdpt((uint64_t *)(new_pdpt + j), mk_pdpt(virt_2_phys(new_pdt), (*(current_pdpt + j)) & 0xfffUL));
  770. uint64_t *current_pdt = (uint64_t *)phys_2_virt((*(current_pdpt + j)) & (~0xfffUL));
  771. // kdebug("current_pdt=%#018lx", current_pdt);
  772. // 循环拷贝三级页表
  773. for (int k = 0; k < 512; ++k)
  774. {
  775. if (*(current_pdt + k) == 0)
  776. continue;
  777. // 获取新的物理页
  778. uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
  779. memset((void *)phys_2_virt(pa), 0, PAGE_2M_SIZE);
  780. set_pdt((uint64_t *)(new_pdt + k), mk_pdt(pa, *(current_pdt + k) & 0x1ffUL));
  781. // 拷贝数据
  782. memcpy(phys_2_virt(pa), phys_2_virt((*(current_pdt + k)) & (~0x1ffUL)), PAGE_2M_SIZE);
  783. }
  784. }
  785. }
  786. return retval;
  787. }
  788. /**
  789. * @brief 释放进程的页表
  790. *
  791. * @param pcb 要被释放页表的进程
  792. * @return uint64_t
  793. */
  794. uint64_t process_exit_mm(struct process_control_block *pcb)
  795. {
  796. if (pcb->flags & CLONE_VM)
  797. return 0;
  798. if (pcb->mm == NULL)
  799. {
  800. kdebug("pcb->mm==NULL");
  801. return 0;
  802. }
  803. if (pcb->mm->pgd == NULL)
  804. {
  805. kdebug("pcb->mm->pgd==NULL");
  806. return 0;
  807. }
  808. // 获取顶层页表
  809. pml4t_t *current_pgd = (pml4t_t *)phys_2_virt(pcb->mm->pgd);
  810. // 迭代地释放用户空间
  811. for (int i = 0; i <= 255; ++i)
  812. {
  813. // 当前页表项为空
  814. if ((current_pgd + i)->pml4t == 0)
  815. continue;
  816. // 二级页表entry
  817. pdpt_t *current_pdpt = (pdpt_t *)phys_2_virt((current_pgd + i)->pml4t & (~0xfffUL));
  818. // 遍历二级页表
  819. for (int j = 0; j < 512; ++j)
  820. {
  821. if ((current_pdpt + j)->pdpt == 0)
  822. continue;
  823. // 三级页表的entry
  824. pdt_t *current_pdt = (pdt_t *)phys_2_virt((current_pdpt + j)->pdpt & (~0xfffUL));
  825. // 释放三级页表的内存页
  826. for (int k = 0; k < 512; ++k)
  827. {
  828. if ((current_pdt + k)->pdt == 0)
  829. continue;
  830. // 存在4级页表
  831. if (unlikely(((current_pdt + k)->pdt & (1 << 7)) == 0))
  832. {
  833. // 存在4K页
  834. uint64_t *pt_ptr = (uint64_t *)phys_2_virt((current_pdt + k)->pdt & (~0x1fffUL));
  835. uint64_t *pte_ptr = pt_ptr;
  836. // 循环处理4K页表, 直接清空
  837. // todo: 当支持使用slab分配4K内存作为进程的4K页之后,在这里需要释放这些4K对象
  838. for (int16_t g = 0; g < 512; ++g, ++pte_ptr)
  839. *pte_ptr = 0;
  840. // 4级页表已经空了,释放页表
  841. if (unlikely(mm_check_page_table(pt_ptr)) == 0)
  842. kfree(pt_ptr);
  843. }
  844. else
  845. {
  846. // 释放内存页
  847. if (mm_is_2M_page((current_pdt + k)->pdt & (~0x1fffUL))) // 校验是否为内存中的物理页
  848. free_pages(Phy_to_2M_Page((current_pdt + k)->pdt & (~0x1fffUL)), 1);
  849. }
  850. }
  851. // 释放三级页表
  852. kfree(current_pdt);
  853. }
  854. // 释放二级页表
  855. kfree(current_pdpt);
  856. }
  857. // 释放顶层页表
  858. kfree(current_pgd);
  859. // 释放内存空间分布结构体
  860. kfree(pcb->mm);
  861. return 0;
  862. }
  863. /**
  864. * @brief 重写内核栈中的rbp地址
  865. *
  866. * @param new_regs 子进程的reg
  867. * @param new_pcb 子进程的pcb
  868. * @return int
  869. */
  870. static int process_rewrite_rbp(struct pt_regs *new_regs, struct process_control_block *new_pcb)
  871. {
  872. uint64_t new_top = ((uint64_t)new_pcb) + STACK_SIZE;
  873. uint64_t old_top = (uint64_t)(current_pcb) + STACK_SIZE;
  874. uint64_t *rbp = &new_regs->rbp;
  875. uint64_t *tmp = rbp;
  876. // 超出内核栈范围
  877. if ((uint64_t)*rbp >= old_top || (uint64_t)*rbp < (old_top - STACK_SIZE))
  878. return 0;
  879. while (1)
  880. {
  881. // 计算delta
  882. uint64_t delta = old_top - *rbp;
  883. // 计算新的rbp值
  884. uint64_t newVal = new_top - delta;
  885. // 新的值不合法
  886. if (unlikely((uint64_t)newVal >= new_top || (uint64_t)newVal < (new_top - STACK_SIZE)))
  887. break;
  888. // 将新的值写入对应位置
  889. *rbp = newVal;
  890. // 跳转栈帧
  891. rbp = (uint64_t *)*rbp;
  892. }
  893. // 设置内核态fork返回到enter_syscall_int()函数内的时候,rsp寄存器的值
  894. new_regs->rsp = new_top - (old_top - new_regs->rsp);
  895. return 0;
  896. }
  897. /**
  898. * @brief 拷贝当前进程的线程结构体
  899. *
  900. * @param clone_flags 克隆标志位
  901. * @param pcb 新的进程的pcb
  902. * @return uint64_t
  903. */
  904. uint64_t 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)
  905. {
  906. // 将线程结构体放置在pcb后方
  907. struct thread_struct *thd = (struct thread_struct *)(pcb + 1);
  908. memset(thd, 0, sizeof(struct thread_struct));
  909. pcb->thread = thd;
  910. struct pt_regs *child_regs = NULL;
  911. // 拷贝栈空间
  912. if (pcb->flags & PF_KFORK) // 内核态下的fork
  913. {
  914. // 内核态下则拷贝整个内核栈
  915. uint32_t size = ((uint64_t)current_pcb) + STACK_SIZE - (uint64_t)(current_regs);
  916. child_regs = (struct pt_regs *)(((uint64_t)pcb) + STACK_SIZE - size);
  917. memcpy(child_regs, (void *)current_regs, size);
  918. barrier();
  919. // 然后重写新的栈中,每个栈帧的rbp值
  920. process_rewrite_rbp(child_regs, pcb);
  921. }
  922. else
  923. {
  924. child_regs = (struct pt_regs *)((uint64_t)pcb + STACK_SIZE - sizeof(struct pt_regs));
  925. memcpy(child_regs, current_regs, sizeof(struct pt_regs));
  926. barrier();
  927. child_regs->rsp = stack_start;
  928. }
  929. // 设置子进程的返回值为0
  930. child_regs->rax = 0;
  931. if (pcb->flags & PF_KFORK)
  932. thd->rbp = (uint64_t)(child_regs + 1); // 设置新的内核线程开始执行时的rbp(也就是进入ret_from_system_call时的rbp)
  933. else
  934. thd->rbp = (uint64_t)pcb + STACK_SIZE;
  935. // 设置新的内核线程开始执行的时候的rsp
  936. thd->rsp = (uint64_t)child_regs;
  937. thd->fs = current_pcb->thread->fs;
  938. thd->gs = current_pcb->thread->gs;
  939. // 根据是否为内核线程、是否在内核态fork,设置进程的开始执行的地址
  940. if (pcb->flags & PF_KFORK)
  941. thd->rip = (uint64_t)ret_from_system_call;
  942. else if (pcb->flags & PF_KTHREAD && (!(pcb->flags & PF_KFORK)))
  943. thd->rip = (uint64_t)kernel_thread_func;
  944. else
  945. thd->rip = (uint64_t)ret_from_system_call;
  946. return 0;
  947. }
  948. /**
  949. * @brief todo: 回收线程结构体
  950. *
  951. * @param pcb
  952. */
  953. void process_exit_thread(struct process_control_block *pcb)
  954. {
  955. }
  956. // #pragma GCC pop_options