sched.h 5.6 KB

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