setup64.S 2.6 KB

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