process.h 7.1 KB

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