head.S 2.3 KB

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