head.S 3.8 KB

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