sched.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/panic.h>
  23. #include <kernel/int.h>
  24. #include <kernel/sched.h>
  25. #include <kernel/console.h>
  26. #include <kernel/mm.h>
  27. #include <sys/syscall.h>
  28. #include <arch/x86/int.h>
  29. #include <arch/io.h>
  30. #include <arch/sys.h>
  31. #include <arch/x86/tss.h>
  32. #include <arch/x86/paging.h>
  33. #include <lib/string.h>
  34. /* kernel/trap.S */
  35. extern void ret_intr(void);
  36. struct task init_task;
  37. struct task *current;
  38. struct task *tasks[MAX_TASKS] = {0};
  39. int do_exit(__uint64_t exitcode) {
  40. printk(white, "do_exit: %q\n", exitcode);
  41. for (;;);
  42. }
  43. int task_init(void) {
  44. /* initial the init-tss */
  45. init_tss.rsp0 = init_tss.rsp1 = init_tss.rsp2 = get_rsp();
  46. init_tss.ist1 = init_tss.ist2 = init_tss.ist3 = get_rsp();
  47. init_tss.ist4 = init_tss.ist5 = init_tss.ist6 = get_rsp();
  48. init_tss.ist7 = get_rsp();
  49. set_tss(&init_tss, 0);
  50. current = &init_task;
  51. return 0;
  52. }
  53. /*
  54. * entry for all kernel tasks. (execute only one time)
  55. * get registers from stack (store in do_fork, memcpy).
  56. * and call function %rcx with argument %rdi, then call
  57. * do_exit.
  58. * */
  59. __asm__(
  60. "ktask_entry:\n\t"
  61. "popq %rax\n\t"
  62. "movq %rax, %gs\n\t"
  63. "popq %rax\n\t"
  64. "movq %rax, %fs\n\t"
  65. "popq %rax\n\t"
  66. "movq %rax, %es\n\t"
  67. "popq %rax\n\t"
  68. "movq %rax, %ds\n\t"
  69. "popq %r15\n\t"
  70. "popq %r14\n\t"
  71. "popq %r13\n\t"
  72. "popq %r12\n\t"
  73. "popq %r11\n\t"
  74. "popq %r10\n\t"
  75. "popq %r9\n\t"
  76. "popq %r8\n\t"
  77. "popq %rdi\n\t"
  78. "popq %rsi\n\t"
  79. "popq %rbp\n\t"
  80. "popq %rbx\n\t"
  81. "popq %rdx\n\t"
  82. "popq %rcx\n\t"
  83. "popq %rax\n\t"
  84. "addq $16, %rsp\n\t"
  85. "popfq\n\t"
  86. "addq $16, %rsp\n\t"
  87. "sti\n\t"
  88. "callq *%rcx\n\t"
  89. /* syscall 0: do_exit */
  90. "movq %rax, %rbx\n\t"
  91. "movq $0, %rax\n\t"
  92. "int $0x80\n\t"
  93. /* it never here */
  94. );
  95. /*
  96. * create a kernel task.
  97. * (just set some registers)
  98. * */
  99. int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
  100. struct registers regs;
  101. memset(&regs, 0x00, sizeof(regs));
  102. /* entry function */
  103. regs.rcx = (__uint64_t) entry;
  104. /* argument */
  105. regs.rdi = arg;
  106. return do_fork(&regs, 0);
  107. }
  108. int utask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
  109. struct registers regs;
  110. memset(&regs, 0x00, sizeof(regs));
  111. regs.rip = (__uint64_t) entry;
  112. regs.rdi = arg;
  113. return do_fork(&regs, 3);
  114. }
  115. static __uint32_t find_free_pid(void) {
  116. __uint32_t freepid;
  117. for (freepid = 0; freepid < MAX_TASKS; freepid++)
  118. if (!tasks[freepid])
  119. return freepid;
  120. return -1;
  121. }
  122. /*
  123. * we use this function to create a new task.
  124. *
  125. * it doesn't like that function that we often use in linux (no argument),
  126. * it needs some arguments.
  127. *
  128. * if now is a user task, its child should be a user task too.
  129. *
  130. * we store reg on the top of stack, because ret_intr (or ktask_entry) will
  131. * popq all these registers when the task runs.
  132. * */
  133. int do_fork(struct registers *regs, __uint64_t dpl) {
  134. __uint64_t start_stack;
  135. if (!dpl)
  136. start_stack = (__uint64_t) kmalloc(&pool, 65536);
  137. else
  138. start_stack = (__uint64_t) kmalloc(&upool, 65536);
  139. if (!start_stack)
  140. return -ENOMEM;
  141. /* += stack_size: point to the top of stack. */
  142. start_stack += 65536;
  143. struct task *child = kmalloc(&pool, sizeof(struct task));
  144. if (!child)
  145. return -ENOMEM;
  146. /* current is this task's father */
  147. memcpy(child, current, sizeof(struct task));
  148. child->state = STATE_IDE;
  149. child->utime = child->ktime = 0;
  150. child->start_time = jiffies;
  151. child->signal = 0;
  152. /* allow all signals */
  153. child->signal_mask = 0xffffffff;
  154. child->exit_code = 0;
  155. child->pid = find_free_pid();
  156. if (child->pid == -1)
  157. return -EAGAIN;
  158. printk(white, "new task pid: %d\n", child->pid);
  159. child->father = current->pid;
  160. child->cr3 = 0x0000000000000000;
  161. /* kernel task */
  162. if (!dpl) {
  163. __asm__ __volatile__ (
  164. "leaq ktask_entry(%%rip), %%rax\n\t"
  165. "movq %%rax, %0\n\t"
  166. : "=m" (child->rip)
  167. :: "ax"
  168. );
  169. /* I don't know why this doesn't work. . . */
  170. // child->rip = (__uint64_t) &ktask_entry;
  171. child->dataseg = KERNEL_DS;
  172. child->codeseg = KERNEL_CS;
  173. /* bullshit! -> ktask_entry will popq it in the stack. */
  174. /* we don't need to set ss in regs, because
  175. * ktask_entry will ignore them
  176. * */
  177. regs->gs = regs->fs = regs->es = regs->ds = KERNEL_DS;
  178. }
  179. /* dpl = 3 */
  180. else {
  181. __asm__ __volatile__ (
  182. "leaq ret_intr(%%rip), %%rax\n\t"
  183. "movq %%rax, %0\n\t"
  184. : "=m" (child->rip)
  185. :: "ax"
  186. );
  187. // child->rip = (__uint64_t) &ret_intr;
  188. child->dataseg = USER_DS;
  189. child->codeseg = USER_CS;
  190. regs->ss = regs->fs = regs->es = regs->ds = regs->gs = USER_DS;
  191. regs->cs = USER_CS;
  192. regs->rflags = 1 << 9;
  193. /* set from the utask_create */
  194. // regs->rip = ;
  195. regs->rsp = start_stack - sizeof(struct registers);
  196. }
  197. /* 20 ms, default */
  198. child->counter = 2;
  199. child->nice = 2;
  200. child->rsp = start_stack - sizeof(struct registers);
  201. __asm__ __volatile__ (
  202. "cld;rep;movsb;\n\t"
  203. :: "S" (regs), "D" (child->rsp), "c" (sizeof(struct registers))
  204. );
  205. child->state = STATE_READY;
  206. tasks[child->pid] = child;
  207. return 0;
  208. }
  209. void set_tss(struct tss *table, __uint64_t dpl) {
  210. __uint64_t tss = (__uint64_t) table;
  211. /* create a descriptor (in gdt) and register it */
  212. struct tss_entry *entry = (struct tss_entry *) (gdt_table + 5);
  213. entry->low = entry->high = 0x0000000000000000;
  214. /* `103` is the size of a tss table */
  215. entry->low = 103ul | ((tss & 0xffff) << 16) | (((tss >> 16) & 0xff) << 32)
  216. | ((0x80 + dpl + 0x09) << 40) | (tss << 56 & 0xff);
  217. entry->high = (tss >> 32) & 0xffffffff;
  218. /* load it to tr */
  219. ltr(40);
  220. return;
  221. }
  222. /* well... but this is ok */
  223. static char need_open_clock = 0;
  224. void schedule(void) {
  225. /* find and switch to a new task */
  226. __uint32_t pid;
  227. __uint32_t best = 17;
  228. __uint32_t bestpid = 0;
  229. for (pid = 0; pid < MAX_TASKS; pid++) {
  230. if (tasks[pid] && tasks[pid]->state == STATE_READY
  231. && tasks[pid]->nice < best) {
  232. bestpid = pid;
  233. best = tasks[pid]->nice;
  234. }
  235. }
  236. if (best == 17) {
  237. /* there must are some STATE_FINISH, change them to STATE_READY
  238. * and get bestpid again.
  239. * */
  240. int i;
  241. best = 17;
  242. for (i = 0; i < MAX_TASKS; i++) {
  243. if (tasks[i] && tasks[i]->state == STATE_FINISH) {
  244. tasks[i]->state = STATE_READY;
  245. tasks[i]->counter = tasks[i]->nice;
  246. if (tasks[i]->nice < best) {
  247. bestpid = i;
  248. best = tasks[pid]->nice;
  249. }
  250. }
  251. }
  252. }
  253. if (current->pid == bestpid)
  254. return;
  255. /* will sti() in switch_to() */
  256. switch_to(tasks[bestpid]);
  257. return;
  258. }
  259. void do_clock(void) {
  260. /* still in the init mode */
  261. if (!current)
  262. return;
  263. current->counter--;
  264. if (!current->counter) {
  265. current->state = STATE_FINISH;
  266. need_open_clock = 1;
  267. schedule();
  268. }
  269. return;
  270. }