process.c 36 KB

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