process.c 31 KB

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