sched.c 6.3 KB

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