process.h 8.7 KB

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