process.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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_mm.vmas = NULL;
  497. initial_tss[proc_current_cpu_id].rsp0 = initial_thread.rbp;
  498. // ========= 在IDLE进程的顶层页表中添加对内核地址空间的映射 =====================
  499. // 由于IDLE进程的顶层页表的高地址部分会被后续进程所复制,为了使所有进程能够共享相同的内核空间,
  500. // 因此需要先在IDLE进程的顶层页表内映射二级页表
  501. uint64_t *idle_pml4t_vaddr = (uint64_t *)phys_2_virt((uint64_t)get_CR3() & (~0xfffUL));
  502. for (int i = 256; i < 512; ++i)
  503. {
  504. uint64_t *tmp = idle_pml4t_vaddr + i;
  505. barrier();
  506. if (*tmp == 0)
  507. {
  508. void *pdpt = kmalloc(PAGE_4K_SIZE, 0);
  509. barrier();
  510. memset(pdpt, 0, PAGE_4K_SIZE);
  511. barrier();
  512. set_pml4t(tmp, mk_pml4t(virt_2_phys(pdpt), PAGE_KERNEL_PGT));
  513. }
  514. }
  515. barrier();
  516. flush_tlb();
  517. /*
  518. kdebug("initial_thread.rbp=%#018lx", initial_thread.rbp);
  519. kdebug("initial_tss[0].rsp1=%#018lx", initial_tss[0].rsp1);
  520. kdebug("initial_tss[0].ist1=%#018lx", initial_tss[0].ist1);
  521. */
  522. // 初始化pid的写锁
  523. spin_init(&process_global_pid_write_lock);
  524. // 初始化进程的循环链表
  525. list_init(&initial_proc_union.pcb.list);
  526. barrier();
  527. kernel_thread(initial_kernel_thread, 10, CLONE_FS | CLONE_SIGNAL); // 初始化内核线程
  528. barrier();
  529. initial_proc_union.pcb.state = PROC_RUNNING;
  530. initial_proc_union.pcb.preempt_count = 0;
  531. initial_proc_union.pcb.cpu_id = 0;
  532. initial_proc_union.pcb.virtual_runtime = (1UL << 60);
  533. current_pcb->virtual_runtime = (1UL << 60);
  534. }
  535. /**
  536. * @brief fork当前进程
  537. *
  538. * @param regs 新的寄存器值
  539. * @param clone_flags 克隆标志
  540. * @param stack_start 堆栈开始地址
  541. * @param stack_size 堆栈大小
  542. * @return unsigned long
  543. */
  544. unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size)
  545. {
  546. int retval = 0;
  547. struct process_control_block *tsk = NULL;
  548. // 为新的进程分配栈空间,并将pcb放置在底部
  549. tsk = (struct process_control_block *)kmalloc(STACK_SIZE, 0);
  550. barrier();
  551. if (tsk == NULL)
  552. {
  553. retval = -ENOMEM;
  554. return retval;
  555. }
  556. barrier();
  557. memset(tsk, 0, sizeof(struct process_control_block));
  558. io_mfence();
  559. // 将当前进程的pcb复制到新的pcb内
  560. memcpy(tsk, current_pcb, sizeof(struct process_control_block));
  561. io_mfence();
  562. // 初始化进程的循环链表结点
  563. list_init(&tsk->list);
  564. io_mfence();
  565. // 判断是否为内核态调用fork
  566. if (current_pcb->flags & PF_KTHREAD && stack_start != 0)
  567. tsk->flags |= PF_KFORK;
  568. tsk->priority = 2;
  569. tsk->preempt_count = 0;
  570. // 增加全局的pid并赋值给新进程的pid
  571. spin_lock(&process_global_pid_write_lock);
  572. tsk->pid = process_global_pid++;
  573. barrier();
  574. // 加入到进程链表中
  575. tsk->next_pcb = initial_proc_union.pcb.next_pcb;
  576. barrier();
  577. initial_proc_union.pcb.next_pcb = tsk;
  578. barrier();
  579. tsk->parent_pcb = current_pcb;
  580. barrier();
  581. spin_unlock(&process_global_pid_write_lock);
  582. tsk->cpu_id = proc_current_cpu_id;
  583. tsk->state = PROC_UNINTERRUPTIBLE;
  584. tsk->parent_pcb = current_pcb;
  585. wait_queue_init(&tsk->wait_child_proc_exit, NULL);
  586. barrier();
  587. list_init(&tsk->list);
  588. retval = -ENOMEM;
  589. // 拷贝标志位
  590. if (process_copy_flags(clone_flags, tsk))
  591. goto copy_flags_failed;
  592. // 拷贝内存空间分布结构体
  593. if (process_copy_mm(clone_flags, tsk))
  594. goto copy_mm_failed;
  595. // 拷贝文件
  596. if (process_copy_files(clone_flags, tsk))
  597. goto copy_files_failed;
  598. // 拷贝线程结构体
  599. if (process_copy_thread(clone_flags, tsk, stack_start, stack_size, regs))
  600. goto copy_thread_failed;
  601. // 拷贝成功
  602. retval = tsk->pid;
  603. tsk->flags &= ~PF_KFORK;
  604. // 唤醒进程
  605. process_wakeup(tsk);
  606. return retval;
  607. copy_thread_failed:;
  608. // 回收线程
  609. process_exit_thread(tsk);
  610. copy_files_failed:;
  611. // 回收文件
  612. process_exit_files(tsk);
  613. copy_mm_failed:;
  614. // 回收内存空间分布结构体
  615. process_exit_mm(tsk);
  616. copy_flags_failed:;
  617. kfree(tsk);
  618. return retval;
  619. return 0;
  620. }
  621. /**
  622. * @brief 根据pid获取进程的pcb
  623. *
  624. * @param pid
  625. * @return struct process_control_block*
  626. */
  627. struct process_control_block *process_get_pcb(long pid)
  628. {
  629. struct process_control_block *pcb = initial_proc_union.pcb.next_pcb;
  630. // 使用蛮力法搜索指定pid的pcb
  631. // todo: 使用哈希表来管理pcb
  632. for (; pcb != &initial_proc_union.pcb; pcb = pcb->next_pcb)
  633. {
  634. if (pcb->pid == pid)
  635. return pcb;
  636. }
  637. return NULL;
  638. }
  639. /**
  640. * @brief 将进程加入到调度器的就绪队列中
  641. *
  642. * @param pcb 进程的pcb
  643. */
  644. void process_wakeup(struct process_control_block *pcb)
  645. {
  646. pcb->state = PROC_RUNNING;
  647. sched_cfs_enqueue(pcb);
  648. }
  649. /**
  650. * @brief 将进程加入到调度器的就绪队列中,并标志当前进程需要被调度
  651. *
  652. * @param pcb 进程的pcb
  653. */
  654. void process_wakeup_immediately(struct process_control_block *pcb)
  655. {
  656. pcb->state = PROC_RUNNING;
  657. sched_cfs_enqueue(pcb);
  658. // 将当前进程标志为需要调度,缩短新进程被wakeup的时间
  659. current_pcb->flags |= PF_NEED_SCHED;
  660. }
  661. /**
  662. * @brief 拷贝当前进程的标志位
  663. *
  664. * @param clone_flags 克隆标志位
  665. * @param pcb 新的进程的pcb
  666. * @return uint64_t
  667. */
  668. uint64_t process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb)
  669. {
  670. if (clone_flags & CLONE_VM)
  671. pcb->flags |= PF_VFORK;
  672. return 0;
  673. }
  674. /**
  675. * @brief 拷贝当前进程的文件描述符等信息
  676. *
  677. * @param clone_flags 克隆标志位
  678. * @param pcb 新的进程的pcb
  679. * @return uint64_t
  680. */
  681. uint64_t process_copy_files(uint64_t clone_flags, struct process_control_block *pcb)
  682. {
  683. int retval = 0;
  684. // 如果CLONE_FS被置位,那么子进程与父进程共享文件描述符
  685. // 文件描述符已经在复制pcb时被拷贝
  686. if (clone_flags & CLONE_FS)
  687. return retval;
  688. // 为新进程拷贝新的文件描述符
  689. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  690. {
  691. if (current_pcb->fds[i] == NULL)
  692. continue;
  693. pcb->fds[i] = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
  694. memcpy(pcb->fds[i], current_pcb->fds[i], sizeof(struct vfs_file_t));
  695. }
  696. return retval;
  697. }
  698. /**
  699. * @brief 回收进程的所有文件描述符
  700. *
  701. * @param pcb 要被回收的进程的pcb
  702. * @return uint64_t
  703. */
  704. uint64_t process_exit_files(struct process_control_block *pcb)
  705. {
  706. // 不与父进程共享文件描述符
  707. if (!(pcb->flags & PF_VFORK))
  708. {
  709. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  710. {
  711. if (pcb->fds[i] == NULL)
  712. continue;
  713. kfree(pcb->fds[i]);
  714. }
  715. }
  716. // 清空当前进程的文件描述符列表
  717. memset(pcb->fds, 0, sizeof(struct vfs_file_t *) * PROC_MAX_FD_NUM);
  718. }
  719. /**
  720. * @brief 拷贝当前进程的内存空间分布结构体信息
  721. *
  722. * @param clone_flags 克隆标志位
  723. * @param pcb 新的进程的pcb
  724. * @return uint64_t
  725. */
  726. uint64_t process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb)
  727. {
  728. int retval = 0;
  729. // 与父进程共享内存空间
  730. if (clone_flags & CLONE_VM)
  731. {
  732. pcb->mm = current_pcb->mm;
  733. return retval;
  734. }
  735. // 分配新的内存空间分布结构体
  736. struct mm_struct *new_mms = (struct mm_struct *)kmalloc(sizeof(struct mm_struct), 0);
  737. memset(new_mms, 0, sizeof(struct mm_struct));
  738. memcpy(new_mms, current_pcb->mm, sizeof(struct mm_struct));
  739. pcb->mm = new_mms;
  740. // 分配顶层页表, 并设置顶层页表的物理地址
  741. new_mms->pgd = (pml4t_t *)virt_2_phys(kmalloc(PAGE_4K_SIZE, 0));
  742. // 由于高2K部分为内核空间,在接下来需要覆盖其数据,因此不用清零
  743. memset(phys_2_virt(new_mms->pgd), 0, PAGE_4K_SIZE / 2);
  744. // 拷贝内核空间的页表指针
  745. memcpy(phys_2_virt(new_mms->pgd) + 256, phys_2_virt(initial_proc[proc_current_cpu_id]->mm->pgd) + 256, PAGE_4K_SIZE / 2);
  746. uint64_t *current_pgd = (uint64_t *)phys_2_virt(current_pcb->mm->pgd);
  747. uint64_t *new_pml4t = (uint64_t *)phys_2_virt(new_mms->pgd);
  748. // 迭代地拷贝用户空间
  749. for (int i = 0; i <= 255; ++i)
  750. {
  751. // 当前页表项为空
  752. if ((*(uint64_t *)(current_pgd + i)) == 0)
  753. continue;
  754. // 分配新的二级页表
  755. uint64_t *new_pdpt = (uint64_t *)kmalloc(PAGE_4K_SIZE, 0);
  756. memset(new_pdpt, 0, PAGE_4K_SIZE);
  757. // 在新的一级页表中设置新的二级页表表项
  758. set_pml4t(new_pml4t + i, mk_pml4t(virt_2_phys(new_pdpt), (*(current_pgd + i)) & 0xfffUL));
  759. uint64_t *current_pdpt = (uint64_t *)phys_2_virt((*(uint64_t *)(current_pgd + i)) & (~0xfffUL));
  760. // kdebug("current_pdpt=%#018lx, current_pid=%d", current_pdpt, current_pcb->pid);
  761. for (int j = 0; j < 512; ++j)
  762. {
  763. if (*(current_pdpt + j) == 0)
  764. continue;
  765. // 分配新的三级页表
  766. uint64_t *new_pdt = (uint64_t *)kmalloc(PAGE_4K_SIZE, 0);
  767. memset(new_pdt, 0, PAGE_4K_SIZE);
  768. // 在二级页表中填写新的三级页表
  769. // 在新的二级页表中设置三级页表的表项
  770. set_pdpt((uint64_t *)(new_pdpt + j), mk_pdpt(virt_2_phys(new_pdt), (*(current_pdpt + j)) & 0xfffUL));
  771. uint64_t *current_pdt = (uint64_t *)phys_2_virt((*(current_pdpt + j)) & (~0xfffUL));
  772. // kdebug("current_pdt=%#018lx", current_pdt);
  773. // 循环拷贝三级页表
  774. for (int k = 0; k < 512; ++k)
  775. {
  776. if (*(current_pdt + k) == 0)
  777. continue;
  778. // 获取新的物理页
  779. uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
  780. memset((void *)phys_2_virt(pa), 0, PAGE_2M_SIZE);
  781. set_pdt((uint64_t *)(new_pdt + k), mk_pdt(pa, *(current_pdt + k) & 0x1ffUL));
  782. // 拷贝数据
  783. memcpy(phys_2_virt(pa), phys_2_virt((*(current_pdt + k)) & (~0x1ffUL)), PAGE_2M_SIZE);
  784. }
  785. }
  786. }
  787. return retval;
  788. }
  789. /**
  790. * @brief 释放进程的页表
  791. *
  792. * @param pcb 要被释放页表的进程
  793. * @return uint64_t
  794. */
  795. uint64_t process_exit_mm(struct process_control_block *pcb)
  796. {
  797. if (pcb->flags & CLONE_VM)
  798. return 0;
  799. if (pcb->mm == NULL)
  800. {
  801. kdebug("pcb->mm==NULL");
  802. return 0;
  803. }
  804. if (pcb->mm->pgd == NULL)
  805. {
  806. kdebug("pcb->mm->pgd==NULL");
  807. return 0;
  808. }
  809. // 获取顶层页表
  810. pml4t_t *current_pgd = (pml4t_t *)phys_2_virt(pcb->mm->pgd);
  811. // 迭代地释放用户空间
  812. for (int i = 0; i <= 255; ++i)
  813. {
  814. // 当前页表项为空
  815. if ((current_pgd + i)->pml4t == 0)
  816. continue;
  817. // 二级页表entry
  818. pdpt_t *current_pdpt = (pdpt_t *)phys_2_virt((current_pgd + i)->pml4t & (~0xfffUL));
  819. // 遍历二级页表
  820. for (int j = 0; j < 512; ++j)
  821. {
  822. if ((current_pdpt + j)->pdpt == 0)
  823. continue;
  824. // 三级页表的entry
  825. pdt_t *current_pdt = (pdt_t *)phys_2_virt((current_pdpt + j)->pdpt & (~0xfffUL));
  826. // 释放三级页表的内存页
  827. for (int k = 0; k < 512; ++k)
  828. {
  829. if ((current_pdt + k)->pdt == 0)
  830. continue;
  831. // 存在4级页表
  832. if (unlikely(((current_pdt + k)->pdt & (1 << 7)) == 0))
  833. {
  834. // 存在4K页
  835. uint64_t *pt_ptr = (uint64_t *)phys_2_virt((current_pdt + k)->pdt & (~0x1fffUL));
  836. uint64_t *pte_ptr = pt_ptr;
  837. // 循环处理4K页表, 直接清空
  838. // todo: 当支持使用slab分配4K内存作为进程的4K页之后,在这里需要释放这些4K对象
  839. for (int16_t g = 0; g < 512; ++g, ++pte_ptr)
  840. *pte_ptr = 0;
  841. // 4级页表已经空了,释放页表
  842. if (unlikely(mm_check_page_table(pt_ptr)) == 0)
  843. kfree(pt_ptr);
  844. }
  845. else
  846. {
  847. // 释放内存页
  848. if (mm_is_2M_page((current_pdt + k)->pdt & (~0x1fffUL))) // 校验是否为内存中的物理页
  849. free_pages(Phy_to_2M_Page((current_pdt + k)->pdt & (~0x1fffUL)), 1);
  850. }
  851. }
  852. // 释放三级页表
  853. kfree(current_pdt);
  854. }
  855. // 释放二级页表
  856. kfree(current_pdpt);
  857. }
  858. // 释放顶层页表
  859. kfree(current_pgd);
  860. // 释放内存空间分布结构体
  861. kfree(pcb->mm);
  862. return 0;
  863. }
  864. /**
  865. * @brief 重写内核栈中的rbp地址
  866. *
  867. * @param new_regs 子进程的reg
  868. * @param new_pcb 子进程的pcb
  869. * @return int
  870. */
  871. static int process_rewrite_rbp(struct pt_regs *new_regs, struct process_control_block *new_pcb)
  872. {
  873. uint64_t new_top = ((uint64_t)new_pcb) + STACK_SIZE;
  874. uint64_t old_top = (uint64_t)(current_pcb) + STACK_SIZE;
  875. uint64_t *rbp = &new_regs->rbp;
  876. uint64_t *tmp = rbp;
  877. // 超出内核栈范围
  878. if ((uint64_t)*rbp >= old_top || (uint64_t)*rbp < (old_top - STACK_SIZE))
  879. return 0;
  880. while (1)
  881. {
  882. // 计算delta
  883. uint64_t delta = old_top - *rbp;
  884. // 计算新的rbp值
  885. uint64_t newVal = new_top - delta;
  886. // 新的值不合法
  887. if (unlikely((uint64_t)newVal >= new_top || (uint64_t)newVal < (new_top - STACK_SIZE)))
  888. break;
  889. // 将新的值写入对应位置
  890. *rbp = newVal;
  891. // 跳转栈帧
  892. rbp = (uint64_t *)*rbp;
  893. }
  894. // 设置内核态fork返回到enter_syscall_int()函数内的时候,rsp寄存器的值
  895. new_regs->rsp = new_top - (old_top - new_regs->rsp);
  896. return 0;
  897. }
  898. /**
  899. * @brief 拷贝当前进程的线程结构体
  900. *
  901. * @param clone_flags 克隆标志位
  902. * @param pcb 新的进程的pcb
  903. * @return uint64_t
  904. */
  905. 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)
  906. {
  907. // 将线程结构体放置在pcb后方
  908. struct thread_struct *thd = (struct thread_struct *)(pcb + 1);
  909. memset(thd, 0, sizeof(struct thread_struct));
  910. pcb->thread = thd;
  911. struct pt_regs *child_regs = NULL;
  912. // 拷贝栈空间
  913. if (pcb->flags & PF_KFORK) // 内核态下的fork
  914. {
  915. // 内核态下则拷贝整个内核栈
  916. uint32_t size = ((uint64_t)current_pcb) + STACK_SIZE - (uint64_t)(current_regs);
  917. child_regs = (struct pt_regs *)(((uint64_t)pcb) + STACK_SIZE - size);
  918. memcpy(child_regs, (void *)current_regs, size);
  919. barrier();
  920. // 然后重写新的栈中,每个栈帧的rbp值
  921. process_rewrite_rbp(child_regs, pcb);
  922. }
  923. else
  924. {
  925. child_regs = (struct pt_regs *)((uint64_t)pcb + STACK_SIZE - sizeof(struct pt_regs));
  926. memcpy(child_regs, current_regs, sizeof(struct pt_regs));
  927. barrier();
  928. child_regs->rsp = stack_start;
  929. }
  930. // 设置子进程的返回值为0
  931. child_regs->rax = 0;
  932. if (pcb->flags & PF_KFORK)
  933. thd->rbp = (uint64_t)(child_regs + 1); // 设置新的内核线程开始执行时的rbp(也就是进入ret_from_system_call时的rbp)
  934. else
  935. thd->rbp = (uint64_t)pcb + STACK_SIZE;
  936. // 设置新的内核线程开始执行的时候的rsp
  937. thd->rsp = (uint64_t)child_regs;
  938. thd->fs = current_pcb->thread->fs;
  939. thd->gs = current_pcb->thread->gs;
  940. // 根据是否为内核线程、是否在内核态fork,设置进程的开始执行的地址
  941. if (pcb->flags & PF_KFORK)
  942. thd->rip = (uint64_t)ret_from_system_call;
  943. else if (pcb->flags & PF_KTHREAD && (!(pcb->flags & PF_KFORK)))
  944. thd->rip = (uint64_t)kernel_thread_func;
  945. else
  946. thd->rip = (uint64_t)ret_from_system_call;
  947. return 0;
  948. }
  949. /**
  950. * @brief todo: 回收线程结构体
  951. *
  952. * @param pcb
  953. */
  954. void process_exit_thread(struct process_control_block *pcb)
  955. {
  956. }
  957. // #pragma GCC pop_options