process.h 6.9 KB

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