process.c 36 KB

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