process.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. extern unsigned long _stack_start; // 导出内核层栈基地址(定义在head.S)
  17. extern void ret_from_intr(void); // 导出从中断返回的函数(定义在entry.S)
  18. // 进程的内核栈大小 32K
  19. #define STACK_SIZE 32768
  20. // 进程的运行状态
  21. // 正在运行
  22. #define PROC_RUNNING (1 << 0)
  23. // 可被中断
  24. #define PROC_INTERRUPTIBLE (1 << 1)
  25. // 不可被中断
  26. #define PROC_UNINTERRUPTIBLE (1 << 2)
  27. // 挂起
  28. #define PROC_ZOMBIE (1 << 3)
  29. // 已停止
  30. #define PROC_STOPPED (1 << 4)
  31. // 内核代码段基地址
  32. #define KERNEL_CS (0x08)
  33. // 内核数据段基地址
  34. #define KERNEL_DS (0x10)
  35. // 用户代码段基地址
  36. #define USER_CS (0x28)
  37. // 用户数据段基地址
  38. #define USER_DS (0x30)
  39. // 进程初始化时的数据拷贝标志位
  40. #define CLONE_FS (1 << 0)
  41. #define CLONE_FILES (1 << 1)
  42. #define CLONE_SIGNAL (1 << 2)
  43. /**
  44. * @brief 内存空间分布结构体
  45. * 包含了进程内存空间分布的信息
  46. */
  47. struct mm_struct
  48. {
  49. pml4t_t *pgd; // 内存页表指针
  50. // 代码段空间
  51. ul code_addr_start, code_addr_end;
  52. // 数据段空间
  53. ul data_addr_start, data_addr_end;
  54. // 只读数据段空间
  55. ul rodata_addr_start, rodata_addr_end;
  56. // 动态内存分配区(堆区域)
  57. ul brk_start, brk_end;
  58. // 应用层栈基地址
  59. ul stack_start;
  60. };
  61. struct thread_struct
  62. {
  63. // 内核层栈基指针
  64. ul rbp; // in tss rsp0
  65. // 内核层代码指针
  66. ul rip;
  67. // 内核层栈指针
  68. ul rsp;
  69. ul fs, gs;
  70. ul cr2;
  71. // 异常号
  72. ul trap_num;
  73. // 错误码
  74. ul err_code;
  75. };
  76. // ========= pcb->flags =========
  77. // 进程标志位
  78. #define PF_KTHREAD (1UL << 0)
  79. #define PROC_NEED_SCHED (1UL << 1) // 进程需要被调度
  80. /**
  81. * @brief 进程控制块
  82. *
  83. */
  84. struct process_control_block
  85. {
  86. // 进程的状态
  87. volatile long state;
  88. // 进程标志:进程、线程、内核线程
  89. unsigned long flags;
  90. long signal;
  91. // 内存空间分布结构体, 记录内存页表和程序段信息
  92. struct mm_struct *mm;
  93. // 进程切换时保存的状态信息
  94. struct thread_struct *thread;
  95. // 连接各个pcb的双向链表
  96. struct List list;
  97. // 地址空间范围
  98. // 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
  99. // 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
  100. uint64_t addr_limit;
  101. long pid;
  102. long priority; // 优先级
  103. long virtual_runtime; // 虚拟运行时间
  104. };
  105. // 将进程的pcb和内核栈融合到一起,8字节对齐
  106. union proc_union
  107. {
  108. struct process_control_block pcb;
  109. ul stack[STACK_SIZE / sizeof(ul)];
  110. } __attribute__((aligned(8)));
  111. struct mm_struct initial_mm;
  112. struct thread_struct initial_thread;
  113. // 设置初始进程的PCB
  114. #define INITIAL_PROC(proc) \
  115. { \
  116. .state = PROC_UNINTERRUPTIBLE, \
  117. .flags = PF_KTHREAD, \
  118. .mm = &initial_mm, \
  119. .thread = &initial_thread, \
  120. .addr_limit = 0xffff800000000000, \
  121. .pid = 0, \
  122. .virtual_runtime = 0, \
  123. .signal = 0, \
  124. .priority = 2 \
  125. }
  126. // 初始化 初始进程的union ,并将其链接到.data.init_proc段内
  127. union proc_union initial_proc_union __attribute__((__section__(".data.init_proc_union"))) = {INITIAL_PROC(initial_proc_union.pcb)};
  128. struct process_control_block *initial_proc[MAX_CPU_NUM] = {&initial_proc_union.pcb, 0};
  129. struct mm_struct initial_mm = {0};
  130. struct thread_struct initial_thread =
  131. {
  132. .rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
  133. .rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
  134. .fs = KERNEL_DS,
  135. .gs = KERNEL_DS,
  136. .cr2 = 0,
  137. .trap_num = 0,
  138. .err_code = 0};
  139. /**
  140. * @brief 任务状态段结构体
  141. *
  142. */
  143. struct tss_struct
  144. {
  145. unsigned int reserved0;
  146. ul rsp0;
  147. ul rsp1;
  148. ul rsp2;
  149. ul reserved1;
  150. ul ist1;
  151. ul ist2;
  152. ul ist3;
  153. ul ist4;
  154. ul ist5;
  155. ul ist6;
  156. ul ist7;
  157. ul reserved2;
  158. unsigned short reserved3;
  159. // io位图基地址
  160. unsigned short io_map_base_addr;
  161. } __attribute__((packed)); // 使用packed表明是紧凑结构,编译器不会对成员变量进行字节对齐。
  162. // 设置初始进程的tss
  163. #define INITIAL_TSS \
  164. { \
  165. .reserved0 = 0, \
  166. .rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  167. .rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  168. .rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  169. .reserved1 = 0, \
  170. .ist1 = 0xffff800000007c00, \
  171. .ist2 = 0xffff800000007c00, \
  172. .ist3 = 0xffff800000007c00, \
  173. .ist4 = 0xffff800000007c00, \
  174. .ist5 = 0xffff800000007c00, \
  175. .ist6 = 0xffff800000007c00, \
  176. .ist7 = 0xffff800000007c00, \
  177. .reserved2 = 0, \
  178. .reserved3 = 0, \
  179. .io_map_base_addr = 0 \
  180. }
  181. // 为每个核心初始化初始进程的tss
  182. struct tss_struct initial_tss[MAX_CPU_NUM] = {[0 ... MAX_CPU_NUM - 1] = INITIAL_TSS};
  183. // 获取当前的pcb
  184. struct process_control_block *get_current_pcb()
  185. {
  186. struct process_control_block *current = NULL;
  187. // 利用了当前pcb和栈空间总大小为32k大小对齐,将rsp低15位清空,即可获得pcb的起始地址
  188. __asm__ __volatile__("andq %%rsp, %0 \n\t"
  189. : "=r"(current)
  190. : "0"(~32767UL));
  191. return current;
  192. };
  193. #define current_pcb get_current_pcb()
  194. #define GET_CURRENT_PCB \
  195. "movq %rsp, %rbx \n\t" \
  196. "andq $-32768, %rbx\n\t"
  197. /**
  198. * @brief 切换进程上下文
  199. * 先把rbp和rax保存到栈中,然后将rsp和rip保存到prev的thread结构体中
  200. * 然后调用__switch_to切换栈,配置其他信息,最后恢复下一个进程的rax rbp。
  201. */
  202. #define switch_proc(prev, next) \
  203. do \
  204. { \
  205. __asm__ __volatile__("pushq %%rbp \n\t" \
  206. "pushq %%rax \n\t" \
  207. "movq %%rsp, %0 \n\t" \
  208. "movq %2, %%rsp \n\t" \
  209. "leaq 1f(%%rip), %%rax \n\t" \
  210. "movq %%rax, %1 \n\t" \
  211. "pushq %3 \n\t" \
  212. "jmp __switch_to \n\t" \
  213. "1: \n\t" \
  214. "popq %%rax \n\t" \
  215. "popq %%rbp \n\t" \
  216. : "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
  217. : "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
  218. : "memory"); \
  219. } while (0)
  220. /**
  221. * @brief 初始化系统的第一个进程
  222. *
  223. */
  224. void process_init();
  225. /**
  226. * @brief fork当前进程
  227. *
  228. * @param regs 新的寄存器值
  229. * @param clone_flags 克隆标志
  230. * @param stack_start 堆栈开始地址
  231. * @param stack_size 堆栈大小
  232. * @return unsigned long
  233. */
  234. unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);