sched.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /* get registers in the new stack \
  113. * note that do_fork also do things \
  114. * looks like SAVEREGS (copy \
  115. * struct registers to stack) */ \
  116. "movq %4, %%rax\n\t" \
  117. "movq %%rax, %%ds\n\t" \
  118. "movq %%rax, %%es\n\t" \
  119. "movq %%rax, %%fs\n\t" \
  120. "movq %%rax, %%gs\n\t" \
  121. "cmpq $0, need_open_clock(%%rip)\n\t" \
  122. "je 2f\n\t" \
  123. "movq $0, need_open_clock(%%rip)\n\t" \
  124. "movq $0x20, %%rax\n\t" \
  125. "movq $0x20, %%rdx\n\t" \
  126. "outb %%al, %%dx\n\t" \
  127. "2:\n\t" \
  128. "sti\n\t" \
  129. /* note that if this is the first \
  130. * run time for this task, we will \
  131. * return to 'ret_intr' or \
  132. * ktask_entry */ \
  133. /* else, we will return to 1f */ \
  134. /* update the current */ \
  135. "movq %5, current\n\t" \
  136. "retq\n\t" \
  137. "1:\n\t" \
  138. RESTORE_REGS \
  139. : "=m" (current->rsp), \
  140. "=m" (current->rip) \
  141. : "m" ((next)->rsp), \
  142. "m" ((next)->rip), \
  143. "r" ((long) (next)->dataseg), \
  144. "r" ((next)) \
  145. : "ax" \
  146. )
  147. /* standard run time for user tasks */
  148. /* 1 jiffy = 10ms */
  149. #define USER_STD_TIME 1
  150. struct task {
  151. /* died */
  152. #define STATE_ZOMBIE 0
  153. /* it's running on cpu */
  154. #define STATE_RUNNING 1
  155. /* waiting for some resource, and can't be schedule() */
  156. #define STATE_WAIT 3
  157. /* you can schedule this task any time */
  158. #define STATE_READY 4
  159. /* you can't run this task, in this time */
  160. #define STATE_FINISH 5
  161. /* task is creating */
  162. #define STATE_IDE 6
  163. __uint16_t state;
  164. /* total run time in kernel space (jiffies) */
  165. __uint32_t ktime;
  166. /* total run time in user space (0 in kernel tasks) */
  167. __uint32_t utime;
  168. /* start time (jiffies) */
  169. __uint32_t start_time;
  170. /* by nice, 0.01 - 0.1 sec, default 2 (nice 2) */
  171. __uint8_t counter;
  172. /* 0 - 16 */
  173. __uint32_t nice;
  174. /* signal (a small bitmap) */
  175. __uint32_t signal;
  176. /* mask of signal (can be set by signal() or sigaction(), but SIGKILL) */
  177. __uint32_t signal_mask;
  178. int exit_code;
  179. #define INIT_PID 0
  180. /* pid and his father's pid */
  181. __uint32_t pid, father;
  182. /* point to its tss */
  183. struct tss *tss;
  184. __uint64_t rsp;
  185. __uint64_t rip;
  186. __uint64_t cr3;
  187. __uint16_t dataseg, codeseg;
  188. };
  189. extern __uint32_t next_pid;
  190. /* unlike linux we use a pointer to save current task */
  191. extern struct task *current;
  192. extern struct task *task_queues[16];
  193. int task_init(void);
  194. void set_tss(struct tss *, __uint64_t);
  195. /* registers in interrupt-handler's stack */
  196. struct registers {
  197. __uint64_t gs, fs, es, ds;
  198. __uint64_t r15, r14, r13, r12;
  199. __uint64_t r11, r10, r9, r8;
  200. __uint64_t rdi, rsi, rbp;
  201. __uint64_t rbx, rdx, rcx, rax;
  202. /* user task will push this when interrupt happens (for iretq) */
  203. __uint64_t rip;
  204. __uint64_t cs;
  205. __uint64_t rflags;
  206. __uint64_t rsp;
  207. __uint64_t ss;
  208. };
  209. #define ltr(nr) __asm__ __volatile__ ("ltr %%ax" :: "a" (nr))
  210. /* kernel/sched.c */
  211. void ktask_entry(void);
  212. int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t);
  213. int do_fork(struct registers *, __uint64_t);
  214. #endif