head64.S 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 setup_paging
  26. movq $0xffff800000000000, %rax
  27. leaq 1f(%rip), %rcx
  28. orq %rcx, %rax
  29. pushq %rax
  30. movq $0xffff800000000000, %rax
  31. leaq init(%rip), %rcx
  32. orq %rcx, %rax
  33. pushq %rax
  34. retq
  35. 1: hlt
  36. jmp 1b
  37. init_gdt:
  38. lgdt gdt_descr(%rip)
  39. movq $0x10, %rax
  40. movq %rax, %ds
  41. movq %rax, %es
  42. movq %rax, %fs
  43. movq %rax, %gs
  44. movq %rax, %ss
  45. ret
  46. setup_stack:
  47. /* `setup_paging` used 0~0x80000, so we must
  48. * find a new space for stack (old stack at 0x7c00).
  49. *
  50. * but sadly, we can't use address above 1048576 because
  51. * the `arch_mm_init` will use them as the page and zone
  52. * structures. so, would you like to use someplace in
  53. * .data? :-) */
  54. movq (%rsp), %rax
  55. movq $0x90000, %rsp
  56. jmp *%rax
  57. setup_paging:
  58. /* map 0-1g memory (512 entries),
  59. * use memory from 0x2000 ~ 0x3000 */
  60. movq $0x2000, %rdi
  61. movq $0x83, %rax
  62. cld
  63. spl: stosq
  64. addq $2097152, %rax
  65. cmpq $0x3000, %rdi
  66. jbe spl
  67. xorq %rax, %rax
  68. movq %rax, %cr3
  69. ret
  70. .globl __stack_chk_fail
  71. __stack_chk_fail:
  72. hlt
  73. jmp __stack_chk_fail
  74. .data
  75. .align 16
  76. .globl gdt
  77. gdt: .quad 0x0000000000000000
  78. .quad 0x0020980000000000
  79. .quad 0x0000920000000000
  80. .globl gdt_descr
  81. gdt_descr:
  82. .word . - gdt
  83. .quad gdt
  84. .globl idt
  85. idt: .fill 512, 8, 0
  86. idt_descr:
  87. .word . - idt
  88. .quad idt
  89. .data