process.c 36 KB

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