head.S 2.4 KB

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