process.h 7.0 KB

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