process.c 30 KB

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