head.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. leaq _end(%rip), %rax
  37. addq $32768, %rax
  38. movq %rax, %rsp
  39. /* setup IDT */
  40. lidt idt_desc(%rip)
  41. setup_idt:
  42. leaq default_int_handler(%rip), %rdx
  43. movq $(0x08 << 16), %rax
  44. /* low 16b address */
  45. movw %dx, %ax
  46. /* 0x8e: interrupt gate, DPL = 0, type = E */
  47. movq $(0x8e00 << 32), %rcx
  48. addq %rcx, %rax
  49. /* high 16b (16-32) address */
  50. movl %edx, %ecx
  51. shrl $16, %ecx
  52. shlq $48, %rcx
  53. /* to %rax */
  54. addq %rcx, %rax
  55. /* high 32b (32-64) address */
  56. shrq $32, %rdx
  57. /* times of repeat */
  58. movq $256, %rcx
  59. leaq idt_table(%rip), %rdi
  60. rep_set_idt:
  61. movq %rax, (%rdi)
  62. movq %rdx, 8(%rdi)
  63. addq $16, %rdi
  64. dec %rcx
  65. jnz rep_set_idt
  66. to_init:
  67. /* we cannot use far-jmp in long mode. */
  68. /*
  69. * 'call' will push address of next code,
  70. * and 'ret' will pop it to %rip
  71. * so we can push address, 'ret' will
  72. * pop it and jump to it.
  73. */
  74. /* 64 bit code */
  75. /* return address */
  76. leaq init_done(%rip), %rax
  77. pushq %rax
  78. /* address will jump */
  79. leaq init(%rip), %rax
  80. pushq %rax
  81. retq
  82. init_done:
  83. /* andq %rax, %rax, but %rax won't change. */
  84. testq %rax, %rax
  85. jnz init_panic
  86. init_die:
  87. jmp init_die
  88. init_panic:
  89. WARNING(panic_msg(%rip))
  90. jmp init_die
  91. default_int_handler:
  92. /* save all registers */
  93. pushq %rax
  94. pushq %rcx
  95. pushq %rbx
  96. pushq %rdx
  97. pushq %rbp
  98. pushq %rsi
  99. pushq %rdi
  100. pushq %r8
  101. pushq %r9
  102. pushq %r10
  103. pushq %r11
  104. pushq %r12
  105. pushq %r13
  106. pushq %r14
  107. pushq %r15
  108. movq %ds, %rax
  109. pushq %rax
  110. movq %es, %rax
  111. pushq %rax
  112. movq %fs, %rax
  113. pushq %rax
  114. movq %gs, %rax
  115. pushq %rax
  116. movq $0x10, %rax
  117. movq %rax, %ds
  118. movq %rax, %es
  119. movq %rax, %fs
  120. movq %rax, %gs
  121. /* put a message on screen, and die */
  122. WARNING(int_msg(%rip))
  123. popq %rax
  124. movq %rax, %gs
  125. popq %rax
  126. movq %rax, %fs
  127. popq %rax
  128. movq %rax, %es
  129. popq %rax
  130. movq %rax, %ds
  131. popq %r15
  132. popq %r14
  133. popq %r13
  134. popq %r12
  135. popq %r11
  136. popq %r10
  137. popq %r9
  138. popq %r8
  139. popq %rdi
  140. popq %rsi
  141. popq %rbp
  142. popq %rdx
  143. popq %rbx
  144. popq %rcx
  145. popq %rax
  146. /* goodbye! */
  147. sti
  148. iretq
  149. .data
  150. .align 16
  151. .globl idt_table
  152. idt_table:
  153. .fill 512, 8, 0
  154. idt_end:
  155. idt_desc:
  156. .word idt_end - idt_table - 1
  157. .quad idt_table
  158. .section .rodata
  159. .align 8
  160. message:
  161. .asciz "XNIX Kernel is starting..."
  162. panic_msg:
  163. .asciz "panic in init: halted"
  164. int_msg:
  165. .asciz "unknow interrupt or fault\n"