head64.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* arch/x86/init/head64.S
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx 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. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. .code64
  19. .globl _start
  20. _start:
  21. /* we are now in the long-mode, just
  22. * do some initializes, and call main */
  23. call setup_stack
  24. call init_gdt
  25. call init_idt
  26. call setup_paging
  27. movq $0xffff800000000000, %rax
  28. leaq 1f(%rip), %rcx
  29. orq %rcx, %rax
  30. pushq %rax
  31. movq $0xffff800000000000, %rax
  32. leaq init(%rip), %rcx
  33. orq %rcx, %rax
  34. pushq %rax
  35. retq
  36. 1: hlt
  37. jmp 1b
  38. init_gdt:
  39. lgdt gdt_descr(%rip)
  40. movq $0x10, %rax
  41. movq %rax, %ds
  42. movq %rax, %es
  43. movq %rax, %fs
  44. movq %rax, %gs
  45. movq %rax, %ss
  46. ret
  47. setup_stack:
  48. /* `setup_paging` used 0~0x80000, so we must
  49. * find a new space for stack (old stack at 0x7c00).
  50. *
  51. * but sadly, we can't use address above 1048576 because
  52. * the `arch_mm_init` will use them as the page and zone
  53. * structures. so, would you like to use someplace in
  54. * .data? :-) */
  55. movq (%rsp), %rax
  56. movq $0x90000, %rsp
  57. jmp *%rax
  58. setup_paging:
  59. /* map 0-1g memory (512 entries),
  60. * use memory from 0x2000 ~ 0x3000 */
  61. movq $0x2000, %rdi
  62. movq $0x83, %rax
  63. cld
  64. spl: stosq
  65. addq $2097152, %rax
  66. cmpq $0x3000, %rdi
  67. jbe spl
  68. xorq %rax, %rax
  69. movq %rax, %cr3
  70. ret
  71. init_idt:
  72. leaq default_int_handler(%rip), %rdx
  73. movq $(8 << 16), %rax
  74. movw %dx, %ax
  75. /* 0x8e: interrupt gate */
  76. movq $(0x8e00 << 32), %rcx
  77. addq %rcx, %rax
  78. movl %edx, %ecx
  79. shrl $16, %ecx
  80. shlq $48, %rcx
  81. addq %rcx, %rax
  82. shrq $32, %rdx
  83. movq $256, %rcx
  84. leaq idt(%rip), %rdi
  85. rsidt: movq %rax, (%rdi)
  86. movq %rdx, 8(%rdi)
  87. addq $16, %rdi
  88. decq %rcx
  89. jnz rsidt
  90. lidt idt_descr(%rip)
  91. ret
  92. default_int_handler:
  93. incq 0xb8000
  94. iretq
  95. .globl __stack_chk_fail
  96. __stack_chk_fail:
  97. hlt
  98. jmp __stack_chk_fail
  99. .data
  100. .align 16
  101. .globl gdt
  102. gdt: .quad 0x0000000000000000
  103. .quad 0x0020980000000000
  104. .quad 0x0000920000000000
  105. .globl gdt_descr
  106. gdt_descr:
  107. .word . - gdt
  108. .quad gdt
  109. .globl idt
  110. .align 16
  111. idt: .fill 512, 8, 0
  112. .globl idt_descr
  113. idt_descr:
  114. .word . - idt
  115. .quad idt
  116. .data