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