process.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // 进程标志位
  77. #define PF_KTHREAD (1 << 0)
  78. /**
  79. * @brief 进程控制块
  80. *
  81. */
  82. struct process_control_block
  83. {
  84. // 连接各个pcb的双向链表
  85. struct List list;
  86. // 进程的状态
  87. volatile long state;
  88. // 进程标志:进程、线程、内核线程
  89. unsigned long flags;
  90. // 内存空间分布结构体, 记录内存页表和程序段信息
  91. struct mm_struct *mm;
  92. // 进程切换时保存的状态信息
  93. struct thread_struct *thread;
  94. // 地址空间范围
  95. // 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
  96. // 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
  97. ul addr_limit;
  98. // 进程id
  99. long pid;
  100. // 可用时间片
  101. long counter;
  102. // 信号
  103. long signal;
  104. // 优先级
  105. long priority;
  106. };
  107. // 将进程的pcb和内核栈融合到一起,8字节对齐
  108. union proc_union
  109. {
  110. struct process_control_block pcb;
  111. ul stack[STACK_SIZE / sizeof(ul)];
  112. } __attribute__((aligned(8)));
  113. struct mm_struct initial_mm;
  114. struct thread_struct initial_thread;
  115. // 设置初始进程的PCB
  116. #define INITIAL_PROC(proc) \
  117. { \
  118. .state = PROC_UNINTERRUPTIBLE, \
  119. .flags = PF_KTHREAD, \
  120. .mm = &initial_mm, \
  121. .thread = &initial_thread, \
  122. .addr_limit = 0xffff800000000000, \
  123. .pid = 0, \
  124. .counter = 1, \
  125. .signal = 0, \
  126. .priority = 0 \
  127. }
  128. // 初始化 初始进程的union ,并将其链接到.data.init_proc段内
  129. union proc_union initial_proc_union __attribute__((__section__(".data.init_proc_union"))) = {INITIAL_PROC(initial_proc_union.pcb)};
  130. struct process_control_block *initial_proc[MAX_CPU_NUM] = {&initial_proc_union.pcb, 0};
  131. struct mm_struct initial_mm = {0};
  132. struct thread_struct initial_thread =
  133. {
  134. .rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
  135. .rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
  136. .fs = KERNEL_DS,
  137. .gs = KERNEL_DS,
  138. .cr2 = 0,
  139. .trap_num = 0,
  140. .err_code = 0};
  141. /**
  142. * @brief 任务状态段结构体
  143. *
  144. */
  145. struct tss_struct
  146. {
  147. unsigned int reserved0;
  148. ul rsp0;
  149. ul rsp1;
  150. ul rsp2;
  151. ul reserved1;
  152. ul ist1;
  153. ul ist2;
  154. ul ist3;
  155. ul ist4;
  156. ul ist5;
  157. ul ist6;
  158. ul ist7;
  159. ul reserved2;
  160. unsigned short reserved3;
  161. // io位图基地址
  162. unsigned short io_map_base_addr;
  163. } __attribute__((packed)); // 使用packed表明是紧凑结构,编译器不会对成员变量进行字节对齐。
  164. // 设置初始进程的tss
  165. #define INITIAL_TSS \
  166. { \
  167. .reserved0 = 0, \
  168. .rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  169. .rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  170. .rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
  171. .reserved1 = 0, \
  172. .ist1 = 0xffff800000007c00, \
  173. .ist2 = 0xffff800000007c00, \
  174. .ist3 = 0xffff800000007c00, \
  175. .ist4 = 0xffff800000007c00, \
  176. .ist5 = 0xffff800000007c00, \
  177. .ist6 = 0xffff800000007c00, \
  178. .ist7 = 0xffff800000007c00, \
  179. .reserved2 = 0, \
  180. .reserved3 = 0, \
  181. .io_map_base_addr = 0 \
  182. }
  183. // 为每个核心初始化初始进程的tss
  184. struct tss_struct initial_tss[MAX_CPU_NUM] = {[0 ... MAX_CPU_NUM - 1] = INITIAL_TSS};
  185. // 获取当前的pcb
  186. struct process_control_block *get_current_pcb()
  187. {
  188. struct process_control_block *current = NULL;
  189. // 利用了当前pcb和栈空间总大小为32k大小对齐,将rsp低15位清空,即可获得pcb的起始地址
  190. __asm__ __volatile__("andq %%rsp, %0 \n\t"
  191. : "=r"(current)
  192. : "0"(~32767UL));
  193. return current;
  194. };
  195. #define current_pcb get_current_pcb()
  196. #define GET_CURRENT_PCB \
  197. "movq %rsp, %rbx \n\t" \
  198. "andq $-32768, %rbx\n\t"
  199. /**
  200. * @brief 切换进程上下文
  201. * 先把rbp和rax保存到栈中,然后将rsp和rip保存到prev的thread结构体中
  202. * 然后调用__switch_to切换栈,配置其他信息,最后恢复下一个进程的rax rbp。
  203. */
  204. #define switch_proc(prev, next) \
  205. do \
  206. { \
  207. \
  208. __asm__ __volatile__("pushq %%rbp \n\t" \
  209. "pushq %%rax \n\t" \
  210. "movq %%rsp, %0 \n\t" \
  211. "movq %2, %%rsp \n\t" \
  212. "leaq 1f(%%rip), %%rax \n\t" \
  213. "movq %%rax, %1 \n\t" \
  214. "pushq %3 \n\t" \
  215. "jmp __switch_to \n\t" \
  216. "1: \n\t" \
  217. "popq %%rax \n\t" \
  218. "popq %%rbp \n\t" \
  219. : "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
  220. : "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
  221. : "memory"); \
  222. } while (0)
  223. /**
  224. * @brief 初始化系统的第一个进程
  225. *
  226. */
  227. void process_init();
  228. /**
  229. * @brief fork当前进程
  230. *
  231. * @param regs 新的寄存器值
  232. * @param clone_flags 克隆标志
  233. * @param stack_start 堆栈开始地址
  234. * @param stack_size 堆栈大小
  235. * @return unsigned long
  236. */
  237. unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);