sched.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/graphic.h>
  23. #include <kernel/int.h>
  24. #include <kernel/sched.h>
  25. #include <arch/x86/int.h>
  26. #include <arch/io.h>
  27. #include <arch/sys.h>
  28. #include <arch/x86/tss.h>
  29. #include <arch/x86/paging.h>
  30. #include <lib/string.h>
  31. struct task init_task;
  32. struct task *current;
  33. struct task *task_queues[16];
  34. __uint32_t next_pid = 0;
  35. int task_init(void) {
  36. /* initial the init-tss */
  37. init_tss.rsp0 = init_tss.rsp1 = init_tss.rsp2 = get_rsp();
  38. init_tss.ist1 = init_tss.ist2 = init_tss.ist3 = get_rsp();
  39. init_tss.ist4 = init_tss.ist5 = init_tss.ist6 = get_rsp();
  40. init_tss.ist7 = get_rsp();
  41. set_tss(&init_tss, 0);
  42. /* initial the task struct for init task */
  43. memset(&init_task, 0x00, sizeof(struct task));
  44. next_pid++;
  45. init_task.state = STATE_RUNNING;
  46. init_task.ktime = init_task.utime = 0;
  47. /* min priority */
  48. init_task.priority = 15;
  49. init_task.signal = 0;
  50. /* init_task can't have any signals */
  51. init_task.signal_mask = 0xffffffff;
  52. init_task.exit_code = 0;
  53. init_task.pid = init_task.father = INIT_PID;
  54. init_task.tss = &init_tss;
  55. init_task.cr3 = PAGE_TABLE_BASE;
  56. init_task.rsp0 = get_rsp();
  57. init_task.rsp = get_rsp();
  58. init_task.dataseg = KERNEL_DS;
  59. init_task.codeseg = KERNEL_CS;
  60. init_task.rip = 0;
  61. /* copy registers to top of its stack */
  62. init_task.next = NULL;
  63. current = &init_task;
  64. task_queues[15] = &init_task;
  65. // switch_to(&init_task);
  66. return 0;
  67. }
  68. int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
  69. return 0;
  70. }
  71. void do_clock(void) {
  72. printk(0x00ff0000, 0x00000000, "%d\n", jiffies / 100);
  73. return;
  74. }
  75. void set_tss(struct tss *table, __uint64_t dpl) {
  76. __uint64_t tss = (__uint64_t) table;
  77. /* create a descriptor (in gdt) and register it */
  78. struct tss_entry *entry = (struct tss_entry *) (gdt_table + 5);
  79. entry->low = entry->high = 0x0000000000000000;
  80. /* `103` is the size of a tss table */
  81. entry->low = 103ul | ((tss & 0xffff) << 16) | (((tss >> 16) & 0xff) << 32)
  82. | ((0x80 + dpl + 0x09) << 40) | (tss << 56 & 0xff);
  83. entry->high = (tss >> 32) & 0xffffffff;
  84. /* load it to tr */
  85. ltr(40);
  86. return;
  87. }