setup64.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* arch/x86/boot/setup64.S
  2. *
  3. * This file is part of xnix.
  4. *
  5. * XNIX 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. * 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. * You should have received a copy of the GNU General Public License
  16. * along with XNIX. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. .code16
  19. /* switch to the long-mode, and to real system */
  20. .globl _start
  21. _start: xorw %ax, %ax
  22. movw %ax, %ds
  23. movw %ax, %es
  24. movw %ax, %ss
  25. /* yeah, we should move it again. */
  26. movw $0x7e00, %di
  27. movw $0x8000, %si
  28. movw $512, %cx
  29. cld
  30. rep
  31. movsb
  32. movw $0x7c00, %sp
  33. ljmp $0, $real_start
  34. real_start:
  35. /* open a20 */
  36. cli
  37. inb $0x92, %al
  38. orb $0x02, %al
  39. outb %al, $0x92
  40. /* setup gdt */
  41. lgdt gdt32_descr
  42. movl %cr0, %eax
  43. orl $1, %eax
  44. /* that's it! */
  45. movl %eax, %cr0
  46. /* setup paging (for long mode) */
  47. /* finally I used the thing that I used, and this is better, I think.
  48. * initial 2m pages at 0 */
  49. /* clear them before using */
  50. xorl %eax, %eax
  51. xorl %edi, %edi
  52. movl $4096, %ecx
  53. cld
  54. rep stosl
  55. movl $0x1007, 0
  56. /* 0xffff800000000000 */
  57. movl $0x1007, 0x800
  58. movl $0x2007, 0x1000
  59. movl $0x000083, 0x2000
  60. movl $0x200083, 0x2008
  61. movl $0x400083, 0x2010
  62. movl $0x600083, 0x2018
  63. movl $0x800083, 0x2020
  64. ljmp $8, $init_prot
  65. .code32
  66. init_prot:
  67. movl $0x10, %eax
  68. movl %eax, %ds
  69. movl %eax, %es
  70. movl %eax, %fs
  71. movl %eax, %gs
  72. movl %eax, %ss
  73. movl $0x7c00, %esp
  74. /* move it to 1mb */
  75. movl $1048576, %edi
  76. movl $0x8200, %esi
  77. movl $131072, %ecx
  78. cld
  79. rep
  80. movsl
  81. to_long:
  82. lgdt gdt64_descr
  83. movl $0x000000a0, %eax
  84. movl %eax, %cr4
  85. xorl %eax, %eax
  86. movl %eax, %cr3
  87. movl $0xc0000080, %ecx
  88. rdmsr
  89. orl $0x100, %eax
  90. wrmsr
  91. /* PG and PE */
  92. movl %cr0, %eax
  93. orl $0x80000001, %eax
  94. movl %eax, %cr0
  95. ljmp $0x08, $init_lm
  96. .code64
  97. init_lm:
  98. movq $0x10, %rax
  99. movq %rax, %ds
  100. movq %rax, %es
  101. movq %rax, %fs
  102. movq %rax, %gs
  103. movq %rax, %ss
  104. movq $0x7c00, %rsp
  105. pushq $1048576
  106. retq
  107. .globl gdt32
  108. gdt32: .long 0x00000000, 0x00000000
  109. .long 0x0000ffff, 0x00cf9a00
  110. .long 0x0000ffff, 0x00cf9200
  111. .globl gdt32_descr
  112. gdt32_descr:
  113. .word . - gdt32
  114. .long gdt32
  115. gdt64: .quad 0x0000000000000000
  116. .quad 0x0020980000000000
  117. .quad 0x0000920000000000
  118. gdt64_descr:
  119. .word . - gdt64
  120. .long gdt64