head.S 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /* #NO! it is BRIGHT+RED (well...) */
  20. /* we are in VGA mode now */
  21. #define RED $0x00ff0000
  22. /*
  23. * arguments in long-mode:
  24. * %rdi, %rsi, %rdx, %rcx, and...
  25. * stack...
  26. */
  27. #define WARNING(msg) \
  28. movq RED, %rdi; \
  29. xorq %rsi, %rsi;\
  30. leaq msg, %rdx; \
  31. call print;
  32. .text
  33. .align 8
  34. .globl _start
  35. _start:
  36. /* setup IDT */
  37. lidt idt_desc(%rip)
  38. setup_idt:
  39. leaq default_int_handler(%rip), %rdx
  40. movq $(0x08 << 16), %rax
  41. /* low 16b address */
  42. movw %dx, %ax
  43. /* 0x8e: interrupt gate, DPL = 0, type = E */
  44. movq $(0x8e00 << 32), %rcx
  45. addq %rcx, %rax
  46. /* high 16b (16-32) address */
  47. movl %edx, %ecx
  48. shrl $16, %ecx
  49. shlq $48, %rcx
  50. /* to %rax */
  51. addq %rcx, %rax
  52. /* high 32b (32-64) address */
  53. shrq $32, %rdx
  54. /* times of repeat */
  55. movq $256, %rcx
  56. leaq idt_table(%rip), %rdi
  57. rep_set_idt:
  58. movq %rax, (%rdi)
  59. movq %rdx, 8(%rdi)
  60. addq $16, %rdi
  61. dec %rcx
  62. jnz rep_set_idt
  63. to_init:
  64. /* we cannot use far-jmp in long mode. */
  65. /*
  66. * 'call' will push address of next code,
  67. * and 'ret' will pop it to %rip
  68. * so we can push address, 'ret' will
  69. * pop it and jump to it.
  70. */
  71. /* 64 bit code */
  72. /* return address */
  73. leaq init_done(%rip), %rax
  74. pushq %rax
  75. /* address will jump */
  76. leaq init(%rip), %rax
  77. pushq %rax
  78. retq
  79. init_done:
  80. /* andq %rax, %rax, but %rax won't change. */
  81. testq %rax, %rax
  82. jnz init_panic
  83. int $0x80
  84. init_die:
  85. hlt
  86. jmp init_die
  87. init_panic:
  88. WARNING(panic_msg(%rip))
  89. jmp init_die
  90. default_int_handler:
  91. /* put a message on screen, and die */
  92. WARNING(int_msg(%rip))
  93. dih_die:
  94. hlt
  95. jmp dih_die
  96. .data
  97. .align 16
  98. .globl idt_table
  99. idt_table:
  100. .fill 512, 8, 0
  101. idt_end:
  102. idt_desc:
  103. .word idt_end - idt_table - 1
  104. .quad idt_table
  105. .section .rodata
  106. .align 8
  107. message:
  108. .asciz "XNIX Kernel is starting..."
  109. panic_msg:
  110. .asciz "panic in init: halted"
  111. int_msg:
  112. .asciz "unknow interrupt or fault\n"