head64.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* arch/x86/init/head64.S
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. .code64
  21. .globl _start
  22. _start:
  23. /* we are now in the long-mode, just
  24. * do some initializes, and call main */
  25. call setup_stack
  26. call init_gdt
  27. call init_idt
  28. call setup_paging
  29. movq $0xffff800000000000, %rax
  30. leaq 1f(%rip), %rcx
  31. orq %rcx, %rax
  32. pushq %rax
  33. movq $0xffff800000000000, %rax
  34. leaq init(%rip), %rcx
  35. orq %rcx, %rax
  36. pushq %rax
  37. retq
  38. 1: hlt
  39. jmp 1b
  40. init_gdt:
  41. lgdt gdt_descr(%rip)
  42. movq $0x10, %rax
  43. movq %rax, %ds
  44. movq %rax, %es
  45. movq %rax, %fs
  46. movq %rax, %gs
  47. movq %rax, %ss
  48. ret
  49. setup_stack:
  50. /* `setup_paging` used 0~0x80000, so we must
  51. * find a new space for stack (old stack at 0x7c00).
  52. *
  53. * but sadly, we can't use address above 1048576 because
  54. * the `arch_mm_init` will use them as the page and zone
  55. * structures. so, would you like to use someplace in
  56. * .data? :-) */
  57. movq (%rsp), %rax
  58. movq $0x90000, %rsp
  59. jmp *%rax
  60. setup_paging:
  61. /* map 0-1g memory (512 entries),
  62. * use memory from 0x2000 ~ 0x3000 */
  63. movq $0x2000, %rdi
  64. movq $0x83, %rax
  65. cld
  66. spl: stosq
  67. addq $2097152, %rax
  68. cmpq $0x3000, %rdi
  69. jbe spl
  70. xorq %rax, %rax
  71. movq %rax, %cr3
  72. ret
  73. init_idt:
  74. leaq default_int_handler(%rip), %rdx
  75. movq $(8 << 16), %rax
  76. movw %dx, %ax
  77. /* 0x8e: interrupt gate */
  78. movq $(0x8e00 << 32), %rcx
  79. addq %rcx, %rax
  80. movl %edx, %ecx
  81. shrl $16, %ecx
  82. shlq $48, %rcx
  83. addq %rcx, %rax
  84. shrq $32, %rdx
  85. movq $256, %rcx
  86. leaq idt(%rip), %rdi
  87. rsidt: movq %rax, (%rdi)
  88. movq %rdx, 8(%rdi)
  89. addq $16, %rdi
  90. decq %rcx
  91. jnz rsidt
  92. lidt idt_descr(%rip)
  93. ret
  94. default_int_handler:
  95. incq 0xb8000
  96. iretq
  97. .globl ret_intr
  98. ret_intr:
  99. popq %rax
  100. movq %rax, %gs
  101. popq %rax
  102. movq %rax, %fs
  103. popq %rax
  104. movq %rax, %es
  105. popq %rax
  106. movq %rax, %ds
  107. popq %r15
  108. popq %r14
  109. popq %r13
  110. popq %r12
  111. popq %r11
  112. popq %r10
  113. popq %r9
  114. popq %r8
  115. popq %rdi
  116. popq %rsi
  117. popq %rbp
  118. popq %rdx
  119. popq %rbx
  120. popq %rcx
  121. popq %rax
  122. /* errrcode */
  123. addq $8, %rsp
  124. iretq
  125. .globl __stack_chk_fail
  126. .globl die
  127. __stack_chk_fail:
  128. die:
  129. hlt
  130. jmp __stack_chk_fail
  131. .data
  132. .align 16
  133. .globl gdt
  134. gdt: .quad 0x0000000000000000
  135. .quad 0x0020980000000000
  136. .quad 0x0000920000000000
  137. .globl gdt_descr
  138. gdt_descr:
  139. .word . - gdt
  140. .quad gdt
  141. .globl idt
  142. .align 16
  143. idt: .fill 512, 8, 0
  144. .globl idt_descr
  145. idt_descr:
  146. .word . - idt
  147. .quad idt
  148. .data