sched.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _SCHED_H
  20. #define _SCHED_H
  21. #include <kernel/types.h>
  22. #include <kernel/errno.h>
  23. #include <arch/io.h>
  24. #include <arch/sys.h>
  25. #define MAX_TASKS 128
  26. /*
  27. * switch_to(next): switch to task next.
  28. *
  29. * STEPS:
  30. * save that registers that will use.
  31. * call __do
  32. * do: set data segments.
  33. * return;
  34. *
  35. * return to next task.
  36. *
  37. * NOTES:
  38. * we don't need to restore / save registers, because
  39. * tasks restored them.
  40. *
  41. * this function may called from an interrupt-handler
  42. * (timer IRQ), so we must sti first, else tasks won't
  43. * switch again.
  44. *
  45. * __do maybe changes some registers, like %rbp
  46. * (maybe doesn't), so we also should save/restore them.
  47. * */
  48. #define SAVE_REGS \
  49. "subq $16, %%rsp\n\t" \
  50. "pushfq\n\t" \
  51. "subq $16, %%rsp\n\t" \
  52. "pushq %%rax\n\t" \
  53. "pushq %%rcx\n\t" \
  54. "pushq %%rdx\n\t" \
  55. "pushq %%rbx\n\t" \
  56. "pushq %%rbp\n\t" \
  57. "pushq %%rsi\n\t" \
  58. "pushq %%rdi\n\t" \
  59. "pushq %%r8\n\t" \
  60. "pushq %%r9\n\t" \
  61. "pushq %%r10\n\t" \
  62. "pushq %%r11\n\t" \
  63. "pushq %%r12\n\t" \
  64. "pushq %%r13\n\t" \
  65. "pushq %%r14\n\t" \
  66. "pushq %%r15\n\t" \
  67. "movq %%ds, %%rax\n\t" \
  68. "pushq %%rax\n\t" \
  69. "movq %%es, %%rax\n\t" \
  70. "pushq %%rax\n\t" \
  71. "movq %%fs, %%rax\n\t" \
  72. "pushq %%rax\n\t" \
  73. "movq %%gs, %%rax\n\t" \
  74. "pushq %%rax\n\t"
  75. #define RESTORE_REGS \
  76. "popq %%rax\n\t" \
  77. "movq %%rax, %%gs\n\t" \
  78. "popq %%rax\n\t" \
  79. "movq %%rax, %%fs\n\t" \
  80. "popq %%rax\n\t" \
  81. "movq %%rax, %%es\n\t" \
  82. "popq %%rax\n\t" \
  83. "movq %%rax, %%ds\n\t" \
  84. "popq %%r15\n\t" \
  85. "popq %%r14\n\t" \
  86. "popq %%r13\n\t" \
  87. "popq %%r12\n\t" \
  88. "popq %%r11\n\t" \
  89. "popq %%r10\n\t" \
  90. "popq %%r9\n\t" \
  91. "popq %%r8\n\t" \
  92. "popq %%rdi\n\t" \
  93. "popq %%rsi\n\t" \
  94. "popq %%rbp\n\t" \
  95. "popq %%rbx\n\t" \
  96. "popq %%rdx\n\t" \
  97. "popq %%rcx\n\t" \
  98. "popq %%rax\n\t" \
  99. "addq $16, %%rsp\n\t" \
  100. "popfq\n\t" \
  101. "addq $16, %%rsp\n\t"
  102. #define switch_to(next) \
  103. __asm__ __volatile__ ( \
  104. "cli\n\t" \
  105. SAVE_REGS \
  106. "movq %%rsp, %0\n\t" \
  107. "movq %2, %%rsp\n\t" \
  108. "leaq 1f(%%rip), %%rax\n\t" \
  109. "movq %%rax, %1\n\t" \
  110. /* fake return address */ \
  111. "pushq %3\n\t" \
  112. /* update the current */ \
  113. "movq %5, current\n\t" \
  114. /* get registers in the new stack \
  115. * note that do_fork also do things \
  116. * looks like SAVEREGS (copy \
  117. * struct registers to stack) */ \
  118. "movq %4, %%rax\n\t" \
  119. "movq %%rax, %%ds\n\t" \
  120. "movq %%rax, %%es\n\t" \
  121. "movq %%rax, %%fs\n\t" \
  122. "movq %%rax, %%gs\n\t" \
  123. "sti\n\t" \
  124. /* note that if this is the first \
  125. * run time for this task, we will \
  126. * return to 'ret_intr' or \
  127. * ktask_entry */ \
  128. /* else, we will return to 1f */ \
  129. "retq\n\t" \
  130. "1:\n\t" \
  131. RESTORE_REGS \
  132. : "=m" (current->rsp), \
  133. "=m" (current->rip) \
  134. : "m" ((next)->rsp), \
  135. "m" ((next)->rip), \
  136. "r" ((long) (next)->dataseg), \
  137. "r" ((next)) \
  138. : "ax" \
  139. )
  140. /* standard run time for user tasks */
  141. /* 1 jiffy = 10ms */
  142. #define USER_STD_TIME 1
  143. struct task {
  144. /* died */
  145. #define STATE_ZOMBIE 0
  146. /* it's running on cpu */
  147. #define STATE_RUNNING 1
  148. /* waiting, but unlike STATE_UNINTERRUPTIBLE, you can wake_up() */
  149. #define STATE_INTERRUPTIBLE 2
  150. /* waiting, and can't wake_up() */
  151. #define STATE_UNINTERRUPTIABLE 3
  152. /* you can schedule this task any time */
  153. #define STATE_READY 4
  154. __uint16_t state;
  155. /* total run time in kernel space (jiffies) */
  156. __uint32_t ktime;
  157. /* total run time in user space (0 in kernel tasks) */
  158. __uint32_t utime;
  159. /* start time (jiffies) */
  160. __uint32_t start_time;
  161. /*
  162. * 0 - 16
  163. * 8 is the default priorty for all user tasks,
  164. * 0 is for all kernel tasks.
  165. * */
  166. __uint32_t nice;
  167. /* signal (a small bitmap) */
  168. __uint32_t signal;
  169. /* mask of signal (can be set by signal() or sigaction(), but SIGKILL) */
  170. __uint32_t signal_mask;
  171. int exit_code;
  172. #define INIT_PID 0
  173. /* pid and his father's pid */
  174. __uint32_t pid, father;
  175. /* point to its tss */
  176. struct tss *tss;
  177. __uint64_t rsp;
  178. __uint64_t rip;
  179. __uint64_t cr3;
  180. __uint16_t dataseg, codeseg;
  181. };
  182. extern __uint32_t next_pid;
  183. /* unlike linux we use a pointer to save current task */
  184. extern struct task *current;
  185. extern struct task *task_queues[16];
  186. int task_init(void);
  187. void set_tss(struct tss *, __uint64_t);
  188. /* registers in interrupt-handler's stack */
  189. struct registers {
  190. __uint64_t gs, fs, es, ds;
  191. __uint64_t r15, r14, r13, r12;
  192. __uint64_t r11, r10, r9, r8;
  193. __uint64_t rdi, rsi, rbp;
  194. __uint64_t rbx, rdx, rcx, rax;
  195. /* user task will push this when interrupt happens (for iretq) */
  196. __uint64_t rip;
  197. __uint64_t cs;
  198. __uint64_t rflags;
  199. __uint64_t rsp;
  200. __uint64_t ss;
  201. };
  202. #define ltr(nr) __asm__ __volatile__ ("ltr %%ax" :: "a" (nr))
  203. /* kernel/sched.c */
  204. void ktask_entry(void);
  205. int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t);
  206. int do_fork(struct registers *, __uint64_t);
  207. #endif