arch_task.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* arch/x86/include/arch/arch_task.h
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #ifndef __ARCH_TASK_H
  21. #define __ARCH_TASK_H
  22. #include <kernel/task.h>
  23. #include <arch/archinfo.h>
  24. #include <arch/int.h>
  25. #include <arch/string.h>
  26. #ifndef __ASSEMBLER__
  27. extern void ret_intr(void);
  28. extern void die(void);
  29. /* it's crazy */
  30. #define STACK_SIZE 512
  31. struct arch_task_descr {
  32. /* current stack pointer */
  33. __archptr_t sp;
  34. /* page table base */
  35. __archptr_t ptable;
  36. /* segment selector, data */
  37. __archptr_t sel;
  38. };
  39. #define ARCH_TASK_DESCR_INIT { \
  40. .sp = 0, \
  41. .ptable = 0, \
  42. .sel = 0x08 \
  43. }
  44. static inline struct arch_task_descr *arch_task_descr_init(struct arch_task_descr *p) {
  45. p->sp = 0;
  46. p->ptable = 0;
  47. p->sel = 0x08;
  48. return p;
  49. }
  50. static inline int arch_stack_init(struct arch_task_descr *p) {
  51. struct registers *regs;
  52. if (unlikely(!(p->sp = (__archptr_t) early_kmalloc(STACK_SIZE, 1))))
  53. panic("arch_stack_init: Out of memory");
  54. p->sp += STACK_SIZE;
  55. regs = (struct registers *) ((void *) p->sp - sizeof(struct registers));
  56. memset(regs, 0, sizeof(struct registers));
  57. regs->gs = regs->fs = regs->es = regs->ds = 0x10;
  58. regs->ip = (__archptr_t) die;
  59. regs->cs = 0x08;
  60. regs->flags = 0x20;
  61. regs->sp = p->sp;
  62. regs->ss = 0x10;
  63. return 0;
  64. }
  65. /* it's not so easy to understand this. we just pop the registers in the stack
  66. * and return. for the first time, the ip (in stack but not in a structure) is
  67. * ret_intr */
  68. #define switch_to(task) ({ \
  69. __asm__ volatile ( \
  70. "movq %%rsp, %0\n\t" \
  71. "movq %1, %%rsp\n\t" \
  72. "jmp ret_intr\n\t" \
  73. : "=m" (current->arch_descr.sp) \
  74. : "m" (task->arch_descr.sp) \
  75. ); \
  76. })
  77. #endif
  78. #endif