process.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <syscall/syscall.h>
  14. #include "ptrace.h"
  15. #include <common/errno.h>
  16. #include <filesystem/VFS/VFS.h>
  17. #include <common/wait_queue.h>
  18. #include <mm/mm-types.h>
  19. #if ARCH(I386) || ARCH(X86_64)
  20. #include <arch/x86_64/current.h>
  21. #else
  22. #error Unsupported architecture!
  23. #endif
  24. #include "proc-types.h"
  25. // 设置初始进程的PCB
  26. #define INITIAL_PROC(proc) \
  27. { \
  28. .state = PROC_UNINTERRUPTIBLE, \
  29. .flags = PF_KTHREAD, \
  30. .preempt_count = 0, \
  31. .signal = 0, \
  32. .cpu_id = 0, \
  33. .mm = &initial_mm, \
  34. .thread = &initial_thread, \
  35. .addr_limit = 0xffffffffffffffff, \
  36. .pid = 0, \
  37. .priority = 2, \
  38. .virtual_runtime = 0, \
  39. .fds = {0}, \
  40. .next_pcb = &proc, \
  41. .parent_pcb = &proc, \
  42. .exit_code = 0, \
  43. .wait_child_proc_exit = 0, \
  44. .worker_private = NULL, \
  45. .policy = SCHED_NORMAL \
  46. }
  47. /**
  48. * @brief 任务状态段结构体
  49. *
  50. */
  51. // 设置初始进程的tss
  52. #define INITIAL_TSS \
  53. { \
  54. .reserved0 = 0, \
  55. .rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  56. .rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  57. .rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  58. .reserved1 = 0, \
  59. .ist1 = 0xffff800000007c00, \
  60. .ist2 = 0xffff800000007c00, \
  61. .ist3 = 0xffff800000007c00, \
  62. .ist4 = 0xffff800000007c00, \
  63. .ist5 = 0xffff800000007c00, \
  64. .ist6 = 0xffff800000007c00, \
  65. .ist7 = 0xffff800000007c00, \
  66. .reserved2 = 0, \
  67. .reserved3 = 0, \
  68. .io_map_base_addr = 0 \
  69. }
  70. #define GET_CURRENT_PCB \
  71. "movq %rsp, %rbx \n\t" \
  72. "andq $-32768, %rbx\n\t"
  73. /**
  74. * @brief 切换进程上下文
  75. * 先把rbp和rax保存到栈中,然后将rsp和rip保存到prev的thread结构体中
  76. * 然后调用__switch_to切换栈,配置其他信息,最后恢复下一个进程的rax rbp。
  77. */
  78. #define switch_proc(prev, next) \
  79. do \
  80. { \
  81. __asm__ __volatile__("pushq %%rbp \n\t" \
  82. "pushq %%rax \n\t" \
  83. "movq %%rsp, %0 \n\t" \
  84. "movq %2, %%rsp \n\t" \
  85. "leaq switch_proc_ret_addr(%%rip), %%rax \n\t" \
  86. "movq %%rax, %1 \n\t" \
  87. "pushq %3 \n\t" \
  88. "jmp __switch_to \n\t" \
  89. "switch_proc_ret_addr: \n\t" \
  90. "popq %%rax \n\t" \
  91. "popq %%rbp \n\t" \
  92. : "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
  93. : "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
  94. : "memory"); \
  95. } while (0)
  96. /**
  97. * @brief 初始化系统的第一个进程
  98. *
  99. */
  100. void process_init();
  101. /**
  102. * @brief fork当前进程
  103. *
  104. * @param regs 新的寄存器值
  105. * @param clone_flags 克隆标志
  106. * @param stack_start 堆栈开始地址
  107. * @param stack_size 堆栈大小
  108. * @return unsigned long
  109. */
  110. unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);
  111. /**
  112. * @brief 根据pid获取进程的pcb
  113. *
  114. * @param pid
  115. * @return struct process_control_block*
  116. */
  117. struct process_control_block *process_get_pcb(long pid);
  118. /**
  119. * @brief 将进程加入到调度器的就绪队列中
  120. *
  121. * @param pcb 进程的pcb
  122. */
  123. int process_wakeup(struct process_control_block *pcb);
  124. /**
  125. * @brief 将进程加入到调度器的就绪队列中,并标志当前进程需要被调度
  126. *
  127. * @param pcb 进程的pcb
  128. */
  129. int process_wakeup_immediately(struct process_control_block *pcb);
  130. /**
  131. * @brief 使当前进程去执行新的代码
  132. *
  133. * @param regs 当前进程的寄存器
  134. * @param path 可执行程序的路径
  135. * @param argv 参数列表
  136. * @param envp 环境变量
  137. * @return ul 错误码
  138. */
  139. ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[]);
  140. /**
  141. * @brief 释放进程的页表
  142. *
  143. * @param pcb 要被释放页表的进程
  144. * @return uint64_t
  145. */
  146. uint64_t process_exit_mm(struct process_control_block *pcb);
  147. /**
  148. * @brief 进程退出时执行的函数
  149. *
  150. * @param code 返回码
  151. * @return ul
  152. */
  153. ul process_do_exit(ul code);
  154. /**
  155. * @brief 当子进程退出后向父进程发送通知
  156. *
  157. */
  158. void process_exit_notify();
  159. /**
  160. * @brief 初始化内核进程
  161. *
  162. * @param fn 目标程序的地址
  163. * @param arg 向目标程序传入的参数
  164. * @param flags
  165. * @return int
  166. */
  167. pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  168. int process_fd_alloc(struct vfs_file_t *file);
  169. /**
  170. * @brief 释放pcb
  171. *
  172. * @param pcb
  173. * @return int
  174. */
  175. int process_release_pcb(struct process_control_block *pcb);
  176. /**
  177. * @brief 切换页表
  178. * @param prev 前一个进程的pcb
  179. * @param next 下一个进程的pcb
  180. *
  181. */
  182. #define process_switch_mm(next_pcb) \
  183. do \
  184. { \
  185. asm volatile("movq %0, %%cr3 \n\t" ::"r"(next_pcb->mm->pgd) \
  186. : "memory"); \
  187. } while (0)
  188. // flush_tlb();
  189. // 获取当前cpu id
  190. #define proc_current_cpu_id (current_pcb->cpu_id)
  191. extern unsigned long head_stack_start; // 导出内核层栈基地址(定义在head.S)
  192. extern ul _stack_start;
  193. extern void ret_from_intr(void); // 导出从中断返回的函数(定义在entry.S)
  194. extern struct tss_struct initial_tss[MAX_CPU_NUM];
  195. extern struct mm_struct initial_mm;
  196. extern struct thread_struct initial_thread;
  197. extern union proc_union initial_proc_union;
  198. extern struct process_control_block *initial_proc[MAX_CPU_NUM];