thread.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef __THREAD_THREAD_H
  2. #define __THREAD_THREAD_H
  3. #include "../lib/stdint.h"
  4. // 通用函数类型,它将在很多线程函数中作为参数类型
  5. typedef void thread_func(void *);
  6. // 线程的状态
  7. enum task_status
  8. {
  9. TASK_RUNNING,
  10. TASK_READY,
  11. TASK_BLOCKED,
  12. TASK_WAITING,
  13. TASK_HANGING,
  14. TASK_DIED
  15. };
  16. /******************** 中断栈 intr_stack **********************************
  17. * 些结构用于中断发生时保护程序(线程或进程)的上下文环境:
  18. * 进程或线程被外部中断或软中断打断时,需要保护程序(线程或进程)的上下文寄存器,
  19. * kernel.S 中的 intr_exit 出栈操作是些结构的逆操作。
  20. * 此栈在线程自己的内核栈中位置固定,所在页的最顶端。
  21. ***********************************************************************/
  22. struct intr_stack
  23. {
  24. uint32_t vec_no; // kernel.S 宏VECTOR中push %1压入的中断号
  25. uint32_t edi;
  26. uint32_t esi;
  27. uint32_t ebp;
  28. uint32_t esp_dummy; // 虽然pushad会把esp压入,但esp是不断变化的,所以会被popad忽略
  29. uint32_t ebx;
  30. uint32_t edx;
  31. uint32_t ecx;
  32. uint32_t eax;
  33. uint32_t gs;
  34. uint32_t fs;
  35. uint32_t es;
  36. uint32_t ds;
  37. // 以下由cpu从低特权级进入高特权级时压入
  38. uint32_t err_code; // err_code会被压入在eip之后
  39. void (*eip)(void);
  40. uint32_t cs;
  41. uint32_t eflags;
  42. void *esp;
  43. uint32_t ss;
  44. };
  45. /********************* 线程栈thread_stack ********************************
  46. * 线程自己的栈,用于存储线程中待执行的函数
  47. * 此结构在线程自己的内核栈中位置不固定
  48. * 仅用在 switch_to 时保存线程环境
  49. * 实际位置取决于实际运行情况
  50. ***********************************************************************/
  51. struct thread_stack
  52. {
  53. // All registers on the Intel386 缸e global and thus visible to both a calling and a called function.
  54. // Registers %ebp, %ebx, %edi, %esi, and %esp “ belong” to the calling function.
  55. // In other words, a called function must preserve these registers' values for its caller.
  56. // Remaining registers “ belong” to the called function.
  57. // If a calling function wants to preserve such a register value across a function call,
  58. // it must save the value in its local stack frame.
  59. uint32_t ebp;
  60. uint32_t ebx;
  61. uint32_t edi;
  62. uint32_t esi;
  63. // 线程第一次执行时,eip指向待调用的函数kernel_thread
  64. // 其他时候,指向switch_to的返回地址
  65. void (*eip)(thread_func *func, void *func_arg);
  66. // 以下仅供第一次被调度上cpu时使用
  67. // 参数unused_ret只为占位置充数为返回地址
  68. void(*unused_retaddr);
  69. thread_func *function; // 由kernel_thread所调用的函数名
  70. void *func_arg; // 由kernel_thread所调用的函数所需的参数
  71. };
  72. // 进程或线程的pcb,程序控制块(process control block)
  73. struct task_struct
  74. {
  75. uint32_t *self_kstack; // 各内核线程都用自己的内核栈
  76. enum task_status status; // 线程状态
  77. uint8_t priority; // 线程优先级
  78. char name[16]; // 进程或线程的名字
  79. uint32_t stack_magic; // 用这串数字做栈的边界标记,用于检测栈的溢出
  80. };
  81. void thread_create(struct task_struct *pthread, thread_func function, void *func_arg);
  82. void init_thread(struct task_struct *pthread, char *name, int prio);
  83. struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg);
  84. #endif // __THREAD_THREAD_H