head.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 0x0e, %rdi;\
  29. leaq msg, %rsi; \
  30. call print;
  31. .text
  32. .align 8
  33. .globl _start
  34. _start:
  35. /* stack_start is the end address of the first page */
  36. movq $0x200000, %rsp
  37. /* setup GDT */
  38. lgdt gdt_desc(%rip)
  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. movq $0, %rax
  87. init_die:
  88. hlt
  89. jmp init_die
  90. init_panic:
  91. WARNING(panic_msg(%rip))
  92. jmp init_die
  93. default_int_handler:
  94. /* save all registers */
  95. pushq %rax
  96. pushq %rcx
  97. pushq %rbx
  98. pushq %rdx
  99. pushq %rbp
  100. pushq %rsi
  101. pushq %rdi
  102. pushq %r8
  103. pushq %r9
  104. pushq %r10
  105. pushq %r11
  106. pushq %r12
  107. pushq %r13
  108. pushq %r14
  109. pushq %r15
  110. movq %ds, %rax
  111. pushq %rax
  112. movq %es, %rax
  113. pushq %rax
  114. movq %fs, %rax
  115. pushq %rax
  116. movq %gs, %rax
  117. pushq %rax
  118. movq $0x10, %rax
  119. movq %rax, %ds
  120. movq %rax, %es
  121. movq %rax, %fs
  122. movq %rax, %gs
  123. /* put a message on screen, and die */
  124. WARNING(int_msg(%rip))
  125. popq %rax
  126. movq %rax, %gs
  127. popq %rax
  128. movq %rax, %fs
  129. popq %rax
  130. movq %rax, %es
  131. popq %rax
  132. movq %rax, %ds
  133. popq %r15
  134. popq %r14
  135. popq %r13
  136. popq %r12
  137. popq %r11
  138. popq %r10
  139. popq %r9
  140. popq %r8
  141. popq %rdi
  142. popq %rsi
  143. popq %rbp
  144. popq %rdx
  145. popq %rbx
  146. popq %rcx
  147. popq %rax
  148. /* goodbye! */
  149. sti
  150. iretq
  151. .data
  152. .align 16
  153. .globl gdt_table
  154. gdt_table:
  155. /* +0: null descriptor */
  156. .quad 0x0000000000000000
  157. /* +8: kernel code 64-bit */
  158. .quad 0x0020980000000000
  159. /* +16: kernel data 64-bit */
  160. .quad 0x0000920000000000
  161. /* +24: user code 64-bit */
  162. .quad 0x0020f80000000000
  163. /* +32: user data 64-bit */
  164. .quad 0x0000f20000000000
  165. /* tss starts at +40 (5th entry) */
  166. /* and gdt_table.size = 64B */
  167. .fill 3, 8, 0
  168. gdt_end:
  169. gdt_desc:
  170. .word gdt_end - gdt_table - 1
  171. .quad gdt_table
  172. .globl idt_table
  173. idt_table:
  174. .fill 512, 8, 0
  175. idt_end:
  176. idt_desc:
  177. .word idt_end - idt_table - 1
  178. .quad idt_table
  179. .globl stack_start
  180. stack_start:
  181. .quad 0x200000
  182. .section .rodata
  183. .align 8
  184. message:
  185. .asciz "XNIX Kernel is starting..."
  186. panic_msg:
  187. .asciz "panic in init: halted"
  188. int_msg:
  189. .asciz "unknow interrupt or fault\n"