process.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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" : "=a"(prev->thread->fs));
  120. __asm__ __volatile__("movq %%gs, %0 \n\t" : "=a"(prev->thread->gs));
  121. __asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
  122. __asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
  123. }
  124. #pragma GCC pop_options
  125. /**
  126. * @brief 打开要执行的程序文件
  127. *
  128. * @param path
  129. * @return struct vfs_file_t*
  130. */
  131. struct vfs_file_t *process_open_exec_file(char *path)
  132. {
  133. struct vfs_dir_entry_t *dentry = NULL;
  134. struct vfs_file_t *filp = NULL;
  135. // kdebug("path=%s", path);
  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. // 对completion完成量进行测试
  428. __test_completion();
  429. // 对一些组件进行单元测试
  430. uint64_t tpid[] = {
  431. ktest_start(ktest_test_bitree, 0), ktest_start(ktest_test_kfifo, 0), ktest_start(ktest_test_mutex, 0),
  432. ktest_start(ktest_test_idr, 0),
  433. // usb_pid,
  434. };
  435. kinfo("Waiting test thread exit...");
  436. // 等待测试进程退出
  437. for (int i = 0; i < sizeof(tpid) / sizeof(uint64_t); ++i)
  438. waitpid(tpid[i], NULL, NULL);
  439. kinfo("All test done.");
  440. // 准备切换到用户态
  441. struct pt_regs *regs;
  442. // 若在后面这段代码中触发中断,return时会导致段选择子错误,从而触发#GP,因此这里需要cli
  443. cli();
  444. current_pcb->thread->rip = (ul)ret_from_system_call;
  445. current_pcb->thread->rsp = (ul)current_pcb + STACK_SIZE - sizeof(struct pt_regs);
  446. current_pcb->thread->fs = USER_DS | 0x3;
  447. barrier();
  448. current_pcb->thread->gs = USER_DS | 0x3;
  449. // 主动放弃内核线程身份
  450. current_pcb->flags &= (~PF_KTHREAD);
  451. kdebug("in initial_kernel_thread: flags=%ld", current_pcb->flags);
  452. regs = (struct pt_regs *)current_pcb->thread->rsp;
  453. // kdebug("current_pcb->thread->rsp=%#018lx", current_pcb->thread->rsp);
  454. current_pcb->flags = 0;
  455. // 将返回用户层的代码压入堆栈,向rdx传入regs的地址,然后jmp到do_execve这个系统调用api的处理函数
  456. // 这里的设计思路和switch_proc类似 加载用户态程序:shell.elf
  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"("/bin/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 要被释放的pcb
  1001. * @return int
  1002. */
  1003. int process_release_pcb(struct process_control_block *pcb)
  1004. {
  1005. // 释放子进程的页表
  1006. process_exit_mm(pcb);
  1007. if ((pcb->flags & PF_KTHREAD)) // 释放内核线程的worker private结构体
  1008. free_kthread_struct(pcb);
  1009. kfree(pcb);
  1010. return 0;
  1011. }
  1012. /**
  1013. * @brief 申请可用的文件句柄
  1014. *
  1015. * @return int
  1016. */
  1017. int process_fd_alloc(struct vfs_file_t *file)
  1018. {
  1019. int fd_num = -1;
  1020. struct vfs_file_t **f = current_pcb->fds;
  1021. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  1022. {
  1023. /* 找到指针数组中的空位 */
  1024. if (f[i] == NULL)
  1025. {
  1026. fd_num = i;
  1027. f[i] = file;
  1028. break;
  1029. }
  1030. }
  1031. return fd_num;
  1032. }
  1033. /**
  1034. * @brief 给pcb设置名字
  1035. *
  1036. * @param pcb 需要设置名字的pcb
  1037. * @param pcb_name 保存名字的char数组
  1038. */
  1039. static void __set_pcb_name(struct process_control_block *pcb, const char *pcb_name)
  1040. {
  1041. // todo:给pcb加锁
  1042. // spin_lock(&pcb->alloc_lock);
  1043. strncpy(pcb->name, pcb_name, PCB_NAME_LEN);
  1044. // spin_unlock(&pcb->alloc_lock);
  1045. }
  1046. /**
  1047. * @brief 给pcb设置名字
  1048. *
  1049. * @param pcb 需要设置名字的pcb
  1050. * @param pcb_name 保存名字的char数组
  1051. */
  1052. void process_set_pcb_name(struct process_control_block *pcb, const char *pcb_name)
  1053. {
  1054. __set_pcb_name(pcb, pcb_name);
  1055. }