process.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @file process.h
  3. * @author longjin
  4. * @brief 进程
  5. * @date 2022-01-29
  6. *
  7. * @copyright Copyright (c) 2022
  8. *
  9. */
  10. #pragma once
  11. #include "../common/cpu.h"
  12. #include "../common/glib.h"
  13. #include "../mm/mm.h"
  14. #include "../syscall/syscall.h"
  15. #include "ptrace.h"
  16. #include <common/errno.h>
  17. #include <filesystem/VFS/VFS.h>
  18. #include <process/wait_queue.h>
  19. // 进程最大可拥有的文件描述符数量
  20. #define PROC_MAX_FD_NUM 16
  21. // 进程的内核栈大小 32K
  22. #define STACK_SIZE 32768
  23. // 进程的运行状态
  24. // 正在运行
  25. #define PROC_RUNNING (1 << 0)
  26. // 可被中断
  27. #define PROC_INTERRUPTIBLE (1 << 1)
  28. // 不可被中断
  29. #define PROC_UNINTERRUPTIBLE (1 << 2)
  30. // 挂起
  31. #define PROC_ZOMBIE (1 << 3)
  32. // 已停止
  33. #define PROC_STOPPED (1 << 4)
  34. // 内核代码段基地址
  35. #define KERNEL_CS (0x08)
  36. // 内核数据段基地址
  37. #define KERNEL_DS (0x10)
  38. // 用户代码段基地址
  39. #define USER_CS (0x28)
  40. // 用户数据段基地址
  41. #define USER_DS (0x30)
  42. // 进程初始化时的数据拷贝标志位
  43. #define CLONE_FS (1 << 0) // 在进程间共享打开的文件
  44. #define CLONE_SIGNAL (1 << 1)
  45. #define CLONE_VM (1 << 2) // 在进程间共享虚拟内存空间
  46. /**
  47. * @brief 内存空间分布结构体
  48. * 包含了进程内存空间分布的信息
  49. */
  50. struct mm_struct
  51. {
  52. pml4t_t *pgd; // 内存页表指针
  53. // 代码段空间
  54. ul code_addr_start, code_addr_end;
  55. // 数据段空间
  56. ul data_addr_start, data_addr_end;
  57. // 只读数据段空间
  58. ul rodata_addr_start, rodata_addr_end;
  59. // BSS段的空间
  60. uint64_t bss_start, bss_end;
  61. // 动态内存分配区(堆区域)
  62. ul brk_start, brk_end;
  63. // 应用层栈基地址
  64. ul stack_start;
  65. };
  66. struct thread_struct
  67. {
  68. // 内核层栈基指针
  69. ul rbp; // in tss rsp0
  70. // 内核层代码指针
  71. ul rip;
  72. // 内核层栈指针
  73. ul rsp;
  74. ul fs, gs;
  75. ul cr2;
  76. // 异常号
  77. ul trap_num;
  78. // 错误码
  79. ul err_code;
  80. };
  81. // ========= pcb->flags =========
  82. // 进程标志位
  83. #define PF_KTHREAD (1UL << 0) // 内核线程
  84. #define PF_NEED_SCHED (1UL << 1) // 进程需要被调度
  85. #define PF_VFORK (1UL << 2) // 标志进程是否由于vfork而存在资源共享
  86. /**
  87. * @brief 进程控制块
  88. *
  89. */
  90. struct process_control_block
  91. {
  92. // 进程的状态
  93. volatile long state;
  94. // 进程标志:进程、线程、内核线程
  95. unsigned long flags;
  96. int64_t preempt_count; // 持有的自旋锁的数量
  97. long signal;
  98. long cpu_id; // 当前进程在哪个CPU核心上运行
  99. // 内存空间分布结构体, 记录内存页表和程序段信息
  100. struct mm_struct *mm;
  101. // 进程切换时保存的状态信息
  102. struct thread_struct *thread;
  103. // 连接各个pcb的双向链表(todo:删除这个变量)
  104. struct List list;
  105. // 地址空间范围
  106. // 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
  107. // 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
  108. uint64_t addr_limit;
  109. long pid;
  110. long priority; // 优先级
  111. int64_t virtual_runtime; // 虚拟运行时间
  112. // 进程拥有的文件描述符的指针数组
  113. // todo: 改用动态指针数组
  114. struct vfs_file_t *fds[PROC_MAX_FD_NUM];
  115. // 链表中的下一个pcb
  116. struct process_control_block *next_pcb;
  117. // 父进程的pcb
  118. struct process_control_block *parent_pcb;
  119. int32_t exit_code; // 进程退出时的返回码
  120. wait_queue_node_t wait_child_proc_exit; // 子进程退出等待队列
  121. };
  122. // 将进程的pcb和内核栈融合到一起,8字节对齐
  123. union proc_union
  124. {
  125. struct process_control_block pcb;
  126. ul stack[STACK_SIZE / sizeof(ul)];
  127. } __attribute__((aligned(8)));
  128. // 设置初始进程的PCB
  129. #define INITIAL_PROC(proc) \
  130. { \
  131. .state = PROC_UNINTERRUPTIBLE, \
  132. .flags = PF_KTHREAD, \
  133. .preempt_count = 0, \
  134. .signal = 0, \
  135. .cpu_id = 0, \
  136. .mm = &initial_mm, \
  137. .thread = &initial_thread, \
  138. .addr_limit = 0xffffffffffffffff, \
  139. .pid = 0, \
  140. .priority = 2, \
  141. .virtual_runtime = 0, \
  142. .fds = {0}, \
  143. .next_pcb = &proc, \
  144. .parent_pcb = &proc, \
  145. .exit_code = 0, \
  146. .wait_child_proc_exit = 0 \
  147. }
  148. /**
  149. * @brief 任务状态段结构体
  150. *
  151. */
  152. struct tss_struct
  153. {
  154. unsigned int reserved0;
  155. ul rsp0;
  156. ul rsp1;
  157. ul rsp2;
  158. ul reserved1;
  159. ul ist1;
  160. ul ist2;
  161. ul ist3;
  162. ul ist4;
  163. ul ist5;
  164. ul ist6;
  165. ul ist7;
  166. ul reserved2;
  167. unsigned short reserved3;
  168. // io位图基地址
  169. unsigned short io_map_base_addr;
  170. } __attribute__((packed)); // 使用packed表明是紧凑结构,编译器不会对成员变量进行字节对齐。
  171. // 设置初始进程的tss
  172. #define INITIAL_TSS \
  173. { \
  174. .reserved0 = 0, \
  175. .rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  176. .rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  177. .rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  178. .reserved1 = 0, \
  179. .ist1 = 0xffff800000007c00, \
  180. .ist2 = 0xffff800000007c00, \
  181. .ist3 = 0xffff800000007c00, \
  182. .ist4 = 0xffff800000007c00, \
  183. .ist5 = 0xffff800000007c00, \
  184. .ist6 = 0xffff800000007c00, \
  185. .ist7 = 0xffff800000007c00, \
  186. .reserved2 = 0, \
  187. .reserved3 = 0, \
  188. .io_map_base_addr = 0 \
  189. }
  190. // 获取当前的pcb
  191. struct process_control_block *get_current_pcb()
  192. {
  193. struct process_control_block *current = NULL;
  194. // 利用了当前pcb和栈空间总大小为32k大小对齐,将rsp低15位清空,即可获得pcb的起始地址
  195. __asm__ __volatile__("andq %%rsp, %0 \n\t"
  196. : "=r"(current)
  197. : "0"(~32767UL));
  198. return current;
  199. };
  200. #define current_pcb get_current_pcb()
  201. #define GET_CURRENT_PCB \
  202. "movq %rsp, %rbx \n\t" \
  203. "andq $-32768, %rbx\n\t"
  204. /**
  205. * @brief 切换进程上下文
  206. * 先把rbp和rax保存到栈中,然后将rsp和rip保存到prev的thread结构体中
  207. * 然后调用__switch_to切换栈,配置其他信息,最后恢复下一个进程的rax rbp。
  208. */
  209. #define switch_proc(prev, next) \
  210. do \
  211. { \
  212. __asm__ __volatile__("pushq %%rbp \n\t" \
  213. "pushq %%rax \n\t" \
  214. "movq %%rsp, %0 \n\t" \
  215. "movq %2, %%rsp \n\t" \
  216. "leaq switch_proc_ret_addr(%%rip), %%rax \n\t" \
  217. "movq %%rax, %1 \n\t" \
  218. "pushq %3 \n\t" \
  219. "jmp __switch_to \n\t" \
  220. "switch_proc_ret_addr: \n\t" \
  221. "popq %%rax \n\t" \
  222. "popq %%rbp \n\t" \
  223. : "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
  224. : "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
  225. : "memory"); \
  226. } while (0)
  227. /**
  228. * @brief 初始化系统的第一个进程
  229. *
  230. */
  231. void process_init();
  232. /**
  233. * @brief fork当前进程
  234. *
  235. * @param regs 新的寄存器值
  236. * @param clone_flags 克隆标志
  237. * @param stack_start 堆栈开始地址
  238. * @param stack_size 堆栈大小
  239. * @return unsigned long
  240. */
  241. unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);
  242. /**
  243. * @brief 根据pid获取进程的pcb
  244. *
  245. * @param pid
  246. * @return struct process_control_block*
  247. */
  248. struct process_control_block *process_get_pcb(long pid);
  249. /**
  250. * @brief 将进程加入到调度器的就绪队列中
  251. *
  252. * @param pcb 进程的pcb
  253. */
  254. void process_wakeup(struct process_control_block *pcb);
  255. /**
  256. * @brief 使当前进程去执行新的代码
  257. *
  258. * @param regs 当前进程的寄存器
  259. * @param path 可执行程序的路径
  260. * @param argv 参数列表
  261. * @param envp 环境变量
  262. * @return ul 错误码
  263. */
  264. ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[]);
  265. /**
  266. * @brief 释放进程的页表
  267. *
  268. * @param pcb 要被释放页表的进程
  269. * @return uint64_t
  270. */
  271. uint64_t process_exit_mm(struct process_control_block *pcb);
  272. /**
  273. * @brief 进程退出时执行的函数
  274. *
  275. * @param code 返回码
  276. * @return ul
  277. */
  278. ul process_do_exit(ul code);
  279. /**
  280. * @brief 当子进程退出后向父进程发送通知
  281. *
  282. */
  283. void process_exit_notify();
  284. /**
  285. * @brief 切换页表
  286. * @param prev 前一个进程的pcb
  287. * @param next 下一个进程的pcb
  288. *
  289. */
  290. #define process_switch_mm(next_pcb) \
  291. do \
  292. { \
  293. asm volatile("movq %0, %%cr3 \n\t" ::"r"(next_pcb->mm->pgd) \
  294. : "memory"); \
  295. } while (0)
  296. // flush_tlb(); \
  297. // 获取当前cpu id
  298. #define proc_current_cpu_id (current_pcb->cpu_id)
  299. extern unsigned long head_stack_start; // 导出内核层栈基地址(定义在head.S)
  300. extern ul _stack_start;
  301. extern void ret_from_intr(void); // 导出从中断返回的函数(定义在entry.S)
  302. extern struct tss_struct initial_tss[MAX_CPU_NUM];
  303. extern struct mm_struct initial_mm;
  304. extern struct thread_struct initial_thread;
  305. extern union proc_union initial_proc_union;
  306. extern struct process_control_block *initial_proc[MAX_CPU_NUM];