sched.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 switch_to(next) \
  48. __asm__ __volatile__ ( \
  49. "cli\n\t" \
  50. "pushq %%rbp\n\t" \
  51. "pushq %%rax\n\t" \
  52. "movq %%rsp, %0\n\t" \
  53. "movq %2, %%rsp\n\t" \
  54. "leaq goback(%%rip), %%rax\n\t" \
  55. /* fake return address */ \
  56. "pushq %3\n\t" \
  57. "jmp __do\n\t" \
  58. "__do:\n\t" \
  59. "movw %4, %%ds\n\t" \
  60. "movw %4, %%es\n\t" \
  61. "movw %4, %%fs\n\t" \
  62. "movw %4, %%gs\n\t" \
  63. "sti\n\t" \
  64. "retq\n\t" \
  65. "goback:\n\t" \
  66. "popq %%rax\n\t" \
  67. "popq %%rbp\n\t" \
  68. : "=m" (current->rsp), \
  69. "=m" (current->rip) \
  70. : "m" ((next)->rsp), \
  71. "m" ((next)->rip), \
  72. "r" ((next)->dataseg) \
  73. )
  74. /* standard run time for user tasks */
  75. /* 1 jiffy = 10ms */
  76. #define USER_STD_TIME 1
  77. struct task {
  78. /* died */
  79. #define STATE_ZOMBIE 0
  80. /* it's running on cpu */
  81. #define STATE_RUNNING 1
  82. /* waiting, but unlike STATE_UNINTERRUPTIBLE, you can wake_up() */
  83. #define STATE_INTERRUPTIBLE 2
  84. /* waiting, and can't wake_up() */
  85. #define STATE_UNINTERRUPTIABLE 3
  86. /* you can schedule this task any time */
  87. #define STATE_READY 4
  88. __uint32_t state;
  89. /* total run time in kernel space (jiffies) */
  90. __uint32_t ktime;
  91. /* total run time in user space (0 in kernel tasks) */
  92. __uint32_t utime;
  93. /* start time (jiffies) */
  94. __uint32_t start_time;
  95. /*
  96. * 0 - 16
  97. * 8 is the default priorty for all user tasks,
  98. * 0 is for all kernel tasks.
  99. * */
  100. __uint32_t nice;
  101. /* signal (a small bitmap) */
  102. __uint32_t signal;
  103. /* mask of signal (can be set by signal() or sigaction(), but SIGKILL) */
  104. __uint32_t signal_mask;
  105. int exit_code;
  106. #define INIT_PID 0
  107. /* pid and his father's pid */
  108. __uint32_t pid, father;
  109. /* point to its tss */
  110. struct tss *tss;
  111. __uint64_t rsp;
  112. __uint64_t rip;
  113. __uint64_t cr3;
  114. __uint16_t dataseg, codeseg;
  115. struct task *next;
  116. };
  117. extern __uint32_t next_pid;
  118. /* unlike linux we use a pointer to save current task */
  119. extern struct task *current;
  120. extern struct task *task_queues[16];
  121. int task_init(void);
  122. void set_tss(struct tss *, __uint64_t);
  123. /* registers in interrupt-handler's stack */
  124. struct registers {
  125. __uint64_t gs, fs, es, ds;
  126. __uint64_t r15, r14, r13, r12;
  127. __uint64_t r11, r10, r9, r8;
  128. __uint64_t rdi, rsi, rbp;
  129. __uint64_t rbx, rdx, rcx, rax;
  130. /* user task will push this when interrupt happens (for iretq) */
  131. __uint64_t rip;
  132. __uint64_t cs;
  133. __uint64_t rflags;
  134. __uint64_t rsp;
  135. __uint64_t ss;
  136. };
  137. #define ltr(nr) __asm__ __volatile__ ("ltr %%ax" :: "a" (nr))
  138. /* kernel/sched.c */
  139. void ktask_entry(void);
  140. int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t);
  141. int do_fork(struct registers *, __uint64_t, __uint64_t, __uint64_t);
  142. #endif